blob: 7e075621bbf43fc62bbf42de4ae662dc9296bf8a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * MTD device concatenation layer
3 *
4 * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
5 *
6 * NAND support by Christian Gan <cgan@iders.ca>
7 *
8 * This code is GPL
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010014#include <linux/sched.h>
15#include <linux/types.h>
David Howells6e232cf2009-02-12 10:40:05 +000016#include <linux/backing-dev.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mtd/mtd.h>
19#include <linux/mtd/concat.h>
20
Andrew Morton6c8b44a2006-05-20 10:17:21 +010021#include <asm/div64.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24 * Our storage structure:
25 * Subdev points to an array of pointers to struct mtd_info objects
26 * which is allocated along with this structure
27 *
28 */
29struct mtd_concat {
30 struct mtd_info mtd;
31 int num_subdev;
32 struct mtd_info **subdev;
33};
34
35/*
36 * how to calculate the size required for the above structure,
37 * including the pointer array subdev points to:
38 */
39#define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
40 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
41
42/*
43 * Given a pointer to the MTD object in the mtd_concat structure,
44 * we can retrieve the pointer to that structure with this macro.
45 */
46#define CONCAT(x) ((struct mtd_concat *)(x))
47
Thomas Gleixner97894cd2005-11-07 11:15:26 +000048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * MTD methods which look up the relevant subdevice, translate the
50 * effective address and pass through to the subdevice.
51 */
52
53static int
54concat_read(struct mtd_info *mtd, loff_t from, size_t len,
55 size_t * retlen, u_char * buf)
56{
57 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020058 int ret = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int i;
60
61 *retlen = 0;
62
63 for (i = 0; i < concat->num_subdev; i++) {
64 struct mtd_info *subdev = concat->subdev[i];
65 size_t size, retsize;
66
67 if (from >= subdev->size) {
68 /* Not destined for this subdev */
69 size = 0;
70 from -= subdev->size;
71 continue;
72 }
73 if (from + len > subdev->size)
74 /* First part goes into this subdev */
75 size = subdev->size - from;
76 else
77 /* Entire transaction goes into this subdev */
78 size = len;
79
80 err = subdev->read(subdev, from, size, &retsize, buf);
81
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +020082 /* Save information about bitflips! */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020083 if (unlikely(err)) {
84 if (err == -EBADMSG) {
85 mtd->ecc_stats.failed++;
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +020086 ret = err;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020087 } else if (err == -EUCLEAN) {
88 mtd->ecc_stats.corrected++;
89 /* Do not overwrite -EBADMSG !! */
90 if (!ret)
91 ret = err;
92 } else
93 return err;
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +020094 }
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *retlen += retsize;
97 len -= size;
98 if (len == 0)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020099 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 buf += size;
102 from = 0;
103 }
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static int
108concat_write(struct mtd_info *mtd, loff_t to, size_t len,
109 size_t * retlen, const u_char * buf)
110{
111 struct mtd_concat *concat = CONCAT(mtd);
112 int err = -EINVAL;
113 int i;
114
115 if (!(mtd->flags & MTD_WRITEABLE))
116 return -EROFS;
117
118 *retlen = 0;
119
120 for (i = 0; i < concat->num_subdev; i++) {
121 struct mtd_info *subdev = concat->subdev[i];
122 size_t size, retsize;
123
124 if (to >= subdev->size) {
125 size = 0;
126 to -= subdev->size;
127 continue;
128 }
129 if (to + len > subdev->size)
130 size = subdev->size - to;
131 else
132 size = len;
133
134 if (!(subdev->flags & MTD_WRITEABLE))
135 err = -EROFS;
136 else
137 err = subdev->write(subdev, to, size, &retsize, buf);
138
139 if (err)
140 break;
141
142 *retlen += retsize;
143 len -= size;
144 if (len == 0)
145 break;
146
147 err = -EINVAL;
148 buf += size;
149 to = 0;
150 }
151 return err;
152}
153
154static int
Thomas Gleixner9d8522d2006-05-23 16:06:03 +0200155concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
156 unsigned long count, loff_t to, size_t * retlen)
Alexander Belyakove8d32932006-05-17 19:11:16 +0400157{
158 struct mtd_concat *concat = CONCAT(mtd);
159 struct kvec *vecs_copy;
160 unsigned long entry_low, entry_high;
161 size_t total_len = 0;
162 int i;
163 int err = -EINVAL;
164
165 if (!(mtd->flags & MTD_WRITEABLE))
166 return -EROFS;
167
168 *retlen = 0;
169
170 /* Calculate total length of data */
171 for (i = 0; i < count; i++)
172 total_len += vecs[i].iov_len;
173
174 /* Do not allow write past end of device */
175 if ((to + total_len) > mtd->size)
176 return -EINVAL;
177
178 /* Check alignment */
Joern Engel28318772006-05-22 23:18:05 +0200179 if (mtd->writesize > 1) {
David Woodhouse0bf97332007-07-23 13:07:06 +0100180 uint64_t __to = to;
Joern Engel28318772006-05-22 23:18:05 +0200181 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
Alexander Belyakove8d32932006-05-17 19:11:16 +0400182 return -EINVAL;
Andrew Morton6c8b44a2006-05-20 10:17:21 +0100183 }
Alexander Belyakove8d32932006-05-17 19:11:16 +0400184
185 /* make a copy of vecs */
Julia Lawalld80f2662010-05-15 23:23:31 +0200186 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
Alexander Belyakove8d32932006-05-17 19:11:16 +0400187 if (!vecs_copy)
188 return -ENOMEM;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400189
190 entry_low = 0;
191 for (i = 0; i < concat->num_subdev; i++) {
192 struct mtd_info *subdev = concat->subdev[i];
193 size_t size, wsize, retsize, old_iov_len;
194
195 if (to >= subdev->size) {
196 to -= subdev->size;
197 continue;
198 }
199
Adrian Hunter69423d92008-12-10 13:37:21 +0000200 size = min_t(uint64_t, total_len, subdev->size - to);
Alexander Belyakove8d32932006-05-17 19:11:16 +0400201 wsize = size; /* store for future use */
202
203 entry_high = entry_low;
204 while (entry_high < count) {
205 if (size <= vecs_copy[entry_high].iov_len)
206 break;
207 size -= vecs_copy[entry_high++].iov_len;
208 }
209
210 old_iov_len = vecs_copy[entry_high].iov_len;
211 vecs_copy[entry_high].iov_len = size;
212
213 if (!(subdev->flags & MTD_WRITEABLE))
214 err = -EROFS;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400215 else
216 err = subdev->writev(subdev, &vecs_copy[entry_low],
217 entry_high - entry_low + 1, to, &retsize);
218
219 vecs_copy[entry_high].iov_len = old_iov_len - size;
220 vecs_copy[entry_high].iov_base += size;
221
222 entry_low = entry_high;
223
224 if (err)
225 break;
226
227 *retlen += retsize;
228 total_len -= wsize;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400229
230 if (total_len == 0)
231 break;
232
233 err = -EINVAL;
234 to = 0;
235 }
236
237 kfree(vecs_copy);
238 return err;
239}
240
241static int
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200242concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200245 struct mtd_oob_ops devops = *ops;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200246 int i, err, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Vitaly Wool70145682006-11-03 18:20:38 +0300248 ops->retlen = ops->oobretlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 for (i = 0; i < concat->num_subdev; i++) {
251 struct mtd_info *subdev = concat->subdev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (from >= subdev->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 from -= subdev->size;
255 continue;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200258 /* partial read ? */
259 if (from + devops.len > subdev->size)
260 devops.len = subdev->size - from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200262 err = subdev->read_oob(subdev, from, &devops);
263 ops->retlen += devops.retlen;
Vitaly Wool70145682006-11-03 18:20:38 +0300264 ops->oobretlen += devops.oobretlen;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200265
266 /* Save information about bitflips! */
267 if (unlikely(err)) {
268 if (err == -EBADMSG) {
269 mtd->ecc_stats.failed++;
270 ret = err;
271 } else if (err == -EUCLEAN) {
272 mtd->ecc_stats.corrected++;
273 /* Do not overwrite -EBADMSG !! */
274 if (!ret)
275 ret = err;
276 } else
277 return err;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Vitaly Wool70145682006-11-03 18:20:38 +0300280 if (devops.datbuf) {
281 devops.len = ops->len - ops->retlen;
282 if (!devops.len)
283 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200284 devops.datbuf += devops.retlen;
Vitaly Wool70145682006-11-03 18:20:38 +0300285 }
286 if (devops.oobbuf) {
287 devops.ooblen = ops->ooblen - ops->oobretlen;
288 if (!devops.ooblen)
289 return ret;
290 devops.oobbuf += ops->oobretlen;
291 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 from = 0;
294 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static int
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200299concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200302 struct mtd_oob_ops devops = *ops;
303 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!(mtd->flags & MTD_WRITEABLE))
306 return -EROFS;
307
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200308 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 for (i = 0; i < concat->num_subdev; i++) {
311 struct mtd_info *subdev = concat->subdev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (to >= subdev->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 to -= subdev->size;
315 continue;
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200318 /* partial write ? */
319 if (to + devops.len > subdev->size)
320 devops.len = subdev->size - to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200322 err = subdev->write_oob(subdev, to, &devops);
323 ops->retlen += devops.retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (err)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Vitaly Wool70145682006-11-03 18:20:38 +0300327 if (devops.datbuf) {
328 devops.len = ops->len - ops->retlen;
329 if (!devops.len)
330 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200331 devops.datbuf += devops.retlen;
Vitaly Wool70145682006-11-03 18:20:38 +0300332 }
333 if (devops.oobbuf) {
334 devops.ooblen = ops->ooblen - ops->oobretlen;
335 if (!devops.ooblen)
336 return 0;
337 devops.oobbuf += devops.oobretlen;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 to = 0;
340 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344static void concat_erase_callback(struct erase_info *instr)
345{
346 wake_up((wait_queue_head_t *) instr->priv);
347}
348
349static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
350{
351 int err;
352 wait_queue_head_t waitq;
353 DECLARE_WAITQUEUE(wait, current);
354
355 /*
356 * This code was stol^H^H^H^Hinspired by mtdchar.c
357 */
358 init_waitqueue_head(&waitq);
359
360 erase->mtd = mtd;
361 erase->callback = concat_erase_callback;
362 erase->priv = (unsigned long) &waitq;
363
364 /*
365 * FIXME: Allow INTERRUPTIBLE. Which means
366 * not having the wait_queue head on the stack.
367 */
368 err = mtd->erase(mtd, erase);
369 if (!err) {
370 set_current_state(TASK_UNINTERRUPTIBLE);
371 add_wait_queue(&waitq, &wait);
372 if (erase->state != MTD_ERASE_DONE
373 && erase->state != MTD_ERASE_FAILED)
374 schedule();
375 remove_wait_queue(&waitq, &wait);
376 set_current_state(TASK_RUNNING);
377
378 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
379 }
380 return err;
381}
382
383static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
384{
385 struct mtd_concat *concat = CONCAT(mtd);
386 struct mtd_info *subdev;
387 int i, err;
Adrian Hunter69423d92008-12-10 13:37:21 +0000388 uint64_t length, offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct erase_info *erase;
390
391 if (!(mtd->flags & MTD_WRITEABLE))
392 return -EROFS;
393
394 if (instr->addr > concat->mtd.size)
395 return -EINVAL;
396
397 if (instr->len + instr->addr > concat->mtd.size)
398 return -EINVAL;
399
400 /*
401 * Check for proper erase block alignment of the to-be-erased area.
402 * It is easier to do this based on the super device's erase
403 * region info rather than looking at each particular sub-device
404 * in turn.
405 */
406 if (!concat->mtd.numeraseregions) {
407 /* the easy case: device has uniform erase block size */
408 if (instr->addr & (concat->mtd.erasesize - 1))
409 return -EINVAL;
410 if (instr->len & (concat->mtd.erasesize - 1))
411 return -EINVAL;
412 } else {
413 /* device has variable erase size */
414 struct mtd_erase_region_info *erase_regions =
415 concat->mtd.eraseregions;
416
417 /*
418 * Find the erase region where the to-be-erased area begins:
419 */
420 for (i = 0; i < concat->mtd.numeraseregions &&
421 instr->addr >= erase_regions[i].offset; i++) ;
422 --i;
423
424 /*
425 * Now erase_regions[i] is the region in which the
426 * to-be-erased area begins. Verify that the starting
427 * offset is aligned to this region's erase size:
428 */
Roel Kluinebf2e932009-09-18 12:51:49 -0700429 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -EINVAL;
431
432 /*
433 * now find the erase region where the to-be-erased area ends:
434 */
435 for (; i < concat->mtd.numeraseregions &&
436 (instr->addr + instr->len) >= erase_regions[i].offset;
437 ++i) ;
438 --i;
439 /*
440 * check if the ending offset is aligned to this region's erase size
441 */
Roel Kluinebf2e932009-09-18 12:51:49 -0700442 if (i < 0 || ((instr->addr + instr->len) &
443 (erase_regions[i].erasesize - 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -EINVAL;
445 }
446
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300447 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* make a local copy of instr to avoid modifying the caller's struct */
450 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
451
452 if (!erase)
453 return -ENOMEM;
454
455 *erase = *instr;
456 length = instr->len;
457
458 /*
459 * find the subdevice where the to-be-erased area begins, adjust
460 * starting offset to be relative to the subdevice start
461 */
462 for (i = 0; i < concat->num_subdev; i++) {
463 subdev = concat->subdev[i];
464 if (subdev->size <= erase->addr) {
465 erase->addr -= subdev->size;
466 offset += subdev->size;
467 } else {
468 break;
469 }
470 }
471
472 /* must never happen since size limit has been verified above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200473 BUG_ON(i >= concat->num_subdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* now do the erase: */
476 err = 0;
477 for (; length > 0; i++) {
478 /* loop for all subdevices affected by this request */
479 subdev = concat->subdev[i]; /* get current subdevice */
480
481 /* limit length to subdevice's size: */
482 if (erase->addr + length > subdev->size)
483 erase->len = subdev->size - erase->addr;
484 else
485 erase->len = length;
486
487 if (!(subdev->flags & MTD_WRITEABLE)) {
488 err = -EROFS;
489 break;
490 }
491 length -= erase->len;
492 if ((err = concat_dev_erase(subdev, erase))) {
493 /* sanity check: should never happen since
494 * block alignment has been checked above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200495 BUG_ON(err == -EINVAL);
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300496 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 instr->fail_addr = erase->fail_addr + offset;
498 break;
499 }
500 /*
501 * erase->addr specifies the offset of the area to be
502 * erased *within the current subdevice*. It can be
503 * non-zero only the first time through this loop, i.e.
504 * for the first subdevice where blocks need to be erased.
505 * All the following erases must begin at the start of the
506 * current subdevice, i.e. at offset zero.
507 */
508 erase->addr = 0;
509 offset += subdev->size;
510 }
511 instr->state = erase->state;
512 kfree(erase);
513 if (err)
514 return err;
515
516 if (instr->callback)
517 instr->callback(instr);
518 return 0;
519}
520
Adrian Hunter69423d92008-12-10 13:37:21 +0000521static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct mtd_concat *concat = CONCAT(mtd);
524 int i, err = -EINVAL;
525
526 if ((len + ofs) > mtd->size)
527 return -EINVAL;
528
529 for (i = 0; i < concat->num_subdev; i++) {
530 struct mtd_info *subdev = concat->subdev[i];
Adrian Hunter69423d92008-12-10 13:37:21 +0000531 uint64_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (ofs >= subdev->size) {
534 size = 0;
535 ofs -= subdev->size;
536 continue;
537 }
538 if (ofs + len > subdev->size)
539 size = subdev->size - ofs;
540 else
541 size = len;
542
543 err = subdev->lock(subdev, ofs, size);
544
545 if (err)
546 break;
547
548 len -= size;
549 if (len == 0)
550 break;
551
552 err = -EINVAL;
553 ofs = 0;
554 }
555
556 return err;
557}
558
Adrian Hunter69423d92008-12-10 13:37:21 +0000559static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct mtd_concat *concat = CONCAT(mtd);
562 int i, err = 0;
563
564 if ((len + ofs) > mtd->size)
565 return -EINVAL;
566
567 for (i = 0; i < concat->num_subdev; i++) {
568 struct mtd_info *subdev = concat->subdev[i];
Adrian Hunter69423d92008-12-10 13:37:21 +0000569 uint64_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (ofs >= subdev->size) {
572 size = 0;
573 ofs -= subdev->size;
574 continue;
575 }
576 if (ofs + len > subdev->size)
577 size = subdev->size - ofs;
578 else
579 size = len;
580
581 err = subdev->unlock(subdev, ofs, size);
582
583 if (err)
584 break;
585
586 len -= size;
587 if (len == 0)
588 break;
589
590 err = -EINVAL;
591 ofs = 0;
592 }
593
594 return err;
595}
596
597static void concat_sync(struct mtd_info *mtd)
598{
599 struct mtd_concat *concat = CONCAT(mtd);
600 int i;
601
602 for (i = 0; i < concat->num_subdev; i++) {
603 struct mtd_info *subdev = concat->subdev[i];
604 subdev->sync(subdev);
605 }
606}
607
608static int concat_suspend(struct mtd_info *mtd)
609{
610 struct mtd_concat *concat = CONCAT(mtd);
611 int i, rc = 0;
612
613 for (i = 0; i < concat->num_subdev; i++) {
614 struct mtd_info *subdev = concat->subdev[i];
615 if ((rc = subdev->suspend(subdev)) < 0)
616 return rc;
617 }
618 return rc;
619}
620
621static void concat_resume(struct mtd_info *mtd)
622{
623 struct mtd_concat *concat = CONCAT(mtd);
624 int i;
625
626 for (i = 0; i < concat->num_subdev; i++) {
627 struct mtd_info *subdev = concat->subdev[i];
628 subdev->resume(subdev);
629 }
630}
631
Alexander Belyakove8d32932006-05-17 19:11:16 +0400632static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
633{
634 struct mtd_concat *concat = CONCAT(mtd);
635 int i, res = 0;
636
637 if (!concat->subdev[0]->block_isbad)
638 return res;
639
640 if (ofs > mtd->size)
641 return -EINVAL;
642
643 for (i = 0; i < concat->num_subdev; i++) {
644 struct mtd_info *subdev = concat->subdev[i];
645
646 if (ofs >= subdev->size) {
647 ofs -= subdev->size;
648 continue;
649 }
650
651 res = subdev->block_isbad(subdev, ofs);
652 break;
653 }
654
655 return res;
656}
657
658static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
659{
660 struct mtd_concat *concat = CONCAT(mtd);
661 int i, err = -EINVAL;
662
663 if (!concat->subdev[0]->block_markbad)
664 return 0;
665
666 if (ofs > mtd->size)
667 return -EINVAL;
668
669 for (i = 0; i < concat->num_subdev; i++) {
670 struct mtd_info *subdev = concat->subdev[i];
671
672 if (ofs >= subdev->size) {
673 ofs -= subdev->size;
674 continue;
675 }
676
677 err = subdev->block_markbad(subdev, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200678 if (!err)
679 mtd->ecc_stats.badblocks++;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400680 break;
681 }
682
683 return err;
684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
David Howells6e232cf2009-02-12 10:40:05 +0000687 * try to support NOMMU mmaps on concatenated devices
688 * - we don't support subdev spanning as we can't guarantee it'll work
689 */
690static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
691 unsigned long len,
692 unsigned long offset,
693 unsigned long flags)
694{
695 struct mtd_concat *concat = CONCAT(mtd);
696 int i;
697
698 for (i = 0; i < concat->num_subdev; i++) {
699 struct mtd_info *subdev = concat->subdev[i];
700
701 if (offset >= subdev->size) {
702 offset -= subdev->size;
703 continue;
704 }
705
706 /* we've found the subdev over which the mapping will reside */
707 if (offset + len > subdev->size)
708 return (unsigned long) -EINVAL;
709
710 if (subdev->get_unmapped_area)
711 return subdev->get_unmapped_area(subdev, len, offset,
712 flags);
713
714 break;
715 }
716
717 return (unsigned long) -ENOSYS;
718}
719
720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * This function constructs a virtual MTD device by concatenating
722 * num_devs MTD devices. A pointer to the new device object is
723 * stored to *new_dev upon success. This function does _not_
724 * register any devices: this is the caller's responsibility.
725 */
726struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
727 int num_devs, /* number of subdevices */
Kay Sievers160bbab2008-12-23 10:00:14 +0000728 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{ /* name for the new device */
730 int i;
731 size_t size;
732 struct mtd_concat *concat;
David Woodhouse26cdb672008-12-10 14:08:12 +0000733 uint32_t max_erasesize, curr_erasesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int num_erase_region;
735
736 printk(KERN_NOTICE "Concatenating MTD devices:\n");
737 for (i = 0; i < num_devs; i++)
738 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
739 printk(KERN_NOTICE "into device \"%s\"\n", name);
740
741 /* allocate the device structure */
742 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
Burman Yan95b93a02006-11-15 21:10:29 +0200743 concat = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!concat) {
745 printk
746 ("memory allocation error while creating concatenated device \"%s\"\n",
747 name);
748 return NULL;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 concat->subdev = (struct mtd_info **) (concat + 1);
751
752 /*
753 * Set up the new "super" device's MTD object structure, check for
754 * incompatibilites between the subdevices.
755 */
756 concat->mtd.type = subdev[0]->type;
757 concat->mtd.flags = subdev[0]->flags;
758 concat->mtd.size = subdev[0]->size;
759 concat->mtd.erasesize = subdev[0]->erasesize;
Joern Engel28318772006-05-22 23:18:05 +0200760 concat->mtd.writesize = subdev[0]->writesize;
Chris Paulson-Ellisa2e1b832007-10-12 10:54:06 +0100761 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 concat->mtd.oobsize = subdev[0]->oobsize;
Vitaly Wool1f922672007-03-06 16:56:34 +0300763 concat->mtd.oobavail = subdev[0]->oobavail;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400764 if (subdev[0]->writev)
765 concat->mtd.writev = concat_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (subdev[0]->read_oob)
767 concat->mtd.read_oob = concat_read_oob;
768 if (subdev[0]->write_oob)
769 concat->mtd.write_oob = concat_write_oob;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400770 if (subdev[0]->block_isbad)
771 concat->mtd.block_isbad = concat_block_isbad;
772 if (subdev[0]->block_markbad)
773 concat->mtd.block_markbad = concat_block_markbad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200775 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
776
David Howells6e232cf2009-02-12 10:40:05 +0000777 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 concat->subdev[0] = subdev[0];
780
781 for (i = 1; i < num_devs; i++) {
782 if (concat->mtd.type != subdev[i]->type) {
783 kfree(concat);
784 printk("Incompatible device type on \"%s\"\n",
785 subdev[i]->name);
786 return NULL;
787 }
788 if (concat->mtd.flags != subdev[i]->flags) {
789 /*
790 * Expect all flags except MTD_WRITEABLE to be
791 * equal on all subdevices.
792 */
793 if ((concat->mtd.flags ^ subdev[i]->
794 flags) & ~MTD_WRITEABLE) {
795 kfree(concat);
796 printk("Incompatible device flags on \"%s\"\n",
797 subdev[i]->name);
798 return NULL;
799 } else
800 /* if writeable attribute differs,
801 make super device writeable */
802 concat->mtd.flags |=
803 subdev[i]->flags & MTD_WRITEABLE;
804 }
David Howells6e232cf2009-02-12 10:40:05 +0000805
806 /* only permit direct mapping if the BDIs are all the same
807 * - copy-mapping is still permitted
808 */
809 if (concat->mtd.backing_dev_info !=
810 subdev[i]->backing_dev_info)
811 concat->mtd.backing_dev_info =
812 &default_backing_dev_info;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 concat->mtd.size += subdev[i]->size;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200815 concat->mtd.ecc_stats.badblocks +=
816 subdev[i]->ecc_stats.badblocks;
Joern Engel28318772006-05-22 23:18:05 +0200817 if (concat->mtd.writesize != subdev[i]->writesize ||
Thomas Gleixner29072b92006-09-28 15:38:36 +0200818 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 concat->mtd.oobsize != subdev[i]->oobsize ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 !concat->mtd.read_oob != !subdev[i]->read_oob ||
821 !concat->mtd.write_oob != !subdev[i]->write_oob) {
822 kfree(concat);
823 printk("Incompatible OOB or ECC data on \"%s\"\n",
824 subdev[i]->name);
825 return NULL;
826 }
827 concat->subdev[i] = subdev[i];
828
829 }
830
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200831 concat->mtd.ecclayout = subdev[0]->ecclayout;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 concat->num_subdev = num_devs;
834 concat->mtd.name = name;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 concat->mtd.erase = concat_erase;
837 concat->mtd.read = concat_read;
838 concat->mtd.write = concat_write;
839 concat->mtd.sync = concat_sync;
840 concat->mtd.lock = concat_lock;
841 concat->mtd.unlock = concat_unlock;
842 concat->mtd.suspend = concat_suspend;
843 concat->mtd.resume = concat_resume;
David Howells6e232cf2009-02-12 10:40:05 +0000844 concat->mtd.get_unmapped_area = concat_get_unmapped_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /*
847 * Combine the erase block size info of the subdevices:
848 *
849 * first, walk the map of the new device and see how
850 * many changes in erase size we have
851 */
852 max_erasesize = curr_erasesize = subdev[0]->erasesize;
853 num_erase_region = 1;
854 for (i = 0; i < num_devs; i++) {
855 if (subdev[i]->numeraseregions == 0) {
856 /* current subdevice has uniform erase size */
857 if (subdev[i]->erasesize != curr_erasesize) {
858 /* if it differs from the last subdevice's erase size, count it */
859 ++num_erase_region;
860 curr_erasesize = subdev[i]->erasesize;
861 if (curr_erasesize > max_erasesize)
862 max_erasesize = curr_erasesize;
863 }
864 } else {
865 /* current subdevice has variable erase size */
866 int j;
867 for (j = 0; j < subdev[i]->numeraseregions; j++) {
868
869 /* walk the list of erase regions, count any changes */
870 if (subdev[i]->eraseregions[j].erasesize !=
871 curr_erasesize) {
872 ++num_erase_region;
873 curr_erasesize =
874 subdev[i]->eraseregions[j].
875 erasesize;
876 if (curr_erasesize > max_erasesize)
877 max_erasesize = curr_erasesize;
878 }
879 }
880 }
881 }
882
883 if (num_erase_region == 1) {
884 /*
885 * All subdevices have the same uniform erase size.
886 * This is easy:
887 */
888 concat->mtd.erasesize = curr_erasesize;
889 concat->mtd.numeraseregions = 0;
890 } else {
Adrian Hunter69423d92008-12-10 13:37:21 +0000891 uint64_t tmp64;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /*
894 * erase block size varies across the subdevices: allocate
895 * space to store the data describing the variable erase regions
896 */
897 struct mtd_erase_region_info *erase_region_p;
Adrian Hunter69423d92008-12-10 13:37:21 +0000898 uint64_t begin, position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 concat->mtd.erasesize = max_erasesize;
901 concat->mtd.numeraseregions = num_erase_region;
902 concat->mtd.eraseregions = erase_region_p =
903 kmalloc(num_erase_region *
904 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
905 if (!erase_region_p) {
906 kfree(concat);
907 printk
908 ("memory allocation error while creating erase region list"
909 " for device \"%s\"\n", name);
910 return NULL;
911 }
912
913 /*
914 * walk the map of the new device once more and fill in
915 * in erase region info:
916 */
917 curr_erasesize = subdev[0]->erasesize;
918 begin = position = 0;
919 for (i = 0; i < num_devs; i++) {
920 if (subdev[i]->numeraseregions == 0) {
921 /* current subdevice has uniform erase size */
922 if (subdev[i]->erasesize != curr_erasesize) {
923 /*
924 * fill in an mtd_erase_region_info structure for the area
925 * we have walked so far:
926 */
927 erase_region_p->offset = begin;
928 erase_region_p->erasesize =
929 curr_erasesize;
Adrian Hunter69423d92008-12-10 13:37:21 +0000930 tmp64 = position - begin;
931 do_div(tmp64, curr_erasesize);
932 erase_region_p->numblocks = tmp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 begin = position;
934
935 curr_erasesize = subdev[i]->erasesize;
936 ++erase_region_p;
937 }
938 position += subdev[i]->size;
939 } else {
940 /* current subdevice has variable erase size */
941 int j;
942 for (j = 0; j < subdev[i]->numeraseregions; j++) {
943 /* walk the list of erase regions, count any changes */
944 if (subdev[i]->eraseregions[j].
945 erasesize != curr_erasesize) {
946 erase_region_p->offset = begin;
947 erase_region_p->erasesize =
948 curr_erasesize;
Adrian Hunter69423d92008-12-10 13:37:21 +0000949 tmp64 = position - begin;
950 do_div(tmp64, curr_erasesize);
951 erase_region_p->numblocks = tmp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 begin = position;
953
954 curr_erasesize =
955 subdev[i]->eraseregions[j].
956 erasesize;
957 ++erase_region_p;
958 }
959 position +=
960 subdev[i]->eraseregions[j].
Adrian Hunter69423d92008-12-10 13:37:21 +0000961 numblocks * (uint64_t)curr_erasesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 }
964 }
965 /* Now write the final entry */
966 erase_region_p->offset = begin;
967 erase_region_p->erasesize = curr_erasesize;
Adrian Hunter69423d92008-12-10 13:37:21 +0000968 tmp64 = position - begin;
969 do_div(tmp64, curr_erasesize);
970 erase_region_p->numblocks = tmp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
973 return &concat->mtd;
974}
975
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000976/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 * This function destroys an MTD object obtained from concat_mtd_devs()
978 */
979
980void mtd_concat_destroy(struct mtd_info *mtd)
981{
982 struct mtd_concat *concat = CONCAT(mtd);
983 if (concat->mtd.numeraseregions)
984 kfree(concat->mtd.eraseregions);
985 kfree(concat);
986}
987
988EXPORT_SYMBOL(mtd_concat_create);
989EXPORT_SYMBOL(mtd_concat_destroy);
990
991MODULE_LICENSE("GPL");
992MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
993MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");