]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | * | |
97894cda | 10 | * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $ |
1da177e4 LT |
11 | */ |
12 | ||
1da177e4 | 13 | #include <linux/kernel.h> |
15fdc52f | 14 | #include <linux/module.h> |
1da177e4 | 15 | #include <linux/slab.h> |
15fdc52f TG |
16 | #include <linux/sched.h> |
17 | #include <linux/types.h> | |
18 | ||
1da177e4 LT |
19 | #include <linux/mtd/mtd.h> |
20 | #include <linux/mtd/concat.h> | |
21 | ||
6c8b44ab AM |
22 | #include <asm/div64.h> |
23 | ||
1da177e4 LT |
24 | /* |
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 | */ | |
30 | struct 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 | ||
97894cda | 49 | /* |
1da177e4 LT |
50 | * MTD methods which look up the relevant subdevice, translate the |
51 | * effective address and pass through to the subdevice. | |
52 | */ | |
53 | ||
54 | static int | |
55 | concat_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 | ||
98 | static int | |
99 | concat_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 | ||
e8d32937 | 145 | static int |
9d8522df TG |
146 | concat_writev(struct mtd_info *mtd, const struct kvec *vecs, |
147 | unsigned long count, loff_t to, size_t * retlen) | |
e8d32937 AB |
148 | { |
149 | struct mtd_concat *concat = CONCAT(mtd); | |
150 | struct kvec *vecs_copy; | |
151 | unsigned long entry_low, entry_high; | |
152 | size_t total_len = 0; | |
153 | int i; | |
154 | int err = -EINVAL; | |
155 | ||
156 | if (!(mtd->flags & MTD_WRITEABLE)) | |
157 | return -EROFS; | |
158 | ||
159 | *retlen = 0; | |
160 | ||
161 | /* Calculate total length of data */ | |
162 | for (i = 0; i < count; i++) | |
163 | total_len += vecs[i].iov_len; | |
164 | ||
165 | /* Do not allow write past end of device */ | |
166 | if ((to + total_len) > mtd->size) | |
167 | return -EINVAL; | |
168 | ||
169 | /* Check alignment */ | |
28318776 | 170 | if (mtd->writesize > 1) { |
6c8b44ab | 171 | loff_t __to = to; |
28318776 | 172 | if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize)) |
e8d32937 | 173 | return -EINVAL; |
6c8b44ab | 174 | } |
e8d32937 AB |
175 | |
176 | /* make a copy of vecs */ | |
177 | vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL); | |
178 | if (!vecs_copy) | |
179 | return -ENOMEM; | |
180 | memcpy(vecs_copy, vecs, sizeof(struct kvec) * count); | |
181 | ||
182 | entry_low = 0; | |
183 | for (i = 0; i < concat->num_subdev; i++) { | |
184 | struct mtd_info *subdev = concat->subdev[i]; | |
185 | size_t size, wsize, retsize, old_iov_len; | |
186 | ||
187 | if (to >= subdev->size) { | |
188 | to -= subdev->size; | |
189 | continue; | |
190 | } | |
191 | ||
192 | size = min(total_len, (size_t)(subdev->size - to)); | |
193 | wsize = size; /* store for future use */ | |
194 | ||
195 | entry_high = entry_low; | |
196 | while (entry_high < count) { | |
197 | if (size <= vecs_copy[entry_high].iov_len) | |
198 | break; | |
199 | size -= vecs_copy[entry_high++].iov_len; | |
200 | } | |
201 | ||
202 | old_iov_len = vecs_copy[entry_high].iov_len; | |
203 | vecs_copy[entry_high].iov_len = size; | |
204 | ||
205 | if (!(subdev->flags & MTD_WRITEABLE)) | |
206 | err = -EROFS; | |
e8d32937 AB |
207 | else |
208 | err = subdev->writev(subdev, &vecs_copy[entry_low], | |
209 | entry_high - entry_low + 1, to, &retsize); | |
210 | ||
211 | vecs_copy[entry_high].iov_len = old_iov_len - size; | |
212 | vecs_copy[entry_high].iov_base += size; | |
213 | ||
214 | entry_low = entry_high; | |
215 | ||
216 | if (err) | |
217 | break; | |
218 | ||
219 | *retlen += retsize; | |
220 | total_len -= wsize; | |
e8d32937 AB |
221 | |
222 | if (total_len == 0) | |
223 | break; | |
224 | ||
225 | err = -EINVAL; | |
226 | to = 0; | |
227 | } | |
228 | ||
229 | kfree(vecs_copy); | |
230 | return err; | |
231 | } | |
232 | ||
1da177e4 LT |
233 | static int |
234 | concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len, | |
235 | size_t * retlen, u_char * buf) | |
236 | { | |
237 | struct mtd_concat *concat = CONCAT(mtd); | |
238 | int err = -EINVAL; | |
239 | int i; | |
240 | ||
241 | *retlen = 0; | |
242 | ||
243 | for (i = 0; i < concat->num_subdev; i++) { | |
244 | struct mtd_info *subdev = concat->subdev[i]; | |
245 | size_t size, retsize; | |
246 | ||
247 | if (from >= subdev->size) { | |
248 | /* Not destined for this subdev */ | |
249 | size = 0; | |
250 | from -= subdev->size; | |
251 | continue; | |
252 | } | |
253 | if (from + len > subdev->size) | |
254 | /* First part goes into this subdev */ | |
255 | size = subdev->size - from; | |
256 | else | |
257 | /* Entire transaction goes into this subdev */ | |
258 | size = len; | |
259 | ||
260 | if (subdev->read_oob) | |
261 | err = subdev->read_oob(subdev, from, size, | |
262 | &retsize, buf); | |
263 | else | |
264 | err = -EINVAL; | |
265 | ||
266 | if (err) | |
267 | break; | |
268 | ||
269 | *retlen += retsize; | |
270 | len -= size; | |
271 | if (len == 0) | |
272 | break; | |
273 | ||
274 | err = -EINVAL; | |
275 | buf += size; | |
276 | from = 0; | |
277 | } | |
278 | return err; | |
279 | } | |
280 | ||
281 | static int | |
282 | concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len, | |
283 | size_t * retlen, const u_char * buf) | |
284 | { | |
285 | struct mtd_concat *concat = CONCAT(mtd); | |
286 | int err = -EINVAL; | |
287 | int i; | |
288 | ||
289 | if (!(mtd->flags & MTD_WRITEABLE)) | |
290 | return -EROFS; | |
291 | ||
292 | *retlen = 0; | |
293 | ||
294 | for (i = 0; i < concat->num_subdev; i++) { | |
295 | struct mtd_info *subdev = concat->subdev[i]; | |
296 | size_t size, retsize; | |
297 | ||
298 | if (to >= subdev->size) { | |
299 | size = 0; | |
300 | to -= subdev->size; | |
301 | continue; | |
302 | } | |
303 | if (to + len > subdev->size) | |
304 | size = subdev->size - to; | |
305 | else | |
306 | size = len; | |
307 | ||
308 | if (!(subdev->flags & MTD_WRITEABLE)) | |
309 | err = -EROFS; | |
310 | else if (subdev->write_oob) | |
311 | err = subdev->write_oob(subdev, to, size, &retsize, | |
312 | buf); | |
313 | else | |
314 | err = -EINVAL; | |
315 | ||
316 | if (err) | |
317 | break; | |
318 | ||
319 | *retlen += retsize; | |
320 | len -= size; | |
321 | if (len == 0) | |
322 | break; | |
323 | ||
324 | err = -EINVAL; | |
325 | buf += size; | |
326 | to = 0; | |
327 | } | |
328 | return err; | |
329 | } | |
330 | ||
331 | static void concat_erase_callback(struct erase_info *instr) | |
332 | { | |
333 | wake_up((wait_queue_head_t *) instr->priv); | |
334 | } | |
335 | ||
336 | static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase) | |
337 | { | |
338 | int err; | |
339 | wait_queue_head_t waitq; | |
340 | DECLARE_WAITQUEUE(wait, current); | |
341 | ||
342 | /* | |
343 | * This code was stol^H^H^H^Hinspired by mtdchar.c | |
344 | */ | |
345 | init_waitqueue_head(&waitq); | |
346 | ||
347 | erase->mtd = mtd; | |
348 | erase->callback = concat_erase_callback; | |
349 | erase->priv = (unsigned long) &waitq; | |
350 | ||
351 | /* | |
352 | * FIXME: Allow INTERRUPTIBLE. Which means | |
353 | * not having the wait_queue head on the stack. | |
354 | */ | |
355 | err = mtd->erase(mtd, erase); | |
356 | if (!err) { | |
357 | set_current_state(TASK_UNINTERRUPTIBLE); | |
358 | add_wait_queue(&waitq, &wait); | |
359 | if (erase->state != MTD_ERASE_DONE | |
360 | && erase->state != MTD_ERASE_FAILED) | |
361 | schedule(); | |
362 | remove_wait_queue(&waitq, &wait); | |
363 | set_current_state(TASK_RUNNING); | |
364 | ||
365 | err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0; | |
366 | } | |
367 | return err; | |
368 | } | |
369 | ||
370 | static int concat_erase(struct mtd_info *mtd, struct erase_info *instr) | |
371 | { | |
372 | struct mtd_concat *concat = CONCAT(mtd); | |
373 | struct mtd_info *subdev; | |
374 | int i, err; | |
375 | u_int32_t length, offset = 0; | |
376 | struct erase_info *erase; | |
377 | ||
378 | if (!(mtd->flags & MTD_WRITEABLE)) | |
379 | return -EROFS; | |
380 | ||
381 | if (instr->addr > concat->mtd.size) | |
382 | return -EINVAL; | |
383 | ||
384 | if (instr->len + instr->addr > concat->mtd.size) | |
385 | return -EINVAL; | |
386 | ||
387 | /* | |
388 | * Check for proper erase block alignment of the to-be-erased area. | |
389 | * It is easier to do this based on the super device's erase | |
390 | * region info rather than looking at each particular sub-device | |
391 | * in turn. | |
392 | */ | |
393 | if (!concat->mtd.numeraseregions) { | |
394 | /* the easy case: device has uniform erase block size */ | |
395 | if (instr->addr & (concat->mtd.erasesize - 1)) | |
396 | return -EINVAL; | |
397 | if (instr->len & (concat->mtd.erasesize - 1)) | |
398 | return -EINVAL; | |
399 | } else { | |
400 | /* device has variable erase size */ | |
401 | struct mtd_erase_region_info *erase_regions = | |
402 | concat->mtd.eraseregions; | |
403 | ||
404 | /* | |
405 | * Find the erase region where the to-be-erased area begins: | |
406 | */ | |
407 | for (i = 0; i < concat->mtd.numeraseregions && | |
408 | instr->addr >= erase_regions[i].offset; i++) ; | |
409 | --i; | |
410 | ||
411 | /* | |
412 | * Now erase_regions[i] is the region in which the | |
413 | * to-be-erased area begins. Verify that the starting | |
414 | * offset is aligned to this region's erase size: | |
415 | */ | |
416 | if (instr->addr & (erase_regions[i].erasesize - 1)) | |
417 | return -EINVAL; | |
418 | ||
419 | /* | |
420 | * now find the erase region where the to-be-erased area ends: | |
421 | */ | |
422 | for (; i < concat->mtd.numeraseregions && | |
423 | (instr->addr + instr->len) >= erase_regions[i].offset; | |
424 | ++i) ; | |
425 | --i; | |
426 | /* | |
427 | * check if the ending offset is aligned to this region's erase size | |
428 | */ | |
429 | if ((instr->addr + instr->len) & (erase_regions[i].erasesize - | |
430 | 1)) | |
431 | return -EINVAL; | |
432 | } | |
433 | ||
434 | instr->fail_addr = 0xffffffff; | |
435 | ||
436 | /* make a local copy of instr to avoid modifying the caller's struct */ | |
437 | erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL); | |
438 | ||
439 | if (!erase) | |
440 | return -ENOMEM; | |
441 | ||
442 | *erase = *instr; | |
443 | length = instr->len; | |
444 | ||
445 | /* | |
446 | * find the subdevice where the to-be-erased area begins, adjust | |
447 | * starting offset to be relative to the subdevice start | |
448 | */ | |
449 | for (i = 0; i < concat->num_subdev; i++) { | |
450 | subdev = concat->subdev[i]; | |
451 | if (subdev->size <= erase->addr) { | |
452 | erase->addr -= subdev->size; | |
453 | offset += subdev->size; | |
454 | } else { | |
455 | break; | |
456 | } | |
457 | } | |
458 | ||
459 | /* must never happen since size limit has been verified above */ | |
373ebfbf | 460 | BUG_ON(i >= concat->num_subdev); |
1da177e4 LT |
461 | |
462 | /* now do the erase: */ | |
463 | err = 0; | |
464 | for (; length > 0; i++) { | |
465 | /* loop for all subdevices affected by this request */ | |
466 | subdev = concat->subdev[i]; /* get current subdevice */ | |
467 | ||
468 | /* limit length to subdevice's size: */ | |
469 | if (erase->addr + length > subdev->size) | |
470 | erase->len = subdev->size - erase->addr; | |
471 | else | |
472 | erase->len = length; | |
473 | ||
474 | if (!(subdev->flags & MTD_WRITEABLE)) { | |
475 | err = -EROFS; | |
476 | break; | |
477 | } | |
478 | length -= erase->len; | |
479 | if ((err = concat_dev_erase(subdev, erase))) { | |
480 | /* sanity check: should never happen since | |
481 | * block alignment has been checked above */ | |
373ebfbf | 482 | BUG_ON(err == -EINVAL); |
1da177e4 LT |
483 | if (erase->fail_addr != 0xffffffff) |
484 | instr->fail_addr = erase->fail_addr + offset; | |
485 | break; | |
486 | } | |
487 | /* | |
488 | * erase->addr specifies the offset of the area to be | |
489 | * erased *within the current subdevice*. It can be | |
490 | * non-zero only the first time through this loop, i.e. | |
491 | * for the first subdevice where blocks need to be erased. | |
492 | * All the following erases must begin at the start of the | |
493 | * current subdevice, i.e. at offset zero. | |
494 | */ | |
495 | erase->addr = 0; | |
496 | offset += subdev->size; | |
497 | } | |
498 | instr->state = erase->state; | |
499 | kfree(erase); | |
500 | if (err) | |
501 | return err; | |
502 | ||
503 | if (instr->callback) | |
504 | instr->callback(instr); | |
505 | return 0; | |
506 | } | |
507 | ||
508 | static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len) | |
509 | { | |
510 | struct mtd_concat *concat = CONCAT(mtd); | |
511 | int i, err = -EINVAL; | |
512 | ||
513 | if ((len + ofs) > mtd->size) | |
514 | return -EINVAL; | |
515 | ||
516 | for (i = 0; i < concat->num_subdev; i++) { | |
517 | struct mtd_info *subdev = concat->subdev[i]; | |
518 | size_t size; | |
519 | ||
520 | if (ofs >= subdev->size) { | |
521 | size = 0; | |
522 | ofs -= subdev->size; | |
523 | continue; | |
524 | } | |
525 | if (ofs + len > subdev->size) | |
526 | size = subdev->size - ofs; | |
527 | else | |
528 | size = len; | |
529 | ||
530 | err = subdev->lock(subdev, ofs, size); | |
531 | ||
532 | if (err) | |
533 | break; | |
534 | ||
535 | len -= size; | |
536 | if (len == 0) | |
537 | break; | |
538 | ||
539 | err = -EINVAL; | |
540 | ofs = 0; | |
541 | } | |
542 | ||
543 | return err; | |
544 | } | |
545 | ||
546 | static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len) | |
547 | { | |
548 | struct mtd_concat *concat = CONCAT(mtd); | |
549 | int i, err = 0; | |
550 | ||
551 | if ((len + ofs) > mtd->size) | |
552 | return -EINVAL; | |
553 | ||
554 | for (i = 0; i < concat->num_subdev; i++) { | |
555 | struct mtd_info *subdev = concat->subdev[i]; | |
556 | size_t size; | |
557 | ||
558 | if (ofs >= subdev->size) { | |
559 | size = 0; | |
560 | ofs -= subdev->size; | |
561 | continue; | |
562 | } | |
563 | if (ofs + len > subdev->size) | |
564 | size = subdev->size - ofs; | |
565 | else | |
566 | size = len; | |
567 | ||
568 | err = subdev->unlock(subdev, ofs, size); | |
569 | ||
570 | if (err) | |
571 | break; | |
572 | ||
573 | len -= size; | |
574 | if (len == 0) | |
575 | break; | |
576 | ||
577 | err = -EINVAL; | |
578 | ofs = 0; | |
579 | } | |
580 | ||
581 | return err; | |
582 | } | |
583 | ||
584 | static void concat_sync(struct mtd_info *mtd) | |
585 | { | |
586 | struct mtd_concat *concat = CONCAT(mtd); | |
587 | int i; | |
588 | ||
589 | for (i = 0; i < concat->num_subdev; i++) { | |
590 | struct mtd_info *subdev = concat->subdev[i]; | |
591 | subdev->sync(subdev); | |
592 | } | |
593 | } | |
594 | ||
595 | static int concat_suspend(struct mtd_info *mtd) | |
596 | { | |
597 | struct mtd_concat *concat = CONCAT(mtd); | |
598 | int i, rc = 0; | |
599 | ||
600 | for (i = 0; i < concat->num_subdev; i++) { | |
601 | struct mtd_info *subdev = concat->subdev[i]; | |
602 | if ((rc = subdev->suspend(subdev)) < 0) | |
603 | return rc; | |
604 | } | |
605 | return rc; | |
606 | } | |
607 | ||
608 | static void concat_resume(struct mtd_info *mtd) | |
609 | { | |
610 | struct mtd_concat *concat = CONCAT(mtd); | |
611 | int i; | |
612 | ||
613 | for (i = 0; i < concat->num_subdev; i++) { | |
614 | struct mtd_info *subdev = concat->subdev[i]; | |
615 | subdev->resume(subdev); | |
616 | } | |
617 | } | |
618 | ||
e8d32937 AB |
619 | static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs) |
620 | { | |
621 | struct mtd_concat *concat = CONCAT(mtd); | |
622 | int i, res = 0; | |
623 | ||
624 | if (!concat->subdev[0]->block_isbad) | |
625 | return res; | |
626 | ||
627 | if (ofs > mtd->size) | |
628 | return -EINVAL; | |
629 | ||
630 | for (i = 0; i < concat->num_subdev; i++) { | |
631 | struct mtd_info *subdev = concat->subdev[i]; | |
632 | ||
633 | if (ofs >= subdev->size) { | |
634 | ofs -= subdev->size; | |
635 | continue; | |
636 | } | |
637 | ||
638 | res = subdev->block_isbad(subdev, ofs); | |
639 | break; | |
640 | } | |
641 | ||
642 | return res; | |
643 | } | |
644 | ||
645 | static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
646 | { | |
647 | struct mtd_concat *concat = CONCAT(mtd); | |
648 | int i, err = -EINVAL; | |
649 | ||
650 | if (!concat->subdev[0]->block_markbad) | |
651 | return 0; | |
652 | ||
653 | if (ofs > mtd->size) | |
654 | return -EINVAL; | |
655 | ||
656 | for (i = 0; i < concat->num_subdev; i++) { | |
657 | struct mtd_info *subdev = concat->subdev[i]; | |
658 | ||
659 | if (ofs >= subdev->size) { | |
660 | ofs -= subdev->size; | |
661 | continue; | |
662 | } | |
663 | ||
664 | err = subdev->block_markbad(subdev, ofs); | |
665 | break; | |
666 | } | |
667 | ||
668 | return err; | |
669 | } | |
670 | ||
1da177e4 LT |
671 | /* |
672 | * This function constructs a virtual MTD device by concatenating | |
673 | * num_devs MTD devices. A pointer to the new device object is | |
674 | * stored to *new_dev upon success. This function does _not_ | |
675 | * register any devices: this is the caller's responsibility. | |
676 | */ | |
677 | struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */ | |
678 | int num_devs, /* number of subdevices */ | |
679 | char *name) | |
680 | { /* name for the new device */ | |
681 | int i; | |
682 | size_t size; | |
683 | struct mtd_concat *concat; | |
684 | u_int32_t max_erasesize, curr_erasesize; | |
685 | int num_erase_region; | |
686 | ||
687 | printk(KERN_NOTICE "Concatenating MTD devices:\n"); | |
688 | for (i = 0; i < num_devs; i++) | |
689 | printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name); | |
690 | printk(KERN_NOTICE "into device \"%s\"\n", name); | |
691 | ||
692 | /* allocate the device structure */ | |
693 | size = SIZEOF_STRUCT_MTD_CONCAT(num_devs); | |
694 | concat = kmalloc(size, GFP_KERNEL); | |
695 | if (!concat) { | |
696 | printk | |
697 | ("memory allocation error while creating concatenated device \"%s\"\n", | |
698 | name); | |
699 | return NULL; | |
700 | } | |
701 | memset(concat, 0, size); | |
702 | concat->subdev = (struct mtd_info **) (concat + 1); | |
703 | ||
704 | /* | |
705 | * Set up the new "super" device's MTD object structure, check for | |
706 | * incompatibilites between the subdevices. | |
707 | */ | |
708 | concat->mtd.type = subdev[0]->type; | |
709 | concat->mtd.flags = subdev[0]->flags; | |
710 | concat->mtd.size = subdev[0]->size; | |
711 | concat->mtd.erasesize = subdev[0]->erasesize; | |
28318776 | 712 | concat->mtd.writesize = subdev[0]->writesize; |
1da177e4 LT |
713 | concat->mtd.oobsize = subdev[0]->oobsize; |
714 | concat->mtd.ecctype = subdev[0]->ecctype; | |
715 | concat->mtd.eccsize = subdev[0]->eccsize; | |
e8d32937 AB |
716 | if (subdev[0]->writev) |
717 | concat->mtd.writev = concat_writev; | |
1da177e4 LT |
718 | if (subdev[0]->read_oob) |
719 | concat->mtd.read_oob = concat_read_oob; | |
720 | if (subdev[0]->write_oob) | |
721 | concat->mtd.write_oob = concat_write_oob; | |
e8d32937 AB |
722 | if (subdev[0]->block_isbad) |
723 | concat->mtd.block_isbad = concat_block_isbad; | |
724 | if (subdev[0]->block_markbad) | |
725 | concat->mtd.block_markbad = concat_block_markbad; | |
1da177e4 LT |
726 | |
727 | concat->subdev[0] = subdev[0]; | |
728 | ||
729 | for (i = 1; i < num_devs; i++) { | |
730 | if (concat->mtd.type != subdev[i]->type) { | |
731 | kfree(concat); | |
732 | printk("Incompatible device type on \"%s\"\n", | |
733 | subdev[i]->name); | |
734 | return NULL; | |
735 | } | |
736 | if (concat->mtd.flags != subdev[i]->flags) { | |
737 | /* | |
738 | * Expect all flags except MTD_WRITEABLE to be | |
739 | * equal on all subdevices. | |
740 | */ | |
741 | if ((concat->mtd.flags ^ subdev[i]-> | |
742 | flags) & ~MTD_WRITEABLE) { | |
743 | kfree(concat); | |
744 | printk("Incompatible device flags on \"%s\"\n", | |
745 | subdev[i]->name); | |
746 | return NULL; | |
747 | } else | |
748 | /* if writeable attribute differs, | |
749 | make super device writeable */ | |
750 | concat->mtd.flags |= | |
751 | subdev[i]->flags & MTD_WRITEABLE; | |
752 | } | |
753 | concat->mtd.size += subdev[i]->size; | |
28318776 | 754 | if (concat->mtd.writesize != subdev[i]->writesize || |
1da177e4 LT |
755 | concat->mtd.oobsize != subdev[i]->oobsize || |
756 | concat->mtd.ecctype != subdev[i]->ecctype || | |
757 | concat->mtd.eccsize != subdev[i]->eccsize || | |
1da177e4 LT |
758 | !concat->mtd.read_oob != !subdev[i]->read_oob || |
759 | !concat->mtd.write_oob != !subdev[i]->write_oob) { | |
760 | kfree(concat); | |
761 | printk("Incompatible OOB or ECC data on \"%s\"\n", | |
762 | subdev[i]->name); | |
763 | return NULL; | |
764 | } | |
765 | concat->subdev[i] = subdev[i]; | |
766 | ||
767 | } | |
768 | ||
ff268fb8 | 769 | concat->mtd.oobinfo = subdev[0]->oobinfo; |
e8d32937 | 770 | |
1da177e4 LT |
771 | concat->num_subdev = num_devs; |
772 | concat->mtd.name = name; | |
773 | ||
1da177e4 LT |
774 | concat->mtd.erase = concat_erase; |
775 | concat->mtd.read = concat_read; | |
776 | concat->mtd.write = concat_write; | |
777 | concat->mtd.sync = concat_sync; | |
778 | concat->mtd.lock = concat_lock; | |
779 | concat->mtd.unlock = concat_unlock; | |
780 | concat->mtd.suspend = concat_suspend; | |
781 | concat->mtd.resume = concat_resume; | |
782 | ||
783 | /* | |
784 | * Combine the erase block size info of the subdevices: | |
785 | * | |
786 | * first, walk the map of the new device and see how | |
787 | * many changes in erase size we have | |
788 | */ | |
789 | max_erasesize = curr_erasesize = subdev[0]->erasesize; | |
790 | num_erase_region = 1; | |
791 | for (i = 0; i < num_devs; i++) { | |
792 | if (subdev[i]->numeraseregions == 0) { | |
793 | /* current subdevice has uniform erase size */ | |
794 | if (subdev[i]->erasesize != curr_erasesize) { | |
795 | /* if it differs from the last subdevice's erase size, count it */ | |
796 | ++num_erase_region; | |
797 | curr_erasesize = subdev[i]->erasesize; | |
798 | if (curr_erasesize > max_erasesize) | |
799 | max_erasesize = curr_erasesize; | |
800 | } | |
801 | } else { | |
802 | /* current subdevice has variable erase size */ | |
803 | int j; | |
804 | for (j = 0; j < subdev[i]->numeraseregions; j++) { | |
805 | ||
806 | /* walk the list of erase regions, count any changes */ | |
807 | if (subdev[i]->eraseregions[j].erasesize != | |
808 | curr_erasesize) { | |
809 | ++num_erase_region; | |
810 | curr_erasesize = | |
811 | subdev[i]->eraseregions[j]. | |
812 | erasesize; | |
813 | if (curr_erasesize > max_erasesize) | |
814 | max_erasesize = curr_erasesize; | |
815 | } | |
816 | } | |
817 | } | |
818 | } | |
819 | ||
820 | if (num_erase_region == 1) { | |
821 | /* | |
822 | * All subdevices have the same uniform erase size. | |
823 | * This is easy: | |
824 | */ | |
825 | concat->mtd.erasesize = curr_erasesize; | |
826 | concat->mtd.numeraseregions = 0; | |
827 | } else { | |
828 | /* | |
829 | * erase block size varies across the subdevices: allocate | |
830 | * space to store the data describing the variable erase regions | |
831 | */ | |
832 | struct mtd_erase_region_info *erase_region_p; | |
833 | u_int32_t begin, position; | |
834 | ||
835 | concat->mtd.erasesize = max_erasesize; | |
836 | concat->mtd.numeraseregions = num_erase_region; | |
837 | concat->mtd.eraseregions = erase_region_p = | |
838 | kmalloc(num_erase_region * | |
839 | sizeof (struct mtd_erase_region_info), GFP_KERNEL); | |
840 | if (!erase_region_p) { | |
841 | kfree(concat); | |
842 | printk | |
843 | ("memory allocation error while creating erase region list" | |
844 | " for device \"%s\"\n", name); | |
845 | return NULL; | |
846 | } | |
847 | ||
848 | /* | |
849 | * walk the map of the new device once more and fill in | |
850 | * in erase region info: | |
851 | */ | |
852 | curr_erasesize = subdev[0]->erasesize; | |
853 | begin = position = 0; | |
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 | /* | |
859 | * fill in an mtd_erase_region_info structure for the area | |
860 | * we have walked so far: | |
861 | */ | |
862 | erase_region_p->offset = begin; | |
863 | erase_region_p->erasesize = | |
864 | curr_erasesize; | |
865 | erase_region_p->numblocks = | |
866 | (position - begin) / curr_erasesize; | |
867 | begin = position; | |
868 | ||
869 | curr_erasesize = subdev[i]->erasesize; | |
870 | ++erase_region_p; | |
871 | } | |
872 | position += subdev[i]->size; | |
873 | } else { | |
874 | /* current subdevice has variable erase size */ | |
875 | int j; | |
876 | for (j = 0; j < subdev[i]->numeraseregions; j++) { | |
877 | /* walk the list of erase regions, count any changes */ | |
878 | if (subdev[i]->eraseregions[j]. | |
879 | erasesize != curr_erasesize) { | |
880 | erase_region_p->offset = begin; | |
881 | erase_region_p->erasesize = | |
882 | curr_erasesize; | |
883 | erase_region_p->numblocks = | |
884 | (position - | |
885 | begin) / curr_erasesize; | |
886 | begin = position; | |
887 | ||
888 | curr_erasesize = | |
889 | subdev[i]->eraseregions[j]. | |
890 | erasesize; | |
891 | ++erase_region_p; | |
892 | } | |
893 | position += | |
894 | subdev[i]->eraseregions[j]. | |
895 | numblocks * curr_erasesize; | |
896 | } | |
897 | } | |
898 | } | |
899 | /* Now write the final entry */ | |
900 | erase_region_p->offset = begin; | |
901 | erase_region_p->erasesize = curr_erasesize; | |
902 | erase_region_p->numblocks = (position - begin) / curr_erasesize; | |
903 | } | |
904 | ||
905 | return &concat->mtd; | |
906 | } | |
907 | ||
97894cda | 908 | /* |
1da177e4 LT |
909 | * This function destroys an MTD object obtained from concat_mtd_devs() |
910 | */ | |
911 | ||
912 | void mtd_concat_destroy(struct mtd_info *mtd) | |
913 | { | |
914 | struct mtd_concat *concat = CONCAT(mtd); | |
915 | if (concat->mtd.numeraseregions) | |
916 | kfree(concat->mtd.eraseregions); | |
917 | kfree(concat); | |
918 | } | |
919 | ||
920 | EXPORT_SYMBOL(mtd_concat_create); | |
921 | EXPORT_SYMBOL(mtd_concat_destroy); | |
922 | ||
923 | MODULE_LICENSE("GPL"); | |
924 | MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>"); | |
925 | MODULE_DESCRIPTION("Generic support for concatenating of MTD devices"); |