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