mtdconcat.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/compat.h>
  12. #include <linux/mtd/concat.h>
  13. #include <ubi_uboot.h>
  14. /*
  15. * Our storage structure:
  16. * Subdev points to an array of pointers to struct mtd_info objects
  17. * which is allocated along with this structure
  18. *
  19. */
  20. struct mtd_concat {
  21. struct mtd_info mtd;
  22. int num_subdev;
  23. struct mtd_info **subdev;
  24. };
  25. /*
  26. * how to calculate the size required for the above structure,
  27. * including the pointer array subdev points to:
  28. */
  29. #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
  30. ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
  31. /*
  32. * Given a pointer to the MTD object in the mtd_concat structure,
  33. * we can retrieve the pointer to that structure with this macro.
  34. */
  35. #define CONCAT(x) ((struct mtd_concat *)(x))
  36. /*
  37. * MTD methods which look up the relevant subdevice, translate the
  38. * effective address and pass through to the subdevice.
  39. */
  40. static int
  41. concat_read(struct mtd_info *mtd, loff_t from, size_t len,
  42. size_t * retlen, u_char * buf)
  43. {
  44. struct mtd_concat *concat = CONCAT(mtd);
  45. int ret = 0, err;
  46. int i;
  47. *retlen = 0;
  48. for (i = 0; i < concat->num_subdev; i++) {
  49. struct mtd_info *subdev = concat->subdev[i];
  50. size_t size, retsize;
  51. if (from >= subdev->size) {
  52. /* Not destined for this subdev */
  53. size = 0;
  54. from -= subdev->size;
  55. continue;
  56. }
  57. if (from + len > subdev->size)
  58. /* First part goes into this subdev */
  59. size = subdev->size - from;
  60. else
  61. /* Entire transaction goes into this subdev */
  62. size = len;
  63. err = subdev->read(subdev, from, size, &retsize, buf);
  64. /* Save information about bitflips! */
  65. if (unlikely(err)) {
  66. if (err == -EBADMSG) {
  67. mtd->ecc_stats.failed++;
  68. ret = err;
  69. } else if (err == -EUCLEAN) {
  70. mtd->ecc_stats.corrected++;
  71. /* Do not overwrite -EBADMSG !! */
  72. if (!ret)
  73. ret = err;
  74. } else
  75. return err;
  76. }
  77. *retlen += retsize;
  78. len -= size;
  79. if (len == 0)
  80. return ret;
  81. buf += size;
  82. from = 0;
  83. }
  84. return -EINVAL;
  85. }
  86. static int
  87. concat_write(struct mtd_info *mtd, loff_t to, size_t len,
  88. size_t * retlen, const u_char * buf)
  89. {
  90. struct mtd_concat *concat = CONCAT(mtd);
  91. int err = -EINVAL;
  92. int i;
  93. if (!(mtd->flags & MTD_WRITEABLE))
  94. return -EROFS;
  95. *retlen = 0;
  96. for (i = 0; i < concat->num_subdev; i++) {
  97. struct mtd_info *subdev = concat->subdev[i];
  98. size_t size, retsize;
  99. if (to >= subdev->size) {
  100. size = 0;
  101. to -= subdev->size;
  102. continue;
  103. }
  104. if (to + len > subdev->size)
  105. size = subdev->size - to;
  106. else
  107. size = len;
  108. if (!(subdev->flags & MTD_WRITEABLE))
  109. err = -EROFS;
  110. else
  111. err = subdev->write(subdev, to, size, &retsize, buf);
  112. if (err)
  113. break;
  114. *retlen += retsize;
  115. len -= size;
  116. if (len == 0)
  117. break;
  118. err = -EINVAL;
  119. buf += size;
  120. to = 0;
  121. }
  122. return err;
  123. }
  124. static int
  125. concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  126. {
  127. struct mtd_concat *concat = CONCAT(mtd);
  128. struct mtd_oob_ops devops = *ops;
  129. int i, err, ret = 0;
  130. ops->retlen = ops->oobretlen = 0;
  131. for (i = 0; i < concat->num_subdev; i++) {
  132. struct mtd_info *subdev = concat->subdev[i];
  133. if (from >= subdev->size) {
  134. from -= subdev->size;
  135. continue;
  136. }
  137. /* partial read ? */
  138. if (from + devops.len > subdev->size)
  139. devops.len = subdev->size - from;
  140. err = subdev->read_oob(subdev, from, &devops);
  141. ops->retlen += devops.retlen;
  142. ops->oobretlen += devops.oobretlen;
  143. /* Save information about bitflips! */
  144. if (unlikely(err)) {
  145. if (err == -EBADMSG) {
  146. mtd->ecc_stats.failed++;
  147. ret = err;
  148. } else if (err == -EUCLEAN) {
  149. mtd->ecc_stats.corrected++;
  150. /* Do not overwrite -EBADMSG !! */
  151. if (!ret)
  152. ret = err;
  153. } else
  154. return err;
  155. }
  156. if (devops.datbuf) {
  157. devops.len = ops->len - ops->retlen;
  158. if (!devops.len)
  159. return ret;
  160. devops.datbuf += devops.retlen;
  161. }
  162. if (devops.oobbuf) {
  163. devops.ooblen = ops->ooblen - ops->oobretlen;
  164. if (!devops.ooblen)
  165. return ret;
  166. devops.oobbuf += ops->oobretlen;
  167. }
  168. from = 0;
  169. }
  170. return -EINVAL;
  171. }
  172. static int
  173. concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
  174. {
  175. struct mtd_concat *concat = CONCAT(mtd);
  176. struct mtd_oob_ops devops = *ops;
  177. int i, err;
  178. if (!(mtd->flags & MTD_WRITEABLE))
  179. return -EROFS;
  180. ops->retlen = 0;
  181. for (i = 0; i < concat->num_subdev; i++) {
  182. struct mtd_info *subdev = concat->subdev[i];
  183. if (to >= subdev->size) {
  184. to -= subdev->size;
  185. continue;
  186. }
  187. /* partial write ? */
  188. if (to + devops.len > subdev->size)
  189. devops.len = subdev->size - to;
  190. err = subdev->write_oob(subdev, to, &devops);
  191. ops->retlen += devops.retlen;
  192. if (err)
  193. return err;
  194. if (devops.datbuf) {
  195. devops.len = ops->len - ops->retlen;
  196. if (!devops.len)
  197. return 0;
  198. devops.datbuf += devops.retlen;
  199. }
  200. if (devops.oobbuf) {
  201. devops.ooblen = ops->ooblen - ops->oobretlen;
  202. if (!devops.ooblen)
  203. return 0;
  204. devops.oobbuf += devops.oobretlen;
  205. }
  206. to = 0;
  207. }
  208. return -EINVAL;
  209. }
  210. static void concat_erase_callback(struct erase_info *instr)
  211. {
  212. /* Nothing to do here in U-Boot */
  213. }
  214. static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
  215. {
  216. int err;
  217. wait_queue_head_t waitq;
  218. DECLARE_WAITQUEUE(wait, current);
  219. /*
  220. * This code was stol^H^H^H^Hinspired by mtdchar.c
  221. */
  222. init_waitqueue_head(&waitq);
  223. erase->mtd = mtd;
  224. erase->callback = concat_erase_callback;
  225. erase->priv = (unsigned long) &waitq;
  226. /*
  227. * FIXME: Allow INTERRUPTIBLE. Which means
  228. * not having the wait_queue head on the stack.
  229. */
  230. err = mtd->erase(mtd, erase);
  231. if (!err) {
  232. set_current_state(TASK_UNINTERRUPTIBLE);
  233. add_wait_queue(&waitq, &wait);
  234. if (erase->state != MTD_ERASE_DONE
  235. && erase->state != MTD_ERASE_FAILED)
  236. schedule();
  237. remove_wait_queue(&waitq, &wait);
  238. set_current_state(TASK_RUNNING);
  239. err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
  240. }
  241. return err;
  242. }
  243. static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
  244. {
  245. struct mtd_concat *concat = CONCAT(mtd);
  246. struct mtd_info *subdev;
  247. int i, err;
  248. uint64_t length, offset = 0;
  249. struct erase_info *erase;
  250. if (!(mtd->flags & MTD_WRITEABLE))
  251. return -EROFS;
  252. if (instr->addr > concat->mtd.size)
  253. return -EINVAL;
  254. if (instr->len + instr->addr > concat->mtd.size)
  255. return -EINVAL;
  256. /*
  257. * Check for proper erase block alignment of the to-be-erased area.
  258. * It is easier to do this based on the super device's erase
  259. * region info rather than looking at each particular sub-device
  260. * in turn.
  261. */
  262. if (!concat->mtd.numeraseregions) {
  263. /* the easy case: device has uniform erase block size */
  264. if (instr->addr & (concat->mtd.erasesize - 1))
  265. return -EINVAL;
  266. if (instr->len & (concat->mtd.erasesize - 1))
  267. return -EINVAL;
  268. } else {
  269. /* device has variable erase size */
  270. struct mtd_erase_region_info *erase_regions =
  271. concat->mtd.eraseregions;
  272. /*
  273. * Find the erase region where the to-be-erased area begins:
  274. */
  275. for (i = 0; i < concat->mtd.numeraseregions &&
  276. instr->addr >= erase_regions[i].offset; i++) ;
  277. --i;
  278. /*
  279. * Now erase_regions[i] is the region in which the
  280. * to-be-erased area begins. Verify that the starting
  281. * offset is aligned to this region's erase size:
  282. */
  283. if (instr->addr & (erase_regions[i].erasesize - 1))
  284. return -EINVAL;
  285. /*
  286. * now find the erase region where the to-be-erased area ends:
  287. */
  288. for (; i < concat->mtd.numeraseregions &&
  289. (instr->addr + instr->len) >= erase_regions[i].offset;
  290. ++i) ;
  291. --i;
  292. /*
  293. * check if the ending offset is aligned to this region's erase size
  294. */
  295. if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
  296. 1))
  297. return -EINVAL;
  298. }
  299. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  300. /* make a local copy of instr to avoid modifying the caller's struct */
  301. erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
  302. if (!erase)
  303. return -ENOMEM;
  304. *erase = *instr;
  305. length = instr->len;
  306. /*
  307. * find the subdevice where the to-be-erased area begins, adjust
  308. * starting offset to be relative to the subdevice start
  309. */
  310. for (i = 0; i < concat->num_subdev; i++) {
  311. subdev = concat->subdev[i];
  312. if (subdev->size <= erase->addr) {
  313. erase->addr -= subdev->size;
  314. offset += subdev->size;
  315. } else {
  316. break;
  317. }
  318. }
  319. /* must never happen since size limit has been verified above */
  320. BUG_ON(i >= concat->num_subdev);
  321. /* now do the erase: */
  322. err = 0;
  323. for (; length > 0; i++) {
  324. /* loop for all subdevices affected by this request */
  325. subdev = concat->subdev[i]; /* get current subdevice */
  326. /* limit length to subdevice's size: */
  327. if (erase->addr + length > subdev->size)
  328. erase->len = subdev->size - erase->addr;
  329. else
  330. erase->len = length;
  331. if (!(subdev->flags & MTD_WRITEABLE)) {
  332. err = -EROFS;
  333. break;
  334. }
  335. length -= erase->len;
  336. if ((err = concat_dev_erase(subdev, erase))) {
  337. /* sanity check: should never happen since
  338. * block alignment has been checked above */
  339. BUG_ON(err == -EINVAL);
  340. if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  341. instr->fail_addr = erase->fail_addr + offset;
  342. break;
  343. }
  344. /*
  345. * erase->addr specifies the offset of the area to be
  346. * erased *within the current subdevice*. It can be
  347. * non-zero only the first time through this loop, i.e.
  348. * for the first subdevice where blocks need to be erased.
  349. * All the following erases must begin at the start of the
  350. * current subdevice, i.e. at offset zero.
  351. */
  352. erase->addr = 0;
  353. offset += subdev->size;
  354. }
  355. instr->state = erase->state;
  356. kfree(erase);
  357. if (err)
  358. return err;
  359. if (instr->callback)
  360. instr->callback(instr);
  361. return 0;
  362. }
  363. static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  364. {
  365. struct mtd_concat *concat = CONCAT(mtd);
  366. int i, err = -EINVAL;
  367. if ((len + ofs) > mtd->size)
  368. return -EINVAL;
  369. for (i = 0; i < concat->num_subdev; i++) {
  370. struct mtd_info *subdev = concat->subdev[i];
  371. uint64_t size;
  372. if (ofs >= subdev->size) {
  373. size = 0;
  374. ofs -= subdev->size;
  375. continue;
  376. }
  377. if (ofs + len > subdev->size)
  378. size = subdev->size - ofs;
  379. else
  380. size = len;
  381. err = subdev->lock(subdev, ofs, size);
  382. if (err)
  383. break;
  384. len -= size;
  385. if (len == 0)
  386. break;
  387. err = -EINVAL;
  388. ofs = 0;
  389. }
  390. return err;
  391. }
  392. static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  393. {
  394. struct mtd_concat *concat = CONCAT(mtd);
  395. int i, err = 0;
  396. if ((len + ofs) > mtd->size)
  397. return -EINVAL;
  398. for (i = 0; i < concat->num_subdev; i++) {
  399. struct mtd_info *subdev = concat->subdev[i];
  400. uint64_t size;
  401. if (ofs >= subdev->size) {
  402. size = 0;
  403. ofs -= subdev->size;
  404. continue;
  405. }
  406. if (ofs + len > subdev->size)
  407. size = subdev->size - ofs;
  408. else
  409. size = len;
  410. err = subdev->unlock(subdev, ofs, size);
  411. if (err)
  412. break;
  413. len -= size;
  414. if (len == 0)
  415. break;
  416. err = -EINVAL;
  417. ofs = 0;
  418. }
  419. return err;
  420. }
  421. static void concat_sync(struct mtd_info *mtd)
  422. {
  423. struct mtd_concat *concat = CONCAT(mtd);
  424. int i;
  425. for (i = 0; i < concat->num_subdev; i++) {
  426. struct mtd_info *subdev = concat->subdev[i];
  427. subdev->sync(subdev);
  428. }
  429. }
  430. static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
  431. {
  432. struct mtd_concat *concat = CONCAT(mtd);
  433. int i, res = 0;
  434. if (!concat->subdev[0]->block_isbad)
  435. return res;
  436. if (ofs > mtd->size)
  437. return -EINVAL;
  438. for (i = 0; i < concat->num_subdev; i++) {
  439. struct mtd_info *subdev = concat->subdev[i];
  440. if (ofs >= subdev->size) {
  441. ofs -= subdev->size;
  442. continue;
  443. }
  444. res = subdev->block_isbad(subdev, ofs);
  445. break;
  446. }
  447. return res;
  448. }
  449. static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
  450. {
  451. struct mtd_concat *concat = CONCAT(mtd);
  452. int i, err = -EINVAL;
  453. if (!concat->subdev[0]->block_markbad)
  454. return 0;
  455. if (ofs > mtd->size)
  456. return -EINVAL;
  457. for (i = 0; i < concat->num_subdev; i++) {
  458. struct mtd_info *subdev = concat->subdev[i];
  459. if (ofs >= subdev->size) {
  460. ofs -= subdev->size;
  461. continue;
  462. }
  463. err = subdev->block_markbad(subdev, ofs);
  464. if (!err)
  465. mtd->ecc_stats.badblocks++;
  466. break;
  467. }
  468. return err;
  469. }
  470. /*
  471. * This function constructs a virtual MTD device by concatenating
  472. * num_devs MTD devices. A pointer to the new device object is
  473. * stored to *new_dev upon success. This function does _not_
  474. * register any devices: this is the caller's responsibility.
  475. */
  476. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  477. int num_devs, /* number of subdevices */
  478. const char *name)
  479. { /* name for the new device */
  480. int i;
  481. size_t size;
  482. struct mtd_concat *concat;
  483. uint32_t max_erasesize, curr_erasesize;
  484. int num_erase_region;
  485. debug("Concatenating MTD devices:\n");
  486. for (i = 0; i < num_devs; i++)
  487. debug("(%d): \"%s\"\n", i, subdev[i]->name);
  488. debug("into device \"%s\"\n", name);
  489. /* allocate the device structure */
  490. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  491. concat = kzalloc(size, GFP_KERNEL);
  492. if (!concat) {
  493. printk
  494. ("memory allocation error while creating concatenated device \"%s\"\n",
  495. name);
  496. return NULL;
  497. }
  498. concat->subdev = (struct mtd_info **) (concat + 1);
  499. /*
  500. * Set up the new "super" device's MTD object structure, check for
  501. * incompatibilites between the subdevices.
  502. */
  503. concat->mtd.type = subdev[0]->type;
  504. concat->mtd.flags = subdev[0]->flags;
  505. concat->mtd.size = subdev[0]->size;
  506. concat->mtd.erasesize = subdev[0]->erasesize;
  507. concat->mtd.writesize = subdev[0]->writesize;
  508. concat->mtd.subpage_sft = subdev[0]->subpage_sft;
  509. concat->mtd.oobsize = subdev[0]->oobsize;
  510. concat->mtd.oobavail = subdev[0]->oobavail;
  511. if (subdev[0]->read_oob)
  512. concat->mtd.read_oob = concat_read_oob;
  513. if (subdev[0]->write_oob)
  514. concat->mtd.write_oob = concat_write_oob;
  515. if (subdev[0]->block_isbad)
  516. concat->mtd.block_isbad = concat_block_isbad;
  517. if (subdev[0]->block_markbad)
  518. concat->mtd.block_markbad = concat_block_markbad;
  519. concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
  520. concat->subdev[0] = subdev[0];
  521. for (i = 1; i < num_devs; i++) {
  522. if (concat->mtd.type != subdev[i]->type) {
  523. kfree(concat);
  524. printk("Incompatible device type on \"%s\"\n",
  525. subdev[i]->name);
  526. return NULL;
  527. }
  528. if (concat->mtd.flags != subdev[i]->flags) {
  529. /*
  530. * Expect all flags except MTD_WRITEABLE to be
  531. * equal on all subdevices.
  532. */
  533. if ((concat->mtd.flags ^ subdev[i]->
  534. flags) & ~MTD_WRITEABLE) {
  535. kfree(concat);
  536. printk("Incompatible device flags on \"%s\"\n",
  537. subdev[i]->name);
  538. return NULL;
  539. } else
  540. /* if writeable attribute differs,
  541. make super device writeable */
  542. concat->mtd.flags |=
  543. subdev[i]->flags & MTD_WRITEABLE;
  544. }
  545. concat->mtd.size += subdev[i]->size;
  546. concat->mtd.ecc_stats.badblocks +=
  547. subdev[i]->ecc_stats.badblocks;
  548. if (concat->mtd.writesize != subdev[i]->writesize ||
  549. concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
  550. concat->mtd.oobsize != subdev[i]->oobsize ||
  551. !concat->mtd.read_oob != !subdev[i]->read_oob ||
  552. !concat->mtd.write_oob != !subdev[i]->write_oob) {
  553. kfree(concat);
  554. printk("Incompatible OOB or ECC data on \"%s\"\n",
  555. subdev[i]->name);
  556. return NULL;
  557. }
  558. concat->subdev[i] = subdev[i];
  559. }
  560. concat->mtd.ecclayout = subdev[0]->ecclayout;
  561. concat->num_subdev = num_devs;
  562. concat->mtd.name = name;
  563. concat->mtd.erase = concat_erase;
  564. concat->mtd.read = concat_read;
  565. concat->mtd.write = concat_write;
  566. concat->mtd.sync = concat_sync;
  567. concat->mtd.lock = concat_lock;
  568. concat->mtd.unlock = concat_unlock;
  569. /*
  570. * Combine the erase block size info of the subdevices:
  571. *
  572. * first, walk the map of the new device and see how
  573. * many changes in erase size we have
  574. */
  575. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  576. num_erase_region = 1;
  577. for (i = 0; i < num_devs; i++) {
  578. if (subdev[i]->numeraseregions == 0) {
  579. /* current subdevice has uniform erase size */
  580. if (subdev[i]->erasesize != curr_erasesize) {
  581. /* if it differs from the last subdevice's erase size, count it */
  582. ++num_erase_region;
  583. curr_erasesize = subdev[i]->erasesize;
  584. if (curr_erasesize > max_erasesize)
  585. max_erasesize = curr_erasesize;
  586. }
  587. } else {
  588. /* current subdevice has variable erase size */
  589. int j;
  590. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  591. /* walk the list of erase regions, count any changes */
  592. if (subdev[i]->eraseregions[j].erasesize !=
  593. curr_erasesize) {
  594. ++num_erase_region;
  595. curr_erasesize =
  596. subdev[i]->eraseregions[j].
  597. erasesize;
  598. if (curr_erasesize > max_erasesize)
  599. max_erasesize = curr_erasesize;
  600. }
  601. }
  602. }
  603. }
  604. if (num_erase_region == 1) {
  605. /*
  606. * All subdevices have the same uniform erase size.
  607. * This is easy:
  608. */
  609. concat->mtd.erasesize = curr_erasesize;
  610. concat->mtd.numeraseregions = 0;
  611. } else {
  612. uint64_t tmp64;
  613. /*
  614. * erase block size varies across the subdevices: allocate
  615. * space to store the data describing the variable erase regions
  616. */
  617. struct mtd_erase_region_info *erase_region_p;
  618. uint64_t begin, position;
  619. concat->mtd.erasesize = max_erasesize;
  620. concat->mtd.numeraseregions = num_erase_region;
  621. concat->mtd.eraseregions = erase_region_p =
  622. kmalloc(num_erase_region *
  623. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  624. if (!erase_region_p) {
  625. kfree(concat);
  626. printk
  627. ("memory allocation error while creating erase region list"
  628. " for device \"%s\"\n", name);
  629. return NULL;
  630. }
  631. /*
  632. * walk the map of the new device once more and fill in
  633. * in erase region info:
  634. */
  635. curr_erasesize = subdev[0]->erasesize;
  636. begin = position = 0;
  637. for (i = 0; i < num_devs; i++) {
  638. if (subdev[i]->numeraseregions == 0) {
  639. /* current subdevice has uniform erase size */
  640. if (subdev[i]->erasesize != curr_erasesize) {
  641. /*
  642. * fill in an mtd_erase_region_info structure for the area
  643. * we have walked so far:
  644. */
  645. erase_region_p->offset = begin;
  646. erase_region_p->erasesize =
  647. curr_erasesize;
  648. tmp64 = position - begin;
  649. do_div(tmp64, curr_erasesize);
  650. erase_region_p->numblocks = tmp64;
  651. begin = position;
  652. curr_erasesize = subdev[i]->erasesize;
  653. ++erase_region_p;
  654. }
  655. position += subdev[i]->size;
  656. } else {
  657. /* current subdevice has variable erase size */
  658. int j;
  659. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  660. /* walk the list of erase regions, count any changes */
  661. if (subdev[i]->eraseregions[j].
  662. erasesize != curr_erasesize) {
  663. erase_region_p->offset = begin;
  664. erase_region_p->erasesize =
  665. curr_erasesize;
  666. tmp64 = position - begin;
  667. do_div(tmp64, curr_erasesize);
  668. erase_region_p->numblocks = tmp64;
  669. begin = position;
  670. curr_erasesize =
  671. subdev[i]->eraseregions[j].
  672. erasesize;
  673. ++erase_region_p;
  674. }
  675. position +=
  676. subdev[i]->eraseregions[j].
  677. numblocks * (uint64_t)curr_erasesize;
  678. }
  679. }
  680. }
  681. /* Now write the final entry */
  682. erase_region_p->offset = begin;
  683. erase_region_p->erasesize = curr_erasesize;
  684. tmp64 = position - begin;
  685. do_div(tmp64, curr_erasesize);
  686. erase_region_p->numblocks = tmp64;
  687. }
  688. return &concat->mtd;
  689. }