blob: ffa46ccea0cf0f92682597d92e2e94187e524be4 [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
David Brownell1f24b5a2009-03-26 00:42:41 -070099static ssize_t mtd_type_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
101{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200102 struct mtd_info *mtd = dev_get_drvdata(dev);
David Brownell1f24b5a2009-03-26 00:42:41 -0700103 char *type;
104
105 switch (mtd->type) {
106 case MTD_ABSENT:
107 type = "absent";
108 break;
109 case MTD_RAM:
110 type = "ram";
111 break;
112 case MTD_ROM:
113 type = "rom";
114 break;
115 case MTD_NORFLASH:
116 type = "nor";
117 break;
118 case MTD_NANDFLASH:
119 type = "nand";
120 break;
121 case MTD_DATAFLASH:
122 type = "dataflash";
123 break;
124 case MTD_UBIVOLUME:
125 type = "ubi";
126 break;
Huang Shijief4837242013-09-25 14:58:19 +0800127 case MTD_MLCNANDFLASH:
128 type = "mlc-nand";
129 break;
David Brownell1f24b5a2009-03-26 00:42:41 -0700130 default:
131 type = "unknown";
132 }
133
Tian Tao5b2fbe02021-04-12 17:35:43 +0800134 return sysfs_emit(buf, "%s\n", type);
David Brownell1f24b5a2009-03-26 00:42:41 -0700135}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700136static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
137
138static ssize_t mtd_flags_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
140{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200141 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700142
Tian Tao5b2fbe02021-04-12 17:35:43 +0800143 return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700144}
145static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
146
147static ssize_t mtd_size_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
149{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200150 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700151
Tian Tao5b2fbe02021-04-12 17:35:43 +0800152 return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700153}
154static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
155
156static ssize_t mtd_erasesize_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200159 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700160
Tian Tao5b2fbe02021-04-12 17:35:43 +0800161 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700162}
163static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
164
165static ssize_t mtd_writesize_show(struct device *dev,
166 struct device_attribute *attr, char *buf)
167{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200168 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700169
Tian Tao5b2fbe02021-04-12 17:35:43 +0800170 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700171}
172static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
173
Artem Bityutskiye7693542009-04-18 12:29:42 +0300174static ssize_t mtd_subpagesize_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
176{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200177 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiye7693542009-04-18 12:29:42 +0300178 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
179
Tian Tao5b2fbe02021-04-12 17:35:43 +0800180 return sysfs_emit(buf, "%u\n", subpagesize);
Artem Bityutskiye7693542009-04-18 12:29:42 +0300181}
182static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
183
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700184static ssize_t mtd_oobsize_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
186{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200187 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700188
Tian Tao5b2fbe02021-04-12 17:35:43 +0800189 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700190}
191static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
192
Xiaolei Li7cc9aa62018-04-02 16:20:10 +0800193static ssize_t mtd_oobavail_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 struct mtd_info *mtd = dev_get_drvdata(dev);
197
Tian Tao5b2fbe02021-04-12 17:35:43 +0800198 return sysfs_emit(buf, "%u\n", mtd->oobavail);
Xiaolei Li7cc9aa62018-04-02 16:20:10 +0800199}
200static DEVICE_ATTR(oobavail, S_IRUGO, mtd_oobavail_show, NULL);
201
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700202static ssize_t mtd_numeraseregions_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
204{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200205 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700206
Tian Tao5b2fbe02021-04-12 17:35:43 +0800207 return sysfs_emit(buf, "%u\n", mtd->numeraseregions);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700208}
209static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
210 NULL);
211
212static ssize_t mtd_name_show(struct device *dev,
213 struct device_attribute *attr, char *buf)
214{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200215 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700216
Tian Tao5b2fbe02021-04-12 17:35:43 +0800217 return sysfs_emit(buf, "%s\n", mtd->name);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700218}
219static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700220
Mike Dunna9b672e2012-04-25 12:06:07 -0700221static ssize_t mtd_ecc_strength_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 struct mtd_info *mtd = dev_get_drvdata(dev);
225
Tian Tao5b2fbe02021-04-12 17:35:43 +0800226 return sysfs_emit(buf, "%u\n", mtd->ecc_strength);
Mike Dunna9b672e2012-04-25 12:06:07 -0700227}
228static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
229
Mike Dunnd062d4e2012-04-25 12:06:08 -0700230static ssize_t mtd_bitflip_threshold_show(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
233{
234 struct mtd_info *mtd = dev_get_drvdata(dev);
235
Tian Tao5b2fbe02021-04-12 17:35:43 +0800236 return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold);
Mike Dunnd062d4e2012-04-25 12:06:08 -0700237}
238
239static ssize_t mtd_bitflip_threshold_store(struct device *dev,
240 struct device_attribute *attr,
241 const char *buf, size_t count)
242{
243 struct mtd_info *mtd = dev_get_drvdata(dev);
244 unsigned int bitflip_threshold;
245 int retval;
246
247 retval = kstrtouint(buf, 0, &bitflip_threshold);
248 if (retval)
249 return retval;
250
251 mtd->bitflip_threshold = bitflip_threshold;
252 return count;
253}
254static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
255 mtd_bitflip_threshold_show,
256 mtd_bitflip_threshold_store);
257
Huang Shijiebf977e32013-08-16 10:10:05 +0800258static ssize_t mtd_ecc_step_size_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
260{
261 struct mtd_info *mtd = dev_get_drvdata(dev);
262
Tian Tao5b2fbe02021-04-12 17:35:43 +0800263 return sysfs_emit(buf, "%u\n", mtd->ecc_step_size);
Huang Shijiebf977e32013-08-16 10:10:05 +0800264
265}
266static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
267
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300268static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270{
271 struct mtd_info *mtd = dev_get_drvdata(dev);
272 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
273
Tian Tao5b2fbe02021-04-12 17:35:43 +0800274 return sysfs_emit(buf, "%u\n", ecc_stats->corrected);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300275}
276static DEVICE_ATTR(corrected_bits, S_IRUGO,
277 mtd_ecc_stats_corrected_show, NULL);
278
279static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
281{
282 struct mtd_info *mtd = dev_get_drvdata(dev);
283 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
284
Tian Tao5b2fbe02021-04-12 17:35:43 +0800285 return sysfs_emit(buf, "%u\n", ecc_stats->failed);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300286}
287static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
288
289static ssize_t mtd_badblocks_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
292 struct mtd_info *mtd = dev_get_drvdata(dev);
293 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
294
Tian Tao5b2fbe02021-04-12 17:35:43 +0800295 return sysfs_emit(buf, "%u\n", ecc_stats->badblocks);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300296}
297static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
298
299static ssize_t mtd_bbtblocks_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct mtd_info *mtd = dev_get_drvdata(dev);
303 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
304
Tian Tao5b2fbe02021-04-12 17:35:43 +0800305 return sysfs_emit(buf, "%u\n", ecc_stats->bbtblocks);
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300306}
307static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
308
David Brownell1f24b5a2009-03-26 00:42:41 -0700309static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700310 &dev_attr_type.attr,
311 &dev_attr_flags.attr,
312 &dev_attr_size.attr,
313 &dev_attr_erasesize.attr,
314 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300315 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700316 &dev_attr_oobsize.attr,
Xiaolei Li7cc9aa62018-04-02 16:20:10 +0800317 &dev_attr_oobavail.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700318 &dev_attr_numeraseregions.attr,
319 &dev_attr_name.attr,
Mike Dunna9b672e2012-04-25 12:06:07 -0700320 &dev_attr_ecc_strength.attr,
Huang Shijiebf977e32013-08-16 10:10:05 +0800321 &dev_attr_ecc_step_size.attr,
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300322 &dev_attr_corrected_bits.attr,
323 &dev_attr_ecc_failures.attr,
324 &dev_attr_bad_blocks.attr,
325 &dev_attr_bbt_blocks.attr,
Mike Dunnd062d4e2012-04-25 12:06:08 -0700326 &dev_attr_bitflip_threshold.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700327 NULL,
328};
Axel Lin54c738f2013-12-02 10:12:25 +0800329ATTRIBUTE_GROUPS(mtd);
David Brownell1f24b5a2009-03-26 00:42:41 -0700330
Bhumika Goyal75864b32017-08-19 13:52:17 +0530331static const struct device_type mtd_devtype = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700332 .name = "mtd",
333 .groups = mtd_groups,
334 .release = mtd_release,
335};
336
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800337static int mtd_partid_debug_show(struct seq_file *s, void *p)
Zhuohao Lee1018c942019-07-01 00:07:10 +0800338{
339 struct mtd_info *mtd = s->private;
340
341 seq_printf(s, "%s\n", mtd->dbg.partid);
342
343 return 0;
344}
345
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800346DEFINE_SHOW_ATTRIBUTE(mtd_partid_debug);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800347
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800348static int mtd_partname_debug_show(struct seq_file *s, void *p)
Zhuohao Lee1018c942019-07-01 00:07:10 +0800349{
350 struct mtd_info *mtd = s->private;
351
352 seq_printf(s, "%s\n", mtd->dbg.partname);
353
354 return 0;
355}
356
Yongqiang Liu5610d1f2020-07-16 17:02:20 +0800357DEFINE_SHOW_ATTRIBUTE(mtd_partname_debug);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800358
359static struct dentry *dfs_dir_mtd;
360
361static void mtd_debugfs_populate(struct mtd_info *mtd)
362{
Petr Malatfeb05fa2021-04-30 08:50:57 +0200363 struct mtd_info *master = mtd_get_master(mtd);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800364 struct device *dev = &mtd->dev;
Greg Kroah-Hartmanc2d73ba2019-11-07 09:51:11 +0100365 struct dentry *root;
Zhuohao Lee1018c942019-07-01 00:07:10 +0800366
367 if (IS_ERR_OR_NULL(dfs_dir_mtd))
368 return;
369
370 root = debugfs_create_dir(dev_name(dev), dfs_dir_mtd);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800371 mtd->dbg.dfs_dir = root;
372
Petr Malatfeb05fa2021-04-30 08:50:57 +0200373 if (master->dbg.partid)
374 debugfs_create_file("partid", 0400, root, master,
Greg Kroah-Hartmanc2d73ba2019-11-07 09:51:11 +0100375 &mtd_partid_debug_fops);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800376
Petr Malatfeb05fa2021-04-30 08:50:57 +0200377 if (master->dbg.partname)
378 debugfs_create_file("partname", 0400, root, master,
Greg Kroah-Hartmanc2d73ba2019-11-07 09:51:11 +0100379 &mtd_partname_debug_fops);
Zhuohao Lee1018c942019-07-01 00:07:10 +0800380}
381
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100382#ifndef CONFIG_MMU
383unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
384{
385 switch (mtd->type) {
386 case MTD_RAM:
387 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
388 NOMMU_MAP_READ | NOMMU_MAP_WRITE;
389 case MTD_ROM:
390 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
391 NOMMU_MAP_READ;
392 default:
393 return NOMMU_MAP_COPY;
394 }
395}
Arnd Bergmann706a4e52015-01-28 11:09:20 -0700396EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100397#endif
398
Brian Norris3efe41b2014-11-26 01:01:08 -0800399static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
400 void *cmd)
401{
402 struct mtd_info *mtd;
403
404 mtd = container_of(n, struct mtd_info, reboot_notifier);
405 mtd->_reboot(mtd);
406
407 return NOTIFY_DONE;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
Boris Brezillon477b0222015-11-16 15:53:13 +0100411 * mtd_wunit_to_pairing_info - get pairing information of a wunit
412 * @mtd: pointer to new MTD device info structure
413 * @wunit: write unit we are interested in
414 * @info: returned pairing information
415 *
416 * Retrieve pairing information associated to the wunit.
417 * This is mainly useful when dealing with MLC/TLC NANDs where pages can be
418 * paired together, and where programming a page may influence the page it is
419 * paired with.
420 * The notion of page is replaced by the term wunit (write-unit) to stay
421 * consistent with the ->writesize field.
422 *
423 * The @wunit argument can be extracted from an absolute offset using
424 * mtd_offset_to_wunit(). @info is filled with the pairing information attached
425 * to @wunit.
426 *
427 * From the pairing info the MTD user can find all the wunits paired with
428 * @wunit using the following loop:
429 *
430 * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
431 * info.pair = i;
432 * mtd_pairing_info_to_wunit(mtd, &info);
433 * ...
434 * }
435 */
436int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
437 struct mtd_pairing_info *info)
438{
Miquel Raynal46b58892020-01-14 10:09:52 +0100439 struct mtd_info *master = mtd_get_master(mtd);
440 int npairs = mtd_wunit_per_eb(master) / mtd_pairing_groups(master);
Boris Brezillon477b0222015-11-16 15:53:13 +0100441
442 if (wunit < 0 || wunit >= npairs)
443 return -EINVAL;
444
Miquel Raynal46b58892020-01-14 10:09:52 +0100445 if (master->pairing && master->pairing->get_info)
446 return master->pairing->get_info(master, wunit, info);
Boris Brezillon477b0222015-11-16 15:53:13 +0100447
448 info->group = 0;
449 info->pair = wunit;
450
451 return 0;
452}
453EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info);
454
455/**
Xiaolei Lic77a9312018-03-29 09:34:58 +0800456 * mtd_pairing_info_to_wunit - get wunit from pairing information
Boris Brezillon477b0222015-11-16 15:53:13 +0100457 * @mtd: pointer to new MTD device info structure
458 * @info: pairing information struct
459 *
460 * Returns a positive number representing the wunit associated to the info
461 * struct, or a negative error code.
462 *
463 * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to
464 * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info()
465 * doc).
466 *
467 * It can also be used to only program the first page of each pair (i.e.
468 * page attached to group 0), which allows one to use an MLC NAND in
469 * software-emulated SLC mode:
470 *
471 * info.group = 0;
472 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
473 * for (info.pair = 0; info.pair < npairs; info.pair++) {
474 * wunit = mtd_pairing_info_to_wunit(mtd, &info);
475 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
476 * mtd->writesize, &retlen, buf + (i * mtd->writesize));
477 * }
478 */
479int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
480 const struct mtd_pairing_info *info)
481{
Miquel Raynal46b58892020-01-14 10:09:52 +0100482 struct mtd_info *master = mtd_get_master(mtd);
483 int ngroups = mtd_pairing_groups(master);
484 int npairs = mtd_wunit_per_eb(master) / ngroups;
Boris Brezillon477b0222015-11-16 15:53:13 +0100485
486 if (!info || info->pair < 0 || info->pair >= npairs ||
487 info->group < 0 || info->group >= ngroups)
488 return -EINVAL;
489
Miquel Raynal46b58892020-01-14 10:09:52 +0100490 if (master->pairing && master->pairing->get_wunit)
491 return mtd->pairing->get_wunit(master, info);
Boris Brezillon477b0222015-11-16 15:53:13 +0100492
493 return info->pair;
494}
495EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit);
496
497/**
498 * mtd_pairing_groups - get the number of pairing groups
499 * @mtd: pointer to new MTD device info structure
500 *
501 * Returns the number of pairing groups.
502 *
503 * This number is usually equal to the number of bits exposed by a single
504 * cell, and can be used in conjunction with mtd_pairing_info_to_wunit()
505 * to iterate over all pages of a given pair.
506 */
507int mtd_pairing_groups(struct mtd_info *mtd)
508{
Miquel Raynal46b58892020-01-14 10:09:52 +0100509 struct mtd_info *master = mtd_get_master(mtd);
510
511 if (!master->pairing || !master->pairing->ngroups)
Boris Brezillon477b0222015-11-16 15:53:13 +0100512 return 1;
513
Miquel Raynal46b58892020-01-14 10:09:52 +0100514 return master->pairing->ngroups;
Boris Brezillon477b0222015-11-16 15:53:13 +0100515}
516EXPORT_SYMBOL_GPL(mtd_pairing_groups);
517
Alban Bedelc4dfa252018-11-13 15:01:10 +0100518static int mtd_nvmem_reg_read(void *priv, unsigned int offset,
519 void *val, size_t bytes)
520{
521 struct mtd_info *mtd = priv;
522 size_t retlen;
523 int err;
524
525 err = mtd_read(mtd, offset, bytes, &retlen, val);
526 if (err && err != -EUCLEAN)
527 return err;
528
529 return retlen == bytes ? 0 : -EIO;
530}
531
532static int mtd_nvmem_add(struct mtd_info *mtd)
533{
Ansuel Smith658c4442021-03-12 07:28:19 +0100534 struct device_node *node = mtd_get_of_node(mtd);
Alban Bedelc4dfa252018-11-13 15:01:10 +0100535 struct nvmem_config config = {};
536
Aneesh Kumar K.V6e952682019-02-11 19:03:37 +0530537 config.id = -1;
Alban Bedelc4dfa252018-11-13 15:01:10 +0100538 config.dev = &mtd->dev;
Ricardo Ribalda Delgado7b01b722020-04-30 15:17:21 +0200539 config.name = dev_name(&mtd->dev);
Alban Bedelc4dfa252018-11-13 15:01:10 +0100540 config.owner = THIS_MODULE;
541 config.reg_read = mtd_nvmem_reg_read;
542 config.size = mtd->size;
543 config.word_size = 1;
544 config.stride = 1;
545 config.read_only = true;
546 config.root_only = true;
Ansuel Smith658c4442021-03-12 07:28:19 +0100547 config.no_of_node = !of_device_is_compatible(node, "nvmem-cells");
Alban Bedelc4dfa252018-11-13 15:01:10 +0100548 config.priv = mtd;
549
550 mtd->nvmem = nvmem_register(&config);
551 if (IS_ERR(mtd->nvmem)) {
552 /* Just ignore if there is no NVMEM support in the kernel */
Boris Brezillon19e16fb2019-01-02 15:36:53 +0100553 if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) {
Alban Bedelc4dfa252018-11-13 15:01:10 +0100554 mtd->nvmem = NULL;
555 } else {
556 dev_err(&mtd->dev, "Failed to register NVMEM device\n");
557 return PTR_ERR(mtd->nvmem);
558 }
559 }
560
561 return 0;
562}
563
Boris Brezillon477b0222015-11-16 15:53:13 +0100564/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * add_mtd_device - register an MTD device
566 * @mtd: pointer to new MTD device info structure
567 *
568 * Add a device to the list of MTD devices present in the system, and
569 * notify each currently active MTD 'user' of its arrival. Returns
Brian Norris57dd9902015-06-01 16:17:18 -0700570 * zero on success or non-zero on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
572
573int add_mtd_device(struct mtd_info *mtd)
574{
Miquel Raynal46b58892020-01-14 10:09:52 +0100575 struct mtd_info *master = mtd_get_master(mtd);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000576 struct mtd_notifier *not;
577 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Brian Norrisbe0dbff2015-06-01 16:17:20 -0700579 /*
580 * May occur, for instance, on buggy drivers which call
581 * mtd_device_parse_register() multiple times on the same master MTD,
582 * especially with CONFIG_MTD_PARTITIONED_MASTER=y.
583 */
Jan Karafa060522017-04-12 12:24:37 +0200584 if (WARN_ONCE(mtd->dev.type, "MTD already registered\n"))
Brian Norrisbe0dbff2015-06-01 16:17:20 -0700585 return -EEXIST;
586
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400587 BUG_ON(mtd->writesize == 0);
Boris Brezillon33f45c42017-12-15 13:39:51 +0100588
Boris Brezillon2431c4f2018-12-20 15:13:20 +0100589 /*
590 * MTD drivers should implement ->_{write,read}() or
591 * ->_{write,read}_oob(), but not both.
592 */
593 if (WARN_ON((mtd->_write && mtd->_write_oob) ||
594 (mtd->_read && mtd->_read_oob)))
595 return -EINVAL;
596
Miquel Raynal46b58892020-01-14 10:09:52 +0100597 if (WARN_ON((!mtd->erasesize || !master->_erase) &&
Boris Brezillon33f45c42017-12-15 13:39:51 +0100598 !(mtd->flags & MTD_NO_ERASE)))
599 return -EINVAL;
600
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200601 /*
602 * MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the
603 * master is an MLC NAND and has a proper pairing scheme defined.
604 * We also reject masters that implement ->_writev() for now, because
605 * NAND controller drivers don't implement this hook, and adding the
606 * SLC -> MLC address/length conversion to this path is useless if we
607 * don't have a user.
608 */
609 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION &&
610 (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH ||
611 !master->pairing || master->_writev))
612 return -EINVAL;
613
Ingo Molnar48b19262006-03-31 02:29:41 -0800614 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Tejun Heo589e9c42013-02-27 17:04:33 -0800616 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
Brian Norris57dd9902015-06-01 16:17:18 -0700617 if (i < 0) {
618 error = i;
Ben Hutchingsb520e412010-01-29 20:59:42 +0000619 goto fail_locked;
Brian Norris57dd9902015-06-01 16:17:18 -0700620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Ben Hutchingsb520e412010-01-29 20:59:42 +0000622 mtd->index = i;
623 mtd->usecount = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000624
Mike Dunnd062d4e2012-04-25 12:06:08 -0700625 /* default value if not set by driver */
626 if (mtd->bitflip_threshold == 0)
627 mtd->bitflip_threshold = mtd->ecc_strength;
628
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200629 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
630 int ngroups = mtd_pairing_groups(master);
631
632 mtd->erasesize /= ngroups;
633 mtd->size = (u64)mtd_div_by_eb(mtd->size, master) *
634 mtd->erasesize;
635 }
636
Ben Hutchingsb520e412010-01-29 20:59:42 +0000637 if (is_power_of_2(mtd->erasesize))
638 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
639 else
640 mtd->erasesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000641
Ben Hutchingsb520e412010-01-29 20:59:42 +0000642 if (is_power_of_2(mtd->writesize))
643 mtd->writesize_shift = ffs(mtd->writesize) - 1;
644 else
645 mtd->writesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000646
Ben Hutchingsb520e412010-01-29 20:59:42 +0000647 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
648 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
Håvard Skinnemoen187ef152006-09-22 10:07:08 +0100649
Ben Hutchingsb520e412010-01-29 20:59:42 +0000650 /* Some chips always power up locked. Unlock them now */
Artem Bityutskiy38134562011-12-30 17:00:35 +0200651 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
652 error = mtd_unlock(mtd, 0, mtd->size);
653 if (error && error != -EOPNOTSUPP)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000654 printk(KERN_WARNING
655 "%s: unlock failed, writes may not work\n",
656 mtd->name);
Brian Norris57dd9902015-06-01 16:17:18 -0700657 /* Ignore unlock failures? */
658 error = 0;
Ben Hutchingsb520e412010-01-29 20:59:42 +0000659 }
David Brownell1f24b5a2009-03-26 00:42:41 -0700660
Ben Hutchingsb520e412010-01-29 20:59:42 +0000661 /* Caller should have set dev.parent to match the
Frans Klaver260e89a2015-06-10 22:38:15 +0200662 * physical device, if appropriate.
Ben Hutchingsb520e412010-01-29 20:59:42 +0000663 */
664 mtd->dev.type = &mtd_devtype;
665 mtd->dev.class = &mtd_class;
666 mtd->dev.devt = MTD_DEVT(i);
667 dev_set_name(&mtd->dev, "mtd%d", i);
668 dev_set_drvdata(&mtd->dev, mtd);
Brian Norris215a02f2015-11-11 16:26:04 -0800669 of_node_get(mtd_get_of_node(mtd));
Brian Norris57dd9902015-06-01 16:17:18 -0700670 error = device_register(&mtd->dev);
671 if (error)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000672 goto fail_added;
David Brownell1f24b5a2009-03-26 00:42:41 -0700673
Alban Bedelc4dfa252018-11-13 15:01:10 +0100674 /* Add the nvmem provider */
675 error = mtd_nvmem_add(mtd);
676 if (error)
677 goto fail_nvmem_add;
678
Zhuohao Lee1018c942019-07-01 00:07:10 +0800679 mtd_debugfs_populate(mtd);
Mario Rugieroe8e3edb2017-05-29 08:38:41 -0300680
Brian Norris5e472122014-07-21 19:06:47 -0700681 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
682 "mtd%dro", i);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000683
Brian Norris289c0522011-07-19 10:06:09 -0700684 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000685 /* No need to get a refcount on the module containing
686 the notifier, since we hold the mtd_table_mutex */
687 list_for_each_entry(not, &mtd_notifiers, list)
688 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000689
Ingo Molnar48b19262006-03-31 02:29:41 -0800690 mutex_unlock(&mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000691 /* We _know_ we aren't being removed, because
692 our caller is still holding us here. So none
693 of this try_ nonsense, and no bitching about it
694 either. :) */
695 __module_get(THIS_MODULE);
696 return 0;
697
Alban Bedelc4dfa252018-11-13 15:01:10 +0100698fail_nvmem_add:
699 device_unregister(&mtd->dev);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000700fail_added:
Brian Norris215a02f2015-11-11 16:26:04 -0800701 of_node_put(mtd_get_of_node(mtd));
Ben Hutchingsb520e412010-01-29 20:59:42 +0000702 idr_remove(&mtd_idr, i);
703fail_locked:
704 mutex_unlock(&mtd_table_mutex);
Brian Norris57dd9902015-06-01 16:17:18 -0700705 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/**
709 * del_mtd_device - unregister an MTD device
710 * @mtd: pointer to MTD device info structure
711 *
712 * Remove a device from the list of MTD devices present in the system,
713 * and notify each currently active MTD 'user' of its departure.
714 * Returns zero on success or 1 on failure, which currently will happen
715 * if the requested device does not appear to be present in the list.
716 */
717
Jamie Ileseea72d52011-05-23 10:23:42 +0100718int del_mtd_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 int ret;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200721 struct mtd_notifier *not;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000722
Ingo Molnar48b19262006-03-31 02:29:41 -0800723 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Mario Rugieroe8e3edb2017-05-29 08:38:41 -0300725 debugfs_remove_recursive(mtd->dbg.dfs_dir);
726
Ben Hutchingsb520e412010-01-29 20:59:42 +0000727 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 ret = -ENODEV;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200729 goto out_error;
730 }
731
732 /* No need to get a refcount on the module containing
733 the notifier, since we hold the mtd_table_mutex */
734 list_for_each_entry(not, &mtd_notifiers, list)
735 not->remove(mtd);
736
737 if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000738 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 mtd->index, mtd->name, mtd->usecount);
740 ret = -EBUSY;
741 } else {
Alban Bedelc4dfa252018-11-13 15:01:10 +0100742 /* Try to remove the NVMEM provider */
743 if (mtd->nvmem)
744 nvmem_unregister(mtd->nvmem);
745
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700746 device_unregister(&mtd->dev);
747
Ben Hutchingsb520e412010-01-29 20:59:42 +0000748 idr_remove(&mtd_idr, mtd->index);
Brian Norris215a02f2015-11-11 16:26:04 -0800749 of_node_put(mtd_get_of_node(mtd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 module_put(THIS_MODULE);
752 ret = 0;
753 }
754
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200755out_error:
Ingo Molnar48b19262006-03-31 02:29:41 -0800756 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return ret;
758}
759
Brian Norris472b4442015-12-11 15:58:01 -0800760/*
761 * Set a few defaults based on the parent devices, if not provided by the
762 * driver
763 */
764static void mtd_set_dev_defaults(struct mtd_info *mtd)
765{
766 if (mtd->dev.parent) {
767 if (!mtd->owner && mtd->dev.parent->driver)
768 mtd->owner = mtd->dev.parent->driver->owner;
769 if (!mtd->name)
770 mtd->name = dev_name(mtd->dev.parent);
771 } else {
772 pr_debug("mtd device won't show a device symlink in sysfs\n");
773 }
Rafał Miłecki1186af42018-11-20 09:55:45 +0100774
Miquel Raynal46b58892020-01-14 10:09:52 +0100775 INIT_LIST_HEAD(&mtd->partitions);
776 mutex_init(&mtd->master.partitions_lock);
Alexander Sverdlin1ad55282021-02-17 22:18:45 +0100777 mutex_init(&mtd->master.chrdev_lock);
Brian Norris472b4442015-12-11 15:58:01 -0800778}
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700779
Michael Walle4b361cf2021-04-24 13:06:08 +0200780static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
781{
782 struct otp_info *info = kmalloc(PAGE_SIZE, GFP_KERNEL);
783 ssize_t size = 0;
784 unsigned int i;
785 size_t retlen;
786 int ret;
787
788 if (is_user)
789 ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info);
790 else
791 ret = mtd_get_fact_prot_info(mtd, PAGE_SIZE, &retlen, info);
792 if (ret)
793 goto err;
794
Jon Hunterbc8e1572021-05-18 19:55:03 +0100795 for (i = 0; i < retlen / sizeof(*info); i++)
796 size += info[i].length;
Michael Walle4b361cf2021-04-24 13:06:08 +0200797
798 kfree(info);
799 return size;
800
801err:
802 kfree(info);
803 return ret;
804}
805
806static struct nvmem_device *mtd_otp_nvmem_register(struct mtd_info *mtd,
807 const char *compatible,
808 int size,
809 nvmem_reg_read_t reg_read)
810{
811 struct nvmem_device *nvmem = NULL;
812 struct nvmem_config config = {};
813 struct device_node *np;
814
815 /* DT binding is optional */
816 np = of_get_compatible_child(mtd->dev.of_node, compatible);
817
818 /* OTP nvmem will be registered on the physical device */
819 config.dev = mtd->dev.parent;
820 /* just reuse the compatible as name */
821 config.name = compatible;
822 config.id = NVMEM_DEVID_NONE;
823 config.owner = THIS_MODULE;
824 config.type = NVMEM_TYPE_OTP;
825 config.root_only = true;
826 config.reg_read = reg_read;
827 config.size = size;
828 config.of_node = np;
829 config.priv = mtd;
830
831 nvmem = nvmem_register(&config);
832 /* Just ignore if there is no NVMEM support in the kernel */
833 if (IS_ERR(nvmem) && PTR_ERR(nvmem) == -EOPNOTSUPP)
834 nvmem = NULL;
835
836 of_node_put(np);
837
838 return nvmem;
839}
840
841static int mtd_nvmem_user_otp_reg_read(void *priv, unsigned int offset,
842 void *val, size_t bytes)
843{
844 struct mtd_info *mtd = priv;
845 size_t retlen;
846 int ret;
847
848 ret = mtd_read_user_prot_reg(mtd, offset, bytes, &retlen, val);
849 if (ret)
850 return ret;
851
852 return retlen == bytes ? 0 : -EIO;
853}
854
855static int mtd_nvmem_fact_otp_reg_read(void *priv, unsigned int offset,
856 void *val, size_t bytes)
857{
858 struct mtd_info *mtd = priv;
859 size_t retlen;
860 int ret;
861
862 ret = mtd_read_fact_prot_reg(mtd, offset, bytes, &retlen, val);
863 if (ret)
864 return ret;
865
866 return retlen == bytes ? 0 : -EIO;
867}
868
869static int mtd_otp_nvmem_add(struct mtd_info *mtd)
870{
871 struct nvmem_device *nvmem;
872 ssize_t size;
873 int err;
874
875 if (mtd->_get_user_prot_info && mtd->_read_user_prot_reg) {
876 size = mtd_otp_size(mtd, true);
877 if (size < 0)
878 return size;
879
880 if (size > 0) {
881 nvmem = mtd_otp_nvmem_register(mtd, "user-otp", size,
882 mtd_nvmem_user_otp_reg_read);
883 if (IS_ERR(nvmem)) {
884 dev_err(&mtd->dev, "Failed to register OTP NVMEM device\n");
885 return PTR_ERR(nvmem);
886 }
887 mtd->otp_user_nvmem = nvmem;
888 }
889 }
890
891 if (mtd->_get_fact_prot_info && mtd->_read_fact_prot_reg) {
892 size = mtd_otp_size(mtd, false);
893 if (size < 0) {
894 err = size;
895 goto err;
896 }
897
898 if (size > 0) {
899 nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size,
900 mtd_nvmem_fact_otp_reg_read);
901 if (IS_ERR(nvmem)) {
902 dev_err(&mtd->dev, "Failed to register OTP NVMEM device\n");
903 err = PTR_ERR(nvmem);
904 goto err;
905 }
906 mtd->otp_factory_nvmem = nvmem;
907 }
908 }
909
910 return 0;
911
912err:
913 if (mtd->otp_user_nvmem)
914 nvmem_unregister(mtd->otp_user_nvmem);
915 return err;
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/**
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300919 * mtd_device_parse_register - parse partitions and register an MTD device.
920 *
921 * @mtd: the MTD device to register
922 * @types: the list of MTD partition probes to try, see
923 * 'parse_mtd_partitions()' for more information
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400924 * @parser_data: MTD partition parser-specific data
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300925 * @parts: fallback partition information to register, if parsing fails;
926 * only valid if %nr_parts > %0
927 * @nr_parts: the number of partitions in parts, if zero then the full
928 * MTD device is registered if no partition info is found
929 *
930 * This function aggregates MTD partitions parsing (done by
931 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
932 * basically follows the most common pattern found in many MTD drivers:
933 *
Rafał Miłecki55a999a2018-03-27 15:36:47 +0200934 * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is
935 * registered first.
936 * * Then It tries to probe partitions on MTD device @mtd using parsers
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300937 * specified in @types (if @types is %NULL, then the default list of parsers
938 * is used, see 'parse_mtd_partitions()' for more information). If none are
939 * found this functions tries to fallback to information specified in
940 * @parts/@nr_parts.
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300941 * * If no partitions were found this function just registers the MTD device
942 * @mtd and exits.
943 *
944 * Returns zero in case of success and a negative error code in case of failure.
945 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200946int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400947 struct mtd_part_parser_data *parser_data,
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300948 const struct mtd_partition *parts,
949 int nr_parts)
950{
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700951 int ret;
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300952
Brian Norris472b4442015-12-11 15:58:01 -0800953 mtd_set_dev_defaults(mtd);
954
Rafał Miłecki2c77c572018-01-16 16:45:41 +0100955 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
956 ret = add_mtd_device(mtd);
957 if (ret)
958 return ret;
959 }
960
Rafał Miłecki0dbe4ea2018-01-16 16:45:42 +0100961 /* Prefer parsed partitions over driver-provided fallback */
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200962 ret = parse_mtd_partitions(mtd, types, parser_data);
Manivannan Sadhasivam08608ad2021-03-02 18:57:56 +0530963 if (ret == -EPROBE_DEFER)
964 goto out;
965
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200966 if (ret > 0)
967 ret = 0;
968 else if (nr_parts)
Rafał Miłecki0dbe4ea2018-01-16 16:45:42 +0100969 ret = add_mtd_partitions(mtd, parts, nr_parts);
970 else if (!device_is_registered(&mtd->dev))
971 ret = add_mtd_device(mtd);
972 else
973 ret = 0;
974
Brian Norris3e00ed02015-06-01 16:17:19 -0700975 if (ret)
976 goto out;
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +0300977
Niklas Cassele1dd8642015-02-01 02:08:50 +0100978 /*
979 * FIXME: some drivers unfortunately call this function more than once.
980 * So we have to check if we've already assigned the reboot notifier.
981 *
982 * Generally, we can make multiple calls work for most cases, but it
983 * does cause problems with parse_mtd_partitions() above (e.g.,
984 * cmdlineparts will register partitions more than once).
985 */
Brian Norrisf8479dd2015-11-03 17:01:53 -0800986 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call,
987 "MTD already registered\n");
Niklas Cassele1dd8642015-02-01 02:08:50 +0100988 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
Brian Norris3efe41b2014-11-26 01:01:08 -0800989 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
990 register_reboot_notifier(&mtd->reboot_notifier);
991 }
992
Michael Walle4b361cf2021-04-24 13:06:08 +0200993 ret = mtd_otp_nvmem_add(mtd);
994
Brian Norris3e00ed02015-06-01 16:17:19 -0700995out:
Rafał Miłecki2c77c572018-01-16 16:45:41 +0100996 if (ret && device_is_registered(&mtd->dev))
997 del_mtd_device(mtd);
998
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700999 return ret;
Dmitry Eremin-Solenikov1c4c215c2011-03-25 22:26:25 +03001000}
1001EXPORT_SYMBOL_GPL(mtd_device_parse_register);
1002
1003/**
Jamie Ilesf5671ab2011-05-23 17:15:46 +01001004 * mtd_device_unregister - unregister an existing MTD device.
1005 *
1006 * @master: the MTD device to unregister. This will unregister both the master
1007 * and any partitions if registered.
1008 */
1009int mtd_device_unregister(struct mtd_info *master)
1010{
1011 int err;
1012
Brian Norris3efe41b2014-11-26 01:01:08 -08001013 if (master->_reboot)
1014 unregister_reboot_notifier(&master->reboot_notifier);
1015
Michael Walle4b361cf2021-04-24 13:06:08 +02001016 if (master->otp_user_nvmem)
1017 nvmem_unregister(master->otp_user_nvmem);
1018
1019 if (master->otp_factory_nvmem)
1020 nvmem_unregister(master->otp_factory_nvmem);
1021
Jamie Ilesf5671ab2011-05-23 17:15:46 +01001022 err = del_mtd_partitions(master);
1023 if (err)
1024 return err;
1025
1026 if (!device_is_registered(&master->dev))
1027 return 0;
1028
1029 return del_mtd_device(master);
1030}
1031EXPORT_SYMBOL_GPL(mtd_device_unregister);
1032
1033/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * register_mtd_user - register a 'user' of MTD devices.
1035 * @new: pointer to notifier info structure
1036 *
1037 * Registers a pair of callbacks function to be called upon addition
1038 * or removal of MTD devices. Causes the 'add' callback to be immediately
1039 * invoked for each MTD device currently present in the system.
1040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041void register_mtd_user (struct mtd_notifier *new)
1042{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001043 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Ingo Molnar48b19262006-03-31 02:29:41 -08001045 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 list_add(&new->list, &mtd_notifiers);
1048
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001049 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +00001050
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001051 mtd_for_each_device(mtd)
1052 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Ingo Molnar48b19262006-03-31 02:29:41 -08001054 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001056EXPORT_SYMBOL_GPL(register_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +00001059 * unregister_mtd_user - unregister a 'user' of MTD devices.
1060 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *
1062 * Removes a callback function pair from the list of 'users' to be
1063 * notified upon addition or removal of MTD devices. Causes the
1064 * 'remove' callback to be immediately invoked for each MTD device
1065 * currently present in the system.
1066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067int unregister_mtd_user (struct mtd_notifier *old)
1068{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001069 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Ingo Molnar48b19262006-03-31 02:29:41 -08001071 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 module_put(THIS_MODULE);
1074
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001075 mtd_for_each_device(mtd)
1076 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +00001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -08001079 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 0;
1081}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001082EXPORT_SYMBOL_GPL(unregister_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084/**
1085 * get_mtd_device - obtain a validated handle for an MTD device
1086 * @mtd: last known address of the required MTD device
1087 * @num: internal device number of the required MTD device
1088 *
1089 * Given a number and NULL address, return the num'th entry in the device
1090 * table, if any. Given an address and num == -1, search the device table
1091 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +03001092 * both, return the num'th driver only if its address matches. Return
1093 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
1096{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001097 struct mtd_info *ret = NULL, *other;
1098 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Ingo Molnar48b19262006-03-31 02:29:41 -08001100 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001103 mtd_for_each_device(other) {
1104 if (other == mtd) {
1105 ret = mtd;
1106 break;
1107 }
1108 }
Ben Hutchingsb520e412010-01-29 20:59:42 +00001109 } else if (num >= 0) {
1110 ret = idr_find(&mtd_idr, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (mtd && mtd != ret)
1112 ret = NULL;
1113 }
1114
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001115 if (!ret) {
1116 ret = ERR_PTR(err);
1117 goto out;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001120 err = __get_mtd_device(ret);
1121 if (err)
1122 ret = ERR_PTR(err);
1123out:
Ingo Molnar48b19262006-03-31 02:29:41 -08001124 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return ret;
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001126}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001127EXPORT_SYMBOL_GPL(get_mtd_device);
Artem Bityutskiy9c740342006-10-11 14:52:47 +03001128
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001129
1130int __get_mtd_device(struct mtd_info *mtd)
1131{
Miquel Raynal46b58892020-01-14 10:09:52 +01001132 struct mtd_info *master = mtd_get_master(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001133 int err;
1134
Miquel Raynal46b58892020-01-14 10:09:52 +01001135 if (!try_module_get(master->owner))
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001136 return -ENODEV;
1137
Miquel Raynal46b58892020-01-14 10:09:52 +01001138 if (master->_get_device) {
1139 err = master->_get_device(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001140
1141 if (err) {
Miquel Raynal46b58892020-01-14 10:09:52 +01001142 module_put(master->owner);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001143 return err;
1144 }
1145 }
Miquel Raynal46b58892020-01-14 10:09:52 +01001146
Richard Weinberger1ca71412020-12-06 21:22:20 +01001147 master->usecount++;
1148
Miquel Raynal46b58892020-01-14 10:09:52 +01001149 while (mtd->parent) {
1150 mtd->usecount++;
1151 mtd = mtd->parent;
1152 }
1153
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001156EXPORT_SYMBOL_GPL(__get_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Artem Bityutskiy77993082006-10-11 14:52:44 +03001158/**
1159 * get_mtd_device_nm - obtain a validated handle for an MTD device by
1160 * device name
1161 * @name: MTD device name to open
1162 *
1163 * This function returns MTD device description structure in case of
1164 * success and an error code in case of failure.
1165 */
Artem Bityutskiy77993082006-10-11 14:52:44 +03001166struct mtd_info *get_mtd_device_nm(const char *name)
1167{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001168 int err = -ENODEV;
1169 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +03001170
1171 mutex_lock(&mtd_table_mutex);
1172
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001173 mtd_for_each_device(other) {
1174 if (!strcmp(name, other->name)) {
1175 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +03001176 break;
1177 }
1178 }
1179
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001180 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +03001181 goto out_unlock;
1182
Wanlong Gao52534f22011-05-17 22:36:18 +08001183 err = __get_mtd_device(mtd);
1184 if (err)
Artem Bityutskiy77993082006-10-11 14:52:44 +03001185 goto out_unlock;
1186
Artem Bityutskiy77993082006-10-11 14:52:44 +03001187 mutex_unlock(&mtd_table_mutex);
1188 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001189
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +03001190out_unlock:
1191 mutex_unlock(&mtd_table_mutex);
1192 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +03001193}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001194EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Artem Bityutskiy77993082006-10-11 14:52:44 +03001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196void put_mtd_device(struct mtd_info *mtd)
1197{
Ingo Molnar48b19262006-03-31 02:29:41 -08001198 mutex_lock(&mtd_table_mutex);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001199 __put_mtd_device(mtd);
1200 mutex_unlock(&mtd_table_mutex);
1201
1202}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001203EXPORT_SYMBOL_GPL(put_mtd_device);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001204
1205void __put_mtd_device(struct mtd_info *mtd)
1206{
Miquel Raynal46b58892020-01-14 10:09:52 +01001207 struct mtd_info *master = mtd_get_master(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +02001208
Miquel Raynal46b58892020-01-14 10:09:52 +01001209 while (mtd->parent) {
1210 --mtd->usecount;
1211 BUG_ON(mtd->usecount < 0);
1212 mtd = mtd->parent;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Richard Weinberger1ca71412020-12-06 21:22:20 +01001215 master->usecount--;
1216
Miquel Raynal46b58892020-01-14 10:09:52 +01001217 if (master->_put_device)
1218 master->_put_device(master);
1219
1220 module_put(master->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +02001222EXPORT_SYMBOL_GPL(__put_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001224/*
Boris Brezillon884cfd92018-02-12 22:03:09 +01001225 * Erase is an synchronous operation. Device drivers are epected to return a
1226 * negative error code if the operation failed and update instr->fail_addr
1227 * to point the portion that was not properly erased.
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001228 */
1229int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1230{
Miquel Raynal46b58892020-01-14 10:09:52 +01001231 struct mtd_info *master = mtd_get_master(mtd);
1232 u64 mst_ofs = mtd_get_master_ofs(mtd, 0);
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001233 struct erase_info adjinstr;
Miquel Raynal46b58892020-01-14 10:09:52 +01001234 int ret;
1235
Boris Brezillonc585da92018-02-12 22:03:07 +01001236 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001237 adjinstr = *instr;
Boris Brezillonc585da92018-02-12 22:03:07 +01001238
Miquel Raynal46b58892020-01-14 10:09:52 +01001239 if (!mtd->erasesize || !master->_erase)
Boris Brezillone6e620f2018-01-22 10:38:01 +01001240 return -ENOTSUPP;
1241
Brian Norris0c2b4e22014-07-21 19:06:27 -07001242 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001243 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001244 if (!(mtd->flags & MTD_WRITEABLE))
1245 return -EROFS;
Boris Brezillone6e620f2018-01-22 10:38:01 +01001246
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01001247 if (!instr->len)
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001248 return 0;
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01001249
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001250 ledtrig_mtd_activity();
Miquel Raynal46b58892020-01-14 10:09:52 +01001251
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001252 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
1253 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) *
1254 master->erasesize;
1255 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) *
1256 master->erasesize) -
1257 adjinstr.addr;
1258 }
Miquel Raynal46b58892020-01-14 10:09:52 +01001259
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001260 adjinstr.addr += mst_ofs;
1261
1262 ret = master->_erase(master, &adjinstr);
1263
1264 if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) {
1265 instr->fail_addr = adjinstr.fail_addr - mst_ofs;
1266 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
1267 instr->fail_addr = mtd_div_by_eb(instr->fail_addr,
1268 master);
1269 instr->fail_addr *= mtd->erasesize;
1270 }
1271 }
1272
Miquel Raynal46b58892020-01-14 10:09:52 +01001273 return ret;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001274}
1275EXPORT_SYMBOL_GPL(mtd_erase);
1276
1277/*
1278 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
1279 */
1280int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1281 void **virt, resource_size_t *phys)
1282{
Miquel Raynal46b58892020-01-14 10:09:52 +01001283 struct mtd_info *master = mtd_get_master(mtd);
1284
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001285 *retlen = 0;
Artem Bityutskiy0dd52352012-02-08 15:13:26 +02001286 *virt = NULL;
1287 if (phys)
1288 *phys = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001289 if (!master->_point)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001290 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001291 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001292 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001293 if (!len)
1294 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001295
1296 from = mtd_get_master_ofs(mtd, from);
1297 return master->_point(master, from, len, retlen, virt, phys);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001298}
1299EXPORT_SYMBOL_GPL(mtd_point);
1300
1301/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
1302int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
1303{
Miquel Raynal46b58892020-01-14 10:09:52 +01001304 struct mtd_info *master = mtd_get_master(mtd);
1305
1306 if (!master->_unpoint)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001307 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001308 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001309 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001310 if (!len)
1311 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001312 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001313}
1314EXPORT_SYMBOL_GPL(mtd_unpoint);
1315
1316/*
1317 * Allow NOMMU mmap() to directly map the device (if not NULL)
1318 * - return the address to which the offset maps
1319 * - return -ENOSYS to indicate refusal to do the mapping
1320 */
1321unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
1322 unsigned long offset, unsigned long flags)
1323{
Nicolas Pitre9eaa9032017-10-30 14:48:32 -04001324 size_t retlen;
1325 void *virt;
1326 int ret;
1327
1328 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL);
1329 if (ret)
1330 return ret;
1331 if (retlen != len) {
1332 mtd_unpoint(mtd, offset, retlen);
1333 return -ENOSYS;
1334 }
1335 return (unsigned long)virt;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001336}
1337EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
1338
Miquel Raynal46b58892020-01-14 10:09:52 +01001339static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master,
1340 const struct mtd_ecc_stats *old_stats)
1341{
1342 struct mtd_ecc_stats diff;
1343
1344 if (master == mtd)
1345 return;
1346
1347 diff = master->ecc_stats;
1348 diff.failed -= old_stats->failed;
1349 diff.corrected -= old_stats->corrected;
1350
1351 while (mtd->parent) {
1352 mtd->ecc_stats.failed += diff.failed;
1353 mtd->ecc_stats.corrected += diff.corrected;
1354 mtd = mtd->parent;
1355 }
1356}
1357
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001358int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1359 u_char *buf)
1360{
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001361 struct mtd_oob_ops ops = {
1362 .len = len,
1363 .datbuf = buf,
1364 };
1365 int ret;
Mike Dunnedbc45402012-04-25 12:06:11 -07001366
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001367 ret = mtd_read_oob(mtd, from, &ops);
1368 *retlen = ops.retlen;
Boris Brezillon24ff1292018-01-09 09:50:34 +01001369
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001370 return ret;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001371}
1372EXPORT_SYMBOL_GPL(mtd_read);
1373
1374int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1375 const u_char *buf)
1376{
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001377 struct mtd_oob_ops ops = {
1378 .len = len,
1379 .datbuf = (u8 *)buf,
1380 };
1381 int ret;
Boris Brezillon24ff1292018-01-09 09:50:34 +01001382
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001383 ret = mtd_write_oob(mtd, to, &ops);
1384 *retlen = ops.retlen;
Boris Brezillon24ff1292018-01-09 09:50:34 +01001385
Boris Brezillon2431c4f2018-12-20 15:13:20 +01001386 return ret;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001387}
1388EXPORT_SYMBOL_GPL(mtd_write);
1389
1390/*
1391 * In blackbox flight recorder like scenarios we want to make successful writes
1392 * in interrupt context. panic_write() is only intended to be called when its
1393 * known the kernel is about to panic and we need the write to succeed. Since
1394 * the kernel is not going to be running for much longer, this function can
1395 * break locks and delay to ensure the write succeeds (but not sleep).
1396 */
1397int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1398 const u_char *buf)
1399{
Miquel Raynal46b58892020-01-14 10:09:52 +01001400 struct mtd_info *master = mtd_get_master(mtd);
1401
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001402 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001403 if (!master->_panic_write)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001404 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001405 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001406 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001407 if (!(mtd->flags & MTD_WRITEABLE))
1408 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001409 if (!len)
1410 return 0;
Kamal Dasu630e8d52020-06-15 11:51:34 -04001411 if (!master->oops_panic_write)
1412 master->oops_panic_write = true;
Kamal Dasu9f897bf2019-05-16 12:41:46 -04001413
Miquel Raynal46b58892020-01-14 10:09:52 +01001414 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len,
1415 retlen, buf);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001416}
1417EXPORT_SYMBOL_GPL(mtd_panic_write);
1418
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001419static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1420 struct mtd_oob_ops *ops)
1421{
1422 /*
1423 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1424 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1425 * this case.
1426 */
1427 if (!ops->datbuf)
1428 ops->len = 0;
1429
1430 if (!ops->oobbuf)
1431 ops->ooblen = 0;
1432
Miquel Raynald82c3682017-12-18 08:26:28 +01001433 if (offs < 0 || offs + ops->len > mtd->size)
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001434 return -EINVAL;
1435
1436 if (ops->ooblen) {
Miquel Raynal89f706d2018-11-18 21:18:31 +01001437 size_t maxooblen;
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001438
1439 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1440 return -EINVAL;
1441
Miquel Raynal89f706d2018-11-18 21:18:31 +01001442 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1443 mtd_div_by_ws(offs, mtd)) *
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001444 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1445 if (ops->ooblen > maxooblen)
1446 return -EINVAL;
1447 }
1448
1449 return 0;
1450}
1451
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001452static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from,
1453 struct mtd_oob_ops *ops)
1454{
1455 struct mtd_info *master = mtd_get_master(mtd);
1456 int ret;
1457
1458 from = mtd_get_master_ofs(mtd, from);
1459 if (master->_read_oob)
1460 ret = master->_read_oob(master, from, ops);
1461 else
1462 ret = master->_read(master, from, ops->len, &ops->retlen,
1463 ops->datbuf);
1464
1465 return ret;
1466}
1467
1468static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to,
1469 struct mtd_oob_ops *ops)
1470{
1471 struct mtd_info *master = mtd_get_master(mtd);
1472 int ret;
1473
1474 to = mtd_get_master_ofs(mtd, to);
1475 if (master->_write_oob)
1476 ret = master->_write_oob(master, to, ops);
1477 else
1478 ret = master->_write(master, to, ops->len, &ops->retlen,
1479 ops->datbuf);
1480
1481 return ret;
1482}
1483
1484static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read,
1485 struct mtd_oob_ops *ops)
1486{
1487 struct mtd_info *master = mtd_get_master(mtd);
1488 int ngroups = mtd_pairing_groups(master);
1489 int npairs = mtd_wunit_per_eb(master) / ngroups;
1490 struct mtd_oob_ops adjops = *ops;
1491 unsigned int wunit, oobavail;
1492 struct mtd_pairing_info info;
1493 int max_bitflips = 0;
1494 u32 ebofs, pageofs;
1495 loff_t base, pos;
1496
1497 ebofs = mtd_mod_by_eb(start, mtd);
1498 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize;
1499 info.group = 0;
1500 info.pair = mtd_div_by_ws(ebofs, mtd);
1501 pageofs = mtd_mod_by_ws(ebofs, mtd);
1502 oobavail = mtd_oobavail(mtd, ops);
1503
1504 while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
1505 int ret;
1506
1507 if (info.pair >= npairs) {
1508 info.pair = 0;
1509 base += master->erasesize;
1510 }
1511
1512 wunit = mtd_pairing_info_to_wunit(master, &info);
1513 pos = mtd_wunit_to_offset(mtd, base, wunit);
1514
1515 adjops.len = ops->len - ops->retlen;
1516 if (adjops.len > mtd->writesize - pageofs)
1517 adjops.len = mtd->writesize - pageofs;
1518
1519 adjops.ooblen = ops->ooblen - ops->oobretlen;
1520 if (adjops.ooblen > oobavail - adjops.ooboffs)
1521 adjops.ooblen = oobavail - adjops.ooboffs;
1522
1523 if (read) {
1524 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops);
1525 if (ret > 0)
1526 max_bitflips = max(max_bitflips, ret);
1527 } else {
1528 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops);
1529 }
1530
1531 if (ret < 0)
1532 return ret;
1533
1534 max_bitflips = max(max_bitflips, ret);
1535 ops->retlen += adjops.retlen;
1536 ops->oobretlen += adjops.oobretlen;
1537 adjops.datbuf += adjops.retlen;
1538 adjops.oobbuf += adjops.oobretlen;
1539 adjops.ooboffs = 0;
1540 pageofs = 0;
1541 info.pair++;
1542 }
1543
1544 return max_bitflips;
1545}
1546
Brian Norrisd2d48482012-06-22 16:35:38 -07001547int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1548{
Miquel Raynal46b58892020-01-14 10:09:52 +01001549 struct mtd_info *master = mtd_get_master(mtd);
1550 struct mtd_ecc_stats old_stats = master->ecc_stats;
Brian Norrise47f6852012-06-22 16:35:39 -07001551 int ret_code;
Miquel Raynal46b58892020-01-14 10:09:52 +01001552
Brian Norrisd2d48482012-06-22 16:35:38 -07001553 ops->retlen = ops->oobretlen = 0;
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001554
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001555 ret_code = mtd_check_oob_ops(mtd, from, ops);
1556 if (ret_code)
1557 return ret_code;
1558
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001559 ledtrig_mtd_activity();
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001560
1561 /* Check the validity of a potential fallback on mtd->_read */
Miquel Raynal46b58892020-01-14 10:09:52 +01001562 if (!master->_read_oob && (!master->_read || ops->oobbuf))
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001563 return -EOPNOTSUPP;
1564
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001565 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
1566 ret_code = mtd_io_emulated_slc(mtd, from, true, ops);
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001567 else
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001568 ret_code = mtd_read_oob_std(mtd, from, ops);
Miquel Raynal46b58892020-01-14 10:09:52 +01001569
1570 mtd_update_ecc_stats(mtd, master, &old_stats);
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001571
Brian Norrise47f6852012-06-22 16:35:39 -07001572 /*
1573 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1574 * similar to mtd->_read(), returning a non-negative integer
1575 * representing max bitflips. In other cases, mtd->_read_oob() may
1576 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1577 */
Brian Norrise47f6852012-06-22 16:35:39 -07001578 if (unlikely(ret_code < 0))
1579 return ret_code;
1580 if (mtd->ecc_strength == 0)
1581 return 0; /* device lacks ecc */
1582 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Brian Norrisd2d48482012-06-22 16:35:38 -07001583}
1584EXPORT_SYMBOL_GPL(mtd_read_oob);
1585
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001586int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1587 struct mtd_oob_ops *ops)
1588{
Miquel Raynal46b58892020-01-14 10:09:52 +01001589 struct mtd_info *master = mtd_get_master(mtd);
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001590 int ret;
1591
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001592 ops->retlen = ops->oobretlen = 0;
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001593
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001594 if (!(mtd->flags & MTD_WRITEABLE))
1595 return -EROFS;
Boris Brezillon5cdd9292017-06-27 21:22:21 +02001596
1597 ret = mtd_check_oob_ops(mtd, to, ops);
1598 if (ret)
1599 return ret;
1600
Ezequiel Garciafea728c2016-04-12 17:46:42 -03001601 ledtrig_mtd_activity();
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001602
1603 /* Check the validity of a potential fallback on mtd->_write */
Miquel Raynal46b58892020-01-14 10:09:52 +01001604 if (!master->_write_oob && (!master->_write || ops->oobbuf))
Miquel Raynal89fd23e2018-07-14 14:42:00 +02001605 return -EOPNOTSUPP;
1606
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001607 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
1608 return mtd_io_emulated_slc(mtd, to, false, ops);
Miquel Raynal46b58892020-01-14 10:09:52 +01001609
Boris Brezillon9e3307a2020-05-03 17:53:37 +02001610 return mtd_write_oob_std(mtd, to, ops);
Ezequiel Garcia0c034fe2016-04-12 17:46:39 -03001611}
1612EXPORT_SYMBOL_GPL(mtd_write_oob);
1613
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001614/**
1615 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1616 * @mtd: MTD device structure
1617 * @section: ECC section. Depending on the layout you may have all the ECC
1618 * bytes stored in a single contiguous section, or one section
1619 * per ECC chunk (and sometime several sections for a single ECC
1620 * ECC chunk)
1621 * @oobecc: OOB region struct filled with the appropriate ECC position
1622 * information
1623 *
Masahiro Yamada7da0fff2016-12-14 09:31:01 +09001624 * This function returns ECC section information in the OOB area. If you want
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001625 * to get all the ECC bytes information, then you should call
1626 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1627 *
1628 * Returns zero on success, a negative error code otherwise.
1629 */
1630int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1631 struct mtd_oob_region *oobecc)
1632{
Miquel Raynal46b58892020-01-14 10:09:52 +01001633 struct mtd_info *master = mtd_get_master(mtd);
1634
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001635 memset(oobecc, 0, sizeof(*oobecc));
1636
Miquel Raynal46b58892020-01-14 10:09:52 +01001637 if (!master || section < 0)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001638 return -EINVAL;
1639
Miquel Raynal46b58892020-01-14 10:09:52 +01001640 if (!master->ooblayout || !master->ooblayout->ecc)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001641 return -ENOTSUPP;
1642
Miquel Raynal46b58892020-01-14 10:09:52 +01001643 return master->ooblayout->ecc(master, section, oobecc);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001644}
1645EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1646
1647/**
1648 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1649 * section
1650 * @mtd: MTD device structure
1651 * @section: Free section you are interested in. Depending on the layout
1652 * you may have all the free bytes stored in a single contiguous
1653 * section, or one section per ECC chunk plus an extra section
1654 * for the remaining bytes (or other funky layout).
1655 * @oobfree: OOB region struct filled with the appropriate free position
1656 * information
1657 *
Masahiro Yamada7da0fff2016-12-14 09:31:01 +09001658 * This function returns free bytes position in the OOB area. If you want
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001659 * to get all the free bytes information, then you should call
1660 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1661 *
1662 * Returns zero on success, a negative error code otherwise.
1663 */
1664int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1665 struct mtd_oob_region *oobfree)
1666{
Miquel Raynal46b58892020-01-14 10:09:52 +01001667 struct mtd_info *master = mtd_get_master(mtd);
1668
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001669 memset(oobfree, 0, sizeof(*oobfree));
1670
Miquel Raynal46b58892020-01-14 10:09:52 +01001671 if (!master || section < 0)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001672 return -EINVAL;
1673
Miquel Raynal46b58892020-01-14 10:09:52 +01001674 if (!master->ooblayout || !master->ooblayout->free)
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001675 return -ENOTSUPP;
1676
Miquel Raynal46b58892020-01-14 10:09:52 +01001677 return master->ooblayout->free(master, section, oobfree);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001678}
1679EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1680
1681/**
1682 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1683 * @mtd: mtd info structure
1684 * @byte: the byte we are searching for
1685 * @sectionp: pointer where the section id will be stored
1686 * @oobregion: used to retrieve the ECC position
1687 * @iter: iterator function. Should be either mtd_ooblayout_free or
1688 * mtd_ooblayout_ecc depending on the region type you're searching for
1689 *
Masahiro Yamada7da0fff2016-12-14 09:31:01 +09001690 * This function returns the section id and oobregion information of a
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001691 * specific byte. For example, say you want to know where the 4th ECC byte is
1692 * stored, you'll use:
1693 *
1694 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1695 *
1696 * Returns zero on success, a negative error code otherwise.
1697 */
1698static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1699 int *sectionp, struct mtd_oob_region *oobregion,
1700 int (*iter)(struct mtd_info *,
1701 int section,
1702 struct mtd_oob_region *oobregion))
1703{
1704 int pos = 0, ret, section = 0;
1705
1706 memset(oobregion, 0, sizeof(*oobregion));
1707
1708 while (1) {
1709 ret = iter(mtd, section, oobregion);
1710 if (ret)
1711 return ret;
1712
1713 if (pos + oobregion->length > byte)
1714 break;
1715
1716 pos += oobregion->length;
1717 section++;
1718 }
1719
1720 /*
1721 * Adjust region info to make it start at the beginning at the
1722 * 'start' ECC byte.
1723 */
1724 oobregion->offset += byte - pos;
1725 oobregion->length -= byte - pos;
1726 *sectionp = section;
1727
1728 return 0;
1729}
1730
1731/**
1732 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1733 * ECC byte
1734 * @mtd: mtd info structure
1735 * @eccbyte: the byte we are searching for
Lee Jones6361f532020-11-09 18:21:48 +00001736 * @section: pointer where the section id will be stored
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001737 * @oobregion: OOB region information
1738 *
1739 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1740 * byte.
1741 *
1742 * Returns zero on success, a negative error code otherwise.
1743 */
1744int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1745 int *section,
1746 struct mtd_oob_region *oobregion)
1747{
1748 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1749 mtd_ooblayout_ecc);
1750}
1751EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1752
1753/**
1754 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1755 * @mtd: mtd info structure
1756 * @buf: destination buffer to store OOB bytes
1757 * @oobbuf: OOB buffer
1758 * @start: first byte to retrieve
1759 * @nbytes: number of bytes to retrieve
1760 * @iter: section iterator
1761 *
1762 * Extract bytes attached to a specific category (ECC or free)
1763 * from the OOB buffer and copy them into buf.
1764 *
1765 * Returns zero on success, a negative error code otherwise.
1766 */
1767static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1768 const u8 *oobbuf, int start, int nbytes,
1769 int (*iter)(struct mtd_info *,
1770 int section,
1771 struct mtd_oob_region *oobregion))
1772{
Masahiro Yamada8e8fd4d2016-11-09 11:08:08 +09001773 struct mtd_oob_region oobregion;
1774 int section, ret;
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001775
1776 ret = mtd_ooblayout_find_region(mtd, start, &section,
1777 &oobregion, iter);
1778
1779 while (!ret) {
1780 int cnt;
1781
Masahiro Yamada7c295ef2016-11-09 11:08:09 +09001782 cnt = min_t(int, nbytes, oobregion.length);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001783 memcpy(buf, oobbuf + oobregion.offset, cnt);
1784 buf += cnt;
1785 nbytes -= cnt;
1786
1787 if (!nbytes)
1788 break;
1789
1790 ret = iter(mtd, ++section, &oobregion);
1791 }
1792
1793 return ret;
1794}
1795
1796/**
1797 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1798 * @mtd: mtd info structure
1799 * @buf: source buffer to get OOB bytes from
1800 * @oobbuf: OOB buffer
1801 * @start: first OOB byte to set
1802 * @nbytes: number of OOB bytes to set
1803 * @iter: section iterator
1804 *
1805 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1806 * is selected by passing the appropriate iterator.
1807 *
1808 * Returns zero on success, a negative error code otherwise.
1809 */
1810static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1811 u8 *oobbuf, int start, int nbytes,
1812 int (*iter)(struct mtd_info *,
1813 int section,
1814 struct mtd_oob_region *oobregion))
1815{
Masahiro Yamada8e8fd4d2016-11-09 11:08:08 +09001816 struct mtd_oob_region oobregion;
1817 int section, ret;
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001818
1819 ret = mtd_ooblayout_find_region(mtd, start, &section,
1820 &oobregion, iter);
1821
1822 while (!ret) {
1823 int cnt;
1824
Masahiro Yamada7c295ef2016-11-09 11:08:09 +09001825 cnt = min_t(int, nbytes, oobregion.length);
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001826 memcpy(oobbuf + oobregion.offset, buf, cnt);
1827 buf += cnt;
1828 nbytes -= cnt;
1829
1830 if (!nbytes)
1831 break;
1832
1833 ret = iter(mtd, ++section, &oobregion);
1834 }
1835
1836 return ret;
1837}
1838
1839/**
1840 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1841 * @mtd: mtd info structure
1842 * @iter: category iterator
1843 *
1844 * Count the number of bytes in a given category.
1845 *
1846 * Returns a positive value on success, a negative error code otherwise.
1847 */
1848static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1849 int (*iter)(struct mtd_info *,
1850 int section,
1851 struct mtd_oob_region *oobregion))
1852{
Masahiro Yamada4d6aecf2016-11-09 11:08:10 +09001853 struct mtd_oob_region oobregion;
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001854 int section = 0, ret, nbytes = 0;
1855
1856 while (1) {
1857 ret = iter(mtd, section++, &oobregion);
1858 if (ret) {
1859 if (ret == -ERANGE)
1860 ret = nbytes;
1861 break;
1862 }
1863
1864 nbytes += oobregion.length;
1865 }
1866
1867 return ret;
1868}
1869
1870/**
1871 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1872 * @mtd: mtd info structure
1873 * @eccbuf: destination buffer to store ECC bytes
1874 * @oobbuf: OOB buffer
1875 * @start: first ECC byte to retrieve
1876 * @nbytes: number of ECC bytes to retrieve
1877 *
1878 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1879 *
1880 * Returns zero on success, a negative error code otherwise.
1881 */
1882int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1883 const u8 *oobbuf, int start, int nbytes)
1884{
1885 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1886 mtd_ooblayout_ecc);
1887}
1888EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1889
1890/**
1891 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1892 * @mtd: mtd info structure
1893 * @eccbuf: source buffer to get ECC bytes from
1894 * @oobbuf: OOB buffer
1895 * @start: first ECC byte to set
1896 * @nbytes: number of ECC bytes to set
1897 *
1898 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1899 *
1900 * Returns zero on success, a negative error code otherwise.
1901 */
1902int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1903 u8 *oobbuf, int start, int nbytes)
1904{
1905 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1906 mtd_ooblayout_ecc);
1907}
1908EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1909
1910/**
1911 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1912 * @mtd: mtd info structure
1913 * @databuf: destination buffer to store ECC bytes
1914 * @oobbuf: OOB buffer
1915 * @start: first ECC byte to retrieve
1916 * @nbytes: number of ECC bytes to retrieve
1917 *
1918 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1919 *
1920 * Returns zero on success, a negative error code otherwise.
1921 */
1922int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1923 const u8 *oobbuf, int start, int nbytes)
1924{
1925 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1926 mtd_ooblayout_free);
1927}
1928EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1929
1930/**
Xiaolei Lic77a9312018-03-29 09:34:58 +08001931 * mtd_ooblayout_set_databytes - set data bytes into the oob buffer
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001932 * @mtd: mtd info structure
Xiaolei Lic77a9312018-03-29 09:34:58 +08001933 * @databuf: source buffer to get data bytes from
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001934 * @oobbuf: OOB buffer
1935 * @start: first ECC byte to set
1936 * @nbytes: number of ECC bytes to set
1937 *
Miquel Raynal519494a2020-05-26 21:56:13 +02001938 * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes.
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001939 *
1940 * Returns zero on success, a negative error code otherwise.
1941 */
1942int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1943 u8 *oobbuf, int start, int nbytes)
1944{
1945 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1946 mtd_ooblayout_free);
1947}
1948EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1949
1950/**
1951 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1952 * @mtd: mtd info structure
1953 *
1954 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1955 *
1956 * Returns zero on success, a negative error code otherwise.
1957 */
1958int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1959{
1960 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1961}
1962EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1963
1964/**
Xiaolei Lic77a9312018-03-29 09:34:58 +08001965 * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB
Boris Brezillon75eb2ce2016-02-04 09:52:30 +01001966 * @mtd: mtd info structure
1967 *
1968 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1969 *
1970 * Returns zero on success, a negative error code otherwise.
1971 */
1972int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1973{
1974 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1975}
1976EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1977
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001978/*
1979 * Method to access the protection register area, present in some flash
1980 * devices. The user data is one time programmable but the factory data is read
1981 * only.
1982 */
Christian Riesch4b78fc42f2014-01-28 09:29:44 +01001983int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1984 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001985{
Miquel Raynal46b58892020-01-14 10:09:52 +01001986 struct mtd_info *master = mtd_get_master(mtd);
1987
1988 if (!master->_get_fact_prot_info)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001989 return -EOPNOTSUPP;
1990 if (!len)
1991 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01001992 return master->_get_fact_prot_info(master, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001993}
1994EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1995
1996int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1997 size_t *retlen, u_char *buf)
1998{
Miquel Raynal46b58892020-01-14 10:09:52 +01001999 struct mtd_info *master = mtd_get_master(mtd);
2000
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002001 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002002 if (!master->_read_fact_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002003 return -EOPNOTSUPP;
2004 if (!len)
2005 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002006 return master->_read_fact_prot_reg(master, from, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002007}
2008EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
2009
Christian Riesch4b78fc42f2014-01-28 09:29:44 +01002010int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
2011 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002012{
Miquel Raynal46b58892020-01-14 10:09:52 +01002013 struct mtd_info *master = mtd_get_master(mtd);
2014
2015 if (!master->_get_user_prot_info)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002016 return -EOPNOTSUPP;
2017 if (!len)
2018 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002019 return master->_get_user_prot_info(master, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002020}
2021EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
2022
2023int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
2024 size_t *retlen, u_char *buf)
2025{
Miquel Raynal46b58892020-01-14 10:09:52 +01002026 struct mtd_info *master = mtd_get_master(mtd);
2027
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002028 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002029 if (!master->_read_user_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002030 return -EOPNOTSUPP;
2031 if (!len)
2032 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002033 return master->_read_user_prot_reg(master, from, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002034}
2035EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
2036
2037int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
Tudor Ambarus1df1fc82021-04-03 09:09:31 +03002038 size_t *retlen, const u_char *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002039{
Miquel Raynal46b58892020-01-14 10:09:52 +01002040 struct mtd_info *master = mtd_get_master(mtd);
Christian Riesch9a78bc82014-03-06 12:42:37 +01002041 int ret;
2042
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002043 *retlen = 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002044 if (!master->_write_user_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002045 return -EOPNOTSUPP;
2046 if (!len)
2047 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002048 ret = master->_write_user_prot_reg(master, to, len, retlen, buf);
Christian Riesch9a78bc82014-03-06 12:42:37 +01002049 if (ret)
2050 return ret;
2051
2052 /*
2053 * If no data could be written at all, we are out of memory and
2054 * must return -ENOSPC.
2055 */
2056 return (*retlen) ? 0 : -ENOSPC;
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002057}
2058EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
2059
2060int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
2061{
Miquel Raynal46b58892020-01-14 10:09:52 +01002062 struct mtd_info *master = mtd_get_master(mtd);
2063
2064 if (!master->_lock_user_prot_reg)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002065 return -EOPNOTSUPP;
2066 if (!len)
2067 return 0;
Miquel Raynal46b58892020-01-14 10:09:52 +01002068 return master->_lock_user_prot_reg(master, from, len);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02002069}
2070EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
2071
Michael Wallee3c1f1c2021-03-03 21:18:19 +01002072int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
2073{
2074 struct mtd_info *master = mtd_get_master(mtd);
2075
2076 if (!master->_erase_user_prot_reg)
2077 return -EOPNOTSUPP;
2078 if (!len)
2079 return 0;
2080 return master->_erase_user_prot_reg(master, from, len);
2081}
2082EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
2083
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002084/* Chip-supported device locking */
2085int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2086{
Miquel Raynal46b58892020-01-14 10:09:52 +01002087 struct mtd_info *master = mtd_get_master(mtd);
2088
2089 if (!master->_lock)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002090 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002091 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002092 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02002093 if (!len)
2094 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002095
2096 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2097 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2098 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2099 }
2100
Miquel Raynal46b58892020-01-14 10:09:52 +01002101 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002102}
2103EXPORT_SYMBOL_GPL(mtd_lock);
2104
2105int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2106{
Miquel Raynal46b58892020-01-14 10:09:52 +01002107 struct mtd_info *master = mtd_get_master(mtd);
2108
2109 if (!master->_unlock)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002110 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002111 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002112 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02002113 if (!len)
2114 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002115
2116 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2117 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2118 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2119 }
2120
Miquel Raynal46b58892020-01-14 10:09:52 +01002121 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002122}
2123EXPORT_SYMBOL_GPL(mtd_unlock);
2124
2125int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2126{
Miquel Raynal46b58892020-01-14 10:09:52 +01002127 struct mtd_info *master = mtd_get_master(mtd);
2128
2129 if (!master->_is_locked)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002130 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002131 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002132 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02002133 if (!len)
2134 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002135
2136 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
2137 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2138 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
2139 }
2140
Miquel Raynal46b58892020-01-14 10:09:52 +01002141 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len);
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002142}
2143EXPORT_SYMBOL_GPL(mtd_is_locked);
2144
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002145int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002146{
Miquel Raynal46b58892020-01-14 10:09:52 +01002147 struct mtd_info *master = mtd_get_master(mtd);
2148
Brian Norris0c2b4e22014-07-21 19:06:27 -07002149 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002150 return -EINVAL;
Miquel Raynal46b58892020-01-14 10:09:52 +01002151 if (!master->_block_isreserved)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002152 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002153
2154 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2155 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2156
Miquel Raynal46b58892020-01-14 10:09:52 +01002157 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs));
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002158}
2159EXPORT_SYMBOL_GPL(mtd_block_isreserved);
2160
2161int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
2162{
Miquel Raynal46b58892020-01-14 10:09:52 +01002163 struct mtd_info *master = mtd_get_master(mtd);
2164
Brian Norris0c2b4e22014-07-21 19:06:27 -07002165 if (ofs < 0 || ofs >= mtd->size)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002166 return -EINVAL;
Miquel Raynal46b58892020-01-14 10:09:52 +01002167 if (!master->_block_isbad)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03002168 return 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002169
2170 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2171 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2172
Miquel Raynal46b58892020-01-14 10:09:52 +01002173 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs));
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002174}
2175EXPORT_SYMBOL_GPL(mtd_block_isbad);
2176
2177int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
2178{
Miquel Raynal46b58892020-01-14 10:09:52 +01002179 struct mtd_info *master = mtd_get_master(mtd);
2180 int ret;
2181
2182 if (!master->_block_markbad)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002183 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07002184 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002185 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02002186 if (!(mtd->flags & MTD_WRITEABLE))
2187 return -EROFS;
Miquel Raynal46b58892020-01-14 10:09:52 +01002188
Boris Brezillon9e3307a2020-05-03 17:53:37 +02002189 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
2190 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
2191
Miquel Raynal46b58892020-01-14 10:09:52 +01002192 ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs));
2193 if (ret)
2194 return ret;
2195
2196 while (mtd->parent) {
2197 mtd->ecc_stats.badblocks++;
2198 mtd = mtd->parent;
2199 }
2200
2201 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02002202}
2203EXPORT_SYMBOL_GPL(mtd_block_markbad);
2204
2205/*
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002206 * default_mtd_writev - the default writev method
2207 * @mtd: mtd device description object pointer
2208 * @vecs: the vectors to write
2209 * @count: count of vectors in @vecs
2210 * @to: the MTD device offset to write to
2211 * @retlen: on exit contains the count of bytes written to the MTD device.
2212 *
2213 * This function returns zero in case of success and a negative error code in
2214 * case of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002216static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
2217 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 unsigned long i;
2220 size_t totlen = 0, thislen;
2221 int ret = 0;
2222
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002223 for (i = 0; i < count; i++) {
2224 if (!vecs[i].iov_len)
2225 continue;
2226 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
2227 vecs[i].iov_base);
2228 totlen += thislen;
2229 if (ret || thislen != vecs[i].iov_len)
2230 break;
2231 to += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002233 *retlen = totlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return ret;
2235}
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002236
2237/*
2238 * mtd_writev - the vector-based MTD write method
2239 * @mtd: mtd device description object pointer
2240 * @vecs: the vectors to write
2241 * @count: count of vectors in @vecs
2242 * @to: the MTD device offset to write to
2243 * @retlen: on exit contains the count of bytes written to the MTD device.
2244 *
2245 * This function returns zero in case of success and a negative error code in
2246 * case of failure.
2247 */
2248int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
2249 unsigned long count, loff_t to, size_t *retlen)
2250{
Miquel Raynal46b58892020-01-14 10:09:52 +01002251 struct mtd_info *master = mtd_get_master(mtd);
2252
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002253 *retlen = 0;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02002254 if (!(mtd->flags & MTD_WRITEABLE))
2255 return -EROFS;
Miquel Raynal46b58892020-01-14 10:09:52 +01002256
2257 if (!master->_writev)
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002258 return default_mtd_writev(mtd, vecs, count, to, retlen);
Miquel Raynal46b58892020-01-14 10:09:52 +01002259
2260 return master->_writev(master, vecs, count,
2261 mtd_get_master_ofs(mtd, to), retlen);
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02002262}
2263EXPORT_SYMBOL_GPL(mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Grant Erickson33b53712011-04-08 08:51:32 -07002265/**
2266 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
Artem Bityutskiy52b02032011-12-30 15:57:25 +02002267 * @mtd: mtd device description object pointer
2268 * @size: a pointer to the ideal or maximum size of the allocation, points
Grant Erickson33b53712011-04-08 08:51:32 -07002269 * to the actual allocation size on success.
2270 *
2271 * This routine attempts to allocate a contiguous kernel buffer up to
2272 * the specified size, backing off the size of the request exponentially
2273 * until the request succeeds or until the allocation size falls below
2274 * the system page size. This attempts to make sure it does not adversely
2275 * impact system performance, so when allocating more than one page, we
Linus Torvaldscaf49192012-12-10 10:51:16 -08002276 * ask the memory allocator to avoid re-trying, swapping, writing back
2277 * or performing I/O.
Grant Erickson33b53712011-04-08 08:51:32 -07002278 *
2279 * Note, this function also makes sure that the allocated buffer is aligned to
2280 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
2281 *
2282 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
2283 * to handle smaller (i.e. degraded) buffer allocations under low- or
2284 * fragmented-memory situations where such reduced allocations, from a
2285 * requested ideal, are allowed.
2286 *
2287 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
2288 */
2289void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
2290{
Mel Gormand0164ad2015-11-06 16:28:21 -08002291 gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY;
Grant Erickson33b53712011-04-08 08:51:32 -07002292 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
2293 void *kbuf;
2294
2295 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
2296
2297 while (*size > min_alloc) {
2298 kbuf = kmalloc(*size, flags);
2299 if (kbuf)
2300 return kbuf;
2301
2302 *size >>= 1;
2303 *size = ALIGN(*size, mtd->writesize);
2304 }
2305
2306 /*
2307 * For the last resort allocation allow 'kmalloc()' to do all sorts of
2308 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
2309 */
2310 return kmalloc(*size, GFP_KERNEL);
2311}
Grant Erickson33b53712011-04-08 08:51:32 -07002312EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Pavel Machek2d2dce02006-03-31 02:29:49 -08002314#ifdef CONFIG_PROC_FS
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/* Support for /proc/mtd */
2318
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03002319static int mtd_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00002321 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03002323 seq_puts(m, "dev: size erasesize name\n");
Ingo Molnar48b19262006-03-31 02:29:41 -08002324 mutex_lock(&mtd_table_mutex);
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00002325 mtd_for_each_device(mtd) {
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03002326 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
2327 mtd->index, (unsigned long long)mtd->size,
2328 mtd->erasesize, mtd->name);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08002329 }
Ingo Molnar48b19262006-03-31 02:29:41 -08002330 mutex_unlock(&mtd_table_mutex);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08002331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
Kevin Cernekee45b09072009-04-04 11:03:04 -07002333#endif /* CONFIG_PROC_FS */
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335/*====================================================================*/
2336/* Init code */
2337
Tomas Winkler462d69a2021-02-25 16:33:29 +02002338static struct backing_dev_info * __init mtd_bdi_init(const char *name)
Jens Axboe0661b1a2010-04-27 09:49:47 +02002339{
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302340 struct backing_dev_info *bdi;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002341 int ret;
2342
Christoph Hellwigaef33c22020-05-04 14:48:00 +02002343 bdi = bdi_alloc(NUMA_NO_NODE);
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302344 if (!bdi)
2345 return ERR_PTR(-ENOMEM);
Christoph Hellwig55b25982020-09-24 08:51:32 +02002346 bdi->ra_pages = 0;
2347 bdi->io_pages = 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002348
Jan Karafa060522017-04-12 12:24:37 +02002349 /*
2350 * We put '-0' suffix to the name to get the same name format as we
2351 * used to get. Since this is called only once, we get a unique name.
2352 */
Jan Kara7c4cc302017-04-12 12:24:49 +02002353 ret = bdi_register(bdi, "%.28s-0", name);
Jens Axboe0661b1a2010-04-27 09:49:47 +02002354 if (ret)
Jan Karafa060522017-04-12 12:24:37 +02002355 bdi_put(bdi);
Jens Axboe0661b1a2010-04-27 09:49:47 +02002356
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302357 return ret ? ERR_PTR(ret) : bdi;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002358}
2359
Artem Bityutskiy93e56212013-03-15 12:56:05 +02002360static struct proc_dir_entry *proc_mtd;
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362static int __init init_mtd(void)
2363{
David Woodhouse15bce402009-04-05 07:40:58 -07002364 int ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -07002365
Jens Axboe0661b1a2010-04-27 09:49:47 +02002366 ret = class_register(&mtd_class);
2367 if (ret)
2368 goto err_reg;
2369
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302370 mtd_bdi = mtd_bdi_init("mtd");
2371 if (IS_ERR(mtd_bdi)) {
2372 ret = PTR_ERR(mtd_bdi);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01002373 goto err_bdi;
Steve Longerbeam445caaa2016-08-04 19:31:15 +05302374 }
Jens Axboe0661b1a2010-04-27 09:49:47 +02002375
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02002376 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show);
Artem Bityutskiy93e56212013-03-15 12:56:05 +02002377
Artem Bityutskiy660685d2013-03-14 13:27:40 +02002378 ret = init_mtdchar();
2379 if (ret)
2380 goto out_procfs;
2381
Mario Rugieroe8e3edb2017-05-29 08:38:41 -03002382 dfs_dir_mtd = debugfs_create_dir("mtd", NULL);
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +02002385
Artem Bityutskiy660685d2013-03-14 13:27:40 +02002386out_procfs:
2387 if (proc_mtd)
2388 remove_proc_entry("mtd", NULL);
Jan Karafa060522017-04-12 12:24:37 +02002389 bdi_put(mtd_bdi);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01002390err_bdi:
Jens Axboe0661b1a2010-04-27 09:49:47 +02002391 class_unregister(&mtd_class);
2392err_reg:
2393 pr_err("Error registering mtd class or bdi: %d\n", ret);
2394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
2397static void __exit cleanup_mtd(void)
2398{
Mario Rugieroe8e3edb2017-05-29 08:38:41 -03002399 debugfs_remove_recursive(dfs_dir_mtd);
Artem Bityutskiy660685d2013-03-14 13:27:40 +02002400 cleanup_mtdchar();
Wanlong Gaod5ca5122011-05-20 21:14:30 +08002401 if (proc_mtd)
Artem Bityutskiy93e56212013-03-15 12:56:05 +02002402 remove_proc_entry("mtd", NULL);
David Woodhouse15bce402009-04-05 07:40:58 -07002403 class_unregister(&mtd_class);
Jan Karafa060522017-04-12 12:24:37 +02002404 bdi_put(mtd_bdi);
Johannes Thumshirn35667b92015-07-08 17:15:34 +02002405 idr_destroy(&mtd_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
2408module_init(init_mtd);
2409module_exit(cleanup_mtd);
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411MODULE_LICENSE("GPL");
2412MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2413MODULE_DESCRIPTION("Core MTD registration and access routines");