Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 4 | */ |
| 5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Dan Williams | d5d30d5 | 2019-02-02 16:35:26 -0800 | [diff] [blame] | 6 | #include <linux/moduleparam.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 7 | #include <linux/vmalloc.h> |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 8 | #include <linux/device.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 9 | #include <linux/ndctl.h> |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include "nd-core.h" |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 15 | #include "label.h" |
Dan Williams | ca6a465 | 2017-01-13 20:36:58 -0800 | [diff] [blame] | 16 | #include "pmem.h" |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 17 | #include "nd.h" |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 18 | |
| 19 | static DEFINE_IDA(dimm_ida); |
| 20 | |
Dan Williams | d5d30d5 | 2019-02-02 16:35:26 -0800 | [diff] [blame] | 21 | static bool noblk; |
| 22 | module_param(noblk, bool, 0444); |
| 23 | MODULE_PARM_DESC(noblk, "force disable BLK / local alias support"); |
| 24 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 25 | /* |
| 26 | * Retrieve bus and dimm handle and return if this bus supports |
| 27 | * get_config_data commands |
| 28 | */ |
Toshi Kani | aee6598 | 2016-08-16 13:08:40 -0600 | [diff] [blame] | 29 | int nvdimm_check_config_data(struct device *dev) |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 30 | { |
Toshi Kani | aee6598 | 2016-08-16 13:08:40 -0600 | [diff] [blame] | 31 | struct nvdimm *nvdimm = to_nvdimm(dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 32 | |
Toshi Kani | aee6598 | 2016-08-16 13:08:40 -0600 | [diff] [blame] | 33 | if (!nvdimm->cmd_mask || |
| 34 | !test_bit(ND_CMD_GET_CONFIG_DATA, &nvdimm->cmd_mask)) { |
Dan Williams | 8f078b3 | 2017-05-04 14:01:24 -0700 | [diff] [blame] | 35 | if (test_bit(NDD_ALIASING, &nvdimm->flags)) |
Toshi Kani | aee6598 | 2016-08-16 13:08:40 -0600 | [diff] [blame] | 36 | return -ENXIO; |
| 37 | else |
| 38 | return -ENOTTY; |
| 39 | } |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int validate_dimm(struct nvdimm_drvdata *ndd) |
| 45 | { |
Toshi Kani | aee6598 | 2016-08-16 13:08:40 -0600 | [diff] [blame] | 46 | int rc; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 47 | |
Toshi Kani | aee6598 | 2016-08-16 13:08:40 -0600 | [diff] [blame] | 48 | if (!ndd) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | rc = nvdimm_check_config_data(ndd->dev); |
| 52 | if (rc) |
Sakari Ailus | d75f773 | 2019-03-25 21:32:28 +0200 | [diff] [blame] | 53 | dev_dbg(ndd->dev, "%ps: %s error: %d\n", |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 54 | __builtin_return_address(0), __func__, rc); |
| 55 | return rc; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area |
| 60 | * @nvdimm: dimm to initialize |
| 61 | */ |
| 62 | int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd) |
| 63 | { |
| 64 | struct nd_cmd_get_config_size *cmd = &ndd->nsarea; |
| 65 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev); |
| 66 | struct nvdimm_bus_descriptor *nd_desc; |
| 67 | int rc = validate_dimm(ndd); |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 68 | int cmd_rc = 0; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 69 | |
| 70 | if (rc) |
| 71 | return rc; |
| 72 | |
| 73 | if (cmd->config_size) |
| 74 | return 0; /* already valid */ |
| 75 | |
| 76 | memset(cmd, 0, sizeof(*cmd)); |
| 77 | nd_desc = nvdimm_bus->nd_desc; |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 78 | rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev), |
| 79 | ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd), &cmd_rc); |
| 80 | if (rc < 0) |
| 81 | return rc; |
| 82 | return cmd_rc; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 85 | int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf, |
| 86 | size_t offset, size_t len) |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 87 | { |
| 88 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev); |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 89 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
Dan Williams | e7c5a57 | 2018-04-09 12:34:24 -0700 | [diff] [blame] | 90 | int rc = validate_dimm(ndd), cmd_rc = 0; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 91 | struct nd_cmd_get_config_data_hdr *cmd; |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 92 | size_t max_cmd_size, buf_offset; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 93 | |
| 94 | if (rc) |
| 95 | return rc; |
| 96 | |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 97 | if (offset + len > ndd->nsarea.config_size) |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 98 | return -ENXIO; |
| 99 | |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 100 | max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer); |
Dan Williams | d11cf4a | 2018-10-10 16:38:24 -0700 | [diff] [blame] | 101 | cmd = kvzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 102 | if (!cmd) |
| 103 | return -ENOMEM; |
| 104 | |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 105 | for (buf_offset = 0; len; |
| 106 | len -= cmd->in_length, buf_offset += cmd->in_length) { |
| 107 | size_t cmd_size; |
| 108 | |
| 109 | cmd->in_offset = offset + buf_offset; |
| 110 | cmd->in_length = min(max_cmd_size, len); |
| 111 | |
| 112 | cmd_size = sizeof(*cmd) + cmd->in_length; |
| 113 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 114 | rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev), |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 115 | ND_CMD_GET_CONFIG_DATA, cmd, cmd_size, &cmd_rc); |
Dan Williams | e7c5a57 | 2018-04-09 12:34:24 -0700 | [diff] [blame] | 116 | if (rc < 0) |
| 117 | break; |
| 118 | if (cmd_rc < 0) { |
| 119 | rc = cmd_rc; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 120 | break; |
| 121 | } |
Alexander Duyck | 2d657d1 | 2018-10-10 16:39:20 -0700 | [diff] [blame] | 122 | |
| 123 | /* out_buf should be valid, copy it into our output buffer */ |
| 124 | memcpy(buf + buf_offset, cmd->out_buf, cmd->in_length); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 125 | } |
Dan Williams | d11cf4a | 2018-10-10 16:38:24 -0700 | [diff] [blame] | 126 | kvfree(cmd); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 127 | |
| 128 | return rc; |
| 129 | } |
| 130 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 131 | int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset, |
| 132 | void *buf, size_t len) |
| 133 | { |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 134 | size_t max_cmd_size, buf_offset; |
| 135 | struct nd_cmd_set_config_hdr *cmd; |
Dan Williams | e7c5a57 | 2018-04-09 12:34:24 -0700 | [diff] [blame] | 136 | int rc = validate_dimm(ndd), cmd_rc = 0; |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 137 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev); |
| 138 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 139 | |
| 140 | if (rc) |
| 141 | return rc; |
| 142 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 143 | if (offset + len > ndd->nsarea.config_size) |
| 144 | return -ENXIO; |
| 145 | |
Dan Williams | d11cf4a | 2018-10-10 16:38:24 -0700 | [diff] [blame] | 146 | max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer); |
| 147 | cmd = kvzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 148 | if (!cmd) |
| 149 | return -ENOMEM; |
| 150 | |
| 151 | for (buf_offset = 0; len; len -= cmd->in_length, |
| 152 | buf_offset += cmd->in_length) { |
| 153 | size_t cmd_size; |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 154 | |
| 155 | cmd->in_offset = offset + buf_offset; |
| 156 | cmd->in_length = min(max_cmd_size, len); |
| 157 | memcpy(cmd->in_buf, buf + buf_offset, cmd->in_length); |
| 158 | |
| 159 | /* status is output in the last 4-bytes of the command buffer */ |
| 160 | cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 161 | |
| 162 | rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev), |
Dan Williams | e7c5a57 | 2018-04-09 12:34:24 -0700 | [diff] [blame] | 163 | ND_CMD_SET_CONFIG_DATA, cmd, cmd_size, &cmd_rc); |
| 164 | if (rc < 0) |
| 165 | break; |
| 166 | if (cmd_rc < 0) { |
| 167 | rc = cmd_rc; |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | } |
Dan Williams | d11cf4a | 2018-10-10 16:38:24 -0700 | [diff] [blame] | 171 | kvfree(cmd); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 172 | |
| 173 | return rc; |
| 174 | } |
| 175 | |
Dan Williams | 42237e3 | 2016-10-15 15:33:52 -0700 | [diff] [blame] | 176 | void nvdimm_set_aliasing(struct device *dev) |
| 177 | { |
| 178 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 179 | |
Dan Williams | 8f078b3 | 2017-05-04 14:01:24 -0700 | [diff] [blame] | 180 | set_bit(NDD_ALIASING, &nvdimm->flags); |
| 181 | } |
| 182 | |
| 183 | void nvdimm_set_locked(struct device *dev) |
| 184 | { |
| 185 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 186 | |
| 187 | set_bit(NDD_LOCKED, &nvdimm->flags); |
Dan Williams | 42237e3 | 2016-10-15 15:33:52 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dan Williams | d34cb80 | 2017-09-25 11:01:31 -0700 | [diff] [blame] | 190 | void nvdimm_clear_locked(struct device *dev) |
| 191 | { |
| 192 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 193 | |
| 194 | clear_bit(NDD_LOCKED, &nvdimm->flags); |
| 195 | } |
| 196 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 197 | static void nvdimm_release(struct device *dev) |
| 198 | { |
| 199 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 200 | |
| 201 | ida_simple_remove(&dimm_ida, nvdimm->id); |
| 202 | kfree(nvdimm); |
| 203 | } |
| 204 | |
| 205 | static struct device_type nvdimm_device_type = { |
| 206 | .name = "nvdimm", |
| 207 | .release = nvdimm_release, |
| 208 | }; |
| 209 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 210 | bool is_nvdimm(struct device *dev) |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 211 | { |
| 212 | return dev->type == &nvdimm_device_type; |
| 213 | } |
| 214 | |
| 215 | struct nvdimm *to_nvdimm(struct device *dev) |
| 216 | { |
| 217 | struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev); |
| 218 | |
| 219 | WARN_ON(!is_nvdimm(dev)); |
| 220 | return nvdimm; |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(to_nvdimm); |
| 223 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 224 | struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr) |
| 225 | { |
| 226 | struct nd_region *nd_region = &ndbr->nd_region; |
| 227 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 228 | |
| 229 | return nd_mapping->nvdimm; |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm); |
| 232 | |
Dan Williams | ca6a465 | 2017-01-13 20:36:58 -0800 | [diff] [blame] | 233 | unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr) |
| 234 | { |
| 235 | /* pmem mapping properties are private to libnvdimm */ |
| 236 | return ARCH_MEMREMAP_PMEM; |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(nd_blk_memremap_flags); |
| 239 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 240 | struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping) |
| 241 | { |
| 242 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 243 | |
| 244 | WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev)); |
| 245 | |
| 246 | return dev_get_drvdata(&nvdimm->dev); |
| 247 | } |
| 248 | EXPORT_SYMBOL(to_ndd); |
| 249 | |
| 250 | void nvdimm_drvdata_release(struct kref *kref) |
| 251 | { |
| 252 | struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref); |
| 253 | struct device *dev = ndd->dev; |
| 254 | struct resource *res, *_r; |
| 255 | |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 256 | dev_dbg(dev, "trace\n"); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 257 | nvdimm_bus_lock(dev); |
| 258 | for_each_dpa_resource_safe(ndd, res, _r) |
| 259 | nvdimm_free_dpa(ndd, res); |
| 260 | nvdimm_bus_unlock(dev); |
| 261 | |
yalin wang | a06a757 | 2015-08-27 19:35:48 -0400 | [diff] [blame] | 262 | kvfree(ndd->data); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 263 | kfree(ndd); |
| 264 | put_device(dev); |
| 265 | } |
| 266 | |
| 267 | void get_ndd(struct nvdimm_drvdata *ndd) |
| 268 | { |
| 269 | kref_get(&ndd->kref); |
| 270 | } |
| 271 | |
| 272 | void put_ndd(struct nvdimm_drvdata *ndd) |
| 273 | { |
| 274 | if (ndd) |
| 275 | kref_put(&ndd->kref, nvdimm_drvdata_release); |
| 276 | } |
| 277 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 278 | const char *nvdimm_name(struct nvdimm *nvdimm) |
| 279 | { |
| 280 | return dev_name(&nvdimm->dev); |
| 281 | } |
| 282 | EXPORT_SYMBOL_GPL(nvdimm_name); |
| 283 | |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 284 | struct kobject *nvdimm_kobj(struct nvdimm *nvdimm) |
| 285 | { |
| 286 | return &nvdimm->dev.kobj; |
| 287 | } |
| 288 | EXPORT_SYMBOL_GPL(nvdimm_kobj); |
| 289 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 290 | unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm) |
| 291 | { |
| 292 | return nvdimm->cmd_mask; |
| 293 | } |
| 294 | EXPORT_SYMBOL_GPL(nvdimm_cmd_mask); |
| 295 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 296 | void *nvdimm_provider_data(struct nvdimm *nvdimm) |
| 297 | { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 298 | if (nvdimm) |
| 299 | return nvdimm->provider_data; |
| 300 | return NULL; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 301 | } |
| 302 | EXPORT_SYMBOL_GPL(nvdimm_provider_data); |
| 303 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 304 | static ssize_t commands_show(struct device *dev, |
| 305 | struct device_attribute *attr, char *buf) |
| 306 | { |
| 307 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 308 | int cmd, len = 0; |
| 309 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 310 | if (!nvdimm->cmd_mask) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 311 | return sprintf(buf, "\n"); |
| 312 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 313 | for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 314 | len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd)); |
| 315 | len += sprintf(buf + len, "\n"); |
| 316 | return len; |
| 317 | } |
| 318 | static DEVICE_ATTR_RO(commands); |
| 319 | |
Dan Williams | efbf6f5 | 2017-09-25 10:24:26 -0700 | [diff] [blame] | 320 | static ssize_t flags_show(struct device *dev, |
| 321 | struct device_attribute *attr, char *buf) |
| 322 | { |
| 323 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 324 | |
| 325 | return sprintf(buf, "%s%s\n", |
| 326 | test_bit(NDD_ALIASING, &nvdimm->flags) ? "alias " : "", |
| 327 | test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : ""); |
| 328 | } |
| 329 | static DEVICE_ATTR_RO(flags); |
| 330 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 331 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 332 | char *buf) |
| 333 | { |
| 334 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 335 | |
| 336 | /* |
| 337 | * The state may be in the process of changing, userspace should |
| 338 | * quiesce probing if it wants a static answer |
| 339 | */ |
| 340 | nvdimm_bus_lock(dev); |
| 341 | nvdimm_bus_unlock(dev); |
| 342 | return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy) |
| 343 | ? "active" : "idle"); |
| 344 | } |
| 345 | static DEVICE_ATTR_RO(state); |
| 346 | |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 347 | static ssize_t available_slots_show(struct device *dev, |
| 348 | struct device_attribute *attr, char *buf) |
| 349 | { |
| 350 | struct nvdimm_drvdata *ndd = dev_get_drvdata(dev); |
| 351 | ssize_t rc; |
| 352 | u32 nfree; |
| 353 | |
| 354 | if (!ndd) |
| 355 | return -ENXIO; |
| 356 | |
| 357 | nvdimm_bus_lock(dev); |
| 358 | nfree = nd_label_nfree(ndd); |
| 359 | if (nfree - 1 > nfree) { |
| 360 | dev_WARN_ONCE(dev, 1, "we ate our last label?\n"); |
| 361 | nfree = 0; |
| 362 | } else |
| 363 | nfree--; |
| 364 | rc = sprintf(buf, "%d\n", nfree); |
| 365 | nvdimm_bus_unlock(dev); |
| 366 | return rc; |
| 367 | } |
| 368 | static DEVICE_ATTR_RO(available_slots); |
| 369 | |
Dave Jiang | 3c13e2a | 2018-12-10 13:20:42 -0700 | [diff] [blame] | 370 | __weak ssize_t security_show(struct device *dev, |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 371 | struct device_attribute *attr, char *buf) |
| 372 | { |
| 373 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 374 | |
| 375 | switch (nvdimm->sec.state) { |
| 376 | case NVDIMM_SECURITY_DISABLED: |
| 377 | return sprintf(buf, "disabled\n"); |
| 378 | case NVDIMM_SECURITY_UNLOCKED: |
| 379 | return sprintf(buf, "unlocked\n"); |
| 380 | case NVDIMM_SECURITY_LOCKED: |
| 381 | return sprintf(buf, "locked\n"); |
| 382 | case NVDIMM_SECURITY_FROZEN: |
| 383 | return sprintf(buf, "frozen\n"); |
| 384 | case NVDIMM_SECURITY_OVERWRITE: |
| 385 | return sprintf(buf, "overwrite\n"); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 386 | default: |
| 387 | return -ENOTTY; |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return -ENOTTY; |
| 391 | } |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 392 | |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 393 | #define OPS \ |
| 394 | C( OP_FREEZE, "freeze", 1), \ |
| 395 | C( OP_DISABLE, "disable", 2), \ |
| 396 | C( OP_UPDATE, "update", 3), \ |
| 397 | C( OP_ERASE, "erase", 2), \ |
| 398 | C( OP_OVERWRITE, "overwrite", 2), \ |
| 399 | C( OP_MASTER_UPDATE, "master_update", 3), \ |
| 400 | C( OP_MASTER_ERASE, "master_erase", 2) |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 401 | #undef C |
| 402 | #define C(a, b, c) a |
| 403 | enum nvdimmsec_op_ids { OPS }; |
| 404 | #undef C |
| 405 | #define C(a, b, c) { b, c } |
| 406 | static struct { |
| 407 | const char *name; |
| 408 | int args; |
| 409 | } ops[] = { OPS }; |
| 410 | #undef C |
| 411 | |
| 412 | #define SEC_CMD_SIZE 32 |
| 413 | #define KEY_ID_SIZE 10 |
| 414 | |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 415 | static ssize_t __security_store(struct device *dev, const char *buf, size_t len) |
| 416 | { |
| 417 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 418 | ssize_t rc; |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 419 | char cmd[SEC_CMD_SIZE+1], keystr[KEY_ID_SIZE+1], |
| 420 | nkeystr[KEY_ID_SIZE+1]; |
| 421 | unsigned int key, newkey; |
| 422 | int i; |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 423 | |
| 424 | if (atomic_read(&nvdimm->busy)) |
| 425 | return -EBUSY; |
| 426 | |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 427 | rc = sscanf(buf, "%"__stringify(SEC_CMD_SIZE)"s" |
| 428 | " %"__stringify(KEY_ID_SIZE)"s" |
| 429 | " %"__stringify(KEY_ID_SIZE)"s", |
| 430 | cmd, keystr, nkeystr); |
| 431 | if (rc < 1) |
| 432 | return -EINVAL; |
| 433 | for (i = 0; i < ARRAY_SIZE(ops); i++) |
| 434 | if (sysfs_streq(cmd, ops[i].name)) |
| 435 | break; |
| 436 | if (i >= ARRAY_SIZE(ops)) |
| 437 | return -EINVAL; |
| 438 | if (ops[i].args > 1) |
| 439 | rc = kstrtouint(keystr, 0, &key); |
| 440 | if (rc >= 0 && ops[i].args > 2) |
| 441 | rc = kstrtouint(nkeystr, 0, &newkey); |
| 442 | if (rc < 0) |
| 443 | return rc; |
| 444 | |
| 445 | if (i == OP_FREEZE) { |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 446 | dev_dbg(dev, "freeze\n"); |
| 447 | rc = nvdimm_security_freeze(nvdimm); |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 448 | } else if (i == OP_DISABLE) { |
| 449 | dev_dbg(dev, "disable %u\n", key); |
| 450 | rc = nvdimm_security_disable(nvdimm, key); |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 451 | } else if (i == OP_UPDATE) { |
| 452 | dev_dbg(dev, "update %u %u\n", key, newkey); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 453 | rc = nvdimm_security_update(nvdimm, key, newkey, NVDIMM_USER); |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 454 | } else if (i == OP_ERASE) { |
| 455 | dev_dbg(dev, "erase %u\n", key); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 456 | rc = nvdimm_security_erase(nvdimm, key, NVDIMM_USER); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 457 | } else if (i == OP_OVERWRITE) { |
| 458 | dev_dbg(dev, "overwrite %u\n", key); |
| 459 | rc = nvdimm_security_overwrite(nvdimm, key); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 460 | } else if (i == OP_MASTER_UPDATE) { |
| 461 | dev_dbg(dev, "master_update %u %u\n", key, newkey); |
| 462 | rc = nvdimm_security_update(nvdimm, key, newkey, |
| 463 | NVDIMM_MASTER); |
| 464 | } else if (i == OP_MASTER_ERASE) { |
| 465 | dev_dbg(dev, "master_erase %u\n", key); |
| 466 | rc = nvdimm_security_erase(nvdimm, key, |
| 467 | NVDIMM_MASTER); |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 468 | } else |
| 469 | return -EINVAL; |
| 470 | |
| 471 | if (rc == 0) |
| 472 | rc = len; |
| 473 | return rc; |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static ssize_t security_store(struct device *dev, |
| 477 | struct device_attribute *attr, const char *buf, size_t len) |
| 478 | |
| 479 | { |
| 480 | ssize_t rc; |
| 481 | |
| 482 | /* |
| 483 | * Require all userspace triggered security management to be |
| 484 | * done while probing is idle and the DIMM is not in active use |
| 485 | * in any region. |
| 486 | */ |
| 487 | device_lock(dev); |
| 488 | nvdimm_bus_lock(dev); |
| 489 | wait_nvdimm_bus_probe_idle(dev); |
| 490 | rc = __security_store(dev, buf, len); |
| 491 | nvdimm_bus_unlock(dev); |
| 492 | device_unlock(dev); |
| 493 | |
| 494 | return rc; |
| 495 | } |
| 496 | static DEVICE_ATTR_RW(security); |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 497 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 498 | static struct attribute *nvdimm_attributes[] = { |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 499 | &dev_attr_state.attr, |
Dan Williams | efbf6f5 | 2017-09-25 10:24:26 -0700 | [diff] [blame] | 500 | &dev_attr_flags.attr, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 501 | &dev_attr_commands.attr, |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 502 | &dev_attr_available_slots.attr, |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 503 | &dev_attr_security.attr, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 504 | NULL, |
| 505 | }; |
| 506 | |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 507 | static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n) |
| 508 | { |
| 509 | struct device *dev = container_of(kobj, typeof(*dev), kobj); |
| 510 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 511 | |
| 512 | if (a != &dev_attr_security.attr) |
| 513 | return a->mode; |
| 514 | if (nvdimm->sec.state < 0) |
| 515 | return 0; |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 516 | /* Are there any state mutation ops? */ |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 517 | if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 518 | || nvdimm->sec.ops->change_key |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 519 | || nvdimm->sec.ops->erase |
| 520 | || nvdimm->sec.ops->overwrite) |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 521 | return a->mode; |
| 522 | return 0444; |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 525 | struct attribute_group nvdimm_attribute_group = { |
| 526 | .attrs = nvdimm_attributes, |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 527 | .is_visible = nvdimm_visible, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 528 | }; |
| 529 | EXPORT_SYMBOL_GPL(nvdimm_attribute_group); |
| 530 | |
Dave Jiang | d6548ae | 2018-12-04 10:31:20 -0800 | [diff] [blame] | 531 | struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus, |
| 532 | void *provider_data, const struct attribute_group **groups, |
| 533 | unsigned long flags, unsigned long cmd_mask, int num_flush, |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 534 | struct resource *flush_wpq, const char *dimm_id, |
| 535 | const struct nvdimm_security_ops *sec_ops) |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 536 | { |
| 537 | struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL); |
| 538 | struct device *dev; |
| 539 | |
| 540 | if (!nvdimm) |
| 541 | return NULL; |
| 542 | |
| 543 | nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL); |
| 544 | if (nvdimm->id < 0) { |
| 545 | kfree(nvdimm); |
| 546 | return NULL; |
| 547 | } |
Dave Jiang | d6548ae | 2018-12-04 10:31:20 -0800 | [diff] [blame] | 548 | |
| 549 | nvdimm->dimm_id = dimm_id; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 550 | nvdimm->provider_data = provider_data; |
Dan Williams | d5d30d5 | 2019-02-02 16:35:26 -0800 | [diff] [blame] | 551 | if (noblk) |
| 552 | flags |= 1 << NDD_NOBLK; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 553 | nvdimm->flags = flags; |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 554 | nvdimm->cmd_mask = cmd_mask; |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 555 | nvdimm->num_flush = num_flush; |
| 556 | nvdimm->flush_wpq = flush_wpq; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 557 | atomic_set(&nvdimm->busy, 0); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 558 | dev = &nvdimm->dev; |
| 559 | dev_set_name(dev, "nmem%d", nvdimm->id); |
| 560 | dev->parent = &nvdimm_bus->dev; |
| 561 | dev->type = &nvdimm_device_type; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 562 | dev->devt = MKDEV(nvdimm_major, nvdimm->id); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 563 | dev->groups = groups; |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 564 | nvdimm->sec.ops = sec_ops; |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 565 | nvdimm->sec.overwrite_tmo = 0; |
| 566 | INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query); |
Dave Jiang | f298939 | 2018-12-05 23:39:29 -0800 | [diff] [blame] | 567 | /* |
| 568 | * Security state must be initialized before device_add() for |
| 569 | * attribute visibility. |
| 570 | */ |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 571 | /* get security state and extended (master) state */ |
| 572 | nvdimm->sec.state = nvdimm_security_state(nvdimm, NVDIMM_USER); |
| 573 | nvdimm->sec.ext_state = nvdimm_security_state(nvdimm, NVDIMM_MASTER); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 574 | nd_device_register(dev); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 575 | |
| 576 | return nvdimm; |
| 577 | } |
Dave Jiang | d6548ae | 2018-12-04 10:31:20 -0800 | [diff] [blame] | 578 | EXPORT_SYMBOL_GPL(__nvdimm_create); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 579 | |
Dan Williams | 1cd7386 | 2019-01-19 08:45:56 -0800 | [diff] [blame] | 580 | static void shutdown_security_notify(void *data) |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 581 | { |
Dan Williams | 1cd7386 | 2019-01-19 08:45:56 -0800 | [diff] [blame] | 582 | struct nvdimm *nvdimm = data; |
| 583 | |
| 584 | sysfs_put(nvdimm->sec.overwrite_state); |
| 585 | } |
| 586 | |
| 587 | int nvdimm_security_setup_events(struct device *dev) |
| 588 | { |
| 589 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 590 | |
| 591 | if (nvdimm->sec.state < 0 || !nvdimm->sec.ops |
| 592 | || !nvdimm->sec.ops->overwrite) |
| 593 | return 0; |
| 594 | nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 595 | if (!nvdimm->sec.overwrite_state) |
Dan Williams | 1cd7386 | 2019-01-19 08:45:56 -0800 | [diff] [blame] | 596 | return -ENOMEM; |
| 597 | |
| 598 | return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 599 | } |
| 600 | EXPORT_SYMBOL_GPL(nvdimm_security_setup_events); |
| 601 | |
| 602 | int nvdimm_in_overwrite(struct nvdimm *nvdimm) |
| 603 | { |
| 604 | return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags); |
| 605 | } |
| 606 | EXPORT_SYMBOL_GPL(nvdimm_in_overwrite); |
| 607 | |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 608 | int nvdimm_security_freeze(struct nvdimm *nvdimm) |
| 609 | { |
| 610 | int rc; |
| 611 | |
| 612 | WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev)); |
| 613 | |
| 614 | if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze) |
| 615 | return -EOPNOTSUPP; |
| 616 | |
| 617 | if (nvdimm->sec.state < 0) |
| 618 | return -EIO; |
| 619 | |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 620 | if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) { |
| 621 | dev_warn(&nvdimm->dev, "Overwrite operation in progress.\n"); |
| 622 | return -EBUSY; |
| 623 | } |
| 624 | |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 625 | rc = nvdimm->sec.ops->freeze(nvdimm); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 626 | nvdimm->sec.state = nvdimm_security_state(nvdimm, NVDIMM_USER); |
Dave Jiang | 37833fb | 2018-12-06 09:14:08 -0800 | [diff] [blame] | 627 | |
| 628 | return rc; |
| 629 | } |
| 630 | |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 631 | int alias_dpa_busy(struct device *dev, void *data) |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 632 | { |
Dan Williams | fe51473 | 2017-04-04 15:08:36 -0700 | [diff] [blame] | 633 | resource_size_t map_end, blk_start, new; |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 634 | struct blk_alloc_info *info = data; |
| 635 | struct nd_mapping *nd_mapping; |
| 636 | struct nd_region *nd_region; |
| 637 | struct nvdimm_drvdata *ndd; |
| 638 | struct resource *res; |
| 639 | int i; |
| 640 | |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 641 | if (!is_memory(dev)) |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | |
| 644 | nd_region = to_nd_region(dev); |
| 645 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 646 | nd_mapping = &nd_region->mapping[i]; |
| 647 | if (nd_mapping->nvdimm == info->nd_mapping->nvdimm) |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | if (i >= nd_region->ndr_mappings) |
| 652 | return 0; |
| 653 | |
| 654 | ndd = to_ndd(nd_mapping); |
| 655 | map_end = nd_mapping->start + nd_mapping->size - 1; |
| 656 | blk_start = nd_mapping->start; |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * In the allocation case ->res is set to free space that we are |
| 660 | * looking to validate against PMEM aliasing collision rules |
| 661 | * (i.e. BLK is allocated after all aliased PMEM). |
| 662 | */ |
| 663 | if (info->res) { |
| 664 | if (info->res->start >= nd_mapping->start |
| 665 | && info->res->start < map_end) |
| 666 | /* pass */; |
| 667 | else |
| 668 | return 0; |
| 669 | } |
| 670 | |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 671 | retry: |
| 672 | /* |
| 673 | * Find the free dpa from the end of the last pmem allocation to |
Dan Williams | fe51473 | 2017-04-04 15:08:36 -0700 | [diff] [blame] | 674 | * the end of the interleave-set mapping. |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 675 | */ |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 676 | for_each_dpa_resource(ndd, res) { |
Dan Williams | fe51473 | 2017-04-04 15:08:36 -0700 | [diff] [blame] | 677 | if (strncmp(res->name, "pmem", 4) != 0) |
| 678 | continue; |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 679 | if ((res->start >= blk_start && res->start < map_end) |
| 680 | || (res->end >= blk_start |
| 681 | && res->end <= map_end)) { |
Dan Williams | fe51473 | 2017-04-04 15:08:36 -0700 | [diff] [blame] | 682 | new = max(blk_start, min(map_end + 1, res->end + 1)); |
| 683 | if (new != blk_start) { |
| 684 | blk_start = new; |
| 685 | goto retry; |
| 686 | } |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 690 | /* update the free space range with the probed blk_start */ |
| 691 | if (info->res && blk_start > info->res->start) { |
| 692 | info->res->start = max(info->res->start, blk_start); |
| 693 | if (info->res->start > info->res->end) |
| 694 | info->res->end = info->res->start - 1; |
| 695 | return 1; |
| 696 | } |
| 697 | |
Dan Williams | fe51473 | 2017-04-04 15:08:36 -0700 | [diff] [blame] | 698 | info->available -= blk_start - nd_mapping->start; |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 699 | |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 703 | /** |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 704 | * nd_blk_available_dpa - account the unused dpa of BLK region |
| 705 | * @nd_mapping: container of dpa-resource-root + labels |
| 706 | * |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 707 | * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but |
| 708 | * we arrange for them to never start at an lower dpa than the last |
| 709 | * PMEM allocation in an aliased region. |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 710 | */ |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 711 | resource_size_t nd_blk_available_dpa(struct nd_region *nd_region) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 712 | { |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 713 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
| 714 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 715 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 716 | struct blk_alloc_info info = { |
| 717 | .nd_mapping = nd_mapping, |
| 718 | .available = nd_mapping->size, |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 719 | .res = NULL, |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 720 | }; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 721 | struct resource *res; |
| 722 | |
| 723 | if (!ndd) |
| 724 | return 0; |
| 725 | |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 726 | device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 727 | |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 728 | /* now account for busy blk allocations in unaliased dpa */ |
| 729 | for_each_dpa_resource(ndd, res) { |
| 730 | if (strncmp(res->name, "blk", 3) != 0) |
| 731 | continue; |
Dan Williams | fe51473 | 2017-04-04 15:08:36 -0700 | [diff] [blame] | 732 | info.available -= resource_size(res); |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | return info.available; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /** |
Keith Busch | 12e3129 | 2018-07-24 15:07:57 -0600 | [diff] [blame] | 739 | * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max |
| 740 | * contiguous unallocated dpa range. |
| 741 | * @nd_region: constrain available space check to this reference region |
| 742 | * @nd_mapping: container of dpa-resource-root + labels |
| 743 | */ |
| 744 | resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region, |
| 745 | struct nd_mapping *nd_mapping) |
| 746 | { |
| 747 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 748 | struct nvdimm_bus *nvdimm_bus; |
| 749 | resource_size_t max = 0; |
| 750 | struct resource *res; |
| 751 | |
| 752 | /* if a dimm is disabled the available capacity is zero */ |
| 753 | if (!ndd) |
| 754 | return 0; |
| 755 | |
| 756 | nvdimm_bus = walk_to_nvdimm_bus(ndd->dev); |
| 757 | if (__reserve_free_pmem(&nd_region->dev, nd_mapping->nvdimm)) |
| 758 | return 0; |
| 759 | for_each_dpa_resource(ndd, res) { |
| 760 | if (strcmp(res->name, "pmem-reserve") != 0) |
| 761 | continue; |
| 762 | if (resource_size(res) > max) |
| 763 | max = resource_size(res); |
| 764 | } |
| 765 | release_free_pmem(nvdimm_bus, nd_mapping); |
| 766 | return max; |
| 767 | } |
| 768 | |
| 769 | /** |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 770 | * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa |
| 771 | * @nd_mapping: container of dpa-resource-root + labels |
| 772 | * @nd_region: constrain available space check to this reference region |
| 773 | * @overlap: calculate available space assuming this level of overlap |
| 774 | * |
| 775 | * Validate that a PMEM label, if present, aligns with the start of an |
| 776 | * interleave set and truncate the available size at the lowest BLK |
| 777 | * overlap point. |
| 778 | * |
| 779 | * The expectation is that this routine is called multiple times as it |
| 780 | * probes for the largest BLK encroachment for any single member DIMM of |
| 781 | * the interleave set. Once that value is determined the PMEM-limit for |
| 782 | * the set can be established. |
| 783 | */ |
| 784 | resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region, |
| 785 | struct nd_mapping *nd_mapping, resource_size_t *overlap) |
| 786 | { |
| 787 | resource_size_t map_start, map_end, busy = 0, available, blk_start; |
| 788 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 789 | struct resource *res; |
| 790 | const char *reason; |
| 791 | |
| 792 | if (!ndd) |
| 793 | return 0; |
| 794 | |
| 795 | map_start = nd_mapping->start; |
| 796 | map_end = map_start + nd_mapping->size - 1; |
| 797 | blk_start = max(map_start, map_end + 1 - *overlap); |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 798 | for_each_dpa_resource(ndd, res) { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 799 | if (res->start >= map_start && res->start < map_end) { |
| 800 | if (strncmp(res->name, "blk", 3) == 0) |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 801 | blk_start = min(blk_start, |
| 802 | max(map_start, res->start)); |
| 803 | else if (res->end > map_end) { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 804 | reason = "misaligned to iset"; |
| 805 | goto err; |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 806 | } else |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 807 | busy += resource_size(res); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 808 | } else if (res->end >= map_start && res->end <= map_end) { |
| 809 | if (strncmp(res->name, "blk", 3) == 0) { |
| 810 | /* |
| 811 | * If a BLK allocation overlaps the start of |
| 812 | * PMEM the entire interleave set may now only |
| 813 | * be used for BLK. |
| 814 | */ |
| 815 | blk_start = map_start; |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 816 | } else |
| 817 | busy += resource_size(res); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 818 | } else if (map_start > res->start && map_start < res->end) { |
| 819 | /* total eclipse of the mapping */ |
| 820 | busy += nd_mapping->size; |
| 821 | blk_start = map_start; |
| 822 | } |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame] | 823 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 824 | |
| 825 | *overlap = map_end + 1 - blk_start; |
| 826 | available = blk_start - map_start; |
| 827 | if (busy < available) |
| 828 | return available - busy; |
| 829 | return 0; |
| 830 | |
| 831 | err: |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 832 | nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason); |
| 833 | return 0; |
| 834 | } |
| 835 | |
Dan Williams | 4a826c8 | 2015-06-09 16:09:36 -0400 | [diff] [blame] | 836 | void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res) |
| 837 | { |
| 838 | WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev)); |
| 839 | kfree(res->name); |
| 840 | __release_region(&ndd->dpa, res->start, resource_size(res)); |
| 841 | } |
| 842 | |
| 843 | struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd, |
| 844 | struct nd_label_id *label_id, resource_size_t start, |
| 845 | resource_size_t n) |
| 846 | { |
| 847 | char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL); |
| 848 | struct resource *res; |
| 849 | |
| 850 | if (!name) |
| 851 | return NULL; |
| 852 | |
| 853 | WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev)); |
| 854 | res = __request_region(&ndd->dpa, start, n, name, 0); |
| 855 | if (!res) |
| 856 | kfree(name); |
| 857 | return res; |
| 858 | } |
| 859 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 860 | /** |
| 861 | * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id |
| 862 | * @nvdimm: container of dpa-resource-root + labels |
| 863 | * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid> |
| 864 | */ |
| 865 | resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd, |
| 866 | struct nd_label_id *label_id) |
| 867 | { |
| 868 | resource_size_t allocated = 0; |
| 869 | struct resource *res; |
| 870 | |
| 871 | for_each_dpa_resource(ndd, res) |
| 872 | if (strcmp(res->name, label_id->id) == 0) |
| 873 | allocated += resource_size(res); |
| 874 | |
| 875 | return allocated; |
| 876 | } |
| 877 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 878 | static int count_dimms(struct device *dev, void *c) |
| 879 | { |
| 880 | int *count = c; |
| 881 | |
| 882 | if (is_nvdimm(dev)) |
| 883 | (*count)++; |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count) |
| 888 | { |
| 889 | int count = 0; |
| 890 | /* Flush any possible dimm registration failures */ |
| 891 | nd_synchronize(); |
| 892 | |
| 893 | device_for_each_child(&nvdimm_bus->dev, &count, count_dimms); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 894 | dev_dbg(&nvdimm_bus->dev, "count: %d\n", count); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 895 | if (count != dimm_count) |
| 896 | return -ENXIO; |
| 897 | return 0; |
| 898 | } |
| 899 | EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count); |
Dan Williams | b354aba | 2016-05-17 20:24:16 -0700 | [diff] [blame] | 900 | |
| 901 | void __exit nvdimm_devs_exit(void) |
| 902 | { |
| 903 | ida_destroy(&dimm_ida); |
| 904 | } |