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