mtdconcat.c 18 KB

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