18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Core registration and callback routines for MTD 48c2ecf20Sopenharmony_ci * drivers and users. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> 78c2ecf20Sopenharmony_ci * Copyright © 2006 Red Hat UK Limited 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/ptrace.h> 138c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 148c2ecf20Sopenharmony_ci#include <linux/string.h> 158c2ecf20Sopenharmony_ci#include <linux/timer.h> 168c2ecf20Sopenharmony_ci#include <linux/major.h> 178c2ecf20Sopenharmony_ci#include <linux/fs.h> 188c2ecf20Sopenharmony_ci#include <linux/err.h> 198c2ecf20Sopenharmony_ci#include <linux/ioctl.h> 208c2ecf20Sopenharmony_ci#include <linux/init.h> 218c2ecf20Sopenharmony_ci#include <linux/of.h> 228c2ecf20Sopenharmony_ci#include <linux/proc_fs.h> 238c2ecf20Sopenharmony_ci#include <linux/idr.h> 248c2ecf20Sopenharmony_ci#include <linux/backing-dev.h> 258c2ecf20Sopenharmony_ci#include <linux/gfp.h> 268c2ecf20Sopenharmony_ci#include <linux/slab.h> 278c2ecf20Sopenharmony_ci#include <linux/reboot.h> 288c2ecf20Sopenharmony_ci#include <linux/leds.h> 298c2ecf20Sopenharmony_ci#include <linux/debugfs.h> 308c2ecf20Sopenharmony_ci#include <linux/nvmem-provider.h> 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h> 338c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h> 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#include "mtdcore.h" 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistruct backing_dev_info *mtd_bdi; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic int mtd_cls_suspend(struct device *dev) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci return mtd ? mtd_suspend(mtd) : 0; 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic int mtd_cls_resume(struct device *dev) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci if (mtd) 538c2ecf20Sopenharmony_ci mtd_resume(mtd); 548c2ecf20Sopenharmony_ci return 0; 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops, mtd_cls_suspend, mtd_cls_resume); 588c2ecf20Sopenharmony_ci#define MTD_CLS_PM_OPS (&mtd_cls_pm_ops) 598c2ecf20Sopenharmony_ci#else 608c2ecf20Sopenharmony_ci#define MTD_CLS_PM_OPS NULL 618c2ecf20Sopenharmony_ci#endif 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic struct class mtd_class = { 648c2ecf20Sopenharmony_ci .name = "mtd", 658c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 668c2ecf20Sopenharmony_ci .pm = MTD_CLS_PM_OPS, 678c2ecf20Sopenharmony_ci}; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic DEFINE_IDR(mtd_idr); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/* These are exported solely for the purpose of mtd_blkdevs.c. You 728c2ecf20Sopenharmony_ci should not use them for _anything_ else */ 738c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(mtd_table_mutex); 748c2ecf20Sopenharmony_cistatic int mtd_table_mutex_depth; 758c2ecf20Sopenharmony_cistatic struct task_struct *mtd_table_mutex_owner; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistruct mtd_info *__mtd_next_device(int i) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci return idr_get_next(&mtd_idr, &i); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__mtd_next_device); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic LIST_HEAD(mtd_notifiers); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_civoid mtd_table_mutex_lock(void) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci if (mtd_table_mutex_owner != current) { 918c2ecf20Sopenharmony_ci mutex_lock(&mtd_table_mutex); 928c2ecf20Sopenharmony_ci mtd_table_mutex_owner = current; 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci mtd_table_mutex_depth++; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_table_mutex_lock); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_civoid mtd_table_mutex_unlock(void) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci if (mtd_table_mutex_owner != current) { 1028c2ecf20Sopenharmony_ci pr_err("MTD:lock_owner is %s, but current is %s\n", 1038c2ecf20Sopenharmony_ci mtd_table_mutex_owner->comm, current->comm); 1048c2ecf20Sopenharmony_ci BUG(); 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci if (--mtd_table_mutex_depth == 0) { 1078c2ecf20Sopenharmony_ci mtd_table_mutex_owner = NULL; 1088c2ecf20Sopenharmony_ci mutex_unlock(&mtd_table_mutex); 1098c2ecf20Sopenharmony_ci } 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_table_mutex_unlock); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_civoid mtd_table_assert_mutex_locked(void) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci if (mtd_table_mutex_owner != current) { 1168c2ecf20Sopenharmony_ci pr_err("MTD:lock_owner is %s, but current is %s\n", 1178c2ecf20Sopenharmony_ci mtd_table_mutex_owner->comm, current->comm); 1188c2ecf20Sopenharmony_ci BUG(); 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_table_assert_mutex_locked); 1228c2ecf20Sopenharmony_ci/* REVISIT once MTD uses the driver model better, whoever allocates 1238c2ecf20Sopenharmony_ci * the mtd_info will probably want to use the release() hook... 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_cistatic void mtd_release(struct device *dev) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 1288c2ecf20Sopenharmony_ci dev_t index = MTD_DEVT(mtd->index); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci /* remove /dev/mtdXro node */ 1318c2ecf20Sopenharmony_ci device_destroy(&mtd_class, index + 1); 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic ssize_t mtd_type_show(struct device *dev, 1358c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 1388c2ecf20Sopenharmony_ci char *type; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci switch (mtd->type) { 1418c2ecf20Sopenharmony_ci case MTD_ABSENT: 1428c2ecf20Sopenharmony_ci type = "absent"; 1438c2ecf20Sopenharmony_ci break; 1448c2ecf20Sopenharmony_ci case MTD_RAM: 1458c2ecf20Sopenharmony_ci type = "ram"; 1468c2ecf20Sopenharmony_ci break; 1478c2ecf20Sopenharmony_ci case MTD_ROM: 1488c2ecf20Sopenharmony_ci type = "rom"; 1498c2ecf20Sopenharmony_ci break; 1508c2ecf20Sopenharmony_ci case MTD_NORFLASH: 1518c2ecf20Sopenharmony_ci type = "nor"; 1528c2ecf20Sopenharmony_ci break; 1538c2ecf20Sopenharmony_ci case MTD_NANDFLASH: 1548c2ecf20Sopenharmony_ci type = "nand"; 1558c2ecf20Sopenharmony_ci break; 1568c2ecf20Sopenharmony_ci case MTD_DATAFLASH: 1578c2ecf20Sopenharmony_ci type = "dataflash"; 1588c2ecf20Sopenharmony_ci break; 1598c2ecf20Sopenharmony_ci case MTD_UBIVOLUME: 1608c2ecf20Sopenharmony_ci type = "ubi"; 1618c2ecf20Sopenharmony_ci break; 1628c2ecf20Sopenharmony_ci case MTD_MLCNANDFLASH: 1638c2ecf20Sopenharmony_ci type = "mlc-nand"; 1648c2ecf20Sopenharmony_ci break; 1658c2ecf20Sopenharmony_ci default: 1668c2ecf20Sopenharmony_ci type = "unknown"; 1678c2ecf20Sopenharmony_ci } 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%s\n", type); 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_cistatic DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic ssize_t mtd_flags_show(struct device *dev, 1748c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_cistatic DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_cistatic ssize_t mtd_size_show(struct device *dev, 1838c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%llu\n", 1888c2ecf20Sopenharmony_ci (unsigned long long)mtd->size); 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_cistatic DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic ssize_t mtd_erasesize_show(struct device *dev, 1938c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_cistatic DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistatic ssize_t mtd_writesize_show(struct device *dev, 2028c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_cistatic DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic ssize_t mtd_subpagesize_show(struct device *dev, 2118c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2148c2ecf20Sopenharmony_ci unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_cistatic DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic ssize_t mtd_oobsize_show(struct device *dev, 2218c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_cistatic DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_cistatic ssize_t mtd_oobavail_show(struct device *dev, 2308c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", mtd->oobavail); 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_cistatic DEVICE_ATTR(oobavail, S_IRUGO, mtd_oobavail_show, NULL); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cistatic ssize_t mtd_numeraseregions_show(struct device *dev, 2398c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_cistatic DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, 2468c2ecf20Sopenharmony_ci NULL); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic ssize_t mtd_name_show(struct device *dev, 2498c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2508c2ecf20Sopenharmony_ci{ 2518c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_cistatic DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_cistatic ssize_t mtd_ecc_strength_show(struct device *dev, 2588c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_cistatic DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cistatic ssize_t mtd_bitflip_threshold_show(struct device *dev, 2678c2ecf20Sopenharmony_ci struct device_attribute *attr, 2688c2ecf20Sopenharmony_ci char *buf) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_cistatic ssize_t mtd_bitflip_threshold_store(struct device *dev, 2768c2ecf20Sopenharmony_ci struct device_attribute *attr, 2778c2ecf20Sopenharmony_ci const char *buf, size_t count) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2808c2ecf20Sopenharmony_ci unsigned int bitflip_threshold; 2818c2ecf20Sopenharmony_ci int retval; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci retval = kstrtouint(buf, 0, &bitflip_threshold); 2848c2ecf20Sopenharmony_ci if (retval) 2858c2ecf20Sopenharmony_ci return retval; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci mtd->bitflip_threshold = bitflip_threshold; 2888c2ecf20Sopenharmony_ci return count; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_cistatic DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR, 2918c2ecf20Sopenharmony_ci mtd_bitflip_threshold_show, 2928c2ecf20Sopenharmony_ci mtd_bitflip_threshold_store); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_cistatic ssize_t mtd_ecc_step_size_show(struct device *dev, 2958c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_cistatic DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cistatic ssize_t mtd_ecc_stats_corrected_show(struct device *dev, 3058c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 3068c2ecf20Sopenharmony_ci{ 3078c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 3088c2ecf20Sopenharmony_ci struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected); 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_cistatic DEVICE_ATTR(corrected_bits, S_IRUGO, 3138c2ecf20Sopenharmony_ci mtd_ecc_stats_corrected_show, NULL); 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_cistatic ssize_t mtd_ecc_stats_errors_show(struct device *dev, 3168c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 3178c2ecf20Sopenharmony_ci{ 3188c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 3198c2ecf20Sopenharmony_ci struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed); 3228c2ecf20Sopenharmony_ci} 3238c2ecf20Sopenharmony_cistatic DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_cistatic ssize_t mtd_badblocks_show(struct device *dev, 3268c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 3278c2ecf20Sopenharmony_ci{ 3288c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 3298c2ecf20Sopenharmony_ci struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks); 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_cistatic DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_cistatic ssize_t mtd_bbtblocks_show(struct device *dev, 3368c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 3378c2ecf20Sopenharmony_ci{ 3388c2ecf20Sopenharmony_ci struct mtd_info *mtd = dev_get_drvdata(dev); 3398c2ecf20Sopenharmony_ci struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks); 3428c2ecf20Sopenharmony_ci} 3438c2ecf20Sopenharmony_cistatic DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_cistatic struct attribute *mtd_attrs[] = { 3468c2ecf20Sopenharmony_ci &dev_attr_type.attr, 3478c2ecf20Sopenharmony_ci &dev_attr_flags.attr, 3488c2ecf20Sopenharmony_ci &dev_attr_size.attr, 3498c2ecf20Sopenharmony_ci &dev_attr_erasesize.attr, 3508c2ecf20Sopenharmony_ci &dev_attr_writesize.attr, 3518c2ecf20Sopenharmony_ci &dev_attr_subpagesize.attr, 3528c2ecf20Sopenharmony_ci &dev_attr_oobsize.attr, 3538c2ecf20Sopenharmony_ci &dev_attr_oobavail.attr, 3548c2ecf20Sopenharmony_ci &dev_attr_numeraseregions.attr, 3558c2ecf20Sopenharmony_ci &dev_attr_name.attr, 3568c2ecf20Sopenharmony_ci &dev_attr_ecc_strength.attr, 3578c2ecf20Sopenharmony_ci &dev_attr_ecc_step_size.attr, 3588c2ecf20Sopenharmony_ci &dev_attr_corrected_bits.attr, 3598c2ecf20Sopenharmony_ci &dev_attr_ecc_failures.attr, 3608c2ecf20Sopenharmony_ci &dev_attr_bad_blocks.attr, 3618c2ecf20Sopenharmony_ci &dev_attr_bbt_blocks.attr, 3628c2ecf20Sopenharmony_ci &dev_attr_bitflip_threshold.attr, 3638c2ecf20Sopenharmony_ci NULL, 3648c2ecf20Sopenharmony_ci}; 3658c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(mtd); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_cistatic const struct device_type mtd_devtype = { 3688c2ecf20Sopenharmony_ci .name = "mtd", 3698c2ecf20Sopenharmony_ci .groups = mtd_groups, 3708c2ecf20Sopenharmony_ci .release = mtd_release, 3718c2ecf20Sopenharmony_ci}; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_cistatic int mtd_partid_debug_show(struct seq_file *s, void *p) 3748c2ecf20Sopenharmony_ci{ 3758c2ecf20Sopenharmony_ci struct mtd_info *mtd = s->private; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci seq_printf(s, "%s\n", mtd->dbg.partid); 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci return 0; 3808c2ecf20Sopenharmony_ci} 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(mtd_partid_debug); 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_cistatic int mtd_partname_debug_show(struct seq_file *s, void *p) 3858c2ecf20Sopenharmony_ci{ 3868c2ecf20Sopenharmony_ci struct mtd_info *mtd = s->private; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci seq_printf(s, "%s\n", mtd->dbg.partname); 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci return 0; 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(mtd_partname_debug); 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_cistatic struct dentry *dfs_dir_mtd; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_cistatic void mtd_debugfs_populate(struct mtd_info *mtd) 3988c2ecf20Sopenharmony_ci{ 3998c2ecf20Sopenharmony_ci struct device *dev = &mtd->dev; 4008c2ecf20Sopenharmony_ci struct dentry *root; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci if (IS_ERR_OR_NULL(dfs_dir_mtd)) 4038c2ecf20Sopenharmony_ci return; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci root = debugfs_create_dir(dev_name(dev), dfs_dir_mtd); 4068c2ecf20Sopenharmony_ci mtd->dbg.dfs_dir = root; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci if (mtd->dbg.partid) 4098c2ecf20Sopenharmony_ci debugfs_create_file("partid", 0400, root, mtd, 4108c2ecf20Sopenharmony_ci &mtd_partid_debug_fops); 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci if (mtd->dbg.partname) 4138c2ecf20Sopenharmony_ci debugfs_create_file("partname", 0400, root, mtd, 4148c2ecf20Sopenharmony_ci &mtd_partname_debug_fops); 4158c2ecf20Sopenharmony_ci} 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci#ifndef CONFIG_MMU 4188c2ecf20Sopenharmony_ciunsigned mtd_mmap_capabilities(struct mtd_info *mtd) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci switch (mtd->type) { 4218c2ecf20Sopenharmony_ci case MTD_RAM: 4228c2ecf20Sopenharmony_ci return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | 4238c2ecf20Sopenharmony_ci NOMMU_MAP_READ | NOMMU_MAP_WRITE; 4248c2ecf20Sopenharmony_ci case MTD_ROM: 4258c2ecf20Sopenharmony_ci return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | 4268c2ecf20Sopenharmony_ci NOMMU_MAP_READ; 4278c2ecf20Sopenharmony_ci default: 4288c2ecf20Sopenharmony_ci return NOMMU_MAP_COPY; 4298c2ecf20Sopenharmony_ci } 4308c2ecf20Sopenharmony_ci} 4318c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_mmap_capabilities); 4328c2ecf20Sopenharmony_ci#endif 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_cistatic int mtd_reboot_notifier(struct notifier_block *n, unsigned long state, 4358c2ecf20Sopenharmony_ci void *cmd) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci struct mtd_info *mtd; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci mtd = container_of(n, struct mtd_info, reboot_notifier); 4408c2ecf20Sopenharmony_ci mtd->_reboot(mtd); 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci return NOTIFY_DONE; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci/** 4468c2ecf20Sopenharmony_ci * mtd_wunit_to_pairing_info - get pairing information of a wunit 4478c2ecf20Sopenharmony_ci * @mtd: pointer to new MTD device info structure 4488c2ecf20Sopenharmony_ci * @wunit: write unit we are interested in 4498c2ecf20Sopenharmony_ci * @info: returned pairing information 4508c2ecf20Sopenharmony_ci * 4518c2ecf20Sopenharmony_ci * Retrieve pairing information associated to the wunit. 4528c2ecf20Sopenharmony_ci * This is mainly useful when dealing with MLC/TLC NANDs where pages can be 4538c2ecf20Sopenharmony_ci * paired together, and where programming a page may influence the page it is 4548c2ecf20Sopenharmony_ci * paired with. 4558c2ecf20Sopenharmony_ci * The notion of page is replaced by the term wunit (write-unit) to stay 4568c2ecf20Sopenharmony_ci * consistent with the ->writesize field. 4578c2ecf20Sopenharmony_ci * 4588c2ecf20Sopenharmony_ci * The @wunit argument can be extracted from an absolute offset using 4598c2ecf20Sopenharmony_ci * mtd_offset_to_wunit(). @info is filled with the pairing information attached 4608c2ecf20Sopenharmony_ci * to @wunit. 4618c2ecf20Sopenharmony_ci * 4628c2ecf20Sopenharmony_ci * From the pairing info the MTD user can find all the wunits paired with 4638c2ecf20Sopenharmony_ci * @wunit using the following loop: 4648c2ecf20Sopenharmony_ci * 4658c2ecf20Sopenharmony_ci * for (i = 0; i < mtd_pairing_groups(mtd); i++) { 4668c2ecf20Sopenharmony_ci * info.pair = i; 4678c2ecf20Sopenharmony_ci * mtd_pairing_info_to_wunit(mtd, &info); 4688c2ecf20Sopenharmony_ci * ... 4698c2ecf20Sopenharmony_ci * } 4708c2ecf20Sopenharmony_ci */ 4718c2ecf20Sopenharmony_ciint mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit, 4728c2ecf20Sopenharmony_ci struct mtd_pairing_info *info) 4738c2ecf20Sopenharmony_ci{ 4748c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 4758c2ecf20Sopenharmony_ci int npairs = mtd_wunit_per_eb(master) / mtd_pairing_groups(master); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci if (wunit < 0 || wunit >= npairs) 4788c2ecf20Sopenharmony_ci return -EINVAL; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci if (master->pairing && master->pairing->get_info) 4818c2ecf20Sopenharmony_ci return master->pairing->get_info(master, wunit, info); 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci info->group = 0; 4848c2ecf20Sopenharmony_ci info->pair = wunit; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci return 0; 4878c2ecf20Sopenharmony_ci} 4888c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info); 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci/** 4918c2ecf20Sopenharmony_ci * mtd_pairing_info_to_wunit - get wunit from pairing information 4928c2ecf20Sopenharmony_ci * @mtd: pointer to new MTD device info structure 4938c2ecf20Sopenharmony_ci * @info: pairing information struct 4948c2ecf20Sopenharmony_ci * 4958c2ecf20Sopenharmony_ci * Returns a positive number representing the wunit associated to the info 4968c2ecf20Sopenharmony_ci * struct, or a negative error code. 4978c2ecf20Sopenharmony_ci * 4988c2ecf20Sopenharmony_ci * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to 4998c2ecf20Sopenharmony_ci * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info() 5008c2ecf20Sopenharmony_ci * doc). 5018c2ecf20Sopenharmony_ci * 5028c2ecf20Sopenharmony_ci * It can also be used to only program the first page of each pair (i.e. 5038c2ecf20Sopenharmony_ci * page attached to group 0), which allows one to use an MLC NAND in 5048c2ecf20Sopenharmony_ci * software-emulated SLC mode: 5058c2ecf20Sopenharmony_ci * 5068c2ecf20Sopenharmony_ci * info.group = 0; 5078c2ecf20Sopenharmony_ci * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd); 5088c2ecf20Sopenharmony_ci * for (info.pair = 0; info.pair < npairs; info.pair++) { 5098c2ecf20Sopenharmony_ci * wunit = mtd_pairing_info_to_wunit(mtd, &info); 5108c2ecf20Sopenharmony_ci * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit), 5118c2ecf20Sopenharmony_ci * mtd->writesize, &retlen, buf + (i * mtd->writesize)); 5128c2ecf20Sopenharmony_ci * } 5138c2ecf20Sopenharmony_ci */ 5148c2ecf20Sopenharmony_ciint mtd_pairing_info_to_wunit(struct mtd_info *mtd, 5158c2ecf20Sopenharmony_ci const struct mtd_pairing_info *info) 5168c2ecf20Sopenharmony_ci{ 5178c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 5188c2ecf20Sopenharmony_ci int ngroups = mtd_pairing_groups(master); 5198c2ecf20Sopenharmony_ci int npairs = mtd_wunit_per_eb(master) / ngroups; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci if (!info || info->pair < 0 || info->pair >= npairs || 5228c2ecf20Sopenharmony_ci info->group < 0 || info->group >= ngroups) 5238c2ecf20Sopenharmony_ci return -EINVAL; 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci if (master->pairing && master->pairing->get_wunit) 5268c2ecf20Sopenharmony_ci return mtd->pairing->get_wunit(master, info); 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci return info->pair; 5298c2ecf20Sopenharmony_ci} 5308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit); 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci/** 5338c2ecf20Sopenharmony_ci * mtd_pairing_groups - get the number of pairing groups 5348c2ecf20Sopenharmony_ci * @mtd: pointer to new MTD device info structure 5358c2ecf20Sopenharmony_ci * 5368c2ecf20Sopenharmony_ci * Returns the number of pairing groups. 5378c2ecf20Sopenharmony_ci * 5388c2ecf20Sopenharmony_ci * This number is usually equal to the number of bits exposed by a single 5398c2ecf20Sopenharmony_ci * cell, and can be used in conjunction with mtd_pairing_info_to_wunit() 5408c2ecf20Sopenharmony_ci * to iterate over all pages of a given pair. 5418c2ecf20Sopenharmony_ci */ 5428c2ecf20Sopenharmony_ciint mtd_pairing_groups(struct mtd_info *mtd) 5438c2ecf20Sopenharmony_ci{ 5448c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci if (!master->pairing || !master->pairing->ngroups) 5478c2ecf20Sopenharmony_ci return 1; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci return master->pairing->ngroups; 5508c2ecf20Sopenharmony_ci} 5518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_pairing_groups); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_cistatic int mtd_nvmem_reg_read(void *priv, unsigned int offset, 5548c2ecf20Sopenharmony_ci void *val, size_t bytes) 5558c2ecf20Sopenharmony_ci{ 5568c2ecf20Sopenharmony_ci struct mtd_info *mtd = priv; 5578c2ecf20Sopenharmony_ci size_t retlen; 5588c2ecf20Sopenharmony_ci int err; 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci err = mtd_read(mtd, offset, bytes, &retlen, val); 5618c2ecf20Sopenharmony_ci if (err && err != -EUCLEAN) 5628c2ecf20Sopenharmony_ci return err; 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci return retlen == bytes ? 0 : -EIO; 5658c2ecf20Sopenharmony_ci} 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_cistatic int mtd_nvmem_add(struct mtd_info *mtd) 5688c2ecf20Sopenharmony_ci{ 5698c2ecf20Sopenharmony_ci struct nvmem_config config = {}; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci config.id = -1; 5728c2ecf20Sopenharmony_ci config.dev = &mtd->dev; 5738c2ecf20Sopenharmony_ci config.name = dev_name(&mtd->dev); 5748c2ecf20Sopenharmony_ci config.owner = THIS_MODULE; 5758c2ecf20Sopenharmony_ci config.reg_read = mtd_nvmem_reg_read; 5768c2ecf20Sopenharmony_ci config.size = mtd->size; 5778c2ecf20Sopenharmony_ci config.word_size = 1; 5788c2ecf20Sopenharmony_ci config.stride = 1; 5798c2ecf20Sopenharmony_ci config.read_only = true; 5808c2ecf20Sopenharmony_ci config.root_only = true; 5818c2ecf20Sopenharmony_ci config.no_of_node = true; 5828c2ecf20Sopenharmony_ci config.priv = mtd; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci mtd->nvmem = nvmem_register(&config); 5858c2ecf20Sopenharmony_ci if (IS_ERR(mtd->nvmem)) { 5868c2ecf20Sopenharmony_ci /* Just ignore if there is no NVMEM support in the kernel */ 5878c2ecf20Sopenharmony_ci if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) { 5888c2ecf20Sopenharmony_ci mtd->nvmem = NULL; 5898c2ecf20Sopenharmony_ci } else { 5908c2ecf20Sopenharmony_ci dev_err(&mtd->dev, "Failed to register NVMEM device\n"); 5918c2ecf20Sopenharmony_ci return PTR_ERR(mtd->nvmem); 5928c2ecf20Sopenharmony_ci } 5938c2ecf20Sopenharmony_ci } 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci return 0; 5968c2ecf20Sopenharmony_ci} 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci/** 5998c2ecf20Sopenharmony_ci * add_mtd_device - register an MTD device 6008c2ecf20Sopenharmony_ci * @mtd: pointer to new MTD device info structure 6018c2ecf20Sopenharmony_ci * 6028c2ecf20Sopenharmony_ci * Add a device to the list of MTD devices present in the system, and 6038c2ecf20Sopenharmony_ci * notify each currently active MTD 'user' of its arrival. Returns 6048c2ecf20Sopenharmony_ci * zero on success or non-zero on failure. 6058c2ecf20Sopenharmony_ci */ 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_ciint add_mtd_device(struct mtd_info *mtd) 6088c2ecf20Sopenharmony_ci{ 6098c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 6108c2ecf20Sopenharmony_ci struct mtd_notifier *not; 6118c2ecf20Sopenharmony_ci int i, error; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci /* 6148c2ecf20Sopenharmony_ci * May occur, for instance, on buggy drivers which call 6158c2ecf20Sopenharmony_ci * mtd_device_parse_register() multiple times on the same master MTD, 6168c2ecf20Sopenharmony_ci * especially with CONFIG_MTD_PARTITIONED_MASTER=y. 6178c2ecf20Sopenharmony_ci */ 6188c2ecf20Sopenharmony_ci if (WARN_ONCE(mtd->dev.type, "MTD already registered\n")) 6198c2ecf20Sopenharmony_ci return -EEXIST; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci BUG_ON(mtd->writesize == 0); 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci /* 6248c2ecf20Sopenharmony_ci * MTD drivers should implement ->_{write,read}() or 6258c2ecf20Sopenharmony_ci * ->_{write,read}_oob(), but not both. 6268c2ecf20Sopenharmony_ci */ 6278c2ecf20Sopenharmony_ci if (WARN_ON((mtd->_write && mtd->_write_oob) || 6288c2ecf20Sopenharmony_ci (mtd->_read && mtd->_read_oob))) 6298c2ecf20Sopenharmony_ci return -EINVAL; 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci if (WARN_ON((!mtd->erasesize || !master->_erase) && 6328c2ecf20Sopenharmony_ci !(mtd->flags & MTD_NO_ERASE))) 6338c2ecf20Sopenharmony_ci return -EINVAL; 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci /* 6368c2ecf20Sopenharmony_ci * MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the 6378c2ecf20Sopenharmony_ci * master is an MLC NAND and has a proper pairing scheme defined. 6388c2ecf20Sopenharmony_ci * We also reject masters that implement ->_writev() for now, because 6398c2ecf20Sopenharmony_ci * NAND controller drivers don't implement this hook, and adding the 6408c2ecf20Sopenharmony_ci * SLC -> MLC address/length conversion to this path is useless if we 6418c2ecf20Sopenharmony_ci * don't have a user. 6428c2ecf20Sopenharmony_ci */ 6438c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION && 6448c2ecf20Sopenharmony_ci (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH || 6458c2ecf20Sopenharmony_ci !master->pairing || master->_writev)) 6468c2ecf20Sopenharmony_ci return -EINVAL; 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); 6518c2ecf20Sopenharmony_ci if (i < 0) { 6528c2ecf20Sopenharmony_ci error = i; 6538c2ecf20Sopenharmony_ci goto fail_locked; 6548c2ecf20Sopenharmony_ci } 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci mtd->index = i; 6578c2ecf20Sopenharmony_ci mtd->usecount = 0; 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_ci /* default value if not set by driver */ 6608c2ecf20Sopenharmony_ci if (mtd->bitflip_threshold == 0) 6618c2ecf20Sopenharmony_ci mtd->bitflip_threshold = mtd->ecc_strength; 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 6648c2ecf20Sopenharmony_ci int ngroups = mtd_pairing_groups(master); 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci mtd->erasesize /= ngroups; 6678c2ecf20Sopenharmony_ci mtd->size = (u64)mtd_div_by_eb(mtd->size, master) * 6688c2ecf20Sopenharmony_ci mtd->erasesize; 6698c2ecf20Sopenharmony_ci } 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_ci if (is_power_of_2(mtd->erasesize)) 6728c2ecf20Sopenharmony_ci mtd->erasesize_shift = ffs(mtd->erasesize) - 1; 6738c2ecf20Sopenharmony_ci else 6748c2ecf20Sopenharmony_ci mtd->erasesize_shift = 0; 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci if (is_power_of_2(mtd->writesize)) 6778c2ecf20Sopenharmony_ci mtd->writesize_shift = ffs(mtd->writesize) - 1; 6788c2ecf20Sopenharmony_ci else 6798c2ecf20Sopenharmony_ci mtd->writesize_shift = 0; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; 6828c2ecf20Sopenharmony_ci mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci /* Some chips always power up locked. Unlock them now */ 6858c2ecf20Sopenharmony_ci if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { 6868c2ecf20Sopenharmony_ci error = mtd_unlock(mtd, 0, mtd->size); 6878c2ecf20Sopenharmony_ci if (error && error != -EOPNOTSUPP) 6888c2ecf20Sopenharmony_ci printk(KERN_WARNING 6898c2ecf20Sopenharmony_ci "%s: unlock failed, writes may not work\n", 6908c2ecf20Sopenharmony_ci mtd->name); 6918c2ecf20Sopenharmony_ci /* Ignore unlock failures? */ 6928c2ecf20Sopenharmony_ci error = 0; 6938c2ecf20Sopenharmony_ci } 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci /* Caller should have set dev.parent to match the 6968c2ecf20Sopenharmony_ci * physical device, if appropriate. 6978c2ecf20Sopenharmony_ci */ 6988c2ecf20Sopenharmony_ci mtd->dev.type = &mtd_devtype; 6998c2ecf20Sopenharmony_ci mtd->dev.class = &mtd_class; 7008c2ecf20Sopenharmony_ci mtd->dev.devt = MTD_DEVT(i); 7018c2ecf20Sopenharmony_ci dev_set_name(&mtd->dev, "mtd%d", i); 7028c2ecf20Sopenharmony_ci dev_set_drvdata(&mtd->dev, mtd); 7038c2ecf20Sopenharmony_ci of_node_get(mtd_get_of_node(mtd)); 7048c2ecf20Sopenharmony_ci error = device_register(&mtd->dev); 7058c2ecf20Sopenharmony_ci if (error) { 7068c2ecf20Sopenharmony_ci put_device(&mtd->dev); 7078c2ecf20Sopenharmony_ci goto fail_added; 7088c2ecf20Sopenharmony_ci } 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci /* Add the nvmem provider */ 7118c2ecf20Sopenharmony_ci error = mtd_nvmem_add(mtd); 7128c2ecf20Sopenharmony_ci if (error) 7138c2ecf20Sopenharmony_ci goto fail_nvmem_add; 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci mtd_debugfs_populate(mtd); 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, 7188c2ecf20Sopenharmony_ci "mtd%dro", i); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); 7218c2ecf20Sopenharmony_ci /* No need to get a refcount on the module containing 7228c2ecf20Sopenharmony_ci the notifier, since we hold the mtd_table_mutex */ 7238c2ecf20Sopenharmony_ci list_for_each_entry(not, &mtd_notifiers, list) 7248c2ecf20Sopenharmony_ci not->add(mtd); 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 7278c2ecf20Sopenharmony_ci /* We _know_ we aren't being removed, because 7288c2ecf20Sopenharmony_ci our caller is still holding us here. So none 7298c2ecf20Sopenharmony_ci of this try_ nonsense, and no bitching about it 7308c2ecf20Sopenharmony_ci either. :) */ 7318c2ecf20Sopenharmony_ci __module_get(THIS_MODULE); 7328c2ecf20Sopenharmony_ci return 0; 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_cifail_nvmem_add: 7358c2ecf20Sopenharmony_ci device_unregister(&mtd->dev); 7368c2ecf20Sopenharmony_cifail_added: 7378c2ecf20Sopenharmony_ci of_node_put(mtd_get_of_node(mtd)); 7388c2ecf20Sopenharmony_ci idr_remove(&mtd_idr, i); 7398c2ecf20Sopenharmony_cifail_locked: 7408c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 7418c2ecf20Sopenharmony_ci return error; 7428c2ecf20Sopenharmony_ci} 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci/** 7458c2ecf20Sopenharmony_ci * del_mtd_device - unregister an MTD device 7468c2ecf20Sopenharmony_ci * @mtd: pointer to MTD device info structure 7478c2ecf20Sopenharmony_ci * 7488c2ecf20Sopenharmony_ci * Remove a device from the list of MTD devices present in the system, 7498c2ecf20Sopenharmony_ci * and notify each currently active MTD 'user' of its departure. 7508c2ecf20Sopenharmony_ci * Returns zero on success or 1 on failure, which currently will happen 7518c2ecf20Sopenharmony_ci * if the requested device does not appear to be present in the list. 7528c2ecf20Sopenharmony_ci */ 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ciint del_mtd_device(struct mtd_info *mtd) 7558c2ecf20Sopenharmony_ci{ 7568c2ecf20Sopenharmony_ci int ret; 7578c2ecf20Sopenharmony_ci struct mtd_notifier *not; 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 7608c2ecf20Sopenharmony_ci 7618c2ecf20Sopenharmony_ci if (idr_find(&mtd_idr, mtd->index) != mtd) { 7628c2ecf20Sopenharmony_ci ret = -ENODEV; 7638c2ecf20Sopenharmony_ci goto out_error; 7648c2ecf20Sopenharmony_ci } 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci /* No need to get a refcount on the module containing 7678c2ecf20Sopenharmony_ci the notifier, since we hold the mtd_table_mutex */ 7688c2ecf20Sopenharmony_ci list_for_each_entry(not, &mtd_notifiers, list) 7698c2ecf20Sopenharmony_ci not->remove(mtd); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci if (mtd->usecount) { 7728c2ecf20Sopenharmony_ci printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", 7738c2ecf20Sopenharmony_ci mtd->index, mtd->name, mtd->usecount); 7748c2ecf20Sopenharmony_ci ret = -EBUSY; 7758c2ecf20Sopenharmony_ci } else { 7768c2ecf20Sopenharmony_ci debugfs_remove_recursive(mtd->dbg.dfs_dir); 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci /* Try to remove the NVMEM provider */ 7798c2ecf20Sopenharmony_ci if (mtd->nvmem) 7808c2ecf20Sopenharmony_ci nvmem_unregister(mtd->nvmem); 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci device_unregister(&mtd->dev); 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci idr_remove(&mtd_idr, mtd->index); 7858c2ecf20Sopenharmony_ci of_node_put(mtd_get_of_node(mtd)); 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci module_put(THIS_MODULE); 7888c2ecf20Sopenharmony_ci ret = 0; 7898c2ecf20Sopenharmony_ci } 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ciout_error: 7928c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 7938c2ecf20Sopenharmony_ci return ret; 7948c2ecf20Sopenharmony_ci} 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci/* 7978c2ecf20Sopenharmony_ci * Set a few defaults based on the parent devices, if not provided by the 7988c2ecf20Sopenharmony_ci * driver 7998c2ecf20Sopenharmony_ci */ 8008c2ecf20Sopenharmony_cistatic void mtd_set_dev_defaults(struct mtd_info *mtd) 8018c2ecf20Sopenharmony_ci{ 8028c2ecf20Sopenharmony_ci if (mtd->dev.parent) { 8038c2ecf20Sopenharmony_ci if (!mtd->owner && mtd->dev.parent->driver) 8048c2ecf20Sopenharmony_ci mtd->owner = mtd->dev.parent->driver->owner; 8058c2ecf20Sopenharmony_ci if (!mtd->name) 8068c2ecf20Sopenharmony_ci mtd->name = dev_name(mtd->dev.parent); 8078c2ecf20Sopenharmony_ci } else { 8088c2ecf20Sopenharmony_ci pr_debug("mtd device won't show a device symlink in sysfs\n"); 8098c2ecf20Sopenharmony_ci } 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&mtd->partitions); 8128c2ecf20Sopenharmony_ci mutex_init(&mtd->master.partitions_lock); 8138c2ecf20Sopenharmony_ci} 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci/** 8168c2ecf20Sopenharmony_ci * mtd_device_parse_register - parse partitions and register an MTD device. 8178c2ecf20Sopenharmony_ci * 8188c2ecf20Sopenharmony_ci * @mtd: the MTD device to register 8198c2ecf20Sopenharmony_ci * @types: the list of MTD partition probes to try, see 8208c2ecf20Sopenharmony_ci * 'parse_mtd_partitions()' for more information 8218c2ecf20Sopenharmony_ci * @parser_data: MTD partition parser-specific data 8228c2ecf20Sopenharmony_ci * @parts: fallback partition information to register, if parsing fails; 8238c2ecf20Sopenharmony_ci * only valid if %nr_parts > %0 8248c2ecf20Sopenharmony_ci * @nr_parts: the number of partitions in parts, if zero then the full 8258c2ecf20Sopenharmony_ci * MTD device is registered if no partition info is found 8268c2ecf20Sopenharmony_ci * 8278c2ecf20Sopenharmony_ci * This function aggregates MTD partitions parsing (done by 8288c2ecf20Sopenharmony_ci * 'parse_mtd_partitions()') and MTD device and partitions registering. It 8298c2ecf20Sopenharmony_ci * basically follows the most common pattern found in many MTD drivers: 8308c2ecf20Sopenharmony_ci * 8318c2ecf20Sopenharmony_ci * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is 8328c2ecf20Sopenharmony_ci * registered first. 8338c2ecf20Sopenharmony_ci * * Then It tries to probe partitions on MTD device @mtd using parsers 8348c2ecf20Sopenharmony_ci * specified in @types (if @types is %NULL, then the default list of parsers 8358c2ecf20Sopenharmony_ci * is used, see 'parse_mtd_partitions()' for more information). If none are 8368c2ecf20Sopenharmony_ci * found this functions tries to fallback to information specified in 8378c2ecf20Sopenharmony_ci * @parts/@nr_parts. 8388c2ecf20Sopenharmony_ci * * If no partitions were found this function just registers the MTD device 8398c2ecf20Sopenharmony_ci * @mtd and exits. 8408c2ecf20Sopenharmony_ci * 8418c2ecf20Sopenharmony_ci * Returns zero in case of success and a negative error code in case of failure. 8428c2ecf20Sopenharmony_ci */ 8438c2ecf20Sopenharmony_ciint mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, 8448c2ecf20Sopenharmony_ci struct mtd_part_parser_data *parser_data, 8458c2ecf20Sopenharmony_ci const struct mtd_partition *parts, 8468c2ecf20Sopenharmony_ci int nr_parts) 8478c2ecf20Sopenharmony_ci{ 8488c2ecf20Sopenharmony_ci int ret; 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci mtd_set_dev_defaults(mtd); 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) { 8538c2ecf20Sopenharmony_ci ret = add_mtd_device(mtd); 8548c2ecf20Sopenharmony_ci if (ret) 8558c2ecf20Sopenharmony_ci return ret; 8568c2ecf20Sopenharmony_ci } 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ci /* Prefer parsed partitions over driver-provided fallback */ 8598c2ecf20Sopenharmony_ci ret = parse_mtd_partitions(mtd, types, parser_data); 8608c2ecf20Sopenharmony_ci if (ret == -EPROBE_DEFER) 8618c2ecf20Sopenharmony_ci goto out; 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci if (ret > 0) 8648c2ecf20Sopenharmony_ci ret = 0; 8658c2ecf20Sopenharmony_ci else if (nr_parts) 8668c2ecf20Sopenharmony_ci ret = add_mtd_partitions(mtd, parts, nr_parts); 8678c2ecf20Sopenharmony_ci else if (!device_is_registered(&mtd->dev)) 8688c2ecf20Sopenharmony_ci ret = add_mtd_device(mtd); 8698c2ecf20Sopenharmony_ci else 8708c2ecf20Sopenharmony_ci ret = 0; 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci if (ret) 8738c2ecf20Sopenharmony_ci goto out; 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci /* 8768c2ecf20Sopenharmony_ci * FIXME: some drivers unfortunately call this function more than once. 8778c2ecf20Sopenharmony_ci * So we have to check if we've already assigned the reboot notifier. 8788c2ecf20Sopenharmony_ci * 8798c2ecf20Sopenharmony_ci * Generally, we can make multiple calls work for most cases, but it 8808c2ecf20Sopenharmony_ci * does cause problems with parse_mtd_partitions() above (e.g., 8818c2ecf20Sopenharmony_ci * cmdlineparts will register partitions more than once). 8828c2ecf20Sopenharmony_ci */ 8838c2ecf20Sopenharmony_ci WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, 8848c2ecf20Sopenharmony_ci "MTD already registered\n"); 8858c2ecf20Sopenharmony_ci if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { 8868c2ecf20Sopenharmony_ci mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; 8878c2ecf20Sopenharmony_ci register_reboot_notifier(&mtd->reboot_notifier); 8888c2ecf20Sopenharmony_ci } 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ciout: 8918c2ecf20Sopenharmony_ci if (ret && device_is_registered(&mtd->dev)) 8928c2ecf20Sopenharmony_ci del_mtd_device(mtd); 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci return ret; 8958c2ecf20Sopenharmony_ci} 8968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_device_parse_register); 8978c2ecf20Sopenharmony_ci 8988c2ecf20Sopenharmony_ci/** 8998c2ecf20Sopenharmony_ci * mtd_device_unregister - unregister an existing MTD device. 9008c2ecf20Sopenharmony_ci * 9018c2ecf20Sopenharmony_ci * @master: the MTD device to unregister. This will unregister both the master 9028c2ecf20Sopenharmony_ci * and any partitions if registered. 9038c2ecf20Sopenharmony_ci */ 9048c2ecf20Sopenharmony_ciint mtd_device_unregister(struct mtd_info *master) 9058c2ecf20Sopenharmony_ci{ 9068c2ecf20Sopenharmony_ci int err; 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci if (master->_reboot) 9098c2ecf20Sopenharmony_ci unregister_reboot_notifier(&master->reboot_notifier); 9108c2ecf20Sopenharmony_ci 9118c2ecf20Sopenharmony_ci err = del_mtd_partitions(master); 9128c2ecf20Sopenharmony_ci if (err) 9138c2ecf20Sopenharmony_ci return err; 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci if (!device_is_registered(&master->dev)) 9168c2ecf20Sopenharmony_ci return 0; 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci return del_mtd_device(master); 9198c2ecf20Sopenharmony_ci} 9208c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_device_unregister); 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci/** 9238c2ecf20Sopenharmony_ci * register_mtd_user - register a 'user' of MTD devices. 9248c2ecf20Sopenharmony_ci * @new: pointer to notifier info structure 9258c2ecf20Sopenharmony_ci * 9268c2ecf20Sopenharmony_ci * Registers a pair of callbacks function to be called upon addition 9278c2ecf20Sopenharmony_ci * or removal of MTD devices. Causes the 'add' callback to be immediately 9288c2ecf20Sopenharmony_ci * invoked for each MTD device currently present in the system. 9298c2ecf20Sopenharmony_ci */ 9308c2ecf20Sopenharmony_civoid register_mtd_user (struct mtd_notifier *new) 9318c2ecf20Sopenharmony_ci{ 9328c2ecf20Sopenharmony_ci struct mtd_info *mtd; 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci list_add(&new->list, &mtd_notifiers); 9378c2ecf20Sopenharmony_ci 9388c2ecf20Sopenharmony_ci __module_get(THIS_MODULE); 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci mtd_for_each_device(mtd) 9418c2ecf20Sopenharmony_ci new->add(mtd); 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 9448c2ecf20Sopenharmony_ci} 9458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(register_mtd_user); 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_ci/** 9488c2ecf20Sopenharmony_ci * unregister_mtd_user - unregister a 'user' of MTD devices. 9498c2ecf20Sopenharmony_ci * @old: pointer to notifier info structure 9508c2ecf20Sopenharmony_ci * 9518c2ecf20Sopenharmony_ci * Removes a callback function pair from the list of 'users' to be 9528c2ecf20Sopenharmony_ci * notified upon addition or removal of MTD devices. Causes the 9538c2ecf20Sopenharmony_ci * 'remove' callback to be immediately invoked for each MTD device 9548c2ecf20Sopenharmony_ci * currently present in the system. 9558c2ecf20Sopenharmony_ci */ 9568c2ecf20Sopenharmony_ciint unregister_mtd_user (struct mtd_notifier *old) 9578c2ecf20Sopenharmony_ci{ 9588c2ecf20Sopenharmony_ci struct mtd_info *mtd; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_ci module_put(THIS_MODULE); 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci mtd_for_each_device(mtd) 9658c2ecf20Sopenharmony_ci old->remove(mtd); 9668c2ecf20Sopenharmony_ci 9678c2ecf20Sopenharmony_ci list_del(&old->list); 9688c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 9698c2ecf20Sopenharmony_ci return 0; 9708c2ecf20Sopenharmony_ci} 9718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(unregister_mtd_user); 9728c2ecf20Sopenharmony_ci 9738c2ecf20Sopenharmony_ci/** 9748c2ecf20Sopenharmony_ci * get_mtd_device - obtain a validated handle for an MTD device 9758c2ecf20Sopenharmony_ci * @mtd: last known address of the required MTD device 9768c2ecf20Sopenharmony_ci * @num: internal device number of the required MTD device 9778c2ecf20Sopenharmony_ci * 9788c2ecf20Sopenharmony_ci * Given a number and NULL address, return the num'th entry in the device 9798c2ecf20Sopenharmony_ci * table, if any. Given an address and num == -1, search the device table 9808c2ecf20Sopenharmony_ci * for a device with that address and return if it's still present. Given 9818c2ecf20Sopenharmony_ci * both, return the num'th driver only if its address matches. Return 9828c2ecf20Sopenharmony_ci * error code if not. 9838c2ecf20Sopenharmony_ci */ 9848c2ecf20Sopenharmony_cistruct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) 9858c2ecf20Sopenharmony_ci{ 9868c2ecf20Sopenharmony_ci struct mtd_info *ret = NULL, *other; 9878c2ecf20Sopenharmony_ci int err = -ENODEV; 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 9908c2ecf20Sopenharmony_ci 9918c2ecf20Sopenharmony_ci if (num == -1) { 9928c2ecf20Sopenharmony_ci mtd_for_each_device(other) { 9938c2ecf20Sopenharmony_ci if (other == mtd) { 9948c2ecf20Sopenharmony_ci ret = mtd; 9958c2ecf20Sopenharmony_ci break; 9968c2ecf20Sopenharmony_ci } 9978c2ecf20Sopenharmony_ci } 9988c2ecf20Sopenharmony_ci } else if (num >= 0) { 9998c2ecf20Sopenharmony_ci ret = idr_find(&mtd_idr, num); 10008c2ecf20Sopenharmony_ci if (mtd && mtd != ret) 10018c2ecf20Sopenharmony_ci ret = NULL; 10028c2ecf20Sopenharmony_ci } 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci if (!ret) { 10058c2ecf20Sopenharmony_ci ret = ERR_PTR(err); 10068c2ecf20Sopenharmony_ci goto out; 10078c2ecf20Sopenharmony_ci } 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_ci err = __get_mtd_device(ret); 10108c2ecf20Sopenharmony_ci if (err) 10118c2ecf20Sopenharmony_ci ret = ERR_PTR(err); 10128c2ecf20Sopenharmony_ciout: 10138c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 10148c2ecf20Sopenharmony_ci return ret; 10158c2ecf20Sopenharmony_ci} 10168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(get_mtd_device); 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_ci 10198c2ecf20Sopenharmony_ciint __get_mtd_device(struct mtd_info *mtd) 10208c2ecf20Sopenharmony_ci{ 10218c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 10228c2ecf20Sopenharmony_ci int err; 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_ci if (!try_module_get(master->owner)) 10258c2ecf20Sopenharmony_ci return -ENODEV; 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci if (master->_get_device) { 10288c2ecf20Sopenharmony_ci err = master->_get_device(mtd); 10298c2ecf20Sopenharmony_ci 10308c2ecf20Sopenharmony_ci if (err) { 10318c2ecf20Sopenharmony_ci module_put(master->owner); 10328c2ecf20Sopenharmony_ci return err; 10338c2ecf20Sopenharmony_ci } 10348c2ecf20Sopenharmony_ci } 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci master->usecount++; 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci while (mtd->parent) { 10398c2ecf20Sopenharmony_ci mtd->usecount++; 10408c2ecf20Sopenharmony_ci mtd = mtd->parent; 10418c2ecf20Sopenharmony_ci } 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci return 0; 10448c2ecf20Sopenharmony_ci} 10458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__get_mtd_device); 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci/** 10488c2ecf20Sopenharmony_ci * get_mtd_device_nm - obtain a validated handle for an MTD device by 10498c2ecf20Sopenharmony_ci * device name 10508c2ecf20Sopenharmony_ci * @name: MTD device name to open 10518c2ecf20Sopenharmony_ci * 10528c2ecf20Sopenharmony_ci * This function returns MTD device description structure in case of 10538c2ecf20Sopenharmony_ci * success and an error code in case of failure. 10548c2ecf20Sopenharmony_ci */ 10558c2ecf20Sopenharmony_cistruct mtd_info *get_mtd_device_nm(const char *name) 10568c2ecf20Sopenharmony_ci{ 10578c2ecf20Sopenharmony_ci int err = -ENODEV; 10588c2ecf20Sopenharmony_ci struct mtd_info *mtd = NULL, *other; 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci mtd_for_each_device(other) { 10638c2ecf20Sopenharmony_ci if (!strcmp(name, other->name)) { 10648c2ecf20Sopenharmony_ci mtd = other; 10658c2ecf20Sopenharmony_ci break; 10668c2ecf20Sopenharmony_ci } 10678c2ecf20Sopenharmony_ci } 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_ci if (!mtd) 10708c2ecf20Sopenharmony_ci goto out_unlock; 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_ci err = __get_mtd_device(mtd); 10738c2ecf20Sopenharmony_ci if (err) 10748c2ecf20Sopenharmony_ci goto out_unlock; 10758c2ecf20Sopenharmony_ci 10768c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 10778c2ecf20Sopenharmony_ci return mtd; 10788c2ecf20Sopenharmony_ci 10798c2ecf20Sopenharmony_ciout_unlock: 10808c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 10818c2ecf20Sopenharmony_ci return ERR_PTR(err); 10828c2ecf20Sopenharmony_ci} 10838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(get_mtd_device_nm); 10848c2ecf20Sopenharmony_ci 10858c2ecf20Sopenharmony_civoid put_mtd_device(struct mtd_info *mtd) 10868c2ecf20Sopenharmony_ci{ 10878c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 10888c2ecf20Sopenharmony_ci __put_mtd_device(mtd); 10898c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci} 10928c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(put_mtd_device); 10938c2ecf20Sopenharmony_ci 10948c2ecf20Sopenharmony_civoid __put_mtd_device(struct mtd_info *mtd) 10958c2ecf20Sopenharmony_ci{ 10968c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 10978c2ecf20Sopenharmony_ci 10988c2ecf20Sopenharmony_ci while (mtd->parent) { 10998c2ecf20Sopenharmony_ci --mtd->usecount; 11008c2ecf20Sopenharmony_ci BUG_ON(mtd->usecount < 0); 11018c2ecf20Sopenharmony_ci mtd = mtd->parent; 11028c2ecf20Sopenharmony_ci } 11038c2ecf20Sopenharmony_ci 11048c2ecf20Sopenharmony_ci master->usecount--; 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_ci if (master->_put_device) 11078c2ecf20Sopenharmony_ci master->_put_device(master); 11088c2ecf20Sopenharmony_ci 11098c2ecf20Sopenharmony_ci module_put(master->owner); 11108c2ecf20Sopenharmony_ci} 11118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__put_mtd_device); 11128c2ecf20Sopenharmony_ci 11138c2ecf20Sopenharmony_ci/* 11148c2ecf20Sopenharmony_ci * Erase is an synchronous operation. Device drivers are epected to return a 11158c2ecf20Sopenharmony_ci * negative error code if the operation failed and update instr->fail_addr 11168c2ecf20Sopenharmony_ci * to point the portion that was not properly erased. 11178c2ecf20Sopenharmony_ci */ 11188c2ecf20Sopenharmony_ciint mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 11198c2ecf20Sopenharmony_ci{ 11208c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 11218c2ecf20Sopenharmony_ci u64 mst_ofs = mtd_get_master_ofs(mtd, 0); 11228c2ecf20Sopenharmony_ci struct erase_info adjinstr; 11238c2ecf20Sopenharmony_ci int ret; 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_ci instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 11268c2ecf20Sopenharmony_ci adjinstr = *instr; 11278c2ecf20Sopenharmony_ci 11288c2ecf20Sopenharmony_ci if (!mtd->erasesize || !master->_erase) 11298c2ecf20Sopenharmony_ci return -ENOTSUPP; 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) 11328c2ecf20Sopenharmony_ci return -EINVAL; 11338c2ecf20Sopenharmony_ci if (!(mtd->flags & MTD_WRITEABLE)) 11348c2ecf20Sopenharmony_ci return -EROFS; 11358c2ecf20Sopenharmony_ci 11368c2ecf20Sopenharmony_ci if (!instr->len) 11378c2ecf20Sopenharmony_ci return 0; 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci ledtrig_mtd_activity(); 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 11428c2ecf20Sopenharmony_ci adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) * 11438c2ecf20Sopenharmony_ci master->erasesize; 11448c2ecf20Sopenharmony_ci adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) * 11458c2ecf20Sopenharmony_ci master->erasesize) - 11468c2ecf20Sopenharmony_ci adjinstr.addr; 11478c2ecf20Sopenharmony_ci } 11488c2ecf20Sopenharmony_ci 11498c2ecf20Sopenharmony_ci adjinstr.addr += mst_ofs; 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_ci ret = master->_erase(master, &adjinstr); 11528c2ecf20Sopenharmony_ci 11538c2ecf20Sopenharmony_ci if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) { 11548c2ecf20Sopenharmony_ci instr->fail_addr = adjinstr.fail_addr - mst_ofs; 11558c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 11568c2ecf20Sopenharmony_ci instr->fail_addr = mtd_div_by_eb(instr->fail_addr, 11578c2ecf20Sopenharmony_ci master); 11588c2ecf20Sopenharmony_ci instr->fail_addr *= mtd->erasesize; 11598c2ecf20Sopenharmony_ci } 11608c2ecf20Sopenharmony_ci } 11618c2ecf20Sopenharmony_ci 11628c2ecf20Sopenharmony_ci return ret; 11638c2ecf20Sopenharmony_ci} 11648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_erase); 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ci/* 11678c2ecf20Sopenharmony_ci * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. 11688c2ecf20Sopenharmony_ci */ 11698c2ecf20Sopenharmony_ciint mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 11708c2ecf20Sopenharmony_ci void **virt, resource_size_t *phys) 11718c2ecf20Sopenharmony_ci{ 11728c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 11738c2ecf20Sopenharmony_ci 11748c2ecf20Sopenharmony_ci *retlen = 0; 11758c2ecf20Sopenharmony_ci *virt = NULL; 11768c2ecf20Sopenharmony_ci if (phys) 11778c2ecf20Sopenharmony_ci *phys = 0; 11788c2ecf20Sopenharmony_ci if (!master->_point) 11798c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 11808c2ecf20Sopenharmony_ci if (from < 0 || from >= mtd->size || len > mtd->size - from) 11818c2ecf20Sopenharmony_ci return -EINVAL; 11828c2ecf20Sopenharmony_ci if (!len) 11838c2ecf20Sopenharmony_ci return 0; 11848c2ecf20Sopenharmony_ci 11858c2ecf20Sopenharmony_ci from = mtd_get_master_ofs(mtd, from); 11868c2ecf20Sopenharmony_ci return master->_point(master, from, len, retlen, virt, phys); 11878c2ecf20Sopenharmony_ci} 11888c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_point); 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci/* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 11918c2ecf20Sopenharmony_ciint mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 11928c2ecf20Sopenharmony_ci{ 11938c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 11948c2ecf20Sopenharmony_ci 11958c2ecf20Sopenharmony_ci if (!master->_unpoint) 11968c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 11978c2ecf20Sopenharmony_ci if (from < 0 || from >= mtd->size || len > mtd->size - from) 11988c2ecf20Sopenharmony_ci return -EINVAL; 11998c2ecf20Sopenharmony_ci if (!len) 12008c2ecf20Sopenharmony_ci return 0; 12018c2ecf20Sopenharmony_ci return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len); 12028c2ecf20Sopenharmony_ci} 12038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_unpoint); 12048c2ecf20Sopenharmony_ci 12058c2ecf20Sopenharmony_ci/* 12068c2ecf20Sopenharmony_ci * Allow NOMMU mmap() to directly map the device (if not NULL) 12078c2ecf20Sopenharmony_ci * - return the address to which the offset maps 12088c2ecf20Sopenharmony_ci * - return -ENOSYS to indicate refusal to do the mapping 12098c2ecf20Sopenharmony_ci */ 12108c2ecf20Sopenharmony_ciunsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, 12118c2ecf20Sopenharmony_ci unsigned long offset, unsigned long flags) 12128c2ecf20Sopenharmony_ci{ 12138c2ecf20Sopenharmony_ci size_t retlen; 12148c2ecf20Sopenharmony_ci void *virt; 12158c2ecf20Sopenharmony_ci int ret; 12168c2ecf20Sopenharmony_ci 12178c2ecf20Sopenharmony_ci ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL); 12188c2ecf20Sopenharmony_ci if (ret) 12198c2ecf20Sopenharmony_ci return ret; 12208c2ecf20Sopenharmony_ci if (retlen != len) { 12218c2ecf20Sopenharmony_ci mtd_unpoint(mtd, offset, retlen); 12228c2ecf20Sopenharmony_ci return -ENOSYS; 12238c2ecf20Sopenharmony_ci } 12248c2ecf20Sopenharmony_ci return (unsigned long)virt; 12258c2ecf20Sopenharmony_ci} 12268c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_get_unmapped_area); 12278c2ecf20Sopenharmony_ci 12288c2ecf20Sopenharmony_cistatic void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master, 12298c2ecf20Sopenharmony_ci const struct mtd_ecc_stats *old_stats) 12308c2ecf20Sopenharmony_ci{ 12318c2ecf20Sopenharmony_ci struct mtd_ecc_stats diff; 12328c2ecf20Sopenharmony_ci 12338c2ecf20Sopenharmony_ci if (master == mtd) 12348c2ecf20Sopenharmony_ci return; 12358c2ecf20Sopenharmony_ci 12368c2ecf20Sopenharmony_ci diff = master->ecc_stats; 12378c2ecf20Sopenharmony_ci diff.failed -= old_stats->failed; 12388c2ecf20Sopenharmony_ci diff.corrected -= old_stats->corrected; 12398c2ecf20Sopenharmony_ci 12408c2ecf20Sopenharmony_ci while (mtd->parent) { 12418c2ecf20Sopenharmony_ci mtd->ecc_stats.failed += diff.failed; 12428c2ecf20Sopenharmony_ci mtd->ecc_stats.corrected += diff.corrected; 12438c2ecf20Sopenharmony_ci mtd = mtd->parent; 12448c2ecf20Sopenharmony_ci } 12458c2ecf20Sopenharmony_ci} 12468c2ecf20Sopenharmony_ci 12478c2ecf20Sopenharmony_ciint mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 12488c2ecf20Sopenharmony_ci u_char *buf) 12498c2ecf20Sopenharmony_ci{ 12508c2ecf20Sopenharmony_ci struct mtd_oob_ops ops = { 12518c2ecf20Sopenharmony_ci .len = len, 12528c2ecf20Sopenharmony_ci .datbuf = buf, 12538c2ecf20Sopenharmony_ci }; 12548c2ecf20Sopenharmony_ci int ret; 12558c2ecf20Sopenharmony_ci 12568c2ecf20Sopenharmony_ci ret = mtd_read_oob(mtd, from, &ops); 12578c2ecf20Sopenharmony_ci *retlen = ops.retlen; 12588c2ecf20Sopenharmony_ci 12598c2ecf20Sopenharmony_ci return ret; 12608c2ecf20Sopenharmony_ci} 12618c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_read); 12628c2ecf20Sopenharmony_ci 12638c2ecf20Sopenharmony_ciint mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 12648c2ecf20Sopenharmony_ci const u_char *buf) 12658c2ecf20Sopenharmony_ci{ 12668c2ecf20Sopenharmony_ci struct mtd_oob_ops ops = { 12678c2ecf20Sopenharmony_ci .len = len, 12688c2ecf20Sopenharmony_ci .datbuf = (u8 *)buf, 12698c2ecf20Sopenharmony_ci }; 12708c2ecf20Sopenharmony_ci int ret; 12718c2ecf20Sopenharmony_ci 12728c2ecf20Sopenharmony_ci ret = mtd_write_oob(mtd, to, &ops); 12738c2ecf20Sopenharmony_ci *retlen = ops.retlen; 12748c2ecf20Sopenharmony_ci 12758c2ecf20Sopenharmony_ci return ret; 12768c2ecf20Sopenharmony_ci} 12778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_write); 12788c2ecf20Sopenharmony_ci 12798c2ecf20Sopenharmony_ci/* 12808c2ecf20Sopenharmony_ci * In blackbox flight recorder like scenarios we want to make successful writes 12818c2ecf20Sopenharmony_ci * in interrupt context. panic_write() is only intended to be called when its 12828c2ecf20Sopenharmony_ci * known the kernel is about to panic and we need the write to succeed. Since 12838c2ecf20Sopenharmony_ci * the kernel is not going to be running for much longer, this function can 12848c2ecf20Sopenharmony_ci * break locks and delay to ensure the write succeeds (but not sleep). 12858c2ecf20Sopenharmony_ci */ 12868c2ecf20Sopenharmony_ciint mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 12878c2ecf20Sopenharmony_ci const u_char *buf) 12888c2ecf20Sopenharmony_ci{ 12898c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 12908c2ecf20Sopenharmony_ci 12918c2ecf20Sopenharmony_ci *retlen = 0; 12928c2ecf20Sopenharmony_ci if (!master->_panic_write) 12938c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 12948c2ecf20Sopenharmony_ci if (to < 0 || to >= mtd->size || len > mtd->size - to) 12958c2ecf20Sopenharmony_ci return -EINVAL; 12968c2ecf20Sopenharmony_ci if (!(mtd->flags & MTD_WRITEABLE)) 12978c2ecf20Sopenharmony_ci return -EROFS; 12988c2ecf20Sopenharmony_ci if (!len) 12998c2ecf20Sopenharmony_ci return 0; 13008c2ecf20Sopenharmony_ci if (!master->oops_panic_write) 13018c2ecf20Sopenharmony_ci master->oops_panic_write = true; 13028c2ecf20Sopenharmony_ci 13038c2ecf20Sopenharmony_ci return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len, 13048c2ecf20Sopenharmony_ci retlen, buf); 13058c2ecf20Sopenharmony_ci} 13068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_panic_write); 13078c2ecf20Sopenharmony_ci 13088c2ecf20Sopenharmony_cistatic int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, 13098c2ecf20Sopenharmony_ci struct mtd_oob_ops *ops) 13108c2ecf20Sopenharmony_ci{ 13118c2ecf20Sopenharmony_ci /* 13128c2ecf20Sopenharmony_ci * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving 13138c2ecf20Sopenharmony_ci * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in 13148c2ecf20Sopenharmony_ci * this case. 13158c2ecf20Sopenharmony_ci */ 13168c2ecf20Sopenharmony_ci if (!ops->datbuf) 13178c2ecf20Sopenharmony_ci ops->len = 0; 13188c2ecf20Sopenharmony_ci 13198c2ecf20Sopenharmony_ci if (!ops->oobbuf) 13208c2ecf20Sopenharmony_ci ops->ooblen = 0; 13218c2ecf20Sopenharmony_ci 13228c2ecf20Sopenharmony_ci if (offs < 0 || offs + ops->len > mtd->size) 13238c2ecf20Sopenharmony_ci return -EINVAL; 13248c2ecf20Sopenharmony_ci 13258c2ecf20Sopenharmony_ci if (ops->ooblen) { 13268c2ecf20Sopenharmony_ci size_t maxooblen; 13278c2ecf20Sopenharmony_ci 13288c2ecf20Sopenharmony_ci if (ops->ooboffs >= mtd_oobavail(mtd, ops)) 13298c2ecf20Sopenharmony_ci return -EINVAL; 13308c2ecf20Sopenharmony_ci 13318c2ecf20Sopenharmony_ci maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) - 13328c2ecf20Sopenharmony_ci mtd_div_by_ws(offs, mtd)) * 13338c2ecf20Sopenharmony_ci mtd_oobavail(mtd, ops)) - ops->ooboffs; 13348c2ecf20Sopenharmony_ci if (ops->ooblen > maxooblen) 13358c2ecf20Sopenharmony_ci return -EINVAL; 13368c2ecf20Sopenharmony_ci } 13378c2ecf20Sopenharmony_ci 13388c2ecf20Sopenharmony_ci return 0; 13398c2ecf20Sopenharmony_ci} 13408c2ecf20Sopenharmony_ci 13418c2ecf20Sopenharmony_cistatic int mtd_read_oob_std(struct mtd_info *mtd, loff_t from, 13428c2ecf20Sopenharmony_ci struct mtd_oob_ops *ops) 13438c2ecf20Sopenharmony_ci{ 13448c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 13458c2ecf20Sopenharmony_ci int ret; 13468c2ecf20Sopenharmony_ci 13478c2ecf20Sopenharmony_ci from = mtd_get_master_ofs(mtd, from); 13488c2ecf20Sopenharmony_ci if (master->_read_oob) 13498c2ecf20Sopenharmony_ci ret = master->_read_oob(master, from, ops); 13508c2ecf20Sopenharmony_ci else 13518c2ecf20Sopenharmony_ci ret = master->_read(master, from, ops->len, &ops->retlen, 13528c2ecf20Sopenharmony_ci ops->datbuf); 13538c2ecf20Sopenharmony_ci 13548c2ecf20Sopenharmony_ci return ret; 13558c2ecf20Sopenharmony_ci} 13568c2ecf20Sopenharmony_ci 13578c2ecf20Sopenharmony_cistatic int mtd_write_oob_std(struct mtd_info *mtd, loff_t to, 13588c2ecf20Sopenharmony_ci struct mtd_oob_ops *ops) 13598c2ecf20Sopenharmony_ci{ 13608c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 13618c2ecf20Sopenharmony_ci int ret; 13628c2ecf20Sopenharmony_ci 13638c2ecf20Sopenharmony_ci to = mtd_get_master_ofs(mtd, to); 13648c2ecf20Sopenharmony_ci if (master->_write_oob) 13658c2ecf20Sopenharmony_ci ret = master->_write_oob(master, to, ops); 13668c2ecf20Sopenharmony_ci else 13678c2ecf20Sopenharmony_ci ret = master->_write(master, to, ops->len, &ops->retlen, 13688c2ecf20Sopenharmony_ci ops->datbuf); 13698c2ecf20Sopenharmony_ci 13708c2ecf20Sopenharmony_ci return ret; 13718c2ecf20Sopenharmony_ci} 13728c2ecf20Sopenharmony_ci 13738c2ecf20Sopenharmony_cistatic int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read, 13748c2ecf20Sopenharmony_ci struct mtd_oob_ops *ops) 13758c2ecf20Sopenharmony_ci{ 13768c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 13778c2ecf20Sopenharmony_ci int ngroups = mtd_pairing_groups(master); 13788c2ecf20Sopenharmony_ci int npairs = mtd_wunit_per_eb(master) / ngroups; 13798c2ecf20Sopenharmony_ci struct mtd_oob_ops adjops = *ops; 13808c2ecf20Sopenharmony_ci unsigned int wunit, oobavail; 13818c2ecf20Sopenharmony_ci struct mtd_pairing_info info; 13828c2ecf20Sopenharmony_ci int max_bitflips = 0; 13838c2ecf20Sopenharmony_ci u32 ebofs, pageofs; 13848c2ecf20Sopenharmony_ci loff_t base, pos; 13858c2ecf20Sopenharmony_ci 13868c2ecf20Sopenharmony_ci ebofs = mtd_mod_by_eb(start, mtd); 13878c2ecf20Sopenharmony_ci base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize; 13888c2ecf20Sopenharmony_ci info.group = 0; 13898c2ecf20Sopenharmony_ci info.pair = mtd_div_by_ws(ebofs, mtd); 13908c2ecf20Sopenharmony_ci pageofs = mtd_mod_by_ws(ebofs, mtd); 13918c2ecf20Sopenharmony_ci oobavail = mtd_oobavail(mtd, ops); 13928c2ecf20Sopenharmony_ci 13938c2ecf20Sopenharmony_ci while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) { 13948c2ecf20Sopenharmony_ci int ret; 13958c2ecf20Sopenharmony_ci 13968c2ecf20Sopenharmony_ci if (info.pair >= npairs) { 13978c2ecf20Sopenharmony_ci info.pair = 0; 13988c2ecf20Sopenharmony_ci base += master->erasesize; 13998c2ecf20Sopenharmony_ci } 14008c2ecf20Sopenharmony_ci 14018c2ecf20Sopenharmony_ci wunit = mtd_pairing_info_to_wunit(master, &info); 14028c2ecf20Sopenharmony_ci pos = mtd_wunit_to_offset(mtd, base, wunit); 14038c2ecf20Sopenharmony_ci 14048c2ecf20Sopenharmony_ci adjops.len = ops->len - ops->retlen; 14058c2ecf20Sopenharmony_ci if (adjops.len > mtd->writesize - pageofs) 14068c2ecf20Sopenharmony_ci adjops.len = mtd->writesize - pageofs; 14078c2ecf20Sopenharmony_ci 14088c2ecf20Sopenharmony_ci adjops.ooblen = ops->ooblen - ops->oobretlen; 14098c2ecf20Sopenharmony_ci if (adjops.ooblen > oobavail - adjops.ooboffs) 14108c2ecf20Sopenharmony_ci adjops.ooblen = oobavail - adjops.ooboffs; 14118c2ecf20Sopenharmony_ci 14128c2ecf20Sopenharmony_ci if (read) { 14138c2ecf20Sopenharmony_ci ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops); 14148c2ecf20Sopenharmony_ci if (ret > 0) 14158c2ecf20Sopenharmony_ci max_bitflips = max(max_bitflips, ret); 14168c2ecf20Sopenharmony_ci } else { 14178c2ecf20Sopenharmony_ci ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops); 14188c2ecf20Sopenharmony_ci } 14198c2ecf20Sopenharmony_ci 14208c2ecf20Sopenharmony_ci if (ret < 0) 14218c2ecf20Sopenharmony_ci return ret; 14228c2ecf20Sopenharmony_ci 14238c2ecf20Sopenharmony_ci max_bitflips = max(max_bitflips, ret); 14248c2ecf20Sopenharmony_ci ops->retlen += adjops.retlen; 14258c2ecf20Sopenharmony_ci ops->oobretlen += adjops.oobretlen; 14268c2ecf20Sopenharmony_ci adjops.datbuf += adjops.retlen; 14278c2ecf20Sopenharmony_ci adjops.oobbuf += adjops.oobretlen; 14288c2ecf20Sopenharmony_ci adjops.ooboffs = 0; 14298c2ecf20Sopenharmony_ci pageofs = 0; 14308c2ecf20Sopenharmony_ci info.pair++; 14318c2ecf20Sopenharmony_ci } 14328c2ecf20Sopenharmony_ci 14338c2ecf20Sopenharmony_ci return max_bitflips; 14348c2ecf20Sopenharmony_ci} 14358c2ecf20Sopenharmony_ci 14368c2ecf20Sopenharmony_ciint mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) 14378c2ecf20Sopenharmony_ci{ 14388c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 14398c2ecf20Sopenharmony_ci struct mtd_ecc_stats old_stats = master->ecc_stats; 14408c2ecf20Sopenharmony_ci int ret_code; 14418c2ecf20Sopenharmony_ci 14428c2ecf20Sopenharmony_ci ops->retlen = ops->oobretlen = 0; 14438c2ecf20Sopenharmony_ci 14448c2ecf20Sopenharmony_ci ret_code = mtd_check_oob_ops(mtd, from, ops); 14458c2ecf20Sopenharmony_ci if (ret_code) 14468c2ecf20Sopenharmony_ci return ret_code; 14478c2ecf20Sopenharmony_ci 14488c2ecf20Sopenharmony_ci ledtrig_mtd_activity(); 14498c2ecf20Sopenharmony_ci 14508c2ecf20Sopenharmony_ci /* Check the validity of a potential fallback on mtd->_read */ 14518c2ecf20Sopenharmony_ci if (!master->_read_oob && (!master->_read || ops->oobbuf)) 14528c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 14538c2ecf20Sopenharmony_ci 14548c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 14558c2ecf20Sopenharmony_ci ret_code = mtd_io_emulated_slc(mtd, from, true, ops); 14568c2ecf20Sopenharmony_ci else 14578c2ecf20Sopenharmony_ci ret_code = mtd_read_oob_std(mtd, from, ops); 14588c2ecf20Sopenharmony_ci 14598c2ecf20Sopenharmony_ci mtd_update_ecc_stats(mtd, master, &old_stats); 14608c2ecf20Sopenharmony_ci 14618c2ecf20Sopenharmony_ci /* 14628c2ecf20Sopenharmony_ci * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics 14638c2ecf20Sopenharmony_ci * similar to mtd->_read(), returning a non-negative integer 14648c2ecf20Sopenharmony_ci * representing max bitflips. In other cases, mtd->_read_oob() may 14658c2ecf20Sopenharmony_ci * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). 14668c2ecf20Sopenharmony_ci */ 14678c2ecf20Sopenharmony_ci if (unlikely(ret_code < 0)) 14688c2ecf20Sopenharmony_ci return ret_code; 14698c2ecf20Sopenharmony_ci if (mtd->ecc_strength == 0) 14708c2ecf20Sopenharmony_ci return 0; /* device lacks ecc */ 14718c2ecf20Sopenharmony_ci return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; 14728c2ecf20Sopenharmony_ci} 14738c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_read_oob); 14748c2ecf20Sopenharmony_ci 14758c2ecf20Sopenharmony_ciint mtd_write_oob(struct mtd_info *mtd, loff_t to, 14768c2ecf20Sopenharmony_ci struct mtd_oob_ops *ops) 14778c2ecf20Sopenharmony_ci{ 14788c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 14798c2ecf20Sopenharmony_ci int ret; 14808c2ecf20Sopenharmony_ci 14818c2ecf20Sopenharmony_ci ops->retlen = ops->oobretlen = 0; 14828c2ecf20Sopenharmony_ci 14838c2ecf20Sopenharmony_ci if (!(mtd->flags & MTD_WRITEABLE)) 14848c2ecf20Sopenharmony_ci return -EROFS; 14858c2ecf20Sopenharmony_ci 14868c2ecf20Sopenharmony_ci ret = mtd_check_oob_ops(mtd, to, ops); 14878c2ecf20Sopenharmony_ci if (ret) 14888c2ecf20Sopenharmony_ci return ret; 14898c2ecf20Sopenharmony_ci 14908c2ecf20Sopenharmony_ci ledtrig_mtd_activity(); 14918c2ecf20Sopenharmony_ci 14928c2ecf20Sopenharmony_ci /* Check the validity of a potential fallback on mtd->_write */ 14938c2ecf20Sopenharmony_ci if (!master->_write_oob && (!master->_write || ops->oobbuf)) 14948c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 14958c2ecf20Sopenharmony_ci 14968c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 14978c2ecf20Sopenharmony_ci return mtd_io_emulated_slc(mtd, to, false, ops); 14988c2ecf20Sopenharmony_ci 14998c2ecf20Sopenharmony_ci return mtd_write_oob_std(mtd, to, ops); 15008c2ecf20Sopenharmony_ci} 15018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_write_oob); 15028c2ecf20Sopenharmony_ci 15038c2ecf20Sopenharmony_ci/** 15048c2ecf20Sopenharmony_ci * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section 15058c2ecf20Sopenharmony_ci * @mtd: MTD device structure 15068c2ecf20Sopenharmony_ci * @section: ECC section. Depending on the layout you may have all the ECC 15078c2ecf20Sopenharmony_ci * bytes stored in a single contiguous section, or one section 15088c2ecf20Sopenharmony_ci * per ECC chunk (and sometime several sections for a single ECC 15098c2ecf20Sopenharmony_ci * ECC chunk) 15108c2ecf20Sopenharmony_ci * @oobecc: OOB region struct filled with the appropriate ECC position 15118c2ecf20Sopenharmony_ci * information 15128c2ecf20Sopenharmony_ci * 15138c2ecf20Sopenharmony_ci * This function returns ECC section information in the OOB area. If you want 15148c2ecf20Sopenharmony_ci * to get all the ECC bytes information, then you should call 15158c2ecf20Sopenharmony_ci * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE. 15168c2ecf20Sopenharmony_ci * 15178c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 15188c2ecf20Sopenharmony_ci */ 15198c2ecf20Sopenharmony_ciint mtd_ooblayout_ecc(struct mtd_info *mtd, int section, 15208c2ecf20Sopenharmony_ci struct mtd_oob_region *oobecc) 15218c2ecf20Sopenharmony_ci{ 15228c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 15238c2ecf20Sopenharmony_ci 15248c2ecf20Sopenharmony_ci memset(oobecc, 0, sizeof(*oobecc)); 15258c2ecf20Sopenharmony_ci 15268c2ecf20Sopenharmony_ci if (!master || section < 0) 15278c2ecf20Sopenharmony_ci return -EINVAL; 15288c2ecf20Sopenharmony_ci 15298c2ecf20Sopenharmony_ci if (!master->ooblayout || !master->ooblayout->ecc) 15308c2ecf20Sopenharmony_ci return -ENOTSUPP; 15318c2ecf20Sopenharmony_ci 15328c2ecf20Sopenharmony_ci return master->ooblayout->ecc(master, section, oobecc); 15338c2ecf20Sopenharmony_ci} 15348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_ecc); 15358c2ecf20Sopenharmony_ci 15368c2ecf20Sopenharmony_ci/** 15378c2ecf20Sopenharmony_ci * mtd_ooblayout_free - Get the OOB region definition of a specific free 15388c2ecf20Sopenharmony_ci * section 15398c2ecf20Sopenharmony_ci * @mtd: MTD device structure 15408c2ecf20Sopenharmony_ci * @section: Free section you are interested in. Depending on the layout 15418c2ecf20Sopenharmony_ci * you may have all the free bytes stored in a single contiguous 15428c2ecf20Sopenharmony_ci * section, or one section per ECC chunk plus an extra section 15438c2ecf20Sopenharmony_ci * for the remaining bytes (or other funky layout). 15448c2ecf20Sopenharmony_ci * @oobfree: OOB region struct filled with the appropriate free position 15458c2ecf20Sopenharmony_ci * information 15468c2ecf20Sopenharmony_ci * 15478c2ecf20Sopenharmony_ci * This function returns free bytes position in the OOB area. If you want 15488c2ecf20Sopenharmony_ci * to get all the free bytes information, then you should call 15498c2ecf20Sopenharmony_ci * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE. 15508c2ecf20Sopenharmony_ci * 15518c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 15528c2ecf20Sopenharmony_ci */ 15538c2ecf20Sopenharmony_ciint mtd_ooblayout_free(struct mtd_info *mtd, int section, 15548c2ecf20Sopenharmony_ci struct mtd_oob_region *oobfree) 15558c2ecf20Sopenharmony_ci{ 15568c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 15578c2ecf20Sopenharmony_ci 15588c2ecf20Sopenharmony_ci memset(oobfree, 0, sizeof(*oobfree)); 15598c2ecf20Sopenharmony_ci 15608c2ecf20Sopenharmony_ci if (!master || section < 0) 15618c2ecf20Sopenharmony_ci return -EINVAL; 15628c2ecf20Sopenharmony_ci 15638c2ecf20Sopenharmony_ci if (!master->ooblayout || !master->ooblayout->free) 15648c2ecf20Sopenharmony_ci return -ENOTSUPP; 15658c2ecf20Sopenharmony_ci 15668c2ecf20Sopenharmony_ci return master->ooblayout->free(master, section, oobfree); 15678c2ecf20Sopenharmony_ci} 15688c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_free); 15698c2ecf20Sopenharmony_ci 15708c2ecf20Sopenharmony_ci/** 15718c2ecf20Sopenharmony_ci * mtd_ooblayout_find_region - Find the region attached to a specific byte 15728c2ecf20Sopenharmony_ci * @mtd: mtd info structure 15738c2ecf20Sopenharmony_ci * @byte: the byte we are searching for 15748c2ecf20Sopenharmony_ci * @sectionp: pointer where the section id will be stored 15758c2ecf20Sopenharmony_ci * @oobregion: used to retrieve the ECC position 15768c2ecf20Sopenharmony_ci * @iter: iterator function. Should be either mtd_ooblayout_free or 15778c2ecf20Sopenharmony_ci * mtd_ooblayout_ecc depending on the region type you're searching for 15788c2ecf20Sopenharmony_ci * 15798c2ecf20Sopenharmony_ci * This function returns the section id and oobregion information of a 15808c2ecf20Sopenharmony_ci * specific byte. For example, say you want to know where the 4th ECC byte is 15818c2ecf20Sopenharmony_ci * stored, you'll use: 15828c2ecf20Sopenharmony_ci * 15838c2ecf20Sopenharmony_ci * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc); 15848c2ecf20Sopenharmony_ci * 15858c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 15868c2ecf20Sopenharmony_ci */ 15878c2ecf20Sopenharmony_cistatic int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, 15888c2ecf20Sopenharmony_ci int *sectionp, struct mtd_oob_region *oobregion, 15898c2ecf20Sopenharmony_ci int (*iter)(struct mtd_info *, 15908c2ecf20Sopenharmony_ci int section, 15918c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion)) 15928c2ecf20Sopenharmony_ci{ 15938c2ecf20Sopenharmony_ci int pos = 0, ret, section = 0; 15948c2ecf20Sopenharmony_ci 15958c2ecf20Sopenharmony_ci memset(oobregion, 0, sizeof(*oobregion)); 15968c2ecf20Sopenharmony_ci 15978c2ecf20Sopenharmony_ci while (1) { 15988c2ecf20Sopenharmony_ci ret = iter(mtd, section, oobregion); 15998c2ecf20Sopenharmony_ci if (ret) 16008c2ecf20Sopenharmony_ci return ret; 16018c2ecf20Sopenharmony_ci 16028c2ecf20Sopenharmony_ci if (pos + oobregion->length > byte) 16038c2ecf20Sopenharmony_ci break; 16048c2ecf20Sopenharmony_ci 16058c2ecf20Sopenharmony_ci pos += oobregion->length; 16068c2ecf20Sopenharmony_ci section++; 16078c2ecf20Sopenharmony_ci } 16088c2ecf20Sopenharmony_ci 16098c2ecf20Sopenharmony_ci /* 16108c2ecf20Sopenharmony_ci * Adjust region info to make it start at the beginning at the 16118c2ecf20Sopenharmony_ci * 'start' ECC byte. 16128c2ecf20Sopenharmony_ci */ 16138c2ecf20Sopenharmony_ci oobregion->offset += byte - pos; 16148c2ecf20Sopenharmony_ci oobregion->length -= byte - pos; 16158c2ecf20Sopenharmony_ci *sectionp = section; 16168c2ecf20Sopenharmony_ci 16178c2ecf20Sopenharmony_ci return 0; 16188c2ecf20Sopenharmony_ci} 16198c2ecf20Sopenharmony_ci 16208c2ecf20Sopenharmony_ci/** 16218c2ecf20Sopenharmony_ci * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific 16228c2ecf20Sopenharmony_ci * ECC byte 16238c2ecf20Sopenharmony_ci * @mtd: mtd info structure 16248c2ecf20Sopenharmony_ci * @eccbyte: the byte we are searching for 16258c2ecf20Sopenharmony_ci * @sectionp: pointer where the section id will be stored 16268c2ecf20Sopenharmony_ci * @oobregion: OOB region information 16278c2ecf20Sopenharmony_ci * 16288c2ecf20Sopenharmony_ci * Works like mtd_ooblayout_find_region() except it searches for a specific ECC 16298c2ecf20Sopenharmony_ci * byte. 16308c2ecf20Sopenharmony_ci * 16318c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 16328c2ecf20Sopenharmony_ci */ 16338c2ecf20Sopenharmony_ciint mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, 16348c2ecf20Sopenharmony_ci int *section, 16358c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion) 16368c2ecf20Sopenharmony_ci{ 16378c2ecf20Sopenharmony_ci return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, 16388c2ecf20Sopenharmony_ci mtd_ooblayout_ecc); 16398c2ecf20Sopenharmony_ci} 16408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion); 16418c2ecf20Sopenharmony_ci 16428c2ecf20Sopenharmony_ci/** 16438c2ecf20Sopenharmony_ci * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer 16448c2ecf20Sopenharmony_ci * @mtd: mtd info structure 16458c2ecf20Sopenharmony_ci * @buf: destination buffer to store OOB bytes 16468c2ecf20Sopenharmony_ci * @oobbuf: OOB buffer 16478c2ecf20Sopenharmony_ci * @start: first byte to retrieve 16488c2ecf20Sopenharmony_ci * @nbytes: number of bytes to retrieve 16498c2ecf20Sopenharmony_ci * @iter: section iterator 16508c2ecf20Sopenharmony_ci * 16518c2ecf20Sopenharmony_ci * Extract bytes attached to a specific category (ECC or free) 16528c2ecf20Sopenharmony_ci * from the OOB buffer and copy them into buf. 16538c2ecf20Sopenharmony_ci * 16548c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 16558c2ecf20Sopenharmony_ci */ 16568c2ecf20Sopenharmony_cistatic int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, 16578c2ecf20Sopenharmony_ci const u8 *oobbuf, int start, int nbytes, 16588c2ecf20Sopenharmony_ci int (*iter)(struct mtd_info *, 16598c2ecf20Sopenharmony_ci int section, 16608c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion)) 16618c2ecf20Sopenharmony_ci{ 16628c2ecf20Sopenharmony_ci struct mtd_oob_region oobregion; 16638c2ecf20Sopenharmony_ci int section, ret; 16648c2ecf20Sopenharmony_ci 16658c2ecf20Sopenharmony_ci ret = mtd_ooblayout_find_region(mtd, start, §ion, 16668c2ecf20Sopenharmony_ci &oobregion, iter); 16678c2ecf20Sopenharmony_ci 16688c2ecf20Sopenharmony_ci while (!ret) { 16698c2ecf20Sopenharmony_ci int cnt; 16708c2ecf20Sopenharmony_ci 16718c2ecf20Sopenharmony_ci cnt = min_t(int, nbytes, oobregion.length); 16728c2ecf20Sopenharmony_ci memcpy(buf, oobbuf + oobregion.offset, cnt); 16738c2ecf20Sopenharmony_ci buf += cnt; 16748c2ecf20Sopenharmony_ci nbytes -= cnt; 16758c2ecf20Sopenharmony_ci 16768c2ecf20Sopenharmony_ci if (!nbytes) 16778c2ecf20Sopenharmony_ci break; 16788c2ecf20Sopenharmony_ci 16798c2ecf20Sopenharmony_ci ret = iter(mtd, ++section, &oobregion); 16808c2ecf20Sopenharmony_ci } 16818c2ecf20Sopenharmony_ci 16828c2ecf20Sopenharmony_ci return ret; 16838c2ecf20Sopenharmony_ci} 16848c2ecf20Sopenharmony_ci 16858c2ecf20Sopenharmony_ci/** 16868c2ecf20Sopenharmony_ci * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer 16878c2ecf20Sopenharmony_ci * @mtd: mtd info structure 16888c2ecf20Sopenharmony_ci * @buf: source buffer to get OOB bytes from 16898c2ecf20Sopenharmony_ci * @oobbuf: OOB buffer 16908c2ecf20Sopenharmony_ci * @start: first OOB byte to set 16918c2ecf20Sopenharmony_ci * @nbytes: number of OOB bytes to set 16928c2ecf20Sopenharmony_ci * @iter: section iterator 16938c2ecf20Sopenharmony_ci * 16948c2ecf20Sopenharmony_ci * Fill the OOB buffer with data provided in buf. The category (ECC or free) 16958c2ecf20Sopenharmony_ci * is selected by passing the appropriate iterator. 16968c2ecf20Sopenharmony_ci * 16978c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 16988c2ecf20Sopenharmony_ci */ 16998c2ecf20Sopenharmony_cistatic int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, 17008c2ecf20Sopenharmony_ci u8 *oobbuf, int start, int nbytes, 17018c2ecf20Sopenharmony_ci int (*iter)(struct mtd_info *, 17028c2ecf20Sopenharmony_ci int section, 17038c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion)) 17048c2ecf20Sopenharmony_ci{ 17058c2ecf20Sopenharmony_ci struct mtd_oob_region oobregion; 17068c2ecf20Sopenharmony_ci int section, ret; 17078c2ecf20Sopenharmony_ci 17088c2ecf20Sopenharmony_ci ret = mtd_ooblayout_find_region(mtd, start, §ion, 17098c2ecf20Sopenharmony_ci &oobregion, iter); 17108c2ecf20Sopenharmony_ci 17118c2ecf20Sopenharmony_ci while (!ret) { 17128c2ecf20Sopenharmony_ci int cnt; 17138c2ecf20Sopenharmony_ci 17148c2ecf20Sopenharmony_ci cnt = min_t(int, nbytes, oobregion.length); 17158c2ecf20Sopenharmony_ci memcpy(oobbuf + oobregion.offset, buf, cnt); 17168c2ecf20Sopenharmony_ci buf += cnt; 17178c2ecf20Sopenharmony_ci nbytes -= cnt; 17188c2ecf20Sopenharmony_ci 17198c2ecf20Sopenharmony_ci if (!nbytes) 17208c2ecf20Sopenharmony_ci break; 17218c2ecf20Sopenharmony_ci 17228c2ecf20Sopenharmony_ci ret = iter(mtd, ++section, &oobregion); 17238c2ecf20Sopenharmony_ci } 17248c2ecf20Sopenharmony_ci 17258c2ecf20Sopenharmony_ci return ret; 17268c2ecf20Sopenharmony_ci} 17278c2ecf20Sopenharmony_ci 17288c2ecf20Sopenharmony_ci/** 17298c2ecf20Sopenharmony_ci * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category 17308c2ecf20Sopenharmony_ci * @mtd: mtd info structure 17318c2ecf20Sopenharmony_ci * @iter: category iterator 17328c2ecf20Sopenharmony_ci * 17338c2ecf20Sopenharmony_ci * Count the number of bytes in a given category. 17348c2ecf20Sopenharmony_ci * 17358c2ecf20Sopenharmony_ci * Returns a positive value on success, a negative error code otherwise. 17368c2ecf20Sopenharmony_ci */ 17378c2ecf20Sopenharmony_cistatic int mtd_ooblayout_count_bytes(struct mtd_info *mtd, 17388c2ecf20Sopenharmony_ci int (*iter)(struct mtd_info *, 17398c2ecf20Sopenharmony_ci int section, 17408c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion)) 17418c2ecf20Sopenharmony_ci{ 17428c2ecf20Sopenharmony_ci struct mtd_oob_region oobregion; 17438c2ecf20Sopenharmony_ci int section = 0, ret, nbytes = 0; 17448c2ecf20Sopenharmony_ci 17458c2ecf20Sopenharmony_ci while (1) { 17468c2ecf20Sopenharmony_ci ret = iter(mtd, section++, &oobregion); 17478c2ecf20Sopenharmony_ci if (ret) { 17488c2ecf20Sopenharmony_ci if (ret == -ERANGE) 17498c2ecf20Sopenharmony_ci ret = nbytes; 17508c2ecf20Sopenharmony_ci break; 17518c2ecf20Sopenharmony_ci } 17528c2ecf20Sopenharmony_ci 17538c2ecf20Sopenharmony_ci nbytes += oobregion.length; 17548c2ecf20Sopenharmony_ci } 17558c2ecf20Sopenharmony_ci 17568c2ecf20Sopenharmony_ci return ret; 17578c2ecf20Sopenharmony_ci} 17588c2ecf20Sopenharmony_ci 17598c2ecf20Sopenharmony_ci/** 17608c2ecf20Sopenharmony_ci * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer 17618c2ecf20Sopenharmony_ci * @mtd: mtd info structure 17628c2ecf20Sopenharmony_ci * @eccbuf: destination buffer to store ECC bytes 17638c2ecf20Sopenharmony_ci * @oobbuf: OOB buffer 17648c2ecf20Sopenharmony_ci * @start: first ECC byte to retrieve 17658c2ecf20Sopenharmony_ci * @nbytes: number of ECC bytes to retrieve 17668c2ecf20Sopenharmony_ci * 17678c2ecf20Sopenharmony_ci * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes. 17688c2ecf20Sopenharmony_ci * 17698c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 17708c2ecf20Sopenharmony_ci */ 17718c2ecf20Sopenharmony_ciint mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, 17728c2ecf20Sopenharmony_ci const u8 *oobbuf, int start, int nbytes) 17738c2ecf20Sopenharmony_ci{ 17748c2ecf20Sopenharmony_ci return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, 17758c2ecf20Sopenharmony_ci mtd_ooblayout_ecc); 17768c2ecf20Sopenharmony_ci} 17778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes); 17788c2ecf20Sopenharmony_ci 17798c2ecf20Sopenharmony_ci/** 17808c2ecf20Sopenharmony_ci * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer 17818c2ecf20Sopenharmony_ci * @mtd: mtd info structure 17828c2ecf20Sopenharmony_ci * @eccbuf: source buffer to get ECC bytes from 17838c2ecf20Sopenharmony_ci * @oobbuf: OOB buffer 17848c2ecf20Sopenharmony_ci * @start: first ECC byte to set 17858c2ecf20Sopenharmony_ci * @nbytes: number of ECC bytes to set 17868c2ecf20Sopenharmony_ci * 17878c2ecf20Sopenharmony_ci * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes. 17888c2ecf20Sopenharmony_ci * 17898c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 17908c2ecf20Sopenharmony_ci */ 17918c2ecf20Sopenharmony_ciint mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, 17928c2ecf20Sopenharmony_ci u8 *oobbuf, int start, int nbytes) 17938c2ecf20Sopenharmony_ci{ 17948c2ecf20Sopenharmony_ci return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, 17958c2ecf20Sopenharmony_ci mtd_ooblayout_ecc); 17968c2ecf20Sopenharmony_ci} 17978c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes); 17988c2ecf20Sopenharmony_ci 17998c2ecf20Sopenharmony_ci/** 18008c2ecf20Sopenharmony_ci * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer 18018c2ecf20Sopenharmony_ci * @mtd: mtd info structure 18028c2ecf20Sopenharmony_ci * @databuf: destination buffer to store ECC bytes 18038c2ecf20Sopenharmony_ci * @oobbuf: OOB buffer 18048c2ecf20Sopenharmony_ci * @start: first ECC byte to retrieve 18058c2ecf20Sopenharmony_ci * @nbytes: number of ECC bytes to retrieve 18068c2ecf20Sopenharmony_ci * 18078c2ecf20Sopenharmony_ci * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. 18088c2ecf20Sopenharmony_ci * 18098c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 18108c2ecf20Sopenharmony_ci */ 18118c2ecf20Sopenharmony_ciint mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, 18128c2ecf20Sopenharmony_ci const u8 *oobbuf, int start, int nbytes) 18138c2ecf20Sopenharmony_ci{ 18148c2ecf20Sopenharmony_ci return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, 18158c2ecf20Sopenharmony_ci mtd_ooblayout_free); 18168c2ecf20Sopenharmony_ci} 18178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes); 18188c2ecf20Sopenharmony_ci 18198c2ecf20Sopenharmony_ci/** 18208c2ecf20Sopenharmony_ci * mtd_ooblayout_set_databytes - set data bytes into the oob buffer 18218c2ecf20Sopenharmony_ci * @mtd: mtd info structure 18228c2ecf20Sopenharmony_ci * @databuf: source buffer to get data bytes from 18238c2ecf20Sopenharmony_ci * @oobbuf: OOB buffer 18248c2ecf20Sopenharmony_ci * @start: first ECC byte to set 18258c2ecf20Sopenharmony_ci * @nbytes: number of ECC bytes to set 18268c2ecf20Sopenharmony_ci * 18278c2ecf20Sopenharmony_ci * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes. 18288c2ecf20Sopenharmony_ci * 18298c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 18308c2ecf20Sopenharmony_ci */ 18318c2ecf20Sopenharmony_ciint mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, 18328c2ecf20Sopenharmony_ci u8 *oobbuf, int start, int nbytes) 18338c2ecf20Sopenharmony_ci{ 18348c2ecf20Sopenharmony_ci return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, 18358c2ecf20Sopenharmony_ci mtd_ooblayout_free); 18368c2ecf20Sopenharmony_ci} 18378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes); 18388c2ecf20Sopenharmony_ci 18398c2ecf20Sopenharmony_ci/** 18408c2ecf20Sopenharmony_ci * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB 18418c2ecf20Sopenharmony_ci * @mtd: mtd info structure 18428c2ecf20Sopenharmony_ci * 18438c2ecf20Sopenharmony_ci * Works like mtd_ooblayout_count_bytes(), except it count free bytes. 18448c2ecf20Sopenharmony_ci * 18458c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 18468c2ecf20Sopenharmony_ci */ 18478c2ecf20Sopenharmony_ciint mtd_ooblayout_count_freebytes(struct mtd_info *mtd) 18488c2ecf20Sopenharmony_ci{ 18498c2ecf20Sopenharmony_ci return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); 18508c2ecf20Sopenharmony_ci} 18518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes); 18528c2ecf20Sopenharmony_ci 18538c2ecf20Sopenharmony_ci/** 18548c2ecf20Sopenharmony_ci * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB 18558c2ecf20Sopenharmony_ci * @mtd: mtd info structure 18568c2ecf20Sopenharmony_ci * 18578c2ecf20Sopenharmony_ci * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes. 18588c2ecf20Sopenharmony_ci * 18598c2ecf20Sopenharmony_ci * Returns zero on success, a negative error code otherwise. 18608c2ecf20Sopenharmony_ci */ 18618c2ecf20Sopenharmony_ciint mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) 18628c2ecf20Sopenharmony_ci{ 18638c2ecf20Sopenharmony_ci return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); 18648c2ecf20Sopenharmony_ci} 18658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes); 18668c2ecf20Sopenharmony_ci 18678c2ecf20Sopenharmony_ci/* 18688c2ecf20Sopenharmony_ci * Method to access the protection register area, present in some flash 18698c2ecf20Sopenharmony_ci * devices. The user data is one time programmable but the factory data is read 18708c2ecf20Sopenharmony_ci * only. 18718c2ecf20Sopenharmony_ci */ 18728c2ecf20Sopenharmony_ciint mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 18738c2ecf20Sopenharmony_ci struct otp_info *buf) 18748c2ecf20Sopenharmony_ci{ 18758c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 18768c2ecf20Sopenharmony_ci 18778c2ecf20Sopenharmony_ci if (!master->_get_fact_prot_info) 18788c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 18798c2ecf20Sopenharmony_ci if (!len) 18808c2ecf20Sopenharmony_ci return 0; 18818c2ecf20Sopenharmony_ci return master->_get_fact_prot_info(master, len, retlen, buf); 18828c2ecf20Sopenharmony_ci} 18838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); 18848c2ecf20Sopenharmony_ci 18858c2ecf20Sopenharmony_ciint mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 18868c2ecf20Sopenharmony_ci size_t *retlen, u_char *buf) 18878c2ecf20Sopenharmony_ci{ 18888c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 18898c2ecf20Sopenharmony_ci 18908c2ecf20Sopenharmony_ci *retlen = 0; 18918c2ecf20Sopenharmony_ci if (!master->_read_fact_prot_reg) 18928c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 18938c2ecf20Sopenharmony_ci if (!len) 18948c2ecf20Sopenharmony_ci return 0; 18958c2ecf20Sopenharmony_ci return master->_read_fact_prot_reg(master, from, len, retlen, buf); 18968c2ecf20Sopenharmony_ci} 18978c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); 18988c2ecf20Sopenharmony_ci 18998c2ecf20Sopenharmony_ciint mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 19008c2ecf20Sopenharmony_ci struct otp_info *buf) 19018c2ecf20Sopenharmony_ci{ 19028c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 19038c2ecf20Sopenharmony_ci 19048c2ecf20Sopenharmony_ci if (!master->_get_user_prot_info) 19058c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 19068c2ecf20Sopenharmony_ci if (!len) 19078c2ecf20Sopenharmony_ci return 0; 19088c2ecf20Sopenharmony_ci return master->_get_user_prot_info(master, len, retlen, buf); 19098c2ecf20Sopenharmony_ci} 19108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_get_user_prot_info); 19118c2ecf20Sopenharmony_ci 19128c2ecf20Sopenharmony_ciint mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 19138c2ecf20Sopenharmony_ci size_t *retlen, u_char *buf) 19148c2ecf20Sopenharmony_ci{ 19158c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 19168c2ecf20Sopenharmony_ci 19178c2ecf20Sopenharmony_ci *retlen = 0; 19188c2ecf20Sopenharmony_ci if (!master->_read_user_prot_reg) 19198c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 19208c2ecf20Sopenharmony_ci if (!len) 19218c2ecf20Sopenharmony_ci return 0; 19228c2ecf20Sopenharmony_ci return master->_read_user_prot_reg(master, from, len, retlen, buf); 19238c2ecf20Sopenharmony_ci} 19248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); 19258c2ecf20Sopenharmony_ci 19268c2ecf20Sopenharmony_ciint mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, 19278c2ecf20Sopenharmony_ci size_t *retlen, u_char *buf) 19288c2ecf20Sopenharmony_ci{ 19298c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 19308c2ecf20Sopenharmony_ci int ret; 19318c2ecf20Sopenharmony_ci 19328c2ecf20Sopenharmony_ci *retlen = 0; 19338c2ecf20Sopenharmony_ci if (!master->_write_user_prot_reg) 19348c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 19358c2ecf20Sopenharmony_ci if (!len) 19368c2ecf20Sopenharmony_ci return 0; 19378c2ecf20Sopenharmony_ci ret = master->_write_user_prot_reg(master, to, len, retlen, buf); 19388c2ecf20Sopenharmony_ci if (ret) 19398c2ecf20Sopenharmony_ci return ret; 19408c2ecf20Sopenharmony_ci 19418c2ecf20Sopenharmony_ci /* 19428c2ecf20Sopenharmony_ci * If no data could be written at all, we are out of memory and 19438c2ecf20Sopenharmony_ci * must return -ENOSPC. 19448c2ecf20Sopenharmony_ci */ 19458c2ecf20Sopenharmony_ci return (*retlen) ? 0 : -ENOSPC; 19468c2ecf20Sopenharmony_ci} 19478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); 19488c2ecf20Sopenharmony_ci 19498c2ecf20Sopenharmony_ciint mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) 19508c2ecf20Sopenharmony_ci{ 19518c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 19528c2ecf20Sopenharmony_ci 19538c2ecf20Sopenharmony_ci if (!master->_lock_user_prot_reg) 19548c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 19558c2ecf20Sopenharmony_ci if (!len) 19568c2ecf20Sopenharmony_ci return 0; 19578c2ecf20Sopenharmony_ci return master->_lock_user_prot_reg(master, from, len); 19588c2ecf20Sopenharmony_ci} 19598c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); 19608c2ecf20Sopenharmony_ci 19618c2ecf20Sopenharmony_ci/* Chip-supported device locking */ 19628c2ecf20Sopenharmony_ciint mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 19638c2ecf20Sopenharmony_ci{ 19648c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 19658c2ecf20Sopenharmony_ci 19668c2ecf20Sopenharmony_ci if (!master->_lock) 19678c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 19688c2ecf20Sopenharmony_ci if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 19698c2ecf20Sopenharmony_ci return -EINVAL; 19708c2ecf20Sopenharmony_ci if (!len) 19718c2ecf20Sopenharmony_ci return 0; 19728c2ecf20Sopenharmony_ci 19738c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 19748c2ecf20Sopenharmony_ci ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 19758c2ecf20Sopenharmony_ci len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 19768c2ecf20Sopenharmony_ci } 19778c2ecf20Sopenharmony_ci 19788c2ecf20Sopenharmony_ci return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len); 19798c2ecf20Sopenharmony_ci} 19808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_lock); 19818c2ecf20Sopenharmony_ci 19828c2ecf20Sopenharmony_ciint mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 19838c2ecf20Sopenharmony_ci{ 19848c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 19858c2ecf20Sopenharmony_ci 19868c2ecf20Sopenharmony_ci if (!master->_unlock) 19878c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 19888c2ecf20Sopenharmony_ci if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 19898c2ecf20Sopenharmony_ci return -EINVAL; 19908c2ecf20Sopenharmony_ci if (!len) 19918c2ecf20Sopenharmony_ci return 0; 19928c2ecf20Sopenharmony_ci 19938c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 19948c2ecf20Sopenharmony_ci ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 19958c2ecf20Sopenharmony_ci len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 19968c2ecf20Sopenharmony_ci } 19978c2ecf20Sopenharmony_ci 19988c2ecf20Sopenharmony_ci return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len); 19998c2ecf20Sopenharmony_ci} 20008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_unlock); 20018c2ecf20Sopenharmony_ci 20028c2ecf20Sopenharmony_ciint mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 20038c2ecf20Sopenharmony_ci{ 20048c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 20058c2ecf20Sopenharmony_ci 20068c2ecf20Sopenharmony_ci if (!master->_is_locked) 20078c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 20088c2ecf20Sopenharmony_ci if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 20098c2ecf20Sopenharmony_ci return -EINVAL; 20108c2ecf20Sopenharmony_ci if (!len) 20118c2ecf20Sopenharmony_ci return 0; 20128c2ecf20Sopenharmony_ci 20138c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 20148c2ecf20Sopenharmony_ci ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 20158c2ecf20Sopenharmony_ci len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 20168c2ecf20Sopenharmony_ci } 20178c2ecf20Sopenharmony_ci 20188c2ecf20Sopenharmony_ci return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len); 20198c2ecf20Sopenharmony_ci} 20208c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_is_locked); 20218c2ecf20Sopenharmony_ci 20228c2ecf20Sopenharmony_ciint mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) 20238c2ecf20Sopenharmony_ci{ 20248c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 20258c2ecf20Sopenharmony_ci 20268c2ecf20Sopenharmony_ci if (ofs < 0 || ofs >= mtd->size) 20278c2ecf20Sopenharmony_ci return -EINVAL; 20288c2ecf20Sopenharmony_ci if (!master->_block_isreserved) 20298c2ecf20Sopenharmony_ci return 0; 20308c2ecf20Sopenharmony_ci 20318c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 20328c2ecf20Sopenharmony_ci ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 20338c2ecf20Sopenharmony_ci 20348c2ecf20Sopenharmony_ci return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs)); 20358c2ecf20Sopenharmony_ci} 20368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_block_isreserved); 20378c2ecf20Sopenharmony_ci 20388c2ecf20Sopenharmony_ciint mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) 20398c2ecf20Sopenharmony_ci{ 20408c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 20418c2ecf20Sopenharmony_ci 20428c2ecf20Sopenharmony_ci if (ofs < 0 || ofs >= mtd->size) 20438c2ecf20Sopenharmony_ci return -EINVAL; 20448c2ecf20Sopenharmony_ci if (!master->_block_isbad) 20458c2ecf20Sopenharmony_ci return 0; 20468c2ecf20Sopenharmony_ci 20478c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 20488c2ecf20Sopenharmony_ci ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 20498c2ecf20Sopenharmony_ci 20508c2ecf20Sopenharmony_ci return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs)); 20518c2ecf20Sopenharmony_ci} 20528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_block_isbad); 20538c2ecf20Sopenharmony_ci 20548c2ecf20Sopenharmony_ciint mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) 20558c2ecf20Sopenharmony_ci{ 20568c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 20578c2ecf20Sopenharmony_ci int ret; 20588c2ecf20Sopenharmony_ci 20598c2ecf20Sopenharmony_ci if (!master->_block_markbad) 20608c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 20618c2ecf20Sopenharmony_ci if (ofs < 0 || ofs >= mtd->size) 20628c2ecf20Sopenharmony_ci return -EINVAL; 20638c2ecf20Sopenharmony_ci if (!(mtd->flags & MTD_WRITEABLE)) 20648c2ecf20Sopenharmony_ci return -EROFS; 20658c2ecf20Sopenharmony_ci 20668c2ecf20Sopenharmony_ci if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 20678c2ecf20Sopenharmony_ci ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 20688c2ecf20Sopenharmony_ci 20698c2ecf20Sopenharmony_ci ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs)); 20708c2ecf20Sopenharmony_ci if (ret) 20718c2ecf20Sopenharmony_ci return ret; 20728c2ecf20Sopenharmony_ci 20738c2ecf20Sopenharmony_ci while (mtd->parent) { 20748c2ecf20Sopenharmony_ci mtd->ecc_stats.badblocks++; 20758c2ecf20Sopenharmony_ci mtd = mtd->parent; 20768c2ecf20Sopenharmony_ci } 20778c2ecf20Sopenharmony_ci 20788c2ecf20Sopenharmony_ci return 0; 20798c2ecf20Sopenharmony_ci} 20808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_block_markbad); 20818c2ecf20Sopenharmony_ci 20828c2ecf20Sopenharmony_ci/* 20838c2ecf20Sopenharmony_ci * default_mtd_writev - the default writev method 20848c2ecf20Sopenharmony_ci * @mtd: mtd device description object pointer 20858c2ecf20Sopenharmony_ci * @vecs: the vectors to write 20868c2ecf20Sopenharmony_ci * @count: count of vectors in @vecs 20878c2ecf20Sopenharmony_ci * @to: the MTD device offset to write to 20888c2ecf20Sopenharmony_ci * @retlen: on exit contains the count of bytes written to the MTD device. 20898c2ecf20Sopenharmony_ci * 20908c2ecf20Sopenharmony_ci * This function returns zero in case of success and a negative error code in 20918c2ecf20Sopenharmony_ci * case of failure. 20928c2ecf20Sopenharmony_ci */ 20938c2ecf20Sopenharmony_cistatic int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 20948c2ecf20Sopenharmony_ci unsigned long count, loff_t to, size_t *retlen) 20958c2ecf20Sopenharmony_ci{ 20968c2ecf20Sopenharmony_ci unsigned long i; 20978c2ecf20Sopenharmony_ci size_t totlen = 0, thislen; 20988c2ecf20Sopenharmony_ci int ret = 0; 20998c2ecf20Sopenharmony_ci 21008c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) { 21018c2ecf20Sopenharmony_ci if (!vecs[i].iov_len) 21028c2ecf20Sopenharmony_ci continue; 21038c2ecf20Sopenharmony_ci ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, 21048c2ecf20Sopenharmony_ci vecs[i].iov_base); 21058c2ecf20Sopenharmony_ci totlen += thislen; 21068c2ecf20Sopenharmony_ci if (ret || thislen != vecs[i].iov_len) 21078c2ecf20Sopenharmony_ci break; 21088c2ecf20Sopenharmony_ci to += vecs[i].iov_len; 21098c2ecf20Sopenharmony_ci } 21108c2ecf20Sopenharmony_ci *retlen = totlen; 21118c2ecf20Sopenharmony_ci return ret; 21128c2ecf20Sopenharmony_ci} 21138c2ecf20Sopenharmony_ci 21148c2ecf20Sopenharmony_ci/* 21158c2ecf20Sopenharmony_ci * mtd_writev - the vector-based MTD write method 21168c2ecf20Sopenharmony_ci * @mtd: mtd device description object pointer 21178c2ecf20Sopenharmony_ci * @vecs: the vectors to write 21188c2ecf20Sopenharmony_ci * @count: count of vectors in @vecs 21198c2ecf20Sopenharmony_ci * @to: the MTD device offset to write to 21208c2ecf20Sopenharmony_ci * @retlen: on exit contains the count of bytes written to the MTD device. 21218c2ecf20Sopenharmony_ci * 21228c2ecf20Sopenharmony_ci * This function returns zero in case of success and a negative error code in 21238c2ecf20Sopenharmony_ci * case of failure. 21248c2ecf20Sopenharmony_ci */ 21258c2ecf20Sopenharmony_ciint mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 21268c2ecf20Sopenharmony_ci unsigned long count, loff_t to, size_t *retlen) 21278c2ecf20Sopenharmony_ci{ 21288c2ecf20Sopenharmony_ci struct mtd_info *master = mtd_get_master(mtd); 21298c2ecf20Sopenharmony_ci 21308c2ecf20Sopenharmony_ci *retlen = 0; 21318c2ecf20Sopenharmony_ci if (!(mtd->flags & MTD_WRITEABLE)) 21328c2ecf20Sopenharmony_ci return -EROFS; 21338c2ecf20Sopenharmony_ci 21348c2ecf20Sopenharmony_ci if (!master->_writev) 21358c2ecf20Sopenharmony_ci return default_mtd_writev(mtd, vecs, count, to, retlen); 21368c2ecf20Sopenharmony_ci 21378c2ecf20Sopenharmony_ci return master->_writev(master, vecs, count, 21388c2ecf20Sopenharmony_ci mtd_get_master_ofs(mtd, to), retlen); 21398c2ecf20Sopenharmony_ci} 21408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_writev); 21418c2ecf20Sopenharmony_ci 21428c2ecf20Sopenharmony_ci/** 21438c2ecf20Sopenharmony_ci * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size 21448c2ecf20Sopenharmony_ci * @mtd: mtd device description object pointer 21458c2ecf20Sopenharmony_ci * @size: a pointer to the ideal or maximum size of the allocation, points 21468c2ecf20Sopenharmony_ci * to the actual allocation size on success. 21478c2ecf20Sopenharmony_ci * 21488c2ecf20Sopenharmony_ci * This routine attempts to allocate a contiguous kernel buffer up to 21498c2ecf20Sopenharmony_ci * the specified size, backing off the size of the request exponentially 21508c2ecf20Sopenharmony_ci * until the request succeeds or until the allocation size falls below 21518c2ecf20Sopenharmony_ci * the system page size. This attempts to make sure it does not adversely 21528c2ecf20Sopenharmony_ci * impact system performance, so when allocating more than one page, we 21538c2ecf20Sopenharmony_ci * ask the memory allocator to avoid re-trying, swapping, writing back 21548c2ecf20Sopenharmony_ci * or performing I/O. 21558c2ecf20Sopenharmony_ci * 21568c2ecf20Sopenharmony_ci * Note, this function also makes sure that the allocated buffer is aligned to 21578c2ecf20Sopenharmony_ci * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. 21588c2ecf20Sopenharmony_ci * 21598c2ecf20Sopenharmony_ci * This is called, for example by mtd_{read,write} and jffs2_scan_medium, 21608c2ecf20Sopenharmony_ci * to handle smaller (i.e. degraded) buffer allocations under low- or 21618c2ecf20Sopenharmony_ci * fragmented-memory situations where such reduced allocations, from a 21628c2ecf20Sopenharmony_ci * requested ideal, are allowed. 21638c2ecf20Sopenharmony_ci * 21648c2ecf20Sopenharmony_ci * Returns a pointer to the allocated buffer on success; otherwise, NULL. 21658c2ecf20Sopenharmony_ci */ 21668c2ecf20Sopenharmony_civoid *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) 21678c2ecf20Sopenharmony_ci{ 21688c2ecf20Sopenharmony_ci gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY; 21698c2ecf20Sopenharmony_ci size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); 21708c2ecf20Sopenharmony_ci void *kbuf; 21718c2ecf20Sopenharmony_ci 21728c2ecf20Sopenharmony_ci *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); 21738c2ecf20Sopenharmony_ci 21748c2ecf20Sopenharmony_ci while (*size > min_alloc) { 21758c2ecf20Sopenharmony_ci kbuf = kmalloc(*size, flags); 21768c2ecf20Sopenharmony_ci if (kbuf) 21778c2ecf20Sopenharmony_ci return kbuf; 21788c2ecf20Sopenharmony_ci 21798c2ecf20Sopenharmony_ci *size >>= 1; 21808c2ecf20Sopenharmony_ci *size = ALIGN(*size, mtd->writesize); 21818c2ecf20Sopenharmony_ci } 21828c2ecf20Sopenharmony_ci 21838c2ecf20Sopenharmony_ci /* 21848c2ecf20Sopenharmony_ci * For the last resort allocation allow 'kmalloc()' to do all sorts of 21858c2ecf20Sopenharmony_ci * things (write-back, dropping caches, etc) by using GFP_KERNEL. 21868c2ecf20Sopenharmony_ci */ 21878c2ecf20Sopenharmony_ci return kmalloc(*size, GFP_KERNEL); 21888c2ecf20Sopenharmony_ci} 21898c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); 21908c2ecf20Sopenharmony_ci 21918c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS 21928c2ecf20Sopenharmony_ci 21938c2ecf20Sopenharmony_ci/*====================================================================*/ 21948c2ecf20Sopenharmony_ci/* Support for /proc/mtd */ 21958c2ecf20Sopenharmony_ci 21968c2ecf20Sopenharmony_cistatic int mtd_proc_show(struct seq_file *m, void *v) 21978c2ecf20Sopenharmony_ci{ 21988c2ecf20Sopenharmony_ci struct mtd_info *mtd; 21998c2ecf20Sopenharmony_ci 22008c2ecf20Sopenharmony_ci seq_puts(m, "dev: size erasesize name\n"); 22018c2ecf20Sopenharmony_ci mtd_table_mutex_lock(); 22028c2ecf20Sopenharmony_ci mtd_for_each_device(mtd) { 22038c2ecf20Sopenharmony_ci seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", 22048c2ecf20Sopenharmony_ci mtd->index, (unsigned long long)mtd->size, 22058c2ecf20Sopenharmony_ci mtd->erasesize, mtd->name); 22068c2ecf20Sopenharmony_ci } 22078c2ecf20Sopenharmony_ci mtd_table_mutex_unlock(); 22088c2ecf20Sopenharmony_ci return 0; 22098c2ecf20Sopenharmony_ci} 22108c2ecf20Sopenharmony_ci#endif /* CONFIG_PROC_FS */ 22118c2ecf20Sopenharmony_ci 22128c2ecf20Sopenharmony_ci/*====================================================================*/ 22138c2ecf20Sopenharmony_ci/* Init code */ 22148c2ecf20Sopenharmony_ci 22158c2ecf20Sopenharmony_cistatic struct backing_dev_info * __init mtd_bdi_init(char *name) 22168c2ecf20Sopenharmony_ci{ 22178c2ecf20Sopenharmony_ci struct backing_dev_info *bdi; 22188c2ecf20Sopenharmony_ci int ret; 22198c2ecf20Sopenharmony_ci 22208c2ecf20Sopenharmony_ci bdi = bdi_alloc(NUMA_NO_NODE); 22218c2ecf20Sopenharmony_ci if (!bdi) 22228c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 22238c2ecf20Sopenharmony_ci bdi->ra_pages = 0; 22248c2ecf20Sopenharmony_ci bdi->io_pages = 0; 22258c2ecf20Sopenharmony_ci 22268c2ecf20Sopenharmony_ci /* 22278c2ecf20Sopenharmony_ci * We put '-0' suffix to the name to get the same name format as we 22288c2ecf20Sopenharmony_ci * used to get. Since this is called only once, we get a unique name. 22298c2ecf20Sopenharmony_ci */ 22308c2ecf20Sopenharmony_ci ret = bdi_register(bdi, "%.28s-0", name); 22318c2ecf20Sopenharmony_ci if (ret) 22328c2ecf20Sopenharmony_ci bdi_put(bdi); 22338c2ecf20Sopenharmony_ci 22348c2ecf20Sopenharmony_ci return ret ? ERR_PTR(ret) : bdi; 22358c2ecf20Sopenharmony_ci} 22368c2ecf20Sopenharmony_ci 22378c2ecf20Sopenharmony_cistatic struct proc_dir_entry *proc_mtd; 22388c2ecf20Sopenharmony_ci 22398c2ecf20Sopenharmony_cistatic int __init init_mtd(void) 22408c2ecf20Sopenharmony_ci{ 22418c2ecf20Sopenharmony_ci int ret; 22428c2ecf20Sopenharmony_ci 22438c2ecf20Sopenharmony_ci ret = class_register(&mtd_class); 22448c2ecf20Sopenharmony_ci if (ret) 22458c2ecf20Sopenharmony_ci goto err_reg; 22468c2ecf20Sopenharmony_ci 22478c2ecf20Sopenharmony_ci mtd_bdi = mtd_bdi_init("mtd"); 22488c2ecf20Sopenharmony_ci if (IS_ERR(mtd_bdi)) { 22498c2ecf20Sopenharmony_ci ret = PTR_ERR(mtd_bdi); 22508c2ecf20Sopenharmony_ci goto err_bdi; 22518c2ecf20Sopenharmony_ci } 22528c2ecf20Sopenharmony_ci 22538c2ecf20Sopenharmony_ci proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show); 22548c2ecf20Sopenharmony_ci 22558c2ecf20Sopenharmony_ci ret = init_mtdchar(); 22568c2ecf20Sopenharmony_ci if (ret) 22578c2ecf20Sopenharmony_ci goto out_procfs; 22588c2ecf20Sopenharmony_ci 22598c2ecf20Sopenharmony_ci dfs_dir_mtd = debugfs_create_dir("mtd", NULL); 22608c2ecf20Sopenharmony_ci 22618c2ecf20Sopenharmony_ci return 0; 22628c2ecf20Sopenharmony_ci 22638c2ecf20Sopenharmony_ciout_procfs: 22648c2ecf20Sopenharmony_ci if (proc_mtd) 22658c2ecf20Sopenharmony_ci remove_proc_entry("mtd", NULL); 22668c2ecf20Sopenharmony_ci bdi_put(mtd_bdi); 22678c2ecf20Sopenharmony_cierr_bdi: 22688c2ecf20Sopenharmony_ci class_unregister(&mtd_class); 22698c2ecf20Sopenharmony_cierr_reg: 22708c2ecf20Sopenharmony_ci pr_err("Error registering mtd class or bdi: %d\n", ret); 22718c2ecf20Sopenharmony_ci return ret; 22728c2ecf20Sopenharmony_ci} 22738c2ecf20Sopenharmony_ci 22748c2ecf20Sopenharmony_cistatic void __exit cleanup_mtd(void) 22758c2ecf20Sopenharmony_ci{ 22768c2ecf20Sopenharmony_ci debugfs_remove_recursive(dfs_dir_mtd); 22778c2ecf20Sopenharmony_ci cleanup_mtdchar(); 22788c2ecf20Sopenharmony_ci if (proc_mtd) 22798c2ecf20Sopenharmony_ci remove_proc_entry("mtd", NULL); 22808c2ecf20Sopenharmony_ci class_unregister(&mtd_class); 22818c2ecf20Sopenharmony_ci bdi_put(mtd_bdi); 22828c2ecf20Sopenharmony_ci idr_destroy(&mtd_idr); 22838c2ecf20Sopenharmony_ci} 22848c2ecf20Sopenharmony_ci 22858c2ecf20Sopenharmony_cimodule_init(init_mtd); 22868c2ecf20Sopenharmony_cimodule_exit(cleanup_mtd); 22878c2ecf20Sopenharmony_ci 22888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 22898c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 22908c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Core MTD registration and access routines"); 2291