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