mtdconcat.c 22 KB

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