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