Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * MTD device concatenation layer
0004  *
0005  * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
0006  * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
0007  *
0008  * NAND support by Christian Gan <cgan@iders.ca>
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/sched.h>
0015 #include <linux/types.h>
0016 #include <linux/backing-dev.h>
0017 
0018 #include <linux/mtd/mtd.h>
0019 #include <linux/mtd/concat.h>
0020 
0021 #include <asm/div64.h>
0022 
0023 /*
0024  * Our storage structure:
0025  * Subdev points to an array of pointers to struct mtd_info objects
0026  * which is allocated along with this structure
0027  *
0028  */
0029 struct mtd_concat {
0030     struct mtd_info mtd;
0031     int num_subdev;
0032     struct mtd_info **subdev;
0033 };
0034 
0035 /*
0036  * how to calculate the size required for the above structure,
0037  * including the pointer array subdev points to:
0038  */
0039 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev)    \
0040     ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
0041 
0042 /*
0043  * Given a pointer to the MTD object in the mtd_concat structure,
0044  * we can retrieve the pointer to that structure with this macro.
0045  */
0046 #define CONCAT(x)  ((struct mtd_concat *)(x))
0047 
0048 /*
0049  * MTD methods which look up the relevant subdevice, translate the
0050  * effective address and pass through to the subdevice.
0051  */
0052 
0053 static int
0054 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
0055         size_t * retlen, u_char * buf)
0056 {
0057     struct mtd_concat *concat = CONCAT(mtd);
0058     int ret = 0, err;
0059     int i;
0060 
0061     for (i = 0; i < concat->num_subdev; i++) {
0062         struct mtd_info *subdev = concat->subdev[i];
0063         size_t size, retsize;
0064 
0065         if (from >= subdev->size) {
0066             /* Not destined for this subdev */
0067             size = 0;
0068             from -= subdev->size;
0069             continue;
0070         }
0071         if (from + len > subdev->size)
0072             /* First part goes into this subdev */
0073             size = subdev->size - from;
0074         else
0075             /* Entire transaction goes into this subdev */
0076             size = len;
0077 
0078         err = mtd_read(subdev, from, size, &retsize, buf);
0079 
0080         /* Save information about bitflips! */
0081         if (unlikely(err)) {
0082             if (mtd_is_eccerr(err)) {
0083                 mtd->ecc_stats.failed++;
0084                 ret = err;
0085             } else if (mtd_is_bitflip(err)) {
0086                 mtd->ecc_stats.corrected++;
0087                 /* Do not overwrite -EBADMSG !! */
0088                 if (!ret)
0089                     ret = err;
0090             } else
0091                 return err;
0092         }
0093 
0094         *retlen += retsize;
0095         len -= size;
0096         if (len == 0)
0097             return ret;
0098 
0099         buf += size;
0100         from = 0;
0101     }
0102     return -EINVAL;
0103 }
0104 
0105 static int
0106 concat_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
0107          size_t * retlen, const u_char * buf)
0108 {
0109     struct mtd_concat *concat = CONCAT(mtd);
0110     int err = -EINVAL;
0111     int i;
0112     for (i = 0; i < concat->num_subdev; i++) {
0113         struct mtd_info *subdev = concat->subdev[i];
0114         size_t size, retsize;
0115 
0116         if (to >= subdev->size) {
0117             to -= subdev->size;
0118             continue;
0119         }
0120         if (to + len > subdev->size)
0121             size = subdev->size - to;
0122         else
0123             size = len;
0124 
0125         err = mtd_panic_write(subdev, to, size, &retsize, buf);
0126         if (err == -EOPNOTSUPP) {
0127             printk(KERN_ERR "mtdconcat: Cannot write from panic without panic_write\n");
0128             return err;
0129         }
0130         if (err)
0131             break;
0132 
0133         *retlen += retsize;
0134         len -= size;
0135         if (len == 0)
0136             break;
0137 
0138         err = -EINVAL;
0139         buf += size;
0140         to = 0;
0141     }
0142     return err;
0143 }
0144 
0145 
0146 static int
0147 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
0148          size_t * retlen, const u_char * buf)
0149 {
0150     struct mtd_concat *concat = CONCAT(mtd);
0151     int err = -EINVAL;
0152     int i;
0153 
0154     for (i = 0; i < concat->num_subdev; i++) {
0155         struct mtd_info *subdev = concat->subdev[i];
0156         size_t size, retsize;
0157 
0158         if (to >= subdev->size) {
0159             size = 0;
0160             to -= subdev->size;
0161             continue;
0162         }
0163         if (to + len > subdev->size)
0164             size = subdev->size - to;
0165         else
0166             size = len;
0167 
0168         err = mtd_write(subdev, to, size, &retsize, buf);
0169         if (err)
0170             break;
0171 
0172         *retlen += retsize;
0173         len -= size;
0174         if (len == 0)
0175             break;
0176 
0177         err = -EINVAL;
0178         buf += size;
0179         to = 0;
0180     }
0181     return err;
0182 }
0183 
0184 static int
0185 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
0186         unsigned long count, loff_t to, size_t * retlen)
0187 {
0188     struct mtd_concat *concat = CONCAT(mtd);
0189     struct kvec *vecs_copy;
0190     unsigned long entry_low, entry_high;
0191     size_t total_len = 0;
0192     int i;
0193     int err = -EINVAL;
0194 
0195     /* Calculate total length of data */
0196     for (i = 0; i < count; i++)
0197         total_len += vecs[i].iov_len;
0198 
0199     /* Check alignment */
0200     if (mtd->writesize > 1) {
0201         uint64_t __to = to;
0202         if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
0203             return -EINVAL;
0204     }
0205 
0206     /* make a copy of vecs */
0207     vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
0208     if (!vecs_copy)
0209         return -ENOMEM;
0210 
0211     entry_low = 0;
0212     for (i = 0; i < concat->num_subdev; i++) {
0213         struct mtd_info *subdev = concat->subdev[i];
0214         size_t size, wsize, retsize, old_iov_len;
0215 
0216         if (to >= subdev->size) {
0217             to -= subdev->size;
0218             continue;
0219         }
0220 
0221         size = min_t(uint64_t, total_len, subdev->size - to);
0222         wsize = size; /* store for future use */
0223 
0224         entry_high = entry_low;
0225         while (entry_high < count) {
0226             if (size <= vecs_copy[entry_high].iov_len)
0227                 break;
0228             size -= vecs_copy[entry_high++].iov_len;
0229         }
0230 
0231         old_iov_len = vecs_copy[entry_high].iov_len;
0232         vecs_copy[entry_high].iov_len = size;
0233 
0234         err = mtd_writev(subdev, &vecs_copy[entry_low],
0235                  entry_high - entry_low + 1, to, &retsize);
0236 
0237         vecs_copy[entry_high].iov_len = old_iov_len - size;
0238         vecs_copy[entry_high].iov_base += size;
0239 
0240         entry_low = entry_high;
0241 
0242         if (err)
0243             break;
0244 
0245         *retlen += retsize;
0246         total_len -= wsize;
0247 
0248         if (total_len == 0)
0249             break;
0250 
0251         err = -EINVAL;
0252         to = 0;
0253     }
0254 
0255     kfree(vecs_copy);
0256     return err;
0257 }
0258 
0259 static int
0260 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
0261 {
0262     struct mtd_concat *concat = CONCAT(mtd);
0263     struct mtd_oob_ops devops = *ops;
0264     int i, err, ret = 0;
0265 
0266     ops->retlen = ops->oobretlen = 0;
0267 
0268     for (i = 0; i < concat->num_subdev; i++) {
0269         struct mtd_info *subdev = concat->subdev[i];
0270 
0271         if (from >= subdev->size) {
0272             from -= subdev->size;
0273             continue;
0274         }
0275 
0276         /* partial read ? */
0277         if (from + devops.len > subdev->size)
0278             devops.len = subdev->size - from;
0279 
0280         err = mtd_read_oob(subdev, from, &devops);
0281         ops->retlen += devops.retlen;
0282         ops->oobretlen += devops.oobretlen;
0283 
0284         /* Save information about bitflips! */
0285         if (unlikely(err)) {
0286             if (mtd_is_eccerr(err)) {
0287                 mtd->ecc_stats.failed++;
0288                 ret = err;
0289             } else if (mtd_is_bitflip(err)) {
0290                 mtd->ecc_stats.corrected++;
0291                 /* Do not overwrite -EBADMSG !! */
0292                 if (!ret)
0293                     ret = err;
0294             } else
0295                 return err;
0296         }
0297 
0298         if (devops.datbuf) {
0299             devops.len = ops->len - ops->retlen;
0300             if (!devops.len)
0301                 return ret;
0302             devops.datbuf += devops.retlen;
0303         }
0304         if (devops.oobbuf) {
0305             devops.ooblen = ops->ooblen - ops->oobretlen;
0306             if (!devops.ooblen)
0307                 return ret;
0308             devops.oobbuf += ops->oobretlen;
0309         }
0310 
0311         from = 0;
0312     }
0313     return -EINVAL;
0314 }
0315 
0316 static int
0317 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
0318 {
0319     struct mtd_concat *concat = CONCAT(mtd);
0320     struct mtd_oob_ops devops = *ops;
0321     int i, err;
0322 
0323     if (!(mtd->flags & MTD_WRITEABLE))
0324         return -EROFS;
0325 
0326     ops->retlen = ops->oobretlen = 0;
0327 
0328     for (i = 0; i < concat->num_subdev; i++) {
0329         struct mtd_info *subdev = concat->subdev[i];
0330 
0331         if (to >= subdev->size) {
0332             to -= subdev->size;
0333             continue;
0334         }
0335 
0336         /* partial write ? */
0337         if (to + devops.len > subdev->size)
0338             devops.len = subdev->size - to;
0339 
0340         err = mtd_write_oob(subdev, to, &devops);
0341         ops->retlen += devops.retlen;
0342         ops->oobretlen += devops.oobretlen;
0343         if (err)
0344             return err;
0345 
0346         if (devops.datbuf) {
0347             devops.len = ops->len - ops->retlen;
0348             if (!devops.len)
0349                 return 0;
0350             devops.datbuf += devops.retlen;
0351         }
0352         if (devops.oobbuf) {
0353             devops.ooblen = ops->ooblen - ops->oobretlen;
0354             if (!devops.ooblen)
0355                 return 0;
0356             devops.oobbuf += devops.oobretlen;
0357         }
0358         to = 0;
0359     }
0360     return -EINVAL;
0361 }
0362 
0363 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
0364 {
0365     struct mtd_concat *concat = CONCAT(mtd);
0366     struct mtd_info *subdev;
0367     int i, err;
0368     uint64_t length, offset = 0;
0369     struct erase_info *erase;
0370 
0371     /*
0372      * Check for proper erase block alignment of the to-be-erased area.
0373      * It is easier to do this based on the super device's erase
0374      * region info rather than looking at each particular sub-device
0375      * in turn.
0376      */
0377     if (!concat->mtd.numeraseregions) {
0378         /* the easy case: device has uniform erase block size */
0379         if (instr->addr & (concat->mtd.erasesize - 1))
0380             return -EINVAL;
0381         if (instr->len & (concat->mtd.erasesize - 1))
0382             return -EINVAL;
0383     } else {
0384         /* device has variable erase size */
0385         struct mtd_erase_region_info *erase_regions =
0386             concat->mtd.eraseregions;
0387 
0388         /*
0389          * Find the erase region where the to-be-erased area begins:
0390          */
0391         for (i = 0; i < concat->mtd.numeraseregions &&
0392              instr->addr >= erase_regions[i].offset; i++) ;
0393         --i;
0394 
0395         /*
0396          * Now erase_regions[i] is the region in which the
0397          * to-be-erased area begins. Verify that the starting
0398          * offset is aligned to this region's erase size:
0399          */
0400         if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
0401             return -EINVAL;
0402 
0403         /*
0404          * now find the erase region where the to-be-erased area ends:
0405          */
0406         for (; i < concat->mtd.numeraseregions &&
0407              (instr->addr + instr->len) >= erase_regions[i].offset;
0408              ++i) ;
0409         --i;
0410         /*
0411          * check if the ending offset is aligned to this region's erase size
0412          */
0413         if (i < 0 || ((instr->addr + instr->len) &
0414                     (erase_regions[i].erasesize - 1)))
0415             return -EINVAL;
0416     }
0417 
0418     /* make a local copy of instr to avoid modifying the caller's struct */
0419     erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
0420 
0421     if (!erase)
0422         return -ENOMEM;
0423 
0424     *erase = *instr;
0425     length = instr->len;
0426 
0427     /*
0428      * find the subdevice where the to-be-erased area begins, adjust
0429      * starting offset to be relative to the subdevice start
0430      */
0431     for (i = 0; i < concat->num_subdev; i++) {
0432         subdev = concat->subdev[i];
0433         if (subdev->size <= erase->addr) {
0434             erase->addr -= subdev->size;
0435             offset += subdev->size;
0436         } else {
0437             break;
0438         }
0439     }
0440 
0441     /* must never happen since size limit has been verified above */
0442     BUG_ON(i >= concat->num_subdev);
0443 
0444     /* now do the erase: */
0445     err = 0;
0446     for (; length > 0; i++) {
0447         /* loop for all subdevices affected by this request */
0448         subdev = concat->subdev[i]; /* get current subdevice */
0449 
0450         /* limit length to subdevice's size: */
0451         if (erase->addr + length > subdev->size)
0452             erase->len = subdev->size - erase->addr;
0453         else
0454             erase->len = length;
0455 
0456         length -= erase->len;
0457         if ((err = mtd_erase(subdev, erase))) {
0458             /* sanity check: should never happen since
0459              * block alignment has been checked above */
0460             BUG_ON(err == -EINVAL);
0461             if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
0462                 instr->fail_addr = erase->fail_addr + offset;
0463             break;
0464         }
0465         /*
0466          * erase->addr specifies the offset of the area to be
0467          * erased *within the current subdevice*. It can be
0468          * non-zero only the first time through this loop, i.e.
0469          * for the first subdevice where blocks need to be erased.
0470          * All the following erases must begin at the start of the
0471          * current subdevice, i.e. at offset zero.
0472          */
0473         erase->addr = 0;
0474         offset += subdev->size;
0475     }
0476     kfree(erase);
0477 
0478     return err;
0479 }
0480 
0481 static int concat_xxlock(struct mtd_info *mtd, loff_t ofs, uint64_t len,
0482              bool is_lock)
0483 {
0484     struct mtd_concat *concat = CONCAT(mtd);
0485     int i, err = -EINVAL;
0486 
0487     for (i = 0; i < concat->num_subdev; i++) {
0488         struct mtd_info *subdev = concat->subdev[i];
0489         uint64_t size;
0490 
0491         if (ofs >= subdev->size) {
0492             size = 0;
0493             ofs -= subdev->size;
0494             continue;
0495         }
0496         if (ofs + len > subdev->size)
0497             size = subdev->size - ofs;
0498         else
0499             size = len;
0500 
0501         if (is_lock)
0502             err = mtd_lock(subdev, ofs, size);
0503         else
0504             err = mtd_unlock(subdev, ofs, size);
0505         if (err)
0506             break;
0507 
0508         len -= size;
0509         if (len == 0)
0510             break;
0511 
0512         err = -EINVAL;
0513         ofs = 0;
0514     }
0515 
0516     return err;
0517 }
0518 
0519 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
0520 {
0521     return concat_xxlock(mtd, ofs, len, true);
0522 }
0523 
0524 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
0525 {
0526     return concat_xxlock(mtd, ofs, len, false);
0527 }
0528 
0529 static int concat_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
0530 {
0531     struct mtd_concat *concat = CONCAT(mtd);
0532     int i, err = -EINVAL;
0533 
0534     for (i = 0; i < concat->num_subdev; i++) {
0535         struct mtd_info *subdev = concat->subdev[i];
0536 
0537         if (ofs >= subdev->size) {
0538             ofs -= subdev->size;
0539             continue;
0540         }
0541 
0542         if (ofs + len > subdev->size)
0543             break;
0544 
0545         return mtd_is_locked(subdev, ofs, len);
0546     }
0547 
0548     return err;
0549 }
0550 
0551 static void concat_sync(struct mtd_info *mtd)
0552 {
0553     struct mtd_concat *concat = CONCAT(mtd);
0554     int i;
0555 
0556     for (i = 0; i < concat->num_subdev; i++) {
0557         struct mtd_info *subdev = concat->subdev[i];
0558         mtd_sync(subdev);
0559     }
0560 }
0561 
0562 static int concat_suspend(struct mtd_info *mtd)
0563 {
0564     struct mtd_concat *concat = CONCAT(mtd);
0565     int i, rc = 0;
0566 
0567     for (i = 0; i < concat->num_subdev; i++) {
0568         struct mtd_info *subdev = concat->subdev[i];
0569         if ((rc = mtd_suspend(subdev)) < 0)
0570             return rc;
0571     }
0572     return rc;
0573 }
0574 
0575 static void concat_resume(struct mtd_info *mtd)
0576 {
0577     struct mtd_concat *concat = CONCAT(mtd);
0578     int i;
0579 
0580     for (i = 0; i < concat->num_subdev; i++) {
0581         struct mtd_info *subdev = concat->subdev[i];
0582         mtd_resume(subdev);
0583     }
0584 }
0585 
0586 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
0587 {
0588     struct mtd_concat *concat = CONCAT(mtd);
0589     int i, res = 0;
0590 
0591     if (!mtd_can_have_bb(concat->subdev[0]))
0592         return res;
0593 
0594     for (i = 0; i < concat->num_subdev; i++) {
0595         struct mtd_info *subdev = concat->subdev[i];
0596 
0597         if (ofs >= subdev->size) {
0598             ofs -= subdev->size;
0599             continue;
0600         }
0601 
0602         res = mtd_block_isbad(subdev, ofs);
0603         break;
0604     }
0605 
0606     return res;
0607 }
0608 
0609 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
0610 {
0611     struct mtd_concat *concat = CONCAT(mtd);
0612     int i, err = -EINVAL;
0613 
0614     for (i = 0; i < concat->num_subdev; i++) {
0615         struct mtd_info *subdev = concat->subdev[i];
0616 
0617         if (ofs >= subdev->size) {
0618             ofs -= subdev->size;
0619             continue;
0620         }
0621 
0622         err = mtd_block_markbad(subdev, ofs);
0623         if (!err)
0624             mtd->ecc_stats.badblocks++;
0625         break;
0626     }
0627 
0628     return err;
0629 }
0630 
0631 /*
0632  * This function constructs a virtual MTD device by concatenating
0633  * num_devs MTD devices. A pointer to the new device object is
0634  * stored to *new_dev upon success. This function does _not_
0635  * register any devices: this is the caller's responsibility.
0636  */
0637 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],   /* subdevices to concatenate */
0638                    int num_devs,    /* number of subdevices      */
0639                    const char *name)
0640 {               /* name for the new device   */
0641     int i;
0642     size_t size;
0643     struct mtd_concat *concat;
0644     struct mtd_info *subdev_master = NULL;
0645     uint32_t max_erasesize, curr_erasesize;
0646     int num_erase_region;
0647     int max_writebufsize = 0;
0648 
0649     printk(KERN_NOTICE "Concatenating MTD devices:\n");
0650     for (i = 0; i < num_devs; i++)
0651         printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
0652     printk(KERN_NOTICE "into device \"%s\"\n", name);
0653 
0654     /* allocate the device structure */
0655     size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
0656     concat = kzalloc(size, GFP_KERNEL);
0657     if (!concat) {
0658         printk
0659             ("memory allocation error while creating concatenated device \"%s\"\n",
0660              name);
0661         return NULL;
0662     }
0663     concat->subdev = (struct mtd_info **) (concat + 1);
0664 
0665     /*
0666      * Set up the new "super" device's MTD object structure, check for
0667      * incompatibilities between the subdevices.
0668      */
0669     concat->mtd.type = subdev[0]->type;
0670     concat->mtd.flags = subdev[0]->flags;
0671     concat->mtd.size = subdev[0]->size;
0672     concat->mtd.erasesize = subdev[0]->erasesize;
0673     concat->mtd.writesize = subdev[0]->writesize;
0674 
0675     for (i = 0; i < num_devs; i++)
0676         if (max_writebufsize < subdev[i]->writebufsize)
0677             max_writebufsize = subdev[i]->writebufsize;
0678     concat->mtd.writebufsize = max_writebufsize;
0679 
0680     concat->mtd.subpage_sft = subdev[0]->subpage_sft;
0681     concat->mtd.oobsize = subdev[0]->oobsize;
0682     concat->mtd.oobavail = subdev[0]->oobavail;
0683 
0684     subdev_master = mtd_get_master(subdev[0]);
0685     if (subdev_master->_writev)
0686         concat->mtd._writev = concat_writev;
0687     if (subdev_master->_read_oob)
0688         concat->mtd._read_oob = concat_read_oob;
0689     if (subdev_master->_write_oob)
0690         concat->mtd._write_oob = concat_write_oob;
0691     if (subdev_master->_block_isbad)
0692         concat->mtd._block_isbad = concat_block_isbad;
0693     if (subdev_master->_block_markbad)
0694         concat->mtd._block_markbad = concat_block_markbad;
0695     if (subdev_master->_panic_write)
0696         concat->mtd._panic_write = concat_panic_write;
0697     if (subdev_master->_read)
0698         concat->mtd._read = concat_read;
0699     if (subdev_master->_write)
0700         concat->mtd._write = concat_write;
0701 
0702     concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
0703 
0704     concat->subdev[0] = subdev[0];
0705 
0706     for (i = 1; i < num_devs; i++) {
0707         if (concat->mtd.type != subdev[i]->type) {
0708             kfree(concat);
0709             printk("Incompatible device type on \"%s\"\n",
0710                    subdev[i]->name);
0711             return NULL;
0712         }
0713         if (concat->mtd.flags != subdev[i]->flags) {
0714             /*
0715              * Expect all flags except MTD_WRITEABLE to be
0716              * equal on all subdevices.
0717              */
0718             if ((concat->mtd.flags ^ subdev[i]->
0719                  flags) & ~MTD_WRITEABLE) {
0720                 kfree(concat);
0721                 printk("Incompatible device flags on \"%s\"\n",
0722                        subdev[i]->name);
0723                 return NULL;
0724             } else
0725                 /* if writeable attribute differs,
0726                    make super device writeable */
0727                 concat->mtd.flags |=
0728                     subdev[i]->flags & MTD_WRITEABLE;
0729         }
0730 
0731         subdev_master = mtd_get_master(subdev[i]);
0732         concat->mtd.size += subdev[i]->size;
0733         concat->mtd.ecc_stats.badblocks +=
0734             subdev[i]->ecc_stats.badblocks;
0735         if (concat->mtd.writesize   !=  subdev[i]->writesize ||
0736             concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
0737             concat->mtd.oobsize    !=  subdev[i]->oobsize ||
0738             !concat->mtd._read_oob  != !subdev_master->_read_oob ||
0739             !concat->mtd._write_oob != !subdev_master->_write_oob) {
0740             /*
0741              * Check against subdev[i] for data members, because
0742              * subdev's attributes may be different from master
0743              * mtd device. Check against subdev's master mtd
0744              * device for callbacks, because the existence of
0745              * subdev's callbacks is decided by master mtd device.
0746              */
0747             kfree(concat);
0748             printk("Incompatible OOB or ECC data on \"%s\"\n",
0749                    subdev[i]->name);
0750             return NULL;
0751         }
0752         concat->subdev[i] = subdev[i];
0753 
0754     }
0755 
0756     mtd_set_ooblayout(&concat->mtd, subdev[0]->ooblayout);
0757 
0758     concat->num_subdev = num_devs;
0759     concat->mtd.name = name;
0760 
0761     concat->mtd._erase = concat_erase;
0762     concat->mtd._sync = concat_sync;
0763     concat->mtd._lock = concat_lock;
0764     concat->mtd._unlock = concat_unlock;
0765     concat->mtd._is_locked = concat_is_locked;
0766     concat->mtd._suspend = concat_suspend;
0767     concat->mtd._resume = concat_resume;
0768 
0769     /*
0770      * Combine the erase block size info of the subdevices:
0771      *
0772      * first, walk the map of the new device and see how
0773      * many changes in erase size we have
0774      */
0775     max_erasesize = curr_erasesize = subdev[0]->erasesize;
0776     num_erase_region = 1;
0777     for (i = 0; i < num_devs; i++) {
0778         if (subdev[i]->numeraseregions == 0) {
0779             /* current subdevice has uniform erase size */
0780             if (subdev[i]->erasesize != curr_erasesize) {
0781                 /* if it differs from the last subdevice's erase size, count it */
0782                 ++num_erase_region;
0783                 curr_erasesize = subdev[i]->erasesize;
0784                 if (curr_erasesize > max_erasesize)
0785                     max_erasesize = curr_erasesize;
0786             }
0787         } else {
0788             /* current subdevice has variable erase size */
0789             int j;
0790             for (j = 0; j < subdev[i]->numeraseregions; j++) {
0791 
0792                 /* walk the list of erase regions, count any changes */
0793                 if (subdev[i]->eraseregions[j].erasesize !=
0794                     curr_erasesize) {
0795                     ++num_erase_region;
0796                     curr_erasesize =
0797                         subdev[i]->eraseregions[j].
0798                         erasesize;
0799                     if (curr_erasesize > max_erasesize)
0800                         max_erasesize = curr_erasesize;
0801                 }
0802             }
0803         }
0804     }
0805 
0806     if (num_erase_region == 1) {
0807         /*
0808          * All subdevices have the same uniform erase size.
0809          * This is easy:
0810          */
0811         concat->mtd.erasesize = curr_erasesize;
0812         concat->mtd.numeraseregions = 0;
0813     } else {
0814         uint64_t tmp64;
0815 
0816         /*
0817          * erase block size varies across the subdevices: allocate
0818          * space to store the data describing the variable erase regions
0819          */
0820         struct mtd_erase_region_info *erase_region_p;
0821         uint64_t begin, position;
0822 
0823         concat->mtd.erasesize = max_erasesize;
0824         concat->mtd.numeraseregions = num_erase_region;
0825         concat->mtd.eraseregions = erase_region_p =
0826             kmalloc_array(num_erase_region,
0827                   sizeof(struct mtd_erase_region_info),
0828                   GFP_KERNEL);
0829         if (!erase_region_p) {
0830             kfree(concat);
0831             printk
0832                 ("memory allocation error while creating erase region list"
0833                  " for device \"%s\"\n", name);
0834             return NULL;
0835         }
0836 
0837         /*
0838          * walk the map of the new device once more and fill in
0839          * in erase region info:
0840          */
0841         curr_erasesize = subdev[0]->erasesize;
0842         begin = position = 0;
0843         for (i = 0; i < num_devs; i++) {
0844             if (subdev[i]->numeraseregions == 0) {
0845                 /* current subdevice has uniform erase size */
0846                 if (subdev[i]->erasesize != curr_erasesize) {
0847                     /*
0848                      *  fill in an mtd_erase_region_info structure for the area
0849                      *  we have walked so far:
0850                      */
0851                     erase_region_p->offset = begin;
0852                     erase_region_p->erasesize =
0853                         curr_erasesize;
0854                     tmp64 = position - begin;
0855                     do_div(tmp64, curr_erasesize);
0856                     erase_region_p->numblocks = tmp64;
0857                     begin = position;
0858 
0859                     curr_erasesize = subdev[i]->erasesize;
0860                     ++erase_region_p;
0861                 }
0862                 position += subdev[i]->size;
0863             } else {
0864                 /* current subdevice has variable erase size */
0865                 int j;
0866                 for (j = 0; j < subdev[i]->numeraseregions; j++) {
0867                     /* walk the list of erase regions, count any changes */
0868                     if (subdev[i]->eraseregions[j].
0869                         erasesize != curr_erasesize) {
0870                         erase_region_p->offset = begin;
0871                         erase_region_p->erasesize =
0872                             curr_erasesize;
0873                         tmp64 = position - begin;
0874                         do_div(tmp64, curr_erasesize);
0875                         erase_region_p->numblocks = tmp64;
0876                         begin = position;
0877 
0878                         curr_erasesize =
0879                             subdev[i]->eraseregions[j].
0880                             erasesize;
0881                         ++erase_region_p;
0882                     }
0883                     position +=
0884                         subdev[i]->eraseregions[j].
0885                         numblocks * (uint64_t)curr_erasesize;
0886                 }
0887             }
0888         }
0889         /* Now write the final entry */
0890         erase_region_p->offset = begin;
0891         erase_region_p->erasesize = curr_erasesize;
0892         tmp64 = position - begin;
0893         do_div(tmp64, curr_erasesize);
0894         erase_region_p->numblocks = tmp64;
0895     }
0896 
0897     return &concat->mtd;
0898 }
0899 
0900 /* Cleans the context obtained from mtd_concat_create() */
0901 void mtd_concat_destroy(struct mtd_info *mtd)
0902 {
0903     struct mtd_concat *concat = CONCAT(mtd);
0904     if (concat->mtd.numeraseregions)
0905         kfree(concat->mtd.eraseregions);
0906     kfree(concat);
0907 }
0908 
0909 EXPORT_SYMBOL(mtd_concat_create);
0910 EXPORT_SYMBOL(mtd_concat_destroy);
0911 
0912 MODULE_LICENSE("GPL");
0913 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
0914 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");