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