mtdconcat.c 23 KB

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