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 | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 17 | #include <linux/blkdev.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 18 | #include <linux/fcntl.h> |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 19 | #include <linux/async.h> |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 20 | #include <linux/genhd.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 21 | #include <linux/ndctl.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 22 | #include <linux/sched.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/io.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 26 | #include <linux/mm.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 27 | #include <linux/nd.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 28 | #include "nd-core.h" |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 29 | #include "nd.h" |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 30 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 31 | int nvdimm_major; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 32 | static int nvdimm_bus_major; |
| 33 | static struct class *nd_class; |
| 34 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 35 | static int to_nd_device_type(struct device *dev) |
| 36 | { |
| 37 | if (is_nvdimm(dev)) |
| 38 | return ND_DEVICE_DIMM; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 39 | else if (is_nd_pmem(dev)) |
| 40 | return ND_DEVICE_REGION_PMEM; |
| 41 | else if (is_nd_blk(dev)) |
| 42 | return ND_DEVICE_REGION_BLK; |
| 43 | else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent)) |
| 44 | return nd_region_to_nstype(to_nd_region(dev->parent)); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 50 | { |
Toshi Kani | 41d7a6d | 2015-06-19 12:18:33 -0600 | [diff] [blame] | 51 | /* |
| 52 | * Ensure that region devices always have their numa node set as |
| 53 | * early as possible. |
| 54 | */ |
| 55 | if (is_nd_pmem(dev) || is_nd_blk(dev)) |
| 56 | set_dev_node(dev, to_nd_region(dev)->numa_node); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 57 | return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT, |
| 58 | to_nd_device_type(dev)); |
| 59 | } |
| 60 | |
| 61 | static int nvdimm_bus_match(struct device *dev, struct device_driver *drv) |
| 62 | { |
| 63 | struct nd_device_driver *nd_drv = to_nd_device_driver(drv); |
| 64 | |
Dan Williams | 82ec2ba | 2016-02-15 09:22:36 +0100 | [diff] [blame] | 65 | return !!test_bit(to_nd_device_type(dev), &nd_drv->type); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 68 | static struct module *to_bus_provider(struct device *dev) |
| 69 | { |
| 70 | /* pin bus providers while regions are enabled */ |
| 71 | if (is_nd_pmem(dev) || is_nd_blk(dev)) { |
| 72 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 73 | |
| 74 | return nvdimm_bus->module; |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 79 | static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus) |
| 80 | { |
| 81 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 82 | nvdimm_bus->probe_active++; |
| 83 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 84 | } |
| 85 | |
| 86 | static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus) |
| 87 | { |
| 88 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 89 | if (--nvdimm_bus->probe_active == 0) |
| 90 | wake_up(&nvdimm_bus->probe_wait); |
| 91 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 92 | } |
| 93 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 94 | static int nvdimm_bus_probe(struct device *dev) |
| 95 | { |
| 96 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 97 | struct module *provider = to_bus_provider(dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 98 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 99 | int rc; |
| 100 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 101 | if (!try_module_get(provider)) |
| 102 | return -ENXIO; |
| 103 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 104 | nvdimm_bus_probe_start(nvdimm_bus); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 105 | rc = nd_drv->probe(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 106 | if (rc == 0) |
| 107 | nd_region_probe_success(nvdimm_bus, dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 108 | else |
| 109 | nd_region_disable(nvdimm_bus, dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 110 | nvdimm_bus_probe_end(nvdimm_bus); |
| 111 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 112 | dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name, |
| 113 | dev_name(dev), rc); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 114 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 115 | if (rc != 0) |
| 116 | module_put(provider); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | static int nvdimm_bus_remove(struct device *dev) |
| 121 | { |
| 122 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 123 | struct module *provider = to_bus_provider(dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 124 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 125 | int rc; |
| 126 | |
| 127 | rc = nd_drv->remove(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 128 | nd_region_disable(nvdimm_bus, dev); |
| 129 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 130 | dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name, |
| 131 | dev_name(dev), rc); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 132 | module_put(provider); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 133 | return rc; |
| 134 | } |
| 135 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 136 | void nd_device_notify(struct device *dev, enum nvdimm_event event) |
| 137 | { |
| 138 | device_lock(dev); |
| 139 | if (dev->driver) { |
| 140 | struct nd_device_driver *nd_drv; |
| 141 | |
| 142 | nd_drv = to_nd_device_driver(dev->driver); |
| 143 | if (nd_drv->notify) |
| 144 | nd_drv->notify(dev, event); |
| 145 | } |
| 146 | device_unlock(dev); |
| 147 | } |
| 148 | EXPORT_SYMBOL(nd_device_notify); |
| 149 | |
| 150 | void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event) |
| 151 | { |
| 152 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
| 153 | |
| 154 | if (!nvdimm_bus) |
| 155 | return; |
| 156 | |
| 157 | /* caller is responsible for holding a reference on the device */ |
| 158 | nd_device_notify(&nd_region->dev, event); |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(nvdimm_region_notify); |
| 161 | |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 162 | long nvdimm_clear_poison(struct device *dev, phys_addr_t phys, |
| 163 | unsigned int len) |
| 164 | { |
| 165 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 166 | struct nvdimm_bus_descriptor *nd_desc; |
| 167 | struct nd_cmd_clear_error clear_err; |
| 168 | struct nd_cmd_ars_cap ars_cap; |
| 169 | u32 clear_err_unit, mask; |
| 170 | int cmd_rc, rc; |
| 171 | |
| 172 | if (!nvdimm_bus) |
| 173 | return -ENXIO; |
| 174 | |
| 175 | nd_desc = nvdimm_bus->nd_desc; |
| 176 | if (!nd_desc->ndctl) |
| 177 | return -ENXIO; |
| 178 | |
| 179 | memset(&ars_cap, 0, sizeof(ars_cap)); |
| 180 | ars_cap.address = phys; |
| 181 | ars_cap.length = len; |
| 182 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap, |
| 183 | sizeof(ars_cap), &cmd_rc); |
| 184 | if (rc < 0) |
| 185 | return rc; |
| 186 | if (cmd_rc < 0) |
| 187 | return cmd_rc; |
| 188 | clear_err_unit = ars_cap.clear_err_unit; |
| 189 | if (!clear_err_unit || !is_power_of_2(clear_err_unit)) |
| 190 | return -ENXIO; |
| 191 | |
| 192 | mask = clear_err_unit - 1; |
| 193 | if ((phys | len) & mask) |
| 194 | return -ENXIO; |
| 195 | memset(&clear_err, 0, sizeof(clear_err)); |
| 196 | clear_err.address = phys; |
| 197 | clear_err.length = len; |
| 198 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err, |
| 199 | sizeof(clear_err), &cmd_rc); |
| 200 | if (rc < 0) |
| 201 | return rc; |
| 202 | if (cmd_rc < 0) |
| 203 | return cmd_rc; |
| 204 | return clear_err.cleared; |
| 205 | } |
| 206 | EXPORT_SYMBOL_GPL(nvdimm_clear_poison); |
| 207 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 208 | static struct bus_type nvdimm_bus_type = { |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 209 | .name = "nd", |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 210 | .uevent = nvdimm_bus_uevent, |
| 211 | .match = nvdimm_bus_match, |
| 212 | .probe = nvdimm_bus_probe, |
| 213 | .remove = nvdimm_bus_remove, |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 214 | }; |
| 215 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 216 | static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain); |
| 217 | |
| 218 | void nd_synchronize(void) |
| 219 | { |
| 220 | async_synchronize_full_domain(&nd_async_domain); |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(nd_synchronize); |
| 223 | |
| 224 | static void nd_async_device_register(void *d, async_cookie_t cookie) |
| 225 | { |
| 226 | struct device *dev = d; |
| 227 | |
| 228 | if (device_add(dev) != 0) { |
| 229 | dev_err(dev, "%s: failed\n", __func__); |
| 230 | put_device(dev); |
| 231 | } |
| 232 | put_device(dev); |
| 233 | } |
| 234 | |
| 235 | static void nd_async_device_unregister(void *d, async_cookie_t cookie) |
| 236 | { |
| 237 | struct device *dev = d; |
| 238 | |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 239 | /* flush bus operations before delete */ |
| 240 | nvdimm_bus_lock(dev); |
| 241 | nvdimm_bus_unlock(dev); |
| 242 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 243 | device_unregister(dev); |
| 244 | put_device(dev); |
| 245 | } |
| 246 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 247 | void __nd_device_register(struct device *dev) |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 248 | { |
| 249 | dev->bus = &nvdimm_bus_type; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 250 | get_device(dev); |
| 251 | async_schedule_domain(nd_async_device_register, dev, |
| 252 | &nd_async_domain); |
| 253 | } |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 254 | |
| 255 | void nd_device_register(struct device *dev) |
| 256 | { |
| 257 | device_initialize(dev); |
| 258 | __nd_device_register(dev); |
| 259 | } |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 260 | EXPORT_SYMBOL(nd_device_register); |
| 261 | |
| 262 | void nd_device_unregister(struct device *dev, enum nd_async_mode mode) |
| 263 | { |
| 264 | switch (mode) { |
| 265 | case ND_ASYNC: |
| 266 | get_device(dev); |
| 267 | async_schedule_domain(nd_async_device_unregister, dev, |
| 268 | &nd_async_domain); |
| 269 | break; |
| 270 | case ND_SYNC: |
| 271 | nd_synchronize(); |
| 272 | device_unregister(dev); |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | EXPORT_SYMBOL(nd_device_unregister); |
| 277 | |
| 278 | /** |
| 279 | * __nd_driver_register() - register a region or a namespace driver |
| 280 | * @nd_drv: driver to register |
| 281 | * @owner: automatically set by nd_driver_register() macro |
| 282 | * @mod_name: automatically set by nd_driver_register() macro |
| 283 | */ |
| 284 | int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner, |
| 285 | const char *mod_name) |
| 286 | { |
| 287 | struct device_driver *drv = &nd_drv->drv; |
| 288 | |
| 289 | if (!nd_drv->type) { |
| 290 | pr_debug("driver type bitmask not set (%pf)\n", |
| 291 | __builtin_return_address(0)); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | |
| 295 | if (!nd_drv->probe || !nd_drv->remove) { |
| 296 | pr_debug("->probe() and ->remove() must be specified\n"); |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
| 300 | drv->bus = &nvdimm_bus_type; |
| 301 | drv->owner = owner; |
| 302 | drv->mod_name = mod_name; |
| 303 | |
| 304 | return driver_register(drv); |
| 305 | } |
| 306 | EXPORT_SYMBOL(__nd_driver_register); |
| 307 | |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 308 | int nvdimm_revalidate_disk(struct gendisk *disk) |
| 309 | { |
| 310 | struct device *dev = disk->driverfs_dev; |
| 311 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 312 | const char *pol = nd_region->ro ? "only" : "write"; |
| 313 | |
| 314 | if (nd_region->ro == get_disk_ro(disk)) |
| 315 | return 0; |
| 316 | |
| 317 | dev_info(dev, "%s read-%s, marking %s read-%s\n", |
| 318 | dev_name(&nd_region->dev), pol, disk->disk_name, pol); |
| 319 | set_disk_ro(disk, nd_region->ro); |
| 320 | |
| 321 | return 0; |
| 322 | |
| 323 | } |
| 324 | EXPORT_SYMBOL(nvdimm_revalidate_disk); |
| 325 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 326 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 327 | char *buf) |
| 328 | { |
| 329 | return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n", |
| 330 | to_nd_device_type(dev)); |
| 331 | } |
| 332 | static DEVICE_ATTR_RO(modalias); |
| 333 | |
| 334 | static ssize_t devtype_show(struct device *dev, struct device_attribute *attr, |
| 335 | char *buf) |
| 336 | { |
| 337 | return sprintf(buf, "%s\n", dev->type->name); |
| 338 | } |
| 339 | static DEVICE_ATTR_RO(devtype); |
| 340 | |
| 341 | static struct attribute *nd_device_attributes[] = { |
| 342 | &dev_attr_modalias.attr, |
| 343 | &dev_attr_devtype.attr, |
| 344 | NULL, |
| 345 | }; |
| 346 | |
| 347 | /** |
| 348 | * nd_device_attribute_group - generic attributes for all devices on an nd bus |
| 349 | */ |
| 350 | struct attribute_group nd_device_attribute_group = { |
| 351 | .attrs = nd_device_attributes, |
| 352 | }; |
| 353 | EXPORT_SYMBOL_GPL(nd_device_attribute_group); |
| 354 | |
Toshi Kani | 74ae66c | 2015-06-19 12:18:34 -0600 | [diff] [blame] | 355 | static ssize_t numa_node_show(struct device *dev, |
| 356 | struct device_attribute *attr, char *buf) |
| 357 | { |
| 358 | return sprintf(buf, "%d\n", dev_to_node(dev)); |
| 359 | } |
| 360 | static DEVICE_ATTR_RO(numa_node); |
| 361 | |
| 362 | static struct attribute *nd_numa_attributes[] = { |
| 363 | &dev_attr_numa_node.attr, |
| 364 | NULL, |
| 365 | }; |
| 366 | |
| 367 | static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a, |
| 368 | int n) |
| 369 | { |
| 370 | if (!IS_ENABLED(CONFIG_NUMA)) |
| 371 | return 0; |
| 372 | |
| 373 | return a->mode; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus |
| 378 | */ |
| 379 | struct attribute_group nd_numa_attribute_group = { |
| 380 | .attrs = nd_numa_attributes, |
| 381 | .is_visible = nd_numa_attr_visible, |
| 382 | }; |
| 383 | EXPORT_SYMBOL_GPL(nd_numa_attribute_group); |
| 384 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 385 | int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus) |
| 386 | { |
| 387 | dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id); |
| 388 | struct device *dev; |
| 389 | |
| 390 | dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus, |
| 391 | "ndctl%d", nvdimm_bus->id); |
| 392 | |
| 393 | if (IS_ERR(dev)) { |
| 394 | dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n", |
| 395 | nvdimm_bus->id, PTR_ERR(dev)); |
| 396 | return PTR_ERR(dev); |
| 397 | } |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus) |
| 402 | { |
| 403 | device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id)); |
| 404 | } |
| 405 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 406 | static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = { |
| 407 | [ND_CMD_IMPLEMENTED] = { }, |
| 408 | [ND_CMD_SMART] = { |
| 409 | .out_num = 2, |
Dan Williams | 2112911 | 2016-04-07 19:58:44 -0700 | [diff] [blame] | 410 | .out_sizes = { 4, 128, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 411 | }, |
| 412 | [ND_CMD_SMART_THRESHOLD] = { |
| 413 | .out_num = 2, |
| 414 | .out_sizes = { 4, 8, }, |
| 415 | }, |
| 416 | [ND_CMD_DIMM_FLAGS] = { |
| 417 | .out_num = 2, |
| 418 | .out_sizes = { 4, 4 }, |
| 419 | }, |
| 420 | [ND_CMD_GET_CONFIG_SIZE] = { |
| 421 | .out_num = 3, |
| 422 | .out_sizes = { 4, 4, 4, }, |
| 423 | }, |
| 424 | [ND_CMD_GET_CONFIG_DATA] = { |
| 425 | .in_num = 2, |
| 426 | .in_sizes = { 4, 4, }, |
| 427 | .out_num = 2, |
| 428 | .out_sizes = { 4, UINT_MAX, }, |
| 429 | }, |
| 430 | [ND_CMD_SET_CONFIG_DATA] = { |
| 431 | .in_num = 3, |
| 432 | .in_sizes = { 4, 4, UINT_MAX, }, |
| 433 | .out_num = 1, |
| 434 | .out_sizes = { 4, }, |
| 435 | }, |
| 436 | [ND_CMD_VENDOR] = { |
| 437 | .in_num = 3, |
| 438 | .in_sizes = { 4, 4, UINT_MAX, }, |
| 439 | .out_num = 3, |
| 440 | .out_sizes = { 4, 4, UINT_MAX, }, |
| 441 | }, |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 442 | [ND_CMD_CALL] = { |
| 443 | .in_num = 2, |
| 444 | .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, }, |
| 445 | .out_num = 1, |
| 446 | .out_sizes = { UINT_MAX, }, |
| 447 | }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 448 | }; |
| 449 | |
| 450 | const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd) |
| 451 | { |
| 452 | if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs)) |
| 453 | return &__nd_cmd_dimm_descs[cmd]; |
| 454 | return NULL; |
| 455 | } |
| 456 | EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc); |
| 457 | |
| 458 | static const struct nd_cmd_desc __nd_cmd_bus_descs[] = { |
| 459 | [ND_CMD_IMPLEMENTED] = { }, |
| 460 | [ND_CMD_ARS_CAP] = { |
| 461 | .in_num = 2, |
| 462 | .in_sizes = { 8, 8, }, |
Dan Williams | 4577b06 | 2016-02-17 13:08:58 -0800 | [diff] [blame] | 463 | .out_num = 4, |
| 464 | .out_sizes = { 4, 4, 4, 4, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 465 | }, |
| 466 | [ND_CMD_ARS_START] = { |
Dan Williams | 4577b06 | 2016-02-17 13:08:58 -0800 | [diff] [blame] | 467 | .in_num = 5, |
| 468 | .in_sizes = { 8, 8, 2, 1, 5, }, |
| 469 | .out_num = 2, |
| 470 | .out_sizes = { 4, 4, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 471 | }, |
| 472 | [ND_CMD_ARS_STATUS] = { |
Dan Williams | 747ffe1 | 2016-02-19 15:21:14 -0800 | [diff] [blame] | 473 | .out_num = 3, |
| 474 | .out_sizes = { 4, 4, UINT_MAX, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 475 | }, |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 476 | [ND_CMD_CLEAR_ERROR] = { |
| 477 | .in_num = 2, |
| 478 | .in_sizes = { 8, 8, }, |
| 479 | .out_num = 3, |
| 480 | .out_sizes = { 4, 4, 8, }, |
| 481 | }, |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 482 | [ND_CMD_CALL] = { |
| 483 | .in_num = 2, |
| 484 | .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, }, |
| 485 | .out_num = 1, |
| 486 | .out_sizes = { UINT_MAX, }, |
| 487 | }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 488 | }; |
| 489 | |
| 490 | const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd) |
| 491 | { |
| 492 | if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs)) |
| 493 | return &__nd_cmd_bus_descs[cmd]; |
| 494 | return NULL; |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(nd_cmd_bus_desc); |
| 497 | |
| 498 | u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd, |
| 499 | const struct nd_cmd_desc *desc, int idx, void *buf) |
| 500 | { |
| 501 | if (idx >= desc->in_num) |
| 502 | return UINT_MAX; |
| 503 | |
| 504 | if (desc->in_sizes[idx] < UINT_MAX) |
| 505 | return desc->in_sizes[idx]; |
| 506 | |
| 507 | if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) { |
| 508 | struct nd_cmd_set_config_hdr *hdr = buf; |
| 509 | |
| 510 | return hdr->in_length; |
| 511 | } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) { |
| 512 | struct nd_cmd_vendor_hdr *hdr = buf; |
| 513 | |
| 514 | return hdr->in_length; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 515 | } else if (cmd == ND_CMD_CALL) { |
| 516 | struct nd_cmd_pkg *pkg = buf; |
| 517 | |
| 518 | return pkg->nd_size_in; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | return UINT_MAX; |
| 522 | } |
| 523 | EXPORT_SYMBOL_GPL(nd_cmd_in_size); |
| 524 | |
| 525 | u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd, |
| 526 | const struct nd_cmd_desc *desc, int idx, const u32 *in_field, |
| 527 | const u32 *out_field) |
| 528 | { |
| 529 | if (idx >= desc->out_num) |
| 530 | return UINT_MAX; |
| 531 | |
| 532 | if (desc->out_sizes[idx] < UINT_MAX) |
| 533 | return desc->out_sizes[idx]; |
| 534 | |
| 535 | if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1) |
| 536 | return in_field[1]; |
| 537 | else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) |
| 538 | return out_field[1]; |
Dan Williams | 747ffe1 | 2016-02-19 15:21:14 -0800 | [diff] [blame] | 539 | else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) |
| 540 | return out_field[1] - 8; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 541 | else if (cmd == ND_CMD_CALL) { |
| 542 | struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field; |
| 543 | |
| 544 | return pkg->nd_size_out; |
| 545 | } |
| 546 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 547 | |
| 548 | return UINT_MAX; |
| 549 | } |
| 550 | EXPORT_SYMBOL_GPL(nd_cmd_out_size); |
| 551 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 552 | void wait_nvdimm_bus_probe_idle(struct device *dev) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 553 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 554 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 555 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 556 | do { |
| 557 | if (nvdimm_bus->probe_active == 0) |
| 558 | break; |
| 559 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 560 | wait_event(nvdimm_bus->probe_wait, |
| 561 | nvdimm_bus->probe_active == 0); |
| 562 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 563 | } while (true); |
| 564 | } |
| 565 | |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 566 | static int pmem_active(struct device *dev, void *data) |
| 567 | { |
| 568 | if (is_nd_pmem(dev) && dev->driver) |
| 569 | return -EBUSY; |
| 570 | return 0; |
| 571 | } |
| 572 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 573 | /* set_config requires an idle interleave set */ |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 574 | static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus, |
| 575 | struct nvdimm *nvdimm, unsigned int cmd) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 576 | { |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 577 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 578 | |
| 579 | /* ask the bus provider if it would like to block this request */ |
| 580 | if (nd_desc->clear_to_send) { |
| 581 | int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd); |
| 582 | |
| 583 | if (rc) |
| 584 | return rc; |
| 585 | } |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 586 | |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 587 | /* require clear error to go through the pmem driver */ |
| 588 | if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR) |
| 589 | return device_for_each_child(&nvdimm_bus->dev, NULL, |
| 590 | pmem_active); |
| 591 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 592 | if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA) |
| 593 | return 0; |
| 594 | |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 595 | /* prevent label manipulation while the kernel owns label updates */ |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 596 | wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 597 | if (atomic_read(&nvdimm->busy)) |
| 598 | return -EBUSY; |
| 599 | return 0; |
| 600 | } |
| 601 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 602 | static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, |
| 603 | int read_only, unsigned int ioctl_cmd, unsigned long arg) |
| 604 | { |
| 605 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 606 | size_t buf_len = 0, in_len = 0, out_len = 0; |
| 607 | static char out_env[ND_CMD_MAX_ENVELOPE]; |
| 608 | static char in_env[ND_CMD_MAX_ENVELOPE]; |
| 609 | const struct nd_cmd_desc *desc = NULL; |
| 610 | unsigned int cmd = _IOC_NR(ioctl_cmd); |
| 611 | void __user *p = (void __user *) arg; |
| 612 | struct device *dev = &nvdimm_bus->dev; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 613 | struct nd_cmd_pkg pkg; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 614 | const char *cmd_name, *dimm_name; |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 615 | unsigned long cmd_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 616 | void *buf; |
| 617 | int rc, i; |
| 618 | |
| 619 | if (nvdimm) { |
| 620 | desc = nd_cmd_dimm_desc(cmd); |
| 621 | cmd_name = nvdimm_cmd_name(cmd); |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 622 | cmd_mask = nvdimm->cmd_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 623 | dimm_name = dev_name(&nvdimm->dev); |
| 624 | } else { |
| 625 | desc = nd_cmd_bus_desc(cmd); |
| 626 | cmd_name = nvdimm_bus_cmd_name(cmd); |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 627 | cmd_mask = nd_desc->cmd_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 628 | dimm_name = "bus"; |
| 629 | } |
| 630 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 631 | if (cmd == ND_CMD_CALL) { |
| 632 | if (copy_from_user(&pkg, p, sizeof(pkg))) |
| 633 | return -EFAULT; |
| 634 | } |
| 635 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 636 | if (!desc || (desc->out_num + desc->in_num == 0) || |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 637 | !test_bit(cmd, &cmd_mask)) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 638 | return -ENOTTY; |
| 639 | |
| 640 | /* fail write commands (when read-only) */ |
| 641 | if (read_only) |
Jerry Hoemann | 07accfa | 2016-01-06 16:03:41 -0700 | [diff] [blame] | 642 | switch (cmd) { |
| 643 | case ND_CMD_VENDOR: |
| 644 | case ND_CMD_SET_CONFIG_DATA: |
| 645 | case ND_CMD_ARS_START: |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 646 | case ND_CMD_CLEAR_ERROR: |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 647 | case ND_CMD_CALL: |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 648 | dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n", |
| 649 | nvdimm ? nvdimm_cmd_name(cmd) |
| 650 | : nvdimm_bus_cmd_name(cmd)); |
| 651 | return -EPERM; |
| 652 | default: |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | /* process an input envelope */ |
| 657 | for (i = 0; i < desc->in_num; i++) { |
| 658 | u32 in_size, copy; |
| 659 | |
| 660 | in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env); |
| 661 | if (in_size == UINT_MAX) { |
| 662 | dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n", |
| 663 | __func__, dimm_name, cmd_name, i); |
| 664 | return -ENXIO; |
| 665 | } |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 666 | if (in_len < sizeof(in_env)) |
| 667 | copy = min_t(u32, sizeof(in_env) - in_len, in_size); |
| 668 | else |
| 669 | copy = 0; |
| 670 | if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) |
| 671 | return -EFAULT; |
| 672 | in_len += in_size; |
| 673 | } |
| 674 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame^] | 675 | if (cmd == ND_CMD_CALL) { |
| 676 | dev_dbg(dev, "%s:%s, idx: %llu, in: %zu, out: %zu, len %zu\n", |
| 677 | __func__, dimm_name, pkg.nd_command, |
| 678 | in_len, out_len, buf_len); |
| 679 | |
| 680 | for (i = 0; i < ARRAY_SIZE(pkg.nd_reserved2); i++) |
| 681 | if (pkg.nd_reserved2[i]) |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 685 | /* process an output envelope */ |
| 686 | for (i = 0; i < desc->out_num; i++) { |
| 687 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, |
| 688 | (u32 *) in_env, (u32 *) out_env); |
| 689 | u32 copy; |
| 690 | |
| 691 | if (out_size == UINT_MAX) { |
| 692 | dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n", |
| 693 | __func__, dimm_name, cmd_name, i); |
| 694 | return -EFAULT; |
| 695 | } |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 696 | if (out_len < sizeof(out_env)) |
| 697 | copy = min_t(u32, sizeof(out_env) - out_len, out_size); |
| 698 | else |
| 699 | copy = 0; |
| 700 | if (copy && copy_from_user(&out_env[out_len], |
| 701 | p + in_len + out_len, copy)) |
| 702 | return -EFAULT; |
| 703 | out_len += out_size; |
| 704 | } |
| 705 | |
| 706 | buf_len = out_len + in_len; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 707 | if (buf_len > ND_IOCTL_MAX_BUFLEN) { |
| 708 | dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__, |
| 709 | dimm_name, cmd_name, buf_len, |
| 710 | ND_IOCTL_MAX_BUFLEN); |
| 711 | return -EINVAL; |
| 712 | } |
| 713 | |
| 714 | buf = vmalloc(buf_len); |
| 715 | if (!buf) |
| 716 | return -ENOMEM; |
| 717 | |
| 718 | if (copy_from_user(buf, p, buf_len)) { |
| 719 | rc = -EFAULT; |
| 720 | goto out; |
| 721 | } |
| 722 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 723 | nvdimm_bus_lock(&nvdimm_bus->dev); |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 724 | rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, cmd); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 725 | if (rc) |
| 726 | goto out_unlock; |
| 727 | |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 728 | rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, NULL); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 729 | if (rc < 0) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 730 | goto out_unlock; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 731 | if (copy_to_user(p, buf, buf_len)) |
| 732 | rc = -EFAULT; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 733 | out_unlock: |
| 734 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 735 | out: |
| 736 | vfree(buf); |
| 737 | return rc; |
| 738 | } |
| 739 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 740 | static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 741 | { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 742 | long id = (long) file->private_data; |
Jerry Hoemann | 4dc0e7be | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 743 | int rc = -ENXIO, ro; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 744 | struct nvdimm_bus *nvdimm_bus; |
| 745 | |
Jerry Hoemann | 4dc0e7be | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 746 | ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 747 | mutex_lock(&nvdimm_bus_list_mutex); |
| 748 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { |
| 749 | if (nvdimm_bus->id == id) { |
Jerry Hoemann | 4dc0e7be | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 750 | rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 751 | break; |
| 752 | } |
| 753 | } |
| 754 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 755 | |
| 756 | return rc; |
| 757 | } |
| 758 | |
| 759 | static int match_dimm(struct device *dev, void *data) |
| 760 | { |
| 761 | long id = (long) data; |
| 762 | |
| 763 | if (is_nvdimm(dev)) { |
| 764 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 765 | |
| 766 | return nvdimm->id == id; |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 773 | { |
Jerry Hoemann | 4dc0e7be | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 774 | int rc = -ENXIO, ro; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 775 | struct nvdimm_bus *nvdimm_bus; |
| 776 | |
Jerry Hoemann | 4dc0e7be | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 777 | ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 778 | mutex_lock(&nvdimm_bus_list_mutex); |
| 779 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { |
| 780 | struct device *dev = device_find_child(&nvdimm_bus->dev, |
| 781 | file->private_data, match_dimm); |
| 782 | struct nvdimm *nvdimm; |
| 783 | |
| 784 | if (!dev) |
| 785 | continue; |
| 786 | |
| 787 | nvdimm = to_nvdimm(dev); |
Jerry Hoemann | 4dc0e7be | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 788 | rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 789 | put_device(dev); |
| 790 | break; |
| 791 | } |
| 792 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 793 | |
| 794 | return rc; |
| 795 | } |
| 796 | |
| 797 | static int nd_open(struct inode *inode, struct file *file) |
| 798 | { |
| 799 | long minor = iminor(inode); |
| 800 | |
| 801 | file->private_data = (void *) minor; |
| 802 | return 0; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static const struct file_operations nvdimm_bus_fops = { |
| 806 | .owner = THIS_MODULE, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 807 | .open = nd_open, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 808 | .unlocked_ioctl = nd_ioctl, |
| 809 | .compat_ioctl = nd_ioctl, |
| 810 | .llseek = noop_llseek, |
| 811 | }; |
| 812 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 813 | static const struct file_operations nvdimm_fops = { |
| 814 | .owner = THIS_MODULE, |
| 815 | .open = nd_open, |
| 816 | .unlocked_ioctl = nvdimm_ioctl, |
| 817 | .compat_ioctl = nvdimm_ioctl, |
| 818 | .llseek = noop_llseek, |
| 819 | }; |
| 820 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 821 | int __init nvdimm_bus_init(void) |
| 822 | { |
| 823 | int rc; |
| 824 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 825 | rc = bus_register(&nvdimm_bus_type); |
| 826 | if (rc) |
| 827 | return rc; |
| 828 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 829 | rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops); |
| 830 | if (rc < 0) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 831 | goto err_bus_chrdev; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 832 | nvdimm_bus_major = rc; |
| 833 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 834 | rc = register_chrdev(0, "dimmctl", &nvdimm_fops); |
| 835 | if (rc < 0) |
| 836 | goto err_dimm_chrdev; |
| 837 | nvdimm_major = rc; |
| 838 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 839 | nd_class = class_create(THIS_MODULE, "nd"); |
Axel Lin | daa1dee | 2015-06-28 17:00:57 +0800 | [diff] [blame] | 840 | if (IS_ERR(nd_class)) { |
| 841 | rc = PTR_ERR(nd_class); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 842 | goto err_class; |
Axel Lin | daa1dee | 2015-06-28 17:00:57 +0800 | [diff] [blame] | 843 | } |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 844 | |
| 845 | return 0; |
| 846 | |
| 847 | err_class: |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 848 | unregister_chrdev(nvdimm_major, "dimmctl"); |
| 849 | err_dimm_chrdev: |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 850 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 851 | err_bus_chrdev: |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 852 | bus_unregister(&nvdimm_bus_type); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 853 | |
| 854 | return rc; |
| 855 | } |
| 856 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 857 | void nvdimm_bus_exit(void) |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 858 | { |
| 859 | class_destroy(nd_class); |
| 860 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 861 | unregister_chrdev(nvdimm_major, "dimmctl"); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 862 | bus_unregister(&nvdimm_bus_type); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 863 | } |