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