mtdconcat.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. err = mtd_lock(subdev, ofs, size);
  469. if (err)
  470. break;
  471. len -= size;
  472. if (len == 0)
  473. break;
  474. err = -EINVAL;
  475. ofs = 0;
  476. }
  477. return err;
  478. }
  479. static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  480. {
  481. struct mtd_concat *concat = CONCAT(mtd);
  482. int i, err = 0;
  483. if ((len + ofs) > mtd->size)
  484. return -EINVAL;
  485. for (i = 0; i < concat->num_subdev; i++) {
  486. struct mtd_info *subdev = concat->subdev[i];
  487. uint64_t size;
  488. if (ofs >= subdev->size) {
  489. size = 0;
  490. ofs -= subdev->size;
  491. continue;
  492. }
  493. if (ofs + len > subdev->size)
  494. size = subdev->size - ofs;
  495. else
  496. size = len;
  497. err = mtd_unlock(subdev, ofs, size);
  498. if (err)
  499. break;
  500. len -= size;
  501. if (len == 0)
  502. break;
  503. err = -EINVAL;
  504. ofs = 0;
  505. }
  506. return err;
  507. }
  508. static void concat_sync(struct mtd_info *mtd)
  509. {
  510. struct mtd_concat *concat = CONCAT(mtd);
  511. int i;
  512. for (i = 0; i < concat->num_subdev; i++) {
  513. struct mtd_info *subdev = concat->subdev[i];
  514. mtd_sync(subdev);
  515. }
  516. }
  517. static int concat_suspend(struct mtd_info *mtd)
  518. {
  519. struct mtd_concat *concat = CONCAT(mtd);
  520. int i, rc = 0;
  521. for (i = 0; i < concat->num_subdev; i++) {
  522. struct mtd_info *subdev = concat->subdev[i];
  523. if ((rc = mtd_suspend(subdev)) < 0)
  524. return rc;
  525. }
  526. return rc;
  527. }
  528. static void concat_resume(struct mtd_info *mtd)
  529. {
  530. struct mtd_concat *concat = CONCAT(mtd);
  531. int i;
  532. for (i = 0; i < concat->num_subdev; i++) {
  533. struct mtd_info *subdev = concat->subdev[i];
  534. mtd_resume(subdev);
  535. }
  536. }
  537. static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
  538. {
  539. struct mtd_concat *concat = CONCAT(mtd);
  540. int i, res = 0;
  541. if (!mtd_can_have_bb(concat->subdev[0]))
  542. return res;
  543. if (ofs > mtd->size)
  544. return -EINVAL;
  545. for (i = 0; i < concat->num_subdev; i++) {
  546. struct mtd_info *subdev = concat->subdev[i];
  547. if (ofs >= subdev->size) {
  548. ofs -= subdev->size;
  549. continue;
  550. }
  551. res = mtd_block_isbad(subdev, ofs);
  552. break;
  553. }
  554. return res;
  555. }
  556. static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
  557. {
  558. struct mtd_concat *concat = CONCAT(mtd);
  559. int i, err = -EINVAL;
  560. if (!mtd_can_have_bb(concat->subdev[0]))
  561. return 0;
  562. if (ofs > mtd->size)
  563. return -EINVAL;
  564. for (i = 0; i < concat->num_subdev; i++) {
  565. struct mtd_info *subdev = concat->subdev[i];
  566. if (ofs >= subdev->size) {
  567. ofs -= subdev->size;
  568. continue;
  569. }
  570. err = mtd_block_markbad(subdev, ofs);
  571. if (!err)
  572. mtd->ecc_stats.badblocks++;
  573. break;
  574. }
  575. return err;
  576. }
  577. /*
  578. * try to support NOMMU mmaps on concatenated devices
  579. * - we don't support subdev spanning as we can't guarantee it'll work
  580. */
  581. static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
  582. unsigned long len,
  583. unsigned long offset,
  584. unsigned long flags)
  585. {
  586. struct mtd_concat *concat = CONCAT(mtd);
  587. int i;
  588. for (i = 0; i < concat->num_subdev; i++) {
  589. struct mtd_info *subdev = concat->subdev[i];
  590. if (offset >= subdev->size) {
  591. offset -= subdev->size;
  592. continue;
  593. }
  594. /* we've found the subdev over which the mapping will reside */
  595. if (offset + len > subdev->size)
  596. return (unsigned long) -EINVAL;
  597. return mtd_get_unmapped_area(subdev, len, offset, flags);
  598. }
  599. return (unsigned long) -ENOSYS;
  600. }
  601. /*
  602. * This function constructs a virtual MTD device by concatenating
  603. * num_devs MTD devices. A pointer to the new device object is
  604. * stored to *new_dev upon success. This function does _not_
  605. * register any devices: this is the caller's responsibility.
  606. */
  607. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  608. int num_devs, /* number of subdevices */
  609. const char *name)
  610. { /* name for the new device */
  611. int i;
  612. size_t size;
  613. struct mtd_concat *concat;
  614. uint32_t max_erasesize, curr_erasesize;
  615. int num_erase_region;
  616. int max_writebufsize = 0;
  617. printk(KERN_NOTICE "Concatenating MTD devices:\n");
  618. for (i = 0; i < num_devs; i++)
  619. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  620. printk(KERN_NOTICE "into device \"%s\"\n", name);
  621. /* allocate the device structure */
  622. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  623. concat = kzalloc(size, GFP_KERNEL);
  624. if (!concat) {
  625. printk
  626. ("memory allocation error while creating concatenated device \"%s\"\n",
  627. name);
  628. return NULL;
  629. }
  630. concat->subdev = (struct mtd_info **) (concat + 1);
  631. /*
  632. * Set up the new "super" device's MTD object structure, check for
  633. * incompatibilities between the subdevices.
  634. */
  635. concat->mtd.type = subdev[0]->type;
  636. concat->mtd.flags = subdev[0]->flags;
  637. concat->mtd.size = subdev[0]->size;
  638. concat->mtd.erasesize = subdev[0]->erasesize;
  639. concat->mtd.writesize = subdev[0]->writesize;
  640. for (i = 0; i < num_devs; i++)
  641. if (max_writebufsize < subdev[i]->writebufsize)
  642. max_writebufsize = subdev[i]->writebufsize;
  643. concat->mtd.writebufsize = max_writebufsize;
  644. concat->mtd.subpage_sft = subdev[0]->subpage_sft;
  645. concat->mtd.oobsize = subdev[0]->oobsize;
  646. concat->mtd.oobavail = subdev[0]->oobavail;
  647. if (subdev[0]->writev)
  648. concat->mtd.writev = concat_writev;
  649. if (subdev[0]->read_oob)
  650. concat->mtd.read_oob = concat_read_oob;
  651. if (subdev[0]->write_oob)
  652. concat->mtd.write_oob = concat_write_oob;
  653. if (subdev[0]->block_isbad)
  654. concat->mtd.block_isbad = concat_block_isbad;
  655. if (subdev[0]->block_markbad)
  656. concat->mtd.block_markbad = concat_block_markbad;
  657. concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
  658. concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
  659. concat->subdev[0] = subdev[0];
  660. for (i = 1; i < num_devs; i++) {
  661. if (concat->mtd.type != subdev[i]->type) {
  662. kfree(concat);
  663. printk("Incompatible device type on \"%s\"\n",
  664. subdev[i]->name);
  665. return NULL;
  666. }
  667. if (concat->mtd.flags != subdev[i]->flags) {
  668. /*
  669. * Expect all flags except MTD_WRITEABLE to be
  670. * equal on all subdevices.
  671. */
  672. if ((concat->mtd.flags ^ subdev[i]->
  673. flags) & ~MTD_WRITEABLE) {
  674. kfree(concat);
  675. printk("Incompatible device flags on \"%s\"\n",
  676. subdev[i]->name);
  677. return NULL;
  678. } else
  679. /* if writeable attribute differs,
  680. make super device writeable */
  681. concat->mtd.flags |=
  682. subdev[i]->flags & MTD_WRITEABLE;
  683. }
  684. /* only permit direct mapping if the BDIs are all the same
  685. * - copy-mapping is still permitted
  686. */
  687. if (concat->mtd.backing_dev_info !=
  688. subdev[i]->backing_dev_info)
  689. concat->mtd.backing_dev_info =
  690. &default_backing_dev_info;
  691. concat->mtd.size += subdev[i]->size;
  692. concat->mtd.ecc_stats.badblocks +=
  693. subdev[i]->ecc_stats.badblocks;
  694. if (concat->mtd.writesize != subdev[i]->writesize ||
  695. concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
  696. concat->mtd.oobsize != subdev[i]->oobsize ||
  697. !concat->mtd.read_oob != !subdev[i]->read_oob ||
  698. !concat->mtd.write_oob != !subdev[i]->write_oob) {
  699. kfree(concat);
  700. printk("Incompatible OOB or ECC data on \"%s\"\n",
  701. subdev[i]->name);
  702. return NULL;
  703. }
  704. concat->subdev[i] = subdev[i];
  705. }
  706. concat->mtd.ecclayout = subdev[0]->ecclayout;
  707. concat->num_subdev = num_devs;
  708. concat->mtd.name = name;
  709. concat->mtd.erase = concat_erase;
  710. concat->mtd.read = concat_read;
  711. concat->mtd.write = concat_write;
  712. concat->mtd.sync = concat_sync;
  713. concat->mtd.lock = concat_lock;
  714. concat->mtd.unlock = concat_unlock;
  715. concat->mtd.suspend = concat_suspend;
  716. concat->mtd.resume = concat_resume;
  717. concat->mtd.get_unmapped_area = concat_get_unmapped_area;
  718. /*
  719. * Combine the erase block size info of the subdevices:
  720. *
  721. * first, walk the map of the new device and see how
  722. * many changes in erase size we have
  723. */
  724. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  725. num_erase_region = 1;
  726. for (i = 0; i < num_devs; i++) {
  727. if (subdev[i]->numeraseregions == 0) {
  728. /* current subdevice has uniform erase size */
  729. if (subdev[i]->erasesize != curr_erasesize) {
  730. /* if it differs from the last subdevice's erase size, count it */
  731. ++num_erase_region;
  732. curr_erasesize = subdev[i]->erasesize;
  733. if (curr_erasesize > max_erasesize)
  734. max_erasesize = curr_erasesize;
  735. }
  736. } else {
  737. /* current subdevice has variable erase size */
  738. int j;
  739. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  740. /* walk the list of erase regions, count any changes */
  741. if (subdev[i]->eraseregions[j].erasesize !=
  742. curr_erasesize) {
  743. ++num_erase_region;
  744. curr_erasesize =
  745. subdev[i]->eraseregions[j].
  746. erasesize;
  747. if (curr_erasesize > max_erasesize)
  748. max_erasesize = curr_erasesize;
  749. }
  750. }
  751. }
  752. }
  753. if (num_erase_region == 1) {
  754. /*
  755. * All subdevices have the same uniform erase size.
  756. * This is easy:
  757. */
  758. concat->mtd.erasesize = curr_erasesize;
  759. concat->mtd.numeraseregions = 0;
  760. } else {
  761. uint64_t tmp64;
  762. /*
  763. * erase block size varies across the subdevices: allocate
  764. * space to store the data describing the variable erase regions
  765. */
  766. struct mtd_erase_region_info *erase_region_p;
  767. uint64_t begin, position;
  768. concat->mtd.erasesize = max_erasesize;
  769. concat->mtd.numeraseregions = num_erase_region;
  770. concat->mtd.eraseregions = erase_region_p =
  771. kmalloc(num_erase_region *
  772. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  773. if (!erase_region_p) {
  774. kfree(concat);
  775. printk
  776. ("memory allocation error while creating erase region list"
  777. " for device \"%s\"\n", name);
  778. return NULL;
  779. }
  780. /*
  781. * walk the map of the new device once more and fill in
  782. * in erase region info:
  783. */
  784. curr_erasesize = subdev[0]->erasesize;
  785. begin = position = 0;
  786. for (i = 0; i < num_devs; i++) {
  787. if (subdev[i]->numeraseregions == 0) {
  788. /* current subdevice has uniform erase size */
  789. if (subdev[i]->erasesize != curr_erasesize) {
  790. /*
  791. * fill in an mtd_erase_region_info structure for the area
  792. * we have walked so far:
  793. */
  794. erase_region_p->offset = begin;
  795. erase_region_p->erasesize =
  796. curr_erasesize;
  797. tmp64 = position - begin;
  798. do_div(tmp64, curr_erasesize);
  799. erase_region_p->numblocks = tmp64;
  800. begin = position;
  801. curr_erasesize = subdev[i]->erasesize;
  802. ++erase_region_p;
  803. }
  804. position += subdev[i]->size;
  805. } else {
  806. /* current subdevice has variable erase size */
  807. int j;
  808. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  809. /* walk the list of erase regions, count any changes */
  810. if (subdev[i]->eraseregions[j].
  811. erasesize != curr_erasesize) {
  812. erase_region_p->offset = begin;
  813. erase_region_p->erasesize =
  814. curr_erasesize;
  815. tmp64 = position - begin;
  816. do_div(tmp64, curr_erasesize);
  817. erase_region_p->numblocks = tmp64;
  818. begin = position;
  819. curr_erasesize =
  820. subdev[i]->eraseregions[j].
  821. erasesize;
  822. ++erase_region_p;
  823. }
  824. position +=
  825. subdev[i]->eraseregions[j].
  826. numblocks * (uint64_t)curr_erasesize;
  827. }
  828. }
  829. }
  830. /* Now write the final entry */
  831. erase_region_p->offset = begin;
  832. erase_region_p->erasesize = curr_erasesize;
  833. tmp64 = position - begin;
  834. do_div(tmp64, curr_erasesize);
  835. erase_region_p->numblocks = tmp64;
  836. }
  837. return &concat->mtd;
  838. }
  839. /*
  840. * This function destroys an MTD object obtained from concat_mtd_devs()
  841. */
  842. void mtd_concat_destroy(struct mtd_info *mtd)
  843. {
  844. struct mtd_concat *concat = CONCAT(mtd);
  845. if (concat->mtd.numeraseregions)
  846. kfree(concat->mtd.eraseregions);
  847. kfree(concat);
  848. }
  849. EXPORT_SYMBOL(mtd_concat_create);
  850. EXPORT_SYMBOL(mtd_concat_destroy);
  851. MODULE_LICENSE("GPL");
  852. MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  853. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");