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