mtdconcat.c 23 KB

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