firmware: smccc: Add function to fetch SMCCC version
[linux-2.6-microblaze.git] / drivers / mtd / mtdpart.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Simple MTD partitioning layer
4  *
5  * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
6  * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
7  * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
8  */
9
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 #include <linux/kmod.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/err.h>
19 #include <linux/of.h>
20
21 #include "mtdcore.h"
22
23 /*
24  * MTD methods which simply translate the effective address and pass through
25  * to the _real_ device.
26  */
27
28 static inline void free_partition(struct mtd_info *mtd)
29 {
30         kfree(mtd->name);
31         kfree(mtd);
32 }
33
34 static struct mtd_info *allocate_partition(struct mtd_info *parent,
35                                            const struct mtd_partition *part,
36                                            int partno, uint64_t cur_offset)
37 {
38         int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
39                                                             parent->erasesize;
40         struct mtd_info *child, *master = mtd_get_master(parent);
41         u32 remainder;
42         char *name;
43         u64 tmp;
44
45         /* allocate the partition structure */
46         child = kzalloc(sizeof(*child), GFP_KERNEL);
47         name = kstrdup(part->name, GFP_KERNEL);
48         if (!name || !child) {
49                 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
50                        parent->name);
51                 kfree(name);
52                 kfree(child);
53                 return ERR_PTR(-ENOMEM);
54         }
55
56         /* set up the MTD object for this partition */
57         child->type = parent->type;
58         child->part.flags = parent->flags & ~part->mask_flags;
59         child->flags = child->part.flags;
60         child->size = part->size;
61         child->writesize = parent->writesize;
62         child->writebufsize = parent->writebufsize;
63         child->oobsize = parent->oobsize;
64         child->oobavail = parent->oobavail;
65         child->subpage_sft = parent->subpage_sft;
66
67         child->name = name;
68         child->owner = parent->owner;
69
70         /* NOTE: Historically, we didn't arrange MTDs as a tree out of
71          * concern for showing the same data in multiple partitions.
72          * However, it is very useful to have the master node present,
73          * so the MTD_PARTITIONED_MASTER option allows that. The master
74          * will have device nodes etc only if this is set, so make the
75          * parent conditional on that option. Note, this is a way to
76          * distinguish between the parent and its partitions in sysfs.
77          */
78         child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
79                             &parent->dev : parent->dev.parent;
80         child->dev.of_node = part->of_node;
81         child->parent = parent;
82         child->part.offset = part->offset;
83         INIT_LIST_HEAD(&child->partitions);
84
85         if (child->part.offset == MTDPART_OFS_APPEND)
86                 child->part.offset = cur_offset;
87         if (child->part.offset == MTDPART_OFS_NXTBLK) {
88                 tmp = cur_offset;
89                 child->part.offset = cur_offset;
90                 remainder = do_div(tmp, wr_alignment);
91                 if (remainder) {
92                         child->part.offset += wr_alignment - remainder;
93                         printk(KERN_NOTICE "Moving partition %d: "
94                                "0x%012llx -> 0x%012llx\n", partno,
95                                (unsigned long long)cur_offset,
96                                child->part.offset);
97                 }
98         }
99         if (child->part.offset == MTDPART_OFS_RETAIN) {
100                 child->part.offset = cur_offset;
101                 if (parent->size - child->part.offset >= child->size) {
102                         child->size = parent->size - child->part.offset -
103                                       child->size;
104                 } else {
105                         printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
106                                 part->name, parent->size - child->part.offset,
107                                 child->size);
108                         /* register to preserve ordering */
109                         goto out_register;
110                 }
111         }
112         if (child->size == MTDPART_SIZ_FULL)
113                 child->size = parent->size - child->part.offset;
114
115         printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n",
116                child->part.offset, child->part.offset + child->size,
117                child->name);
118
119         /* let's do some sanity checks */
120         if (child->part.offset >= parent->size) {
121                 /* let's register it anyway to preserve ordering */
122                 child->part.offset = 0;
123                 child->size = 0;
124
125                 /* Initialize ->erasesize to make add_mtd_device() happy. */
126                 child->erasesize = parent->erasesize;
127                 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
128                         part->name);
129                 goto out_register;
130         }
131         if (child->part.offset + child->size > parent->size) {
132                 child->size = parent->size - child->part.offset;
133                 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
134                         part->name, parent->name, child->size);
135         }
136         if (parent->numeraseregions > 1) {
137                 /* Deal with variable erase size stuff */
138                 int i, max = parent->numeraseregions;
139                 u64 end = child->part.offset + child->size;
140                 struct mtd_erase_region_info *regions = parent->eraseregions;
141
142                 /* Find the first erase regions which is part of this
143                  * partition. */
144                 for (i = 0; i < max && regions[i].offset <= child->part.offset;
145                      i++)
146                         ;
147                 /* The loop searched for the region _behind_ the first one */
148                 if (i > 0)
149                         i--;
150
151                 /* Pick biggest erasesize */
152                 for (; i < max && regions[i].offset < end; i++) {
153                         if (child->erasesize < regions[i].erasesize)
154                                 child->erasesize = regions[i].erasesize;
155                 }
156                 BUG_ON(child->erasesize == 0);
157         } else {
158                 /* Single erase size */
159                 child->erasesize = parent->erasesize;
160         }
161
162         /*
163          * Child erasesize might differ from the parent one if the parent
164          * exposes several regions with different erasesize. Adjust
165          * wr_alignment accordingly.
166          */
167         if (!(child->flags & MTD_NO_ERASE))
168                 wr_alignment = child->erasesize;
169
170         tmp = mtd_get_master_ofs(child, 0);
171         remainder = do_div(tmp, wr_alignment);
172         if ((child->flags & MTD_WRITEABLE) && remainder) {
173                 /* Doesn't start on a boundary of major erase size */
174                 /* FIXME: Let it be writable if it is on a boundary of
175                  * _minor_ erase size though */
176                 child->flags &= ~MTD_WRITEABLE;
177                 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
178                         part->name);
179         }
180
181         tmp = mtd_get_master_ofs(child, 0) + child->size;
182         remainder = do_div(tmp, wr_alignment);
183         if ((child->flags & MTD_WRITEABLE) && remainder) {
184                 child->flags &= ~MTD_WRITEABLE;
185                 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
186                         part->name);
187         }
188
189         child->ecc_step_size = parent->ecc_step_size;
190         child->ecc_strength = parent->ecc_strength;
191         child->bitflip_threshold = parent->bitflip_threshold;
192
193         if (master->_block_isbad) {
194                 uint64_t offs = 0;
195
196                 while (offs < child->size) {
197                         if (mtd_block_isreserved(child, offs))
198                                 child->ecc_stats.bbtblocks++;
199                         else if (mtd_block_isbad(child, offs))
200                                 child->ecc_stats.badblocks++;
201                         offs += child->erasesize;
202                 }
203         }
204
205 out_register:
206         return child;
207 }
208
209 static ssize_t mtd_partition_offset_show(struct device *dev,
210                 struct device_attribute *attr, char *buf)
211 {
212         struct mtd_info *mtd = dev_get_drvdata(dev);
213
214         return snprintf(buf, PAGE_SIZE, "%lld\n", mtd->part.offset);
215 }
216
217 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
218
219 static const struct attribute *mtd_partition_attrs[] = {
220         &dev_attr_offset.attr,
221         NULL
222 };
223
224 static int mtd_add_partition_attrs(struct mtd_info *new)
225 {
226         int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs);
227         if (ret)
228                 printk(KERN_WARNING
229                        "mtd: failed to create partition attrs, err=%d\n", ret);
230         return ret;
231 }
232
233 int mtd_add_partition(struct mtd_info *parent, const char *name,
234                       long long offset, long long length)
235 {
236         struct mtd_info *master = mtd_get_master(parent);
237         struct mtd_partition part;
238         struct mtd_info *child;
239         int ret = 0;
240
241         /* the direct offset is expected */
242         if (offset == MTDPART_OFS_APPEND ||
243             offset == MTDPART_OFS_NXTBLK)
244                 return -EINVAL;
245
246         if (length == MTDPART_SIZ_FULL)
247                 length = parent->size - offset;
248
249         if (length <= 0)
250                 return -EINVAL;
251
252         memset(&part, 0, sizeof(part));
253         part.name = name;
254         part.size = length;
255         part.offset = offset;
256
257         child = allocate_partition(parent, &part, -1, offset);
258         if (IS_ERR(child))
259                 return PTR_ERR(child);
260
261         mutex_lock(&master->master.partitions_lock);
262         list_add_tail(&child->part.node, &parent->partitions);
263         mutex_unlock(&master->master.partitions_lock);
264
265         ret = add_mtd_device(child);
266         if (ret)
267                 goto err_remove_part;
268
269         mtd_add_partition_attrs(child);
270
271         return 0;
272
273 err_remove_part:
274         mutex_lock(&master->master.partitions_lock);
275         list_del(&child->part.node);
276         mutex_unlock(&master->master.partitions_lock);
277
278         free_partition(child);
279
280         return ret;
281 }
282 EXPORT_SYMBOL_GPL(mtd_add_partition);
283
284 /**
285  * __mtd_del_partition - delete MTD partition
286  *
287  * @priv: MTD structure to be deleted
288  *
289  * This function must be called with the partitions mutex locked.
290  */
291 static int __mtd_del_partition(struct mtd_info *mtd)
292 {
293         struct mtd_info *child, *next;
294         int err;
295
296         list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
297                 err = __mtd_del_partition(child);
298                 if (err)
299                         return err;
300         }
301
302         sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
303
304         err = del_mtd_device(mtd);
305         if (err)
306                 return err;
307
308         list_del(&child->part.node);
309         free_partition(mtd);
310
311         return 0;
312 }
313
314 /*
315  * This function unregisters and destroy all slave MTD objects which are
316  * attached to the given MTD object, recursively.
317  */
318 static int __del_mtd_partitions(struct mtd_info *mtd)
319 {
320         struct mtd_info *child, *next;
321         LIST_HEAD(tmp_list);
322         int ret, err = 0;
323
324         list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
325                 if (mtd_has_partitions(child))
326                         del_mtd_partitions(child);
327
328                 pr_info("Deleting %s MTD partition\n", child->name);
329                 ret = del_mtd_device(child);
330                 if (ret < 0) {
331                         pr_err("Error when deleting partition \"%s\" (%d)\n",
332                                child->name, ret);
333                         err = ret;
334                         continue;
335                 }
336
337                 list_del(&child->part.node);
338                 free_partition(child);
339         }
340
341         return err;
342 }
343
344 int del_mtd_partitions(struct mtd_info *mtd)
345 {
346         struct mtd_info *master = mtd_get_master(mtd);
347         int ret;
348
349         pr_info("Deleting MTD partitions on \"%s\":\n", mtd->name);
350
351         mutex_lock(&master->master.partitions_lock);
352         ret = __del_mtd_partitions(mtd);
353         mutex_unlock(&master->master.partitions_lock);
354
355         return ret;
356 }
357
358 int mtd_del_partition(struct mtd_info *mtd, int partno)
359 {
360         struct mtd_info *child, *master = mtd_get_master(mtd);
361         int ret = -EINVAL;
362
363         mutex_lock(&master->master.partitions_lock);
364         list_for_each_entry(child, &mtd->partitions, part.node) {
365                 if (child->index == partno) {
366                         ret = __mtd_del_partition(child);
367                         break;
368                 }
369         }
370         mutex_unlock(&master->master.partitions_lock);
371
372         return ret;
373 }
374 EXPORT_SYMBOL_GPL(mtd_del_partition);
375
376 /*
377  * This function, given a parent MTD object and a partition table, creates
378  * and registers the child MTD objects which are bound to the parent according
379  * to the partition definitions.
380  *
381  * For historical reasons, this function's caller only registers the parent
382  * if the MTD_PARTITIONED_MASTER config option is set.
383  */
384
385 int add_mtd_partitions(struct mtd_info *parent,
386                        const struct mtd_partition *parts,
387                        int nbparts)
388 {
389         struct mtd_info *child, *master = mtd_get_master(parent);
390         uint64_t cur_offset = 0;
391         int i, ret;
392
393         printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n",
394                nbparts, parent->name);
395
396         for (i = 0; i < nbparts; i++) {
397                 child = allocate_partition(parent, parts + i, i, cur_offset);
398                 if (IS_ERR(child)) {
399                         ret = PTR_ERR(child);
400                         goto err_del_partitions;
401                 }
402
403                 mutex_lock(&master->master.partitions_lock);
404                 list_add_tail(&child->part.node, &parent->partitions);
405                 mutex_unlock(&master->master.partitions_lock);
406
407                 ret = add_mtd_device(child);
408                 if (ret) {
409                         mutex_lock(&master->master.partitions_lock);
410                         list_del(&child->part.node);
411                         mutex_unlock(&master->master.partitions_lock);
412
413                         free_partition(child);
414                         goto err_del_partitions;
415                 }
416
417                 mtd_add_partition_attrs(child);
418
419                 /* Look for subpartitions */
420                 parse_mtd_partitions(child, parts[i].types, NULL);
421
422                 cur_offset = child->part.offset + child->size;
423         }
424
425         return 0;
426
427 err_del_partitions:
428         del_mtd_partitions(master);
429
430         return ret;
431 }
432
433 static DEFINE_SPINLOCK(part_parser_lock);
434 static LIST_HEAD(part_parsers);
435
436 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
437 {
438         struct mtd_part_parser *p, *ret = NULL;
439
440         spin_lock(&part_parser_lock);
441
442         list_for_each_entry(p, &part_parsers, list)
443                 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
444                         ret = p;
445                         break;
446                 }
447
448         spin_unlock(&part_parser_lock);
449
450         return ret;
451 }
452
453 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
454 {
455         module_put(p->owner);
456 }
457
458 /*
459  * Many partition parsers just expected the core to kfree() all their data in
460  * one chunk. Do that by default.
461  */
462 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
463                                             int nr_parts)
464 {
465         kfree(pparts);
466 }
467
468 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
469 {
470         p->owner = owner;
471
472         if (!p->cleanup)
473                 p->cleanup = &mtd_part_parser_cleanup_default;
474
475         spin_lock(&part_parser_lock);
476         list_add(&p->list, &part_parsers);
477         spin_unlock(&part_parser_lock);
478
479         return 0;
480 }
481 EXPORT_SYMBOL_GPL(__register_mtd_parser);
482
483 void deregister_mtd_parser(struct mtd_part_parser *p)
484 {
485         spin_lock(&part_parser_lock);
486         list_del(&p->list);
487         spin_unlock(&part_parser_lock);
488 }
489 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
490
491 /*
492  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
493  * are changing this array!
494  */
495 static const char * const default_mtd_part_types[] = {
496         "cmdlinepart",
497         "ofpart",
498         NULL
499 };
500
501 /* Check DT only when looking for subpartitions. */
502 static const char * const default_subpartition_types[] = {
503         "ofpart",
504         NULL
505 };
506
507 static int mtd_part_do_parse(struct mtd_part_parser *parser,
508                              struct mtd_info *master,
509                              struct mtd_partitions *pparts,
510                              struct mtd_part_parser_data *data)
511 {
512         int ret;
513
514         ret = (*parser->parse_fn)(master, &pparts->parts, data);
515         pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
516         if (ret <= 0)
517                 return ret;
518
519         pr_notice("%d %s partitions found on MTD device %s\n", ret,
520                   parser->name, master->name);
521
522         pparts->nr_parts = ret;
523         pparts->parser = parser;
524
525         return ret;
526 }
527
528 /**
529  * mtd_part_get_compatible_parser - find MTD parser by a compatible string
530  *
531  * @compat: compatible string describing partitions in a device tree
532  *
533  * MTD parsers can specify supported partitions by providing a table of
534  * compatibility strings. This function finds a parser that advertises support
535  * for a passed value of "compatible".
536  */
537 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
538 {
539         struct mtd_part_parser *p, *ret = NULL;
540
541         spin_lock(&part_parser_lock);
542
543         list_for_each_entry(p, &part_parsers, list) {
544                 const struct of_device_id *matches;
545
546                 matches = p->of_match_table;
547                 if (!matches)
548                         continue;
549
550                 for (; matches->compatible[0]; matches++) {
551                         if (!strcmp(matches->compatible, compat) &&
552                             try_module_get(p->owner)) {
553                                 ret = p;
554                                 break;
555                         }
556                 }
557
558                 if (ret)
559                         break;
560         }
561
562         spin_unlock(&part_parser_lock);
563
564         return ret;
565 }
566
567 static int mtd_part_of_parse(struct mtd_info *master,
568                              struct mtd_partitions *pparts)
569 {
570         struct mtd_part_parser *parser;
571         struct device_node *np;
572         struct property *prop;
573         const char *compat;
574         const char *fixed = "fixed-partitions";
575         int ret, err = 0;
576
577         np = mtd_get_of_node(master);
578         if (mtd_is_partition(master))
579                 of_node_get(np);
580         else
581                 np = of_get_child_by_name(np, "partitions");
582
583         of_property_for_each_string(np, "compatible", prop, compat) {
584                 parser = mtd_part_get_compatible_parser(compat);
585                 if (!parser)
586                         continue;
587                 ret = mtd_part_do_parse(parser, master, pparts, NULL);
588                 if (ret > 0) {
589                         of_node_put(np);
590                         return ret;
591                 }
592                 mtd_part_parser_put(parser);
593                 if (ret < 0 && !err)
594                         err = ret;
595         }
596         of_node_put(np);
597
598         /*
599          * For backward compatibility we have to try the "fixed-partitions"
600          * parser. It supports old DT format with partitions specified as a
601          * direct subnodes of a flash device DT node without any compatibility
602          * specified we could match.
603          */
604         parser = mtd_part_parser_get(fixed);
605         if (!parser && !request_module("%s", fixed))
606                 parser = mtd_part_parser_get(fixed);
607         if (parser) {
608                 ret = mtd_part_do_parse(parser, master, pparts, NULL);
609                 if (ret > 0)
610                         return ret;
611                 mtd_part_parser_put(parser);
612                 if (ret < 0 && !err)
613                         err = ret;
614         }
615
616         return err;
617 }
618
619 /**
620  * parse_mtd_partitions - parse and register MTD partitions
621  *
622  * @master: the master partition (describes whole MTD device)
623  * @types: names of partition parsers to try or %NULL
624  * @data: MTD partition parser-specific data
625  *
626  * This function tries to find & register partitions on MTD device @master. It
627  * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
628  * then the default list of parsers is used. The default list contains only the
629  * "cmdlinepart" and "ofpart" parsers ATM.
630  * Note: If there are more then one parser in @types, the kernel only takes the
631  * partitions parsed out by the first parser.
632  *
633  * This function may return:
634  * o a negative error code in case of failure
635  * o number of found partitions otherwise
636  */
637 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
638                          struct mtd_part_parser_data *data)
639 {
640         struct mtd_partitions pparts = { };
641         struct mtd_part_parser *parser;
642         int ret, err = 0;
643
644         if (!types)
645                 types = mtd_is_partition(master) ? default_subpartition_types :
646                         default_mtd_part_types;
647
648         for ( ; *types; types++) {
649                 /*
650                  * ofpart is a special type that means OF partitioning info
651                  * should be used. It requires a bit different logic so it is
652                  * handled in a separated function.
653                  */
654                 if (!strcmp(*types, "ofpart")) {
655                         ret = mtd_part_of_parse(master, &pparts);
656                 } else {
657                         pr_debug("%s: parsing partitions %s\n", master->name,
658                                  *types);
659                         parser = mtd_part_parser_get(*types);
660                         if (!parser && !request_module("%s", *types))
661                                 parser = mtd_part_parser_get(*types);
662                         pr_debug("%s: got parser %s\n", master->name,
663                                 parser ? parser->name : NULL);
664                         if (!parser)
665                                 continue;
666                         ret = mtd_part_do_parse(parser, master, &pparts, data);
667                         if (ret <= 0)
668                                 mtd_part_parser_put(parser);
669                 }
670                 /* Found partitions! */
671                 if (ret > 0) {
672                         err = add_mtd_partitions(master, pparts.parts,
673                                                  pparts.nr_parts);
674                         mtd_part_parser_cleanup(&pparts);
675                         return err ? err : pparts.nr_parts;
676                 }
677                 /*
678                  * Stash the first error we see; only report it if no parser
679                  * succeeds
680                  */
681                 if (ret < 0 && !err)
682                         err = ret;
683         }
684         return err;
685 }
686
687 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
688 {
689         const struct mtd_part_parser *parser;
690
691         if (!parts)
692                 return;
693
694         parser = parts->parser;
695         if (parser) {
696                 if (parser->cleanup)
697                         parser->cleanup(parts->parts, parts->nr_parts);
698
699                 mtd_part_parser_put(parser);
700         }
701 }
702
703 /* Returns the size of the entire flash chip */
704 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
705 {
706         struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
707
708         return master->size;
709 }
710 EXPORT_SYMBOL_GPL(mtd_get_device_size);