blob: 5bd64bd6ad43e58c2653181506c561beff2505d6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkf6d5e252008-11-19 16:20:36 +01002/*
3 * Core registration and callback routines for MTD
4 * drivers and users.
5 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02006 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
Wolfgang Denk9d328a62021-09-27 17:42:38 +02007 * Copyright © 2006 Red Hat UK Limited
Heiko Schocherf5895d12014-06-24 10:10:04 +02008 *
Kyungmin Parkf6d5e252008-11-19 16:20:36 +01009 */
10
Heiko Schocherf5895d12014-06-24 10:10:04 +020011#ifndef __UBOOT__
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/ptrace.h>
15#include <linux/seq_file.h>
16#include <linux/string.h>
17#include <linux/timer.h>
18#include <linux/major.h>
19#include <linux/fs.h>
20#include <linux/err.h>
21#include <linux/ioctl.h>
22#include <linux/init.h>
23#include <linux/proc_fs.h>
24#include <linux/idr.h>
25#include <linux/backing-dev.h>
26#include <linux/gfp.h>
27#include <linux/slab.h>
28#else
Simon Glass4dcacfc2020-05-10 11:40:13 -060029#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060030#include <linux/bug.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020031#include <linux/err.h>
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010032#include <ubi_uboot.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020033#endif
34
Fabio Estevam0297d1e2015-11-05 12:43:39 -020035#include <linux/log2.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020036#include <linux/mtd/mtd.h>
37#include <linux/mtd/partitions.h>
38
39#include "mtdcore.h"
40
41#ifndef __UBOOT__
42/*
43 * backing device capabilities for non-mappable devices (such as NAND flash)
44 * - permits private mappings, copies are taken of the data
45 */
46static struct backing_dev_info mtd_bdi_unmappable = {
47 .capabilities = BDI_CAP_MAP_COPY,
48};
49
50/*
51 * backing device capabilities for R/O mappable devices (such as ROM)
52 * - permits private mappings, copies are taken of the data
53 * - permits non-writable shared mappings
54 */
55static struct backing_dev_info mtd_bdi_ro_mappable = {
56 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
57 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
58};
59
60/*
61 * backing device capabilities for writable mappable devices (such as RAM)
62 * - permits private mappings, copies are taken of the data
63 * - permits non-writable shared mappings
64 */
65static struct backing_dev_info mtd_bdi_rw_mappable = {
66 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
67 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
68 BDI_CAP_WRITE_MAP),
69};
70
71static int mtd_cls_suspend(struct device *dev, pm_message_t state);
72static int mtd_cls_resume(struct device *dev);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010073
Heiko Schocherf5895d12014-06-24 10:10:04 +020074static struct class mtd_class = {
75 .name = "mtd",
76 .owner = THIS_MODULE,
77 .suspend = mtd_cls_suspend,
78 .resume = mtd_cls_resume,
79};
80#else
Heiko Schocherf5895d12014-06-24 10:10:04 +020081#define MAX_IDR_ID 64
82
83struct idr_layer {
84 int used;
85 void *ptr;
86};
87
88struct idr {
89 struct idr_layer id[MAX_IDR_ID];
Boris Brezillon059e4a62018-12-02 10:54:22 +010090 bool updated;
Heiko Schocherf5895d12014-06-24 10:10:04 +020091};
92
93#define DEFINE_IDR(name) struct idr name;
94
95void idr_remove(struct idr *idp, int id)
96{
Boris Brezillon059e4a62018-12-02 10:54:22 +010097 if (idp->id[id].used) {
Heiko Schocherf5895d12014-06-24 10:10:04 +020098 idp->id[id].used = 0;
Boris Brezillon059e4a62018-12-02 10:54:22 +010099 idp->updated = true;
100 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200101
102 return;
103}
104void *idr_find(struct idr *idp, int id)
105{
106 if (idp->id[id].used)
107 return idp->id[id].ptr;
108
109 return NULL;
110}
111
112void *idr_get_next(struct idr *idp, int *next)
113{
114 void *ret;
115 int id = *next;
116
117 ret = idr_find(idp, id);
118 if (ret) {
119 id ++;
120 if (!idp->id[id].used)
121 id = 0;
122 *next = id;
123 } else {
124 *next = 0;
125 }
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200126
Heiko Schocherf5895d12014-06-24 10:10:04 +0200127 return ret;
128}
129
130int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
131{
132 struct idr_layer *idl;
133 int i = 0;
134
135 while (i < MAX_IDR_ID) {
136 idl = &idp->id[i];
137 if (idl->used == 0) {
138 idl->used = 1;
139 idl->ptr = ptr;
Boris Brezillon059e4a62018-12-02 10:54:22 +0100140 idp->updated = true;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200141 return i;
142 }
143 i++;
144 }
145 return -ENOSPC;
146}
147#endif
148
149static DEFINE_IDR(mtd_idr);
150
151/* These are exported solely for the purpose of mtd_blkdevs.c. You
152 should not use them for _anything_ else */
153DEFINE_MUTEX(mtd_table_mutex);
154EXPORT_SYMBOL_GPL(mtd_table_mutex);
155
156struct mtd_info *__mtd_next_device(int i)
157{
158 return idr_get_next(&mtd_idr, &i);
159}
160EXPORT_SYMBOL_GPL(__mtd_next_device);
161
Boris Brezillon059e4a62018-12-02 10:54:22 +0100162bool mtd_dev_list_updated(void)
163{
164 if (mtd_idr.updated) {
165 mtd_idr.updated = false;
166 return true;
167 }
168
169 return false;
170}
171
Heiko Schocherf5895d12014-06-24 10:10:04 +0200172#ifndef __UBOOT__
173static LIST_HEAD(mtd_notifiers);
174
Heiko Schocherf5895d12014-06-24 10:10:04 +0200175#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
176
177/* REVISIT once MTD uses the driver model better, whoever allocates
178 * the mtd_info will probably want to use the release() hook...
179 */
180static void mtd_release(struct device *dev)
181{
182 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
183 dev_t index = MTD_DEVT(mtd->index);
184
185 /* remove /dev/mtdXro node if needed */
186 if (index)
187 device_destroy(&mtd_class, index + 1);
188}
189
190static int mtd_cls_suspend(struct device *dev, pm_message_t state)
191{
192 struct mtd_info *mtd = dev_get_drvdata(dev);
193
194 return mtd ? mtd_suspend(mtd) : 0;
195}
196
197static int mtd_cls_resume(struct device *dev)
198{
199 struct mtd_info *mtd = dev_get_drvdata(dev);
200
201 if (mtd)
202 mtd_resume(mtd);
203 return 0;
204}
205
206static ssize_t mtd_type_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
209 struct mtd_info *mtd = dev_get_drvdata(dev);
210 char *type;
211
212 switch (mtd->type) {
213 case MTD_ABSENT:
214 type = "absent";
215 break;
216 case MTD_RAM:
217 type = "ram";
218 break;
219 case MTD_ROM:
220 type = "rom";
221 break;
222 case MTD_NORFLASH:
223 type = "nor";
224 break;
225 case MTD_NANDFLASH:
226 type = "nand";
227 break;
228 case MTD_DATAFLASH:
229 type = "dataflash";
230 break;
231 case MTD_UBIVOLUME:
232 type = "ubi";
233 break;
234 case MTD_MLCNANDFLASH:
235 type = "mlc-nand";
236 break;
237 default:
238 type = "unknown";
239 }
240
241 return snprintf(buf, PAGE_SIZE, "%s\n", type);
242}
243static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
244
245static ssize_t mtd_flags_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
247{
248 struct mtd_info *mtd = dev_get_drvdata(dev);
249
250 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
251
252}
253static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
254
255static ssize_t mtd_size_show(struct device *dev,
256 struct device_attribute *attr, char *buf)
257{
258 struct mtd_info *mtd = dev_get_drvdata(dev);
259
260 return snprintf(buf, PAGE_SIZE, "%llu\n",
261 (unsigned long long)mtd->size);
262
263}
264static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
265
266static ssize_t mtd_erasesize_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269 struct mtd_info *mtd = dev_get_drvdata(dev);
270
271 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
272
273}
274static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
275
276static ssize_t mtd_writesize_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 struct mtd_info *mtd = dev_get_drvdata(dev);
280
281 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
282
283}
284static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
285
286static ssize_t mtd_subpagesize_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
289 struct mtd_info *mtd = dev_get_drvdata(dev);
290 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
291
292 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
293
294}
295static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
296
297static ssize_t mtd_oobsize_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
299{
300 struct mtd_info *mtd = dev_get_drvdata(dev);
301
302 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
303
304}
305static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
306
307static ssize_t mtd_numeraseregions_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
309{
310 struct mtd_info *mtd = dev_get_drvdata(dev);
311
312 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
313
314}
315static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
316 NULL);
317
318static ssize_t mtd_name_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
320{
321 struct mtd_info *mtd = dev_get_drvdata(dev);
322
323 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
324
325}
326static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
327
328static ssize_t mtd_ecc_strength_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
330{
331 struct mtd_info *mtd = dev_get_drvdata(dev);
332
333 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
334}
335static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
336
337static ssize_t mtd_bitflip_threshold_show(struct device *dev,
338 struct device_attribute *attr,
339 char *buf)
340{
341 struct mtd_info *mtd = dev_get_drvdata(dev);
342
343 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
344}
345
346static ssize_t mtd_bitflip_threshold_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
349{
350 struct mtd_info *mtd = dev_get_drvdata(dev);
351 unsigned int bitflip_threshold;
352 int retval;
353
354 retval = kstrtouint(buf, 0, &bitflip_threshold);
355 if (retval)
356 return retval;
357
358 mtd->bitflip_threshold = bitflip_threshold;
359 return count;
360}
361static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
362 mtd_bitflip_threshold_show,
363 mtd_bitflip_threshold_store);
364
365static ssize_t mtd_ecc_step_size_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
368 struct mtd_info *mtd = dev_get_drvdata(dev);
369
370 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
371
372}
373static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
374
375static struct attribute *mtd_attrs[] = {
376 &dev_attr_type.attr,
377 &dev_attr_flags.attr,
378 &dev_attr_size.attr,
379 &dev_attr_erasesize.attr,
380 &dev_attr_writesize.attr,
381 &dev_attr_subpagesize.attr,
382 &dev_attr_oobsize.attr,
383 &dev_attr_numeraseregions.attr,
384 &dev_attr_name.attr,
385 &dev_attr_ecc_strength.attr,
386 &dev_attr_ecc_step_size.attr,
387 &dev_attr_bitflip_threshold.attr,
388 NULL,
389};
390ATTRIBUTE_GROUPS(mtd);
391
392static struct device_type mtd_devtype = {
393 .name = "mtd",
394 .groups = mtd_groups,
395 .release = mtd_release,
396};
397#endif
398
399/**
400 * add_mtd_device - register an MTD device
401 * @mtd: pointer to new MTD device info structure
402 *
403 * Add a device to the list of MTD devices present in the system, and
404 * notify each currently active MTD 'user' of its arrival. Returns
405 * zero on success or 1 on failure, which currently will only happen
406 * if there is insufficient memory or a sysfs error.
407 */
408
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100409int add_mtd_device(struct mtd_info *mtd)
410{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200411#ifndef __UBOOT__
412 struct mtd_notifier *not;
413#endif
414 int i, error;
415
416#ifndef __UBOOT__
417 if (!mtd->backing_dev_info) {
418 switch (mtd->type) {
419 case MTD_RAM:
420 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
421 break;
422 case MTD_ROM:
423 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
424 break;
425 default:
426 mtd->backing_dev_info = &mtd_bdi_unmappable;
427 break;
428 }
429 }
430#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100431
432 BUG_ON(mtd->writesize == 0);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200433 mutex_lock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100434
Heiko Schocherf5895d12014-06-24 10:10:04 +0200435 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
436 if (i < 0)
437 goto fail_locked;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100438
Heiko Schocherf5895d12014-06-24 10:10:04 +0200439 mtd->index = i;
440 mtd->usecount = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000441
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200442 INIT_LIST_HEAD(&mtd->partitions);
443
Heiko Schocherf5895d12014-06-24 10:10:04 +0200444 /* default value if not set by driver */
445 if (mtd->bitflip_threshold == 0)
446 mtd->bitflip_threshold = mtd->ecc_strength;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000447
Heiko Schocherf5895d12014-06-24 10:10:04 +0200448 if (is_power_of_2(mtd->erasesize))
449 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
450 else
451 mtd->erasesize_shift = 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100452
Heiko Schocherf5895d12014-06-24 10:10:04 +0200453 if (is_power_of_2(mtd->writesize))
454 mtd->writesize_shift = ffs(mtd->writesize) - 1;
455 else
456 mtd->writesize_shift = 0;
457
458 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
459 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
460
461 /* Some chips always power up locked. Unlock them now */
462 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
463 error = mtd_unlock(mtd, 0, mtd->size);
464 if (error && error != -EOPNOTSUPP)
465 printk(KERN_WARNING
466 "%s: unlock failed, writes may not work\n",
467 mtd->name);
468 }
469
470#ifndef __UBOOT__
471 /* Caller should have set dev.parent to match the
472 * physical device.
473 */
474 mtd->dev.type = &mtd_devtype;
475 mtd->dev.class = &mtd_class;
476 mtd->dev.devt = MTD_DEVT(i);
477 dev_set_name(&mtd->dev, "mtd%d", i);
478 dev_set_drvdata(&mtd->dev, mtd);
479 if (device_register(&mtd->dev) != 0)
480 goto fail_added;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100481
Heiko Schocherf5895d12014-06-24 10:10:04 +0200482 if (MTD_DEVT(i))
483 device_create(&mtd_class, mtd->dev.parent,
484 MTD_DEVT(i) + 1,
485 NULL, "mtd%dro", i);
486
487 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
488 /* No need to get a refcount on the module containing
489 the notifier, since we hold the mtd_table_mutex */
490 list_for_each_entry(not, &mtd_notifiers, list)
491 not->add(mtd);
Heiko Schocherb24c4272014-07-15 16:08:42 +0200492#else
493 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200494#endif
495
496 mutex_unlock(&mtd_table_mutex);
497 /* We _know_ we aren't being removed, because
498 our caller is still holding us here. So none
499 of this try_ nonsense, and no bitching about it
500 either. :) */
501 __module_get(THIS_MODULE);
502 return 0;
503
504#ifndef __UBOOT__
505fail_added:
506 idr_remove(&mtd_idr, i);
507#endif
508fail_locked:
509 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100510 return 1;
511}
512
513/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200514 * del_mtd_device - unregister an MTD device
515 * @mtd: pointer to MTD device info structure
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100516 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200517 * Remove a device from the list of MTD devices present in the system,
518 * and notify each currently active MTD 'user' of its departure.
519 * Returns zero on success or 1 on failure, which currently will happen
520 * if the requested device does not appear to be present in the list.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100521 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200522
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100523int del_mtd_device(struct mtd_info *mtd)
524{
525 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200526#ifndef __UBOOT__
527 struct mtd_notifier *not;
528#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100529
Boris Brezillondb0d8052018-12-02 10:54:24 +0100530 ret = del_mtd_partitions(mtd);
531 if (ret) {
532 debug("Failed to delete MTD partitions attached to %s (err %d)\n",
533 mtd->name, ret);
534 return ret;
535 }
536
Heiko Schocherf5895d12014-06-24 10:10:04 +0200537 mutex_lock(&mtd_table_mutex);
538
539 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100540 ret = -ENODEV;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200541 goto out_error;
542 }
543
544#ifndef __UBOOT__
545 /* No need to get a refcount on the module containing
546 the notifier, since we hold the mtd_table_mutex */
547 list_for_each_entry(not, &mtd_notifiers, list)
548 not->remove(mtd);
549#endif
550
551 if (mtd->usecount) {
552 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
553 mtd->index, mtd->name, mtd->usecount);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100554 ret = -EBUSY;
555 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200556#ifndef __UBOOT__
557 device_unregister(&mtd->dev);
558#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100559
Heiko Schocherf5895d12014-06-24 10:10:04 +0200560 idr_remove(&mtd_idr, mtd->index);
561
562 module_put(THIS_MODULE);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100563 ret = 0;
564 }
565
Heiko Schocherf5895d12014-06-24 10:10:04 +0200566out_error:
567 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100568 return ret;
569}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200570
571#ifndef __UBOOT__
572/**
573 * mtd_device_parse_register - parse partitions and register an MTD device.
574 *
575 * @mtd: the MTD device to register
576 * @types: the list of MTD partition probes to try, see
577 * 'parse_mtd_partitions()' for more information
578 * @parser_data: MTD partition parser-specific data
579 * @parts: fallback partition information to register, if parsing fails;
580 * only valid if %nr_parts > %0
581 * @nr_parts: the number of partitions in parts, if zero then the full
582 * MTD device is registered if no partition info is found
583 *
584 * This function aggregates MTD partitions parsing (done by
585 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
586 * basically follows the most common pattern found in many MTD drivers:
587 *
588 * * It first tries to probe partitions on MTD device @mtd using parsers
589 * specified in @types (if @types is %NULL, then the default list of parsers
590 * is used, see 'parse_mtd_partitions()' for more information). If none are
591 * found this functions tries to fallback to information specified in
592 * @parts/@nr_parts.
593 * * If any partitioning info was found, this function registers the found
594 * partitions.
595 * * If no partitions were found this function just registers the MTD device
596 * @mtd and exits.
597 *
598 * Returns zero in case of success and a negative error code in case of failure.
599 */
600int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
601 struct mtd_part_parser_data *parser_data,
602 const struct mtd_partition *parts,
603 int nr_parts)
604{
605 int err;
606 struct mtd_partition *real_parts;
607
608 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
609 if (err <= 0 && nr_parts && parts) {
610 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
611 GFP_KERNEL);
612 if (!real_parts)
613 err = -ENOMEM;
614 else
615 err = nr_parts;
616 }
617
618 if (err > 0) {
619 err = add_mtd_partitions(mtd, real_parts, err);
620 kfree(real_parts);
621 } else if (err == 0) {
622 err = add_mtd_device(mtd);
623 if (err == 1)
624 err = -ENODEV;
625 }
626
627 return err;
628}
629EXPORT_SYMBOL_GPL(mtd_device_parse_register);
630
631/**
632 * mtd_device_unregister - unregister an existing MTD device.
633 *
634 * @master: the MTD device to unregister. This will unregister both the master
635 * and any partitions if registered.
636 */
637int mtd_device_unregister(struct mtd_info *master)
638{
639 int err;
640
641 err = del_mtd_partitions(master);
642 if (err)
643 return err;
644
645 if (!device_is_registered(&master->dev))
646 return 0;
647
648 return del_mtd_device(master);
649}
650EXPORT_SYMBOL_GPL(mtd_device_unregister);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100651
652/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200653 * register_mtd_user - register a 'user' of MTD devices.
654 * @new: pointer to notifier info structure
655 *
656 * Registers a pair of callbacks function to be called upon addition
657 * or removal of MTD devices. Causes the 'add' callback to be immediately
658 * invoked for each MTD device currently present in the system.
659 */
660void register_mtd_user (struct mtd_notifier *new)
661{
662 struct mtd_info *mtd;
663
664 mutex_lock(&mtd_table_mutex);
665
666 list_add(&new->list, &mtd_notifiers);
667
668 __module_get(THIS_MODULE);
669
670 mtd_for_each_device(mtd)
671 new->add(mtd);
672
673 mutex_unlock(&mtd_table_mutex);
674}
675EXPORT_SYMBOL_GPL(register_mtd_user);
676
677/**
678 * unregister_mtd_user - unregister a 'user' of MTD devices.
679 * @old: pointer to notifier info structure
680 *
681 * Removes a callback function pair from the list of 'users' to be
682 * notified upon addition or removal of MTD devices. Causes the
683 * 'remove' callback to be immediately invoked for each MTD device
684 * currently present in the system.
685 */
686int unregister_mtd_user (struct mtd_notifier *old)
687{
688 struct mtd_info *mtd;
689
690 mutex_lock(&mtd_table_mutex);
691
692 module_put(THIS_MODULE);
693
694 mtd_for_each_device(mtd)
695 old->remove(mtd);
696
697 list_del(&old->list);
698 mutex_unlock(&mtd_table_mutex);
699 return 0;
700}
701EXPORT_SYMBOL_GPL(unregister_mtd_user);
702#endif
703
704/**
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100705 * get_mtd_device - obtain a validated handle for an MTD device
706 * @mtd: last known address of the required MTD device
707 * @num: internal device number of the required MTD device
708 *
709 * Given a number and NULL address, return the num'th entry in the device
Heiko Schocherf5895d12014-06-24 10:10:04 +0200710 * table, if any. Given an address and num == -1, search the device table
711 * for a device with that address and return if it's still present. Given
712 * both, return the num'th driver only if its address matches. Return
713 * error code if not.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100714 */
715struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
716{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200717 struct mtd_info *ret = NULL, *other;
718 int err = -ENODEV;
719
720 mutex_lock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100721
722 if (num == -1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200723 mtd_for_each_device(other) {
724 if (other == mtd) {
725 ret = mtd;
726 break;
727 }
728 }
729 } else if (num >= 0) {
730 ret = idr_find(&mtd_idr, num);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100731 if (mtd && mtd != ret)
732 ret = NULL;
733 }
734
Heiko Schocherf5895d12014-06-24 10:10:04 +0200735 if (!ret) {
736 ret = ERR_PTR(err);
737 goto out;
738 }
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100739
Heiko Schocherf5895d12014-06-24 10:10:04 +0200740 err = __get_mtd_device(ret);
741 if (err)
742 ret = ERR_PTR(err);
743out:
744 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100745 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200746}
747EXPORT_SYMBOL_GPL(get_mtd_device);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100748
Heiko Schocherf5895d12014-06-24 10:10:04 +0200749int __get_mtd_device(struct mtd_info *mtd)
750{
751 int err;
752
753 if (!try_module_get(mtd->owner))
754 return -ENODEV;
755
756 if (mtd->_get_device) {
757 err = mtd->_get_device(mtd);
758
759 if (err) {
760 module_put(mtd->owner);
761 return err;
762 }
763 }
764 mtd->usecount++;
765 return 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100766}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200767EXPORT_SYMBOL_GPL(__get_mtd_device);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100768
Marek Behúnf96299f2021-05-26 14:08:25 +0200769#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL)
770static bool mtd_device_matches_name(struct mtd_info *mtd, const char *name)
771{
772 struct udevice *dev = NULL;
773 bool is_part;
774
775 /*
776 * If the first character of mtd name is '/', try interpreting as OF
777 * path. Otherwise try comparing by mtd->name and mtd->dev->name.
778 */
779 if (*name == '/')
780 device_get_global_by_ofnode(ofnode_path(name), &dev);
781
782 is_part = mtd_is_partition(mtd);
783
784 return (!is_part && dev && mtd->dev == dev) ||
785 !strcmp(name, mtd->name) ||
786 (is_part && mtd->dev && !strcmp(name, mtd->dev->name));
787}
788#else
789static bool mtd_device_matches_name(struct mtd_info *mtd, const char *name)
790{
791 return !strcmp(name, mtd->name);
792}
793#endif
794
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100795/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200796 * get_mtd_device_nm - obtain a validated handle for an MTD device by
797 * device name
798 * @name: MTD device name to open
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100799 *
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200800 * This function returns MTD device description structure in case of
801 * success and an error code in case of failure.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100802 */
803struct mtd_info *get_mtd_device_nm(const char *name)
804{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200805 int err = -ENODEV;
806 struct mtd_info *mtd = NULL, *other;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100807
Heiko Schocherf5895d12014-06-24 10:10:04 +0200808 mutex_lock(&mtd_table_mutex);
809
810 mtd_for_each_device(other) {
Marek Behúnf96299f2021-05-26 14:08:25 +0200811#ifdef __UBOOT__
812 if (mtd_device_matches_name(other, name)) {
813 if (mtd)
814 printf("\nWarning: MTD name \"%s\" is not unique!\n\n",
815 name);
816 mtd = other;
817 }
818#else /* !__UBOOT__ */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200819 if (!strcmp(name, other->name)) {
820 mtd = other;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100821 break;
822 }
Marek Behúnf96299f2021-05-26 14:08:25 +0200823#endif /* !__UBOOT__ */
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100824 }
825
826 if (!mtd)
827 goto out_unlock;
828
Heiko Schocherf5895d12014-06-24 10:10:04 +0200829 err = __get_mtd_device(mtd);
830 if (err)
831 goto out_unlock;
832
833 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100834 return mtd;
835
836out_unlock:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200837 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100838 return ERR_PTR(err);
839}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200840EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Ben Gardiner50bae732010-08-31 17:48:01 -0400841
842#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
843/**
844 * mtd_get_len_incl_bad
845 *
846 * Check if length including bad blocks fits into device.
847 *
848 * @param mtd an MTD device
849 * @param offset offset in flash
850 * @param length image length
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100851 * Return: image length including bad blocks in *len_incl_bad and whether or not
Ben Gardiner50bae732010-08-31 17:48:01 -0400852 * the length returned was truncated in *truncated
853 */
854void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
855 const uint64_t length, uint64_t *len_incl_bad,
856 int *truncated)
857{
858 *truncated = 0;
859 *len_incl_bad = 0;
860
maxin.john@enea.comb5ee6e22014-09-08 19:04:16 +0200861 if (!mtd->_block_isbad) {
Ben Gardiner50bae732010-08-31 17:48:01 -0400862 *len_incl_bad = length;
863 return;
864 }
865
866 uint64_t len_excl_bad = 0;
867 uint64_t block_len;
868
869 while (len_excl_bad < length) {
Scott Wood10390ce2010-09-09 15:40:03 -0500870 if (offset >= mtd->size) {
871 *truncated = 1;
872 return;
873 }
874
Ben Gardiner50bae732010-08-31 17:48:01 -0400875 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
876
maxin.john@enea.comb5ee6e22014-09-08 19:04:16 +0200877 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
Ben Gardiner50bae732010-08-31 17:48:01 -0400878 len_excl_bad += block_len;
879
880 *len_incl_bad += block_len;
881 offset += block_len;
Ben Gardiner50bae732010-08-31 17:48:01 -0400882 }
883}
884#endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000885
Heiko Schocherf5895d12014-06-24 10:10:04 +0200886void put_mtd_device(struct mtd_info *mtd)
887{
888 mutex_lock(&mtd_table_mutex);
889 __put_mtd_device(mtd);
890 mutex_unlock(&mtd_table_mutex);
891
892}
893EXPORT_SYMBOL_GPL(put_mtd_device);
894
895void __put_mtd_device(struct mtd_info *mtd)
896{
897 --mtd->usecount;
898 BUG_ON(mtd->usecount < 0);
899
900 if (mtd->_put_device)
901 mtd->_put_device(mtd);
902
903 module_put(mtd->owner);
904}
905EXPORT_SYMBOL_GPL(__put_mtd_device);
906
Sergey Lapin3a38a552013-01-14 03:46:50 +0000907int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
908{
909 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
910 return -EINVAL;
911 if (!(mtd->flags & MTD_WRITEABLE))
912 return -EROFS;
913 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
914 if (!instr->len) {
915 instr->state = MTD_ERASE_DONE;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000916 return 0;
917 }
918 return mtd->_erase(mtd, instr);
919}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200920EXPORT_SYMBOL_GPL(mtd_erase);
921
922#ifndef __UBOOT__
923/*
924 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
925 */
926int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
927 void **virt, resource_size_t *phys)
928{
929 *retlen = 0;
930 *virt = NULL;
931 if (phys)
932 *phys = 0;
933 if (!mtd->_point)
934 return -EOPNOTSUPP;
935 if (from < 0 || from > mtd->size || len > mtd->size - from)
936 return -EINVAL;
937 if (!len)
938 return 0;
939 return mtd->_point(mtd, from, len, retlen, virt, phys);
940}
941EXPORT_SYMBOL_GPL(mtd_point);
942
943/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
944int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
945{
946 if (!mtd->_point)
947 return -EOPNOTSUPP;
948 if (from < 0 || from > mtd->size || len > mtd->size - from)
949 return -EINVAL;
950 if (!len)
951 return 0;
952 return mtd->_unpoint(mtd, from, len);
953}
954EXPORT_SYMBOL_GPL(mtd_unpoint);
955#endif
956
957/*
958 * Allow NOMMU mmap() to directly map the device (if not NULL)
959 * - return the address to which the offset maps
960 * - return -ENOSYS to indicate refusal to do the mapping
961 */
962unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
963 unsigned long offset, unsigned long flags)
964{
965 if (!mtd->_get_unmapped_area)
966 return -EOPNOTSUPP;
967 if (offset > mtd->size || len > mtd->size - offset)
968 return -EINVAL;
969 return mtd->_get_unmapped_area(mtd, len, offset, flags);
970}
971EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000972
973int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
974 u_char *buf)
975{
Paul Burton700a76c2013-09-04 15:16:56 +0100976 int ret_code;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200977 *retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000978 if (from < 0 || from > mtd->size || len > mtd->size - from)
979 return -EINVAL;
980 if (!len)
981 return 0;
Paul Burton700a76c2013-09-04 15:16:56 +0100982
983 /*
984 * In the absence of an error, drivers return a non-negative integer
985 * representing the maximum number of bitflips that were corrected on
986 * any one ecc region (if applicable; zero otherwise).
987 */
Boris Brezillon6c20df72018-08-16 17:29:59 +0200988 if (mtd->_read) {
989 ret_code = mtd->_read(mtd, from, len, retlen, buf);
990 } else if (mtd->_read_oob) {
991 struct mtd_oob_ops ops = {
992 .len = len,
993 .datbuf = buf,
994 };
995
996 ret_code = mtd->_read_oob(mtd, from, &ops);
997 *retlen = ops.retlen;
998 } else {
999 return -ENOTSUPP;
1000 }
1001
Paul Burton700a76c2013-09-04 15:16:56 +01001002 if (unlikely(ret_code < 0))
1003 return ret_code;
1004 if (mtd->ecc_strength == 0)
1005 return 0; /* device lacks ecc */
1006 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001007}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001008EXPORT_SYMBOL_GPL(mtd_read);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001009
1010int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1011 const u_char *buf)
1012{
1013 *retlen = 0;
1014 if (to < 0 || to > mtd->size || len > mtd->size - to)
1015 return -EINVAL;
Boris Brezillon6c20df72018-08-16 17:29:59 +02001016 if ((!mtd->_write && !mtd->_write_oob) ||
1017 !(mtd->flags & MTD_WRITEABLE))
Sergey Lapin3a38a552013-01-14 03:46:50 +00001018 return -EROFS;
1019 if (!len)
1020 return 0;
Boris Brezillon6c20df72018-08-16 17:29:59 +02001021
1022 if (!mtd->_write) {
1023 struct mtd_oob_ops ops = {
1024 .len = len,
1025 .datbuf = (u8 *)buf,
1026 };
1027 int ret;
1028
1029 ret = mtd->_write_oob(mtd, to, &ops);
1030 *retlen = ops.retlen;
1031 return ret;
1032 }
1033
Sergey Lapin3a38a552013-01-14 03:46:50 +00001034 return mtd->_write(mtd, to, len, retlen, buf);
1035}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001036EXPORT_SYMBOL_GPL(mtd_write);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001037
1038/*
1039 * In blackbox flight recorder like scenarios we want to make successful writes
1040 * in interrupt context. panic_write() is only intended to be called when its
1041 * known the kernel is about to panic and we need the write to succeed. Since
1042 * the kernel is not going to be running for much longer, this function can
1043 * break locks and delay to ensure the write succeeds (but not sleep).
1044 */
1045int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1046 const u_char *buf)
1047{
1048 *retlen = 0;
1049 if (!mtd->_panic_write)
1050 return -EOPNOTSUPP;
1051 if (to < 0 || to > mtd->size || len > mtd->size - to)
1052 return -EINVAL;
1053 if (!(mtd->flags & MTD_WRITEABLE))
1054 return -EROFS;
1055 if (!len)
1056 return 0;
1057 return mtd->_panic_write(mtd, to, len, retlen, buf);
1058}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001059EXPORT_SYMBOL_GPL(mtd_panic_write);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001060
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001061static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1062 struct mtd_oob_ops *ops)
1063{
1064 /*
1065 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1066 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1067 * this case.
1068 */
1069 if (!ops->datbuf)
1070 ops->len = 0;
1071
1072 if (!ops->oobbuf)
1073 ops->ooblen = 0;
1074
1075 if (offs < 0 || offs + ops->len > mtd->size)
1076 return -EINVAL;
1077
1078 if (ops->ooblen) {
Miquel Raynal3a3146c2018-11-18 21:11:47 +01001079 size_t maxooblen;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001080
1081 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1082 return -EINVAL;
1083
Miquel Raynal3a3146c2018-11-18 21:11:47 +01001084 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1085 mtd_div_by_ws(offs, mtd)) *
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001086 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1087 if (ops->ooblen > maxooblen)
1088 return -EINVAL;
1089 }
1090
1091 return 0;
1092}
1093
Sergey Lapin3a38a552013-01-14 03:46:50 +00001094int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1095{
Heiko Schocherf5895d12014-06-24 10:10:04 +02001096 int ret_code;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001097 ops->retlen = ops->oobretlen = 0;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001098
1099 ret_code = mtd_check_oob_ops(mtd, from, ops);
1100 if (ret_code)
1101 return ret_code;
1102
Miquel Raynal19ea9252018-08-16 17:30:02 +02001103 /* Check the validity of a potential fallback on mtd->_read */
1104 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1105 return -EOPNOTSUPP;
1106
1107 if (mtd->_read_oob)
1108 ret_code = mtd->_read_oob(mtd, from, ops);
1109 else
1110 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1111 ops->datbuf);
1112
Heiko Schocherf5895d12014-06-24 10:10:04 +02001113 /*
1114 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1115 * similar to mtd->_read(), returning a non-negative integer
1116 * representing max bitflips. In other cases, mtd->_read_oob() may
1117 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1118 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001119 if (unlikely(ret_code < 0))
1120 return ret_code;
1121 if (mtd->ecc_strength == 0)
1122 return 0; /* device lacks ecc */
1123 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001124}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001125EXPORT_SYMBOL_GPL(mtd_read_oob);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001126
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001127int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1128 struct mtd_oob_ops *ops)
1129{
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001130 int ret;
1131
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001132 ops->retlen = ops->oobretlen = 0;
Miquel Raynal19ea9252018-08-16 17:30:02 +02001133
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001134 if (!(mtd->flags & MTD_WRITEABLE))
1135 return -EROFS;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001136
1137 ret = mtd_check_oob_ops(mtd, to, ops);
1138 if (ret)
1139 return ret;
1140
Miquel Raynal19ea9252018-08-16 17:30:02 +02001141 /* Check the validity of a potential fallback on mtd->_write */
1142 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1143 return -EOPNOTSUPP;
1144
1145 if (mtd->_write_oob)
1146 return mtd->_write_oob(mtd, to, ops);
1147 else
1148 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1149 ops->datbuf);
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001150}
1151EXPORT_SYMBOL_GPL(mtd_write_oob);
1152
Boris Brezillone1b1e3a2017-11-22 02:38:23 +09001153/**
1154 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1155 * @mtd: MTD device structure
1156 * @section: ECC section. Depending on the layout you may have all the ECC
1157 * bytes stored in a single contiguous section, or one section
1158 * per ECC chunk (and sometime several sections for a single ECC
1159 * ECC chunk)
1160 * @oobecc: OOB region struct filled with the appropriate ECC position
1161 * information
1162 *
1163 * This function returns ECC section information in the OOB area. If you want
1164 * to get all the ECC bytes information, then you should call
1165 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1166 *
1167 * Returns zero on success, a negative error code otherwise.
1168 */
1169int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1170 struct mtd_oob_region *oobecc)
1171{
1172 memset(oobecc, 0, sizeof(*oobecc));
1173
1174 if (!mtd || section < 0)
1175 return -EINVAL;
1176
1177 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1178 return -ENOTSUPP;
1179
1180 return mtd->ooblayout->ecc(mtd, section, oobecc);
1181}
1182EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1183
1184/**
1185 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1186 * section
1187 * @mtd: MTD device structure
1188 * @section: Free section you are interested in. Depending on the layout
1189 * you may have all the free bytes stored in a single contiguous
1190 * section, or one section per ECC chunk plus an extra section
1191 * for the remaining bytes (or other funky layout).
1192 * @oobfree: OOB region struct filled with the appropriate free position
1193 * information
1194 *
1195 * This function returns free bytes position in the OOB area. If you want
1196 * to get all the free bytes information, then you should call
1197 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1198 *
1199 * Returns zero on success, a negative error code otherwise.
1200 */
1201int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1202 struct mtd_oob_region *oobfree)
1203{
1204 memset(oobfree, 0, sizeof(*oobfree));
1205
1206 if (!mtd || section < 0)
1207 return -EINVAL;
1208
Simon Glass62fd1a42020-02-03 07:35:56 -07001209 if (!mtd->ooblayout || !mtd->ooblayout->rfree)
Boris Brezillone1b1e3a2017-11-22 02:38:23 +09001210 return -ENOTSUPP;
1211
Simon Glass62fd1a42020-02-03 07:35:56 -07001212 return mtd->ooblayout->rfree(mtd, section, oobfree);
Boris Brezillone1b1e3a2017-11-22 02:38:23 +09001213}
1214EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1215
1216/**
1217 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1218 * @mtd: mtd info structure
1219 * @byte: the byte we are searching for
1220 * @sectionp: pointer where the section id will be stored
1221 * @oobregion: used to retrieve the ECC position
1222 * @iter: iterator function. Should be either mtd_ooblayout_free or
1223 * mtd_ooblayout_ecc depending on the region type you're searching for
1224 *
1225 * This function returns the section id and oobregion information of a
1226 * specific byte. For example, say you want to know where the 4th ECC byte is
1227 * stored, you'll use:
1228 *
1229 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1230 *
1231 * Returns zero on success, a negative error code otherwise.
1232 */
1233static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1234 int *sectionp, struct mtd_oob_region *oobregion,
1235 int (*iter)(struct mtd_info *,
1236 int section,
1237 struct mtd_oob_region *oobregion))
1238{
1239 int pos = 0, ret, section = 0;
1240
1241 memset(oobregion, 0, sizeof(*oobregion));
1242
1243 while (1) {
1244 ret = iter(mtd, section, oobregion);
1245 if (ret)
1246 return ret;
1247
1248 if (pos + oobregion->length > byte)
1249 break;
1250
1251 pos += oobregion->length;
1252 section++;
1253 }
1254
1255 /*
1256 * Adjust region info to make it start at the beginning at the
1257 * 'start' ECC byte.
1258 */
1259 oobregion->offset += byte - pos;
1260 oobregion->length -= byte - pos;
1261 *sectionp = section;
1262
1263 return 0;
1264}
1265
1266/**
1267 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1268 * ECC byte
1269 * @mtd: mtd info structure
1270 * @eccbyte: the byte we are searching for
1271 * @sectionp: pointer where the section id will be stored
1272 * @oobregion: OOB region information
1273 *
1274 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1275 * byte.
1276 *
1277 * Returns zero on success, a negative error code otherwise.
1278 */
1279int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1280 int *section,
1281 struct mtd_oob_region *oobregion)
1282{
1283 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1284 mtd_ooblayout_ecc);
1285}
1286EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1287
1288/**
1289 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1290 * @mtd: mtd info structure
1291 * @buf: destination buffer to store OOB bytes
1292 * @oobbuf: OOB buffer
1293 * @start: first byte to retrieve
1294 * @nbytes: number of bytes to retrieve
1295 * @iter: section iterator
1296 *
1297 * Extract bytes attached to a specific category (ECC or free)
1298 * from the OOB buffer and copy them into buf.
1299 *
1300 * Returns zero on success, a negative error code otherwise.
1301 */
1302static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1303 const u8 *oobbuf, int start, int nbytes,
1304 int (*iter)(struct mtd_info *,
1305 int section,
1306 struct mtd_oob_region *oobregion))
1307{
1308 struct mtd_oob_region oobregion;
1309 int section, ret;
1310
1311 ret = mtd_ooblayout_find_region(mtd, start, &section,
1312 &oobregion, iter);
1313
1314 while (!ret) {
1315 int cnt;
1316
1317 cnt = min_t(int, nbytes, oobregion.length);
1318 memcpy(buf, oobbuf + oobregion.offset, cnt);
1319 buf += cnt;
1320 nbytes -= cnt;
1321
1322 if (!nbytes)
1323 break;
1324
1325 ret = iter(mtd, ++section, &oobregion);
1326 }
1327
1328 return ret;
1329}
1330
1331/**
1332 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1333 * @mtd: mtd info structure
1334 * @buf: source buffer to get OOB bytes from
1335 * @oobbuf: OOB buffer
1336 * @start: first OOB byte to set
1337 * @nbytes: number of OOB bytes to set
1338 * @iter: section iterator
1339 *
1340 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1341 * is selected by passing the appropriate iterator.
1342 *
1343 * Returns zero on success, a negative error code otherwise.
1344 */
1345static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1346 u8 *oobbuf, int start, int nbytes,
1347 int (*iter)(struct mtd_info *,
1348 int section,
1349 struct mtd_oob_region *oobregion))
1350{
1351 struct mtd_oob_region oobregion;
1352 int section, ret;
1353
1354 ret = mtd_ooblayout_find_region(mtd, start, &section,
1355 &oobregion, iter);
1356
1357 while (!ret) {
1358 int cnt;
1359
1360 cnt = min_t(int, nbytes, oobregion.length);
1361 memcpy(oobbuf + oobregion.offset, buf, cnt);
1362 buf += cnt;
1363 nbytes -= cnt;
1364
1365 if (!nbytes)
1366 break;
1367
1368 ret = iter(mtd, ++section, &oobregion);
1369 }
1370
1371 return ret;
1372}
1373
1374/**
1375 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1376 * @mtd: mtd info structure
1377 * @iter: category iterator
1378 *
1379 * Count the number of bytes in a given category.
1380 *
1381 * Returns a positive value on success, a negative error code otherwise.
1382 */
1383static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1384 int (*iter)(struct mtd_info *,
1385 int section,
1386 struct mtd_oob_region *oobregion))
1387{
1388 struct mtd_oob_region oobregion;
1389 int section = 0, ret, nbytes = 0;
1390
1391 while (1) {
1392 ret = iter(mtd, section++, &oobregion);
1393 if (ret) {
1394 if (ret == -ERANGE)
1395 ret = nbytes;
1396 break;
1397 }
1398
1399 nbytes += oobregion.length;
1400 }
1401
1402 return ret;
1403}
1404
1405/**
1406 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1407 * @mtd: mtd info structure
1408 * @eccbuf: destination buffer to store ECC bytes
1409 * @oobbuf: OOB buffer
1410 * @start: first ECC byte to retrieve
1411 * @nbytes: number of ECC bytes to retrieve
1412 *
1413 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1414 *
1415 * Returns zero on success, a negative error code otherwise.
1416 */
1417int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1418 const u8 *oobbuf, int start, int nbytes)
1419{
1420 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1421 mtd_ooblayout_ecc);
1422}
1423EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1424
1425/**
1426 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1427 * @mtd: mtd info structure
1428 * @eccbuf: source buffer to get ECC bytes from
1429 * @oobbuf: OOB buffer
1430 * @start: first ECC byte to set
1431 * @nbytes: number of ECC bytes to set
1432 *
1433 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1434 *
1435 * Returns zero on success, a negative error code otherwise.
1436 */
1437int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1438 u8 *oobbuf, int start, int nbytes)
1439{
1440 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1441 mtd_ooblayout_ecc);
1442}
1443EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1444
1445/**
1446 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1447 * @mtd: mtd info structure
1448 * @databuf: destination buffer to store ECC bytes
1449 * @oobbuf: OOB buffer
1450 * @start: first ECC byte to retrieve
1451 * @nbytes: number of ECC bytes to retrieve
1452 *
1453 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1454 *
1455 * Returns zero on success, a negative error code otherwise.
1456 */
1457int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1458 const u8 *oobbuf, int start, int nbytes)
1459{
1460 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1461 mtd_ooblayout_free);
1462}
1463EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1464
1465/**
1466 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1467 * @mtd: mtd info structure
1468 * @eccbuf: source buffer to get data bytes from
1469 * @oobbuf: OOB buffer
1470 * @start: first ECC byte to set
1471 * @nbytes: number of ECC bytes to set
1472 *
1473 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1474 *
1475 * Returns zero on success, a negative error code otherwise.
1476 */
1477int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1478 u8 *oobbuf, int start, int nbytes)
1479{
1480 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1481 mtd_ooblayout_free);
1482}
1483EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1484
1485/**
1486 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1487 * @mtd: mtd info structure
1488 *
1489 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1490 *
1491 * Returns zero on success, a negative error code otherwise.
1492 */
1493int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1494{
1495 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1496}
1497EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1498
1499/**
1500 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1501 * @mtd: mtd info structure
1502 *
1503 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1504 *
1505 * Returns zero on success, a negative error code otherwise.
1506 */
1507int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1508{
1509 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1510}
1511EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1512
Sergey Lapin3a38a552013-01-14 03:46:50 +00001513/*
1514 * Method to access the protection register area, present in some flash
1515 * devices. The user data is one time programmable but the factory data is read
1516 * only.
1517 */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001518int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1519 struct otp_info *buf)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001520{
1521 if (!mtd->_get_fact_prot_info)
1522 return -EOPNOTSUPP;
1523 if (!len)
1524 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001525 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001526}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001527EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001528
1529int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1530 size_t *retlen, u_char *buf)
1531{
1532 *retlen = 0;
1533 if (!mtd->_read_fact_prot_reg)
1534 return -EOPNOTSUPP;
1535 if (!len)
1536 return 0;
1537 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1538}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001539EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001540
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001541int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1542 struct otp_info *buf)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001543{
1544 if (!mtd->_get_user_prot_info)
1545 return -EOPNOTSUPP;
1546 if (!len)
1547 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001548 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001549}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001550EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001551
1552int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1553 size_t *retlen, u_char *buf)
1554{
1555 *retlen = 0;
1556 if (!mtd->_read_user_prot_reg)
1557 return -EOPNOTSUPP;
1558 if (!len)
1559 return 0;
1560 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1561}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001562EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001563
1564int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1565 size_t *retlen, u_char *buf)
1566{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001567 int ret;
1568
Sergey Lapin3a38a552013-01-14 03:46:50 +00001569 *retlen = 0;
1570 if (!mtd->_write_user_prot_reg)
1571 return -EOPNOTSUPP;
1572 if (!len)
1573 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001574 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1575 if (ret)
1576 return ret;
1577
1578 /*
1579 * If no data could be written at all, we are out of memory and
1580 * must return -ENOSPC.
1581 */
1582 return (*retlen) ? 0 : -ENOSPC;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001583}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001584EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001585
1586int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1587{
1588 if (!mtd->_lock_user_prot_reg)
1589 return -EOPNOTSUPP;
1590 if (!len)
1591 return 0;
1592 return mtd->_lock_user_prot_reg(mtd, from, len);
1593}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001594EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001595
1596/* Chip-supported device locking */
1597int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1598{
1599 if (!mtd->_lock)
1600 return -EOPNOTSUPP;
1601 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1602 return -EINVAL;
1603 if (!len)
1604 return 0;
1605 return mtd->_lock(mtd, ofs, len);
1606}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001607EXPORT_SYMBOL_GPL(mtd_lock);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001608
1609int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1610{
1611 if (!mtd->_unlock)
1612 return -EOPNOTSUPP;
1613 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1614 return -EINVAL;
1615 if (!len)
1616 return 0;
1617 return mtd->_unlock(mtd, ofs, len);
1618}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001619EXPORT_SYMBOL_GPL(mtd_unlock);
1620
1621int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1622{
1623 if (!mtd->_is_locked)
1624 return -EOPNOTSUPP;
1625 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1626 return -EINVAL;
1627 if (!len)
1628 return 0;
1629 return mtd->_is_locked(mtd, ofs, len);
1630}
1631EXPORT_SYMBOL_GPL(mtd_is_locked);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001632
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001633int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001634{
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001635 if (ofs < 0 || ofs > mtd->size)
1636 return -EINVAL;
1637 if (!mtd->_block_isreserved)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001638 return 0;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001639 return mtd->_block_isreserved(mtd, ofs);
1640}
1641EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1642
1643int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1644{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001645 if (ofs < 0 || ofs > mtd->size)
1646 return -EINVAL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001647 if (!mtd->_block_isbad)
1648 return 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001649 return mtd->_block_isbad(mtd, ofs);
1650}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001651EXPORT_SYMBOL_GPL(mtd_block_isbad);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001652
1653int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1654{
1655 if (!mtd->_block_markbad)
1656 return -EOPNOTSUPP;
1657 if (ofs < 0 || ofs > mtd->size)
1658 return -EINVAL;
1659 if (!(mtd->flags & MTD_WRITEABLE))
1660 return -EROFS;
1661 return mtd->_block_markbad(mtd, ofs);
1662}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001663EXPORT_SYMBOL_GPL(mtd_block_markbad);
1664
1665#ifndef __UBOOT__
1666/*
1667 * default_mtd_writev - the default writev method
1668 * @mtd: mtd device description object pointer
1669 * @vecs: the vectors to write
1670 * @count: count of vectors in @vecs
1671 * @to: the MTD device offset to write to
1672 * @retlen: on exit contains the count of bytes written to the MTD device.
1673 *
1674 * This function returns zero in case of success and a negative error code in
1675 * case of failure.
1676 */
1677static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1678 unsigned long count, loff_t to, size_t *retlen)
1679{
1680 unsigned long i;
1681 size_t totlen = 0, thislen;
1682 int ret = 0;
1683
1684 for (i = 0; i < count; i++) {
1685 if (!vecs[i].iov_len)
1686 continue;
1687 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1688 vecs[i].iov_base);
1689 totlen += thislen;
1690 if (ret || thislen != vecs[i].iov_len)
1691 break;
1692 to += vecs[i].iov_len;
1693 }
1694 *retlen = totlen;
1695 return ret;
1696}
1697
1698/*
1699 * mtd_writev - the vector-based MTD write method
1700 * @mtd: mtd device description object pointer
1701 * @vecs: the vectors to write
1702 * @count: count of vectors in @vecs
1703 * @to: the MTD device offset to write to
1704 * @retlen: on exit contains the count of bytes written to the MTD device.
1705 *
1706 * This function returns zero in case of success and a negative error code in
1707 * case of failure.
1708 */
1709int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1710 unsigned long count, loff_t to, size_t *retlen)
1711{
1712 *retlen = 0;
1713 if (!(mtd->flags & MTD_WRITEABLE))
1714 return -EROFS;
1715 if (!mtd->_writev)
1716 return default_mtd_writev(mtd, vecs, count, to, retlen);
1717 return mtd->_writev(mtd, vecs, count, to, retlen);
1718}
1719EXPORT_SYMBOL_GPL(mtd_writev);
1720
1721/**
1722 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1723 * @mtd: mtd device description object pointer
1724 * @size: a pointer to the ideal or maximum size of the allocation, points
1725 * to the actual allocation size on success.
1726 *
1727 * This routine attempts to allocate a contiguous kernel buffer up to
1728 * the specified size, backing off the size of the request exponentially
1729 * until the request succeeds or until the allocation size falls below
1730 * the system page size. This attempts to make sure it does not adversely
1731 * impact system performance, so when allocating more than one page, we
1732 * ask the memory allocator to avoid re-trying, swapping, writing back
1733 * or performing I/O.
1734 *
1735 * Note, this function also makes sure that the allocated buffer is aligned to
1736 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1737 *
1738 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1739 * to handle smaller (i.e. degraded) buffer allocations under low- or
1740 * fragmented-memory situations where such reduced allocations, from a
1741 * requested ideal, are allowed.
1742 *
1743 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1744 */
1745void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1746{
1747 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1748 __GFP_NORETRY | __GFP_NO_KSWAPD;
1749 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1750 void *kbuf;
1751
1752 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1753
1754 while (*size > min_alloc) {
1755 kbuf = kmalloc(*size, flags);
1756 if (kbuf)
1757 return kbuf;
1758
1759 *size >>= 1;
1760 *size = ALIGN(*size, mtd->writesize);
1761 }
1762
1763 /*
1764 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1765 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1766 */
1767 return kmalloc(*size, GFP_KERNEL);
1768}
1769EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1770#endif
1771
1772#ifdef CONFIG_PROC_FS
1773
1774/*====================================================================*/
1775/* Support for /proc/mtd */
1776
1777static int mtd_proc_show(struct seq_file *m, void *v)
1778{
1779 struct mtd_info *mtd;
1780
1781 seq_puts(m, "dev: size erasesize name\n");
1782 mutex_lock(&mtd_table_mutex);
1783 mtd_for_each_device(mtd) {
1784 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1785 mtd->index, (unsigned long long)mtd->size,
1786 mtd->erasesize, mtd->name);
1787 }
1788 mutex_unlock(&mtd_table_mutex);
1789 return 0;
1790}
1791
1792static int mtd_proc_open(struct inode *inode, struct file *file)
1793{
1794 return single_open(file, mtd_proc_show, NULL);
1795}
1796
1797static const struct file_operations mtd_proc_ops = {
1798 .open = mtd_proc_open,
1799 .read = seq_read,
1800 .llseek = seq_lseek,
1801 .release = single_release,
1802};
1803#endif /* CONFIG_PROC_FS */
1804
1805/*====================================================================*/
1806/* Init code */
1807
1808#ifndef __UBOOT__
1809static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1810{
1811 int ret;
1812
1813 ret = bdi_init(bdi);
1814 if (!ret)
1815 ret = bdi_register(bdi, NULL, "%s", name);
1816
1817 if (ret)
1818 bdi_destroy(bdi);
1819
1820 return ret;
1821}
1822
1823static struct proc_dir_entry *proc_mtd;
1824
1825static int __init init_mtd(void)
1826{
1827 int ret;
1828
1829 ret = class_register(&mtd_class);
1830 if (ret)
1831 goto err_reg;
1832
1833 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1834 if (ret)
1835 goto err_bdi1;
1836
1837 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1838 if (ret)
1839 goto err_bdi2;
1840
1841 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1842 if (ret)
1843 goto err_bdi3;
1844
1845 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1846
1847 ret = init_mtdchar();
1848 if (ret)
1849 goto out_procfs;
1850
1851 return 0;
1852
1853out_procfs:
1854 if (proc_mtd)
1855 remove_proc_entry("mtd", NULL);
1856err_bdi3:
1857 bdi_destroy(&mtd_bdi_ro_mappable);
1858err_bdi2:
1859 bdi_destroy(&mtd_bdi_unmappable);
1860err_bdi1:
1861 class_unregister(&mtd_class);
1862err_reg:
1863 pr_err("Error registering mtd class or bdi: %d\n", ret);
1864 return ret;
1865}
1866
1867static void __exit cleanup_mtd(void)
1868{
1869 cleanup_mtdchar();
1870 if (proc_mtd)
1871 remove_proc_entry("mtd", NULL);
1872 class_unregister(&mtd_class);
1873 bdi_destroy(&mtd_bdi_unmappable);
1874 bdi_destroy(&mtd_bdi_ro_mappable);
1875 bdi_destroy(&mtd_bdi_rw_mappable);
1876}
1877
1878module_init(init_mtd);
1879module_exit(cleanup_mtd);
1880#endif
1881
1882MODULE_LICENSE("GPL");
1883MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1884MODULE_DESCRIPTION("Core MTD registration and access routines");