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 = 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 = ops->oobretlen = 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.oobretlen;
  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. int max_writebufsize = 0;
  625. printk(KERN_NOTICE "Concatenating MTD devices:\n");
  626. for (i = 0; i < num_devs; i++)
  627. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  628. printk(KERN_NOTICE "into device \"%s\"\n", name);
  629. /* allocate the device structure */
  630. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  631. concat = kzalloc(size, GFP_KERNEL);
  632. if (!concat) {
  633. printk
  634. ("memory allocation error while creating concatenated device \"%s\"\n",
  635. name);
  636. return NULL;
  637. }
  638. concat->subdev = (struct mtd_info **) (concat + 1);
  639. /*
  640. * Set up the new "super" device's MTD object structure, check for
  641. * incompatibilites between the subdevices.
  642. */
  643. concat->mtd.type = subdev[0]->type;
  644. concat->mtd.flags = subdev[0]->flags;
  645. concat->mtd.size = subdev[0]->size;
  646. concat->mtd.erasesize = subdev[0]->erasesize;
  647. concat->mtd.writesize = subdev[0]->writesize;
  648. for (i = 0; i < num_devs; i++)
  649. if (max_writebufsize < subdev[i]->writebufsize)
  650. max_writebufsize = subdev[i]->writebufsize;
  651. concat->mtd.writebufsize = max_writebufsize;
  652. concat->mtd.subpage_sft = subdev[0]->subpage_sft;
  653. concat->mtd.oobsize = subdev[0]->oobsize;
  654. concat->mtd.oobavail = subdev[0]->oobavail;
  655. if (subdev[0]->writev)
  656. concat->mtd.writev = concat_writev;
  657. if (subdev[0]->read_oob)
  658. concat->mtd.read_oob = concat_read_oob;
  659. if (subdev[0]->write_oob)
  660. concat->mtd.write_oob = concat_write_oob;
  661. if (subdev[0]->block_isbad)
  662. concat->mtd.block_isbad = concat_block_isbad;
  663. if (subdev[0]->block_markbad)
  664. concat->mtd.block_markbad = concat_block_markbad;
  665. concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
  666. concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
  667. concat->subdev[0] = subdev[0];
  668. for (i = 1; i < num_devs; i++) {
  669. if (concat->mtd.type != subdev[i]->type) {
  670. kfree(concat);
  671. printk("Incompatible device type on \"%s\"\n",
  672. subdev[i]->name);
  673. return NULL;
  674. }
  675. if (concat->mtd.flags != subdev[i]->flags) {
  676. /*
  677. * Expect all flags except MTD_WRITEABLE to be
  678. * equal on all subdevices.
  679. */
  680. if ((concat->mtd.flags ^ subdev[i]->
  681. flags) & ~MTD_WRITEABLE) {
  682. kfree(concat);
  683. printk("Incompatible device flags on \"%s\"\n",
  684. subdev[i]->name);
  685. return NULL;
  686. } else
  687. /* if writeable attribute differs,
  688. make super device writeable */
  689. concat->mtd.flags |=
  690. subdev[i]->flags & MTD_WRITEABLE;
  691. }
  692. /* only permit direct mapping if the BDIs are all the same
  693. * - copy-mapping is still permitted
  694. */
  695. if (concat->mtd.backing_dev_info !=
  696. subdev[i]->backing_dev_info)
  697. concat->mtd.backing_dev_info =
  698. &default_backing_dev_info;
  699. concat->mtd.size += subdev[i]->size;
  700. concat->mtd.ecc_stats.badblocks +=
  701. subdev[i]->ecc_stats.badblocks;
  702. if (concat->mtd.writesize != subdev[i]->writesize ||
  703. concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
  704. concat->mtd.oobsize != subdev[i]->oobsize ||
  705. !concat->mtd.read_oob != !subdev[i]->read_oob ||
  706. !concat->mtd.write_oob != !subdev[i]->write_oob) {
  707. kfree(concat);
  708. printk("Incompatible OOB or ECC data on \"%s\"\n",
  709. subdev[i]->name);
  710. return NULL;
  711. }
  712. concat->subdev[i] = subdev[i];
  713. }
  714. concat->mtd.ecclayout = subdev[0]->ecclayout;
  715. concat->num_subdev = num_devs;
  716. concat->mtd.name = name;
  717. concat->mtd.erase = concat_erase;
  718. concat->mtd.read = concat_read;
  719. concat->mtd.write = concat_write;
  720. concat->mtd.sync = concat_sync;
  721. concat->mtd.lock = concat_lock;
  722. concat->mtd.unlock = concat_unlock;
  723. concat->mtd.suspend = concat_suspend;
  724. concat->mtd.resume = concat_resume;
  725. concat->mtd.get_unmapped_area = concat_get_unmapped_area;
  726. /*
  727. * Combine the erase block size info of the subdevices:
  728. *
  729. * first, walk the map of the new device and see how
  730. * many changes in erase size we have
  731. */
  732. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  733. num_erase_region = 1;
  734. for (i = 0; i < num_devs; i++) {
  735. if (subdev[i]->numeraseregions == 0) {
  736. /* current subdevice has uniform erase size */
  737. if (subdev[i]->erasesize != curr_erasesize) {
  738. /* if it differs from the last subdevice's erase size, count it */
  739. ++num_erase_region;
  740. curr_erasesize = subdev[i]->erasesize;
  741. if (curr_erasesize > max_erasesize)
  742. max_erasesize = curr_erasesize;
  743. }
  744. } else {
  745. /* current subdevice has variable erase size */
  746. int j;
  747. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  748. /* walk the list of erase regions, count any changes */
  749. if (subdev[i]->eraseregions[j].erasesize !=
  750. curr_erasesize) {
  751. ++num_erase_region;
  752. curr_erasesize =
  753. subdev[i]->eraseregions[j].
  754. erasesize;
  755. if (curr_erasesize > max_erasesize)
  756. max_erasesize = curr_erasesize;
  757. }
  758. }
  759. }
  760. }
  761. if (num_erase_region == 1) {
  762. /*
  763. * All subdevices have the same uniform erase size.
  764. * This is easy:
  765. */
  766. concat->mtd.erasesize = curr_erasesize;
  767. concat->mtd.numeraseregions = 0;
  768. } else {
  769. uint64_t tmp64;
  770. /*
  771. * erase block size varies across the subdevices: allocate
  772. * space to store the data describing the variable erase regions
  773. */
  774. struct mtd_erase_region_info *erase_region_p;
  775. uint64_t begin, position;
  776. concat->mtd.erasesize = max_erasesize;
  777. concat->mtd.numeraseregions = num_erase_region;
  778. concat->mtd.eraseregions = erase_region_p =
  779. kmalloc(num_erase_region *
  780. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  781. if (!erase_region_p) {
  782. kfree(concat);
  783. printk
  784. ("memory allocation error while creating erase region list"
  785. " for device \"%s\"\n", name);
  786. return NULL;
  787. }
  788. /*
  789. * walk the map of the new device once more and fill in
  790. * in erase region info:
  791. */
  792. curr_erasesize = subdev[0]->erasesize;
  793. begin = position = 0;
  794. for (i = 0; i < num_devs; i++) {
  795. if (subdev[i]->numeraseregions == 0) {
  796. /* current subdevice has uniform erase size */
  797. if (subdev[i]->erasesize != curr_erasesize) {
  798. /*
  799. * fill in an mtd_erase_region_info structure for the area
  800. * we have walked so far:
  801. */
  802. erase_region_p->offset = begin;
  803. erase_region_p->erasesize =
  804. curr_erasesize;
  805. tmp64 = position - begin;
  806. do_div(tmp64, curr_erasesize);
  807. erase_region_p->numblocks = tmp64;
  808. begin = position;
  809. curr_erasesize = subdev[i]->erasesize;
  810. ++erase_region_p;
  811. }
  812. position += subdev[i]->size;
  813. } else {
  814. /* current subdevice has variable erase size */
  815. int j;
  816. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  817. /* walk the list of erase regions, count any changes */
  818. if (subdev[i]->eraseregions[j].
  819. erasesize != curr_erasesize) {
  820. erase_region_p->offset = begin;
  821. erase_region_p->erasesize =
  822. curr_erasesize;
  823. tmp64 = position - begin;
  824. do_div(tmp64, curr_erasesize);
  825. erase_region_p->numblocks = tmp64;
  826. begin = position;
  827. curr_erasesize =
  828. subdev[i]->eraseregions[j].
  829. erasesize;
  830. ++erase_region_p;
  831. }
  832. position +=
  833. subdev[i]->eraseregions[j].
  834. numblocks * (uint64_t)curr_erasesize;
  835. }
  836. }
  837. }
  838. /* Now write the final entry */
  839. erase_region_p->offset = begin;
  840. erase_region_p->erasesize = curr_erasesize;
  841. tmp64 = position - begin;
  842. do_div(tmp64, curr_erasesize);
  843. erase_region_p->numblocks = tmp64;
  844. }
  845. return &concat->mtd;
  846. }
  847. /*
  848. * This function destroys an MTD object obtained from concat_mtd_devs()
  849. */
  850. void mtd_concat_destroy(struct mtd_info *mtd)
  851. {
  852. struct mtd_concat *concat = CONCAT(mtd);
  853. if (concat->mtd.numeraseregions)
  854. kfree(concat->mtd.eraseregions);
  855. kfree(concat);
  856. }
  857. EXPORT_SYMBOL(mtd_concat_create);
  858. EXPORT_SYMBOL(mtd_concat_destroy);
  859. MODULE_LICENSE("GPL");
  860. MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  861. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");