xref: /openbmc/linux/drivers/nvdimm/bus.c (revision 53809828)
1 /*
2  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3  *
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.
7  *
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.
12  */
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/fs.h>
27 #include <linux/io.h>
28 #include <linux/mm.h>
29 #include <linux/nd.h>
30 #include "nd-core.h"
31 #include "nd.h"
32 #include "pfn.h"
33 
34 int nvdimm_major;
35 static int nvdimm_bus_major;
36 static struct class *nd_class;
37 static DEFINE_IDA(nd_ida);
38 
39 static int to_nd_device_type(struct device *dev)
40 {
41 	if (is_nvdimm(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));
51 
52 	return 0;
53 }
54 
55 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
56 {
57 	return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
58 			to_nd_device_type(dev));
59 }
60 
61 static struct module *to_bus_provider(struct device *dev)
62 {
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);
66 
67 		return nvdimm_bus->nd_desc->module;
68 	}
69 	return NULL;
70 }
71 
72 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
73 {
74 	nvdimm_bus_lock(&nvdimm_bus->dev);
75 	nvdimm_bus->probe_active++;
76 	nvdimm_bus_unlock(&nvdimm_bus->dev);
77 }
78 
79 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
80 {
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);
85 }
86 
87 static int nvdimm_bus_probe(struct device *dev)
88 {
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);
92 	int rc;
93 
94 	if (!try_module_get(provider))
95 		return -ENXIO;
96 
97 	dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
98 			dev->driver->name, dev_name(dev));
99 
100 	nvdimm_bus_probe_start(nvdimm_bus);
101 	rc = nd_drv->probe(dev);
102 	if (rc == 0)
103 		nd_region_probe_success(nvdimm_bus, dev);
104 	else
105 		nd_region_disable(nvdimm_bus, dev);
106 	nvdimm_bus_probe_end(nvdimm_bus);
107 
108 	dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
109 			dev_name(dev), rc);
110 
111 	if (rc != 0)
112 		module_put(provider);
113 	return rc;
114 }
115 
116 static int nvdimm_bus_remove(struct device *dev)
117 {
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);
121 	int rc = 0;
122 
123 	if (nd_drv->remove)
124 		rc = nd_drv->remove(dev);
125 	nd_region_disable(nvdimm_bus, dev);
126 
127 	dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
128 			dev_name(dev), rc);
129 	module_put(provider);
130 	return rc;
131 }
132 
133 static void nvdimm_bus_shutdown(struct device *dev)
134 {
135 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
136 	struct nd_device_driver *nd_drv = NULL;
137 
138 	if (dev->driver)
139 		nd_drv = to_nd_device_driver(dev->driver);
140 
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));
145 	}
146 }
147 
148 void nd_device_notify(struct device *dev, enum nvdimm_event event)
149 {
150 	device_lock(dev);
151 	if (dev->driver) {
152 		struct nd_device_driver *nd_drv;
153 
154 		nd_drv = to_nd_device_driver(dev->driver);
155 		if (nd_drv->notify)
156 			nd_drv->notify(dev, event);
157 	}
158 	device_unlock(dev);
159 }
160 EXPORT_SYMBOL(nd_device_notify);
161 
162 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
163 {
164 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
165 
166 	if (!nvdimm_bus)
167 		return;
168 
169 	/* caller is responsible for holding a reference on the device */
170 	nd_device_notify(&nd_region->dev, event);
171 }
172 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
173 
174 struct clear_badblocks_context {
175 	resource_size_t phys, cleared;
176 };
177 
178 static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
179 {
180 	struct clear_badblocks_context *ctx = data;
181 	struct nd_region *nd_region;
182 	resource_size_t ndr_end;
183 	sector_t sector;
184 
185 	/* make sure device is a region */
186 	if (!is_nd_pmem(dev))
187 		return 0;
188 
189 	nd_region = to_nd_region(dev);
190 	ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
191 
192 	/* make sure we are in the region */
193 	if (ctx->phys < nd_region->ndr_start
194 			|| (ctx->phys + ctx->cleared) > ndr_end)
195 		return 0;
196 
197 	sector = (ctx->phys - nd_region->ndr_start) / 512;
198 	badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
199 
200 	if (nd_region->bb_state)
201 		sysfs_notify_dirent(nd_region->bb_state);
202 
203 	return 0;
204 }
205 
206 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
207 		phys_addr_t phys, u64 cleared)
208 {
209 	struct clear_badblocks_context ctx = {
210 		.phys = phys,
211 		.cleared = cleared,
212 	};
213 
214 	device_for_each_child(&nvdimm_bus->dev, &ctx,
215 			nvdimm_clear_badblocks_region);
216 }
217 
218 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
219 		phys_addr_t phys, u64 cleared)
220 {
221 	if (cleared > 0)
222 		badrange_forget(&nvdimm_bus->badrange, phys, cleared);
223 
224 	if (cleared > 0 && cleared / 512)
225 		nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
226 }
227 
228 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
229 		unsigned int len)
230 {
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;
237 	int cmd_rc, rc;
238 
239 	if (!nvdimm_bus)
240 		return -ENXIO;
241 
242 	nd_desc = nvdimm_bus->nd_desc;
243 	/*
244 	 * if ndctl does not exist, it's PMEM_LEGACY and
245 	 * we want to just pretend everything is handled.
246 	 */
247 	if (!nd_desc->ndctl)
248 		return len;
249 
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);
257 	if (rc < 0)
258 		return rc;
259 	if (cmd_rc < 0)
260 		return cmd_rc;
261 	clear_err_unit = ars_cap.clear_err_unit;
262 	if (!clear_err_unit || !is_power_of_2(clear_err_unit))
263 		return -ENXIO;
264 
265 	mask = clear_err_unit - 1;
266 	if ((phys | len) & mask)
267 		return -ENXIO;
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);
275 	if (rc < 0)
276 		return rc;
277 	if (cmd_rc < 0)
278 		return cmd_rc;
279 
280 	nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
281 
282 	return clear_err.cleared;
283 }
284 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
285 
286 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
287 
288 static struct bus_type nvdimm_bus_type = {
289 	.name = "nd",
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,
295 };
296 
297 static void nvdimm_bus_release(struct device *dev)
298 {
299 	struct nvdimm_bus *nvdimm_bus;
300 
301 	nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
302 	ida_simple_remove(&nd_ida, nvdimm_bus->id);
303 	kfree(nvdimm_bus);
304 }
305 
306 static bool is_nvdimm_bus(struct device *dev)
307 {
308 	return dev->release == nvdimm_bus_release;
309 }
310 
311 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
312 {
313 	struct device *dev;
314 
315 	for (dev = nd_dev; dev; dev = dev->parent)
316 		if (is_nvdimm_bus(dev))
317 			break;
318 	dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
319 	if (dev)
320 		return to_nvdimm_bus(dev);
321 	return NULL;
322 }
323 
324 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
325 {
326 	struct nvdimm_bus *nvdimm_bus;
327 
328 	nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
329 	WARN_ON(!is_nvdimm_bus(dev));
330 	return nvdimm_bus;
331 }
332 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
333 
334 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
335 		struct nvdimm_bus_descriptor *nd_desc)
336 {
337 	struct nvdimm_bus *nvdimm_bus;
338 	int rc;
339 
340 	nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
341 	if (!nvdimm_bus)
342 		return NULL;
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) {
350 		kfree(nvdimm_bus);
351 		return NULL;
352 	}
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);
361 	if (rc) {
362 		dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
363 		goto err;
364 	}
365 
366 	return nvdimm_bus;
367  err:
368 	put_device(&nvdimm_bus->dev);
369 	return NULL;
370 }
371 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
372 
373 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
374 {
375 	if (!nvdimm_bus)
376 		return;
377 	device_unregister(&nvdimm_bus->dev);
378 }
379 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
380 
381 static int child_unregister(struct device *dev, void *data)
382 {
383 	/*
384 	 * the singular ndctl class device per bus needs to be
385 	 * "device_destroy"ed, so skip it here
386 	 *
387 	 * i.e. remove classless children
388 	 */
389 	if (dev->class)
390 		/* pass */;
391 	else
392 		nd_device_unregister(dev, ND_SYNC);
393 	return 0;
394 }
395 
396 static void free_badrange_list(struct list_head *badrange_list)
397 {
398 	struct badrange_entry *bre, *next;
399 
400 	list_for_each_entry_safe(bre, next, badrange_list, list) {
401 		list_del(&bre->list);
402 		kfree(bre);
403 	}
404 	list_del_init(badrange_list);
405 }
406 
407 static int nd_bus_remove(struct device *dev)
408 {
409 	struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
410 
411 	mutex_lock(&nvdimm_bus_list_mutex);
412 	list_del_init(&nvdimm_bus->list);
413 	mutex_unlock(&nvdimm_bus_list_mutex);
414 
415 	nd_synchronize();
416 	device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
417 
418 	spin_lock(&nvdimm_bus->badrange.lock);
419 	free_badrange_list(&nvdimm_bus->badrange.list);
420 	spin_unlock(&nvdimm_bus->badrange.lock);
421 
422 	nvdimm_bus_destroy_ndctl(nvdimm_bus);
423 
424 	return 0;
425 }
426 
427 static int nd_bus_probe(struct device *dev)
428 {
429 	struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
430 	int rc;
431 
432 	rc = nvdimm_bus_create_ndctl(nvdimm_bus);
433 	if (rc)
434 		return rc;
435 
436 	mutex_lock(&nvdimm_bus_list_mutex);
437 	list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
438 	mutex_unlock(&nvdimm_bus_list_mutex);
439 
440 	/* enable bus provider attributes to look up their local context */
441 	dev_set_drvdata(dev, nvdimm_bus->nd_desc);
442 
443 	return 0;
444 }
445 
446 static struct nd_device_driver nd_bus_driver = {
447 	.probe = nd_bus_probe,
448 	.remove = nd_bus_remove,
449 	.drv = {
450 		.name = "nd_bus",
451 		.suppress_bind_attrs = true,
452 		.bus = &nvdimm_bus_type,
453 		.owner = THIS_MODULE,
454 		.mod_name = KBUILD_MODNAME,
455 	},
456 };
457 
458 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
459 {
460 	struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
461 
462 	if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
463 		return true;
464 
465 	return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
466 }
467 
468 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
469 
470 void nd_synchronize(void)
471 {
472 	async_synchronize_full_domain(&nd_async_domain);
473 }
474 EXPORT_SYMBOL_GPL(nd_synchronize);
475 
476 static void nd_async_device_register(void *d, async_cookie_t cookie)
477 {
478 	struct device *dev = d;
479 
480 	if (device_add(dev) != 0) {
481 		dev_err(dev, "%s: failed\n", __func__);
482 		put_device(dev);
483 	}
484 	put_device(dev);
485 	if (dev->parent)
486 		put_device(dev->parent);
487 }
488 
489 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
490 {
491 	struct device *dev = d;
492 
493 	/* flush bus operations before delete */
494 	nvdimm_bus_lock(dev);
495 	nvdimm_bus_unlock(dev);
496 
497 	device_unregister(dev);
498 	put_device(dev);
499 }
500 
501 void __nd_device_register(struct device *dev)
502 {
503 	if (!dev)
504 		return;
505 
506 	/*
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.
511 	 */
512 	if (is_nd_region(dev))
513 		set_dev_node(dev, to_nd_region(dev)->numa_node);
514 
515 	dev->bus = &nvdimm_bus_type;
516 	if (dev->parent)
517 		get_device(dev->parent);
518 	get_device(dev);
519 	async_schedule_domain(nd_async_device_register, dev,
520 			&nd_async_domain);
521 }
522 
523 void nd_device_register(struct device *dev)
524 {
525 	device_initialize(dev);
526 	__nd_device_register(dev);
527 }
528 EXPORT_SYMBOL(nd_device_register);
529 
530 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
531 {
532 	switch (mode) {
533 	case ND_ASYNC:
534 		get_device(dev);
535 		async_schedule_domain(nd_async_device_unregister, dev,
536 				&nd_async_domain);
537 		break;
538 	case ND_SYNC:
539 		nd_synchronize();
540 		device_unregister(dev);
541 		break;
542 	}
543 }
544 EXPORT_SYMBOL(nd_device_unregister);
545 
546 /**
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
551  */
552 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
553 		const char *mod_name)
554 {
555 	struct device_driver *drv = &nd_drv->drv;
556 
557 	if (!nd_drv->type) {
558 		pr_debug("driver type bitmask not set (%pf)\n",
559 				__builtin_return_address(0));
560 		return -EINVAL;
561 	}
562 
563 	if (!nd_drv->probe) {
564 		pr_debug("%s ->probe() must be specified\n", mod_name);
565 		return -EINVAL;
566 	}
567 
568 	drv->bus = &nvdimm_bus_type;
569 	drv->owner = owner;
570 	drv->mod_name = mod_name;
571 
572 	return driver_register(drv);
573 }
574 EXPORT_SYMBOL(__nd_driver_register);
575 
576 int nvdimm_revalidate_disk(struct gendisk *disk)
577 {
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);
581 
582 	/*
583 	 * Upgrade to read-only if the region is read-only preserve as
584 	 * read-only if the disk is already read-only.
585 	 */
586 	if (disk_ro || nd_region->ro == disk_ro)
587 		return 0;
588 
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);
592 
593 	return 0;
594 
595 }
596 EXPORT_SYMBOL(nvdimm_revalidate_disk);
597 
598 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
599 		char *buf)
600 {
601 	return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
602 			to_nd_device_type(dev));
603 }
604 static DEVICE_ATTR_RO(modalias);
605 
606 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
607 		char *buf)
608 {
609 	return sprintf(buf, "%s\n", dev->type->name);
610 }
611 static DEVICE_ATTR_RO(devtype);
612 
613 static struct attribute *nd_device_attributes[] = {
614 	&dev_attr_modalias.attr,
615 	&dev_attr_devtype.attr,
616 	NULL,
617 };
618 
619 /**
620  * nd_device_attribute_group - generic attributes for all devices on an nd bus
621  */
622 struct attribute_group nd_device_attribute_group = {
623 	.attrs = nd_device_attributes,
624 };
625 EXPORT_SYMBOL_GPL(nd_device_attribute_group);
626 
627 static ssize_t numa_node_show(struct device *dev,
628 		struct device_attribute *attr, char *buf)
629 {
630 	return sprintf(buf, "%d\n", dev_to_node(dev));
631 }
632 static DEVICE_ATTR_RO(numa_node);
633 
634 static struct attribute *nd_numa_attributes[] = {
635 	&dev_attr_numa_node.attr,
636 	NULL,
637 };
638 
639 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
640 		int n)
641 {
642 	if (!IS_ENABLED(CONFIG_NUMA))
643 		return 0;
644 
645 	return a->mode;
646 }
647 
648 /**
649  * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
650  */
651 struct attribute_group nd_numa_attribute_group = {
652 	.attrs = nd_numa_attributes,
653 	.is_visible = nd_numa_attr_visible,
654 };
655 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
656 
657 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
658 {
659 	dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
660 	struct device *dev;
661 
662 	dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
663 			"ndctl%d", nvdimm_bus->id);
664 
665 	if (IS_ERR(dev))
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);
669 }
670 
671 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
672 {
673 	device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
674 }
675 
676 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
677 	[ND_CMD_IMPLEMENTED] = { },
678 	[ND_CMD_SMART] = {
679 		.out_num = 2,
680 		.out_sizes = { 4, 128, },
681 	},
682 	[ND_CMD_SMART_THRESHOLD] = {
683 		.out_num = 2,
684 		.out_sizes = { 4, 8, },
685 	},
686 	[ND_CMD_DIMM_FLAGS] = {
687 		.out_num = 2,
688 		.out_sizes = { 4, 4 },
689 	},
690 	[ND_CMD_GET_CONFIG_SIZE] = {
691 		.out_num = 3,
692 		.out_sizes = { 4, 4, 4, },
693 	},
694 	[ND_CMD_GET_CONFIG_DATA] = {
695 		.in_num = 2,
696 		.in_sizes = { 4, 4, },
697 		.out_num = 2,
698 		.out_sizes = { 4, UINT_MAX, },
699 	},
700 	[ND_CMD_SET_CONFIG_DATA] = {
701 		.in_num = 3,
702 		.in_sizes = { 4, 4, UINT_MAX, },
703 		.out_num = 1,
704 		.out_sizes = { 4, },
705 	},
706 	[ND_CMD_VENDOR] = {
707 		.in_num = 3,
708 		.in_sizes = { 4, 4, UINT_MAX, },
709 		.out_num = 3,
710 		.out_sizes = { 4, 4, UINT_MAX, },
711 	},
712 	[ND_CMD_CALL] = {
713 		.in_num = 2,
714 		.in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
715 		.out_num = 1,
716 		.out_sizes = { UINT_MAX, },
717 	},
718 };
719 
720 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
721 {
722 	if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
723 		return &__nd_cmd_dimm_descs[cmd];
724 	return NULL;
725 }
726 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
727 
728 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
729 	[ND_CMD_IMPLEMENTED] = { },
730 	[ND_CMD_ARS_CAP] = {
731 		.in_num = 2,
732 		.in_sizes = { 8, 8, },
733 		.out_num = 4,
734 		.out_sizes = { 4, 4, 4, 4, },
735 	},
736 	[ND_CMD_ARS_START] = {
737 		.in_num = 5,
738 		.in_sizes = { 8, 8, 2, 1, 5, },
739 		.out_num = 2,
740 		.out_sizes = { 4, 4, },
741 	},
742 	[ND_CMD_ARS_STATUS] = {
743 		.out_num = 3,
744 		.out_sizes = { 4, 4, UINT_MAX, },
745 	},
746 	[ND_CMD_CLEAR_ERROR] = {
747 		.in_num = 2,
748 		.in_sizes = { 8, 8, },
749 		.out_num = 3,
750 		.out_sizes = { 4, 4, 8, },
751 	},
752 	[ND_CMD_CALL] = {
753 		.in_num = 2,
754 		.in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
755 		.out_num = 1,
756 		.out_sizes = { UINT_MAX, },
757 	},
758 };
759 
760 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
761 {
762 	if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
763 		return &__nd_cmd_bus_descs[cmd];
764 	return NULL;
765 }
766 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
767 
768 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
769 		const struct nd_cmd_desc *desc, int idx, void *buf)
770 {
771 	if (idx >= desc->in_num)
772 		return UINT_MAX;
773 
774 	if (desc->in_sizes[idx] < UINT_MAX)
775 		return desc->in_sizes[idx];
776 
777 	if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
778 		struct nd_cmd_set_config_hdr *hdr = buf;
779 
780 		return hdr->in_length;
781 	} else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
782 		struct nd_cmd_vendor_hdr *hdr = buf;
783 
784 		return hdr->in_length;
785 	} else if (cmd == ND_CMD_CALL) {
786 		struct nd_cmd_pkg *pkg = buf;
787 
788 		return pkg->nd_size_in;
789 	}
790 
791 	return UINT_MAX;
792 }
793 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
794 
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)
798 {
799 	if (idx >= desc->out_num)
800 		return UINT_MAX;
801 
802 	if (desc->out_sizes[idx] < UINT_MAX)
803 		return desc->out_sizes[idx];
804 
805 	if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
806 		return in_field[1];
807 	else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
808 		return out_field[1];
809 	else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
810 		/*
811 		 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
812 		 * "Size of Output Buffer in bytes, including this
813 		 * field."
814 		 */
815 		if (out_field[1] < 4)
816 			return 0;
817 		/*
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'.
822 		 */
823 		if (out_field[1] - 4 == remainder)
824 			return 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;
828 
829 		return pkg->nd_size_out;
830 	}
831 
832 
833 	return UINT_MAX;
834 }
835 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
836 
837 void wait_nvdimm_bus_probe_idle(struct device *dev)
838 {
839 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
840 
841 	do {
842 		if (nvdimm_bus->probe_active == 0)
843 			break;
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);
848 	} while (true);
849 }
850 
851 static int nd_pmem_forget_poison_check(struct device *dev, void *data)
852 {
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;
861 
862 	if (nd_dax || !dev->driver)
863 		return 0;
864 
865 	start = clear_err->address;
866 	end = clear_err->address + clear_err->cleared - 1;
867 
868 	if (nd_btt || nd_pfn || nd_dax) {
869 		if (nd_btt)
870 			ndns = nd_btt->ndns;
871 		else if (nd_pfn)
872 			ndns = nd_pfn->ndns;
873 		else if (nd_dax)
874 			ndns = nd_dax->nd_pfn.ndns;
875 
876 		if (!ndns)
877 			return 0;
878 	} else
879 		ndns = to_ndns(dev);
880 
881 	nsio = to_nd_namespace_io(&ndns->dev);
882 	pstart = nsio->res.start + offset;
883 	pend = nsio->res.end - end_trunc;
884 
885 	if ((pstart >= start) && (pend <= end))
886 		return -EBUSY;
887 
888 	return 0;
889 
890 }
891 
892 static int nd_ns_forget_poison_check(struct device *dev, void *data)
893 {
894 	return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
895 }
896 
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)
900 {
901 	struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
902 
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);
906 
907 		if (rc)
908 			return rc;
909 	}
910 
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);
915 
916 	if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
917 		return 0;
918 
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))
922 		return -EBUSY;
923 	return 0;
924 }
925 
926 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
927 		int read_only, unsigned int ioctl_cmd, unsigned long arg)
928 {
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;
941 	int rc, i, cmd_rc;
942 	u64 buf_len = 0;
943 	void *buf;
944 
945 	if (nvdimm) {
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);
950 	} else {
951 		desc = nd_cmd_bus_desc(cmd);
952 		cmd_name = nvdimm_bus_cmd_name(cmd);
953 		cmd_mask = nd_desc->cmd_mask;
954 		dimm_name = "bus";
955 	}
956 
957 	if (cmd == ND_CMD_CALL) {
958 		if (copy_from_user(&pkg, p, sizeof(pkg)))
959 			return -EFAULT;
960 	}
961 
962 	if (!desc || (desc->out_num + desc->in_num == 0) ||
963 			!test_bit(cmd, &cmd_mask))
964 		return -ENOTTY;
965 
966 	/* fail write commands (when read-only) */
967 	if (read_only)
968 		switch (cmd) {
969 		case ND_CMD_VENDOR:
970 		case ND_CMD_SET_CONFIG_DATA:
971 		case ND_CMD_ARS_START:
972 		case ND_CMD_CLEAR_ERROR:
973 		case ND_CMD_CALL:
974 			dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
975 					nvdimm ? nvdimm_cmd_name(cmd)
976 					: nvdimm_bus_cmd_name(cmd));
977 			return -EPERM;
978 		default:
979 			break;
980 		}
981 
982 	/* process an input envelope */
983 	for (i = 0; i < desc->in_num; i++) {
984 		u32 in_size, copy;
985 
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);
990 			return -ENXIO;
991 		}
992 		if (in_len < sizeof(in_env))
993 			copy = min_t(u32, sizeof(in_env) - in_len, in_size);
994 		else
995 			copy = 0;
996 		if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
997 			return -EFAULT;
998 		in_len += in_size;
999 	}
1000 
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);
1006 	}
1007 
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);
1012 		u32 copy;
1013 
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);
1017 			return -EFAULT;
1018 		}
1019 		if (out_len < sizeof(out_env))
1020 			copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1021 		else
1022 			copy = 0;
1023 		if (copy && copy_from_user(&out_env[out_len],
1024 					p + in_len + out_len, copy))
1025 			return -EFAULT;
1026 		out_len += out_size;
1027 	}
1028 
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);
1033 		return -EINVAL;
1034 	}
1035 
1036 	buf = vmalloc(buf_len);
1037 	if (!buf)
1038 		return -ENOMEM;
1039 
1040 	if (copy_from_user(buf, p, buf_len)) {
1041 		rc = -EFAULT;
1042 		goto out;
1043 	}
1044 
1045 	nvdimm_bus_lock(&nvdimm_bus->dev);
1046 	rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
1047 	if (rc)
1048 		goto out_unlock;
1049 
1050 	rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
1051 	if (rc < 0)
1052 		goto out_unlock;
1053 
1054 	if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1055 		struct nd_cmd_clear_error *clear_err = buf;
1056 
1057 		nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1058 				clear_err->cleared);
1059 	}
1060 	nvdimm_bus_unlock(&nvdimm_bus->dev);
1061 
1062 	if (copy_to_user(p, buf, buf_len))
1063 		rc = -EFAULT;
1064 
1065 	vfree(buf);
1066 	return rc;
1067 
1068  out_unlock:
1069 	nvdimm_bus_unlock(&nvdimm_bus->dev);
1070  out:
1071 	vfree(buf);
1072 	return rc;
1073 }
1074 
1075 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1076 {
1077 	long id = (long) file->private_data;
1078 	int rc = -ENXIO, ro;
1079 	struct nvdimm_bus *nvdimm_bus;
1080 
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);
1086 			break;
1087 		}
1088 	}
1089 	mutex_unlock(&nvdimm_bus_list_mutex);
1090 
1091 	return rc;
1092 }
1093 
1094 static int match_dimm(struct device *dev, void *data)
1095 {
1096 	long id = (long) data;
1097 
1098 	if (is_nvdimm(dev)) {
1099 		struct nvdimm *nvdimm = to_nvdimm(dev);
1100 
1101 		return nvdimm->id == id;
1102 	}
1103 
1104 	return 0;
1105 }
1106 
1107 static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1108 {
1109 	int rc = -ENXIO, ro;
1110 	struct nvdimm_bus *nvdimm_bus;
1111 
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;
1118 
1119 		if (!dev)
1120 			continue;
1121 
1122 		nvdimm = to_nvdimm(dev);
1123 		rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1124 		put_device(dev);
1125 		break;
1126 	}
1127 	mutex_unlock(&nvdimm_bus_list_mutex);
1128 
1129 	return rc;
1130 }
1131 
1132 static int nd_open(struct inode *inode, struct file *file)
1133 {
1134 	long minor = iminor(inode);
1135 
1136 	file->private_data = (void *) minor;
1137 	return 0;
1138 }
1139 
1140 static const struct file_operations nvdimm_bus_fops = {
1141 	.owner = THIS_MODULE,
1142 	.open = nd_open,
1143 	.unlocked_ioctl = nd_ioctl,
1144 	.compat_ioctl = nd_ioctl,
1145 	.llseek = noop_llseek,
1146 };
1147 
1148 static const struct file_operations nvdimm_fops = {
1149 	.owner = THIS_MODULE,
1150 	.open = nd_open,
1151 	.unlocked_ioctl = nvdimm_ioctl,
1152 	.compat_ioctl = nvdimm_ioctl,
1153 	.llseek = noop_llseek,
1154 };
1155 
1156 int __init nvdimm_bus_init(void)
1157 {
1158 	int rc;
1159 
1160 	rc = bus_register(&nvdimm_bus_type);
1161 	if (rc)
1162 		return rc;
1163 
1164 	rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1165 	if (rc < 0)
1166 		goto err_bus_chrdev;
1167 	nvdimm_bus_major = rc;
1168 
1169 	rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1170 	if (rc < 0)
1171 		goto err_dimm_chrdev;
1172 	nvdimm_major = rc;
1173 
1174 	nd_class = class_create(THIS_MODULE, "nd");
1175 	if (IS_ERR(nd_class)) {
1176 		rc = PTR_ERR(nd_class);
1177 		goto err_class;
1178 	}
1179 
1180 	rc = driver_register(&nd_bus_driver.drv);
1181 	if (rc)
1182 		goto err_nd_bus;
1183 
1184 	return 0;
1185 
1186  err_nd_bus:
1187 	class_destroy(nd_class);
1188  err_class:
1189 	unregister_chrdev(nvdimm_major, "dimmctl");
1190  err_dimm_chrdev:
1191 	unregister_chrdev(nvdimm_bus_major, "ndctl");
1192  err_bus_chrdev:
1193 	bus_unregister(&nvdimm_bus_type);
1194 
1195 	return rc;
1196 }
1197 
1198 void nvdimm_bus_exit(void)
1199 {
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);
1206 }
1207