2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
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>
35 static int nvdimm_bus_major;
36 static struct class *nd_class;
37 static DEFINE_IDA(nd_ida);
39 static int to_nd_device_type(struct device *dev)
42 return ND_DEVICE_DIMM;
43 else if (is_memory(dev))
44 return ND_DEVICE_REGION_PMEM;
45 else if (is_nd_blk(dev))
46 return ND_DEVICE_REGION_BLK;
47 else if (is_nd_dax(dev))
48 return ND_DEVICE_DAX_PMEM;
49 else if (is_nd_region(dev->parent))
50 return nd_region_to_nstype(to_nd_region(dev->parent));
55 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
58 * Ensure that region devices always have their numa node set as
61 if (is_nd_region(dev))
62 set_dev_node(dev, to_nd_region(dev)->numa_node);
63 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
64 to_nd_device_type(dev));
67 static struct module *to_bus_provider(struct device *dev)
69 /* pin bus providers while regions are enabled */
70 if (is_nd_region(dev)) {
71 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
73 return nvdimm_bus->nd_desc->module;
78 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
80 nvdimm_bus_lock(&nvdimm_bus->dev);
81 nvdimm_bus->probe_active++;
82 nvdimm_bus_unlock(&nvdimm_bus->dev);
85 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
87 nvdimm_bus_lock(&nvdimm_bus->dev);
88 if (--nvdimm_bus->probe_active == 0)
89 wake_up(&nvdimm_bus->wait);
90 nvdimm_bus_unlock(&nvdimm_bus->dev);
93 static int nvdimm_bus_probe(struct device *dev)
95 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
96 struct module *provider = to_bus_provider(dev);
97 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
100 if (!try_module_get(provider))
103 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
104 dev->driver->name, dev_name(dev));
106 nvdimm_bus_probe_start(nvdimm_bus);
107 rc = nd_drv->probe(dev);
109 nd_region_probe_success(nvdimm_bus, dev);
111 nd_region_disable(nvdimm_bus, dev);
112 nvdimm_bus_probe_end(nvdimm_bus);
114 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
118 module_put(provider);
122 static int nvdimm_bus_remove(struct device *dev)
124 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
125 struct module *provider = to_bus_provider(dev);
126 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
130 rc = nd_drv->remove(dev);
131 nd_region_disable(nvdimm_bus, dev);
133 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
135 module_put(provider);
139 static void nvdimm_bus_shutdown(struct device *dev)
141 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
142 struct nd_device_driver *nd_drv = NULL;
145 nd_drv = to_nd_device_driver(dev->driver);
147 if (nd_drv && nd_drv->shutdown) {
148 nd_drv->shutdown(dev);
149 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
150 dev->driver->name, dev_name(dev));
154 void nd_device_notify(struct device *dev, enum nvdimm_event event)
158 struct nd_device_driver *nd_drv;
160 nd_drv = to_nd_device_driver(dev->driver);
162 nd_drv->notify(dev, event);
166 EXPORT_SYMBOL(nd_device_notify);
168 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
170 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
175 /* caller is responsible for holding a reference on the device */
176 nd_device_notify(&nd_region->dev, event);
178 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
180 struct clear_badblocks_context {
181 resource_size_t phys, cleared;
184 static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
186 struct clear_badblocks_context *ctx = data;
187 struct nd_region *nd_region;
188 resource_size_t ndr_end;
191 /* make sure device is a region */
195 nd_region = to_nd_region(dev);
196 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
198 /* make sure we are in the region */
199 if (ctx->phys < nd_region->ndr_start ||
200 (ctx->phys + ctx->cleared - 1) > ndr_end)
203 sector = (ctx->phys - nd_region->ndr_start) / 512;
204 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
206 if (nd_region->bb_state)
207 sysfs_notify_dirent(nd_region->bb_state);
212 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
213 phys_addr_t phys, u64 cleared)
215 struct clear_badblocks_context ctx = {
220 device_for_each_child(&nvdimm_bus->dev, &ctx,
221 nvdimm_clear_badblocks_region);
224 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
225 phys_addr_t phys, u64 cleared)
228 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
230 if (cleared > 0 && cleared / 512)
231 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
234 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
237 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
238 struct nvdimm_bus_descriptor *nd_desc;
239 struct nd_cmd_clear_error clear_err;
240 struct nd_cmd_ars_cap ars_cap;
241 u32 clear_err_unit, mask;
242 unsigned int noio_flag;
248 nd_desc = nvdimm_bus->nd_desc;
250 * if ndctl does not exist, it's PMEM_LEGACY and
251 * we want to just pretend everything is handled.
256 memset(&ars_cap, 0, sizeof(ars_cap));
257 ars_cap.address = phys;
258 ars_cap.length = len;
259 noio_flag = memalloc_noio_save();
260 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
261 sizeof(ars_cap), &cmd_rc);
262 memalloc_noio_restore(noio_flag);
267 clear_err_unit = ars_cap.clear_err_unit;
268 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
271 mask = clear_err_unit - 1;
272 if ((phys | len) & mask)
274 memset(&clear_err, 0, sizeof(clear_err));
275 clear_err.address = phys;
276 clear_err.length = len;
277 noio_flag = memalloc_noio_save();
278 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
279 sizeof(clear_err), &cmd_rc);
280 memalloc_noio_restore(noio_flag);
286 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
288 return clear_err.cleared;
290 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
292 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
294 static struct bus_type nvdimm_bus_type = {
296 .uevent = nvdimm_bus_uevent,
297 .match = nvdimm_bus_match,
298 .probe = nvdimm_bus_probe,
299 .remove = nvdimm_bus_remove,
300 .shutdown = nvdimm_bus_shutdown,
303 static void nvdimm_bus_release(struct device *dev)
305 struct nvdimm_bus *nvdimm_bus;
307 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
308 ida_simple_remove(&nd_ida, nvdimm_bus->id);
312 static bool is_nvdimm_bus(struct device *dev)
314 return dev->release == nvdimm_bus_release;
317 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
321 for (dev = nd_dev; dev; dev = dev->parent)
322 if (is_nvdimm_bus(dev))
324 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
326 return to_nvdimm_bus(dev);
330 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
332 struct nvdimm_bus *nvdimm_bus;
334 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
335 WARN_ON(!is_nvdimm_bus(dev));
338 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
340 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
341 struct nvdimm_bus_descriptor *nd_desc)
343 struct nvdimm_bus *nvdimm_bus;
346 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
349 INIT_LIST_HEAD(&nvdimm_bus->list);
350 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
351 init_waitqueue_head(&nvdimm_bus->wait);
352 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
353 mutex_init(&nvdimm_bus->reconfig_mutex);
354 badrange_init(&nvdimm_bus->badrange);
355 if (nvdimm_bus->id < 0) {
359 nvdimm_bus->nd_desc = nd_desc;
360 nvdimm_bus->dev.parent = parent;
361 nvdimm_bus->dev.release = nvdimm_bus_release;
362 nvdimm_bus->dev.groups = nd_desc->attr_groups;
363 nvdimm_bus->dev.bus = &nvdimm_bus_type;
364 nvdimm_bus->dev.of_node = nd_desc->of_node;
365 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
366 rc = device_register(&nvdimm_bus->dev);
368 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
374 put_device(&nvdimm_bus->dev);
377 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
379 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
383 device_unregister(&nvdimm_bus->dev);
385 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
387 static int child_unregister(struct device *dev, void *data)
390 * the singular ndctl class device per bus needs to be
391 * "device_destroy"ed, so skip it here
393 * i.e. remove classless children
398 nd_device_unregister(dev, ND_SYNC);
402 static void free_badrange_list(struct list_head *badrange_list)
404 struct badrange_entry *bre, *next;
406 list_for_each_entry_safe(bre, next, badrange_list, list) {
407 list_del(&bre->list);
410 list_del_init(badrange_list);
413 static int nd_bus_remove(struct device *dev)
415 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
417 mutex_lock(&nvdimm_bus_list_mutex);
418 list_del_init(&nvdimm_bus->list);
419 mutex_unlock(&nvdimm_bus_list_mutex);
421 wait_event(nvdimm_bus->wait,
422 atomic_read(&nvdimm_bus->ioctl_active) == 0);
425 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
427 spin_lock(&nvdimm_bus->badrange.lock);
428 free_badrange_list(&nvdimm_bus->badrange.list);
429 spin_unlock(&nvdimm_bus->badrange.lock);
431 nvdimm_bus_destroy_ndctl(nvdimm_bus);
436 static int nd_bus_probe(struct device *dev)
438 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
441 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
445 mutex_lock(&nvdimm_bus_list_mutex);
446 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
447 mutex_unlock(&nvdimm_bus_list_mutex);
449 /* enable bus provider attributes to look up their local context */
450 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
455 static struct nd_device_driver nd_bus_driver = {
456 .probe = nd_bus_probe,
457 .remove = nd_bus_remove,
460 .suppress_bind_attrs = true,
461 .bus = &nvdimm_bus_type,
462 .owner = THIS_MODULE,
463 .mod_name = KBUILD_MODNAME,
467 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
469 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
471 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
474 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
477 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
479 void nd_synchronize(void)
481 async_synchronize_full_domain(&nd_async_domain);
483 EXPORT_SYMBOL_GPL(nd_synchronize);
485 static void nd_async_device_register(void *d, async_cookie_t cookie)
487 struct device *dev = d;
489 if (device_add(dev) != 0) {
490 dev_err(dev, "%s: failed\n", __func__);
495 put_device(dev->parent);
498 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
500 struct device *dev = d;
502 /* flush bus operations before delete */
503 nvdimm_bus_lock(dev);
504 nvdimm_bus_unlock(dev);
506 device_unregister(dev);
510 void __nd_device_register(struct device *dev)
514 dev->bus = &nvdimm_bus_type;
516 get_device(dev->parent);
518 async_schedule_domain(nd_async_device_register, dev,
522 void nd_device_register(struct device *dev)
524 device_initialize(dev);
525 __nd_device_register(dev);
527 EXPORT_SYMBOL(nd_device_register);
529 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
536 * In the async case this is being triggered with the
537 * device lock held and the unregistration work needs to
538 * be moved out of line iff this is thread has won the
539 * race to schedule the deletion.
541 if (!kill_device(dev))
545 async_schedule_domain(nd_async_device_unregister, dev,
550 * In the sync case the device is being unregistered due
551 * to a state change of the parent. Claim the kill state
552 * to synchronize against other unregistration requests,
553 * or otherwise let the async path handle it if the
554 * unregistration was already queued.
557 killed = kill_device(dev);
564 device_unregister(dev);
568 EXPORT_SYMBOL(nd_device_unregister);
571 * __nd_driver_register() - register a region or a namespace driver
572 * @nd_drv: driver to register
573 * @owner: automatically set by nd_driver_register() macro
574 * @mod_name: automatically set by nd_driver_register() macro
576 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
577 const char *mod_name)
579 struct device_driver *drv = &nd_drv->drv;
582 pr_debug("driver type bitmask not set (%pf)\n",
583 __builtin_return_address(0));
587 if (!nd_drv->probe) {
588 pr_debug("%s ->probe() must be specified\n", mod_name);
592 drv->bus = &nvdimm_bus_type;
594 drv->mod_name = mod_name;
596 return driver_register(drv);
598 EXPORT_SYMBOL(__nd_driver_register);
600 int nvdimm_revalidate_disk(struct gendisk *disk)
602 struct device *dev = disk_to_dev(disk)->parent;
603 struct nd_region *nd_region = to_nd_region(dev->parent);
604 int disk_ro = get_disk_ro(disk);
607 * Upgrade to read-only if the region is read-only preserve as
608 * read-only if the disk is already read-only.
610 if (disk_ro || nd_region->ro == disk_ro)
613 dev_info(dev, "%s read-only, marking %s read-only\n",
614 dev_name(&nd_region->dev), disk->disk_name);
615 set_disk_ro(disk, 1);
620 EXPORT_SYMBOL(nvdimm_revalidate_disk);
622 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
625 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
626 to_nd_device_type(dev));
628 static DEVICE_ATTR_RO(modalias);
630 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
633 return sprintf(buf, "%s\n", dev->type->name);
635 static DEVICE_ATTR_RO(devtype);
637 static struct attribute *nd_device_attributes[] = {
638 &dev_attr_modalias.attr,
639 &dev_attr_devtype.attr,
644 * nd_device_attribute_group - generic attributes for all devices on an nd bus
646 struct attribute_group nd_device_attribute_group = {
647 .attrs = nd_device_attributes,
649 EXPORT_SYMBOL_GPL(nd_device_attribute_group);
651 static ssize_t numa_node_show(struct device *dev,
652 struct device_attribute *attr, char *buf)
654 return sprintf(buf, "%d\n", dev_to_node(dev));
656 static DEVICE_ATTR_RO(numa_node);
658 static struct attribute *nd_numa_attributes[] = {
659 &dev_attr_numa_node.attr,
663 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
666 if (!IS_ENABLED(CONFIG_NUMA))
673 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
675 struct attribute_group nd_numa_attribute_group = {
676 .attrs = nd_numa_attributes,
677 .is_visible = nd_numa_attr_visible,
679 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
681 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
683 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
686 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
687 "ndctl%d", nvdimm_bus->id);
690 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
691 nvdimm_bus->id, PTR_ERR(dev));
692 return PTR_ERR_OR_ZERO(dev);
695 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
697 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
700 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
701 [ND_CMD_IMPLEMENTED] = { },
704 .out_sizes = { 4, 128, },
706 [ND_CMD_SMART_THRESHOLD] = {
708 .out_sizes = { 4, 8, },
710 [ND_CMD_DIMM_FLAGS] = {
712 .out_sizes = { 4, 4 },
714 [ND_CMD_GET_CONFIG_SIZE] = {
716 .out_sizes = { 4, 4, 4, },
718 [ND_CMD_GET_CONFIG_DATA] = {
720 .in_sizes = { 4, 4, },
722 .out_sizes = { 4, UINT_MAX, },
724 [ND_CMD_SET_CONFIG_DATA] = {
726 .in_sizes = { 4, 4, UINT_MAX, },
732 .in_sizes = { 4, 4, UINT_MAX, },
734 .out_sizes = { 4, 4, UINT_MAX, },
738 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
740 .out_sizes = { UINT_MAX, },
744 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
746 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
747 return &__nd_cmd_dimm_descs[cmd];
750 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
752 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
753 [ND_CMD_IMPLEMENTED] = { },
756 .in_sizes = { 8, 8, },
758 .out_sizes = { 4, 4, 4, 4, },
760 [ND_CMD_ARS_START] = {
762 .in_sizes = { 8, 8, 2, 1, 5, },
764 .out_sizes = { 4, 4, },
766 [ND_CMD_ARS_STATUS] = {
768 .out_sizes = { 4, 4, UINT_MAX, },
770 [ND_CMD_CLEAR_ERROR] = {
772 .in_sizes = { 8, 8, },
774 .out_sizes = { 4, 4, 8, },
778 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
780 .out_sizes = { UINT_MAX, },
784 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
786 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
787 return &__nd_cmd_bus_descs[cmd];
790 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
792 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
793 const struct nd_cmd_desc *desc, int idx, void *buf)
795 if (idx >= desc->in_num)
798 if (desc->in_sizes[idx] < UINT_MAX)
799 return desc->in_sizes[idx];
801 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
802 struct nd_cmd_set_config_hdr *hdr = buf;
804 return hdr->in_length;
805 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
806 struct nd_cmd_vendor_hdr *hdr = buf;
808 return hdr->in_length;
809 } else if (cmd == ND_CMD_CALL) {
810 struct nd_cmd_pkg *pkg = buf;
812 return pkg->nd_size_in;
817 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
819 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
820 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
821 const u32 *out_field, unsigned long remainder)
823 if (idx >= desc->out_num)
826 if (desc->out_sizes[idx] < UINT_MAX)
827 return desc->out_sizes[idx];
829 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
831 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
833 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
835 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
836 * "Size of Output Buffer in bytes, including this
839 if (out_field[1] < 4)
842 * ACPI 6.1 is ambiguous if 'status' is included in the
843 * output size. If we encounter an output size that
844 * overshoots the remainder by 4 bytes, assume it was
845 * including 'status'.
847 if (out_field[1] - 4 == remainder)
849 return out_field[1] - 8;
850 } else if (cmd == ND_CMD_CALL) {
851 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
853 return pkg->nd_size_out;
859 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
861 void wait_nvdimm_bus_probe_idle(struct device *dev)
863 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
866 if (nvdimm_bus->probe_active == 0)
868 nvdimm_bus_unlock(dev);
870 wait_event(nvdimm_bus->wait,
871 nvdimm_bus->probe_active == 0);
873 nvdimm_bus_lock(dev);
877 static int nd_pmem_forget_poison_check(struct device *dev, void *data)
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;
888 if (nd_dax || !dev->driver)
891 start = clear_err->address;
892 end = clear_err->address + clear_err->cleared - 1;
894 if (nd_btt || nd_pfn || nd_dax) {
900 ndns = nd_dax->nd_pfn.ndns;
907 nsio = to_nd_namespace_io(&ndns->dev);
908 pstart = nsio->res.start + offset;
909 pend = nsio->res.end - end_trunc;
911 if ((pstart >= start) && (pend <= end))
918 static int nd_ns_forget_poison_check(struct device *dev, void *data)
920 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
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)
927 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
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);
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);
942 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
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))
952 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
953 int read_only, unsigned int ioctl_cmd, unsigned long arg)
955 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
956 const struct nd_cmd_desc *desc = NULL;
957 unsigned int cmd = _IOC_NR(ioctl_cmd);
958 struct device *dev = &nvdimm_bus->dev;
959 void __user *p = (void __user *) arg;
960 char *out_env = NULL, *in_env = NULL;
961 const char *cmd_name, *dimm_name;
962 u32 in_len = 0, out_len = 0;
963 unsigned int func = cmd;
964 unsigned long cmd_mask;
965 struct nd_cmd_pkg pkg;
971 desc = nd_cmd_dimm_desc(cmd);
972 cmd_name = nvdimm_cmd_name(cmd);
973 cmd_mask = nvdimm->cmd_mask;
974 dimm_name = dev_name(&nvdimm->dev);
976 desc = nd_cmd_bus_desc(cmd);
977 cmd_name = nvdimm_bus_cmd_name(cmd);
978 cmd_mask = nd_desc->cmd_mask;
982 if (cmd == ND_CMD_CALL) {
983 if (copy_from_user(&pkg, p, sizeof(pkg)))
988 (desc->out_num + desc->in_num == 0) ||
990 !test_bit(cmd, &cmd_mask))
993 /* fail write commands (when read-only) */
997 case ND_CMD_SET_CONFIG_DATA:
998 case ND_CMD_ARS_START:
999 case ND_CMD_CLEAR_ERROR:
1001 dev_dbg(dev, "'%s' command while read-only.\n",
1002 nvdimm ? nvdimm_cmd_name(cmd)
1003 : nvdimm_bus_cmd_name(cmd));
1009 /* process an input envelope */
1010 in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1013 for (i = 0; i < desc->in_num; i++) {
1016 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
1017 if (in_size == UINT_MAX) {
1018 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
1019 __func__, dimm_name, cmd_name, i);
1023 if (in_len < ND_CMD_MAX_ENVELOPE)
1024 copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
1027 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) {
1034 if (cmd == ND_CMD_CALL) {
1035 func = pkg.nd_command;
1036 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1037 dimm_name, pkg.nd_command,
1038 in_len, out_len, buf_len);
1041 /* process an output envelope */
1042 out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1048 for (i = 0; i < desc->out_num; i++) {
1049 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
1050 (u32 *) in_env, (u32 *) out_env, 0);
1053 if (out_size == UINT_MAX) {
1054 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1055 dimm_name, cmd_name, i);
1059 if (out_len < ND_CMD_MAX_ENVELOPE)
1060 copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
1063 if (copy && copy_from_user(&out_env[out_len],
1064 p + in_len + out_len, copy)) {
1068 out_len += out_size;
1071 buf_len = (u64) out_len + (u64) in_len;
1072 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
1073 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1074 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
1079 buf = vmalloc(buf_len);
1085 if (copy_from_user(buf, p, buf_len)) {
1091 nvdimm_bus_lock(dev);
1092 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
1096 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
1100 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1101 struct nd_cmd_clear_error *clear_err = buf;
1103 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1104 clear_err->cleared);
1107 if (copy_to_user(p, buf, buf_len))
1111 nvdimm_bus_unlock(dev);
1120 enum nd_ioctl_mode {
1125 static int match_dimm(struct device *dev, void *data)
1127 long id = (long) data;
1129 if (is_nvdimm(dev)) {
1130 struct nvdimm *nvdimm = to_nvdimm(dev);
1132 return nvdimm->id == id;
1138 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1139 enum nd_ioctl_mode mode)
1142 struct nvdimm_bus *nvdimm_bus, *found = NULL;
1143 long id = (long) file->private_data;
1144 struct nvdimm *nvdimm = NULL;
1147 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1148 mutex_lock(&nvdimm_bus_list_mutex);
1149 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1150 if (mode == DIMM_IOCTL) {
1153 dev = device_find_child(&nvdimm_bus->dev,
1154 file->private_data, match_dimm);
1157 nvdimm = to_nvdimm(dev);
1159 } else if (nvdimm_bus->id == id) {
1164 atomic_inc(&nvdimm_bus->ioctl_active);
1168 mutex_unlock(&nvdimm_bus_list_mutex);
1174 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1177 put_device(&nvdimm->dev);
1178 if (atomic_dec_and_test(&nvdimm_bus->ioctl_active))
1179 wake_up(&nvdimm_bus->wait);
1184 static long bus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1186 return nd_ioctl(file, cmd, arg, BUS_IOCTL);
1189 static long dimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1191 return nd_ioctl(file, cmd, arg, DIMM_IOCTL);
1194 static int nd_open(struct inode *inode, struct file *file)
1196 long minor = iminor(inode);
1198 file->private_data = (void *) minor;
1202 static const struct file_operations nvdimm_bus_fops = {
1203 .owner = THIS_MODULE,
1205 .unlocked_ioctl = bus_ioctl,
1206 .compat_ioctl = bus_ioctl,
1207 .llseek = noop_llseek,
1210 static const struct file_operations nvdimm_fops = {
1211 .owner = THIS_MODULE,
1213 .unlocked_ioctl = dimm_ioctl,
1214 .compat_ioctl = dimm_ioctl,
1215 .llseek = noop_llseek,
1218 int __init nvdimm_bus_init(void)
1222 rc = bus_register(&nvdimm_bus_type);
1226 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1228 goto err_bus_chrdev;
1229 nvdimm_bus_major = rc;
1231 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1233 goto err_dimm_chrdev;
1236 nd_class = class_create(THIS_MODULE, "nd");
1237 if (IS_ERR(nd_class)) {
1238 rc = PTR_ERR(nd_class);
1242 rc = driver_register(&nd_bus_driver.drv);
1249 class_destroy(nd_class);
1251 unregister_chrdev(nvdimm_major, "dimmctl");
1253 unregister_chrdev(nvdimm_bus_major, "ndctl");
1255 bus_unregister(&nvdimm_bus_type);
1260 void nvdimm_bus_exit(void)
1262 driver_unregister(&nd_bus_driver.drv);
1263 class_destroy(nd_class);
1264 unregister_chrdev(nvdimm_bus_major, "ndctl");
1265 unregister_chrdev(nvdimm_major, "dimmctl");
1266 bus_unregister(&nvdimm_bus_type);
1267 ida_destroy(&nd_ida);