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