blob: cf99cce8ef33a88733fa8b90754d559555026e4e [file] [log] [blame]
Dan Williamsb94d5232015-05-19 22:54:31 -04001/*
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>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040017#include <linux/ctype.h>
Dan Williams62232e452015-06-08 14:27:06 -040018#include <linux/ndctl.h>
Dan Williams45def222015-04-26 19:26:48 -040019#include <linux/mutex.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040020#include <linux/slab.h>
21#include "nd-core.h"
Dan Williams4d88a972015-05-31 14:41:48 -040022#include "nd.h"
Dan Williamsb94d5232015-05-19 22:54:31 -040023
Dan Williamse6dfb2d2015-04-25 03:56:17 -040024LIST_HEAD(nvdimm_bus_list);
25DEFINE_MUTEX(nvdimm_bus_list_mutex);
Dan Williamsb94d5232015-05-19 22:54:31 -040026static DEFINE_IDA(nd_ida);
27
Dan Williams3d880022015-05-31 15:02:11 -040028void nvdimm_bus_lock(struct device *dev)
29{
30 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
31
32 if (!nvdimm_bus)
33 return;
34 mutex_lock(&nvdimm_bus->reconfig_mutex);
35}
36EXPORT_SYMBOL(nvdimm_bus_lock);
37
38void nvdimm_bus_unlock(struct device *dev)
39{
40 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
41
42 if (!nvdimm_bus)
43 return;
44 mutex_unlock(&nvdimm_bus->reconfig_mutex);
45}
46EXPORT_SYMBOL(nvdimm_bus_unlock);
47
48bool is_nvdimm_bus_locked(struct device *dev)
49{
50 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
51
52 if (!nvdimm_bus)
53 return false;
54 return mutex_is_locked(&nvdimm_bus->reconfig_mutex);
55}
56EXPORT_SYMBOL(is_nvdimm_bus_locked);
57
Dan Williamseaf96152015-05-01 13:11:27 -040058u64 nd_fletcher64(void *addr, size_t len, bool le)
59{
60 u32 *buf = addr;
61 u32 lo32 = 0;
62 u64 hi32 = 0;
63 int i;
64
65 for (i = 0; i < len / sizeof(u32); i++) {
66 lo32 += le ? le32_to_cpu((__le32) buf[i]) : buf[i];
67 hi32 += lo32;
68 }
69
70 return hi32 << 32 | lo32;
71}
72EXPORT_SYMBOL_GPL(nd_fletcher64);
73
Dan Williamsb94d5232015-05-19 22:54:31 -040074static void nvdimm_bus_release(struct device *dev)
75{
76 struct nvdimm_bus *nvdimm_bus;
77
78 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
79 ida_simple_remove(&nd_ida, nvdimm_bus->id);
80 kfree(nvdimm_bus);
81}
82
Dan Williams45def222015-04-26 19:26:48 -040083struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
84{
85 struct nvdimm_bus *nvdimm_bus;
86
87 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
88 WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release);
89 return nvdimm_bus;
90}
91EXPORT_SYMBOL_GPL(to_nvdimm_bus);
92
93struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus)
94{
95 /* struct nvdimm_bus definition is private to libnvdimm */
96 return nvdimm_bus->nd_desc;
97}
98EXPORT_SYMBOL_GPL(to_nd_desc);
99
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400100struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
101{
102 struct device *dev;
103
104 for (dev = nd_dev; dev; dev = dev->parent)
105 if (dev->release == nvdimm_bus_release)
106 break;
107 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
108 if (dev)
109 return to_nvdimm_bus(dev);
110 return NULL;
111}
112
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400113static bool is_uuid_sep(char sep)
114{
115 if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0')
116 return true;
117 return false;
118}
119
120static int nd_uuid_parse(struct device *dev, u8 *uuid_out, const char *buf,
121 size_t len)
122{
123 const char *str = buf;
124 u8 uuid[16];
125 int i;
126
127 for (i = 0; i < 16; i++) {
128 if (!isxdigit(str[0]) || !isxdigit(str[1])) {
129 dev_dbg(dev, "%s: pos: %d buf[%zd]: %c buf[%zd]: %c\n",
130 __func__, i, str - buf, str[0],
131 str + 1 - buf, str[1]);
132 return -EINVAL;
133 }
134
135 uuid[i] = (hex_to_bin(str[0]) << 4) | hex_to_bin(str[1]);
136 str += 2;
137 if (is_uuid_sep(*str))
138 str++;
139 }
140
141 memcpy(uuid_out, uuid, sizeof(uuid));
142 return 0;
143}
144
145/**
146 * nd_uuid_store: common implementation for writing 'uuid' sysfs attributes
147 * @dev: container device for the uuid property
148 * @uuid_out: uuid buffer to replace
149 * @buf: raw sysfs buffer to parse
150 *
151 * Enforce that uuids can only be changed while the device is disabled
152 * (driver detached)
153 * LOCKING: expects device_lock() is held on entry
154 */
155int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
156 size_t len)
157{
158 u8 uuid[16];
159 int rc;
160
161 if (dev->driver)
162 return -EBUSY;
163
164 rc = nd_uuid_parse(dev, uuid, buf, len);
165 if (rc)
166 return rc;
167
168 kfree(*uuid_out);
169 *uuid_out = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
170 if (!(*uuid_out))
171 return -ENOMEM;
172
173 return 0;
174}
175
Dan Williams62232e452015-06-08 14:27:06 -0400176static ssize_t commands_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
178{
179 int cmd, len = 0;
180 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
181 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
182
183 for_each_set_bit(cmd, &nd_desc->dsm_mask, BITS_PER_LONG)
184 len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd));
185 len += sprintf(buf + len, "\n");
186 return len;
187}
188static DEVICE_ATTR_RO(commands);
189
Dan Williams45def222015-04-26 19:26:48 -0400190static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
191{
192 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
193 struct device *parent = nvdimm_bus->dev.parent;
194
195 if (nd_desc->provider_name)
196 return nd_desc->provider_name;
197 else if (parent)
198 return dev_name(parent);
199 else
200 return "unknown";
201}
202
203static ssize_t provider_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
205{
206 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
207
208 return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
209}
210static DEVICE_ATTR_RO(provider);
211
Dan Williams4d88a972015-05-31 14:41:48 -0400212static int flush_namespaces(struct device *dev, void *data)
213{
214 device_lock(dev);
215 device_unlock(dev);
216 return 0;
217}
218
219static int flush_regions_dimms(struct device *dev, void *data)
220{
221 device_lock(dev);
222 device_unlock(dev);
223 device_for_each_child(dev, NULL, flush_namespaces);
224 return 0;
225}
226
227static ssize_t wait_probe_show(struct device *dev,
228 struct device_attribute *attr, char *buf)
229{
230 nd_synchronize();
231 device_for_each_child(dev, NULL, flush_regions_dimms);
232 return sprintf(buf, "1\n");
233}
234static DEVICE_ATTR_RO(wait_probe);
235
Dan Williams45def222015-04-26 19:26:48 -0400236static struct attribute *nvdimm_bus_attributes[] = {
Dan Williams62232e452015-06-08 14:27:06 -0400237 &dev_attr_commands.attr,
Dan Williams4d88a972015-05-31 14:41:48 -0400238 &dev_attr_wait_probe.attr,
Dan Williams45def222015-04-26 19:26:48 -0400239 &dev_attr_provider.attr,
240 NULL,
241};
242
243struct attribute_group nvdimm_bus_attribute_group = {
244 .attrs = nvdimm_bus_attributes,
245};
246EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
247
Dan Williams3d880022015-05-31 15:02:11 -0400248struct nvdimm_bus *__nvdimm_bus_register(struct device *parent,
249 struct nvdimm_bus_descriptor *nd_desc, struct module *module)
Dan Williamsb94d5232015-05-19 22:54:31 -0400250{
251 struct nvdimm_bus *nvdimm_bus;
252 int rc;
253
254 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
255 if (!nvdimm_bus)
256 return NULL;
Dan Williams45def222015-04-26 19:26:48 -0400257 INIT_LIST_HEAD(&nvdimm_bus->list);
Dan Williamseaf96152015-05-01 13:11:27 -0400258 init_waitqueue_head(&nvdimm_bus->probe_wait);
Dan Williamsb94d5232015-05-19 22:54:31 -0400259 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
Dan Williams3d880022015-05-31 15:02:11 -0400260 mutex_init(&nvdimm_bus->reconfig_mutex);
Dan Williamsb94d5232015-05-19 22:54:31 -0400261 if (nvdimm_bus->id < 0) {
262 kfree(nvdimm_bus);
263 return NULL;
264 }
265 nvdimm_bus->nd_desc = nd_desc;
Dan Williams3d880022015-05-31 15:02:11 -0400266 nvdimm_bus->module = module;
Dan Williamsb94d5232015-05-19 22:54:31 -0400267 nvdimm_bus->dev.parent = parent;
268 nvdimm_bus->dev.release = nvdimm_bus_release;
Dan Williams45def222015-04-26 19:26:48 -0400269 nvdimm_bus->dev.groups = nd_desc->attr_groups;
Dan Williamsb94d5232015-05-19 22:54:31 -0400270 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
271 rc = device_register(&nvdimm_bus->dev);
272 if (rc) {
273 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
Dan Williams45def222015-04-26 19:26:48 -0400274 goto err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400275 }
276
Dan Williams45def222015-04-26 19:26:48 -0400277 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
278 if (rc)
279 goto err;
280
281 mutex_lock(&nvdimm_bus_list_mutex);
282 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
283 mutex_unlock(&nvdimm_bus_list_mutex);
284
Dan Williamsb94d5232015-05-19 22:54:31 -0400285 return nvdimm_bus;
Dan Williams45def222015-04-26 19:26:48 -0400286 err:
287 put_device(&nvdimm_bus->dev);
288 return NULL;
Dan Williamsb94d5232015-05-19 22:54:31 -0400289}
Dan Williams3d880022015-05-31 15:02:11 -0400290EXPORT_SYMBOL_GPL(__nvdimm_bus_register);
Dan Williamsb94d5232015-05-19 22:54:31 -0400291
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400292static int child_unregister(struct device *dev, void *data)
293{
294 /*
295 * the singular ndctl class device per bus needs to be
296 * "device_destroy"ed, so skip it here
297 *
298 * i.e. remove classless children
299 */
300 if (dev->class)
301 /* pass */;
302 else
Dan Williams4d88a972015-05-31 14:41:48 -0400303 nd_device_unregister(dev, ND_SYNC);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400304 return 0;
305}
306
Dan Williamsb94d5232015-05-19 22:54:31 -0400307void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
308{
309 if (!nvdimm_bus)
310 return;
Dan Williams45def222015-04-26 19:26:48 -0400311
312 mutex_lock(&nvdimm_bus_list_mutex);
313 list_del_init(&nvdimm_bus->list);
314 mutex_unlock(&nvdimm_bus_list_mutex);
315
Dan Williams4d88a972015-05-31 14:41:48 -0400316 nd_synchronize();
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400317 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
Dan Williams45def222015-04-26 19:26:48 -0400318 nvdimm_bus_destroy_ndctl(nvdimm_bus);
319
Dan Williamsb94d5232015-05-19 22:54:31 -0400320 device_unregister(&nvdimm_bus->dev);
321}
322EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
323
Dan Williams45def222015-04-26 19:26:48 -0400324static __init int libnvdimm_init(void)
325{
Dan Williams4d88a972015-05-31 14:41:48 -0400326 int rc;
327
328 rc = nvdimm_bus_init();
329 if (rc)
330 return rc;
331 rc = nvdimm_init();
332 if (rc)
333 goto err_dimm;
Dan Williams3d880022015-05-31 15:02:11 -0400334 rc = nd_region_init();
335 if (rc)
336 goto err_region;
Dan Williams4d88a972015-05-31 14:41:48 -0400337 return 0;
Dan Williams3d880022015-05-31 15:02:11 -0400338 err_region:
339 nvdimm_exit();
Dan Williams4d88a972015-05-31 14:41:48 -0400340 err_dimm:
341 nvdimm_bus_exit();
342 return rc;
Dan Williams45def222015-04-26 19:26:48 -0400343}
344
345static __exit void libnvdimm_exit(void)
346{
347 WARN_ON(!list_empty(&nvdimm_bus_list));
Dan Williams3d880022015-05-31 15:02:11 -0400348 nd_region_exit();
Dan Williams4d88a972015-05-31 14:41:48 -0400349 nvdimm_exit();
Dan Williams45def222015-04-26 19:26:48 -0400350 nvdimm_bus_exit();
351}
352
Dan Williamsb94d5232015-05-19 22:54:31 -0400353MODULE_LICENSE("GPL v2");
354MODULE_AUTHOR("Intel Corporation");
Dan Williams45def222015-04-26 19:26:48 -0400355subsys_initcall(libnvdimm_init);
356module_exit(libnvdimm_exit);