mtdconcat.c 23 KB

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