mtdconcat.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * MTD device concatenation layer
  4. *
  5. * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
  6. * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * NAND support by Christian Gan <cgan@iders.ca>
  9. *
  10. */
  11. #ifndef __UBOOT__
  12. #include <dm/devres.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched.h>
  17. #include <linux/types.h>
  18. #include <linux/backing-dev.h>
  19. #include <asm/div64.h>
  20. #else
  21. #include <div64.h>
  22. #include <linux/compat.h>
  23. #endif
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/concat.h>
  26. #include <ubi_uboot.h>
  27. /*
  28. * Our storage structure:
  29. * Subdev points to an array of pointers to struct mtd_info objects
  30. * which is allocated along with this structure
  31. *
  32. */
  33. struct mtd_concat {
  34. struct mtd_info mtd;
  35. int num_subdev;
  36. struct mtd_info **subdev;
  37. };
  38. /*
  39. * how to calculate the size required for the above structure,
  40. * including the pointer array subdev points to:
  41. */
  42. #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
  43. ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
  44. /*
  45. * Given a pointer to the MTD object in the mtd_concat structure,
  46. * we can retrieve the pointer to that structure with this macro.
  47. */
  48. #define CONCAT(x) ((struct mtd_concat *)(x))
  49. /*
  50. * MTD methods which look up the relevant subdevice, translate the
  51. * effective address and pass through to the subdevice.
  52. */
  53. static int
  54. concat_read(struct mtd_info *mtd, loff_t from, size_t len,
  55. size_t * retlen, u_char * buf)
  56. {
  57. struct mtd_concat *concat = CONCAT(mtd);
  58. int ret = 0, err;
  59. int i;
  60. #ifdef __UBOOT__
  61. *retlen = 0;
  62. #endif
  63. for (i = 0; i < concat->num_subdev; i++) {
  64. struct mtd_info *subdev = concat->subdev[i];
  65. size_t size, retsize;
  66. if (from >= subdev->size) {
  67. /* Not destined for this subdev */
  68. size = 0;
  69. from -= subdev->size;
  70. continue;
  71. }
  72. if (from + len > subdev->size)
  73. /* First part goes into this subdev */
  74. size = subdev->size - from;
  75. else
  76. /* Entire transaction goes into this subdev */
  77. size = len;
  78. err = mtd_read(subdev, from, size, &retsize, buf);
  79. /* Save information about bitflips! */
  80. if (unlikely(err)) {
  81. if (mtd_is_eccerr(err)) {
  82. mtd->ecc_stats.failed++;
  83. ret = err;
  84. } else if (mtd_is_bitflip(err)) {
  85. mtd->ecc_stats.corrected++;
  86. /* Do not overwrite -EBADMSG !! */
  87. if (!ret)
  88. ret = err;
  89. } else
  90. return err;
  91. }
  92. *retlen += retsize;
  93. len -= size;
  94. if (len == 0)
  95. return ret;
  96. buf += size;
  97. from = 0;
  98. }
  99. return -EINVAL;
  100. }
  101. static int
  102. concat_write(struct mtd_info *mtd, loff_t to, size_t len,
  103. size_t * retlen, const u_char * buf)
  104. {
  105. struct mtd_concat *concat = CONCAT(mtd);
  106. int err = -EINVAL;
  107. int i;
  108. #ifdef __UBOOT__
  109. *retlen = 0;
  110. #endif
  111. for (i = 0; i < concat->num_subdev; i++) {
  112. struct mtd_info *subdev = concat->subdev[i];
  113. size_t size, retsize;
  114. if (to >= subdev->size) {
  115. size = 0;
  116. to -= subdev->size;
  117. continue;
  118. }
  119. if (to + len > subdev->size)
  120. size = subdev->size - to;
  121. else
  122. size = len;
  123. err = mtd_write(subdev, to, size, &retsize, buf);
  124. if (err)
  125. break;
  126. *retlen += retsize;
  127. len -= size;
  128. if (len == 0)
  129. break;
  130. err = -EINVAL;
  131. buf += size;
  132. to = 0;
  133. }
  134. return err;
  135. }
  136. #ifndef __UBOOT__
  137. static int
  138. concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
  139. unsigned long count, loff_t to, size_t * retlen)
  140. {
  141. struct mtd_concat *concat = CONCAT(mtd);
  142. struct kvec *vecs_copy;
  143. unsigned long entry_low, entry_high;
  144. size_t total_len = 0;
  145. int i;
  146. int err = -EINVAL;
  147. /* Calculate total length of data */
  148. for (i = 0; i < count; i++)
  149. total_len += vecs[i].iov_len;
  150. /* Check alignment */
  151. if (mtd->writesize > 1) {
  152. uint64_t __to = to;
  153. if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
  154. return -EINVAL;
  155. }
  156. /* make a copy of vecs */
  157. vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
  158. if (!vecs_copy)
  159. return -ENOMEM;
  160. entry_low = 0;
  161. for (i = 0; i < concat->num_subdev; i++) {
  162. struct mtd_info *subdev = concat->subdev[i];
  163. size_t size, wsize, retsize, old_iov_len;
  164. if (to >= subdev->size) {
  165. to -= subdev->size;
  166. continue;
  167. }
  168. size = min_t(uint64_t, total_len, subdev->size - to);
  169. wsize = size; /* store for future use */
  170. entry_high = entry_low;
  171. while (entry_high < count) {
  172. if (size <= vecs_copy[entry_high].iov_len)
  173. break;
  174. size -= vecs_copy[entry_high++].iov_len;
  175. }
  176. old_iov_len = vecs_copy[entry_high].iov_len;
  177. vecs_copy[entry_high].iov_len = size;
  178. err = mtd_writev(subdev, &vecs_copy[entry_low],
  179. entry_high - entry_low + 1, to, &retsize);
  180. vecs_copy[entry_high].iov_len = old_iov_len - size;
  181. vecs_copy[entry_high].iov_base += size;
  182. entry_low = entry_high;
  183. if (err)
  184. break;
  185. *retlen += retsize;
  186. total_len -= wsize;
  187. if (total_len == 0)
  188. break;
  189. err = -EINVAL;
  190. to = 0;
  191. }
  192. kfree(vecs_copy);
  193. return err;
  194. }
  195. #endif
  196. static int
  197. concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  198. {
  199. struct mtd_concat *concat = CONCAT(mtd);
  200. struct mtd_oob_ops devops = *ops;
  201. int i, err, ret = 0;
  202. ops->retlen = ops->oobretlen = 0;
  203. for (i = 0; i < concat->num_subdev; i++) {
  204. struct mtd_info *subdev = concat->subdev[i];
  205. if (from >= subdev->size) {
  206. from -= subdev->size;
  207. continue;
  208. }
  209. /* partial read ? */
  210. if (from + devops.len > subdev->size)
  211. devops.len = subdev->size - from;
  212. err = mtd_read_oob(subdev, from, &devops);
  213. ops->retlen += devops.retlen;
  214. ops->oobretlen += devops.oobretlen;
  215. /* Save information about bitflips! */
  216. if (unlikely(err)) {
  217. if (mtd_is_eccerr(err)) {
  218. mtd->ecc_stats.failed++;
  219. ret = err;
  220. } else if (mtd_is_bitflip(err)) {
  221. mtd->ecc_stats.corrected++;
  222. /* Do not overwrite -EBADMSG !! */
  223. if (!ret)
  224. ret = err;
  225. } else
  226. return err;
  227. }
  228. if (devops.datbuf) {
  229. devops.len = ops->len - ops->retlen;
  230. if (!devops.len)
  231. return ret;
  232. devops.datbuf += devops.retlen;
  233. }
  234. if (devops.oobbuf) {
  235. devops.ooblen = ops->ooblen - ops->oobretlen;
  236. if (!devops.ooblen)
  237. return ret;
  238. devops.oobbuf += ops->oobretlen;
  239. }
  240. from = 0;
  241. }
  242. return -EINVAL;
  243. }
  244. static int
  245. concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
  246. {
  247. struct mtd_concat *concat = CONCAT(mtd);
  248. struct mtd_oob_ops devops = *ops;
  249. int i, err;
  250. if (!(mtd->flags & MTD_WRITEABLE))
  251. return -EROFS;
  252. ops->retlen = ops->oobretlen = 0;
  253. for (i = 0; i < concat->num_subdev; i++) {
  254. struct mtd_info *subdev = concat->subdev[i];
  255. if (to >= subdev->size) {
  256. to -= subdev->size;
  257. continue;
  258. }
  259. /* partial write ? */
  260. if (to + devops.len > subdev->size)
  261. devops.len = subdev->size - to;
  262. err = mtd_write_oob(subdev, to, &devops);
  263. ops->retlen += devops.oobretlen;
  264. if (err)
  265. return err;
  266. if (devops.datbuf) {
  267. devops.len = ops->len - ops->retlen;
  268. if (!devops.len)
  269. return 0;
  270. devops.datbuf += devops.retlen;
  271. }
  272. if (devops.oobbuf) {
  273. devops.ooblen = ops->ooblen - ops->oobretlen;
  274. if (!devops.ooblen)
  275. return 0;
  276. devops.oobbuf += devops.oobretlen;
  277. }
  278. to = 0;
  279. }
  280. return -EINVAL;
  281. }
  282. static void concat_erase_callback(struct erase_info *instr)
  283. {
  284. /* Nothing to do here in U-Boot */
  285. #ifndef __UBOOT__
  286. wake_up((wait_queue_head_t *) instr->priv);
  287. #endif
  288. }
  289. static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
  290. {
  291. int err;
  292. wait_queue_head_t waitq;
  293. DECLARE_WAITQUEUE(wait, current);
  294. /*
  295. * This code was stol^H^H^H^Hinspired by mtdchar.c
  296. */
  297. init_waitqueue_head(&waitq);
  298. erase->mtd = mtd;
  299. erase->callback = concat_erase_callback;
  300. erase->priv = (unsigned long) &waitq;
  301. /*
  302. * FIXME: Allow INTERRUPTIBLE. Which means
  303. * not having the wait_queue head on the stack.
  304. */
  305. err = mtd_erase(mtd, erase);
  306. if (!err) {
  307. set_current_state(TASK_UNINTERRUPTIBLE);
  308. add_wait_queue(&waitq, &wait);
  309. if (erase->state != MTD_ERASE_DONE
  310. && erase->state != MTD_ERASE_FAILED)
  311. schedule();
  312. remove_wait_queue(&waitq, &wait);
  313. set_current_state(TASK_RUNNING);
  314. err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
  315. }
  316. return err;
  317. }
  318. static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
  319. {
  320. struct mtd_concat *concat = CONCAT(mtd);
  321. struct mtd_info *subdev;
  322. int i, err;
  323. uint64_t length, offset = 0;
  324. struct erase_info *erase;
  325. /*
  326. * Check for proper erase block alignment of the to-be-erased area.
  327. * It is easier to do this based on the super device's erase
  328. * region info rather than looking at each particular sub-device
  329. * in turn.
  330. */
  331. if (!concat->mtd.numeraseregions) {
  332. /* the easy case: device has uniform erase block size */
  333. if (instr->addr & (concat->mtd.erasesize - 1))
  334. return -EINVAL;
  335. if (instr->len & (concat->mtd.erasesize - 1))
  336. return -EINVAL;
  337. } else {
  338. /* device has variable erase size */
  339. struct mtd_erase_region_info *erase_regions =
  340. concat->mtd.eraseregions;
  341. /*
  342. * Find the erase region where the to-be-erased area begins:
  343. */
  344. for (i = 0; i < concat->mtd.numeraseregions &&
  345. instr->addr >= erase_regions[i].offset; i++) ;
  346. --i;
  347. /*
  348. * Now erase_regions[i] is the region in which the
  349. * to-be-erased area begins. Verify that the starting
  350. * offset is aligned to this region's erase size:
  351. */
  352. if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
  353. return -EINVAL;
  354. /*
  355. * now find the erase region where the to-be-erased area ends:
  356. */
  357. for (; i < concat->mtd.numeraseregions &&
  358. (instr->addr + instr->len) >= erase_regions[i].offset;
  359. ++i) ;
  360. --i;
  361. /*
  362. * check if the ending offset is aligned to this region's erase size
  363. */
  364. if (i < 0 || ((instr->addr + instr->len) &
  365. (erase_regions[i].erasesize - 1)))
  366. return -EINVAL;
  367. }
  368. /* make a local copy of instr to avoid modifying the caller's struct */
  369. erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
  370. if (!erase)
  371. return -ENOMEM;
  372. *erase = *instr;
  373. length = instr->len;
  374. /*
  375. * find the subdevice where the to-be-erased area begins, adjust
  376. * starting offset to be relative to the subdevice start
  377. */
  378. for (i = 0; i < concat->num_subdev; i++) {
  379. subdev = concat->subdev[i];
  380. if (subdev->size <= erase->addr) {
  381. erase->addr -= subdev->size;
  382. offset += subdev->size;
  383. } else {
  384. break;
  385. }
  386. }
  387. /* must never happen since size limit has been verified above */
  388. BUG_ON(i >= concat->num_subdev);
  389. /* now do the erase: */
  390. err = 0;
  391. for (; length > 0; i++) {
  392. /* loop for all subdevices affected by this request */
  393. subdev = concat->subdev[i]; /* get current subdevice */
  394. /* limit length to subdevice's size: */
  395. if (erase->addr + length > subdev->size)
  396. erase->len = subdev->size - erase->addr;
  397. else
  398. erase->len = length;
  399. length -= erase->len;
  400. if ((err = concat_dev_erase(subdev, erase))) {
  401. /* sanity check: should never happen since
  402. * block alignment has been checked above */
  403. BUG_ON(err == -EINVAL);
  404. if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  405. instr->fail_addr = erase->fail_addr + offset;
  406. break;
  407. }
  408. /*
  409. * erase->addr specifies the offset of the area to be
  410. * erased *within the current subdevice*. It can be
  411. * non-zero only the first time through this loop, i.e.
  412. * for the first subdevice where blocks need to be erased.
  413. * All the following erases must begin at the start of the
  414. * current subdevice, i.e. at offset zero.
  415. */
  416. erase->addr = 0;
  417. offset += subdev->size;
  418. }
  419. instr->state = erase->state;
  420. kfree(erase);
  421. if (err)
  422. return err;
  423. if (instr->callback)
  424. instr->callback(instr);
  425. return 0;
  426. }
  427. static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  428. {
  429. struct mtd_concat *concat = CONCAT(mtd);
  430. int i, err = -EINVAL;
  431. for (i = 0; i < concat->num_subdev; i++) {
  432. struct mtd_info *subdev = concat->subdev[i];
  433. uint64_t size;
  434. if (ofs >= subdev->size) {
  435. size = 0;
  436. ofs -= subdev->size;
  437. continue;
  438. }
  439. if (ofs + len > subdev->size)
  440. size = subdev->size - ofs;
  441. else
  442. size = len;
  443. err = mtd_lock(subdev, ofs, size);
  444. if (err)
  445. break;
  446. len -= size;
  447. if (len == 0)
  448. break;
  449. err = -EINVAL;
  450. ofs = 0;
  451. }
  452. return err;
  453. }
  454. static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  455. {
  456. struct mtd_concat *concat = CONCAT(mtd);
  457. int i, err = 0;
  458. for (i = 0; i < concat->num_subdev; i++) {
  459. struct mtd_info *subdev = concat->subdev[i];
  460. uint64_t size;
  461. if (ofs >= subdev->size) {
  462. size = 0;
  463. ofs -= subdev->size;
  464. continue;
  465. }
  466. if (ofs + len > subdev->size)
  467. size = subdev->size - ofs;
  468. else
  469. size = len;
  470. err = mtd_unlock(subdev, ofs, size);
  471. if (err)
  472. break;
  473. len -= size;
  474. if (len == 0)
  475. break;
  476. err = -EINVAL;
  477. ofs = 0;
  478. }
  479. return err;
  480. }
  481. static void concat_sync(struct mtd_info *mtd)
  482. {
  483. struct mtd_concat *concat = CONCAT(mtd);
  484. int i;
  485. for (i = 0; i < concat->num_subdev; i++) {
  486. struct mtd_info *subdev = concat->subdev[i];
  487. mtd_sync(subdev);
  488. }
  489. }
  490. #ifndef __UBOOT__
  491. static int concat_suspend(struct mtd_info *mtd)
  492. {
  493. struct mtd_concat *concat = CONCAT(mtd);
  494. int i, rc = 0;
  495. for (i = 0; i < concat->num_subdev; i++) {
  496. struct mtd_info *subdev = concat->subdev[i];
  497. if ((rc = mtd_suspend(subdev)) < 0)
  498. return rc;
  499. }
  500. return rc;
  501. }
  502. static void concat_resume(struct mtd_info *mtd)
  503. {
  504. struct mtd_concat *concat = CONCAT(mtd);
  505. int i;
  506. for (i = 0; i < concat->num_subdev; i++) {
  507. struct mtd_info *subdev = concat->subdev[i];
  508. mtd_resume(subdev);
  509. }
  510. }
  511. #endif
  512. static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
  513. {
  514. struct mtd_concat *concat = CONCAT(mtd);
  515. int i, res = 0;
  516. if (!mtd_can_have_bb(concat->subdev[0]))
  517. return res;
  518. for (i = 0; i < concat->num_subdev; i++) {
  519. struct mtd_info *subdev = concat->subdev[i];
  520. if (ofs >= subdev->size) {
  521. ofs -= subdev->size;
  522. continue;
  523. }
  524. res = mtd_block_isbad(subdev, ofs);
  525. break;
  526. }
  527. return res;
  528. }
  529. static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
  530. {
  531. struct mtd_concat *concat = CONCAT(mtd);
  532. int i, err = -EINVAL;
  533. for (i = 0; i < concat->num_subdev; i++) {
  534. struct mtd_info *subdev = concat->subdev[i];
  535. if (ofs >= subdev->size) {
  536. ofs -= subdev->size;
  537. continue;
  538. }
  539. err = mtd_block_markbad(subdev, ofs);
  540. if (!err)
  541. mtd->ecc_stats.badblocks++;
  542. break;
  543. }
  544. return err;
  545. }
  546. /*
  547. * try to support NOMMU mmaps on concatenated devices
  548. * - we don't support subdev spanning as we can't guarantee it'll work
  549. */
  550. static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
  551. unsigned long len,
  552. unsigned long offset,
  553. unsigned long flags)
  554. {
  555. struct mtd_concat *concat = CONCAT(mtd);
  556. int i;
  557. for (i = 0; i < concat->num_subdev; i++) {
  558. struct mtd_info *subdev = concat->subdev[i];
  559. if (offset >= subdev->size) {
  560. offset -= subdev->size;
  561. continue;
  562. }
  563. return mtd_get_unmapped_area(subdev, len, offset, flags);
  564. }
  565. return (unsigned long) -ENOSYS;
  566. }
  567. /*
  568. * This function constructs a virtual MTD device by concatenating
  569. * num_devs MTD devices. A pointer to the new device object is
  570. * stored to *new_dev upon success. This function does _not_
  571. * register any devices: this is the caller's responsibility.
  572. */
  573. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  574. int num_devs, /* number of subdevices */
  575. #ifndef __UBOOT__
  576. const char *name)
  577. #else
  578. char *name)
  579. #endif
  580. { /* name for the new device */
  581. int i;
  582. size_t size;
  583. struct mtd_concat *concat;
  584. uint32_t max_erasesize, curr_erasesize;
  585. int num_erase_region;
  586. int max_writebufsize = 0;
  587. debug("Concatenating MTD devices:\n");
  588. for (i = 0; i < num_devs; i++)
  589. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  590. debug("into device \"%s\"\n", name);
  591. /* allocate the device structure */
  592. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  593. concat = kzalloc(size, GFP_KERNEL);
  594. if (!concat) {
  595. printk
  596. ("memory allocation error while creating concatenated device \"%s\"\n",
  597. name);
  598. return NULL;
  599. }
  600. concat->subdev = (struct mtd_info **) (concat + 1);
  601. /*
  602. * Set up the new "super" device's MTD object structure, check for
  603. * incompatibilities between the subdevices.
  604. */
  605. concat->mtd.type = subdev[0]->type;
  606. concat->mtd.flags = subdev[0]->flags;
  607. concat->mtd.size = subdev[0]->size;
  608. concat->mtd.erasesize = subdev[0]->erasesize;
  609. concat->mtd.writesize = subdev[0]->writesize;
  610. for (i = 0; i < num_devs; i++)
  611. if (max_writebufsize < subdev[i]->writebufsize)
  612. max_writebufsize = subdev[i]->writebufsize;
  613. concat->mtd.writebufsize = max_writebufsize;
  614. concat->mtd.subpage_sft = subdev[0]->subpage_sft;
  615. concat->mtd.oobsize = subdev[0]->oobsize;
  616. concat->mtd.oobavail = subdev[0]->oobavail;
  617. #ifndef __UBOOT__
  618. if (subdev[0]->_writev)
  619. concat->mtd._writev = concat_writev;
  620. #endif
  621. if (subdev[0]->_read_oob)
  622. concat->mtd._read_oob = concat_read_oob;
  623. if (subdev[0]->_write_oob)
  624. concat->mtd._write_oob = concat_write_oob;
  625. if (subdev[0]->_block_isbad)
  626. concat->mtd._block_isbad = concat_block_isbad;
  627. if (subdev[0]->_block_markbad)
  628. concat->mtd._block_markbad = concat_block_markbad;
  629. concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
  630. #ifndef __UBOOT__
  631. concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
  632. #endif
  633. concat->subdev[0] = subdev[0];
  634. for (i = 1; i < num_devs; i++) {
  635. if (concat->mtd.type != subdev[i]->type) {
  636. kfree(concat);
  637. printk("Incompatible device type on \"%s\"\n",
  638. subdev[i]->name);
  639. return NULL;
  640. }
  641. if (concat->mtd.flags != subdev[i]->flags) {
  642. /*
  643. * Expect all flags except MTD_WRITEABLE to be
  644. * equal on all subdevices.
  645. */
  646. if ((concat->mtd.flags ^ subdev[i]->
  647. flags) & ~MTD_WRITEABLE) {
  648. kfree(concat);
  649. printk("Incompatible device flags on \"%s\"\n",
  650. subdev[i]->name);
  651. return NULL;
  652. } else
  653. /* if writeable attribute differs,
  654. make super device writeable */
  655. concat->mtd.flags |=
  656. subdev[i]->flags & MTD_WRITEABLE;
  657. }
  658. #ifndef __UBOOT__
  659. /* only permit direct mapping if the BDIs are all the same
  660. * - copy-mapping is still permitted
  661. */
  662. if (concat->mtd.backing_dev_info !=
  663. subdev[i]->backing_dev_info)
  664. concat->mtd.backing_dev_info =
  665. &default_backing_dev_info;
  666. #endif
  667. concat->mtd.size += subdev[i]->size;
  668. concat->mtd.ecc_stats.badblocks +=
  669. subdev[i]->ecc_stats.badblocks;
  670. if (concat->mtd.writesize != subdev[i]->writesize ||
  671. concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
  672. concat->mtd.oobsize != subdev[i]->oobsize ||
  673. !concat->mtd._read_oob != !subdev[i]->_read_oob ||
  674. !concat->mtd._write_oob != !subdev[i]->_write_oob) {
  675. kfree(concat);
  676. printk("Incompatible OOB or ECC data on \"%s\"\n",
  677. subdev[i]->name);
  678. return NULL;
  679. }
  680. concat->subdev[i] = subdev[i];
  681. }
  682. concat->mtd.ecclayout = subdev[0]->ecclayout;
  683. concat->num_subdev = num_devs;
  684. concat->mtd.name = name;
  685. concat->mtd._erase = concat_erase;
  686. concat->mtd._read = concat_read;
  687. concat->mtd._write = concat_write;
  688. concat->mtd._sync = concat_sync;
  689. concat->mtd._lock = concat_lock;
  690. concat->mtd._unlock = concat_unlock;
  691. #ifndef __UBOOT__
  692. concat->mtd._suspend = concat_suspend;
  693. concat->mtd._resume = concat_resume;
  694. #endif
  695. concat->mtd._get_unmapped_area = concat_get_unmapped_area;
  696. /*
  697. * Combine the erase block size info of the subdevices:
  698. *
  699. * first, walk the map of the new device and see how
  700. * many changes in erase size we have
  701. */
  702. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  703. num_erase_region = 1;
  704. for (i = 0; i < num_devs; i++) {
  705. if (subdev[i]->numeraseregions == 0) {
  706. /* current subdevice has uniform erase size */
  707. if (subdev[i]->erasesize != curr_erasesize) {
  708. /* if it differs from the last subdevice's erase size, count it */
  709. ++num_erase_region;
  710. curr_erasesize = subdev[i]->erasesize;
  711. if (curr_erasesize > max_erasesize)
  712. max_erasesize = curr_erasesize;
  713. }
  714. } else {
  715. /* current subdevice has variable erase size */
  716. int j;
  717. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  718. /* walk the list of erase regions, count any changes */
  719. if (subdev[i]->eraseregions[j].erasesize !=
  720. curr_erasesize) {
  721. ++num_erase_region;
  722. curr_erasesize =
  723. subdev[i]->eraseregions[j].
  724. erasesize;
  725. if (curr_erasesize > max_erasesize)
  726. max_erasesize = curr_erasesize;
  727. }
  728. }
  729. }
  730. }
  731. if (num_erase_region == 1) {
  732. /*
  733. * All subdevices have the same uniform erase size.
  734. * This is easy:
  735. */
  736. concat->mtd.erasesize = curr_erasesize;
  737. concat->mtd.numeraseregions = 0;
  738. } else {
  739. uint64_t tmp64;
  740. /*
  741. * erase block size varies across the subdevices: allocate
  742. * space to store the data describing the variable erase regions
  743. */
  744. struct mtd_erase_region_info *erase_region_p;
  745. uint64_t begin, position;
  746. concat->mtd.erasesize = max_erasesize;
  747. concat->mtd.numeraseregions = num_erase_region;
  748. concat->mtd.eraseregions = erase_region_p =
  749. kmalloc(num_erase_region *
  750. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  751. if (!erase_region_p) {
  752. kfree(concat);
  753. printk
  754. ("memory allocation error while creating erase region list"
  755. " for device \"%s\"\n", name);
  756. return NULL;
  757. }
  758. /*
  759. * walk the map of the new device once more and fill in
  760. * in erase region info:
  761. */
  762. curr_erasesize = subdev[0]->erasesize;
  763. begin = position = 0;
  764. for (i = 0; i < num_devs; i++) {
  765. if (subdev[i]->numeraseregions == 0) {
  766. /* current subdevice has uniform erase size */
  767. if (subdev[i]->erasesize != curr_erasesize) {
  768. /*
  769. * fill in an mtd_erase_region_info structure for the area
  770. * we have walked so far:
  771. */
  772. erase_region_p->offset = begin;
  773. erase_region_p->erasesize =
  774. curr_erasesize;
  775. tmp64 = position - begin;
  776. do_div(tmp64, curr_erasesize);
  777. erase_region_p->numblocks = tmp64;
  778. begin = position;
  779. curr_erasesize = subdev[i]->erasesize;
  780. ++erase_region_p;
  781. }
  782. position += subdev[i]->size;
  783. } else {
  784. /* current subdevice has variable erase size */
  785. int j;
  786. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  787. /* walk the list of erase regions, count any changes */
  788. if (subdev[i]->eraseregions[j].
  789. erasesize != curr_erasesize) {
  790. erase_region_p->offset = begin;
  791. erase_region_p->erasesize =
  792. curr_erasesize;
  793. tmp64 = position - begin;
  794. do_div(tmp64, curr_erasesize);
  795. erase_region_p->numblocks = tmp64;
  796. begin = position;
  797. curr_erasesize =
  798. subdev[i]->eraseregions[j].
  799. erasesize;
  800. ++erase_region_p;
  801. }
  802. position +=
  803. subdev[i]->eraseregions[j].
  804. numblocks * (uint64_t)curr_erasesize;
  805. }
  806. }
  807. }
  808. /* Now write the final entry */
  809. erase_region_p->offset = begin;
  810. erase_region_p->erasesize = curr_erasesize;
  811. tmp64 = position - begin;
  812. do_div(tmp64, curr_erasesize);
  813. erase_region_p->numblocks = tmp64;
  814. }
  815. return &concat->mtd;
  816. }
  817. /*
  818. * This function destroys an MTD object obtained from concat_mtd_devs()
  819. */
  820. void mtd_concat_destroy(struct mtd_info *mtd)
  821. {
  822. struct mtd_concat *concat = CONCAT(mtd);
  823. if (concat->mtd.numeraseregions)
  824. kfree(concat->mtd.eraseregions);
  825. kfree(concat);
  826. }
  827. EXPORT_SYMBOL(mtd_concat_create);
  828. EXPORT_SYMBOL(mtd_concat_destroy);
  829. MODULE_LICENSE("GPL");
  830. MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  831. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");