1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Core registration and callback routines for MTD
4 * drivers and users.
5 *
6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
7 * Copyright © 2006 Red Hat UK Limited
8 */
9
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/ptrace.h>
13 #include <linux/seq_file.h>
14 #include <linux/string.h>
15 #include <linux/timer.h>
16 #include <linux/major.h>
17 #include <linux/fs.h>
18 #include <linux/err.h>
19 #include <linux/ioctl.h>
20 #include <linux/init.h>
21 #include <linux/of.h>
22 #include <linux/proc_fs.h>
23 #include <linux/idr.h>
24 #include <linux/backing-dev.h>
25 #include <linux/gfp.h>
26 #include <linux/random.h>
27 #include <linux/slab.h>
28 #include <linux/reboot.h>
29 #include <linux/leds.h>
30 #include <linux/debugfs.h>
31 #include <linux/nvmem-provider.h>
32 #include <linux/root_dev.h>
33 #include <linux/error-injection.h>
34
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/partitions.h>
37
38 #include "mtdcore.h"
39
40 struct backing_dev_info *mtd_bdi;
41
42 #ifdef CONFIG_PM_SLEEP
43
mtd_cls_suspend(struct device * dev)44 static int mtd_cls_suspend(struct device *dev)
45 {
46 struct mtd_info *mtd = dev_get_drvdata(dev);
47
48 return mtd ? mtd_suspend(mtd) : 0;
49 }
50
mtd_cls_resume(struct device * dev)51 static int mtd_cls_resume(struct device *dev)
52 {
53 struct mtd_info *mtd = dev_get_drvdata(dev);
54
55 if (mtd)
56 mtd_resume(mtd);
57 return 0;
58 }
59
60 static SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops, mtd_cls_suspend, mtd_cls_resume);
61 #define MTD_CLS_PM_OPS (&mtd_cls_pm_ops)
62 #else
63 #define MTD_CLS_PM_OPS NULL
64 #endif
65
66 static struct class mtd_class = {
67 .name = "mtd",
68 .pm = MTD_CLS_PM_OPS,
69 };
70
71 static DEFINE_IDR(mtd_idr);
72
73 /* These are exported solely for the purpose of mtd_blkdevs.c. You
74 should not use them for _anything_ else */
75 DEFINE_MUTEX(mtd_table_mutex);
76 EXPORT_SYMBOL_GPL(mtd_table_mutex);
77
__mtd_next_device(int i)78 struct mtd_info *__mtd_next_device(int i)
79 {
80 return idr_get_next(&mtd_idr, &i);
81 }
82 EXPORT_SYMBOL_GPL(__mtd_next_device);
83
84 static LIST_HEAD(mtd_notifiers);
85
86
87 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
88
89 /* REVISIT once MTD uses the driver model better, whoever allocates
90 * the mtd_info will probably want to use the release() hook...
91 */
mtd_release(struct device * dev)92 static void mtd_release(struct device *dev)
93 {
94 struct mtd_info *mtd = dev_get_drvdata(dev);
95 dev_t index = MTD_DEVT(mtd->index);
96
97 idr_remove(&mtd_idr, mtd->index);
98 of_node_put(mtd_get_of_node(mtd));
99
100 if (mtd_is_partition(mtd))
101 release_mtd_partition(mtd);
102
103 /* remove /dev/mtdXro node */
104 device_destroy(&mtd_class, index + 1);
105 }
106
mtd_device_release(struct kref * kref)107 static void mtd_device_release(struct kref *kref)
108 {
109 struct mtd_info *mtd = container_of(kref, struct mtd_info, refcnt);
110 bool is_partition = mtd_is_partition(mtd);
111
112 debugfs_remove_recursive(mtd->dbg.dfs_dir);
113
114 /* Try to remove the NVMEM provider */
115 nvmem_unregister(mtd->nvmem);
116
117 device_unregister(&mtd->dev);
118
119 /*
120 * Clear dev so mtd can be safely re-registered later if desired.
121 * Should not be done for partition,
122 * as it was already destroyed in device_unregister().
123 */
124 if (!is_partition)
125 memset(&mtd->dev, 0, sizeof(mtd->dev));
126
127 module_put(THIS_MODULE);
128 }
129
130 #define MTD_DEVICE_ATTR_RO(name) \
131 static DEVICE_ATTR(name, 0444, mtd_##name##_show, NULL)
132
133 #define MTD_DEVICE_ATTR_RW(name) \
134 static DEVICE_ATTR(name, 0644, mtd_##name##_show, mtd_##name##_store)
135
mtd_type_show(struct device * dev,struct device_attribute * attr,char * buf)136 static ssize_t mtd_type_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138 {
139 struct mtd_info *mtd = dev_get_drvdata(dev);
140 char *type;
141
142 switch (mtd->type) {
143 case MTD_ABSENT:
144 type = "absent";
145 break;
146 case MTD_RAM:
147 type = "ram";
148 break;
149 case MTD_ROM:
150 type = "rom";
151 break;
152 case MTD_NORFLASH:
153 type = "nor";
154 break;
155 case MTD_NANDFLASH:
156 type = "nand";
157 break;
158 case MTD_DATAFLASH:
159 type = "dataflash";
160 break;
161 case MTD_UBIVOLUME:
162 type = "ubi";
163 break;
164 case MTD_MLCNANDFLASH:
165 type = "mlc-nand";
166 break;
167 default:
168 type = "unknown";
169 }
170
171 return sysfs_emit(buf, "%s\n", type);
172 }
173 MTD_DEVICE_ATTR_RO(type);
174
mtd_flags_show(struct device * dev,struct device_attribute * attr,char * buf)175 static ssize_t mtd_flags_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
177 {
178 struct mtd_info *mtd = dev_get_drvdata(dev);
179
180 return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags);
181 }
182 MTD_DEVICE_ATTR_RO(flags);
183
mtd_size_show(struct device * dev,struct device_attribute * attr,char * buf)184 static ssize_t mtd_size_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
186 {
187 struct mtd_info *mtd = dev_get_drvdata(dev);
188
189 return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size);
190 }
191 MTD_DEVICE_ATTR_RO(size);
192
mtd_erasesize_show(struct device * dev,struct device_attribute * attr,char * buf)193 static ssize_t mtd_erasesize_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195 {
196 struct mtd_info *mtd = dev_get_drvdata(dev);
197
198 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize);
199 }
200 MTD_DEVICE_ATTR_RO(erasesize);
201
mtd_writesize_show(struct device * dev,struct device_attribute * attr,char * buf)202 static ssize_t mtd_writesize_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
204 {
205 struct mtd_info *mtd = dev_get_drvdata(dev);
206
207 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize);
208 }
209 MTD_DEVICE_ATTR_RO(writesize);
210
mtd_subpagesize_show(struct device * dev,struct device_attribute * attr,char * buf)211 static ssize_t mtd_subpagesize_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
213 {
214 struct mtd_info *mtd = dev_get_drvdata(dev);
215 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
216
217 return sysfs_emit(buf, "%u\n", subpagesize);
218 }
219 MTD_DEVICE_ATTR_RO(subpagesize);
220
mtd_oobsize_show(struct device * dev,struct device_attribute * attr,char * buf)221 static ssize_t mtd_oobsize_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223 {
224 struct mtd_info *mtd = dev_get_drvdata(dev);
225
226 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize);
227 }
228 MTD_DEVICE_ATTR_RO(oobsize);
229
mtd_oobavail_show(struct device * dev,struct device_attribute * attr,char * buf)230 static ssize_t mtd_oobavail_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
232 {
233 struct mtd_info *mtd = dev_get_drvdata(dev);
234
235 return sysfs_emit(buf, "%u\n", mtd->oobavail);
236 }
237 MTD_DEVICE_ATTR_RO(oobavail);
238
mtd_numeraseregions_show(struct device * dev,struct device_attribute * attr,char * buf)239 static ssize_t mtd_numeraseregions_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241 {
242 struct mtd_info *mtd = dev_get_drvdata(dev);
243
244 return sysfs_emit(buf, "%u\n", mtd->numeraseregions);
245 }
246 MTD_DEVICE_ATTR_RO(numeraseregions);
247
mtd_name_show(struct device * dev,struct device_attribute * attr,char * buf)248 static ssize_t mtd_name_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250 {
251 struct mtd_info *mtd = dev_get_drvdata(dev);
252
253 return sysfs_emit(buf, "%s\n", mtd->name);
254 }
255 MTD_DEVICE_ATTR_RO(name);
256
mtd_ecc_strength_show(struct device * dev,struct device_attribute * attr,char * buf)257 static ssize_t mtd_ecc_strength_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
259 {
260 struct mtd_info *mtd = dev_get_drvdata(dev);
261
262 return sysfs_emit(buf, "%u\n", mtd->ecc_strength);
263 }
264 MTD_DEVICE_ATTR_RO(ecc_strength);
265
mtd_bitflip_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)266 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
267 struct device_attribute *attr,
268 char *buf)
269 {
270 struct mtd_info *mtd = dev_get_drvdata(dev);
271
272 return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold);
273 }
274
mtd_bitflip_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)275 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
276 struct device_attribute *attr,
277 const char *buf, size_t count)
278 {
279 struct mtd_info *mtd = dev_get_drvdata(dev);
280 unsigned int bitflip_threshold;
281 int retval;
282
283 retval = kstrtouint(buf, 0, &bitflip_threshold);
284 if (retval)
285 return retval;
286
287 mtd->bitflip_threshold = bitflip_threshold;
288 return count;
289 }
290 MTD_DEVICE_ATTR_RW(bitflip_threshold);
291
mtd_ecc_step_size_show(struct device * dev,struct device_attribute * attr,char * buf)292 static ssize_t mtd_ecc_step_size_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294 {
295 struct mtd_info *mtd = dev_get_drvdata(dev);
296
297 return sysfs_emit(buf, "%u\n", mtd->ecc_step_size);
298
299 }
300 MTD_DEVICE_ATTR_RO(ecc_step_size);
301
mtd_corrected_bits_show(struct device * dev,struct device_attribute * attr,char * buf)302 static ssize_t mtd_corrected_bits_show(struct device *dev,
303 struct device_attribute *attr, char *buf)
304 {
305 struct mtd_info *mtd = dev_get_drvdata(dev);
306 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
307
308 return sysfs_emit(buf, "%u\n", ecc_stats->corrected);
309 }
310 MTD_DEVICE_ATTR_RO(corrected_bits); /* ecc stats corrected */
311
mtd_ecc_failures_show(struct device * dev,struct device_attribute * attr,char * buf)312 static ssize_t mtd_ecc_failures_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314 {
315 struct mtd_info *mtd = dev_get_drvdata(dev);
316 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
317
318 return sysfs_emit(buf, "%u\n", ecc_stats->failed);
319 }
320 MTD_DEVICE_ATTR_RO(ecc_failures); /* ecc stats errors */
321
mtd_bad_blocks_show(struct device * dev,struct device_attribute * attr,char * buf)322 static ssize_t mtd_bad_blocks_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324 {
325 struct mtd_info *mtd = dev_get_drvdata(dev);
326 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
327
328 return sysfs_emit(buf, "%u\n", ecc_stats->badblocks);
329 }
330 MTD_DEVICE_ATTR_RO(bad_blocks);
331
mtd_bbt_blocks_show(struct device * dev,struct device_attribute * attr,char * buf)332 static ssize_t mtd_bbt_blocks_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
334 {
335 struct mtd_info *mtd = dev_get_drvdata(dev);
336 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
337
338 return sysfs_emit(buf, "%u\n", ecc_stats->bbtblocks);
339 }
340 MTD_DEVICE_ATTR_RO(bbt_blocks);
341
342 static struct attribute *mtd_attrs[] = {
343 &dev_attr_type.attr,
344 &dev_attr_flags.attr,
345 &dev_attr_size.attr,
346 &dev_attr_erasesize.attr,
347 &dev_attr_writesize.attr,
348 &dev_attr_subpagesize.attr,
349 &dev_attr_oobsize.attr,
350 &dev_attr_oobavail.attr,
351 &dev_attr_numeraseregions.attr,
352 &dev_attr_name.attr,
353 &dev_attr_ecc_strength.attr,
354 &dev_attr_ecc_step_size.attr,
355 &dev_attr_corrected_bits.attr,
356 &dev_attr_ecc_failures.attr,
357 &dev_attr_bad_blocks.attr,
358 &dev_attr_bbt_blocks.attr,
359 &dev_attr_bitflip_threshold.attr,
360 NULL,
361 };
362 ATTRIBUTE_GROUPS(mtd);
363
364 static const struct device_type mtd_devtype = {
365 .name = "mtd",
366 .groups = mtd_groups,
367 .release = mtd_release,
368 };
369
370 static bool mtd_expert_analysis_mode;
371
372 #ifdef CONFIG_DEBUG_FS
mtd_check_expert_analysis_mode(void)373 bool mtd_check_expert_analysis_mode(void)
374 {
375 const char *mtd_expert_analysis_warning =
376 "Bad block checks have been entirely disabled.\n"
377 "This is only reserved for post-mortem forensics and debug purposes.\n"
378 "Never enable this mode if you do not know what you are doing!\n";
379
380 return WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning);
381 }
382 EXPORT_SYMBOL_GPL(mtd_check_expert_analysis_mode);
383 #endif
384
385 static struct dentry *dfs_dir_mtd;
386
mtd_ooblayout_show(struct seq_file * s,void * p,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * region))387 static int mtd_ooblayout_show(struct seq_file *s, void *p,
388 int (*iter)(struct mtd_info *, int section,
389 struct mtd_oob_region *region))
390 {
391 struct mtd_info *mtd = s->private;
392 int section;
393
394 for (section = 0;; section++) {
395 struct mtd_oob_region region;
396 int err;
397
398 err = iter(mtd, section, ®ion);
399 if (err) {
400 if (err == -ERANGE)
401 break;
402
403 return err;
404 }
405
406 seq_printf(s, "%-3d %4u %4u\n", section, region.offset,
407 region.length);
408 }
409
410 return 0;
411 }
412
mtd_ooblayout_ecc_show(struct seq_file * s,void * p)413 static int mtd_ooblayout_ecc_show(struct seq_file *s, void *p)
414 {
415 return mtd_ooblayout_show(s, p, mtd_ooblayout_ecc);
416 }
417 DEFINE_SHOW_ATTRIBUTE(mtd_ooblayout_ecc);
418
mtd_ooblayout_free_show(struct seq_file * s,void * p)419 static int mtd_ooblayout_free_show(struct seq_file *s, void *p)
420 {
421 return mtd_ooblayout_show(s, p, mtd_ooblayout_free);
422 }
423 DEFINE_SHOW_ATTRIBUTE(mtd_ooblayout_free);
424
mtd_debugfs_populate(struct mtd_info * mtd)425 static void mtd_debugfs_populate(struct mtd_info *mtd)
426 {
427 struct device *dev = &mtd->dev;
428 struct mtd_oob_region region;
429
430 if (IS_ERR_OR_NULL(dfs_dir_mtd))
431 return;
432
433 mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(dev), dfs_dir_mtd);
434 if (IS_ERR_OR_NULL(mtd->dbg.dfs_dir))
435 return;
436
437 /* Create ooblayout files only if at least one region is present. */
438 if (mtd_ooblayout_ecc(mtd, 0, ®ion) == 0)
439 debugfs_create_file("ooblayout_ecc", 0444, mtd->dbg.dfs_dir,
440 mtd, &mtd_ooblayout_ecc_fops);
441
442 if (mtd_ooblayout_free(mtd, 0, ®ion) == 0)
443 debugfs_create_file("ooblayout_free", 0444, mtd->dbg.dfs_dir,
444 mtd, &mtd_ooblayout_free_fops);
445 }
446
447 #ifndef CONFIG_MMU
mtd_mmap_capabilities(struct mtd_info * mtd)448 unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
449 {
450 switch (mtd->type) {
451 case MTD_RAM:
452 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
453 NOMMU_MAP_READ | NOMMU_MAP_WRITE;
454 case MTD_ROM:
455 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
456 NOMMU_MAP_READ;
457 default:
458 return NOMMU_MAP_COPY;
459 }
460 }
461 EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
462 #endif
463
mtd_reboot_notifier(struct notifier_block * n,unsigned long state,void * cmd)464 static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
465 void *cmd)
466 {
467 struct mtd_info *mtd;
468
469 mtd = container_of(n, struct mtd_info, reboot_notifier);
470 mtd->_reboot(mtd);
471
472 return NOTIFY_DONE;
473 }
474
475 /**
476 * mtd_wunit_to_pairing_info - get pairing information of a wunit
477 * @mtd: pointer to new MTD device info structure
478 * @wunit: write unit we are interested in
479 * @info: returned pairing information
480 *
481 * Retrieve pairing information associated to the wunit.
482 * This is mainly useful when dealing with MLC/TLC NANDs where pages can be
483 * paired together, and where programming a page may influence the page it is
484 * paired with.
485 * The notion of page is replaced by the term wunit (write-unit) to stay
486 * consistent with the ->writesize field.
487 *
488 * The @wunit argument can be extracted from an absolute offset using
489 * mtd_offset_to_wunit(). @info is filled with the pairing information attached
490 * to @wunit.
491 *
492 * From the pairing info the MTD user can find all the wunits paired with
493 * @wunit using the following loop:
494 *
495 * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
496 * info.pair = i;
497 * mtd_pairing_info_to_wunit(mtd, &info);
498 * ...
499 * }
500 */
mtd_wunit_to_pairing_info(struct mtd_info * mtd,int wunit,struct mtd_pairing_info * info)501 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
502 struct mtd_pairing_info *info)
503 {
504 struct mtd_info *master = mtd_get_master(mtd);
505 int npairs = mtd_wunit_per_eb(master) / mtd_pairing_groups(master);
506
507 if (wunit < 0 || wunit >= npairs)
508 return -EINVAL;
509
510 if (master->pairing && master->pairing->get_info)
511 return master->pairing->get_info(master, wunit, info);
512
513 info->group = 0;
514 info->pair = wunit;
515
516 return 0;
517 }
518 EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info);
519
520 /**
521 * mtd_pairing_info_to_wunit - get wunit from pairing information
522 * @mtd: pointer to new MTD device info structure
523 * @info: pairing information struct
524 *
525 * Returns a positive number representing the wunit associated to the info
526 * struct, or a negative error code.
527 *
528 * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to
529 * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info()
530 * doc).
531 *
532 * It can also be used to only program the first page of each pair (i.e.
533 * page attached to group 0), which allows one to use an MLC NAND in
534 * software-emulated SLC mode:
535 *
536 * info.group = 0;
537 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
538 * for (info.pair = 0; info.pair < npairs; info.pair++) {
539 * wunit = mtd_pairing_info_to_wunit(mtd, &info);
540 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
541 * mtd->writesize, &retlen, buf + (i * mtd->writesize));
542 * }
543 */
mtd_pairing_info_to_wunit(struct mtd_info * mtd,const struct mtd_pairing_info * info)544 int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
545 const struct mtd_pairing_info *info)
546 {
547 struct mtd_info *master = mtd_get_master(mtd);
548 int ngroups = mtd_pairing_groups(master);
549 int npairs = mtd_wunit_per_eb(master) / ngroups;
550
551 if (!info || info->pair < 0 || info->pair >= npairs ||
552 info->group < 0 || info->group >= ngroups)
553 return -EINVAL;
554
555 if (master->pairing && master->pairing->get_wunit)
556 return mtd->pairing->get_wunit(master, info);
557
558 return info->pair;
559 }
560 EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit);
561
562 /**
563 * mtd_pairing_groups - get the number of pairing groups
564 * @mtd: pointer to new MTD device info structure
565 *
566 * Returns the number of pairing groups.
567 *
568 * This number is usually equal to the number of bits exposed by a single
569 * cell, and can be used in conjunction with mtd_pairing_info_to_wunit()
570 * to iterate over all pages of a given pair.
571 */
mtd_pairing_groups(struct mtd_info * mtd)572 int mtd_pairing_groups(struct mtd_info *mtd)
573 {
574 struct mtd_info *master = mtd_get_master(mtd);
575
576 if (!master->pairing || !master->pairing->ngroups)
577 return 1;
578
579 return master->pairing->ngroups;
580 }
581 EXPORT_SYMBOL_GPL(mtd_pairing_groups);
582
mtd_nvmem_reg_read(void * priv,unsigned int offset,void * val,size_t bytes)583 static int mtd_nvmem_reg_read(void *priv, unsigned int offset,
584 void *val, size_t bytes)
585 {
586 struct mtd_info *mtd = priv;
587 size_t retlen;
588 int err;
589
590 err = mtd_read(mtd, offset, bytes, &retlen, val);
591 if (err && err != -EUCLEAN)
592 return err;
593
594 return retlen == bytes ? 0 : -EIO;
595 }
596
mtd_nvmem_add(struct mtd_info * mtd)597 static int mtd_nvmem_add(struct mtd_info *mtd)
598 {
599 struct device_node *node = mtd_get_of_node(mtd);
600 struct nvmem_config config = {};
601
602 config.id = NVMEM_DEVID_NONE;
603 config.dev = &mtd->dev;
604 config.name = dev_name(&mtd->dev);
605 config.owner = THIS_MODULE;
606 config.add_legacy_fixed_of_cells = of_device_is_compatible(node, "nvmem-cells");
607 config.reg_read = mtd_nvmem_reg_read;
608 config.size = mtd->size;
609 config.word_size = 1;
610 config.stride = 1;
611 config.read_only = true;
612 config.root_only = true;
613 config.ignore_wp = true;
614 config.priv = mtd;
615
616 mtd->nvmem = nvmem_register(&config);
617 if (IS_ERR(mtd->nvmem)) {
618 /* Just ignore if there is no NVMEM support in the kernel */
619 if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP)
620 mtd->nvmem = NULL;
621 else
622 return dev_err_probe(&mtd->dev, PTR_ERR(mtd->nvmem),
623 "Failed to register NVMEM device\n");
624 }
625
626 return 0;
627 }
628
mtd_check_of_node(struct mtd_info * mtd)629 static void mtd_check_of_node(struct mtd_info *mtd)
630 {
631 struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
632 const char *pname, *prefix = "partition-";
633 int plen, mtd_name_len, offset, prefix_len;
634
635 /* Check if MTD already has a device node */
636 if (mtd_get_of_node(mtd))
637 return;
638
639 if (!mtd_is_partition(mtd))
640 return;
641
642 parent_dn = of_node_get(mtd_get_of_node(mtd->parent));
643 if (!parent_dn)
644 return;
645
646 if (mtd_is_partition(mtd->parent))
647 partitions = of_node_get(parent_dn);
648 else
649 partitions = of_get_child_by_name(parent_dn, "partitions");
650 if (!partitions)
651 goto exit_parent;
652
653 prefix_len = strlen(prefix);
654 mtd_name_len = strlen(mtd->name);
655
656 /* Search if a partition is defined with the same name */
657 for_each_child_of_node(partitions, mtd_dn) {
658 /* Skip partition with no/wrong prefix */
659 if (!of_node_name_prefix(mtd_dn, prefix))
660 continue;
661
662 /* Label have priority. Check that first */
663 if (!of_property_read_string(mtd_dn, "label", &pname)) {
664 offset = 0;
665 } else {
666 pname = mtd_dn->name;
667 offset = prefix_len;
668 }
669
670 plen = strlen(pname) - offset;
671 if (plen == mtd_name_len &&
672 !strncmp(mtd->name, pname + offset, plen)) {
673 mtd_set_of_node(mtd, mtd_dn);
674 of_node_put(mtd_dn);
675 break;
676 }
677 }
678
679 of_node_put(partitions);
680 exit_parent:
681 of_node_put(parent_dn);
682 }
683
684 /**
685 * add_mtd_device - register an MTD device
686 * @mtd: pointer to new MTD device info structure
687 *
688 * Add a device to the list of MTD devices present in the system, and
689 * notify each currently active MTD 'user' of its arrival. Returns
690 * zero on success or non-zero on failure.
691 */
692
add_mtd_device(struct mtd_info * mtd)693 int add_mtd_device(struct mtd_info *mtd)
694 {
695 struct device_node *np = mtd_get_of_node(mtd);
696 struct mtd_info *master = mtd_get_master(mtd);
697 struct mtd_notifier *not;
698 int i, error, ofidx;
699
700 /*
701 * May occur, for instance, on buggy drivers which call
702 * mtd_device_parse_register() multiple times on the same master MTD,
703 * especially with CONFIG_MTD_PARTITIONED_MASTER=y.
704 */
705 if (WARN_ONCE(mtd->dev.type, "MTD already registered\n"))
706 return -EEXIST;
707
708 BUG_ON(mtd->writesize == 0);
709
710 /*
711 * MTD drivers should implement ->_{write,read}() or
712 * ->_{write,read}_oob(), but not both.
713 */
714 if (WARN_ON((mtd->_write && mtd->_write_oob) ||
715 (mtd->_read && mtd->_read_oob)))
716 return -EINVAL;
717
718 if (WARN_ON((!mtd->erasesize || !master->_erase) &&
719 !(mtd->flags & MTD_NO_ERASE)))
720 return -EINVAL;
721
722 /*
723 * MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the
724 * master is an MLC NAND and has a proper pairing scheme defined.
725 * We also reject masters that implement ->_writev() for now, because
726 * NAND controller drivers don't implement this hook, and adding the
727 * SLC -> MLC address/length conversion to this path is useless if we
728 * don't have a user.
729 */
730 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION &&
731 (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH ||
732 !master->pairing || master->_writev))
733 return -EINVAL;
734
735 mutex_lock(&mtd_table_mutex);
736
737 ofidx = -1;
738 if (np)
739 ofidx = of_alias_get_id(np, "mtd");
740 if (ofidx >= 0)
741 i = idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL);
742 else
743 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
744 if (i < 0) {
745 error = i;
746 goto fail_locked;
747 }
748
749 mtd->index = i;
750 kref_init(&mtd->refcnt);
751
752 /* default value if not set by driver */
753 if (mtd->bitflip_threshold == 0)
754 mtd->bitflip_threshold = mtd->ecc_strength;
755
756 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
757 int ngroups = mtd_pairing_groups(master);
758
759 mtd->erasesize /= ngroups;
760 mtd->size = (u64)mtd_div_by_eb(mtd->size, master) *
761 mtd->erasesize;
762 }
763
764 if (is_power_of_2(mtd->erasesize))
765 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
766 else
767 mtd->erasesize_shift = 0;
768
769 if (is_power_of_2(mtd->writesize))
770 mtd->writesize_shift = ffs(mtd->writesize) - 1;
771 else
772 mtd->writesize_shift = 0;
773
774 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
775 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
776
777 /* Some chips always power up locked. Unlock them now */
778 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
779 error = mtd_unlock(mtd, 0, mtd->size);
780 if (error && error != -EOPNOTSUPP)
781 printk(KERN_WARNING
782 "%s: unlock failed, writes may not work\n",
783 mtd->name);
784 /* Ignore unlock failures? */
785 error = 0;
786 }
787
788 /* Caller should have set dev.parent to match the
789 * physical device, if appropriate.
790 */
791 mtd->dev.type = &mtd_devtype;
792 mtd->dev.class = &mtd_class;
793 mtd->dev.devt = MTD_DEVT(i);
794 error = dev_set_name(&mtd->dev, "mtd%d", i);
795 if (error)
796 goto fail_devname;
797 dev_set_drvdata(&mtd->dev, mtd);
798 mtd_check_of_node(mtd);
799 of_node_get(mtd_get_of_node(mtd));
800 error = device_register(&mtd->dev);
801 if (error) {
802 put_device(&mtd->dev);
803 goto fail_added;
804 }
805
806 /* Add the nvmem provider */
807 error = mtd_nvmem_add(mtd);
808 if (error)
809 goto fail_nvmem_add;
810
811 mtd_debugfs_populate(mtd);
812
813 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
814 "mtd%dro", i);
815
816 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
817 /* No need to get a refcount on the module containing
818 the notifier, since we hold the mtd_table_mutex */
819 list_for_each_entry(not, &mtd_notifiers, list)
820 not->add(mtd);
821
822 mutex_unlock(&mtd_table_mutex);
823
824 if (of_property_read_bool(mtd_get_of_node(mtd), "linux,rootfs")) {
825 if (IS_BUILTIN(CONFIG_MTD)) {
826 pr_info("mtd: setting mtd%d (%s) as root device\n", mtd->index, mtd->name);
827 ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, mtd->index);
828 } else {
829 pr_warn("mtd: can't set mtd%d (%s) as root device - mtd must be builtin\n",
830 mtd->index, mtd->name);
831 }
832 }
833
834 /* We _know_ we aren't being removed, because
835 our caller is still holding us here. So none
836 of this try_ nonsense, and no bitching about it
837 either. :) */
838 __module_get(THIS_MODULE);
839 return 0;
840
841 fail_nvmem_add:
842 device_unregister(&mtd->dev);
843 fail_added:
844 of_node_put(mtd_get_of_node(mtd));
845 fail_devname:
846 idr_remove(&mtd_idr, i);
847 fail_locked:
848 mutex_unlock(&mtd_table_mutex);
849 return error;
850 }
851
852 /**
853 * del_mtd_device - unregister an MTD device
854 * @mtd: pointer to MTD device info structure
855 *
856 * Remove a device from the list of MTD devices present in the system,
857 * and notify each currently active MTD 'user' of its departure.
858 * Returns zero on success or 1 on failure, which currently will happen
859 * if the requested device does not appear to be present in the list.
860 */
861
del_mtd_device(struct mtd_info * mtd)862 int del_mtd_device(struct mtd_info *mtd)
863 {
864 int ret;
865 struct mtd_notifier *not;
866
867 mutex_lock(&mtd_table_mutex);
868
869 if (idr_find(&mtd_idr, mtd->index) != mtd) {
870 ret = -ENODEV;
871 goto out_error;
872 }
873
874 /* No need to get a refcount on the module containing
875 the notifier, since we hold the mtd_table_mutex */
876 list_for_each_entry(not, &mtd_notifiers, list)
877 not->remove(mtd);
878
879 kref_put(&mtd->refcnt, mtd_device_release);
880 ret = 0;
881
882 out_error:
883 mutex_unlock(&mtd_table_mutex);
884 return ret;
885 }
886
887 /*
888 * Set a few defaults based on the parent devices, if not provided by the
889 * driver
890 */
mtd_set_dev_defaults(struct mtd_info * mtd)891 static void mtd_set_dev_defaults(struct mtd_info *mtd)
892 {
893 if (mtd->dev.parent) {
894 if (!mtd->owner && mtd->dev.parent->driver)
895 mtd->owner = mtd->dev.parent->driver->owner;
896 if (!mtd->name)
897 mtd->name = dev_name(mtd->dev.parent);
898 } else {
899 pr_debug("mtd device won't show a device symlink in sysfs\n");
900 }
901
902 INIT_LIST_HEAD(&mtd->partitions);
903 mutex_init(&mtd->master.partitions_lock);
904 mutex_init(&mtd->master.chrdev_lock);
905 }
906
mtd_otp_size(struct mtd_info * mtd,bool is_user)907 static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
908 {
909 struct otp_info *info;
910 ssize_t size = 0;
911 unsigned int i;
912 size_t retlen;
913 int ret;
914
915 info = kmalloc(PAGE_SIZE, GFP_KERNEL);
916 if (!info)
917 return -ENOMEM;
918
919 if (is_user)
920 ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info);
921 else
922 ret = mtd_get_fact_prot_info(mtd, PAGE_SIZE, &retlen, info);
923 if (ret)
924 goto err;
925
926 for (i = 0; i < retlen / sizeof(*info); i++)
927 size += info[i].length;
928
929 kfree(info);
930 return size;
931
932 err:
933 kfree(info);
934
935 /* ENODATA means there is no OTP region. */
936 return ret == -ENODATA ? 0 : ret;
937 }
938
mtd_otp_nvmem_register(struct mtd_info * mtd,const char * compatible,int size,nvmem_reg_read_t reg_read)939 static struct nvmem_device *mtd_otp_nvmem_register(struct mtd_info *mtd,
940 const char *compatible,
941 int size,
942 nvmem_reg_read_t reg_read)
943 {
944 struct nvmem_device *nvmem = NULL;
945 struct nvmem_config config = {};
946 struct device_node *np;
947
948 /* DT binding is optional */
949 np = of_get_compatible_child(mtd->dev.of_node, compatible);
950
951 /* OTP nvmem will be registered on the physical device */
952 config.dev = mtd->dev.parent;
953 config.name = compatible;
954 config.id = NVMEM_DEVID_AUTO;
955 config.owner = THIS_MODULE;
956 config.add_legacy_fixed_of_cells = !mtd_type_is_nand(mtd);
957 config.type = NVMEM_TYPE_OTP;
958 config.root_only = true;
959 config.ignore_wp = true;
960 config.reg_read = reg_read;
961 config.size = size;
962 config.of_node = np;
963 config.priv = mtd;
964
965 nvmem = nvmem_register(&config);
966 /* Just ignore if there is no NVMEM support in the kernel */
967 if (IS_ERR(nvmem) && PTR_ERR(nvmem) == -EOPNOTSUPP)
968 nvmem = NULL;
969
970 of_node_put(np);
971
972 return nvmem;
973 }
974
mtd_nvmem_user_otp_reg_read(void * priv,unsigned int offset,void * val,size_t bytes)975 static int mtd_nvmem_user_otp_reg_read(void *priv, unsigned int offset,
976 void *val, size_t bytes)
977 {
978 struct mtd_info *mtd = priv;
979 size_t retlen;
980 int ret;
981
982 ret = mtd_read_user_prot_reg(mtd, offset, bytes, &retlen, val);
983 if (ret)
984 return ret;
985
986 return retlen == bytes ? 0 : -EIO;
987 }
988
mtd_nvmem_fact_otp_reg_read(void * priv,unsigned int offset,void * val,size_t bytes)989 static int mtd_nvmem_fact_otp_reg_read(void *priv, unsigned int offset,
990 void *val, size_t bytes)
991 {
992 struct mtd_info *mtd = priv;
993 size_t retlen;
994 int ret;
995
996 ret = mtd_read_fact_prot_reg(mtd, offset, bytes, &retlen, val);
997 if (ret)
998 return ret;
999
1000 return retlen == bytes ? 0 : -EIO;
1001 }
1002
mtd_otp_nvmem_add(struct mtd_info * mtd)1003 static int mtd_otp_nvmem_add(struct mtd_info *mtd)
1004 {
1005 struct device *dev = mtd->dev.parent;
1006 struct nvmem_device *nvmem;
1007 ssize_t size;
1008 int err;
1009
1010 if (mtd->_get_user_prot_info && mtd->_read_user_prot_reg) {
1011 size = mtd_otp_size(mtd, true);
1012 if (size < 0) {
1013 err = size;
1014 goto err;
1015 }
1016
1017 if (size > 0) {
1018 nvmem = mtd_otp_nvmem_register(mtd, "user-otp", size,
1019 mtd_nvmem_user_otp_reg_read);
1020 if (IS_ERR(nvmem)) {
1021 err = PTR_ERR(nvmem);
1022 goto err;
1023 }
1024 mtd->otp_user_nvmem = nvmem;
1025 }
1026 }
1027
1028 if (mtd->_get_fact_prot_info && mtd->_read_fact_prot_reg) {
1029 size = mtd_otp_size(mtd, false);
1030 if (size < 0) {
1031 err = size;
1032 goto err;
1033 }
1034
1035 if (size > 0) {
1036 /*
1037 * The factory OTP contains thing such as a unique serial
1038 * number and is small, so let's read it out and put it
1039 * into the entropy pool.
1040 */
1041 void *otp;
1042
1043 otp = kmalloc(size, GFP_KERNEL);
1044 if (!otp) {
1045 err = -ENOMEM;
1046 goto err;
1047 }
1048 err = mtd_nvmem_fact_otp_reg_read(mtd, 0, otp, size);
1049 if (err < 0) {
1050 kfree(otp);
1051 goto err;
1052 }
1053 add_device_randomness(otp, err);
1054 kfree(otp);
1055
1056 nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size,
1057 mtd_nvmem_fact_otp_reg_read);
1058 if (IS_ERR(nvmem)) {
1059 err = PTR_ERR(nvmem);
1060 goto err;
1061 }
1062 mtd->otp_factory_nvmem = nvmem;
1063 }
1064 }
1065
1066 return 0;
1067
1068 err:
1069 nvmem_unregister(mtd->otp_user_nvmem);
1070 /* Don't report error if OTP is not supported. */
1071 if (err == -EOPNOTSUPP)
1072 return 0;
1073 return dev_err_probe(dev, err, "Failed to register OTP NVMEM device\n");
1074 }
1075
1076 /**
1077 * mtd_device_parse_register - parse partitions and register an MTD device.
1078 *
1079 * @mtd: the MTD device to register
1080 * @types: the list of MTD partition probes to try, see
1081 * 'parse_mtd_partitions()' for more information
1082 * @parser_data: MTD partition parser-specific data
1083 * @parts: fallback partition information to register, if parsing fails;
1084 * only valid if %nr_parts > %0
1085 * @nr_parts: the number of partitions in parts, if zero then the full
1086 * MTD device is registered if no partition info is found
1087 *
1088 * This function aggregates MTD partitions parsing (done by
1089 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
1090 * basically follows the most common pattern found in many MTD drivers:
1091 *
1092 * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is
1093 * registered first.
1094 * * Then It tries to probe partitions on MTD device @mtd using parsers
1095 * specified in @types (if @types is %NULL, then the default list of parsers
1096 * is used, see 'parse_mtd_partitions()' for more information). If none are
1097 * found this functions tries to fallback to information specified in
1098 * @parts/@nr_parts.
1099 * * If no partitions were found this function just registers the MTD device
1100 * @mtd and exits.
1101 *
1102 * Returns zero in case of success and a negative error code in case of failure.
1103 */
mtd_device_parse_register(struct mtd_info * mtd,const char * const * types,struct mtd_part_parser_data * parser_data,const struct mtd_partition * parts,int nr_parts)1104 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
1105 struct mtd_part_parser_data *parser_data,
1106 const struct mtd_partition *parts,
1107 int nr_parts)
1108 {
1109 int ret, err;
1110
1111 mtd_set_dev_defaults(mtd);
1112
1113 ret = mtd_otp_nvmem_add(mtd);
1114 if (ret)
1115 goto out;
1116
1117 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
1118 ret = add_mtd_device(mtd);
1119 if (ret)
1120 goto out;
1121 }
1122
1123 /* Prefer parsed partitions over driver-provided fallback */
1124 ret = parse_mtd_partitions(mtd, types, parser_data);
1125 if (ret == -EPROBE_DEFER)
1126 goto out;
1127
1128 if (ret > 0)
1129 ret = 0;
1130 else if (nr_parts)
1131 ret = add_mtd_partitions(mtd, parts, nr_parts);
1132 else if (!device_is_registered(&mtd->dev))
1133 ret = add_mtd_device(mtd);
1134 else
1135 ret = 0;
1136
1137 if (ret)
1138 goto out;
1139
1140 /*
1141 * FIXME: some drivers unfortunately call this function more than once.
1142 * So we have to check if we've already assigned the reboot notifier.
1143 *
1144 * Generally, we can make multiple calls work for most cases, but it
1145 * does cause problems with parse_mtd_partitions() above (e.g.,
1146 * cmdlineparts will register partitions more than once).
1147 */
1148 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call,
1149 "MTD already registered\n");
1150 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
1151 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
1152 register_reboot_notifier(&mtd->reboot_notifier);
1153 }
1154
1155 out:
1156 if (ret) {
1157 nvmem_unregister(mtd->otp_user_nvmem);
1158 nvmem_unregister(mtd->otp_factory_nvmem);
1159 }
1160
1161 if (ret && device_is_registered(&mtd->dev)) {
1162 err = del_mtd_device(mtd);
1163 if (err)
1164 pr_err("Error when deleting MTD device (%d)\n", err);
1165 }
1166
1167 return ret;
1168 }
1169 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
1170
1171 /**
1172 * mtd_device_unregister - unregister an existing MTD device.
1173 *
1174 * @master: the MTD device to unregister. This will unregister both the master
1175 * and any partitions if registered.
1176 */
mtd_device_unregister(struct mtd_info * master)1177 int mtd_device_unregister(struct mtd_info *master)
1178 {
1179 int err;
1180
1181 if (master->_reboot) {
1182 unregister_reboot_notifier(&master->reboot_notifier);
1183 memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
1184 }
1185
1186 nvmem_unregister(master->otp_user_nvmem);
1187 nvmem_unregister(master->otp_factory_nvmem);
1188
1189 err = del_mtd_partitions(master);
1190 if (err)
1191 return err;
1192
1193 if (!device_is_registered(&master->dev))
1194 return 0;
1195
1196 return del_mtd_device(master);
1197 }
1198 EXPORT_SYMBOL_GPL(mtd_device_unregister);
1199
1200 /**
1201 * register_mtd_user - register a 'user' of MTD devices.
1202 * @new: pointer to notifier info structure
1203 *
1204 * Registers a pair of callbacks function to be called upon addition
1205 * or removal of MTD devices. Causes the 'add' callback to be immediately
1206 * invoked for each MTD device currently present in the system.
1207 */
register_mtd_user(struct mtd_notifier * new)1208 void register_mtd_user (struct mtd_notifier *new)
1209 {
1210 struct mtd_info *mtd;
1211
1212 mutex_lock(&mtd_table_mutex);
1213
1214 list_add(&new->list, &mtd_notifiers);
1215
1216 __module_get(THIS_MODULE);
1217
1218 mtd_for_each_device(mtd)
1219 new->add(mtd);
1220
1221 mutex_unlock(&mtd_table_mutex);
1222 }
1223 EXPORT_SYMBOL_GPL(register_mtd_user);
1224
1225 /**
1226 * unregister_mtd_user - unregister a 'user' of MTD devices.
1227 * @old: pointer to notifier info structure
1228 *
1229 * Removes a callback function pair from the list of 'users' to be
1230 * notified upon addition or removal of MTD devices. Causes the
1231 * 'remove' callback to be immediately invoked for each MTD device
1232 * currently present in the system.
1233 */
unregister_mtd_user(struct mtd_notifier * old)1234 int unregister_mtd_user (struct mtd_notifier *old)
1235 {
1236 struct mtd_info *mtd;
1237
1238 mutex_lock(&mtd_table_mutex);
1239
1240 module_put(THIS_MODULE);
1241
1242 mtd_for_each_device(mtd)
1243 old->remove(mtd);
1244
1245 list_del(&old->list);
1246 mutex_unlock(&mtd_table_mutex);
1247 return 0;
1248 }
1249 EXPORT_SYMBOL_GPL(unregister_mtd_user);
1250
1251 /**
1252 * get_mtd_device - obtain a validated handle for an MTD device
1253 * @mtd: last known address of the required MTD device
1254 * @num: internal device number of the required MTD device
1255 *
1256 * Given a number and NULL address, return the num'th entry in the device
1257 * table, if any. Given an address and num == -1, search the device table
1258 * for a device with that address and return if it's still present. Given
1259 * both, return the num'th driver only if its address matches. Return
1260 * error code if not.
1261 */
get_mtd_device(struct mtd_info * mtd,int num)1262 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
1263 {
1264 struct mtd_info *ret = NULL, *other;
1265 int err = -ENODEV;
1266
1267 mutex_lock(&mtd_table_mutex);
1268
1269 if (num == -1) {
1270 mtd_for_each_device(other) {
1271 if (other == mtd) {
1272 ret = mtd;
1273 break;
1274 }
1275 }
1276 } else if (num >= 0) {
1277 ret = idr_find(&mtd_idr, num);
1278 if (mtd && mtd != ret)
1279 ret = NULL;
1280 }
1281
1282 if (!ret) {
1283 ret = ERR_PTR(err);
1284 goto out;
1285 }
1286
1287 err = __get_mtd_device(ret);
1288 if (err)
1289 ret = ERR_PTR(err);
1290 out:
1291 mutex_unlock(&mtd_table_mutex);
1292 return ret;
1293 }
1294 EXPORT_SYMBOL_GPL(get_mtd_device);
1295
1296
__get_mtd_device(struct mtd_info * mtd)1297 int __get_mtd_device(struct mtd_info *mtd)
1298 {
1299 struct mtd_info *master = mtd_get_master(mtd);
1300 int err;
1301
1302 if (master->_get_device) {
1303 err = master->_get_device(mtd);
1304 if (err)
1305 return err;
1306 }
1307
1308 if (!try_module_get(master->owner)) {
1309 if (master->_put_device)
1310 master->_put_device(master);
1311 return -ENODEV;
1312 }
1313
1314 while (mtd) {
1315 if (mtd != master)
1316 kref_get(&mtd->refcnt);
1317 mtd = mtd->parent;
1318 }
1319
1320 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1321 kref_get(&master->refcnt);
1322
1323 return 0;
1324 }
1325 EXPORT_SYMBOL_GPL(__get_mtd_device);
1326
1327 /**
1328 * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
1329 *
1330 * @np: device tree node
1331 */
of_get_mtd_device_by_node(struct device_node * np)1332 struct mtd_info *of_get_mtd_device_by_node(struct device_node *np)
1333 {
1334 struct mtd_info *mtd = NULL;
1335 struct mtd_info *tmp;
1336 int err;
1337
1338 mutex_lock(&mtd_table_mutex);
1339
1340 err = -EPROBE_DEFER;
1341 mtd_for_each_device(tmp) {
1342 if (mtd_get_of_node(tmp) == np) {
1343 mtd = tmp;
1344 err = __get_mtd_device(mtd);
1345 break;
1346 }
1347 }
1348
1349 mutex_unlock(&mtd_table_mutex);
1350
1351 return err ? ERR_PTR(err) : mtd;
1352 }
1353 EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node);
1354
1355 /**
1356 * get_mtd_device_nm - obtain a validated handle for an MTD device by
1357 * device name
1358 * @name: MTD device name to open
1359 *
1360 * This function returns MTD device description structure in case of
1361 * success and an error code in case of failure.
1362 */
get_mtd_device_nm(const char * name)1363 struct mtd_info *get_mtd_device_nm(const char *name)
1364 {
1365 int err = -ENODEV;
1366 struct mtd_info *mtd = NULL, *other;
1367
1368 mutex_lock(&mtd_table_mutex);
1369
1370 mtd_for_each_device(other) {
1371 if (!strcmp(name, other->name)) {
1372 mtd = other;
1373 break;
1374 }
1375 }
1376
1377 if (!mtd)
1378 goto out_unlock;
1379
1380 err = __get_mtd_device(mtd);
1381 if (err)
1382 goto out_unlock;
1383
1384 mutex_unlock(&mtd_table_mutex);
1385 return mtd;
1386
1387 out_unlock:
1388 mutex_unlock(&mtd_table_mutex);
1389 return ERR_PTR(err);
1390 }
1391 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
1392
put_mtd_device(struct mtd_info * mtd)1393 void put_mtd_device(struct mtd_info *mtd)
1394 {
1395 mutex_lock(&mtd_table_mutex);
1396 __put_mtd_device(mtd);
1397 mutex_unlock(&mtd_table_mutex);
1398
1399 }
1400 EXPORT_SYMBOL_GPL(put_mtd_device);
1401
__put_mtd_device(struct mtd_info * mtd)1402 void __put_mtd_device(struct mtd_info *mtd)
1403 {
1404 struct mtd_info *master = mtd_get_master(mtd);
1405
1406 while (mtd) {
1407 /* kref_put() can relese mtd, so keep a reference mtd->parent */
1408 struct mtd_info *parent = mtd->parent;
1409
1410 if (mtd != master)
1411 kref_put(&mtd->refcnt, mtd_device_release);
1412 mtd = parent;
1413 }
1414
1415 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1416 kref_put(&master->refcnt, mtd_device_release);
1417
1418 module_put(master->owner);
1419
1420 /* must be the last as master can be freed in the _put_device */
1421 if (master->_put_device)
1422 master->_put_device(master);
1423 }
1424 EXPORT_SYMBOL_GPL(__put_mtd_device);
1425
1426 /*
1427 * Erase is an synchronous operation. Device drivers are epected to return a
1428 * negative error code if the operation failed and update instr->fail_addr
1429 * to point the portion that was not properly erased.
1430 */
mtd_erase(struct mtd_info * mtd,struct erase_info * instr)1431 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1432 {
1433 struct mtd_info *master = mtd_get_master(mtd);
1434 u64 mst_ofs = mtd_get_master_ofs(mtd, 0);
1435 struct erase_info adjinstr;
1436 int ret;
1437
1438 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
1439 adjinstr = *instr;
1440
1441 if (!mtd->erasesize || !master->_erase)
1442 return -ENOTSUPP;
1443
1444 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
1445 return -EINVAL;
1446 if (!(mtd->flags & MTD_WRITEABLE))
1447 return -EROFS;
1448
1449 if (!instr->len)
1450 return 0;
1451
1452 ledtrig_mtd_activity();
1453
1454 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
1455 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) *
1456 master->erasesize;
1457 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) *
1458 master->erasesize) -
1459 adjinstr.addr;
1460 }
1461
1462 adjinstr.addr += mst_ofs;
1463
1464 ret = master->_erase(master, &adjinstr);
1465
1466 if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) {
1467 instr->fail_addr = adjinstr.fail_addr - mst_ofs;
1468 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
1469 instr->fail_addr = mtd_div_by_eb(instr->fail_addr,
1470 master);
1471 instr->fail_addr *= mtd->erasesize;
1472 }
1473 }
1474
1475 return ret;
1476 }
1477 EXPORT_SYMBOL_GPL(mtd_erase);
1478 ALLOW_ERROR_INJECTION(mtd_erase, ERRNO);
1479
1480 /*
1481 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
1482 */
mtd_point(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,void ** virt,resource_size_t * phys)1483 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1484 void **virt, resource_size_t *phys)
1485 {
1486 struct mtd_info *master = mtd_get_master(mtd);
1487
1488 *retlen = 0;
1489 *virt = NULL;
1490 if (phys)
1491 *phys = 0;
1492 if (!master->_point)
1493 return -EOPNOTSUPP;
1494 if (from < 0 || from >= mtd->size || len > mtd->size - from)
1495 return -EINVAL;
1496 if (!len)
1497 return 0;
1498
1499 from = mtd_get_master_ofs(mtd, from);
1500 return master->_point(master, from, len, retlen, virt, phys);
1501 }
1502 EXPORT_SYMBOL_GPL(mtd_point);
1503
1504 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
mtd_unpoint(struct mtd_info * mtd,loff_t from,size_t len)1505 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
1506 {
1507 struct mtd_info *master = mtd_get_master(mtd);
1508
1509 if (!master->_unpoint)
1510 return -EOPNOTSUPP;
1511 if (from < 0 || from >= mtd->size || len > mtd->size - from)
1512 return -EINVAL;
1513 if (!len)
1514 return 0;
1515 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len);
1516 }
1517 EXPORT_SYMBOL_GPL(mtd_unpoint);
1518
1519 /*
1520 * Allow NOMMU mmap() to directly map the device (if not NULL)
1521 * - return the address to which the offset maps
1522 * - return -ENOSYS to indicate refusal to do the mapping
1523 */
mtd_get_unmapped_area(struct mtd_info * mtd,unsigned long len,unsigned long offset,unsigned long flags)1524 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
1525 unsigned long offset, unsigned long flags)
1526 {
1527 size_t retlen;
1528 void *virt;
1529 int ret;
1530
1531 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL);
1532 if (ret)
1533 return ret;
1534 if (retlen != len) {
1535 mtd_unpoint(mtd, offset, retlen);
1536 return -ENOSYS;
1537 }
1538 return (unsigned long)virt;
1539 }
1540 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
1541
mtd_update_ecc_stats(struct mtd_info * mtd,struct mtd_info * master,const struct mtd_ecc_stats * old_stats)1542 static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master,
1543 const struct mtd_ecc_stats *old_stats)
1544 {
1545 struct mtd_ecc_stats diff;
1546
1547 if (master == mtd)
1548 return;
1549
1550 diff = master->ecc_stats;
1551 diff.failed -= old_stats->failed;
1552 diff.corrected -= old_stats->corrected;
1553
1554 while (mtd->parent) {
1555 mtd->ecc_stats.failed += diff.failed;
1556 mtd->ecc_stats.corrected += diff.corrected;
1557 mtd = mtd->parent;
1558 }
1559 }
1560
mtd_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)1561 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1562 u_char *buf)
1563 {
1564 struct mtd_oob_ops ops = {
1565 .len = len,
1566 .datbuf = buf,
1567 };
1568 int ret;
1569
1570 ret = mtd_read_oob(mtd, from, &ops);
1571 *retlen = ops.retlen;
1572
1573 WARN_ON_ONCE(*retlen != len && mtd_is_bitflip_or_eccerr(ret));
1574
1575 return ret;
1576 }
1577 EXPORT_SYMBOL_GPL(mtd_read);
1578 ALLOW_ERROR_INJECTION(mtd_read, ERRNO);
1579
mtd_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)1580 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1581 const u_char *buf)
1582 {
1583 struct mtd_oob_ops ops = {
1584 .len = len,
1585 .datbuf = (u8 *)buf,
1586 };
1587 int ret;
1588
1589 ret = mtd_write_oob(mtd, to, &ops);
1590 *retlen = ops.retlen;
1591
1592 return ret;
1593 }
1594 EXPORT_SYMBOL_GPL(mtd_write);
1595 ALLOW_ERROR_INJECTION(mtd_write, ERRNO);
1596
1597 /*
1598 * In blackbox flight recorder like scenarios we want to make successful writes
1599 * in interrupt context. panic_write() is only intended to be called when its
1600 * known the kernel is about to panic and we need the write to succeed. Since
1601 * the kernel is not going to be running for much longer, this function can
1602 * break locks and delay to ensure the write succeeds (but not sleep).
1603 */
mtd_panic_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)1604 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1605 const u_char *buf)
1606 {
1607 struct mtd_info *master = mtd_get_master(mtd);
1608
1609 *retlen = 0;
1610 if (!master->_panic_write)
1611 return -EOPNOTSUPP;
1612 if (to < 0 || to >= mtd->size || len > mtd->size - to)
1613 return -EINVAL;
1614 if (!(mtd->flags & MTD_WRITEABLE))
1615 return -EROFS;
1616 if (!len)
1617 return 0;
1618 if (!master->oops_panic_write)
1619 master->oops_panic_write = true;
1620
1621 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len,
1622 retlen, buf);
1623 }
1624 EXPORT_SYMBOL_GPL(mtd_panic_write);
1625
mtd_check_oob_ops(struct mtd_info * mtd,loff_t offs,struct mtd_oob_ops * ops)1626 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1627 struct mtd_oob_ops *ops)
1628 {
1629 /*
1630 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1631 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1632 * this case.
1633 */
1634 if (!ops->datbuf)
1635 ops->len = 0;
1636
1637 if (!ops->oobbuf)
1638 ops->ooblen = 0;
1639
1640 if (offs < 0 || offs + ops->len > mtd->size)
1641 return -EINVAL;
1642
1643 if (ops->ooblen) {
1644 size_t maxooblen;
1645
1646 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1647 return -EINVAL;
1648
1649 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1650 mtd_div_by_ws(offs, mtd)) *
1651 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1652 if (ops->ooblen > maxooblen)
1653 return -EINVAL;
1654 }
1655
1656 return 0;
1657 }
1658
mtd_read_oob_std(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)1659 static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from,
1660 struct mtd_oob_ops *ops)
1661 {
1662 struct mtd_info *master = mtd_get_master(mtd);
1663 int ret;
1664
1665 from = mtd_get_master_ofs(mtd, from);
1666 if (master->_read_oob)
1667 ret = master->_read_oob(master, from, ops);
1668 else
1669 ret = master->_read(master, from, ops->len, &ops->retlen,
1670 ops->datbuf);
1671
1672 return ret;
1673 }
1674
mtd_write_oob_std(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)1675 static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to,
1676 struct mtd_oob_ops *ops)
1677 {
1678 struct mtd_info *master = mtd_get_master(mtd);
1679 int ret;
1680
1681 to = mtd_get_master_ofs(mtd, to);
1682 if (master->_write_oob)
1683 ret = master->_write_oob(master, to, ops);
1684 else
1685 ret = master->_write(master, to, ops->len, &ops->retlen,
1686 ops->datbuf);
1687
1688 return ret;
1689 }
1690
mtd_io_emulated_slc(struct mtd_info * mtd,loff_t start,bool read,struct mtd_oob_ops * ops)1691 static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read,
1692 struct mtd_oob_ops *ops)
1693 {
1694 struct mtd_info *master = mtd_get_master(mtd);
1695 int ngroups = mtd_pairing_groups(master);
1696 int npairs = mtd_wunit_per_eb(master) / ngroups;
1697 struct mtd_oob_ops adjops = *ops;
1698 unsigned int wunit, oobavail;
1699 struct mtd_pairing_info info;
1700 int max_bitflips = 0;
1701 u32 ebofs, pageofs;
1702 loff_t base, pos;
1703
1704 ebofs = mtd_mod_by_eb(start, mtd);
1705 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize;
1706 info.group = 0;
1707 info.pair = mtd_div_by_ws(ebofs, mtd);
1708 pageofs = mtd_mod_by_ws(ebofs, mtd);
1709 oobavail = mtd_oobavail(mtd, ops);
1710
1711 while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
1712 int ret;
1713
1714 if (info.pair >= npairs) {
1715 info.pair = 0;
1716 base += master->erasesize;
1717 }
1718
1719 wunit = mtd_pairing_info_to_wunit(master, &info);
1720 pos = mtd_wunit_to_offset(mtd, base, wunit);
1721
1722 adjops.len = ops->len - ops->retlen;
1723 if (adjops.len > mtd->writesize - pageofs)
1724 adjops.len = mtd->writesize - pageofs;
1725
1726 adjops.ooblen = ops->ooblen - ops->oobretlen;
1727 if (adjops.ooblen > oobavail - adjops.ooboffs)
1728 adjops.ooblen = oobavail - adjops.ooboffs;
1729
1730 if (read) {
1731 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops);
1732 if (ret > 0)
1733 max_bitflips = max(max_bitflips, ret);
1734 } else {
1735 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops);
1736 }
1737
1738 if (ret < 0)
1739 return ret;
1740
1741 max_bitflips = max(max_bitflips, ret);
1742 ops->retlen += adjops.retlen;
1743 ops->oobretlen += adjops.oobretlen;
1744 adjops.datbuf += adjops.retlen;
1745 adjops.oobbuf += adjops.oobretlen;
1746 adjops.ooboffs = 0;
1747 pageofs = 0;
1748 info.pair++;
1749 }
1750
1751 return max_bitflips;
1752 }
1753
mtd_read_oob(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)1754 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1755 {
1756 struct mtd_info *master = mtd_get_master(mtd);
1757 struct mtd_ecc_stats old_stats = master->ecc_stats;
1758 int ret_code;
1759
1760 ops->retlen = ops->oobretlen = 0;
1761
1762 ret_code = mtd_check_oob_ops(mtd, from, ops);
1763 if (ret_code)
1764 return ret_code;
1765
1766 ledtrig_mtd_activity();
1767
1768 /* Check the validity of a potential fallback on mtd->_read */
1769 if (!master->_read_oob && (!master->_read || ops->oobbuf))
1770 return -EOPNOTSUPP;
1771
1772 if (ops->stats)
1773 memset(ops->stats, 0, sizeof(*ops->stats));
1774
1775 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
1776 ret_code = mtd_io_emulated_slc(mtd, from, true, ops);
1777 else
1778 ret_code = mtd_read_oob_std(mtd, from, ops);
1779
1780 mtd_update_ecc_stats(mtd, master, &old_stats);
1781
1782 /*
1783 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1784 * similar to mtd->_read(), returning a non-negative integer
1785 * representing max bitflips. In other cases, mtd->_read_oob() may
1786 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1787 */
1788 if (unlikely(ret_code < 0))
1789 return ret_code;
1790 if (mtd->ecc_strength == 0)
1791 return 0; /* device lacks ecc */
1792 if (ops->stats)
1793 ops->stats->max_bitflips = ret_code;
1794 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1795 }
1796 EXPORT_SYMBOL_GPL(mtd_read_oob);
1797
mtd_write_oob(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)1798 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1799 struct mtd_oob_ops *ops)
1800 {
1801 struct mtd_info *master = mtd_get_master(mtd);
1802 int ret;
1803
1804 ops->retlen = ops->oobretlen = 0;
1805
1806 if (!(mtd->flags & MTD_WRITEABLE))
1807 return -EROFS;
1808
1809 ret = mtd_check_oob_ops(mtd, to, ops);
1810 if (ret)
1811 return ret;
1812
1813 ledtrig_mtd_activity();
1814
1815 /* Check the validity of a potential fallback on mtd->_write */
1816 if (!master->_write_oob && (!master->_write || ops->oobbuf))
1817 return -EOPNOTSUPP;
1818
1819 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
1820 return mtd_io_emulated_slc(mtd, to, false, ops);
1821
1822 return mtd_write_oob_std(mtd, to, ops);
1823 }
1824 EXPORT_SYMBOL_GPL(mtd_write_oob);
1825
1826 /**
1827 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1828 * @mtd: MTD device structure
1829 * @section: ECC section. Depending on the layout you may have all the ECC
1830 * bytes stored in a single contiguous section, or one section
1831 * per ECC chunk (and sometime several sections for a single ECC
1832 * ECC chunk)
1833 * @oobecc: OOB region struct filled with the appropriate ECC position
1834 * information
1835 *
1836 * This function returns ECC section information in the OOB area. If you want
1837 * to get all the ECC bytes information, then you should call
1838 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1839 *
1840 * Returns zero on success, a negative error code otherwise.
1841 */
mtd_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobecc)1842 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1843 struct mtd_oob_region *oobecc)
1844 {
1845 struct mtd_info *master = mtd_get_master(mtd);
1846
1847 memset(oobecc, 0, sizeof(*oobecc));
1848
1849 if (!master || section < 0)
1850 return -EINVAL;
1851
1852 if (!master->ooblayout || !master->ooblayout->ecc)
1853 return -ENOTSUPP;
1854
1855 return master->ooblayout->ecc(master, section, oobecc);
1856 }
1857 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1858
1859 /**
1860 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1861 * section
1862 * @mtd: MTD device structure
1863 * @section: Free section you are interested in. Depending on the layout
1864 * you may have all the free bytes stored in a single contiguous
1865 * section, or one section per ECC chunk plus an extra section
1866 * for the remaining bytes (or other funky layout).
1867 * @oobfree: OOB region struct filled with the appropriate free position
1868 * information
1869 *
1870 * This function returns free bytes position in the OOB area. If you want
1871 * to get all the free bytes information, then you should call
1872 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1873 *
1874 * Returns zero on success, a negative error code otherwise.
1875 */
mtd_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobfree)1876 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1877 struct mtd_oob_region *oobfree)
1878 {
1879 struct mtd_info *master = mtd_get_master(mtd);
1880
1881 memset(oobfree, 0, sizeof(*oobfree));
1882
1883 if (!master || section < 0)
1884 return -EINVAL;
1885
1886 if (!master->ooblayout || !master->ooblayout->free)
1887 return -ENOTSUPP;
1888
1889 return master->ooblayout->free(master, section, oobfree);
1890 }
1891 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1892
1893 /**
1894 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1895 * @mtd: mtd info structure
1896 * @byte: the byte we are searching for
1897 * @sectionp: pointer where the section id will be stored
1898 * @oobregion: used to retrieve the ECC position
1899 * @iter: iterator function. Should be either mtd_ooblayout_free or
1900 * mtd_ooblayout_ecc depending on the region type you're searching for
1901 *
1902 * This function returns the section id and oobregion information of a
1903 * specific byte. For example, say you want to know where the 4th ECC byte is
1904 * stored, you'll use:
1905 *
1906 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1907 *
1908 * Returns zero on success, a negative error code otherwise.
1909 */
mtd_ooblayout_find_region(struct mtd_info * mtd,int byte,int * sectionp,struct mtd_oob_region * oobregion,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))1910 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1911 int *sectionp, struct mtd_oob_region *oobregion,
1912 int (*iter)(struct mtd_info *,
1913 int section,
1914 struct mtd_oob_region *oobregion))
1915 {
1916 int pos = 0, ret, section = 0;
1917
1918 memset(oobregion, 0, sizeof(*oobregion));
1919
1920 while (1) {
1921 ret = iter(mtd, section, oobregion);
1922 if (ret)
1923 return ret;
1924
1925 if (pos + oobregion->length > byte)
1926 break;
1927
1928 pos += oobregion->length;
1929 section++;
1930 }
1931
1932 /*
1933 * Adjust region info to make it start at the beginning at the
1934 * 'start' ECC byte.
1935 */
1936 oobregion->offset += byte - pos;
1937 oobregion->length -= byte - pos;
1938 *sectionp = section;
1939
1940 return 0;
1941 }
1942
1943 /**
1944 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1945 * ECC byte
1946 * @mtd: mtd info structure
1947 * @eccbyte: the byte we are searching for
1948 * @section: pointer where the section id will be stored
1949 * @oobregion: OOB region information
1950 *
1951 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1952 * byte.
1953 *
1954 * Returns zero on success, a negative error code otherwise.
1955 */
mtd_ooblayout_find_eccregion(struct mtd_info * mtd,int eccbyte,int * section,struct mtd_oob_region * oobregion)1956 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1957 int *section,
1958 struct mtd_oob_region *oobregion)
1959 {
1960 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1961 mtd_ooblayout_ecc);
1962 }
1963 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1964
1965 /**
1966 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1967 * @mtd: mtd info structure
1968 * @buf: destination buffer to store OOB bytes
1969 * @oobbuf: OOB buffer
1970 * @start: first byte to retrieve
1971 * @nbytes: number of bytes to retrieve
1972 * @iter: section iterator
1973 *
1974 * Extract bytes attached to a specific category (ECC or free)
1975 * from the OOB buffer and copy them into buf.
1976 *
1977 * Returns zero on success, a negative error code otherwise.
1978 */
mtd_ooblayout_get_bytes(struct mtd_info * mtd,u8 * buf,const u8 * oobbuf,int start,int nbytes,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))1979 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1980 const u8 *oobbuf, int start, int nbytes,
1981 int (*iter)(struct mtd_info *,
1982 int section,
1983 struct mtd_oob_region *oobregion))
1984 {
1985 struct mtd_oob_region oobregion;
1986 int section, ret;
1987
1988 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1989 &oobregion, iter);
1990
1991 while (!ret) {
1992 int cnt;
1993
1994 cnt = min_t(int, nbytes, oobregion.length);
1995 memcpy(buf, oobbuf + oobregion.offset, cnt);
1996 buf += cnt;
1997 nbytes -= cnt;
1998
1999 if (!nbytes)
2000 break;
2001
2002 ret = iter(mtd, ++section, &oobregion);
2003 }
2004
2005 return ret;
2006 }
2007
2008 /**
2009 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
2010 * @mtd: mtd info structure
2011 * @buf: source buffer to get OOB bytes from
2012 * @oobbuf: OOB buffer
2013 * @start: first OOB byte to set
2014 * @nbytes: number of OOB bytes to set
2015 * @iter: section iterator
2016 *
2017 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
2018 * is selected by passing the appropriate iterator.
2019 *
2020 * Returns zero on success, a negative error code otherwise.
2021 */
mtd_ooblayout_set_bytes(struct mtd_info * mtd,const u8 * buf,u8 * oobbuf,int start,int nbytes,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))2022 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
2023 u8 *oobbuf, int start, int nbytes,
2024 int (*iter)(struct mtd_info *,
2025 int section,
2026 struct mtd_oob_region *oobregion))
2027 {
2028 struct mtd_oob_region oobregion;
2029 int section, ret;
2030
2031 ret = mtd_ooblayout_find_region(mtd, start, §ion,
2032 &oobregion, iter);
2033
2034 while (!ret) {
2035 int cnt;
2036
2037 cnt = min_t(int, nbytes, oobregion.length);
2038 memcpy(oobbuf + oobregion.offset, buf, cnt);
2039 buf += cnt;
2040 nbytes -= cnt;
2041
2042 if (!nbytes)
2043 break;
2044
2045 ret = iter(mtd, ++section, &oobregion);
2046 }
2047
2048 return ret;
2049 }
2050
2051 /**
2052 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
2053 * @mtd: mtd info structure
2054 * @iter: category iterator
2055 *
2056 * Count the number of bytes in a given category.
2057 *
2058 * Returns a positive value on success, a negative error code otherwise.
2059 */
mtd_ooblayout_count_bytes(struct mtd_info * mtd,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))2060 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
2061 int (*iter)(struct mtd_info *,
2062 int section,
2063 struct mtd_oob_region *oobregion))
2064 {
2065 struct mtd_oob_region oobregion;
2066 int section = 0, ret, nbytes = 0;
2067
2068 while (1) {
2069 ret = iter(mtd, section++, &oobregion);
2070 if (ret) {
2071 if (ret == -ERANGE)
2072 ret = nbytes;
2073 break;
2074 }
2075
2076 nbytes += oobregion.length;
2077 }
2078
2079 return ret;
2080 }
2081
2082 /**
2083 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
2084 * @mtd: mtd info structure
2085 * @eccbuf: destination buffer to store ECC bytes
2086 * @oobbuf: OOB buffer
2087 * @start: first ECC byte to retrieve
2088 * @nbytes: number of ECC bytes to retrieve
2089 *
2090 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
2091 *
2092 * Returns zero on success, a negative error code otherwise.
2093 */
mtd_ooblayout_get_eccbytes(struct mtd_info * mtd,u8 * eccbuf,const u8 * oobbuf,int start,int nbytes)2094 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
2095 const u8 *oobbuf, int start, int nbytes)
2096 {
2097 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
2098 mtd_ooblayout_ecc);
2099 }
2100 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
2101
2102 /**
2103 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
2104 * @mtd: mtd info structure
2105 * @eccbuf: source buffer to get ECC bytes from
2106 * @oobbuf: OOB buffer
2107 * @start: first ECC byte to set
2108 * @nbytes: number of ECC bytes to set
2109 *
2110 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
2111 *
2112 * Returns zero on success, a negative error code otherwise.
2113 */
mtd_ooblayout_set_eccbytes(struct mtd_info * mtd,const u8 * eccbuf,u8 * oobbuf,int start,int nbytes)2114 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
2115 u8 *oobbuf, int start, int nbytes)
2116 {
2117 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
2118 mtd_ooblayout_ecc);
2119 }
2120 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
2121
2122 /**
2123 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
2124 * @mtd: mtd info structure
2125 * @databuf: destination buffer to store ECC bytes
2126 * @oobbuf: OOB buffer
2127 * @start: first ECC byte to retrieve
2128 * @nbytes: number of ECC bytes to retrieve
2129 *
2130 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
2131 *
2132 * Returns zero on success, a negative error code otherwise.
2133 */
mtd_ooblayout_get_databytes(struct mtd_info * mtd,u8 * databuf,const u8 * oobbuf,int start,int nbytes)2134 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
2135 const u8 *oobbuf, int start, int nbytes)
2136 {
2137 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
2138 mtd_ooblayout_free);
2139 }
2140 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
2141
2142 /**
2143 * mtd_ooblayout_set_databytes - set data bytes into the oob buffer
2144 * @mtd: mtd info structure
2145 * @databuf: source buffer to get data bytes from
2146 * @oobbuf: OOB buffer
2147 * @start: first ECC byte to set
2148 * @nbytes: number of ECC bytes to set
2149 *
2150 * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes.
2151 *
2152 * Returns zero on success, a negative error code otherwise.
2153 */
mtd_ooblayout_set_databytes(struct mtd_info * mtd,const u8 * databuf,u8 * oobbuf,int start,int nbytes)2154 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
2155 u8 *oobbuf, int start, int nbytes)
2156 {
2157 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
2158 mtd_ooblayout_free);
2159 }
2160 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
2161
2162 /**
2163 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
2164 * @mtd: mtd info structure
2165 *
2166 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
2167 *
2168 * Returns zero on success, a negative error code otherwise.
2169 */
mtd_ooblayout_count_freebytes(struct mtd_info * mtd)2170 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
2171 {
2172 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
2173 }
2174 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
2175
2176 /**
2177 * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB
2178 * @mtd: mtd info structure
2179 *
2180 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
2181 *
2182 * Returns zero on success, a negative error code otherwise.
2183 */
mtd_ooblayout_count_eccbytes(struct mtd_info * mtd)2184 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
2185 {
2186 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
2187 }
2188 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
2189
2190 /*
2191 * Method to access the protection register area, present in some flash
2192 * devices. The user data is one time programmable but the factory data is read
2193 * only.
2194 */
mtd_get_fact_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)2195 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
2196 struct otp_info *buf)
2197 {
2198 struct mtd_info *master = mtd_get_master(mtd);
2199
2200 if (!master->_get_fact_prot_info)
2201 return -EOPNOTSUPP;
2202 if (!len)
2203 return 0;
2204 return master->_get_fact_prot_info(master, len, retlen, buf);
2205 }
2206 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
2207
mtd_read_fact_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)2208 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
2209 size_t *retlen, u_char *buf)
2210 {
2211 struct mtd_info *master = mtd_get_master(mtd);
2212
2213 *retlen = 0;
2214 if (!master->_read_fact_prot_reg)
2215 return -EOPNOTSUPP;
2216 if (!len)
2217 return 0;
2218 return master->_read_fact_prot_reg(master, from, len, retlen, buf);
2219 }
2220 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
2221
mtd_get_user_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)2222 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
2223 struct otp_info *buf)
2224 {
2225 struct mtd_info *master = mtd_get_master(mtd);
2226
2227 if (!master->_get_user_prot_info)
2228 return -EOPNOTSUPP;
2229 if (!len)
2230 return 0;
2231 return master->_get_user_prot_info(master, len, retlen, buf);
2232 }
2233 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
2234
mtd_read_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)2235 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
2236 size_t *retlen, u_char *buf)
2237 {
2238 struct mtd_info *master = mtd_get_master(mtd);
2239
2240 *retlen = 0;
2241 if (!master->_read_user_prot_reg)
2242 return -EOPNOTSUPP;
2243 if (!len)
2244 return 0;
2245 return master->_read_user_prot_reg(master, from, len, retlen, buf);
2246 }
2247 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
2248
mtd_write_user_prot_reg(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)2249 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
2250 size_t *retlen, const u_char *buf)
2251 {
2252 struct mtd_info *master = mtd_get_master(mtd);
2253 int ret;
2254
2255 *retlen = 0;
2256 if (!master->_write_user_prot_reg)
2257 return -EOPNOTSUPP;
2258 if (!len)
2259 return 0;
2260 ret = master->_write_user_prot_reg(master, to, len, retlen, buf);
2261 if (ret)
2262 return ret;
2263
2264 /*
2265 * If no data could be written at all, we are out of memory and
2266 * must return -ENOSPC.
2267 */
2268 return (*retlen) ? 0 : -ENOSPC;
2269 }
2270 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
2271
mtd_lock_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len)2272 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
2273 {
2274 struct mtd_info *master = mtd_get_master(mtd);
2275
2276 if (!master->_lock_user_prot_reg)
2277 return -EOPNOTSUPP;
2278 if (!len)
2279 return 0;
2280 return master->_lock_user_prot_reg(master, from, len);
2281 }
2282 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
2283
mtd_erase_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len)2284 int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
2285 {
2286 struct mtd_info *master = mtd_get_master(mtd);
2287
2288 if (!master->_erase_user_prot_reg)
2289 return -EOPNOTSUPP;
2290 if (!len)
2291 return 0;
2292 return master->_erase_user_prot_reg(master, from, len);
2293 }
2294 EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
2295
2296 /* Chip-supported device locking */
mtd_lock(struct mtd_info * mtd,loff_t ofs,uint64_t len)2297 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2298 {
2299 struct mtd_info *master = mtd_get_master(mtd);
2300
2301 if (!master->_lock)
2302 return -EOPNOTSUPP;
2303 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
2304 return -EINVAL;
2305 if (!len)
2306 return 0;
2307
2308 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2309 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2310 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2311 }
2312
2313 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len);
2314 }
2315 EXPORT_SYMBOL_GPL(mtd_lock);
2316
mtd_unlock(struct mtd_info * mtd,loff_t ofs,uint64_t len)2317 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2318 {
2319 struct mtd_info *master = mtd_get_master(mtd);
2320
2321 if (!master->_unlock)
2322 return -EOPNOTSUPP;
2323 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
2324 return -EINVAL;
2325 if (!len)
2326 return 0;
2327
2328 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2329 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2330 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2331 }
2332
2333 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len);
2334 }
2335 EXPORT_SYMBOL_GPL(mtd_unlock);
2336
mtd_is_locked(struct mtd_info * mtd,loff_t ofs,uint64_t len)2337 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2338 {
2339 struct mtd_info *master = mtd_get_master(mtd);
2340
2341 if (!master->_is_locked)
2342 return -EOPNOTSUPP;
2343 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
2344 return -EINVAL;
2345 if (!len)
2346 return 0;
2347
2348 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2349 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2350 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2351 }
2352
2353 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len);
2354 }
2355 EXPORT_SYMBOL_GPL(mtd_is_locked);
2356
mtd_block_isreserved(struct mtd_info * mtd,loff_t ofs)2357 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
2358 {
2359 struct mtd_info *master = mtd_get_master(mtd);
2360
2361 if (ofs < 0 || ofs >= mtd->size)
2362 return -EINVAL;
2363 if (!master->_block_isreserved)
2364 return 0;
2365
2366 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2367 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2368
2369 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs));
2370 }
2371 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
2372
mtd_block_isbad(struct mtd_info * mtd,loff_t ofs)2373 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
2374 {
2375 struct mtd_info *master = mtd_get_master(mtd);
2376
2377 if (ofs < 0 || ofs >= mtd->size)
2378 return -EINVAL;
2379 if (!master->_block_isbad)
2380 return 0;
2381
2382 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2383 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2384
2385 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs));
2386 }
2387 EXPORT_SYMBOL_GPL(mtd_block_isbad);
2388
mtd_block_markbad(struct mtd_info * mtd,loff_t ofs)2389 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
2390 {
2391 struct mtd_info *master = mtd_get_master(mtd);
2392 loff_t moffs;
2393 int ret;
2394
2395 if (!master->_block_markbad)
2396 return -EOPNOTSUPP;
2397 if (ofs < 0 || ofs >= mtd->size)
2398 return -EINVAL;
2399 if (!(mtd->flags & MTD_WRITEABLE))
2400 return -EROFS;
2401
2402 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2403 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2404
2405 moffs = mtd_get_master_ofs(mtd, ofs);
2406
2407 if (master->_block_isbad) {
2408 ret = master->_block_isbad(master, moffs);
2409 if (ret > 0)
2410 return 0;
2411 }
2412
2413 ret = master->_block_markbad(master, moffs);
2414 if (ret)
2415 return ret;
2416
2417 while (mtd->parent) {
2418 mtd->ecc_stats.badblocks++;
2419 mtd = mtd->parent;
2420 }
2421
2422 return 0;
2423 }
2424 EXPORT_SYMBOL_GPL(mtd_block_markbad);
2425 ALLOW_ERROR_INJECTION(mtd_block_markbad, ERRNO);
2426
2427 /*
2428 * default_mtd_writev - the default writev method
2429 * @mtd: mtd device description object pointer
2430 * @vecs: the vectors to write
2431 * @count: count of vectors in @vecs
2432 * @to: the MTD device offset to write to
2433 * @retlen: on exit contains the count of bytes written to the MTD device.
2434 *
2435 * This function returns zero in case of success and a negative error code in
2436 * case of failure.
2437 */
default_mtd_writev(struct mtd_info * mtd,const struct kvec * vecs,unsigned long count,loff_t to,size_t * retlen)2438 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
2439 unsigned long count, loff_t to, size_t *retlen)
2440 {
2441 unsigned long i;
2442 size_t totlen = 0, thislen;
2443 int ret = 0;
2444
2445 for (i = 0; i < count; i++) {
2446 if (!vecs[i].iov_len)
2447 continue;
2448 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
2449 vecs[i].iov_base);
2450 totlen += thislen;
2451 if (ret || thislen != vecs[i].iov_len)
2452 break;
2453 to += vecs[i].iov_len;
2454 }
2455 *retlen = totlen;
2456 return ret;
2457 }
2458
2459 /*
2460 * mtd_writev - the vector-based MTD write method
2461 * @mtd: mtd device description object pointer
2462 * @vecs: the vectors to write
2463 * @count: count of vectors in @vecs
2464 * @to: the MTD device offset to write to
2465 * @retlen: on exit contains the count of bytes written to the MTD device.
2466 *
2467 * This function returns zero in case of success and a negative error code in
2468 * case of failure.
2469 */
mtd_writev(struct mtd_info * mtd,const struct kvec * vecs,unsigned long count,loff_t to,size_t * retlen)2470 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
2471 unsigned long count, loff_t to, size_t *retlen)
2472 {
2473 struct mtd_info *master = mtd_get_master(mtd);
2474
2475 *retlen = 0;
2476 if (!(mtd->flags & MTD_WRITEABLE))
2477 return -EROFS;
2478
2479 if (!master->_writev)
2480 return default_mtd_writev(mtd, vecs, count, to, retlen);
2481
2482 return master->_writev(master, vecs, count,
2483 mtd_get_master_ofs(mtd, to), retlen);
2484 }
2485 EXPORT_SYMBOL_GPL(mtd_writev);
2486
2487 /**
2488 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
2489 * @mtd: mtd device description object pointer
2490 * @size: a pointer to the ideal or maximum size of the allocation, points
2491 * to the actual allocation size on success.
2492 *
2493 * This routine attempts to allocate a contiguous kernel buffer up to
2494 * the specified size, backing off the size of the request exponentially
2495 * until the request succeeds or until the allocation size falls below
2496 * the system page size. This attempts to make sure it does not adversely
2497 * impact system performance, so when allocating more than one page, we
2498 * ask the memory allocator to avoid re-trying, swapping, writing back
2499 * or performing I/O.
2500 *
2501 * Note, this function also makes sure that the allocated buffer is aligned to
2502 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
2503 *
2504 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
2505 * to handle smaller (i.e. degraded) buffer allocations under low- or
2506 * fragmented-memory situations where such reduced allocations, from a
2507 * requested ideal, are allowed.
2508 *
2509 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
2510 */
mtd_kmalloc_up_to(const struct mtd_info * mtd,size_t * size)2511 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
2512 {
2513 gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY;
2514 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
2515 void *kbuf;
2516
2517 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
2518
2519 while (*size > min_alloc) {
2520 kbuf = kmalloc(*size, flags);
2521 if (kbuf)
2522 return kbuf;
2523
2524 *size >>= 1;
2525 *size = ALIGN(*size, mtd->writesize);
2526 }
2527
2528 /*
2529 * For the last resort allocation allow 'kmalloc()' to do all sorts of
2530 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
2531 */
2532 return kmalloc(*size, GFP_KERNEL);
2533 }
2534 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
2535
2536 #ifdef CONFIG_PROC_FS
2537
2538 /*====================================================================*/
2539 /* Support for /proc/mtd */
2540
mtd_proc_show(struct seq_file * m,void * v)2541 static int mtd_proc_show(struct seq_file *m, void *v)
2542 {
2543 struct mtd_info *mtd;
2544
2545 seq_puts(m, "dev: size erasesize name\n");
2546 mutex_lock(&mtd_table_mutex);
2547 mtd_for_each_device(mtd) {
2548 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
2549 mtd->index, (unsigned long long)mtd->size,
2550 mtd->erasesize, mtd->name);
2551 }
2552 mutex_unlock(&mtd_table_mutex);
2553 return 0;
2554 }
2555 #endif /* CONFIG_PROC_FS */
2556
2557 /*====================================================================*/
2558 /* Init code */
2559
mtd_bdi_init(const char * name)2560 static struct backing_dev_info * __init mtd_bdi_init(const char *name)
2561 {
2562 struct backing_dev_info *bdi;
2563 int ret;
2564
2565 bdi = bdi_alloc(NUMA_NO_NODE);
2566 if (!bdi)
2567 return ERR_PTR(-ENOMEM);
2568 bdi->ra_pages = 0;
2569 bdi->io_pages = 0;
2570
2571 /*
2572 * We put '-0' suffix to the name to get the same name format as we
2573 * used to get. Since this is called only once, we get a unique name.
2574 */
2575 ret = bdi_register(bdi, "%.28s-0", name);
2576 if (ret)
2577 bdi_put(bdi);
2578
2579 return ret ? ERR_PTR(ret) : bdi;
2580 }
2581
2582 static struct proc_dir_entry *proc_mtd;
2583
init_mtd(void)2584 static int __init init_mtd(void)
2585 {
2586 int ret;
2587
2588 ret = class_register(&mtd_class);
2589 if (ret)
2590 goto err_reg;
2591
2592 mtd_bdi = mtd_bdi_init("mtd");
2593 if (IS_ERR(mtd_bdi)) {
2594 ret = PTR_ERR(mtd_bdi);
2595 goto err_bdi;
2596 }
2597
2598 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show);
2599
2600 ret = init_mtdchar();
2601 if (ret)
2602 goto out_procfs;
2603
2604 dfs_dir_mtd = debugfs_create_dir("mtd", NULL);
2605 debugfs_create_bool("expert_analysis_mode", 0600, dfs_dir_mtd,
2606 &mtd_expert_analysis_mode);
2607
2608 return 0;
2609
2610 out_procfs:
2611 if (proc_mtd)
2612 remove_proc_entry("mtd", NULL);
2613 bdi_unregister(mtd_bdi);
2614 bdi_put(mtd_bdi);
2615 err_bdi:
2616 class_unregister(&mtd_class);
2617 err_reg:
2618 pr_err("Error registering mtd class or bdi: %d\n", ret);
2619 return ret;
2620 }
2621
cleanup_mtd(void)2622 static void __exit cleanup_mtd(void)
2623 {
2624 debugfs_remove_recursive(dfs_dir_mtd);
2625 cleanup_mtdchar();
2626 if (proc_mtd)
2627 remove_proc_entry("mtd", NULL);
2628 class_unregister(&mtd_class);
2629 bdi_unregister(mtd_bdi);
2630 bdi_put(mtd_bdi);
2631 idr_destroy(&mtd_idr);
2632 }
2633
2634 module_init(init_mtd);
2635 module_exit(cleanup_mtd);
2636
2637 MODULE_LICENSE("GPL");
2638 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2639 MODULE_DESCRIPTION("Core MTD registration and access routines");
2640