Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/device.h> |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 7 | #include <linux/sort.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 8 | #include <linux/slab.h> |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 9 | #include <linux/list.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 10 | #include <linux/nd.h> |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 11 | #include "nd-core.h" |
Dan Williams | ca6a465 | 2017-01-13 20:36:58 -0800 | [diff] [blame] | 12 | #include "pmem.h" |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 13 | #include "nd.h" |
| 14 | |
| 15 | static void namespace_io_release(struct device *dev) |
| 16 | { |
| 17 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); |
| 18 | |
| 19 | kfree(nsio); |
| 20 | } |
| 21 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 22 | static void namespace_pmem_release(struct device *dev) |
| 23 | { |
| 24 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 25 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 26 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 27 | if (nspm->id >= 0) |
| 28 | ida_simple_remove(&nd_region->ns_ida, nspm->id); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 29 | kfree(nspm->alt_name); |
| 30 | kfree(nspm->uuid); |
| 31 | kfree(nspm); |
| 32 | } |
| 33 | |
| 34 | static void namespace_blk_release(struct device *dev) |
| 35 | { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 36 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 37 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 38 | |
| 39 | if (nsblk->id >= 0) |
| 40 | ida_simple_remove(&nd_region->ns_ida, nsblk->id); |
| 41 | kfree(nsblk->alt_name); |
| 42 | kfree(nsblk->uuid); |
| 43 | kfree(nsblk->res); |
| 44 | kfree(nsblk); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Dan Williams | 78c81cc | 2019-11-06 19:56:41 -0800 | [diff] [blame^] | 47 | static bool is_namespace_pmem(const struct device *dev); |
| 48 | static bool is_namespace_blk(const struct device *dev); |
| 49 | static bool is_namespace_io(const struct device *dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 50 | |
Dan Williams | e07ecd7 | 2016-01-05 18:37:23 -0800 | [diff] [blame] | 51 | static int is_uuid_busy(struct device *dev, void *data) |
| 52 | { |
| 53 | u8 *uuid1 = data, *uuid2 = NULL; |
| 54 | |
| 55 | if (is_namespace_pmem(dev)) { |
| 56 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 57 | |
| 58 | uuid2 = nspm->uuid; |
| 59 | } else if (is_namespace_blk(dev)) { |
| 60 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 61 | |
| 62 | uuid2 = nsblk->uuid; |
| 63 | } else if (is_nd_btt(dev)) { |
| 64 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 65 | |
| 66 | uuid2 = nd_btt->uuid; |
| 67 | } else if (is_nd_pfn(dev)) { |
| 68 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 69 | |
| 70 | uuid2 = nd_pfn->uuid; |
| 71 | } |
| 72 | |
| 73 | if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0) |
| 74 | return -EBUSY; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int is_namespace_uuid_busy(struct device *dev, void *data) |
| 80 | { |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 81 | if (is_nd_region(dev)) |
Dan Williams | e07ecd7 | 2016-01-05 18:37:23 -0800 | [diff] [blame] | 82 | return device_for_each_child(dev, data, is_uuid_busy); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * nd_is_uuid_unique - verify that no other namespace has @uuid |
| 88 | * @dev: any device on a nvdimm_bus |
| 89 | * @uuid: uuid to check |
| 90 | */ |
| 91 | bool nd_is_uuid_unique(struct device *dev, u8 *uuid) |
| 92 | { |
| 93 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 94 | |
| 95 | if (!nvdimm_bus) |
| 96 | return false; |
| 97 | WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev)); |
| 98 | if (device_for_each_child(&nvdimm_bus->dev, uuid, |
| 99 | is_namespace_uuid_busy) != 0) |
| 100 | return false; |
| 101 | return true; |
| 102 | } |
| 103 | |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 104 | bool pmem_should_map_pages(struct device *dev) |
| 105 | { |
| 106 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | fa7d2e6 | 2019-01-24 17:33:06 -0800 | [diff] [blame] | 107 | struct nd_namespace_common *ndns = to_ndns(dev); |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 108 | struct nd_namespace_io *nsio; |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 109 | |
| 110 | if (!IS_ENABLED(CONFIG_ZONE_DEVICE)) |
| 111 | return false; |
| 112 | |
| 113 | if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags)) |
| 114 | return false; |
| 115 | |
| 116 | if (is_nd_pfn(dev) || is_nd_btt(dev)) |
| 117 | return false; |
| 118 | |
Dan Williams | fa7d2e6 | 2019-01-24 17:33:06 -0800 | [diff] [blame] | 119 | if (ndns->force_raw) |
| 120 | return false; |
| 121 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 122 | nsio = to_nd_namespace_io(dev); |
| 123 | if (region_intersects(nsio->res.start, resource_size(&nsio->res), |
| 124 | IORESOURCE_SYSTEM_RAM, |
| 125 | IORES_DESC_NONE) == REGION_MIXED) |
| 126 | return false; |
| 127 | |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 128 | return ARCH_MEMREMAP_PMEM == MEMREMAP_WB; |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 129 | } |
| 130 | EXPORT_SYMBOL(pmem_should_map_pages); |
| 131 | |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 132 | unsigned int pmem_sector_size(struct nd_namespace_common *ndns) |
| 133 | { |
| 134 | if (is_namespace_pmem(&ndns->dev)) { |
| 135 | struct nd_namespace_pmem *nspm; |
| 136 | |
| 137 | nspm = to_nd_namespace_pmem(&ndns->dev); |
| 138 | if (nspm->lbasize == 0 || nspm->lbasize == 512) |
| 139 | /* default */; |
| 140 | else if (nspm->lbasize == 4096) |
| 141 | return 4096; |
| 142 | else |
| 143 | dev_WARN(&ndns->dev, "unsupported sector size: %ld\n", |
| 144 | nspm->lbasize); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * There is no namespace label (is_namespace_io()), or the label |
| 149 | * indicates the default sector size. |
| 150 | */ |
| 151 | return 512; |
| 152 | } |
| 153 | EXPORT_SYMBOL(pmem_sector_size); |
| 154 | |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 155 | const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, |
| 156 | char *name) |
| 157 | { |
| 158 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 159 | const char *suffix = NULL; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 160 | |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 161 | if (ndns->claim && is_nd_btt(ndns->claim)) |
| 162 | suffix = "s"; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 163 | |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 164 | if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) { |
Dan Williams | 0122073 | 2016-10-05 09:09:44 -0700 | [diff] [blame] | 165 | int nsidx = 0; |
| 166 | |
| 167 | if (is_namespace_pmem(&ndns->dev)) { |
| 168 | struct nd_namespace_pmem *nspm; |
| 169 | |
| 170 | nspm = to_nd_namespace_pmem(&ndns->dev); |
| 171 | nsidx = nspm->id; |
| 172 | } |
| 173 | |
| 174 | if (nsidx) |
| 175 | sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx, |
| 176 | suffix ? suffix : ""); |
| 177 | else |
| 178 | sprintf(name, "pmem%d%s", nd_region->id, |
| 179 | suffix ? suffix : ""); |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 180 | } else if (is_namespace_blk(&ndns->dev)) { |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 181 | struct nd_namespace_blk *nsblk; |
| 182 | |
| 183 | nsblk = to_nd_namespace_blk(&ndns->dev); |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 184 | sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, |
| 185 | suffix ? suffix : ""); |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 186 | } else { |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | return name; |
| 191 | } |
| 192 | EXPORT_SYMBOL(nvdimm_namespace_disk_name); |
| 193 | |
Vishal Verma | 6ec6895 | 2015-07-29 14:58:09 -0600 | [diff] [blame] | 194 | const u8 *nd_dev_to_uuid(struct device *dev) |
| 195 | { |
| 196 | static const u8 null_uuid[16]; |
| 197 | |
| 198 | if (!dev) |
| 199 | return null_uuid; |
| 200 | |
| 201 | if (is_namespace_pmem(dev)) { |
| 202 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 203 | |
| 204 | return nspm->uuid; |
| 205 | } else if (is_namespace_blk(dev)) { |
| 206 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 207 | |
| 208 | return nsblk->uuid; |
| 209 | } else |
| 210 | return null_uuid; |
| 211 | } |
| 212 | EXPORT_SYMBOL(nd_dev_to_uuid); |
| 213 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 214 | static ssize_t nstype_show(struct device *dev, |
| 215 | struct device_attribute *attr, char *buf) |
| 216 | { |
| 217 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 218 | |
| 219 | return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); |
| 220 | } |
| 221 | static DEVICE_ATTR_RO(nstype); |
| 222 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 223 | static ssize_t __alt_name_store(struct device *dev, const char *buf, |
| 224 | const size_t len) |
| 225 | { |
| 226 | char *input, *pos, *alt_name, **ns_altname; |
| 227 | ssize_t rc; |
| 228 | |
| 229 | if (is_namespace_pmem(dev)) { |
| 230 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 231 | |
| 232 | ns_altname = &nspm->alt_name; |
| 233 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 234 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 235 | |
| 236 | ns_altname = &nsblk->alt_name; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 237 | } else |
| 238 | return -ENXIO; |
| 239 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 240 | if (dev->driver || to_ndns(dev)->claim) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 241 | return -EBUSY; |
| 242 | |
Andy Shevchenko | 3d9cbe3 | 2018-06-11 16:47:21 +0300 | [diff] [blame] | 243 | input = kstrndup(buf, len, GFP_KERNEL); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 244 | if (!input) |
| 245 | return -ENOMEM; |
| 246 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 247 | pos = strim(input); |
| 248 | if (strlen(pos) + 1 > NSLABEL_NAME_LEN) { |
| 249 | rc = -EINVAL; |
| 250 | goto out; |
| 251 | } |
| 252 | |
| 253 | alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL); |
| 254 | if (!alt_name) { |
| 255 | rc = -ENOMEM; |
| 256 | goto out; |
| 257 | } |
| 258 | kfree(*ns_altname); |
| 259 | *ns_altname = alt_name; |
| 260 | sprintf(*ns_altname, "%s", pos); |
| 261 | rc = len; |
| 262 | |
| 263 | out: |
| 264 | kfree(input); |
| 265 | return rc; |
| 266 | } |
| 267 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 268 | static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk) |
| 269 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 270 | struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 271 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 272 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 273 | struct nd_label_id label_id; |
| 274 | resource_size_t size = 0; |
| 275 | struct resource *res; |
| 276 | |
| 277 | if (!nsblk->uuid) |
| 278 | return 0; |
| 279 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); |
| 280 | for_each_dpa_resource(ndd, res) |
| 281 | if (strcmp(res->name, label_id.id) == 0) |
| 282 | size += resource_size(res); |
| 283 | return size; |
| 284 | } |
| 285 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 286 | static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) |
| 287 | { |
| 288 | struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); |
| 289 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 290 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 291 | struct nd_label_id label_id; |
| 292 | struct resource *res; |
| 293 | int count, i; |
| 294 | |
| 295 | if (!nsblk->uuid || !nsblk->lbasize || !ndd) |
| 296 | return false; |
| 297 | |
| 298 | count = 0; |
| 299 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); |
| 300 | for_each_dpa_resource(ndd, res) { |
| 301 | if (strcmp(res->name, label_id.id) != 0) |
| 302 | continue; |
| 303 | /* |
Geert Uytterhoeven | ae551e9 | 2016-08-31 11:45:25 +0200 | [diff] [blame] | 304 | * Resources with unacknowledged adjustments indicate a |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 305 | * failure to update labels |
| 306 | */ |
| 307 | if (res->flags & DPA_RESOURCE_ADJUSTED) |
| 308 | return false; |
| 309 | count++; |
| 310 | } |
| 311 | |
| 312 | /* These values match after a successful label update */ |
| 313 | if (count != nsblk->num_resources) |
| 314 | return false; |
| 315 | |
| 316 | for (i = 0; i < nsblk->num_resources; i++) { |
| 317 | struct resource *found = NULL; |
| 318 | |
| 319 | for_each_dpa_resource(ndd, res) |
| 320 | if (res == nsblk->res[i]) { |
| 321 | found = res; |
| 322 | break; |
| 323 | } |
| 324 | /* stale resource */ |
| 325 | if (!found) |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) |
| 333 | { |
| 334 | resource_size_t size; |
| 335 | |
| 336 | nvdimm_bus_lock(&nsblk->common.dev); |
| 337 | size = __nd_namespace_blk_validate(nsblk); |
| 338 | nvdimm_bus_unlock(&nsblk->common.dev); |
| 339 | |
| 340 | return size; |
| 341 | } |
| 342 | EXPORT_SYMBOL(nd_namespace_blk_validate); |
| 343 | |
| 344 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 345 | static int nd_namespace_label_update(struct nd_region *nd_region, |
| 346 | struct device *dev) |
| 347 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 348 | dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim, |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 349 | "namespace must be idle during label update\n"); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 350 | if (dev->driver || to_ndns(dev)->claim) |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 351 | return 0; |
| 352 | |
| 353 | /* |
| 354 | * Only allow label writes that will result in a valid namespace |
| 355 | * or deletion of an existing namespace. |
| 356 | */ |
| 357 | if (is_namespace_pmem(dev)) { |
| 358 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 359 | resource_size_t size = resource_size(&nspm->nsio.res); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 360 | |
| 361 | if (size == 0 && nspm->uuid) |
| 362 | /* delete allocation */; |
| 363 | else if (!nspm->uuid) |
| 364 | return 0; |
| 365 | |
| 366 | return nd_pmem_namespace_label_update(nd_region, nspm, size); |
| 367 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 368 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 369 | resource_size_t size = nd_namespace_blk_size(nsblk); |
| 370 | |
| 371 | if (size == 0 && nsblk->uuid) |
| 372 | /* delete allocation */; |
| 373 | else if (!nsblk->uuid || !nsblk->lbasize) |
| 374 | return 0; |
| 375 | |
| 376 | return nd_blk_namespace_label_update(nd_region, nsblk, size); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 377 | } else |
| 378 | return -ENXIO; |
| 379 | } |
| 380 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 381 | static ssize_t alt_name_store(struct device *dev, |
| 382 | struct device_attribute *attr, const char *buf, size_t len) |
| 383 | { |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 384 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 385 | ssize_t rc; |
| 386 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 387 | nd_device_lock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 388 | nvdimm_bus_lock(dev); |
| 389 | wait_nvdimm_bus_probe_idle(dev); |
| 390 | rc = __alt_name_store(dev, buf, len); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 391 | if (rc >= 0) |
| 392 | rc = nd_namespace_label_update(nd_region, dev); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 393 | dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 394 | nvdimm_bus_unlock(dev); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 395 | nd_device_unlock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 396 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 397 | return rc < 0 ? rc : len; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static ssize_t alt_name_show(struct device *dev, |
| 401 | struct device_attribute *attr, char *buf) |
| 402 | { |
| 403 | char *ns_altname; |
| 404 | |
| 405 | if (is_namespace_pmem(dev)) { |
| 406 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 407 | |
| 408 | ns_altname = nspm->alt_name; |
| 409 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 410 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 411 | |
| 412 | ns_altname = nsblk->alt_name; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 413 | } else |
| 414 | return -ENXIO; |
| 415 | |
| 416 | return sprintf(buf, "%s\n", ns_altname ? ns_altname : ""); |
| 417 | } |
| 418 | static DEVICE_ATTR_RW(alt_name); |
| 419 | |
| 420 | static int scan_free(struct nd_region *nd_region, |
| 421 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, |
| 422 | resource_size_t n) |
| 423 | { |
| 424 | bool is_blk = strncmp(label_id->id, "blk", 3) == 0; |
| 425 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 426 | int rc = 0; |
| 427 | |
| 428 | while (n) { |
| 429 | struct resource *res, *last; |
| 430 | resource_size_t new_start; |
| 431 | |
| 432 | last = NULL; |
| 433 | for_each_dpa_resource(ndd, res) |
| 434 | if (strcmp(res->name, label_id->id) == 0) |
| 435 | last = res; |
| 436 | res = last; |
| 437 | if (!res) |
| 438 | return 0; |
| 439 | |
| 440 | if (n >= resource_size(res)) { |
| 441 | n -= resource_size(res); |
| 442 | nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc); |
| 443 | nvdimm_free_dpa(ndd, res); |
| 444 | /* retry with last resource deleted */ |
| 445 | continue; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Keep BLK allocations relegated to high DPA as much as |
| 450 | * possible |
| 451 | */ |
| 452 | if (is_blk) |
| 453 | new_start = res->start + n; |
| 454 | else |
| 455 | new_start = res->start; |
| 456 | |
| 457 | rc = adjust_resource(res, new_start, resource_size(res) - n); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 458 | if (rc == 0) |
| 459 | res->flags |= DPA_RESOURCE_ADJUSTED; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 460 | nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc); |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | return rc; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * shrink_dpa_allocation - for each dimm in region free n bytes for label_id |
| 469 | * @nd_region: the set of dimms to reclaim @n bytes from |
| 470 | * @label_id: unique identifier for the namespace consuming this dpa range |
| 471 | * @n: number of bytes per-dimm to release |
| 472 | * |
| 473 | * Assumes resources are ordered. Starting from the end try to |
| 474 | * adjust_resource() the allocation to @n, but if @n is larger than the |
| 475 | * allocation delete it and find the 'new' last allocation in the label |
| 476 | * set. |
| 477 | */ |
| 478 | static int shrink_dpa_allocation(struct nd_region *nd_region, |
| 479 | struct nd_label_id *label_id, resource_size_t n) |
| 480 | { |
| 481 | int i; |
| 482 | |
| 483 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 484 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 485 | int rc; |
| 486 | |
| 487 | rc = scan_free(nd_region, nd_mapping, label_id, n); |
| 488 | if (rc) |
| 489 | return rc; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static resource_size_t init_dpa_allocation(struct nd_label_id *label_id, |
| 496 | struct nd_region *nd_region, struct nd_mapping *nd_mapping, |
| 497 | resource_size_t n) |
| 498 | { |
| 499 | bool is_blk = strncmp(label_id->id, "blk", 3) == 0; |
| 500 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 501 | resource_size_t first_dpa; |
| 502 | struct resource *res; |
| 503 | int rc = 0; |
| 504 | |
| 505 | /* allocate blk from highest dpa first */ |
| 506 | if (is_blk) |
| 507 | first_dpa = nd_mapping->start + nd_mapping->size - n; |
| 508 | else |
| 509 | first_dpa = nd_mapping->start; |
| 510 | |
| 511 | /* first resource allocation for this label-id or dimm */ |
| 512 | res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n); |
| 513 | if (!res) |
| 514 | rc = -EBUSY; |
| 515 | |
| 516 | nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc); |
| 517 | return rc ? n : 0; |
| 518 | } |
| 519 | |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 520 | |
| 521 | /** |
| 522 | * space_valid() - validate free dpa space against constraints |
| 523 | * @nd_region: hosting region of the free space |
| 524 | * @ndd: dimm device data for debug |
| 525 | * @label_id: namespace id to allocate space |
| 526 | * @prev: potential allocation that precedes free space |
| 527 | * @next: allocation that follows the given free space range |
| 528 | * @exist: first allocation with same id in the mapping |
| 529 | * @n: range that must satisfied for pmem allocations |
| 530 | * @valid: free space range to validate |
| 531 | * |
| 532 | * BLK-space is valid as long as it does not precede a PMEM |
| 533 | * allocation in a given region. PMEM-space must be contiguous |
| 534 | * and adjacent to an existing existing allocation (if one |
| 535 | * exists). If reserving PMEM any space is valid. |
| 536 | */ |
| 537 | static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd, |
| 538 | struct nd_label_id *label_id, struct resource *prev, |
| 539 | struct resource *next, struct resource *exist, |
| 540 | resource_size_t n, struct resource *valid) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 541 | { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 542 | bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0; |
| 543 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; |
| 544 | |
| 545 | if (valid->start >= valid->end) |
| 546 | goto invalid; |
| 547 | |
| 548 | if (is_reserve) |
| 549 | return; |
| 550 | |
| 551 | if (!is_pmem) { |
| 552 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 553 | struct nvdimm_bus *nvdimm_bus; |
| 554 | struct blk_alloc_info info = { |
| 555 | .nd_mapping = nd_mapping, |
| 556 | .available = nd_mapping->size, |
| 557 | .res = valid, |
| 558 | }; |
| 559 | |
| 560 | WARN_ON(!is_nd_blk(&nd_region->dev)); |
| 561 | nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
| 562 | device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | /* allocation needs to be contiguous, so this is all or nothing */ |
| 567 | if (resource_size(valid) < n) |
| 568 | goto invalid; |
| 569 | |
| 570 | /* we've got all the space we need and no existing allocation */ |
| 571 | if (!exist) |
| 572 | return; |
| 573 | |
| 574 | /* allocation needs to be contiguous with the existing namespace */ |
| 575 | if (valid->start == exist->end + 1 |
| 576 | || valid->end == exist->start - 1) |
| 577 | return; |
| 578 | |
| 579 | invalid: |
| 580 | /* truncate @valid size to 0 */ |
| 581 | valid->end = valid->start - 1; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | enum alloc_loc { |
| 585 | ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER, |
| 586 | }; |
| 587 | |
| 588 | static resource_size_t scan_allocate(struct nd_region *nd_region, |
| 589 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, |
| 590 | resource_size_t n) |
| 591 | { |
| 592 | resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1; |
| 593 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; |
| 594 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 595 | struct resource *res, *exist = NULL, valid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 596 | const resource_size_t to_allocate = n; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 597 | int first; |
| 598 | |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 599 | for_each_dpa_resource(ndd, res) |
| 600 | if (strcmp(label_id->id, res->name) == 0) |
| 601 | exist = res; |
| 602 | |
| 603 | valid.start = nd_mapping->start; |
| 604 | valid.end = mapping_end; |
| 605 | valid.name = "free space"; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 606 | retry: |
| 607 | first = 0; |
| 608 | for_each_dpa_resource(ndd, res) { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 609 | struct resource *next = res->sibling, *new_res = NULL; |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 610 | resource_size_t allocate, available = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 611 | enum alloc_loc loc = ALLOC_ERR; |
| 612 | const char *action; |
| 613 | int rc = 0; |
| 614 | |
| 615 | /* ignore resources outside this nd_mapping */ |
| 616 | if (res->start > mapping_end) |
| 617 | continue; |
| 618 | if (res->end < nd_mapping->start) |
| 619 | continue; |
| 620 | |
| 621 | /* space at the beginning of the mapping */ |
| 622 | if (!first++ && res->start > nd_mapping->start) { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 623 | valid.start = nd_mapping->start; |
| 624 | valid.end = res->start - 1; |
| 625 | space_valid(nd_region, ndd, label_id, NULL, next, exist, |
| 626 | to_allocate, &valid); |
| 627 | available = resource_size(&valid); |
| 628 | if (available) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 629 | loc = ALLOC_BEFORE; |
| 630 | } |
| 631 | |
| 632 | /* space between allocations */ |
| 633 | if (!loc && next) { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 634 | valid.start = res->start + resource_size(res); |
| 635 | valid.end = min(mapping_end, next->start - 1); |
| 636 | space_valid(nd_region, ndd, label_id, res, next, exist, |
| 637 | to_allocate, &valid); |
| 638 | available = resource_size(&valid); |
| 639 | if (available) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 640 | loc = ALLOC_MID; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* space at the end of the mapping */ |
| 644 | if (!loc && !next) { |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 645 | valid.start = res->start + resource_size(res); |
| 646 | valid.end = mapping_end; |
| 647 | space_valid(nd_region, ndd, label_id, res, next, exist, |
| 648 | to_allocate, &valid); |
| 649 | available = resource_size(&valid); |
| 650 | if (available) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 651 | loc = ALLOC_AFTER; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (!loc || !available) |
| 655 | continue; |
| 656 | allocate = min(available, n); |
| 657 | switch (loc) { |
| 658 | case ALLOC_BEFORE: |
| 659 | if (strcmp(res->name, label_id->id) == 0) { |
| 660 | /* adjust current resource up */ |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 661 | rc = adjust_resource(res, res->start - allocate, |
| 662 | resource_size(res) + allocate); |
| 663 | action = "cur grow up"; |
| 664 | } else |
| 665 | action = "allocate"; |
| 666 | break; |
| 667 | case ALLOC_MID: |
| 668 | if (strcmp(next->name, label_id->id) == 0) { |
| 669 | /* adjust next resource up */ |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 670 | rc = adjust_resource(next, next->start |
| 671 | - allocate, resource_size(next) |
| 672 | + allocate); |
| 673 | new_res = next; |
| 674 | action = "next grow up"; |
| 675 | } else if (strcmp(res->name, label_id->id) == 0) { |
| 676 | action = "grow down"; |
| 677 | } else |
| 678 | action = "allocate"; |
| 679 | break; |
| 680 | case ALLOC_AFTER: |
| 681 | if (strcmp(res->name, label_id->id) == 0) |
| 682 | action = "grow down"; |
| 683 | else |
| 684 | action = "allocate"; |
| 685 | break; |
| 686 | default: |
| 687 | return n; |
| 688 | } |
| 689 | |
| 690 | if (strcmp(action, "allocate") == 0) { |
| 691 | /* BLK allocate bottom up */ |
| 692 | if (!is_pmem) |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 693 | valid.start += available - allocate; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 694 | |
| 695 | new_res = nvdimm_allocate_dpa(ndd, label_id, |
Dan Williams | 762d067 | 2016-10-04 16:09:59 -0700 | [diff] [blame] | 696 | valid.start, allocate); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 697 | if (!new_res) |
| 698 | rc = -EBUSY; |
| 699 | } else if (strcmp(action, "grow down") == 0) { |
| 700 | /* adjust current resource down */ |
| 701 | rc = adjust_resource(res, res->start, resource_size(res) |
| 702 | + allocate); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 703 | if (rc == 0) |
| 704 | res->flags |= DPA_RESOURCE_ADJUSTED; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | if (!new_res) |
| 708 | new_res = res; |
| 709 | |
| 710 | nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n", |
| 711 | action, loc, rc); |
| 712 | |
| 713 | if (rc) |
| 714 | return n; |
| 715 | |
| 716 | n -= allocate; |
| 717 | if (n) { |
| 718 | /* |
| 719 | * Retry scan with newly inserted resources. |
| 720 | * For example, if we did an ALLOC_BEFORE |
| 721 | * insertion there may also have been space |
| 722 | * available for an ALLOC_AFTER insertion, so we |
| 723 | * need to check this same resource again |
| 724 | */ |
| 725 | goto retry; |
| 726 | } else |
| 727 | return 0; |
| 728 | } |
| 729 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 730 | /* |
| 731 | * If we allocated nothing in the BLK case it may be because we are in |
| 732 | * an initial "pmem-reserve pass". Only do an initial BLK allocation |
| 733 | * when none of the DPA space is reserved. |
| 734 | */ |
| 735 | if ((is_pmem || !ndd->dpa.child) && n == to_allocate) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 736 | return init_dpa_allocation(label_id, nd_region, nd_mapping, n); |
| 737 | return n; |
| 738 | } |
| 739 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 740 | static int merge_dpa(struct nd_region *nd_region, |
| 741 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id) |
| 742 | { |
| 743 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 744 | struct resource *res; |
| 745 | |
| 746 | if (strncmp("pmem", label_id->id, 4) == 0) |
| 747 | return 0; |
| 748 | retry: |
| 749 | for_each_dpa_resource(ndd, res) { |
| 750 | int rc; |
| 751 | struct resource *next = res->sibling; |
| 752 | resource_size_t end = res->start + resource_size(res); |
| 753 | |
| 754 | if (!next || strcmp(res->name, label_id->id) != 0 |
| 755 | || strcmp(next->name, label_id->id) != 0 |
| 756 | || end != next->start) |
| 757 | continue; |
| 758 | end += resource_size(next); |
| 759 | nvdimm_free_dpa(ndd, next); |
| 760 | rc = adjust_resource(res, res->start, end - res->start); |
| 761 | nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc); |
| 762 | if (rc) |
| 763 | return rc; |
| 764 | res->flags |= DPA_RESOURCE_ADJUSTED; |
| 765 | goto retry; |
| 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
Keith Busch | 12e3129 | 2018-07-24 15:07:57 -0600 | [diff] [blame] | 771 | int __reserve_free_pmem(struct device *dev, void *data) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 772 | { |
| 773 | struct nvdimm *nvdimm = data; |
| 774 | struct nd_region *nd_region; |
| 775 | struct nd_label_id label_id; |
| 776 | int i; |
| 777 | |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 778 | if (!is_memory(dev)) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 779 | return 0; |
| 780 | |
| 781 | nd_region = to_nd_region(dev); |
| 782 | if (nd_region->ndr_mappings == 0) |
| 783 | return 0; |
| 784 | |
| 785 | memset(&label_id, 0, sizeof(label_id)); |
| 786 | strcat(label_id.id, "pmem-reserve"); |
| 787 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 788 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 789 | resource_size_t n, rem = 0; |
| 790 | |
| 791 | if (nd_mapping->nvdimm != nvdimm) |
| 792 | continue; |
| 793 | |
| 794 | n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem); |
| 795 | if (n == 0) |
| 796 | return 0; |
| 797 | rem = scan_allocate(nd_region, nd_mapping, &label_id, n); |
| 798 | dev_WARN_ONCE(&nd_region->dev, rem, |
| 799 | "pmem reserve underrun: %#llx of %#llx bytes\n", |
| 800 | (unsigned long long) n - rem, |
| 801 | (unsigned long long) n); |
| 802 | return rem ? -ENXIO : 0; |
| 803 | } |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
Keith Busch | 12e3129 | 2018-07-24 15:07:57 -0600 | [diff] [blame] | 808 | void release_free_pmem(struct nvdimm_bus *nvdimm_bus, |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 809 | struct nd_mapping *nd_mapping) |
| 810 | { |
| 811 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 812 | struct resource *res, *_res; |
| 813 | |
| 814 | for_each_dpa_resource_safe(ndd, res, _res) |
| 815 | if (strcmp(res->name, "pmem-reserve") == 0) |
| 816 | nvdimm_free_dpa(ndd, res); |
| 817 | } |
| 818 | |
| 819 | static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus, |
| 820 | struct nd_mapping *nd_mapping) |
| 821 | { |
| 822 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 823 | int rc; |
| 824 | |
| 825 | rc = device_for_each_child(&nvdimm_bus->dev, nvdimm, |
| 826 | __reserve_free_pmem); |
| 827 | if (rc) |
| 828 | release_free_pmem(nvdimm_bus, nd_mapping); |
| 829 | return rc; |
| 830 | } |
| 831 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 832 | /** |
| 833 | * grow_dpa_allocation - for each dimm allocate n bytes for @label_id |
| 834 | * @nd_region: the set of dimms to allocate @n more bytes from |
| 835 | * @label_id: unique identifier for the namespace consuming this dpa range |
| 836 | * @n: number of bytes per-dimm to add to the existing allocation |
| 837 | * |
| 838 | * Assumes resources are ordered. For BLK regions, first consume |
| 839 | * BLK-only available DPA free space, then consume PMEM-aliased DPA |
| 840 | * space starting at the highest DPA. For PMEM regions start |
| 841 | * allocations from the start of an interleave set and end at the first |
| 842 | * BLK allocation or the end of the interleave set, whichever comes |
| 843 | * first. |
| 844 | */ |
| 845 | static int grow_dpa_allocation(struct nd_region *nd_region, |
| 846 | struct nd_label_id *label_id, resource_size_t n) |
| 847 | { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 848 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
| 849 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 850 | int i; |
| 851 | |
| 852 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 853 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 854 | resource_size_t rem = n; |
| 855 | int rc, j; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 856 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 857 | /* |
| 858 | * In the BLK case try once with all unallocated PMEM |
| 859 | * reserved, and once without |
| 860 | */ |
| 861 | for (j = is_pmem; j < 2; j++) { |
| 862 | bool blk_only = j == 0; |
| 863 | |
| 864 | if (blk_only) { |
| 865 | rc = reserve_free_pmem(nvdimm_bus, nd_mapping); |
| 866 | if (rc) |
| 867 | return rc; |
| 868 | } |
| 869 | rem = scan_allocate(nd_region, nd_mapping, |
| 870 | label_id, rem); |
| 871 | if (blk_only) |
| 872 | release_free_pmem(nvdimm_bus, nd_mapping); |
| 873 | |
| 874 | /* try again and allow encroachments into PMEM */ |
| 875 | if (rem == 0) |
| 876 | break; |
| 877 | } |
| 878 | |
| 879 | dev_WARN_ONCE(&nd_region->dev, rem, |
| 880 | "allocation underrun: %#llx of %#llx bytes\n", |
| 881 | (unsigned long long) n - rem, |
| 882 | (unsigned long long) n); |
| 883 | if (rem) |
| 884 | return -ENXIO; |
| 885 | |
| 886 | rc = merge_dpa(nd_region, nd_mapping, label_id); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 887 | if (rc) |
| 888 | return rc; |
| 889 | } |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 894 | static void nd_namespace_pmem_set_resource(struct nd_region *nd_region, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 895 | struct nd_namespace_pmem *nspm, resource_size_t size) |
| 896 | { |
| 897 | struct resource *res = &nspm->nsio.res; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 898 | resource_size_t offset = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 899 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 900 | if (size && !nspm->uuid) { |
| 901 | WARN_ON_ONCE(1); |
| 902 | size = 0; |
| 903 | } |
| 904 | |
| 905 | if (size && nspm->uuid) { |
| 906 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 907 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 908 | struct nd_label_id label_id; |
| 909 | struct resource *res; |
| 910 | |
| 911 | if (!ndd) { |
| 912 | size = 0; |
| 913 | goto out; |
| 914 | } |
| 915 | |
| 916 | nd_label_gen_id(&label_id, nspm->uuid, 0); |
| 917 | |
| 918 | /* calculate a spa offset from the dpa allocation offset */ |
| 919 | for_each_dpa_resource(ndd, res) |
| 920 | if (strcmp(res->name, label_id.id) == 0) { |
| 921 | offset = (res->start - nd_mapping->start) |
| 922 | * nd_region->ndr_mappings; |
| 923 | goto out; |
| 924 | } |
| 925 | |
| 926 | WARN_ON_ONCE(1); |
| 927 | size = 0; |
| 928 | } |
| 929 | |
| 930 | out: |
| 931 | res->start = nd_region->ndr_start + offset; |
| 932 | res->end = res->start + size - 1; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 933 | } |
| 934 | |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 935 | static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where) |
| 936 | { |
| 937 | if (!uuid) { |
| 938 | dev_dbg(dev, "%s: uuid not set\n", where); |
| 939 | return true; |
| 940 | } |
| 941 | return false; |
| 942 | } |
| 943 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 944 | static ssize_t __size_store(struct device *dev, unsigned long long val) |
| 945 | { |
| 946 | resource_size_t allocated = 0, available = 0; |
| 947 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 948 | struct nd_namespace_common *ndns = to_ndns(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 949 | struct nd_mapping *nd_mapping; |
| 950 | struct nvdimm_drvdata *ndd; |
| 951 | struct nd_label_id label_id; |
| 952 | u32 flags = 0, remainder; |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 953 | int rc, i, id = -1; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 954 | u8 *uuid = NULL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 955 | |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 956 | if (dev->driver || ndns->claim) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 957 | return -EBUSY; |
| 958 | |
| 959 | if (is_namespace_pmem(dev)) { |
| 960 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 961 | |
| 962 | uuid = nspm->uuid; |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 963 | id = nspm->id; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 964 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 965 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 966 | |
| 967 | uuid = nsblk->uuid; |
| 968 | flags = NSLABEL_FLAG_LOCAL; |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 969 | id = nsblk->id; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* |
| 973 | * We need a uuid for the allocation-label and dimm(s) on which |
| 974 | * to store the label. |
| 975 | */ |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 976 | if (uuid_not_set(uuid, dev, __func__)) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 977 | return -ENXIO; |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 978 | if (nd_region->ndr_mappings == 0) { |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 979 | dev_dbg(dev, "not associated with dimm(s)\n"); |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 980 | return -ENXIO; |
| 981 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 982 | |
Aneesh Kumar K.V | 5b26db9 | 2019-09-05 21:16:02 +0530 | [diff] [blame] | 983 | div_u64_rem(val, PAGE_SIZE * nd_region->ndr_mappings, &remainder); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 984 | if (remainder) { |
Aneesh Kumar K.V | 5b26db9 | 2019-09-05 21:16:02 +0530 | [diff] [blame] | 985 | dev_dbg(dev, "%llu is not %ldK aligned\n", val, |
| 986 | (PAGE_SIZE * nd_region->ndr_mappings) / SZ_1K); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 987 | return -EINVAL; |
| 988 | } |
| 989 | |
| 990 | nd_label_gen_id(&label_id, uuid, flags); |
| 991 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 992 | nd_mapping = &nd_region->mapping[i]; |
| 993 | ndd = to_ndd(nd_mapping); |
| 994 | |
| 995 | /* |
| 996 | * All dimms in an interleave set, or the base dimm for a blk |
| 997 | * region, need to be enabled for the size to be changed. |
| 998 | */ |
| 999 | if (!ndd) |
| 1000 | return -ENXIO; |
| 1001 | |
| 1002 | allocated += nvdimm_allocated_dpa(ndd, &label_id); |
| 1003 | } |
Keith Busch | 12e3129 | 2018-07-24 15:07:57 -0600 | [diff] [blame] | 1004 | available = nd_region_allocatable_dpa(nd_region); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1005 | |
| 1006 | if (val > available + allocated) |
| 1007 | return -ENOSPC; |
| 1008 | |
| 1009 | if (val == allocated) |
| 1010 | return 0; |
| 1011 | |
| 1012 | val = div_u64(val, nd_region->ndr_mappings); |
| 1013 | allocated = div_u64(allocated, nd_region->ndr_mappings); |
| 1014 | if (val < allocated) |
| 1015 | rc = shrink_dpa_allocation(nd_region, &label_id, |
| 1016 | allocated - val); |
| 1017 | else |
| 1018 | rc = grow_dpa_allocation(nd_region, &label_id, val - allocated); |
| 1019 | |
| 1020 | if (rc) |
| 1021 | return rc; |
| 1022 | |
| 1023 | if (is_namespace_pmem(dev)) { |
| 1024 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1025 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1026 | nd_namespace_pmem_set_resource(nd_region, nspm, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1027 | val * nd_region->ndr_mappings); |
| 1028 | } |
| 1029 | |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 1030 | /* |
| 1031 | * Try to delete the namespace if we deleted all of its |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 1032 | * allocation, this is not the seed or 0th device for the |
| 1033 | * region, and it is not actively claimed by a btt, pfn, or dax |
| 1034 | * instance. |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 1035 | */ |
Dan Williams | 9d032f4 | 2017-01-25 00:54:07 +0530 | [diff] [blame] | 1036 | if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim) |
Dan Williams | 1f19b98 | 2017-01-09 17:30:49 -0800 | [diff] [blame] | 1037 | nd_device_unregister(dev, ND_ASYNC); |
| 1038 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1039 | return rc; |
| 1040 | } |
| 1041 | |
| 1042 | static ssize_t size_store(struct device *dev, |
| 1043 | struct device_attribute *attr, const char *buf, size_t len) |
| 1044 | { |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1045 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1046 | unsigned long long val; |
| 1047 | u8 **uuid = NULL; |
| 1048 | int rc; |
| 1049 | |
| 1050 | rc = kstrtoull(buf, 0, &val); |
| 1051 | if (rc) |
| 1052 | return rc; |
| 1053 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1054 | nd_device_lock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1055 | nvdimm_bus_lock(dev); |
| 1056 | wait_nvdimm_bus_probe_idle(dev); |
| 1057 | rc = __size_store(dev, val); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1058 | if (rc >= 0) |
| 1059 | rc = nd_namespace_label_update(nd_region, dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1060 | |
| 1061 | if (is_namespace_pmem(dev)) { |
| 1062 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1063 | |
| 1064 | uuid = &nspm->uuid; |
| 1065 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1066 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1067 | |
| 1068 | uuid = &nsblk->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | if (rc == 0 && val == 0 && uuid) { |
| 1072 | /* setting size zero == 'delete namespace' */ |
| 1073 | kfree(*uuid); |
| 1074 | *uuid = NULL; |
| 1075 | } |
| 1076 | |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 1077 | dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1078 | |
| 1079 | nvdimm_bus_unlock(dev); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1080 | nd_device_unlock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1081 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1082 | return rc < 0 ? rc : len; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1085 | resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1086 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1087 | struct device *dev = &ndns->dev; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1088 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1089 | if (is_namespace_pmem(dev)) { |
| 1090 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1091 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1092 | return resource_size(&nspm->nsio.res); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1093 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1094 | return nd_namespace_blk_size(to_nd_namespace_blk(dev)); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1095 | } else if (is_namespace_io(dev)) { |
| 1096 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); |
| 1097 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1098 | return resource_size(&nsio->res); |
| 1099 | } else |
| 1100 | WARN_ONCE(1, "unknown namespace type\n"); |
| 1101 | return 0; |
| 1102 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1103 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1104 | resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) |
| 1105 | { |
| 1106 | resource_size_t size; |
| 1107 | |
| 1108 | nvdimm_bus_lock(&ndns->dev); |
| 1109 | size = __nvdimm_namespace_capacity(ndns); |
| 1110 | nvdimm_bus_unlock(&ndns->dev); |
| 1111 | |
| 1112 | return size; |
| 1113 | } |
| 1114 | EXPORT_SYMBOL(nvdimm_namespace_capacity); |
| 1115 | |
Dan Williams | 08e6b3c | 2018-06-13 09:08:36 -0700 | [diff] [blame] | 1116 | bool nvdimm_namespace_locked(struct nd_namespace_common *ndns) |
| 1117 | { |
| 1118 | int i; |
| 1119 | bool locked = false; |
| 1120 | struct device *dev = &ndns->dev; |
| 1121 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 1122 | |
| 1123 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1124 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1125 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 1126 | |
| 1127 | if (test_bit(NDD_LOCKED, &nvdimm->flags)) { |
| 1128 | dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm)); |
| 1129 | locked = true; |
| 1130 | } |
| 1131 | } |
| 1132 | return locked; |
| 1133 | } |
| 1134 | EXPORT_SYMBOL(nvdimm_namespace_locked); |
| 1135 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1136 | static ssize_t size_show(struct device *dev, |
| 1137 | struct device_attribute *attr, char *buf) |
| 1138 | { |
| 1139 | return sprintf(buf, "%llu\n", (unsigned long long) |
| 1140 | nvdimm_namespace_capacity(to_ndns(dev))); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1141 | } |
Fabian Frederick | b44fe76 | 2016-12-04 10:54:08 -0800 | [diff] [blame] | 1142 | static DEVICE_ATTR(size, 0444, size_show, size_store); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1143 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1144 | static u8 *namespace_to_uuid(struct device *dev) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1145 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1146 | if (is_namespace_pmem(dev)) { |
| 1147 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1148 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1149 | return nspm->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1150 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1151 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1152 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1153 | return nsblk->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1154 | } else |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1155 | return ERR_PTR(-ENXIO); |
| 1156 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1157 | |
Dan Williams | f95b4bc | 2016-09-21 18:16:21 -0700 | [diff] [blame] | 1158 | static ssize_t uuid_show(struct device *dev, |
| 1159 | struct device_attribute *attr, char *buf) |
| 1160 | { |
| 1161 | u8 *uuid = namespace_to_uuid(dev); |
| 1162 | |
| 1163 | if (IS_ERR(uuid)) |
| 1164 | return PTR_ERR(uuid); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1165 | if (uuid) |
| 1166 | return sprintf(buf, "%pUb\n", uuid); |
| 1167 | return sprintf(buf, "\n"); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * namespace_update_uuid - check for a unique uuid and whether we're "renaming" |
| 1172 | * @nd_region: parent region so we can updates all dimms in the set |
| 1173 | * @dev: namespace type for generating label_id |
| 1174 | * @new_uuid: incoming uuid |
| 1175 | * @old_uuid: reference to the uuid storage location in the namespace object |
| 1176 | */ |
| 1177 | static int namespace_update_uuid(struct nd_region *nd_region, |
| 1178 | struct device *dev, u8 *new_uuid, u8 **old_uuid) |
| 1179 | { |
| 1180 | u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0; |
| 1181 | struct nd_label_id old_label_id; |
| 1182 | struct nd_label_id new_label_id; |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1183 | int i; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1184 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1185 | if (!nd_is_uuid_unique(dev, new_uuid)) |
| 1186 | return -EINVAL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1187 | |
| 1188 | if (*old_uuid == NULL) |
| 1189 | goto out; |
| 1190 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1191 | /* |
| 1192 | * If we've already written a label with this uuid, then it's |
| 1193 | * too late to rename because we can't reliably update the uuid |
| 1194 | * without losing the old namespace. Userspace must delete this |
| 1195 | * namespace to abandon the old uuid. |
| 1196 | */ |
| 1197 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1198 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1199 | |
| 1200 | /* |
| 1201 | * This check by itself is sufficient because old_uuid |
| 1202 | * would be NULL above if this uuid did not exist in the |
| 1203 | * currently written set. |
| 1204 | * |
| 1205 | * FIXME: can we delete uuid with zero dpa allocated? |
| 1206 | */ |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1207 | if (list_empty(&nd_mapping->labels)) |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1208 | return -EBUSY; |
| 1209 | } |
| 1210 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1211 | nd_label_gen_id(&old_label_id, *old_uuid, flags); |
| 1212 | nd_label_gen_id(&new_label_id, new_uuid, flags); |
| 1213 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1214 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1215 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | c4703ce | 2019-04-30 21:51:21 -0700 | [diff] [blame] | 1216 | struct nd_label_ent *label_ent; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1217 | struct resource *res; |
| 1218 | |
| 1219 | for_each_dpa_resource(ndd, res) |
| 1220 | if (strcmp(res->name, old_label_id.id) == 0) |
| 1221 | sprintf((void *) res->name, "%s", |
| 1222 | new_label_id.id); |
Dan Williams | c4703ce | 2019-04-30 21:51:21 -0700 | [diff] [blame] | 1223 | |
| 1224 | mutex_lock(&nd_mapping->lock); |
| 1225 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
| 1226 | struct nd_namespace_label *nd_label = label_ent->label; |
| 1227 | struct nd_label_id label_id; |
| 1228 | |
| 1229 | if (!nd_label) |
| 1230 | continue; |
| 1231 | nd_label_gen_id(&label_id, nd_label->uuid, |
| 1232 | __le32_to_cpu(nd_label->flags)); |
| 1233 | if (strcmp(old_label_id.id, label_id.id) == 0) |
| 1234 | set_bit(ND_LABEL_REAP, &label_ent->flags); |
| 1235 | } |
| 1236 | mutex_unlock(&nd_mapping->lock); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1237 | } |
| 1238 | kfree(*old_uuid); |
| 1239 | out: |
| 1240 | *old_uuid = new_uuid; |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | static ssize_t uuid_store(struct device *dev, |
| 1245 | struct device_attribute *attr, const char *buf, size_t len) |
| 1246 | { |
| 1247 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 1248 | u8 *uuid = NULL; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1249 | ssize_t rc = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1250 | u8 **ns_uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1251 | |
| 1252 | if (is_namespace_pmem(dev)) { |
| 1253 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1254 | |
| 1255 | ns_uuid = &nspm->uuid; |
| 1256 | } else if (is_namespace_blk(dev)) { |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1257 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1258 | |
| 1259 | ns_uuid = &nsblk->uuid; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1260 | } else |
| 1261 | return -ENXIO; |
| 1262 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1263 | nd_device_lock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1264 | nvdimm_bus_lock(dev); |
| 1265 | wait_nvdimm_bus_probe_idle(dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1266 | if (to_ndns(dev)->claim) |
| 1267 | rc = -EBUSY; |
| 1268 | if (rc >= 0) |
| 1269 | rc = nd_uuid_store(dev, &uuid, buf, len); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1270 | if (rc >= 0) |
| 1271 | rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1272 | if (rc >= 0) |
| 1273 | rc = nd_namespace_label_update(nd_region, dev); |
| 1274 | else |
| 1275 | kfree(uuid); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 1276 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 1277 | buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1278 | nvdimm_bus_unlock(dev); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1279 | nd_device_unlock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1280 | |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1281 | return rc < 0 ? rc : len; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1282 | } |
| 1283 | static DEVICE_ATTR_RW(uuid); |
| 1284 | |
| 1285 | static ssize_t resource_show(struct device *dev, |
| 1286 | struct device_attribute *attr, char *buf) |
| 1287 | { |
| 1288 | struct resource *res; |
| 1289 | |
| 1290 | if (is_namespace_pmem(dev)) { |
| 1291 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1292 | |
| 1293 | res = &nspm->nsio.res; |
| 1294 | } else if (is_namespace_io(dev)) { |
| 1295 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); |
| 1296 | |
| 1297 | res = &nsio->res; |
| 1298 | } else |
| 1299 | return -ENXIO; |
| 1300 | |
| 1301 | /* no address to convey if the namespace has no allocation */ |
| 1302 | if (resource_size(res) == 0) |
| 1303 | return -ENXIO; |
| 1304 | return sprintf(buf, "%#llx\n", (unsigned long long) res->start); |
| 1305 | } |
| 1306 | static DEVICE_ATTR_RO(resource); |
| 1307 | |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1308 | static const unsigned long blk_lbasize_supported[] = { 512, 520, 528, |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 1309 | 4096, 4104, 4160, 4224, 0 }; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1310 | |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1311 | static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 }; |
| 1312 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1313 | static ssize_t sector_size_show(struct device *dev, |
| 1314 | struct device_attribute *attr, char *buf) |
| 1315 | { |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1316 | if (is_namespace_blk(dev)) { |
| 1317 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1318 | |
Dan Williams | b2c48f9 | 2017-08-11 17:36:54 -0700 | [diff] [blame] | 1319 | return nd_size_select_show(nsblk->lbasize, |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1320 | blk_lbasize_supported, buf); |
| 1321 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1322 | |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1323 | if (is_namespace_pmem(dev)) { |
| 1324 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1325 | |
Dan Williams | b2c48f9 | 2017-08-11 17:36:54 -0700 | [diff] [blame] | 1326 | return nd_size_select_show(nspm->lbasize, |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1327 | pmem_lbasize_supported, buf); |
| 1328 | } |
| 1329 | return -ENXIO; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static ssize_t sector_size_store(struct device *dev, |
| 1333 | struct device_attribute *attr, const char *buf, size_t len) |
| 1334 | { |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1335 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1336 | const unsigned long *supported; |
| 1337 | unsigned long *lbasize; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1338 | ssize_t rc = 0; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1339 | |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 1340 | if (is_namespace_blk(dev)) { |
| 1341 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1342 | |
| 1343 | lbasize = &nsblk->lbasize; |
| 1344 | supported = blk_lbasize_supported; |
| 1345 | } else if (is_namespace_pmem(dev)) { |
| 1346 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1347 | |
| 1348 | lbasize = &nspm->lbasize; |
| 1349 | supported = pmem_lbasize_supported; |
| 1350 | } else |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1351 | return -ENXIO; |
| 1352 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1353 | nd_device_lock(dev); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1354 | nvdimm_bus_lock(dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1355 | if (to_ndns(dev)->claim) |
| 1356 | rc = -EBUSY; |
| 1357 | if (rc >= 0) |
Dan Williams | b2c48f9 | 2017-08-11 17:36:54 -0700 | [diff] [blame] | 1358 | rc = nd_size_select_store(dev, buf, lbasize, supported); |
Dan Williams | f524bf2 | 2015-05-30 12:36:02 -0400 | [diff] [blame] | 1359 | if (rc >= 0) |
| 1360 | rc = nd_namespace_label_update(nd_region, dev); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 1361 | dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote", |
| 1362 | buf, buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1363 | nvdimm_bus_unlock(dev); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1364 | nd_device_unlock(dev); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1365 | |
| 1366 | return rc ? rc : len; |
| 1367 | } |
| 1368 | static DEVICE_ATTR_RW(sector_size); |
| 1369 | |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 1370 | static ssize_t dpa_extents_show(struct device *dev, |
| 1371 | struct device_attribute *attr, char *buf) |
| 1372 | { |
| 1373 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 1374 | struct nd_label_id label_id; |
| 1375 | int count = 0, i; |
| 1376 | u8 *uuid = NULL; |
| 1377 | u32 flags = 0; |
| 1378 | |
| 1379 | nvdimm_bus_lock(dev); |
| 1380 | if (is_namespace_pmem(dev)) { |
| 1381 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
| 1382 | |
| 1383 | uuid = nspm->uuid; |
| 1384 | flags = 0; |
| 1385 | } else if (is_namespace_blk(dev)) { |
| 1386 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
| 1387 | |
| 1388 | uuid = nsblk->uuid; |
| 1389 | flags = NSLABEL_FLAG_LOCAL; |
| 1390 | } |
| 1391 | |
| 1392 | if (!uuid) |
| 1393 | goto out; |
| 1394 | |
| 1395 | nd_label_gen_id(&label_id, uuid, flags); |
| 1396 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1397 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1398 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 1399 | struct resource *res; |
| 1400 | |
| 1401 | for_each_dpa_resource(ndd, res) |
| 1402 | if (strcmp(res->name, label_id.id) == 0) |
| 1403 | count++; |
| 1404 | } |
| 1405 | out: |
| 1406 | nvdimm_bus_unlock(dev); |
| 1407 | |
| 1408 | return sprintf(buf, "%d\n", count); |
| 1409 | } |
| 1410 | static DEVICE_ATTR_RO(dpa_extents); |
| 1411 | |
Vishal Verma | 14e4945 | 2017-06-28 14:25:00 -0600 | [diff] [blame] | 1412 | static int btt_claim_class(struct device *dev) |
| 1413 | { |
| 1414 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 1415 | int i, loop_bitmask = 0; |
| 1416 | |
| 1417 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1418 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 1419 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 1420 | struct nd_namespace_index *nsindex; |
| 1421 | |
Dan Williams | 33a5608 | 2017-09-18 14:48:58 -0700 | [diff] [blame] | 1422 | /* |
| 1423 | * If any of the DIMMs do not support labels the only |
| 1424 | * possible BTT format is v1. |
| 1425 | */ |
| 1426 | if (!ndd) { |
| 1427 | loop_bitmask = 0; |
| 1428 | break; |
| 1429 | } |
| 1430 | |
Vishal Verma | 14e4945 | 2017-06-28 14:25:00 -0600 | [diff] [blame] | 1431 | nsindex = to_namespace_index(ndd, ndd->ns_current); |
| 1432 | if (nsindex == NULL) |
| 1433 | loop_bitmask |= 1; |
| 1434 | else { |
| 1435 | /* check whether existing labels are v1.1 or v1.2 */ |
| 1436 | if (__le16_to_cpu(nsindex->major) == 1 |
| 1437 | && __le16_to_cpu(nsindex->minor) == 1) |
| 1438 | loop_bitmask |= 2; |
| 1439 | else |
| 1440 | loop_bitmask |= 4; |
| 1441 | } |
| 1442 | } |
| 1443 | /* |
| 1444 | * If nsindex is null loop_bitmask's bit 0 will be set, and if an index |
| 1445 | * block is found, a v1.1 label for any mapping will set bit 1, and a |
| 1446 | * v1.2 label will set bit 2. |
| 1447 | * |
| 1448 | * At the end of the loop, at most one of the three bits must be set. |
| 1449 | * If multiple bits were set, it means the different mappings disagree |
| 1450 | * about their labels, and this must be cleaned up first. |
| 1451 | * |
| 1452 | * If all the label index blocks are found to agree, nsindex of NULL |
| 1453 | * implies labels haven't been initialized yet, and when they will, |
| 1454 | * they will be of the 1.2 format, so we can assume BTT2.0 |
| 1455 | * |
| 1456 | * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are |
| 1457 | * found, we enforce BTT2.0 |
| 1458 | * |
| 1459 | * If the loop was never entered, default to BTT1.1 (legacy namespaces) |
| 1460 | */ |
| 1461 | switch (loop_bitmask) { |
| 1462 | case 0: |
| 1463 | case 2: |
| 1464 | return NVDIMM_CCLASS_BTT; |
| 1465 | case 1: |
| 1466 | case 4: |
| 1467 | return NVDIMM_CCLASS_BTT2; |
| 1468 | default: |
| 1469 | return -ENXIO; |
| 1470 | } |
| 1471 | } |
| 1472 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1473 | static ssize_t holder_show(struct device *dev, |
| 1474 | struct device_attribute *attr, char *buf) |
| 1475 | { |
| 1476 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1477 | ssize_t rc; |
| 1478 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1479 | nd_device_lock(dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1480 | rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : ""); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1481 | nd_device_unlock(dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1482 | |
| 1483 | return rc; |
| 1484 | } |
| 1485 | static DEVICE_ATTR_RO(holder); |
| 1486 | |
Ira Weiny | ab84b77 | 2019-11-14 19:06:34 -0800 | [diff] [blame] | 1487 | static int __holder_class_store(struct device *dev, const char *buf) |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1488 | { |
| 1489 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1490 | |
| 1491 | if (dev->driver || ndns->claim) |
| 1492 | return -EBUSY; |
| 1493 | |
Ira Weiny | ab84b77 | 2019-11-14 19:06:34 -0800 | [diff] [blame] | 1494 | if (sysfs_streq(buf, "btt")) { |
| 1495 | int rc = btt_claim_class(dev); |
| 1496 | |
| 1497 | if (rc < NVDIMM_CCLASS_NONE) |
| 1498 | return rc; |
| 1499 | ndns->claim_class = rc; |
| 1500 | } else if (sysfs_streq(buf, "pfn")) |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1501 | ndns->claim_class = NVDIMM_CCLASS_PFN; |
Dan Williams | 075c3fd | 2019-03-04 12:14:04 -0800 | [diff] [blame] | 1502 | else if (sysfs_streq(buf, "dax")) |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1503 | ndns->claim_class = NVDIMM_CCLASS_DAX; |
Dan Williams | 075c3fd | 2019-03-04 12:14:04 -0800 | [diff] [blame] | 1504 | else if (sysfs_streq(buf, "")) |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1505 | ndns->claim_class = NVDIMM_CCLASS_NONE; |
| 1506 | else |
| 1507 | return -EINVAL; |
| 1508 | |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | static ssize_t holder_class_store(struct device *dev, |
| 1513 | struct device_attribute *attr, const char *buf, size_t len) |
| 1514 | { |
| 1515 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Ira Weiny | ab84b77 | 2019-11-14 19:06:34 -0800 | [diff] [blame] | 1516 | int rc; |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1517 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1518 | nd_device_lock(dev); |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1519 | nvdimm_bus_lock(dev); |
| 1520 | wait_nvdimm_bus_probe_idle(dev); |
| 1521 | rc = __holder_class_store(dev, buf); |
| 1522 | if (rc >= 0) |
| 1523 | rc = nd_namespace_label_update(nd_region, dev); |
Ira Weiny | ab84b77 | 2019-11-14 19:06:34 -0800 | [diff] [blame] | 1524 | dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc); |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1525 | nvdimm_bus_unlock(dev); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1526 | nd_device_unlock(dev); |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1527 | |
| 1528 | return rc < 0 ? rc : len; |
| 1529 | } |
| 1530 | |
| 1531 | static ssize_t holder_class_show(struct device *dev, |
| 1532 | struct device_attribute *attr, char *buf) |
| 1533 | { |
| 1534 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1535 | ssize_t rc; |
| 1536 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1537 | nd_device_lock(dev); |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1538 | if (ndns->claim_class == NVDIMM_CCLASS_NONE) |
| 1539 | rc = sprintf(buf, "\n"); |
Vishal Verma | 14e4945 | 2017-06-28 14:25:00 -0600 | [diff] [blame] | 1540 | else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) || |
| 1541 | (ndns->claim_class == NVDIMM_CCLASS_BTT2)) |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1542 | rc = sprintf(buf, "btt\n"); |
| 1543 | else if (ndns->claim_class == NVDIMM_CCLASS_PFN) |
| 1544 | rc = sprintf(buf, "pfn\n"); |
| 1545 | else if (ndns->claim_class == NVDIMM_CCLASS_DAX) |
| 1546 | rc = sprintf(buf, "dax\n"); |
| 1547 | else |
| 1548 | rc = sprintf(buf, "<unknown>\n"); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1549 | nd_device_unlock(dev); |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1550 | |
| 1551 | return rc; |
| 1552 | } |
| 1553 | static DEVICE_ATTR_RW(holder_class); |
| 1554 | |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1555 | static ssize_t mode_show(struct device *dev, |
| 1556 | struct device_attribute *attr, char *buf) |
| 1557 | { |
| 1558 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1559 | struct device *claim; |
| 1560 | char *mode; |
| 1561 | ssize_t rc; |
| 1562 | |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1563 | nd_device_lock(dev); |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1564 | claim = ndns->claim; |
Dan Williams | 9c41242 | 2016-01-23 15:34:10 -0800 | [diff] [blame] | 1565 | if (claim && is_nd_btt(claim)) |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1566 | mode = "safe"; |
Dan Williams | 9c41242 | 2016-01-23 15:34:10 -0800 | [diff] [blame] | 1567 | else if (claim && is_nd_pfn(claim)) |
| 1568 | mode = "memory"; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1569 | else if (claim && is_nd_dax(claim)) |
| 1570 | mode = "dax"; |
Dan Williams | 9c41242 | 2016-01-23 15:34:10 -0800 | [diff] [blame] | 1571 | else if (!claim && pmem_should_map_pages(dev)) |
| 1572 | mode = "memory"; |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1573 | else |
| 1574 | mode = "raw"; |
| 1575 | rc = sprintf(buf, "%s\n", mode); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1576 | nd_device_unlock(dev); |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1577 | |
| 1578 | return rc; |
| 1579 | } |
| 1580 | static DEVICE_ATTR_RO(mode); |
| 1581 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1582 | static ssize_t force_raw_store(struct device *dev, |
| 1583 | struct device_attribute *attr, const char *buf, size_t len) |
| 1584 | { |
| 1585 | bool force_raw; |
| 1586 | int rc = strtobool(buf, &force_raw); |
| 1587 | |
| 1588 | if (rc) |
| 1589 | return rc; |
| 1590 | |
| 1591 | to_ndns(dev)->force_raw = force_raw; |
| 1592 | return len; |
| 1593 | } |
| 1594 | |
| 1595 | static ssize_t force_raw_show(struct device *dev, |
| 1596 | struct device_attribute *attr, char *buf) |
| 1597 | { |
| 1598 | return sprintf(buf, "%d\n", to_ndns(dev)->force_raw); |
| 1599 | } |
| 1600 | static DEVICE_ATTR_RW(force_raw); |
| 1601 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1602 | static struct attribute *nd_namespace_attributes[] = { |
| 1603 | &dev_attr_nstype.attr, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1604 | &dev_attr_size.attr, |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1605 | &dev_attr_mode.attr, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1606 | &dev_attr_uuid.attr, |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1607 | &dev_attr_holder.attr, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1608 | &dev_attr_resource.attr, |
| 1609 | &dev_attr_alt_name.attr, |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1610 | &dev_attr_force_raw.attr, |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1611 | &dev_attr_sector_size.attr, |
Dan Williams | 0ba1c63 | 2015-05-30 12:35:36 -0400 | [diff] [blame] | 1612 | &dev_attr_dpa_extents.attr, |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1613 | &dev_attr_holder_class.attr, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1614 | NULL, |
| 1615 | }; |
| 1616 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1617 | static umode_t namespace_visible(struct kobject *kobj, |
| 1618 | struct attribute *a, int n) |
| 1619 | { |
| 1620 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1621 | |
| 1622 | if (a == &dev_attr_resource.attr) { |
| 1623 | if (is_namespace_blk(dev)) |
| 1624 | return 0; |
Dan Williams | c1fb354 | 2017-09-26 11:21:24 -0700 | [diff] [blame] | 1625 | return 0400; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | if (is_namespace_pmem(dev) || is_namespace_blk(dev)) { |
| 1629 | if (a == &dev_attr_size.attr) |
Fabian Frederick | b44fe76 | 2016-12-04 10:54:08 -0800 | [diff] [blame] | 1630 | return 0644; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 1631 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1632 | return a->mode; |
| 1633 | } |
| 1634 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1635 | if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr |
| 1636 | || a == &dev_attr_holder.attr |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 1637 | || a == &dev_attr_holder_class.attr |
Dan Williams | 0731de0 | 2015-12-14 15:34:15 -0800 | [diff] [blame] | 1638 | || a == &dev_attr_force_raw.attr |
| 1639 | || a == &dev_attr_mode.attr) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1640 | return a->mode; |
| 1641 | |
| 1642 | return 0; |
| 1643 | } |
| 1644 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1645 | static struct attribute_group nd_namespace_attribute_group = { |
| 1646 | .attrs = nd_namespace_attributes, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1647 | .is_visible = namespace_visible, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1648 | }; |
| 1649 | |
| 1650 | static const struct attribute_group *nd_namespace_attribute_groups[] = { |
| 1651 | &nd_device_attribute_group, |
| 1652 | &nd_namespace_attribute_group, |
Toshi Kani | 74ae66c | 2015-06-19 12:18:34 -0600 | [diff] [blame] | 1653 | &nd_numa_attribute_group, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1654 | NULL, |
| 1655 | }; |
| 1656 | |
Dan Williams | 78c81cc | 2019-11-06 19:56:41 -0800 | [diff] [blame^] | 1657 | static const struct device_type namespace_io_device_type = { |
| 1658 | .name = "nd_namespace_io", |
| 1659 | .release = namespace_io_release, |
| 1660 | .groups = nd_namespace_attribute_groups, |
| 1661 | }; |
| 1662 | |
| 1663 | static const struct device_type namespace_pmem_device_type = { |
| 1664 | .name = "nd_namespace_pmem", |
| 1665 | .release = namespace_pmem_release, |
| 1666 | .groups = nd_namespace_attribute_groups, |
| 1667 | }; |
| 1668 | |
| 1669 | static const struct device_type namespace_blk_device_type = { |
| 1670 | .name = "nd_namespace_blk", |
| 1671 | .release = namespace_blk_release, |
| 1672 | .groups = nd_namespace_attribute_groups, |
| 1673 | }; |
| 1674 | |
| 1675 | static bool is_namespace_pmem(const struct device *dev) |
| 1676 | { |
| 1677 | return dev ? dev->type == &namespace_pmem_device_type : false; |
| 1678 | } |
| 1679 | |
| 1680 | static bool is_namespace_blk(const struct device *dev) |
| 1681 | { |
| 1682 | return dev ? dev->type == &namespace_blk_device_type : false; |
| 1683 | } |
| 1684 | |
| 1685 | static bool is_namespace_io(const struct device *dev) |
| 1686 | { |
| 1687 | return dev ? dev->type == &namespace_io_device_type : false; |
| 1688 | } |
| 1689 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1690 | struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev) |
| 1691 | { |
| 1692 | struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1693 | struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1694 | struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL; |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1695 | struct nd_namespace_common *ndns = NULL; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1696 | resource_size_t size; |
| 1697 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1698 | if (nd_btt || nd_pfn || nd_dax) { |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1699 | if (nd_btt) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1700 | ndns = nd_btt->ndns; |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1701 | else if (nd_pfn) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1702 | ndns = nd_pfn->ndns; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 1703 | else if (nd_dax) |
| 1704 | ndns = nd_dax->nd_pfn.ndns; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1705 | |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1706 | if (!ndns) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1707 | return ERR_PTR(-ENODEV); |
| 1708 | |
| 1709 | /* |
| 1710 | * Flush any in-progess probes / removals in the driver |
| 1711 | * for the raw personality of this namespace. |
| 1712 | */ |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1713 | nd_device_lock(&ndns->dev); |
| 1714 | nd_device_unlock(&ndns->dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1715 | if (ndns->dev.driver) { |
| 1716 | dev_dbg(&ndns->dev, "is active, can't bind %s\n", |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1717 | dev_name(dev)); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1718 | return ERR_PTR(-EBUSY); |
| 1719 | } |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1720 | if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev, |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1721 | "host (%s) vs claim (%s) mismatch\n", |
Dan Williams | 0bfb8dd | 2016-04-13 17:06:48 -0700 | [diff] [blame] | 1722 | dev_name(dev), |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1723 | dev_name(ndns->claim))) |
| 1724 | return ERR_PTR(-ENXIO); |
| 1725 | } else { |
| 1726 | ndns = to_ndns(dev); |
| 1727 | if (ndns->claim) { |
| 1728 | dev_dbg(dev, "claimed by %s, failing probe\n", |
| 1729 | dev_name(ndns->claim)); |
| 1730 | |
| 1731 | return ERR_PTR(-ENXIO); |
| 1732 | } |
| 1733 | } |
| 1734 | |
Dan Williams | 08e6b3c | 2018-06-13 09:08:36 -0700 | [diff] [blame] | 1735 | if (nvdimm_namespace_locked(ndns)) |
| 1736 | return ERR_PTR(-EACCES); |
| 1737 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1738 | size = nvdimm_namespace_capacity(ndns); |
| 1739 | if (size < ND_MIN_NAMESPACE_SIZE) { |
| 1740 | dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n", |
| 1741 | &size, ND_MIN_NAMESPACE_SIZE); |
| 1742 | return ERR_PTR(-ENODEV); |
| 1743 | } |
| 1744 | |
| 1745 | if (is_namespace_pmem(&ndns->dev)) { |
| 1746 | struct nd_namespace_pmem *nspm; |
| 1747 | |
| 1748 | nspm = to_nd_namespace_pmem(&ndns->dev); |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 1749 | if (uuid_not_set(nspm->uuid, &ndns->dev, __func__)) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1750 | return ERR_PTR(-ENODEV); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1751 | } else if (is_namespace_blk(&ndns->dev)) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1752 | struct nd_namespace_blk *nsblk; |
| 1753 | |
| 1754 | nsblk = to_nd_namespace_blk(&ndns->dev); |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 1755 | if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__)) |
| 1756 | return ERR_PTR(-ENODEV); |
| 1757 | if (!nsblk->lbasize) { |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 1758 | dev_dbg(&ndns->dev, "sector size not set\n"); |
Dmitry Krivenok | bd26d0d | 2015-12-02 00:48:12 +0300 | [diff] [blame] | 1759 | return ERR_PTR(-ENODEV); |
| 1760 | } |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1761 | if (!nd_namespace_blk_validate(nsblk)) |
| 1762 | return ERR_PTR(-ENODEV); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | return ndns; |
| 1766 | } |
| 1767 | EXPORT_SYMBOL(nvdimm_namespace_common_probe); |
| 1768 | |
Aneesh Kumar K.V | 8f4b01f | 2019-10-31 16:27:41 +0530 | [diff] [blame] | 1769 | int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns, |
| 1770 | resource_size_t size) |
| 1771 | { |
| 1772 | if (is_namespace_blk(&ndns->dev)) |
| 1773 | return 0; |
| 1774 | return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size); |
| 1775 | } |
| 1776 | EXPORT_SYMBOL_GPL(devm_namespace_enable); |
| 1777 | |
| 1778 | void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns) |
| 1779 | { |
| 1780 | if (is_namespace_blk(&ndns->dev)) |
| 1781 | return; |
| 1782 | devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev)); |
| 1783 | } |
| 1784 | EXPORT_SYMBOL_GPL(devm_namespace_disable); |
| 1785 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1786 | static struct device **create_namespace_io(struct nd_region *nd_region) |
| 1787 | { |
| 1788 | struct nd_namespace_io *nsio; |
| 1789 | struct device *dev, **devs; |
| 1790 | struct resource *res; |
| 1791 | |
| 1792 | nsio = kzalloc(sizeof(*nsio), GFP_KERNEL); |
| 1793 | if (!nsio) |
| 1794 | return NULL; |
| 1795 | |
| 1796 | devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); |
| 1797 | if (!devs) { |
| 1798 | kfree(nsio); |
| 1799 | return NULL; |
| 1800 | } |
| 1801 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 1802 | dev = &nsio->common.dev; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1803 | dev->type = &namespace_io_device_type; |
| 1804 | dev->parent = &nd_region->dev; |
| 1805 | res = &nsio->res; |
| 1806 | res->name = dev_name(&nd_region->dev); |
| 1807 | res->flags = IORESOURCE_MEM; |
| 1808 | res->start = nd_region->ndr_start; |
| 1809 | res->end = res->start + nd_region->ndr_size - 1; |
| 1810 | |
| 1811 | devs[0] = dev; |
| 1812 | return devs; |
| 1813 | } |
| 1814 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1815 | static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid, |
| 1816 | u64 cookie, u16 pos) |
| 1817 | { |
| 1818 | struct nd_namespace_label *found = NULL; |
| 1819 | int i; |
| 1820 | |
| 1821 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1822 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 1823 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
| 1824 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1825 | struct nd_label_ent *label_ent; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1826 | bool found_uuid = false; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1827 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1828 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
| 1829 | struct nd_namespace_label *nd_label = label_ent->label; |
| 1830 | u16 position, nlabel; |
| 1831 | u64 isetcookie; |
| 1832 | |
| 1833 | if (!nd_label) |
| 1834 | continue; |
| 1835 | isetcookie = __le64_to_cpu(nd_label->isetcookie); |
| 1836 | position = __le16_to_cpu(nd_label->position); |
| 1837 | nlabel = __le16_to_cpu(nd_label->nlabel); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1838 | |
| 1839 | if (isetcookie != cookie) |
| 1840 | continue; |
| 1841 | |
| 1842 | if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0) |
| 1843 | continue; |
| 1844 | |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 1845 | if (namespace_label_has(ndd, type_guid) |
| 1846 | && !guid_equal(&nd_set->type_guid, |
| 1847 | &nd_label->type_guid)) { |
| 1848 | dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", |
Andy Shevchenko | db5d00c | 2019-06-21 14:45:18 +0300 | [diff] [blame] | 1849 | &nd_set->type_guid, |
| 1850 | &nd_label->type_guid); |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 1851 | continue; |
| 1852 | } |
| 1853 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1854 | if (found_uuid) { |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 1855 | dev_dbg(ndd->dev, "duplicate entry for uuid\n"); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1856 | return false; |
| 1857 | } |
| 1858 | found_uuid = true; |
| 1859 | if (nlabel != nd_region->ndr_mappings) |
| 1860 | continue; |
| 1861 | if (position != pos) |
| 1862 | continue; |
| 1863 | found = nd_label; |
| 1864 | break; |
| 1865 | } |
| 1866 | if (found) |
| 1867 | break; |
| 1868 | } |
| 1869 | return found != NULL; |
| 1870 | } |
| 1871 | |
| 1872 | static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id) |
| 1873 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1874 | int i; |
| 1875 | |
| 1876 | if (!pmem_id) |
| 1877 | return -ENODEV; |
| 1878 | |
| 1879 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1880 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1881 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1882 | struct nd_namespace_label *nd_label = NULL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1883 | u64 hw_start, hw_end, pmem_start, pmem_end; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1884 | struct nd_label_ent *label_ent; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1885 | |
Dan Williams | 9cf8bd5 | 2016-12-15 20:04:31 -0800 | [diff] [blame] | 1886 | lockdep_assert_held(&nd_mapping->lock); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1887 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
| 1888 | nd_label = label_ent->label; |
| 1889 | if (!nd_label) |
| 1890 | continue; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1891 | if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0) |
| 1892 | break; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1893 | nd_label = NULL; |
| 1894 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1895 | |
| 1896 | if (!nd_label) { |
| 1897 | WARN_ON(1); |
| 1898 | return -EINVAL; |
| 1899 | } |
| 1900 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1901 | /* |
| 1902 | * Check that this label is compliant with the dpa |
| 1903 | * range published in NFIT |
| 1904 | */ |
| 1905 | hw_start = nd_mapping->start; |
| 1906 | hw_end = hw_start + nd_mapping->size; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1907 | pmem_start = __le64_to_cpu(nd_label->dpa); |
| 1908 | pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize); |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1909 | if (pmem_start >= hw_start && pmem_start < hw_end |
| 1910 | && pmem_end <= hw_end && pmem_end > hw_start) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1911 | /* pass */; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1912 | else { |
| 1913 | dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n", |
| 1914 | dev_name(ndd->dev), nd_label->uuid); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1915 | return -EINVAL; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1916 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1917 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1918 | /* move recently validated label to the front of the list */ |
| 1919 | list_move(&label_ent->list, &nd_mapping->labels); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1920 | } |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | /** |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1925 | * create_namespace_pmem - validate interleave set labelling, retrieve label0 |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1926 | * @nd_region: region with mappings to validate |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1927 | * @nspm: target namespace to create |
| 1928 | * @nd_label: target pmem namespace label to evaluate |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1929 | */ |
Colin Ian King | 65853a1 | 2017-10-05 10:55:57 +0100 | [diff] [blame] | 1930 | static struct device *create_namespace_pmem(struct nd_region *nd_region, |
Dan Williams | c12c48c | 2017-06-04 10:59:15 +0900 | [diff] [blame] | 1931 | struct nd_namespace_index *nsindex, |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1932 | struct nd_namespace_label *nd_label) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1933 | { |
Dan Williams | c12c48c | 2017-06-04 10:59:15 +0900 | [diff] [blame] | 1934 | u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex); |
Dan Williams | 86ef58a | 2017-02-28 18:32:48 -0800 | [diff] [blame] | 1935 | u64 altcookie = nd_region_interleave_set_altcookie(nd_region); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1936 | struct nd_label_ent *label_ent; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1937 | struct nd_namespace_pmem *nspm; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1938 | struct nd_mapping *nd_mapping; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1939 | resource_size_t size = 0; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1940 | struct resource *res; |
| 1941 | struct device *dev; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1942 | int rc = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1943 | u16 i; |
| 1944 | |
Dan Williams | 4765218 | 2016-09-15 18:08:05 -0700 | [diff] [blame] | 1945 | if (cookie == 0) { |
| 1946 | dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n"); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1947 | return ERR_PTR(-ENXIO); |
Dan Williams | 4765218 | 2016-09-15 18:08:05 -0700 | [diff] [blame] | 1948 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1949 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1950 | if (__le64_to_cpu(nd_label->isetcookie) != cookie) { |
| 1951 | dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n", |
| 1952 | nd_label->uuid); |
Dan Williams | 86ef58a | 2017-02-28 18:32:48 -0800 | [diff] [blame] | 1953 | if (__le64_to_cpu(nd_label->isetcookie) != altcookie) |
| 1954 | return ERR_PTR(-EAGAIN); |
| 1955 | |
| 1956 | dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n", |
| 1957 | nd_label->uuid); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1958 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1959 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1960 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
| 1961 | if (!nspm) |
| 1962 | return ERR_PTR(-ENOMEM); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1963 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1964 | nspm->id = -1; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1965 | dev = &nspm->nsio.common.dev; |
| 1966 | dev->type = &namespace_pmem_device_type; |
| 1967 | dev->parent = &nd_region->dev; |
| 1968 | res = &nspm->nsio.res; |
| 1969 | res->name = dev_name(&nd_region->dev); |
| 1970 | res->flags = IORESOURCE_MEM; |
| 1971 | |
Dan Williams | 86ef58a | 2017-02-28 18:32:48 -0800 | [diff] [blame] | 1972 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 1973 | if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i)) |
| 1974 | continue; |
| 1975 | if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i)) |
| 1976 | continue; |
| 1977 | break; |
| 1978 | } |
| 1979 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1980 | if (i < nd_region->ndr_mappings) { |
Dan Williams | 4f86722 | 2018-04-06 16:37:21 -0700 | [diff] [blame] | 1981 | struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1982 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1983 | /* |
| 1984 | * Give up if we don't find an instance of a uuid at each |
| 1985 | * position (from 0 to nd_region->ndr_mappings - 1), or if we |
| 1986 | * find a dimm with two instances of the same uuid. |
| 1987 | */ |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 1988 | dev_err(&nd_region->dev, "%s missing label for %pUb\n", |
Dan Williams | 4f86722 | 2018-04-06 16:37:21 -0700 | [diff] [blame] | 1989 | nvdimm_name(nvdimm), nd_label->uuid); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1990 | rc = -EINVAL; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 1991 | goto err; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 1992 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 1993 | |
| 1994 | /* |
| 1995 | * Fix up each mapping's 'labels' to have the validated pmem label for |
| 1996 | * that position at labels[0], and NULL at labels[1]. In the process, |
| 1997 | * check that the namespace aligns with interleave-set. We know |
| 1998 | * that it does not overlap with any blk namespaces by virtue of |
| 1999 | * the dimm being enabled (i.e. nd_label_reserve_dpa() |
| 2000 | * succeeded). |
| 2001 | */ |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2002 | rc = select_pmem_id(nd_region, nd_label->uuid); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2003 | if (rc) |
| 2004 | goto err; |
| 2005 | |
| 2006 | /* Calculate total size and populate namespace properties from label0 */ |
| 2007 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2008 | struct nd_namespace_label *label0; |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 2009 | struct nvdimm_drvdata *ndd; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2010 | |
| 2011 | nd_mapping = &nd_region->mapping[i]; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2012 | label_ent = list_first_entry_or_null(&nd_mapping->labels, |
| 2013 | typeof(*label_ent), list); |
Aneesh Kumar K.V | 86aa666 | 2019-08-09 13:17:26 +0530 | [diff] [blame] | 2014 | label0 = label_ent ? label_ent->label : NULL; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2015 | |
| 2016 | if (!label0) { |
| 2017 | WARN_ON(1); |
| 2018 | continue; |
| 2019 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2020 | |
| 2021 | size += __le64_to_cpu(label0->rawsize); |
| 2022 | if (__le16_to_cpu(label0->position) != 0) |
| 2023 | continue; |
| 2024 | WARN_ON(nspm->alt_name || nspm->uuid); |
| 2025 | nspm->alt_name = kmemdup((void __force *) label0->name, |
| 2026 | NSLABEL_NAME_LEN, GFP_KERNEL); |
| 2027 | nspm->uuid = kmemdup((void __force *) label0->uuid, |
| 2028 | NSLABEL_UUID_LEN, GFP_KERNEL); |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 2029 | nspm->lbasize = __le64_to_cpu(label0->lbasize); |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 2030 | ndd = to_ndd(nd_mapping); |
| 2031 | if (namespace_label_has(ndd, abstraction_guid)) |
| 2032 | nspm->nsio.common.claim_class |
| 2033 | = to_nvdimm_cclass(&label0->abstraction_guid); |
| 2034 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | if (!nspm->alt_name || !nspm->uuid) { |
| 2038 | rc = -ENOMEM; |
| 2039 | goto err; |
| 2040 | } |
| 2041 | |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2042 | nd_namespace_pmem_set_resource(nd_region, nspm, size); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2043 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2044 | return dev; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2045 | err: |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2046 | namespace_pmem_release(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2047 | switch (rc) { |
| 2048 | case -EINVAL: |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 2049 | dev_dbg(&nd_region->dev, "invalid label(s)\n"); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2050 | break; |
| 2051 | case -ENODEV: |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 2052 | dev_dbg(&nd_region->dev, "label not found\n"); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2053 | break; |
| 2054 | default: |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 2055 | dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2056 | break; |
| 2057 | } |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2058 | return ERR_PTR(rc); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2059 | } |
| 2060 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2061 | struct resource *nsblk_add_resource(struct nd_region *nd_region, |
| 2062 | struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk, |
| 2063 | resource_size_t start) |
| 2064 | { |
| 2065 | struct nd_label_id label_id; |
| 2066 | struct resource *res; |
| 2067 | |
| 2068 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); |
| 2069 | res = krealloc(nsblk->res, |
| 2070 | sizeof(void *) * (nsblk->num_resources + 1), |
| 2071 | GFP_KERNEL); |
| 2072 | if (!res) |
| 2073 | return NULL; |
| 2074 | nsblk->res = (struct resource **) res; |
| 2075 | for_each_dpa_resource(ndd, res) |
| 2076 | if (strcmp(res->name, label_id.id) == 0 |
| 2077 | && res->start == start) { |
| 2078 | nsblk->res[nsblk->num_resources++] = res; |
| 2079 | return res; |
| 2080 | } |
| 2081 | return NULL; |
| 2082 | } |
| 2083 | |
| 2084 | static struct device *nd_namespace_blk_create(struct nd_region *nd_region) |
| 2085 | { |
| 2086 | struct nd_namespace_blk *nsblk; |
| 2087 | struct device *dev; |
| 2088 | |
| 2089 | if (!is_nd_blk(&nd_region->dev)) |
| 2090 | return NULL; |
| 2091 | |
| 2092 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); |
| 2093 | if (!nsblk) |
| 2094 | return NULL; |
| 2095 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 2096 | dev = &nsblk->common.dev; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2097 | dev->type = &namespace_blk_device_type; |
| 2098 | nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); |
| 2099 | if (nsblk->id < 0) { |
| 2100 | kfree(nsblk); |
| 2101 | return NULL; |
| 2102 | } |
| 2103 | dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id); |
| 2104 | dev->parent = &nd_region->dev; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2105 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 2106 | return &nsblk->common.dev; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2107 | } |
| 2108 | |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 2109 | static struct device *nd_namespace_pmem_create(struct nd_region *nd_region) |
| 2110 | { |
| 2111 | struct nd_namespace_pmem *nspm; |
| 2112 | struct resource *res; |
| 2113 | struct device *dev; |
| 2114 | |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 2115 | if (!is_memory(&nd_region->dev)) |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 2116 | return NULL; |
| 2117 | |
| 2118 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
| 2119 | if (!nspm) |
| 2120 | return NULL; |
| 2121 | |
| 2122 | dev = &nspm->nsio.common.dev; |
| 2123 | dev->type = &namespace_pmem_device_type; |
| 2124 | dev->parent = &nd_region->dev; |
| 2125 | res = &nspm->nsio.res; |
| 2126 | res->name = dev_name(&nd_region->dev); |
| 2127 | res->flags = IORESOURCE_MEM; |
| 2128 | |
| 2129 | nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); |
| 2130 | if (nspm->id < 0) { |
| 2131 | kfree(nspm); |
| 2132 | return NULL; |
| 2133 | } |
| 2134 | dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id); |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 2135 | nd_namespace_pmem_set_resource(nd_region, nspm, 0); |
| 2136 | |
| 2137 | return dev; |
| 2138 | } |
| 2139 | |
| 2140 | void nd_region_create_ns_seed(struct nd_region *nd_region) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2141 | { |
| 2142 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 2143 | |
| 2144 | if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO) |
| 2145 | return; |
| 2146 | |
| 2147 | if (is_nd_blk(&nd_region->dev)) |
| 2148 | nd_region->ns_seed = nd_namespace_blk_create(nd_region); |
| 2149 | else |
| 2150 | nd_region->ns_seed = nd_namespace_pmem_create(nd_region); |
| 2151 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2152 | /* |
| 2153 | * Seed creation failures are not fatal, provisioning is simply |
| 2154 | * disabled until memory becomes available |
| 2155 | */ |
| 2156 | if (!nd_region->ns_seed) |
Dan Williams | 98a29c3 | 2016-09-30 15:28:27 -0700 | [diff] [blame] | 2157 | dev_err(&nd_region->dev, "failed to create %s namespace\n", |
| 2158 | is_nd_blk(&nd_region->dev) ? "blk" : "pmem"); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2159 | else |
| 2160 | nd_device_register(nd_region->ns_seed); |
| 2161 | } |
| 2162 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 2163 | void nd_region_create_dax_seed(struct nd_region *nd_region) |
| 2164 | { |
| 2165 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
| 2166 | nd_region->dax_seed = nd_dax_create(nd_region); |
| 2167 | /* |
| 2168 | * Seed creation failures are not fatal, provisioning is simply |
| 2169 | * disabled until memory becomes available |
| 2170 | */ |
| 2171 | if (!nd_region->dax_seed) |
| 2172 | dev_err(&nd_region->dev, "failed to create dax namespace\n"); |
| 2173 | } |
| 2174 | |
Dan Williams | 2dc4333 | 2015-12-13 11:41:36 -0800 | [diff] [blame] | 2175 | void nd_region_create_pfn_seed(struct nd_region *nd_region) |
| 2176 | { |
| 2177 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
| 2178 | nd_region->pfn_seed = nd_pfn_create(nd_region); |
| 2179 | /* |
| 2180 | * Seed creation failures are not fatal, provisioning is simply |
| 2181 | * disabled until memory becomes available |
| 2182 | */ |
| 2183 | if (!nd_region->pfn_seed) |
| 2184 | dev_err(&nd_region->dev, "failed to create pfn namespace\n"); |
| 2185 | } |
| 2186 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 2187 | void nd_region_create_btt_seed(struct nd_region *nd_region) |
| 2188 | { |
| 2189 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
| 2190 | nd_region->btt_seed = nd_btt_create(nd_region); |
| 2191 | /* |
| 2192 | * Seed creation failures are not fatal, provisioning is simply |
| 2193 | * disabled until memory becomes available |
| 2194 | */ |
| 2195 | if (!nd_region->btt_seed) |
| 2196 | dev_err(&nd_region->dev, "failed to create btt namespace\n"); |
| 2197 | } |
| 2198 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2199 | static int add_namespace_resource(struct nd_region *nd_region, |
| 2200 | struct nd_namespace_label *nd_label, struct device **devs, |
| 2201 | int count) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2202 | { |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2203 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2204 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2205 | int i; |
| 2206 | |
| 2207 | for (i = 0; i < count; i++) { |
| 2208 | u8 *uuid = namespace_to_uuid(devs[i]); |
| 2209 | struct resource *res; |
| 2210 | |
| 2211 | if (IS_ERR_OR_NULL(uuid)) { |
| 2212 | WARN_ON(1); |
| 2213 | continue; |
| 2214 | } |
| 2215 | |
| 2216 | if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0) |
| 2217 | continue; |
| 2218 | if (is_namespace_blk(devs[i])) { |
| 2219 | res = nsblk_add_resource(nd_region, ndd, |
| 2220 | to_nd_namespace_blk(devs[i]), |
| 2221 | __le64_to_cpu(nd_label->dpa)); |
| 2222 | if (!res) |
| 2223 | return -ENXIO; |
| 2224 | nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count); |
| 2225 | } else { |
| 2226 | dev_err(&nd_region->dev, |
| 2227 | "error: conflicting extents for uuid: %pUb\n", |
| 2228 | nd_label->uuid); |
| 2229 | return -ENXIO; |
| 2230 | } |
| 2231 | break; |
| 2232 | } |
| 2233 | |
| 2234 | return i; |
| 2235 | } |
| 2236 | |
Colin Ian King | 65853a1 | 2017-10-05 10:55:57 +0100 | [diff] [blame] | 2237 | static struct device *create_namespace_blk(struct nd_region *nd_region, |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2238 | struct nd_namespace_label *nd_label, int count) |
| 2239 | { |
| 2240 | |
| 2241 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 2242 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2243 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2244 | struct nd_namespace_blk *nsblk; |
Nicolas Iooss | 238b323 | 2016-11-26 20:18:04 +0100 | [diff] [blame] | 2245 | char name[NSLABEL_NAME_LEN]; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2246 | struct device *dev = NULL; |
| 2247 | struct resource *res; |
| 2248 | |
Dan Williams | 8f2bc24 | 2017-06-06 11:39:30 -0700 | [diff] [blame] | 2249 | if (namespace_label_has(ndd, type_guid)) { |
| 2250 | if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) { |
| 2251 | dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", |
Andy Shevchenko | db5d00c | 2019-06-21 14:45:18 +0300 | [diff] [blame] | 2252 | &nd_set->type_guid, |
| 2253 | &nd_label->type_guid); |
Dan Williams | 8f2bc24 | 2017-06-06 11:39:30 -0700 | [diff] [blame] | 2254 | return ERR_PTR(-EAGAIN); |
| 2255 | } |
| 2256 | |
| 2257 | if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) { |
| 2258 | dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n", |
| 2259 | nd_set->cookie2, |
| 2260 | __le64_to_cpu(nd_label->isetcookie)); |
| 2261 | return ERR_PTR(-EAGAIN); |
| 2262 | } |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2265 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); |
| 2266 | if (!nsblk) |
| 2267 | return ERR_PTR(-ENOMEM); |
| 2268 | dev = &nsblk->common.dev; |
| 2269 | dev->type = &namespace_blk_device_type; |
| 2270 | dev->parent = &nd_region->dev; |
| 2271 | nsblk->id = -1; |
| 2272 | nsblk->lbasize = __le64_to_cpu(nd_label->lbasize); |
| 2273 | nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN, |
| 2274 | GFP_KERNEL); |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 2275 | if (namespace_label_has(ndd, abstraction_guid)) |
| 2276 | nsblk->common.claim_class |
| 2277 | = to_nvdimm_cclass(&nd_label->abstraction_guid); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2278 | if (!nsblk->uuid) |
| 2279 | goto blk_err; |
| 2280 | memcpy(name, nd_label->name, NSLABEL_NAME_LEN); |
Kangjie Lu | 55c1fc0 | 2019-03-12 03:20:34 -0500 | [diff] [blame] | 2281 | if (name[0]) { |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2282 | nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, |
| 2283 | GFP_KERNEL); |
Kangjie Lu | 55c1fc0 | 2019-03-12 03:20:34 -0500 | [diff] [blame] | 2284 | if (!nsblk->alt_name) |
| 2285 | goto blk_err; |
| 2286 | } |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2287 | res = nsblk_add_resource(nd_region, ndd, nsblk, |
| 2288 | __le64_to_cpu(nd_label->dpa)); |
| 2289 | if (!res) |
| 2290 | goto blk_err; |
| 2291 | nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count); |
| 2292 | return dev; |
| 2293 | blk_err: |
| 2294 | namespace_blk_release(dev); |
| 2295 | return ERR_PTR(-ENXIO); |
| 2296 | } |
| 2297 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 2298 | static int cmp_dpa(const void *a, const void *b) |
| 2299 | { |
| 2300 | const struct device *dev_a = *(const struct device **) a; |
| 2301 | const struct device *dev_b = *(const struct device **) b; |
| 2302 | struct nd_namespace_blk *nsblk_a, *nsblk_b; |
| 2303 | struct nd_namespace_pmem *nspm_a, *nspm_b; |
| 2304 | |
| 2305 | if (is_namespace_io(dev_a)) |
| 2306 | return 0; |
| 2307 | |
| 2308 | if (is_namespace_blk(dev_a)) { |
| 2309 | nsblk_a = to_nd_namespace_blk(dev_a); |
| 2310 | nsblk_b = to_nd_namespace_blk(dev_b); |
| 2311 | |
| 2312 | return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start, |
| 2313 | sizeof(resource_size_t)); |
| 2314 | } |
| 2315 | |
| 2316 | nspm_a = to_nd_namespace_pmem(dev_a); |
| 2317 | nspm_b = to_nd_namespace_pmem(dev_b); |
| 2318 | |
| 2319 | return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start, |
| 2320 | sizeof(resource_size_t)); |
| 2321 | } |
| 2322 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2323 | static struct device **scan_labels(struct nd_region *nd_region) |
| 2324 | { |
Dan Williams | c969e24 | 2016-10-05 15:54:46 -0700 | [diff] [blame] | 2325 | int i, count = 0; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2326 | struct device *dev, **devs = NULL; |
| 2327 | struct nd_label_ent *label_ent, *e; |
Dan Williams | c969e24 | 2016-10-05 15:54:46 -0700 | [diff] [blame] | 2328 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 2329 | resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2330 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2331 | /* "safe" because create_namespace_pmem() might list_move() label_ent */ |
| 2332 | list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) { |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2333 | struct nd_namespace_label *nd_label = label_ent->label; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2334 | struct device **__devs; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2335 | u32 flags; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2336 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2337 | if (!nd_label) |
| 2338 | continue; |
| 2339 | flags = __le32_to_cpu(nd_label->flags); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2340 | if (is_nd_blk(&nd_region->dev) |
| 2341 | == !!(flags & NSLABEL_FLAG_LOCAL)) |
| 2342 | /* pass, region matches label type */; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2343 | else |
| 2344 | continue; |
| 2345 | |
Dan Williams | c969e24 | 2016-10-05 15:54:46 -0700 | [diff] [blame] | 2346 | /* skip labels that describe extents outside of the region */ |
Aneesh Kumar K.V | 86aa666 | 2019-08-09 13:17:26 +0530 | [diff] [blame] | 2347 | if (__le64_to_cpu(nd_label->dpa) < nd_mapping->start || |
| 2348 | __le64_to_cpu(nd_label->dpa) > map_end) |
| 2349 | continue; |
Dan Williams | c969e24 | 2016-10-05 15:54:46 -0700 | [diff] [blame] | 2350 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2351 | i = add_namespace_resource(nd_region, nd_label, devs, count); |
| 2352 | if (i < 0) |
| 2353 | goto err; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2354 | if (i < count) |
| 2355 | continue; |
| 2356 | __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL); |
| 2357 | if (!__devs) |
| 2358 | goto err; |
| 2359 | memcpy(__devs, devs, sizeof(dev) * count); |
| 2360 | kfree(devs); |
| 2361 | devs = __devs; |
| 2362 | |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 2363 | if (is_nd_blk(&nd_region->dev)) |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2364 | dev = create_namespace_blk(nd_region, nd_label, count); |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 2365 | else { |
Dan Williams | c12c48c | 2017-06-04 10:59:15 +0900 | [diff] [blame] | 2366 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 2367 | struct nd_namespace_index *nsindex; |
| 2368 | |
| 2369 | nsindex = to_namespace_index(ndd, ndd->ns_current); |
| 2370 | dev = create_namespace_pmem(nd_region, nsindex, nd_label); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2371 | } |
Dan Williams | faec6f8 | 2017-06-06 11:10:51 -0700 | [diff] [blame] | 2372 | |
| 2373 | if (IS_ERR(dev)) { |
| 2374 | switch (PTR_ERR(dev)) { |
| 2375 | case -EAGAIN: |
| 2376 | /* skip invalid labels */ |
| 2377 | continue; |
| 2378 | case -ENODEV: |
| 2379 | /* fallthrough to seed creation */ |
| 2380 | break; |
| 2381 | default: |
| 2382 | goto err; |
| 2383 | } |
| 2384 | } else |
| 2385 | devs[count++] = dev; |
| 2386 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2387 | } |
| 2388 | |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 2389 | dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n", |
| 2390 | count, is_nd_blk(&nd_region->dev) |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2391 | ? "blk" : "pmem", count == 1 ? "" : "s"); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2392 | |
| 2393 | if (count == 0) { |
| 2394 | /* Publish a zero-sized namespace for userspace to configure. */ |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2395 | nd_mapping_free_labels(nd_mapping); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2396 | |
| 2397 | devs = kcalloc(2, sizeof(dev), GFP_KERNEL); |
| 2398 | if (!devs) |
| 2399 | goto err; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2400 | if (is_nd_blk(&nd_region->dev)) { |
| 2401 | struct nd_namespace_blk *nsblk; |
| 2402 | |
| 2403 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); |
| 2404 | if (!nsblk) |
| 2405 | goto err; |
| 2406 | dev = &nsblk->common.dev; |
| 2407 | dev->type = &namespace_blk_device_type; |
| 2408 | } else { |
| 2409 | struct nd_namespace_pmem *nspm; |
| 2410 | |
| 2411 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
| 2412 | if (!nspm) |
| 2413 | goto err; |
| 2414 | dev = &nspm->nsio.common.dev; |
| 2415 | dev->type = &namespace_pmem_device_type; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2416 | nd_namespace_pmem_set_resource(nd_region, nspm, 0); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2417 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2418 | dev->parent = &nd_region->dev; |
| 2419 | devs[count++] = dev; |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 2420 | } else if (is_memory(&nd_region->dev)) { |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2421 | /* clean unselected labels */ |
| 2422 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2423 | struct list_head *l, *e; |
| 2424 | LIST_HEAD(list); |
| 2425 | int j; |
| 2426 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2427 | nd_mapping = &nd_region->mapping[i]; |
| 2428 | if (list_empty(&nd_mapping->labels)) { |
| 2429 | WARN_ON(1); |
| 2430 | continue; |
| 2431 | } |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2432 | |
| 2433 | j = count; |
| 2434 | list_for_each_safe(l, e, &nd_mapping->labels) { |
| 2435 | if (!j--) |
| 2436 | break; |
| 2437 | list_move_tail(l, &list); |
| 2438 | } |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2439 | nd_mapping_free_labels(nd_mapping); |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2440 | list_splice_init(&list, &nd_mapping->labels); |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2441 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2442 | } |
| 2443 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 2444 | if (count > 1) |
| 2445 | sort(devs, count, sizeof(struct device *), cmp_dpa, NULL); |
| 2446 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2447 | return devs; |
| 2448 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2449 | err: |
Dan Carpenter | 75d2971 | 2016-10-12 09:34:29 +0300 | [diff] [blame] | 2450 | if (devs) { |
| 2451 | for (i = 0; devs[i]; i++) |
| 2452 | if (is_nd_blk(&nd_region->dev)) |
| 2453 | namespace_blk_release(devs[i]); |
| 2454 | else |
| 2455 | namespace_pmem_release(devs[i]); |
| 2456 | kfree(devs); |
| 2457 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2458 | return NULL; |
| 2459 | } |
| 2460 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2461 | static struct device **create_namespaces(struct nd_region *nd_region) |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2462 | { |
Colin Ian King | 59858d3 | 2018-01-30 17:47:07 +0000 | [diff] [blame] | 2463 | struct nd_mapping *nd_mapping; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2464 | struct device **devs; |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2465 | int i; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2466 | |
| 2467 | if (nd_region->ndr_mappings == 0) |
| 2468 | return NULL; |
| 2469 | |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2470 | /* lock down all mappings while we scan labels */ |
| 2471 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 2472 | nd_mapping = &nd_region->mapping[i]; |
| 2473 | mutex_lock_nested(&nd_mapping->lock, i); |
| 2474 | } |
| 2475 | |
| 2476 | devs = scan_labels(nd_region); |
| 2477 | |
| 2478 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 2479 | int reverse = nd_region->ndr_mappings - 1 - i; |
| 2480 | |
| 2481 | nd_mapping = &nd_region->mapping[reverse]; |
| 2482 | mutex_unlock(&nd_mapping->lock); |
| 2483 | } |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2484 | |
| 2485 | return devs; |
| 2486 | } |
| 2487 | |
Dan Williams | a2d1c7a | 2019-09-05 21:15:57 +0530 | [diff] [blame] | 2488 | static void deactivate_labels(void *region) |
| 2489 | { |
| 2490 | struct nd_region *nd_region = region; |
| 2491 | int i; |
| 2492 | |
| 2493 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 2494 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 2495 | struct nvdimm_drvdata *ndd = nd_mapping->ndd; |
| 2496 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 2497 | |
| 2498 | mutex_lock(&nd_mapping->lock); |
| 2499 | nd_mapping_free_labels(nd_mapping); |
| 2500 | mutex_unlock(&nd_mapping->lock); |
| 2501 | |
| 2502 | put_ndd(ndd); |
| 2503 | nd_mapping->ndd = NULL; |
| 2504 | if (ndd) |
| 2505 | atomic_dec(&nvdimm->busy); |
| 2506 | } |
| 2507 | } |
| 2508 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2509 | static int init_active_labels(struct nd_region *nd_region) |
| 2510 | { |
| 2511 | int i; |
| 2512 | |
| 2513 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 2514 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 2515 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 2516 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2517 | struct nd_label_ent *label_ent; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2518 | int count, j; |
| 2519 | |
| 2520 | /* |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 2521 | * If the dimm is disabled then we may need to prevent |
| 2522 | * the region from being activated. |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2523 | */ |
| 2524 | if (!ndd) { |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 2525 | if (test_bit(NDD_LOCKED, &nvdimm->flags)) |
| 2526 | /* fail, label data may be unreadable */; |
| 2527 | else if (test_bit(NDD_ALIASING, &nvdimm->flags)) |
| 2528 | /* fail, labels needed to disambiguate dpa */; |
| 2529 | else |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2530 | return 0; |
Dan Williams | 9d62ed9 | 2017-05-04 11:47:22 -0700 | [diff] [blame] | 2531 | |
| 2532 | dev_err(&nd_region->dev, "%s: is %s, failing probe\n", |
| 2533 | dev_name(&nd_mapping->nvdimm->dev), |
| 2534 | test_bit(NDD_LOCKED, &nvdimm->flags) |
| 2535 | ? "locked" : "disabled"); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2536 | return -ENXIO; |
| 2537 | } |
| 2538 | nd_mapping->ndd = ndd; |
| 2539 | atomic_inc(&nvdimm->busy); |
| 2540 | get_ndd(ndd); |
| 2541 | |
| 2542 | count = nd_label_active_count(ndd); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 2543 | dev_dbg(ndd->dev, "count: %d\n", count); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2544 | if (!count) |
| 2545 | continue; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2546 | for (j = 0; j < count; j++) { |
| 2547 | struct nd_namespace_label *label; |
| 2548 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2549 | label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL); |
| 2550 | if (!label_ent) |
| 2551 | break; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2552 | label = nd_label_active(ndd, j); |
Dan Williams | d5d30d5 | 2019-02-02 16:35:26 -0800 | [diff] [blame] | 2553 | if (test_bit(NDD_NOBLK, &nvdimm->flags)) { |
| 2554 | u32 flags = __le32_to_cpu(label->flags); |
| 2555 | |
| 2556 | flags &= ~NSLABEL_FLAG_LOCAL; |
| 2557 | label->flags = __cpu_to_le32(flags); |
| 2558 | } |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2559 | label_ent->label = label; |
| 2560 | |
| 2561 | mutex_lock(&nd_mapping->lock); |
| 2562 | list_add_tail(&label_ent->list, &nd_mapping->labels); |
| 2563 | mutex_unlock(&nd_mapping->lock); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2564 | } |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2565 | |
Dan Williams | a2d1c7a | 2019-09-05 21:15:57 +0530 | [diff] [blame] | 2566 | if (j < count) |
| 2567 | break; |
| 2568 | } |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2569 | |
Dan Williams | a2d1c7a | 2019-09-05 21:15:57 +0530 | [diff] [blame] | 2570 | if (i < nd_region->ndr_mappings) { |
| 2571 | deactivate_labels(nd_region); |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 2572 | return -ENOMEM; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2573 | } |
| 2574 | |
Dan Williams | a2d1c7a | 2019-09-05 21:15:57 +0530 | [diff] [blame] | 2575 | return devm_add_action_or_reset(&nd_region->dev, deactivate_labels, |
| 2576 | nd_region); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2577 | } |
| 2578 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2579 | int nd_region_register_namespaces(struct nd_region *nd_region, int *err) |
| 2580 | { |
| 2581 | struct device **devs = NULL; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2582 | int i, rc = 0, type; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2583 | |
| 2584 | *err = 0; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2585 | nvdimm_bus_lock(&nd_region->dev); |
| 2586 | rc = init_active_labels(nd_region); |
| 2587 | if (rc) { |
| 2588 | nvdimm_bus_unlock(&nd_region->dev); |
| 2589 | return rc; |
| 2590 | } |
| 2591 | |
| 2592 | type = nd_region_to_nstype(nd_region); |
| 2593 | switch (type) { |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2594 | case ND_DEVICE_NAMESPACE_IO: |
| 2595 | devs = create_namespace_io(nd_region); |
| 2596 | break; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2597 | case ND_DEVICE_NAMESPACE_PMEM: |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2598 | case ND_DEVICE_NAMESPACE_BLK: |
Dan Williams | 8a5f50d | 2016-09-22 15:42:59 -0700 | [diff] [blame] | 2599 | devs = create_namespaces(nd_region); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2600 | break; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2601 | default: |
| 2602 | break; |
| 2603 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 2604 | nvdimm_bus_unlock(&nd_region->dev); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2605 | |
| 2606 | if (!devs) |
| 2607 | return -ENODEV; |
| 2608 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2609 | for (i = 0; devs[i]; i++) { |
| 2610 | struct device *dev = devs[i]; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2611 | int id; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2612 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2613 | if (type == ND_DEVICE_NAMESPACE_BLK) { |
| 2614 | struct nd_namespace_blk *nsblk; |
| 2615 | |
| 2616 | nsblk = to_nd_namespace_blk(dev); |
| 2617 | id = ida_simple_get(&nd_region->ns_ida, 0, 0, |
| 2618 | GFP_KERNEL); |
| 2619 | nsblk->id = id; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 2620 | } else if (type == ND_DEVICE_NAMESPACE_PMEM) { |
| 2621 | struct nd_namespace_pmem *nspm; |
| 2622 | |
| 2623 | nspm = to_nd_namespace_pmem(dev); |
| 2624 | id = ida_simple_get(&nd_region->ns_ida, 0, 0, |
| 2625 | GFP_KERNEL); |
| 2626 | nspm->id = id; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2627 | } else |
| 2628 | id = i; |
| 2629 | |
| 2630 | if (id < 0) |
| 2631 | break; |
| 2632 | dev_set_name(dev, "namespace%d.%d", nd_region->id, id); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2633 | nd_device_register(dev); |
| 2634 | } |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2635 | if (i) |
| 2636 | nd_region->ns_seed = devs[0]; |
| 2637 | |
| 2638 | if (devs[i]) { |
| 2639 | int j; |
| 2640 | |
| 2641 | for (j = i; devs[j]; j++) { |
| 2642 | struct device *dev = devs[j]; |
| 2643 | |
| 2644 | device_initialize(dev); |
| 2645 | put_device(dev); |
| 2646 | } |
| 2647 | *err = j - i; |
| 2648 | /* |
| 2649 | * All of the namespaces we tried to register failed, so |
| 2650 | * fail region activation. |
| 2651 | */ |
| 2652 | if (*err == 0) |
| 2653 | rc = -ENODEV; |
| 2654 | } |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2655 | kfree(devs); |
| 2656 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 2657 | if (rc == -ENODEV) |
| 2658 | return rc; |
| 2659 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2660 | return i; |
| 2661 | } |