1 // SPDX-License-Identifier: GPL-2.0+
3 * Core registration and callback routines for MTD
6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
7 * Copyright © 2006 Red Hat UK Limited
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/ptrace.h>
15 #include <linux/seq_file.h>
16 #include <linux/string.h>
17 #include <linux/timer.h>
18 #include <linux/major.h>
20 #include <linux/err.h>
21 #include <linux/ioctl.h>
22 #include <linux/init.h>
23 #include <linux/proc_fs.h>
24 #include <linux/idr.h>
25 #include <linux/backing-dev.h>
26 #include <linux/gfp.h>
27 #include <linux/slab.h>
29 #include <linux/err.h>
30 #include <ubi_uboot.h>
33 #include <linux/log2.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/partitions.h>
41 * backing device capabilities for non-mappable devices (such as NAND flash)
42 * - permits private mappings, copies are taken of the data
44 static struct backing_dev_info mtd_bdi_unmappable = {
45 .capabilities = BDI_CAP_MAP_COPY,
49 * backing device capabilities for R/O mappable devices (such as ROM)
50 * - permits private mappings, copies are taken of the data
51 * - permits non-writable shared mappings
53 static struct backing_dev_info mtd_bdi_ro_mappable = {
54 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
55 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
59 * backing device capabilities for writable mappable devices (such as RAM)
60 * - permits private mappings, copies are taken of the data
61 * - permits non-writable shared mappings
63 static struct backing_dev_info mtd_bdi_rw_mappable = {
64 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
65 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
69 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
70 static int mtd_cls_resume(struct device *dev);
72 static struct class mtd_class = {
75 .suspend = mtd_cls_suspend,
76 .resume = mtd_cls_resume,
79 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
89 struct idr_layer id[MAX_IDR_ID];
92 #define DEFINE_IDR(name) struct idr name;
94 void idr_remove(struct idr *idp, int id)
101 void *idr_find(struct idr *idp, int id)
103 if (idp->id[id].used)
104 return idp->id[id].ptr;
109 void *idr_get_next(struct idr *idp, int *next)
114 ret = idr_find(idp, id);
117 if (!idp->id[id].used)
127 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
129 struct idr_layer *idl;
132 while (i < MAX_IDR_ID) {
134 if (idl->used == 0) {
145 static DEFINE_IDR(mtd_idr);
147 /* These are exported solely for the purpose of mtd_blkdevs.c. You
148 should not use them for _anything_ else */
149 DEFINE_MUTEX(mtd_table_mutex);
150 EXPORT_SYMBOL_GPL(mtd_table_mutex);
152 struct mtd_info *__mtd_next_device(int i)
154 return idr_get_next(&mtd_idr, &i);
156 EXPORT_SYMBOL_GPL(__mtd_next_device);
159 static LIST_HEAD(mtd_notifiers);
162 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
164 /* REVISIT once MTD uses the driver model better, whoever allocates
165 * the mtd_info will probably want to use the release() hook...
167 static void mtd_release(struct device *dev)
169 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
170 dev_t index = MTD_DEVT(mtd->index);
172 /* remove /dev/mtdXro node if needed */
174 device_destroy(&mtd_class, index + 1);
177 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
179 struct mtd_info *mtd = dev_get_drvdata(dev);
181 return mtd ? mtd_suspend(mtd) : 0;
184 static int mtd_cls_resume(struct device *dev)
186 struct mtd_info *mtd = dev_get_drvdata(dev);
193 static ssize_t mtd_type_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 struct mtd_info *mtd = dev_get_drvdata(dev);
221 case MTD_MLCNANDFLASH:
228 return snprintf(buf, PAGE_SIZE, "%s\n", type);
230 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
232 static ssize_t mtd_flags_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct mtd_info *mtd = dev_get_drvdata(dev);
237 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
240 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
242 static ssize_t mtd_size_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
245 struct mtd_info *mtd = dev_get_drvdata(dev);
247 return snprintf(buf, PAGE_SIZE, "%llu\n",
248 (unsigned long long)mtd->size);
251 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
253 static ssize_t mtd_erasesize_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
256 struct mtd_info *mtd = dev_get_drvdata(dev);
258 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
261 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
263 static ssize_t mtd_writesize_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
266 struct mtd_info *mtd = dev_get_drvdata(dev);
268 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
271 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
273 static ssize_t mtd_subpagesize_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
276 struct mtd_info *mtd = dev_get_drvdata(dev);
277 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
279 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
282 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
284 static ssize_t mtd_oobsize_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct mtd_info *mtd = dev_get_drvdata(dev);
289 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
292 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
294 static ssize_t mtd_numeraseregions_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
297 struct mtd_info *mtd = dev_get_drvdata(dev);
299 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
302 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
305 static ssize_t mtd_name_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct mtd_info *mtd = dev_get_drvdata(dev);
310 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
313 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
315 static ssize_t mtd_ecc_strength_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct mtd_info *mtd = dev_get_drvdata(dev);
320 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
322 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
324 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
325 struct device_attribute *attr,
328 struct mtd_info *mtd = dev_get_drvdata(dev);
330 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
333 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
337 struct mtd_info *mtd = dev_get_drvdata(dev);
338 unsigned int bitflip_threshold;
341 retval = kstrtouint(buf, 0, &bitflip_threshold);
345 mtd->bitflip_threshold = bitflip_threshold;
348 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
349 mtd_bitflip_threshold_show,
350 mtd_bitflip_threshold_store);
352 static ssize_t mtd_ecc_step_size_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 struct mtd_info *mtd = dev_get_drvdata(dev);
357 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
360 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
362 static struct attribute *mtd_attrs[] = {
364 &dev_attr_flags.attr,
366 &dev_attr_erasesize.attr,
367 &dev_attr_writesize.attr,
368 &dev_attr_subpagesize.attr,
369 &dev_attr_oobsize.attr,
370 &dev_attr_numeraseregions.attr,
372 &dev_attr_ecc_strength.attr,
373 &dev_attr_ecc_step_size.attr,
374 &dev_attr_bitflip_threshold.attr,
377 ATTRIBUTE_GROUPS(mtd);
379 static struct device_type mtd_devtype = {
381 .groups = mtd_groups,
382 .release = mtd_release,
387 * add_mtd_device - register an MTD device
388 * @mtd: pointer to new MTD device info structure
390 * Add a device to the list of MTD devices present in the system, and
391 * notify each currently active MTD 'user' of its arrival. Returns
392 * zero on success or 1 on failure, which currently will only happen
393 * if there is insufficient memory or a sysfs error.
396 int add_mtd_device(struct mtd_info *mtd)
399 struct mtd_notifier *not;
404 if (!mtd->backing_dev_info) {
407 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
410 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
413 mtd->backing_dev_info = &mtd_bdi_unmappable;
419 BUG_ON(mtd->writesize == 0);
420 mutex_lock(&mtd_table_mutex);
422 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
429 /* default value if not set by driver */
430 if (mtd->bitflip_threshold == 0)
431 mtd->bitflip_threshold = mtd->ecc_strength;
433 if (is_power_of_2(mtd->erasesize))
434 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
436 mtd->erasesize_shift = 0;
438 if (is_power_of_2(mtd->writesize))
439 mtd->writesize_shift = ffs(mtd->writesize) - 1;
441 mtd->writesize_shift = 0;
443 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
444 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
446 /* Some chips always power up locked. Unlock them now */
447 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
448 error = mtd_unlock(mtd, 0, mtd->size);
449 if (error && error != -EOPNOTSUPP)
451 "%s: unlock failed, writes may not work\n",
456 /* Caller should have set dev.parent to match the
459 mtd->dev.type = &mtd_devtype;
460 mtd->dev.class = &mtd_class;
461 mtd->dev.devt = MTD_DEVT(i);
462 dev_set_name(&mtd->dev, "mtd%d", i);
463 dev_set_drvdata(&mtd->dev, mtd);
464 if (device_register(&mtd->dev) != 0)
468 device_create(&mtd_class, mtd->dev.parent,
472 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
473 /* No need to get a refcount on the module containing
474 the notifier, since we hold the mtd_table_mutex */
475 list_for_each_entry(not, &mtd_notifiers, list)
478 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
481 mutex_unlock(&mtd_table_mutex);
482 /* We _know_ we aren't being removed, because
483 our caller is still holding us here. So none
484 of this try_ nonsense, and no bitching about it
486 __module_get(THIS_MODULE);
491 idr_remove(&mtd_idr, i);
494 mutex_unlock(&mtd_table_mutex);
499 * del_mtd_device - unregister an MTD device
500 * @mtd: pointer to MTD device info structure
502 * Remove a device from the list of MTD devices present in the system,
503 * and notify each currently active MTD 'user' of its departure.
504 * Returns zero on success or 1 on failure, which currently will happen
505 * if the requested device does not appear to be present in the list.
508 int del_mtd_device(struct mtd_info *mtd)
512 struct mtd_notifier *not;
515 mutex_lock(&mtd_table_mutex);
517 if (idr_find(&mtd_idr, mtd->index) != mtd) {
523 /* No need to get a refcount on the module containing
524 the notifier, since we hold the mtd_table_mutex */
525 list_for_each_entry(not, &mtd_notifiers, list)
530 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
531 mtd->index, mtd->name, mtd->usecount);
535 device_unregister(&mtd->dev);
538 idr_remove(&mtd_idr, mtd->index);
540 module_put(THIS_MODULE);
545 mutex_unlock(&mtd_table_mutex);
551 * mtd_device_parse_register - parse partitions and register an MTD device.
553 * @mtd: the MTD device to register
554 * @types: the list of MTD partition probes to try, see
555 * 'parse_mtd_partitions()' for more information
556 * @parser_data: MTD partition parser-specific data
557 * @parts: fallback partition information to register, if parsing fails;
558 * only valid if %nr_parts > %0
559 * @nr_parts: the number of partitions in parts, if zero then the full
560 * MTD device is registered if no partition info is found
562 * This function aggregates MTD partitions parsing (done by
563 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
564 * basically follows the most common pattern found in many MTD drivers:
566 * * It first tries to probe partitions on MTD device @mtd using parsers
567 * specified in @types (if @types is %NULL, then the default list of parsers
568 * is used, see 'parse_mtd_partitions()' for more information). If none are
569 * found this functions tries to fallback to information specified in
571 * * If any partitioning info was found, this function registers the found
573 * * If no partitions were found this function just registers the MTD device
576 * Returns zero in case of success and a negative error code in case of failure.
578 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
579 struct mtd_part_parser_data *parser_data,
580 const struct mtd_partition *parts,
584 struct mtd_partition *real_parts;
586 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
587 if (err <= 0 && nr_parts && parts) {
588 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
597 err = add_mtd_partitions(mtd, real_parts, err);
599 } else if (err == 0) {
600 err = add_mtd_device(mtd);
607 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
610 * mtd_device_unregister - unregister an existing MTD device.
612 * @master: the MTD device to unregister. This will unregister both the master
613 * and any partitions if registered.
615 int mtd_device_unregister(struct mtd_info *master)
619 err = del_mtd_partitions(master);
623 if (!device_is_registered(&master->dev))
626 return del_mtd_device(master);
628 EXPORT_SYMBOL_GPL(mtd_device_unregister);
631 * register_mtd_user - register a 'user' of MTD devices.
632 * @new: pointer to notifier info structure
634 * Registers a pair of callbacks function to be called upon addition
635 * or removal of MTD devices. Causes the 'add' callback to be immediately
636 * invoked for each MTD device currently present in the system.
638 void register_mtd_user (struct mtd_notifier *new)
640 struct mtd_info *mtd;
642 mutex_lock(&mtd_table_mutex);
644 list_add(&new->list, &mtd_notifiers);
646 __module_get(THIS_MODULE);
648 mtd_for_each_device(mtd)
651 mutex_unlock(&mtd_table_mutex);
653 EXPORT_SYMBOL_GPL(register_mtd_user);
656 * unregister_mtd_user - unregister a 'user' of MTD devices.
657 * @old: pointer to notifier info structure
659 * Removes a callback function pair from the list of 'users' to be
660 * notified upon addition or removal of MTD devices. Causes the
661 * 'remove' callback to be immediately invoked for each MTD device
662 * currently present in the system.
664 int unregister_mtd_user (struct mtd_notifier *old)
666 struct mtd_info *mtd;
668 mutex_lock(&mtd_table_mutex);
670 module_put(THIS_MODULE);
672 mtd_for_each_device(mtd)
675 list_del(&old->list);
676 mutex_unlock(&mtd_table_mutex);
679 EXPORT_SYMBOL_GPL(unregister_mtd_user);
683 * get_mtd_device - obtain a validated handle for an MTD device
684 * @mtd: last known address of the required MTD device
685 * @num: internal device number of the required MTD device
687 * Given a number and NULL address, return the num'th entry in the device
688 * table, if any. Given an address and num == -1, search the device table
689 * for a device with that address and return if it's still present. Given
690 * both, return the num'th driver only if its address matches. Return
693 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
695 struct mtd_info *ret = NULL, *other;
698 mutex_lock(&mtd_table_mutex);
701 mtd_for_each_device(other) {
707 } else if (num >= 0) {
708 ret = idr_find(&mtd_idr, num);
709 if (mtd && mtd != ret)
718 err = __get_mtd_device(ret);
722 mutex_unlock(&mtd_table_mutex);
725 EXPORT_SYMBOL_GPL(get_mtd_device);
728 int __get_mtd_device(struct mtd_info *mtd)
732 if (!try_module_get(mtd->owner))
735 if (mtd->_get_device) {
736 err = mtd->_get_device(mtd);
739 module_put(mtd->owner);
746 EXPORT_SYMBOL_GPL(__get_mtd_device);
749 * get_mtd_device_nm - obtain a validated handle for an MTD device by
751 * @name: MTD device name to open
753 * This function returns MTD device description structure in case of
754 * success and an error code in case of failure.
756 struct mtd_info *get_mtd_device_nm(const char *name)
759 struct mtd_info *mtd = NULL, *other;
761 mutex_lock(&mtd_table_mutex);
763 mtd_for_each_device(other) {
764 if (!strcmp(name, other->name)) {
773 err = __get_mtd_device(mtd);
777 mutex_unlock(&mtd_table_mutex);
781 mutex_unlock(&mtd_table_mutex);
784 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
786 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
788 * mtd_get_len_incl_bad
790 * Check if length including bad blocks fits into device.
792 * @param mtd an MTD device
793 * @param offset offset in flash
794 * @param length image length
795 * @return image length including bad blocks in *len_incl_bad and whether or not
796 * the length returned was truncated in *truncated
798 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
799 const uint64_t length, uint64_t *len_incl_bad,
805 if (!mtd->_block_isbad) {
806 *len_incl_bad = length;
810 uint64_t len_excl_bad = 0;
813 while (len_excl_bad < length) {
814 if (offset >= mtd->size) {
819 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
821 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
822 len_excl_bad += block_len;
824 *len_incl_bad += block_len;
828 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
830 void put_mtd_device(struct mtd_info *mtd)
832 mutex_lock(&mtd_table_mutex);
833 __put_mtd_device(mtd);
834 mutex_unlock(&mtd_table_mutex);
837 EXPORT_SYMBOL_GPL(put_mtd_device);
839 void __put_mtd_device(struct mtd_info *mtd)
842 BUG_ON(mtd->usecount < 0);
844 if (mtd->_put_device)
845 mtd->_put_device(mtd);
847 module_put(mtd->owner);
849 EXPORT_SYMBOL_GPL(__put_mtd_device);
852 * Erase is an asynchronous operation. Device drivers are supposed
853 * to call instr->callback() whenever the operation completes, even
854 * if it completes with a failure.
855 * Callers are supposed to pass a callback function and wait for it
856 * to be called before writing to the block.
858 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
860 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
862 if (!(mtd->flags & MTD_WRITEABLE))
864 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
866 instr->state = MTD_ERASE_DONE;
867 mtd_erase_callback(instr);
870 return mtd->_erase(mtd, instr);
872 EXPORT_SYMBOL_GPL(mtd_erase);
876 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
878 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
879 void **virt, resource_size_t *phys)
887 if (from < 0 || from > mtd->size || len > mtd->size - from)
891 return mtd->_point(mtd, from, len, retlen, virt, phys);
893 EXPORT_SYMBOL_GPL(mtd_point);
895 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
896 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
900 if (from < 0 || from > mtd->size || len > mtd->size - from)
904 return mtd->_unpoint(mtd, from, len);
906 EXPORT_SYMBOL_GPL(mtd_unpoint);
910 * Allow NOMMU mmap() to directly map the device (if not NULL)
911 * - return the address to which the offset maps
912 * - return -ENOSYS to indicate refusal to do the mapping
914 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
915 unsigned long offset, unsigned long flags)
917 if (!mtd->_get_unmapped_area)
919 if (offset > mtd->size || len > mtd->size - offset)
921 return mtd->_get_unmapped_area(mtd, len, offset, flags);
923 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
925 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
930 if (from < 0 || from > mtd->size || len > mtd->size - from)
936 * In the absence of an error, drivers return a non-negative integer
937 * representing the maximum number of bitflips that were corrected on
938 * any one ecc region (if applicable; zero otherwise).
941 ret_code = mtd->_read(mtd, from, len, retlen, buf);
942 } else if (mtd->_read_oob) {
943 struct mtd_oob_ops ops = {
948 ret_code = mtd->_read_oob(mtd, from, &ops);
949 *retlen = ops.retlen;
954 if (unlikely(ret_code < 0))
956 if (mtd->ecc_strength == 0)
957 return 0; /* device lacks ecc */
958 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
960 EXPORT_SYMBOL_GPL(mtd_read);
962 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
966 if (to < 0 || to > mtd->size || len > mtd->size - to)
968 if ((!mtd->_write && !mtd->_write_oob) ||
969 !(mtd->flags & MTD_WRITEABLE))
975 struct mtd_oob_ops ops = {
981 ret = mtd->_write_oob(mtd, to, &ops);
982 *retlen = ops.retlen;
986 return mtd->_write(mtd, to, len, retlen, buf);
988 EXPORT_SYMBOL_GPL(mtd_write);
991 * In blackbox flight recorder like scenarios we want to make successful writes
992 * in interrupt context. panic_write() is only intended to be called when its
993 * known the kernel is about to panic and we need the write to succeed. Since
994 * the kernel is not going to be running for much longer, this function can
995 * break locks and delay to ensure the write succeeds (but not sleep).
997 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1001 if (!mtd->_panic_write)
1003 if (to < 0 || to > mtd->size || len > mtd->size - to)
1005 if (!(mtd->flags & MTD_WRITEABLE))
1009 return mtd->_panic_write(mtd, to, len, retlen, buf);
1011 EXPORT_SYMBOL_GPL(mtd_panic_write);
1013 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1016 ops->retlen = ops->oobretlen = 0;
1017 if (!mtd->_read_oob)
1020 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1021 * similar to mtd->_read(), returning a non-negative integer
1022 * representing max bitflips. In other cases, mtd->_read_oob() may
1023 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1025 ret_code = mtd->_read_oob(mtd, from, ops);
1026 if (unlikely(ret_code < 0))
1028 if (mtd->ecc_strength == 0)
1029 return 0; /* device lacks ecc */
1030 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1032 EXPORT_SYMBOL_GPL(mtd_read_oob);
1034 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1035 struct mtd_oob_ops *ops)
1037 ops->retlen = ops->oobretlen = 0;
1038 if (!mtd->_write_oob)
1040 if (!(mtd->flags & MTD_WRITEABLE))
1042 return mtd->_write_oob(mtd, to, ops);
1044 EXPORT_SYMBOL_GPL(mtd_write_oob);
1047 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1048 * @mtd: MTD device structure
1049 * @section: ECC section. Depending on the layout you may have all the ECC
1050 * bytes stored in a single contiguous section, or one section
1051 * per ECC chunk (and sometime several sections for a single ECC
1053 * @oobecc: OOB region struct filled with the appropriate ECC position
1056 * This function returns ECC section information in the OOB area. If you want
1057 * to get all the ECC bytes information, then you should call
1058 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1060 * Returns zero on success, a negative error code otherwise.
1062 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1063 struct mtd_oob_region *oobecc)
1065 memset(oobecc, 0, sizeof(*oobecc));
1067 if (!mtd || section < 0)
1070 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1073 return mtd->ooblayout->ecc(mtd, section, oobecc);
1075 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1078 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1080 * @mtd: MTD device structure
1081 * @section: Free section you are interested in. Depending on the layout
1082 * you may have all the free bytes stored in a single contiguous
1083 * section, or one section per ECC chunk plus an extra section
1084 * for the remaining bytes (or other funky layout).
1085 * @oobfree: OOB region struct filled with the appropriate free position
1088 * This function returns free bytes position in the OOB area. If you want
1089 * to get all the free bytes information, then you should call
1090 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1092 * Returns zero on success, a negative error code otherwise.
1094 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1095 struct mtd_oob_region *oobfree)
1097 memset(oobfree, 0, sizeof(*oobfree));
1099 if (!mtd || section < 0)
1102 if (!mtd->ooblayout || !mtd->ooblayout->free)
1105 return mtd->ooblayout->free(mtd, section, oobfree);
1107 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1110 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1111 * @mtd: mtd info structure
1112 * @byte: the byte we are searching for
1113 * @sectionp: pointer where the section id will be stored
1114 * @oobregion: used to retrieve the ECC position
1115 * @iter: iterator function. Should be either mtd_ooblayout_free or
1116 * mtd_ooblayout_ecc depending on the region type you're searching for
1118 * This function returns the section id and oobregion information of a
1119 * specific byte. For example, say you want to know where the 4th ECC byte is
1120 * stored, you'll use:
1122 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1124 * Returns zero on success, a negative error code otherwise.
1126 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1127 int *sectionp, struct mtd_oob_region *oobregion,
1128 int (*iter)(struct mtd_info *,
1130 struct mtd_oob_region *oobregion))
1132 int pos = 0, ret, section = 0;
1134 memset(oobregion, 0, sizeof(*oobregion));
1137 ret = iter(mtd, section, oobregion);
1141 if (pos + oobregion->length > byte)
1144 pos += oobregion->length;
1149 * Adjust region info to make it start at the beginning at the
1152 oobregion->offset += byte - pos;
1153 oobregion->length -= byte - pos;
1154 *sectionp = section;
1160 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1162 * @mtd: mtd info structure
1163 * @eccbyte: the byte we are searching for
1164 * @sectionp: pointer where the section id will be stored
1165 * @oobregion: OOB region information
1167 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1170 * Returns zero on success, a negative error code otherwise.
1172 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1174 struct mtd_oob_region *oobregion)
1176 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1179 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1182 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1183 * @mtd: mtd info structure
1184 * @buf: destination buffer to store OOB bytes
1185 * @oobbuf: OOB buffer
1186 * @start: first byte to retrieve
1187 * @nbytes: number of bytes to retrieve
1188 * @iter: section iterator
1190 * Extract bytes attached to a specific category (ECC or free)
1191 * from the OOB buffer and copy them into buf.
1193 * Returns zero on success, a negative error code otherwise.
1195 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1196 const u8 *oobbuf, int start, int nbytes,
1197 int (*iter)(struct mtd_info *,
1199 struct mtd_oob_region *oobregion))
1201 struct mtd_oob_region oobregion;
1204 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1210 cnt = min_t(int, nbytes, oobregion.length);
1211 memcpy(buf, oobbuf + oobregion.offset, cnt);
1218 ret = iter(mtd, ++section, &oobregion);
1225 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1226 * @mtd: mtd info structure
1227 * @buf: source buffer to get OOB bytes from
1228 * @oobbuf: OOB buffer
1229 * @start: first OOB byte to set
1230 * @nbytes: number of OOB bytes to set
1231 * @iter: section iterator
1233 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1234 * is selected by passing the appropriate iterator.
1236 * Returns zero on success, a negative error code otherwise.
1238 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1239 u8 *oobbuf, int start, int nbytes,
1240 int (*iter)(struct mtd_info *,
1242 struct mtd_oob_region *oobregion))
1244 struct mtd_oob_region oobregion;
1247 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1253 cnt = min_t(int, nbytes, oobregion.length);
1254 memcpy(oobbuf + oobregion.offset, buf, cnt);
1261 ret = iter(mtd, ++section, &oobregion);
1268 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1269 * @mtd: mtd info structure
1270 * @iter: category iterator
1272 * Count the number of bytes in a given category.
1274 * Returns a positive value on success, a negative error code otherwise.
1276 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1277 int (*iter)(struct mtd_info *,
1279 struct mtd_oob_region *oobregion))
1281 struct mtd_oob_region oobregion;
1282 int section = 0, ret, nbytes = 0;
1285 ret = iter(mtd, section++, &oobregion);
1292 nbytes += oobregion.length;
1299 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1300 * @mtd: mtd info structure
1301 * @eccbuf: destination buffer to store ECC bytes
1302 * @oobbuf: OOB buffer
1303 * @start: first ECC byte to retrieve
1304 * @nbytes: number of ECC bytes to retrieve
1306 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1308 * Returns zero on success, a negative error code otherwise.
1310 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1311 const u8 *oobbuf, int start, int nbytes)
1313 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1316 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1319 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1320 * @mtd: mtd info structure
1321 * @eccbuf: source buffer to get ECC bytes from
1322 * @oobbuf: OOB buffer
1323 * @start: first ECC byte to set
1324 * @nbytes: number of ECC bytes to set
1326 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1328 * Returns zero on success, a negative error code otherwise.
1330 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1331 u8 *oobbuf, int start, int nbytes)
1333 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1336 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1339 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1340 * @mtd: mtd info structure
1341 * @databuf: destination buffer to store ECC bytes
1342 * @oobbuf: OOB buffer
1343 * @start: first ECC byte to retrieve
1344 * @nbytes: number of ECC bytes to retrieve
1346 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1348 * Returns zero on success, a negative error code otherwise.
1350 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1351 const u8 *oobbuf, int start, int nbytes)
1353 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1354 mtd_ooblayout_free);
1356 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1359 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1360 * @mtd: mtd info structure
1361 * @eccbuf: source buffer to get data bytes from
1362 * @oobbuf: OOB buffer
1363 * @start: first ECC byte to set
1364 * @nbytes: number of ECC bytes to set
1366 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1368 * Returns zero on success, a negative error code otherwise.
1370 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1371 u8 *oobbuf, int start, int nbytes)
1373 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1374 mtd_ooblayout_free);
1376 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1379 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1380 * @mtd: mtd info structure
1382 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1384 * Returns zero on success, a negative error code otherwise.
1386 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1388 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1390 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1393 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1394 * @mtd: mtd info structure
1396 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1398 * Returns zero on success, a negative error code otherwise.
1400 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1402 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1404 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1407 * Method to access the protection register area, present in some flash
1408 * devices. The user data is one time programmable but the factory data is read
1411 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1412 struct otp_info *buf)
1414 if (!mtd->_get_fact_prot_info)
1418 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1420 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1422 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1423 size_t *retlen, u_char *buf)
1426 if (!mtd->_read_fact_prot_reg)
1430 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1432 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1434 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1435 struct otp_info *buf)
1437 if (!mtd->_get_user_prot_info)
1441 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1443 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1445 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1446 size_t *retlen, u_char *buf)
1449 if (!mtd->_read_user_prot_reg)
1453 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1455 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1457 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1458 size_t *retlen, u_char *buf)
1463 if (!mtd->_write_user_prot_reg)
1467 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1472 * If no data could be written at all, we are out of memory and
1473 * must return -ENOSPC.
1475 return (*retlen) ? 0 : -ENOSPC;
1477 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1479 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1481 if (!mtd->_lock_user_prot_reg)
1485 return mtd->_lock_user_prot_reg(mtd, from, len);
1487 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1489 /* Chip-supported device locking */
1490 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1494 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1498 return mtd->_lock(mtd, ofs, len);
1500 EXPORT_SYMBOL_GPL(mtd_lock);
1502 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1506 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1510 return mtd->_unlock(mtd, ofs, len);
1512 EXPORT_SYMBOL_GPL(mtd_unlock);
1514 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1516 if (!mtd->_is_locked)
1518 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1522 return mtd->_is_locked(mtd, ofs, len);
1524 EXPORT_SYMBOL_GPL(mtd_is_locked);
1526 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1528 if (ofs < 0 || ofs > mtd->size)
1530 if (!mtd->_block_isreserved)
1532 return mtd->_block_isreserved(mtd, ofs);
1534 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1536 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1538 if (ofs < 0 || ofs > mtd->size)
1540 if (!mtd->_block_isbad)
1542 return mtd->_block_isbad(mtd, ofs);
1544 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1546 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1548 if (!mtd->_block_markbad)
1550 if (ofs < 0 || ofs > mtd->size)
1552 if (!(mtd->flags & MTD_WRITEABLE))
1554 return mtd->_block_markbad(mtd, ofs);
1556 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1560 * default_mtd_writev - the default writev method
1561 * @mtd: mtd device description object pointer
1562 * @vecs: the vectors to write
1563 * @count: count of vectors in @vecs
1564 * @to: the MTD device offset to write to
1565 * @retlen: on exit contains the count of bytes written to the MTD device.
1567 * This function returns zero in case of success and a negative error code in
1570 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1571 unsigned long count, loff_t to, size_t *retlen)
1574 size_t totlen = 0, thislen;
1577 for (i = 0; i < count; i++) {
1578 if (!vecs[i].iov_len)
1580 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1583 if (ret || thislen != vecs[i].iov_len)
1585 to += vecs[i].iov_len;
1592 * mtd_writev - the vector-based MTD write method
1593 * @mtd: mtd device description object pointer
1594 * @vecs: the vectors to write
1595 * @count: count of vectors in @vecs
1596 * @to: the MTD device offset to write to
1597 * @retlen: on exit contains the count of bytes written to the MTD device.
1599 * This function returns zero in case of success and a negative error code in
1602 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1603 unsigned long count, loff_t to, size_t *retlen)
1606 if (!(mtd->flags & MTD_WRITEABLE))
1609 return default_mtd_writev(mtd, vecs, count, to, retlen);
1610 return mtd->_writev(mtd, vecs, count, to, retlen);
1612 EXPORT_SYMBOL_GPL(mtd_writev);
1615 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1616 * @mtd: mtd device description object pointer
1617 * @size: a pointer to the ideal or maximum size of the allocation, points
1618 * to the actual allocation size on success.
1620 * This routine attempts to allocate a contiguous kernel buffer up to
1621 * the specified size, backing off the size of the request exponentially
1622 * until the request succeeds or until the allocation size falls below
1623 * the system page size. This attempts to make sure it does not adversely
1624 * impact system performance, so when allocating more than one page, we
1625 * ask the memory allocator to avoid re-trying, swapping, writing back
1626 * or performing I/O.
1628 * Note, this function also makes sure that the allocated buffer is aligned to
1629 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1631 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1632 * to handle smaller (i.e. degraded) buffer allocations under low- or
1633 * fragmented-memory situations where such reduced allocations, from a
1634 * requested ideal, are allowed.
1636 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1638 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1640 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1641 __GFP_NORETRY | __GFP_NO_KSWAPD;
1642 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1645 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1647 while (*size > min_alloc) {
1648 kbuf = kmalloc(*size, flags);
1653 *size = ALIGN(*size, mtd->writesize);
1657 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1658 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1660 return kmalloc(*size, GFP_KERNEL);
1662 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1665 #ifdef CONFIG_PROC_FS
1667 /*====================================================================*/
1668 /* Support for /proc/mtd */
1670 static int mtd_proc_show(struct seq_file *m, void *v)
1672 struct mtd_info *mtd;
1674 seq_puts(m, "dev: size erasesize name\n");
1675 mutex_lock(&mtd_table_mutex);
1676 mtd_for_each_device(mtd) {
1677 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1678 mtd->index, (unsigned long long)mtd->size,
1679 mtd->erasesize, mtd->name);
1681 mutex_unlock(&mtd_table_mutex);
1685 static int mtd_proc_open(struct inode *inode, struct file *file)
1687 return single_open(file, mtd_proc_show, NULL);
1690 static const struct file_operations mtd_proc_ops = {
1691 .open = mtd_proc_open,
1693 .llseek = seq_lseek,
1694 .release = single_release,
1696 #endif /* CONFIG_PROC_FS */
1698 /*====================================================================*/
1702 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1706 ret = bdi_init(bdi);
1708 ret = bdi_register(bdi, NULL, "%s", name);
1716 static struct proc_dir_entry *proc_mtd;
1718 static int __init init_mtd(void)
1722 ret = class_register(&mtd_class);
1726 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1730 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1734 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1738 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1740 ret = init_mtdchar();
1748 remove_proc_entry("mtd", NULL);
1750 bdi_destroy(&mtd_bdi_ro_mappable);
1752 bdi_destroy(&mtd_bdi_unmappable);
1754 class_unregister(&mtd_class);
1756 pr_err("Error registering mtd class or bdi: %d\n", ret);
1760 static void __exit cleanup_mtd(void)
1764 remove_proc_entry("mtd", NULL);
1765 class_unregister(&mtd_class);
1766 bdi_destroy(&mtd_bdi_unmappable);
1767 bdi_destroy(&mtd_bdi_ro_mappable);
1768 bdi_destroy(&mtd_bdi_rw_mappable);
1771 module_init(init_mtd);
1772 module_exit(cleanup_mtd);
1775 MODULE_LICENSE("GPL");
1776 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1777 MODULE_DESCRIPTION("Core MTD registration and access routines");