2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
13 #include <linux/libnvdimm.h>
14 #include <linux/export.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/ctype.h>
18 #include <linux/ndctl.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
24 LIST_HEAD(nvdimm_bus_list);
25 DEFINE_MUTEX(nvdimm_bus_list_mutex);
26 static DEFINE_IDA(nd_ida);
28 void nvdimm_bus_lock(struct device *dev)
30 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
34 mutex_lock(&nvdimm_bus->reconfig_mutex);
36 EXPORT_SYMBOL(nvdimm_bus_lock);
38 void nvdimm_bus_unlock(struct device *dev)
40 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
44 mutex_unlock(&nvdimm_bus->reconfig_mutex);
46 EXPORT_SYMBOL(nvdimm_bus_unlock);
48 bool is_nvdimm_bus_locked(struct device *dev)
50 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
54 return mutex_is_locked(&nvdimm_bus->reconfig_mutex);
56 EXPORT_SYMBOL(is_nvdimm_bus_locked);
58 u64 nd_fletcher64(void *addr, size_t len, bool le)
65 for (i = 0; i < len / sizeof(u32); i++) {
66 lo32 += le ? le32_to_cpu((__le32) buf[i]) : buf[i];
70 return hi32 << 32 | lo32;
72 EXPORT_SYMBOL_GPL(nd_fletcher64);
74 static void nvdimm_bus_release(struct device *dev)
76 struct nvdimm_bus *nvdimm_bus;
78 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
79 ida_simple_remove(&nd_ida, nvdimm_bus->id);
83 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
85 struct nvdimm_bus *nvdimm_bus;
87 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
88 WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release);
91 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
93 struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus)
95 /* struct nvdimm_bus definition is private to libnvdimm */
96 return nvdimm_bus->nd_desc;
98 EXPORT_SYMBOL_GPL(to_nd_desc);
100 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
104 for (dev = nd_dev; dev; dev = dev->parent)
105 if (dev->release == nvdimm_bus_release)
107 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
109 return to_nvdimm_bus(dev);
113 static bool is_uuid_sep(char sep)
115 if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0')
120 static int nd_uuid_parse(struct device *dev, u8 *uuid_out, const char *buf,
123 const char *str = buf;
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]);
135 uuid[i] = (hex_to_bin(str[0]) << 4) | hex_to_bin(str[1]);
137 if (is_uuid_sep(*str))
141 memcpy(uuid_out, uuid, sizeof(uuid));
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
151 * Enforce that uuids can only be changed while the device is disabled
153 * LOCKING: expects device_lock() is held on entry
155 int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
164 rc = nd_uuid_parse(dev, uuid, buf, len);
169 *uuid_out = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
176 ssize_t nd_sector_size_show(unsigned long current_lbasize,
177 const unsigned long *supported, char *buf)
182 for (i = 0; supported[i]; i++)
183 if (current_lbasize == supported[i])
184 len += sprintf(buf + len, "[%ld] ", supported[i]);
186 len += sprintf(buf + len, "%ld ", supported[i]);
187 len += sprintf(buf + len, "\n");
191 ssize_t nd_sector_size_store(struct device *dev, const char *buf,
192 unsigned long *current_lbasize, const unsigned long *supported)
194 unsigned long lbasize;
200 rc = kstrtoul(buf, 0, &lbasize);
204 for (i = 0; supported[i]; i++)
205 if (lbasize == supported[i])
209 *current_lbasize = lbasize;
216 static ssize_t commands_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
220 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
221 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
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");
228 static DEVICE_ATTR_RO(commands);
230 static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
232 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
233 struct device *parent = nvdimm_bus->dev.parent;
235 if (nd_desc->provider_name)
236 return nd_desc->provider_name;
238 return dev_name(parent);
243 static ssize_t provider_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
246 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
248 return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
250 static DEVICE_ATTR_RO(provider);
252 static int flush_namespaces(struct device *dev, void *data)
259 static int flush_regions_dimms(struct device *dev, void *data)
263 device_for_each_child(dev, NULL, flush_namespaces);
267 static ssize_t wait_probe_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
271 device_for_each_child(dev, NULL, flush_regions_dimms);
272 return sprintf(buf, "1\n");
274 static DEVICE_ATTR_RO(wait_probe);
276 static struct attribute *nvdimm_bus_attributes[] = {
277 &dev_attr_commands.attr,
278 &dev_attr_wait_probe.attr,
279 &dev_attr_provider.attr,
283 struct attribute_group nvdimm_bus_attribute_group = {
284 .attrs = nvdimm_bus_attributes,
286 EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
288 struct nvdimm_bus *__nvdimm_bus_register(struct device *parent,
289 struct nvdimm_bus_descriptor *nd_desc, struct module *module)
291 struct nvdimm_bus *nvdimm_bus;
294 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
297 INIT_LIST_HEAD(&nvdimm_bus->list);
298 init_waitqueue_head(&nvdimm_bus->probe_wait);
299 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
300 mutex_init(&nvdimm_bus->reconfig_mutex);
301 if (nvdimm_bus->id < 0) {
305 nvdimm_bus->nd_desc = nd_desc;
306 nvdimm_bus->module = module;
307 nvdimm_bus->dev.parent = parent;
308 nvdimm_bus->dev.release = nvdimm_bus_release;
309 nvdimm_bus->dev.groups = nd_desc->attr_groups;
310 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
311 rc = device_register(&nvdimm_bus->dev);
313 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
317 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
321 mutex_lock(&nvdimm_bus_list_mutex);
322 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
323 mutex_unlock(&nvdimm_bus_list_mutex);
327 put_device(&nvdimm_bus->dev);
330 EXPORT_SYMBOL_GPL(__nvdimm_bus_register);
332 static int child_unregister(struct device *dev, void *data)
335 * the singular ndctl class device per bus needs to be
336 * "device_destroy"ed, so skip it here
338 * i.e. remove classless children
343 nd_device_unregister(dev, ND_SYNC);
347 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
352 mutex_lock(&nvdimm_bus_list_mutex);
353 list_del_init(&nvdimm_bus->list);
354 mutex_unlock(&nvdimm_bus_list_mutex);
357 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
358 nvdimm_bus_destroy_ndctl(nvdimm_bus);
360 device_unregister(&nvdimm_bus->dev);
362 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
364 static __init int libnvdimm_init(void)
368 rc = nvdimm_bus_init();
374 rc = nd_region_init();
385 static __exit void libnvdimm_exit(void)
387 WARN_ON(!list_empty(&nvdimm_bus_list));
393 MODULE_LICENSE("GPL v2");
394 MODULE_AUTHOR("Intel Corporation");
395 subsys_initcall(libnvdimm_init);
396 module_exit(libnvdimm_exit);