mtdconcat.c 24 KB

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