mtdconcat.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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/mtd/mtd.h>
  16. #include <linux/mtd/concat.h>
  17. #include <asm/div64.h>
  18. /*
  19. * Our storage structure:
  20. * Subdev points to an array of pointers to struct mtd_info objects
  21. * which is allocated along with this structure
  22. *
  23. */
  24. struct mtd_concat {
  25. struct mtd_info mtd;
  26. int num_subdev;
  27. struct mtd_info **subdev;
  28. };
  29. /*
  30. * how to calculate the size required for the above structure,
  31. * including the pointer array subdev points to:
  32. */
  33. #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
  34. ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
  35. /*
  36. * Given a pointer to the MTD object in the mtd_concat structure,
  37. * we can retrieve the pointer to that structure with this macro.
  38. */
  39. #define CONCAT(x) ((struct mtd_concat *)(x))
  40. /*
  41. * MTD methods which look up the relevant subdevice, translate the
  42. * effective address and pass through to the subdevice.
  43. */
  44. static int
  45. concat_read(struct mtd_info *mtd, loff_t from, size_t len,
  46. size_t * retlen, u_char * buf)
  47. {
  48. struct mtd_concat *concat = CONCAT(mtd);
  49. int ret = 0, err;
  50. int i;
  51. *retlen = 0;
  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 = subdev->read(subdev, from, size, &retsize, buf);
  68. /* Save information about bitflips! */
  69. if (unlikely(err)) {
  70. if (err == -EBADMSG) {
  71. mtd->ecc_stats.failed++;
  72. ret = err;
  73. } else if (err == -EUCLEAN) {
  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_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. if (!(mtd->flags & MTD_WRITEABLE))
  98. return -EROFS;
  99. *retlen = 0;
  100. for (i = 0; i < concat->num_subdev; i++) {
  101. struct mtd_info *subdev = concat->subdev[i];
  102. size_t size, retsize;
  103. if (to >= subdev->size) {
  104. size = 0;
  105. to -= subdev->size;
  106. continue;
  107. }
  108. if (to + len > subdev->size)
  109. size = subdev->size - to;
  110. else
  111. size = len;
  112. if (!(subdev->flags & MTD_WRITEABLE))
  113. err = -EROFS;
  114. else
  115. err = subdev->write(subdev, to, size, &retsize, buf);
  116. if (err)
  117. break;
  118. *retlen += retsize;
  119. len -= size;
  120. if (len == 0)
  121. break;
  122. err = -EINVAL;
  123. buf += size;
  124. to = 0;
  125. }
  126. return err;
  127. }
  128. static int
  129. concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
  130. unsigned long count, loff_t to, size_t * retlen)
  131. {
  132. struct mtd_concat *concat = CONCAT(mtd);
  133. struct kvec *vecs_copy;
  134. unsigned long entry_low, entry_high;
  135. size_t total_len = 0;
  136. int i;
  137. int err = -EINVAL;
  138. if (!(mtd->flags & MTD_WRITEABLE))
  139. return -EROFS;
  140. *retlen = 0;
  141. /* Calculate total length of data */
  142. for (i = 0; i < count; i++)
  143. total_len += vecs[i].iov_len;
  144. /* Do not allow write past end of device */
  145. if ((to + total_len) > mtd->size)
  146. return -EINVAL;
  147. /* Check alignment */
  148. if (mtd->writesize > 1) {
  149. uint64_t __to = to;
  150. if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
  151. return -EINVAL;
  152. }
  153. /* make a copy of vecs */
  154. vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
  155. if (!vecs_copy)
  156. return -ENOMEM;
  157. memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
  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(total_len, (size_t)(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. u_int32_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 (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 ((instr->addr + instr->len) & (erase_regions[i].erasesize -
  368. 1))
  369. return -EINVAL;
  370. }
  371. instr->fail_addr = 0xffffffff;
  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 != 0xffffffff)
  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, size_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. size_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, size_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. size_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. * This function constructs a virtual MTD device by concatenating
  564. * num_devs MTD devices. A pointer to the new device object is
  565. * stored to *new_dev upon success. This function does _not_
  566. * register any devices: this is the caller's responsibility.
  567. */
  568. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  569. int num_devs, /* number of subdevices */
  570. char *name)
  571. { /* name for the new device */
  572. int i;
  573. size_t size;
  574. struct mtd_concat *concat;
  575. u_int32_t max_erasesize, curr_erasesize;
  576. int num_erase_region;
  577. printk(KERN_NOTICE "Concatenating MTD devices:\n");
  578. for (i = 0; i < num_devs; i++)
  579. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  580. printk(KERN_NOTICE "into device \"%s\"\n", name);
  581. /* allocate the device structure */
  582. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  583. concat = kzalloc(size, GFP_KERNEL);
  584. if (!concat) {
  585. printk
  586. ("memory allocation error while creating concatenated device \"%s\"\n",
  587. name);
  588. return NULL;
  589. }
  590. concat->subdev = (struct mtd_info **) (concat + 1);
  591. /*
  592. * Set up the new "super" device's MTD object structure, check for
  593. * incompatibilites between the subdevices.
  594. */
  595. concat->mtd.type = subdev[0]->type;
  596. concat->mtd.flags = subdev[0]->flags;
  597. concat->mtd.size = subdev[0]->size;
  598. concat->mtd.erasesize = subdev[0]->erasesize;
  599. concat->mtd.writesize = subdev[0]->writesize;
  600. concat->mtd.subpage_sft = subdev[0]->subpage_sft;
  601. concat->mtd.oobsize = subdev[0]->oobsize;
  602. concat->mtd.oobavail = subdev[0]->oobavail;
  603. if (subdev[0]->writev)
  604. concat->mtd.writev = concat_writev;
  605. if (subdev[0]->read_oob)
  606. concat->mtd.read_oob = concat_read_oob;
  607. if (subdev[0]->write_oob)
  608. concat->mtd.write_oob = concat_write_oob;
  609. if (subdev[0]->block_isbad)
  610. concat->mtd.block_isbad = concat_block_isbad;
  611. if (subdev[0]->block_markbad)
  612. concat->mtd.block_markbad = concat_block_markbad;
  613. concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
  614. concat->subdev[0] = subdev[0];
  615. for (i = 1; i < num_devs; i++) {
  616. if (concat->mtd.type != subdev[i]->type) {
  617. kfree(concat);
  618. printk("Incompatible device type on \"%s\"\n",
  619. subdev[i]->name);
  620. return NULL;
  621. }
  622. if (concat->mtd.flags != subdev[i]->flags) {
  623. /*
  624. * Expect all flags except MTD_WRITEABLE to be
  625. * equal on all subdevices.
  626. */
  627. if ((concat->mtd.flags ^ subdev[i]->
  628. flags) & ~MTD_WRITEABLE) {
  629. kfree(concat);
  630. printk("Incompatible device flags on \"%s\"\n",
  631. subdev[i]->name);
  632. return NULL;
  633. } else
  634. /* if writeable attribute differs,
  635. make super device writeable */
  636. concat->mtd.flags |=
  637. subdev[i]->flags & MTD_WRITEABLE;
  638. }
  639. concat->mtd.size += subdev[i]->size;
  640. concat->mtd.ecc_stats.badblocks +=
  641. subdev[i]->ecc_stats.badblocks;
  642. if (concat->mtd.writesize != subdev[i]->writesize ||
  643. concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
  644. concat->mtd.oobsize != subdev[i]->oobsize ||
  645. !concat->mtd.read_oob != !subdev[i]->read_oob ||
  646. !concat->mtd.write_oob != !subdev[i]->write_oob) {
  647. kfree(concat);
  648. printk("Incompatible OOB or ECC data on \"%s\"\n",
  649. subdev[i]->name);
  650. return NULL;
  651. }
  652. concat->subdev[i] = subdev[i];
  653. }
  654. concat->mtd.ecclayout = subdev[0]->ecclayout;
  655. concat->num_subdev = num_devs;
  656. concat->mtd.name = name;
  657. concat->mtd.erase = concat_erase;
  658. concat->mtd.read = concat_read;
  659. concat->mtd.write = concat_write;
  660. concat->mtd.sync = concat_sync;
  661. concat->mtd.lock = concat_lock;
  662. concat->mtd.unlock = concat_unlock;
  663. concat->mtd.suspend = concat_suspend;
  664. concat->mtd.resume = concat_resume;
  665. /*
  666. * Combine the erase block size info of the subdevices:
  667. *
  668. * first, walk the map of the new device and see how
  669. * many changes in erase size we have
  670. */
  671. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  672. num_erase_region = 1;
  673. for (i = 0; i < num_devs; i++) {
  674. if (subdev[i]->numeraseregions == 0) {
  675. /* current subdevice has uniform erase size */
  676. if (subdev[i]->erasesize != curr_erasesize) {
  677. /* if it differs from the last subdevice's erase size, count it */
  678. ++num_erase_region;
  679. curr_erasesize = subdev[i]->erasesize;
  680. if (curr_erasesize > max_erasesize)
  681. max_erasesize = curr_erasesize;
  682. }
  683. } else {
  684. /* current subdevice has variable erase size */
  685. int j;
  686. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  687. /* walk the list of erase regions, count any changes */
  688. if (subdev[i]->eraseregions[j].erasesize !=
  689. curr_erasesize) {
  690. ++num_erase_region;
  691. curr_erasesize =
  692. subdev[i]->eraseregions[j].
  693. erasesize;
  694. if (curr_erasesize > max_erasesize)
  695. max_erasesize = curr_erasesize;
  696. }
  697. }
  698. }
  699. }
  700. if (num_erase_region == 1) {
  701. /*
  702. * All subdevices have the same uniform erase size.
  703. * This is easy:
  704. */
  705. concat->mtd.erasesize = curr_erasesize;
  706. concat->mtd.numeraseregions = 0;
  707. } else {
  708. /*
  709. * erase block size varies across the subdevices: allocate
  710. * space to store the data describing the variable erase regions
  711. */
  712. struct mtd_erase_region_info *erase_region_p;
  713. u_int32_t begin, position;
  714. concat->mtd.erasesize = max_erasesize;
  715. concat->mtd.numeraseregions = num_erase_region;
  716. concat->mtd.eraseregions = erase_region_p =
  717. kmalloc(num_erase_region *
  718. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  719. if (!erase_region_p) {
  720. kfree(concat);
  721. printk
  722. ("memory allocation error while creating erase region list"
  723. " for device \"%s\"\n", name);
  724. return NULL;
  725. }
  726. /*
  727. * walk the map of the new device once more and fill in
  728. * in erase region info:
  729. */
  730. curr_erasesize = subdev[0]->erasesize;
  731. begin = position = 0;
  732. for (i = 0; i < num_devs; i++) {
  733. if (subdev[i]->numeraseregions == 0) {
  734. /* current subdevice has uniform erase size */
  735. if (subdev[i]->erasesize != curr_erasesize) {
  736. /*
  737. * fill in an mtd_erase_region_info structure for the area
  738. * we have walked so far:
  739. */
  740. erase_region_p->offset = begin;
  741. erase_region_p->erasesize =
  742. curr_erasesize;
  743. erase_region_p->numblocks =
  744. (position - begin) / curr_erasesize;
  745. begin = position;
  746. curr_erasesize = subdev[i]->erasesize;
  747. ++erase_region_p;
  748. }
  749. position += subdev[i]->size;
  750. } else {
  751. /* current subdevice has variable erase size */
  752. int j;
  753. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  754. /* walk the list of erase regions, count any changes */
  755. if (subdev[i]->eraseregions[j].
  756. erasesize != curr_erasesize) {
  757. erase_region_p->offset = begin;
  758. erase_region_p->erasesize =
  759. curr_erasesize;
  760. erase_region_p->numblocks =
  761. (position -
  762. begin) / curr_erasesize;
  763. begin = position;
  764. curr_erasesize =
  765. subdev[i]->eraseregions[j].
  766. erasesize;
  767. ++erase_region_p;
  768. }
  769. position +=
  770. subdev[i]->eraseregions[j].
  771. numblocks * curr_erasesize;
  772. }
  773. }
  774. }
  775. /* Now write the final entry */
  776. erase_region_p->offset = begin;
  777. erase_region_p->erasesize = curr_erasesize;
  778. erase_region_p->numblocks = (position - begin) / curr_erasesize;
  779. }
  780. return &concat->mtd;
  781. }
  782. /*
  783. * This function destroys an MTD object obtained from concat_mtd_devs()
  784. */
  785. void mtd_concat_destroy(struct mtd_info *mtd)
  786. {
  787. struct mtd_concat *concat = CONCAT(mtd);
  788. if (concat->mtd.numeraseregions)
  789. kfree(concat->mtd.eraseregions);
  790. kfree(concat);
  791. }
  792. EXPORT_SYMBOL(mtd_concat_create);
  793. EXPORT_SYMBOL(mtd_concat_destroy);
  794. MODULE_LICENSE("GPL");
  795. MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  796. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");