mtdconcat.c 23 KB

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