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; |
Dan Williams | 1851594 | 2016-07-22 23:46:08 -0700 | [diff] [blame] | 34 | static DEFINE_IDA(nd_ida); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 35 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 36 | static int to_nd_device_type(struct device *dev) |
| 37 | { |
| 38 | if (is_nvdimm(dev)) |
| 39 | return ND_DEVICE_DIMM; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 40 | else if (is_nd_pmem(dev)) |
| 41 | return ND_DEVICE_REGION_PMEM; |
| 42 | else if (is_nd_blk(dev)) |
| 43 | return ND_DEVICE_REGION_BLK; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 44 | else if (is_nd_dax(dev)) |
| 45 | return ND_DEVICE_DAX_PMEM; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 46 | else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent)) |
| 47 | return nd_region_to_nstype(to_nd_region(dev->parent)); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 53 | { |
Toshi Kani | 41d7a6d | 2015-06-19 12:18:33 -0600 | [diff] [blame] | 54 | /* |
| 55 | * Ensure that region devices always have their numa node set as |
| 56 | * early as possible. |
| 57 | */ |
| 58 | if (is_nd_pmem(dev) || is_nd_blk(dev)) |
| 59 | set_dev_node(dev, to_nd_region(dev)->numa_node); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 60 | return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT, |
| 61 | to_nd_device_type(dev)); |
| 62 | } |
| 63 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 64 | static struct module *to_bus_provider(struct device *dev) |
| 65 | { |
| 66 | /* pin bus providers while regions are enabled */ |
| 67 | if (is_nd_pmem(dev) || is_nd_blk(dev)) { |
| 68 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 69 | |
Dan Williams | bc9775d | 2016-07-21 20:03:19 -0700 | [diff] [blame] | 70 | return nvdimm_bus->nd_desc->module; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 71 | } |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 75 | static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus) |
| 76 | { |
| 77 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 78 | nvdimm_bus->probe_active++; |
| 79 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 80 | } |
| 81 | |
| 82 | static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus) |
| 83 | { |
| 84 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 85 | if (--nvdimm_bus->probe_active == 0) |
| 86 | wake_up(&nvdimm_bus->probe_wait); |
| 87 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 88 | } |
| 89 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 90 | static int nvdimm_bus_probe(struct device *dev) |
| 91 | { |
| 92 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 93 | struct module *provider = to_bus_provider(dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 94 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 95 | int rc; |
| 96 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 97 | if (!try_module_get(provider)) |
| 98 | return -ENXIO; |
| 99 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 100 | nvdimm_bus_probe_start(nvdimm_bus); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 101 | rc = nd_drv->probe(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 102 | if (rc == 0) |
| 103 | nd_region_probe_success(nvdimm_bus, dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 104 | else |
| 105 | nd_region_disable(nvdimm_bus, dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 106 | nvdimm_bus_probe_end(nvdimm_bus); |
| 107 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 108 | dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name, |
| 109 | dev_name(dev), rc); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 110 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 111 | if (rc != 0) |
| 112 | module_put(provider); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 113 | return rc; |
| 114 | } |
| 115 | |
| 116 | static int nvdimm_bus_remove(struct device *dev) |
| 117 | { |
| 118 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 119 | struct module *provider = to_bus_provider(dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 120 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
Dan Williams | 6cf9c5b | 2016-05-18 09:13:13 -0700 | [diff] [blame] | 121 | int rc = 0; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 122 | |
Dan Williams | 6cf9c5b | 2016-05-18 09:13:13 -0700 | [diff] [blame] | 123 | if (nd_drv->remove) |
| 124 | rc = nd_drv->remove(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 125 | nd_region_disable(nvdimm_bus, dev); |
| 126 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 127 | dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name, |
| 128 | dev_name(dev), rc); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 129 | module_put(provider); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 130 | return rc; |
| 131 | } |
| 132 | |
Dan Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 133 | static void nvdimm_bus_shutdown(struct device *dev) |
| 134 | { |
| 135 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 136 | struct nd_device_driver *nd_drv = NULL; |
| 137 | |
| 138 | if (dev->driver) |
| 139 | nd_drv = to_nd_device_driver(dev->driver); |
| 140 | |
| 141 | if (nd_drv && nd_drv->shutdown) { |
| 142 | nd_drv->shutdown(dev); |
| 143 | dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n", |
| 144 | dev->driver->name, dev_name(dev)); |
| 145 | } |
| 146 | } |
| 147 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 148 | void nd_device_notify(struct device *dev, enum nvdimm_event event) |
| 149 | { |
| 150 | device_lock(dev); |
| 151 | if (dev->driver) { |
| 152 | struct nd_device_driver *nd_drv; |
| 153 | |
| 154 | nd_drv = to_nd_device_driver(dev->driver); |
| 155 | if (nd_drv->notify) |
| 156 | nd_drv->notify(dev, event); |
| 157 | } |
| 158 | device_unlock(dev); |
| 159 | } |
| 160 | EXPORT_SYMBOL(nd_device_notify); |
| 161 | |
| 162 | void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event) |
| 163 | { |
| 164 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
| 165 | |
| 166 | if (!nvdimm_bus) |
| 167 | return; |
| 168 | |
| 169 | /* caller is responsible for holding a reference on the device */ |
| 170 | nd_device_notify(&nd_region->dev, event); |
| 171 | } |
| 172 | EXPORT_SYMBOL_GPL(nvdimm_region_notify); |
| 173 | |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 174 | long nvdimm_clear_poison(struct device *dev, phys_addr_t phys, |
| 175 | unsigned int len) |
| 176 | { |
| 177 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 178 | struct nvdimm_bus_descriptor *nd_desc; |
| 179 | struct nd_cmd_clear_error clear_err; |
| 180 | struct nd_cmd_ars_cap ars_cap; |
| 181 | u32 clear_err_unit, mask; |
| 182 | int cmd_rc, rc; |
| 183 | |
| 184 | if (!nvdimm_bus) |
| 185 | return -ENXIO; |
| 186 | |
| 187 | nd_desc = nvdimm_bus->nd_desc; |
Dave Jiang | 1e8b8d9 | 2016-09-09 09:10:08 -0700 | [diff] [blame] | 188 | /* |
| 189 | * if ndctl does not exist, it's PMEM_LEGACY and |
| 190 | * we want to just pretend everything is handled. |
| 191 | */ |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 192 | if (!nd_desc->ndctl) |
Dave Jiang | 1e8b8d9 | 2016-09-09 09:10:08 -0700 | [diff] [blame] | 193 | return len; |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 194 | |
| 195 | memset(&ars_cap, 0, sizeof(ars_cap)); |
| 196 | ars_cap.address = phys; |
| 197 | ars_cap.length = len; |
| 198 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap, |
| 199 | sizeof(ars_cap), &cmd_rc); |
| 200 | if (rc < 0) |
| 201 | return rc; |
| 202 | if (cmd_rc < 0) |
| 203 | return cmd_rc; |
| 204 | clear_err_unit = ars_cap.clear_err_unit; |
| 205 | if (!clear_err_unit || !is_power_of_2(clear_err_unit)) |
| 206 | return -ENXIO; |
| 207 | |
| 208 | mask = clear_err_unit - 1; |
| 209 | if ((phys | len) & mask) |
| 210 | return -ENXIO; |
| 211 | memset(&clear_err, 0, sizeof(clear_err)); |
| 212 | clear_err.address = phys; |
| 213 | clear_err.length = len; |
| 214 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err, |
| 215 | sizeof(clear_err), &cmd_rc); |
| 216 | if (rc < 0) |
| 217 | return rc; |
| 218 | if (cmd_rc < 0) |
| 219 | return cmd_rc; |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 220 | |
Toshi Kani | fa313fd | 2017-04-27 16:57:05 -0600 | [diff] [blame] | 221 | if (clear_err.cleared > 0) |
| 222 | nvdimm_clear_from_poison_list(nvdimm_bus, phys, |
| 223 | clear_err.cleared); |
| 224 | |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 225 | return clear_err.cleared; |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(nvdimm_clear_poison); |
| 228 | |
Dan Williams | 1851594 | 2016-07-22 23:46:08 -0700 | [diff] [blame] | 229 | static int nvdimm_bus_match(struct device *dev, struct device_driver *drv); |
| 230 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 231 | static struct bus_type nvdimm_bus_type = { |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 232 | .name = "nd", |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 233 | .uevent = nvdimm_bus_uevent, |
| 234 | .match = nvdimm_bus_match, |
| 235 | .probe = nvdimm_bus_probe, |
| 236 | .remove = nvdimm_bus_remove, |
Dan Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 237 | .shutdown = nvdimm_bus_shutdown, |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 238 | }; |
| 239 | |
Dan Williams | 1851594 | 2016-07-22 23:46:08 -0700 | [diff] [blame] | 240 | static void nvdimm_bus_release(struct device *dev) |
| 241 | { |
| 242 | struct nvdimm_bus *nvdimm_bus; |
| 243 | |
| 244 | nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); |
| 245 | ida_simple_remove(&nd_ida, nvdimm_bus->id); |
| 246 | kfree(nvdimm_bus); |
| 247 | } |
| 248 | |
| 249 | static bool is_nvdimm_bus(struct device *dev) |
| 250 | { |
| 251 | return dev->release == nvdimm_bus_release; |
| 252 | } |
| 253 | |
| 254 | struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev) |
| 255 | { |
| 256 | struct device *dev; |
| 257 | |
| 258 | for (dev = nd_dev; dev; dev = dev->parent) |
| 259 | if (is_nvdimm_bus(dev)) |
| 260 | break; |
| 261 | dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n"); |
| 262 | if (dev) |
| 263 | return to_nvdimm_bus(dev); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | struct nvdimm_bus *to_nvdimm_bus(struct device *dev) |
| 268 | { |
| 269 | struct nvdimm_bus *nvdimm_bus; |
| 270 | |
| 271 | nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); |
| 272 | WARN_ON(!is_nvdimm_bus(dev)); |
| 273 | return nvdimm_bus; |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(to_nvdimm_bus); |
| 276 | |
| 277 | struct nvdimm_bus *nvdimm_bus_register(struct device *parent, |
| 278 | struct nvdimm_bus_descriptor *nd_desc) |
| 279 | { |
| 280 | struct nvdimm_bus *nvdimm_bus; |
| 281 | int rc; |
| 282 | |
| 283 | nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL); |
| 284 | if (!nvdimm_bus) |
| 285 | return NULL; |
| 286 | INIT_LIST_HEAD(&nvdimm_bus->list); |
| 287 | INIT_LIST_HEAD(&nvdimm_bus->mapping_list); |
| 288 | INIT_LIST_HEAD(&nvdimm_bus->poison_list); |
| 289 | init_waitqueue_head(&nvdimm_bus->probe_wait); |
| 290 | nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); |
| 291 | mutex_init(&nvdimm_bus->reconfig_mutex); |
| 292 | if (nvdimm_bus->id < 0) { |
| 293 | kfree(nvdimm_bus); |
| 294 | return NULL; |
| 295 | } |
| 296 | nvdimm_bus->nd_desc = nd_desc; |
| 297 | nvdimm_bus->dev.parent = parent; |
| 298 | nvdimm_bus->dev.release = nvdimm_bus_release; |
| 299 | nvdimm_bus->dev.groups = nd_desc->attr_groups; |
| 300 | nvdimm_bus->dev.bus = &nvdimm_bus_type; |
| 301 | dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id); |
| 302 | rc = device_register(&nvdimm_bus->dev); |
| 303 | if (rc) { |
| 304 | dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc); |
| 305 | goto err; |
| 306 | } |
| 307 | |
| 308 | return nvdimm_bus; |
| 309 | err: |
| 310 | put_device(&nvdimm_bus->dev); |
| 311 | return NULL; |
| 312 | } |
| 313 | EXPORT_SYMBOL_GPL(nvdimm_bus_register); |
| 314 | |
| 315 | void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus) |
| 316 | { |
| 317 | if (!nvdimm_bus) |
| 318 | return; |
| 319 | device_unregister(&nvdimm_bus->dev); |
| 320 | } |
| 321 | EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); |
| 322 | |
| 323 | static int child_unregister(struct device *dev, void *data) |
| 324 | { |
| 325 | /* |
| 326 | * the singular ndctl class device per bus needs to be |
| 327 | * "device_destroy"ed, so skip it here |
| 328 | * |
| 329 | * i.e. remove classless children |
| 330 | */ |
| 331 | if (dev->class) |
| 332 | /* pass */; |
| 333 | else |
| 334 | nd_device_unregister(dev, ND_SYNC); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static void free_poison_list(struct list_head *poison_list) |
| 339 | { |
| 340 | struct nd_poison *pl, *next; |
| 341 | |
| 342 | list_for_each_entry_safe(pl, next, poison_list, list) { |
| 343 | list_del(&pl->list); |
| 344 | kfree(pl); |
| 345 | } |
| 346 | list_del_init(poison_list); |
| 347 | } |
| 348 | |
| 349 | static int nd_bus_remove(struct device *dev) |
| 350 | { |
| 351 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 352 | |
| 353 | mutex_lock(&nvdimm_bus_list_mutex); |
| 354 | list_del_init(&nvdimm_bus->list); |
| 355 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 356 | |
| 357 | nd_synchronize(); |
| 358 | device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister); |
| 359 | |
| 360 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 361 | free_poison_list(&nvdimm_bus->poison_list); |
| 362 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 363 | |
| 364 | nvdimm_bus_destroy_ndctl(nvdimm_bus); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int nd_bus_probe(struct device *dev) |
| 370 | { |
| 371 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 372 | int rc; |
| 373 | |
| 374 | rc = nvdimm_bus_create_ndctl(nvdimm_bus); |
| 375 | if (rc) |
| 376 | return rc; |
| 377 | |
| 378 | mutex_lock(&nvdimm_bus_list_mutex); |
| 379 | list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list); |
| 380 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 381 | |
| 382 | /* enable bus provider attributes to look up their local context */ |
| 383 | dev_set_drvdata(dev, nvdimm_bus->nd_desc); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static struct nd_device_driver nd_bus_driver = { |
| 389 | .probe = nd_bus_probe, |
| 390 | .remove = nd_bus_remove, |
| 391 | .drv = { |
| 392 | .name = "nd_bus", |
| 393 | .suppress_bind_attrs = true, |
| 394 | .bus = &nvdimm_bus_type, |
| 395 | .owner = THIS_MODULE, |
| 396 | .mod_name = KBUILD_MODNAME, |
| 397 | }, |
| 398 | }; |
| 399 | |
| 400 | static int nvdimm_bus_match(struct device *dev, struct device_driver *drv) |
| 401 | { |
| 402 | struct nd_device_driver *nd_drv = to_nd_device_driver(drv); |
| 403 | |
| 404 | if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver) |
| 405 | return true; |
| 406 | |
| 407 | return !!test_bit(to_nd_device_type(dev), &nd_drv->type); |
| 408 | } |
| 409 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 410 | static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain); |
| 411 | |
| 412 | void nd_synchronize(void) |
| 413 | { |
| 414 | async_synchronize_full_domain(&nd_async_domain); |
| 415 | } |
| 416 | EXPORT_SYMBOL_GPL(nd_synchronize); |
| 417 | |
| 418 | static void nd_async_device_register(void *d, async_cookie_t cookie) |
| 419 | { |
| 420 | struct device *dev = d; |
| 421 | |
| 422 | if (device_add(dev) != 0) { |
| 423 | dev_err(dev, "%s: failed\n", __func__); |
| 424 | put_device(dev); |
| 425 | } |
| 426 | put_device(dev); |
| 427 | } |
| 428 | |
| 429 | static void nd_async_device_unregister(void *d, async_cookie_t cookie) |
| 430 | { |
| 431 | struct device *dev = d; |
| 432 | |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 433 | /* flush bus operations before delete */ |
| 434 | nvdimm_bus_lock(dev); |
| 435 | nvdimm_bus_unlock(dev); |
| 436 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 437 | device_unregister(dev); |
| 438 | put_device(dev); |
| 439 | } |
| 440 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 441 | void __nd_device_register(struct device *dev) |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 442 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 443 | if (!dev) |
| 444 | return; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 445 | dev->bus = &nvdimm_bus_type; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 446 | get_device(dev); |
| 447 | async_schedule_domain(nd_async_device_register, dev, |
| 448 | &nd_async_domain); |
| 449 | } |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 450 | |
| 451 | void nd_device_register(struct device *dev) |
| 452 | { |
| 453 | device_initialize(dev); |
| 454 | __nd_device_register(dev); |
| 455 | } |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 456 | EXPORT_SYMBOL(nd_device_register); |
| 457 | |
| 458 | void nd_device_unregister(struct device *dev, enum nd_async_mode mode) |
| 459 | { |
| 460 | switch (mode) { |
| 461 | case ND_ASYNC: |
| 462 | get_device(dev); |
| 463 | async_schedule_domain(nd_async_device_unregister, dev, |
| 464 | &nd_async_domain); |
| 465 | break; |
| 466 | case ND_SYNC: |
| 467 | nd_synchronize(); |
| 468 | device_unregister(dev); |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | EXPORT_SYMBOL(nd_device_unregister); |
| 473 | |
| 474 | /** |
| 475 | * __nd_driver_register() - register a region or a namespace driver |
| 476 | * @nd_drv: driver to register |
| 477 | * @owner: automatically set by nd_driver_register() macro |
| 478 | * @mod_name: automatically set by nd_driver_register() macro |
| 479 | */ |
| 480 | int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner, |
| 481 | const char *mod_name) |
| 482 | { |
| 483 | struct device_driver *drv = &nd_drv->drv; |
| 484 | |
| 485 | if (!nd_drv->type) { |
| 486 | pr_debug("driver type bitmask not set (%pf)\n", |
| 487 | __builtin_return_address(0)); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
Dan Williams | 6cf9c5b | 2016-05-18 09:13:13 -0700 | [diff] [blame] | 491 | if (!nd_drv->probe) { |
| 492 | pr_debug("%s ->probe() must be specified\n", mod_name); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 493 | return -EINVAL; |
| 494 | } |
| 495 | |
| 496 | drv->bus = &nvdimm_bus_type; |
| 497 | drv->owner = owner; |
| 498 | drv->mod_name = mod_name; |
| 499 | |
| 500 | return driver_register(drv); |
| 501 | } |
| 502 | EXPORT_SYMBOL(__nd_driver_register); |
| 503 | |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 504 | int nvdimm_revalidate_disk(struct gendisk *disk) |
| 505 | { |
Dan Williams | 52c44d9 | 2016-06-15 19:43:07 -0700 | [diff] [blame] | 506 | struct device *dev = disk_to_dev(disk)->parent; |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 507 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 508 | const char *pol = nd_region->ro ? "only" : "write"; |
| 509 | |
| 510 | if (nd_region->ro == get_disk_ro(disk)) |
| 511 | return 0; |
| 512 | |
| 513 | dev_info(dev, "%s read-%s, marking %s read-%s\n", |
| 514 | dev_name(&nd_region->dev), pol, disk->disk_name, pol); |
| 515 | set_disk_ro(disk, nd_region->ro); |
| 516 | |
| 517 | return 0; |
| 518 | |
| 519 | } |
| 520 | EXPORT_SYMBOL(nvdimm_revalidate_disk); |
| 521 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 522 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 523 | char *buf) |
| 524 | { |
| 525 | return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n", |
| 526 | to_nd_device_type(dev)); |
| 527 | } |
| 528 | static DEVICE_ATTR_RO(modalias); |
| 529 | |
| 530 | static ssize_t devtype_show(struct device *dev, struct device_attribute *attr, |
| 531 | char *buf) |
| 532 | { |
| 533 | return sprintf(buf, "%s\n", dev->type->name); |
| 534 | } |
| 535 | static DEVICE_ATTR_RO(devtype); |
| 536 | |
| 537 | static struct attribute *nd_device_attributes[] = { |
| 538 | &dev_attr_modalias.attr, |
| 539 | &dev_attr_devtype.attr, |
| 540 | NULL, |
| 541 | }; |
| 542 | |
| 543 | /** |
| 544 | * nd_device_attribute_group - generic attributes for all devices on an nd bus |
| 545 | */ |
| 546 | struct attribute_group nd_device_attribute_group = { |
| 547 | .attrs = nd_device_attributes, |
| 548 | }; |
| 549 | EXPORT_SYMBOL_GPL(nd_device_attribute_group); |
| 550 | |
Toshi Kani | 74ae66c | 2015-06-19 12:18:34 -0600 | [diff] [blame] | 551 | static ssize_t numa_node_show(struct device *dev, |
| 552 | struct device_attribute *attr, char *buf) |
| 553 | { |
| 554 | return sprintf(buf, "%d\n", dev_to_node(dev)); |
| 555 | } |
| 556 | static DEVICE_ATTR_RO(numa_node); |
| 557 | |
| 558 | static struct attribute *nd_numa_attributes[] = { |
| 559 | &dev_attr_numa_node.attr, |
| 560 | NULL, |
| 561 | }; |
| 562 | |
| 563 | static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a, |
| 564 | int n) |
| 565 | { |
| 566 | if (!IS_ENABLED(CONFIG_NUMA)) |
| 567 | return 0; |
| 568 | |
| 569 | return a->mode; |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus |
| 574 | */ |
| 575 | struct attribute_group nd_numa_attribute_group = { |
| 576 | .attrs = nd_numa_attributes, |
| 577 | .is_visible = nd_numa_attr_visible, |
| 578 | }; |
| 579 | EXPORT_SYMBOL_GPL(nd_numa_attribute_group); |
| 580 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 581 | int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus) |
| 582 | { |
| 583 | dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id); |
| 584 | struct device *dev; |
| 585 | |
| 586 | dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus, |
| 587 | "ndctl%d", nvdimm_bus->id); |
| 588 | |
Dan Williams | 4258895 | 2016-05-27 13:28:31 -0700 | [diff] [blame] | 589 | if (IS_ERR(dev)) |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 590 | dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n", |
| 591 | nvdimm_bus->id, PTR_ERR(dev)); |
Dan Williams | 4258895 | 2016-05-27 13:28:31 -0700 | [diff] [blame] | 592 | return PTR_ERR_OR_ZERO(dev); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus) |
| 596 | { |
| 597 | device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id)); |
| 598 | } |
| 599 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 600 | static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = { |
| 601 | [ND_CMD_IMPLEMENTED] = { }, |
| 602 | [ND_CMD_SMART] = { |
| 603 | .out_num = 2, |
Dan Williams | 2112911 | 2016-04-07 19:58:44 -0700 | [diff] [blame] | 604 | .out_sizes = { 4, 128, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 605 | }, |
| 606 | [ND_CMD_SMART_THRESHOLD] = { |
| 607 | .out_num = 2, |
| 608 | .out_sizes = { 4, 8, }, |
| 609 | }, |
| 610 | [ND_CMD_DIMM_FLAGS] = { |
| 611 | .out_num = 2, |
| 612 | .out_sizes = { 4, 4 }, |
| 613 | }, |
| 614 | [ND_CMD_GET_CONFIG_SIZE] = { |
| 615 | .out_num = 3, |
| 616 | .out_sizes = { 4, 4, 4, }, |
| 617 | }, |
| 618 | [ND_CMD_GET_CONFIG_DATA] = { |
| 619 | .in_num = 2, |
| 620 | .in_sizes = { 4, 4, }, |
| 621 | .out_num = 2, |
| 622 | .out_sizes = { 4, UINT_MAX, }, |
| 623 | }, |
| 624 | [ND_CMD_SET_CONFIG_DATA] = { |
| 625 | .in_num = 3, |
| 626 | .in_sizes = { 4, 4, UINT_MAX, }, |
| 627 | .out_num = 1, |
| 628 | .out_sizes = { 4, }, |
| 629 | }, |
| 630 | [ND_CMD_VENDOR] = { |
| 631 | .in_num = 3, |
| 632 | .in_sizes = { 4, 4, UINT_MAX, }, |
| 633 | .out_num = 3, |
| 634 | .out_sizes = { 4, 4, UINT_MAX, }, |
| 635 | }, |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 636 | [ND_CMD_CALL] = { |
| 637 | .in_num = 2, |
| 638 | .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, }, |
| 639 | .out_num = 1, |
| 640 | .out_sizes = { UINT_MAX, }, |
| 641 | }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 642 | }; |
| 643 | |
| 644 | const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd) |
| 645 | { |
| 646 | if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs)) |
| 647 | return &__nd_cmd_dimm_descs[cmd]; |
| 648 | return NULL; |
| 649 | } |
| 650 | EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc); |
| 651 | |
| 652 | static const struct nd_cmd_desc __nd_cmd_bus_descs[] = { |
| 653 | [ND_CMD_IMPLEMENTED] = { }, |
| 654 | [ND_CMD_ARS_CAP] = { |
| 655 | .in_num = 2, |
| 656 | .in_sizes = { 8, 8, }, |
Dan Williams | 4577b06 | 2016-02-17 13:08:58 -0800 | [diff] [blame] | 657 | .out_num = 4, |
| 658 | .out_sizes = { 4, 4, 4, 4, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 659 | }, |
| 660 | [ND_CMD_ARS_START] = { |
Dan Williams | 4577b06 | 2016-02-17 13:08:58 -0800 | [diff] [blame] | 661 | .in_num = 5, |
| 662 | .in_sizes = { 8, 8, 2, 1, 5, }, |
| 663 | .out_num = 2, |
| 664 | .out_sizes = { 4, 4, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 665 | }, |
| 666 | [ND_CMD_ARS_STATUS] = { |
Dan Williams | 747ffe1 | 2016-02-19 15:21:14 -0800 | [diff] [blame] | 667 | .out_num = 3, |
| 668 | .out_sizes = { 4, 4, UINT_MAX, }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 669 | }, |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 670 | [ND_CMD_CLEAR_ERROR] = { |
| 671 | .in_num = 2, |
| 672 | .in_sizes = { 8, 8, }, |
| 673 | .out_num = 3, |
| 674 | .out_sizes = { 4, 4, 8, }, |
| 675 | }, |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 676 | [ND_CMD_CALL] = { |
| 677 | .in_num = 2, |
| 678 | .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, }, |
| 679 | .out_num = 1, |
| 680 | .out_sizes = { UINT_MAX, }, |
| 681 | }, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 682 | }; |
| 683 | |
| 684 | const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd) |
| 685 | { |
| 686 | if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs)) |
| 687 | return &__nd_cmd_bus_descs[cmd]; |
| 688 | return NULL; |
| 689 | } |
| 690 | EXPORT_SYMBOL_GPL(nd_cmd_bus_desc); |
| 691 | |
| 692 | u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd, |
| 693 | const struct nd_cmd_desc *desc, int idx, void *buf) |
| 694 | { |
| 695 | if (idx >= desc->in_num) |
| 696 | return UINT_MAX; |
| 697 | |
| 698 | if (desc->in_sizes[idx] < UINT_MAX) |
| 699 | return desc->in_sizes[idx]; |
| 700 | |
| 701 | if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) { |
| 702 | struct nd_cmd_set_config_hdr *hdr = buf; |
| 703 | |
| 704 | return hdr->in_length; |
| 705 | } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) { |
| 706 | struct nd_cmd_vendor_hdr *hdr = buf; |
| 707 | |
| 708 | return hdr->in_length; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 709 | } else if (cmd == ND_CMD_CALL) { |
| 710 | struct nd_cmd_pkg *pkg = buf; |
| 711 | |
| 712 | return pkg->nd_size_in; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | return UINT_MAX; |
| 716 | } |
| 717 | EXPORT_SYMBOL_GPL(nd_cmd_in_size); |
| 718 | |
| 719 | u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd, |
| 720 | const struct nd_cmd_desc *desc, int idx, const u32 *in_field, |
Dan Williams | efda1b5d | 2016-12-06 09:10:12 -0800 | [diff] [blame] | 721 | const u32 *out_field, unsigned long remainder) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 722 | { |
| 723 | if (idx >= desc->out_num) |
| 724 | return UINT_MAX; |
| 725 | |
| 726 | if (desc->out_sizes[idx] < UINT_MAX) |
| 727 | return desc->out_sizes[idx]; |
| 728 | |
| 729 | if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1) |
| 730 | return in_field[1]; |
| 731 | else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) |
| 732 | return out_field[1]; |
Dan Williams | efda1b5d | 2016-12-06 09:10:12 -0800 | [diff] [blame] | 733 | else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) { |
| 734 | /* |
| 735 | * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is |
| 736 | * "Size of Output Buffer in bytes, including this |
| 737 | * field." |
| 738 | */ |
| 739 | if (out_field[1] < 4) |
| 740 | return 0; |
| 741 | /* |
| 742 | * ACPI 6.1 is ambiguous if 'status' is included in the |
| 743 | * output size. If we encounter an output size that |
| 744 | * overshoots the remainder by 4 bytes, assume it was |
| 745 | * including 'status'. |
| 746 | */ |
| 747 | if (out_field[1] - 8 == remainder) |
| 748 | return remainder; |
| 749 | return out_field[1] - 4; |
| 750 | } else if (cmd == ND_CMD_CALL) { |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 751 | struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field; |
| 752 | |
| 753 | return pkg->nd_size_out; |
| 754 | } |
| 755 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 756 | |
| 757 | return UINT_MAX; |
| 758 | } |
| 759 | EXPORT_SYMBOL_GPL(nd_cmd_out_size); |
| 760 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 761 | void wait_nvdimm_bus_probe_idle(struct device *dev) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 762 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 763 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 764 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 765 | do { |
| 766 | if (nvdimm_bus->probe_active == 0) |
| 767 | break; |
| 768 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 769 | wait_event(nvdimm_bus->probe_wait, |
| 770 | nvdimm_bus->probe_active == 0); |
| 771 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 772 | } while (true); |
| 773 | } |
| 774 | |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 775 | static int pmem_active(struct device *dev, void *data) |
| 776 | { |
| 777 | if (is_nd_pmem(dev) && dev->driver) |
| 778 | return -EBUSY; |
| 779 | return 0; |
| 780 | } |
| 781 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 782 | /* set_config requires an idle interleave set */ |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 783 | static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus, |
| 784 | struct nvdimm *nvdimm, unsigned int cmd) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 785 | { |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 786 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 787 | |
| 788 | /* ask the bus provider if it would like to block this request */ |
| 789 | if (nd_desc->clear_to_send) { |
| 790 | int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd); |
| 791 | |
| 792 | if (rc) |
| 793 | return rc; |
| 794 | } |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 795 | |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 796 | /* require clear error to go through the pmem driver */ |
| 797 | if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR) |
| 798 | return device_for_each_child(&nvdimm_bus->dev, NULL, |
| 799 | pmem_active); |
| 800 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 801 | if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA) |
| 802 | return 0; |
| 803 | |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 804 | /* prevent label manipulation while the kernel owns label updates */ |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 805 | wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 806 | if (atomic_read(&nvdimm->busy)) |
| 807 | return -EBUSY; |
| 808 | return 0; |
| 809 | } |
| 810 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 811 | static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, |
| 812 | int read_only, unsigned int ioctl_cmd, unsigned long arg) |
| 813 | { |
| 814 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 815 | size_t buf_len = 0, in_len = 0, out_len = 0; |
| 816 | static char out_env[ND_CMD_MAX_ENVELOPE]; |
| 817 | static char in_env[ND_CMD_MAX_ENVELOPE]; |
| 818 | const struct nd_cmd_desc *desc = NULL; |
| 819 | unsigned int cmd = _IOC_NR(ioctl_cmd); |
| 820 | void __user *p = (void __user *) arg; |
| 821 | struct device *dev = &nvdimm_bus->dev; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 822 | struct nd_cmd_pkg pkg; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 823 | const char *cmd_name, *dimm_name; |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 824 | unsigned long cmd_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 825 | void *buf; |
| 826 | int rc, i; |
| 827 | |
| 828 | if (nvdimm) { |
| 829 | desc = nd_cmd_dimm_desc(cmd); |
| 830 | cmd_name = nvdimm_cmd_name(cmd); |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 831 | cmd_mask = nvdimm->cmd_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 832 | dimm_name = dev_name(&nvdimm->dev); |
| 833 | } else { |
| 834 | desc = nd_cmd_bus_desc(cmd); |
| 835 | cmd_name = nvdimm_bus_cmd_name(cmd); |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 836 | cmd_mask = nd_desc->cmd_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 837 | dimm_name = "bus"; |
| 838 | } |
| 839 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 840 | if (cmd == ND_CMD_CALL) { |
| 841 | if (copy_from_user(&pkg, p, sizeof(pkg))) |
| 842 | return -EFAULT; |
| 843 | } |
| 844 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 845 | if (!desc || (desc->out_num + desc->in_num == 0) || |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 846 | !test_bit(cmd, &cmd_mask)) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 847 | return -ENOTTY; |
| 848 | |
| 849 | /* fail write commands (when read-only) */ |
| 850 | if (read_only) |
Jerry Hoemann | 07accfa | 2016-01-06 16:03:41 -0700 | [diff] [blame] | 851 | switch (cmd) { |
| 852 | case ND_CMD_VENDOR: |
| 853 | case ND_CMD_SET_CONFIG_DATA: |
| 854 | case ND_CMD_ARS_START: |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 855 | case ND_CMD_CLEAR_ERROR: |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 856 | case ND_CMD_CALL: |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 857 | dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n", |
| 858 | nvdimm ? nvdimm_cmd_name(cmd) |
| 859 | : nvdimm_bus_cmd_name(cmd)); |
| 860 | return -EPERM; |
| 861 | default: |
| 862 | break; |
| 863 | } |
| 864 | |
| 865 | /* process an input envelope */ |
| 866 | for (i = 0; i < desc->in_num; i++) { |
| 867 | u32 in_size, copy; |
| 868 | |
| 869 | in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env); |
| 870 | if (in_size == UINT_MAX) { |
| 871 | dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n", |
| 872 | __func__, dimm_name, cmd_name, i); |
| 873 | return -ENXIO; |
| 874 | } |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 875 | if (in_len < sizeof(in_env)) |
| 876 | copy = min_t(u32, sizeof(in_env) - in_len, in_size); |
| 877 | else |
| 878 | copy = 0; |
| 879 | if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) |
| 880 | return -EFAULT; |
| 881 | in_len += in_size; |
| 882 | } |
| 883 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 884 | if (cmd == ND_CMD_CALL) { |
| 885 | dev_dbg(dev, "%s:%s, idx: %llu, in: %zu, out: %zu, len %zu\n", |
| 886 | __func__, dimm_name, pkg.nd_command, |
| 887 | in_len, out_len, buf_len); |
| 888 | |
| 889 | for (i = 0; i < ARRAY_SIZE(pkg.nd_reserved2); i++) |
| 890 | if (pkg.nd_reserved2[i]) |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 894 | /* process an output envelope */ |
| 895 | for (i = 0; i < desc->out_num; i++) { |
| 896 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, |
Dan Williams | efda1b5d | 2016-12-06 09:10:12 -0800 | [diff] [blame] | 897 | (u32 *) in_env, (u32 *) out_env, 0); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 898 | u32 copy; |
| 899 | |
| 900 | if (out_size == UINT_MAX) { |
| 901 | dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n", |
| 902 | __func__, dimm_name, cmd_name, i); |
| 903 | return -EFAULT; |
| 904 | } |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 905 | if (out_len < sizeof(out_env)) |
| 906 | copy = min_t(u32, sizeof(out_env) - out_len, out_size); |
| 907 | else |
| 908 | copy = 0; |
| 909 | if (copy && copy_from_user(&out_env[out_len], |
| 910 | p + in_len + out_len, copy)) |
| 911 | return -EFAULT; |
| 912 | out_len += out_size; |
| 913 | } |
| 914 | |
| 915 | buf_len = out_len + in_len; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 916 | if (buf_len > ND_IOCTL_MAX_BUFLEN) { |
| 917 | dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__, |
| 918 | dimm_name, cmd_name, buf_len, |
| 919 | ND_IOCTL_MAX_BUFLEN); |
| 920 | return -EINVAL; |
| 921 | } |
| 922 | |
| 923 | buf = vmalloc(buf_len); |
| 924 | if (!buf) |
| 925 | return -ENOMEM; |
| 926 | |
| 927 | if (copy_from_user(buf, p, buf_len)) { |
| 928 | rc = -EFAULT; |
| 929 | goto out; |
| 930 | } |
| 931 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 932 | nvdimm_bus_lock(&nvdimm_bus->dev); |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 933 | rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, cmd); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 934 | if (rc) |
| 935 | goto out_unlock; |
| 936 | |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 937 | rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, NULL); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 938 | if (rc < 0) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 939 | goto out_unlock; |
Dan Williams | 5ac50e7 | 2017-04-07 09:47:24 -0700 | [diff] [blame] | 940 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 941 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 942 | if (copy_to_user(p, buf, buf_len)) |
| 943 | rc = -EFAULT; |
Dan Williams | 5ac50e7 | 2017-04-07 09:47:24 -0700 | [diff] [blame] | 944 | |
| 945 | vfree(buf); |
| 946 | return rc; |
| 947 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 948 | out_unlock: |
| 949 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 950 | out: |
| 951 | vfree(buf); |
| 952 | return rc; |
| 953 | } |
| 954 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 955 | static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 956 | { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 957 | long id = (long) file->private_data; |
Jerry Hoemann | 4dc0e7b | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 958 | int rc = -ENXIO, ro; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 959 | struct nvdimm_bus *nvdimm_bus; |
| 960 | |
Jerry Hoemann | 4dc0e7b | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 961 | ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 962 | mutex_lock(&nvdimm_bus_list_mutex); |
| 963 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { |
| 964 | if (nvdimm_bus->id == id) { |
Jerry Hoemann | 4dc0e7b | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 965 | rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 966 | break; |
| 967 | } |
| 968 | } |
| 969 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 970 | |
| 971 | return rc; |
| 972 | } |
| 973 | |
| 974 | static int match_dimm(struct device *dev, void *data) |
| 975 | { |
| 976 | long id = (long) data; |
| 977 | |
| 978 | if (is_nvdimm(dev)) { |
| 979 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 980 | |
| 981 | return nvdimm->id == id; |
| 982 | } |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 988 | { |
Jerry Hoemann | 4dc0e7b | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 989 | int rc = -ENXIO, ro; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 990 | struct nvdimm_bus *nvdimm_bus; |
| 991 | |
Jerry Hoemann | 4dc0e7b | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 992 | ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 993 | mutex_lock(&nvdimm_bus_list_mutex); |
| 994 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { |
| 995 | struct device *dev = device_find_child(&nvdimm_bus->dev, |
| 996 | file->private_data, match_dimm); |
| 997 | struct nvdimm *nvdimm; |
| 998 | |
| 999 | if (!dev) |
| 1000 | continue; |
| 1001 | |
| 1002 | nvdimm = to_nvdimm(dev); |
Jerry Hoemann | 4dc0e7b | 2016-01-06 16:03:39 -0700 | [diff] [blame] | 1003 | rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1004 | put_device(dev); |
| 1005 | break; |
| 1006 | } |
| 1007 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 1008 | |
| 1009 | return rc; |
| 1010 | } |
| 1011 | |
| 1012 | static int nd_open(struct inode *inode, struct file *file) |
| 1013 | { |
| 1014 | long minor = iminor(inode); |
| 1015 | |
| 1016 | file->private_data = (void *) minor; |
| 1017 | return 0; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | static const struct file_operations nvdimm_bus_fops = { |
| 1021 | .owner = THIS_MODULE, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1022 | .open = nd_open, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1023 | .unlocked_ioctl = nd_ioctl, |
| 1024 | .compat_ioctl = nd_ioctl, |
| 1025 | .llseek = noop_llseek, |
| 1026 | }; |
| 1027 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1028 | static const struct file_operations nvdimm_fops = { |
| 1029 | .owner = THIS_MODULE, |
| 1030 | .open = nd_open, |
| 1031 | .unlocked_ioctl = nvdimm_ioctl, |
| 1032 | .compat_ioctl = nvdimm_ioctl, |
| 1033 | .llseek = noop_llseek, |
| 1034 | }; |
| 1035 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1036 | int __init nvdimm_bus_init(void) |
| 1037 | { |
| 1038 | int rc; |
| 1039 | |
Dan Williams | baa5127 | 2016-04-05 17:40:52 -0700 | [diff] [blame] | 1040 | BUILD_BUG_ON(sizeof(struct nd_smart_payload) != 128); |
| 1041 | BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload) != 8); |
| 1042 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1043 | rc = bus_register(&nvdimm_bus_type); |
| 1044 | if (rc) |
| 1045 | return rc; |
| 1046 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1047 | rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops); |
| 1048 | if (rc < 0) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1049 | goto err_bus_chrdev; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1050 | nvdimm_bus_major = rc; |
| 1051 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1052 | rc = register_chrdev(0, "dimmctl", &nvdimm_fops); |
| 1053 | if (rc < 0) |
| 1054 | goto err_dimm_chrdev; |
| 1055 | nvdimm_major = rc; |
| 1056 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1057 | nd_class = class_create(THIS_MODULE, "nd"); |
Axel Lin | daa1dee | 2015-06-28 17:00:57 +0800 | [diff] [blame] | 1058 | if (IS_ERR(nd_class)) { |
| 1059 | rc = PTR_ERR(nd_class); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1060 | goto err_class; |
Axel Lin | daa1dee | 2015-06-28 17:00:57 +0800 | [diff] [blame] | 1061 | } |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1062 | |
Dan Williams | 1851594 | 2016-07-22 23:46:08 -0700 | [diff] [blame] | 1063 | rc = driver_register(&nd_bus_driver.drv); |
| 1064 | if (rc) |
| 1065 | goto err_nd_bus; |
| 1066 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1067 | return 0; |
| 1068 | |
Dan Williams | 1851594 | 2016-07-22 23:46:08 -0700 | [diff] [blame] | 1069 | err_nd_bus: |
| 1070 | class_destroy(nd_class); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1071 | err_class: |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1072 | unregister_chrdev(nvdimm_major, "dimmctl"); |
| 1073 | err_dimm_chrdev: |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1074 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1075 | err_bus_chrdev: |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1076 | bus_unregister(&nvdimm_bus_type); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1077 | |
| 1078 | return rc; |
| 1079 | } |
| 1080 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 1081 | void nvdimm_bus_exit(void) |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1082 | { |
Dan Williams | 1851594 | 2016-07-22 23:46:08 -0700 | [diff] [blame] | 1083 | driver_unregister(&nd_bus_driver.drv); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1084 | class_destroy(nd_class); |
| 1085 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1086 | unregister_chrdev(nvdimm_major, "dimmctl"); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1087 | bus_unregister(&nvdimm_bus_type); |
Dan Williams | 1851594 | 2016-07-22 23:46:08 -0700 | [diff] [blame] | 1088 | ida_destroy(&nd_ida); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 1089 | } |