2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
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>
32 static int nvdimm_bus_major
;
33 static struct class *nd_class
;
35 static int to_nd_device_type(struct device
*dev
)
38 return ND_DEVICE_DIMM
;
39 else if (is_nd_pmem(dev
))
40 return ND_DEVICE_REGION_PMEM
;
41 else if (is_nd_blk(dev
))
42 return ND_DEVICE_REGION_BLK
;
43 else if (is_nd_pmem(dev
->parent
) || is_nd_blk(dev
->parent
))
44 return nd_region_to_nstype(to_nd_region(dev
->parent
));
49 static int nvdimm_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
51 return add_uevent_var(env
, "MODALIAS=" ND_DEVICE_MODALIAS_FMT
,
52 to_nd_device_type(dev
));
55 static int nvdimm_bus_match(struct device
*dev
, struct device_driver
*drv
)
57 struct nd_device_driver
*nd_drv
= to_nd_device_driver(drv
);
59 return test_bit(to_nd_device_type(dev
), &nd_drv
->type
);
62 static struct module
*to_bus_provider(struct device
*dev
)
64 /* pin bus providers while regions are enabled */
65 if (is_nd_pmem(dev
) || is_nd_blk(dev
)) {
66 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
68 return nvdimm_bus
->module
;
73 static void nvdimm_bus_probe_start(struct nvdimm_bus
*nvdimm_bus
)
75 nvdimm_bus_lock(&nvdimm_bus
->dev
);
76 nvdimm_bus
->probe_active
++;
77 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
80 static void nvdimm_bus_probe_end(struct nvdimm_bus
*nvdimm_bus
)
82 nvdimm_bus_lock(&nvdimm_bus
->dev
);
83 if (--nvdimm_bus
->probe_active
== 0)
84 wake_up(&nvdimm_bus
->probe_wait
);
85 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
88 static int nvdimm_bus_probe(struct device
*dev
)
90 struct nd_device_driver
*nd_drv
= to_nd_device_driver(dev
->driver
);
91 struct module
*provider
= to_bus_provider(dev
);
92 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
95 if (!try_module_get(provider
))
98 nvdimm_bus_probe_start(nvdimm_bus
);
99 rc
= nd_drv
->probe(dev
);
101 nd_region_probe_success(nvdimm_bus
, dev
);
103 nd_region_disable(nvdimm_bus
, dev
);
104 nvdimm_bus_probe_end(nvdimm_bus
);
106 dev_dbg(&nvdimm_bus
->dev
, "%s.probe(%s) = %d\n", dev
->driver
->name
,
110 module_put(provider
);
114 static int nvdimm_bus_remove(struct device
*dev
)
116 struct nd_device_driver
*nd_drv
= to_nd_device_driver(dev
->driver
);
117 struct module
*provider
= to_bus_provider(dev
);
118 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
121 rc
= nd_drv
->remove(dev
);
122 nd_region_disable(nvdimm_bus
, dev
);
124 dev_dbg(&nvdimm_bus
->dev
, "%s.remove(%s) = %d\n", dev
->driver
->name
,
126 module_put(provider
);
130 static struct bus_type nvdimm_bus_type
= {
132 .uevent
= nvdimm_bus_uevent
,
133 .match
= nvdimm_bus_match
,
134 .probe
= nvdimm_bus_probe
,
135 .remove
= nvdimm_bus_remove
,
138 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain
);
140 void nd_synchronize(void)
142 async_synchronize_full_domain(&nd_async_domain
);
144 EXPORT_SYMBOL_GPL(nd_synchronize
);
146 static void nd_async_device_register(void *d
, async_cookie_t cookie
)
148 struct device
*dev
= d
;
150 if (device_add(dev
) != 0) {
151 dev_err(dev
, "%s: failed\n", __func__
);
157 static void nd_async_device_unregister(void *d
, async_cookie_t cookie
)
159 struct device
*dev
= d
;
161 /* flush bus operations before delete */
162 nvdimm_bus_lock(dev
);
163 nvdimm_bus_unlock(dev
);
165 device_unregister(dev
);
169 void __nd_device_register(struct device
*dev
)
171 dev
->bus
= &nvdimm_bus_type
;
173 async_schedule_domain(nd_async_device_register
, dev
,
177 void nd_device_register(struct device
*dev
)
179 device_initialize(dev
);
180 __nd_device_register(dev
);
182 EXPORT_SYMBOL(nd_device_register
);
184 void nd_device_unregister(struct device
*dev
, enum nd_async_mode mode
)
189 async_schedule_domain(nd_async_device_unregister
, dev
,
194 device_unregister(dev
);
198 EXPORT_SYMBOL(nd_device_unregister
);
201 * __nd_driver_register() - register a region or a namespace driver
202 * @nd_drv: driver to register
203 * @owner: automatically set by nd_driver_register() macro
204 * @mod_name: automatically set by nd_driver_register() macro
206 int __nd_driver_register(struct nd_device_driver
*nd_drv
, struct module
*owner
,
207 const char *mod_name
)
209 struct device_driver
*drv
= &nd_drv
->drv
;
212 pr_debug("driver type bitmask not set (%pf)\n",
213 __builtin_return_address(0));
217 if (!nd_drv
->probe
|| !nd_drv
->remove
) {
218 pr_debug("->probe() and ->remove() must be specified\n");
222 drv
->bus
= &nvdimm_bus_type
;
224 drv
->mod_name
= mod_name
;
226 return driver_register(drv
);
228 EXPORT_SYMBOL(__nd_driver_register
);
230 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
233 return sprintf(buf
, ND_DEVICE_MODALIAS_FMT
"\n",
234 to_nd_device_type(dev
));
236 static DEVICE_ATTR_RO(modalias
);
238 static ssize_t
devtype_show(struct device
*dev
, struct device_attribute
*attr
,
241 return sprintf(buf
, "%s\n", dev
->type
->name
);
243 static DEVICE_ATTR_RO(devtype
);
245 static struct attribute
*nd_device_attributes
[] = {
246 &dev_attr_modalias
.attr
,
247 &dev_attr_devtype
.attr
,
252 * nd_device_attribute_group - generic attributes for all devices on an nd bus
254 struct attribute_group nd_device_attribute_group
= {
255 .attrs
= nd_device_attributes
,
257 EXPORT_SYMBOL_GPL(nd_device_attribute_group
);
259 int nvdimm_bus_create_ndctl(struct nvdimm_bus
*nvdimm_bus
)
261 dev_t devt
= MKDEV(nvdimm_bus_major
, nvdimm_bus
->id
);
264 dev
= device_create(nd_class
, &nvdimm_bus
->dev
, devt
, nvdimm_bus
,
265 "ndctl%d", nvdimm_bus
->id
);
268 dev_dbg(&nvdimm_bus
->dev
, "failed to register ndctl%d: %ld\n",
269 nvdimm_bus
->id
, PTR_ERR(dev
));
275 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus
*nvdimm_bus
)
277 device_destroy(nd_class
, MKDEV(nvdimm_bus_major
, nvdimm_bus
->id
));
280 static const struct nd_cmd_desc __nd_cmd_dimm_descs
[] = {
281 [ND_CMD_IMPLEMENTED
] = { },
284 .out_sizes
= { 4, 8, },
286 [ND_CMD_SMART_THRESHOLD
] = {
288 .out_sizes
= { 4, 8, },
290 [ND_CMD_DIMM_FLAGS
] = {
292 .out_sizes
= { 4, 4 },
294 [ND_CMD_GET_CONFIG_SIZE
] = {
296 .out_sizes
= { 4, 4, 4, },
298 [ND_CMD_GET_CONFIG_DATA
] = {
300 .in_sizes
= { 4, 4, },
302 .out_sizes
= { 4, UINT_MAX
, },
304 [ND_CMD_SET_CONFIG_DATA
] = {
306 .in_sizes
= { 4, 4, UINT_MAX
, },
312 .in_sizes
= { 4, 4, UINT_MAX
, },
314 .out_sizes
= { 4, 4, UINT_MAX
, },
318 const struct nd_cmd_desc
*nd_cmd_dimm_desc(int cmd
)
320 if (cmd
< ARRAY_SIZE(__nd_cmd_dimm_descs
))
321 return &__nd_cmd_dimm_descs
[cmd
];
324 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc
);
326 static const struct nd_cmd_desc __nd_cmd_bus_descs
[] = {
327 [ND_CMD_IMPLEMENTED
] = { },
330 .in_sizes
= { 8, 8, },
332 .out_sizes
= { 4, 4, },
334 [ND_CMD_ARS_START
] = {
336 .in_sizes
= { 8, 8, 2, 6, },
340 [ND_CMD_ARS_STATUS
] = {
342 .out_sizes
= { 4, UINT_MAX
, },
346 const struct nd_cmd_desc
*nd_cmd_bus_desc(int cmd
)
348 if (cmd
< ARRAY_SIZE(__nd_cmd_bus_descs
))
349 return &__nd_cmd_bus_descs
[cmd
];
352 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc
);
354 u32
nd_cmd_in_size(struct nvdimm
*nvdimm
, int cmd
,
355 const struct nd_cmd_desc
*desc
, int idx
, void *buf
)
357 if (idx
>= desc
->in_num
)
360 if (desc
->in_sizes
[idx
] < UINT_MAX
)
361 return desc
->in_sizes
[idx
];
363 if (nvdimm
&& cmd
== ND_CMD_SET_CONFIG_DATA
&& idx
== 2) {
364 struct nd_cmd_set_config_hdr
*hdr
= buf
;
366 return hdr
->in_length
;
367 } else if (nvdimm
&& cmd
== ND_CMD_VENDOR
&& idx
== 2) {
368 struct nd_cmd_vendor_hdr
*hdr
= buf
;
370 return hdr
->in_length
;
375 EXPORT_SYMBOL_GPL(nd_cmd_in_size
);
377 u32
nd_cmd_out_size(struct nvdimm
*nvdimm
, int cmd
,
378 const struct nd_cmd_desc
*desc
, int idx
, const u32
*in_field
,
379 const u32
*out_field
)
381 if (idx
>= desc
->out_num
)
384 if (desc
->out_sizes
[idx
] < UINT_MAX
)
385 return desc
->out_sizes
[idx
];
387 if (nvdimm
&& cmd
== ND_CMD_GET_CONFIG_DATA
&& idx
== 1)
389 else if (nvdimm
&& cmd
== ND_CMD_VENDOR
&& idx
== 2)
391 else if (!nvdimm
&& cmd
== ND_CMD_ARS_STATUS
&& idx
== 1)
392 return ND_CMD_ARS_STATUS_MAX
;
396 EXPORT_SYMBOL_GPL(nd_cmd_out_size
);
398 void wait_nvdimm_bus_probe_idle(struct device
*dev
)
400 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
403 if (nvdimm_bus
->probe_active
== 0)
405 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
406 wait_event(nvdimm_bus
->probe_wait
,
407 nvdimm_bus
->probe_active
== 0);
408 nvdimm_bus_lock(&nvdimm_bus
->dev
);
412 /* set_config requires an idle interleave set */
413 static int nd_cmd_clear_to_send(struct nvdimm
*nvdimm
, unsigned int cmd
)
415 struct nvdimm_bus
*nvdimm_bus
;
417 if (!nvdimm
|| cmd
!= ND_CMD_SET_CONFIG_DATA
)
420 nvdimm_bus
= walk_to_nvdimm_bus(&nvdimm
->dev
);
421 wait_nvdimm_bus_probe_idle(&nvdimm_bus
->dev
);
423 if (atomic_read(&nvdimm
->busy
))
428 static int __nd_ioctl(struct nvdimm_bus
*nvdimm_bus
, struct nvdimm
*nvdimm
,
429 int read_only
, unsigned int ioctl_cmd
, unsigned long arg
)
431 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
432 size_t buf_len
= 0, in_len
= 0, out_len
= 0;
433 static char out_env
[ND_CMD_MAX_ENVELOPE
];
434 static char in_env
[ND_CMD_MAX_ENVELOPE
];
435 const struct nd_cmd_desc
*desc
= NULL
;
436 unsigned int cmd
= _IOC_NR(ioctl_cmd
);
437 void __user
*p
= (void __user
*) arg
;
438 struct device
*dev
= &nvdimm_bus
->dev
;
439 const char *cmd_name
, *dimm_name
;
440 unsigned long dsm_mask
;
445 desc
= nd_cmd_dimm_desc(cmd
);
446 cmd_name
= nvdimm_cmd_name(cmd
);
447 dsm_mask
= nvdimm
->dsm_mask
? *(nvdimm
->dsm_mask
) : 0;
448 dimm_name
= dev_name(&nvdimm
->dev
);
450 desc
= nd_cmd_bus_desc(cmd
);
451 cmd_name
= nvdimm_bus_cmd_name(cmd
);
452 dsm_mask
= nd_desc
->dsm_mask
;
456 if (!desc
|| (desc
->out_num
+ desc
->in_num
== 0) ||
457 !test_bit(cmd
, &dsm_mask
))
460 /* fail write commands (when read-only) */
463 case ND_IOCTL_VENDOR
:
464 case ND_IOCTL_SET_CONFIG_DATA
:
465 case ND_IOCTL_ARS_START
:
466 dev_dbg(&nvdimm_bus
->dev
, "'%s' command while read-only.\n",
467 nvdimm
? nvdimm_cmd_name(cmd
)
468 : nvdimm_bus_cmd_name(cmd
));
474 /* process an input envelope */
475 for (i
= 0; i
< desc
->in_num
; i
++) {
478 in_size
= nd_cmd_in_size(nvdimm
, cmd
, desc
, i
, in_env
);
479 if (in_size
== UINT_MAX
) {
480 dev_err(dev
, "%s:%s unknown input size cmd: %s field: %d\n",
481 __func__
, dimm_name
, cmd_name
, i
);
484 if (!access_ok(VERIFY_READ
, p
+ in_len
, in_size
))
486 if (in_len
< sizeof(in_env
))
487 copy
= min_t(u32
, sizeof(in_env
) - in_len
, in_size
);
490 if (copy
&& copy_from_user(&in_env
[in_len
], p
+ in_len
, copy
))
495 /* process an output envelope */
496 for (i
= 0; i
< desc
->out_num
; i
++) {
497 u32 out_size
= nd_cmd_out_size(nvdimm
, cmd
, desc
, i
,
498 (u32
*) in_env
, (u32
*) out_env
);
501 if (out_size
== UINT_MAX
) {
502 dev_dbg(dev
, "%s:%s unknown output size cmd: %s field: %d\n",
503 __func__
, dimm_name
, cmd_name
, i
);
506 if (!access_ok(VERIFY_WRITE
, p
+ in_len
+ out_len
, out_size
))
508 if (out_len
< sizeof(out_env
))
509 copy
= min_t(u32
, sizeof(out_env
) - out_len
, out_size
);
512 if (copy
&& copy_from_user(&out_env
[out_len
],
513 p
+ in_len
+ out_len
, copy
))
518 buf_len
= out_len
+ in_len
;
519 if (!access_ok(VERIFY_WRITE
, p
, sizeof(buf_len
)))
522 if (buf_len
> ND_IOCTL_MAX_BUFLEN
) {
523 dev_dbg(dev
, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__
,
524 dimm_name
, cmd_name
, buf_len
,
525 ND_IOCTL_MAX_BUFLEN
);
529 buf
= vmalloc(buf_len
);
533 if (copy_from_user(buf
, p
, buf_len
)) {
538 nvdimm_bus_lock(&nvdimm_bus
->dev
);
539 rc
= nd_cmd_clear_to_send(nvdimm
, cmd
);
543 rc
= nd_desc
->ndctl(nd_desc
, nvdimm
, cmd
, buf
, buf_len
);
546 if (copy_to_user(p
, buf
, buf_len
))
549 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
555 static long nd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
557 long id
= (long) file
->private_data
;
558 int rc
= -ENXIO
, read_only
;
559 struct nvdimm_bus
*nvdimm_bus
;
561 read_only
= (O_RDWR
!= (file
->f_flags
& O_ACCMODE
));
562 mutex_lock(&nvdimm_bus_list_mutex
);
563 list_for_each_entry(nvdimm_bus
, &nvdimm_bus_list
, list
) {
564 if (nvdimm_bus
->id
== id
) {
565 rc
= __nd_ioctl(nvdimm_bus
, NULL
, read_only
, cmd
, arg
);
569 mutex_unlock(&nvdimm_bus_list_mutex
);
574 static int match_dimm(struct device
*dev
, void *data
)
576 long id
= (long) data
;
578 if (is_nvdimm(dev
)) {
579 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
581 return nvdimm
->id
== id
;
587 static long nvdimm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
589 int rc
= -ENXIO
, read_only
;
590 struct nvdimm_bus
*nvdimm_bus
;
592 read_only
= (O_RDWR
!= (file
->f_flags
& O_ACCMODE
));
593 mutex_lock(&nvdimm_bus_list_mutex
);
594 list_for_each_entry(nvdimm_bus
, &nvdimm_bus_list
, list
) {
595 struct device
*dev
= device_find_child(&nvdimm_bus
->dev
,
596 file
->private_data
, match_dimm
);
597 struct nvdimm
*nvdimm
;
602 nvdimm
= to_nvdimm(dev
);
603 rc
= __nd_ioctl(nvdimm_bus
, nvdimm
, read_only
, cmd
, arg
);
607 mutex_unlock(&nvdimm_bus_list_mutex
);
612 static int nd_open(struct inode
*inode
, struct file
*file
)
614 long minor
= iminor(inode
);
616 file
->private_data
= (void *) minor
;
620 static const struct file_operations nvdimm_bus_fops
= {
621 .owner
= THIS_MODULE
,
623 .unlocked_ioctl
= nd_ioctl
,
624 .compat_ioctl
= nd_ioctl
,
625 .llseek
= noop_llseek
,
628 static const struct file_operations nvdimm_fops
= {
629 .owner
= THIS_MODULE
,
631 .unlocked_ioctl
= nvdimm_ioctl
,
632 .compat_ioctl
= nvdimm_ioctl
,
633 .llseek
= noop_llseek
,
636 int __init
nvdimm_bus_init(void)
640 rc
= bus_register(&nvdimm_bus_type
);
644 rc
= register_chrdev(0, "ndctl", &nvdimm_bus_fops
);
647 nvdimm_bus_major
= rc
;
649 rc
= register_chrdev(0, "dimmctl", &nvdimm_fops
);
651 goto err_dimm_chrdev
;
654 nd_class
= class_create(THIS_MODULE
, "nd");
655 if (IS_ERR(nd_class
))
661 unregister_chrdev(nvdimm_major
, "dimmctl");
663 unregister_chrdev(nvdimm_bus_major
, "ndctl");
665 bus_unregister(&nvdimm_bus_type
);
670 void nvdimm_bus_exit(void)
672 class_destroy(nd_class
);
673 unregister_chrdev(nvdimm_bus_major
, "ndctl");
674 unregister_chrdev(nvdimm_major
, "dimmctl");
675 bus_unregister(&nvdimm_bus_type
);