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/sched/mm.h>
15 #include <linux/vmalloc.h>
16 #include <linux/uaccess.h>
17 #include <linux/module.h>
18 #include <linux/blkdev.h>
19 #include <linux/fcntl.h>
20 #include <linux/async.h>
21 #include <linux/genhd.h>
22 #include <linux/ndctl.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
34 static int nvdimm_bus_major;
35 static struct class *nd_class;
36 static DEFINE_IDA(nd_ida);
38 static int to_nd_device_type(struct device *dev)
41 return ND_DEVICE_DIMM;
42 else if (is_memory(dev))
43 return ND_DEVICE_REGION_PMEM;
44 else if (is_nd_blk(dev))
45 return ND_DEVICE_REGION_BLK;
46 else if (is_nd_dax(dev))
47 return ND_DEVICE_DAX_PMEM;
48 else if (is_nd_region(dev->parent))
49 return nd_region_to_nstype(to_nd_region(dev->parent));
54 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
57 * Ensure that region devices always have their numa node set as
60 if (is_nd_region(dev))
61 set_dev_node(dev, to_nd_region(dev)->numa_node);
62 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
63 to_nd_device_type(dev));
66 static struct module *to_bus_provider(struct device *dev)
68 /* pin bus providers while regions are enabled */
69 if (is_nd_region(dev)) {
70 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
72 return nvdimm_bus->nd_desc->module;
77 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
79 nvdimm_bus_lock(&nvdimm_bus->dev);
80 nvdimm_bus->probe_active++;
81 nvdimm_bus_unlock(&nvdimm_bus->dev);
84 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
86 nvdimm_bus_lock(&nvdimm_bus->dev);
87 if (--nvdimm_bus->probe_active == 0)
88 wake_up(&nvdimm_bus->probe_wait);
89 nvdimm_bus_unlock(&nvdimm_bus->dev);
92 static int nvdimm_bus_probe(struct device *dev)
94 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
95 struct module *provider = to_bus_provider(dev);
96 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
99 if (!try_module_get(provider))
102 nvdimm_bus_probe_start(nvdimm_bus);
103 rc = nd_drv->probe(dev);
105 nd_region_probe_success(nvdimm_bus, dev);
107 nd_region_disable(nvdimm_bus, dev);
108 nvdimm_bus_probe_end(nvdimm_bus);
110 dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
114 module_put(provider);
118 static int nvdimm_bus_remove(struct device *dev)
120 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
121 struct module *provider = to_bus_provider(dev);
122 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
126 rc = nd_drv->remove(dev);
127 nd_region_disable(nvdimm_bus, dev);
129 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
131 module_put(provider);
135 static void nvdimm_bus_shutdown(struct device *dev)
137 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
138 struct nd_device_driver *nd_drv = NULL;
141 nd_drv = to_nd_device_driver(dev->driver);
143 if (nd_drv && nd_drv->shutdown) {
144 nd_drv->shutdown(dev);
145 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
146 dev->driver->name, dev_name(dev));
150 void nd_device_notify(struct device *dev, enum nvdimm_event event)
154 struct nd_device_driver *nd_drv;
156 nd_drv = to_nd_device_driver(dev->driver);
158 nd_drv->notify(dev, event);
162 EXPORT_SYMBOL(nd_device_notify);
164 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
166 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
171 /* caller is responsible for holding a reference on the device */
172 nd_device_notify(&nd_region->dev, event);
174 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
176 struct clear_badblocks_context {
177 resource_size_t phys, cleared;
180 static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
182 struct clear_badblocks_context *ctx = data;
183 struct nd_region *nd_region;
184 resource_size_t ndr_end;
187 /* make sure device is a region */
188 if (!is_nd_pmem(dev))
191 nd_region = to_nd_region(dev);
192 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
194 /* make sure we are in the region */
195 if (ctx->phys < nd_region->ndr_start
196 || (ctx->phys + ctx->cleared) > ndr_end)
199 sector = (ctx->phys - nd_region->ndr_start) / 512;
200 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
202 if (nd_region->bb_state)
203 sysfs_notify_dirent(nd_region->bb_state);
208 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
209 phys_addr_t phys, u64 cleared)
211 struct clear_badblocks_context ctx = {
216 device_for_each_child(&nvdimm_bus->dev, &ctx,
217 nvdimm_clear_badblocks_region);
220 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
221 phys_addr_t phys, u64 cleared)
224 nvdimm_forget_poison(nvdimm_bus, phys, cleared);
226 if (cleared > 0 && cleared / 512)
227 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
230 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
233 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
234 struct nvdimm_bus_descriptor *nd_desc;
235 struct nd_cmd_clear_error clear_err;
236 struct nd_cmd_ars_cap ars_cap;
237 u32 clear_err_unit, mask;
238 unsigned int noio_flag;
244 nd_desc = nvdimm_bus->nd_desc;
246 * if ndctl does not exist, it's PMEM_LEGACY and
247 * we want to just pretend everything is handled.
252 memset(&ars_cap, 0, sizeof(ars_cap));
253 ars_cap.address = phys;
254 ars_cap.length = len;
255 noio_flag = memalloc_noio_save();
256 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
257 sizeof(ars_cap), &cmd_rc);
258 memalloc_noio_restore(noio_flag);
263 clear_err_unit = ars_cap.clear_err_unit;
264 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
267 mask = clear_err_unit - 1;
268 if ((phys | len) & mask)
270 memset(&clear_err, 0, sizeof(clear_err));
271 clear_err.address = phys;
272 clear_err.length = len;
273 noio_flag = memalloc_noio_save();
274 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
275 sizeof(clear_err), &cmd_rc);
276 memalloc_noio_restore(noio_flag);
282 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
284 return clear_err.cleared;
286 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
288 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
290 static struct bus_type nvdimm_bus_type = {
292 .uevent = nvdimm_bus_uevent,
293 .match = nvdimm_bus_match,
294 .probe = nvdimm_bus_probe,
295 .remove = nvdimm_bus_remove,
296 .shutdown = nvdimm_bus_shutdown,
299 static void nvdimm_bus_release(struct device *dev)
301 struct nvdimm_bus *nvdimm_bus;
303 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
304 ida_simple_remove(&nd_ida, nvdimm_bus->id);
308 static bool is_nvdimm_bus(struct device *dev)
310 return dev->release == nvdimm_bus_release;
313 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
317 for (dev = nd_dev; dev; dev = dev->parent)
318 if (is_nvdimm_bus(dev))
320 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
322 return to_nvdimm_bus(dev);
326 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
328 struct nvdimm_bus *nvdimm_bus;
330 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
331 WARN_ON(!is_nvdimm_bus(dev));
334 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
336 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
337 struct nvdimm_bus_descriptor *nd_desc)
339 struct nvdimm_bus *nvdimm_bus;
342 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
345 INIT_LIST_HEAD(&nvdimm_bus->list);
346 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
347 INIT_LIST_HEAD(&nvdimm_bus->poison_list);
348 init_waitqueue_head(&nvdimm_bus->probe_wait);
349 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
350 mutex_init(&nvdimm_bus->reconfig_mutex);
351 spin_lock_init(&nvdimm_bus->poison_lock);
352 if (nvdimm_bus->id < 0) {
356 nvdimm_bus->nd_desc = nd_desc;
357 nvdimm_bus->dev.parent = parent;
358 nvdimm_bus->dev.release = nvdimm_bus_release;
359 nvdimm_bus->dev.groups = nd_desc->attr_groups;
360 nvdimm_bus->dev.bus = &nvdimm_bus_type;
361 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
362 rc = device_register(&nvdimm_bus->dev);
364 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
370 put_device(&nvdimm_bus->dev);
373 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
375 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
379 device_unregister(&nvdimm_bus->dev);
381 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
383 static int child_unregister(struct device *dev, void *data)
386 * the singular ndctl class device per bus needs to be
387 * "device_destroy"ed, so skip it here
389 * i.e. remove classless children
394 nd_device_unregister(dev, ND_SYNC);
398 static void free_poison_list(struct list_head *poison_list)
400 struct nd_poison *pl, *next;
402 list_for_each_entry_safe(pl, next, poison_list, list) {
406 list_del_init(poison_list);
409 static int nd_bus_remove(struct device *dev)
411 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
413 mutex_lock(&nvdimm_bus_list_mutex);
414 list_del_init(&nvdimm_bus->list);
415 mutex_unlock(&nvdimm_bus_list_mutex);
418 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
420 spin_lock(&nvdimm_bus->poison_lock);
421 free_poison_list(&nvdimm_bus->poison_list);
422 spin_unlock(&nvdimm_bus->poison_lock);
424 nvdimm_bus_destroy_ndctl(nvdimm_bus);
429 static int nd_bus_probe(struct device *dev)
431 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
434 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
438 mutex_lock(&nvdimm_bus_list_mutex);
439 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
440 mutex_unlock(&nvdimm_bus_list_mutex);
442 /* enable bus provider attributes to look up their local context */
443 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
448 static struct nd_device_driver nd_bus_driver = {
449 .probe = nd_bus_probe,
450 .remove = nd_bus_remove,
453 .suppress_bind_attrs = true,
454 .bus = &nvdimm_bus_type,
455 .owner = THIS_MODULE,
456 .mod_name = KBUILD_MODNAME,
460 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
462 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
464 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
467 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
470 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
472 void nd_synchronize(void)
474 async_synchronize_full_domain(&nd_async_domain);
476 EXPORT_SYMBOL_GPL(nd_synchronize);
478 static void nd_async_device_register(void *d, async_cookie_t cookie)
480 struct device *dev = d;
482 if (device_add(dev) != 0) {
483 dev_err(dev, "%s: failed\n", __func__);
488 put_device(dev->parent);
491 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
493 struct device *dev = d;
495 /* flush bus operations before delete */
496 nvdimm_bus_lock(dev);
497 nvdimm_bus_unlock(dev);
499 device_unregister(dev);
503 void __nd_device_register(struct device *dev)
507 dev->bus = &nvdimm_bus_type;
509 get_device(dev->parent);
511 async_schedule_domain(nd_async_device_register, dev,
515 void nd_device_register(struct device *dev)
517 device_initialize(dev);
518 __nd_device_register(dev);
520 EXPORT_SYMBOL(nd_device_register);
522 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
527 async_schedule_domain(nd_async_device_unregister, dev,
532 device_unregister(dev);
536 EXPORT_SYMBOL(nd_device_unregister);
539 * __nd_driver_register() - register a region or a namespace driver
540 * @nd_drv: driver to register
541 * @owner: automatically set by nd_driver_register() macro
542 * @mod_name: automatically set by nd_driver_register() macro
544 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
545 const char *mod_name)
547 struct device_driver *drv = &nd_drv->drv;
550 pr_debug("driver type bitmask not set (%pf)\n",
551 __builtin_return_address(0));
555 if (!nd_drv->probe) {
556 pr_debug("%s ->probe() must be specified\n", mod_name);
560 drv->bus = &nvdimm_bus_type;
562 drv->mod_name = mod_name;
564 return driver_register(drv);
566 EXPORT_SYMBOL(__nd_driver_register);
568 int nvdimm_revalidate_disk(struct gendisk *disk)
570 struct device *dev = disk_to_dev(disk)->parent;
571 struct nd_region *nd_region = to_nd_region(dev->parent);
572 int disk_ro = get_disk_ro(disk);
575 * Upgrade to read-only if the region is read-only preserve as
576 * read-only if the disk is already read-only.
578 if (disk_ro || nd_region->ro == disk_ro)
581 dev_info(dev, "%s read-only, marking %s read-only\n",
582 dev_name(&nd_region->dev), disk->disk_name);
583 set_disk_ro(disk, 1);
588 EXPORT_SYMBOL(nvdimm_revalidate_disk);
590 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
593 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
594 to_nd_device_type(dev));
596 static DEVICE_ATTR_RO(modalias);
598 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
601 return sprintf(buf, "%s\n", dev->type->name);
603 static DEVICE_ATTR_RO(devtype);
605 static struct attribute *nd_device_attributes[] = {
606 &dev_attr_modalias.attr,
607 &dev_attr_devtype.attr,
612 * nd_device_attribute_group - generic attributes for all devices on an nd bus
614 struct attribute_group nd_device_attribute_group = {
615 .attrs = nd_device_attributes,
617 EXPORT_SYMBOL_GPL(nd_device_attribute_group);
619 static ssize_t numa_node_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
622 return sprintf(buf, "%d\n", dev_to_node(dev));
624 static DEVICE_ATTR_RO(numa_node);
626 static struct attribute *nd_numa_attributes[] = {
627 &dev_attr_numa_node.attr,
631 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
634 if (!IS_ENABLED(CONFIG_NUMA))
641 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
643 struct attribute_group nd_numa_attribute_group = {
644 .attrs = nd_numa_attributes,
645 .is_visible = nd_numa_attr_visible,
647 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
649 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
651 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
654 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
655 "ndctl%d", nvdimm_bus->id);
658 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
659 nvdimm_bus->id, PTR_ERR(dev));
660 return PTR_ERR_OR_ZERO(dev);
663 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
665 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
668 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
669 [ND_CMD_IMPLEMENTED] = { },
672 .out_sizes = { 4, 128, },
674 [ND_CMD_SMART_THRESHOLD] = {
676 .out_sizes = { 4, 8, },
678 [ND_CMD_DIMM_FLAGS] = {
680 .out_sizes = { 4, 4 },
682 [ND_CMD_GET_CONFIG_SIZE] = {
684 .out_sizes = { 4, 4, 4, },
686 [ND_CMD_GET_CONFIG_DATA] = {
688 .in_sizes = { 4, 4, },
690 .out_sizes = { 4, UINT_MAX, },
692 [ND_CMD_SET_CONFIG_DATA] = {
694 .in_sizes = { 4, 4, UINT_MAX, },
700 .in_sizes = { 4, 4, UINT_MAX, },
702 .out_sizes = { 4, 4, UINT_MAX, },
706 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
708 .out_sizes = { UINT_MAX, },
712 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
714 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
715 return &__nd_cmd_dimm_descs[cmd];
718 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
720 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
721 [ND_CMD_IMPLEMENTED] = { },
724 .in_sizes = { 8, 8, },
726 .out_sizes = { 4, 4, 4, 4, },
728 [ND_CMD_ARS_START] = {
730 .in_sizes = { 8, 8, 2, 1, 5, },
732 .out_sizes = { 4, 4, },
734 [ND_CMD_ARS_STATUS] = {
736 .out_sizes = { 4, 4, UINT_MAX, },
738 [ND_CMD_CLEAR_ERROR] = {
740 .in_sizes = { 8, 8, },
742 .out_sizes = { 4, 4, 8, },
746 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
748 .out_sizes = { UINT_MAX, },
752 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
754 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
755 return &__nd_cmd_bus_descs[cmd];
758 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
760 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
761 const struct nd_cmd_desc *desc, int idx, void *buf)
763 if (idx >= desc->in_num)
766 if (desc->in_sizes[idx] < UINT_MAX)
767 return desc->in_sizes[idx];
769 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
770 struct nd_cmd_set_config_hdr *hdr = buf;
772 return hdr->in_length;
773 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
774 struct nd_cmd_vendor_hdr *hdr = buf;
776 return hdr->in_length;
777 } else if (cmd == ND_CMD_CALL) {
778 struct nd_cmd_pkg *pkg = buf;
780 return pkg->nd_size_in;
785 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
787 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
788 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
789 const u32 *out_field, unsigned long remainder)
791 if (idx >= desc->out_num)
794 if (desc->out_sizes[idx] < UINT_MAX)
795 return desc->out_sizes[idx];
797 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
799 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
801 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
803 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
804 * "Size of Output Buffer in bytes, including this
807 if (out_field[1] < 4)
810 * ACPI 6.1 is ambiguous if 'status' is included in the
811 * output size. If we encounter an output size that
812 * overshoots the remainder by 4 bytes, assume it was
813 * including 'status'.
815 if (out_field[1] - 4 == remainder)
817 return out_field[1] - 8;
818 } else if (cmd == ND_CMD_CALL) {
819 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
821 return pkg->nd_size_out;
827 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
829 void wait_nvdimm_bus_probe_idle(struct device *dev)
831 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
834 if (nvdimm_bus->probe_active == 0)
836 nvdimm_bus_unlock(&nvdimm_bus->dev);
837 wait_event(nvdimm_bus->probe_wait,
838 nvdimm_bus->probe_active == 0);
839 nvdimm_bus_lock(&nvdimm_bus->dev);
843 static int nd_pmem_forget_poison_check(struct device *dev, void *data)
845 struct nd_cmd_clear_error *clear_err =
846 (struct nd_cmd_clear_error *)data;
847 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
848 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
849 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
850 struct nd_namespace_common *ndns = NULL;
851 struct nd_namespace_io *nsio;
852 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
854 if (nd_dax || !dev->driver)
857 start = clear_err->address;
858 end = clear_err->address + clear_err->cleared - 1;
860 if (nd_btt || nd_pfn || nd_dax) {
866 ndns = nd_dax->nd_pfn.ndns;
873 nsio = to_nd_namespace_io(&ndns->dev);
874 pstart = nsio->res.start + offset;
875 pend = nsio->res.end - end_trunc;
877 if ((pstart >= start) && (pend <= end))
884 static int nd_ns_forget_poison_check(struct device *dev, void *data)
886 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
889 /* set_config requires an idle interleave set */
890 static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
891 struct nvdimm *nvdimm, unsigned int cmd, void *data)
893 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
895 /* ask the bus provider if it would like to block this request */
896 if (nd_desc->clear_to_send) {
897 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
903 /* require clear error to go through the pmem driver */
904 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
905 return device_for_each_child(&nvdimm_bus->dev, data,
906 nd_ns_forget_poison_check);
908 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
911 /* prevent label manipulation while the kernel owns label updates */
912 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
913 if (atomic_read(&nvdimm->busy))
918 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
919 int read_only, unsigned int ioctl_cmd, unsigned long arg)
921 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
922 static char out_env[ND_CMD_MAX_ENVELOPE];
923 static char in_env[ND_CMD_MAX_ENVELOPE];
924 const struct nd_cmd_desc *desc = NULL;
925 unsigned int cmd = _IOC_NR(ioctl_cmd);
926 struct device *dev = &nvdimm_bus->dev;
927 void __user *p = (void __user *) arg;
928 const char *cmd_name, *dimm_name;
929 u32 in_len = 0, out_len = 0;
930 unsigned int func = cmd;
931 unsigned long cmd_mask;
932 struct nd_cmd_pkg pkg;
938 desc = nd_cmd_dimm_desc(cmd);
939 cmd_name = nvdimm_cmd_name(cmd);
940 cmd_mask = nvdimm->cmd_mask;
941 dimm_name = dev_name(&nvdimm->dev);
943 desc = nd_cmd_bus_desc(cmd);
944 cmd_name = nvdimm_bus_cmd_name(cmd);
945 cmd_mask = nd_desc->cmd_mask;
949 if (cmd == ND_CMD_CALL) {
950 if (copy_from_user(&pkg, p, sizeof(pkg)))
955 (desc->out_num + desc->in_num == 0) ||
957 !test_bit(cmd, &cmd_mask))
960 /* fail write commands (when read-only) */
964 case ND_CMD_SET_CONFIG_DATA:
965 case ND_CMD_ARS_START:
966 case ND_CMD_CLEAR_ERROR:
968 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
969 nvdimm ? nvdimm_cmd_name(cmd)
970 : nvdimm_bus_cmd_name(cmd));
976 /* process an input envelope */
977 for (i = 0; i < desc->in_num; i++) {
980 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
981 if (in_size == UINT_MAX) {
982 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
983 __func__, dimm_name, cmd_name, i);
986 if (in_len < sizeof(in_env))
987 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
990 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
995 if (cmd == ND_CMD_CALL) {
996 func = pkg.nd_command;
997 dev_dbg(dev, "%s:%s, idx: %llu, in: %u, out: %u, len %llu\n",
998 __func__, dimm_name, pkg.nd_command,
999 in_len, out_len, buf_len);
1002 /* process an output envelope */
1003 for (i = 0; i < desc->out_num; i++) {
1004 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
1005 (u32 *) in_env, (u32 *) out_env, 0);
1008 if (out_size == UINT_MAX) {
1009 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
1010 __func__, dimm_name, cmd_name, i);
1013 if (out_len < sizeof(out_env))
1014 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1017 if (copy && copy_from_user(&out_env[out_len],
1018 p + in_len + out_len, copy))
1020 out_len += out_size;
1023 buf_len = (u64) out_len + (u64) in_len;
1024 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
1025 dev_dbg(dev, "%s:%s cmd: %s buf_len: %llu > %d\n", __func__,
1026 dimm_name, cmd_name, buf_len,
1027 ND_IOCTL_MAX_BUFLEN);
1031 buf = vmalloc(buf_len);
1035 if (copy_from_user(buf, p, buf_len)) {
1040 nvdimm_bus_lock(&nvdimm_bus->dev);
1041 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
1045 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
1049 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1050 struct nd_cmd_clear_error *clear_err = buf;
1052 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1053 clear_err->cleared);
1055 nvdimm_bus_unlock(&nvdimm_bus->dev);
1057 if (copy_to_user(p, buf, buf_len))
1064 nvdimm_bus_unlock(&nvdimm_bus->dev);
1070 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1072 long id = (long) file->private_data;
1073 int rc = -ENXIO, ro;
1074 struct nvdimm_bus *nvdimm_bus;
1076 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1077 mutex_lock(&nvdimm_bus_list_mutex);
1078 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1079 if (nvdimm_bus->id == id) {
1080 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
1084 mutex_unlock(&nvdimm_bus_list_mutex);
1089 static int match_dimm(struct device *dev, void *data)
1091 long id = (long) data;
1093 if (is_nvdimm(dev)) {
1094 struct nvdimm *nvdimm = to_nvdimm(dev);
1096 return nvdimm->id == id;
1102 static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1104 int rc = -ENXIO, ro;
1105 struct nvdimm_bus *nvdimm_bus;
1107 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1108 mutex_lock(&nvdimm_bus_list_mutex);
1109 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1110 struct device *dev = device_find_child(&nvdimm_bus->dev,
1111 file->private_data, match_dimm);
1112 struct nvdimm *nvdimm;
1117 nvdimm = to_nvdimm(dev);
1118 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1122 mutex_unlock(&nvdimm_bus_list_mutex);
1127 static int nd_open(struct inode *inode, struct file *file)
1129 long minor = iminor(inode);
1131 file->private_data = (void *) minor;
1135 static const struct file_operations nvdimm_bus_fops = {
1136 .owner = THIS_MODULE,
1138 .unlocked_ioctl = nd_ioctl,
1139 .compat_ioctl = nd_ioctl,
1140 .llseek = noop_llseek,
1143 static const struct file_operations nvdimm_fops = {
1144 .owner = THIS_MODULE,
1146 .unlocked_ioctl = nvdimm_ioctl,
1147 .compat_ioctl = nvdimm_ioctl,
1148 .llseek = noop_llseek,
1151 int __init nvdimm_bus_init(void)
1155 BUILD_BUG_ON(sizeof(struct nd_smart_payload) != 128);
1156 BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload) != 8);
1158 rc = bus_register(&nvdimm_bus_type);
1162 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1164 goto err_bus_chrdev;
1165 nvdimm_bus_major = rc;
1167 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1169 goto err_dimm_chrdev;
1172 nd_class = class_create(THIS_MODULE, "nd");
1173 if (IS_ERR(nd_class)) {
1174 rc = PTR_ERR(nd_class);
1178 rc = driver_register(&nd_bus_driver.drv);
1185 class_destroy(nd_class);
1187 unregister_chrdev(nvdimm_major, "dimmctl");
1189 unregister_chrdev(nvdimm_bus_major, "ndctl");
1191 bus_unregister(&nvdimm_bus_type);
1196 void nvdimm_bus_exit(void)
1198 driver_unregister(&nd_bus_driver.drv);
1199 class_destroy(nd_class);
1200 unregister_chrdev(nvdimm_bus_major, "ndctl");
1201 unregister_chrdev(nvdimm_major, "dimmctl");
1202 bus_unregister(&nvdimm_bus_type);
1203 ida_destroy(&nd_ida);