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)
57 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
58 to_nd_device_type(dev));
61 static struct module *to_bus_provider(struct device *dev)
63 /* pin bus providers while regions are enabled */
64 if (is_nd_region(dev)) {
65 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
67 return nvdimm_bus->nd_desc->module;
72 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
74 nvdimm_bus_lock(&nvdimm_bus->dev);
75 nvdimm_bus->probe_active++;
76 nvdimm_bus_unlock(&nvdimm_bus->dev);
79 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
81 nvdimm_bus_lock(&nvdimm_bus->dev);
82 if (--nvdimm_bus->probe_active == 0)
83 wake_up(&nvdimm_bus->probe_wait);
84 nvdimm_bus_unlock(&nvdimm_bus->dev);
87 static int nvdimm_bus_probe(struct device *dev)
89 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
90 struct module *provider = to_bus_provider(dev);
91 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
94 if (!try_module_get(provider))
97 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
98 dev->driver->name, dev_name(dev));
100 nvdimm_bus_probe_start(nvdimm_bus);
101 rc = nd_drv->probe(dev);
103 nd_region_probe_success(nvdimm_bus, dev);
105 nd_region_disable(nvdimm_bus, dev);
106 nvdimm_bus_probe_end(nvdimm_bus);
108 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
112 module_put(provider);
116 static int nvdimm_bus_remove(struct device *dev)
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);
124 rc = nd_drv->remove(dev);
125 nd_region_disable(nvdimm_bus, dev);
127 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
129 module_put(provider);
133 static void nvdimm_bus_shutdown(struct device *dev)
135 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
136 struct nd_device_driver *nd_drv = NULL;
139 nd_drv = to_nd_device_driver(dev->driver);
141 if (nd_drv && nd_drv->shutdown) {
142 nd_drv->shutdown(dev);
143 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
144 dev->driver->name, dev_name(dev));
148 void nd_device_notify(struct device *dev, enum nvdimm_event event)
152 struct nd_device_driver *nd_drv;
154 nd_drv = to_nd_device_driver(dev->driver);
156 nd_drv->notify(dev, event);
160 EXPORT_SYMBOL(nd_device_notify);
162 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
164 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
169 /* caller is responsible for holding a reference on the device */
170 nd_device_notify(&nd_region->dev, event);
172 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
174 struct clear_badblocks_context {
175 resource_size_t phys, cleared;
178 static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
180 struct clear_badblocks_context *ctx = data;
181 struct nd_region *nd_region;
182 resource_size_t ndr_end;
185 /* make sure device is a region */
186 if (!is_nd_pmem(dev))
189 nd_region = to_nd_region(dev);
190 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
192 /* make sure we are in the region */
193 if (ctx->phys < nd_region->ndr_start
194 || (ctx->phys + ctx->cleared) > ndr_end)
197 sector = (ctx->phys - nd_region->ndr_start) / 512;
198 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
200 if (nd_region->bb_state)
201 sysfs_notify_dirent(nd_region->bb_state);
206 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
207 phys_addr_t phys, u64 cleared)
209 struct clear_badblocks_context ctx = {
214 device_for_each_child(&nvdimm_bus->dev, &ctx,
215 nvdimm_clear_badblocks_region);
218 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
219 phys_addr_t phys, u64 cleared)
222 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
224 if (cleared > 0 && cleared / 512)
225 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
228 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
231 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
232 struct nvdimm_bus_descriptor *nd_desc;
233 struct nd_cmd_clear_error clear_err;
234 struct nd_cmd_ars_cap ars_cap;
235 u32 clear_err_unit, mask;
236 unsigned int noio_flag;
242 nd_desc = nvdimm_bus->nd_desc;
244 * if ndctl does not exist, it's PMEM_LEGACY and
245 * we want to just pretend everything is handled.
250 memset(&ars_cap, 0, sizeof(ars_cap));
251 ars_cap.address = phys;
252 ars_cap.length = len;
253 noio_flag = memalloc_noio_save();
254 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
255 sizeof(ars_cap), &cmd_rc);
256 memalloc_noio_restore(noio_flag);
261 clear_err_unit = ars_cap.clear_err_unit;
262 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
265 mask = clear_err_unit - 1;
266 if ((phys | len) & mask)
268 memset(&clear_err, 0, sizeof(clear_err));
269 clear_err.address = phys;
270 clear_err.length = len;
271 noio_flag = memalloc_noio_save();
272 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
273 sizeof(clear_err), &cmd_rc);
274 memalloc_noio_restore(noio_flag);
280 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
282 return clear_err.cleared;
284 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
286 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
288 static struct bus_type nvdimm_bus_type = {
290 .uevent = nvdimm_bus_uevent,
291 .match = nvdimm_bus_match,
292 .probe = nvdimm_bus_probe,
293 .remove = nvdimm_bus_remove,
294 .shutdown = nvdimm_bus_shutdown,
297 static void nvdimm_bus_release(struct device *dev)
299 struct nvdimm_bus *nvdimm_bus;
301 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
302 ida_simple_remove(&nd_ida, nvdimm_bus->id);
306 static bool is_nvdimm_bus(struct device *dev)
308 return dev->release == nvdimm_bus_release;
311 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
315 for (dev = nd_dev; dev; dev = dev->parent)
316 if (is_nvdimm_bus(dev))
318 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
320 return to_nvdimm_bus(dev);
324 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
326 struct nvdimm_bus *nvdimm_bus;
328 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
329 WARN_ON(!is_nvdimm_bus(dev));
332 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
334 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
335 struct nvdimm_bus_descriptor *nd_desc)
337 struct nvdimm_bus *nvdimm_bus;
340 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
343 INIT_LIST_HEAD(&nvdimm_bus->list);
344 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
345 init_waitqueue_head(&nvdimm_bus->probe_wait);
346 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
347 mutex_init(&nvdimm_bus->reconfig_mutex);
348 badrange_init(&nvdimm_bus->badrange);
349 if (nvdimm_bus->id < 0) {
353 nvdimm_bus->nd_desc = nd_desc;
354 nvdimm_bus->dev.parent = parent;
355 nvdimm_bus->dev.release = nvdimm_bus_release;
356 nvdimm_bus->dev.groups = nd_desc->attr_groups;
357 nvdimm_bus->dev.bus = &nvdimm_bus_type;
358 nvdimm_bus->dev.of_node = nd_desc->of_node;
359 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
360 rc = device_register(&nvdimm_bus->dev);
362 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
368 put_device(&nvdimm_bus->dev);
371 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
373 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
377 device_unregister(&nvdimm_bus->dev);
379 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
381 static int child_unregister(struct device *dev, void *data)
384 * the singular ndctl class device per bus needs to be
385 * "device_destroy"ed, so skip it here
387 * i.e. remove classless children
392 nd_device_unregister(dev, ND_SYNC);
396 static void free_badrange_list(struct list_head *badrange_list)
398 struct badrange_entry *bre, *next;
400 list_for_each_entry_safe(bre, next, badrange_list, list) {
401 list_del(&bre->list);
404 list_del_init(badrange_list);
407 static int nd_bus_remove(struct device *dev)
409 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
411 mutex_lock(&nvdimm_bus_list_mutex);
412 list_del_init(&nvdimm_bus->list);
413 mutex_unlock(&nvdimm_bus_list_mutex);
416 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
418 spin_lock(&nvdimm_bus->badrange.lock);
419 free_badrange_list(&nvdimm_bus->badrange.list);
420 spin_unlock(&nvdimm_bus->badrange.lock);
422 nvdimm_bus_destroy_ndctl(nvdimm_bus);
427 static int nd_bus_probe(struct device *dev)
429 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
432 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
436 mutex_lock(&nvdimm_bus_list_mutex);
437 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
438 mutex_unlock(&nvdimm_bus_list_mutex);
440 /* enable bus provider attributes to look up their local context */
441 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
446 static struct nd_device_driver nd_bus_driver = {
447 .probe = nd_bus_probe,
448 .remove = nd_bus_remove,
451 .suppress_bind_attrs = true,
452 .bus = &nvdimm_bus_type,
453 .owner = THIS_MODULE,
454 .mod_name = KBUILD_MODNAME,
458 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
460 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
462 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
465 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
468 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
470 void nd_synchronize(void)
472 async_synchronize_full_domain(&nd_async_domain);
474 EXPORT_SYMBOL_GPL(nd_synchronize);
476 static void nd_async_device_register(void *d, async_cookie_t cookie)
478 struct device *dev = d;
480 if (device_add(dev) != 0) {
481 dev_err(dev, "%s: failed\n", __func__);
486 put_device(dev->parent);
489 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
491 struct device *dev = d;
493 /* flush bus operations before delete */
494 nvdimm_bus_lock(dev);
495 nvdimm_bus_unlock(dev);
497 device_unregister(dev);
501 void __nd_device_register(struct device *dev)
507 * Ensure that region devices always have their NUMA node set as
508 * early as possible. This way we are able to make certain that
509 * any memory associated with the creation and the creation
510 * itself of the region is associated with the correct node.
512 if (is_nd_region(dev))
513 set_dev_node(dev, to_nd_region(dev)->numa_node);
515 dev->bus = &nvdimm_bus_type;
517 get_device(dev->parent);
519 async_schedule_domain(nd_async_device_register, dev,
523 void nd_device_register(struct device *dev)
525 device_initialize(dev);
526 __nd_device_register(dev);
528 EXPORT_SYMBOL(nd_device_register);
530 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
535 async_schedule_domain(nd_async_device_unregister, dev,
540 device_unregister(dev);
544 EXPORT_SYMBOL(nd_device_unregister);
547 * __nd_driver_register() - register a region or a namespace driver
548 * @nd_drv: driver to register
549 * @owner: automatically set by nd_driver_register() macro
550 * @mod_name: automatically set by nd_driver_register() macro
552 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
553 const char *mod_name)
555 struct device_driver *drv = &nd_drv->drv;
558 pr_debug("driver type bitmask not set (%pf)\n",
559 __builtin_return_address(0));
563 if (!nd_drv->probe) {
564 pr_debug("%s ->probe() must be specified\n", mod_name);
568 drv->bus = &nvdimm_bus_type;
570 drv->mod_name = mod_name;
572 return driver_register(drv);
574 EXPORT_SYMBOL(__nd_driver_register);
576 int nvdimm_revalidate_disk(struct gendisk *disk)
578 struct device *dev = disk_to_dev(disk)->parent;
579 struct nd_region *nd_region = to_nd_region(dev->parent);
580 int disk_ro = get_disk_ro(disk);
583 * Upgrade to read-only if the region is read-only preserve as
584 * read-only if the disk is already read-only.
586 if (disk_ro || nd_region->ro == disk_ro)
589 dev_info(dev, "%s read-only, marking %s read-only\n",
590 dev_name(&nd_region->dev), disk->disk_name);
591 set_disk_ro(disk, 1);
596 EXPORT_SYMBOL(nvdimm_revalidate_disk);
598 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
601 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
602 to_nd_device_type(dev));
604 static DEVICE_ATTR_RO(modalias);
606 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
609 return sprintf(buf, "%s\n", dev->type->name);
611 static DEVICE_ATTR_RO(devtype);
613 static struct attribute *nd_device_attributes[] = {
614 &dev_attr_modalias.attr,
615 &dev_attr_devtype.attr,
620 * nd_device_attribute_group - generic attributes for all devices on an nd bus
622 struct attribute_group nd_device_attribute_group = {
623 .attrs = nd_device_attributes,
625 EXPORT_SYMBOL_GPL(nd_device_attribute_group);
627 static ssize_t numa_node_show(struct device *dev,
628 struct device_attribute *attr, char *buf)
630 return sprintf(buf, "%d\n", dev_to_node(dev));
632 static DEVICE_ATTR_RO(numa_node);
634 static struct attribute *nd_numa_attributes[] = {
635 &dev_attr_numa_node.attr,
639 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
642 if (!IS_ENABLED(CONFIG_NUMA))
649 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
651 struct attribute_group nd_numa_attribute_group = {
652 .attrs = nd_numa_attributes,
653 .is_visible = nd_numa_attr_visible,
655 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
657 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
659 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
662 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
663 "ndctl%d", nvdimm_bus->id);
666 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
667 nvdimm_bus->id, PTR_ERR(dev));
668 return PTR_ERR_OR_ZERO(dev);
671 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
673 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
676 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
677 [ND_CMD_IMPLEMENTED] = { },
680 .out_sizes = { 4, 128, },
682 [ND_CMD_SMART_THRESHOLD] = {
684 .out_sizes = { 4, 8, },
686 [ND_CMD_DIMM_FLAGS] = {
688 .out_sizes = { 4, 4 },
690 [ND_CMD_GET_CONFIG_SIZE] = {
692 .out_sizes = { 4, 4, 4, },
694 [ND_CMD_GET_CONFIG_DATA] = {
696 .in_sizes = { 4, 4, },
698 .out_sizes = { 4, UINT_MAX, },
700 [ND_CMD_SET_CONFIG_DATA] = {
702 .in_sizes = { 4, 4, UINT_MAX, },
708 .in_sizes = { 4, 4, UINT_MAX, },
710 .out_sizes = { 4, 4, UINT_MAX, },
714 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
716 .out_sizes = { UINT_MAX, },
720 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
722 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
723 return &__nd_cmd_dimm_descs[cmd];
726 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
728 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
729 [ND_CMD_IMPLEMENTED] = { },
732 .in_sizes = { 8, 8, },
734 .out_sizes = { 4, 4, 4, 4, },
736 [ND_CMD_ARS_START] = {
738 .in_sizes = { 8, 8, 2, 1, 5, },
740 .out_sizes = { 4, 4, },
742 [ND_CMD_ARS_STATUS] = {
744 .out_sizes = { 4, 4, UINT_MAX, },
746 [ND_CMD_CLEAR_ERROR] = {
748 .in_sizes = { 8, 8, },
750 .out_sizes = { 4, 4, 8, },
754 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
756 .out_sizes = { UINT_MAX, },
760 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
762 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
763 return &__nd_cmd_bus_descs[cmd];
766 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
768 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
769 const struct nd_cmd_desc *desc, int idx, void *buf)
771 if (idx >= desc->in_num)
774 if (desc->in_sizes[idx] < UINT_MAX)
775 return desc->in_sizes[idx];
777 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
778 struct nd_cmd_set_config_hdr *hdr = buf;
780 return hdr->in_length;
781 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
782 struct nd_cmd_vendor_hdr *hdr = buf;
784 return hdr->in_length;
785 } else if (cmd == ND_CMD_CALL) {
786 struct nd_cmd_pkg *pkg = buf;
788 return pkg->nd_size_in;
793 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
795 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
796 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
797 const u32 *out_field, unsigned long remainder)
799 if (idx >= desc->out_num)
802 if (desc->out_sizes[idx] < UINT_MAX)
803 return desc->out_sizes[idx];
805 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
807 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
809 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
811 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
812 * "Size of Output Buffer in bytes, including this
815 if (out_field[1] < 4)
818 * ACPI 6.1 is ambiguous if 'status' is included in the
819 * output size. If we encounter an output size that
820 * overshoots the remainder by 4 bytes, assume it was
821 * including 'status'.
823 if (out_field[1] - 4 == remainder)
825 return out_field[1] - 8;
826 } else if (cmd == ND_CMD_CALL) {
827 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
829 return pkg->nd_size_out;
835 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
837 void wait_nvdimm_bus_probe_idle(struct device *dev)
839 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
842 if (nvdimm_bus->probe_active == 0)
844 nvdimm_bus_unlock(&nvdimm_bus->dev);
845 wait_event(nvdimm_bus->probe_wait,
846 nvdimm_bus->probe_active == 0);
847 nvdimm_bus_lock(&nvdimm_bus->dev);
851 static int nd_pmem_forget_poison_check(struct device *dev, void *data)
853 struct nd_cmd_clear_error *clear_err =
854 (struct nd_cmd_clear_error *)data;
855 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
856 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
857 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
858 struct nd_namespace_common *ndns = NULL;
859 struct nd_namespace_io *nsio;
860 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
862 if (nd_dax || !dev->driver)
865 start = clear_err->address;
866 end = clear_err->address + clear_err->cleared - 1;
868 if (nd_btt || nd_pfn || nd_dax) {
874 ndns = nd_dax->nd_pfn.ndns;
881 nsio = to_nd_namespace_io(&ndns->dev);
882 pstart = nsio->res.start + offset;
883 pend = nsio->res.end - end_trunc;
885 if ((pstart >= start) && (pend <= end))
892 static int nd_ns_forget_poison_check(struct device *dev, void *data)
894 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
897 /* set_config requires an idle interleave set */
898 static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
899 struct nvdimm *nvdimm, unsigned int cmd, void *data)
901 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
903 /* ask the bus provider if it would like to block this request */
904 if (nd_desc->clear_to_send) {
905 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
911 /* require clear error to go through the pmem driver */
912 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
913 return device_for_each_child(&nvdimm_bus->dev, data,
914 nd_ns_forget_poison_check);
916 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
919 /* prevent label manipulation while the kernel owns label updates */
920 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
921 if (atomic_read(&nvdimm->busy))
926 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
927 int read_only, unsigned int ioctl_cmd, unsigned long arg)
929 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
930 static char out_env[ND_CMD_MAX_ENVELOPE];
931 static char in_env[ND_CMD_MAX_ENVELOPE];
932 const struct nd_cmd_desc *desc = NULL;
933 unsigned int cmd = _IOC_NR(ioctl_cmd);
934 struct device *dev = &nvdimm_bus->dev;
935 void __user *p = (void __user *) arg;
936 const char *cmd_name, *dimm_name;
937 u32 in_len = 0, out_len = 0;
938 unsigned int func = cmd;
939 unsigned long cmd_mask;
940 struct nd_cmd_pkg pkg;
946 desc = nd_cmd_dimm_desc(cmd);
947 cmd_name = nvdimm_cmd_name(cmd);
948 cmd_mask = nvdimm->cmd_mask;
949 dimm_name = dev_name(&nvdimm->dev);
951 desc = nd_cmd_bus_desc(cmd);
952 cmd_name = nvdimm_bus_cmd_name(cmd);
953 cmd_mask = nd_desc->cmd_mask;
957 if (cmd == ND_CMD_CALL) {
958 if (copy_from_user(&pkg, p, sizeof(pkg)))
962 if (!desc || (desc->out_num + desc->in_num == 0) ||
963 !test_bit(cmd, &cmd_mask))
966 /* fail write commands (when read-only) */
970 case ND_CMD_SET_CONFIG_DATA:
971 case ND_CMD_ARS_START:
972 case ND_CMD_CLEAR_ERROR:
974 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
975 nvdimm ? nvdimm_cmd_name(cmd)
976 : nvdimm_bus_cmd_name(cmd));
982 /* process an input envelope */
983 for (i = 0; i < desc->in_num; i++) {
986 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
987 if (in_size == UINT_MAX) {
988 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
989 __func__, dimm_name, cmd_name, i);
992 if (in_len < sizeof(in_env))
993 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
996 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
1001 if (cmd == ND_CMD_CALL) {
1002 func = pkg.nd_command;
1003 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1004 dimm_name, pkg.nd_command,
1005 in_len, out_len, buf_len);
1008 /* process an output envelope */
1009 for (i = 0; i < desc->out_num; i++) {
1010 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
1011 (u32 *) in_env, (u32 *) out_env, 0);
1014 if (out_size == UINT_MAX) {
1015 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1016 dimm_name, cmd_name, i);
1019 if (out_len < sizeof(out_env))
1020 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1023 if (copy && copy_from_user(&out_env[out_len],
1024 p + in_len + out_len, copy))
1026 out_len += out_size;
1029 buf_len = (u64) out_len + (u64) in_len;
1030 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
1031 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1032 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
1036 buf = vmalloc(buf_len);
1040 if (copy_from_user(buf, p, buf_len)) {
1045 nvdimm_bus_lock(&nvdimm_bus->dev);
1046 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
1050 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
1054 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1055 struct nd_cmd_clear_error *clear_err = buf;
1057 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1058 clear_err->cleared);
1060 nvdimm_bus_unlock(&nvdimm_bus->dev);
1062 if (copy_to_user(p, buf, buf_len))
1069 nvdimm_bus_unlock(&nvdimm_bus->dev);
1075 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1077 long id = (long) file->private_data;
1078 int rc = -ENXIO, ro;
1079 struct nvdimm_bus *nvdimm_bus;
1081 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1082 mutex_lock(&nvdimm_bus_list_mutex);
1083 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1084 if (nvdimm_bus->id == id) {
1085 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
1089 mutex_unlock(&nvdimm_bus_list_mutex);
1094 static int match_dimm(struct device *dev, void *data)
1096 long id = (long) data;
1098 if (is_nvdimm(dev)) {
1099 struct nvdimm *nvdimm = to_nvdimm(dev);
1101 return nvdimm->id == id;
1107 static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1109 int rc = -ENXIO, ro;
1110 struct nvdimm_bus *nvdimm_bus;
1112 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1113 mutex_lock(&nvdimm_bus_list_mutex);
1114 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1115 struct device *dev = device_find_child(&nvdimm_bus->dev,
1116 file->private_data, match_dimm);
1117 struct nvdimm *nvdimm;
1122 nvdimm = to_nvdimm(dev);
1123 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1127 mutex_unlock(&nvdimm_bus_list_mutex);
1132 static int nd_open(struct inode *inode, struct file *file)
1134 long minor = iminor(inode);
1136 file->private_data = (void *) minor;
1140 static const struct file_operations nvdimm_bus_fops = {
1141 .owner = THIS_MODULE,
1143 .unlocked_ioctl = nd_ioctl,
1144 .compat_ioctl = nd_ioctl,
1145 .llseek = noop_llseek,
1148 static const struct file_operations nvdimm_fops = {
1149 .owner = THIS_MODULE,
1151 .unlocked_ioctl = nvdimm_ioctl,
1152 .compat_ioctl = nvdimm_ioctl,
1153 .llseek = noop_llseek,
1156 int __init nvdimm_bus_init(void)
1160 rc = bus_register(&nvdimm_bus_type);
1164 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1166 goto err_bus_chrdev;
1167 nvdimm_bus_major = rc;
1169 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1171 goto err_dimm_chrdev;
1174 nd_class = class_create(THIS_MODULE, "nd");
1175 if (IS_ERR(nd_class)) {
1176 rc = PTR_ERR(nd_class);
1180 rc = driver_register(&nd_bus_driver.drv);
1187 class_destroy(nd_class);
1189 unregister_chrdev(nvdimm_major, "dimmctl");
1191 unregister_chrdev(nvdimm_bus_major, "ndctl");
1193 bus_unregister(&nvdimm_bus_type);
1198 void nvdimm_bus_exit(void)
1200 driver_unregister(&nd_bus_driver.drv);
1201 class_destroy(nd_class);
1202 unregister_chrdev(nvdimm_bus_major, "ndctl");
1203 unregister_chrdev(nvdimm_major, "dimmctl");
1204 bus_unregister(&nvdimm_bus_type);
1205 ida_destroy(&nd_ida);