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