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