blob: 770f64d6701567a29cb1da37d51685eb009e4d6f [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Core registration and callback routines for MTD
4 * drivers and users.
5 *
David Woodhousea1452a32010-08-08 20:58:20 +01006 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
7 * Copyright © 2006 Red Hat UK Limited
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/ptrace.h>
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +030013#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/string.h>
15#include <linux/timer.h>
16#include <linux/major.h>
17#include <linux/fs.h>
Artem Bityutskiy77993082006-10-11 14:52:44 +030018#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/init.h>
Brian Norris215a02f2015-11-11 16:26:04 -080021#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/proc_fs.h>
Ben Hutchingsb520e412010-01-29 20:59:42 +000023#include <linux/idr.h>
Jörn Engela33eb6b2010-04-27 09:40:52 +020024#include <linux/backing-dev.h>
Tejun Heo05d71b462010-03-30 02:52:39 +090025#include <linux/gfp.h>
David Howells0d01ff22013-04-11 23:51:01 +010026#include <linux/slab.h>
Brian Norris3efe41b2014-11-26 01:01:08 -080027#include <linux/reboot.h>
Ezequiel Garciafea728c2016-04-12 17:46:42 -030028#include <linux/leds.h>
Mario Rugieroe8e3edb2017-05-29 08:38:41 -030029#include <linux/debugfs.h>
Alban Bedelc4dfa252018-11-13 15:01:10 +010030#include <linux/nvmem-provider.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/mtd/mtd.h>
Jamie Ilesf5671ab2011-05-23 17:15:46 +010033#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Ben Dooks356d70f2007-05-28 20:28:34 +010035#include "mtdcore.h"
Artem Bityutskiy660685d2013-03-14 13:27:40 +020036
Jan Karafa060522017-04-12 12:24:37 +020037struct backing_dev_info *mtd_bdi;
Ben Dooks356d70f2007-05-28 20:28:34 +010038
Lars-Peter Clausen57b8045d2015-04-06 12:00:39 +020039#ifdef CONFIG_PM_SLEEP
40
41static 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
48static 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
57static 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
David Brownell1f24b5a2009-03-26 00:42:41 -070062
David Woodhouse15bce402009-04-05 07:40:58 -070063static struct class mtd_class = {
64 .name = "mtd",
65 .owner = THIS_MODULE,
Lars-Peter Clausen57b8045d2015-04-06 12:00:39 +020066 .pm = MTD_CLS_PM_OPS,
David Woodhouse15bce402009-04-05 07:40:58 -070067};
David Brownell1f24b5a2009-03-26 00:42:41 -070068
Ben Hutchingsb520e412010-01-29 20:59:42 +000069static DEFINE_IDR(mtd_idr);
70
Thomas Gleixner97894cd2005-11-07 11:15:26 +000071/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080073DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074EXPORT_SYMBOL_GPL(mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +000075
76struct mtd_info *__mtd_next_device(int i)
77{
78 return idr_get_next(&mtd_idr, &i);
79}
80EXPORT_SYMBOL_GPL(__mtd_next_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static LIST_HEAD(mtd_notifiers);
83
David Brownell1f24b5a2009-03-26 00:42:41 -070084
David Brownell1f24b5a2009-03-26 00:42:41 -070085#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
David Brownell1f24b5a2009-03-26 00:42:41 -070086
87/* REVISIT once MTD uses the driver model better, whoever allocates
88 * the mtd_info will probably want to use the release() hook...
89 */
90static void mtd_release(struct device *dev)
91{
Brian Norris5e472122014-07-21 19:06:47 -070092 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020093 dev_t index = MTD_DEVT(mtd->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070094
Brian Norris5e472122014-07-21 19:06:47 -070095 /* remove /dev/mtdXro node */
96 device_destroy(&mtd_class, index + 1);
David Woodhouse15bce402009-04-05 07:40:58 -070097}
98
Zhen Leib4e24862021-06-03 20:53:22 +080099#define MTD_DEVICE_ATTR_RO(name) \
100static DEVICE_ATTR(name, 0444, mtd_##name##_show, NULL)
101
102#define MTD_DEVICE_ATTR_RW(name) \
103static DEVICE_ATTR(name, 0644, mtd_##name##_show, mtd_##name##_store)
104
David Brownell1f24b5a2009-03-26 00:42:41 -0700105static ssize_t mtd_type_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
107{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200108 struct mtd_info *mtd = dev_get_drvdata(dev);
David Brownell1f24b5a2009-03-26 00:42:41 -0700109 char *type;
110
111 switch (mtd->type) {
112 case MTD_ABSENT:
113 type = "absent";
114 break;
115 case MTD_RAM:
116 type = "ram";
117 break;
118 case MTD_ROM:
119 type = "rom";
120 break;
121 case MTD_NORFLASH:
122 type = "nor";
123 break;
124 case MTD_NANDFLASH:
125 type = "nand";
126 break;
127 case MTD_DATAFLASH:
128 type = "dataflash";
129 break;
130 case MTD_UBIVOLUME:
131 type = "ubi";
132 break;
Huang Shijief4837242013-09-25 14:58:19 +0800133 case MTD_MLCNANDFLASH:
134 type = "mlc-nand";
135 break;
David Brownell1f24b5a2009-03-26 00:42:41 -0700136 default:
137 type = "unknown";
138 }
139
Tian Tao5b2fbe02021-04-12 17:35:43 +0800140 return sysfs_emit(buf, "%s\n", type);
David Brownell1f24b5a2009-03-26 00:42:41 -0700141}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700142static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
143
144static ssize_t mtd_flags_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200147 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700148
Tian Tao5b2fbe02021-04-12 17:35:43 +0800149 return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700150}
151static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
152
153static ssize_t mtd_size_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200156 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700157
Tian Tao5b2fbe02021-04-12 17:35:43 +0800158 return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700159}
160static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
161
162static ssize_t mtd_erasesize_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
164{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200165 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700166
Tian Tao5b2fbe02021-04-12 17:35:43 +0800167 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700168}
169static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
170
171static ssize_t mtd_writesize_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200174 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700175
Tian Tao5b2fbe02021-04-12 17:35:43 +0800176 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700177}
178static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
179
Artem Bityutskiye7693542009-04-18 12:29:42 +0300180static ssize_t mtd_subpagesize_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
182{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200183 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiye7693542009-04-18 12:29:42 +0300184 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
185
Tian Tao5b2fbe02021-04-12 17:35:43 +0800186 return sysfs_emit(buf, "%u\n", subpagesize);
Artem Bityutskiye7693542009-04-18 12:29:42 +0300187}
188static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
189
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700190static ssize_t mtd_oobsize_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200193 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700194
Tian Tao5b2fbe02021-04-12 17:35:43 +0800195 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700196}
197static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
198
Xiaolei Li7cc9aa62018-04-02 16:20:10 +0800199static ssize_t mtd_oobavail_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201{
202 struct mtd_info *mtd = dev_get_drvdata(dev);
203
Tian Tao5b2fbe02021-04-12 17:35:43 +0800204 return sysfs_emit(buf, "%u\n", mtd->oobavail);
Xiaolei Li7cc9aa62018-04-02 16:20:10 +0800205}
206static DEVICE_ATTR(oobavail, S_IRUGO, mtd_oobavail_show, NULL);
207
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700208static ssize_t mtd_numeraseregions_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200211 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700212
Tian Tao5b2fbe02021-04-12 17:35:43 +0800213 return sysfs_emit(buf, "%u\n", mtd->numeraseregions);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700214}
215static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
216 NULL);
217
218static ssize_t mtd_name_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
220{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200221 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700222
Tian Tao5b2fbe02021-04-12 17:35:43 +0800223 return sysfs_emit(buf, "%s\n", mtd->name);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700224}
225static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700226
Mike Dunna9b672e2012-04-25 12:06:07 -0700227static ssize_t mtd_ecc_strength_show(struct device *dev,
228 struct device_attribute *attr, char *buf)
229{
230 struct mtd_info *mtd = dev_get_drvdata(dev);
231
Tian Tao5b2fbe02021-04-12 17:35:43 +0800232 return sysfs_emit(buf, "%u\n", mtd->ecc_strength);
Mike Dunna9b672e2012-04-25 12:06:07 -0700233}
234static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
235
Mike Dunnd062d4e2012-04-25 12:06:08 -0700236static ssize_t mtd_bitflip_threshold_show(struct device *dev,
237 struct device_attribute *attr,
238 char *buf)
239{
240 struct mtd_info *mtd = dev_get_drvdata(dev);
241
Tian Tao5b2fbe02021-04-12 17:35:43 +0800242 return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold);
Mike Dunnd062d4e2012-04-25 12:06:08 -0700243}
244
245static ssize_t mtd_bitflip_threshold_store(struct device *dev,
246 struct device_attribute *attr,
247 const char *buf, size_t count)
248{
249 struct mtd_info *mtd = dev_get_drvdata(dev);
250 unsigned int bitflip_threshold;
251 int retval;
252
253 retval = kstrtouint(buf, 0, &bitflip_threshold);
254 if (retval)
255 return retval;
256
257 mtd->bitflip_threshold = bitflip_threshold;
258 return count;
259}
260static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
261 mtd_bitflip_threshold_show,
262 mtd_bitflip_threshold_store);
263
Huang Shijiebf977e32013-08-16 10:10:05 +0800264static ssize_t mtd_ecc_step_size_show(struct device *dev,
265 struct device_attribute *attr, char *buf)
266{
267 struct mtd_info *mtd = dev_get_drvdata(dev);
268
Tian Tao5b2fbe02021-04-12 17:35:43 +0800269 return sysfs_emit(buf, "%u\n", mtd->ecc_step_size);
Huang Shijiebf977e32013-08-16 10:10:05 +0800270
271}
272static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
273
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300274static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
276{
277 struct mtd_info *mtd = dev_get_drvdata(dev);
278 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
279
Tian Tao5b2fbe02021-04-12 17:35:43 +0800280 return sysfs_emit(buf, "%u\n", ecc_stats->corrected);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300281}
282static DEVICE_ATTR(corrected_bits, S_IRUGO,
283 mtd_ecc_stats_corrected_show, NULL);
284
285static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct mtd_info *mtd = dev_get_drvdata(dev);
289 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
290
Tian Tao5b2fbe02021-04-12 17:35:43 +0800291 return sysfs_emit(buf, "%u\n", ecc_stats->failed);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300292}
293static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
294
295static ssize_t mtd_badblocks_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct mtd_info *mtd = dev_get_drvdata(dev);
299 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
300
Tian Tao5b2fbe02021-04-12 17:35:43 +0800301 return sysfs_emit(buf, "%u\n", ecc_stats->badblocks);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300302}
303static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
304
305static ssize_t mtd_bbtblocks_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
308 struct mtd_info *mtd = dev_get_drvdata(dev);
309 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
310
Tian Tao5b2fbe02021-04-12 17:35:43 +0800311 return sysfs_emit(buf, "%u\n", ecc_stats->bbtblocks);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300312}
313static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
314
David Brownell1f24b5a2009-03-26 00:42:41 -0700315static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700316 &dev_attr_type.attr,
317 &dev_attr_flags.attr,
318 &dev_attr_size.attr,
319 &dev_attr_erasesize.attr,
320 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300321 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700322 &dev_attr_oobsize.attr,
Xiaolei Li7cc9aa62018-04-02 16:20:10 +0800323 &dev_attr_oobavail.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700324 &dev_attr_numeraseregions.attr,
325 &dev_attr_name.attr,
Mike Dunna9b672e2012-04-25 12:06:07 -0700326 &dev_attr_ecc_strength.attr,
Huang Shijiebf977e32013-08-16 10:10:05 +0800327 &dev_attr_ecc_step_size.attr,
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300328 &dev_attr_corrected_bits.attr,
329 &dev_attr_ecc_failures.attr,
330 &dev_attr_bad_blocks.attr,
331 &dev_attr_bbt_blocks.attr,
Mike Dunnd062d4e2012-04-25 12:06:08 -0700332 &dev_attr_bitflip_threshold.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700333 NULL,
334};
Axel Lin54c738f2013-12-02 10:12:25 +0800335ATTRIBUTE_GROUPS(mtd);
David Brownell1f24b5a2009-03-26 00:42:41 -0700336
Bhumika Goyal75864b32017-08-19 13:52:17 +0530337static const struct device_type mtd_devtype = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700338 .name = "mtd",
339 .groups = mtd_groups,
340 .release = mtd_release,
341};
342
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800343static int mtd_partid_debug_show(struct seq_file *s, void *p)
Zhuohao Lee1018c942019-07-01 00:07:10 +0800344{
345 struct mtd_info *mtd = s->private;
346
347 seq_printf(s, "%s\n", mtd->dbg.partid);
348
349 return 0;
350}
351
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800352DEFINE_SHOW_ATTRIBUTE(mtd_partid_debug);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800353
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800354static int mtd_partname_debug_show(struct seq_file *s, void *p)
Zhuohao Lee1018c942019-07-01 00:07:10 +0800355{
356 struct mtd_info *mtd = s->private;
357
358 seq_printf(s, "%s\n", mtd->dbg.partname);
359
360 return 0;
361}
362
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800363DEFINE_SHOW_ATTRIBUTE(mtd_partname_debug);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800364
365static struct dentry *dfs_dir_mtd;
366
367static void mtd_debugfs_populate(struct mtd_info *mtd)
368{
Petr Malatfeb05fa2021-04-30 08:50:57 +0200369 struct mtd_info *master = mtd_get_master(mtd);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800370 struct device *dev = &mtd->dev;
Greg Kroah-Hartmanc2d73ba2019-11-07 09:51:11 +0100371 struct dentry *root;
Zhuohao Lee1018c942019-07-01 00:07:10 +0800372
373 if (IS_ERR_OR_NULL(dfs_dir_mtd))
374 return;
375
376 root = debugfs_create_dir(dev_name(dev), dfs_dir_mtd);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800377 mtd->dbg.dfs_dir = root;
378
Petr Malatfeb05fa2021-04-30 08:50:57 +0200379 if (master->dbg.partid)
380 debugfs_create_file("partid", 0400, root, master,
Greg Kroah-Hartmanc2d73ba2019-11-07 09:51:11 +0100381 &mtd_partid_debug_fops);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800382
Petr Malatfeb05fa2021-04-30 08:50:57 +0200383 if (master->dbg.partname)
384 debugfs_create_file("partname", 0400, root, master,
Greg Kroah-Hartmanc2d73ba2019-11-07 09:51:11 +0100385 &mtd_partname_debug_fops);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800386}
387
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100388#ifndef CONFIG_MMU
389unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
390{
391 switch (mtd->type) {
392 case MTD_RAM:
393 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
394 NOMMU_MAP_READ | NOMMU_MAP_WRITE;
395 case MTD_ROM:
396 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
397 NOMMU_MAP_READ;
398 default:
399 return NOMMU_MAP_COPY;
400 }
401}
Arnd Bergmann706a4e52015-01-28 11:09:20 -0700402EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100403#endif
404
Brian Norris3efe41b2014-11-26 01:01:08 -0800405static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
406 void *cmd)
407{
408 struct mtd_info *mtd;
409
410 mtd = container_of(n, struct mtd_info, reboot_notifier);
411 mtd->_reboot(mtd);
412
413 return NOTIFY_DONE;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/**
Boris Brezillon477b0222015-11-16 15:53:13 +0100417 * mtd_wunit_to_pairing_info - get pairing information of a wunit
418 * @mtd: pointer to new MTD device info structure
419 * @wunit: write unit we are interested in
420 * @info: returned pairing information
421 *
422 * Retrieve pairing information associated to the wunit.
423 * This is mainly useful when dealing with MLC/TLC NANDs where pages can be
424 * paired together, and where programming a page may influence the page it is
425 * paired with.
426 * The notion of page is replaced by the term wunit (write-unit) to stay
427 * consistent with the ->writesize field.
428 *
429 * The @wunit argument can be extracted from an absolute offset using
430 * mtd_offset_to_wunit(). @info is filled with the pairing information attached
431 * to @wunit.
432 *
433 * From the pairing info the MTD user can find all the wunits paired with
434 * @wunit using the following loop:
435 *
436 * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
437 * info.pair = i;
438 * mtd_pairing_info_to_wunit(mtd, &info);
439 * ...
440 * }
441 */
442int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
443 struct mtd_pairing_info *info)
444{
Miquel Raynal46b58892020-01-14 10:09:52 +0100445 struct mtd_info *master = mtd_get_master(mtd);
446 int npairs = mtd_wunit_per_eb(master) / mtd_pairing_groups(master);
Boris Brezillon477b0222015-11-16 15:53:13 +0100447
448 if (wunit < 0 || wunit >= npairs)
449 return -EINVAL;
450
Miquel Raynal46b58892020-01-14 10:09:52 +0100451 if (master->pairing && master->pairing->get_info)
452 return master->pairing->get_info(master, wunit, info);
Boris Brezillon477b0222015-11-16 15:53:13 +0100453
454 info->group = 0;
455 info->pair = wunit;
456
457 return 0;
458}
459EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info);
460
461/**
Xiaolei Lic77a9312018-03-29 09:34:58 +0800462 * mtd_pairing_info_to_wunit - get wunit from pairing information
Boris Brezillon477b0222015-11-16 15:53:13 +0100463 * @mtd: pointer to new MTD device info structure
464 * @info: pairing information struct
465 *
466 * Returns a positive number representing the wunit associated to the info
467 * struct, or a negative error code.
468 *
469 * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to
470 * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info()
471 * doc).
472 *
473 * It can also be used to only program the first page of each pair (i.e.
474 * page attached to group 0), which allows one to use an MLC NAND in
475 * software-emulated SLC mode:
476 *
477 * info.group = 0;
478 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
479 * for (info.pair = 0; info.pair < npairs; info.pair++) {
480 * wunit = mtd_pairing_info_to_wunit(mtd, &info);
481 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
482 * mtd->writesize, &retlen, buf + (i * mtd->writesize));
483 * }
484 */
485int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
486 const struct mtd_pairing_info *info)
487{
Miquel Raynal46b58892020-01-14 10:09:52 +0100488 struct mtd_info *master = mtd_get_master(mtd);
489 int ngroups = mtd_pairing_groups(master);
490 int npairs = mtd_wunit_per_eb(master) / ngroups;
Boris Brezillon477b0222015-11-16 15:53:13 +0100491
492 if (!info || info->pair < 0 || info->pair >= npairs ||
493 info->group < 0 || info->group >= ngroups)
494 return -EINVAL;
495
Miquel Raynal46b58892020-01-14 10:09:52 +0100496 if (master->pairing && master->pairing->get_wunit)
497 return mtd->pairing->get_wunit(master, info);
Boris Brezillon477b0222015-11-16 15:53:13 +0100498
499 return info->pair;
500}
501EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit);
502
503/**
504 * mtd_pairing_groups - get the number of pairing groups
505 * @mtd: pointer to new MTD device info structure
506 *
507 * Returns the number of pairing groups.
508 *
509 * This number is usually equal to the number of bits exposed by a single
510 * cell, and can be used in conjunction with mtd_pairing_info_to_wunit()
511 * to iterate over all pages of a given pair.
512 */
513int mtd_pairing_groups(struct mtd_info *mtd)
514{
Miquel Raynal46b58892020-01-14 10:09:52 +0100515 struct mtd_info *master = mtd_get_master(mtd);
516
517 if (!master->pairing || !master->pairing->ngroups)
Boris Brezillon477b0222015-11-16 15:53:13 +0100518 return 1;
519
Miquel Raynal46b58892020-01-14 10:09:52 +0100520 return master->pairing->ngroups;
Boris Brezillon477b0222015-11-16 15:53:13 +0100521}
522EXPORT_SYMBOL_GPL(mtd_pairing_groups);
523
Alban Bedelc4dfa252018-11-13 15:01:10 +0100524static int mtd_nvmem_reg_read(void *priv, unsigned int offset,
525 void *val, size_t bytes)
526{
527 struct mtd_info *mtd = priv;
528 size_t retlen;
529 int err;
530
531 err = mtd_read(mtd, offset, bytes, &retlen, val);
532 if (err && err != -EUCLEAN)
533 return err;
534
535 return retlen == bytes ? 0 : -EIO;
536}
537
538static int mtd_nvmem_add(struct mtd_info *mtd)
539{
Ansuel Smith658c4442021-03-12 07:28:19 +0100540 struct device_node *node = mtd_get_of_node(mtd);
Alban Bedelc4dfa252018-11-13 15:01:10 +0100541 struct nvmem_config config = {};
542
Aneesh Kumar K.V6e952682019-02-11 19:03:37 +0530543 config.id = -1;
Alban Bedelc4dfa252018-11-13 15:01:10 +0100544 config.dev = &mtd->dev;
Ricardo Ribalda Delgado7b01b722020-04-30 15:17:21 +0200545 config.name = dev_name(&mtd->dev);
Alban Bedelc4dfa252018-11-13 15:01:10 +0100546 config.owner = THIS_MODULE;
547 config.reg_read = mtd_nvmem_reg_read;
548 config.size = mtd->size;
549 config.word_size = 1;
550 config.stride = 1;
551 config.read_only = true;
552 config.root_only = true;
Ansuel Smith658c4442021-03-12 07:28:19 +0100553 config.no_of_node = !of_device_is_compatible(node, "nvmem-cells");
Alban Bedelc4dfa252018-11-13 15:01:10 +0100554 config.priv = mtd;
555
556 mtd->nvmem = nvmem_register(&config);
557 if (IS_ERR(mtd->nvmem)) {
558 /* Just ignore if there is no NVMEM support in the kernel */
Boris Brezillon19e16fb2019-01-02 15:36:53 +0100559 if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) {
Alban Bedelc4dfa252018-11-13 15:01:10 +0100560 mtd->nvmem = NULL;
561 } else {
562 dev_err(&mtd->dev, "Failed to register NVMEM device\n");
563 return PTR_ERR(mtd->nvmem);
564 }
565 }
566
567 return 0;
568}
569
Boris Brezillon477b0222015-11-16 15:53:13 +0100570/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * add_mtd_device - register an MTD device
572 * @mtd: pointer to new MTD device info structure
573 *
574 * Add a device to the list of MTD devices present in the system, and
575 * notify each currently active MTD 'user' of its arrival. Returns
Brian Norris57dd9902015-06-01 16:17:18 -0700576 * zero on success or non-zero on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578
579int add_mtd_device(struct mtd_info *mtd)
580{
Miquel Raynal46b58892020-01-14 10:09:52 +0100581 struct mtd_info *master = mtd_get_master(mtd);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000582 struct mtd_notifier *not;
583 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Brian Norrisbe0dbff2015-06-01 16:17:20 -0700585 /*
586 * May occur, for instance, on buggy drivers which call
587 * mtd_device_parse_register() multiple times on the same master MTD,
588 * especially with CONFIG_MTD_PARTITIONED_MASTER=y.
589 */
Jan Karafa060522017-04-12 12:24:37 +0200590 if (WARN_ONCE(mtd->dev.type, "MTD already registered\n"))
Brian Norrisbe0dbff2015-06-01 16:17:20 -0700591 return -EEXIST;
592
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400593 BUG_ON(mtd->writesize == 0);
Boris Brezillon33f45c42017-12-15 13:39:51 +0100594
Boris Brezillon2431c4f2018-12-20 15:13:20 +0100595 /*
596 * MTD drivers should implement ->_{write,read}() or
597 * ->_{write,read}_oob(), but not both.
598 */
599 if (WARN_ON((mtd->_write && mtd->_write_oob) ||
600 (mtd->_read && mtd->_read_oob)))
601 return -EINVAL;
602
Miquel Raynal46b58892020-01-14 10:09:52 +0100603 if (WARN_ON((!mtd->erasesize || !master->_erase) &&
Boris Brezillon33f45c42017-12-15 13:39:51 +0100604 !(mtd->flags & MTD_NO_ERASE)))
605 return -EINVAL;
606
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200607 /*
608 * MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the
609 * master is an MLC NAND and has a proper pairing scheme defined.
610 * We also reject masters that implement ->_writev() for now, because
611 * NAND controller drivers don't implement this hook, and adding the
612 * SLC -> MLC address/length conversion to this path is useless if we
613 * don't have a user.
614 */
615 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION &&
616 (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH ||
617 !master->pairing || master->_writev))
618 return -EINVAL;
619
Ingo Molnar48b19262006-03-31 02:29:41 -0800620 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Tejun Heo589e9c42013-02-27 17:04:33 -0800622 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
Brian Norris57dd9902015-06-01 16:17:18 -0700623 if (i < 0) {
624 error = i;
Ben Hutchingsb520e412010-01-29 20:59:42 +0000625 goto fail_locked;
Brian Norris57dd9902015-06-01 16:17:18 -0700626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Ben Hutchingsb520e412010-01-29 20:59:42 +0000628 mtd->index = i;
629 mtd->usecount = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000630
Mike Dunnd062d4e2012-04-25 12:06:08 -0700631 /* default value if not set by driver */
632 if (mtd->bitflip_threshold == 0)
633 mtd->bitflip_threshold = mtd->ecc_strength;
634
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200635 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
636 int ngroups = mtd_pairing_groups(master);
637
638 mtd->erasesize /= ngroups;
639 mtd->size = (u64)mtd_div_by_eb(mtd->size, master) *
640 mtd->erasesize;
641 }
642
Ben Hutchingsb520e412010-01-29 20:59:42 +0000643 if (is_power_of_2(mtd->erasesize))
644 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
645 else
646 mtd->erasesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000647
Ben Hutchingsb520e412010-01-29 20:59:42 +0000648 if (is_power_of_2(mtd->writesize))
649 mtd->writesize_shift = ffs(mtd->writesize) - 1;
650 else
651 mtd->writesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000652
Ben Hutchingsb520e412010-01-29 20:59:42 +0000653 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
654 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
Håvard Skinnemoen187ef152006-09-22 10:07:08 +0100655
Ben Hutchingsb520e412010-01-29 20:59:42 +0000656 /* Some chips always power up locked. Unlock them now */
Artem Bityutskiy38134562011-12-30 17:00:35 +0200657 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
658 error = mtd_unlock(mtd, 0, mtd->size);
659 if (error && error != -EOPNOTSUPP)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000660 printk(KERN_WARNING
661 "%s: unlock failed, writes may not work\n",
662 mtd->name);
Brian Norris57dd9902015-06-01 16:17:18 -0700663 /* Ignore unlock failures? */
664 error = 0;
Ben Hutchingsb520e412010-01-29 20:59:42 +0000665 }
David Brownell1f24b5a2009-03-26 00:42:41 -0700666
Ben Hutchingsb520e412010-01-29 20:59:42 +0000667 /* Caller should have set dev.parent to match the
Frans Klaver260e89a2015-06-10 22:38:15 +0200668 * physical device, if appropriate.
Ben Hutchingsb520e412010-01-29 20:59:42 +0000669 */
670 mtd->dev.type = &mtd_devtype;
671 mtd->dev.class = &mtd_class;
672 mtd->dev.devt = MTD_DEVT(i);
673 dev_set_name(&mtd->dev, "mtd%d", i);
674 dev_set_drvdata(&mtd->dev, mtd);
Brian Norris215a02f2015-11-11 16:26:04 -0800675 of_node_get(mtd_get_of_node(mtd));
Brian Norris57dd9902015-06-01 16:17:18 -0700676 error = device_register(&mtd->dev);
677 if (error)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000678 goto fail_added;
David Brownell1f24b5a2009-03-26 00:42:41 -0700679
Alban Bedelc4dfa252018-11-13 15:01:10 +0100680 /* Add the nvmem provider */
681 error = mtd_nvmem_add(mtd);
682 if (error)
683 goto fail_nvmem_add;
684
Zhuohao Lee1018c942019-07-01 00:07:10 +0800685 mtd_debugfs_populate(mtd);
Mario Rugieroe8e3edb2017-05-29 08:38:41 -0300686
Brian Norris5e472122014-07-21 19:06:47 -0700687 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
688 "mtd%dro", i);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000689
Brian Norris289c0522011-07-19 10:06:09 -0700690 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000691 /* No need to get a refcount on the module containing
692 the notifier, since we hold the mtd_table_mutex */
693 list_for_each_entry(not, &mtd_notifiers, list)
694 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000695
Ingo Molnar48b19262006-03-31 02:29:41 -0800696 mutex_unlock(&mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000697 /* We _know_ we aren't being removed, because
698 our caller is still holding us here. So none
699 of this try_ nonsense, and no bitching about it
700 either. :) */
701 __module_get(THIS_MODULE);
702 return 0;
703
Alban Bedelc4dfa252018-11-13 15:01:10 +0100704fail_nvmem_add:
705 device_unregister(&mtd->dev);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000706fail_added:
Brian Norris215a02f2015-11-11 16:26:04 -0800707 of_node_put(mtd_get_of_node(mtd));
Ben Hutchingsb520e412010-01-29 20:59:42 +0000708 idr_remove(&mtd_idr, i);
709fail_locked:
710 mutex_unlock(&mtd_table_mutex);
Brian Norris57dd9902015-06-01 16:17:18 -0700711 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/**
715 * del_mtd_device - unregister an MTD device
716 * @mtd: pointer to MTD device info structure
717 *
718 * Remove a device from the list of MTD devices present in the system,
719 * and notify each currently active MTD 'user' of its departure.
720 * Returns zero on success or 1 on failure, which currently will happen
721 * if the requested device does not appear to be present in the list.
722 */
723
Jamie Ileseea72d52011-05-23 10:23:42 +0100724int del_mtd_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 int ret;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200727 struct mtd_notifier *not;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000728
Ingo Molnar48b19262006-03-31 02:29:41 -0800729 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Mario Rugieroe8e3edb2017-05-29 08:38:41 -0300731 debugfs_remove_recursive(mtd->dbg.dfs_dir);
732
Ben Hutchingsb520e412010-01-29 20:59:42 +0000733 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ret = -ENODEV;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200735 goto out_error;
736 }
737
738 /* No need to get a refcount on the module containing
739 the notifier, since we hold the mtd_table_mutex */
740 list_for_each_entry(not, &mtd_notifiers, list)
741 not->remove(mtd);
742
743 if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000744 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 mtd->index, mtd->name, mtd->usecount);
746 ret = -EBUSY;
747 } else {
Alban Bedelc4dfa252018-11-13 15:01:10 +0100748 /* Try to remove the NVMEM provider */
749 if (mtd->nvmem)
750 nvmem_unregister(mtd->nvmem);
751
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700752 device_unregister(&mtd->dev);
753
Ben Hutchingsb520e412010-01-29 20:59:42 +0000754 idr_remove(&mtd_idr, mtd->index);
Brian Norris215a02f2015-11-11 16:26:04 -0800755 of_node_put(mtd_get_of_node(mtd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 module_put(THIS_MODULE);
758 ret = 0;
759 }
760
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200761out_error:
Ingo Molnar48b19262006-03-31 02:29:41 -0800762 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return ret;
764}
765
Brian Norris472b4442015-12-11 15:58:01 -0800766/*
767 * Set a few defaults based on the parent devices, if not provided by the
768 * driver
769 */
770static void mtd_set_dev_defaults(struct mtd_info *mtd)
771{
772 if (mtd->dev.parent) {
773 if (!mtd->owner && mtd->dev.parent->driver)
774 mtd->owner = mtd->dev.parent->driver->owner;
775 if (!mtd->name)
776 mtd->name = dev_name(mtd->dev.parent);
777 } else {
778 pr_debug("mtd device won't show a device symlink in sysfs\n");
779 }
Rafał Miłecki1186af42018-11-20 09:55:45 +0100780
Miquel Raynal46b58892020-01-14 10:09:52 +0100781 INIT_LIST_HEAD(&mtd->partitions);
782 mutex_init(&mtd->master.partitions_lock);
Alexander Sverdlin1ad55282021-02-17 22:18:45 +0100783 mutex_init(&mtd->master.chrdev_lock);
Brian Norris472b4442015-12-11 15:58:01 -0800784}
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700785
Michael Walle4b361cf2021-04-24 13:06:08 +0200786static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
787{
Dan Carpenterc3c8c052021-05-14 17:27:15 +0300788 struct otp_info *info;
Michael Walle4b361cf2021-04-24 13:06:08 +0200789 ssize_t size = 0;
790 unsigned int i;
791 size_t retlen;
792 int ret;
793
Dan Carpenterc3c8c052021-05-14 17:27:15 +0300794 info = kmalloc(PAGE_SIZE, GFP_KERNEL);
795 if (!info)
796 return -ENOMEM;
797
Michael Walle4b361cf2021-04-24 13:06:08 +0200798 if (is_user)
799 ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info);
800 else
801 ret = mtd_get_fact_prot_info(mtd, PAGE_SIZE, &retlen, info);
802 if (ret)
803 goto err;
804
Jon Hunterbc8e1572021-05-18 19:55:03 +0100805 for (i = 0; i < retlen / sizeof(*info); i++)
806 size += info[i].length;
Michael Walle4b361cf2021-04-24 13:06:08 +0200807
808 kfree(info);
809 return size;
810
811err:
812 kfree(info);
813 return ret;
814}
815
816static struct nvmem_device *mtd_otp_nvmem_register(struct mtd_info *mtd,
817 const char *compatible,
818 int size,
819 nvmem_reg_read_t reg_read)
820{
821 struct nvmem_device *nvmem = NULL;
822 struct nvmem_config config = {};
823 struct device_node *np;
824
825 /* DT binding is optional */
826 np = of_get_compatible_child(mtd->dev.of_node, compatible);
827
828 /* OTP nvmem will be registered on the physical device */
829 config.dev = mtd->dev.parent;
830 /* just reuse the compatible as name */
831 config.name = compatible;
832 config.id = NVMEM_DEVID_NONE;
833 config.owner = THIS_MODULE;
834 config.type = NVMEM_TYPE_OTP;
835 config.root_only = true;
836 config.reg_read = reg_read;
837 config.size = size;
838 config.of_node = np;
839 config.priv = mtd;
840
841 nvmem = nvmem_register(&config);
842 /* Just ignore if there is no NVMEM support in the kernel */
843 if (IS_ERR(nvmem) && PTR_ERR(nvmem) == -EOPNOTSUPP)
844 nvmem = NULL;
845
846 of_node_put(np);
847
848 return nvmem;
849}
850
851static int mtd_nvmem_user_otp_reg_read(void *priv, unsigned int offset,
852 void *val, size_t bytes)
853{
854 struct mtd_info *mtd = priv;
855 size_t retlen;
856 int ret;
857
858 ret = mtd_read_user_prot_reg(mtd, offset, bytes, &retlen, val);
859 if (ret)
860 return ret;
861
862 return retlen == bytes ? 0 : -EIO;
863}
864
865static int mtd_nvmem_fact_otp_reg_read(void *priv, unsigned int offset,
866 void *val, size_t bytes)
867{
868 struct mtd_info *mtd = priv;
869 size_t retlen;
870 int ret;
871
872 ret = mtd_read_fact_prot_reg(mtd, offset, bytes, &retlen, val);
873 if (ret)
874 return ret;
875
876 return retlen == bytes ? 0 : -EIO;
877}
878
879static int mtd_otp_nvmem_add(struct mtd_info *mtd)
880{
881 struct nvmem_device *nvmem;
882 ssize_t size;
883 int err;
884
885 if (mtd->_get_user_prot_info && mtd->_read_user_prot_reg) {
886 size = mtd_otp_size(mtd, true);
887 if (size < 0)
888 return size;
889
890 if (size > 0) {
891 nvmem = mtd_otp_nvmem_register(mtd, "user-otp", size,
892 mtd_nvmem_user_otp_reg_read);
893 if (IS_ERR(nvmem)) {
894 dev_err(&mtd->dev, "Failed to register OTP NVMEM device\n");
895 return PTR_ERR(nvmem);
896 }
897 mtd->otp_user_nvmem = nvmem;
898 }
899 }
900
901 if (mtd->_get_fact_prot_info && mtd->_read_fact_prot_reg) {
902 size = mtd_otp_size(mtd, false);
903 if (size < 0) {
904 err = size;
905 goto err;
906 }
907
908 if (size > 0) {
909 nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size,
910 mtd_nvmem_fact_otp_reg_read);
911 if (IS_ERR(nvmem)) {
912 dev_err(&mtd->dev, "Failed to register OTP NVMEM device\n");
913 err = PTR_ERR(nvmem);
914 goto err;
915 }
916 mtd->otp_factory_nvmem = nvmem;
917 }
918 }
919
920 return 0;
921
922err:
923 if (mtd->otp_user_nvmem)
924 nvmem_unregister(mtd->otp_user_nvmem);
925 return err;
926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/**
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300929 * mtd_device_parse_register - parse partitions and register an MTD device.
930 *
931 * @mtd: the MTD device to register
932 * @types: the list of MTD partition probes to try, see
933 * 'parse_mtd_partitions()' for more information
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400934 * @parser_data: MTD partition parser-specific data
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300935 * @parts: fallback partition information to register, if parsing fails;
936 * only valid if %nr_parts > %0
937 * @nr_parts: the number of partitions in parts, if zero then the full
938 * MTD device is registered if no partition info is found
939 *
940 * This function aggregates MTD partitions parsing (done by
941 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
942 * basically follows the most common pattern found in many MTD drivers:
943 *
Rafał Miłecki55a999a2018-03-27 15:36:47 +0200944 * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is
945 * registered first.
946 * * Then It tries to probe partitions on MTD device @mtd using parsers
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300947 * specified in @types (if @types is %NULL, then the default list of parsers
948 * is used, see 'parse_mtd_partitions()' for more information). If none are
949 * found this functions tries to fallback to information specified in
950 * @parts/@nr_parts.
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300951 * * If no partitions were found this function just registers the MTD device
952 * @mtd and exits.
953 *
954 * Returns zero in case of success and a negative error code in case of failure.
955 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200956int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400957 struct mtd_part_parser_data *parser_data,
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300958 const struct mtd_partition *parts,
959 int nr_parts)
960{
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700961 int ret;
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300962
Brian Norris472b4442015-12-11 15:58:01 -0800963 mtd_set_dev_defaults(mtd);
964
Rafał Miłecki2c77c572018-01-16 16:45:41 +0100965 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
966 ret = add_mtd_device(mtd);
967 if (ret)
968 return ret;
969 }
970
Rafał Miłecki0dbe4ea2018-01-16 16:45:42 +0100971 /* Prefer parsed partitions over driver-provided fallback */
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200972 ret = parse_mtd_partitions(mtd, types, parser_data);
Manivannan Sadhasivam08608ad2021-03-02 18:57:56 +0530973 if (ret == -EPROBE_DEFER)
974 goto out;
975
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200976 if (ret > 0)
977 ret = 0;
978 else if (nr_parts)
Rafał Miłecki0dbe4ea2018-01-16 16:45:42 +0100979 ret = add_mtd_partitions(mtd, parts, nr_parts);
980 else if (!device_is_registered(&mtd->dev))
981 ret = add_mtd_device(mtd);
982 else
983 ret = 0;
984
Brian Norris3e00ed02015-06-01 16:17:19 -0700985 if (ret)
986 goto out;
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300987
Niklas Cassele1dd8642015-02-01 02:08:50 +0100988 /*
989 * FIXME: some drivers unfortunately call this function more than once.
990 * So we have to check if we've already assigned the reboot notifier.
991 *
992 * Generally, we can make multiple calls work for most cases, but it
993 * does cause problems with parse_mtd_partitions() above (e.g.,
994 * cmdlineparts will register partitions more than once).
995 */
Brian Norrisf8479dd2015-11-03 17:01:53 -0800996 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call,
997 "MTD already registered\n");
Niklas Cassele1dd8642015-02-01 02:08:50 +0100998 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
Brian Norris3efe41b2014-11-26 01:01:08 -0800999 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
1000 register_reboot_notifier(&mtd->reboot_notifier);
1001 }
1002
Michael Walle4b361cf2021-04-24 13:06:08 +02001003 ret = mtd_otp_nvmem_add(mtd);
1004
Brian Norris3e00ed02015-06-01 16:17:19 -07001005out:
Rafał Miłecki2c77c572018-01-16 16:45:41 +01001006 if (ret && device_is_registered(&mtd->dev))
1007 del_mtd_device(mtd);
1008
Dan Ehrenberg727dc612015-04-02 15:15:10 -07001009 return ret;
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +03001010}
1011EXPORT_SYMBOL_GPL(mtd_device_parse_register);
1012
1013/**
Jamie Ilesf5671ab2011-05-23 17:15:46 +01001014 * mtd_device_unregister - unregister an existing MTD device.
1015 *
1016 * @master: the MTD device to unregister. This will unregister both the master
1017 * and any partitions if registered.
1018 */
1019int mtd_device_unregister(struct mtd_info *master)
1020{
1021 int err;
1022
Brian Norris3efe41b2014-11-26 01:01:08 -08001023 if (master->_reboot)
1024 unregister_reboot_notifier(&master->reboot_notifier);
1025
Michael Walle4b361cf2021-04-24 13:06:08 +02001026 if (master->otp_user_nvmem)
1027 nvmem_unregister(master->otp_user_nvmem);
1028
1029 if (master->otp_factory_nvmem)
1030 nvmem_unregister(master->otp_factory_nvmem);
1031
Jamie Ilesf5671ab2011-05-23 17:15:46 +01001032 err = del_mtd_partitions(master);
1033 if (err)
1034 return err;
1035
1036 if (!device_is_registered(&master->dev))
1037 return 0;
1038
1039 return del_mtd_device(master);
1040}
1041EXPORT_SYMBOL_GPL(mtd_device_unregister);
1042
1043/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * register_mtd_user - register a 'user' of MTD devices.
1045 * @new: pointer to notifier info structure
1046 *
1047 * Registers a pair of callbacks function to be called upon addition
1048 * or removal of MTD devices. Causes the 'add' callback to be immediately
1049 * invoked for each MTD device currently present in the system.
1050 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051void register_mtd_user (struct mtd_notifier *new)
1052{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001053 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Ingo Molnar48b19262006-03-31 02:29:41 -08001055 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 list_add(&new->list, &mtd_notifiers);
1058
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001059 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +00001060
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001061 mtd_for_each_device(mtd)
1062 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Ingo Molnar48b19262006-03-31 02:29:41 -08001064 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001066EXPORT_SYMBOL_GPL(register_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +00001069 * unregister_mtd_user - unregister a 'user' of MTD devices.
1070 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 *
1072 * Removes a callback function pair from the list of 'users' to be
1073 * notified upon addition or removal of MTD devices. Causes the
1074 * 'remove' callback to be immediately invoked for each MTD device
1075 * currently present in the system.
1076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077int unregister_mtd_user (struct mtd_notifier *old)
1078{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001079 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Ingo Molnar48b19262006-03-31 02:29:41 -08001081 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 module_put(THIS_MODULE);
1084
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001085 mtd_for_each_device(mtd)
1086 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +00001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -08001089 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return 0;
1091}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001092EXPORT_SYMBOL_GPL(unregister_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094/**
1095 * get_mtd_device - obtain a validated handle for an MTD device
1096 * @mtd: last known address of the required MTD device
1097 * @num: internal device number of the required MTD device
1098 *
1099 * Given a number and NULL address, return the num'th entry in the device
1100 * table, if any. Given an address and num == -1, search the device table
1101 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +03001102 * both, return the num'th driver only if its address matches. Return
1103 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
1106{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001107 struct mtd_info *ret = NULL, *other;
1108 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Ingo Molnar48b19262006-03-31 02:29:41 -08001110 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001113 mtd_for_each_device(other) {
1114 if (other == mtd) {
1115 ret = mtd;
1116 break;
1117 }
1118 }
Ben Hutchingsb520e412010-01-29 20:59:42 +00001119 } else if (num >= 0) {
1120 ret = idr_find(&mtd_idr, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (mtd && mtd != ret)
1122 ret = NULL;
1123 }
1124
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001125 if (!ret) {
1126 ret = ERR_PTR(err);
1127 goto out;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001130 err = __get_mtd_device(ret);
1131 if (err)
1132 ret = ERR_PTR(err);
1133out:
Ingo Molnar48b19262006-03-31 02:29:41 -08001134 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return ret;
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001136}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001137EXPORT_SYMBOL_GPL(get_mtd_device);
Artem Bityutskiy9c740342006-10-11 14:52:47 +03001138
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001139
1140int __get_mtd_device(struct mtd_info *mtd)
1141{
Miquel Raynal46b58892020-01-14 10:09:52 +01001142 struct mtd_info *master = mtd_get_master(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001143 int err;
1144
Miquel Raynal46b58892020-01-14 10:09:52 +01001145 if (!try_module_get(master->owner))
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001146 return -ENODEV;
1147
Miquel Raynal46b58892020-01-14 10:09:52 +01001148 if (master->_get_device) {
1149 err = master->_get_device(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001150
1151 if (err) {
Miquel Raynal46b58892020-01-14 10:09:52 +01001152 module_put(master->owner);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001153 return err;
1154 }
1155 }
Miquel Raynal46b58892020-01-14 10:09:52 +01001156
Richard Weinberger1ca71412020-12-06 21:22:20 +01001157 master->usecount++;
1158
Miquel Raynal46b58892020-01-14 10:09:52 +01001159 while (mtd->parent) {
1160 mtd->usecount++;
1161 mtd = mtd->parent;
1162 }
1163
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001166EXPORT_SYMBOL_GPL(__get_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Artem Bityutskiy77993082006-10-11 14:52:44 +03001168/**
1169 * get_mtd_device_nm - obtain a validated handle for an MTD device by
1170 * device name
1171 * @name: MTD device name to open
1172 *
1173 * This function returns MTD device description structure in case of
1174 * success and an error code in case of failure.
1175 */
Artem Bityutskiy77993082006-10-11 14:52:44 +03001176struct mtd_info *get_mtd_device_nm(const char *name)
1177{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001178 int err = -ENODEV;
1179 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +03001180
1181 mutex_lock(&mtd_table_mutex);
1182
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001183 mtd_for_each_device(other) {
1184 if (!strcmp(name, other->name)) {
1185 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +03001186 break;
1187 }
1188 }
1189
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001190 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +03001191 goto out_unlock;
1192
Wanlong Gao52534f22011-05-17 22:36:18 +08001193 err = __get_mtd_device(mtd);
1194 if (err)
Artem Bityutskiy77993082006-10-11 14:52:44 +03001195 goto out_unlock;
1196
Artem Bityutskiy77993082006-10-11 14:52:44 +03001197 mutex_unlock(&mtd_table_mutex);
1198 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001199
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001200out_unlock:
1201 mutex_unlock(&mtd_table_mutex);
1202 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +03001203}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001204EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Artem Bityutskiy77993082006-10-11 14:52:44 +03001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206void put_mtd_device(struct mtd_info *mtd)
1207{
Ingo Molnar48b19262006-03-31 02:29:41 -08001208 mutex_lock(&mtd_table_mutex);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001209 __put_mtd_device(mtd);
1210 mutex_unlock(&mtd_table_mutex);
1211
1212}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001213EXPORT_SYMBOL_GPL(put_mtd_device);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001214
1215void __put_mtd_device(struct mtd_info *mtd)
1216{
Miquel Raynal46b58892020-01-14 10:09:52 +01001217 struct mtd_info *master = mtd_get_master(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001218
Miquel Raynal46b58892020-01-14 10:09:52 +01001219 while (mtd->parent) {
1220 --mtd->usecount;
1221 BUG_ON(mtd->usecount < 0);
1222 mtd = mtd->parent;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Richard Weinberger1ca71412020-12-06 21:22:20 +01001225 master->usecount--;
1226
Miquel Raynal46b58892020-01-14 10:09:52 +01001227 if (master->_put_device)
1228 master->_put_device(master);
1229
1230 module_put(master->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001232EXPORT_SYMBOL_GPL(__put_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001234/*
Boris Brezillon884cfd92018-02-12 22:03:09 +01001235 * Erase is an synchronous operation. Device drivers are epected to return a
1236 * negative error code if the operation failed and update instr->fail_addr
1237 * to point the portion that was not properly erased.
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001238 */
1239int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1240{
Miquel Raynal46b58892020-01-14 10:09:52 +01001241 struct mtd_info *master = mtd_get_master(mtd);
1242 u64 mst_ofs = mtd_get_master_ofs(mtd, 0);
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001243 struct erase_info adjinstr;
Miquel Raynal46b58892020-01-14 10:09:52 +01001244 int ret;
1245
Boris Brezillonc585da92018-02-12 22:03:07 +01001246 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001247 adjinstr = *instr;
Boris Brezillonc585da92018-02-12 22:03:07 +01001248
Miquel Raynal46b58892020-01-14 10:09:52 +01001249 if (!mtd->erasesize || !master->_erase)
Boris Brezillone6e620f2018-01-22 10:38:01 +01001250 return -ENOTSUPP;
1251
Brian Norris0c2b4e22014-07-21 19:06:27 -07001252 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001253 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001254 if (!(mtd->flags & MTD_WRITEABLE))
1255 return -EROFS;
Boris Brezillone6e620f2018-01-22 10:38:01 +01001256
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01001257 if (!instr->len)
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001258 return 0;
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01001259
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001260 ledtrig_mtd_activity();
Miquel Raynal46b58892020-01-14 10:09:52 +01001261
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001262 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
1263 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) *
1264 master->erasesize;
1265 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) *
1266 master->erasesize) -
1267 adjinstr.addr;
1268 }
Miquel Raynal46b58892020-01-14 10:09:52 +01001269
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001270 adjinstr.addr += mst_ofs;
1271
1272 ret = master->_erase(master, &adjinstr);
1273
1274 if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) {
1275 instr->fail_addr = adjinstr.fail_addr - mst_ofs;
1276 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
1277 instr->fail_addr = mtd_div_by_eb(instr->fail_addr,
1278 master);
1279 instr->fail_addr *= mtd->erasesize;
1280 }
1281 }
1282
Miquel Raynal46b58892020-01-14 10:09:52 +01001283 return ret;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001284}
1285EXPORT_SYMBOL_GPL(mtd_erase);
1286
1287/*
1288 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
1289 */
1290int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1291 void **virt, resource_size_t *phys)
1292{
Miquel Raynal46b58892020-01-14 10:09:52 +01001293 struct mtd_info *master = mtd_get_master(mtd);
1294
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001295 *retlen = 0;
Artem Bityutskiy0dd52352012-02-08 15:13:26 +02001296 *virt = NULL;
1297 if (phys)
1298 *phys = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001299 if (!master->_point)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001300 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001301 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001302 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001303 if (!len)
1304 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001305
1306 from = mtd_get_master_ofs(mtd, from);
1307 return master->_point(master, from, len, retlen, virt, phys);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001308}
1309EXPORT_SYMBOL_GPL(mtd_point);
1310
1311/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
1312int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
1313{
Miquel Raynal46b58892020-01-14 10:09:52 +01001314 struct mtd_info *master = mtd_get_master(mtd);
1315
1316 if (!master->_unpoint)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001317 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001318 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001319 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001320 if (!len)
1321 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001322 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001323}
1324EXPORT_SYMBOL_GPL(mtd_unpoint);
1325
1326/*
1327 * Allow NOMMU mmap() to directly map the device (if not NULL)
1328 * - return the address to which the offset maps
1329 * - return -ENOSYS to indicate refusal to do the mapping
1330 */
1331unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
1332 unsigned long offset, unsigned long flags)
1333{
Nicolas Pitre9eaa9032017-10-30 14:48:32 -04001334 size_t retlen;
1335 void *virt;
1336 int ret;
1337
1338 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL);
1339 if (ret)
1340 return ret;
1341 if (retlen != len) {
1342 mtd_unpoint(mtd, offset, retlen);
1343 return -ENOSYS;
1344 }
1345 return (unsigned long)virt;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001346}
1347EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
1348
Miquel Raynal46b58892020-01-14 10:09:52 +01001349static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master,
1350 const struct mtd_ecc_stats *old_stats)
1351{
1352 struct mtd_ecc_stats diff;
1353
1354 if (master == mtd)
1355 return;
1356
1357 diff = master->ecc_stats;
1358 diff.failed -= old_stats->failed;
1359 diff.corrected -= old_stats->corrected;
1360
1361 while (mtd->parent) {
1362 mtd->ecc_stats.failed += diff.failed;
1363 mtd->ecc_stats.corrected += diff.corrected;
1364 mtd = mtd->parent;
1365 }
1366}
1367
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001368int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1369 u_char *buf)
1370{
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001371 struct mtd_oob_ops ops = {
1372 .len = len,
1373 .datbuf = buf,
1374 };
1375 int ret;
Mike Dunnedbc45402012-04-25 12:06:11 -07001376
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001377 ret = mtd_read_oob(mtd, from, &ops);
1378 *retlen = ops.retlen;
Boris Brezillon24ff1292018-01-09 09:50:34 +01001379
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001380 return ret;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001381}
1382EXPORT_SYMBOL_GPL(mtd_read);
1383
1384int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1385 const u_char *buf)
1386{
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001387 struct mtd_oob_ops ops = {
1388 .len = len,
1389 .datbuf = (u8 *)buf,
1390 };
1391 int ret;
Boris Brezillon24ff1292018-01-09 09:50:34 +01001392
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001393 ret = mtd_write_oob(mtd, to, &ops);
1394 *retlen = ops.retlen;
Boris Brezillon24ff1292018-01-09 09:50:34 +01001395
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001396 return ret;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001397}
1398EXPORT_SYMBOL_GPL(mtd_write);
1399
1400/*
1401 * In blackbox flight recorder like scenarios we want to make successful writes
1402 * in interrupt context. panic_write() is only intended to be called when its
1403 * known the kernel is about to panic and we need the write to succeed. Since
1404 * the kernel is not going to be running for much longer, this function can
1405 * break locks and delay to ensure the write succeeds (but not sleep).
1406 */
1407int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1408 const u_char *buf)
1409{
Miquel Raynal46b58892020-01-14 10:09:52 +01001410 struct mtd_info *master = mtd_get_master(mtd);
1411
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001412 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001413 if (!master->_panic_write)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001414 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001415 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001416 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001417 if (!(mtd->flags & MTD_WRITEABLE))
1418 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001419 if (!len)
1420 return 0;
Kamal Dasu630e8d52020-06-15 11:51:34 -04001421 if (!master->oops_panic_write)
1422 master->oops_panic_write = true;
Kamal Dasu9f897bf2019-05-16 12:41:46 -04001423
Miquel Raynal46b58892020-01-14 10:09:52 +01001424 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len,
1425 retlen, buf);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001426}
1427EXPORT_SYMBOL_GPL(mtd_panic_write);
1428
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001429static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1430 struct mtd_oob_ops *ops)
1431{
1432 /*
1433 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1434 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1435 * this case.
1436 */
1437 if (!ops->datbuf)
1438 ops->len = 0;
1439
1440 if (!ops->oobbuf)
1441 ops->ooblen = 0;
1442
Miquel Raynald82c3682017-12-18 08:26:28 +01001443 if (offs < 0 || offs + ops->len > mtd->size)
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001444 return -EINVAL;
1445
1446 if (ops->ooblen) {
Miquel Raynal89f706d2018-11-18 21:18:31 +01001447 size_t maxooblen;
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001448
1449 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1450 return -EINVAL;
1451
Miquel Raynal89f706d2018-11-18 21:18:31 +01001452 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1453 mtd_div_by_ws(offs, mtd)) *
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001454 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1455 if (ops->ooblen > maxooblen)
1456 return -EINVAL;
1457 }
1458
1459 return 0;
1460}
1461
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001462static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from,
1463 struct mtd_oob_ops *ops)
1464{
1465 struct mtd_info *master = mtd_get_master(mtd);
1466 int ret;
1467
1468 from = mtd_get_master_ofs(mtd, from);
1469 if (master->_read_oob)
1470 ret = master->_read_oob(master, from, ops);
1471 else
1472 ret = master->_read(master, from, ops->len, &ops->retlen,
1473 ops->datbuf);
1474
1475 return ret;
1476}
1477
1478static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to,
1479 struct mtd_oob_ops *ops)
1480{
1481 struct mtd_info *master = mtd_get_master(mtd);
1482 int ret;
1483
1484 to = mtd_get_master_ofs(mtd, to);
1485 if (master->_write_oob)
1486 ret = master->_write_oob(master, to, ops);
1487 else
1488 ret = master->_write(master, to, ops->len, &ops->retlen,
1489 ops->datbuf);
1490
1491 return ret;
1492}
1493
1494static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read,
1495 struct mtd_oob_ops *ops)
1496{
1497 struct mtd_info *master = mtd_get_master(mtd);
1498 int ngroups = mtd_pairing_groups(master);
1499 int npairs = mtd_wunit_per_eb(master) / ngroups;
1500 struct mtd_oob_ops adjops = *ops;
1501 unsigned int wunit, oobavail;
1502 struct mtd_pairing_info info;
1503 int max_bitflips = 0;
1504 u32 ebofs, pageofs;
1505 loff_t base, pos;
1506
1507 ebofs = mtd_mod_by_eb(start, mtd);
1508 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize;
1509 info.group = 0;
1510 info.pair = mtd_div_by_ws(ebofs, mtd);
1511 pageofs = mtd_mod_by_ws(ebofs, mtd);
1512 oobavail = mtd_oobavail(mtd, ops);
1513
1514 while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
1515 int ret;
1516
1517 if (info.pair >= npairs) {
1518 info.pair = 0;
1519 base += master->erasesize;
1520 }
1521
1522 wunit = mtd_pairing_info_to_wunit(master, &info);
1523 pos = mtd_wunit_to_offset(mtd, base, wunit);
1524
1525 adjops.len = ops->len - ops->retlen;
1526 if (adjops.len > mtd->writesize - pageofs)
1527 adjops.len = mtd->writesize - pageofs;
1528
1529 adjops.ooblen = ops->ooblen - ops->oobretlen;
1530 if (adjops.ooblen > oobavail - adjops.ooboffs)
1531 adjops.ooblen = oobavail - adjops.ooboffs;
1532
1533 if (read) {
1534 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops);
1535 if (ret > 0)
1536 max_bitflips = max(max_bitflips, ret);
1537 } else {
1538 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops);
1539 }
1540
1541 if (ret < 0)
1542 return ret;
1543
1544 max_bitflips = max(max_bitflips, ret);
1545 ops->retlen += adjops.retlen;
1546 ops->oobretlen += adjops.oobretlen;
1547 adjops.datbuf += adjops.retlen;
1548 adjops.oobbuf += adjops.oobretlen;
1549 adjops.ooboffs = 0;
1550 pageofs = 0;
1551 info.pair++;
1552 }
1553
1554 return max_bitflips;
1555}
1556
Brian Norrisd2d48482012-06-22 16:35:38 -07001557int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1558{
Miquel Raynal46b58892020-01-14 10:09:52 +01001559 struct mtd_info *master = mtd_get_master(mtd);
1560 struct mtd_ecc_stats old_stats = master->ecc_stats;
Brian Norrise47f6852012-06-22 16:35:39 -07001561 int ret_code;
Miquel Raynal46b58892020-01-14 10:09:52 +01001562
Brian Norrisd2d48482012-06-22 16:35:38 -07001563 ops->retlen = ops->oobretlen = 0;
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001564
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001565 ret_code = mtd_check_oob_ops(mtd, from, ops);
1566 if (ret_code)
1567 return ret_code;
1568
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001569 ledtrig_mtd_activity();
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001570
1571 /* Check the validity of a potential fallback on mtd->_read */
Miquel Raynal46b58892020-01-14 10:09:52 +01001572 if (!master->_read_oob && (!master->_read || ops->oobbuf))
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001573 return -EOPNOTSUPP;
1574
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001575 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
1576 ret_code = mtd_io_emulated_slc(mtd, from, true, ops);
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001577 else
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001578 ret_code = mtd_read_oob_std(mtd, from, ops);
Miquel Raynal46b58892020-01-14 10:09:52 +01001579
1580 mtd_update_ecc_stats(mtd, master, &old_stats);
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001581
Brian Norrise47f6852012-06-22 16:35:39 -07001582 /*
1583 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1584 * similar to mtd->_read(), returning a non-negative integer
1585 * representing max bitflips. In other cases, mtd->_read_oob() may
1586 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1587 */
Brian Norrise47f6852012-06-22 16:35:39 -07001588 if (unlikely(ret_code < 0))
1589 return ret_code;
1590 if (mtd->ecc_strength == 0)
1591 return 0; /* device lacks ecc */
1592 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Brian Norrisd2d48482012-06-22 16:35:38 -07001593}
1594EXPORT_SYMBOL_GPL(mtd_read_oob);
1595
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001596int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1597 struct mtd_oob_ops *ops)
1598{
Miquel Raynal46b58892020-01-14 10:09:52 +01001599 struct mtd_info *master = mtd_get_master(mtd);
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001600 int ret;
1601
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001602 ops->retlen = ops->oobretlen = 0;
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001603
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001604 if (!(mtd->flags & MTD_WRITEABLE))
1605 return -EROFS;
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001606
1607 ret = mtd_check_oob_ops(mtd, to, ops);
1608 if (ret)
1609 return ret;
1610
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001611 ledtrig_mtd_activity();
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001612
1613 /* Check the validity of a potential fallback on mtd->_write */
Miquel Raynal46b58892020-01-14 10:09:52 +01001614 if (!master->_write_oob && (!master->_write || ops->oobbuf))
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001615 return -EOPNOTSUPP;
1616
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001617 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
1618 return mtd_io_emulated_slc(mtd, to, false, ops);
Miquel Raynal46b58892020-01-14 10:09:52 +01001619
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001620 return mtd_write_oob_std(mtd, to, ops);
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001621}
1622EXPORT_SYMBOL_GPL(mtd_write_oob);
1623
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001624/**
1625 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1626 * @mtd: MTD device structure
1627 * @section: ECC section. Depending on the layout you may have all the ECC
1628 * bytes stored in a single contiguous section, or one section
1629 * per ECC chunk (and sometime several sections for a single ECC
1630 * ECC chunk)
1631 * @oobecc: OOB region struct filled with the appropriate ECC position
1632 * information
1633 *
Masahiro Yamada7da0fff2016-12-14 09:31:01 +09001634 * This function returns ECC section information in the OOB area. If you want
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001635 * to get all the ECC bytes information, then you should call
1636 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1637 *
1638 * Returns zero on success, a negative error code otherwise.
1639 */
1640int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1641 struct mtd_oob_region *oobecc)
1642{
Miquel Raynal46b58892020-01-14 10:09:52 +01001643 struct mtd_info *master = mtd_get_master(mtd);
1644
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001645 memset(oobecc, 0, sizeof(*oobecc));
1646
Miquel Raynal46b58892020-01-14 10:09:52 +01001647 if (!master || section < 0)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001648 return -EINVAL;
1649
Miquel Raynal46b58892020-01-14 10:09:52 +01001650 if (!master->ooblayout || !master->ooblayout->ecc)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001651 return -ENOTSUPP;
1652
Miquel Raynal46b58892020-01-14 10:09:52 +01001653 return master->ooblayout->ecc(master, section, oobecc);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001654}
1655EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1656
1657/**
1658 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1659 * section
1660 * @mtd: MTD device structure
1661 * @section: Free section you are interested in. Depending on the layout
1662 * you may have all the free bytes stored in a single contiguous
1663 * section, or one section per ECC chunk plus an extra section
1664 * for the remaining bytes (or other funky layout).
1665 * @oobfree: OOB region struct filled with the appropriate free position
1666 * information
1667 *
Masahiro Yamada7da0fff2016-12-14 09:31:01 +09001668 * This function returns free bytes position in the OOB area. If you want
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001669 * to get all the free bytes information, then you should call
1670 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1671 *
1672 * Returns zero on success, a negative error code otherwise.
1673 */
1674int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1675 struct mtd_oob_region *oobfree)
1676{
Miquel Raynal46b58892020-01-14 10:09:52 +01001677 struct mtd_info *master = mtd_get_master(mtd);
1678
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001679 memset(oobfree, 0, sizeof(*oobfree));
1680
Miquel Raynal46b58892020-01-14 10:09:52 +01001681 if (!master || section < 0)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001682 return -EINVAL;
1683
Miquel Raynal46b58892020-01-14 10:09:52 +01001684 if (!master->ooblayout || !master->ooblayout->free)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001685 return -ENOTSUPP;
1686
Miquel Raynal46b58892020-01-14 10:09:52 +01001687 return master->ooblayout->free(master, section, oobfree);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001688}
1689EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1690
1691/**
1692 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1693 * @mtd: mtd info structure
1694 * @byte: the byte we are searching for
1695 * @sectionp: pointer where the section id will be stored
1696 * @oobregion: used to retrieve the ECC position
1697 * @iter: iterator function. Should be either mtd_ooblayout_free or
1698 * mtd_ooblayout_ecc depending on the region type you're searching for
1699 *
Masahiro Yamada7da0fff2016-12-14 09:31:01 +09001700 * This function returns the section id and oobregion information of a
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001701 * specific byte. For example, say you want to know where the 4th ECC byte is
1702 * stored, you'll use:
1703 *
1704 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1705 *
1706 * Returns zero on success, a negative error code otherwise.
1707 */
1708static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1709 int *sectionp, struct mtd_oob_region *oobregion,
1710 int (*iter)(struct mtd_info *,
1711 int section,
1712 struct mtd_oob_region *oobregion))
1713{
1714 int pos = 0, ret, section = 0;
1715
1716 memset(oobregion, 0, sizeof(*oobregion));
1717
1718 while (1) {
1719 ret = iter(mtd, section, oobregion);
1720 if (ret)
1721 return ret;
1722
1723 if (pos + oobregion->length > byte)
1724 break;
1725
1726 pos += oobregion->length;
1727 section++;
1728 }
1729
1730 /*
1731 * Adjust region info to make it start at the beginning at the
1732 * 'start' ECC byte.
1733 */
1734 oobregion->offset += byte - pos;
1735 oobregion->length -= byte - pos;
1736 *sectionp = section;
1737
1738 return 0;
1739}
1740
1741/**
1742 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1743 * ECC byte
1744 * @mtd: mtd info structure
1745 * @eccbyte: the byte we are searching for
Lee Jones6361f532020-11-09 18:21:48 +00001746 * @section: pointer where the section id will be stored
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001747 * @oobregion: OOB region information
1748 *
1749 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1750 * byte.
1751 *
1752 * Returns zero on success, a negative error code otherwise.
1753 */
1754int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1755 int *section,
1756 struct mtd_oob_region *oobregion)
1757{
1758 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1759 mtd_ooblayout_ecc);
1760}
1761EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1762
1763/**
1764 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1765 * @mtd: mtd info structure
1766 * @buf: destination buffer to store OOB bytes
1767 * @oobbuf: OOB buffer
1768 * @start: first byte to retrieve
1769 * @nbytes: number of bytes to retrieve
1770 * @iter: section iterator
1771 *
1772 * Extract bytes attached to a specific category (ECC or free)
1773 * from the OOB buffer and copy them into buf.
1774 *
1775 * Returns zero on success, a negative error code otherwise.
1776 */
1777static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1778 const u8 *oobbuf, int start, int nbytes,
1779 int (*iter)(struct mtd_info *,
1780 int section,
1781 struct mtd_oob_region *oobregion))
1782{
Masahiro Yamada8e8fd4d2016-11-09 11:08:08 +09001783 struct mtd_oob_region oobregion;
1784 int section, ret;
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001785
1786 ret = mtd_ooblayout_find_region(mtd, start, &section,
1787 &oobregion, iter);
1788
1789 while (!ret) {
1790 int cnt;
1791
Masahiro Yamada7c295ef2016-11-09 11:08:09 +09001792 cnt = min_t(int, nbytes, oobregion.length);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001793 memcpy(buf, oobbuf + oobregion.offset, cnt);
1794 buf += cnt;
1795 nbytes -= cnt;
1796
1797 if (!nbytes)
1798 break;
1799
1800 ret = iter(mtd, ++section, &oobregion);
1801 }
1802
1803 return ret;
1804}
1805
1806/**
1807 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1808 * @mtd: mtd info structure
1809 * @buf: source buffer to get OOB bytes from
1810 * @oobbuf: OOB buffer
1811 * @start: first OOB byte to set
1812 * @nbytes: number of OOB bytes to set
1813 * @iter: section iterator
1814 *
1815 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1816 * is selected by passing the appropriate iterator.
1817 *
1818 * Returns zero on success, a negative error code otherwise.
1819 */
1820static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1821 u8 *oobbuf, int start, int nbytes,
1822 int (*iter)(struct mtd_info *,
1823 int section,
1824 struct mtd_oob_region *oobregion))
1825{
Masahiro Yamada8e8fd4d2016-11-09 11:08:08 +09001826 struct mtd_oob_region oobregion;
1827 int section, ret;
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001828
1829 ret = mtd_ooblayout_find_region(mtd, start, &section,
1830 &oobregion, iter);
1831
1832 while (!ret) {
1833 int cnt;
1834
Masahiro Yamada7c295ef2016-11-09 11:08:09 +09001835 cnt = min_t(int, nbytes, oobregion.length);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001836 memcpy(oobbuf + oobregion.offset, buf, cnt);
1837 buf += cnt;
1838 nbytes -= cnt;
1839
1840 if (!nbytes)
1841 break;
1842
1843 ret = iter(mtd, ++section, &oobregion);
1844 }
1845
1846 return ret;
1847}
1848
1849/**
1850 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1851 * @mtd: mtd info structure
1852 * @iter: category iterator
1853 *
1854 * Count the number of bytes in a given category.
1855 *
1856 * Returns a positive value on success, a negative error code otherwise.
1857 */
1858static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1859 int (*iter)(struct mtd_info *,
1860 int section,
1861 struct mtd_oob_region *oobregion))
1862{
Masahiro Yamada4d6aecf2016-11-09 11:08:10 +09001863 struct mtd_oob_region oobregion;
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001864 int section = 0, ret, nbytes = 0;
1865
1866 while (1) {
1867 ret = iter(mtd, section++, &oobregion);
1868 if (ret) {
1869 if (ret == -ERANGE)
1870 ret = nbytes;
1871 break;
1872 }
1873
1874 nbytes += oobregion.length;
1875 }
1876
1877 return ret;
1878}
1879
1880/**
1881 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1882 * @mtd: mtd info structure
1883 * @eccbuf: destination buffer to store ECC bytes
1884 * @oobbuf: OOB buffer
1885 * @start: first ECC byte to retrieve
1886 * @nbytes: number of ECC bytes to retrieve
1887 *
1888 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1889 *
1890 * Returns zero on success, a negative error code otherwise.
1891 */
1892int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1893 const u8 *oobbuf, int start, int nbytes)
1894{
1895 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1896 mtd_ooblayout_ecc);
1897}
1898EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1899
1900/**
1901 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1902 * @mtd: mtd info structure
1903 * @eccbuf: source buffer to get ECC bytes from
1904 * @oobbuf: OOB buffer
1905 * @start: first ECC byte to set
1906 * @nbytes: number of ECC bytes to set
1907 *
1908 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1909 *
1910 * Returns zero on success, a negative error code otherwise.
1911 */
1912int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1913 u8 *oobbuf, int start, int nbytes)
1914{
1915 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1916 mtd_ooblayout_ecc);
1917}
1918EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1919
1920/**
1921 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1922 * @mtd: mtd info structure
1923 * @databuf: destination buffer to store ECC bytes
1924 * @oobbuf: OOB buffer
1925 * @start: first ECC byte to retrieve
1926 * @nbytes: number of ECC bytes to retrieve
1927 *
1928 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1929 *
1930 * Returns zero on success, a negative error code otherwise.
1931 */
1932int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1933 const u8 *oobbuf, int start, int nbytes)
1934{
1935 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1936 mtd_ooblayout_free);
1937}
1938EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1939
1940/**
Xiaolei Lic77a9312018-03-29 09:34:58 +08001941 * mtd_ooblayout_set_databytes - set data bytes into the oob buffer
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001942 * @mtd: mtd info structure
Xiaolei Lic77a9312018-03-29 09:34:58 +08001943 * @databuf: source buffer to get data bytes from
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001944 * @oobbuf: OOB buffer
1945 * @start: first ECC byte to set
1946 * @nbytes: number of ECC bytes to set
1947 *
Miquel Raynal519494a2020-05-26 21:56:13 +02001948 * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes.
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001949 *
1950 * Returns zero on success, a negative error code otherwise.
1951 */
1952int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1953 u8 *oobbuf, int start, int nbytes)
1954{
1955 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1956 mtd_ooblayout_free);
1957}
1958EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1959
1960/**
1961 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1962 * @mtd: mtd info structure
1963 *
1964 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1965 *
1966 * Returns zero on success, a negative error code otherwise.
1967 */
1968int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1969{
1970 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1971}
1972EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1973
1974/**
Xiaolei Lic77a9312018-03-29 09:34:58 +08001975 * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001976 * @mtd: mtd info structure
1977 *
1978 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1979 *
1980 * Returns zero on success, a negative error code otherwise.
1981 */
1982int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1983{
1984 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1985}
1986EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1987
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001988/*
1989 * Method to access the protection register area, present in some flash
1990 * devices. The user data is one time programmable but the factory data is read
1991 * only.
1992 */
Christian Riesch4b78fc42f2014-01-28 09:29:44 +01001993int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1994 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001995{
Miquel Raynal46b58892020-01-14 10:09:52 +01001996 struct mtd_info *master = mtd_get_master(mtd);
1997
1998 if (!master->_get_fact_prot_info)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001999 return -EOPNOTSUPP;
2000 if (!len)
2001 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002002 return master->_get_fact_prot_info(master, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002003}
2004EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
2005
2006int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
2007 size_t *retlen, u_char *buf)
2008{
Miquel Raynal46b58892020-01-14 10:09:52 +01002009 struct mtd_info *master = mtd_get_master(mtd);
2010
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002011 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002012 if (!master->_read_fact_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002013 return -EOPNOTSUPP;
2014 if (!len)
2015 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002016 return master->_read_fact_prot_reg(master, from, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002017}
2018EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
2019
Christian Riesch4b78fc42f2014-01-28 09:29:44 +01002020int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
2021 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002022{
Miquel Raynal46b58892020-01-14 10:09:52 +01002023 struct mtd_info *master = mtd_get_master(mtd);
2024
2025 if (!master->_get_user_prot_info)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002026 return -EOPNOTSUPP;
2027 if (!len)
2028 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002029 return master->_get_user_prot_info(master, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002030}
2031EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
2032
2033int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
2034 size_t *retlen, u_char *buf)
2035{
Miquel Raynal46b58892020-01-14 10:09:52 +01002036 struct mtd_info *master = mtd_get_master(mtd);
2037
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002038 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002039 if (!master->_read_user_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002040 return -EOPNOTSUPP;
2041 if (!len)
2042 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002043 return master->_read_user_prot_reg(master, from, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002044}
2045EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
2046
2047int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
Tudor Ambarus1df1fc82021-04-03 09:09:31 +03002048 size_t *retlen, const u_char *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002049{
Miquel Raynal46b58892020-01-14 10:09:52 +01002050 struct mtd_info *master = mtd_get_master(mtd);
Christian Riesch9a78bc82014-03-06 12:42:37 +01002051 int ret;
2052
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002053 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002054 if (!master->_write_user_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002055 return -EOPNOTSUPP;
2056 if (!len)
2057 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002058 ret = master->_write_user_prot_reg(master, to, len, retlen, buf);
Christian Riesch9a78bc82014-03-06 12:42:37 +01002059 if (ret)
2060 return ret;
2061
2062 /*
2063 * If no data could be written at all, we are out of memory and
2064 * must return -ENOSPC.
2065 */
2066 return (*retlen) ? 0 : -ENOSPC;
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002067}
2068EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
2069
2070int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
2071{
Miquel Raynal46b58892020-01-14 10:09:52 +01002072 struct mtd_info *master = mtd_get_master(mtd);
2073
2074 if (!master->_lock_user_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002075 return -EOPNOTSUPP;
2076 if (!len)
2077 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002078 return master->_lock_user_prot_reg(master, from, len);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002079}
2080EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
2081
Michael Wallee3c1f1c2021-03-03 21:18:19 +01002082int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
2083{
2084 struct mtd_info *master = mtd_get_master(mtd);
2085
2086 if (!master->_erase_user_prot_reg)
2087 return -EOPNOTSUPP;
2088 if (!len)
2089 return 0;
2090 return master->_erase_user_prot_reg(master, from, len);
2091}
2092EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
2093
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002094/* Chip-supported device locking */
2095int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2096{
Miquel Raynal46b58892020-01-14 10:09:52 +01002097 struct mtd_info *master = mtd_get_master(mtd);
2098
2099 if (!master->_lock)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002100 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002101 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002102 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02002103 if (!len)
2104 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002105
2106 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2107 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2108 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2109 }
2110
Miquel Raynal46b58892020-01-14 10:09:52 +01002111 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002112}
2113EXPORT_SYMBOL_GPL(mtd_lock);
2114
2115int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2116{
Miquel Raynal46b58892020-01-14 10:09:52 +01002117 struct mtd_info *master = mtd_get_master(mtd);
2118
2119 if (!master->_unlock)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002120 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002121 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002122 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02002123 if (!len)
2124 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002125
2126 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2127 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2128 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2129 }
2130
Miquel Raynal46b58892020-01-14 10:09:52 +01002131 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002132}
2133EXPORT_SYMBOL_GPL(mtd_unlock);
2134
2135int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2136{
Miquel Raynal46b58892020-01-14 10:09:52 +01002137 struct mtd_info *master = mtd_get_master(mtd);
2138
2139 if (!master->_is_locked)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002140 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002141 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002142 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02002143 if (!len)
2144 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002145
2146 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2147 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2148 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2149 }
2150
Miquel Raynal46b58892020-01-14 10:09:52 +01002151 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002152}
2153EXPORT_SYMBOL_GPL(mtd_is_locked);
2154
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002155int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002156{
Miquel Raynal46b58892020-01-14 10:09:52 +01002157 struct mtd_info *master = mtd_get_master(mtd);
2158
Brian Norris0c2b4e22014-07-21 19:06:27 -07002159 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002160 return -EINVAL;
Miquel Raynal46b58892020-01-14 10:09:52 +01002161 if (!master->_block_isreserved)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002162 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002163
2164 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2165 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2166
Miquel Raynal46b58892020-01-14 10:09:52 +01002167 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs));
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002168}
2169EXPORT_SYMBOL_GPL(mtd_block_isreserved);
2170
2171int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
2172{
Miquel Raynal46b58892020-01-14 10:09:52 +01002173 struct mtd_info *master = mtd_get_master(mtd);
2174
Brian Norris0c2b4e22014-07-21 19:06:27 -07002175 if (ofs < 0 || ofs >= mtd->size)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002176 return -EINVAL;
Miquel Raynal46b58892020-01-14 10:09:52 +01002177 if (!master->_block_isbad)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002178 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002179
2180 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2181 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2182
Miquel Raynal46b58892020-01-14 10:09:52 +01002183 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs));
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002184}
2185EXPORT_SYMBOL_GPL(mtd_block_isbad);
2186
2187int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
2188{
Miquel Raynal46b58892020-01-14 10:09:52 +01002189 struct mtd_info *master = mtd_get_master(mtd);
2190 int ret;
2191
2192 if (!master->_block_markbad)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002193 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002194 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002195 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02002196 if (!(mtd->flags & MTD_WRITEABLE))
2197 return -EROFS;
Miquel Raynal46b58892020-01-14 10:09:52 +01002198
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002199 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2200 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2201
Miquel Raynal46b58892020-01-14 10:09:52 +01002202 ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs));
2203 if (ret)
2204 return ret;
2205
2206 while (mtd->parent) {
2207 mtd->ecc_stats.badblocks++;
2208 mtd = mtd->parent;
2209 }
2210
2211 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002212}
2213EXPORT_SYMBOL_GPL(mtd_block_markbad);
2214
2215/*
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002216 * default_mtd_writev - the default writev method
2217 * @mtd: mtd device description object pointer
2218 * @vecs: the vectors to write
2219 * @count: count of vectors in @vecs
2220 * @to: the MTD device offset to write to
2221 * @retlen: on exit contains the count of bytes written to the MTD device.
2222 *
2223 * This function returns zero in case of success and a negative error code in
2224 * case of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 */
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002226static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
2227 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 unsigned long i;
2230 size_t totlen = 0, thislen;
2231 int ret = 0;
2232
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002233 for (i = 0; i < count; i++) {
2234 if (!vecs[i].iov_len)
2235 continue;
2236 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
2237 vecs[i].iov_base);
2238 totlen += thislen;
2239 if (ret || thislen != vecs[i].iov_len)
2240 break;
2241 to += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002243 *retlen = totlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return ret;
2245}
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002246
2247/*
2248 * mtd_writev - the vector-based MTD write method
2249 * @mtd: mtd device description object pointer
2250 * @vecs: the vectors to write
2251 * @count: count of vectors in @vecs
2252 * @to: the MTD device offset to write to
2253 * @retlen: on exit contains the count of bytes written to the MTD device.
2254 *
2255 * This function returns zero in case of success and a negative error code in
2256 * case of failure.
2257 */
2258int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
2259 unsigned long count, loff_t to, size_t *retlen)
2260{
Miquel Raynal46b58892020-01-14 10:09:52 +01002261 struct mtd_info *master = mtd_get_master(mtd);
2262
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002263 *retlen = 0;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02002264 if (!(mtd->flags & MTD_WRITEABLE))
2265 return -EROFS;
Miquel Raynal46b58892020-01-14 10:09:52 +01002266
2267 if (!master->_writev)
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002268 return default_mtd_writev(mtd, vecs, count, to, retlen);
Miquel Raynal46b58892020-01-14 10:09:52 +01002269
2270 return master->_writev(master, vecs, count,
2271 mtd_get_master_ofs(mtd, to), retlen);
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002272}
2273EXPORT_SYMBOL_GPL(mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Grant Erickson33b53712011-04-08 08:51:32 -07002275/**
2276 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002277 * @mtd: mtd device description object pointer
2278 * @size: a pointer to the ideal or maximum size of the allocation, points
Grant Erickson33b53712011-04-08 08:51:32 -07002279 * to the actual allocation size on success.
2280 *
2281 * This routine attempts to allocate a contiguous kernel buffer up to
2282 * the specified size, backing off the size of the request exponentially
2283 * until the request succeeds or until the allocation size falls below
2284 * the system page size. This attempts to make sure it does not adversely
2285 * impact system performance, so when allocating more than one page, we
Linus Torvaldscaf49192012-12-10 10:51:16 -08002286 * ask the memory allocator to avoid re-trying, swapping, writing back
2287 * or performing I/O.
Grant Erickson33b53712011-04-08 08:51:32 -07002288 *
2289 * Note, this function also makes sure that the allocated buffer is aligned to
2290 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
2291 *
2292 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
2293 * to handle smaller (i.e. degraded) buffer allocations under low- or
2294 * fragmented-memory situations where such reduced allocations, from a
2295 * requested ideal, are allowed.
2296 *
2297 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
2298 */
2299void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
2300{
Mel Gormand0164ad2015-11-06 16:28:21 -08002301 gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY;
Grant Erickson33b53712011-04-08 08:51:32 -07002302 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
2303 void *kbuf;
2304
2305 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
2306
2307 while (*size > min_alloc) {
2308 kbuf = kmalloc(*size, flags);
2309 if (kbuf)
2310 return kbuf;
2311
2312 *size >>= 1;
2313 *size = ALIGN(*size, mtd->writesize);
2314 }
2315
2316 /*
2317 * For the last resort allocation allow 'kmalloc()' to do all sorts of
2318 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
2319 */
2320 return kmalloc(*size, GFP_KERNEL);
2321}
Grant Erickson33b53712011-04-08 08:51:32 -07002322EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Pavel Machek2d2dce02006-03-31 02:29:49 -08002324#ifdef CONFIG_PROC_FS
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/* Support for /proc/mtd */
2328
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03002329static int mtd_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00002331 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03002333 seq_puts(m, "dev: size erasesize name\n");
Ingo Molnar48b19262006-03-31 02:29:41 -08002334 mutex_lock(&mtd_table_mutex);
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00002335 mtd_for_each_device(mtd) {
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03002336 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
2337 mtd->index, (unsigned long long)mtd->size,
2338 mtd->erasesize, mtd->name);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08002339 }
Ingo Molnar48b19262006-03-31 02:29:41 -08002340 mutex_unlock(&mtd_table_mutex);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08002341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
Kevin Cernekee45b09072009-04-04 11:03:04 -07002343#endif /* CONFIG_PROC_FS */
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*====================================================================*/
2346/* Init code */
2347
Tomas Winkler462d69a2021-02-25 16:33:29 +02002348static struct backing_dev_info * __init mtd_bdi_init(const char *name)
Jens Axboe0661b1a2010-04-27 09:49:47 +02002349{
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302350 struct backing_dev_info *bdi;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002351 int ret;
2352
Christoph Hellwigaef33c22020-05-04 14:48:00 +02002353 bdi = bdi_alloc(NUMA_NO_NODE);
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302354 if (!bdi)
2355 return ERR_PTR(-ENOMEM);
Christoph Hellwig55b25982020-09-24 08:51:32 +02002356 bdi->ra_pages = 0;
2357 bdi->io_pages = 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002358
Jan Karafa060522017-04-12 12:24:37 +02002359 /*
2360 * We put '-0' suffix to the name to get the same name format as we
2361 * used to get. Since this is called only once, we get a unique name.
2362 */
Jan Kara7c4cc302017-04-12 12:24:49 +02002363 ret = bdi_register(bdi, "%.28s-0", name);
Jens Axboe0661b1a2010-04-27 09:49:47 +02002364 if (ret)
Jan Karafa060522017-04-12 12:24:37 +02002365 bdi_put(bdi);
Jens Axboe0661b1a2010-04-27 09:49:47 +02002366
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302367 return ret ? ERR_PTR(ret) : bdi;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002368}
2369
Artem Bityutskiy93e56212013-03-15 12:56:05 +02002370static struct proc_dir_entry *proc_mtd;
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372static int __init init_mtd(void)
2373{
David Woodhouse15bce402009-04-05 07:40:58 -07002374 int ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -07002375
Jens Axboe0661b1a2010-04-27 09:49:47 +02002376 ret = class_register(&mtd_class);
2377 if (ret)
2378 goto err_reg;
2379
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302380 mtd_bdi = mtd_bdi_init("mtd");
2381 if (IS_ERR(mtd_bdi)) {
2382 ret = PTR_ERR(mtd_bdi);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01002383 goto err_bdi;
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302384 }
Jens Axboe0661b1a2010-04-27 09:49:47 +02002385
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02002386 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show);
Artem Bityutskiy93e56212013-03-15 12:56:05 +02002387
Artem Bityutskiy660685d2013-03-14 13:27:40 +02002388 ret = init_mtdchar();
2389 if (ret)
2390 goto out_procfs;
2391
Mario Rugieroe8e3edb2017-05-29 08:38:41 -03002392 dfs_dir_mtd = debugfs_create_dir("mtd", NULL);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002395
Artem Bityutskiy660685d2013-03-14 13:27:40 +02002396out_procfs:
2397 if (proc_mtd)
2398 remove_proc_entry("mtd", NULL);
Jan Karafa060522017-04-12 12:24:37 +02002399 bdi_put(mtd_bdi);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01002400err_bdi:
Jens Axboe0661b1a2010-04-27 09:49:47 +02002401 class_unregister(&mtd_class);
2402err_reg:
2403 pr_err("Error registering mtd class or bdi: %d\n", ret);
2404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
2407static void __exit cleanup_mtd(void)
2408{
Mario Rugieroe8e3edb2017-05-29 08:38:41 -03002409 debugfs_remove_recursive(dfs_dir_mtd);
Artem Bityutskiy660685d2013-03-14 13:27:40 +02002410 cleanup_mtdchar();
Wanlong Gaod5ca5122011-05-20 21:14:30 +08002411 if (proc_mtd)
Artem Bityutskiy93e56212013-03-15 12:56:05 +02002412 remove_proc_entry("mtd", NULL);
David Woodhouse15bce402009-04-05 07:40:58 -07002413 class_unregister(&mtd_class);
Jan Karafa060522017-04-12 12:24:37 +02002414 bdi_put(mtd_bdi);
Johannes Thumshirn35667b92015-07-08 17:15:34 +02002415 idr_destroy(&mtd_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418module_init(init_mtd);
2419module_exit(cleanup_mtd);
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421MODULE_LICENSE("GPL");
2422MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2423MODULE_DESCRIPTION("Core MTD registration and access routines");