summaryrefslogtreecommitdiffstats
path: root/drivers/nvdimm
diff options
context:
space:
mode:
authorDan Williams <dan.j.williams@intel.com>2015-04-26 19:26:48 -0400
committerDan Williams <dan.j.williams@intel.com>2015-06-24 21:24:10 -0400
commit45def22c1fab85764646746ce38d45b2f3281fa5 (patch)
tree05a417ff9c0540728bb29be48fe999a3f04ec79d /drivers/nvdimm
parentb94d5230d06eb930be82e67fb1a9a58271e78297 (diff)
libnvdimm: control character device and nvdimm_bus sysfs attributes
The control device for a nvdimm_bus is registered as an "nd" class device. The expectation is that there will usually only be one "nd" bus registered under /sys/class/nd. However, we allow for the possibility of multiple buses and they will listed in discovery order as ndctl0...ndctlN. This character device hosts the ioctl for passing control messages. The initial command set has a 1:1 correlation with the commands listed in the by the "NFIT DSM Example" document [1], but this scheme is extensible to future command sets. Note, nd_ioctl() and the backing ->ndctl() implementation are defined in a subsequent patch. This is simply the initial registrations and sysfs attributes. [1]: http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf Cc: Neil Brown <neilb@suse.de> Cc: Greg KH <gregkh@linuxfoundation.org> Cc: <linux-acpi@vger.kernel.org> Cc: Robert Moore <robert.moore@intel.com> Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Acked-by: Christoph Hellwig <hch@lst.de> Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Tested-by: Toshi Kani <toshi.kani@hp.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Diffstat (limited to 'drivers/nvdimm')
-rw-r--r--drivers/nvdimm/Makefile1
-rw-r--r--drivers/nvdimm/bus.c83
-rw-r--r--drivers/nvdimm/core.c88
-rw-r--r--drivers/nvdimm/nd-core.h6
4 files changed, 176 insertions, 2 deletions
diff --git a/drivers/nvdimm/Makefile b/drivers/nvdimm/Makefile
index 10bc7af47992..8a8293c72c7c 100644
--- a/drivers/nvdimm/Makefile
+++ b/drivers/nvdimm/Makefile
@@ -1,3 +1,4 @@
1obj-$(CONFIG_LIBNVDIMM) += libnvdimm.o 1obj-$(CONFIG_LIBNVDIMM) += libnvdimm.o
2 2
3libnvdimm-y := core.o 3libnvdimm-y := core.o
4libnvdimm-y += bus.o
diff --git a/drivers/nvdimm/bus.c b/drivers/nvdimm/bus.c
new file mode 100644
index 000000000000..3f7c690a5d0c
--- /dev/null
+++ b/drivers/nvdimm/bus.c
@@ -0,0 +1,83 @@
1/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/uaccess.h>
15#include <linux/fcntl.h>
16#include <linux/slab.h>
17#include <linux/fs.h>
18#include <linux/io.h>
19#include "nd-core.h"
20
21static int nvdimm_bus_major;
22static struct class *nd_class;
23
24int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
25{
26 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
27 struct device *dev;
28
29 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
30 "ndctl%d", nvdimm_bus->id);
31
32 if (IS_ERR(dev)) {
33 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
34 nvdimm_bus->id, PTR_ERR(dev));
35 return PTR_ERR(dev);
36 }
37 return 0;
38}
39
40void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
41{
42 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
43}
44
45static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
46{
47 return -ENXIO;
48}
49
50static const struct file_operations nvdimm_bus_fops = {
51 .owner = THIS_MODULE,
52 .open = nonseekable_open,
53 .unlocked_ioctl = nd_ioctl,
54 .compat_ioctl = nd_ioctl,
55 .llseek = noop_llseek,
56};
57
58int __init nvdimm_bus_init(void)
59{
60 int rc;
61
62 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
63 if (rc < 0)
64 return rc;
65 nvdimm_bus_major = rc;
66
67 nd_class = class_create(THIS_MODULE, "nd");
68 if (IS_ERR(nd_class))
69 goto err_class;
70
71 return 0;
72
73 err_class:
74 unregister_chrdev(nvdimm_bus_major, "ndctl");
75
76 return rc;
77}
78
79void __exit nvdimm_bus_exit(void)
80{
81 class_destroy(nd_class);
82 unregister_chrdev(nvdimm_bus_major, "ndctl");
83}
diff --git a/drivers/nvdimm/core.c b/drivers/nvdimm/core.c
index c578a49867ac..fa7ab5ad0318 100644
--- a/drivers/nvdimm/core.c
+++ b/drivers/nvdimm/core.c
@@ -14,9 +14,12 @@
14#include <linux/export.h> 14#include <linux/export.h>
15#include <linux/module.h> 15#include <linux/module.h>
16#include <linux/device.h> 16#include <linux/device.h>
17#include <linux/mutex.h>
17#include <linux/slab.h> 18#include <linux/slab.h>
18#include "nd-core.h" 19#include "nd-core.h"
19 20
21static LIST_HEAD(nvdimm_bus_list);
22static DEFINE_MUTEX(nvdimm_bus_list_mutex);
20static DEFINE_IDA(nd_ida); 23static DEFINE_IDA(nd_ida);
21 24
22static void nvdimm_bus_release(struct device *dev) 25static void nvdimm_bus_release(struct device *dev)
@@ -28,6 +31,55 @@ static void nvdimm_bus_release(struct device *dev)
28 kfree(nvdimm_bus); 31 kfree(nvdimm_bus);
29} 32}
30 33
34struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
35{
36 struct nvdimm_bus *nvdimm_bus;
37
38 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
39 WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release);
40 return nvdimm_bus;
41}
42EXPORT_SYMBOL_GPL(to_nvdimm_bus);
43
44struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus)
45{
46 /* struct nvdimm_bus definition is private to libnvdimm */
47 return nvdimm_bus->nd_desc;
48}
49EXPORT_SYMBOL_GPL(to_nd_desc);
50
51static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
52{
53 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
54 struct device *parent = nvdimm_bus->dev.parent;
55
56 if (nd_desc->provider_name)
57 return nd_desc->provider_name;
58 else if (parent)
59 return dev_name(parent);
60 else
61 return "unknown";
62}
63
64static ssize_t provider_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
66{
67 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
68
69 return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
70}
71static DEVICE_ATTR_RO(provider);
72
73static struct attribute *nvdimm_bus_attributes[] = {
74 &dev_attr_provider.attr,
75 NULL,
76};
77
78struct attribute_group nvdimm_bus_attribute_group = {
79 .attrs = nvdimm_bus_attributes,
80};
81EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
82
31struct nvdimm_bus *nvdimm_bus_register(struct device *parent, 83struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
32 struct nvdimm_bus_descriptor *nd_desc) 84 struct nvdimm_bus_descriptor *nd_desc)
33{ 85{
@@ -37,6 +89,7 @@ struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
37 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL); 89 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
38 if (!nvdimm_bus) 90 if (!nvdimm_bus)
39 return NULL; 91 return NULL;
92 INIT_LIST_HEAD(&nvdimm_bus->list);
40 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); 93 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
41 if (nvdimm_bus->id < 0) { 94 if (nvdimm_bus->id < 0) {
42 kfree(nvdimm_bus); 95 kfree(nvdimm_bus);
@@ -45,15 +98,26 @@ struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
45 nvdimm_bus->nd_desc = nd_desc; 98 nvdimm_bus->nd_desc = nd_desc;
46 nvdimm_bus->dev.parent = parent; 99 nvdimm_bus->dev.parent = parent;
47 nvdimm_bus->dev.release = nvdimm_bus_release; 100 nvdimm_bus->dev.release = nvdimm_bus_release;
101 nvdimm_bus->dev.groups = nd_desc->attr_groups;
48 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id); 102 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
49 rc = device_register(&nvdimm_bus->dev); 103 rc = device_register(&nvdimm_bus->dev);
50 if (rc) { 104 if (rc) {
51 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc); 105 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
52 put_device(&nvdimm_bus->dev); 106 goto err;
53 return NULL;
54 } 107 }
55 108
109 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
110 if (rc)
111 goto err;
112
113 mutex_lock(&nvdimm_bus_list_mutex);
114 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
115 mutex_unlock(&nvdimm_bus_list_mutex);
116
56 return nvdimm_bus; 117 return nvdimm_bus;
118 err:
119 put_device(&nvdimm_bus->dev);
120 return NULL;
57} 121}
58EXPORT_SYMBOL_GPL(nvdimm_bus_register); 122EXPORT_SYMBOL_GPL(nvdimm_bus_register);
59 123
@@ -61,9 +125,29 @@ void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
61{ 125{
62 if (!nvdimm_bus) 126 if (!nvdimm_bus)
63 return; 127 return;
128
129 mutex_lock(&nvdimm_bus_list_mutex);
130 list_del_init(&nvdimm_bus->list);
131 mutex_unlock(&nvdimm_bus_list_mutex);
132
133 nvdimm_bus_destroy_ndctl(nvdimm_bus);
134
64 device_unregister(&nvdimm_bus->dev); 135 device_unregister(&nvdimm_bus->dev);
65} 136}
66EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); 137EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
67 138
139static __init int libnvdimm_init(void)
140{
141 return nvdimm_bus_init();
142}
143
144static __exit void libnvdimm_exit(void)
145{
146 WARN_ON(!list_empty(&nvdimm_bus_list));
147 nvdimm_bus_exit();
148}
149
68MODULE_LICENSE("GPL v2"); 150MODULE_LICENSE("GPL v2");
69MODULE_AUTHOR("Intel Corporation"); 151MODULE_AUTHOR("Intel Corporation");
152subsys_initcall(libnvdimm_init);
153module_exit(libnvdimm_exit);
diff --git a/drivers/nvdimm/nd-core.h b/drivers/nvdimm/nd-core.h
index 291b2fdcd96b..0b0ff2423161 100644
--- a/drivers/nvdimm/nd-core.h
+++ b/drivers/nvdimm/nd-core.h
@@ -17,7 +17,13 @@
17 17
18struct nvdimm_bus { 18struct nvdimm_bus {
19 struct nvdimm_bus_descriptor *nd_desc; 19 struct nvdimm_bus_descriptor *nd_desc;
20 struct list_head list;
20 struct device dev; 21 struct device dev;
21 int id; 22 int id;
22}; 23};
24
25int __init nvdimm_bus_init(void);
26void __exit nvdimm_bus_exit(void);
27int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus);
28void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus);
23#endif /* __ND_CORE_H__ */ 29#endif /* __ND_CORE_H__ */