Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 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> |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 17 | #include <linux/ctype.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 18 | #include <linux/ndctl.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include "nd-core.h" |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 22 | #include "nd.h" |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 23 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 24 | LIST_HEAD(nvdimm_bus_list); |
| 25 | DEFINE_MUTEX(nvdimm_bus_list_mutex); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 26 | static DEFINE_IDA(nd_ida); |
| 27 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 28 | void 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 | } |
| 36 | EXPORT_SYMBOL(nvdimm_bus_lock); |
| 37 | |
| 38 | void 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 | } |
| 46 | EXPORT_SYMBOL(nvdimm_bus_unlock); |
| 47 | |
| 48 | bool 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 | } |
| 56 | EXPORT_SYMBOL(is_nvdimm_bus_locked); |
| 57 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 58 | u64 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 | } |
| 72 | EXPORT_SYMBOL_GPL(nd_fletcher64); |
| 73 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 74 | static 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 Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 83 | struct 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 | } |
| 91 | EXPORT_SYMBOL_GPL(to_nvdimm_bus); |
| 92 | |
| 93 | struct 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 | } |
| 98 | EXPORT_SYMBOL_GPL(to_nd_desc); |
| 99 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 100 | struct 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 Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 113 | static bool is_uuid_sep(char sep) |
| 114 | { |
| 115 | if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0') |
| 116 | return true; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | static 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 | */ |
| 155 | int 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 Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 176 | ssize_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 | |
| 191 | ssize_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 Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 216 | static 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 | } |
| 228 | static DEVICE_ATTR_RO(commands); |
| 229 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 230 | static 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 | |
| 243 | static 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 | } |
| 250 | static DEVICE_ATTR_RO(provider); |
| 251 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 252 | static int flush_namespaces(struct device *dev, void *data) |
| 253 | { |
| 254 | device_lock(dev); |
| 255 | device_unlock(dev); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static 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 | |
| 267 | static 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 | } |
| 274 | static DEVICE_ATTR_RO(wait_probe); |
| 275 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 276 | static struct attribute *nvdimm_bus_attributes[] = { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 277 | &dev_attr_commands.attr, |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 278 | &dev_attr_wait_probe.attr, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 279 | &dev_attr_provider.attr, |
| 280 | NULL, |
| 281 | }; |
| 282 | |
| 283 | struct attribute_group nvdimm_bus_attribute_group = { |
| 284 | .attrs = nvdimm_bus_attributes, |
| 285 | }; |
| 286 | EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group); |
| 287 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 288 | struct nvdimm_bus *__nvdimm_bus_register(struct device *parent, |
| 289 | struct nvdimm_bus_descriptor *nd_desc, struct module *module) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 290 | { |
| 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 Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 297 | INIT_LIST_HEAD(&nvdimm_bus->list); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 298 | init_waitqueue_head(&nvdimm_bus->probe_wait); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 299 | nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 300 | mutex_init(&nvdimm_bus->reconfig_mutex); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 301 | if (nvdimm_bus->id < 0) { |
| 302 | kfree(nvdimm_bus); |
| 303 | return NULL; |
| 304 | } |
| 305 | nvdimm_bus->nd_desc = nd_desc; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 306 | nvdimm_bus->module = module; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 307 | nvdimm_bus->dev.parent = parent; |
| 308 | nvdimm_bus->dev.release = nvdimm_bus_release; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 309 | nvdimm_bus->dev.groups = nd_desc->attr_groups; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 310 | 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 Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 314 | goto err; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 317 | 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 Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 325 | return nvdimm_bus; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 326 | err: |
| 327 | put_device(&nvdimm_bus->dev); |
| 328 | return NULL; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 329 | } |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 330 | EXPORT_SYMBOL_GPL(__nvdimm_bus_register); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 331 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 332 | static 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 Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 343 | nd_device_unregister(dev, ND_SYNC); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 347 | void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus) |
| 348 | { |
| 349 | if (!nvdimm_bus) |
| 350 | return; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 351 | |
| 352 | mutex_lock(&nvdimm_bus_list_mutex); |
| 353 | list_del_init(&nvdimm_bus->list); |
| 354 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 355 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 356 | nd_synchronize(); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 357 | device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 358 | nvdimm_bus_destroy_ndctl(nvdimm_bus); |
| 359 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 360 | device_unregister(&nvdimm_bus->dev); |
| 361 | } |
| 362 | EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); |
| 363 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 364 | static __init int libnvdimm_init(void) |
| 365 | { |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 366 | 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 Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 374 | rc = nd_region_init(); |
| 375 | if (rc) |
| 376 | goto err_region; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 377 | return 0; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 378 | err_region: |
| 379 | nvdimm_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 380 | err_dimm: |
| 381 | nvdimm_bus_exit(); |
| 382 | return rc; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static __exit void libnvdimm_exit(void) |
| 386 | { |
| 387 | WARN_ON(!list_empty(&nvdimm_bus_list)); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 388 | nd_region_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 389 | nvdimm_exit(); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 390 | nvdimm_bus_exit(); |
| 391 | } |
| 392 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 393 | MODULE_LICENSE("GPL v2"); |
| 394 | MODULE_AUTHOR("Intel Corporation"); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 395 | subsys_initcall(libnvdimm_init); |
| 396 | module_exit(libnvdimm_exit); |