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