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