blob: 3c8d5e6fa0109796bba1e24c92abef19a39724c6 [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 Gleixner9a1fcdf2006-05-29 14:56:39 +020059 int ret = 0, err = -EINVAL;
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 if (err && (err != -EBADMSG) && (err != -EUCLEAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 break;
85
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +020086 /* Save information about bitflips! */
87 if (err) {
88 if (err == -EBADMSG)
89 ret = err;
90 else if (!ret)
91 ret = err;
92 err = 0;
93 }
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *retlen += retsize;
96 len -= size;
97 if (len == 0)
98 break;
99
100 err = -EINVAL;
101 buf += size;
102 from = 0;
103 }
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200104 return err ? err : ret;
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) {
Andrew Morton6c8b44a2006-05-20 10:17:21 +0100180 loff_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 */
186 vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
187 if (!vecs_copy)
188 return -ENOMEM;
189 memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
190
191 entry_low = 0;
192 for (i = 0; i < concat->num_subdev; i++) {
193 struct mtd_info *subdev = concat->subdev[i];
194 size_t size, wsize, retsize, old_iov_len;
195
196 if (to >= subdev->size) {
197 to -= subdev->size;
198 continue;
199 }
200
201 size = min(total_len, (size_t)(subdev->size - to));
202 wsize = size; /* store for future use */
203
204 entry_high = entry_low;
205 while (entry_high < count) {
206 if (size <= vecs_copy[entry_high].iov_len)
207 break;
208 size -= vecs_copy[entry_high++].iov_len;
209 }
210
211 old_iov_len = vecs_copy[entry_high].iov_len;
212 vecs_copy[entry_high].iov_len = size;
213
214 if (!(subdev->flags & MTD_WRITEABLE))
215 err = -EROFS;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400216 else
217 err = subdev->writev(subdev, &vecs_copy[entry_low],
218 entry_high - entry_low + 1, to, &retsize);
219
220 vecs_copy[entry_high].iov_len = old_iov_len - size;
221 vecs_copy[entry_high].iov_base += size;
222
223 entry_low = entry_high;
224
225 if (err)
226 break;
227
228 *retlen += retsize;
229 total_len -= wsize;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400230
231 if (total_len == 0)
232 break;
233
234 err = -EINVAL;
235 to = 0;
236 }
237
238 kfree(vecs_copy);
239 return err;
240}
241
242static int
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200243concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200246 struct mtd_oob_ops devops = *ops;
247 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200249 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 for (i = 0; i < concat->num_subdev; i++) {
252 struct mtd_info *subdev = concat->subdev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (from >= subdev->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 from -= subdev->size;
256 continue;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200259 /* partial read ? */
260 if (from + devops.len > subdev->size)
261 devops.len = subdev->size - from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200263 err = subdev->read_oob(subdev, from, &devops);
264 ops->retlen += devops.retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (err)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200268 devops.len = ops->len - ops->retlen;
269 if (!devops.len)
270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200272 if (devops.datbuf)
273 devops.datbuf += devops.retlen;
274 if (devops.oobbuf)
275 devops.oobbuf += devops.ooblen;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 from = 0;
278 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200279 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static int
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200283concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct mtd_concat *concat = CONCAT(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200286 struct mtd_oob_ops devops = *ops;
287 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (!(mtd->flags & MTD_WRITEABLE))
290 return -EROFS;
291
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200292 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 for (i = 0; i < concat->num_subdev; i++) {
295 struct mtd_info *subdev = concat->subdev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (to >= subdev->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 to -= subdev->size;
299 continue;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200302 /* partial write ? */
303 if (to + devops.len > subdev->size)
304 devops.len = subdev->size - to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200306 err = subdev->write_oob(subdev, to, &devops);
307 ops->retlen += devops.retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (err)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200311 devops.len = ops->len - ops->retlen;
312 if (!devops.len)
313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200315 if (devops.datbuf)
316 devops.datbuf += devops.retlen;
317 if (devops.oobbuf)
318 devops.oobbuf += devops.ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 to = 0;
320 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324static void concat_erase_callback(struct erase_info *instr)
325{
326 wake_up((wait_queue_head_t *) instr->priv);
327}
328
329static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
330{
331 int err;
332 wait_queue_head_t waitq;
333 DECLARE_WAITQUEUE(wait, current);
334
335 /*
336 * This code was stol^H^H^H^Hinspired by mtdchar.c
337 */
338 init_waitqueue_head(&waitq);
339
340 erase->mtd = mtd;
341 erase->callback = concat_erase_callback;
342 erase->priv = (unsigned long) &waitq;
343
344 /*
345 * FIXME: Allow INTERRUPTIBLE. Which means
346 * not having the wait_queue head on the stack.
347 */
348 err = mtd->erase(mtd, erase);
349 if (!err) {
350 set_current_state(TASK_UNINTERRUPTIBLE);
351 add_wait_queue(&waitq, &wait);
352 if (erase->state != MTD_ERASE_DONE
353 && erase->state != MTD_ERASE_FAILED)
354 schedule();
355 remove_wait_queue(&waitq, &wait);
356 set_current_state(TASK_RUNNING);
357
358 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
359 }
360 return err;
361}
362
363static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
364{
365 struct mtd_concat *concat = CONCAT(mtd);
366 struct mtd_info *subdev;
367 int i, err;
368 u_int32_t length, offset = 0;
369 struct erase_info *erase;
370
371 if (!(mtd->flags & MTD_WRITEABLE))
372 return -EROFS;
373
374 if (instr->addr > concat->mtd.size)
375 return -EINVAL;
376
377 if (instr->len + instr->addr > concat->mtd.size)
378 return -EINVAL;
379
380 /*
381 * Check for proper erase block alignment of the to-be-erased area.
382 * It is easier to do this based on the super device's erase
383 * region info rather than looking at each particular sub-device
384 * in turn.
385 */
386 if (!concat->mtd.numeraseregions) {
387 /* the easy case: device has uniform erase block size */
388 if (instr->addr & (concat->mtd.erasesize - 1))
389 return -EINVAL;
390 if (instr->len & (concat->mtd.erasesize - 1))
391 return -EINVAL;
392 } else {
393 /* device has variable erase size */
394 struct mtd_erase_region_info *erase_regions =
395 concat->mtd.eraseregions;
396
397 /*
398 * Find the erase region where the to-be-erased area begins:
399 */
400 for (i = 0; i < concat->mtd.numeraseregions &&
401 instr->addr >= erase_regions[i].offset; i++) ;
402 --i;
403
404 /*
405 * Now erase_regions[i] is the region in which the
406 * to-be-erased area begins. Verify that the starting
407 * offset is aligned to this region's erase size:
408 */
409 if (instr->addr & (erase_regions[i].erasesize - 1))
410 return -EINVAL;
411
412 /*
413 * now find the erase region where the to-be-erased area ends:
414 */
415 for (; i < concat->mtd.numeraseregions &&
416 (instr->addr + instr->len) >= erase_regions[i].offset;
417 ++i) ;
418 --i;
419 /*
420 * check if the ending offset is aligned to this region's erase size
421 */
422 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
423 1))
424 return -EINVAL;
425 }
426
427 instr->fail_addr = 0xffffffff;
428
429 /* make a local copy of instr to avoid modifying the caller's struct */
430 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
431
432 if (!erase)
433 return -ENOMEM;
434
435 *erase = *instr;
436 length = instr->len;
437
438 /*
439 * find the subdevice where the to-be-erased area begins, adjust
440 * starting offset to be relative to the subdevice start
441 */
442 for (i = 0; i < concat->num_subdev; i++) {
443 subdev = concat->subdev[i];
444 if (subdev->size <= erase->addr) {
445 erase->addr -= subdev->size;
446 offset += subdev->size;
447 } else {
448 break;
449 }
450 }
451
452 /* must never happen since size limit has been verified above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200453 BUG_ON(i >= concat->num_subdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* now do the erase: */
456 err = 0;
457 for (; length > 0; i++) {
458 /* loop for all subdevices affected by this request */
459 subdev = concat->subdev[i]; /* get current subdevice */
460
461 /* limit length to subdevice's size: */
462 if (erase->addr + length > subdev->size)
463 erase->len = subdev->size - erase->addr;
464 else
465 erase->len = length;
466
467 if (!(subdev->flags & MTD_WRITEABLE)) {
468 err = -EROFS;
469 break;
470 }
471 length -= erase->len;
472 if ((err = concat_dev_erase(subdev, erase))) {
473 /* sanity check: should never happen since
474 * block alignment has been checked above */
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200475 BUG_ON(err == -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (erase->fail_addr != 0xffffffff)
477 instr->fail_addr = erase->fail_addr + offset;
478 break;
479 }
480 /*
481 * erase->addr specifies the offset of the area to be
482 * erased *within the current subdevice*. It can be
483 * non-zero only the first time through this loop, i.e.
484 * for the first subdevice where blocks need to be erased.
485 * All the following erases must begin at the start of the
486 * current subdevice, i.e. at offset zero.
487 */
488 erase->addr = 0;
489 offset += subdev->size;
490 }
491 instr->state = erase->state;
492 kfree(erase);
493 if (err)
494 return err;
495
496 if (instr->callback)
497 instr->callback(instr);
498 return 0;
499}
500
501static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
502{
503 struct mtd_concat *concat = CONCAT(mtd);
504 int i, err = -EINVAL;
505
506 if ((len + ofs) > mtd->size)
507 return -EINVAL;
508
509 for (i = 0; i < concat->num_subdev; i++) {
510 struct mtd_info *subdev = concat->subdev[i];
511 size_t size;
512
513 if (ofs >= subdev->size) {
514 size = 0;
515 ofs -= subdev->size;
516 continue;
517 }
518 if (ofs + len > subdev->size)
519 size = subdev->size - ofs;
520 else
521 size = len;
522
523 err = subdev->lock(subdev, ofs, size);
524
525 if (err)
526 break;
527
528 len -= size;
529 if (len == 0)
530 break;
531
532 err = -EINVAL;
533 ofs = 0;
534 }
535
536 return err;
537}
538
539static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
540{
541 struct mtd_concat *concat = CONCAT(mtd);
542 int i, err = 0;
543
544 if ((len + ofs) > mtd->size)
545 return -EINVAL;
546
547 for (i = 0; i < concat->num_subdev; i++) {
548 struct mtd_info *subdev = concat->subdev[i];
549 size_t size;
550
551 if (ofs >= subdev->size) {
552 size = 0;
553 ofs -= subdev->size;
554 continue;
555 }
556 if (ofs + len > subdev->size)
557 size = subdev->size - ofs;
558 else
559 size = len;
560
561 err = subdev->unlock(subdev, ofs, size);
562
563 if (err)
564 break;
565
566 len -= size;
567 if (len == 0)
568 break;
569
570 err = -EINVAL;
571 ofs = 0;
572 }
573
574 return err;
575}
576
577static void concat_sync(struct mtd_info *mtd)
578{
579 struct mtd_concat *concat = CONCAT(mtd);
580 int i;
581
582 for (i = 0; i < concat->num_subdev; i++) {
583 struct mtd_info *subdev = concat->subdev[i];
584 subdev->sync(subdev);
585 }
586}
587
588static int concat_suspend(struct mtd_info *mtd)
589{
590 struct mtd_concat *concat = CONCAT(mtd);
591 int i, rc = 0;
592
593 for (i = 0; i < concat->num_subdev; i++) {
594 struct mtd_info *subdev = concat->subdev[i];
595 if ((rc = subdev->suspend(subdev)) < 0)
596 return rc;
597 }
598 return rc;
599}
600
601static void concat_resume(struct mtd_info *mtd)
602{
603 struct mtd_concat *concat = CONCAT(mtd);
604 int i;
605
606 for (i = 0; i < concat->num_subdev; i++) {
607 struct mtd_info *subdev = concat->subdev[i];
608 subdev->resume(subdev);
609 }
610}
611
Alexander Belyakove8d32932006-05-17 19:11:16 +0400612static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
613{
614 struct mtd_concat *concat = CONCAT(mtd);
615 int i, res = 0;
616
617 if (!concat->subdev[0]->block_isbad)
618 return res;
619
620 if (ofs > mtd->size)
621 return -EINVAL;
622
623 for (i = 0; i < concat->num_subdev; i++) {
624 struct mtd_info *subdev = concat->subdev[i];
625
626 if (ofs >= subdev->size) {
627 ofs -= subdev->size;
628 continue;
629 }
630
631 res = subdev->block_isbad(subdev, ofs);
632 break;
633 }
634
635 return res;
636}
637
638static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
639{
640 struct mtd_concat *concat = CONCAT(mtd);
641 int i, err = -EINVAL;
642
643 if (!concat->subdev[0]->block_markbad)
644 return 0;
645
646 if (ofs > mtd->size)
647 return -EINVAL;
648
649 for (i = 0; i < concat->num_subdev; i++) {
650 struct mtd_info *subdev = concat->subdev[i];
651
652 if (ofs >= subdev->size) {
653 ofs -= subdev->size;
654 continue;
655 }
656
657 err = subdev->block_markbad(subdev, ofs);
658 break;
659 }
660
661 return err;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/*
665 * This function constructs a virtual MTD device by concatenating
666 * num_devs MTD devices. A pointer to the new device object is
667 * stored to *new_dev upon success. This function does _not_
668 * register any devices: this is the caller's responsibility.
669 */
670struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
671 int num_devs, /* number of subdevices */
672 char *name)
673{ /* name for the new device */
674 int i;
675 size_t size;
676 struct mtd_concat *concat;
677 u_int32_t max_erasesize, curr_erasesize;
678 int num_erase_region;
679
680 printk(KERN_NOTICE "Concatenating MTD devices:\n");
681 for (i = 0; i < num_devs; i++)
682 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
683 printk(KERN_NOTICE "into device \"%s\"\n", name);
684
685 /* allocate the device structure */
686 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
687 concat = kmalloc(size, GFP_KERNEL);
688 if (!concat) {
689 printk
690 ("memory allocation error while creating concatenated device \"%s\"\n",
691 name);
692 return NULL;
693 }
694 memset(concat, 0, size);
695 concat->subdev = (struct mtd_info **) (concat + 1);
696
697 /*
698 * Set up the new "super" device's MTD object structure, check for
699 * incompatibilites between the subdevices.
700 */
701 concat->mtd.type = subdev[0]->type;
702 concat->mtd.flags = subdev[0]->flags;
703 concat->mtd.size = subdev[0]->size;
704 concat->mtd.erasesize = subdev[0]->erasesize;
Joern Engel28318772006-05-22 23:18:05 +0200705 concat->mtd.writesize = subdev[0]->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 concat->mtd.oobsize = subdev[0]->oobsize;
707 concat->mtd.ecctype = subdev[0]->ecctype;
708 concat->mtd.eccsize = subdev[0]->eccsize;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400709 if (subdev[0]->writev)
710 concat->mtd.writev = concat_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (subdev[0]->read_oob)
712 concat->mtd.read_oob = concat_read_oob;
713 if (subdev[0]->write_oob)
714 concat->mtd.write_oob = concat_write_oob;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400715 if (subdev[0]->block_isbad)
716 concat->mtd.block_isbad = concat_block_isbad;
717 if (subdev[0]->block_markbad)
718 concat->mtd.block_markbad = concat_block_markbad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 concat->subdev[0] = subdev[0];
721
722 for (i = 1; i < num_devs; i++) {
723 if (concat->mtd.type != subdev[i]->type) {
724 kfree(concat);
725 printk("Incompatible device type on \"%s\"\n",
726 subdev[i]->name);
727 return NULL;
728 }
729 if (concat->mtd.flags != subdev[i]->flags) {
730 /*
731 * Expect all flags except MTD_WRITEABLE to be
732 * equal on all subdevices.
733 */
734 if ((concat->mtd.flags ^ subdev[i]->
735 flags) & ~MTD_WRITEABLE) {
736 kfree(concat);
737 printk("Incompatible device flags on \"%s\"\n",
738 subdev[i]->name);
739 return NULL;
740 } else
741 /* if writeable attribute differs,
742 make super device writeable */
743 concat->mtd.flags |=
744 subdev[i]->flags & MTD_WRITEABLE;
745 }
746 concat->mtd.size += subdev[i]->size;
Joern Engel28318772006-05-22 23:18:05 +0200747 if (concat->mtd.writesize != subdev[i]->writesize ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 concat->mtd.oobsize != subdev[i]->oobsize ||
749 concat->mtd.ecctype != subdev[i]->ecctype ||
750 concat->mtd.eccsize != subdev[i]->eccsize ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 !concat->mtd.read_oob != !subdev[i]->read_oob ||
752 !concat->mtd.write_oob != !subdev[i]->write_oob) {
753 kfree(concat);
754 printk("Incompatible OOB or ECC data on \"%s\"\n",
755 subdev[i]->name);
756 return NULL;
757 }
758 concat->subdev[i] = subdev[i];
759
760 }
761
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200762 concat->mtd.ecclayout = subdev[0]->ecclayout;
Alexander Belyakove8d32932006-05-17 19:11:16 +0400763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 concat->num_subdev = num_devs;
765 concat->mtd.name = name;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 concat->mtd.erase = concat_erase;
768 concat->mtd.read = concat_read;
769 concat->mtd.write = concat_write;
770 concat->mtd.sync = concat_sync;
771 concat->mtd.lock = concat_lock;
772 concat->mtd.unlock = concat_unlock;
773 concat->mtd.suspend = concat_suspend;
774 concat->mtd.resume = concat_resume;
775
776 /*
777 * Combine the erase block size info of the subdevices:
778 *
779 * first, walk the map of the new device and see how
780 * many changes in erase size we have
781 */
782 max_erasesize = curr_erasesize = subdev[0]->erasesize;
783 num_erase_region = 1;
784 for (i = 0; i < num_devs; i++) {
785 if (subdev[i]->numeraseregions == 0) {
786 /* current subdevice has uniform erase size */
787 if (subdev[i]->erasesize != curr_erasesize) {
788 /* if it differs from the last subdevice's erase size, count it */
789 ++num_erase_region;
790 curr_erasesize = subdev[i]->erasesize;
791 if (curr_erasesize > max_erasesize)
792 max_erasesize = curr_erasesize;
793 }
794 } else {
795 /* current subdevice has variable erase size */
796 int j;
797 for (j = 0; j < subdev[i]->numeraseregions; j++) {
798
799 /* walk the list of erase regions, count any changes */
800 if (subdev[i]->eraseregions[j].erasesize !=
801 curr_erasesize) {
802 ++num_erase_region;
803 curr_erasesize =
804 subdev[i]->eraseregions[j].
805 erasesize;
806 if (curr_erasesize > max_erasesize)
807 max_erasesize = curr_erasesize;
808 }
809 }
810 }
811 }
812
813 if (num_erase_region == 1) {
814 /*
815 * All subdevices have the same uniform erase size.
816 * This is easy:
817 */
818 concat->mtd.erasesize = curr_erasesize;
819 concat->mtd.numeraseregions = 0;
820 } else {
821 /*
822 * erase block size varies across the subdevices: allocate
823 * space to store the data describing the variable erase regions
824 */
825 struct mtd_erase_region_info *erase_region_p;
826 u_int32_t begin, position;
827
828 concat->mtd.erasesize = max_erasesize;
829 concat->mtd.numeraseregions = num_erase_region;
830 concat->mtd.eraseregions = erase_region_p =
831 kmalloc(num_erase_region *
832 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
833 if (!erase_region_p) {
834 kfree(concat);
835 printk
836 ("memory allocation error while creating erase region list"
837 " for device \"%s\"\n", name);
838 return NULL;
839 }
840
841 /*
842 * walk the map of the new device once more and fill in
843 * in erase region info:
844 */
845 curr_erasesize = subdev[0]->erasesize;
846 begin = position = 0;
847 for (i = 0; i < num_devs; i++) {
848 if (subdev[i]->numeraseregions == 0) {
849 /* current subdevice has uniform erase size */
850 if (subdev[i]->erasesize != curr_erasesize) {
851 /*
852 * fill in an mtd_erase_region_info structure for the area
853 * we have walked so far:
854 */
855 erase_region_p->offset = begin;
856 erase_region_p->erasesize =
857 curr_erasesize;
858 erase_region_p->numblocks =
859 (position - begin) / curr_erasesize;
860 begin = position;
861
862 curr_erasesize = subdev[i]->erasesize;
863 ++erase_region_p;
864 }
865 position += subdev[i]->size;
866 } else {
867 /* current subdevice has variable erase size */
868 int j;
869 for (j = 0; j < subdev[i]->numeraseregions; j++) {
870 /* walk the list of erase regions, count any changes */
871 if (subdev[i]->eraseregions[j].
872 erasesize != curr_erasesize) {
873 erase_region_p->offset = begin;
874 erase_region_p->erasesize =
875 curr_erasesize;
876 erase_region_p->numblocks =
877 (position -
878 begin) / curr_erasesize;
879 begin = position;
880
881 curr_erasesize =
882 subdev[i]->eraseregions[j].
883 erasesize;
884 ++erase_region_p;
885 }
886 position +=
887 subdev[i]->eraseregions[j].
888 numblocks * curr_erasesize;
889 }
890 }
891 }
892 /* Now write the final entry */
893 erase_region_p->offset = begin;
894 erase_region_p->erasesize = curr_erasesize;
895 erase_region_p->numblocks = (position - begin) / curr_erasesize;
896 }
897
898 return &concat->mtd;
899}
900
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000901/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 * This function destroys an MTD object obtained from concat_mtd_devs()
903 */
904
905void mtd_concat_destroy(struct mtd_info *mtd)
906{
907 struct mtd_concat *concat = CONCAT(mtd);
908 if (concat->mtd.numeraseregions)
909 kfree(concat->mtd.eraseregions);
910 kfree(concat);
911}
912
913EXPORT_SYMBOL(mtd_concat_create);
914EXPORT_SYMBOL(mtd_concat_destroy);
915
916MODULE_LICENSE("GPL");
917MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
918MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");