mtdconcat.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MTD device concatenation layer
  4. *
  5. * Copyright © 2002 Robert Kaiser <[email protected]>
  6. * Copyright © 2002-2010 David Woodhouse <[email protected]>
  7. *
  8. * NAND support by Christian Gan <[email protected]>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/concat.h>
  18. #include <asm/div64.h>
  19. /*
  20. * Our storage structure:
  21. * Subdev points to an array of pointers to struct mtd_info objects
  22. * which is allocated along with this structure
  23. *
  24. */
  25. struct mtd_concat {
  26. struct mtd_info mtd;
  27. int num_subdev;
  28. struct mtd_info **subdev;
  29. };
  30. /*
  31. * how to calculate the size required for the above structure,
  32. * including the pointer array subdev points to:
  33. */
  34. #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
  35. ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
  36. /*
  37. * Given a pointer to the MTD object in the mtd_concat structure,
  38. * we can retrieve the pointer to that structure with this macro.
  39. */
  40. #define CONCAT(x) ((struct mtd_concat *)(x))
  41. /*
  42. * MTD methods which look up the relevant subdevice, translate the
  43. * effective address and pass through to the subdevice.
  44. */
  45. static int
  46. concat_read(struct mtd_info *mtd, loff_t from, size_t len,
  47. size_t * retlen, u_char * buf)
  48. {
  49. struct mtd_concat *concat = CONCAT(mtd);
  50. int ret = 0, err;
  51. int i;
  52. for (i = 0; i < concat->num_subdev; i++) {
  53. struct mtd_info *subdev = concat->subdev[i];
  54. size_t size, retsize;
  55. if (from >= subdev->size) {
  56. /* Not destined for this subdev */
  57. size = 0;
  58. from -= subdev->size;
  59. continue;
  60. }
  61. if (from + len > subdev->size)
  62. /* First part goes into this subdev */
  63. size = subdev->size - from;
  64. else
  65. /* Entire transaction goes into this subdev */
  66. size = len;
  67. err = mtd_read(subdev, from, size, &retsize, buf);
  68. /* Save information about bitflips! */
  69. if (unlikely(err)) {
  70. if (mtd_is_eccerr(err)) {
  71. mtd->ecc_stats.failed++;
  72. ret = err;
  73. } else if (mtd_is_bitflip(err)) {
  74. mtd->ecc_stats.corrected++;
  75. /* Do not overwrite -EBADMSG !! */
  76. if (!ret)
  77. ret = err;
  78. } else
  79. return err;
  80. }
  81. *retlen += retsize;
  82. len -= size;
  83. if (len == 0)
  84. return ret;
  85. buf += size;
  86. from = 0;
  87. }
  88. return -EINVAL;
  89. }
  90. static int
  91. concat_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  92. size_t * retlen, const u_char * buf)
  93. {
  94. struct mtd_concat *concat = CONCAT(mtd);
  95. int err = -EINVAL;
  96. int i;
  97. for (i = 0; i < concat->num_subdev; i++) {
  98. struct mtd_info *subdev = concat->subdev[i];
  99. size_t size, retsize;
  100. if (to >= subdev->size) {
  101. to -= subdev->size;
  102. continue;
  103. }
  104. if (to + len > subdev->size)
  105. size = subdev->size - to;
  106. else
  107. size = len;
  108. err = mtd_panic_write(subdev, to, size, &retsize, buf);
  109. if (err == -EOPNOTSUPP) {
  110. printk(KERN_ERR "mtdconcat: Cannot write from panic without panic_write\n");
  111. return err;
  112. }
  113. if (err)
  114. break;
  115. *retlen += retsize;
  116. len -= size;
  117. if (len == 0)
  118. break;
  119. err = -EINVAL;
  120. buf += size;
  121. to = 0;
  122. }
  123. return err;
  124. }
  125. static int
  126. concat_write(struct mtd_info *mtd, loff_t to, size_t len,
  127. size_t * retlen, const u_char * buf)
  128. {
  129. struct mtd_concat *concat = CONCAT(mtd);
  130. int err = -EINVAL;
  131. int i;
  132. for (i = 0; i < concat->num_subdev; i++) {
  133. struct mtd_info *subdev = concat->subdev[i];
  134. size_t size, retsize;
  135. if (to >= subdev->size) {
  136. size = 0;
  137. to -= subdev->size;
  138. continue;
  139. }
  140. if (to + len > subdev->size)
  141. size = subdev->size - to;
  142. else
  143. size = len;
  144. err = mtd_write(subdev, to, size, &retsize, buf);
  145. if (err)
  146. break;
  147. *retlen += retsize;
  148. len -= size;
  149. if (len == 0)
  150. break;
  151. err = -EINVAL;
  152. buf += size;
  153. to = 0;
  154. }
  155. return err;
  156. }
  157. static int
  158. concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
  159. unsigned long count, loff_t to, size_t * retlen)
  160. {
  161. struct mtd_concat *concat = CONCAT(mtd);
  162. struct kvec *vecs_copy;
  163. unsigned long entry_low, entry_high;
  164. size_t total_len = 0;
  165. int i;
  166. int err = -EINVAL;
  167. /* Calculate total length of data */
  168. for (i = 0; i < count; i++)
  169. total_len += vecs[i].iov_len;
  170. /* Check alignment */
  171. if (mtd->writesize > 1) {
  172. uint64_t __to = to;
  173. if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
  174. return -EINVAL;
  175. }
  176. /* make a copy of vecs */
  177. vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
  178. if (!vecs_copy)
  179. return -ENOMEM;
  180. entry_low = 0;
  181. for (i = 0; i < concat->num_subdev; i++) {
  182. struct mtd_info *subdev = concat->subdev[i];
  183. size_t size, wsize, retsize, old_iov_len;
  184. if (to >= subdev->size) {
  185. to -= subdev->size;
  186. continue;
  187. }
  188. size = min_t(uint64_t, total_len, subdev->size - to);
  189. wsize = size; /* store for future use */
  190. entry_high = entry_low;
  191. while (entry_high < count) {
  192. if (size <= vecs_copy[entry_high].iov_len)
  193. break;
  194. size -= vecs_copy[entry_high++].iov_len;
  195. }
  196. old_iov_len = vecs_copy[entry_high].iov_len;
  197. vecs_copy[entry_high].iov_len = size;
  198. err = mtd_writev(subdev, &vecs_copy[entry_low],
  199. entry_high - entry_low + 1, to, &retsize);
  200. vecs_copy[entry_high].iov_len = old_iov_len - size;
  201. vecs_copy[entry_high].iov_base += size;
  202. entry_low = entry_high;
  203. if (err)
  204. break;
  205. *retlen += retsize;
  206. total_len -= wsize;
  207. if (total_len == 0)
  208. break;
  209. err = -EINVAL;
  210. to = 0;
  211. }
  212. kfree(vecs_copy);
  213. return err;
  214. }
  215. static int
  216. concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  217. {
  218. struct mtd_concat *concat = CONCAT(mtd);
  219. struct mtd_oob_ops devops = *ops;
  220. int i, err, ret = 0;
  221. ops->retlen = ops->oobretlen = 0;
  222. for (i = 0; i < concat->num_subdev; i++) {
  223. struct mtd_info *subdev = concat->subdev[i];
  224. if (from >= subdev->size) {
  225. from -= subdev->size;
  226. continue;
  227. }
  228. /* partial read ? */
  229. if (from + devops.len > subdev->size)
  230. devops.len = subdev->size - from;
  231. err = mtd_read_oob(subdev, from, &devops);
  232. ops->retlen += devops.retlen;
  233. ops->oobretlen += devops.oobretlen;
  234. /* Save information about bitflips! */
  235. if (unlikely(err)) {
  236. if (mtd_is_eccerr(err)) {
  237. mtd->ecc_stats.failed++;
  238. ret = err;
  239. } else if (mtd_is_bitflip(err)) {
  240. mtd->ecc_stats.corrected++;
  241. /* Do not overwrite -EBADMSG !! */
  242. if (!ret)
  243. ret = err;
  244. } else
  245. return err;
  246. }
  247. if (devops.datbuf) {
  248. devops.len = ops->len - ops->retlen;
  249. if (!devops.len)
  250. return ret;
  251. devops.datbuf += devops.retlen;
  252. }
  253. if (devops.oobbuf) {
  254. devops.ooblen = ops->ooblen - ops->oobretlen;
  255. if (!devops.ooblen)
  256. return ret;
  257. devops.oobbuf += ops->oobretlen;
  258. }
  259. from = 0;
  260. }
  261. return -EINVAL;
  262. }
  263. static int
  264. concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
  265. {
  266. struct mtd_concat *concat = CONCAT(mtd);
  267. struct mtd_oob_ops devops = *ops;
  268. int i, err;
  269. if (!(mtd->flags & MTD_WRITEABLE))
  270. return -EROFS;
  271. ops->retlen = ops->oobretlen = 0;
  272. for (i = 0; i < concat->num_subdev; i++) {
  273. struct mtd_info *subdev = concat->subdev[i];
  274. if (to >= subdev->size) {
  275. to -= subdev->size;
  276. continue;
  277. }
  278. /* partial write ? */
  279. if (to + devops.len > subdev->size)
  280. devops.len = subdev->size - to;
  281. err = mtd_write_oob(subdev, to, &devops);
  282. ops->retlen += devops.retlen;
  283. ops->oobretlen += devops.oobretlen;
  284. if (err)
  285. return err;
  286. if (devops.datbuf) {
  287. devops.len = ops->len - ops->retlen;
  288. if (!devops.len)
  289. return 0;
  290. devops.datbuf += devops.retlen;
  291. }
  292. if (devops.oobbuf) {
  293. devops.ooblen = ops->ooblen - ops->oobretlen;
  294. if (!devops.ooblen)
  295. return 0;
  296. devops.oobbuf += devops.oobretlen;
  297. }
  298. to = 0;
  299. }
  300. return -EINVAL;
  301. }
  302. static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
  303. {
  304. struct mtd_concat *concat = CONCAT(mtd);
  305. struct mtd_info *subdev;
  306. int i, err;
  307. uint64_t length, offset = 0;
  308. struct erase_info *erase;
  309. /*
  310. * Check for proper erase block alignment of the to-be-erased area.
  311. * It is easier to do this based on the super device's erase
  312. * region info rather than looking at each particular sub-device
  313. * in turn.
  314. */
  315. if (!concat->mtd.numeraseregions) {
  316. /* the easy case: device has uniform erase block size */
  317. if (instr->addr & (concat->mtd.erasesize - 1))
  318. return -EINVAL;
  319. if (instr->len & (concat->mtd.erasesize - 1))
  320. return -EINVAL;
  321. } else {
  322. /* device has variable erase size */
  323. struct mtd_erase_region_info *erase_regions =
  324. concat->mtd.eraseregions;
  325. /*
  326. * Find the erase region where the to-be-erased area begins:
  327. */
  328. for (i = 0; i < concat->mtd.numeraseregions &&
  329. instr->addr >= erase_regions[i].offset; i++) ;
  330. --i;
  331. /*
  332. * Now erase_regions[i] is the region in which the
  333. * to-be-erased area begins. Verify that the starting
  334. * offset is aligned to this region's erase size:
  335. */
  336. if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
  337. return -EINVAL;
  338. /*
  339. * now find the erase region where the to-be-erased area ends:
  340. */
  341. for (; i < concat->mtd.numeraseregions &&
  342. (instr->addr + instr->len) >= erase_regions[i].offset;
  343. ++i) ;
  344. --i;
  345. /*
  346. * check if the ending offset is aligned to this region's erase size
  347. */
  348. if (i < 0 || ((instr->addr + instr->len) &
  349. (erase_regions[i].erasesize - 1)))
  350. return -EINVAL;
  351. }
  352. /* make a local copy of instr to avoid modifying the caller's struct */
  353. erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
  354. if (!erase)
  355. return -ENOMEM;
  356. *erase = *instr;
  357. length = instr->len;
  358. /*
  359. * find the subdevice where the to-be-erased area begins, adjust
  360. * starting offset to be relative to the subdevice start
  361. */
  362. for (i = 0; i < concat->num_subdev; i++) {
  363. subdev = concat->subdev[i];
  364. if (subdev->size <= erase->addr) {
  365. erase->addr -= subdev->size;
  366. offset += subdev->size;
  367. } else {
  368. break;
  369. }
  370. }
  371. /* must never happen since size limit has been verified above */
  372. BUG_ON(i >= concat->num_subdev);
  373. /* now do the erase: */
  374. err = 0;
  375. for (; length > 0; i++) {
  376. /* loop for all subdevices affected by this request */
  377. subdev = concat->subdev[i]; /* get current subdevice */
  378. /* limit length to subdevice's size: */
  379. if (erase->addr + length > subdev->size)
  380. erase->len = subdev->size - erase->addr;
  381. else
  382. erase->len = length;
  383. length -= erase->len;
  384. if ((err = mtd_erase(subdev, erase))) {
  385. /* sanity check: should never happen since
  386. * block alignment has been checked above */
  387. BUG_ON(err == -EINVAL);
  388. if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  389. instr->fail_addr = erase->fail_addr + offset;
  390. break;
  391. }
  392. /*
  393. * erase->addr specifies the offset of the area to be
  394. * erased *within the current subdevice*. It can be
  395. * non-zero only the first time through this loop, i.e.
  396. * for the first subdevice where blocks need to be erased.
  397. * All the following erases must begin at the start of the
  398. * current subdevice, i.e. at offset zero.
  399. */
  400. erase->addr = 0;
  401. offset += subdev->size;
  402. }
  403. kfree(erase);
  404. return err;
  405. }
  406. static int concat_xxlock(struct mtd_info *mtd, loff_t ofs, uint64_t len,
  407. bool is_lock)
  408. {
  409. struct mtd_concat *concat = CONCAT(mtd);
  410. int i, err = -EINVAL;
  411. for (i = 0; i < concat->num_subdev; i++) {
  412. struct mtd_info *subdev = concat->subdev[i];
  413. uint64_t size;
  414. if (ofs >= subdev->size) {
  415. size = 0;
  416. ofs -= subdev->size;
  417. continue;
  418. }
  419. if (ofs + len > subdev->size)
  420. size = subdev->size - ofs;
  421. else
  422. size = len;
  423. if (is_lock)
  424. err = mtd_lock(subdev, ofs, size);
  425. else
  426. err = mtd_unlock(subdev, ofs, size);
  427. if (err)
  428. break;
  429. len -= size;
  430. if (len == 0)
  431. break;
  432. err = -EINVAL;
  433. ofs = 0;
  434. }
  435. return err;
  436. }
  437. static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  438. {
  439. return concat_xxlock(mtd, ofs, len, true);
  440. }
  441. static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  442. {
  443. return concat_xxlock(mtd, ofs, len, false);
  444. }
  445. static int concat_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  446. {
  447. struct mtd_concat *concat = CONCAT(mtd);
  448. int i, err = -EINVAL;
  449. for (i = 0; i < concat->num_subdev; i++) {
  450. struct mtd_info *subdev = concat->subdev[i];
  451. if (ofs >= subdev->size) {
  452. ofs -= subdev->size;
  453. continue;
  454. }
  455. if (ofs + len > subdev->size)
  456. break;
  457. return mtd_is_locked(subdev, ofs, len);
  458. }
  459. return err;
  460. }
  461. static void concat_sync(struct mtd_info *mtd)
  462. {
  463. struct mtd_concat *concat = CONCAT(mtd);
  464. int i;
  465. for (i = 0; i < concat->num_subdev; i++) {
  466. struct mtd_info *subdev = concat->subdev[i];
  467. mtd_sync(subdev);
  468. }
  469. }
  470. static int concat_suspend(struct mtd_info *mtd)
  471. {
  472. struct mtd_concat *concat = CONCAT(mtd);
  473. int i, rc = 0;
  474. for (i = 0; i < concat->num_subdev; i++) {
  475. struct mtd_info *subdev = concat->subdev[i];
  476. if ((rc = mtd_suspend(subdev)) < 0)
  477. return rc;
  478. }
  479. return rc;
  480. }
  481. static void concat_resume(struct mtd_info *mtd)
  482. {
  483. struct mtd_concat *concat = CONCAT(mtd);
  484. int i;
  485. for (i = 0; i < concat->num_subdev; i++) {
  486. struct mtd_info *subdev = concat->subdev[i];
  487. mtd_resume(subdev);
  488. }
  489. }
  490. static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
  491. {
  492. struct mtd_concat *concat = CONCAT(mtd);
  493. int i, res = 0;
  494. if (!mtd_can_have_bb(concat->subdev[0]))
  495. return res;
  496. for (i = 0; i < concat->num_subdev; i++) {
  497. struct mtd_info *subdev = concat->subdev[i];
  498. if (ofs >= subdev->size) {
  499. ofs -= subdev->size;
  500. continue;
  501. }
  502. res = mtd_block_isbad(subdev, ofs);
  503. break;
  504. }
  505. return res;
  506. }
  507. static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
  508. {
  509. struct mtd_concat *concat = CONCAT(mtd);
  510. int i, err = -EINVAL;
  511. for (i = 0; i < concat->num_subdev; i++) {
  512. struct mtd_info *subdev = concat->subdev[i];
  513. if (ofs >= subdev->size) {
  514. ofs -= subdev->size;
  515. continue;
  516. }
  517. err = mtd_block_markbad(subdev, ofs);
  518. if (!err)
  519. mtd->ecc_stats.badblocks++;
  520. break;
  521. }
  522. return err;
  523. }
  524. /*
  525. * This function constructs a virtual MTD device by concatenating
  526. * num_devs MTD devices. A pointer to the new device object is
  527. * stored to *new_dev upon success. This function does _not_
  528. * register any devices: this is the caller's responsibility.
  529. */
  530. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  531. int num_devs, /* number of subdevices */
  532. const char *name)
  533. { /* name for the new device */
  534. int i;
  535. size_t size;
  536. struct mtd_concat *concat;
  537. struct mtd_info *subdev_master = NULL;
  538. uint32_t max_erasesize, curr_erasesize;
  539. int num_erase_region;
  540. int max_writebufsize = 0;
  541. printk(KERN_NOTICE "Concatenating MTD devices:\n");
  542. for (i = 0; i < num_devs; i++)
  543. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  544. printk(KERN_NOTICE "into device \"%s\"\n", name);
  545. /* allocate the device structure */
  546. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  547. concat = kzalloc(size, GFP_KERNEL);
  548. if (!concat) {
  549. printk
  550. ("memory allocation error while creating concatenated device \"%s\"\n",
  551. name);
  552. return NULL;
  553. }
  554. concat->subdev = (struct mtd_info **) (concat + 1);
  555. /*
  556. * Set up the new "super" device's MTD object structure, check for
  557. * incompatibilities between the subdevices.
  558. */
  559. concat->mtd.type = subdev[0]->type;
  560. concat->mtd.flags = subdev[0]->flags;
  561. concat->mtd.size = subdev[0]->size;
  562. concat->mtd.erasesize = subdev[0]->erasesize;
  563. concat->mtd.writesize = subdev[0]->writesize;
  564. for (i = 0; i < num_devs; i++)
  565. if (max_writebufsize < subdev[i]->writebufsize)
  566. max_writebufsize = subdev[i]->writebufsize;
  567. concat->mtd.writebufsize = max_writebufsize;
  568. concat->mtd.subpage_sft = subdev[0]->subpage_sft;
  569. concat->mtd.oobsize = subdev[0]->oobsize;
  570. concat->mtd.oobavail = subdev[0]->oobavail;
  571. subdev_master = mtd_get_master(subdev[0]);
  572. if (subdev_master->_writev)
  573. concat->mtd._writev = concat_writev;
  574. if (subdev_master->_read_oob)
  575. concat->mtd._read_oob = concat_read_oob;
  576. if (subdev_master->_write_oob)
  577. concat->mtd._write_oob = concat_write_oob;
  578. if (subdev_master->_block_isbad)
  579. concat->mtd._block_isbad = concat_block_isbad;
  580. if (subdev_master->_block_markbad)
  581. concat->mtd._block_markbad = concat_block_markbad;
  582. if (subdev_master->_panic_write)
  583. concat->mtd._panic_write = concat_panic_write;
  584. if (subdev_master->_read)
  585. concat->mtd._read = concat_read;
  586. if (subdev_master->_write)
  587. concat->mtd._write = concat_write;
  588. concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
  589. concat->subdev[0] = subdev[0];
  590. for (i = 1; i < num_devs; i++) {
  591. if (concat->mtd.type != subdev[i]->type) {
  592. kfree(concat);
  593. printk("Incompatible device type on \"%s\"\n",
  594. subdev[i]->name);
  595. return NULL;
  596. }
  597. if (concat->mtd.flags != subdev[i]->flags) {
  598. /*
  599. * Expect all flags except MTD_WRITEABLE to be
  600. * equal on all subdevices.
  601. */
  602. if ((concat->mtd.flags ^ subdev[i]->
  603. flags) & ~MTD_WRITEABLE) {
  604. kfree(concat);
  605. printk("Incompatible device flags on \"%s\"\n",
  606. subdev[i]->name);
  607. return NULL;
  608. } else
  609. /* if writeable attribute differs,
  610. make super device writeable */
  611. concat->mtd.flags |=
  612. subdev[i]->flags & MTD_WRITEABLE;
  613. }
  614. subdev_master = mtd_get_master(subdev[i]);
  615. concat->mtd.size += subdev[i]->size;
  616. concat->mtd.ecc_stats.badblocks +=
  617. subdev[i]->ecc_stats.badblocks;
  618. if (concat->mtd.writesize != subdev[i]->writesize ||
  619. concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
  620. concat->mtd.oobsize != subdev[i]->oobsize ||
  621. !concat->mtd._read_oob != !subdev_master->_read_oob ||
  622. !concat->mtd._write_oob != !subdev_master->_write_oob) {
  623. /*
  624. * Check against subdev[i] for data members, because
  625. * subdev's attributes may be different from master
  626. * mtd device. Check against subdev's master mtd
  627. * device for callbacks, because the existence of
  628. * subdev's callbacks is decided by master mtd device.
  629. */
  630. kfree(concat);
  631. printk("Incompatible OOB or ECC data on \"%s\"\n",
  632. subdev[i]->name);
  633. return NULL;
  634. }
  635. concat->subdev[i] = subdev[i];
  636. }
  637. mtd_set_ooblayout(&concat->mtd, subdev[0]->ooblayout);
  638. concat->num_subdev = num_devs;
  639. concat->mtd.name = name;
  640. concat->mtd._erase = concat_erase;
  641. concat->mtd._sync = concat_sync;
  642. concat->mtd._lock = concat_lock;
  643. concat->mtd._unlock = concat_unlock;
  644. concat->mtd._is_locked = concat_is_locked;
  645. concat->mtd._suspend = concat_suspend;
  646. concat->mtd._resume = concat_resume;
  647. /*
  648. * Combine the erase block size info of the subdevices:
  649. *
  650. * first, walk the map of the new device and see how
  651. * many changes in erase size we have
  652. */
  653. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  654. num_erase_region = 1;
  655. for (i = 0; i < num_devs; i++) {
  656. if (subdev[i]->numeraseregions == 0) {
  657. /* current subdevice has uniform erase size */
  658. if (subdev[i]->erasesize != curr_erasesize) {
  659. /* if it differs from the last subdevice's erase size, count it */
  660. ++num_erase_region;
  661. curr_erasesize = subdev[i]->erasesize;
  662. if (curr_erasesize > max_erasesize)
  663. max_erasesize = curr_erasesize;
  664. }
  665. } else {
  666. /* current subdevice has variable erase size */
  667. int j;
  668. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  669. /* walk the list of erase regions, count any changes */
  670. if (subdev[i]->eraseregions[j].erasesize !=
  671. curr_erasesize) {
  672. ++num_erase_region;
  673. curr_erasesize =
  674. subdev[i]->eraseregions[j].
  675. erasesize;
  676. if (curr_erasesize > max_erasesize)
  677. max_erasesize = curr_erasesize;
  678. }
  679. }
  680. }
  681. }
  682. if (num_erase_region == 1) {
  683. /*
  684. * All subdevices have the same uniform erase size.
  685. * This is easy:
  686. */
  687. concat->mtd.erasesize = curr_erasesize;
  688. concat->mtd.numeraseregions = 0;
  689. } else {
  690. uint64_t tmp64;
  691. /*
  692. * erase block size varies across the subdevices: allocate
  693. * space to store the data describing the variable erase regions
  694. */
  695. struct mtd_erase_region_info *erase_region_p;
  696. uint64_t begin, position;
  697. concat->mtd.erasesize = max_erasesize;
  698. concat->mtd.numeraseregions = num_erase_region;
  699. concat->mtd.eraseregions = erase_region_p =
  700. kmalloc_array(num_erase_region,
  701. sizeof(struct mtd_erase_region_info),
  702. GFP_KERNEL);
  703. if (!erase_region_p) {
  704. kfree(concat);
  705. printk
  706. ("memory allocation error while creating erase region list"
  707. " for device \"%s\"\n", name);
  708. return NULL;
  709. }
  710. /*
  711. * walk the map of the new device once more and fill in
  712. * erase region info:
  713. */
  714. curr_erasesize = subdev[0]->erasesize;
  715. begin = position = 0;
  716. for (i = 0; i < num_devs; i++) {
  717. if (subdev[i]->numeraseregions == 0) {
  718. /* current subdevice has uniform erase size */
  719. if (subdev[i]->erasesize != curr_erasesize) {
  720. /*
  721. * fill in an mtd_erase_region_info structure for the area
  722. * we have walked so far:
  723. */
  724. erase_region_p->offset = begin;
  725. erase_region_p->erasesize =
  726. curr_erasesize;
  727. tmp64 = position - begin;
  728. do_div(tmp64, curr_erasesize);
  729. erase_region_p->numblocks = tmp64;
  730. begin = position;
  731. curr_erasesize = subdev[i]->erasesize;
  732. ++erase_region_p;
  733. }
  734. position += subdev[i]->size;
  735. } else {
  736. /* current subdevice has variable erase size */
  737. int j;
  738. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  739. /* walk the list of erase regions, count any changes */
  740. if (subdev[i]->eraseregions[j].
  741. erasesize != curr_erasesize) {
  742. erase_region_p->offset = begin;
  743. erase_region_p->erasesize =
  744. curr_erasesize;
  745. tmp64 = position - begin;
  746. do_div(tmp64, curr_erasesize);
  747. erase_region_p->numblocks = tmp64;
  748. begin = position;
  749. curr_erasesize =
  750. subdev[i]->eraseregions[j].
  751. erasesize;
  752. ++erase_region_p;
  753. }
  754. position +=
  755. subdev[i]->eraseregions[j].
  756. numblocks * (uint64_t)curr_erasesize;
  757. }
  758. }
  759. }
  760. /* Now write the final entry */
  761. erase_region_p->offset = begin;
  762. erase_region_p->erasesize = curr_erasesize;
  763. tmp64 = position - begin;
  764. do_div(tmp64, curr_erasesize);
  765. erase_region_p->numblocks = tmp64;
  766. }
  767. return &concat->mtd;
  768. }
  769. /* Cleans the context obtained from mtd_concat_create() */
  770. void mtd_concat_destroy(struct mtd_info *mtd)
  771. {
  772. struct mtd_concat *concat = CONCAT(mtd);
  773. if (concat->mtd.numeraseregions)
  774. kfree(concat->mtd.eraseregions);
  775. kfree(concat);
  776. }
  777. EXPORT_SYMBOL(mtd_concat_create);
  778. EXPORT_SYMBOL(mtd_concat_destroy);
  779. MODULE_LICENSE("GPL");
  780. MODULE_AUTHOR("Robert Kaiser <[email protected]>");
  781. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");