libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure
[firefly-linux-kernel-4.4.55.git] / drivers / nvdimm / core.c
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 #include <linux/libnvdimm.h>
14 #include <linux/export.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/ndctl.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 #include "nd-core.h"
21 #include "nd.h"
22
23 LIST_HEAD(nvdimm_bus_list);
24 DEFINE_MUTEX(nvdimm_bus_list_mutex);
25 static DEFINE_IDA(nd_ida);
26
27 static void nvdimm_bus_release(struct device *dev)
28 {
29         struct nvdimm_bus *nvdimm_bus;
30
31         nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
32         ida_simple_remove(&nd_ida, nvdimm_bus->id);
33         kfree(nvdimm_bus);
34 }
35
36 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
37 {
38         struct nvdimm_bus *nvdimm_bus;
39
40         nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
41         WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release);
42         return nvdimm_bus;
43 }
44 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
45
46 struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus)
47 {
48         /* struct nvdimm_bus definition is private to libnvdimm */
49         return nvdimm_bus->nd_desc;
50 }
51 EXPORT_SYMBOL_GPL(to_nd_desc);
52
53 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
54 {
55         struct device *dev;
56
57         for (dev = nd_dev; dev; dev = dev->parent)
58                 if (dev->release == nvdimm_bus_release)
59                         break;
60         dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
61         if (dev)
62                 return to_nvdimm_bus(dev);
63         return NULL;
64 }
65
66 static ssize_t commands_show(struct device *dev,
67                 struct device_attribute *attr, char *buf)
68 {
69         int cmd, len = 0;
70         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
71         struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
72
73         for_each_set_bit(cmd, &nd_desc->dsm_mask, BITS_PER_LONG)
74                 len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd));
75         len += sprintf(buf + len, "\n");
76         return len;
77 }
78 static DEVICE_ATTR_RO(commands);
79
80 static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
81 {
82         struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
83         struct device *parent = nvdimm_bus->dev.parent;
84
85         if (nd_desc->provider_name)
86                 return nd_desc->provider_name;
87         else if (parent)
88                 return dev_name(parent);
89         else
90                 return "unknown";
91 }
92
93 static ssize_t provider_show(struct device *dev,
94                 struct device_attribute *attr, char *buf)
95 {
96         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
97
98         return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
99 }
100 static DEVICE_ATTR_RO(provider);
101
102 static int flush_namespaces(struct device *dev, void *data)
103 {
104         device_lock(dev);
105         device_unlock(dev);
106         return 0;
107 }
108
109 static int flush_regions_dimms(struct device *dev, void *data)
110 {
111         device_lock(dev);
112         device_unlock(dev);
113         device_for_each_child(dev, NULL, flush_namespaces);
114         return 0;
115 }
116
117 static ssize_t wait_probe_show(struct device *dev,
118                 struct device_attribute *attr, char *buf)
119 {
120         nd_synchronize();
121         device_for_each_child(dev, NULL, flush_regions_dimms);
122         return sprintf(buf, "1\n");
123 }
124 static DEVICE_ATTR_RO(wait_probe);
125
126 static struct attribute *nvdimm_bus_attributes[] = {
127         &dev_attr_commands.attr,
128         &dev_attr_wait_probe.attr,
129         &dev_attr_provider.attr,
130         NULL,
131 };
132
133 struct attribute_group nvdimm_bus_attribute_group = {
134         .attrs = nvdimm_bus_attributes,
135 };
136 EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
137
138 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
139                 struct nvdimm_bus_descriptor *nd_desc)
140 {
141         struct nvdimm_bus *nvdimm_bus;
142         int rc;
143
144         nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
145         if (!nvdimm_bus)
146                 return NULL;
147         INIT_LIST_HEAD(&nvdimm_bus->list);
148         nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
149         if (nvdimm_bus->id < 0) {
150                 kfree(nvdimm_bus);
151                 return NULL;
152         }
153         nvdimm_bus->nd_desc = nd_desc;
154         nvdimm_bus->dev.parent = parent;
155         nvdimm_bus->dev.release = nvdimm_bus_release;
156         nvdimm_bus->dev.groups = nd_desc->attr_groups;
157         dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
158         rc = device_register(&nvdimm_bus->dev);
159         if (rc) {
160                 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
161                 goto err;
162         }
163
164         rc = nvdimm_bus_create_ndctl(nvdimm_bus);
165         if (rc)
166                 goto err;
167
168         mutex_lock(&nvdimm_bus_list_mutex);
169         list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
170         mutex_unlock(&nvdimm_bus_list_mutex);
171
172         return nvdimm_bus;
173  err:
174         put_device(&nvdimm_bus->dev);
175         return NULL;
176 }
177 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
178
179 static int child_unregister(struct device *dev, void *data)
180 {
181         /*
182          * the singular ndctl class device per bus needs to be
183          * "device_destroy"ed, so skip it here
184          *
185          * i.e. remove classless children
186          */
187         if (dev->class)
188                 /* pass */;
189         else
190                 nd_device_unregister(dev, ND_SYNC);
191         return 0;
192 }
193
194 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
195 {
196         if (!nvdimm_bus)
197                 return;
198
199         mutex_lock(&nvdimm_bus_list_mutex);
200         list_del_init(&nvdimm_bus->list);
201         mutex_unlock(&nvdimm_bus_list_mutex);
202
203         nd_synchronize();
204         device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
205         nvdimm_bus_destroy_ndctl(nvdimm_bus);
206
207         device_unregister(&nvdimm_bus->dev);
208 }
209 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
210
211 static __init int libnvdimm_init(void)
212 {
213         int rc;
214
215         rc = nvdimm_bus_init();
216         if (rc)
217                 return rc;
218         rc = nvdimm_init();
219         if (rc)
220                 goto err_dimm;
221         return 0;
222  err_dimm:
223         nvdimm_bus_exit();
224         return rc;
225 }
226
227 static __exit void libnvdimm_exit(void)
228 {
229         WARN_ON(!list_empty(&nvdimm_bus_list));
230         nvdimm_exit();
231         nvdimm_bus_exit();
232 }
233
234 MODULE_LICENSE("GPL v2");
235 MODULE_AUTHOR("Intel Corporation");
236 subsys_initcall(libnvdimm_init);
237 module_exit(libnvdimm_exit);