Blame view

drivers/mtd/mtdconcat.c 21.5 KB
fd534e9b5   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
  /*
   * MTD device concatenation layer
   *
a1452a377   David Woodhouse   mtd: Update copyr...
5
6
   * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
   * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
   *
   * NAND support by Christian Gan <cgan@iders.ca>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
  #include <linux/kernel.h>
15fdc52f3   Thomas Gleixner   [MTD] Tidy up Tim...
11
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
  #include <linux/slab.h>
15fdc52f3   Thomas Gleixner   [MTD] Tidy up Tim...
13
14
  #include <linux/sched.h>
  #include <linux/types.h>
6e232cfce   David Howells   NOMMU: Add suppor...
15
  #include <linux/backing-dev.h>
15fdc52f3   Thomas Gleixner   [MTD] Tidy up Tim...
16

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  #include <linux/mtd/mtd.h>
  #include <linux/mtd/concat.h>
6c8b44abc   Andrew Morton   [MTD] Avoid 64-bi...
19
  #include <asm/div64.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  /*
   * Our storage structure:
   * Subdev points to an array of pointers to struct mtd_info objects
   * which is allocated along with this structure
   *
   */
  struct mtd_concat {
  	struct mtd_info mtd;
  	int num_subdev;
  	struct mtd_info **subdev;
  };
  
  /*
   * how to calculate the size required for the above structure,
   * including the pointer array subdev points to:
   */
  #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev)	\
  	((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
  
  /*
   * Given a pointer to the MTD object in the mtd_concat structure,
   * we can retrieve the pointer to that structure with this macro.
   */
  #define CONCAT(x)  ((struct mtd_concat *)(x))
97894cda5   Thomas Gleixner   [MTD] core: Clean...
44
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
   * MTD methods which look up the relevant subdevice, translate the
   * effective address and pass through to the subdevice.
   */
  
  static int
  concat_read(struct mtd_info *mtd, loff_t from, size_t len,
  	    size_t * retlen, u_char * buf)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
54
  	int ret = 0, err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
  		size_t size, retsize;
  
  		if (from >= subdev->size) {
  			/* Not destined for this subdev */
  			size = 0;
  			from -= subdev->size;
  			continue;
  		}
  		if (from + len > subdev->size)
  			/* First part goes into this subdev */
  			size = subdev->size - from;
  		else
  			/* Entire transaction goes into this subdev */
  			size = len;
329ad399a   Artem Bityutskiy   mtd: introduce mt...
72
  		err = mtd_read(subdev, from, size, &retsize, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73

9a1fcdfd4   Thomas Gleixner   [MTD] NAND Signal...
74
  		/* Save information about bitflips! */
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
75
  		if (unlikely(err)) {
d57f40544   Brian Norris   mtd: utilize `mtd...
76
  			if (mtd_is_eccerr(err)) {
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
77
  				mtd->ecc_stats.failed++;
9a1fcdfd4   Thomas Gleixner   [MTD] NAND Signal...
78
  				ret = err;
d57f40544   Brian Norris   mtd: utilize `mtd...
79
  			} else if (mtd_is_bitflip(err)) {
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
80
81
82
83
84
85
  				mtd->ecc_stats.corrected++;
  				/* Do not overwrite -EBADMSG !! */
  				if (!ret)
  					ret = err;
  			} else
  				return err;
9a1fcdfd4   Thomas Gleixner   [MTD] NAND Signal...
86
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
89
  		*retlen += retsize;
  		len -= size;
  		if (len == 0)
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
90
  			return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
  		buf += size;
  		from = 0;
  	}
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
95
  	return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
  }
  
  static int
3fbe507eb   Matt Weber   mtd: mtdconcat: m...
99
100
101
102
103
104
105
106
107
108
109
  concat_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  	     size_t * retlen, const u_char * buf)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int err = -EINVAL;
  	int i;
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
  		size_t size, retsize;
  
  		if (to >= subdev->size) {
3fbe507eb   Matt Weber   mtd: mtdconcat: m...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  			to -= subdev->size;
  			continue;
  		}
  		if (to + len > subdev->size)
  			size = subdev->size - to;
  		else
  			size = len;
  
  		err = mtd_panic_write(subdev, to, size, &retsize, buf);
  		if (err == -EOPNOTSUPP) {
  			printk(KERN_ERR "mtdconcat: Cannot write from panic without panic_write
  ");
  			return err;
  		}
  		if (err)
  			break;
  
  		*retlen += retsize;
  		len -= size;
  		if (len == 0)
  			break;
  
  		err = -EINVAL;
  		buf += size;
  		to = 0;
  	}
  	return err;
  }
  
  
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
143
144
145
146
  concat_write(struct mtd_info *mtd, loff_t to, size_t len,
  	     size_t * retlen, const u_char * buf)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int err = -EINVAL;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
