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