mtdconcat.c 24 KB

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