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
;
34 static DEFINE_IDA(nd_ida
);
36 static int to_nd_device_type(struct device
*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
));
52 static int nvdimm_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
55 * Ensure that region devices always have their numa node set as
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
));
64 static struct module
*to_bus_provider(struct device
*dev
)
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
);
70 return nvdimm_bus
->nd_desc
->module
;
75 static void nvdimm_bus_probe_start(struct nvdimm_bus
*nvdimm_bus
)
77 nvdimm_bus_lock(&nvdimm_bus
->dev
);
78 nvdimm_bus
->probe_active
++;
79 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
82 static void nvdimm_bus_probe_end(struct nvdimm_bus
*nvdimm_bus
)
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
);
90 static int nvdimm_bus_probe(struct device
*dev
)
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
);
97 if (!try_module_get(provider
))
100 nvdimm_bus_probe_start(nvdimm_bus
);
101 rc
= nd_drv
->probe(dev
);
103 nd_region_probe_success(nvdimm_bus
, dev
);
105 nd_region_disable(nvdimm_bus
, dev
);
106 nvdimm_bus_probe_end(nvdimm_bus
);
108 dev_dbg(&nvdimm_bus
->dev
, "%s.probe(%s) = %d\n", dev
->driver
->name
,
112 module_put(provider
);
116 static int nvdimm_bus_remove(struct device
*dev
)
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
);
124 rc
= nd_drv
->remove(dev
);
125 nd_region_disable(nvdimm_bus
, dev
);
127 dev_dbg(&nvdimm_bus
->dev
, "%s.remove(%s) = %d\n", dev
->driver
->name
,
129 module_put(provider
);
133 static void nvdimm_bus_shutdown(struct device
*dev
)
135 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
136 struct nd_device_driver
*nd_drv
= NULL
;
139 nd_drv
= to_nd_device_driver(dev
->driver
);
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
));
148 void nd_device_notify(struct device
*dev
, enum nvdimm_event event
)
152 struct nd_device_driver
*nd_drv
;
154 nd_drv
= to_nd_device_driver(dev
->driver
);
156 nd_drv
->notify(dev
, event
);
160 EXPORT_SYMBOL(nd_device_notify
);
162 void nvdimm_region_notify(struct nd_region
*nd_region
, enum nvdimm_event event
)
164 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
169 /* caller is responsible for holding a reference on the device */
170 nd_device_notify(&nd_region
->dev
, event
);
172 EXPORT_SYMBOL_GPL(nvdimm_region_notify
);
174 long nvdimm_clear_poison(struct device
*dev
, phys_addr_t phys
,
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
;
187 nd_desc
= nvdimm_bus
->nd_desc
;
189 * if ndctl does not exist, it's PMEM_LEGACY and
190 * we want to just pretend everything is handled.
195 memset(&ars_cap
, 0, sizeof(ars_cap
));
196 ars_cap
.address
= phys
;
197 ars_cap
.length
= len
;
198 rc
= nd_desc
->ndctl(nd_desc
, NULL
, ND_CMD_ARS_CAP
, &ars_cap
,
199 sizeof(ars_cap
), &cmd_rc
);
204 clear_err_unit
= ars_cap
.clear_err_unit
;
205 if (!clear_err_unit
|| !is_power_of_2(clear_err_unit
))
208 mask
= clear_err_unit
- 1;
209 if ((phys
| len
) & mask
)
211 memset(&clear_err
, 0, sizeof(clear_err
));
212 clear_err
.address
= phys
;
213 clear_err
.length
= len
;
214 rc
= nd_desc
->ndctl(nd_desc
, NULL
, ND_CMD_CLEAR_ERROR
, &clear_err
,
215 sizeof(clear_err
), &cmd_rc
);
221 nvdimm_clear_from_poison_list(nvdimm_bus
, phys
, len
);
222 return clear_err
.cleared
;
224 EXPORT_SYMBOL_GPL(nvdimm_clear_poison
);
226 static int nvdimm_bus_match(struct device
*dev
, struct device_driver
*drv
);
228 static struct bus_type nvdimm_bus_type
= {
230 .uevent
= nvdimm_bus_uevent
,
231 .match
= nvdimm_bus_match
,
232 .probe
= nvdimm_bus_probe
,
233 .remove
= nvdimm_bus_remove
,
234 .shutdown
= nvdimm_bus_shutdown
,
237 static void nvdimm_bus_release(struct device
*dev
)
239 struct nvdimm_bus
*nvdimm_bus
;
241 nvdimm_bus
= container_of(dev
, struct nvdimm_bus
, dev
);
242 ida_simple_remove(&nd_ida
, nvdimm_bus
->id
);
246 static bool is_nvdimm_bus(struct device
*dev
)
248 return dev
->release
== nvdimm_bus_release
;
251 struct nvdimm_bus
*walk_to_nvdimm_bus(struct device
*nd_dev
)
255 for (dev
= nd_dev
; dev
; dev
= dev
->parent
)
256 if (is_nvdimm_bus(dev
))
258 dev_WARN_ONCE(nd_dev
, !dev
, "invalid dev, not on nd bus\n");
260 return to_nvdimm_bus(dev
);
264 struct nvdimm_bus
*to_nvdimm_bus(struct device
*dev
)
266 struct nvdimm_bus
*nvdimm_bus
;
268 nvdimm_bus
= container_of(dev
, struct nvdimm_bus
, dev
);
269 WARN_ON(!is_nvdimm_bus(dev
));
272 EXPORT_SYMBOL_GPL(to_nvdimm_bus
);
274 struct nvdimm_bus
*nvdimm_bus_register(struct device
*parent
,
275 struct nvdimm_bus_descriptor
*nd_desc
)
277 struct nvdimm_bus
*nvdimm_bus
;
280 nvdimm_bus
= kzalloc(sizeof(*nvdimm_bus
), GFP_KERNEL
);
283 INIT_LIST_HEAD(&nvdimm_bus
->list
);
284 INIT_LIST_HEAD(&nvdimm_bus
->mapping_list
);
285 INIT_LIST_HEAD(&nvdimm_bus
->poison_list
);
286 init_waitqueue_head(&nvdimm_bus
->probe_wait
);
287 nvdimm_bus
->id
= ida_simple_get(&nd_ida
, 0, 0, GFP_KERNEL
);
288 mutex_init(&nvdimm_bus
->reconfig_mutex
);
289 if (nvdimm_bus
->id
< 0) {
293 nvdimm_bus
->nd_desc
= nd_desc
;
294 nvdimm_bus
->dev
.parent
= parent
;
295 nvdimm_bus
->dev
.release
= nvdimm_bus_release
;
296 nvdimm_bus
->dev
.groups
= nd_desc
->attr_groups
;
297 nvdimm_bus
->dev
.bus
= &nvdimm_bus_type
;
298 dev_set_name(&nvdimm_bus
->dev
, "ndbus%d", nvdimm_bus
->id
);
299 rc
= device_register(&nvdimm_bus
->dev
);
301 dev_dbg(&nvdimm_bus
->dev
, "registration failed: %d\n", rc
);
307 put_device(&nvdimm_bus
->dev
);
310 EXPORT_SYMBOL_GPL(nvdimm_bus_register
);
312 void nvdimm_bus_unregister(struct nvdimm_bus
*nvdimm_bus
)
316 device_unregister(&nvdimm_bus
->dev
);
318 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister
);
320 static int child_unregister(struct device
*dev
, void *data
)
323 * the singular ndctl class device per bus needs to be
324 * "device_destroy"ed, so skip it here
326 * i.e. remove classless children
331 nd_device_unregister(dev
, ND_SYNC
);
335 static void free_poison_list(struct list_head
*poison_list
)
337 struct nd_poison
*pl
, *next
;
339 list_for_each_entry_safe(pl
, next
, poison_list
, list
) {
343 list_del_init(poison_list
);
346 static int nd_bus_remove(struct device
*dev
)
348 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
350 mutex_lock(&nvdimm_bus_list_mutex
);
351 list_del_init(&nvdimm_bus
->list
);
352 mutex_unlock(&nvdimm_bus_list_mutex
);
355 device_for_each_child(&nvdimm_bus
->dev
, NULL
, child_unregister
);
357 nvdimm_bus_lock(&nvdimm_bus
->dev
);
358 free_poison_list(&nvdimm_bus
->poison_list
);
359 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
361 nvdimm_bus_destroy_ndctl(nvdimm_bus
);
366 static int nd_bus_probe(struct device
*dev
)
368 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
371 rc
= nvdimm_bus_create_ndctl(nvdimm_bus
);
375 mutex_lock(&nvdimm_bus_list_mutex
);
376 list_add_tail(&nvdimm_bus
->list
, &nvdimm_bus_list
);
377 mutex_unlock(&nvdimm_bus_list_mutex
);
379 /* enable bus provider attributes to look up their local context */
380 dev_set_drvdata(dev
, nvdimm_bus
->nd_desc
);
385 static struct nd_device_driver nd_bus_driver
= {
386 .probe
= nd_bus_probe
,
387 .remove
= nd_bus_remove
,
390 .suppress_bind_attrs
= true,
391 .bus
= &nvdimm_bus_type
,
392 .owner
= THIS_MODULE
,
393 .mod_name
= KBUILD_MODNAME
,
397 static int nvdimm_bus_match(struct device
*dev
, struct device_driver
*drv
)
399 struct nd_device_driver
*nd_drv
= to_nd_device_driver(drv
);
401 if (is_nvdimm_bus(dev
) && nd_drv
== &nd_bus_driver
)
404 return !!test_bit(to_nd_device_type(dev
), &nd_drv
->type
);
407 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain
);
409 void nd_synchronize(void)
411 async_synchronize_full_domain(&nd_async_domain
);
413 EXPORT_SYMBOL_GPL(nd_synchronize
);
415 static void nd_async_device_register(void *d
, async_cookie_t cookie
)
417 struct device
*dev
= d
;
419 if (device_add(dev
) != 0) {
420 dev_err(dev
, "%s: failed\n", __func__
);
426 static void nd_async_device_unregister(void *d
, async_cookie_t cookie
)
428 struct device
*dev
= d
;
430 /* flush bus operations before delete */
431 nvdimm_bus_lock(dev
);
432 nvdimm_bus_unlock(dev
);
434 device_unregister(dev
);
438 void __nd_device_register(struct device
*dev
)
442 dev
->bus
= &nvdimm_bus_type
;
444 async_schedule_domain(nd_async_device_register
, dev
,
448 void nd_device_register(struct device
*dev
)
450 device_initialize(dev
);
451 __nd_device_register(dev
);
453 EXPORT_SYMBOL(nd_device_register
);
455 void nd_device_unregister(struct device
*dev
, enum nd_async_mode mode
)
460 async_schedule_domain(nd_async_device_unregister
, dev
,
465 device_unregister(dev
);
469 EXPORT_SYMBOL(nd_device_unregister
);
472 * __nd_driver_register() - register a region or a namespace driver
473 * @nd_drv: driver to register
474 * @owner: automatically set by nd_driver_register() macro
475 * @mod_name: automatically set by nd_driver_register() macro
477 int __nd_driver_register(struct nd_device_driver
*nd_drv
, struct module
*owner
,
478 const char *mod_name
)
480 struct device_driver
*drv
= &nd_drv
->drv
;
483 pr_debug("driver type bitmask not set (%pf)\n",
484 __builtin_return_address(0));
488 if (!nd_drv
->probe
) {
489 pr_debug("%s ->probe() must be specified\n", mod_name
);
493 drv
->bus
= &nvdimm_bus_type
;
495 drv
->mod_name
= mod_name
;
497 return driver_register(drv
);
499 EXPORT_SYMBOL(__nd_driver_register
);
501 int nvdimm_revalidate_disk(struct gendisk
*disk
)
503 struct device
*dev
= disk_to_dev(disk
)->parent
;
504 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
505 const char *pol
= nd_region
->ro
? "only" : "write";
507 if (nd_region
->ro
== get_disk_ro(disk
))
510 dev_info(dev
, "%s read-%s, marking %s read-%s\n",
511 dev_name(&nd_region
->dev
), pol
, disk
->disk_name
, pol
);
512 set_disk_ro(disk
, nd_region
->ro
);
517 EXPORT_SYMBOL(nvdimm_revalidate_disk
);
519 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
522 return sprintf(buf
, ND_DEVICE_MODALIAS_FMT
"\n",
523 to_nd_device_type(dev
));
525 static DEVICE_ATTR_RO(modalias
);
527 static ssize_t
devtype_show(struct device
*dev
, struct device_attribute
*attr
,
530 return sprintf(buf
, "%s\n", dev
->type
->name
);
532 static DEVICE_ATTR_RO(devtype
);
534 static struct attribute
*nd_device_attributes
[] = {
535 &dev_attr_modalias
.attr
,
536 &dev_attr_devtype
.attr
,
541 * nd_device_attribute_group - generic attributes for all devices on an nd bus
543 struct attribute_group nd_device_attribute_group
= {
544 .attrs
= nd_device_attributes
,
546 EXPORT_SYMBOL_GPL(nd_device_attribute_group
);
548 static ssize_t
numa_node_show(struct device
*dev
,
549 struct device_attribute
*attr
, char *buf
)
551 return sprintf(buf
, "%d\n", dev_to_node(dev
));
553 static DEVICE_ATTR_RO(numa_node
);
555 static struct attribute
*nd_numa_attributes
[] = {
556 &dev_attr_numa_node
.attr
,
560 static umode_t
nd_numa_attr_visible(struct kobject
*kobj
, struct attribute
*a
,
563 if (!IS_ENABLED(CONFIG_NUMA
))
570 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
572 struct attribute_group nd_numa_attribute_group
= {
573 .attrs
= nd_numa_attributes
,
574 .is_visible
= nd_numa_attr_visible
,
576 EXPORT_SYMBOL_GPL(nd_numa_attribute_group
);
578 int nvdimm_bus_create_ndctl(struct nvdimm_bus
*nvdimm_bus
)
580 dev_t devt
= MKDEV(nvdimm_bus_major
, nvdimm_bus
->id
);
583 dev
= device_create(nd_class
, &nvdimm_bus
->dev
, devt
, nvdimm_bus
,
584 "ndctl%d", nvdimm_bus
->id
);
587 dev_dbg(&nvdimm_bus
->dev
, "failed to register ndctl%d: %ld\n",
588 nvdimm_bus
->id
, PTR_ERR(dev
));
589 return PTR_ERR_OR_ZERO(dev
);
592 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus
*nvdimm_bus
)
594 device_destroy(nd_class
, MKDEV(nvdimm_bus_major
, nvdimm_bus
->id
));
597 static const struct nd_cmd_desc __nd_cmd_dimm_descs
[] = {
598 [ND_CMD_IMPLEMENTED
] = { },
601 .out_sizes
= { 4, 128, },
603 [ND_CMD_SMART_THRESHOLD
] = {
605 .out_sizes
= { 4, 8, },
607 [ND_CMD_DIMM_FLAGS
] = {
609 .out_sizes
= { 4, 4 },
611 [ND_CMD_GET_CONFIG_SIZE
] = {
613 .out_sizes
= { 4, 4, 4, },
615 [ND_CMD_GET_CONFIG_DATA
] = {
617 .in_sizes
= { 4, 4, },
619 .out_sizes
= { 4, UINT_MAX
, },
621 [ND_CMD_SET_CONFIG_DATA
] = {
623 .in_sizes
= { 4, 4, UINT_MAX
, },
629 .in_sizes
= { 4, 4, UINT_MAX
, },
631 .out_sizes
= { 4, 4, UINT_MAX
, },
635 .in_sizes
= { sizeof(struct nd_cmd_pkg
), UINT_MAX
, },
637 .out_sizes
= { UINT_MAX
, },
641 const struct nd_cmd_desc
*nd_cmd_dimm_desc(int cmd
)
643 if (cmd
< ARRAY_SIZE(__nd_cmd_dimm_descs
))
644 return &__nd_cmd_dimm_descs
[cmd
];
647 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc
);
649 static const struct nd_cmd_desc __nd_cmd_bus_descs
[] = {
650 [ND_CMD_IMPLEMENTED
] = { },
653 .in_sizes
= { 8, 8, },
655 .out_sizes
= { 4, 4, 4, 4, },
657 [ND_CMD_ARS_START
] = {
659 .in_sizes
= { 8, 8, 2, 1, 5, },
661 .out_sizes
= { 4, 4, },
663 [ND_CMD_ARS_STATUS
] = {
665 .out_sizes
= { 4, 4, UINT_MAX
, },
667 [ND_CMD_CLEAR_ERROR
] = {
669 .in_sizes
= { 8, 8, },
671 .out_sizes
= { 4, 4, 8, },
675 .in_sizes
= { sizeof(struct nd_cmd_pkg
), UINT_MAX
, },
677 .out_sizes
= { UINT_MAX
, },
681 const struct nd_cmd_desc
*nd_cmd_bus_desc(int cmd
)
683 if (cmd
< ARRAY_SIZE(__nd_cmd_bus_descs
))
684 return &__nd_cmd_bus_descs
[cmd
];
687 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc
);
689 u32
nd_cmd_in_size(struct nvdimm
*nvdimm
, int cmd
,
690 const struct nd_cmd_desc
*desc
, int idx
, void *buf
)
692 if (idx
>= desc
->in_num
)
695 if (desc
->in_sizes
[idx
] < UINT_MAX
)
696 return desc
->in_sizes
[idx
];
698 if (nvdimm
&& cmd
== ND_CMD_SET_CONFIG_DATA
&& idx
== 2) {
699 struct nd_cmd_set_config_hdr
*hdr
= buf
;
701 return hdr
->in_length
;
702 } else if (nvdimm
&& cmd
== ND_CMD_VENDOR
&& idx
== 2) {
703 struct nd_cmd_vendor_hdr
*hdr
= buf
;
705 return hdr
->in_length
;
706 } else if (cmd
== ND_CMD_CALL
) {
707 struct nd_cmd_pkg
*pkg
= buf
;
709 return pkg
->nd_size_in
;
714 EXPORT_SYMBOL_GPL(nd_cmd_in_size
);
716 u32
nd_cmd_out_size(struct nvdimm
*nvdimm
, int cmd
,
717 const struct nd_cmd_desc
*desc
, int idx
, const u32
*in_field
,
718 const u32
*out_field
)
720 if (idx
>= desc
->out_num
)
723 if (desc
->out_sizes
[idx
] < UINT_MAX
)
724 return desc
->out_sizes
[idx
];
726 if (nvdimm
&& cmd
== ND_CMD_GET_CONFIG_DATA
&& idx
== 1)
728 else if (nvdimm
&& cmd
== ND_CMD_VENDOR
&& idx
== 2)
730 else if (!nvdimm
&& cmd
== ND_CMD_ARS_STATUS
&& idx
== 2)
731 return out_field
[1] - 8;
732 else if (cmd
== ND_CMD_CALL
) {
733 struct nd_cmd_pkg
*pkg
= (struct nd_cmd_pkg
*) in_field
;
735 return pkg
->nd_size_out
;
741 EXPORT_SYMBOL_GPL(nd_cmd_out_size
);
743 void wait_nvdimm_bus_probe_idle(struct device
*dev
)
745 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
748 if (nvdimm_bus
->probe_active
== 0)
750 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
751 wait_event(nvdimm_bus
->probe_wait
,
752 nvdimm_bus
->probe_active
== 0);
753 nvdimm_bus_lock(&nvdimm_bus
->dev
);
757 static int pmem_active(struct device
*dev
, void *data
)
759 if (is_nd_pmem(dev
) && dev
->driver
)
764 /* set_config requires an idle interleave set */
765 static int nd_cmd_clear_to_send(struct nvdimm_bus
*nvdimm_bus
,
766 struct nvdimm
*nvdimm
, unsigned int cmd
)
768 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
770 /* ask the bus provider if it would like to block this request */
771 if (nd_desc
->clear_to_send
) {
772 int rc
= nd_desc
->clear_to_send(nd_desc
, nvdimm
, cmd
);
778 /* require clear error to go through the pmem driver */
779 if (!nvdimm
&& cmd
== ND_CMD_CLEAR_ERROR
)
780 return device_for_each_child(&nvdimm_bus
->dev
, NULL
,
783 if (!nvdimm
|| cmd
!= ND_CMD_SET_CONFIG_DATA
)
786 /* prevent label manipulation while the kernel owns label updates */
787 wait_nvdimm_bus_probe_idle(&nvdimm_bus
->dev
);
788 if (atomic_read(&nvdimm
->busy
))
793 static int __nd_ioctl(struct nvdimm_bus
*nvdimm_bus
, struct nvdimm
*nvdimm
,
794 int read_only
, unsigned int ioctl_cmd
, unsigned long arg
)
796 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
797 size_t buf_len
= 0, in_len
= 0, out_len
= 0;
798 static char out_env
[ND_CMD_MAX_ENVELOPE
];
799 static char in_env
[ND_CMD_MAX_ENVELOPE
];
800 const struct nd_cmd_desc
*desc
= NULL
;
801 unsigned int cmd
= _IOC_NR(ioctl_cmd
);
802 void __user
*p
= (void __user
*) arg
;
803 struct device
*dev
= &nvdimm_bus
->dev
;
804 struct nd_cmd_pkg pkg
;
805 const char *cmd_name
, *dimm_name
;
806 unsigned long cmd_mask
;
811 desc
= nd_cmd_dimm_desc(cmd
);
812 cmd_name
= nvdimm_cmd_name(cmd
);
813 cmd_mask
= nvdimm
->cmd_mask
;
814 dimm_name
= dev_name(&nvdimm
->dev
);
816 desc
= nd_cmd_bus_desc(cmd
);
817 cmd_name
= nvdimm_bus_cmd_name(cmd
);
818 cmd_mask
= nd_desc
->cmd_mask
;
822 if (cmd
== ND_CMD_CALL
) {
823 if (copy_from_user(&pkg
, p
, sizeof(pkg
)))
827 if (!desc
|| (desc
->out_num
+ desc
->in_num
== 0) ||
828 !test_bit(cmd
, &cmd_mask
))
831 /* fail write commands (when read-only) */
835 case ND_CMD_SET_CONFIG_DATA
:
836 case ND_CMD_ARS_START
:
837 case ND_CMD_CLEAR_ERROR
:
839 dev_dbg(&nvdimm_bus
->dev
, "'%s' command while read-only.\n",
840 nvdimm
? nvdimm_cmd_name(cmd
)
841 : nvdimm_bus_cmd_name(cmd
));
847 /* process an input envelope */
848 for (i
= 0; i
< desc
->in_num
; i
++) {
851 in_size
= nd_cmd_in_size(nvdimm
, cmd
, desc
, i
, in_env
);
852 if (in_size
== UINT_MAX
) {
853 dev_err(dev
, "%s:%s unknown input size cmd: %s field: %d\n",
854 __func__
, dimm_name
, cmd_name
, i
);
857 if (in_len
< sizeof(in_env
))
858 copy
= min_t(u32
, sizeof(in_env
) - in_len
, in_size
);
861 if (copy
&& copy_from_user(&in_env
[in_len
], p
+ in_len
, copy
))
866 if (cmd
== ND_CMD_CALL
) {
867 dev_dbg(dev
, "%s:%s, idx: %llu, in: %zu, out: %zu, len %zu\n",
868 __func__
, dimm_name
, pkg
.nd_command
,
869 in_len
, out_len
, buf_len
);
871 for (i
= 0; i
< ARRAY_SIZE(pkg
.nd_reserved2
); i
++)
872 if (pkg
.nd_reserved2
[i
])
876 /* process an output envelope */
877 for (i
= 0; i
< desc
->out_num
; i
++) {
878 u32 out_size
= nd_cmd_out_size(nvdimm
, cmd
, desc
, i
,
879 (u32
*) in_env
, (u32
*) out_env
);
882 if (out_size
== UINT_MAX
) {
883 dev_dbg(dev
, "%s:%s unknown output size cmd: %s field: %d\n",
884 __func__
, dimm_name
, cmd_name
, i
);
887 if (out_len
< sizeof(out_env
))
888 copy
= min_t(u32
, sizeof(out_env
) - out_len
, out_size
);
891 if (copy
&& copy_from_user(&out_env
[out_len
],
892 p
+ in_len
+ out_len
, copy
))
897 buf_len
= out_len
+ in_len
;
898 if (buf_len
> ND_IOCTL_MAX_BUFLEN
) {
899 dev_dbg(dev
, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__
,
900 dimm_name
, cmd_name
, buf_len
,
901 ND_IOCTL_MAX_BUFLEN
);
905 buf
= vmalloc(buf_len
);
909 if (copy_from_user(buf
, p
, buf_len
)) {
914 nvdimm_bus_lock(&nvdimm_bus
->dev
);
915 rc
= nd_cmd_clear_to_send(nvdimm_bus
, nvdimm
, cmd
);
919 rc
= nd_desc
->ndctl(nd_desc
, nvdimm
, cmd
, buf
, buf_len
, NULL
);
922 if (copy_to_user(p
, buf
, buf_len
))
925 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
931 static long nd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
933 long id
= (long) file
->private_data
;
935 struct nvdimm_bus
*nvdimm_bus
;
937 ro
= ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
);
938 mutex_lock(&nvdimm_bus_list_mutex
);
939 list_for_each_entry(nvdimm_bus
, &nvdimm_bus_list
, list
) {
940 if (nvdimm_bus
->id
== id
) {
941 rc
= __nd_ioctl(nvdimm_bus
, NULL
, ro
, cmd
, arg
);
945 mutex_unlock(&nvdimm_bus_list_mutex
);
950 static int match_dimm(struct device
*dev
, void *data
)
952 long id
= (long) data
;
954 if (is_nvdimm(dev
)) {
955 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
957 return nvdimm
->id
== id
;
963 static long nvdimm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
966 struct nvdimm_bus
*nvdimm_bus
;
968 ro
= ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
);
969 mutex_lock(&nvdimm_bus_list_mutex
);
970 list_for_each_entry(nvdimm_bus
, &nvdimm_bus_list
, list
) {
971 struct device
*dev
= device_find_child(&nvdimm_bus
->dev
,
972 file
->private_data
, match_dimm
);
973 struct nvdimm
*nvdimm
;
978 nvdimm
= to_nvdimm(dev
);
979 rc
= __nd_ioctl(nvdimm_bus
, nvdimm
, ro
, cmd
, arg
);
983 mutex_unlock(&nvdimm_bus_list_mutex
);
988 static int nd_open(struct inode
*inode
, struct file
*file
)
990 long minor
= iminor(inode
);
992 file
->private_data
= (void *) minor
;
996 static const struct file_operations nvdimm_bus_fops
= {
997 .owner
= THIS_MODULE
,
999 .unlocked_ioctl
= nd_ioctl
,
1000 .compat_ioctl
= nd_ioctl
,
1001 .llseek
= noop_llseek
,
1004 static const struct file_operations nvdimm_fops
= {
1005 .owner
= THIS_MODULE
,
1007 .unlocked_ioctl
= nvdimm_ioctl
,
1008 .compat_ioctl
= nvdimm_ioctl
,
1009 .llseek
= noop_llseek
,
1012 int __init
nvdimm_bus_init(void)
1016 BUILD_BUG_ON(sizeof(struct nd_smart_payload
) != 128);
1017 BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload
) != 8);
1019 rc
= bus_register(&nvdimm_bus_type
);
1023 rc
= register_chrdev(0, "ndctl", &nvdimm_bus_fops
);
1025 goto err_bus_chrdev
;
1026 nvdimm_bus_major
= rc
;
1028 rc
= register_chrdev(0, "dimmctl", &nvdimm_fops
);
1030 goto err_dimm_chrdev
;
1033 nd_class
= class_create(THIS_MODULE
, "nd");
1034 if (IS_ERR(nd_class
)) {
1035 rc
= PTR_ERR(nd_class
);
1039 rc
= driver_register(&nd_bus_driver
.drv
);
1046 class_destroy(nd_class
);
1048 unregister_chrdev(nvdimm_major
, "dimmctl");
1050 unregister_chrdev(nvdimm_bus_major
, "ndctl");
1052 bus_unregister(&nvdimm_bus_type
);
1057 void nvdimm_bus_exit(void)
1059 driver_unregister(&nd_bus_driver
.drv
);
1060 class_destroy(nd_class
);
1061 unregister_chrdev(nvdimm_bus_major
, "ndctl");
1062 unregister_chrdev(nvdimm_major
, "dimmctl");
1063 bus_unregister(&nvdimm_bus_type
);
1064 ida_destroy(&nd_ida
);