Dan Williams | 45def22 | 2015-04-26 19:26:48 -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 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 17 | #include <linux/fcntl.h> |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 18 | #include <linux/async.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 19 | #include <linux/ndctl.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/io.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 24 | #include <linux/mm.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 25 | #include <linux/nd.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 26 | #include "nd-core.h" |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 27 | #include "nd.h" |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 28 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 29 | int nvdimm_major; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 30 | static int nvdimm_bus_major; |
| 31 | static struct class *nd_class; |
| 32 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 33 | static int to_nd_device_type(struct device *dev) |
| 34 | { |
| 35 | if (is_nvdimm(dev)) |
| 36 | return ND_DEVICE_DIMM; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 37 | else if (is_nd_pmem(dev)) |
| 38 | return ND_DEVICE_REGION_PMEM; |
| 39 | else if (is_nd_blk(dev)) |
| 40 | return ND_DEVICE_REGION_BLK; |
| 41 | else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent)) |
| 42 | return nd_region_to_nstype(to_nd_region(dev->parent)); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 48 | { |
| 49 | return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT, |
| 50 | to_nd_device_type(dev)); |
| 51 | } |
| 52 | |
| 53 | static int nvdimm_bus_match(struct device *dev, struct device_driver *drv) |
| 54 | { |
| 55 | struct nd_device_driver *nd_drv = to_nd_device_driver(drv); |
| 56 | |
| 57 | return test_bit(to_nd_device_type(dev), &nd_drv->type); |
| 58 | } |
| 59 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 60 | static struct module *to_bus_provider(struct device *dev) |
| 61 | { |
| 62 | /* pin bus providers while regions are enabled */ |
| 63 | if (is_nd_pmem(dev) || is_nd_blk(dev)) { |
| 64 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 65 | |
| 66 | return nvdimm_bus->module; |
| 67 | } |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 71 | static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus) |
| 72 | { |
| 73 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 74 | nvdimm_bus->probe_active++; |
| 75 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 76 | } |
| 77 | |
| 78 | static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus) |
| 79 | { |
| 80 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 81 | if (--nvdimm_bus->probe_active == 0) |
| 82 | wake_up(&nvdimm_bus->probe_wait); |
| 83 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 84 | } |
| 85 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 86 | static int nvdimm_bus_probe(struct device *dev) |
| 87 | { |
| 88 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 89 | struct module *provider = to_bus_provider(dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 90 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 91 | int rc; |
| 92 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 93 | if (!try_module_get(provider)) |
| 94 | return -ENXIO; |
| 95 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 96 | nvdimm_bus_probe_start(nvdimm_bus); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 97 | rc = nd_drv->probe(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 98 | if (rc == 0) |
| 99 | nd_region_probe_success(nvdimm_bus, dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame^] | 100 | else |
| 101 | nd_region_disable(nvdimm_bus, dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 102 | nvdimm_bus_probe_end(nvdimm_bus); |
| 103 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 104 | dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name, |
| 105 | dev_name(dev), rc); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 106 | if (rc != 0) |
| 107 | module_put(provider); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 108 | return rc; |
| 109 | } |
| 110 | |
| 111 | static int nvdimm_bus_remove(struct device *dev) |
| 112 | { |
| 113 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 114 | struct module *provider = to_bus_provider(dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 115 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 116 | int rc; |
| 117 | |
| 118 | rc = nd_drv->remove(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 119 | nd_region_disable(nvdimm_bus, dev); |
| 120 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 121 | dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name, |
| 122 | dev_name(dev), rc); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 123 | module_put(provider); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | static struct bus_type nvdimm_bus_type = { |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 128 | .name = "nd", |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 129 | .uevent = nvdimm_bus_uevent, |
| 130 | .match = nvdimm_bus_match, |
| 131 | .probe = nvdimm_bus_probe, |
| 132 | .remove = nvdimm_bus_remove, |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 133 | }; |
| 134 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 135 | static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain); |
| 136 | |
| 137 | void nd_synchronize(void) |
| 138 | { |
| 139 | async_synchronize_full_domain(&nd_async_domain); |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(nd_synchronize); |
| 142 | |
| 143 | static void nd_async_device_register(void *d, async_cookie_t cookie) |
| 144 | { |
| 145 | struct device *dev = d; |
| 146 | |
| 147 | if (device_add(dev) != 0) { |
| 148 | dev_err(dev, "%s: failed\n", __func__); |
| 149 | put_device(dev); |
| 150 | } |
| 151 | put_device(dev); |
| 152 | } |
| 153 | |
| 154 | static void nd_async_device_unregister(void *d, async_cookie_t cookie) |
| 155 | { |
| 156 | struct device *dev = d; |
| 157 | |
| 158 | device_unregister(dev); |
| 159 | put_device(dev); |
| 160 | } |
| 161 | |
| 162 | void nd_device_register(struct device *dev) |
| 163 | { |
| 164 | dev->bus = &nvdimm_bus_type; |
| 165 | device_initialize(dev); |
| 166 | get_device(dev); |
| 167 | async_schedule_domain(nd_async_device_register, dev, |
| 168 | &nd_async_domain); |
| 169 | } |
| 170 | EXPORT_SYMBOL(nd_device_register); |
| 171 | |
| 172 | void nd_device_unregister(struct device *dev, enum nd_async_mode mode) |
| 173 | { |
| 174 | switch (mode) { |
| 175 | case ND_ASYNC: |
| 176 | get_device(dev); |
| 177 | async_schedule_domain(nd_async_device_unregister, dev, |
| 178 | &nd_async_domain); |
| 179 | break; |
| 180 | case ND_SYNC: |
| 181 | nd_synchronize(); |
| 182 | device_unregister(dev); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | EXPORT_SYMBOL(nd_device_unregister); |
| 187 | |
| 188 | /** |
| 189 | * __nd_driver_register() - register a region or a namespace driver |
| 190 | * @nd_drv: driver to register |
| 191 | * @owner: automatically set by nd_driver_register() macro |
| 192 | * @mod_name: automatically set by nd_driver_register() macro |
| 193 | */ |
| 194 | int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner, |
| 195 | const char *mod_name) |
| 196 | { |
| 197 | struct device_driver *drv = &nd_drv->drv; |
| 198 | |
| 199 | if (!nd_drv->type) { |
| 200 | pr_debug("driver type bitmask not set (%pf)\n", |
| 201 | __builtin_return_address(0)); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | if (!nd_drv->probe || !nd_drv->remove) { |
| 206 | pr_debug("->probe() and ->remove() must be specified\n"); |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
| 210 | drv->bus = &nvdimm_bus_type; |
| 211 | drv->owner = owner; |
| 212 | drv->mod_name = mod_name; |
| 213 | |
| 214 | return driver_register(drv); |
| 215 | } |
| 216 | EXPORT_SYMBOL(__nd_driver_register); |
| 217 | |
| 218 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 219 | char *buf) |
| 220 | { |
| 221 | return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n", |
| 222 | to_nd_device_type(dev)); |
| 223 | } |
| 224 | static DEVICE_ATTR_RO(modalias); |
| 225 | |
| 226 | static ssize_t devtype_show(struct device *dev, struct device_attribute *attr, |
| 227 | char *buf) |
| 228 | { |
| 229 | return sprintf(buf, "%s\n", dev->type->name); |
| 230 | } |
| 231 | static DEVICE_ATTR_RO(devtype); |
| 232 | |
| 233 | static struct attribute *nd_device_attributes[] = { |
| 234 | &dev_attr_modalias.attr, |
| 235 | &dev_attr_devtype.attr, |
| 236 | NULL, |
| 237 | }; |
| 238 | |
| 239 | /** |
| 240 | * nd_device_attribute_group - generic attributes for all devices on an nd bus |
| 241 | */ |
| 242 | struct attribute_group nd_device_attribute_group = { |
| 243 | .attrs = nd_device_attributes, |
| 244 | }; |
| 245 | EXPORT_SYMBOL_GPL(nd_device_attribute_group); |
| 246 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 247 | int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus) |
| 248 | { |
| 249 | dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id); |
| 250 | struct device *dev; |
| 251 | |
| 252 | dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus, |
| 253 | "ndctl%d", nvdimm_bus->id); |
| 254 | |
| 255 | if (IS_ERR(dev)) { |
| 256 | dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n", |
| 257 | nvdimm_bus->id, PTR_ERR(dev)); |
| 258 | return PTR_ERR(dev); |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus) |
| 264 | { |
| 265 | device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id)); |
| 266 | } |
| 267 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 268 | static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = { |
| 269 | [ND_CMD_IMPLEMENTED] = { }, |
| 270 | [ND_CMD_SMART] = { |
| 271 | .out_num = 2, |
| 272 | .out_sizes = { 4, 8, }, |
| 273 | }, |
| 274 | [ND_CMD_SMART_THRESHOLD] = { |
| 275 | .out_num = 2, |
| 276 | .out_sizes = { 4, 8, }, |
| 277 | }, |
| 278 | [ND_CMD_DIMM_FLAGS] = { |
| 279 | .out_num = 2, |
| 280 | .out_sizes = { 4, 4 }, |
| 281 | }, |
| 282 | [ND_CMD_GET_CONFIG_SIZE] = { |
| 283 | .out_num = 3, |
| 284 | .out_sizes = { 4, 4, 4, }, |
| 285 | }, |
| 286 | [ND_CMD_GET_CONFIG_DATA] = { |
| 287 | .in_num = 2, |
| 288 | .in_sizes = { 4, 4, }, |
| 289 | .out_num = 2, |
| 290 | .out_sizes = { 4, UINT_MAX, }, |
| 291 | }, |
| 292 | [ND_CMD_SET_CONFIG_DATA] = { |
| 293 | .in_num = 3, |
| 294 | .in_sizes = { 4, 4, UINT_MAX, }, |
| 295 | .out_num = 1, |
| 296 | .out_sizes = { 4, }, |
| 297 | }, |
| 298 | [ND_CMD_VENDOR] = { |
| 299 | .in_num = 3, |
| 300 | .in_sizes = { 4, 4, UINT_MAX, }, |
| 301 | .out_num = 3, |
| 302 | .out_sizes = { 4, 4, UINT_MAX, }, |
| 303 | }, |
| 304 | }; |
| 305 | |
| 306 | const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd) |
| 307 | { |
| 308 | if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs)) |
| 309 | return &__nd_cmd_dimm_descs[cmd]; |
| 310 | return NULL; |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc); |
| 313 | |
| 314 | static const struct nd_cmd_desc __nd_cmd_bus_descs[] = { |
| 315 | [ND_CMD_IMPLEMENTED] = { }, |
| 316 | [ND_CMD_ARS_CAP] = { |
| 317 | .in_num = 2, |
| 318 | .in_sizes = { 8, 8, }, |
| 319 | .out_num = 2, |
| 320 | .out_sizes = { 4, 4, }, |
| 321 | }, |
| 322 | [ND_CMD_ARS_START] = { |
| 323 | .in_num = 4, |
| 324 | .in_sizes = { 8, 8, 2, 6, }, |
| 325 | .out_num = 1, |
| 326 | .out_sizes = { 4, }, |
| 327 | }, |
| 328 | [ND_CMD_ARS_STATUS] = { |
| 329 | .out_num = 2, |
| 330 | .out_sizes = { 4, UINT_MAX, }, |
| 331 | }, |
| 332 | }; |
| 333 | |
| 334 | const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd) |
| 335 | { |
| 336 | if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs)) |
| 337 | return &__nd_cmd_bus_descs[cmd]; |
| 338 | return NULL; |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(nd_cmd_bus_desc); |
| 341 | |
| 342 | u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd, |
| 343 | const struct nd_cmd_desc *desc, int idx, void *buf) |
| 344 | { |
| 345 | if (idx >= desc->in_num) |
| 346 | return UINT_MAX; |
| 347 | |
| 348 | if (desc->in_sizes[idx] < UINT_MAX) |
| 349 | return desc->in_sizes[idx]; |
| 350 | |
| 351 | if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) { |
| 352 | struct nd_cmd_set_config_hdr *hdr = buf; |
| 353 | |
| 354 | return hdr->in_length; |
| 355 | } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) { |
| 356 | struct nd_cmd_vendor_hdr *hdr = buf; |
| 357 | |
| 358 | return hdr->in_length; |
| 359 | } |
| 360 | |
| 361 | return UINT_MAX; |
| 362 | } |
| 363 | EXPORT_SYMBOL_GPL(nd_cmd_in_size); |
| 364 | |
| 365 | u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd, |
| 366 | const struct nd_cmd_desc *desc, int idx, const u32 *in_field, |
| 367 | const u32 *out_field) |
| 368 | { |
| 369 | if (idx >= desc->out_num) |
| 370 | return UINT_MAX; |
| 371 | |
| 372 | if (desc->out_sizes[idx] < UINT_MAX) |
| 373 | return desc->out_sizes[idx]; |
| 374 | |
| 375 | if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1) |
| 376 | return in_field[1]; |
| 377 | else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) |
| 378 | return out_field[1]; |
| 379 | else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 1) |
| 380 | return ND_CMD_ARS_STATUS_MAX; |
| 381 | |
| 382 | return UINT_MAX; |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(nd_cmd_out_size); |
| 385 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame^] | 386 | void wait_nvdimm_bus_probe_idle(struct device *dev) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 387 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame^] | 388 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 389 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 390 | do { |
| 391 | if (nvdimm_bus->probe_active == 0) |
| 392 | break; |
| 393 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 394 | wait_event(nvdimm_bus->probe_wait, |
| 395 | nvdimm_bus->probe_active == 0); |
| 396 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 397 | } while (true); |
| 398 | } |
| 399 | |
| 400 | /* set_config requires an idle interleave set */ |
| 401 | static int nd_cmd_clear_to_send(struct nvdimm *nvdimm, unsigned int cmd) |
| 402 | { |
| 403 | struct nvdimm_bus *nvdimm_bus; |
| 404 | |
| 405 | if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA) |
| 406 | return 0; |
| 407 | |
| 408 | nvdimm_bus = walk_to_nvdimm_bus(&nvdimm->dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame^] | 409 | wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 410 | |
| 411 | if (atomic_read(&nvdimm->busy)) |
| 412 | return -EBUSY; |
| 413 | return 0; |
| 414 | } |
| 415 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 416 | static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, |
| 417 | int read_only, unsigned int ioctl_cmd, unsigned long arg) |
| 418 | { |
| 419 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 420 | size_t buf_len = 0, in_len = 0, out_len = 0; |
| 421 | static char out_env[ND_CMD_MAX_ENVELOPE]; |
| 422 | static char in_env[ND_CMD_MAX_ENVELOPE]; |
| 423 | const struct nd_cmd_desc *desc = NULL; |
| 424 | unsigned int cmd = _IOC_NR(ioctl_cmd); |
| 425 | void __user *p = (void __user *) arg; |
| 426 | struct device *dev = &nvdimm_bus->dev; |
| 427 | const char *cmd_name, *dimm_name; |
| 428 | unsigned long dsm_mask; |
| 429 | void *buf; |
| 430 | int rc, i; |
| 431 | |
| 432 | if (nvdimm) { |
| 433 | desc = nd_cmd_dimm_desc(cmd); |
| 434 | cmd_name = nvdimm_cmd_name(cmd); |
| 435 | dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0; |
| 436 | dimm_name = dev_name(&nvdimm->dev); |
| 437 | } else { |
| 438 | desc = nd_cmd_bus_desc(cmd); |
| 439 | cmd_name = nvdimm_bus_cmd_name(cmd); |
| 440 | dsm_mask = nd_desc->dsm_mask; |
| 441 | dimm_name = "bus"; |
| 442 | } |
| 443 | |
| 444 | if (!desc || (desc->out_num + desc->in_num == 0) || |
| 445 | !test_bit(cmd, &dsm_mask)) |
| 446 | return -ENOTTY; |
| 447 | |
| 448 | /* fail write commands (when read-only) */ |
| 449 | if (read_only) |
| 450 | switch (ioctl_cmd) { |
| 451 | case ND_IOCTL_VENDOR: |
| 452 | case ND_IOCTL_SET_CONFIG_DATA: |
| 453 | case ND_IOCTL_ARS_START: |
| 454 | dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n", |
| 455 | nvdimm ? nvdimm_cmd_name(cmd) |
| 456 | : nvdimm_bus_cmd_name(cmd)); |
| 457 | return -EPERM; |
| 458 | default: |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | /* process an input envelope */ |
| 463 | for (i = 0; i < desc->in_num; i++) { |
| 464 | u32 in_size, copy; |
| 465 | |
| 466 | in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env); |
| 467 | if (in_size == UINT_MAX) { |
| 468 | dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n", |
| 469 | __func__, dimm_name, cmd_name, i); |
| 470 | return -ENXIO; |
| 471 | } |
| 472 | if (!access_ok(VERIFY_READ, p + in_len, in_size)) |
| 473 | return -EFAULT; |
| 474 | if (in_len < sizeof(in_env)) |
| 475 | copy = min_t(u32, sizeof(in_env) - in_len, in_size); |
| 476 | else |
| 477 | copy = 0; |
| 478 | if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) |
| 479 | return -EFAULT; |
| 480 | in_len += in_size; |
| 481 | } |
| 482 | |
| 483 | /* process an output envelope */ |
| 484 | for (i = 0; i < desc->out_num; i++) { |
| 485 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, |
| 486 | (u32 *) in_env, (u32 *) out_env); |
| 487 | u32 copy; |
| 488 | |
| 489 | if (out_size == UINT_MAX) { |
| 490 | dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n", |
| 491 | __func__, dimm_name, cmd_name, i); |
| 492 | return -EFAULT; |
| 493 | } |
| 494 | if (!access_ok(VERIFY_WRITE, p + in_len + out_len, out_size)) |
| 495 | return -EFAULT; |
| 496 | if (out_len < sizeof(out_env)) |
| 497 | copy = min_t(u32, sizeof(out_env) - out_len, out_size); |
| 498 | else |
| 499 | copy = 0; |
| 500 | if (copy && copy_from_user(&out_env[out_len], |
| 501 | p + in_len + out_len, copy)) |
| 502 | return -EFAULT; |
| 503 | out_len += out_size; |
| 504 | } |
| 505 | |
| 506 | buf_len = out_len + in_len; |
| 507 | if (!access_ok(VERIFY_WRITE, p, sizeof(buf_len))) |
| 508 | return -EFAULT; |
| 509 | |
| 510 | if (buf_len > ND_IOCTL_MAX_BUFLEN) { |
| 511 | dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__, |
| 512 | dimm_name, cmd_name, buf_len, |
| 513 | ND_IOCTL_MAX_BUFLEN); |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | |
| 517 | buf = vmalloc(buf_len); |
| 518 | if (!buf) |
| 519 | return -ENOMEM; |
| 520 | |
| 521 | if (copy_from_user(buf, p, buf_len)) { |
| 522 | rc = -EFAULT; |
| 523 | goto out; |
| 524 | } |
| 525 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 526 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 527 | rc = nd_cmd_clear_to_send(nvdimm, cmd); |
| 528 | if (rc) |
| 529 | goto out_unlock; |
| 530 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 531 | rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len); |
| 532 | if (rc < 0) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 533 | goto out_unlock; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 534 | if (copy_to_user(p, buf, buf_len)) |
| 535 | rc = -EFAULT; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 536 | out_unlock: |
| 537 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 538 | out: |
| 539 | vfree(buf); |
| 540 | return rc; |
| 541 | } |
| 542 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 543 | static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 544 | { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 545 | long id = (long) file->private_data; |
| 546 | int rc = -ENXIO, read_only; |
| 547 | struct nvdimm_bus *nvdimm_bus; |
| 548 | |
| 549 | read_only = (O_RDWR != (file->f_flags & O_ACCMODE)); |
| 550 | mutex_lock(&nvdimm_bus_list_mutex); |
| 551 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { |
| 552 | if (nvdimm_bus->id == id) { |
| 553 | rc = __nd_ioctl(nvdimm_bus, NULL, read_only, cmd, arg); |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 558 | |
| 559 | return rc; |
| 560 | } |
| 561 | |
| 562 | static int match_dimm(struct device *dev, void *data) |
| 563 | { |
| 564 | long id = (long) data; |
| 565 | |
| 566 | if (is_nvdimm(dev)) { |
| 567 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 568 | |
| 569 | return nvdimm->id == id; |
| 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 576 | { |
| 577 | int rc = -ENXIO, read_only; |
| 578 | struct nvdimm_bus *nvdimm_bus; |
| 579 | |
| 580 | read_only = (O_RDWR != (file->f_flags & O_ACCMODE)); |
| 581 | mutex_lock(&nvdimm_bus_list_mutex); |
| 582 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { |
| 583 | struct device *dev = device_find_child(&nvdimm_bus->dev, |
| 584 | file->private_data, match_dimm); |
| 585 | struct nvdimm *nvdimm; |
| 586 | |
| 587 | if (!dev) |
| 588 | continue; |
| 589 | |
| 590 | nvdimm = to_nvdimm(dev); |
| 591 | rc = __nd_ioctl(nvdimm_bus, nvdimm, read_only, cmd, arg); |
| 592 | put_device(dev); |
| 593 | break; |
| 594 | } |
| 595 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 596 | |
| 597 | return rc; |
| 598 | } |
| 599 | |
| 600 | static int nd_open(struct inode *inode, struct file *file) |
| 601 | { |
| 602 | long minor = iminor(inode); |
| 603 | |
| 604 | file->private_data = (void *) minor; |
| 605 | return 0; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static const struct file_operations nvdimm_bus_fops = { |
| 609 | .owner = THIS_MODULE, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 610 | .open = nd_open, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 611 | .unlocked_ioctl = nd_ioctl, |
| 612 | .compat_ioctl = nd_ioctl, |
| 613 | .llseek = noop_llseek, |
| 614 | }; |
| 615 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 616 | static const struct file_operations nvdimm_fops = { |
| 617 | .owner = THIS_MODULE, |
| 618 | .open = nd_open, |
| 619 | .unlocked_ioctl = nvdimm_ioctl, |
| 620 | .compat_ioctl = nvdimm_ioctl, |
| 621 | .llseek = noop_llseek, |
| 622 | }; |
| 623 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 624 | int __init nvdimm_bus_init(void) |
| 625 | { |
| 626 | int rc; |
| 627 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 628 | rc = bus_register(&nvdimm_bus_type); |
| 629 | if (rc) |
| 630 | return rc; |
| 631 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 632 | rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops); |
| 633 | if (rc < 0) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 634 | goto err_bus_chrdev; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 635 | nvdimm_bus_major = rc; |
| 636 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 637 | rc = register_chrdev(0, "dimmctl", &nvdimm_fops); |
| 638 | if (rc < 0) |
| 639 | goto err_dimm_chrdev; |
| 640 | nvdimm_major = rc; |
| 641 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 642 | nd_class = class_create(THIS_MODULE, "nd"); |
| 643 | if (IS_ERR(nd_class)) |
| 644 | goto err_class; |
| 645 | |
| 646 | return 0; |
| 647 | |
| 648 | err_class: |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 649 | unregister_chrdev(nvdimm_major, "dimmctl"); |
| 650 | err_dimm_chrdev: |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 651 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 652 | err_bus_chrdev: |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 653 | bus_unregister(&nvdimm_bus_type); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 654 | |
| 655 | return rc; |
| 656 | } |
| 657 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 658 | void nvdimm_bus_exit(void) |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 659 | { |
| 660 | class_destroy(nd_class); |
| 661 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 662 | unregister_chrdev(nvdimm_major, "dimmctl"); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 663 | bus_unregister(&nvdimm_bus_type); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 664 | } |