blob: dd824d7c266986c6a3883f1a8865abc87f4c2da8 [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 Williams1b40e092015-05-01 13:34:01 -0400176ssize_t nd_sector_size_show(unsigned long current_lbasize,
177 const unsigned long *supported, char *buf)
178{
179 ssize_t len = 0;
180 int i;
181
182 for (i = 0; supported[i]; i++)
183 if (current_lbasize == supported[i])
184 len += sprintf(buf + len, "[%ld] ", supported[i]);
185 else
186 len += sprintf(buf + len, "%ld ", supported[i]);
187 len += sprintf(buf + len, "\n");
188 return len;
189}
190
191ssize_t nd_sector_size_store(struct device *dev, const char *buf,
192 unsigned long *current_lbasize, const unsigned long *supported)
193{
194 unsigned long lbasize;
195 int rc, i;
196
197 if (dev->driver)
198 return -EBUSY;
199
200 rc = kstrtoul(buf, 0, &lbasize);
201 if (rc)
202 return rc;
203
204 for (i = 0; supported[i]; i++)
205 if (lbasize == supported[i])
206 break;
207
208 if (supported[i]) {
209 *current_lbasize = lbasize;
210 return 0;
211 } else {
212 return -EINVAL;
213 }
214}
215
Dan Williams62232e452015-06-08 14:27:06 -0400216static ssize_t commands_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
218{
219 int cmd, len = 0;
220 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
221 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
222
223 for_each_set_bit(cmd, &nd_desc->dsm_mask, BITS_PER_LONG)
224 len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd));
225 len += sprintf(buf + len, "\n");
226 return len;
227}
228static DEVICE_ATTR_RO(commands);
229
Dan Williams45def222015-04-26 19:26:48 -0400230static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
231{
232 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
233 struct device *parent = nvdimm_bus->dev.parent;
234
235 if (nd_desc->provider_name)
236 return nd_desc->provider_name;
237 else if (parent)
238 return dev_name(parent);
239 else
240 return "unknown";
241}
242
243static ssize_t provider_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
245{
246 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
247
248 return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
249}
250static DEVICE_ATTR_RO(provider);
251
Dan Williams4d88a972015-05-31 14:41:48 -0400252static int flush_namespaces(struct device *dev, void *data)
253{
254 device_lock(dev);
255 device_unlock(dev);
256 return 0;
257}
258
259static int flush_regions_dimms(struct device *dev, void *data)
260{
261 device_lock(dev);
262 device_unlock(dev);
263 device_for_each_child(dev, NULL, flush_namespaces);
264 return 0;
265}
266
267static ssize_t wait_probe_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
269{
270 nd_synchronize();
271 device_for_each_child(dev, NULL, flush_regions_dimms);
272 return sprintf(buf, "1\n");
273}
274static DEVICE_ATTR_RO(wait_probe);
275
Dan Williams45def222015-04-26 19:26:48 -0400276static struct attribute *nvdimm_bus_attributes[] = {
Dan Williams62232e452015-06-08 14:27:06 -0400277 &dev_attr_commands.attr,
Dan Williams4d88a972015-05-31 14:41:48 -0400278 &dev_attr_wait_probe.attr,
Dan Williams45def222015-04-26 19:26:48 -0400279 &dev_attr_provider.attr,
280 NULL,
281};
282
283struct attribute_group nvdimm_bus_attribute_group = {
284 .attrs = nvdimm_bus_attributes,
285};
286EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
287
Dan Williams3d880022015-05-31 15:02:11 -0400288struct nvdimm_bus *__nvdimm_bus_register(struct device *parent,
289 struct nvdimm_bus_descriptor *nd_desc, struct module *module)
Dan Williamsb94d5232015-05-19 22:54:31 -0400290{
291 struct nvdimm_bus *nvdimm_bus;
292 int rc;
293
294 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
295 if (!nvdimm_bus)
296 return NULL;
Dan Williams45def222015-04-26 19:26:48 -0400297 INIT_LIST_HEAD(&nvdimm_bus->list);
Dan Williamseaf96152015-05-01 13:11:27 -0400298 init_waitqueue_head(&nvdimm_bus->probe_wait);
Dan Williamsb94d5232015-05-19 22:54:31 -0400299 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
Dan Williams3d880022015-05-31 15:02:11 -0400300 mutex_init(&nvdimm_bus->reconfig_mutex);
Dan Williamsb94d5232015-05-19 22:54:31 -0400301 if (nvdimm_bus->id < 0) {
302 kfree(nvdimm_bus);
303 return NULL;
304 }
305 nvdimm_bus->nd_desc = nd_desc;
Dan Williams3d880022015-05-31 15:02:11 -0400306 nvdimm_bus->module = module;
Dan Williamsb94d5232015-05-19 22:54:31 -0400307 nvdimm_bus->dev.parent = parent;
308 nvdimm_bus->dev.release = nvdimm_bus_release;
Dan Williams45def222015-04-26 19:26:48 -0400309 nvdimm_bus->dev.groups = nd_desc->attr_groups;
Dan Williamsb94d5232015-05-19 22:54:31 -0400310 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
311 rc = device_register(&nvdimm_bus->dev);
312 if (rc) {
313 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
Dan Williams45def222015-04-26 19:26:48 -0400314 goto err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400315 }
316
Dan Williams45def222015-04-26 19:26:48 -0400317 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
318 if (rc)
319 goto err;
320
321 mutex_lock(&nvdimm_bus_list_mutex);
322 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
323 mutex_unlock(&nvdimm_bus_list_mutex);
324
Dan Williamsb94d5232015-05-19 22:54:31 -0400325 return nvdimm_bus;
Dan Williams45def222015-04-26 19:26:48 -0400326 err:
327 put_device(&nvdimm_bus->dev);
328 return NULL;
Dan Williamsb94d5232015-05-19 22:54:31 -0400329}
Dan Williams3d880022015-05-31 15:02:11 -0400330EXPORT_SYMBOL_GPL(__nvdimm_bus_register);
Dan Williamsb94d5232015-05-19 22:54:31 -0400331
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400332static int child_unregister(struct device *dev, void *data)
333{
334 /*
335 * the singular ndctl class device per bus needs to be
336 * "device_destroy"ed, so skip it here
337 *
338 * i.e. remove classless children
339 */
340 if (dev->class)
341 /* pass */;
342 else
Dan Williams4d88a972015-05-31 14:41:48 -0400343 nd_device_unregister(dev, ND_SYNC);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400344 return 0;
345}
346
Dan Williamsb94d5232015-05-19 22:54:31 -0400347void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
348{
349 if (!nvdimm_bus)
350 return;
Dan Williams45def222015-04-26 19:26:48 -0400351
352 mutex_lock(&nvdimm_bus_list_mutex);
353 list_del_init(&nvdimm_bus->list);
354 mutex_unlock(&nvdimm_bus_list_mutex);
355
Dan Williams4d88a972015-05-31 14:41:48 -0400356 nd_synchronize();
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400357 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
Dan Williams45def222015-04-26 19:26:48 -0400358 nvdimm_bus_destroy_ndctl(nvdimm_bus);
359
Dan Williamsb94d5232015-05-19 22:54:31 -0400360 device_unregister(&nvdimm_bus->dev);
361}
362EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
363
Dan Williams45def222015-04-26 19:26:48 -0400364static __init int libnvdimm_init(void)
365{
Dan Williams4d88a972015-05-31 14:41:48 -0400366 int rc;
367
368 rc = nvdimm_bus_init();
369 if (rc)
370 return rc;
371 rc = nvdimm_init();
372 if (rc)
373 goto err_dimm;
Dan Williams3d880022015-05-31 15:02:11 -0400374 rc = nd_region_init();
375 if (rc)
376 goto err_region;
Dan Williams4d88a972015-05-31 14:41:48 -0400377 return 0;
Dan Williams3d880022015-05-31 15:02:11 -0400378 err_region:
379 nvdimm_exit();
Dan Williams4d88a972015-05-31 14:41:48 -0400380 err_dimm:
381 nvdimm_bus_exit();
382 return rc;
Dan Williams45def222015-04-26 19:26:48 -0400383}
384
385static __exit void libnvdimm_exit(void)
386{
387 WARN_ON(!list_empty(&nvdimm_bus_list));
Dan Williams3d880022015-05-31 15:02:11 -0400388 nd_region_exit();
Dan Williams4d88a972015-05-31 14:41:48 -0400389 nvdimm_exit();
Dan Williams45def222015-04-26 19:26:48 -0400390 nvdimm_bus_exit();
391}
392
Dan Williamsb94d5232015-05-19 22:54:31 -0400393MODULE_LICENSE("GPL v2");
394MODULE_AUTHOR("Intel Corporation");
Dan Williams45def222015-04-26 19:26:48 -0400395subsys_initcall(libnvdimm_init);
396module_exit(libnvdimm_exit);