mtdconcat.c 24 KB

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