mtdconcat.c 22 KB

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