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