mtdconcat.c 24 KB

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