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