blob: 684bc949985b0f7916eb201333697a9860e23800 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese0a572652009-05-12 14:29:39 +02002/*
3 * MTD device concatenation layer
4 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02005 * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
6 * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
Stefan Roese0a572652009-05-12 14:29:39 +02007 *
8 * NAND support by Christian Gan <cgan@iders.ca>
9 *
Stefan Roese0a572652009-05-12 14:29:39 +020010 */
11
Heiko Schocherff94bc42014-06-24 10:10:04 +020012#ifndef __UBOOT__
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass61b29b82020-02-03 07:36:15 -070014#include <dm/devres.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/sched.h>
19#include <linux/types.h>
20#include <linux/backing-dev.h>
21#include <asm/div64.h>
22#else
23#include <div64.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060024#include <linux/bug.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000025#include <linux/compat.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020026#endif
27
28#include <linux/mtd/mtd.h>
Stefan Roese0a572652009-05-12 14:29:39 +020029#include <linux/mtd/concat.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020030
Stefan Roese0a572652009-05-12 14:29:39 +020031#include <ubi_uboot.h>
32
33/*
34 * Our storage structure:
35 * Subdev points to an array of pointers to struct mtd_info objects
36 * which is allocated along with this structure
37 *
38 */
39struct mtd_concat {
40 struct mtd_info mtd;
41 int num_subdev;
42 struct mtd_info **subdev;
43};
44
45/*
46 * how to calculate the size required for the above structure,
47 * including the pointer array subdev points to:
48 */
49#define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
50 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
51
52/*
53 * Given a pointer to the MTD object in the mtd_concat structure,
54 * we can retrieve the pointer to that structure with this macro.
55 */
56#define CONCAT(x) ((struct mtd_concat *)(x))
57
58/*
59 * MTD methods which look up the relevant subdevice, translate the
60 * effective address and pass through to the subdevice.
61 */
62
63static int
64concat_read(struct mtd_info *mtd, loff_t from, size_t len,
65 size_t * retlen, u_char * buf)
66{
67 struct mtd_concat *concat = CONCAT(mtd);
68 int ret = 0, err;
69 int i;
70
Heiko Schocherff94bc42014-06-24 10:10:04 +020071#ifdef __UBOOT__
Stefan Roese0a572652009-05-12 14:29:39 +020072 *retlen = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +020073#endif
Stefan Roese0a572652009-05-12 14:29:39 +020074
75 for (i = 0; i < concat->num_subdev; i++) {
76 struct mtd_info *subdev = concat->subdev[i];
77 size_t size, retsize;
78
79 if (from >= subdev->size) {
80 /* Not destined for this subdev */
81 size = 0;
82 from -= subdev->size;
83 continue;
84 }
85 if (from + len > subdev->size)
86 /* First part goes into this subdev */
87 size = subdev->size - from;
88 else
89 /* Entire transaction goes into this subdev */
90 size = len;
91
Sergey Lapindfe64e22013-01-14 03:46:50 +000092 err = mtd_read(subdev, from, size, &retsize, buf);
Stefan Roese0a572652009-05-12 14:29:39 +020093
94 /* Save information about bitflips! */
95 if (unlikely(err)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +000096 if (mtd_is_eccerr(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +020097 mtd->ecc_stats.failed++;
98 ret = err;
Sergey Lapindfe64e22013-01-14 03:46:50 +000099 } else if (mtd_is_bitflip(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +0200100 mtd->ecc_stats.corrected++;
101 /* Do not overwrite -EBADMSG !! */
102 if (!ret)
103 ret = err;
104 } else
105 return err;
106 }
107
108 *retlen += retsize;
109 len -= size;
110 if (len == 0)
111 return ret;
112
113 buf += size;
114 from = 0;
115 }
116 return -EINVAL;
117}
118
119static int
120concat_write(struct mtd_info *mtd, loff_t to, size_t len,
121 size_t * retlen, const u_char * buf)
122{
123 struct mtd_concat *concat = CONCAT(mtd);
124 int err = -EINVAL;
125 int i;
126
Heiko Schocherff94bc42014-06-24 10:10:04 +0200127#ifdef __UBOOT__
Stefan Roese0a572652009-05-12 14:29:39 +0200128 *retlen = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200129#endif
Stefan Roese0a572652009-05-12 14:29:39 +0200130
131 for (i = 0; i < concat->num_subdev; i++) {
132 struct mtd_info *subdev = concat->subdev[i];
133 size_t size, retsize;
134
135 if (to >= subdev->size) {
136 size = 0;
137 to -= subdev->size;
138 continue;
139 }
140 if (to + len > subdev->size)
141 size = subdev->size - to;
142 else
143 size = len;
144
Sergey Lapindfe64e22013-01-14 03:46:50 +0000145 err = mtd_write(subdev, to, size, &retsize, buf);
Stefan Roese0a572652009-05-12 14:29:39 +0200146 if (err)
147 break;
148
149 *retlen += retsize;
150 len -= size;
151 if (len == 0)
152 break;
153
154 err = -EINVAL;
155 buf += size;
156 to = 0;
157 }
158 return err;
159}
160
Heiko Schocherff94bc42014-06-24 10:10:04 +0200161#ifndef __UBOOT__
162static int
163concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
164 unsigned long count, loff_t to, size_t * retlen)
165{
166 struct mtd_concat *concat = CONCAT(mtd);
167 struct kvec *vecs_copy;
168 unsigned long entry_low, entry_high;
169 size_t total_len = 0;
170 int i;
171 int err = -EINVAL;
172
173 /* Calculate total length of data */
174 for (i = 0; i < count; i++)
175 total_len += vecs[i].iov_len;
176
177 /* Check alignment */
178 if (mtd->writesize > 1) {
179 uint64_t __to = to;
180 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
181 return -EINVAL;
182 }
183
184 /* make a copy of vecs */
185 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
186 if (!vecs_copy)
187 return -ENOMEM;
188
189 entry_low = 0;
190 for (i = 0; i < concat->num_subdev; i++) {
191 struct mtd_info *subdev = concat->subdev[i];
192 size_t size, wsize, retsize, old_iov_len;
193
194 if (to >= subdev->size) {
195 to -= subdev->size;
196 continue;
197 }
198
199 size = min_t(uint64_t, total_len, subdev->size - to);
200 wsize = size; /* store for future use */
201
202 entry_high = entry_low;
203 while (entry_high < count) {
204 if (size <= vecs_copy[entry_high].iov_len)
205 break;
206 size -= vecs_copy[entry_high++].iov_len;
207 }
208
209 old_iov_len = vecs_copy[entry_high].iov_len;
210 vecs_copy[entry_high].iov_len = size;
211
212 err = mtd_writev(subdev, &vecs_copy[entry_low],
213 entry_high - entry_low + 1, to, &retsize);
214
215 vecs_copy[entry_high].iov_len = old_iov_len - size;
216 vecs_copy[entry_high].iov_base += size;
217
218 entry_low = entry_high;
219
220 if (err)
221 break;
222
223 *retlen += retsize;
224 total_len -= wsize;
225
226 if (total_len == 0)
227 break;
228
229 err = -EINVAL;
230 to = 0;
231 }
232
233 kfree(vecs_copy);
234 return err;
235}
236#endif
237
Stefan Roese0a572652009-05-12 14:29:39 +0200238static int
239concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
240{
241 struct mtd_concat *concat = CONCAT(mtd);
242 struct mtd_oob_ops devops = *ops;
243 int i, err, ret = 0;
244
245 ops->retlen = ops->oobretlen = 0;
246
247 for (i = 0; i < concat->num_subdev; i++) {
248 struct mtd_info *subdev = concat->subdev[i];
249
250 if (from >= subdev->size) {
251 from -= subdev->size;
252 continue;
253 }
254
255 /* partial read ? */
256 if (from + devops.len > subdev->size)
257 devops.len = subdev->size - from;
258
Sergey Lapindfe64e22013-01-14 03:46:50 +0000259 err = mtd_read_oob(subdev, from, &devops);
Stefan Roese0a572652009-05-12 14:29:39 +0200260 ops->retlen += devops.retlen;
261 ops->oobretlen += devops.oobretlen;
262
263 /* Save information about bitflips! */
264 if (unlikely(err)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000265 if (mtd_is_eccerr(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +0200266 mtd->ecc_stats.failed++;
267 ret = err;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000268 } else if (mtd_is_bitflip(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +0200269 mtd->ecc_stats.corrected++;
270 /* Do not overwrite -EBADMSG !! */
271 if (!ret)
272 ret = err;
273 } else
274 return err;
275 }
276
277 if (devops.datbuf) {
278 devops.len = ops->len - ops->retlen;
279 if (!devops.len)
280 return ret;
281 devops.datbuf += devops.retlen;
282 }
283 if (devops.oobbuf) {
284 devops.ooblen = ops->ooblen - ops->oobretlen;
285 if (!devops.ooblen)
286 return ret;
287 devops.oobbuf += ops->oobretlen;
288 }
289
290 from = 0;
291 }
292 return -EINVAL;
293}
294
295static int
296concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
297{
298 struct mtd_concat *concat = CONCAT(mtd);
299 struct mtd_oob_ops devops = *ops;
300 int i, err;
301
302 if (!(mtd->flags & MTD_WRITEABLE))
303 return -EROFS;
304
Heiko Schocherff94bc42014-06-24 10:10:04 +0200305 ops->retlen = ops->oobretlen = 0;
Stefan Roese0a572652009-05-12 14:29:39 +0200306
307 for (i = 0; i < concat->num_subdev; i++) {
308 struct mtd_info *subdev = concat->subdev[i];
309
310 if (to >= subdev->size) {
311 to -= subdev->size;
312 continue;
313 }
314
315 /* partial write ? */
316 if (to + devops.len > subdev->size)
317 devops.len = subdev->size - to;
318
Sergey Lapindfe64e22013-01-14 03:46:50 +0000319 err = mtd_write_oob(subdev, to, &devops);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200320 ops->retlen += devops.oobretlen;
Stefan Roese0a572652009-05-12 14:29:39 +0200321 if (err)
322 return err;
323
324 if (devops.datbuf) {
325 devops.len = ops->len - ops->retlen;
326 if (!devops.len)
327 return 0;
328 devops.datbuf += devops.retlen;
329 }
330 if (devops.oobbuf) {
331 devops.ooblen = ops->ooblen - ops->oobretlen;
332 if (!devops.ooblen)
333 return 0;
334 devops.oobbuf += devops.oobretlen;
335 }
336 to = 0;
337 }
338 return -EINVAL;
339}
340
341static void concat_erase_callback(struct erase_info *instr)
342{
343 /* Nothing to do here in U-Boot */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200344#ifndef __UBOOT__
345 wake_up((wait_queue_head_t *) instr->priv);
346#endif
Stefan Roese0a572652009-05-12 14:29:39 +0200347}
348
349static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
350{
351 int err;
352 wait_queue_head_t waitq;
353 DECLARE_WAITQUEUE(wait, current);
354
355 /*
356 * This code was stol^H^H^H^Hinspired by mtdchar.c
357 */
358 init_waitqueue_head(&waitq);
359
360 erase->mtd = mtd;
361 erase->callback = concat_erase_callback;
362 erase->priv = (unsigned long) &waitq;
363
364 /*
365 * FIXME: Allow INTERRUPTIBLE. Which means
366 * not having the wait_queue head on the stack.
367 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000368 err = mtd_erase(mtd, erase);
Stefan Roese0a572652009-05-12 14:29:39 +0200369 if (!err) {
370 set_current_state(TASK_UNINTERRUPTIBLE);
371 add_wait_queue(&waitq, &wait);
372 if (erase->state != MTD_ERASE_DONE
373 && erase->state != MTD_ERASE_FAILED)
374 schedule();
375 remove_wait_queue(&waitq, &wait);
376 set_current_state(TASK_RUNNING);
377
378 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
379 }
380 return err;
381}
382
383static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
384{
385 struct mtd_concat *concat = CONCAT(mtd);
386 struct mtd_info *subdev;
387 int i, err;
388 uint64_t length, offset = 0;
389 struct erase_info *erase;
390
Stefan Roese0a572652009-05-12 14:29:39 +0200391 /*
392 * Check for proper erase block alignment of the to-be-erased area.
393 * It is easier to do this based on the super device's erase
394 * region info rather than looking at each particular sub-device
395 * in turn.
396 */
397 if (!concat->mtd.numeraseregions) {
398 /* the easy case: device has uniform erase block size */
399 if (instr->addr & (concat->mtd.erasesize - 1))
400 return -EINVAL;
401 if (instr->len & (concat->mtd.erasesize - 1))
402 return -EINVAL;
403 } else {
404 /* device has variable erase size */
405 struct mtd_erase_region_info *erase_regions =
406 concat->mtd.eraseregions;
407
408 /*
409 * Find the erase region where the to-be-erased area begins:
410 */
411 for (i = 0; i < concat->mtd.numeraseregions &&
412 instr->addr >= erase_regions[i].offset; i++) ;
413 --i;
414
415 /*
416 * Now erase_regions[i] is the region in which the
417 * to-be-erased area begins. Verify that the starting
418 * offset is aligned to this region's erase size:
419 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200420 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
Stefan Roese0a572652009-05-12 14:29:39 +0200421 return -EINVAL;
422
423 /*
424 * now find the erase region where the to-be-erased area ends:
425 */
426 for (; i < concat->mtd.numeraseregions &&
427 (instr->addr + instr->len) >= erase_regions[i].offset;
428 ++i) ;
429 --i;
430 /*
431 * check if the ending offset is aligned to this region's erase size
432 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200433 if (i < 0 || ((instr->addr + instr->len) &
434 (erase_regions[i].erasesize - 1)))
Stefan Roese0a572652009-05-12 14:29:39 +0200435 return -EINVAL;
436 }
437
Stefan Roese0a572652009-05-12 14:29:39 +0200438 /* make a local copy of instr to avoid modifying the caller's struct */
439 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
440
441 if (!erase)
442 return -ENOMEM;
443
444 *erase = *instr;
445 length = instr->len;
446
447 /*
448 * find the subdevice where the to-be-erased area begins, adjust
449 * starting offset to be relative to the subdevice start
450 */
451 for (i = 0; i < concat->num_subdev; i++) {
452 subdev = concat->subdev[i];
453 if (subdev->size <= erase->addr) {
454 erase->addr -= subdev->size;
455 offset += subdev->size;
456 } else {
457 break;
458 }
459 }
460
461 /* must never happen since size limit has been verified above */
462 BUG_ON(i >= concat->num_subdev);
463
464 /* now do the erase: */
465 err = 0;
466 for (; length > 0; i++) {
467 /* loop for all subdevices affected by this request */
468 subdev = concat->subdev[i]; /* get current subdevice */
469
470 /* limit length to subdevice's size: */
471 if (erase->addr + length > subdev->size)
472 erase->len = subdev->size - erase->addr;
473 else
474 erase->len = length;
475
Stefan Roese0a572652009-05-12 14:29:39 +0200476 length -= erase->len;
477 if ((err = concat_dev_erase(subdev, erase))) {
478 /* sanity check: should never happen since
479 * block alignment has been checked above */
480 BUG_ON(err == -EINVAL);
481 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
482 instr->fail_addr = erase->fail_addr + offset;
483 break;
484 }
485 /*
486 * erase->addr specifies the offset of the area to be
487 * erased *within the current subdevice*. It can be
488 * non-zero only the first time through this loop, i.e.
489 * for the first subdevice where blocks need to be erased.
490 * All the following erases must begin at the start of the
491 * current subdevice, i.e. at offset zero.
492 */
493 erase->addr = 0;
494 offset += subdev->size;
495 }
496 instr->state = erase->state;
497 kfree(erase);
498 if (err)
499 return err;
500
501 if (instr->callback)
502 instr->callback(instr);
503 return 0;
504}
505
506static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
507{
508 struct mtd_concat *concat = CONCAT(mtd);
509 int i, err = -EINVAL;
510
Stefan Roese0a572652009-05-12 14:29:39 +0200511 for (i = 0; i < concat->num_subdev; i++) {
512 struct mtd_info *subdev = concat->subdev[i];
513 uint64_t size;
514
515 if (ofs >= subdev->size) {
516 size = 0;
517 ofs -= subdev->size;
518 continue;
519 }
520 if (ofs + len > subdev->size)
521 size = subdev->size - ofs;
522 else
523 size = len;
524
Sergey Lapindfe64e22013-01-14 03:46:50 +0000525 err = mtd_lock(subdev, ofs, size);
Stefan Roese0a572652009-05-12 14:29:39 +0200526 if (err)
527 break;
528
529 len -= size;
530 if (len == 0)
531 break;
532
533 err = -EINVAL;
534 ofs = 0;
535 }
536
537 return err;
538}
539
540static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
541{
542 struct mtd_concat *concat = CONCAT(mtd);
543 int i, err = 0;
544
Stefan Roese0a572652009-05-12 14:29:39 +0200545 for (i = 0; i < concat->num_subdev; i++) {
546 struct mtd_info *subdev = concat->subdev[i];
547 uint64_t size;
548
549 if (ofs >= subdev->size) {
550 size = 0;
551 ofs -= subdev->size;
552 continue;
553 }
554 if (ofs + len > subdev->size)
555 size = subdev->size - ofs;
556 else
557 size = len;
558
Sergey Lapindfe64e22013-01-14 03:46:50 +0000559 err = mtd_unlock(subdev, ofs, size);
Stefan Roese0a572652009-05-12 14:29:39 +0200560 if (err)
561 break;
562
563 len -= size;
564 if (len == 0)
565 break;
566
567 err = -EINVAL;
568 ofs = 0;
569 }
570
571 return err;
572}
573
574static void concat_sync(struct mtd_info *mtd)
575{
576 struct mtd_concat *concat = CONCAT(mtd);
577 int i;
578
579 for (i = 0; i < concat->num_subdev; i++) {
580 struct mtd_info *subdev = concat->subdev[i];
Sergey Lapindfe64e22013-01-14 03:46:50 +0000581 mtd_sync(subdev);
Stefan Roese0a572652009-05-12 14:29:39 +0200582 }
583}
584
Heiko Schocherff94bc42014-06-24 10:10:04 +0200585#ifndef __UBOOT__
586static int concat_suspend(struct mtd_info *mtd)
587{
588 struct mtd_concat *concat = CONCAT(mtd);
589 int i, rc = 0;
590
591 for (i = 0; i < concat->num_subdev; i++) {
592 struct mtd_info *subdev = concat->subdev[i];
593 if ((rc = mtd_suspend(subdev)) < 0)
594 return rc;
595 }
596 return rc;
597}
598
599static void concat_resume(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 mtd_resume(subdev);
607 }
608}
609#endif
610
Stefan Roese0a572652009-05-12 14:29:39 +0200611static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
612{
613 struct mtd_concat *concat = CONCAT(mtd);
614 int i, res = 0;
615
Sergey Lapindfe64e22013-01-14 03:46:50 +0000616 if (!mtd_can_have_bb(concat->subdev[0]))
Stefan Roese0a572652009-05-12 14:29:39 +0200617 return res;
618
Stefan Roese0a572652009-05-12 14:29:39 +0200619 for (i = 0; i < concat->num_subdev; i++) {
620 struct mtd_info *subdev = concat->subdev[i];
621
622 if (ofs >= subdev->size) {
623 ofs -= subdev->size;
624 continue;
625 }
626
Sergey Lapindfe64e22013-01-14 03:46:50 +0000627 res = mtd_block_isbad(subdev, ofs);
Stefan Roese0a572652009-05-12 14:29:39 +0200628 break;
629 }
630
631 return res;
632}
633
634static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
635{
636 struct mtd_concat *concat = CONCAT(mtd);
637 int i, err = -EINVAL;
638
Stefan Roese0a572652009-05-12 14:29:39 +0200639 for (i = 0; i < concat->num_subdev; i++) {
640 struct mtd_info *subdev = concat->subdev[i];
641
642 if (ofs >= subdev->size) {
643 ofs -= subdev->size;
644 continue;
645 }
646
Sergey Lapindfe64e22013-01-14 03:46:50 +0000647 err = mtd_block_markbad(subdev, ofs);
Stefan Roese0a572652009-05-12 14:29:39 +0200648 if (!err)
649 mtd->ecc_stats.badblocks++;
650 break;
651 }
652
653 return err;
654}
655
656/*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200657 * try to support NOMMU mmaps on concatenated devices
658 * - we don't support subdev spanning as we can't guarantee it'll work
659 */
660static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
661 unsigned long len,
662 unsigned long offset,
663 unsigned long flags)
664{
665 struct mtd_concat *concat = CONCAT(mtd);
666 int i;
667
668 for (i = 0; i < concat->num_subdev; i++) {
669 struct mtd_info *subdev = concat->subdev[i];
670
671 if (offset >= subdev->size) {
672 offset -= subdev->size;
673 continue;
674 }
675
676 return mtd_get_unmapped_area(subdev, len, offset, flags);
677 }
678
679 return (unsigned long) -ENOSYS;
680}
681
682/*
Stefan Roese0a572652009-05-12 14:29:39 +0200683 * This function constructs a virtual MTD device by concatenating
684 * num_devs MTD devices. A pointer to the new device object is
685 * stored to *new_dev upon success. This function does _not_
686 * register any devices: this is the caller's responsibility.
687 */
688struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
689 int num_devs, /* number of subdevices */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200690#ifndef __UBOOT__
Stefan Roese0a572652009-05-12 14:29:39 +0200691 const char *name)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200692#else
693 char *name)
694#endif
Stefan Roese0a572652009-05-12 14:29:39 +0200695{ /* name for the new device */
696 int i;
697 size_t size;
698 struct mtd_concat *concat;
699 uint32_t max_erasesize, curr_erasesize;
700 int num_erase_region;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200701 int max_writebufsize = 0;
Stefan Roese0a572652009-05-12 14:29:39 +0200702
703 debug("Concatenating MTD devices:\n");
704 for (i = 0; i < num_devs; i++)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200705 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
Stefan Roese0a572652009-05-12 14:29:39 +0200706 debug("into device \"%s\"\n", name);
707
708 /* allocate the device structure */
709 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
710 concat = kzalloc(size, GFP_KERNEL);
711 if (!concat) {
712 printk
713 ("memory allocation error while creating concatenated device \"%s\"\n",
714 name);
715 return NULL;
716 }
717 concat->subdev = (struct mtd_info **) (concat + 1);
718
719 /*
720 * Set up the new "super" device's MTD object structure, check for
Heiko Schocherff94bc42014-06-24 10:10:04 +0200721 * incompatibilities between the subdevices.
Stefan Roese0a572652009-05-12 14:29:39 +0200722 */
723 concat->mtd.type = subdev[0]->type;
724 concat->mtd.flags = subdev[0]->flags;
725 concat->mtd.size = subdev[0]->size;
726 concat->mtd.erasesize = subdev[0]->erasesize;
727 concat->mtd.writesize = subdev[0]->writesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200728
729 for (i = 0; i < num_devs; i++)
730 if (max_writebufsize < subdev[i]->writebufsize)
731 max_writebufsize = subdev[i]->writebufsize;
732 concat->mtd.writebufsize = max_writebufsize;
733
Stefan Roese0a572652009-05-12 14:29:39 +0200734 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
735 concat->mtd.oobsize = subdev[0]->oobsize;
736 concat->mtd.oobavail = subdev[0]->oobavail;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200737#ifndef __UBOOT__
738 if (subdev[0]->_writev)
739 concat->mtd._writev = concat_writev;
740#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000741 if (subdev[0]->_read_oob)
742 concat->mtd._read_oob = concat_read_oob;
743 if (subdev[0]->_write_oob)
744 concat->mtd._write_oob = concat_write_oob;
745 if (subdev[0]->_block_isbad)
746 concat->mtd._block_isbad = concat_block_isbad;
747 if (subdev[0]->_block_markbad)
748 concat->mtd._block_markbad = concat_block_markbad;
Stefan Roese0a572652009-05-12 14:29:39 +0200749
750 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
751
Heiko Schocherff94bc42014-06-24 10:10:04 +0200752#ifndef __UBOOT__
753 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
754#endif
755
Stefan Roese0a572652009-05-12 14:29:39 +0200756 concat->subdev[0] = subdev[0];
757
758 for (i = 1; i < num_devs; i++) {
759 if (concat->mtd.type != subdev[i]->type) {
760 kfree(concat);
761 printk("Incompatible device type on \"%s\"\n",
762 subdev[i]->name);
763 return NULL;
764 }
765 if (concat->mtd.flags != subdev[i]->flags) {
766 /*
767 * Expect all flags except MTD_WRITEABLE to be
768 * equal on all subdevices.
769 */
770 if ((concat->mtd.flags ^ subdev[i]->
771 flags) & ~MTD_WRITEABLE) {
772 kfree(concat);
773 printk("Incompatible device flags on \"%s\"\n",
774 subdev[i]->name);
775 return NULL;
776 } else
777 /* if writeable attribute differs,
778 make super device writeable */
779 concat->mtd.flags |=
780 subdev[i]->flags & MTD_WRITEABLE;
781 }
782
Heiko Schocherff94bc42014-06-24 10:10:04 +0200783#ifndef __UBOOT__
784 /* only permit direct mapping if the BDIs are all the same
785 * - copy-mapping is still permitted
786 */
787 if (concat->mtd.backing_dev_info !=
788 subdev[i]->backing_dev_info)
789 concat->mtd.backing_dev_info =
790 &default_backing_dev_info;
791#endif
792
Stefan Roese0a572652009-05-12 14:29:39 +0200793 concat->mtd.size += subdev[i]->size;
794 concat->mtd.ecc_stats.badblocks +=
795 subdev[i]->ecc_stats.badblocks;
796 if (concat->mtd.writesize != subdev[i]->writesize ||
797 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
798 concat->mtd.oobsize != subdev[i]->oobsize ||
Sergey Lapindfe64e22013-01-14 03:46:50 +0000799 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
800 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
Stefan Roese0a572652009-05-12 14:29:39 +0200801 kfree(concat);
802 printk("Incompatible OOB or ECC data on \"%s\"\n",
803 subdev[i]->name);
804 return NULL;
805 }
806 concat->subdev[i] = subdev[i];
807
808 }
809
810 concat->mtd.ecclayout = subdev[0]->ecclayout;
811
812 concat->num_subdev = num_devs;
813 concat->mtd.name = name;
814
Sergey Lapindfe64e22013-01-14 03:46:50 +0000815 concat->mtd._erase = concat_erase;
816 concat->mtd._read = concat_read;
817 concat->mtd._write = concat_write;
818 concat->mtd._sync = concat_sync;
819 concat->mtd._lock = concat_lock;
820 concat->mtd._unlock = concat_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200821#ifndef __UBOOT__
822 concat->mtd._suspend = concat_suspend;
823 concat->mtd._resume = concat_resume;
824#endif
825 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
Stefan Roese0a572652009-05-12 14:29:39 +0200826
827 /*
828 * Combine the erase block size info of the subdevices:
829 *
830 * first, walk the map of the new device and see how
831 * many changes in erase size we have
832 */
833 max_erasesize = curr_erasesize = subdev[0]->erasesize;
834 num_erase_region = 1;
835 for (i = 0; i < num_devs; i++) {
836 if (subdev[i]->numeraseregions == 0) {
837 /* current subdevice has uniform erase size */
838 if (subdev[i]->erasesize != curr_erasesize) {
839 /* if it differs from the last subdevice's erase size, count it */
840 ++num_erase_region;
841 curr_erasesize = subdev[i]->erasesize;
842 if (curr_erasesize > max_erasesize)
843 max_erasesize = curr_erasesize;
844 }
845 } else {
846 /* current subdevice has variable erase size */
847 int j;
848 for (j = 0; j < subdev[i]->numeraseregions; j++) {
849
850 /* walk the list of erase regions, count any changes */
851 if (subdev[i]->eraseregions[j].erasesize !=
852 curr_erasesize) {
853 ++num_erase_region;
854 curr_erasesize =
855 subdev[i]->eraseregions[j].
856 erasesize;
857 if (curr_erasesize > max_erasesize)
858 max_erasesize = curr_erasesize;
859 }
860 }
861 }
862 }
863
864 if (num_erase_region == 1) {
865 /*
866 * All subdevices have the same uniform erase size.
867 * This is easy:
868 */
869 concat->mtd.erasesize = curr_erasesize;
870 concat->mtd.numeraseregions = 0;
871 } else {
872 uint64_t tmp64;
873
874 /*
875 * erase block size varies across the subdevices: allocate
876 * space to store the data describing the variable erase regions
877 */
878 struct mtd_erase_region_info *erase_region_p;
879 uint64_t begin, position;
880
881 concat->mtd.erasesize = max_erasesize;
882 concat->mtd.numeraseregions = num_erase_region;
883 concat->mtd.eraseregions = erase_region_p =
884 kmalloc(num_erase_region *
885 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
886 if (!erase_region_p) {
887 kfree(concat);
888 printk
889 ("memory allocation error while creating erase region list"
890 " for device \"%s\"\n", name);
891 return NULL;
892 }
893
894 /*
895 * walk the map of the new device once more and fill in
896 * in erase region info:
897 */
898 curr_erasesize = subdev[0]->erasesize;
899 begin = position = 0;
900 for (i = 0; i < num_devs; i++) {
901 if (subdev[i]->numeraseregions == 0) {
902 /* current subdevice has uniform erase size */
903 if (subdev[i]->erasesize != curr_erasesize) {
904 /*
905 * fill in an mtd_erase_region_info structure for the area
906 * we have walked so far:
907 */
908 erase_region_p->offset = begin;
909 erase_region_p->erasesize =
910 curr_erasesize;
911 tmp64 = position - begin;
912 do_div(tmp64, curr_erasesize);
913 erase_region_p->numblocks = tmp64;
914 begin = position;
915
916 curr_erasesize = subdev[i]->erasesize;
917 ++erase_region_p;
918 }
919 position += subdev[i]->size;
920 } else {
921 /* current subdevice has variable erase size */
922 int j;
923 for (j = 0; j < subdev[i]->numeraseregions; j++) {
924 /* walk the list of erase regions, count any changes */
925 if (subdev[i]->eraseregions[j].
926 erasesize != curr_erasesize) {
927 erase_region_p->offset = begin;
928 erase_region_p->erasesize =
929 curr_erasesize;
930 tmp64 = position - begin;
931 do_div(tmp64, curr_erasesize);
932 erase_region_p->numblocks = tmp64;
933 begin = position;
934
935 curr_erasesize =
936 subdev[i]->eraseregions[j].
937 erasesize;
938 ++erase_region_p;
939 }
940 position +=
941 subdev[i]->eraseregions[j].
942 numblocks * (uint64_t)curr_erasesize;
943 }
944 }
945 }
946 /* Now write the final entry */
947 erase_region_p->offset = begin;
948 erase_region_p->erasesize = curr_erasesize;
949 tmp64 = position - begin;
950 do_div(tmp64, curr_erasesize);
951 erase_region_p->numblocks = tmp64;
952 }
953
954 return &concat->mtd;
955}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200956
957/*
958 * This function destroys an MTD object obtained from concat_mtd_devs()
959 */
960
961void mtd_concat_destroy(struct mtd_info *mtd)
962{
963 struct mtd_concat *concat = CONCAT(mtd);
964 if (concat->mtd.numeraseregions)
965 kfree(concat->mtd.eraseregions);
966 kfree(concat);
967}
968
969EXPORT_SYMBOL(mtd_concat_create);
970EXPORT_SYMBOL(mtd_concat_destroy);
971
972MODULE_LICENSE("GPL");
973MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
974MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");