blob: 06902683bc2a7c0a71541558b76aaad032e0c578 [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
9 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +000010 * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010016#include <linux/sched.h>
17#include <linux/types.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mtd/mtd.h>
20#include <linux/mtd/concat.h>
21
Andrew Morton6c8b44a2006-05-20 10:17:21 +010022#include <asm/div64.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * Our storage structure:
26 * Subdev points to an array of pointers to struct mtd_info objects
27 * which is allocated along with this structure
28 *
29 */
30struct mtd_concat {
31 struct mtd_info mtd;
32 int num_subdev;
33 struct mtd_info **subdev;
34};
35
36/*
37 * how to calculate the size required for the above structure,
38 * including the pointer array subdev points to:
39 */
40#define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
41 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
42
43/*
44 * Given a pointer to the MTD object in the mtd_concat structure,
45 * we can retrieve the pointer to that structure with this macro.
46 */
47#define CONCAT(x) ((struct mtd_concat *)(x))
48
Thomas Gleixner97894cd2005-11-07 11:15:26 +000049/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * MTD methods which look up the relevant subdevice, translate the
51 * effective address and pass through to the subdevice.
52 */
53
54static int
55concat_read(struct mtd_info *mtd, loff_t from, size_t len,
56 size_t * retlen, u_char * buf)
57{
58 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020059 int ret = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int i;
61
62 *retlen = 0;
63
64 for (i = 0; i < concat->num_subdev; i++) {
65 struct mtd_info *subdev = concat->subdev[i];
66 size_t size, retsize;
67
68 if (from >= subdev->size) {
69 /* Not destined for this subdev */
70 size = 0;
71 from -= subdev->size;
72 continue;
73 }
74 if (from + len > subdev->size)
75 /* First part goes into this subdev */
76 size = subdev->size - from;
77 else
78 /* Entire transaction goes into this subdev */
79 size = len;
80
81 err = subdev->read(subdev, from, size, &retsize, buf);
82
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +020083 /* Save information about bitflips! */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020084 if (unlikely(err)) {
85 if (err == -EBADMSG) {
86 mtd->ecc_stats.failed++;
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +020087 ret = err;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020088 } else if (err == -EUCLEAN) {
89 mtd->ecc_stats.corrected++;
90 /* Do not overwrite -EBADMSG !! */
91 if (!ret)
92 ret = err;
93 } else
94 return err;
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +020095 }
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *retlen += retsize;
98 len -= size;
99 if (len == 0)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200100 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 buf += size;
103 from = 0;
104 }
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200105 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108static int
109concat_write(struct mtd_info *mtd, loff_t to, size_t len,
110 size_t * retlen, const u_char * buf)
111{
112 struct mtd_concat *concat = CONCAT(mtd);
113 int err = -EINVAL;
114 int i;
115
116 if (!(mtd->flags & MTD_WRITEABLE))
117 return -EROFS;
118
119 *retlen = 0;
120
121 for (i = 0; i < concat->num_subdev; i++) {
122 struct mtd_info *subdev = concat->subdev[i];
123 size_t size, retsize;
124
125 if (to >= subdev->size) {
126 size = 0;
127 to -= subdev->size;
128 continue;
129 }
130 if (to + len > subdev->size)
131 size = subdev->size - to;
132 else
133 size = len;
134
135 if (!(subdev->flags & MTD_WRITEABLE))
136 err = -EROFS;
137 else
138 err = subdev->write(subdev, to, size, &retsize, buf);
139
140 if (err)
141 break;
142
143 *retlen += retsize;
144 len -= size;
145 if (len == 0)
146 break;
147
148 err = -EINVAL;
149 buf += size;
150 to = 0;
151 }
152 return err;
153}
154
155static int
Thomas Gleixner9d8522d2006-05-23 16:06:03 +0200156concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
157 unsigned long count, loff_t to, size_t * retlen)
Alexander Belyakove8d32932006-05-17 19:11:16 +0400158{
159 struct mtd_concat *concat = CONCAT(mtd);
160 struct kvec *vecs_copy;
161 unsigned long entry_low, entry_high;
162 size_t total_len = 0;
163 int i;
164 int err = -EINVAL;
165
166 if (!(mtd->flags & MTD_WRITEABLE))
167 return -EROFS;
168
169 *retlen = 0;
170
171 /* Calculate total length of data */
172 for (i = 0; i < count; i++)
173 total_len += vecs[i].iov_len;
174
175 /* Do not allow write past end of device */
176 if ((to + total_len) > mtd->size)
177 return -EINVAL;
178
179 /* Check alignment */
Joern Engel28318772006-05-22 23:18:05 +0200180 if (mtd->writesize > 1) {
Andrew Morton6c8b44a2006-05-20 10:17:21 +0100181 loff_t __to = to;
Joern Engel28318772006-05-22 23:18:05 +0200182 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
Alexander Belyakove8d32932006-05-17 19:11:16 +0400183 return -EINVAL;
Andrew Morton6c8b44a2006-05-20 10:17:21 +0100184 }
Alexander Belyakove8d32932006-05-17 19:11:16 +0400185
186 /* make a copy of vecs */
187 vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
188 if (!vecs_copy)
189 return -ENOMEM;
190 memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
191
192 entry_low = 0;
193 for (i = 0; i < concat->num_subdev; i++) {
194 struct mtd_info *subdev = concat->subdev[i];
195 size_t size, wsize, retsize, old_iov_len;
196
197 if (to >= subdev->size) {
198 to -= subdev->size;
199 continue;
200 }
201
202 size = min(total_len, (size_t)(subdev->size - to));
203 wsize = size; /* store for future use */
204
205 entry_high = entry_low;
206 while (entry_high < count) {
207 if (size <= vecs_copy[entry_high].iov_len)
208 break;
209 size -= vecs_copy[entry_high++].iov_len;
210 }
211
212 old_iov_len = vecs_copy[entry_high].iov_len;
213 vecs_copy[entry_high].iov_len = size;
214
215 if (!(subdev->flags & MTD_WRITEABLE))
216 err = -EROFS;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400217 else
218 err = subdev->writev(subdev, &vecs_copy[entry_low],
219 entry_high - entry_low + 1, to, &retsize);
220
221 vecs_copy[entry_high].iov_len = old_iov_len - size;
222 vecs_copy[entry_high].iov_base += size;
223
224 entry_low = entry_high;
225
226 if (err)
227 break;
228
229 *retlen += retsize;
230 total_len -= wsize;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400231
232 if (total_len == 0)
233 break;
234
235 err = -EINVAL;
236 to = 0;
237 }
238
239 kfree(vecs_copy);
240 return err;
241}
242
243static int
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200244concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200247 struct mtd_oob_ops devops = *ops;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200248 int i, err, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Vitaly Wool70145682006-11-03 18:20:38 +0300250 ops->retlen = ops->oobretlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 for (i = 0; i < concat->num_subdev; i++) {
253 struct mtd_info *subdev = concat->subdev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (from >= subdev->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 from -= subdev->size;
257 continue;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200260 /* partial read ? */
261 if (from + devops.len > subdev->size)
262 devops.len = subdev->size - from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200264 err = subdev->read_oob(subdev, from, &devops);
265 ops->retlen += devops.retlen;
Vitaly Wool70145682006-11-03 18:20:38 +0300266 ops->oobretlen += devops.oobretlen;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200267
268 /* Save information about bitflips! */
269 if (unlikely(err)) {
270 if (err == -EBADMSG) {
271 mtd->ecc_stats.failed++;
272 ret = err;
273 } else if (err == -EUCLEAN) {
274 mtd->ecc_stats.corrected++;
275 /* Do not overwrite -EBADMSG !! */
276 if (!ret)
277 ret = err;
278 } else
279 return err;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Vitaly Wool70145682006-11-03 18:20:38 +0300282 if (devops.datbuf) {
283 devops.len = ops->len - ops->retlen;
284 if (!devops.len)
285 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200286 devops.datbuf += devops.retlen;
Vitaly Wool70145682006-11-03 18:20:38 +0300287 }
288 if (devops.oobbuf) {
289 devops.ooblen = ops->ooblen - ops->oobretlen;
290 if (!devops.ooblen)
291 return ret;
292 devops.oobbuf += ops->oobretlen;
293 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 from = 0;
296 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static int
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200301concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200304 struct mtd_oob_ops devops = *ops;
305 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (!(mtd->flags & MTD_WRITEABLE))
308 return -EROFS;
309
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200310 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 for (i = 0; i < concat->num_subdev; i++) {
313 struct mtd_info *subdev = concat->subdev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (to >= subdev->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 to -= subdev->size;
317 continue;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200320 /* partial write ? */
321 if (to + devops.len > subdev->size)
322 devops.len = subdev->size - to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200324 err = subdev->write_oob(subdev, to, &devops);
325 ops->retlen += devops.retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (err)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200327 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Vitaly Wool70145682006-11-03 18:20:38 +0300329 if (devops.datbuf) {
330 devops.len = ops->len - ops->retlen;
331 if (!devops.len)
332 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200333 devops.datbuf += devops.retlen;
Vitaly Wool70145682006-11-03 18:20:38 +0300334 }
335 if (devops.oobbuf) {
336 devops.ooblen = ops->ooblen - ops->oobretlen;
337 if (!devops.ooblen)
338 return 0;
339 devops.oobbuf += devops.oobretlen;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 to = 0;
342 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200343 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346static void concat_erase_callback(struct erase_info *instr)
347{
348 wake_up((wait_queue_head_t *) instr->priv);
349}
350
351static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
352{
353 int err;
354 wait_queue_head_t waitq;
355 DECLARE_WAITQUEUE(wait, current);
356
357 /*
358 * This code was stol^H^H^H^Hinspired by mtdchar.c
359 */
360 init_waitqueue_head(&waitq);
361
362 erase->mtd = mtd;
363 erase->callback = concat_erase_callback;
364 erase->priv = (unsigned long) &waitq;
365
366 /*
367 * FIXME: Allow INTERRUPTIBLE. Which means
368 * not having the wait_queue head on the stack.
369 */
370 err = mtd->erase(mtd, erase);
371 if (!err) {
372 set_current_state(TASK_UNINTERRUPTIBLE);
373 add_wait_queue(&waitq, &wait);
374 if (erase->state != MTD_ERASE_DONE
375 && erase->state != MTD_ERASE_FAILED)
376 schedule();
377 remove_wait_queue(&waitq, &wait);
378 set_current_state(TASK_RUNNING);
379
380 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
381 }
382 return err;
383}
384
385static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
386{
387 struct mtd_concat *concat = CONCAT(mtd);
388 struct mtd_info *subdev;
389 int i, err;
390 u_int32_t length, offset = 0;
391 struct erase_info *erase;
392
393 if (!(mtd->flags & MTD_WRITEABLE))
394 return -EROFS;
395
396 if (instr->addr > concat->mtd.size)
397 return -EINVAL;
398
399 if (instr->len + instr->addr > concat->mtd.size)
400 return -EINVAL;
401
402 /*
403 * Check for proper erase block alignment of the to-be-erased area.
404 * It is easier to do this based on the super device's erase
405 * region info rather than looking at each particular sub-device
406 * in turn.
407 */
408 if (!concat->mtd.numeraseregions) {
409 /* the easy case: device has uniform erase block size */
410 if (instr->addr & (concat->mtd.erasesize - 1))
411 return -EINVAL;
412 if (instr->len & (concat->mtd.erasesize - 1))
413 return -EINVAL;
414 } else {
415 /* device has variable erase size */
416 struct mtd_erase_region_info *erase_regions =
417 concat->mtd.eraseregions;
418
419 /*
420 * Find the erase region where the to-be-erased area begins:
421 */
422 for (i = 0; i < concat->mtd.numeraseregions &&
423 instr->addr >= erase_regions[i].offset; i++) ;
424 --i;
425
426 /*
427 * Now erase_regions[i] is the region in which the
428 * to-be-erased area begins. Verify that the starting
429 * offset is aligned to this region's erase size:
430 */
431 if (instr->addr & (erase_regions[i].erasesize - 1))
432 return -EINVAL;
433
434 /*
435 * now find the erase region where the to-be-erased area ends:
436 */
437 for (; i < concat->mtd.numeraseregions &&
438 (instr->addr + instr->len) >= erase_regions[i].offset;
439 ++i) ;
440 --i;
441 /*
442 * check if the ending offset is aligned to this region's erase size
443 */
444 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
445 1))
446 return -EINVAL;
447 }
448
449 instr->fail_addr = 0xffffffff;
450
451 /* make a local copy of instr to avoid modifying the caller's struct */
452 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
453
454 if (!erase)
455 return -ENOMEM;
456
457 *erase = *instr;
458 length = instr->len;
459
460 /*
461 * find the subdevice where the to-be-erased area begins, adjust
462 * starting offset to be relative to the subdevice start
463 */
464 for (i = 0; i < concat->num_subdev; i++) {
465 subdev = concat->subdev[i];
466 if (subdev->size <= erase->addr) {
467 erase->addr -= subdev->size;
468 offset += subdev->size;
469 } else {
470 break;
471 }
472 }
473
474 /* must never happen since size limit has been verified above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200475 BUG_ON(i >= concat->num_subdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* now do the erase: */
478 err = 0;
479 for (; length > 0; i++) {
480 /* loop for all subdevices affected by this request */
481 subdev = concat->subdev[i]; /* get current subdevice */
482
483 /* limit length to subdevice's size: */
484 if (erase->addr + length > subdev->size)
485 erase->len = subdev->size - erase->addr;
486 else
487 erase->len = length;
488
489 if (!(subdev->flags & MTD_WRITEABLE)) {
490 err = -EROFS;
491 break;
492 }
493 length -= erase->len;
494 if ((err = concat_dev_erase(subdev, erase))) {
495 /* sanity check: should never happen since
496 * block alignment has been checked above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200497 BUG_ON(err == -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (erase->fail_addr != 0xffffffff)
499 instr->fail_addr = erase->fail_addr + offset;
500 break;
501 }
502 /*
503 * erase->addr specifies the offset of the area to be
504 * erased *within the current subdevice*. It can be
505 * non-zero only the first time through this loop, i.e.
506 * for the first subdevice where blocks need to be erased.
507 * All the following erases must begin at the start of the
508 * current subdevice, i.e. at offset zero.
509 */
510 erase->addr = 0;
511 offset += subdev->size;
512 }
513 instr->state = erase->state;
514 kfree(erase);
515 if (err)
516 return err;
517
518 if (instr->callback)
519 instr->callback(instr);
520 return 0;
521}
522
523static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
524{
525 struct mtd_concat *concat = CONCAT(mtd);
526 int i, err = -EINVAL;
527
528 if ((len + ofs) > mtd->size)
529 return -EINVAL;
530
531 for (i = 0; i < concat->num_subdev; i++) {
532 struct mtd_info *subdev = concat->subdev[i];
533 size_t size;
534
535 if (ofs >= subdev->size) {
536 size = 0;
537 ofs -= subdev->size;
538 continue;
539 }
540 if (ofs + len > subdev->size)
541 size = subdev->size - ofs;
542 else
543 size = len;
544
545 err = subdev->lock(subdev, ofs, size);
546
547 if (err)
548 break;
549
550 len -= size;
551 if (len == 0)
552 break;
553
554 err = -EINVAL;
555 ofs = 0;
556 }
557
558 return err;
559}
560
561static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
562{
563 struct mtd_concat *concat = CONCAT(mtd);
564 int i, err = 0;
565
566 if ((len + ofs) > mtd->size)
567 return -EINVAL;
568
569 for (i = 0; i < concat->num_subdev; i++) {
570 struct mtd_info *subdev = concat->subdev[i];
571 size_t size;
572
573 if (ofs >= subdev->size) {
574 size = 0;
575 ofs -= subdev->size;
576 continue;
577 }
578 if (ofs + len > subdev->size)
579 size = subdev->size - ofs;
580 else
581 size = len;
582
583 err = subdev->unlock(subdev, ofs, size);
584
585 if (err)
586 break;
587
588 len -= size;
589 if (len == 0)
590 break;
591
592 err = -EINVAL;
593 ofs = 0;
594 }
595
596 return err;
597}
598
599static void concat_sync(struct mtd_info *mtd)
600{
601 struct mtd_concat *concat = CONCAT(mtd);
602 int i;
603
604 for (i = 0; i < concat->num_subdev; i++) {
605 struct mtd_info *subdev = concat->subdev[i];
606 subdev->sync(subdev);
607 }
608}
609
610static int concat_suspend(struct mtd_info *mtd)
611{
612 struct mtd_concat *concat = CONCAT(mtd);
613 int i, rc = 0;
614
615 for (i = 0; i < concat->num_subdev; i++) {
616 struct mtd_info *subdev = concat->subdev[i];
617 if ((rc = subdev->suspend(subdev)) < 0)
618 return rc;
619 }
620 return rc;
621}
622
623static void concat_resume(struct mtd_info *mtd)
624{
625 struct mtd_concat *concat = CONCAT(mtd);
626 int i;
627
628 for (i = 0; i < concat->num_subdev; i++) {
629 struct mtd_info *subdev = concat->subdev[i];
630 subdev->resume(subdev);
631 }
632}
633
Alexander Belyakove8d32932006-05-17 19:11:16 +0400634static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
635{
636 struct mtd_concat *concat = CONCAT(mtd);
637 int i, res = 0;
638
639 if (!concat->subdev[0]->block_isbad)
640 return res;
641
642 if (ofs > mtd->size)
643 return -EINVAL;
644
645 for (i = 0; i < concat->num_subdev; i++) {
646 struct mtd_info *subdev = concat->subdev[i];
647
648 if (ofs >= subdev->size) {
649 ofs -= subdev->size;
650 continue;
651 }
652
653 res = subdev->block_isbad(subdev, ofs);
654 break;
655 }
656
657 return res;
658}
659
660static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
661{
662 struct mtd_concat *concat = CONCAT(mtd);
663 int i, err = -EINVAL;
664
665 if (!concat->subdev[0]->block_markbad)
666 return 0;
667
668 if (ofs > mtd->size)
669 return -EINVAL;
670
671 for (i = 0; i < concat->num_subdev; i++) {
672 struct mtd_info *subdev = concat->subdev[i];
673
674 if (ofs >= subdev->size) {
675 ofs -= subdev->size;
676 continue;
677 }
678
679 err = subdev->block_markbad(subdev, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200680 if (!err)
681 mtd->ecc_stats.badblocks++;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400682 break;
683 }
684
685 return err;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*
689 * This function constructs a virtual MTD device by concatenating
690 * num_devs MTD devices. A pointer to the new device object is
691 * stored to *new_dev upon success. This function does _not_
692 * register any devices: this is the caller's responsibility.
693 */
694struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
695 int num_devs, /* number of subdevices */
696 char *name)
697{ /* name for the new device */
698 int i;
699 size_t size;
700 struct mtd_concat *concat;
701 u_int32_t max_erasesize, curr_erasesize;
702 int num_erase_region;
703
704 printk(KERN_NOTICE "Concatenating MTD devices:\n");
705 for (i = 0; i < num_devs; i++)
706 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
707 printk(KERN_NOTICE "into device \"%s\"\n", name);
708
709 /* allocate the device structure */
710 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
Burman Yan95b93a02006-11-15 21:10:29 +0200711 concat = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!concat) {
713 printk
714 ("memory allocation error while creating concatenated device \"%s\"\n",
715 name);
716 return NULL;
717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 concat->subdev = (struct mtd_info **) (concat + 1);
719
720 /*
721 * Set up the new "super" device's MTD object structure, check for
722 * incompatibilites between the subdevices.
723 */
724 concat->mtd.type = subdev[0]->type;
725 concat->mtd.flags = subdev[0]->flags;
726 concat->mtd.size = subdev[0]->size;
727 concat->mtd.erasesize = subdev[0]->erasesize;
Joern Engel28318772006-05-22 23:18:05 +0200728 concat->mtd.writesize = subdev[0]->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 concat->mtd.oobsize = subdev[0]->oobsize;
730 concat->mtd.ecctype = subdev[0]->ecctype;
731 concat->mtd.eccsize = subdev[0]->eccsize;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400732 if (subdev[0]->writev)
733 concat->mtd.writev = concat_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (subdev[0]->read_oob)
735 concat->mtd.read_oob = concat_read_oob;
736 if (subdev[0]->write_oob)
737 concat->mtd.write_oob = concat_write_oob;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400738 if (subdev[0]->block_isbad)
739 concat->mtd.block_isbad = concat_block_isbad;
740 if (subdev[0]->block_markbad)
741 concat->mtd.block_markbad = concat_block_markbad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200743 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 concat->subdev[0] = subdev[0];
746
747 for (i = 1; i < num_devs; i++) {
748 if (concat->mtd.type != subdev[i]->type) {
749 kfree(concat);
750 printk("Incompatible device type on \"%s\"\n",
751 subdev[i]->name);
752 return NULL;
753 }
754 if (concat->mtd.flags != subdev[i]->flags) {
755 /*
756 * Expect all flags except MTD_WRITEABLE to be
757 * equal on all subdevices.
758 */
759 if ((concat->mtd.flags ^ subdev[i]->
760 flags) & ~MTD_WRITEABLE) {
761 kfree(concat);
762 printk("Incompatible device flags on \"%s\"\n",
763 subdev[i]->name);
764 return NULL;
765 } else
766 /* if writeable attribute differs,
767 make super device writeable */
768 concat->mtd.flags |=
769 subdev[i]->flags & MTD_WRITEABLE;
770 }
771 concat->mtd.size += subdev[i]->size;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200772 concat->mtd.ecc_stats.badblocks +=
773 subdev[i]->ecc_stats.badblocks;
Joern Engel28318772006-05-22 23:18:05 +0200774 if (concat->mtd.writesize != subdev[i]->writesize ||
Thomas Gleixner29072b92006-09-28 15:38:36 +0200775 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 concat->mtd.oobsize != subdev[i]->oobsize ||
777 concat->mtd.ecctype != subdev[i]->ecctype ||
778 concat->mtd.eccsize != subdev[i]->eccsize ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 !concat->mtd.read_oob != !subdev[i]->read_oob ||
780 !concat->mtd.write_oob != !subdev[i]->write_oob) {
781 kfree(concat);
782 printk("Incompatible OOB or ECC data on \"%s\"\n",
783 subdev[i]->name);
784 return NULL;
785 }
786 concat->subdev[i] = subdev[i];
787
788 }
789
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200790 concat->mtd.ecclayout = subdev[0]->ecclayout;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 concat->num_subdev = num_devs;
793 concat->mtd.name = name;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 concat->mtd.erase = concat_erase;
796 concat->mtd.read = concat_read;
797 concat->mtd.write = concat_write;
798 concat->mtd.sync = concat_sync;
799 concat->mtd.lock = concat_lock;
800 concat->mtd.unlock = concat_unlock;
801 concat->mtd.suspend = concat_suspend;
802 concat->mtd.resume = concat_resume;
803
804 /*
805 * Combine the erase block size info of the subdevices:
806 *
807 * first, walk the map of the new device and see how
808 * many changes in erase size we have
809 */
810 max_erasesize = curr_erasesize = subdev[0]->erasesize;
811 num_erase_region = 1;
812 for (i = 0; i < num_devs; i++) {
813 if (subdev[i]->numeraseregions == 0) {
814 /* current subdevice has uniform erase size */
815 if (subdev[i]->erasesize != curr_erasesize) {
816 /* if it differs from the last subdevice's erase size, count it */
817 ++num_erase_region;
818 curr_erasesize = subdev[i]->erasesize;
819 if (curr_erasesize > max_erasesize)
820 max_erasesize = curr_erasesize;
821 }
822 } else {
823 /* current subdevice has variable erase size */
824 int j;
825 for (j = 0; j < subdev[i]->numeraseregions; j++) {
826
827 /* walk the list of erase regions, count any changes */
828 if (subdev[i]->eraseregions[j].erasesize !=
829 curr_erasesize) {
830 ++num_erase_region;
831 curr_erasesize =
832 subdev[i]->eraseregions[j].
833 erasesize;
834 if (curr_erasesize > max_erasesize)
835 max_erasesize = curr_erasesize;
836 }
837 }
838 }
839 }
840
841 if (num_erase_region == 1) {
842 /*
843 * All subdevices have the same uniform erase size.
844 * This is easy:
845 */
846 concat->mtd.erasesize = curr_erasesize;
847 concat->mtd.numeraseregions = 0;
848 } else {
849 /*
850 * erase block size varies across the subdevices: allocate
851 * space to store the data describing the variable erase regions
852 */
853 struct mtd_erase_region_info *erase_region_p;
854 u_int32_t begin, position;
855
856 concat->mtd.erasesize = max_erasesize;
857 concat->mtd.numeraseregions = num_erase_region;
858 concat->mtd.eraseregions = erase_region_p =
859 kmalloc(num_erase_region *
860 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
861 if (!erase_region_p) {
862 kfree(concat);
863 printk
864 ("memory allocation error while creating erase region list"
865 " for device \"%s\"\n", name);
866 return NULL;
867 }
868
869 /*
870 * walk the map of the new device once more and fill in
871 * in erase region info:
872 */
873 curr_erasesize = subdev[0]->erasesize;
874 begin = position = 0;
875 for (i = 0; i < num_devs; i++) {
876 if (subdev[i]->numeraseregions == 0) {
877 /* current subdevice has uniform erase size */
878 if (subdev[i]->erasesize != curr_erasesize) {
879 /*
880 * fill in an mtd_erase_region_info structure for the area
881 * we have walked so far:
882 */
883 erase_region_p->offset = begin;
884 erase_region_p->erasesize =
885 curr_erasesize;
886 erase_region_p->numblocks =
887 (position - begin) / curr_erasesize;
888 begin = position;
889
890 curr_erasesize = subdev[i]->erasesize;
891 ++erase_region_p;
892 }
893 position += subdev[i]->size;
894 } else {
895 /* current subdevice has variable erase size */
896 int j;
897 for (j = 0; j < subdev[i]->numeraseregions; j++) {
898 /* walk the list of erase regions, count any changes */
899 if (subdev[i]->eraseregions[j].
900 erasesize != curr_erasesize) {
901 erase_region_p->offset = begin;
902 erase_region_p->erasesize =
903 curr_erasesize;
904 erase_region_p->numblocks =
905 (position -
906 begin) / curr_erasesize;
907 begin = position;
908
909 curr_erasesize =
910 subdev[i]->eraseregions[j].
911 erasesize;
912 ++erase_region_p;
913 }
914 position +=
915 subdev[i]->eraseregions[j].
916 numblocks * curr_erasesize;
917 }
918 }
919 }
920 /* Now write the final entry */
921 erase_region_p->offset = begin;
922 erase_region_p->erasesize = curr_erasesize;
923 erase_region_p->numblocks = (position - begin) / curr_erasesize;
924 }
925
926 return &concat->mtd;
927}
928
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000929/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * This function destroys an MTD object obtained from concat_mtd_devs()
931 */
932
933void mtd_concat_destroy(struct mtd_info *mtd)
934{
935 struct mtd_concat *concat = CONCAT(mtd);
936 if (concat->mtd.numeraseregions)
937 kfree(concat->mtd.eraseregions);
938 kfree(concat);
939}
940
941EXPORT_SYMBOL(mtd_concat_create);
942EXPORT_SYMBOL(mtd_concat_destroy);
943
944MODULE_LICENSE("GPL");
945MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
946MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");