mtdconcat.c 21 KB

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