150
151
152
153
154
155
156
157
158
159
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
  		size_t size, retsize;
  
  		if (to >= subdev->size) {
  			size = 0;
  			to -= subdev->size;
  			continue;
  		}
  		if (to + len > subdev->size)
  			size = subdev->size - to;
  		else
  			size = len;
664addc24   Artem Bityutskiy   mtd: remove R/O c...
160
  		err = mtd_write(subdev, to, size, &retsize, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  		if (err)
  			break;
  
  		*retlen += retsize;
  		len -= size;
  		if (len == 0)
  			break;
  
  		err = -EINVAL;
  		buf += size;
  		to = 0;
  	}
  	return err;
  }
  
  static int
9d8522df3   Thomas Gleixner   [MTD] Remove nand...
177
178
  concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
  		unsigned long count, loff_t to, size_t * retlen)
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
179
180
181
182
183
184
185
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	struct kvec *vecs_copy;
  	unsigned long entry_low, entry_high;
  	size_t total_len = 0;
  	int i;
  	int err = -EINVAL;
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
186
187
188
  	/* Calculate total length of data */
  	for (i = 0; i < count; i++)
  		total_len += vecs[i].iov_len;
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
189
  	/* Check alignment */
28318776a   Joern Engel   [MTD] Introduce w...
190
  	if (mtd->writesize > 1) {
0bf9733d0   David Woodhouse   [MTD] Fix do_div(...
191
  		uint64_t __to = to;
28318776a   Joern Engel   [MTD] Introduce w...
192
  		if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
193
  			return -EINVAL;
6c8b44abc   Andrew Morton   [MTD] Avoid 64-bi...
194
  	}
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
195
196
  
  	/* make a copy of vecs */
d80f2666b   Julia Lawall   drivers/mtd: Use ...
197
  	vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
198
199
  	if (!vecs_copy)
  		return -ENOMEM;
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
200
201
202
203
204
205
206
207
208
209
  
  	entry_low = 0;
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
  		size_t size, wsize, retsize, old_iov_len;
  
  		if (to >= subdev->size) {
  			to -= subdev->size;
  			continue;
  		}
69423d99f   Adrian Hunter   [MTD] update inte...
210
  		size = min_t(uint64_t, total_len, subdev->size - to);
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
211
212
213
214
215
216
217
218
219
220
221
  		wsize = size; /* store for future use */
  
  		entry_high = entry_low;
  		while (entry_high < count) {
  			if (size <= vecs_copy[entry_high].iov_len)
  				break;
  			size -= vecs_copy[entry_high++].iov_len;
  		}
  
  		old_iov_len = vecs_copy[entry_high].iov_len;
  		vecs_copy[entry_high].iov_len = size;
664addc24   Artem Bityutskiy   mtd: remove R/O c...
222
223
  		err = mtd_writev(subdev, &vecs_copy[entry_low],
  				 entry_high - entry_low + 1, to, &retsize);
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
224
225
226
227
228
229
230
231
232
233
234
  
  		vecs_copy[entry_high].iov_len = old_iov_len - size;
  		vecs_copy[entry_high].iov_base += size;
  
  		entry_low = entry_high;
  
  		if (err)
  			break;
  
  		*retlen += retsize;
  		total_len -= wsize;
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
235
236
237
238
239
240
241
242
243
244
245
246
247
  
  		if (total_len == 0)
  			break;
  
  		err = -EINVAL;
  		to = 0;
  	}
  
  	kfree(vecs_copy);
  	return err;
  }
  
  static int
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
248
  concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
  {
  	struct mtd_concat *concat = CONCAT(mtd);
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
251
  	struct mtd_oob_ops devops = *ops;
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
252
  	int i, err, ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253

7014568ba   Vitaly Wool   [MTD] [NAND] remo...
254
  	ops->retlen = ops->oobretlen = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
257
  
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
  
  		if (from >= subdev->size) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
261
262
  			from -= subdev->size;
  			continue;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263

8593fbc68   Thomas Gleixner   [MTD] Rework the ...
264
265
266
  		/* partial read ? */
  		if (from + devops.len > subdev->size)
  			devops.len = subdev->size - from;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267

fd2819bbc   Artem Bityutskiy   mtd: introduce mt...
268
  		err = mtd_read_oob(subdev, from, &devops);
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
269
  		ops->retlen += devops.retlen;
7014568ba   Vitaly Wool   [MTD] [NAND] remo...
270
  		ops->oobretlen += devops.oobretlen;
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
271
272
273
  
  		/* Save information about bitflips! */
  		if (unlikely(err)) {
d57f40544   Brian Norris   mtd: utilize `mtd...
274
  			if (mtd_is_eccerr(err)) {
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
275
276
  				mtd->ecc_stats.failed++;
  				ret = err;
d57f40544   Brian Norris   mtd: utilize `mtd...
277
  			} else if (mtd_is_bitflip(err)) {
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
278
279
280
281
282
283
284
  				mtd->ecc_stats.corrected++;
  				/* Do not overwrite -EBADMSG !! */
  				if (!ret)
  					ret = err;
  			} else
  				return err;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285

7014568ba   Vitaly Wool   [MTD] [NAND] remo...
286
287
288
289
  		if (devops.datbuf) {
  			devops.len = ops->len - ops->retlen;
  			if (!devops.len)
  				return ret;
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
290
  			devops.datbuf += devops.retlen;
7014568ba   Vitaly Wool   [MTD] [NAND] remo...
291
292
293
294
295
296
297
  		}
  		if (devops.oobbuf) {
  			devops.ooblen = ops->ooblen - ops->oobretlen;
  			if (!devops.ooblen)
  				return ret;
  			devops.oobbuf += ops->oobretlen;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
  		from = 0;
  	}
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
301
  	return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
303
304
  }
  
  static int
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
305
  concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
  {
  	struct mtd_concat *concat = CONCAT(mtd);
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
308
309
  	struct mtd_oob_ops devops = *ops;
  	int i, err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310
311
312
  
  	if (!(mtd->flags & MTD_WRITEABLE))
  		return -EROFS;
431e1ecab   Felix Radensky   mtd: mtdconcat: f...
313
  	ops->retlen = ops->oobretlen = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
  
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
  
  		if (to >= subdev->size) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
  			to -= subdev->size;
  			continue;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322

8593fbc68   Thomas Gleixner   [MTD] Rework the ...
323
324
325
  		/* partial write ? */
  		if (to + devops.len > subdev->size)
  			devops.len = subdev->size - to;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326

a2cc5ba07   Artem Bityutskiy   mtd: introduce mt...
327
  		err = mtd_write_oob(subdev, to, &devops);
d164ea326   Niklas Cassel   mtd: concat: set ...
328
329
  		ops->retlen += devops.retlen;
  		ops->oobretlen += devops.oobretlen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  		if (err)
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
331
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332

7014568ba   Vitaly Wool   [MTD] [NAND] remo...
333
334
335
336
  		if (devops.datbuf) {
  			devops.len = ops->len - ops->retlen;
  			if (!devops.len)
  				return 0;
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
337
  			devops.datbuf += devops.retlen;
7014568ba   Vitaly Wool   [MTD] [NAND] remo...
338
339
340
341
342
343
344
  		}
  		if (devops.oobbuf) {
  			devops.ooblen = ops->ooblen - ops->oobretlen;
  			if (!devops.ooblen)
  				return 0;
  			devops.oobbuf += devops.oobretlen;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
  		to = 0;
  	}
8593fbc68   Thomas Gleixner   [MTD] Rework the ...
347
  	return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
350
351
352
353
  static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	struct mtd_info *subdev;
  	int i, err;
69423d99f   Adrian Hunter   [MTD] update inte...
354
  	uint64_t length, offset = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
  	struct erase_info *erase;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  	/*
  	 * Check for proper erase block alignment of the to-be-erased area.
  	 * It is easier to do this based on the super device's erase
  	 * region info rather than looking at each particular sub-device
  	 * in turn.
  	 */
  	if (!concat->mtd.numeraseregions) {
  		/* the easy case: device has uniform erase block size */
  		if (instr->addr & (concat->mtd.erasesize - 1))
  			return -EINVAL;
  		if (instr->len & (concat->mtd.erasesize - 1))
  			return -EINVAL;
  	} else {
  		/* device has variable erase size */
  		struct mtd_erase_region_info *erase_regions =
  		    concat->mtd.eraseregions;
  
  		/*
  		 * Find the erase region where the to-be-erased area begins:
  		 */
  		for (i = 0; i < concat->mtd.numeraseregions &&
  		     instr->addr >= erase_regions[i].offset; i++) ;
  		--i;
  
  		/*
  		 * Now erase_regions[i] is the region in which the
  		 * to-be-erased area begins. Verify that the starting
  		 * offset is aligned to this region's erase size:
  		 */
ebf2e9303   Roel Kluin   mtd: mtdconcat: p...
385
  		if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
388
389
390
391
392
393
394
395
396
397
  			return -EINVAL;
  
  		/*
  		 * now find the erase region where the to-be-erased area ends:
  		 */
  		for (; i < concat->mtd.numeraseregions &&
  		     (instr->addr + instr->len) >= erase_regions[i].offset;
  		     ++i) ;
  		--i;
  		/*
  		 * check if the ending offset is aligned to this region's erase size
  		 */
ebf2e9303   Roel Kluin   mtd: mtdconcat: p...
398
399
  		if (i < 0 || ((instr->addr + instr->len) &
  					(erase_regions[i].erasesize - 1)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
  			return -EINVAL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  	/* make a local copy of instr to avoid modifying the caller's struct */
  	erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
  
  	if (!erase)
  		return -ENOMEM;
  
  	*erase = *instr;
  	length = instr->len;
  
  	/*
  	 * find the subdevice where the to-be-erased area begins, adjust
  	 * starting offset to be relative to the subdevice start
  	 */
  	for (i = 0; i < concat->num_subdev; i++) {
  		subdev = concat->subdev[i];
  		if (subdev->size <= erase->addr) {
  			erase->addr -= subdev->size;
  			offset += subdev->size;
  		} else {
  			break;
  		}
  	}
  
  	/* must never happen since size limit has been verified above */
373ebfbf1   Eric Sesterhenn   BUG_ON() Conversi...
426
  	BUG_ON(i >= concat->num_subdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
428
429
430
431
432
433
434
435
436
437
438
  
  	/* now do the erase: */
  	err = 0;
  	for (; length > 0; i++) {
  		/* loop for all subdevices affected by this request */
  		subdev = concat->subdev[i];	/* get current subdevice */
  
  		/* limit length to subdevice's size: */
  		if (erase->addr + length > subdev->size)
  			erase->len = subdev->size - erase->addr;
  		else
  			erase->len = length;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439
  		length -= erase->len;
884cfd902   Boris Brezillon   mtd: Stop assumin...
440
  		if ((err = mtd_erase(subdev, erase))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441
442
  			/* sanity check: should never happen since
  			 * block alignment has been checked above */
373ebfbf1   Eric Sesterhenn   BUG_ON() Conversi...
443
  			BUG_ON(err == -EINVAL);
bb0eb217c   Adrian Hunter   [MTD] Define and ...
444
  			if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  				instr->fail_addr = erase->fail_addr + offset;
  			break;
  		}
  		/*
  		 * erase->addr specifies the offset of the area to be
  		 * erased *within the current subdevice*. It can be
  		 * non-zero only the first time through this loop, i.e.
  		 * for the first subdevice where blocks need to be erased.
  		 * All the following erases must begin at the start of the
  		 * current subdevice, i.e. at offset zero.
  		 */
  		erase->addr = 0;
  		offset += subdev->size;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
  	kfree(erase);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460

884cfd902   Boris Brezillon   mtd: Stop assumin...
461
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
  }
6a08a2f12   Chris Packham   mtd: concat: refa...
463
464
  static int concat_xxlock(struct mtd_info *mtd, loff_t ofs, uint64_t len,
  			 bool is_lock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
466
467
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int i, err = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
69423d99f   Adrian Hunter   [MTD] update inte...
470
  		uint64_t size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
473
474
475
476
477
478
479
480
  
  		if (ofs >= subdev->size) {
  			size = 0;
  			ofs -= subdev->size;
  			continue;
  		}
  		if (ofs + len > subdev->size)
  			size = subdev->size - ofs;
  		else
  			size = len;
6a08a2f12   Chris Packham   mtd: concat: refa...
481
482
483
484
  		if (is_lock)
  			err = mtd_lock(subdev, ofs, size);
  		else
  			err = mtd_unlock(subdev, ofs, size);
381345652   Artem Bityutskiy   mtd: do not use m...
485
486
  		if (err)
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
489
490
491
492
493
494
495
496
497
  
  		len -= size;
  		if (len == 0)
  			break;
  
  		err = -EINVAL;
  		ofs = 0;
  	}
  
  	return err;
  }
6a08a2f12   Chris Packham   mtd: concat: refa...
498
  static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
  {
6a08a2f12   Chris Packham   mtd: concat: refa...
500
501
  	return concat_xxlock(mtd, ofs, len, true);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502

6a08a2f12   Chris Packham   mtd: concat: refa...
503
504
505
  static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  {
  	return concat_xxlock(mtd, ofs, len, false);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
  }
3bb4bba79   Chris Packham   mtd: concat: impl...
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  static int concat_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int i, err = -EINVAL;
  
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
  
  		if (ofs >= subdev->size) {
  			ofs -= subdev->size;
  			continue;
  		}
  
  		if (ofs + len > subdev->size)
  			break;
  
  		return mtd_is_locked(subdev, ofs, len);
  	}
  
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
529
530
531
532
533
534
  static void concat_sync(struct mtd_info *mtd)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int i;
  
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
85f2f2a80   Artem Bityutskiy   mtd: introduce mt...
535
  		mtd_sync(subdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
536
537
538
539
540
541
542
543
544
545
  	}
  }
  
  static int concat_suspend(struct mtd_info *mtd)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int i, rc = 0;
  
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
3fe4bae88   Artem Bityutskiy   mtd: introduce mt...
546
  		if ((rc = mtd_suspend(subdev)) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
549
550
551
552
553
554
555
556
557
558
  			return rc;
  	}
  	return rc;
  }
  
  static void concat_resume(struct mtd_info *mtd)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int i;
  
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
ead995f8d   Artem Bityutskiy   mtd: introduce mt...
559
  		mtd_resume(subdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
  	}
  }
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
562
563
564
565
  static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int i, res = 0;
8f461a730   Artem Bityutskiy   mtd: introduce mt...
566
  	if (!mtd_can_have_bb(concat->subdev[0]))
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
567
  		return res;
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
568
569
570
571
572
573
574
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
  
  		if (ofs >= subdev->size) {
  			ofs -= subdev->size;
  			continue;
  		}
7086c19d0   Artem Bityutskiy   mtd: introduce mt...
575
  		res = mtd_block_isbad(subdev, ofs);
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
576
577
578
579
580
581
582
583
584
585
  		break;
  	}
  
  	return res;
  }
  
  static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	int i, err = -EINVAL;
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
586
587
588
589
590
591
592
  	for (i = 0; i < concat->num_subdev; i++) {
  		struct mtd_info *subdev = concat->subdev[i];
  
  		if (ofs >= subdev->size) {
  			ofs -= subdev->size;
  			continue;
  		}
5942ddbc5   Artem Bityutskiy   mtd: introduce mt...
593
  		err = mtd_block_markbad(subdev, ofs);
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
594
595
  		if (!err)
  			mtd->ecc_stats.badblocks++;
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
596
597
598
599
600
  		break;
  	}
  
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
601
602
603
604
605
606
607
608
  /*
   * This function constructs a virtual MTD device by concatenating
   * num_devs MTD devices. A pointer to the new device object is
   * stored to *new_dev upon success. This function does _not_
   * register any devices: this is the caller's responsibility.
   */
  struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],	/* subdevices to concatenate */
  				   int num_devs,	/* number of subdevices      */
