mtdconcat.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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 void concat_erase_callback(struct erase_info *instr)
  285. {
  286. /* Nothing to do here in U-Boot */
  287. #ifndef __UBOOT__
  288. wake_up((wait_queue_head_t *) instr->priv);
  289. #endif
  290. }
  291. static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
  292. {
  293. int err;
  294. wait_queue_head_t waitq;
  295. DECLARE_WAITQUEUE(wait, current);
  296. /*
  297. * This code was stol^H^H^H^Hinspired by mtdchar.c
  298. */
  299. init_waitqueue_head(&waitq);
  300. erase->mtd = mtd;
  301. erase->callback = concat_erase_callback;
  302. erase->priv = (unsigned long) &waitq;
  303. /*
  304. * FIXME: Allow INTERRUPTIBLE. Which means
  305. * not having the wait_queue head on the stack.
  306. */
  307. err = mtd_erase(mtd, erase);
  308. if (!err) {
  309. set_current_state(TASK_UNINTERRUPTIBLE);
  310. add_wait_queue(&waitq, &wait);
  311. if (erase->state != MTD_ERASE_DONE
  312. && erase->state != MTD_ERASE_FAILED)
  313. schedule();
  314. remove_wait_queue(&waitq, &wait);
  315. set_current_state(TASK_RUNNING);
  316. err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
  317. }
  318. return err;
  319. }
  320. static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
  321. {
  322. struct mtd_concat *concat = CONCAT(mtd);
  323. struct mtd_info *subdev;
  324. int i, err;
  325. uint64_t length, offset = 0;
  326. struct erase_info *erase;
  327. /*
  328. * Check for proper erase block alignment of the to-be-erased area.
  329. * It is easier to do this based on the super device's erase
  330. * region info rather than looking at each particular sub-device
  331. * in turn.
  332. */
  333. if (!concat->mtd.numeraseregions) {
  334. /* the easy case: device has uniform erase block size */
  335. if (instr->addr & (concat->mtd.erasesize - 1))
  336. return -EINVAL;
  337. if (instr->len & (concat->mtd.erasesize - 1))
  338. return -EINVAL;
  339. } else {
  340. /* device has variable erase size */
  341. struct mtd_erase_region_info *erase_regions =
  342. concat->mtd.eraseregions;
  343. /*
  344. * Find the erase region where the to-be-erased area begins:
  345. */
  346. for (i = 0; i < concat->mtd.numeraseregions &&
  347. instr->addr >= erase_regions[i].offset; i++) ;
  348. --i;
  349. /*
  350. * Now erase_regions[i] is the region in which the
  351. * to-be-erased area begins. Verify that the starting
  352. * offset is aligned to this region's erase size:
  353. */
  354. if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
  355. return -EINVAL;
  356. /*
  357. * now find the erase region where the to-be-erased area ends:
  358. */
  359. for (; i < concat->mtd.numeraseregions &&
  360. (instr->addr + instr->len) >= erase_regions[i].offset;
  361. ++i) ;
  362. --i;
  363. /*
  364. * check if the ending offset is aligned to this region's erase size
  365. */
  366. if (i < 0 || ((instr->addr + instr->len) &
  367. (erase_regions[i].erasesize - 1)))
  368. return -EINVAL;
  369. }
  370. /* make a local copy of instr to avoid modifying the caller's struct */
  371. erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
  372. if (!erase)
  373. return -ENOMEM;
  374. *erase = *instr;
  375. length = instr->len;
  376. /*
  377. * find the subdevice where the to-be-erased area begins, adjust
  378. * starting offset to be relative to the subdevice start
  379. */
  380. for (i = 0; i < concat->num_subdev; i++) {
  381. subdev = concat->subdev[i];
  382. if (subdev->size <= erase->addr) {
  383. erase->addr -= subdev->size;
  384. offset += subdev->size;
  385. } else {
  386. break;
  387. }
  388. }
  389. /* must never happen since size limit has been verified above */
  390. BUG_ON(i >= concat->num_subdev);
  391. /* now do the erase: */
  392. err = 0;
  393. for (; length > 0; i++) {
  394. /* loop for all subdevices affected by this request */
  395. subdev = concat->subdev[i]; /* get current subdevice */
  396. /* limit length to subdevice's size: */
  397. if (erase->addr + length > subdev->size)
  398. erase->len = subdev->size - erase->addr;
  399. else
  400. erase->len = length;
  401. length -= erase->len;
  402. if ((err = concat_dev_erase(subdev, erase))) {
  403. /* sanity check: should never happen since
  404. * block alignment has been checked above */
  405. BUG_ON(err == -EINVAL);
  406. if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  407. instr->fail_addr = erase->fail_addr + offset;
  408. break;
  409. }
  410. /*
  411. * erase->addr specifies the offset of the area to be
  412. * erased *within the current subdevice*. It can be
  413. * non-zero only the first time through this loop, i.e.
  414. * for the first subdevice where blocks need to be erased.
  415. * All the following erases must begin at the start of the
  416. * current subdevice, i.e. at offset zero.
  417. */
  418. erase->addr = 0;
  419. offset += subdev->size;
  420. }
  421. instr->state = erase->state;
  422. kfree(erase);
  423. if (err)
  424. return err;
  425. if (instr->callback)
  426. instr->callback(instr);
  427. return 0;
  428. }
  429. static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  430. {
  431. struct mtd_concat *concat = CONCAT(mtd);
  432. int i, err = -EINVAL;
  433. for (i = 0; i < concat->num_subdev; i++) {
  434. struct mtd_info *subdev = concat->subdev[i];
  435. uint64_t size;
  436. if (ofs >= subdev->size) {
  437. size = 0;
  438. ofs -= subdev->size;
  439. continue;
  440. }
  441. if (ofs + len > subdev->size)
  442. size = subdev->size - ofs;
  443. else
  444. size = len;
  445. err = mtd_lock(subdev, ofs, size);
  446. if (err)
  447. break;
  448. len -= size;
  449. if (len == 0)
  450. break;
  451. err = -EINVAL;
  452. ofs = 0;
  453. }
  454. return err;
  455. }
  456. static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  457. {
  458. struct mtd_concat *concat = CONCAT(mtd);
  459. int i, err = 0;
  460. for (i = 0; i < concat->num_subdev; i++) {
  461. struct mtd_info *subdev = concat->subdev[i];
  462. uint64_t size;
  463. if (ofs >= subdev->size) {
  464. size = 0;
  465. ofs -= subdev->size;
  466. continue;
  467. }
  468. if (ofs + len > subdev->size)
  469. size = subdev->size - ofs;
  470. else
  471. size = len;
  472. err = mtd_unlock(subdev, ofs, size);
  473. if (err)
  474. break;
  475. len -= size;
  476. if (len == 0)
  477. break;
  478. err = -EINVAL;
  479. ofs = 0;
  480. }
  481. return err;
  482. }
  483. static void concat_sync(struct mtd_info *mtd)
  484. {
  485. struct mtd_concat *concat = CONCAT(mtd);
  486. int i;
  487. for (i = 0; i < concat->num_subdev; i++) {
  488. struct mtd_info *subdev = concat->subdev[i];
  489. mtd_sync(subdev);
  490. }
  491. }
  492. #ifndef __UBOOT__
  493. static int concat_suspend(struct mtd_info *mtd)
  494. {
  495. struct mtd_concat *concat = CONCAT(mtd);
  496. int i, rc = 0;
  497. for (i = 0; i < concat->num_subdev; i++) {
  498. struct mtd_info *subdev = concat->subdev[i];
  499. if ((rc = mtd_suspend(subdev)) < 0)
  500. return rc;
  501. }
  502. return rc;
  503. }
  504. static void concat_resume(struct mtd_info *mtd)
  505. {
  506. struct mtd_concat *concat = CONCAT(mtd);
  507. int i;
  508. for (i = 0; i < concat->num_subdev; i++) {
  509. struct mtd_info *subdev = concat->subdev[i];
  510. mtd_resume(subdev);
  511. }
  512. }
  513. #endif
  514. static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
  515. {
  516. struct mtd_concat *concat = CONCAT(mtd);
  517. int i, res = 0;
  518. if (!mtd_can_have_bb(concat->subdev[0]))
  519. return res;
  520. for (i = 0; i < concat->num_subdev; i++) {
  521. struct mtd_info *subdev = concat->subdev[i];
  522. if (ofs >= subdev->size) {
  523. ofs -= subdev->size;
  524. continue;
  525. }
  526. res = mtd_block_isbad(subdev, ofs);
  527. break;
  528. }
  529. return res;
  530. }
  531. static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
  532. {
  533. struct mtd_concat *concat = CONCAT(mtd);
  534. int i, err = -EINVAL;
  535. for (i = 0; i < concat->num_subdev; i++) {
  536. struct mtd_info *subdev = concat->subdev[i];
  537. if (ofs >= subdev->size) {
  538. ofs -= subdev->size;
  539. continue;
  540. }
  541. err = mtd_block_markbad(subdev, ofs);
  542. if (!err)
  543. mtd->ecc_stats.badblocks++;
  544. break;
  545. }
  546. return err;
  547. }
  548. /*
  549. * try to support NOMMU mmaps on concatenated devices
  550. * - we don't support subdev spanning as we can't guarantee it'll work
  551. */
  552. static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
  553. unsigned long len,
  554. unsigned long offset,
  555. unsigned long flags)
  556. {
  557. struct mtd_concat *concat = CONCAT(mtd);
  558. int i;
  559. for (i = 0; i < concat->num_subdev; i++) {
  560. struct mtd_info *subdev = concat->subdev[i];
  561. if (offset >= subdev->size) {
  562. offset -= subdev->size;
  563. continue;
  564. }
  565. return mtd_get_unmapped_area(subdev, len, offset, flags);
  566. }
  567. return (unsigned long) -ENOSYS;
  568. }
  569. /*
  570. * This function constructs a virtual MTD device by concatenating
  571. * num_devs MTD devices. A pointer to the new device object is
  572. * stored to *new_dev upon success. This function does _not_
  573. * register any devices: this is the caller's responsibility.
  574. */
  575. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  576. int num_devs, /* number of subdevices */
  577. #ifndef __UBOOT__
  578. const char *name)
  579. #else
  580. char *name)
  581. #endif
  582. { /* name for the new device */
  583. int i;
  584. size_t size;
  585. struct mtd_concat *concat;
  586. uint32_t max_erasesize, curr_erasesize;
  587. int num_erase_region;
  588. int max_writebufsize = 0;
  589. debug("Concatenating MTD devices:\n");
  590. for (i = 0; i < num_devs; i++)
  591. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  592. debug("into device \"%s\"\n", name);
  593. /* allocate the device structure */
  594. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  595. concat = kzalloc(size, GFP_KERNEL);
  596. if (!concat) {
  597. printk
  598. ("memory allocation error while creating concatenated device \"%s\"\n",
  599. name);
  600. return NULL;
  601. }
  602. concat->subdev = (struct mtd_info **) (concat + 1);
  603. /*
  604. * Set up the new "super" device's MTD object structure, check for
  605. * incompatibilities between the subdevices.
  606. */
  607. concat->mtd.type = subdev[0]->type;
  608. concat->mtd.flags = subdev[0]->flags;
  609. concat->mtd.size = subdev[0]->size;
  610. concat->mtd.erasesize = subdev[0]->erasesize;
  611. concat->mtd.writesize = subdev[0]->writesize;
  612. for (i = 0; i < num_devs; i++)
  613. if (max_writebufsize < subdev[i]->writebufsize)
  614. max_writebufsize = subdev[i]->writebufsize;
  615. concat->mtd.writebufsize = max_writebufsize;
  616. concat->mtd.subpage_sft = subdev[0]->subpage_sft;
  617. concat->mtd.oobsize = subdev[0]->oobsize;
  618. concat->mtd.oobavail = subdev[0]->oobavail;
  619. #ifndef __UBOOT__
  620. if (subdev[0]->_writev)
  621. concat->mtd._writev = concat_writev;
  622. #endif
  623. if (subdev[0]->_read_oob)
  624. concat->mtd._read_oob = concat_read_oob;
  625. if (subdev[0]->_write_oob)
  626. concat->mtd._write_oob = concat_write_oob;
  627. if (subdev[0]->_block_isbad)
  628. concat->mtd._block_isbad = concat_block_isbad;
  629. if (subdev[0]->_block_markbad)
  630. concat->mtd._block_markbad = concat_block_markbad;
  631. concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
  632. #ifndef __UBOOT__
  633. concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
  634. #endif
  635. concat->subdev[0] = subdev[0];
  636. for (i = 1; i < num_devs; i++) {
  637. if (concat->mtd.type != subdev[i]->type) {
  638. kfree(concat);
  639. printk("Incompatible device type on \"%s\"\n",
  640. subdev[i]->name);
  641. return NULL;
  642. }
  643. if (concat->mtd.flags != subdev[i]->flags) {
  644. /*
  645. * Expect all flags except MTD_WRITEABLE to be
  646. * equal on all subdevices.
  647. */
  648. if ((concat->mtd.flags ^ subdev[i]->
  649. flags) & ~MTD_WRITEABLE) {
  650. kfree(concat);
  651. printk("Incompatible device flags on \"%s\"\n",
  652. subdev[i]->name);
  653. return NULL;
  654. } else
  655. /* if writeable attribute differs,
  656. make super device writeable */
  657. concat->mtd.flags |=
  658. subdev[i]->flags & MTD_WRITEABLE;
  659. }
  660. #ifndef __UBOOT__
  661. /* only permit direct mapping if the BDIs are all the same
  662. * - copy-mapping is still permitted
  663. */
  664. if (concat->mtd.backing_dev_info !=
  665. subdev[i]->backing_dev_info)
  666. concat->mtd.backing_dev_info =
  667. &default_backing_dev_info;
  668. #endif
  669. concat->mtd.size += subdev[i]->size;
  670. concat->mtd.ecc_stats.badblocks +=
  671. subdev[i]->ecc_stats.badblocks;
  672. if (concat->mtd.writesize != subdev[i]->writesize ||
  673. concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
  674. concat->mtd.oobsize != subdev[i]->oobsize ||
  675. !concat->mtd._read_oob != !subdev[i]->_read_oob ||
  676. !concat->mtd._write_oob != !subdev[i]->_write_oob) {
  677. kfree(concat);
  678. printk("Incompatible OOB or ECC data on \"%s\"\n",
  679. subdev[i]->name);
  680. return NULL;
  681. }
  682. concat->subdev[i] = subdev[i];
  683. }
  684. concat->mtd.ecclayout = subdev[0]->ecclayout;
  685. concat->num_subdev = num_devs;
  686. concat->mtd.name = name;
  687. concat->mtd._erase = concat_erase;
  688. concat->mtd._read = concat_read;
  689. concat->mtd._write = concat_write;
  690. concat->mtd._sync = concat_sync;
  691. concat->mtd._lock = concat_lock;
  692. concat->mtd._unlock = concat_unlock;
  693. #ifndef __UBOOT__
  694. concat->mtd._suspend = concat_suspend;
  695. concat->mtd._resume = concat_resume;
  696. #endif
  697. concat->mtd._get_unmapped_area = concat_get_unmapped_area;
  698. /*
  699. * Combine the erase block size info of the subdevices:
  700. *
  701. * first, walk the map of the new device and see how
  702. * many changes in erase size we have
  703. */
  704. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  705. num_erase_region = 1;
  706. for (i = 0; i < num_devs; i++) {
  707. if (subdev[i]->numeraseregions == 0) {
  708. /* current subdevice has uniform erase size */
  709. if (subdev[i]->erasesize != curr_erasesize) {
  710. /* if it differs from the last subdevice's erase size, count it */
  711. ++num_erase_region;
  712. curr_erasesize = subdev[i]->erasesize;
  713. if (curr_erasesize > max_erasesize)
  714. max_erasesize = curr_erasesize;
  715. }
  716. } else {
  717. /* current subdevice has variable erase size */
  718. int j;
  719. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  720. /* walk the list of erase regions, count any changes */
  721. if (subdev[i]->eraseregions[j].erasesize !=
  722. curr_erasesize) {
  723. ++num_erase_region;
  724. curr_erasesize =
  725. subdev[i]->eraseregions[j].
  726. erasesize;
  727. if (curr_erasesize > max_erasesize)
  728. max_erasesize = curr_erasesize;
  729. }
  730. }
  731. }
  732. }
  733. if (num_erase_region == 1) {
  734. /*
  735. * All subdevices have the same uniform erase size.
  736. * This is easy:
  737. */
  738. concat->mtd.erasesize = curr_erasesize;
  739. concat->mtd.numeraseregions = 0;
  740. } else {
  741. uint64_t tmp64;
  742. /*
  743. * erase block size varies across the subdevices: allocate
  744. * space to store the data describing the variable erase regions
  745. */
  746. struct mtd_erase_region_info *erase_region_p;
  747. uint64_t begin, position;
  748. concat->mtd.erasesize = max_erasesize;
  749. concat->mtd.numeraseregions = num_erase_region;
  750. concat->mtd.eraseregions = erase_region_p =
  751. kmalloc(num_erase_region *
  752. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  753. if (!erase_region_p) {
  754. kfree(concat);
  755. printk
  756. ("memory allocation error while creating erase region list"
  757. " for device \"%s\"\n", name);
  758. return NULL;
  759. }
  760. /*
  761. * walk the map of the new device once more and fill in
  762. * in erase region info:
  763. */
  764. curr_erasesize = subdev[0]->erasesize;
  765. begin = position = 0;
  766. for (i = 0; i < num_devs; i++) {
  767. if (subdev[i]->numeraseregions == 0) {
  768. /* current subdevice has uniform erase size */
  769. if (subdev[i]->erasesize != curr_erasesize) {
  770. /*
  771. * fill in an mtd_erase_region_info structure for the area
  772. * we have walked so far:
  773. */
  774. erase_region_p->offset = begin;
  775. erase_region_p->erasesize =
  776. curr_erasesize;
  777. tmp64 = position - begin;
  778. do_div(tmp64, curr_erasesize);
  779. erase_region_p->numblocks = tmp64;
  780. begin = position;
  781. curr_erasesize = subdev[i]->erasesize;
  782. ++erase_region_p;
  783. }
  784. position += subdev[i]->size;
  785. } else {
  786. /* current subdevice has variable erase size */
  787. int j;
  788. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  789. /* walk the list of erase regions, count any changes */
  790. if (subdev[i]->eraseregions[j].
  791. erasesize != curr_erasesize) {
  792. erase_region_p->offset = begin;
  793. erase_region_p->erasesize =
  794. curr_erasesize;
  795. tmp64 = position - begin;
  796. do_div(tmp64, curr_erasesize);
  797. erase_region_p->numblocks = tmp64;
  798. begin = position;
  799. curr_erasesize =
  800. subdev[i]->eraseregions[j].
  801. erasesize;
  802. ++erase_region_p;
  803. }
  804. position +=
  805. subdev[i]->eraseregions[j].
  806. numblocks * (uint64_t)curr_erasesize;
  807. }
  808. }
  809. }
  810. /* Now write the final entry */
  811. erase_region_p->offset = begin;
  812. erase_region_p->erasesize = curr_erasesize;
  813. tmp64 = position - begin;
  814. do_div(tmp64, curr_erasesize);
  815. erase_region_p->numblocks = tmp64;
  816. }
  817. return &concat->mtd;
  818. }
  819. /*
  820. * This function destroys an MTD object obtained from concat_mtd_devs()
  821. */
  822. void mtd_concat_destroy(struct mtd_info *mtd)
  823. {
  824. struct mtd_concat *concat = CONCAT(mtd);
  825. if (concat->mtd.numeraseregions)
  826. kfree(concat->mtd.eraseregions);
  827. kfree(concat);
  828. }
  829. EXPORT_SYMBOL(mtd_concat_create);
  830. EXPORT_SYMBOL(mtd_concat_destroy);
  831. MODULE_LICENSE("GPL");
  832. MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  833. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");