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