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