xref: /openbmc/linux/drivers/nvdimm/bus.c (revision 8d13c029)
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/vmalloc.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/fcntl.h>
19 #include <linux/async.h>
20 #include <linux/genhd.h>
21 #include <linux/ndctl.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/fs.h>
25 #include <linux/io.h>
26 #include <linux/mm.h>
27 #include <linux/nd.h>
28 #include "nd-core.h"
29 #include "nd.h"
30 #include "pfn.h"
31 
32 int nvdimm_major;
33 static int nvdimm_bus_major;
34 static struct class *nd_class;
35 static DEFINE_IDA(nd_ida);
36 
37 static int to_nd_device_type(struct device *dev)
38 {
39 	if (is_nvdimm(dev))
40 		return ND_DEVICE_DIMM;
41 	else if (is_nd_pmem(dev))
42 		return ND_DEVICE_REGION_PMEM;
43 	else if (is_nd_blk(dev))
44 		return ND_DEVICE_REGION_BLK;
45 	else if (is_nd_dax(dev))
46 		return ND_DEVICE_DAX_PMEM;
47 	else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent))
48 		return nd_region_to_nstype(to_nd_region(dev->parent));
49 
50 	return 0;
51 }
52 
53 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
54 {
55 	/*
56 	 * Ensure that region devices always have their numa node set as
57 	 * early as possible.
58 	 */
59 	if (is_nd_pmem(dev) || is_nd_blk(dev))
60 		set_dev_node(dev, to_nd_region(dev)->numa_node);
61 	return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
62 			to_nd_device_type(dev));
63 }
64 
65 static struct module *to_bus_provider(struct device *dev)
66 {
67 	/* pin bus providers while regions are enabled */
68 	if (is_nd_pmem(dev) || is_nd_blk(dev)) {
69 		struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
70 
71 		return nvdimm_bus->nd_desc->module;
72 	}
73 	return NULL;
74 }
75 
76 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
77 {
78 	nvdimm_bus_lock(&nvdimm_bus->dev);
79 	nvdimm_bus->probe_active++;
80 	nvdimm_bus_unlock(&nvdimm_bus->dev);
81 }
82 
83 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
84 {
85 	nvdimm_bus_lock(&nvdimm_bus->dev);
86 	if (--nvdimm_bus->probe_active == 0)
87 		wake_up(&nvdimm_bus->probe_wait);
88 	nvdimm_bus_unlock(&nvdimm_bus->dev);
89 }
90 
91 static int nvdimm_bus_probe(struct device *dev)
92 {
93 	struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
94 	struct module *provider = to_bus_provider(dev);
95 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
96 	int rc;
97 
98 	if (!try_module_get(provider))
99 		return -ENXIO;
100 
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);
108 
109 	dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
110 			dev_name(dev), rc);
111 
112 	if (rc != 0)
113 		module_put(provider);
114 	return rc;
115 }
116 
117 static int nvdimm_bus_remove(struct device *dev)
118 {
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;
123 
124 	if (nd_drv->remove)
125 		rc = nd_drv->remove(dev);
126 	nd_region_disable(nvdimm_bus, dev);
127 
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;
132 }
133 
134 static void nvdimm_bus_shutdown(struct device *dev)
135 {
136 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
137 	struct nd_device_driver *nd_drv = NULL;
138 
139 	if (dev->driver)
140 		nd_drv = to_nd_device_driver(dev->driver);
141 
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));
146 	}
147 }
148 
149 void nd_device_notify(struct device *dev, enum nvdimm_event event)
150 {
151 	device_lock(dev);
152 	if (dev->driver) {
153 		struct nd_device_driver *nd_drv;
154 
155 		nd_drv = to_nd_device_driver(dev->driver);
156 		if (nd_drv->notify)
157 			nd_drv->notify(dev, event);
158 	}
159 	device_unlock(dev);
160 }
161 EXPORT_SYMBOL(nd_device_notify);
162 
163 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
164 {
165 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
166 
167 	if (!nvdimm_bus)
168 		return;
169 
170 	/* caller is responsible for holding a reference on the device */
171 	nd_device_notify(&nd_region->dev, event);
172 }
173 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
174 
175 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
176 		unsigned int len)
177 {
178 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
179 	struct nvdimm_bus_descriptor *nd_desc;
180 	struct nd_cmd_clear_error clear_err;
181 	struct nd_cmd_ars_cap ars_cap;
182 	u32 clear_err_unit, mask;
183 	int cmd_rc, rc;
184 
185 	if (!nvdimm_bus)
186 		return -ENXIO;
187 
188 	nd_desc = nvdimm_bus->nd_desc;
189 	/*
190 	 * if ndctl does not exist, it's PMEM_LEGACY and
191 	 * we want to just pretend everything is handled.
192 	 */
193 	if (!nd_desc->ndctl)
194 		return len;
195 
196 	memset(&ars_cap, 0, sizeof(ars_cap));
197 	ars_cap.address = phys;
198 	ars_cap.length = len;
199 	rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
200 			sizeof(ars_cap), &cmd_rc);
201 	if (rc < 0)
202 		return rc;
203 	if (cmd_rc < 0)
204 		return cmd_rc;
205 	clear_err_unit = ars_cap.clear_err_unit;
206 	if (!clear_err_unit || !is_power_of_2(clear_err_unit))
207 		return -ENXIO;
208 
209 	mask = clear_err_unit - 1;
210 	if ((phys | len) & mask)
211 		return -ENXIO;
212 	memset(&clear_err, 0, sizeof(clear_err));
213 	clear_err.address = phys;
214 	clear_err.length = len;
215 	rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
216 			sizeof(clear_err), &cmd_rc);
217 	if (rc < 0)
218 		return rc;
219 	if (cmd_rc < 0)
220 		return cmd_rc;
221 
222 	if (clear_err.cleared > 0)
223 		nvdimm_forget_poison(nvdimm_bus, phys, clear_err.cleared);
224 
225 	return clear_err.cleared;
226 }
227 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
228 
229 void __nvdimm_bus_badblocks_clear(struct nvdimm_bus *nvdimm_bus,
230 		struct resource *res)
231 {
232 	lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
233 	device_for_each_child(&nvdimm_bus->dev, (void *)res,
234 			nvdimm_region_badblocks_clear);
235 }
236 EXPORT_SYMBOL_GPL(__nvdimm_bus_badblocks_clear);
237 
238 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
239 
240 static struct bus_type nvdimm_bus_type = {
241 	.name = "nd",
242 	.uevent = nvdimm_bus_uevent,
243 	.match = nvdimm_bus_match,
244 	.probe = nvdimm_bus_probe,
245 	.remove = nvdimm_bus_remove,
246 	.shutdown = nvdimm_bus_shutdown,
247 };
248 
249 static void nvdimm_bus_release(struct device *dev)
250 {
251 	struct nvdimm_bus *nvdimm_bus;
252 
253 	nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
254 	ida_simple_remove(&nd_ida, nvdimm_bus->id);
255 	kfree(nvdimm_bus);
256 }
257 
258 static bool is_nvdimm_bus(struct device *dev)
259 {
260 	return dev->release == nvdimm_bus_release;
261 }
262 
263 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
264 {
265 	struct device *dev;
266 
267 	for (dev = nd_dev; dev; dev = dev->parent)
268 		if (is_nvdimm_bus(dev))
269 			break;
270 	dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
271 	if (dev)
272 		return to_nvdimm_bus(dev);
273 	return NULL;
274 }
275 
276 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
277 {
278 	struct nvdimm_bus *nvdimm_bus;
279 
280 	nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
281 	WARN_ON(!is_nvdimm_bus(dev));
282 	return nvdimm_bus;
283 }
284 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
285 
286 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
287 		struct nvdimm_bus_descriptor *nd_desc)
288 {
289 	struct nvdimm_bus *nvdimm_bus;
290 	int rc;
291 
292 	nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
293 	if (!nvdimm_bus)
294 		return NULL;
295 	INIT_LIST_HEAD(&nvdimm_bus->list);
296 	INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
297 	INIT_LIST_HEAD(&nvdimm_bus->poison_list);
298 	init_waitqueue_head(&nvdimm_bus->probe_wait);
299 	nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
300 	mutex_init(&nvdimm_bus->reconfig_mutex);
301 	spin_lock_init(&nvdimm_bus->poison_lock);
302 	if (nvdimm_bus->id < 0) {
303 		kfree(nvdimm_bus);
304 		return NULL;
305 	}
306 	nvdimm_bus->nd_desc = nd_desc;
307 	nvdimm_bus->dev.parent = parent;
308 	nvdimm_bus->dev.release = nvdimm_bus_release;
309 	nvdimm_bus->dev.groups = nd_desc->attr_groups;
310 	nvdimm_bus->dev.bus = &nvdimm_bus_type;
311 	dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
312 	rc = device_register(&nvdimm_bus->dev);
313 	if (rc) {
314 		dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
315 		goto err;
316 	}
317 
318 	return nvdimm_bus;
319  err:
320 	put_device(&nvdimm_bus->dev);
321 	return NULL;
322 }
323 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
324 
325 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
326 {
327 	if (!nvdimm_bus)
328 		return;
329 	device_unregister(&nvdimm_bus->dev);
330 }
331 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
332 
333 static int child_unregister(struct device *dev, void *data)
334 {
335 	/*
336 	 * the singular ndctl class device per bus needs to be
337 	 * "device_destroy"ed, so skip it here
338 	 *
339 	 * i.e. remove classless children
340 	 */
341 	if (dev->class)
342 		/* pass */;
343 	else
344 		nd_device_unregister(dev, ND_SYNC);
345 	return 0;
346 }
347 
348 static void free_poison_list(struct list_head *poison_list)
349 {
350 	struct nd_poison *pl, *next;
351 
352 	list_for_each_entry_safe(pl, next, poison_list, list) {
353 		list_del(&pl->list);
354 		kfree(pl);
355 	}
356 	list_del_init(poison_list);
357 }
358 
359 static int nd_bus_remove(struct device *dev)
360 {
361 	struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
362 
363 	mutex_lock(&nvdimm_bus_list_mutex);
364 	list_del_init(&nvdimm_bus->list);
365 	mutex_unlock(&nvdimm_bus_list_mutex);
366 
367 	nd_synchronize();
368 	device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
369 
370 	spin_lock(&nvdimm_bus->poison_lock);
371 	free_poison_list(&nvdimm_bus->poison_list);
372 	spin_unlock(&nvdimm_bus->poison_lock);
373 
374 	nvdimm_bus_destroy_ndctl(nvdimm_bus);
375 
376 	return 0;
377 }
378 
379 static int nd_bus_probe(struct device *dev)
380 {
381 	struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
382 	int rc;
383 
384 	rc = nvdimm_bus_create_ndctl(nvdimm_bus);
385 	if (rc)
386 		return rc;
387 
388 	mutex_lock(&nvdimm_bus_list_mutex);
389 	list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
390 	mutex_unlock(&nvdimm_bus_list_mutex);
391 
392 	/* enable bus provider attributes to look up their local context */
393 	dev_set_drvdata(dev, nvdimm_bus->nd_desc);
394 
395 	return 0;
396 }
397 
398 static struct nd_device_driver nd_bus_driver = {
399 	.probe = nd_bus_probe,
400 	.remove = nd_bus_remove,
401 	.drv = {
402 		.name = "nd_bus",
403 		.suppress_bind_attrs = true,
404 		.bus = &nvdimm_bus_type,
405 		.owner = THIS_MODULE,
406 		.mod_name = KBUILD_MODNAME,
407 	},
408 };
409 
410 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
411 {
412 	struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
413 
414 	if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
415 		return true;
416 
417 	return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
418 }
419 
420 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
421 
422 void nd_synchronize(void)
423 {
424 	async_synchronize_full_domain(&nd_async_domain);
425 }
426 EXPORT_SYMBOL_GPL(nd_synchronize);
427 
428 static void nd_async_device_register(void *d, async_cookie_t cookie)
429 {
430 	struct device *dev = d;
431 
432 	if (device_add(dev) != 0) {
433 		dev_err(dev, "%s: failed\n", __func__);
434 		put_device(dev);
435 	}
436 	put_device(dev);
437 }
438 
439 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
440 {
441 	struct device *dev = d;
442 
443 	/* flush bus operations before delete */
444 	nvdimm_bus_lock(dev);
445 	nvdimm_bus_unlock(dev);
446 
447 	device_unregister(dev);
448 	put_device(dev);
449 }
450 
451 void __nd_device_register(struct device *dev)
452 {
453 	if (!dev)
454 		return;
455 	dev->bus = &nvdimm_bus_type;
456 	get_device(dev);
457 	async_schedule_domain(nd_async_device_register, dev,
458 			&nd_async_domain);
459 }
460 
461 void nd_device_register(struct device *dev)
462 {
463 	device_initialize(dev);
464 	__nd_device_register(dev);
465 }
466 EXPORT_SYMBOL(nd_device_register);
467 
468 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
469 {
470 	switch (mode) {
471 	case ND_ASYNC:
472 		get_device(dev);
473 		async_schedule_domain(nd_async_device_unregister, dev,
474 				&nd_async_domain);
475 		break;
476 	case ND_SYNC:
477 		nd_synchronize();
478 		device_unregister(dev);
479 		break;
480 	}
481 }
482 EXPORT_SYMBOL(nd_device_unregister);
483 
484 /**
485  * __nd_driver_register() - register a region or a namespace driver
486  * @nd_drv: driver to register
487  * @owner: automatically set by nd_driver_register() macro
488  * @mod_name: automatically set by nd_driver_register() macro
489  */
490 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
491 		const char *mod_name)
492 {
493 	struct device_driver *drv = &nd_drv->drv;
494 
495 	if (!nd_drv->type) {
496 		pr_debug("driver type bitmask not set (%pf)\n",
497 				__builtin_return_address(0));
498 		return -EINVAL;
499 	}
500 
501 	if (!nd_drv->probe) {
502 		pr_debug("%s ->probe() must be specified\n", mod_name);
503 		return -EINVAL;
504 	}
505 
506 	drv->bus = &nvdimm_bus_type;
507 	drv->owner = owner;
508 	drv->mod_name = mod_name;
509 
510 	return driver_register(drv);
511 }
512 EXPORT_SYMBOL(__nd_driver_register);
513 
514 int nvdimm_revalidate_disk(struct gendisk *disk)
515 {
516 	struct device *dev = disk_to_dev(disk)->parent;
517 	struct nd_region *nd_region = to_nd_region(dev->parent);
518 	const char *pol = nd_region->ro ? "only" : "write";
519 
520 	if (nd_region->ro == get_disk_ro(disk))
521 		return 0;
522 
523 	dev_info(dev, "%s read-%s, marking %s read-%s\n",
524 			dev_name(&nd_region->dev), pol, disk->disk_name, pol);
525 	set_disk_ro(disk, nd_region->ro);
526 
527 	return 0;
528 
529 }
530 EXPORT_SYMBOL(nvdimm_revalidate_disk);
531 
532 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
533 		char *buf)
534 {
535 	return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
536 			to_nd_device_type(dev));
537 }
538 static DEVICE_ATTR_RO(modalias);
539 
540 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
541 		char *buf)
542 {
543 	return sprintf(buf, "%s\n", dev->type->name);
544 }
545 static DEVICE_ATTR_RO(devtype);
546 
547 static struct attribute *nd_device_attributes[] = {
548 	&dev_attr_modalias.attr,
549 	&dev_attr_devtype.attr,
550 	NULL,
551 };
552 
553 /**
554  * nd_device_attribute_group - generic attributes for all devices on an nd bus
555  */
556 struct attribute_group nd_device_attribute_group = {
557 	.attrs = nd_device_attributes,
558 };
559 EXPORT_SYMBOL_GPL(nd_device_attribute_group);
560 
561 static ssize_t numa_node_show(struct device *dev,
562 		struct device_attribute *attr, char *buf)
563 {
564 	return sprintf(buf, "%d\n", dev_to_node(dev));
565 }
566 static DEVICE_ATTR_RO(numa_node);
567 
568 static struct attribute *nd_numa_attributes[] = {
569 	&dev_attr_numa_node.attr,
570 	NULL,
571 };
572 
573 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
574 		int n)
575 {
576 	if (!IS_ENABLED(CONFIG_NUMA))
577 		return 0;
578 
579 	return a->mode;
580 }
581 
582 /**
583  * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
584  */
585 struct attribute_group nd_numa_attribute_group = {
586 	.attrs = nd_numa_attributes,
587 	.is_visible = nd_numa_attr_visible,
588 };
589 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
590 
591 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
592 {
593 	dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
594 	struct device *dev;
595 
596 	dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
597 			"ndctl%d", nvdimm_bus->id);
598 
599 	if (IS_ERR(dev))
600 		dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
601 				nvdimm_bus->id, PTR_ERR(dev));
602 	return PTR_ERR_OR_ZERO(dev);
603 }
604 
605 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
606 {
607 	device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
608 }
609 
610 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
611 	[ND_CMD_IMPLEMENTED] = { },
612 	[ND_CMD_SMART] = {
613 		.out_num = 2,
614 		.out_sizes = { 4, 128, },
615 	},
616 	[ND_CMD_SMART_THRESHOLD] = {
617 		.out_num = 2,
618 		.out_sizes = { 4, 8, },
619 	},
620 	[ND_CMD_DIMM_FLAGS] = {
621 		.out_num = 2,
622 		.out_sizes = { 4, 4 },
623 	},
624 	[ND_CMD_GET_CONFIG_SIZE] = {
625 		.out_num = 3,
626 		.out_sizes = { 4, 4, 4, },
627 	},
628 	[ND_CMD_GET_CONFIG_DATA] = {
629 		.in_num = 2,
630 		.in_sizes = { 4, 4, },
631 		.out_num = 2,
632 		.out_sizes = { 4, UINT_MAX, },
633 	},
634 	[ND_CMD_SET_CONFIG_DATA] = {
635 		.in_num = 3,
636 		.in_sizes = { 4, 4, UINT_MAX, },
637 		.out_num = 1,
638 		.out_sizes = { 4, },
639 	},
640 	[ND_CMD_VENDOR] = {
641 		.in_num = 3,
642 		.in_sizes = { 4, 4, UINT_MAX, },
643 		.out_num = 3,
644 		.out_sizes = { 4, 4, UINT_MAX, },
645 	},
646 	[ND_CMD_CALL] = {
647 		.in_num = 2,
648 		.in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
649 		.out_num = 1,
650 		.out_sizes = { UINT_MAX, },
651 	},
652 };
653 
654 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
655 {
656 	if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
657 		return &__nd_cmd_dimm_descs[cmd];
658 	return NULL;
659 }
660 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
661 
662 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
663 	[ND_CMD_IMPLEMENTED] = { },
664 	[ND_CMD_ARS_CAP] = {
665 		.in_num = 2,
666 		.in_sizes = { 8, 8, },
667 		.out_num = 4,
668 		.out_sizes = { 4, 4, 4, 4, },
669 	},
670 	[ND_CMD_ARS_START] = {
671 		.in_num = 5,
672 		.in_sizes = { 8, 8, 2, 1, 5, },
673 		.out_num = 2,
674 		.out_sizes = { 4, 4, },
675 	},
676 	[ND_CMD_ARS_STATUS] = {
677 		.out_num = 3,
678 		.out_sizes = { 4, 4, UINT_MAX, },
679 	},
680 	[ND_CMD_CLEAR_ERROR] = {
681 		.in_num = 2,
682 		.in_sizes = { 8, 8, },
683 		.out_num = 3,
684 		.out_sizes = { 4, 4, 8, },
685 	},
686 	[ND_CMD_CALL] = {
687 		.in_num = 2,
688 		.in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
689 		.out_num = 1,
690 		.out_sizes = { UINT_MAX, },
691 	},
692 };
693 
694 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
695 {
696 	if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
697 		return &__nd_cmd_bus_descs[cmd];
698 	return NULL;
699 }
700 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
701 
702 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
703 		const struct nd_cmd_desc *desc, int idx, void *buf)
704 {
705 	if (idx >= desc->in_num)
706 		return UINT_MAX;
707 
708 	if (desc->in_sizes[idx] < UINT_MAX)
709 		return desc->in_sizes[idx];
710 
711 	if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
712 		struct nd_cmd_set_config_hdr *hdr = buf;
713 
714 		return hdr->in_length;
715 	} else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
716 		struct nd_cmd_vendor_hdr *hdr = buf;
717 
718 		return hdr->in_length;
719 	} else if (cmd == ND_CMD_CALL) {
720 		struct nd_cmd_pkg *pkg = buf;
721 
722 		return pkg->nd_size_in;
723 	}
724 
725 	return UINT_MAX;
726 }
727 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
728 
729 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
730 		const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
731 		const u32 *out_field, unsigned long remainder)
732 {
733 	if (idx >= desc->out_num)
734 		return UINT_MAX;
735 
736 	if (desc->out_sizes[idx] < UINT_MAX)
737 		return desc->out_sizes[idx];
738 
739 	if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
740 		return in_field[1];
741 	else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
742 		return out_field[1];
743 	else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
744 		/*
745 		 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
746 		 * "Size of Output Buffer in bytes, including this
747 		 * field."
748 		 */
749 		if (out_field[1] < 4)
750 			return 0;
751 		/*
752 		 * ACPI 6.1 is ambiguous if 'status' is included in the
753 		 * output size. If we encounter an output size that
754 		 * overshoots the remainder by 4 bytes, assume it was
755 		 * including 'status'.
756 		 */
757 		if (out_field[1] - 8 == remainder)
758 			return remainder;
759 		return out_field[1] - 4;
760 	} else if (cmd == ND_CMD_CALL) {
761 		struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
762 
763 		return pkg->nd_size_out;
764 	}
765 
766 
767 	return UINT_MAX;
768 }
769 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
770 
771 void wait_nvdimm_bus_probe_idle(struct device *dev)
772 {
773 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
774 
775 	do {
776 		if (nvdimm_bus->probe_active == 0)
777 			break;
778 		nvdimm_bus_unlock(&nvdimm_bus->dev);
779 		wait_event(nvdimm_bus->probe_wait,
780 				nvdimm_bus->probe_active == 0);
781 		nvdimm_bus_lock(&nvdimm_bus->dev);
782 	} while (true);
783 }
784 
785 static int nd_pmem_forget_poison_check(struct device *dev, void *data)
786 {
787 	struct nd_cmd_clear_error *clear_err =
788 		(struct nd_cmd_clear_error *)data;
789 	struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
790 	struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
791 	struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
792 	struct nd_namespace_common *ndns = NULL;
793 	struct nd_namespace_io *nsio;
794 	resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
795 
796 	if (nd_dax || !dev->driver)
797 		return 0;
798 
799 	start = clear_err->address;
800 	end = clear_err->address + clear_err->cleared - 1;
801 
802 	if (nd_btt || nd_pfn || nd_dax) {
803 		if (nd_btt)
804 			ndns = nd_btt->ndns;
805 		else if (nd_pfn)
806 			ndns = nd_pfn->ndns;
807 		else if (nd_dax)
808 			ndns = nd_dax->nd_pfn.ndns;
809 
810 		if (!ndns)
811 			return 0;
812 	} else
813 		ndns = to_ndns(dev);
814 
815 	nsio = to_nd_namespace_io(&ndns->dev);
816 	pstart = nsio->res.start + offset;
817 	pend = nsio->res.end - end_trunc;
818 
819 	if ((pstart >= start) && (pend <= end))
820 		return -EBUSY;
821 
822 	return 0;
823 
824 }
825 
826 static int nd_ns_forget_poison_check(struct device *dev, void *data)
827 {
828 	return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
829 }
830 
831 /* set_config requires an idle interleave set */
832 static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
833 		struct nvdimm *nvdimm, unsigned int cmd, void *data)
834 {
835 	struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
836 
837 	/* ask the bus provider if it would like to block this request */
838 	if (nd_desc->clear_to_send) {
839 		int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
840 
841 		if (rc)
842 			return rc;
843 	}
844 
845 	/* require clear error to go through the pmem driver */
846 	if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
847 		return device_for_each_child(&nvdimm_bus->dev, data,
848 				nd_ns_forget_poison_check);
849 
850 	if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
851 		return 0;
852 
853 	/* prevent label manipulation while the kernel owns label updates */
854 	wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
855 	if (atomic_read(&nvdimm->busy))
856 		return -EBUSY;
857 	return 0;
858 }
859 
860 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
861 		int read_only, unsigned int ioctl_cmd, unsigned long arg)
862 {
863 	struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
864 	size_t buf_len = 0, in_len = 0, out_len = 0;
865 	static char out_env[ND_CMD_MAX_ENVELOPE];
866 	static char in_env[ND_CMD_MAX_ENVELOPE];
867 	const struct nd_cmd_desc *desc = NULL;
868 	unsigned int cmd = _IOC_NR(ioctl_cmd);
869 	void __user *p = (void __user *) arg;
870 	struct device *dev = &nvdimm_bus->dev;
871 	struct nd_cmd_pkg pkg;
872 	const char *cmd_name, *dimm_name;
873 	unsigned long cmd_mask;
874 	void *buf;
875 	int rc, i, cmd_rc;
876 
877 	if (nvdimm) {
878 		desc = nd_cmd_dimm_desc(cmd);
879 		cmd_name = nvdimm_cmd_name(cmd);
880 		cmd_mask = nvdimm->cmd_mask;
881 		dimm_name = dev_name(&nvdimm->dev);
882 	} else {
883 		desc = nd_cmd_bus_desc(cmd);
884 		cmd_name = nvdimm_bus_cmd_name(cmd);
885 		cmd_mask = nd_desc->cmd_mask;
886 		dimm_name = "bus";
887 	}
888 
889 	if (cmd == ND_CMD_CALL) {
890 		if (copy_from_user(&pkg, p, sizeof(pkg)))
891 			return -EFAULT;
892 	}
893 
894 	if (!desc || (desc->out_num + desc->in_num == 0) ||
895 			!test_bit(cmd, &cmd_mask))
896 		return -ENOTTY;
897 
898 	/* fail write commands (when read-only) */
899 	if (read_only)
900 		switch (cmd) {
901 		case ND_CMD_VENDOR:
902 		case ND_CMD_SET_CONFIG_DATA:
903 		case ND_CMD_ARS_START:
904 		case ND_CMD_CLEAR_ERROR:
905 		case ND_CMD_CALL:
906 			dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
907 					nvdimm ? nvdimm_cmd_name(cmd)
908 					: nvdimm_bus_cmd_name(cmd));
909 			return -EPERM;
910 		default:
911 			break;
912 		}
913 
914 	/* process an input envelope */
915 	for (i = 0; i < desc->in_num; i++) {
916 		u32 in_size, copy;
917 
918 		in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
919 		if (in_size == UINT_MAX) {
920 			dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
921 					__func__, dimm_name, cmd_name, i);
922 			return -ENXIO;
923 		}
924 		if (in_len < sizeof(in_env))
925 			copy = min_t(u32, sizeof(in_env) - in_len, in_size);
926 		else
927 			copy = 0;
928 		if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
929 			return -EFAULT;
930 		in_len += in_size;
931 	}
932 
933 	if (cmd == ND_CMD_CALL) {
934 		dev_dbg(dev, "%s:%s, idx: %llu, in: %zu, out: %zu, len %zu\n",
935 				__func__, dimm_name, pkg.nd_command,
936 				in_len, out_len, buf_len);
937 
938 		for (i = 0; i < ARRAY_SIZE(pkg.nd_reserved2); i++)
939 			if (pkg.nd_reserved2[i])
940 				return -EINVAL;
941 	}
942 
943 	/* process an output envelope */
944 	for (i = 0; i < desc->out_num; i++) {
945 		u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
946 				(u32 *) in_env, (u32 *) out_env, 0);
947 		u32 copy;
948 
949 		if (out_size == UINT_MAX) {
950 			dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
951 					__func__, dimm_name, cmd_name, i);
952 			return -EFAULT;
953 		}
954 		if (out_len < sizeof(out_env))
955 			copy = min_t(u32, sizeof(out_env) - out_len, out_size);
956 		else
957 			copy = 0;
958 		if (copy && copy_from_user(&out_env[out_len],
959 					p + in_len + out_len, copy))
960 			return -EFAULT;
961 		out_len += out_size;
962 	}
963 
964 	buf_len = out_len + in_len;
965 	if (buf_len > ND_IOCTL_MAX_BUFLEN) {
966 		dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
967 				dimm_name, cmd_name, buf_len,
968 				ND_IOCTL_MAX_BUFLEN);
969 		return -EINVAL;
970 	}
971 
972 	buf = vmalloc(buf_len);
973 	if (!buf)
974 		return -ENOMEM;
975 
976 	if (copy_from_user(buf, p, buf_len)) {
977 		rc = -EFAULT;
978 		goto out;
979 	}
980 
981 	nvdimm_bus_lock(&nvdimm_bus->dev);
982 	rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, cmd, buf);
983 	if (rc)
984 		goto out_unlock;
985 
986 	rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
987 	if (rc < 0)
988 		goto out_unlock;
989 
990 	if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
991 		struct nd_cmd_clear_error *clear_err = buf;
992 		struct resource res;
993 
994 		if (clear_err->cleared) {
995 			/* clearing the poison list we keep track of */
996 			nvdimm_forget_poison(nvdimm_bus, clear_err->address,
997 					clear_err->cleared);
998 
999 			/* now sync the badblocks lists */
1000 			res.start = clear_err->address;
1001 			res.end = clear_err->address + clear_err->cleared - 1;
1002 			__nvdimm_bus_badblocks_clear(nvdimm_bus, &res);
1003 		}
1004 	}
1005 	nvdimm_bus_unlock(&nvdimm_bus->dev);
1006 
1007 	if (copy_to_user(p, buf, buf_len))
1008 		rc = -EFAULT;
1009 
1010 	vfree(buf);
1011 	return rc;
1012 
1013  out_unlock:
1014 	nvdimm_bus_unlock(&nvdimm_bus->dev);
1015  out:
1016 	vfree(buf);
1017 	return rc;
1018 }
1019 
1020 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1021 {
1022 	long id = (long) file->private_data;
1023 	int rc = -ENXIO, ro;
1024 	struct nvdimm_bus *nvdimm_bus;
1025 
1026 	ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1027 	mutex_lock(&nvdimm_bus_list_mutex);
1028 	list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1029 		if (nvdimm_bus->id == id) {
1030 			rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
1031 			break;
1032 		}
1033 	}
1034 	mutex_unlock(&nvdimm_bus_list_mutex);
1035 
1036 	return rc;
1037 }
1038 
1039 static int match_dimm(struct device *dev, void *data)
1040 {
1041 	long id = (long) data;
1042 
1043 	if (is_nvdimm(dev)) {
1044 		struct nvdimm *nvdimm = to_nvdimm(dev);
1045 
1046 		return nvdimm->id == id;
1047 	}
1048 
1049 	return 0;
1050 }
1051 
1052 static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1053 {
1054 	int rc = -ENXIO, ro;
1055 	struct nvdimm_bus *nvdimm_bus;
1056 
1057 	ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1058 	mutex_lock(&nvdimm_bus_list_mutex);
1059 	list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1060 		struct device *dev = device_find_child(&nvdimm_bus->dev,
1061 				file->private_data, match_dimm);
1062 		struct nvdimm *nvdimm;
1063 
1064 		if (!dev)
1065 			continue;
1066 
1067 		nvdimm = to_nvdimm(dev);
1068 		rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1069 		put_device(dev);
1070 		break;
1071 	}
1072 	mutex_unlock(&nvdimm_bus_list_mutex);
1073 
1074 	return rc;
1075 }
1076 
1077 static int nd_open(struct inode *inode, struct file *file)
1078 {
1079 	long minor = iminor(inode);
1080 
1081 	file->private_data = (void *) minor;
1082 	return 0;
1083 }
1084 
1085 static const struct file_operations nvdimm_bus_fops = {
1086 	.owner = THIS_MODULE,
1087 	.open = nd_open,
1088 	.unlocked_ioctl = nd_ioctl,
1089 	.compat_ioctl = nd_ioctl,
1090 	.llseek = noop_llseek,
1091 };
1092 
1093 static const struct file_operations nvdimm_fops = {
1094 	.owner = THIS_MODULE,
1095 	.open = nd_open,
1096 	.unlocked_ioctl = nvdimm_ioctl,
1097 	.compat_ioctl = nvdimm_ioctl,
1098 	.llseek = noop_llseek,
1099 };
1100 
1101 int __init nvdimm_bus_init(void)
1102 {
1103 	int rc;
1104 
1105 	BUILD_BUG_ON(sizeof(struct nd_smart_payload) != 128);
1106 	BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload) != 8);
1107 
1108 	rc = bus_register(&nvdimm_bus_type);
1109 	if (rc)
1110 		return rc;
1111 
1112 	rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1113 	if (rc < 0)
1114 		goto err_bus_chrdev;
1115 	nvdimm_bus_major = rc;
1116 
1117 	rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1118 	if (rc < 0)
1119 		goto err_dimm_chrdev;
1120 	nvdimm_major = rc;
1121 
1122 	nd_class = class_create(THIS_MODULE, "nd");
1123 	if (IS_ERR(nd_class)) {
1124 		rc = PTR_ERR(nd_class);
1125 		goto err_class;
1126 	}
1127 
1128 	rc = driver_register(&nd_bus_driver.drv);
1129 	if (rc)
1130 		goto err_nd_bus;
1131 
1132 	return 0;
1133 
1134  err_nd_bus:
1135 	class_destroy(nd_class);
1136  err_class:
1137 	unregister_chrdev(nvdimm_major, "dimmctl");
1138  err_dimm_chrdev:
1139 	unregister_chrdev(nvdimm_bus_major, "ndctl");
1140  err_bus_chrdev:
1141 	bus_unregister(&nvdimm_bus_type);
1142 
1143 	return rc;
1144 }
1145 
1146 void nvdimm_bus_exit(void)
1147 {
1148 	driver_unregister(&nd_bus_driver.drv);
1149 	class_destroy(nd_class);
1150 	unregister_chrdev(nvdimm_bus_major, "ndctl");
1151 	unregister_chrdev(nvdimm_major, "dimmctl");
1152 	bus_unregister(&nvdimm_bus_type);
1153 	ida_destroy(&nd_ida);
1154 }
1155