blob: 51232581d966aa58986d9fa8e0353c37b0600e51 [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>
Simon Glass1e94b462023-09-14 18:21:46 -060026#include <linux/printk.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020027#endif
28
29#include <linux/mtd/mtd.h>
Stefan Roese0a572652009-05-12 14:29:39 +020030#include <linux/mtd/concat.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020031
Stefan Roese0a572652009-05-12 14:29:39 +020032#include <ubi_uboot.h>
33
34/*
35 * Our storage structure:
36 * Subdev points to an array of pointers to struct mtd_info objects
37 * which is allocated along with this structure
38 *
39 */
40struct mtd_concat {
41 struct mtd_info mtd;
42 int num_subdev;
43 struct mtd_info **subdev;
44};
45
46/*
47 * how to calculate the size required for the above structure,
48 * including the pointer array subdev points to:
49 */
50#define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
51 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
52
53/*
54 * Given a pointer to the MTD object in the mtd_concat structure,
55 * we can retrieve the pointer to that structure with this macro.
56 */
57#define CONCAT(x) ((struct mtd_concat *)(x))
58
59/*
60 * MTD methods which look up the relevant subdevice, translate the
61 * effective address and pass through to the subdevice.
62 */
63
64static int
65concat_read(struct mtd_info *mtd, loff_t from, size_t len,
66 size_t * retlen, u_char * buf)
67{
68 struct mtd_concat *concat = CONCAT(mtd);
69 int ret = 0, err;
70 int i;
71
Heiko Schocherff94bc42014-06-24 10:10:04 +020072#ifdef __UBOOT__
Stefan Roese0a572652009-05-12 14:29:39 +020073 *retlen = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +020074#endif
Stefan Roese0a572652009-05-12 14:29:39 +020075
76 for (i = 0; i < concat->num_subdev; i++) {
77 struct mtd_info *subdev = concat->subdev[i];
78 size_t size, retsize;
79
80 if (from >= subdev->size) {
81 /* Not destined for this subdev */
82 size = 0;
83 from -= subdev->size;
84 continue;
85 }
86 if (from + len > subdev->size)
87 /* First part goes into this subdev */
88 size = subdev->size - from;
89 else
90 /* Entire transaction goes into this subdev */
91 size = len;
92
Sergey Lapindfe64e22013-01-14 03:46:50 +000093 err = mtd_read(subdev, from, size, &retsize, buf);
Stefan Roese0a572652009-05-12 14:29:39 +020094
95 /* Save information about bitflips! */
96 if (unlikely(err)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +000097 if (mtd_is_eccerr(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +020098 mtd->ecc_stats.failed++;
99 ret = err;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000100 } else if (mtd_is_bitflip(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +0200101 mtd->ecc_stats.corrected++;
102 /* Do not overwrite -EBADMSG !! */
103 if (!ret)
104 ret = err;
105 } else
106 return err;
107 }
108
109 *retlen += retsize;
110 len -= size;
111 if (len == 0)
112 return ret;
113
114 buf += size;
115 from = 0;
116 }
117 return -EINVAL;
118}
119
120static int
121concat_write(struct mtd_info *mtd, loff_t to, size_t len,
122 size_t * retlen, const u_char * buf)
123{
124 struct mtd_concat *concat = CONCAT(mtd);
125 int err = -EINVAL;
126 int i;
127
Heiko Schocherff94bc42014-06-24 10:10:04 +0200128#ifdef __UBOOT__
Stefan Roese0a572652009-05-12 14:29:39 +0200129 *retlen = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200130#endif
Stefan Roese0a572652009-05-12 14:29:39 +0200131
132 for (i = 0; i < concat->num_subdev; i++) {
133 struct mtd_info *subdev = concat->subdev[i];
134 size_t size, retsize;
135
136 if (to >= subdev->size) {
137 size = 0;
138 to -= subdev->size;
139 continue;
140 }
141 if (to + len > subdev->size)
142 size = subdev->size - to;
143 else
144 size = len;
145
Sergey Lapindfe64e22013-01-14 03:46:50 +0000146 err = mtd_write(subdev, to, size, &retsize, buf);
Stefan Roese0a572652009-05-12 14:29:39 +0200147 if (err)
148 break;
149
150 *retlen += retsize;
151 len -= size;
152 if (len == 0)
153 break;
154
155 err = -EINVAL;
156 buf += size;
157 to = 0;
158 }
159 return err;
160}
161
Heiko Schocherff94bc42014-06-24 10:10:04 +0200162#ifndef __UBOOT__
163static int
164concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
165 unsigned long count, loff_t to, size_t * retlen)
166{
167 struct mtd_concat *concat = CONCAT(mtd);
168 struct kvec *vecs_copy;
169 unsigned long entry_low, entry_high;
170 size_t total_len = 0;
171 int i;
172 int err = -EINVAL;
173
174 /* Calculate total length of data */
175 for (i = 0; i < count; i++)
176 total_len += vecs[i].iov_len;
177
178 /* Check alignment */
179 if (mtd->writesize > 1) {
180 uint64_t __to = to;
181 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
182 return -EINVAL;
183 }
184
185 /* make a copy of vecs */
186 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
187 if (!vecs_copy)
188 return -ENOMEM;
189
190 entry_low = 0;
191 for (i = 0; i < concat->num_subdev; i++) {
192 struct mtd_info *subdev = concat->subdev[i];
193 size_t size, wsize, retsize, old_iov_len;
194
195 if (to >= subdev->size) {
196 to -= subdev->size;
197 continue;
198 }
199
200 size = min_t(uint64_t, total_len, subdev->size - to);
201 wsize = size; /* store for future use */
202
203 entry_high = entry_low;
204 while (entry_high < count) {
205 if (size <= vecs_copy[entry_high].iov_len)
206 break;
207 size -= vecs_copy[entry_high++].iov_len;
208 }
209
210 old_iov_len = vecs_copy[entry_high].iov_len;
211 vecs_copy[entry_high].iov_len = size;
212
213 err = mtd_writev(subdev, &vecs_copy[entry_low],
214 entry_high - entry_low + 1, to, &retsize);
215
216 vecs_copy[entry_high].iov_len = old_iov_len - size;
217 vecs_copy[entry_high].iov_base += size;
218
219 entry_low = entry_high;
220
221 if (err)
222 break;
223
224 *retlen += retsize;
225 total_len -= wsize;
226
227 if (total_len == 0)
228 break;
229
230 err = -EINVAL;
231 to = 0;
232 }
233
234 kfree(vecs_copy);
235 return err;
236}
237#endif
238
Stefan Roese0a572652009-05-12 14:29:39 +0200239static int
240concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
241{
242 struct mtd_concat *concat = CONCAT(mtd);
243 struct mtd_oob_ops devops = *ops;
244 int i, err, ret = 0;
245
246 ops->retlen = ops->oobretlen = 0;
247
248 for (i = 0; i < concat->num_subdev; i++) {
249 struct mtd_info *subdev = concat->subdev[i];
250
251 if (from >= subdev->size) {
252 from -= subdev->size;
253 continue;
254 }
255
256 /* partial read ? */
257 if (from + devops.len > subdev->size)
258 devops.len = subdev->size - from;
259
Sergey Lapindfe64e22013-01-14 03:46:50 +0000260 err = mtd_read_oob(subdev, from, &devops);
Stefan Roese0a572652009-05-12 14:29:39 +0200261 ops->retlen += devops.retlen;
262 ops->oobretlen += devops.oobretlen;
263
264 /* Save information about bitflips! */
265 if (unlikely(err)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000266 if (mtd_is_eccerr(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +0200267 mtd->ecc_stats.failed++;
268 ret = err;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000269 } else if (mtd_is_bitflip(err)) {
Stefan Roese0a572652009-05-12 14:29:39 +0200270 mtd->ecc_stats.corrected++;
271 /* Do not overwrite -EBADMSG !! */
272 if (!ret)
273 ret = err;
274 } else
275 return err;
276 }
277
278 if (devops.datbuf) {
279 devops.len = ops->len - ops->retlen;
280 if (!devops.len)
281 return ret;
282 devops.datbuf += devops.retlen;
283 }
284 if (devops.oobbuf) {
285 devops.ooblen = ops->ooblen - ops->oobretlen;
286 if (!devops.ooblen)
287 return ret;
288 devops.oobbuf += ops->oobretlen;
289 }
290
291 from = 0;
292 }
293 return -EINVAL;
294}
295
296static int
297concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
298{
299 struct mtd_concat *concat = CONCAT(mtd);
300 struct mtd_oob_ops devops = *ops;
301 int i, err;
302
303 if (!(mtd->flags & MTD_WRITEABLE))
304 return -EROFS;
305
Heiko Schocherff94bc42014-06-24 10:10:04 +0200306 ops->retlen = ops->oobretlen = 0;
Stefan Roese0a572652009-05-12 14:29:39 +0200307
308 for (i = 0; i < concat->num_subdev; i++) {
309 struct mtd_info *subdev = concat->subdev[i];
310
311 if (to >= subdev->size) {
312 to -= subdev->size;
313 continue;
314 }
315
316 /* partial write ? */
317 if (to + devops.len > subdev->size)
318 devops.len = subdev->size - to;
319
Sergey Lapindfe64e22013-01-14 03:46:50 +0000320 err = mtd_write_oob(subdev, to, &devops);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200321 ops->retlen += devops.oobretlen;
Stefan Roese0a572652009-05-12 14:29:39 +0200322 if (err)
323 return err;
324
325 if (devops.datbuf) {
326 devops.len = ops->len - ops->retlen;
327 if (!devops.len)
328 return 0;
329 devops.datbuf += devops.retlen;
330 }
331 if (devops.oobbuf) {
332 devops.ooblen = ops->ooblen - ops->oobretlen;
333 if (!devops.ooblen)
334 return 0;
335 devops.oobbuf += devops.oobretlen;
336 }
337 to = 0;
338 }
339 return -EINVAL;
340}
341
Stefan Roese0a572652009-05-12 14:29:39 +0200342static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
343{
344 int err;
345 wait_queue_head_t waitq;
346 DECLARE_WAITQUEUE(wait, current);
347
348 /*
349 * This code was stol^H^H^H^Hinspired by mtdchar.c
350 */
351 init_waitqueue_head(&waitq);
352
353 erase->mtd = mtd;
Stefan Roese0a572652009-05-12 14:29:39 +0200354 erase->priv = (unsigned long) &waitq;
355
356 /*
357 * FIXME: Allow INTERRUPTIBLE. Which means
358 * not having the wait_queue head on the stack.
359 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000360 err = mtd_erase(mtd, erase);
Stefan Roese0a572652009-05-12 14:29:39 +0200361 if (!err) {
362 set_current_state(TASK_UNINTERRUPTIBLE);
363 add_wait_queue(&waitq, &wait);
364 if (erase->state != MTD_ERASE_DONE
365 && erase->state != MTD_ERASE_FAILED)
366 schedule();
367 remove_wait_queue(&waitq, &wait);
368 set_current_state(TASK_RUNNING);
369
370 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
371 }
372 return err;
373}
374
375static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
376{
377 struct mtd_concat *concat = CONCAT(mtd);
378 struct mtd_info *subdev;
379 int i, err;
380 uint64_t length, offset = 0;
381 struct erase_info *erase;
382
Stefan Roese0a572652009-05-12 14:29:39 +0200383 /*
384 * Check for proper erase block alignment of the to-be-erased area.
385 * It is easier to do this based on the super device's erase
386 * region info rather than looking at each particular sub-device
387 * in turn.
388 */
389 if (!concat->mtd.numeraseregions) {
390 /* the easy case: device has uniform erase block size */
391 if (instr->addr & (concat->mtd.erasesize - 1))
392 return -EINVAL;
393 if (instr->len & (concat->mtd.erasesize - 1))
394 return -EINVAL;
395 } else {
396 /* device has variable erase size */
397 struct mtd_erase_region_info *erase_regions =
398 concat->mtd.eraseregions;
399
400 /*
401 * Find the erase region where the to-be-erased area begins:
402 */
403 for (i = 0; i < concat->mtd.numeraseregions &&
404 instr->addr >= erase_regions[i].offset; i++) ;
405 --i;
406
407 /*
408 * Now erase_regions[i] is the region in which the
409 * to-be-erased area begins. Verify that the starting
410 * offset is aligned to this region's erase size:
411 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200412 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
Stefan Roese0a572652009-05-12 14:29:39 +0200413 return -EINVAL;
414
415 /*
416 * now find the erase region where the to-be-erased area ends:
417 */
418 for (; i < concat->mtd.numeraseregions &&
419 (instr->addr + instr->len) >= erase_regions[i].offset;
420 ++i) ;
421 --i;
422 /*
423 * check if the ending offset is aligned to this region's erase size
424 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200425 if (i < 0 || ((instr->addr + instr->len) &
426 (erase_regions[i].erasesize - 1)))
Stefan Roese0a572652009-05-12 14:29:39 +0200427 return -EINVAL;
428 }
429
Stefan Roese0a572652009-05-12 14:29:39 +0200430 /* make a local copy of instr to avoid modifying the caller's struct */
431 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
432
433 if (!erase)
434 return -ENOMEM;
435
436 *erase = *instr;
437 length = instr->len;
438
439 /*
440 * find the subdevice where the to-be-erased area begins, adjust
441 * starting offset to be relative to the subdevice start
442 */
443 for (i = 0; i < concat->num_subdev; i++) {
444 subdev = concat->subdev[i];
445 if (subdev->size <= erase->addr) {
446 erase->addr -= subdev->size;
447 offset += subdev->size;
448 } else {
449 break;
450 }
451 }
452
453 /* must never happen since size limit has been verified above */
454 BUG_ON(i >= concat->num_subdev);
455
456 /* now do the erase: */
457 err = 0;
458 for (; length > 0; i++) {
459 /* loop for all subdevices affected by this request */
460 subdev = concat->subdev[i]; /* get current subdevice */
461
462 /* limit length to subdevice's size: */
463 if (erase->addr + length > subdev->size)
464 erase->len = subdev->size - erase->addr;
465 else
466 erase->len = length;
467
Stefan Roese0a572652009-05-12 14:29:39 +0200468 length -= erase->len;
469 if ((err = concat_dev_erase(subdev, erase))) {
470 /* sanity check: should never happen since
471 * block alignment has been checked above */
472 BUG_ON(err == -EINVAL);
473 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
474 instr->fail_addr = erase->fail_addr + offset;
475 break;
476 }
477 /*
478 * erase->addr specifies the offset of the area to be
479 * erased *within the current subdevice*. It can be
480 * non-zero only the first time through this loop, i.e.
481 * for the first subdevice where blocks need to be erased.
482 * All the following erases must begin at the start of the
483 * current subdevice, i.e. at offset zero.
484 */
485 erase->addr = 0;
486 offset += subdev->size;
487 }
488 instr->state = erase->state;
489 kfree(erase);
490 if (err)
491 return err;
492
Stefan Roese0a572652009-05-12 14:29:39 +0200493 return 0;
494}
495
496static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
497{
498 struct mtd_concat *concat = CONCAT(mtd);
499 int i, err = -EINVAL;
500
Stefan Roese0a572652009-05-12 14:29:39 +0200501 for (i = 0; i < concat->num_subdev; i++) {
502 struct mtd_info *subdev = concat->subdev[i];
503 uint64_t size;
504
505 if (ofs >= subdev->size) {
506 size = 0;
507 ofs -= subdev->size;
508 continue;
509 }
510 if (ofs + len > subdev->size)
511 size = subdev->size - ofs;
512 else
513 size = len;
514
Sergey Lapindfe64e22013-01-14 03:46:50 +0000515 err = mtd_lock(subdev, ofs, size);
Stefan Roese0a572652009-05-12 14:29:39 +0200516 if (err)
517 break;
518
519 len -= size;
520 if (len == 0)
521 break;
522
523 err = -EINVAL;
524 ofs = 0;
525 }
526
527 return err;
528}
529
530static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
531{
532 struct mtd_concat *concat = CONCAT(mtd);
533 int i, err = 0;
534
Stefan Roese0a572652009-05-12 14:29:39 +0200535 for (i = 0; i < concat->num_subdev; i++) {
536 struct mtd_info *subdev = concat->subdev[i];
537 uint64_t size;
538
539 if (ofs >= subdev->size) {
540 size = 0;
541 ofs -= subdev->size;
542 continue;
543 }
544 if (ofs + len > subdev->size)
545 size = subdev->size - ofs;
546 else
547 size = len;
548
Sergey Lapindfe64e22013-01-14 03:46:50 +0000549 err = mtd_unlock(subdev, ofs, size);
Stefan Roese0a572652009-05-12 14:29:39 +0200550 if (err)
551 break;
552
553 len -= size;
554 if (len == 0)
555 break;
556
557 err = -EINVAL;
558 ofs = 0;
559 }
560
561 return err;
562}
563
564static void concat_sync(struct mtd_info *mtd)
565{
566 struct mtd_concat *concat = CONCAT(mtd);
567 int i;
568
569 for (i = 0; i < concat->num_subdev; i++) {
570 struct mtd_info *subdev = concat->subdev[i];
Sergey Lapindfe64e22013-01-14 03:46:50 +0000571 mtd_sync(subdev);
Stefan Roese0a572652009-05-12 14:29:39 +0200572 }
573}
574
Heiko Schocherff94bc42014-06-24 10:10:04 +0200575#ifndef __UBOOT__
576static int concat_suspend(struct mtd_info *mtd)
577{
578 struct mtd_concat *concat = CONCAT(mtd);
579 int i, rc = 0;
580
581 for (i = 0; i < concat->num_subdev; i++) {
582 struct mtd_info *subdev = concat->subdev[i];
583 if ((rc = mtd_suspend(subdev)) < 0)
584 return rc;
585 }
586 return rc;
587}
588
589static void concat_resume(struct mtd_info *mtd)
590{
591 struct mtd_concat *concat = CONCAT(mtd);
592 int i;
593
594 for (i = 0; i < concat->num_subdev; i++) {
595 struct mtd_info *subdev = concat->subdev[i];
596 mtd_resume(subdev);
597 }
598}
599#endif
600
Stefan Roese0a572652009-05-12 14:29:39 +0200601static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
602{
603 struct mtd_concat *concat = CONCAT(mtd);
604 int i, res = 0;
605
Sergey Lapindfe64e22013-01-14 03:46:50 +0000606 if (!mtd_can_have_bb(concat->subdev[0]))
Stefan Roese0a572652009-05-12 14:29:39 +0200607 return res;
608
Stefan Roese0a572652009-05-12 14:29:39 +0200609 for (i = 0; i < concat->num_subdev; i++) {
610 struct mtd_info *subdev = concat->subdev[i];
611
612 if (ofs >= subdev->size) {
613 ofs -= subdev->size;
614 continue;
615 }
616
Sergey Lapindfe64e22013-01-14 03:46:50 +0000617 res = mtd_block_isbad(subdev, ofs);
Stefan Roese0a572652009-05-12 14:29:39 +0200618 break;
619 }
620
621 return res;
622}
623
624static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
625{
626 struct mtd_concat *concat = CONCAT(mtd);
627 int i, err = -EINVAL;
628
Stefan Roese0a572652009-05-12 14:29:39 +0200629 for (i = 0; i < concat->num_subdev; i++) {
630 struct mtd_info *subdev = concat->subdev[i];
631
632 if (ofs >= subdev->size) {
633 ofs -= subdev->size;
634 continue;
635 }
636
Sergey Lapindfe64e22013-01-14 03:46:50 +0000637 err = mtd_block_markbad(subdev, ofs);
Stefan Roese0a572652009-05-12 14:29:39 +0200638 if (!err)
639 mtd->ecc_stats.badblocks++;
640 break;
641 }
642
643 return err;
644}
645
646/*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200647 * try to support NOMMU mmaps on concatenated devices
648 * - we don't support subdev spanning as we can't guarantee it'll work
649 */
650static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
651 unsigned long len,
652 unsigned long offset,
653 unsigned long flags)
654{
655 struct mtd_concat *concat = CONCAT(mtd);
656 int i;
657
658 for (i = 0; i < concat->num_subdev; i++) {
659 struct mtd_info *subdev = concat->subdev[i];
660
661 if (offset >= subdev->size) {
662 offset -= subdev->size;
663 continue;
664 }
665
666 return mtd_get_unmapped_area(subdev, len, offset, flags);
667 }
668
669 return (unsigned long) -ENOSYS;
670}
671
672/*
Stefan Roese0a572652009-05-12 14:29:39 +0200673 * This function constructs a virtual MTD device by concatenating
674 * num_devs MTD devices. A pointer to the new device object is
675 * stored to *new_dev upon success. This function does _not_
676 * register any devices: this is the caller's responsibility.
677 */
678struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
679 int num_devs, /* number of subdevices */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200680#ifndef __UBOOT__
Stefan Roese0a572652009-05-12 14:29:39 +0200681 const char *name)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200682#else
683 char *name)
684#endif
Stefan Roese0a572652009-05-12 14:29:39 +0200685{ /* name for the new device */
686 int i;
687 size_t size;
688 struct mtd_concat *concat;
689 uint32_t max_erasesize, curr_erasesize;
690 int num_erase_region;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200691 int max_writebufsize = 0;
Stefan Roese0a572652009-05-12 14:29:39 +0200692
693 debug("Concatenating MTD devices:\n");
694 for (i = 0; i < num_devs; i++)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200695 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
Stefan Roese0a572652009-05-12 14:29:39 +0200696 debug("into device \"%s\"\n", name);
697
698 /* allocate the device structure */
699 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
700 concat = kzalloc(size, GFP_KERNEL);
701 if (!concat) {
702 printk
703 ("memory allocation error while creating concatenated device \"%s\"\n",
704 name);
705 return NULL;
706 }
707 concat->subdev = (struct mtd_info **) (concat + 1);
708
709 /*
710 * Set up the new "super" device's MTD object structure, check for
Heiko Schocherff94bc42014-06-24 10:10:04 +0200711 * incompatibilities between the subdevices.
Stefan Roese0a572652009-05-12 14:29:39 +0200712 */
713 concat->mtd.type = subdev[0]->type;
714 concat->mtd.flags = subdev[0]->flags;
715 concat->mtd.size = subdev[0]->size;
716 concat->mtd.erasesize = subdev[0]->erasesize;
717 concat->mtd.writesize = subdev[0]->writesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200718
719 for (i = 0; i < num_devs; i++)
720 if (max_writebufsize < subdev[i]->writebufsize)
721 max_writebufsize = subdev[i]->writebufsize;
722 concat->mtd.writebufsize = max_writebufsize;
723
Stefan Roese0a572652009-05-12 14:29:39 +0200724 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
725 concat->mtd.oobsize = subdev[0]->oobsize;
726 concat->mtd.oobavail = subdev[0]->oobavail;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200727#ifndef __UBOOT__
728 if (subdev[0]->_writev)
729 concat->mtd._writev = concat_writev;
730#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000731 if (subdev[0]->_read_oob)
732 concat->mtd._read_oob = concat_read_oob;
733 if (subdev[0]->_write_oob)
734 concat->mtd._write_oob = concat_write_oob;
735 if (subdev[0]->_block_isbad)
736 concat->mtd._block_isbad = concat_block_isbad;
737 if (subdev[0]->_block_markbad)
738 concat->mtd._block_markbad = concat_block_markbad;
Stefan Roese0a572652009-05-12 14:29:39 +0200739
740 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
741
Heiko Schocherff94bc42014-06-24 10:10:04 +0200742#ifndef __UBOOT__
743 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
744#endif
745
Stefan Roese0a572652009-05-12 14:29:39 +0200746 concat->subdev[0] = subdev[0];
747
748 for (i = 1; i < num_devs; i++) {
749 if (concat->mtd.type != subdev[i]->type) {
750 kfree(concat);
751 printk("Incompatible device type on \"%s\"\n",
752 subdev[i]->name);
753 return NULL;
754 }
755 if (concat->mtd.flags != subdev[i]->flags) {
756 /*
757 * Expect all flags except MTD_WRITEABLE to be
758 * equal on all subdevices.
759 */
760 if ((concat->mtd.flags ^ subdev[i]->
761 flags) & ~MTD_WRITEABLE) {
762 kfree(concat);
763 printk("Incompatible device flags on \"%s\"\n",
764 subdev[i]->name);
765 return NULL;
766 } else
767 /* if writeable attribute differs,
768 make super device writeable */
769 concat->mtd.flags |=
770 subdev[i]->flags & MTD_WRITEABLE;
771 }
772
Heiko Schocherff94bc42014-06-24 10:10:04 +0200773#ifndef __UBOOT__
774 /* only permit direct mapping if the BDIs are all the same
775 * - copy-mapping is still permitted
776 */
777 if (concat->mtd.backing_dev_info !=
778 subdev[i]->backing_dev_info)
779 concat->mtd.backing_dev_info =
780 &default_backing_dev_info;
781#endif
782
Stefan Roese0a572652009-05-12 14:29:39 +0200783 concat->mtd.size += subdev[i]->size;
784 concat->mtd.ecc_stats.badblocks +=
785 subdev[i]->ecc_stats.badblocks;
786 if (concat->mtd.writesize != subdev[i]->writesize ||
787 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
788 concat->mtd.oobsize != subdev[i]->oobsize ||
Sergey Lapindfe64e22013-01-14 03:46:50 +0000789 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
790 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
Stefan Roese0a572652009-05-12 14:29:39 +0200791 kfree(concat);
792 printk("Incompatible OOB or ECC data on \"%s\"\n",
793 subdev[i]->name);
794 return NULL;
795 }
796 concat->subdev[i] = subdev[i];
797
798 }
799
800 concat->mtd.ecclayout = subdev[0]->ecclayout;
801
802 concat->num_subdev = num_devs;
803 concat->mtd.name = name;
804
Sergey Lapindfe64e22013-01-14 03:46:50 +0000805 concat->mtd._erase = concat_erase;
806 concat->mtd._read = concat_read;
807 concat->mtd._write = concat_write;
808 concat->mtd._sync = concat_sync;
809 concat->mtd._lock = concat_lock;
810 concat->mtd._unlock = concat_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200811#ifndef __UBOOT__
812 concat->mtd._suspend = concat_suspend;
813 concat->mtd._resume = concat_resume;
814#endif
815 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
Stefan Roese0a572652009-05-12 14:29:39 +0200816
817 /*
818 * Combine the erase block size info of the subdevices:
819 *
820 * first, walk the map of the new device and see how
821 * many changes in erase size we have
822 */
823 max_erasesize = curr_erasesize = subdev[0]->erasesize;
824 num_erase_region = 1;
825 for (i = 0; i < num_devs; i++) {
826 if (subdev[i]->numeraseregions == 0) {
827 /* current subdevice has uniform erase size */
828 if (subdev[i]->erasesize != curr_erasesize) {
829 /* if it differs from the last subdevice's erase size, count it */
830 ++num_erase_region;
831 curr_erasesize = subdev[i]->erasesize;
832 if (curr_erasesize > max_erasesize)
833 max_erasesize = curr_erasesize;
834 }
835 } else {
836 /* current subdevice has variable erase size */
837 int j;
838 for (j = 0; j < subdev[i]->numeraseregions; j++) {
839
840 /* walk the list of erase regions, count any changes */
841 if (subdev[i]->eraseregions[j].erasesize !=
842 curr_erasesize) {
843 ++num_erase_region;
844 curr_erasesize =
845 subdev[i]->eraseregions[j].
846 erasesize;
847 if (curr_erasesize > max_erasesize)
848 max_erasesize = curr_erasesize;
849 }
850 }
851 }
852 }
853
854 if (num_erase_region == 1) {
855 /*
856 * All subdevices have the same uniform erase size.
857 * This is easy:
858 */
859 concat->mtd.erasesize = curr_erasesize;
860 concat->mtd.numeraseregions = 0;
861 } else {
862 uint64_t tmp64;
863
864 /*
865 * erase block size varies across the subdevices: allocate
866 * space to store the data describing the variable erase regions
867 */
868 struct mtd_erase_region_info *erase_region_p;
869 uint64_t begin, position;
870
871 concat->mtd.erasesize = max_erasesize;
872 concat->mtd.numeraseregions = num_erase_region;
873 concat->mtd.eraseregions = erase_region_p =
874 kmalloc(num_erase_region *
875 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
876 if (!erase_region_p) {
877 kfree(concat);
878 printk
879 ("memory allocation error while creating erase region list"
880 " for device \"%s\"\n", name);
881 return NULL;
882 }
883
884 /*
885 * walk the map of the new device once more and fill in
886 * in erase region info:
887 */
888 curr_erasesize = subdev[0]->erasesize;
889 begin = position = 0;
890 for (i = 0; i < num_devs; i++) {
891 if (subdev[i]->numeraseregions == 0) {
892 /* current subdevice has uniform erase size */
893 if (subdev[i]->erasesize != curr_erasesize) {
894 /*
895 * fill in an mtd_erase_region_info structure for the area
896 * we have walked so far:
897 */
898 erase_region_p->offset = begin;
899 erase_region_p->erasesize =
900 curr_erasesize;
901 tmp64 = position - begin;
902 do_div(tmp64, curr_erasesize);
903 erase_region_p->numblocks = tmp64;
904 begin = position;
905
906 curr_erasesize = subdev[i]->erasesize;
907 ++erase_region_p;
908 }
909 position += subdev[i]->size;
910 } else {
911 /* current subdevice has variable erase size */
912 int j;
913 for (j = 0; j < subdev[i]->numeraseregions; j++) {
914 /* walk the list of erase regions, count any changes */
915 if (subdev[i]->eraseregions[j].
916 erasesize != curr_erasesize) {
917 erase_region_p->offset = begin;
918 erase_region_p->erasesize =
919 curr_erasesize;
920 tmp64 = position - begin;
921 do_div(tmp64, curr_erasesize);
922 erase_region_p->numblocks = tmp64;
923 begin = position;
924
925 curr_erasesize =
926 subdev[i]->eraseregions[j].
927 erasesize;
928 ++erase_region_p;
929 }
930 position +=
931 subdev[i]->eraseregions[j].
932 numblocks * (uint64_t)curr_erasesize;
933 }
934 }
935 }
936 /* Now write the final entry */
937 erase_region_p->offset = begin;
938 erase_region_p->erasesize = curr_erasesize;
939 tmp64 = position - begin;
940 do_div(tmp64, curr_erasesize);
941 erase_region_p->numblocks = tmp64;
942 }
943
944 return &concat->mtd;
945}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200946
947/*
948 * This function destroys an MTD object obtained from concat_mtd_devs()
949 */
950
951void mtd_concat_destroy(struct mtd_info *mtd)
952{
953 struct mtd_concat *concat = CONCAT(mtd);
954 if (concat->mtd.numeraseregions)
955 kfree(concat->mtd.eraseregions);
956 kfree(concat);
957}
958
959EXPORT_SYMBOL(mtd_concat_create);
960EXPORT_SYMBOL(mtd_concat_destroy);
961
962MODULE_LICENSE("GPL");
963MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
964MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");