perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / drivers / nvdimm / bus.c
blob2eb6a6cfe9e4cf61c014a1b96c17626120dbb84d
1 /*
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>
26 #include <linux/cpu.h>
27 #include <linux/fs.h>
28 #include <linux/io.h>
29 #include <linux/mm.h>
30 #include <linux/nd.h>
31 #include "nd-core.h"
32 #include "nd.h"
33 #include "pfn.h"
35 int nvdimm_major;
36 static int nvdimm_bus_major;
37 static struct class *nd_class;
38 static DEFINE_IDA(nd_ida);
40 static int to_nd_device_type(struct device *dev)
42 if (is_nvdimm(dev))
43 return ND_DEVICE_DIMM;
44 else if (is_memory(dev))
45 return ND_DEVICE_REGION_PMEM;
46 else if (is_nd_blk(dev))
47 return ND_DEVICE_REGION_BLK;
48 else if (is_nd_dax(dev))
49 return ND_DEVICE_DAX_PMEM;
50 else if (is_nd_region(dev->parent))
51 return nd_region_to_nstype(to_nd_region(dev->parent));
53 return 0;
56 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
58 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
59 to_nd_device_type(dev));
62 static struct module *to_bus_provider(struct device *dev)
64 /* pin bus providers while regions are enabled */
65 if (is_nd_region(dev)) {
66 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
68 return nvdimm_bus->nd_desc->module;
70 return NULL;
73 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
75 nvdimm_bus_lock(&nvdimm_bus->dev);
76 nvdimm_bus->probe_active++;
77 nvdimm_bus_unlock(&nvdimm_bus->dev);
80 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
82 nvdimm_bus_lock(&nvdimm_bus->dev);
83 if (--nvdimm_bus->probe_active == 0)
84 wake_up(&nvdimm_bus->probe_wait);
85 nvdimm_bus_unlock(&nvdimm_bus->dev);
88 static int nvdimm_bus_probe(struct device *dev)
90 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
91 struct module *provider = to_bus_provider(dev);
92 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
93 int rc;
95 if (!try_module_get(provider))
96 return -ENXIO;
98 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
99 dev->driver->name, dev_name(dev));
101 nvdimm_bus_probe_start(nvdimm_bus);
102 rc = nd_drv->probe(dev);
103 if (rc == 0)
104 nd_region_probe_success(nvdimm_bus, dev);
105 else
106 nd_region_disable(nvdimm_bus, dev);
107 nvdimm_bus_probe_end(nvdimm_bus);
109 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
110 dev_name(dev), rc);
112 if (rc != 0)
113 module_put(provider);
114 return rc;
117 static int nvdimm_bus_remove(struct device *dev)
119 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
120 struct module *provider = to_bus_provider(dev);
121 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
122 int rc = 0;
124 if (nd_drv->remove)
125 rc = nd_drv->remove(dev);
126 nd_region_disable(nvdimm_bus, dev);
128 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
129 dev_name(dev), rc);
130 module_put(provider);
131 return rc;
134 static void nvdimm_bus_shutdown(struct device *dev)
136 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
137 struct nd_device_driver *nd_drv = NULL;
139 if (dev->driver)
140 nd_drv = to_nd_device_driver(dev->driver);
142 if (nd_drv && nd_drv->shutdown) {
143 nd_drv->shutdown(dev);
144 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
145 dev->driver->name, dev_name(dev));
149 void nd_device_notify(struct device *dev, enum nvdimm_event event)
151 device_lock(dev);
152 if (dev->driver) {
153 struct nd_device_driver *nd_drv;
155 nd_drv = to_nd_device_driver(dev->driver);
156 if (nd_drv->notify)
157 nd_drv->notify(dev, event);
159 device_unlock(dev);
161 EXPORT_SYMBOL(nd_device_notify);
163 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
165 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
167 if (!nvdimm_bus)
168 return;
170 /* caller is responsible for holding a reference on the device */
171 nd_device_notify(&nd_region->dev, event);
173 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
175 struct clear_badblocks_context {
176 resource_size_t phys, cleared;
179 static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
181 struct clear_badblocks_context *ctx = data;
182 struct nd_region *nd_region;
183 resource_size_t ndr_end;
184 sector_t sector;
186 /* make sure device is a region */
187 if (!is_nd_pmem(dev))
188 return 0;
190 nd_region = to_nd_region(dev);
191 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
193 /* make sure we are in the region */
194 if (ctx->phys < nd_region->ndr_start
195 || (ctx->phys + ctx->cleared) > ndr_end)
196 return 0;
198 sector = (ctx->phys - nd_region->ndr_start) / 512;
199 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
201 if (nd_region->bb_state)
202 sysfs_notify_dirent(nd_region->bb_state);
204 return 0;
207 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
208 phys_addr_t phys, u64 cleared)
210 struct clear_badblocks_context ctx = {
211 .phys = phys,
212 .cleared = cleared,
215 device_for_each_child(&nvdimm_bus->dev, &ctx,
216 nvdimm_clear_badblocks_region);
219 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
220 phys_addr_t phys, u64 cleared)
222 if (cleared > 0)
223 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
225 if (cleared > 0 && cleared / 512)
226 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
229 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
230 unsigned int len)
232 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
233 struct nvdimm_bus_descriptor *nd_desc;
234 struct nd_cmd_clear_error clear_err;
235 struct nd_cmd_ars_cap ars_cap;
236 u32 clear_err_unit, mask;
237 unsigned int noio_flag;
238 int cmd_rc, rc;
240 if (!nvdimm_bus)
241 return -ENXIO;
243 nd_desc = nvdimm_bus->nd_desc;
245 * if ndctl does not exist, it's PMEM_LEGACY and
246 * we want to just pretend everything is handled.
248 if (!nd_desc->ndctl)
249 return len;
251 memset(&ars_cap, 0, sizeof(ars_cap));
252 ars_cap.address = phys;
253 ars_cap.length = len;
254 noio_flag = memalloc_noio_save();
255 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
256 sizeof(ars_cap), &cmd_rc);
257 memalloc_noio_restore(noio_flag);
258 if (rc < 0)
259 return rc;
260 if (cmd_rc < 0)
261 return cmd_rc;
262 clear_err_unit = ars_cap.clear_err_unit;
263 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
264 return -ENXIO;
266 mask = clear_err_unit - 1;
267 if ((phys | len) & mask)
268 return -ENXIO;
269 memset(&clear_err, 0, sizeof(clear_err));
270 clear_err.address = phys;
271 clear_err.length = len;
272 noio_flag = memalloc_noio_save();
273 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
274 sizeof(clear_err), &cmd_rc);
275 memalloc_noio_restore(noio_flag);
276 if (rc < 0)
277 return rc;
278 if (cmd_rc < 0)
279 return cmd_rc;
281 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
283 return clear_err.cleared;
285 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
287 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
289 static struct bus_type nvdimm_bus_type = {
290 .name = "nd",
291 .uevent = nvdimm_bus_uevent,
292 .match = nvdimm_bus_match,
293 .probe = nvdimm_bus_probe,
294 .remove = nvdimm_bus_remove,
295 .shutdown = nvdimm_bus_shutdown,
298 static void nvdimm_bus_release(struct device *dev)
300 struct nvdimm_bus *nvdimm_bus;
302 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
303 ida_simple_remove(&nd_ida, nvdimm_bus->id);
304 kfree(nvdimm_bus);
307 static bool is_nvdimm_bus(struct device *dev)
309 return dev->release == nvdimm_bus_release;
312 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
314 struct device *dev;
316 for (dev = nd_dev; dev; dev = dev->parent)
317 if (is_nvdimm_bus(dev))
318 break;
319 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
320 if (dev)
321 return to_nvdimm_bus(dev);
322 return NULL;
325 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
327 struct nvdimm_bus *nvdimm_bus;
329 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
330 WARN_ON(!is_nvdimm_bus(dev));
331 return nvdimm_bus;
333 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
335 struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm)
337 return to_nvdimm_bus(nvdimm->dev.parent);
339 EXPORT_SYMBOL_GPL(nvdimm_to_bus);
341 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
342 struct nvdimm_bus_descriptor *nd_desc)
344 struct nvdimm_bus *nvdimm_bus;
345 int rc;
347 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
348 if (!nvdimm_bus)
349 return NULL;
350 INIT_LIST_HEAD(&nvdimm_bus->list);
351 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
352 init_waitqueue_head(&nvdimm_bus->probe_wait);
353 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
354 if (nvdimm_bus->id < 0) {
355 kfree(nvdimm_bus);
356 return NULL;
358 mutex_init(&nvdimm_bus->reconfig_mutex);
359 badrange_init(&nvdimm_bus->badrange);
360 nvdimm_bus->nd_desc = nd_desc;
361 nvdimm_bus->dev.parent = parent;
362 nvdimm_bus->dev.release = nvdimm_bus_release;
363 nvdimm_bus->dev.groups = nd_desc->attr_groups;
364 nvdimm_bus->dev.bus = &nvdimm_bus_type;
365 nvdimm_bus->dev.of_node = nd_desc->of_node;
366 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
367 rc = device_register(&nvdimm_bus->dev);
368 if (rc) {
369 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
370 goto err;
373 return nvdimm_bus;
374 err:
375 put_device(&nvdimm_bus->dev);
376 return NULL;
378 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
380 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
382 if (!nvdimm_bus)
383 return;
384 device_unregister(&nvdimm_bus->dev);
386 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
388 static int child_unregister(struct device *dev, void *data)
391 * the singular ndctl class device per bus needs to be
392 * "device_destroy"ed, so skip it here
394 * i.e. remove classless children
396 if (dev->class)
397 return 0;
399 if (is_nvdimm(dev)) {
400 struct nvdimm *nvdimm = to_nvdimm(dev);
401 bool dev_put = false;
403 /* We are shutting down. Make state frozen artificially. */
404 nvdimm_bus_lock(dev);
405 nvdimm->sec.state = NVDIMM_SECURITY_FROZEN;
406 if (test_and_clear_bit(NDD_WORK_PENDING, &nvdimm->flags))
407 dev_put = true;
408 nvdimm_bus_unlock(dev);
409 cancel_delayed_work_sync(&nvdimm->dwork);
410 if (dev_put)
411 put_device(dev);
413 nd_device_unregister(dev, ND_SYNC);
415 return 0;
418 static void free_badrange_list(struct list_head *badrange_list)
420 struct badrange_entry *bre, *next;
422 list_for_each_entry_safe(bre, next, badrange_list, list) {
423 list_del(&bre->list);
424 kfree(bre);
426 list_del_init(badrange_list);
429 static int nd_bus_remove(struct device *dev)
431 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
433 mutex_lock(&nvdimm_bus_list_mutex);
434 list_del_init(&nvdimm_bus->list);
435 mutex_unlock(&nvdimm_bus_list_mutex);
437 nd_synchronize();
438 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
440 spin_lock(&nvdimm_bus->badrange.lock);
441 free_badrange_list(&nvdimm_bus->badrange.list);
442 spin_unlock(&nvdimm_bus->badrange.lock);
444 nvdimm_bus_destroy_ndctl(nvdimm_bus);
446 return 0;
449 static int nd_bus_probe(struct device *dev)
451 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
452 int rc;
454 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
455 if (rc)
456 return rc;
458 mutex_lock(&nvdimm_bus_list_mutex);
459 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
460 mutex_unlock(&nvdimm_bus_list_mutex);
462 /* enable bus provider attributes to look up their local context */
463 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
465 return 0;
468 static struct nd_device_driver nd_bus_driver = {
469 .probe = nd_bus_probe,
470 .remove = nd_bus_remove,
471 .drv = {
472 .name = "nd_bus",
473 .suppress_bind_attrs = true,
474 .bus = &nvdimm_bus_type,
475 .owner = THIS_MODULE,
476 .mod_name = KBUILD_MODNAME,
480 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
482 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
484 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
485 return true;
487 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
490 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
492 void nd_synchronize(void)
494 async_synchronize_full_domain(&nd_async_domain);
496 EXPORT_SYMBOL_GPL(nd_synchronize);
498 static void nd_async_device_register(void *d, async_cookie_t cookie)
500 struct device *dev = d;
502 if (device_add(dev) != 0) {
503 dev_err(dev, "%s: failed\n", __func__);
504 put_device(dev);
506 put_device(dev);
507 if (dev->parent)
508 put_device(dev->parent);
511 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
513 struct device *dev = d;
515 /* flush bus operations before delete */
516 nvdimm_bus_lock(dev);
517 nvdimm_bus_unlock(dev);
519 device_unregister(dev);
520 put_device(dev);
523 void __nd_device_register(struct device *dev)
525 if (!dev)
526 return;
529 * Ensure that region devices always have their NUMA node set as
530 * early as possible. This way we are able to make certain that
531 * any memory associated with the creation and the creation
532 * itself of the region is associated with the correct node.
534 if (is_nd_region(dev))
535 set_dev_node(dev, to_nd_region(dev)->numa_node);
537 dev->bus = &nvdimm_bus_type;
538 if (dev->parent) {
539 get_device(dev->parent);
540 if (dev_to_node(dev) == NUMA_NO_NODE)
541 set_dev_node(dev, dev_to_node(dev->parent));
543 get_device(dev);
545 async_schedule_dev_domain(nd_async_device_register, dev,
546 &nd_async_domain);
549 void nd_device_register(struct device *dev)
551 device_initialize(dev);
552 __nd_device_register(dev);
554 EXPORT_SYMBOL(nd_device_register);
556 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
558 switch (mode) {
559 case ND_ASYNC:
560 get_device(dev);
561 async_schedule_domain(nd_async_device_unregister, dev,
562 &nd_async_domain);
563 break;
564 case ND_SYNC:
565 nd_synchronize();
566 device_unregister(dev);
567 break;
570 EXPORT_SYMBOL(nd_device_unregister);
573 * __nd_driver_register() - register a region or a namespace driver
574 * @nd_drv: driver to register
575 * @owner: automatically set by nd_driver_register() macro
576 * @mod_name: automatically set by nd_driver_register() macro
578 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
579 const char *mod_name)
581 struct device_driver *drv = &nd_drv->drv;
583 if (!nd_drv->type) {
584 pr_debug("driver type bitmask not set (%ps)\n",
585 __builtin_return_address(0));
586 return -EINVAL;
589 if (!nd_drv->probe) {
590 pr_debug("%s ->probe() must be specified\n", mod_name);
591 return -EINVAL;
594 drv->bus = &nvdimm_bus_type;
595 drv->owner = owner;
596 drv->mod_name = mod_name;
598 return driver_register(drv);
600 EXPORT_SYMBOL(__nd_driver_register);
602 int nvdimm_revalidate_disk(struct gendisk *disk)
604 struct device *dev = disk_to_dev(disk)->parent;
605 struct nd_region *nd_region = to_nd_region(dev->parent);
606 int disk_ro = get_disk_ro(disk);
609 * Upgrade to read-only if the region is read-only preserve as
610 * read-only if the disk is already read-only.
612 if (disk_ro || nd_region->ro == disk_ro)
613 return 0;
615 dev_info(dev, "%s read-only, marking %s read-only\n",
616 dev_name(&nd_region->dev), disk->disk_name);
617 set_disk_ro(disk, 1);
619 return 0;
622 EXPORT_SYMBOL(nvdimm_revalidate_disk);
624 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
625 char *buf)
627 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
628 to_nd_device_type(dev));
630 static DEVICE_ATTR_RO(modalias);
632 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
633 char *buf)
635 return sprintf(buf, "%s\n", dev->type->name);
637 static DEVICE_ATTR_RO(devtype);
639 static struct attribute *nd_device_attributes[] = {
640 &dev_attr_modalias.attr,
641 &dev_attr_devtype.attr,
642 NULL,
646 * nd_device_attribute_group - generic attributes for all devices on an nd bus
648 struct attribute_group nd_device_attribute_group = {
649 .attrs = nd_device_attributes,
651 EXPORT_SYMBOL_GPL(nd_device_attribute_group);
653 static ssize_t numa_node_show(struct device *dev,
654 struct device_attribute *attr, char *buf)
656 return sprintf(buf, "%d\n", dev_to_node(dev));
658 static DEVICE_ATTR_RO(numa_node);
660 static struct attribute *nd_numa_attributes[] = {
661 &dev_attr_numa_node.attr,
662 NULL,
665 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
666 int n)
668 if (!IS_ENABLED(CONFIG_NUMA))
669 return 0;
671 return a->mode;
675 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
677 struct attribute_group nd_numa_attribute_group = {
678 .attrs = nd_numa_attributes,
679 .is_visible = nd_numa_attr_visible,
681 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
683 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
685 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
686 struct device *dev;
688 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
689 "ndctl%d", nvdimm_bus->id);
691 if (IS_ERR(dev))
692 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
693 nvdimm_bus->id, PTR_ERR(dev));
694 return PTR_ERR_OR_ZERO(dev);
697 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
699 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
702 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
703 [ND_CMD_IMPLEMENTED] = { },
704 [ND_CMD_SMART] = {
705 .out_num = 2,
706 .out_sizes = { 4, 128, },
708 [ND_CMD_SMART_THRESHOLD] = {
709 .out_num = 2,
710 .out_sizes = { 4, 8, },
712 [ND_CMD_DIMM_FLAGS] = {
713 .out_num = 2,
714 .out_sizes = { 4, 4 },
716 [ND_CMD_GET_CONFIG_SIZE] = {
717 .out_num = 3,
718 .out_sizes = { 4, 4, 4, },
720 [ND_CMD_GET_CONFIG_DATA] = {
721 .in_num = 2,
722 .in_sizes = { 4, 4, },
723 .out_num = 2,
724 .out_sizes = { 4, UINT_MAX, },
726 [ND_CMD_SET_CONFIG_DATA] = {
727 .in_num = 3,
728 .in_sizes = { 4, 4, UINT_MAX, },
729 .out_num = 1,
730 .out_sizes = { 4, },
732 [ND_CMD_VENDOR] = {
733 .in_num = 3,
734 .in_sizes = { 4, 4, UINT_MAX, },
735 .out_num = 3,
736 .out_sizes = { 4, 4, UINT_MAX, },
738 [ND_CMD_CALL] = {
739 .in_num = 2,
740 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
741 .out_num = 1,
742 .out_sizes = { UINT_MAX, },
746 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
748 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
749 return &__nd_cmd_dimm_descs[cmd];
750 return NULL;
752 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
754 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
755 [ND_CMD_IMPLEMENTED] = { },
756 [ND_CMD_ARS_CAP] = {
757 .in_num = 2,
758 .in_sizes = { 8, 8, },
759 .out_num = 4,
760 .out_sizes = { 4, 4, 4, 4, },
762 [ND_CMD_ARS_START] = {
763 .in_num = 5,
764 .in_sizes = { 8, 8, 2, 1, 5, },
765 .out_num = 2,
766 .out_sizes = { 4, 4, },
768 [ND_CMD_ARS_STATUS] = {
769 .out_num = 3,
770 .out_sizes = { 4, 4, UINT_MAX, },
772 [ND_CMD_CLEAR_ERROR] = {
773 .in_num = 2,
774 .in_sizes = { 8, 8, },
775 .out_num = 3,
776 .out_sizes = { 4, 4, 8, },
778 [ND_CMD_CALL] = {
779 .in_num = 2,
780 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
781 .out_num = 1,
782 .out_sizes = { UINT_MAX, },
786 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
788 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
789 return &__nd_cmd_bus_descs[cmd];
790 return NULL;
792 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
794 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
795 const struct nd_cmd_desc *desc, int idx, void *buf)
797 if (idx >= desc->in_num)
798 return UINT_MAX;
800 if (desc->in_sizes[idx] < UINT_MAX)
801 return desc->in_sizes[idx];
803 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
804 struct nd_cmd_set_config_hdr *hdr = buf;
806 return hdr->in_length;
807 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
808 struct nd_cmd_vendor_hdr *hdr = buf;
810 return hdr->in_length;
811 } else if (cmd == ND_CMD_CALL) {
812 struct nd_cmd_pkg *pkg = buf;
814 return pkg->nd_size_in;
817 return UINT_MAX;
819 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
821 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
822 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
823 const u32 *out_field, unsigned long remainder)
825 if (idx >= desc->out_num)
826 return UINT_MAX;
828 if (desc->out_sizes[idx] < UINT_MAX)
829 return desc->out_sizes[idx];
831 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
832 return in_field[1];
833 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
834 return out_field[1];
835 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
837 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
838 * "Size of Output Buffer in bytes, including this
839 * field."
841 if (out_field[1] < 4)
842 return 0;
844 * ACPI 6.1 is ambiguous if 'status' is included in the
845 * output size. If we encounter an output size that
846 * overshoots the remainder by 4 bytes, assume it was
847 * including 'status'.
849 if (out_field[1] - 4 == remainder)
850 return remainder;
851 return out_field[1] - 8;
852 } else if (cmd == ND_CMD_CALL) {
853 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
855 return pkg->nd_size_out;
859 return UINT_MAX;
861 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
863 void wait_nvdimm_bus_probe_idle(struct device *dev)
865 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
867 do {
868 if (nvdimm_bus->probe_active == 0)
869 break;
870 nvdimm_bus_unlock(&nvdimm_bus->dev);
871 wait_event(nvdimm_bus->probe_wait,
872 nvdimm_bus->probe_active == 0);
873 nvdimm_bus_lock(&nvdimm_bus->dev);
874 } while (true);
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)
889 return 0;
891 start = clear_err->address;
892 end = clear_err->address + clear_err->cleared - 1;
894 if (nd_btt || nd_pfn || nd_dax) {
895 if (nd_btt)
896 ndns = nd_btt->ndns;
897 else if (nd_pfn)
898 ndns = nd_pfn->ndns;
899 else if (nd_dax)
900 ndns = nd_dax->nd_pfn.ndns;
902 if (!ndns)
903 return 0;
904 } else
905 ndns = to_ndns(dev);
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))
912 return -EBUSY;
914 return 0;
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, data);
933 if (rc)
934 return rc;
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)
943 return 0;
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))
948 return -EBUSY;
949 return 0;
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 static char out_env[ND_CMD_MAX_ENVELOPE];
957 static char in_env[ND_CMD_MAX_ENVELOPE];
958 const struct nd_cmd_desc *desc = NULL;
959 unsigned int cmd = _IOC_NR(ioctl_cmd);
960 struct device *dev = &nvdimm_bus->dev;
961 void __user *p = (void __user *) arg;
962 const char *cmd_name, *dimm_name;
963 u32 in_len = 0, out_len = 0;
964 unsigned int func = cmd;
965 unsigned long cmd_mask;
966 struct nd_cmd_pkg pkg;
967 int rc, i, cmd_rc;
968 u64 buf_len = 0;
969 void *buf;
971 if (nvdimm) {
972 desc = nd_cmd_dimm_desc(cmd);
973 cmd_name = nvdimm_cmd_name(cmd);
974 cmd_mask = nvdimm->cmd_mask;
975 dimm_name = dev_name(&nvdimm->dev);
976 } else {
977 desc = nd_cmd_bus_desc(cmd);
978 cmd_name = nvdimm_bus_cmd_name(cmd);
979 cmd_mask = nd_desc->cmd_mask;
980 dimm_name = "bus";
983 if (cmd == ND_CMD_CALL) {
984 if (copy_from_user(&pkg, p, sizeof(pkg)))
985 return -EFAULT;
988 if (!desc || (desc->out_num + desc->in_num == 0) ||
989 !test_bit(cmd, &cmd_mask))
990 return -ENOTTY;
992 /* fail write commands (when read-only) */
993 if (read_only)
994 switch (cmd) {
995 case ND_CMD_VENDOR:
996 case ND_CMD_SET_CONFIG_DATA:
997 case ND_CMD_ARS_START:
998 case ND_CMD_CLEAR_ERROR:
999 case ND_CMD_CALL:
1000 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
1001 nvdimm ? nvdimm_cmd_name(cmd)
1002 : nvdimm_bus_cmd_name(cmd));
1003 return -EPERM;
1004 default:
1005 break;
1008 /* process an input envelope */
1009 for (i = 0; i < desc->in_num; i++) {
1010 u32 in_size, copy;
1012 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
1013 if (in_size == UINT_MAX) {
1014 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
1015 __func__, dimm_name, cmd_name, i);
1016 return -ENXIO;
1018 if (in_len < sizeof(in_env))
1019 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
1020 else
1021 copy = 0;
1022 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
1023 return -EFAULT;
1024 in_len += in_size;
1027 if (cmd == ND_CMD_CALL) {
1028 func = pkg.nd_command;
1029 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1030 dimm_name, pkg.nd_command,
1031 in_len, out_len, buf_len);
1034 /* process an output envelope */
1035 for (i = 0; i < desc->out_num; i++) {
1036 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
1037 (u32 *) in_env, (u32 *) out_env, 0);
1038 u32 copy;
1040 if (out_size == UINT_MAX) {
1041 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1042 dimm_name, cmd_name, i);
1043 return -EFAULT;
1045 if (out_len < sizeof(out_env))
1046 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1047 else
1048 copy = 0;
1049 if (copy && copy_from_user(&out_env[out_len],
1050 p + in_len + out_len, copy))
1051 return -EFAULT;
1052 out_len += out_size;
1055 buf_len = (u64) out_len + (u64) in_len;
1056 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
1057 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1058 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
1059 return -EINVAL;
1062 buf = vmalloc(buf_len);
1063 if (!buf)
1064 return -ENOMEM;
1066 if (copy_from_user(buf, p, buf_len)) {
1067 rc = -EFAULT;
1068 goto out;
1071 nvdimm_bus_lock(&nvdimm_bus->dev);
1072 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
1073 if (rc)
1074 goto out_unlock;
1076 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
1077 if (rc < 0)
1078 goto out_unlock;
1080 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1081 struct nd_cmd_clear_error *clear_err = buf;
1083 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1084 clear_err->cleared);
1086 nvdimm_bus_unlock(&nvdimm_bus->dev);
1088 if (copy_to_user(p, buf, buf_len))
1089 rc = -EFAULT;
1091 vfree(buf);
1092 return rc;
1094 out_unlock:
1095 nvdimm_bus_unlock(&nvdimm_bus->dev);
1096 out:
1097 vfree(buf);
1098 return rc;
1101 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1103 long id = (long) file->private_data;
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 if (nvdimm_bus->id == id) {
1111 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
1112 break;
1115 mutex_unlock(&nvdimm_bus_list_mutex);
1117 return rc;
1120 static int match_dimm(struct device *dev, void *data)
1122 long id = (long) data;
1124 if (is_nvdimm(dev)) {
1125 struct nvdimm *nvdimm = to_nvdimm(dev);
1127 return nvdimm->id == id;
1130 return 0;
1133 static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1135 int rc = -ENXIO, ro;
1136 struct nvdimm_bus *nvdimm_bus;
1138 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1139 mutex_lock(&nvdimm_bus_list_mutex);
1140 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1141 struct device *dev = device_find_child(&nvdimm_bus->dev,
1142 file->private_data, match_dimm);
1143 struct nvdimm *nvdimm;
1145 if (!dev)
1146 continue;
1148 nvdimm = to_nvdimm(dev);
1149 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1150 put_device(dev);
1151 break;
1153 mutex_unlock(&nvdimm_bus_list_mutex);
1155 return rc;
1158 static int nd_open(struct inode *inode, struct file *file)
1160 long minor = iminor(inode);
1162 file->private_data = (void *) minor;
1163 return 0;
1166 static const struct file_operations nvdimm_bus_fops = {
1167 .owner = THIS_MODULE,
1168 .open = nd_open,
1169 .unlocked_ioctl = nd_ioctl,
1170 .compat_ioctl = nd_ioctl,
1171 .llseek = noop_llseek,
1174 static const struct file_operations nvdimm_fops = {
1175 .owner = THIS_MODULE,
1176 .open = nd_open,
1177 .unlocked_ioctl = nvdimm_ioctl,
1178 .compat_ioctl = nvdimm_ioctl,
1179 .llseek = noop_llseek,
1182 int __init nvdimm_bus_init(void)
1184 int rc;
1186 rc = bus_register(&nvdimm_bus_type);
1187 if (rc)
1188 return rc;
1190 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1191 if (rc < 0)
1192 goto err_bus_chrdev;
1193 nvdimm_bus_major = rc;
1195 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1196 if (rc < 0)
1197 goto err_dimm_chrdev;
1198 nvdimm_major = rc;
1200 nd_class = class_create(THIS_MODULE, "nd");
1201 if (IS_ERR(nd_class)) {
1202 rc = PTR_ERR(nd_class);
1203 goto err_class;
1206 rc = driver_register(&nd_bus_driver.drv);
1207 if (rc)
1208 goto err_nd_bus;
1210 return 0;
1212 err_nd_bus:
1213 class_destroy(nd_class);
1214 err_class:
1215 unregister_chrdev(nvdimm_major, "dimmctl");
1216 err_dimm_chrdev:
1217 unregister_chrdev(nvdimm_bus_major, "ndctl");
1218 err_bus_chrdev:
1219 bus_unregister(&nvdimm_bus_type);
1221 return rc;
1224 void nvdimm_bus_exit(void)
1226 driver_unregister(&nd_bus_driver.drv);
1227 class_destroy(nd_class);
1228 unregister_chrdev(nvdimm_bus_major, "ndctl");
1229 unregister_chrdev(nvdimm_major, "dimmctl");
1230 bus_unregister(&nvdimm_bus_type);
1231 ida_destroy(&nd_ida);