mtdconcat.c 23 KB

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