160bbab30   Kay Sievers   [MTD] struct devi...
609
  				   const char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
611
612
613
  {				/* name for the new device   */
  	int i;
  	size_t size;
  	struct mtd_concat *concat;
26cdb67c7   David Woodhouse   [MTD] Remove more...
614
  	uint32_t max_erasesize, curr_erasesize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
615
  	int num_erase_region;
771df6194   Holger Brunck   mtd: adapt writeb...
616
  	int max_writebufsize = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617
618
619
620
621
622
623
624
625
626
627
  
  	printk(KERN_NOTICE "Concatenating MTD devices:
  ");
  	for (i = 0; i < num_devs; i++)
  		printk(KERN_NOTICE "(%d): \"%s\"
  ", i, subdev[i]->name);
  	printk(KERN_NOTICE "into device \"%s\"
  ", name);
  
  	/* allocate the device structure */
  	size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
95b93a0cd   Burman Yan   [MTD] replace kma...
628
  	concat = kzalloc(size, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
630
631
632
633
634
635
  	if (!concat) {
  		printk
  		    ("memory allocation error while creating concatenated device \"%s\"
  ",
  		     name);
  		return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
636
637
638
639
  	concat->subdev = (struct mtd_info **) (concat + 1);
  
  	/*
  	 * Set up the new "super" device's MTD object structure, check for
92394b5c2   Brian Norris   mtd: spelling fixes
640
  	 * incompatibilities between the subdevices.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
643
644
645
  	 */
  	concat->mtd.type = subdev[0]->type;
  	concat->mtd.flags = subdev[0]->flags;
  	concat->mtd.size = subdev[0]->size;
  	concat->mtd.erasesize = subdev[0]->erasesize;
28318776a   Joern Engel   [MTD] Introduce w...
646
  	concat->mtd.writesize = subdev[0]->writesize;
771df6194   Holger Brunck   mtd: adapt writeb...
647
648
649
650
651
  
  	for (i = 0; i < num_devs; i++)
  		if (max_writebufsize < subdev[i]->writebufsize)
  			max_writebufsize = subdev[i]->writebufsize;
  	concat->mtd.writebufsize = max_writebufsize;
a2e1b833d   Chris Paulson-Ellis   [MTD] fix mtdconc...
652
  	concat->mtd.subpage_sft = subdev[0]->subpage_sft;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
  	concat->mtd.oobsize = subdev[0]->oobsize;
1f92267c5   Vitaly Wool   [MTD] [NAND] make...
654
  	concat->mtd.oobavail = subdev[0]->oobavail;
3c3c10bba   Artem Bityutskiy   mtd: add leading ...
655
656
657
658
659
660
661
662
663
664
  	if (subdev[0]->_writev)
  		concat->mtd._writev = concat_writev;
  	if (subdev[0]->_read_oob)
  		concat->mtd._read_oob = concat_read_oob;
  	if (subdev[0]->_write_oob)
  		concat->mtd._write_oob = concat_write_oob;
  	if (subdev[0]->_block_isbad)
  		concat->mtd._block_isbad = concat_block_isbad;
  	if (subdev[0]->_block_markbad)
  		concat->mtd._block_markbad = concat_block_markbad;
3fbe507eb   Matt Weber   mtd: mtdconcat: m...
665
666
  	if (subdev[0]->_panic_write)
  		concat->mtd._panic_write = concat_panic_write;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
667

f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
668
  	concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
  	concat->subdev[0] = subdev[0];
  
  	for (i = 1; i < num_devs; i++) {
  		if (concat->mtd.type != subdev[i]->type) {
  			kfree(concat);
  			printk("Incompatible device type on \"%s\"
  ",
  			       subdev[i]->name);
  			return NULL;
  		}
  		if (concat->mtd.flags != subdev[i]->flags) {
  			/*
  			 * Expect all flags except MTD_WRITEABLE to be
  			 * equal on all subdevices.
  			 */
  			if ((concat->mtd.flags ^ subdev[i]->
  			     flags) & ~MTD_WRITEABLE) {
  				kfree(concat);
  				printk("Incompatible device flags on \"%s\"
  ",
  				       subdev[i]->name);
  				return NULL;
  			} else
  				/* if writeable attribute differs,
  				   make super device writeable */
  				concat->mtd.flags |=
  				    subdev[i]->flags & MTD_WRITEABLE;
  		}
6e232cfce   David Howells   NOMMU: Add suppor...
697

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698
  		concat->mtd.size += subdev[i]->size;
f1a28c028   Thomas Gleixner   [MTD] NAND Expose...
699
700
  		concat->mtd.ecc_stats.badblocks +=
  			subdev[i]->ecc_stats.badblocks;
28318776a   Joern Engel   [MTD] Introduce w...
701
  		if (concat->mtd.writesize   !=  subdev[i]->writesize ||
29072b960   Thomas Gleixner   [MTD] NAND: add s...
702
  		    concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
  		    concat->mtd.oobsize    !=  subdev[i]->oobsize ||
3c3c10bba   Artem Bityutskiy   mtd: add leading ...
704
705
  		    !concat->mtd._read_oob  != !subdev[i]->_read_oob ||
  		    !concat->mtd._write_oob != !subdev[i]->_write_oob) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
707
708
709
710
711
712
713
714
  			kfree(concat);
  			printk("Incompatible OOB or ECC data on \"%s\"
  ",
  			       subdev[i]->name);
  			return NULL;
  		}
  		concat->subdev[i] = subdev[i];
  
  	}
adbbc3bc8   Boris Brezillon   mtd: create an mt...
715
  	mtd_set_ooblayout(&concat->mtd, subdev[0]->ooblayout);
e8d32937d   Alexander Belyakov   MTD: mtdconcat NA...
716

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
718
  	concat->num_subdev = num_devs;
  	concat->mtd.name = name;
3c3c10bba   Artem Bityutskiy   mtd: add leading ...
719
720
721
722
723
724
  	concat->mtd._erase = concat_erase;
  	concat->mtd._read = concat_read;
  	concat->mtd._write = concat_write;
  	concat->mtd._sync = concat_sync;
  	concat->mtd._lock = concat_lock;
  	concat->mtd._unlock = concat_unlock;
3bb4bba79   Chris Packham   mtd: concat: impl...
725
  	concat->mtd._is_locked = concat_is_locked;
3c3c10bba   Artem Bityutskiy   mtd: add leading ...
726
727
  	concat->mtd._suspend = concat_suspend;
  	concat->mtd._resume = concat_resume;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  
  	/*
  	 * Combine the erase block size info of the subdevices:
  	 *
  	 * first, walk the map of the new device and see how
  	 * many changes in erase size we have
  	 */
  	max_erasesize = curr_erasesize = subdev[0]->erasesize;
  	num_erase_region = 1;
  	for (i = 0; i < num_devs; i++) {
  		if (subdev[i]->numeraseregions == 0) {
  			/* current subdevice has uniform erase size */
  			if (subdev[i]->erasesize != curr_erasesize) {
  				/* if it differs from the last subdevice's erase size, count it */
  				++num_erase_region;
  				curr_erasesize = subdev[i]->erasesize;
  				if (curr_erasesize > max_erasesize)
  					max_erasesize = curr_erasesize;
  			}
  		} else {
  			/* current subdevice has variable erase size */
  			int j;
  			for (j = 0; j < subdev[i]->numeraseregions; j++) {
  
  				/* walk the list of erase regions, count any changes */
  				if (subdev[i]->eraseregions[j].erasesize !=
  				    curr_erasesize) {
  					++num_erase_region;
  					curr_erasesize =
  					    subdev[i]->eraseregions[j].
  					    erasesize;
  					if (curr_erasesize > max_erasesize)
  						max_erasesize = curr_erasesize;
  				}
  			}
  		}
  	}
  
  	if (num_erase_region == 1) {
  		/*
  		 * All subdevices have the same uniform erase size.
  		 * This is easy:
  		 */
  		concat->mtd.erasesize = curr_erasesize;
  		concat->mtd.numeraseregions = 0;
  	} else {
69423d99f   Adrian Hunter   [MTD] update inte...
774
  		uint64_t tmp64;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
776
777
778
779
  		/*
  		 * erase block size varies across the subdevices: allocate
  		 * space to store the data describing the variable erase regions
  		 */
  		struct mtd_erase_region_info *erase_region_p;
69423d99f   Adrian Hunter   [MTD] update inte...
780
  		uint64_t begin, position;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
782
783
784
  
  		concat->mtd.erasesize = max_erasesize;
  		concat->mtd.numeraseregions = num_erase_region;
  		concat->mtd.eraseregions = erase_region_p =
6da2ec560   Kees Cook   treewide: kmalloc...
785
786
787
  		    kmalloc_array(num_erase_region,
  				  sizeof(struct mtd_erase_region_info),
  				  GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
  		if (!erase_region_p) {
  			kfree(concat);
  			printk
  			    ("memory allocation error while creating erase region list"
  			     " for device \"%s\"
  ", name);
  			return NULL;
  		}
  
  		/*
  		 * walk the map of the new device once more and fill in
  		 * in erase region info:
  		 */
  		curr_erasesize = subdev[0]->erasesize;
  		begin = position = 0;
  		for (i = 0; i < num_devs; i++) {
  			if (subdev[i]->numeraseregions == 0) {
  				/* current subdevice has uniform erase size */
  				if (subdev[i]->erasesize != curr_erasesize) {
  					/*
  					 *  fill in an mtd_erase_region_info structure for the area
  					 *  we have walked so far:
  					 */
  					erase_region_p->offset = begin;
  					erase_region_p->erasesize =
  					    curr_erasesize;
69423d99f   Adrian Hunter   [MTD] update inte...
814
815
816
  					tmp64 = position - begin;
  					do_div(tmp64, curr_erasesize);
  					erase_region_p->numblocks = tmp64;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
  					begin = position;
  
  					curr_erasesize = subdev[i]->erasesize;
  					++erase_region_p;
  				}
  				position += subdev[i]->size;
  			} else {
  				/* current subdevice has variable erase size */
  				int j;
  				for (j = 0; j < subdev[i]->numeraseregions; j++) {
  					/* walk the list of erase regions, count any changes */
  					if (subdev[i]->eraseregions[j].
  					    erasesize != curr_erasesize) {
  						erase_region_p->offset = begin;
  						erase_region_p->erasesize =
  						    curr_erasesize;
69423d99f   Adrian Hunter   [MTD] update inte...
833
834
835
  						tmp64 = position - begin;
  						do_div(tmp64, curr_erasesize);
  						erase_region_p->numblocks = tmp64;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
836
837
838
839
840
841
842
843
844
  						begin = position;
  
  						curr_erasesize =
  						    subdev[i]->eraseregions[j].
  						    erasesize;
  						++erase_region_p;
  					}
  					position +=
  					    subdev[i]->eraseregions[j].
69423d99f   Adrian Hunter   [MTD] update inte...
845
  					    numblocks * (uint64_t)curr_erasesize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
847
848
849
850
851
  				}
  			}
  		}
  		/* Now write the final entry */
  		erase_region_p->offset = begin;
  		erase_region_p->erasesize = curr_erasesize;
69423d99f   Adrian Hunter   [MTD] update inte...
852
853
854
  		tmp64 = position - begin;
  		do_div(tmp64, curr_erasesize);
  		erase_region_p->numblocks = tmp64;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
857
858
  	}
  
  	return &concat->mtd;
  }
0dcf25727   Miquel Raynal   mtd: concat: Fix ...
859
  /* Cleans the context obtained from mtd_concat_create() */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
860
861
862
863
864
865
866
867
868
869
870
871
872
873
  void mtd_concat_destroy(struct mtd_info *mtd)
  {
  	struct mtd_concat *concat = CONCAT(mtd);
  	if (concat->mtd.numeraseregions)
  		kfree(concat->mtd.eraseregions);
  	kfree(concat);
  }
  
  EXPORT_SYMBOL(mtd_concat_create);
  EXPORT_SYMBOL(mtd_concat_destroy);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");