mtdconcat.c 24 KB

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