blob: a6fcee2713b06b2e14fb042b543af9b70267db82 [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);
59 int err = -EINVAL;
60 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
83 if (err)
84 break;
85
86 *retlen += retsize;
87 len -= size;
88 if (len == 0)
89 break;
90
91 err = -EINVAL;
92 buf += size;
93 from = 0;
94 }
95 return err;
96}
97
98static int
99concat_write(struct mtd_info *mtd, loff_t to, size_t len,
100 size_t * retlen, const u_char * buf)
101{
102 struct mtd_concat *concat = CONCAT(mtd);
103 int err = -EINVAL;
104 int i;
105
106 if (!(mtd->flags & MTD_WRITEABLE))
107 return -EROFS;
108
109 *retlen = 0;
110
111 for (i = 0; i < concat->num_subdev; i++) {
112 struct mtd_info *subdev = concat->subdev[i];
113 size_t size, retsize;
114
115 if (to >= subdev->size) {
116 size = 0;
117 to -= subdev->size;
118 continue;
119 }
120 if (to + len > subdev->size)
121 size = subdev->size - to;
122 else
123 size = len;
124
125 if (!(subdev->flags & MTD_WRITEABLE))
126 err = -EROFS;
127 else
128 err = subdev->write(subdev, to, size, &retsize, buf);
129
130 if (err)
131 break;
132
133 *retlen += retsize;
134 len -= size;
135 if (len == 0)
136 break;
137
138 err = -EINVAL;
139 buf += size;
140 to = 0;
141 }
142 return err;
143}
144
145static int
146concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
147 size_t * retlen, u_char * buf, u_char * eccbuf,
148 struct nand_oobinfo *oobsel)
149{
150 struct mtd_concat *concat = CONCAT(mtd);
151 int err = -EINVAL;
152 int i;
153
154 *retlen = 0;
155
156 for (i = 0; i < concat->num_subdev; i++) {
157 struct mtd_info *subdev = concat->subdev[i];
158 size_t size, retsize;
159
160 if (from >= subdev->size) {
161 /* Not destined for this subdev */
162 size = 0;
163 from -= subdev->size;
164 continue;
165 }
166
167 if (from + len > subdev->size)
168 /* First part goes into this subdev */
169 size = subdev->size - from;
170 else
171 /* Entire transaction goes into this subdev */
172 size = len;
173
174 if (subdev->read_ecc)
175 err = subdev->read_ecc(subdev, from, size,
176 &retsize, buf, eccbuf, oobsel);
177 else
178 err = -EINVAL;
179
180 if (err)
181 break;
182
183 *retlen += retsize;
184 len -= size;
185 if (len == 0)
186 break;
187
188 err = -EINVAL;
189 buf += size;
190 if (eccbuf) {
191 eccbuf += subdev->oobsize;
192 /* in nand.c at least, eccbufs are
193 tagged with 2 (int)eccstatus'; we
194 must account for these */
195 eccbuf += 2 * (sizeof (int));
196 }
197 from = 0;
198 }
199 return err;
200}
201
202static int
203concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
204 size_t * retlen, const u_char * buf, u_char * eccbuf,
205 struct nand_oobinfo *oobsel)
206{
207 struct mtd_concat *concat = CONCAT(mtd);
208 int err = -EINVAL;
209 int i;
210
211 if (!(mtd->flags & MTD_WRITEABLE))
212 return -EROFS;
213
214 *retlen = 0;
215
216 for (i = 0; i < concat->num_subdev; i++) {
217 struct mtd_info *subdev = concat->subdev[i];
218 size_t size, retsize;
219
220 if (to >= subdev->size) {
221 size = 0;
222 to -= subdev->size;
223 continue;
224 }
225 if (to + len > subdev->size)
226 size = subdev->size - to;
227 else
228 size = len;
229
230 if (!(subdev->flags & MTD_WRITEABLE))
231 err = -EROFS;
232 else if (subdev->write_ecc)
233 err = subdev->write_ecc(subdev, to, size,
234 &retsize, buf, eccbuf, oobsel);
235 else
236 err = -EINVAL;
237
238 if (err)
239 break;
240
241 *retlen += retsize;
242 len -= size;
243 if (len == 0)
244 break;
245
246 err = -EINVAL;
247 buf += size;
248 if (eccbuf)
249 eccbuf += subdev->oobsize;
250 to = 0;
251 }
252 return err;
253}
254
255static int
Thomas Gleixner9d8522d2006-05-23 16:06:03 +0200256concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
257 unsigned long count, loff_t to, size_t * retlen)
Alexander Belyakove8d32932006-05-17 19:11:16 +0400258{
259 struct mtd_concat *concat = CONCAT(mtd);
260 struct kvec *vecs_copy;
261 unsigned long entry_low, entry_high;
262 size_t total_len = 0;
263 int i;
264 int err = -EINVAL;
265
266 if (!(mtd->flags & MTD_WRITEABLE))
267 return -EROFS;
268
269 *retlen = 0;
270
271 /* Calculate total length of data */
272 for (i = 0; i < count; i++)
273 total_len += vecs[i].iov_len;
274
275 /* Do not allow write past end of device */
276 if ((to + total_len) > mtd->size)
277 return -EINVAL;
278
279 /* Check alignment */
Joern Engel28318772006-05-22 23:18:05 +0200280 if (mtd->writesize > 1) {
Andrew Morton6c8b44a2006-05-20 10:17:21 +0100281 loff_t __to = to;
Joern Engel28318772006-05-22 23:18:05 +0200282 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
Alexander Belyakove8d32932006-05-17 19:11:16 +0400283 return -EINVAL;
Andrew Morton6c8b44a2006-05-20 10:17:21 +0100284 }
Alexander Belyakove8d32932006-05-17 19:11:16 +0400285
286 /* make a copy of vecs */
287 vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
288 if (!vecs_copy)
289 return -ENOMEM;
290 memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
291
292 entry_low = 0;
293 for (i = 0; i < concat->num_subdev; i++) {
294 struct mtd_info *subdev = concat->subdev[i];
295 size_t size, wsize, retsize, old_iov_len;
296
297 if (to >= subdev->size) {
298 to -= subdev->size;
299 continue;
300 }
301
302 size = min(total_len, (size_t)(subdev->size - to));
303 wsize = size; /* store for future use */
304
305 entry_high = entry_low;
306 while (entry_high < count) {
307 if (size <= vecs_copy[entry_high].iov_len)
308 break;
309 size -= vecs_copy[entry_high++].iov_len;
310 }
311
312 old_iov_len = vecs_copy[entry_high].iov_len;
313 vecs_copy[entry_high].iov_len = size;
314
315 if (!(subdev->flags & MTD_WRITEABLE))
316 err = -EROFS;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400317 else
318 err = subdev->writev(subdev, &vecs_copy[entry_low],
319 entry_high - entry_low + 1, to, &retsize);
320
321 vecs_copy[entry_high].iov_len = old_iov_len - size;
322 vecs_copy[entry_high].iov_base += size;
323
324 entry_low = entry_high;
325
326 if (err)
327 break;
328
329 *retlen += retsize;
330 total_len -= wsize;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400331
332 if (total_len == 0)
333 break;
334
335 err = -EINVAL;
336 to = 0;
337 }
338
339 kfree(vecs_copy);
340 return err;
341}
342
343static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
345 size_t * retlen, u_char * buf)
346{
347 struct mtd_concat *concat = CONCAT(mtd);
348 int err = -EINVAL;
349 int i;
350
351 *retlen = 0;
352
353 for (i = 0; i < concat->num_subdev; i++) {
354 struct mtd_info *subdev = concat->subdev[i];
355 size_t size, retsize;
356
357 if (from >= subdev->size) {
358 /* Not destined for this subdev */
359 size = 0;
360 from -= subdev->size;
361 continue;
362 }
363 if (from + len > subdev->size)
364 /* First part goes into this subdev */
365 size = subdev->size - from;
366 else
367 /* Entire transaction goes into this subdev */
368 size = len;
369
370 if (subdev->read_oob)
371 err = subdev->read_oob(subdev, from, size,
372 &retsize, buf);
373 else
374 err = -EINVAL;
375
376 if (err)
377 break;
378
379 *retlen += retsize;
380 len -= size;
381 if (len == 0)
382 break;
383
384 err = -EINVAL;
385 buf += size;
386 from = 0;
387 }
388 return err;
389}
390
391static int
392concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
393 size_t * retlen, const u_char * buf)
394{
395 struct mtd_concat *concat = CONCAT(mtd);
396 int err = -EINVAL;
397 int i;
398
399 if (!(mtd->flags & MTD_WRITEABLE))
400 return -EROFS;
401
402 *retlen = 0;
403
404 for (i = 0; i < concat->num_subdev; i++) {
405 struct mtd_info *subdev = concat->subdev[i];
406 size_t size, retsize;
407
408 if (to >= subdev->size) {
409 size = 0;
410 to -= subdev->size;
411 continue;
412 }
413 if (to + len > subdev->size)
414 size = subdev->size - to;
415 else
416 size = len;
417
418 if (!(subdev->flags & MTD_WRITEABLE))
419 err = -EROFS;
420 else if (subdev->write_oob)
421 err = subdev->write_oob(subdev, to, size, &retsize,
422 buf);
423 else
424 err = -EINVAL;
425
426 if (err)
427 break;
428
429 *retlen += retsize;
430 len -= size;
431 if (len == 0)
432 break;
433
434 err = -EINVAL;
435 buf += size;
436 to = 0;
437 }
438 return err;
439}
440
441static void concat_erase_callback(struct erase_info *instr)
442{
443 wake_up((wait_queue_head_t *) instr->priv);
444}
445
446static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
447{
448 int err;
449 wait_queue_head_t waitq;
450 DECLARE_WAITQUEUE(wait, current);
451
452 /*
453 * This code was stol^H^H^H^Hinspired by mtdchar.c
454 */
455 init_waitqueue_head(&waitq);
456
457 erase->mtd = mtd;
458 erase->callback = concat_erase_callback;
459 erase->priv = (unsigned long) &waitq;
460
461 /*
462 * FIXME: Allow INTERRUPTIBLE. Which means
463 * not having the wait_queue head on the stack.
464 */
465 err = mtd->erase(mtd, erase);
466 if (!err) {
467 set_current_state(TASK_UNINTERRUPTIBLE);
468 add_wait_queue(&waitq, &wait);
469 if (erase->state != MTD_ERASE_DONE
470 && erase->state != MTD_ERASE_FAILED)
471 schedule();
472 remove_wait_queue(&waitq, &wait);
473 set_current_state(TASK_RUNNING);
474
475 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
476 }
477 return err;
478}
479
480static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
481{
482 struct mtd_concat *concat = CONCAT(mtd);
483 struct mtd_info *subdev;
484 int i, err;
485 u_int32_t length, offset = 0;
486 struct erase_info *erase;
487
488 if (!(mtd->flags & MTD_WRITEABLE))
489 return -EROFS;
490
491 if (instr->addr > concat->mtd.size)
492 return -EINVAL;
493
494 if (instr->len + instr->addr > concat->mtd.size)
495 return -EINVAL;
496
497 /*
498 * Check for proper erase block alignment of the to-be-erased area.
499 * It is easier to do this based on the super device's erase
500 * region info rather than looking at each particular sub-device
501 * in turn.
502 */
503 if (!concat->mtd.numeraseregions) {
504 /* the easy case: device has uniform erase block size */
505 if (instr->addr & (concat->mtd.erasesize - 1))
506 return -EINVAL;
507 if (instr->len & (concat->mtd.erasesize - 1))
508 return -EINVAL;
509 } else {
510 /* device has variable erase size */
511 struct mtd_erase_region_info *erase_regions =
512 concat->mtd.eraseregions;
513
514 /*
515 * Find the erase region where the to-be-erased area begins:
516 */
517 for (i = 0; i < concat->mtd.numeraseregions &&
518 instr->addr >= erase_regions[i].offset; i++) ;
519 --i;
520
521 /*
522 * Now erase_regions[i] is the region in which the
523 * to-be-erased area begins. Verify that the starting
524 * offset is aligned to this region's erase size:
525 */
526 if (instr->addr & (erase_regions[i].erasesize - 1))
527 return -EINVAL;
528
529 /*
530 * now find the erase region where the to-be-erased area ends:
531 */
532 for (; i < concat->mtd.numeraseregions &&
533 (instr->addr + instr->len) >= erase_regions[i].offset;
534 ++i) ;
535 --i;
536 /*
537 * check if the ending offset is aligned to this region's erase size
538 */
539 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
540 1))
541 return -EINVAL;
542 }
543
544 instr->fail_addr = 0xffffffff;
545
546 /* make a local copy of instr to avoid modifying the caller's struct */
547 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
548
549 if (!erase)
550 return -ENOMEM;
551
552 *erase = *instr;
553 length = instr->len;
554
555 /*
556 * find the subdevice where the to-be-erased area begins, adjust
557 * starting offset to be relative to the subdevice start
558 */
559 for (i = 0; i < concat->num_subdev; i++) {
560 subdev = concat->subdev[i];
561 if (subdev->size <= erase->addr) {
562 erase->addr -= subdev->size;
563 offset += subdev->size;
564 } else {
565 break;
566 }
567 }
568
569 /* must never happen since size limit has been verified above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200570 BUG_ON(i >= concat->num_subdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* now do the erase: */
573 err = 0;
574 for (; length > 0; i++) {
575 /* loop for all subdevices affected by this request */
576 subdev = concat->subdev[i]; /* get current subdevice */
577
578 /* limit length to subdevice's size: */
579 if (erase->addr + length > subdev->size)
580 erase->len = subdev->size - erase->addr;
581 else
582 erase->len = length;
583
584 if (!(subdev->flags & MTD_WRITEABLE)) {
585 err = -EROFS;
586 break;
587 }
588 length -= erase->len;
589 if ((err = concat_dev_erase(subdev, erase))) {
590 /* sanity check: should never happen since
591 * block alignment has been checked above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200592 BUG_ON(err == -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (erase->fail_addr != 0xffffffff)
594 instr->fail_addr = erase->fail_addr + offset;
595 break;
596 }
597 /*
598 * erase->addr specifies the offset of the area to be
599 * erased *within the current subdevice*. It can be
600 * non-zero only the first time through this loop, i.e.
601 * for the first subdevice where blocks need to be erased.
602 * All the following erases must begin at the start of the
603 * current subdevice, i.e. at offset zero.
604 */
605 erase->addr = 0;
606 offset += subdev->size;
607 }
608 instr->state = erase->state;
609 kfree(erase);
610 if (err)
611 return err;
612
613 if (instr->callback)
614 instr->callback(instr);
615 return 0;
616}
617
618static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
619{
620 struct mtd_concat *concat = CONCAT(mtd);
621 int i, err = -EINVAL;
622
623 if ((len + ofs) > mtd->size)
624 return -EINVAL;
625
626 for (i = 0; i < concat->num_subdev; i++) {
627 struct mtd_info *subdev = concat->subdev[i];
628 size_t size;
629
630 if (ofs >= subdev->size) {
631 size = 0;
632 ofs -= subdev->size;
633 continue;
634 }
635 if (ofs + len > subdev->size)
636 size = subdev->size - ofs;
637 else
638 size = len;
639
640 err = subdev->lock(subdev, ofs, size);
641
642 if (err)
643 break;
644
645 len -= size;
646 if (len == 0)
647 break;
648
649 err = -EINVAL;
650 ofs = 0;
651 }
652
653 return err;
654}
655
656static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
657{
658 struct mtd_concat *concat = CONCAT(mtd);
659 int i, err = 0;
660
661 if ((len + ofs) > mtd->size)
662 return -EINVAL;
663
664 for (i = 0; i < concat->num_subdev; i++) {
665 struct mtd_info *subdev = concat->subdev[i];
666 size_t size;
667
668 if (ofs >= subdev->size) {
669 size = 0;
670 ofs -= subdev->size;
671 continue;
672 }
673 if (ofs + len > subdev->size)
674 size = subdev->size - ofs;
675 else
676 size = len;
677
678 err = subdev->unlock(subdev, ofs, size);
679
680 if (err)
681 break;
682
683 len -= size;
684 if (len == 0)
685 break;
686
687 err = -EINVAL;
688 ofs = 0;
689 }
690
691 return err;
692}
693
694static void concat_sync(struct mtd_info *mtd)
695{
696 struct mtd_concat *concat = CONCAT(mtd);
697 int i;
698
699 for (i = 0; i < concat->num_subdev; i++) {
700 struct mtd_info *subdev = concat->subdev[i];
701 subdev->sync(subdev);
702 }
703}
704
705static int concat_suspend(struct mtd_info *mtd)
706{
707 struct mtd_concat *concat = CONCAT(mtd);
708 int i, rc = 0;
709
710 for (i = 0; i < concat->num_subdev; i++) {
711 struct mtd_info *subdev = concat->subdev[i];
712 if ((rc = subdev->suspend(subdev)) < 0)
713 return rc;
714 }
715 return rc;
716}
717
718static void concat_resume(struct mtd_info *mtd)
719{
720 struct mtd_concat *concat = CONCAT(mtd);
721 int i;
722
723 for (i = 0; i < concat->num_subdev; i++) {
724 struct mtd_info *subdev = concat->subdev[i];
725 subdev->resume(subdev);
726 }
727}
728
Alexander Belyakove8d32932006-05-17 19:11:16 +0400729static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
730{
731 struct mtd_concat *concat = CONCAT(mtd);
732 int i, res = 0;
733
734 if (!concat->subdev[0]->block_isbad)
735 return res;
736
737 if (ofs > mtd->size)
738 return -EINVAL;
739
740 for (i = 0; i < concat->num_subdev; i++) {
741 struct mtd_info *subdev = concat->subdev[i];
742
743 if (ofs >= subdev->size) {
744 ofs -= subdev->size;
745 continue;
746 }
747
748 res = subdev->block_isbad(subdev, ofs);
749 break;
750 }
751
752 return res;
753}
754
755static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
756{
757 struct mtd_concat *concat = CONCAT(mtd);
758 int i, err = -EINVAL;
759
760 if (!concat->subdev[0]->block_markbad)
761 return 0;
762
763 if (ofs > mtd->size)
764 return -EINVAL;
765
766 for (i = 0; i < concat->num_subdev; i++) {
767 struct mtd_info *subdev = concat->subdev[i];
768
769 if (ofs >= subdev->size) {
770 ofs -= subdev->size;
771 continue;
772 }
773
774 err = subdev->block_markbad(subdev, ofs);
775 break;
776 }
777
778 return err;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781/*
782 * This function constructs a virtual MTD device by concatenating
783 * num_devs MTD devices. A pointer to the new device object is
784 * stored to *new_dev upon success. This function does _not_
785 * register any devices: this is the caller's responsibility.
786 */
787struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
788 int num_devs, /* number of subdevices */
789 char *name)
790{ /* name for the new device */
791 int i;
792 size_t size;
793 struct mtd_concat *concat;
794 u_int32_t max_erasesize, curr_erasesize;
795 int num_erase_region;
796
797 printk(KERN_NOTICE "Concatenating MTD devices:\n");
798 for (i = 0; i < num_devs; i++)
799 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
800 printk(KERN_NOTICE "into device \"%s\"\n", name);
801
802 /* allocate the device structure */
803 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
804 concat = kmalloc(size, GFP_KERNEL);
805 if (!concat) {
806 printk
807 ("memory allocation error while creating concatenated device \"%s\"\n",
808 name);
809 return NULL;
810 }
811 memset(concat, 0, size);
812 concat->subdev = (struct mtd_info **) (concat + 1);
813
814 /*
815 * Set up the new "super" device's MTD object structure, check for
816 * incompatibilites between the subdevices.
817 */
818 concat->mtd.type = subdev[0]->type;
819 concat->mtd.flags = subdev[0]->flags;
820 concat->mtd.size = subdev[0]->size;
821 concat->mtd.erasesize = subdev[0]->erasesize;
Joern Engel28318772006-05-22 23:18:05 +0200822 concat->mtd.writesize = subdev[0]->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 concat->mtd.oobsize = subdev[0]->oobsize;
824 concat->mtd.ecctype = subdev[0]->ecctype;
825 concat->mtd.eccsize = subdev[0]->eccsize;
826 if (subdev[0]->read_ecc)
827 concat->mtd.read_ecc = concat_read_ecc;
828 if (subdev[0]->write_ecc)
829 concat->mtd.write_ecc = concat_write_ecc;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400830 if (subdev[0]->writev)
831 concat->mtd.writev = concat_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (subdev[0]->read_oob)
833 concat->mtd.read_oob = concat_read_oob;
834 if (subdev[0]->write_oob)
835 concat->mtd.write_oob = concat_write_oob;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400836 if (subdev[0]->block_isbad)
837 concat->mtd.block_isbad = concat_block_isbad;
838 if (subdev[0]->block_markbad)
839 concat->mtd.block_markbad = concat_block_markbad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 concat->subdev[0] = subdev[0];
842
843 for (i = 1; i < num_devs; i++) {
844 if (concat->mtd.type != subdev[i]->type) {
845 kfree(concat);
846 printk("Incompatible device type on \"%s\"\n",
847 subdev[i]->name);
848 return NULL;
849 }
850 if (concat->mtd.flags != subdev[i]->flags) {
851 /*
852 * Expect all flags except MTD_WRITEABLE to be
853 * equal on all subdevices.
854 */
855 if ((concat->mtd.flags ^ subdev[i]->
856 flags) & ~MTD_WRITEABLE) {
857 kfree(concat);
858 printk("Incompatible device flags on \"%s\"\n",
859 subdev[i]->name);
860 return NULL;
861 } else
862 /* if writeable attribute differs,
863 make super device writeable */
864 concat->mtd.flags |=
865 subdev[i]->flags & MTD_WRITEABLE;
866 }
867 concat->mtd.size += subdev[i]->size;
Joern Engel28318772006-05-22 23:18:05 +0200868 if (concat->mtd.writesize != subdev[i]->writesize ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 concat->mtd.oobsize != subdev[i]->oobsize ||
870 concat->mtd.ecctype != subdev[i]->ecctype ||
871 concat->mtd.eccsize != subdev[i]->eccsize ||
872 !concat->mtd.read_ecc != !subdev[i]->read_ecc ||
873 !concat->mtd.write_ecc != !subdev[i]->write_ecc ||
874 !concat->mtd.read_oob != !subdev[i]->read_oob ||
875 !concat->mtd.write_oob != !subdev[i]->write_oob) {
876 kfree(concat);
877 printk("Incompatible OOB or ECC data on \"%s\"\n",
878 subdev[i]->name);
879 return NULL;
880 }
881 concat->subdev[i] = subdev[i];
882
883 }
884
Alexander Belyakove8d32932006-05-17 19:11:16 +0400885 if(concat->mtd.type == MTD_NANDFLASH)
886 memcpy(&concat->mtd.oobinfo, &subdev[0]->oobinfo,
887 sizeof(struct nand_oobinfo));
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 concat->num_subdev = num_devs;
890 concat->mtd.name = name;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 concat->mtd.erase = concat_erase;
893 concat->mtd.read = concat_read;
894 concat->mtd.write = concat_write;
895 concat->mtd.sync = concat_sync;
896 concat->mtd.lock = concat_lock;
897 concat->mtd.unlock = concat_unlock;
898 concat->mtd.suspend = concat_suspend;
899 concat->mtd.resume = concat_resume;
900
901 /*
902 * Combine the erase block size info of the subdevices:
903 *
904 * first, walk the map of the new device and see how
905 * many changes in erase size we have
906 */
907 max_erasesize = curr_erasesize = subdev[0]->erasesize;
908 num_erase_region = 1;
909 for (i = 0; i < num_devs; i++) {
910 if (subdev[i]->numeraseregions == 0) {
911 /* current subdevice has uniform erase size */
912 if (subdev[i]->erasesize != curr_erasesize) {
913 /* if it differs from the last subdevice's erase size, count it */
914 ++num_erase_region;
915 curr_erasesize = subdev[i]->erasesize;
916 if (curr_erasesize > max_erasesize)
917 max_erasesize = curr_erasesize;
918 }
919 } else {
920 /* current subdevice has variable erase size */
921 int j;
922 for (j = 0; j < subdev[i]->numeraseregions; j++) {
923
924 /* walk the list of erase regions, count any changes */
925 if (subdev[i]->eraseregions[j].erasesize !=
926 curr_erasesize) {
927 ++num_erase_region;
928 curr_erasesize =
929 subdev[i]->eraseregions[j].
930 erasesize;
931 if (curr_erasesize > max_erasesize)
932 max_erasesize = curr_erasesize;
933 }
934 }
935 }
936 }
937
938 if (num_erase_region == 1) {
939 /*
940 * All subdevices have the same uniform erase size.
941 * This is easy:
942 */
943 concat->mtd.erasesize = curr_erasesize;
944 concat->mtd.numeraseregions = 0;
945 } else {
946 /*
947 * erase block size varies across the subdevices: allocate
948 * space to store the data describing the variable erase regions
949 */
950 struct mtd_erase_region_info *erase_region_p;
951 u_int32_t begin, position;
952
953 concat->mtd.erasesize = max_erasesize;
954 concat->mtd.numeraseregions = num_erase_region;
955 concat->mtd.eraseregions = erase_region_p =
956 kmalloc(num_erase_region *
957 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
958 if (!erase_region_p) {
959 kfree(concat);
960 printk
961 ("memory allocation error while creating erase region list"
962 " for device \"%s\"\n", name);
963 return NULL;
964 }
965
966 /*
967 * walk the map of the new device once more and fill in
968 * in erase region info:
969 */
970 curr_erasesize = subdev[0]->erasesize;
971 begin = position = 0;
972 for (i = 0; i < num_devs; i++) {
973 if (subdev[i]->numeraseregions == 0) {
974 /* current subdevice has uniform erase size */
975 if (subdev[i]->erasesize != curr_erasesize) {
976 /*
977 * fill in an mtd_erase_region_info structure for the area
978 * we have walked so far:
979 */
980 erase_region_p->offset = begin;
981 erase_region_p->erasesize =
982 curr_erasesize;
983 erase_region_p->numblocks =
984 (position - begin) / curr_erasesize;
985 begin = position;
986
987 curr_erasesize = subdev[i]->erasesize;
988 ++erase_region_p;
989 }
990 position += subdev[i]->size;
991 } else {
992 /* current subdevice has variable erase size */
993 int j;
994 for (j = 0; j < subdev[i]->numeraseregions; j++) {
995 /* walk the list of erase regions, count any changes */
996 if (subdev[i]->eraseregions[j].
997 erasesize != curr_erasesize) {
998 erase_region_p->offset = begin;
999 erase_region_p->erasesize =
1000 curr_erasesize;
1001 erase_region_p->numblocks =
1002 (position -
1003 begin) / curr_erasesize;
1004 begin = position;
1005
1006 curr_erasesize =
1007 subdev[i]->eraseregions[j].
1008 erasesize;
1009 ++erase_region_p;
1010 }
1011 position +=
1012 subdev[i]->eraseregions[j].
1013 numblocks * curr_erasesize;
1014 }
1015 }
1016 }
1017 /* Now write the final entry */
1018 erase_region_p->offset = begin;
1019 erase_region_p->erasesize = curr_erasesize;
1020 erase_region_p->numblocks = (position - begin) / curr_erasesize;
1021 }
1022
1023 return &concat->mtd;
1024}
1025
Thomas Gleixner97894cd2005-11-07 11:15:26 +00001026/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * This function destroys an MTD object obtained from concat_mtd_devs()
1028 */
1029
1030void mtd_concat_destroy(struct mtd_info *mtd)
1031{
1032 struct mtd_concat *concat = CONCAT(mtd);
1033 if (concat->mtd.numeraseregions)
1034 kfree(concat->mtd.eraseregions);
1035 kfree(concat);
1036}
1037
1038EXPORT_SYMBOL(mtd_concat_create);
1039EXPORT_SYMBOL(mtd_concat_destroy);
1040
1041MODULE_LICENSE("GPL");
1042MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
1043MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");