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