Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
0004  */
0005 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0006 #include <linux/libnvdimm.h>
0007 #include <linux/sched/mm.h>
0008 #include <linux/vmalloc.h>
0009 #include <linux/uaccess.h>
0010 #include <linux/module.h>
0011 #include <linux/blkdev.h>
0012 #include <linux/fcntl.h>
0013 #include <linux/async.h>
0014 #include <linux/ndctl.h>
0015 #include <linux/sched.h>
0016 #include <linux/slab.h>
0017 #include <linux/cpu.h>
0018 #include <linux/fs.h>
0019 #include <linux/io.h>
0020 #include <linux/mm.h>
0021 #include <linux/nd.h>
0022 #include "nd-core.h"
0023 #include "nd.h"
0024 #include "pfn.h"
0025 
0026 int nvdimm_major;
0027 static int nvdimm_bus_major;
0028 struct class *nd_class;
0029 static DEFINE_IDA(nd_ida);
0030 
0031 static int to_nd_device_type(struct device *dev)
0032 {
0033     if (is_nvdimm(dev))
0034         return ND_DEVICE_DIMM;
0035     else if (is_memory(dev))
0036         return ND_DEVICE_REGION_PMEM;
0037     else if (is_nd_dax(dev))
0038         return ND_DEVICE_DAX_PMEM;
0039     else if (is_nd_region(dev->parent))
0040         return nd_region_to_nstype(to_nd_region(dev->parent));
0041 
0042     return 0;
0043 }
0044 
0045 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
0046 {
0047     return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
0048             to_nd_device_type(dev));
0049 }
0050 
0051 static struct module *to_bus_provider(struct device *dev)
0052 {
0053     /* pin bus providers while regions are enabled */
0054     if (is_nd_region(dev)) {
0055         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
0056 
0057         return nvdimm_bus->nd_desc->module;
0058     }
0059     return NULL;
0060 }
0061 
0062 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
0063 {
0064     nvdimm_bus_lock(&nvdimm_bus->dev);
0065     nvdimm_bus->probe_active++;
0066     nvdimm_bus_unlock(&nvdimm_bus->dev);
0067 }
0068 
0069 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
0070 {
0071     nvdimm_bus_lock(&nvdimm_bus->dev);
0072     if (--nvdimm_bus->probe_active == 0)
0073         wake_up(&nvdimm_bus->wait);
0074     nvdimm_bus_unlock(&nvdimm_bus->dev);
0075 }
0076 
0077 static int nvdimm_bus_probe(struct device *dev)
0078 {
0079     struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
0080     struct module *provider = to_bus_provider(dev);
0081     struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
0082     int rc;
0083 
0084     if (!try_module_get(provider))
0085         return -ENXIO;
0086 
0087     dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
0088             dev->driver->name, dev_name(dev));
0089 
0090     nvdimm_bus_probe_start(nvdimm_bus);
0091     rc = nd_drv->probe(dev);
0092     if ((rc == 0 || rc == -EOPNOTSUPP) &&
0093             dev->parent && is_nd_region(dev->parent))
0094         nd_region_advance_seeds(to_nd_region(dev->parent), dev);
0095     nvdimm_bus_probe_end(nvdimm_bus);
0096 
0097     dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
0098             dev_name(dev), rc);
0099 
0100     if (rc != 0)
0101         module_put(provider);
0102     return rc;
0103 }
0104 
0105 static void nvdimm_bus_remove(struct device *dev)
0106 {
0107     struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
0108     struct module *provider = to_bus_provider(dev);
0109     struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
0110 
0111     if (nd_drv->remove)
0112         nd_drv->remove(dev);
0113 
0114     dev_dbg(&nvdimm_bus->dev, "%s.remove(%s)\n", dev->driver->name,
0115             dev_name(dev));
0116     module_put(provider);
0117 }
0118 
0119 static void nvdimm_bus_shutdown(struct device *dev)
0120 {
0121     struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
0122     struct nd_device_driver *nd_drv = NULL;
0123 
0124     if (dev->driver)
0125         nd_drv = to_nd_device_driver(dev->driver);
0126 
0127     if (nd_drv && nd_drv->shutdown) {
0128         nd_drv->shutdown(dev);
0129         dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
0130                 dev->driver->name, dev_name(dev));
0131     }
0132 }
0133 
0134 void nd_device_notify(struct device *dev, enum nvdimm_event event)
0135 {
0136     device_lock(dev);
0137     if (dev->driver) {
0138         struct nd_device_driver *nd_drv;
0139 
0140         nd_drv = to_nd_device_driver(dev->driver);
0141         if (nd_drv->notify)
0142             nd_drv->notify(dev, event);
0143     }
0144     device_unlock(dev);
0145 }
0146 EXPORT_SYMBOL(nd_device_notify);
0147 
0148 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
0149 {
0150     struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
0151 
0152     if (!nvdimm_bus)
0153         return;
0154 
0155     /* caller is responsible for holding a reference on the device */
0156     nd_device_notify(&nd_region->dev, event);
0157 }
0158 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
0159 
0160 struct clear_badblocks_context {
0161     resource_size_t phys, cleared;
0162 };
0163 
0164 static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
0165 {
0166     struct clear_badblocks_context *ctx = data;
0167     struct nd_region *nd_region;
0168     resource_size_t ndr_end;
0169     sector_t sector;
0170 
0171     /* make sure device is a region */
0172     if (!is_memory(dev))
0173         return 0;
0174 
0175     nd_region = to_nd_region(dev);
0176     ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
0177 
0178     /* make sure we are in the region */
0179     if (ctx->phys < nd_region->ndr_start ||
0180         (ctx->phys + ctx->cleared - 1) > ndr_end)
0181         return 0;
0182 
0183     sector = (ctx->phys - nd_region->ndr_start) / 512;
0184     badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
0185 
0186     if (nd_region->bb_state)
0187         sysfs_notify_dirent(nd_region->bb_state);
0188 
0189     return 0;
0190 }
0191 
0192 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
0193         phys_addr_t phys, u64 cleared)
0194 {
0195     struct clear_badblocks_context ctx = {
0196         .phys = phys,
0197         .cleared = cleared,
0198     };
0199 
0200     device_for_each_child(&nvdimm_bus->dev, &ctx,
0201             nvdimm_clear_badblocks_region);
0202 }
0203 
0204 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
0205         phys_addr_t phys, u64 cleared)
0206 {
0207     if (cleared > 0)
0208         badrange_forget(&nvdimm_bus->badrange, phys, cleared);
0209 
0210     if (cleared > 0 && cleared / 512)
0211         nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
0212 }
0213 
0214 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
0215         unsigned int len)
0216 {
0217     struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
0218     struct nvdimm_bus_descriptor *nd_desc;
0219     struct nd_cmd_clear_error clear_err;
0220     struct nd_cmd_ars_cap ars_cap;
0221     u32 clear_err_unit, mask;
0222     unsigned int noio_flag;
0223     int cmd_rc, rc;
0224 
0225     if (!nvdimm_bus)
0226         return -ENXIO;
0227 
0228     nd_desc = nvdimm_bus->nd_desc;
0229     /*
0230      * if ndctl does not exist, it's PMEM_LEGACY and
0231      * we want to just pretend everything is handled.
0232      */
0233     if (!nd_desc->ndctl)
0234         return len;
0235 
0236     memset(&ars_cap, 0, sizeof(ars_cap));
0237     ars_cap.address = phys;
0238     ars_cap.length = len;
0239     noio_flag = memalloc_noio_save();
0240     rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
0241             sizeof(ars_cap), &cmd_rc);
0242     memalloc_noio_restore(noio_flag);
0243     if (rc < 0)
0244         return rc;
0245     if (cmd_rc < 0)
0246         return cmd_rc;
0247     clear_err_unit = ars_cap.clear_err_unit;
0248     if (!clear_err_unit || !is_power_of_2(clear_err_unit))
0249         return -ENXIO;
0250 
0251     mask = clear_err_unit - 1;
0252     if ((phys | len) & mask)
0253         return -ENXIO;
0254     memset(&clear_err, 0, sizeof(clear_err));
0255     clear_err.address = phys;
0256     clear_err.length = len;
0257     noio_flag = memalloc_noio_save();
0258     rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
0259             sizeof(clear_err), &cmd_rc);
0260     memalloc_noio_restore(noio_flag);
0261     if (rc < 0)
0262         return rc;
0263     if (cmd_rc < 0)
0264         return cmd_rc;
0265 
0266     nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
0267 
0268     return clear_err.cleared;
0269 }
0270 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
0271 
0272 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
0273 
0274 static struct bus_type nvdimm_bus_type = {
0275     .name = "nd",
0276     .uevent = nvdimm_bus_uevent,
0277     .match = nvdimm_bus_match,
0278     .probe = nvdimm_bus_probe,
0279     .remove = nvdimm_bus_remove,
0280     .shutdown = nvdimm_bus_shutdown,
0281 };
0282 
0283 static void nvdimm_bus_release(struct device *dev)
0284 {
0285     struct nvdimm_bus *nvdimm_bus;
0286 
0287     nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
0288     ida_simple_remove(&nd_ida, nvdimm_bus->id);
0289     kfree(nvdimm_bus);
0290 }
0291 
0292 static const struct device_type nvdimm_bus_dev_type = {
0293     .release = nvdimm_bus_release,
0294     .groups = nvdimm_bus_attribute_groups,
0295 };
0296 
0297 bool is_nvdimm_bus(struct device *dev)
0298 {
0299     return dev->type == &nvdimm_bus_dev_type;
0300 }
0301 
0302 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
0303 {
0304     struct device *dev;
0305 
0306     for (dev = nd_dev; dev; dev = dev->parent)
0307         if (is_nvdimm_bus(dev))
0308             break;
0309     dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
0310     if (dev)
0311         return to_nvdimm_bus(dev);
0312     return NULL;
0313 }
0314 
0315 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
0316 {
0317     struct nvdimm_bus *nvdimm_bus;
0318 
0319     nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
0320     WARN_ON(!is_nvdimm_bus(dev));
0321     return nvdimm_bus;
0322 }
0323 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
0324 
0325 struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm)
0326 {
0327     return to_nvdimm_bus(nvdimm->dev.parent);
0328 }
0329 EXPORT_SYMBOL_GPL(nvdimm_to_bus);
0330 
0331 static struct lock_class_key nvdimm_bus_key;
0332 
0333 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
0334         struct nvdimm_bus_descriptor *nd_desc)
0335 {
0336     struct nvdimm_bus *nvdimm_bus;
0337     int rc;
0338 
0339     nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
0340     if (!nvdimm_bus)
0341         return NULL;
0342     INIT_LIST_HEAD(&nvdimm_bus->list);
0343     INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
0344     init_waitqueue_head(&nvdimm_bus->wait);
0345     nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
0346     if (nvdimm_bus->id < 0) {
0347         kfree(nvdimm_bus);
0348         return NULL;
0349     }
0350     mutex_init(&nvdimm_bus->reconfig_mutex);
0351     badrange_init(&nvdimm_bus->badrange);
0352     nvdimm_bus->nd_desc = nd_desc;
0353     nvdimm_bus->dev.parent = parent;
0354     nvdimm_bus->dev.type = &nvdimm_bus_dev_type;
0355     nvdimm_bus->dev.groups = nd_desc->attr_groups;
0356     nvdimm_bus->dev.bus = &nvdimm_bus_type;
0357     nvdimm_bus->dev.of_node = nd_desc->of_node;
0358     device_initialize(&nvdimm_bus->dev);
0359     lockdep_set_class(&nvdimm_bus->dev.mutex, &nvdimm_bus_key);
0360     device_set_pm_not_required(&nvdimm_bus->dev);
0361     rc = dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
0362     if (rc)
0363         goto err;
0364 
0365     rc = device_add(&nvdimm_bus->dev);
0366     if (rc) {
0367         dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
0368         goto err;
0369     }
0370 
0371     return nvdimm_bus;
0372  err:
0373     put_device(&nvdimm_bus->dev);
0374     return NULL;
0375 }
0376 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
0377 
0378 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
0379 {
0380     if (!nvdimm_bus)
0381         return;
0382     device_unregister(&nvdimm_bus->dev);
0383 }
0384 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
0385 
0386 static int child_unregister(struct device *dev, void *data)
0387 {
0388     /*
0389      * the singular ndctl class device per bus needs to be
0390      * "device_destroy"ed, so skip it here
0391      *
0392      * i.e. remove classless children
0393      */
0394     if (dev->class)
0395         return 0;
0396 
0397     if (is_nvdimm(dev))
0398         nvdimm_delete(to_nvdimm(dev));
0399     else
0400         nd_device_unregister(dev, ND_SYNC);
0401 
0402     return 0;
0403 }
0404 
0405 static void free_badrange_list(struct list_head *badrange_list)
0406 {
0407     struct badrange_entry *bre, *next;
0408 
0409     list_for_each_entry_safe(bre, next, badrange_list, list) {
0410         list_del(&bre->list);
0411         kfree(bre);
0412     }
0413     list_del_init(badrange_list);
0414 }
0415 
0416 static void nd_bus_remove(struct device *dev)
0417 {
0418     struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
0419 
0420     mutex_lock(&nvdimm_bus_list_mutex);
0421     list_del_init(&nvdimm_bus->list);
0422     mutex_unlock(&nvdimm_bus_list_mutex);
0423 
0424     wait_event(nvdimm_bus->wait,
0425             atomic_read(&nvdimm_bus->ioctl_active) == 0);
0426 
0427     nd_synchronize();
0428     device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
0429 
0430     spin_lock(&nvdimm_bus->badrange.lock);
0431     free_badrange_list(&nvdimm_bus->badrange.list);
0432     spin_unlock(&nvdimm_bus->badrange.lock);
0433 
0434     nvdimm_bus_destroy_ndctl(nvdimm_bus);
0435 }
0436 
0437 static int nd_bus_probe(struct device *dev)
0438 {
0439     struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
0440     int rc;
0441 
0442     rc = nvdimm_bus_create_ndctl(nvdimm_bus);
0443     if (rc)
0444         return rc;
0445 
0446     mutex_lock(&nvdimm_bus_list_mutex);
0447     list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
0448     mutex_unlock(&nvdimm_bus_list_mutex);
0449 
0450     /* enable bus provider attributes to look up their local context */
0451     dev_set_drvdata(dev, nvdimm_bus->nd_desc);
0452 
0453     return 0;
0454 }
0455 
0456 static struct nd_device_driver nd_bus_driver = {
0457     .probe = nd_bus_probe,
0458     .remove = nd_bus_remove,
0459     .drv = {
0460         .name = "nd_bus",
0461         .suppress_bind_attrs = true,
0462         .bus = &nvdimm_bus_type,
0463         .owner = THIS_MODULE,
0464         .mod_name = KBUILD_MODNAME,
0465     },
0466 };
0467 
0468 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
0469 {
0470     struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
0471 
0472     if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
0473         return true;
0474 
0475     return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
0476 }
0477 
0478 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
0479 
0480 void nd_synchronize(void)
0481 {
0482     async_synchronize_full_domain(&nd_async_domain);
0483 }
0484 EXPORT_SYMBOL_GPL(nd_synchronize);
0485 
0486 static void nd_async_device_register(void *d, async_cookie_t cookie)
0487 {
0488     struct device *dev = d;
0489 
0490     if (device_add(dev) != 0) {
0491         dev_err(dev, "%s: failed\n", __func__);
0492         put_device(dev);
0493     }
0494     put_device(dev);
0495     if (dev->parent)
0496         put_device(dev->parent);
0497 }
0498 
0499 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
0500 {
0501     struct device *dev = d;
0502 
0503     /* flush bus operations before delete */
0504     nvdimm_bus_lock(dev);
0505     nvdimm_bus_unlock(dev);
0506 
0507     device_unregister(dev);
0508     put_device(dev);
0509 }
0510 
0511 void nd_device_register(struct device *dev)
0512 {
0513     if (!dev)
0514         return;
0515 
0516     /*
0517      * Ensure that region devices always have their NUMA node set as
0518      * early as possible. This way we are able to make certain that
0519      * any memory associated with the creation and the creation
0520      * itself of the region is associated with the correct node.
0521      */
0522     if (is_nd_region(dev))
0523         set_dev_node(dev, to_nd_region(dev)->numa_node);
0524 
0525     dev->bus = &nvdimm_bus_type;
0526     device_set_pm_not_required(dev);
0527     if (dev->parent) {
0528         get_device(dev->parent);
0529         if (dev_to_node(dev) == NUMA_NO_NODE)
0530             set_dev_node(dev, dev_to_node(dev->parent));
0531     }
0532     get_device(dev);
0533 
0534     async_schedule_dev_domain(nd_async_device_register, dev,
0535                   &nd_async_domain);
0536 }
0537 EXPORT_SYMBOL(nd_device_register);
0538 
0539 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
0540 {
0541     bool killed;
0542 
0543     switch (mode) {
0544     case ND_ASYNC:
0545         /*
0546          * In the async case this is being triggered with the
0547          * device lock held and the unregistration work needs to
0548          * be moved out of line iff this is thread has won the
0549          * race to schedule the deletion.
0550          */
0551         if (!kill_device(dev))
0552             return;
0553 
0554         get_device(dev);
0555         async_schedule_domain(nd_async_device_unregister, dev,
0556                 &nd_async_domain);
0557         break;
0558     case ND_SYNC:
0559         /*
0560          * In the sync case the device is being unregistered due
0561          * to a state change of the parent. Claim the kill state
0562          * to synchronize against other unregistration requests,
0563          * or otherwise let the async path handle it if the
0564          * unregistration was already queued.
0565          */
0566         device_lock(dev);
0567         killed = kill_device(dev);
0568         device_unlock(dev);
0569 
0570         if (!killed)
0571             return;
0572 
0573         nd_synchronize();
0574         device_unregister(dev);
0575         break;
0576     }
0577 }
0578 EXPORT_SYMBOL(nd_device_unregister);
0579 
0580 /**
0581  * __nd_driver_register() - register a region or a namespace driver
0582  * @nd_drv: driver to register
0583  * @owner: automatically set by nd_driver_register() macro
0584  * @mod_name: automatically set by nd_driver_register() macro
0585  */
0586 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
0587         const char *mod_name)
0588 {
0589     struct device_driver *drv = &nd_drv->drv;
0590 
0591     if (!nd_drv->type) {
0592         pr_debug("driver type bitmask not set (%ps)\n",
0593                 __builtin_return_address(0));
0594         return -EINVAL;
0595     }
0596 
0597     if (!nd_drv->probe) {
0598         pr_debug("%s ->probe() must be specified\n", mod_name);
0599         return -EINVAL;
0600     }
0601 
0602     drv->bus = &nvdimm_bus_type;
0603     drv->owner = owner;
0604     drv->mod_name = mod_name;
0605 
0606     return driver_register(drv);
0607 }
0608 EXPORT_SYMBOL(__nd_driver_register);
0609 
0610 void nvdimm_check_and_set_ro(struct gendisk *disk)
0611 {
0612     struct device *dev = disk_to_dev(disk)->parent;
0613     struct nd_region *nd_region = to_nd_region(dev->parent);
0614     int disk_ro = get_disk_ro(disk);
0615 
0616     /* catch the disk up with the region ro state */
0617     if (disk_ro == nd_region->ro)
0618         return;
0619 
0620     dev_info(dev, "%s read-%s, marking %s read-%s\n",
0621          dev_name(&nd_region->dev), nd_region->ro ? "only" : "write",
0622          disk->disk_name, nd_region->ro ? "only" : "write");
0623     set_disk_ro(disk, nd_region->ro);
0624 }
0625 EXPORT_SYMBOL(nvdimm_check_and_set_ro);
0626 
0627 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
0628         char *buf)
0629 {
0630     return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
0631             to_nd_device_type(dev));
0632 }
0633 static DEVICE_ATTR_RO(modalias);
0634 
0635 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
0636         char *buf)
0637 {
0638     return sprintf(buf, "%s\n", dev->type->name);
0639 }
0640 static DEVICE_ATTR_RO(devtype);
0641 
0642 static struct attribute *nd_device_attributes[] = {
0643     &dev_attr_modalias.attr,
0644     &dev_attr_devtype.attr,
0645     NULL,
0646 };
0647 
0648 /*
0649  * nd_device_attribute_group - generic attributes for all devices on an nd bus
0650  */
0651 const struct attribute_group nd_device_attribute_group = {
0652     .attrs = nd_device_attributes,
0653 };
0654 
0655 static ssize_t numa_node_show(struct device *dev,
0656         struct device_attribute *attr, char *buf)
0657 {
0658     return sprintf(buf, "%d\n", dev_to_node(dev));
0659 }
0660 static DEVICE_ATTR_RO(numa_node);
0661 
0662 static int nvdimm_dev_to_target_node(struct device *dev)
0663 {
0664     struct device *parent = dev->parent;
0665     struct nd_region *nd_region = NULL;
0666 
0667     if (is_nd_region(dev))
0668         nd_region = to_nd_region(dev);
0669     else if (parent && is_nd_region(parent))
0670         nd_region = to_nd_region(parent);
0671 
0672     if (!nd_region)
0673         return NUMA_NO_NODE;
0674     return nd_region->target_node;
0675 }
0676 
0677 static ssize_t target_node_show(struct device *dev,
0678         struct device_attribute *attr, char *buf)
0679 {
0680     return sprintf(buf, "%d\n", nvdimm_dev_to_target_node(dev));
0681 }
0682 static DEVICE_ATTR_RO(target_node);
0683 
0684 static struct attribute *nd_numa_attributes[] = {
0685     &dev_attr_numa_node.attr,
0686     &dev_attr_target_node.attr,
0687     NULL,
0688 };
0689 
0690 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
0691         int n)
0692 {
0693     struct device *dev = container_of(kobj, typeof(*dev), kobj);
0694 
0695     if (!IS_ENABLED(CONFIG_NUMA))
0696         return 0;
0697 
0698     if (a == &dev_attr_target_node.attr &&
0699             nvdimm_dev_to_target_node(dev) == NUMA_NO_NODE)
0700         return 0;
0701 
0702     return a->mode;
0703 }
0704 
0705 /*
0706  * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
0707  */
0708 const struct attribute_group nd_numa_attribute_group = {
0709     .attrs = nd_numa_attributes,
0710     .is_visible = nd_numa_attr_visible,
0711 };
0712 
0713 static void ndctl_release(struct device *dev)
0714 {
0715     kfree(dev);
0716 }
0717 
0718 static struct lock_class_key nvdimm_ndctl_key;
0719 
0720 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
0721 {
0722     dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
0723     struct device *dev;
0724     int rc;
0725 
0726     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0727     if (!dev)
0728         return -ENOMEM;
0729     device_initialize(dev);
0730     lockdep_set_class(&dev->mutex, &nvdimm_ndctl_key);
0731     device_set_pm_not_required(dev);
0732     dev->class = nd_class;
0733     dev->parent = &nvdimm_bus->dev;
0734     dev->devt = devt;
0735     dev->release = ndctl_release;
0736     rc = dev_set_name(dev, "ndctl%d", nvdimm_bus->id);
0737     if (rc)
0738         goto err;
0739 
0740     rc = device_add(dev);
0741     if (rc) {
0742         dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %d\n",
0743                 nvdimm_bus->id, rc);
0744         goto err;
0745     }
0746     return 0;
0747 
0748 err:
0749     put_device(dev);
0750     return rc;
0751 }
0752 
0753 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
0754 {
0755     device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
0756 }
0757 
0758 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
0759     [ND_CMD_IMPLEMENTED] = { },
0760     [ND_CMD_SMART] = {
0761         .out_num = 2,
0762         .out_sizes = { 4, 128, },
0763     },
0764     [ND_CMD_SMART_THRESHOLD] = {
0765         .out_num = 2,
0766         .out_sizes = { 4, 8, },
0767     },
0768     [ND_CMD_DIMM_FLAGS] = {
0769         .out_num = 2,
0770         .out_sizes = { 4, 4 },
0771     },
0772     [ND_CMD_GET_CONFIG_SIZE] = {
0773         .out_num = 3,
0774         .out_sizes = { 4, 4, 4, },
0775     },
0776     [ND_CMD_GET_CONFIG_DATA] = {
0777         .in_num = 2,
0778         .in_sizes = { 4, 4, },
0779         .out_num = 2,
0780         .out_sizes = { 4, UINT_MAX, },
0781     },
0782     [ND_CMD_SET_CONFIG_DATA] = {
0783         .in_num = 3,
0784         .in_sizes = { 4, 4, UINT_MAX, },
0785         .out_num = 1,
0786         .out_sizes = { 4, },
0787     },
0788     [ND_CMD_VENDOR] = {
0789         .in_num = 3,
0790         .in_sizes = { 4, 4, UINT_MAX, },
0791         .out_num = 3,
0792         .out_sizes = { 4, 4, UINT_MAX, },
0793     },
0794     [ND_CMD_CALL] = {
0795         .in_num = 2,
0796         .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
0797         .out_num = 1,
0798         .out_sizes = { UINT_MAX, },
0799     },
0800 };
0801 
0802 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
0803 {
0804     if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
0805         return &__nd_cmd_dimm_descs[cmd];
0806     return NULL;
0807 }
0808 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
0809 
0810 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
0811     [ND_CMD_IMPLEMENTED] = { },
0812     [ND_CMD_ARS_CAP] = {
0813         .in_num = 2,
0814         .in_sizes = { 8, 8, },
0815         .out_num = 4,
0816         .out_sizes = { 4, 4, 4, 4, },
0817     },
0818     [ND_CMD_ARS_START] = {
0819         .in_num = 5,
0820         .in_sizes = { 8, 8, 2, 1, 5, },
0821         .out_num = 2,
0822         .out_sizes = { 4, 4, },
0823     },
0824     [ND_CMD_ARS_STATUS] = {
0825         .out_num = 3,
0826         .out_sizes = { 4, 4, UINT_MAX, },
0827     },
0828     [ND_CMD_CLEAR_ERROR] = {
0829         .in_num = 2,
0830         .in_sizes = { 8, 8, },
0831         .out_num = 3,
0832         .out_sizes = { 4, 4, 8, },
0833     },
0834     [ND_CMD_CALL] = {
0835         .in_num = 2,
0836         .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
0837         .out_num = 1,
0838         .out_sizes = { UINT_MAX, },
0839     },
0840 };
0841 
0842 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
0843 {
0844     if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
0845         return &__nd_cmd_bus_descs[cmd];
0846     return NULL;
0847 }
0848 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
0849 
0850 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
0851         const struct nd_cmd_desc *desc, int idx, void *buf)
0852 {
0853     if (idx >= desc->in_num)
0854         return UINT_MAX;
0855 
0856     if (desc->in_sizes[idx] < UINT_MAX)
0857         return desc->in_sizes[idx];
0858 
0859     if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
0860         struct nd_cmd_set_config_hdr *hdr = buf;
0861 
0862         return hdr->in_length;
0863     } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
0864         struct nd_cmd_vendor_hdr *hdr = buf;
0865 
0866         return hdr->in_length;
0867     } else if (cmd == ND_CMD_CALL) {
0868         struct nd_cmd_pkg *pkg = buf;
0869 
0870         return pkg->nd_size_in;
0871     }
0872 
0873     return UINT_MAX;
0874 }
0875 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
0876 
0877 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
0878         const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
0879         const u32 *out_field, unsigned long remainder)
0880 {
0881     if (idx >= desc->out_num)
0882         return UINT_MAX;
0883 
0884     if (desc->out_sizes[idx] < UINT_MAX)
0885         return desc->out_sizes[idx];
0886 
0887     if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
0888         return in_field[1];
0889     else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
0890         return out_field[1];
0891     else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
0892         /*
0893          * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
0894          * "Size of Output Buffer in bytes, including this
0895          * field."
0896          */
0897         if (out_field[1] < 4)
0898             return 0;
0899         /*
0900          * ACPI 6.1 is ambiguous if 'status' is included in the
0901          * output size. If we encounter an output size that
0902          * overshoots the remainder by 4 bytes, assume it was
0903          * including 'status'.
0904          */
0905         if (out_field[1] - 4 == remainder)
0906             return remainder;
0907         return out_field[1] - 8;
0908     } else if (cmd == ND_CMD_CALL) {
0909         struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
0910 
0911         return pkg->nd_size_out;
0912     }
0913 
0914 
0915     return UINT_MAX;
0916 }
0917 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
0918 
0919 void wait_nvdimm_bus_probe_idle(struct device *dev)
0920 {
0921     struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
0922 
0923     do {
0924         if (nvdimm_bus->probe_active == 0)
0925             break;
0926         nvdimm_bus_unlock(dev);
0927         device_unlock(dev);
0928         wait_event(nvdimm_bus->wait,
0929                 nvdimm_bus->probe_active == 0);
0930         device_lock(dev);
0931         nvdimm_bus_lock(dev);
0932     } while (true);
0933 }
0934 
0935 static int nd_pmem_forget_poison_check(struct device *dev, void *data)
0936 {
0937     struct nd_cmd_clear_error *clear_err =
0938         (struct nd_cmd_clear_error *)data;
0939     struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
0940     struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
0941     struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
0942     struct nd_namespace_common *ndns = NULL;
0943     struct nd_namespace_io *nsio;
0944     resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
0945 
0946     if (nd_dax || !dev->driver)
0947         return 0;
0948 
0949     start = clear_err->address;
0950     end = clear_err->address + clear_err->cleared - 1;
0951 
0952     if (nd_btt || nd_pfn || nd_dax) {
0953         if (nd_btt)
0954             ndns = nd_btt->ndns;
0955         else if (nd_pfn)
0956             ndns = nd_pfn->ndns;
0957         else if (nd_dax)
0958             ndns = nd_dax->nd_pfn.ndns;
0959 
0960         if (!ndns)
0961             return 0;
0962     } else
0963         ndns = to_ndns(dev);
0964 
0965     nsio = to_nd_namespace_io(&ndns->dev);
0966     pstart = nsio->res.start + offset;
0967     pend = nsio->res.end - end_trunc;
0968 
0969     if ((pstart >= start) && (pend <= end))
0970         return -EBUSY;
0971 
0972     return 0;
0973 
0974 }
0975 
0976 static int nd_ns_forget_poison_check(struct device *dev, void *data)
0977 {
0978     return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
0979 }
0980 
0981 /* set_config requires an idle interleave set */
0982 static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
0983         struct nvdimm *nvdimm, unsigned int cmd, void *data)
0984 {
0985     struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
0986 
0987     /* ask the bus provider if it would like to block this request */
0988     if (nd_desc->clear_to_send) {
0989         int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd, data);
0990 
0991         if (rc)
0992             return rc;
0993     }
0994 
0995     /* require clear error to go through the pmem driver */
0996     if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
0997         return device_for_each_child(&nvdimm_bus->dev, data,
0998                 nd_ns_forget_poison_check);
0999 
1000     if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
1001         return 0;
1002 
1003     /* prevent label manipulation while the kernel owns label updates */
1004     wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
1005     if (atomic_read(&nvdimm->busy))
1006         return -EBUSY;
1007     return 0;
1008 }
1009 
1010 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
1011         int read_only, unsigned int ioctl_cmd, unsigned long arg)
1012 {
1013     struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
1014     const struct nd_cmd_desc *desc = NULL;
1015     unsigned int cmd = _IOC_NR(ioctl_cmd);
1016     struct device *dev = &nvdimm_bus->dev;
1017     void __user *p = (void __user *) arg;
1018     char *out_env = NULL, *in_env = NULL;
1019     const char *cmd_name, *dimm_name;
1020     u32 in_len = 0, out_len = 0;
1021     unsigned int func = cmd;
1022     unsigned long cmd_mask;
1023     struct nd_cmd_pkg pkg;
1024     int rc, i, cmd_rc;
1025     void *buf = NULL;
1026     u64 buf_len = 0;
1027 
1028     if (nvdimm) {
1029         desc = nd_cmd_dimm_desc(cmd);
1030         cmd_name = nvdimm_cmd_name(cmd);
1031         cmd_mask = nvdimm->cmd_mask;
1032         dimm_name = dev_name(&nvdimm->dev);
1033     } else {
1034         desc = nd_cmd_bus_desc(cmd);
1035         cmd_name = nvdimm_bus_cmd_name(cmd);
1036         cmd_mask = nd_desc->cmd_mask;
1037         dimm_name = "bus";
1038     }
1039 
1040     /* Validate command family support against bus declared support */
1041     if (cmd == ND_CMD_CALL) {
1042         unsigned long *mask;
1043 
1044         if (copy_from_user(&pkg, p, sizeof(pkg)))
1045             return -EFAULT;
1046 
1047         if (nvdimm) {
1048             if (pkg.nd_family > NVDIMM_FAMILY_MAX)
1049                 return -EINVAL;
1050             mask = &nd_desc->dimm_family_mask;
1051         } else {
1052             if (pkg.nd_family > NVDIMM_BUS_FAMILY_MAX)
1053                 return -EINVAL;
1054             mask = &nd_desc->bus_family_mask;
1055         }
1056 
1057         if (!test_bit(pkg.nd_family, mask))
1058             return -EINVAL;
1059     }
1060 
1061     if (!desc ||
1062         (desc->out_num + desc->in_num == 0) ||
1063         cmd > ND_CMD_CALL ||
1064         !test_bit(cmd, &cmd_mask))
1065         return -ENOTTY;
1066 
1067     /* fail write commands (when read-only) */
1068     if (read_only)
1069         switch (cmd) {
1070         case ND_CMD_VENDOR:
1071         case ND_CMD_SET_CONFIG_DATA:
1072         case ND_CMD_ARS_START:
1073         case ND_CMD_CLEAR_ERROR:
1074         case ND_CMD_CALL:
1075             dev_dbg(dev, "'%s' command while read-only.\n",
1076                     nvdimm ? nvdimm_cmd_name(cmd)
1077                     : nvdimm_bus_cmd_name(cmd));
1078             return -EPERM;
1079         default:
1080             break;
1081         }
1082 
1083     /* process an input envelope */
1084     in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1085     if (!in_env)
1086         return -ENOMEM;
1087     for (i = 0; i < desc->in_num; i++) {
1088         u32 in_size, copy;
1089 
1090         in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
1091         if (in_size == UINT_MAX) {
1092             dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
1093                     __func__, dimm_name, cmd_name, i);
1094             rc = -ENXIO;
1095             goto out;
1096         }
1097         if (in_len < ND_CMD_MAX_ENVELOPE)
1098             copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
1099         else
1100             copy = 0;
1101         if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) {
1102             rc = -EFAULT;
1103             goto out;
1104         }
1105         in_len += in_size;
1106     }
1107 
1108     if (cmd == ND_CMD_CALL) {
1109         func = pkg.nd_command;
1110         dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1111                 dimm_name, pkg.nd_command,
1112                 in_len, out_len, buf_len);
1113     }
1114 
1115     /* process an output envelope */
1116     out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1117     if (!out_env) {
1118         rc = -ENOMEM;
1119         goto out;
1120     }
1121 
1122     for (i = 0; i < desc->out_num; i++) {
1123         u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
1124                 (u32 *) in_env, (u32 *) out_env, 0);
1125         u32 copy;
1126 
1127         if (out_size == UINT_MAX) {
1128             dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1129                     dimm_name, cmd_name, i);
1130             rc = -EFAULT;
1131             goto out;
1132         }
1133         if (out_len < ND_CMD_MAX_ENVELOPE)
1134             copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
1135         else
1136             copy = 0;
1137         if (copy && copy_from_user(&out_env[out_len],
1138                     p + in_len + out_len, copy)) {
1139             rc = -EFAULT;
1140             goto out;
1141         }
1142         out_len += out_size;
1143     }
1144 
1145     buf_len = (u64) out_len + (u64) in_len;
1146     if (buf_len > ND_IOCTL_MAX_BUFLEN) {
1147         dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1148                 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
1149         rc = -EINVAL;
1150         goto out;
1151     }
1152 
1153     buf = vmalloc(buf_len);
1154     if (!buf) {
1155         rc = -ENOMEM;
1156         goto out;
1157     }
1158 
1159     if (copy_from_user(buf, p, buf_len)) {
1160         rc = -EFAULT;
1161         goto out;
1162     }
1163 
1164     device_lock(dev);
1165     nvdimm_bus_lock(dev);
1166     rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
1167     if (rc)
1168         goto out_unlock;
1169 
1170     rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
1171     if (rc < 0)
1172         goto out_unlock;
1173 
1174     if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1175         struct nd_cmd_clear_error *clear_err = buf;
1176 
1177         nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1178                 clear_err->cleared);
1179     }
1180 
1181     if (copy_to_user(p, buf, buf_len))
1182         rc = -EFAULT;
1183 
1184 out_unlock:
1185     nvdimm_bus_unlock(dev);
1186     device_unlock(dev);
1187 out:
1188     kfree(in_env);
1189     kfree(out_env);
1190     vfree(buf);
1191     return rc;
1192 }
1193 
1194 enum nd_ioctl_mode {
1195     BUS_IOCTL,
1196     DIMM_IOCTL,
1197 };
1198 
1199 static int match_dimm(struct device *dev, void *data)
1200 {
1201     long id = (long) data;
1202 
1203     if (is_nvdimm(dev)) {
1204         struct nvdimm *nvdimm = to_nvdimm(dev);
1205 
1206         return nvdimm->id == id;
1207     }
1208 
1209     return 0;
1210 }
1211 
1212 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1213         enum nd_ioctl_mode mode)
1214 
1215 {
1216     struct nvdimm_bus *nvdimm_bus, *found = NULL;
1217     long id = (long) file->private_data;
1218     struct nvdimm *nvdimm = NULL;
1219     int rc, ro;
1220 
1221     ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1222     mutex_lock(&nvdimm_bus_list_mutex);
1223     list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1224         if (mode == DIMM_IOCTL) {
1225             struct device *dev;
1226 
1227             dev = device_find_child(&nvdimm_bus->dev,
1228                     file->private_data, match_dimm);
1229             if (!dev)
1230                 continue;
1231             nvdimm = to_nvdimm(dev);
1232             found = nvdimm_bus;
1233         } else if (nvdimm_bus->id == id) {
1234             found = nvdimm_bus;
1235         }
1236 
1237         if (found) {
1238             atomic_inc(&nvdimm_bus->ioctl_active);
1239             break;
1240         }
1241     }
1242     mutex_unlock(&nvdimm_bus_list_mutex);
1243 
1244     if (!found)
1245         return -ENXIO;
1246 
1247     nvdimm_bus = found;
1248     rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1249 
1250     if (nvdimm)
1251         put_device(&nvdimm->dev);
1252     if (atomic_dec_and_test(&nvdimm_bus->ioctl_active))
1253         wake_up(&nvdimm_bus->wait);
1254 
1255     return rc;
1256 }
1257 
1258 static long bus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1259 {
1260     return nd_ioctl(file, cmd, arg, BUS_IOCTL);
1261 }
1262 
1263 static long dimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1264 {
1265     return nd_ioctl(file, cmd, arg, DIMM_IOCTL);
1266 }
1267 
1268 static int nd_open(struct inode *inode, struct file *file)
1269 {
1270     long minor = iminor(inode);
1271 
1272     file->private_data = (void *) minor;
1273     return 0;
1274 }
1275 
1276 static const struct file_operations nvdimm_bus_fops = {
1277     .owner = THIS_MODULE,
1278     .open = nd_open,
1279     .unlocked_ioctl = bus_ioctl,
1280     .compat_ioctl = compat_ptr_ioctl,
1281     .llseek = noop_llseek,
1282 };
1283 
1284 static const struct file_operations nvdimm_fops = {
1285     .owner = THIS_MODULE,
1286     .open = nd_open,
1287     .unlocked_ioctl = dimm_ioctl,
1288     .compat_ioctl = compat_ptr_ioctl,
1289     .llseek = noop_llseek,
1290 };
1291 
1292 int __init nvdimm_bus_init(void)
1293 {
1294     int rc;
1295 
1296     rc = bus_register(&nvdimm_bus_type);
1297     if (rc)
1298         return rc;
1299 
1300     rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1301     if (rc < 0)
1302         goto err_bus_chrdev;
1303     nvdimm_bus_major = rc;
1304 
1305     rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1306     if (rc < 0)
1307         goto err_dimm_chrdev;
1308     nvdimm_major = rc;
1309 
1310     nd_class = class_create(THIS_MODULE, "nd");
1311     if (IS_ERR(nd_class)) {
1312         rc = PTR_ERR(nd_class);
1313         goto err_class;
1314     }
1315 
1316     rc = driver_register(&nd_bus_driver.drv);
1317     if (rc)
1318         goto err_nd_bus;
1319 
1320     return 0;
1321 
1322  err_nd_bus:
1323     class_destroy(nd_class);
1324  err_class:
1325     unregister_chrdev(nvdimm_major, "dimmctl");
1326  err_dimm_chrdev:
1327     unregister_chrdev(nvdimm_bus_major, "ndctl");
1328  err_bus_chrdev:
1329     bus_unregister(&nvdimm_bus_type);
1330 
1331     return rc;
1332 }
1333 
1334 void nvdimm_bus_exit(void)
1335 {
1336     driver_unregister(&nd_bus_driver.drv);
1337     class_destroy(nd_class);
1338     unregister_chrdev(nvdimm_bus_major, "ndctl");
1339     unregister_chrdev(nvdimm_major, "dimmctl");
1340     bus_unregister(&nvdimm_bus_type);
1341     ida_destroy(&nd_ida);
1342 }