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