1 /* 2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * General Public License for more details. 12 */ 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 #include <linux/libnvdimm.h> 15 #include <linux/sched/mm.h> 16 #include <linux/vmalloc.h> 17 #include <linux/uaccess.h> 18 #include <linux/module.h> 19 #include <linux/blkdev.h> 20 #include <linux/fcntl.h> 21 #include <linux/async.h> 22 #include <linux/genhd.h> 23 #include <linux/ndctl.h> 24 #include <linux/sched.h> 25 #include <linux/slab.h> 26 #include <linux/cpu.h> 27 #include <linux/fs.h> 28 #include <linux/io.h> 29 #include <linux/mm.h> 30 #include <linux/nd.h> 31 #include "nd-core.h" 32 #include "nd.h" 33 #include "pfn.h" 34 35 int nvdimm_major; 36 static int nvdimm_bus_major; 37 static struct class *nd_class; 38 static DEFINE_IDA(nd_ida); 39 40 static int to_nd_device_type(struct device *dev) 41 { 42 if (is_nvdimm(dev)) 43 return ND_DEVICE_DIMM; 44 else if (is_memory(dev)) 45 return ND_DEVICE_REGION_PMEM; 46 else if (is_nd_blk(dev)) 47 return ND_DEVICE_REGION_BLK; 48 else if (is_nd_dax(dev)) 49 return ND_DEVICE_DAX_PMEM; 50 else if (is_nd_region(dev->parent)) 51 return nd_region_to_nstype(to_nd_region(dev->parent)); 52 53 return 0; 54 } 55 56 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 57 { 58 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT, 59 to_nd_device_type(dev)); 60 } 61 62 static struct module *to_bus_provider(struct device *dev) 63 { 64 /* pin bus providers while regions are enabled */ 65 if (is_nd_region(dev)) { 66 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 67 68 return nvdimm_bus->nd_desc->module; 69 } 70 return NULL; 71 } 72 73 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus) 74 { 75 nvdimm_bus_lock(&nvdimm_bus->dev); 76 nvdimm_bus->probe_active++; 77 nvdimm_bus_unlock(&nvdimm_bus->dev); 78 } 79 80 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus) 81 { 82 nvdimm_bus_lock(&nvdimm_bus->dev); 83 if (--nvdimm_bus->probe_active == 0) 84 wake_up(&nvdimm_bus->probe_wait); 85 nvdimm_bus_unlock(&nvdimm_bus->dev); 86 } 87 88 static int nvdimm_bus_probe(struct device *dev) 89 { 90 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); 91 struct module *provider = to_bus_provider(dev); 92 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 93 int rc; 94 95 if (!try_module_get(provider)) 96 return -ENXIO; 97 98 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n", 99 dev->driver->name, dev_name(dev)); 100 101 nvdimm_bus_probe_start(nvdimm_bus); 102 rc = nd_drv->probe(dev); 103 if (rc == 0) 104 nd_region_probe_success(nvdimm_bus, dev); 105 else 106 nd_region_disable(nvdimm_bus, dev); 107 nvdimm_bus_probe_end(nvdimm_bus); 108 109 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name, 110 dev_name(dev), rc); 111 112 if (rc != 0) 113 module_put(provider); 114 return rc; 115 } 116 117 static int nvdimm_bus_remove(struct device *dev) 118 { 119 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); 120 struct module *provider = to_bus_provider(dev); 121 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 122 int rc = 0; 123 124 if (nd_drv->remove) 125 rc = nd_drv->remove(dev); 126 nd_region_disable(nvdimm_bus, dev); 127 128 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name, 129 dev_name(dev), rc); 130 module_put(provider); 131 return rc; 132 } 133 134 static void nvdimm_bus_shutdown(struct device *dev) 135 { 136 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 137 struct nd_device_driver *nd_drv = NULL; 138 139 if (dev->driver) 140 nd_drv = to_nd_device_driver(dev->driver); 141 142 if (nd_drv && nd_drv->shutdown) { 143 nd_drv->shutdown(dev); 144 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n", 145 dev->driver->name, dev_name(dev)); 146 } 147 } 148 149 void nd_device_notify(struct device *dev, enum nvdimm_event event) 150 { 151 device_lock(dev); 152 if (dev->driver) { 153 struct nd_device_driver *nd_drv; 154 155 nd_drv = to_nd_device_driver(dev->driver); 156 if (nd_drv->notify) 157 nd_drv->notify(dev, event); 158 } 159 device_unlock(dev); 160 } 161 EXPORT_SYMBOL(nd_device_notify); 162 163 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event) 164 { 165 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); 166 167 if (!nvdimm_bus) 168 return; 169 170 /* caller is responsible for holding a reference on the device */ 171 nd_device_notify(&nd_region->dev, event); 172 } 173 EXPORT_SYMBOL_GPL(nvdimm_region_notify); 174 175 struct clear_badblocks_context { 176 resource_size_t phys, cleared; 177 }; 178 179 static int nvdimm_clear_badblocks_region(struct device *dev, void *data) 180 { 181 struct clear_badblocks_context *ctx = data; 182 struct nd_region *nd_region; 183 resource_size_t ndr_end; 184 sector_t sector; 185 186 /* make sure device is a region */ 187 if (!is_nd_pmem(dev)) 188 return 0; 189 190 nd_region = to_nd_region(dev); 191 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1; 192 193 /* make sure we are in the region */ 194 if (ctx->phys < nd_region->ndr_start 195 || (ctx->phys + ctx->cleared) > ndr_end) 196 return 0; 197 198 sector = (ctx->phys - nd_region->ndr_start) / 512; 199 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512); 200 201 if (nd_region->bb_state) 202 sysfs_notify_dirent(nd_region->bb_state); 203 204 return 0; 205 } 206 207 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus, 208 phys_addr_t phys, u64 cleared) 209 { 210 struct clear_badblocks_context ctx = { 211 .phys = phys, 212 .cleared = cleared, 213 }; 214 215 device_for_each_child(&nvdimm_bus->dev, &ctx, 216 nvdimm_clear_badblocks_region); 217 } 218 219 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus, 220 phys_addr_t phys, u64 cleared) 221 { 222 if (cleared > 0) 223 badrange_forget(&nvdimm_bus->badrange, phys, cleared); 224 225 if (cleared > 0 && cleared / 512) 226 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared); 227 } 228 229 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys, 230 unsigned int len) 231 { 232 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 233 struct nvdimm_bus_descriptor *nd_desc; 234 struct nd_cmd_clear_error clear_err; 235 struct nd_cmd_ars_cap ars_cap; 236 u32 clear_err_unit, mask; 237 unsigned int noio_flag; 238 int cmd_rc, rc; 239 240 if (!nvdimm_bus) 241 return -ENXIO; 242 243 nd_desc = nvdimm_bus->nd_desc; 244 /* 245 * if ndctl does not exist, it's PMEM_LEGACY and 246 * we want to just pretend everything is handled. 247 */ 248 if (!nd_desc->ndctl) 249 return len; 250 251 memset(&ars_cap, 0, sizeof(ars_cap)); 252 ars_cap.address = phys; 253 ars_cap.length = len; 254 noio_flag = memalloc_noio_save(); 255 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap, 256 sizeof(ars_cap), &cmd_rc); 257 memalloc_noio_restore(noio_flag); 258 if (rc < 0) 259 return rc; 260 if (cmd_rc < 0) 261 return cmd_rc; 262 clear_err_unit = ars_cap.clear_err_unit; 263 if (!clear_err_unit || !is_power_of_2(clear_err_unit)) 264 return -ENXIO; 265 266 mask = clear_err_unit - 1; 267 if ((phys | len) & mask) 268 return -ENXIO; 269 memset(&clear_err, 0, sizeof(clear_err)); 270 clear_err.address = phys; 271 clear_err.length = len; 272 noio_flag = memalloc_noio_save(); 273 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err, 274 sizeof(clear_err), &cmd_rc); 275 memalloc_noio_restore(noio_flag); 276 if (rc < 0) 277 return rc; 278 if (cmd_rc < 0) 279 return cmd_rc; 280 281 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared); 282 283 return clear_err.cleared; 284 } 285 EXPORT_SYMBOL_GPL(nvdimm_clear_poison); 286 287 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv); 288 289 static struct bus_type nvdimm_bus_type = { 290 .name = "nd", 291 .uevent = nvdimm_bus_uevent, 292 .match = nvdimm_bus_match, 293 .probe = nvdimm_bus_probe, 294 .remove = nvdimm_bus_remove, 295 .shutdown = nvdimm_bus_shutdown, 296 }; 297 298 static void nvdimm_bus_release(struct device *dev) 299 { 300 struct nvdimm_bus *nvdimm_bus; 301 302 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); 303 ida_simple_remove(&nd_ida, nvdimm_bus->id); 304 kfree(nvdimm_bus); 305 } 306 307 static bool is_nvdimm_bus(struct device *dev) 308 { 309 return dev->release == nvdimm_bus_release; 310 } 311 312 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev) 313 { 314 struct device *dev; 315 316 for (dev = nd_dev; dev; dev = dev->parent) 317 if (is_nvdimm_bus(dev)) 318 break; 319 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n"); 320 if (dev) 321 return to_nvdimm_bus(dev); 322 return NULL; 323 } 324 325 struct nvdimm_bus *to_nvdimm_bus(struct device *dev) 326 { 327 struct nvdimm_bus *nvdimm_bus; 328 329 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); 330 WARN_ON(!is_nvdimm_bus(dev)); 331 return nvdimm_bus; 332 } 333 EXPORT_SYMBOL_GPL(to_nvdimm_bus); 334 335 struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm) 336 { 337 return to_nvdimm_bus(nvdimm->dev.parent); 338 } 339 EXPORT_SYMBOL_GPL(nvdimm_to_bus); 340 341 struct nvdimm_bus *nvdimm_bus_register(struct device *parent, 342 struct nvdimm_bus_descriptor *nd_desc) 343 { 344 struct nvdimm_bus *nvdimm_bus; 345 int rc; 346 347 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL); 348 if (!nvdimm_bus) 349 return NULL; 350 INIT_LIST_HEAD(&nvdimm_bus->list); 351 INIT_LIST_HEAD(&nvdimm_bus->mapping_list); 352 init_waitqueue_head(&nvdimm_bus->probe_wait); 353 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); 354 if (nvdimm_bus->id < 0) { 355 kfree(nvdimm_bus); 356 return NULL; 357 } 358 mutex_init(&nvdimm_bus->reconfig_mutex); 359 badrange_init(&nvdimm_bus->badrange); 360 nvdimm_bus->nd_desc = nd_desc; 361 nvdimm_bus->dev.parent = parent; 362 nvdimm_bus->dev.release = nvdimm_bus_release; 363 nvdimm_bus->dev.groups = nd_desc->attr_groups; 364 nvdimm_bus->dev.bus = &nvdimm_bus_type; 365 nvdimm_bus->dev.of_node = nd_desc->of_node; 366 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id); 367 rc = device_register(&nvdimm_bus->dev); 368 if (rc) { 369 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc); 370 goto err; 371 } 372 373 return nvdimm_bus; 374 err: 375 put_device(&nvdimm_bus->dev); 376 return NULL; 377 } 378 EXPORT_SYMBOL_GPL(nvdimm_bus_register); 379 380 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus) 381 { 382 if (!nvdimm_bus) 383 return; 384 device_unregister(&nvdimm_bus->dev); 385 } 386 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); 387 388 static int child_unregister(struct device *dev, void *data) 389 { 390 /* 391 * the singular ndctl class device per bus needs to be 392 * "device_destroy"ed, so skip it here 393 * 394 * i.e. remove classless children 395 */ 396 if (dev->class) 397 return 0; 398 399 if (is_nvdimm(dev)) { 400 struct nvdimm *nvdimm = to_nvdimm(dev); 401 bool dev_put = false; 402 403 /* We are shutting down. Make state frozen artificially. */ 404 nvdimm_bus_lock(dev); 405 nvdimm->sec.state = NVDIMM_SECURITY_FROZEN; 406 if (test_and_clear_bit(NDD_WORK_PENDING, &nvdimm->flags)) 407 dev_put = true; 408 nvdimm_bus_unlock(dev); 409 cancel_delayed_work_sync(&nvdimm->dwork); 410 if (dev_put) 411 put_device(dev); 412 } 413 nd_device_unregister(dev, ND_SYNC); 414 415 return 0; 416 } 417 418 static void free_badrange_list(struct list_head *badrange_list) 419 { 420 struct badrange_entry *bre, *next; 421 422 list_for_each_entry_safe(bre, next, badrange_list, list) { 423 list_del(&bre->list); 424 kfree(bre); 425 } 426 list_del_init(badrange_list); 427 } 428 429 static int nd_bus_remove(struct device *dev) 430 { 431 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); 432 433 mutex_lock(&nvdimm_bus_list_mutex); 434 list_del_init(&nvdimm_bus->list); 435 mutex_unlock(&nvdimm_bus_list_mutex); 436 437 nd_synchronize(); 438 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister); 439 440 spin_lock(&nvdimm_bus->badrange.lock); 441 free_badrange_list(&nvdimm_bus->badrange.list); 442 spin_unlock(&nvdimm_bus->badrange.lock); 443 444 nvdimm_bus_destroy_ndctl(nvdimm_bus); 445 446 return 0; 447 } 448 449 static int nd_bus_probe(struct device *dev) 450 { 451 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); 452 int rc; 453 454 rc = nvdimm_bus_create_ndctl(nvdimm_bus); 455 if (rc) 456 return rc; 457 458 mutex_lock(&nvdimm_bus_list_mutex); 459 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list); 460 mutex_unlock(&nvdimm_bus_list_mutex); 461 462 /* enable bus provider attributes to look up their local context */ 463 dev_set_drvdata(dev, nvdimm_bus->nd_desc); 464 465 return 0; 466 } 467 468 static struct nd_device_driver nd_bus_driver = { 469 .probe = nd_bus_probe, 470 .remove = nd_bus_remove, 471 .drv = { 472 .name = "nd_bus", 473 .suppress_bind_attrs = true, 474 .bus = &nvdimm_bus_type, 475 .owner = THIS_MODULE, 476 .mod_name = KBUILD_MODNAME, 477 }, 478 }; 479 480 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv) 481 { 482 struct nd_device_driver *nd_drv = to_nd_device_driver(drv); 483 484 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver) 485 return true; 486 487 return !!test_bit(to_nd_device_type(dev), &nd_drv->type); 488 } 489 490 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain); 491 492 void nd_synchronize(void) 493 { 494 async_synchronize_full_domain(&nd_async_domain); 495 } 496 EXPORT_SYMBOL_GPL(nd_synchronize); 497 498 static void nd_async_device_register(void *d, async_cookie_t cookie) 499 { 500 struct device *dev = d; 501 502 if (device_add(dev) != 0) { 503 dev_err(dev, "%s: failed\n", __func__); 504 put_device(dev); 505 } 506 put_device(dev); 507 if (dev->parent) 508 put_device(dev->parent); 509 } 510 511 static void nd_async_device_unregister(void *d, async_cookie_t cookie) 512 { 513 struct device *dev = d; 514 515 /* flush bus operations before delete */ 516 nvdimm_bus_lock(dev); 517 nvdimm_bus_unlock(dev); 518 519 device_unregister(dev); 520 put_device(dev); 521 } 522 523 void __nd_device_register(struct device *dev) 524 { 525 if (!dev) 526 return; 527 528 /* 529 * Ensure that region devices always have their NUMA node set as 530 * early as possible. This way we are able to make certain that 531 * any memory associated with the creation and the creation 532 * itself of the region is associated with the correct node. 533 */ 534 if (is_nd_region(dev)) 535 set_dev_node(dev, to_nd_region(dev)->numa_node); 536 537 dev->bus = &nvdimm_bus_type; 538 if (dev->parent) { 539 get_device(dev->parent); 540 if (dev_to_node(dev) == NUMA_NO_NODE) 541 set_dev_node(dev, dev_to_node(dev->parent)); 542 } 543 get_device(dev); 544 545 async_schedule_dev_domain(nd_async_device_register, dev, 546 &nd_async_domain); 547 } 548 549 void nd_device_register(struct device *dev) 550 { 551 device_initialize(dev); 552 __nd_device_register(dev); 553 } 554 EXPORT_SYMBOL(nd_device_register); 555 556 void nd_device_unregister(struct device *dev, enum nd_async_mode mode) 557 { 558 switch (mode) { 559 case ND_ASYNC: 560 get_device(dev); 561 async_schedule_domain(nd_async_device_unregister, dev, 562 &nd_async_domain); 563 break; 564 case ND_SYNC: 565 nd_synchronize(); 566 device_unregister(dev); 567 break; 568 } 569 } 570 EXPORT_SYMBOL(nd_device_unregister); 571 572 /** 573 * __nd_driver_register() - register a region or a namespace driver 574 * @nd_drv: driver to register 575 * @owner: automatically set by nd_driver_register() macro 576 * @mod_name: automatically set by nd_driver_register() macro 577 */ 578 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner, 579 const char *mod_name) 580 { 581 struct device_driver *drv = &nd_drv->drv; 582 583 if (!nd_drv->type) { 584 pr_debug("driver type bitmask not set (%ps)\n", 585 __builtin_return_address(0)); 586 return -EINVAL; 587 } 588 589 if (!nd_drv->probe) { 590 pr_debug("%s ->probe() must be specified\n", mod_name); 591 return -EINVAL; 592 } 593 594 drv->bus = &nvdimm_bus_type; 595 drv->owner = owner; 596 drv->mod_name = mod_name; 597 598 return driver_register(drv); 599 } 600 EXPORT_SYMBOL(__nd_driver_register); 601 602 int nvdimm_revalidate_disk(struct gendisk *disk) 603 { 604 struct device *dev = disk_to_dev(disk)->parent; 605 struct nd_region *nd_region = to_nd_region(dev->parent); 606 int disk_ro = get_disk_ro(disk); 607 608 /* 609 * Upgrade to read-only if the region is read-only preserve as 610 * read-only if the disk is already read-only. 611 */ 612 if (disk_ro || nd_region->ro == disk_ro) 613 return 0; 614 615 dev_info(dev, "%s read-only, marking %s read-only\n", 616 dev_name(&nd_region->dev), disk->disk_name); 617 set_disk_ro(disk, 1); 618 619 return 0; 620 621 } 622 EXPORT_SYMBOL(nvdimm_revalidate_disk); 623 624 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 625 char *buf) 626 { 627 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n", 628 to_nd_device_type(dev)); 629 } 630 static DEVICE_ATTR_RO(modalias); 631 632 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr, 633 char *buf) 634 { 635 return sprintf(buf, "%s\n", dev->type->name); 636 } 637 static DEVICE_ATTR_RO(devtype); 638 639 static struct attribute *nd_device_attributes[] = { 640 &dev_attr_modalias.attr, 641 &dev_attr_devtype.attr, 642 NULL, 643 }; 644 645 /* 646 * nd_device_attribute_group - generic attributes for all devices on an nd bus 647 */ 648 struct attribute_group nd_device_attribute_group = { 649 .attrs = nd_device_attributes, 650 }; 651 EXPORT_SYMBOL_GPL(nd_device_attribute_group); 652 653 static ssize_t numa_node_show(struct device *dev, 654 struct device_attribute *attr, char *buf) 655 { 656 return sprintf(buf, "%d\n", dev_to_node(dev)); 657 } 658 static DEVICE_ATTR_RO(numa_node); 659 660 static struct attribute *nd_numa_attributes[] = { 661 &dev_attr_numa_node.attr, 662 NULL, 663 }; 664 665 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a, 666 int n) 667 { 668 if (!IS_ENABLED(CONFIG_NUMA)) 669 return 0; 670 671 return a->mode; 672 } 673 674 /* 675 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus 676 */ 677 struct attribute_group nd_numa_attribute_group = { 678 .attrs = nd_numa_attributes, 679 .is_visible = nd_numa_attr_visible, 680 }; 681 EXPORT_SYMBOL_GPL(nd_numa_attribute_group); 682 683 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus) 684 { 685 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id); 686 struct device *dev; 687 688 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus, 689 "ndctl%d", nvdimm_bus->id); 690 691 if (IS_ERR(dev)) 692 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n", 693 nvdimm_bus->id, PTR_ERR(dev)); 694 return PTR_ERR_OR_ZERO(dev); 695 } 696 697 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus) 698 { 699 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id)); 700 } 701 702 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = { 703 [ND_CMD_IMPLEMENTED] = { }, 704 [ND_CMD_SMART] = { 705 .out_num = 2, 706 .out_sizes = { 4, 128, }, 707 }, 708 [ND_CMD_SMART_THRESHOLD] = { 709 .out_num = 2, 710 .out_sizes = { 4, 8, }, 711 }, 712 [ND_CMD_DIMM_FLAGS] = { 713 .out_num = 2, 714 .out_sizes = { 4, 4 }, 715 }, 716 [ND_CMD_GET_CONFIG_SIZE] = { 717 .out_num = 3, 718 .out_sizes = { 4, 4, 4, }, 719 }, 720 [ND_CMD_GET_CONFIG_DATA] = { 721 .in_num = 2, 722 .in_sizes = { 4, 4, }, 723 .out_num = 2, 724 .out_sizes = { 4, UINT_MAX, }, 725 }, 726 [ND_CMD_SET_CONFIG_DATA] = { 727 .in_num = 3, 728 .in_sizes = { 4, 4, UINT_MAX, }, 729 .out_num = 1, 730 .out_sizes = { 4, }, 731 }, 732 [ND_CMD_VENDOR] = { 733 .in_num = 3, 734 .in_sizes = { 4, 4, UINT_MAX, }, 735 .out_num = 3, 736 .out_sizes = { 4, 4, UINT_MAX, }, 737 }, 738 [ND_CMD_CALL] = { 739 .in_num = 2, 740 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, }, 741 .out_num = 1, 742 .out_sizes = { UINT_MAX, }, 743 }, 744 }; 745 746 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd) 747 { 748 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs)) 749 return &__nd_cmd_dimm_descs[cmd]; 750 return NULL; 751 } 752 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc); 753 754 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = { 755 [ND_CMD_IMPLEMENTED] = { }, 756 [ND_CMD_ARS_CAP] = { 757 .in_num = 2, 758 .in_sizes = { 8, 8, }, 759 .out_num = 4, 760 .out_sizes = { 4, 4, 4, 4, }, 761 }, 762 [ND_CMD_ARS_START] = { 763 .in_num = 5, 764 .in_sizes = { 8, 8, 2, 1, 5, }, 765 .out_num = 2, 766 .out_sizes = { 4, 4, }, 767 }, 768 [ND_CMD_ARS_STATUS] = { 769 .out_num = 3, 770 .out_sizes = { 4, 4, UINT_MAX, }, 771 }, 772 [ND_CMD_CLEAR_ERROR] = { 773 .in_num = 2, 774 .in_sizes = { 8, 8, }, 775 .out_num = 3, 776 .out_sizes = { 4, 4, 8, }, 777 }, 778 [ND_CMD_CALL] = { 779 .in_num = 2, 780 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, }, 781 .out_num = 1, 782 .out_sizes = { UINT_MAX, }, 783 }, 784 }; 785 786 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd) 787 { 788 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs)) 789 return &__nd_cmd_bus_descs[cmd]; 790 return NULL; 791 } 792 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc); 793 794 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd, 795 const struct nd_cmd_desc *desc, int idx, void *buf) 796 { 797 if (idx >= desc->in_num) 798 return UINT_MAX; 799 800 if (desc->in_sizes[idx] < UINT_MAX) 801 return desc->in_sizes[idx]; 802 803 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) { 804 struct nd_cmd_set_config_hdr *hdr = buf; 805 806 return hdr->in_length; 807 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) { 808 struct nd_cmd_vendor_hdr *hdr = buf; 809 810 return hdr->in_length; 811 } else if (cmd == ND_CMD_CALL) { 812 struct nd_cmd_pkg *pkg = buf; 813 814 return pkg->nd_size_in; 815 } 816 817 return UINT_MAX; 818 } 819 EXPORT_SYMBOL_GPL(nd_cmd_in_size); 820 821 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd, 822 const struct nd_cmd_desc *desc, int idx, const u32 *in_field, 823 const u32 *out_field, unsigned long remainder) 824 { 825 if (idx >= desc->out_num) 826 return UINT_MAX; 827 828 if (desc->out_sizes[idx] < UINT_MAX) 829 return desc->out_sizes[idx]; 830 831 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1) 832 return in_field[1]; 833 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) 834 return out_field[1]; 835 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) { 836 /* 837 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is 838 * "Size of Output Buffer in bytes, including this 839 * field." 840 */ 841 if (out_field[1] < 4) 842 return 0; 843 /* 844 * ACPI 6.1 is ambiguous if 'status' is included in the 845 * output size. If we encounter an output size that 846 * overshoots the remainder by 4 bytes, assume it was 847 * including 'status'. 848 */ 849 if (out_field[1] - 4 == remainder) 850 return remainder; 851 return out_field[1] - 8; 852 } else if (cmd == ND_CMD_CALL) { 853 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field; 854 855 return pkg->nd_size_out; 856 } 857 858 859 return UINT_MAX; 860 } 861 EXPORT_SYMBOL_GPL(nd_cmd_out_size); 862 863 void wait_nvdimm_bus_probe_idle(struct device *dev) 864 { 865 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 866 867 do { 868 if (nvdimm_bus->probe_active == 0) 869 break; 870 nvdimm_bus_unlock(&nvdimm_bus->dev); 871 wait_event(nvdimm_bus->probe_wait, 872 nvdimm_bus->probe_active == 0); 873 nvdimm_bus_lock(&nvdimm_bus->dev); 874 } while (true); 875 } 876 877 static int nd_pmem_forget_poison_check(struct device *dev, void *data) 878 { 879 struct nd_cmd_clear_error *clear_err = 880 (struct nd_cmd_clear_error *)data; 881 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL; 882 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL; 883 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL; 884 struct nd_namespace_common *ndns = NULL; 885 struct nd_namespace_io *nsio; 886 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend; 887 888 if (nd_dax || !dev->driver) 889 return 0; 890 891 start = clear_err->address; 892 end = clear_err->address + clear_err->cleared - 1; 893 894 if (nd_btt || nd_pfn || nd_dax) { 895 if (nd_btt) 896 ndns = nd_btt->ndns; 897 else if (nd_pfn) 898 ndns = nd_pfn->ndns; 899 else if (nd_dax) 900 ndns = nd_dax->nd_pfn.ndns; 901 902 if (!ndns) 903 return 0; 904 } else 905 ndns = to_ndns(dev); 906 907 nsio = to_nd_namespace_io(&ndns->dev); 908 pstart = nsio->res.start + offset; 909 pend = nsio->res.end - end_trunc; 910 911 if ((pstart >= start) && (pend <= end)) 912 return -EBUSY; 913 914 return 0; 915 916 } 917 918 static int nd_ns_forget_poison_check(struct device *dev, void *data) 919 { 920 return device_for_each_child(dev, data, nd_pmem_forget_poison_check); 921 } 922 923 /* set_config requires an idle interleave set */ 924 static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus, 925 struct nvdimm *nvdimm, unsigned int cmd, void *data) 926 { 927 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; 928 929 /* ask the bus provider if it would like to block this request */ 930 if (nd_desc->clear_to_send) { 931 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd, data); 932 933 if (rc) 934 return rc; 935 } 936 937 /* require clear error to go through the pmem driver */ 938 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR) 939 return device_for_each_child(&nvdimm_bus->dev, data, 940 nd_ns_forget_poison_check); 941 942 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA) 943 return 0; 944 945 /* prevent label manipulation while the kernel owns label updates */ 946 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev); 947 if (atomic_read(&nvdimm->busy)) 948 return -EBUSY; 949 return 0; 950 } 951 952 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, 953 int read_only, unsigned int ioctl_cmd, unsigned long arg) 954 { 955 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; 956 static char out_env[ND_CMD_MAX_ENVELOPE]; 957 static char in_env[ND_CMD_MAX_ENVELOPE]; 958 const struct nd_cmd_desc *desc = NULL; 959 unsigned int cmd = _IOC_NR(ioctl_cmd); 960 struct device *dev = &nvdimm_bus->dev; 961 void __user *p = (void __user *) arg; 962 const char *cmd_name, *dimm_name; 963 u32 in_len = 0, out_len = 0; 964 unsigned int func = cmd; 965 unsigned long cmd_mask; 966 struct nd_cmd_pkg pkg; 967 int rc, i, cmd_rc; 968 u64 buf_len = 0; 969 void *buf; 970 971 if (nvdimm) { 972 desc = nd_cmd_dimm_desc(cmd); 973 cmd_name = nvdimm_cmd_name(cmd); 974 cmd_mask = nvdimm->cmd_mask; 975 dimm_name = dev_name(&nvdimm->dev); 976 } else { 977 desc = nd_cmd_bus_desc(cmd); 978 cmd_name = nvdimm_bus_cmd_name(cmd); 979 cmd_mask = nd_desc->cmd_mask; 980 dimm_name = "bus"; 981 } 982 983 if (cmd == ND_CMD_CALL) { 984 if (copy_from_user(&pkg, p, sizeof(pkg))) 985 return -EFAULT; 986 } 987 988 if (!desc || (desc->out_num + desc->in_num == 0) || 989 !test_bit(cmd, &cmd_mask)) 990 return -ENOTTY; 991 992 /* fail write commands (when read-only) */ 993 if (read_only) 994 switch (cmd) { 995 case ND_CMD_VENDOR: 996 case ND_CMD_SET_CONFIG_DATA: 997 case ND_CMD_ARS_START: 998 case ND_CMD_CLEAR_ERROR: 999 case ND_CMD_CALL: 1000 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n", 1001 nvdimm ? nvdimm_cmd_name(cmd) 1002 : nvdimm_bus_cmd_name(cmd)); 1003 return -EPERM; 1004 default: 1005 break; 1006 } 1007 1008 /* process an input envelope */ 1009 for (i = 0; i < desc->in_num; i++) { 1010 u32 in_size, copy; 1011 1012 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env); 1013 if (in_size == UINT_MAX) { 1014 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n", 1015 __func__, dimm_name, cmd_name, i); 1016 return -ENXIO; 1017 } 1018 if (in_len < sizeof(in_env)) 1019 copy = min_t(u32, sizeof(in_env) - in_len, in_size); 1020 else 1021 copy = 0; 1022 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) 1023 return -EFAULT; 1024 in_len += in_size; 1025 } 1026 1027 if (cmd == ND_CMD_CALL) { 1028 func = pkg.nd_command; 1029 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n", 1030 dimm_name, pkg.nd_command, 1031 in_len, out_len, buf_len); 1032 } 1033 1034 /* process an output envelope */ 1035 for (i = 0; i < desc->out_num; i++) { 1036 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, 1037 (u32 *) in_env, (u32 *) out_env, 0); 1038 u32 copy; 1039 1040 if (out_size == UINT_MAX) { 1041 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n", 1042 dimm_name, cmd_name, i); 1043 return -EFAULT; 1044 } 1045 if (out_len < sizeof(out_env)) 1046 copy = min_t(u32, sizeof(out_env) - out_len, out_size); 1047 else 1048 copy = 0; 1049 if (copy && copy_from_user(&out_env[out_len], 1050 p + in_len + out_len, copy)) 1051 return -EFAULT; 1052 out_len += out_size; 1053 } 1054 1055 buf_len = (u64) out_len + (u64) in_len; 1056 if (buf_len > ND_IOCTL_MAX_BUFLEN) { 1057 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name, 1058 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN); 1059 return -EINVAL; 1060 } 1061 1062 buf = vmalloc(buf_len); 1063 if (!buf) 1064 return -ENOMEM; 1065 1066 if (copy_from_user(buf, p, buf_len)) { 1067 rc = -EFAULT; 1068 goto out; 1069 } 1070 1071 nvdimm_bus_lock(&nvdimm_bus->dev); 1072 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf); 1073 if (rc) 1074 goto out_unlock; 1075 1076 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc); 1077 if (rc < 0) 1078 goto out_unlock; 1079 1080 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) { 1081 struct nd_cmd_clear_error *clear_err = buf; 1082 1083 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address, 1084 clear_err->cleared); 1085 } 1086 nvdimm_bus_unlock(&nvdimm_bus->dev); 1087 1088 if (copy_to_user(p, buf, buf_len)) 1089 rc = -EFAULT; 1090 1091 vfree(buf); 1092 return rc; 1093 1094 out_unlock: 1095 nvdimm_bus_unlock(&nvdimm_bus->dev); 1096 out: 1097 vfree(buf); 1098 return rc; 1099 } 1100 1101 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1102 { 1103 long id = (long) file->private_data; 1104 int rc = -ENXIO, ro; 1105 struct nvdimm_bus *nvdimm_bus; 1106 1107 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); 1108 mutex_lock(&nvdimm_bus_list_mutex); 1109 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { 1110 if (nvdimm_bus->id == id) { 1111 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg); 1112 break; 1113 } 1114 } 1115 mutex_unlock(&nvdimm_bus_list_mutex); 1116 1117 return rc; 1118 } 1119 1120 static int match_dimm(struct device *dev, void *data) 1121 { 1122 long id = (long) data; 1123 1124 if (is_nvdimm(dev)) { 1125 struct nvdimm *nvdimm = to_nvdimm(dev); 1126 1127 return nvdimm->id == id; 1128 } 1129 1130 return 0; 1131 } 1132 1133 static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1134 { 1135 int rc = -ENXIO, ro; 1136 struct nvdimm_bus *nvdimm_bus; 1137 1138 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); 1139 mutex_lock(&nvdimm_bus_list_mutex); 1140 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { 1141 struct device *dev = device_find_child(&nvdimm_bus->dev, 1142 file->private_data, match_dimm); 1143 struct nvdimm *nvdimm; 1144 1145 if (!dev) 1146 continue; 1147 1148 nvdimm = to_nvdimm(dev); 1149 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg); 1150 put_device(dev); 1151 break; 1152 } 1153 mutex_unlock(&nvdimm_bus_list_mutex); 1154 1155 return rc; 1156 } 1157 1158 static int nd_open(struct inode *inode, struct file *file) 1159 { 1160 long minor = iminor(inode); 1161 1162 file->private_data = (void *) minor; 1163 return 0; 1164 } 1165 1166 static const struct file_operations nvdimm_bus_fops = { 1167 .owner = THIS_MODULE, 1168 .open = nd_open, 1169 .unlocked_ioctl = nd_ioctl, 1170 .compat_ioctl = nd_ioctl, 1171 .llseek = noop_llseek, 1172 }; 1173 1174 static const struct file_operations nvdimm_fops = { 1175 .owner = THIS_MODULE, 1176 .open = nd_open, 1177 .unlocked_ioctl = nvdimm_ioctl, 1178 .compat_ioctl = nvdimm_ioctl, 1179 .llseek = noop_llseek, 1180 }; 1181 1182 int __init nvdimm_bus_init(void) 1183 { 1184 int rc; 1185 1186 rc = bus_register(&nvdimm_bus_type); 1187 if (rc) 1188 return rc; 1189 1190 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops); 1191 if (rc < 0) 1192 goto err_bus_chrdev; 1193 nvdimm_bus_major = rc; 1194 1195 rc = register_chrdev(0, "dimmctl", &nvdimm_fops); 1196 if (rc < 0) 1197 goto err_dimm_chrdev; 1198 nvdimm_major = rc; 1199 1200 nd_class = class_create(THIS_MODULE, "nd"); 1201 if (IS_ERR(nd_class)) { 1202 rc = PTR_ERR(nd_class); 1203 goto err_class; 1204 } 1205 1206 rc = driver_register(&nd_bus_driver.drv); 1207 if (rc) 1208 goto err_nd_bus; 1209 1210 return 0; 1211 1212 err_nd_bus: 1213 class_destroy(nd_class); 1214 err_class: 1215 unregister_chrdev(nvdimm_major, "dimmctl"); 1216 err_dimm_chrdev: 1217 unregister_chrdev(nvdimm_bus_major, "ndctl"); 1218 err_bus_chrdev: 1219 bus_unregister(&nvdimm_bus_type); 1220 1221 return rc; 1222 } 1223 1224 void nvdimm_bus_exit(void) 1225 { 1226 driver_unregister(&nd_bus_driver.drv); 1227 class_destroy(nd_class); 1228 unregister_chrdev(nvdimm_bus_major, "ndctl"); 1229 unregister_chrdev(nvdimm_major, "dimmctl"); 1230 bus_unregister(&nvdimm_bus_type); 1231 ida_destroy(&nd_ida); 1232 } 1233