Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 13 | #include <linux/scatterlist.h> |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 14 | #include <linux/highmem.h> |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 15 | #include <linux/sched.h> |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 16 | #include <linux/slab.h> |
Dan Williams | 0c27af6 | 2016-05-27 09:23:01 -0700 | [diff] [blame] | 17 | #include <linux/hash.h> |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 18 | #include <linux/pmem.h> |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 19 | #include <linux/sort.h> |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 20 | #include <linux/io.h> |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 21 | #include <linux/nd.h> |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 22 | #include "nd-core.h" |
| 23 | #include "nd.h" |
| 24 | |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 25 | /* |
| 26 | * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is |
| 27 | * irrelevant. |
| 28 | */ |
| 29 | #include <linux/io-64-nonatomic-hi-lo.h> |
| 30 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 31 | static DEFINE_IDA(region_ida); |
Dan Williams | 0c27af6 | 2016-05-27 09:23:01 -0700 | [diff] [blame] | 32 | static DEFINE_PER_CPU(int, flush_idx); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 33 | |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 34 | static int nvdimm_map_flush(struct device *dev, struct nvdimm *nvdimm, int dimm, |
| 35 | struct nd_region_data *ndrd) |
| 36 | { |
| 37 | int i, j; |
| 38 | |
| 39 | dev_dbg(dev, "%s: map %d flush address%s\n", nvdimm_name(nvdimm), |
| 40 | nvdimm->num_flush, nvdimm->num_flush == 1 ? "" : "es"); |
| 41 | for (i = 0; i < nvdimm->num_flush; i++) { |
| 42 | struct resource *res = &nvdimm->flush_wpq[i]; |
| 43 | unsigned long pfn = PHYS_PFN(res->start); |
| 44 | void __iomem *flush_page; |
| 45 | |
| 46 | /* check if flush hints share a page */ |
| 47 | for (j = 0; j < i; j++) { |
| 48 | struct resource *res_j = &nvdimm->flush_wpq[j]; |
| 49 | unsigned long pfn_j = PHYS_PFN(res_j->start); |
| 50 | |
| 51 | if (pfn == pfn_j) |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | if (j < i) |
| 56 | flush_page = (void __iomem *) ((unsigned long) |
| 57 | ndrd->flush_wpq[dimm][j] & PAGE_MASK); |
| 58 | else |
| 59 | flush_page = devm_nvdimm_ioremap(dev, |
| 60 | PHYS_PFN(pfn), PAGE_SIZE); |
| 61 | if (!flush_page) |
| 62 | return -ENXIO; |
| 63 | ndrd->flush_wpq[dimm][i] = flush_page |
| 64 | + (res->start & ~PAGE_MASK); |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int nd_region_activate(struct nd_region *nd_region) |
| 71 | { |
Dan Williams | 0c27af6 | 2016-05-27 09:23:01 -0700 | [diff] [blame] | 72 | int i, num_flush = 0; |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 73 | struct nd_region_data *ndrd; |
| 74 | struct device *dev = &nd_region->dev; |
| 75 | size_t flush_data_size = sizeof(void *); |
| 76 | |
| 77 | nvdimm_bus_lock(&nd_region->dev); |
| 78 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 79 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 80 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 81 | |
| 82 | /* at least one null hint slot per-dimm for the "no-hint" case */ |
| 83 | flush_data_size += sizeof(void *); |
Dan Williams | 0c27af6 | 2016-05-27 09:23:01 -0700 | [diff] [blame] | 84 | num_flush = min_not_zero(num_flush, nvdimm->num_flush); |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 85 | if (!nvdimm->num_flush) |
| 86 | continue; |
| 87 | flush_data_size += nvdimm->num_flush * sizeof(void *); |
| 88 | } |
| 89 | nvdimm_bus_unlock(&nd_region->dev); |
| 90 | |
| 91 | ndrd = devm_kzalloc(dev, sizeof(*ndrd) + flush_data_size, GFP_KERNEL); |
| 92 | if (!ndrd) |
| 93 | return -ENOMEM; |
| 94 | dev_set_drvdata(dev, ndrd); |
| 95 | |
Dan Williams | 0c27af6 | 2016-05-27 09:23:01 -0700 | [diff] [blame] | 96 | ndrd->flush_mask = (1 << ilog2(num_flush)) - 1; |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 97 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 98 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 99 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 100 | int rc = nvdimm_map_flush(&nd_region->dev, nvdimm, i, ndrd); |
| 101 | |
| 102 | if (rc) |
| 103 | return rc; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 109 | static void nd_region_release(struct device *dev) |
| 110 | { |
| 111 | struct nd_region *nd_region = to_nd_region(dev); |
| 112 | u16 i; |
| 113 | |
| 114 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 115 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 116 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 117 | |
| 118 | put_device(&nvdimm->dev); |
| 119 | } |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 120 | free_percpu(nd_region->lane); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 121 | ida_simple_remove(®ion_ida, nd_region->id); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 122 | if (is_nd_blk(dev)) |
| 123 | kfree(to_nd_blk_region(dev)); |
| 124 | else |
| 125 | kfree(nd_region); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static struct device_type nd_blk_device_type = { |
| 129 | .name = "nd_blk", |
| 130 | .release = nd_region_release, |
| 131 | }; |
| 132 | |
| 133 | static struct device_type nd_pmem_device_type = { |
| 134 | .name = "nd_pmem", |
| 135 | .release = nd_region_release, |
| 136 | }; |
| 137 | |
| 138 | static struct device_type nd_volatile_device_type = { |
| 139 | .name = "nd_volatile", |
| 140 | .release = nd_region_release, |
| 141 | }; |
| 142 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 143 | bool is_nd_pmem(struct device *dev) |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 144 | { |
| 145 | return dev ? dev->type == &nd_pmem_device_type : false; |
| 146 | } |
| 147 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 148 | bool is_nd_blk(struct device *dev) |
| 149 | { |
| 150 | return dev ? dev->type == &nd_blk_device_type : false; |
| 151 | } |
| 152 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 153 | struct nd_region *to_nd_region(struct device *dev) |
| 154 | { |
| 155 | struct nd_region *nd_region = container_of(dev, struct nd_region, dev); |
| 156 | |
| 157 | WARN_ON(dev->type->release != nd_region_release); |
| 158 | return nd_region; |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(to_nd_region); |
| 161 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 162 | struct nd_blk_region *to_nd_blk_region(struct device *dev) |
| 163 | { |
| 164 | struct nd_region *nd_region = to_nd_region(dev); |
| 165 | |
| 166 | WARN_ON(!is_nd_blk(dev)); |
| 167 | return container_of(nd_region, struct nd_blk_region, nd_region); |
| 168 | } |
| 169 | EXPORT_SYMBOL_GPL(to_nd_blk_region); |
| 170 | |
| 171 | void *nd_region_provider_data(struct nd_region *nd_region) |
| 172 | { |
| 173 | return nd_region->provider_data; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(nd_region_provider_data); |
| 176 | |
| 177 | void *nd_blk_region_provider_data(struct nd_blk_region *ndbr) |
| 178 | { |
| 179 | return ndbr->blk_provider_data; |
| 180 | } |
| 181 | EXPORT_SYMBOL_GPL(nd_blk_region_provider_data); |
| 182 | |
| 183 | void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data) |
| 184 | { |
| 185 | ndbr->blk_provider_data = data; |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data); |
| 188 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 189 | /** |
| 190 | * nd_region_to_nstype() - region to an integer namespace type |
| 191 | * @nd_region: region-device to interrogate |
| 192 | * |
| 193 | * This is the 'nstype' attribute of a region as well, an input to the |
| 194 | * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match |
| 195 | * namespace devices with namespace drivers. |
| 196 | */ |
| 197 | int nd_region_to_nstype(struct nd_region *nd_region) |
| 198 | { |
| 199 | if (is_nd_pmem(&nd_region->dev)) { |
| 200 | u16 i, alias; |
| 201 | |
| 202 | for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) { |
| 203 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 204 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 205 | |
| 206 | if (nvdimm->flags & NDD_ALIASING) |
| 207 | alias++; |
| 208 | } |
| 209 | if (alias) |
| 210 | return ND_DEVICE_NAMESPACE_PMEM; |
| 211 | else |
| 212 | return ND_DEVICE_NAMESPACE_IO; |
| 213 | } else if (is_nd_blk(&nd_region->dev)) { |
| 214 | return ND_DEVICE_NAMESPACE_BLK; |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 219 | EXPORT_SYMBOL(nd_region_to_nstype); |
| 220 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 221 | static ssize_t size_show(struct device *dev, |
| 222 | struct device_attribute *attr, char *buf) |
| 223 | { |
| 224 | struct nd_region *nd_region = to_nd_region(dev); |
| 225 | unsigned long long size = 0; |
| 226 | |
| 227 | if (is_nd_pmem(dev)) { |
| 228 | size = nd_region->ndr_size; |
| 229 | } else if (nd_region->ndr_mappings == 1) { |
| 230 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
| 231 | |
| 232 | size = nd_mapping->size; |
| 233 | } |
| 234 | |
| 235 | return sprintf(buf, "%llu\n", size); |
| 236 | } |
| 237 | static DEVICE_ATTR_RO(size); |
| 238 | |
| 239 | static ssize_t mappings_show(struct device *dev, |
| 240 | struct device_attribute *attr, char *buf) |
| 241 | { |
| 242 | struct nd_region *nd_region = to_nd_region(dev); |
| 243 | |
| 244 | return sprintf(buf, "%d\n", nd_region->ndr_mappings); |
| 245 | } |
| 246 | static DEVICE_ATTR_RO(mappings); |
| 247 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 248 | static ssize_t nstype_show(struct device *dev, |
| 249 | struct device_attribute *attr, char *buf) |
| 250 | { |
| 251 | struct nd_region *nd_region = to_nd_region(dev); |
| 252 | |
| 253 | return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); |
| 254 | } |
| 255 | static DEVICE_ATTR_RO(nstype); |
| 256 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 257 | static ssize_t set_cookie_show(struct device *dev, |
| 258 | struct device_attribute *attr, char *buf) |
| 259 | { |
| 260 | struct nd_region *nd_region = to_nd_region(dev); |
| 261 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
| 262 | |
| 263 | if (is_nd_pmem(dev) && nd_set) |
| 264 | /* pass, should be precluded by region_visible */; |
| 265 | else |
| 266 | return -ENXIO; |
| 267 | |
| 268 | return sprintf(buf, "%#llx\n", nd_set->cookie); |
| 269 | } |
| 270 | static DEVICE_ATTR_RO(set_cookie); |
| 271 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 272 | resource_size_t nd_region_available_dpa(struct nd_region *nd_region) |
| 273 | { |
| 274 | resource_size_t blk_max_overlap = 0, available, overlap; |
| 275 | int i; |
| 276 | |
| 277 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); |
| 278 | |
| 279 | retry: |
| 280 | available = 0; |
| 281 | overlap = blk_max_overlap; |
| 282 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 283 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
| 284 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
| 285 | |
| 286 | /* if a dimm is disabled the available capacity is zero */ |
| 287 | if (!ndd) |
| 288 | return 0; |
| 289 | |
| 290 | if (is_nd_pmem(&nd_region->dev)) { |
| 291 | available += nd_pmem_available_dpa(nd_region, |
| 292 | nd_mapping, &overlap); |
| 293 | if (overlap > blk_max_overlap) { |
| 294 | blk_max_overlap = overlap; |
| 295 | goto retry; |
| 296 | } |
Dan Williams | a1f3e4d | 2016-09-30 17:28:58 -0700 | [diff] [blame^] | 297 | } else if (is_nd_blk(&nd_region->dev)) |
| 298 | available += nd_blk_available_dpa(nd_region); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return available; |
| 302 | } |
| 303 | |
| 304 | static ssize_t available_size_show(struct device *dev, |
| 305 | struct device_attribute *attr, char *buf) |
| 306 | { |
| 307 | struct nd_region *nd_region = to_nd_region(dev); |
| 308 | unsigned long long available = 0; |
| 309 | |
| 310 | /* |
| 311 | * Flush in-flight updates and grab a snapshot of the available |
| 312 | * size. Of course, this value is potentially invalidated the |
| 313 | * memory nvdimm_bus_lock() is dropped, but that's userspace's |
| 314 | * problem to not race itself. |
| 315 | */ |
| 316 | nvdimm_bus_lock(dev); |
| 317 | wait_nvdimm_bus_probe_idle(dev); |
| 318 | available = nd_region_available_dpa(nd_region); |
| 319 | nvdimm_bus_unlock(dev); |
| 320 | |
| 321 | return sprintf(buf, "%llu\n", available); |
| 322 | } |
| 323 | static DEVICE_ATTR_RO(available_size); |
| 324 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 325 | static ssize_t init_namespaces_show(struct device *dev, |
| 326 | struct device_attribute *attr, char *buf) |
| 327 | { |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 328 | struct nd_region_data *ndrd = dev_get_drvdata(dev); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 329 | ssize_t rc; |
| 330 | |
| 331 | nvdimm_bus_lock(dev); |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 332 | if (ndrd) |
| 333 | rc = sprintf(buf, "%d/%d\n", ndrd->ns_active, ndrd->ns_count); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 334 | else |
| 335 | rc = -ENXIO; |
| 336 | nvdimm_bus_unlock(dev); |
| 337 | |
| 338 | return rc; |
| 339 | } |
| 340 | static DEVICE_ATTR_RO(init_namespaces); |
| 341 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 342 | static ssize_t namespace_seed_show(struct device *dev, |
| 343 | struct device_attribute *attr, char *buf) |
| 344 | { |
| 345 | struct nd_region *nd_region = to_nd_region(dev); |
| 346 | ssize_t rc; |
| 347 | |
| 348 | nvdimm_bus_lock(dev); |
| 349 | if (nd_region->ns_seed) |
| 350 | rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed)); |
| 351 | else |
| 352 | rc = sprintf(buf, "\n"); |
| 353 | nvdimm_bus_unlock(dev); |
| 354 | return rc; |
| 355 | } |
| 356 | static DEVICE_ATTR_RO(namespace_seed); |
| 357 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 358 | static ssize_t btt_seed_show(struct device *dev, |
| 359 | struct device_attribute *attr, char *buf) |
| 360 | { |
| 361 | struct nd_region *nd_region = to_nd_region(dev); |
| 362 | ssize_t rc; |
| 363 | |
| 364 | nvdimm_bus_lock(dev); |
| 365 | if (nd_region->btt_seed) |
| 366 | rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed)); |
| 367 | else |
| 368 | rc = sprintf(buf, "\n"); |
| 369 | nvdimm_bus_unlock(dev); |
| 370 | |
| 371 | return rc; |
| 372 | } |
| 373 | static DEVICE_ATTR_RO(btt_seed); |
| 374 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 375 | static ssize_t pfn_seed_show(struct device *dev, |
| 376 | struct device_attribute *attr, char *buf) |
| 377 | { |
| 378 | struct nd_region *nd_region = to_nd_region(dev); |
| 379 | ssize_t rc; |
| 380 | |
| 381 | nvdimm_bus_lock(dev); |
| 382 | if (nd_region->pfn_seed) |
| 383 | rc = sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed)); |
| 384 | else |
| 385 | rc = sprintf(buf, "\n"); |
| 386 | nvdimm_bus_unlock(dev); |
| 387 | |
| 388 | return rc; |
| 389 | } |
| 390 | static DEVICE_ATTR_RO(pfn_seed); |
| 391 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 392 | static ssize_t dax_seed_show(struct device *dev, |
| 393 | struct device_attribute *attr, char *buf) |
| 394 | { |
| 395 | struct nd_region *nd_region = to_nd_region(dev); |
| 396 | ssize_t rc; |
| 397 | |
| 398 | nvdimm_bus_lock(dev); |
| 399 | if (nd_region->dax_seed) |
| 400 | rc = sprintf(buf, "%s\n", dev_name(nd_region->dax_seed)); |
| 401 | else |
| 402 | rc = sprintf(buf, "\n"); |
| 403 | nvdimm_bus_unlock(dev); |
| 404 | |
| 405 | return rc; |
| 406 | } |
| 407 | static DEVICE_ATTR_RO(dax_seed); |
| 408 | |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 409 | static ssize_t read_only_show(struct device *dev, |
| 410 | struct device_attribute *attr, char *buf) |
| 411 | { |
| 412 | struct nd_region *nd_region = to_nd_region(dev); |
| 413 | |
| 414 | return sprintf(buf, "%d\n", nd_region->ro); |
| 415 | } |
| 416 | |
| 417 | static ssize_t read_only_store(struct device *dev, |
| 418 | struct device_attribute *attr, const char *buf, size_t len) |
| 419 | { |
| 420 | bool ro; |
| 421 | int rc = strtobool(buf, &ro); |
| 422 | struct nd_region *nd_region = to_nd_region(dev); |
| 423 | |
| 424 | if (rc) |
| 425 | return rc; |
| 426 | |
| 427 | nd_region->ro = ro; |
| 428 | return len; |
| 429 | } |
| 430 | static DEVICE_ATTR_RW(read_only); |
| 431 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 432 | static struct attribute *nd_region_attributes[] = { |
| 433 | &dev_attr_size.attr, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 434 | &dev_attr_nstype.attr, |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 435 | &dev_attr_mappings.attr, |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 436 | &dev_attr_btt_seed.attr, |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 437 | &dev_attr_pfn_seed.attr, |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 438 | &dev_attr_dax_seed.attr, |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 439 | &dev_attr_read_only.attr, |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 440 | &dev_attr_set_cookie.attr, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 441 | &dev_attr_available_size.attr, |
| 442 | &dev_attr_namespace_seed.attr, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 443 | &dev_attr_init_namespaces.attr, |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 444 | NULL, |
| 445 | }; |
| 446 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 447 | static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n) |
| 448 | { |
| 449 | struct device *dev = container_of(kobj, typeof(*dev), kobj); |
| 450 | struct nd_region *nd_region = to_nd_region(dev); |
| 451 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 452 | int type = nd_region_to_nstype(nd_region); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 453 | |
Dmitry Krivenok | 6bb691a | 2015-12-02 09:39:29 +0300 | [diff] [blame] | 454 | if (!is_nd_pmem(dev) && a == &dev_attr_pfn_seed.attr) |
| 455 | return 0; |
| 456 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 457 | if (!is_nd_pmem(dev) && a == &dev_attr_dax_seed.attr) |
| 458 | return 0; |
| 459 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 460 | if (a != &dev_attr_set_cookie.attr |
| 461 | && a != &dev_attr_available_size.attr) |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 462 | return a->mode; |
| 463 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 464 | if ((type == ND_DEVICE_NAMESPACE_PMEM |
| 465 | || type == ND_DEVICE_NAMESPACE_BLK) |
| 466 | && a == &dev_attr_available_size.attr) |
| 467 | return a->mode; |
| 468 | else if (is_nd_pmem(dev) && nd_set) |
| 469 | return a->mode; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 474 | struct attribute_group nd_region_attribute_group = { |
| 475 | .attrs = nd_region_attributes, |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 476 | .is_visible = region_visible, |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 477 | }; |
| 478 | EXPORT_SYMBOL_GPL(nd_region_attribute_group); |
| 479 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 480 | u64 nd_region_interleave_set_cookie(struct nd_region *nd_region) |
| 481 | { |
| 482 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
| 483 | |
| 484 | if (nd_set) |
| 485 | return nd_set->cookie; |
| 486 | return 0; |
| 487 | } |
| 488 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 489 | void nd_mapping_free_labels(struct nd_mapping *nd_mapping) |
| 490 | { |
| 491 | struct nd_label_ent *label_ent, *e; |
| 492 | |
| 493 | WARN_ON(!mutex_is_locked(&nd_mapping->lock)); |
| 494 | list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) { |
| 495 | list_del(&label_ent->list); |
| 496 | kfree(label_ent); |
| 497 | } |
| 498 | } |
| 499 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 500 | /* |
| 501 | * Upon successful probe/remove, take/release a reference on the |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 502 | * associated interleave set (if present), and plant new btt + namespace |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 503 | * seeds. Also, on the removal of a BLK region, notify the provider to |
| 504 | * disable the region. |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 505 | */ |
| 506 | static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus, |
| 507 | struct device *dev, bool probe) |
| 508 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 509 | struct nd_region *nd_region; |
| 510 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 511 | if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) { |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 512 | int i; |
| 513 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 514 | nd_region = to_nd_region(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 515 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
| 516 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 517 | struct nvdimm_drvdata *ndd = nd_mapping->ndd; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 518 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 519 | |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 520 | mutex_lock(&nd_mapping->lock); |
| 521 | nd_mapping_free_labels(nd_mapping); |
| 522 | mutex_unlock(&nd_mapping->lock); |
| 523 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 524 | put_ndd(ndd); |
| 525 | nd_mapping->ndd = NULL; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 526 | if (ndd) |
| 527 | atomic_dec(&nvdimm->busy); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 528 | } |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 529 | |
| 530 | if (is_nd_pmem(dev)) |
| 531 | return; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 532 | } |
| 533 | if (dev->parent && is_nd_blk(dev->parent) && probe) { |
| 534 | nd_region = to_nd_region(dev->parent); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 535 | nvdimm_bus_lock(dev); |
| 536 | if (nd_region->ns_seed == dev) |
| 537 | nd_region_create_blk_seed(nd_region); |
| 538 | nvdimm_bus_unlock(dev); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 539 | } |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 540 | if (is_nd_btt(dev) && probe) { |
Dan Williams | 8ca2435 | 2015-07-24 23:42:34 -0400 | [diff] [blame] | 541 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 542 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 543 | nd_region = to_nd_region(dev->parent); |
| 544 | nvdimm_bus_lock(dev); |
| 545 | if (nd_region->btt_seed == dev) |
| 546 | nd_region_create_btt_seed(nd_region); |
Dan Williams | 8ca2435 | 2015-07-24 23:42:34 -0400 | [diff] [blame] | 547 | if (nd_region->ns_seed == &nd_btt->ndns->dev && |
| 548 | is_nd_blk(dev->parent)) |
| 549 | nd_region_create_blk_seed(nd_region); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 550 | nvdimm_bus_unlock(dev); |
| 551 | } |
Dan Williams | 2dc4333 | 2015-12-13 11:41:36 -0800 | [diff] [blame] | 552 | if (is_nd_pfn(dev) && probe) { |
| 553 | nd_region = to_nd_region(dev->parent); |
| 554 | nvdimm_bus_lock(dev); |
| 555 | if (nd_region->pfn_seed == dev) |
| 556 | nd_region_create_pfn_seed(nd_region); |
| 557 | nvdimm_bus_unlock(dev); |
| 558 | } |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 559 | if (is_nd_dax(dev) && probe) { |
| 560 | nd_region = to_nd_region(dev->parent); |
| 561 | nvdimm_bus_lock(dev); |
| 562 | if (nd_region->dax_seed == dev) |
| 563 | nd_region_create_dax_seed(nd_region); |
| 564 | nvdimm_bus_unlock(dev); |
| 565 | } |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev) |
| 569 | { |
| 570 | nd_region_notify_driver_action(nvdimm_bus, dev, true); |
| 571 | } |
| 572 | |
| 573 | void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev) |
| 574 | { |
| 575 | nd_region_notify_driver_action(nvdimm_bus, dev, false); |
| 576 | } |
| 577 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 578 | static ssize_t mappingN(struct device *dev, char *buf, int n) |
| 579 | { |
| 580 | struct nd_region *nd_region = to_nd_region(dev); |
| 581 | struct nd_mapping *nd_mapping; |
| 582 | struct nvdimm *nvdimm; |
| 583 | |
| 584 | if (n >= nd_region->ndr_mappings) |
| 585 | return -ENXIO; |
| 586 | nd_mapping = &nd_region->mapping[n]; |
| 587 | nvdimm = nd_mapping->nvdimm; |
| 588 | |
| 589 | return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev), |
| 590 | nd_mapping->start, nd_mapping->size); |
| 591 | } |
| 592 | |
| 593 | #define REGION_MAPPING(idx) \ |
| 594 | static ssize_t mapping##idx##_show(struct device *dev, \ |
| 595 | struct device_attribute *attr, char *buf) \ |
| 596 | { \ |
| 597 | return mappingN(dev, buf, idx); \ |
| 598 | } \ |
| 599 | static DEVICE_ATTR_RO(mapping##idx) |
| 600 | |
| 601 | /* |
| 602 | * 32 should be enough for a while, even in the presence of socket |
| 603 | * interleave a 32-way interleave set is a degenerate case. |
| 604 | */ |
| 605 | REGION_MAPPING(0); |
| 606 | REGION_MAPPING(1); |
| 607 | REGION_MAPPING(2); |
| 608 | REGION_MAPPING(3); |
| 609 | REGION_MAPPING(4); |
| 610 | REGION_MAPPING(5); |
| 611 | REGION_MAPPING(6); |
| 612 | REGION_MAPPING(7); |
| 613 | REGION_MAPPING(8); |
| 614 | REGION_MAPPING(9); |
| 615 | REGION_MAPPING(10); |
| 616 | REGION_MAPPING(11); |
| 617 | REGION_MAPPING(12); |
| 618 | REGION_MAPPING(13); |
| 619 | REGION_MAPPING(14); |
| 620 | REGION_MAPPING(15); |
| 621 | REGION_MAPPING(16); |
| 622 | REGION_MAPPING(17); |
| 623 | REGION_MAPPING(18); |
| 624 | REGION_MAPPING(19); |
| 625 | REGION_MAPPING(20); |
| 626 | REGION_MAPPING(21); |
| 627 | REGION_MAPPING(22); |
| 628 | REGION_MAPPING(23); |
| 629 | REGION_MAPPING(24); |
| 630 | REGION_MAPPING(25); |
| 631 | REGION_MAPPING(26); |
| 632 | REGION_MAPPING(27); |
| 633 | REGION_MAPPING(28); |
| 634 | REGION_MAPPING(29); |
| 635 | REGION_MAPPING(30); |
| 636 | REGION_MAPPING(31); |
| 637 | |
| 638 | static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n) |
| 639 | { |
| 640 | struct device *dev = container_of(kobj, struct device, kobj); |
| 641 | struct nd_region *nd_region = to_nd_region(dev); |
| 642 | |
| 643 | if (n < nd_region->ndr_mappings) |
| 644 | return a->mode; |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static struct attribute *mapping_attributes[] = { |
| 649 | &dev_attr_mapping0.attr, |
| 650 | &dev_attr_mapping1.attr, |
| 651 | &dev_attr_mapping2.attr, |
| 652 | &dev_attr_mapping3.attr, |
| 653 | &dev_attr_mapping4.attr, |
| 654 | &dev_attr_mapping5.attr, |
| 655 | &dev_attr_mapping6.attr, |
| 656 | &dev_attr_mapping7.attr, |
| 657 | &dev_attr_mapping8.attr, |
| 658 | &dev_attr_mapping9.attr, |
| 659 | &dev_attr_mapping10.attr, |
| 660 | &dev_attr_mapping11.attr, |
| 661 | &dev_attr_mapping12.attr, |
| 662 | &dev_attr_mapping13.attr, |
| 663 | &dev_attr_mapping14.attr, |
| 664 | &dev_attr_mapping15.attr, |
| 665 | &dev_attr_mapping16.attr, |
| 666 | &dev_attr_mapping17.attr, |
| 667 | &dev_attr_mapping18.attr, |
| 668 | &dev_attr_mapping19.attr, |
| 669 | &dev_attr_mapping20.attr, |
| 670 | &dev_attr_mapping21.attr, |
| 671 | &dev_attr_mapping22.attr, |
| 672 | &dev_attr_mapping23.attr, |
| 673 | &dev_attr_mapping24.attr, |
| 674 | &dev_attr_mapping25.attr, |
| 675 | &dev_attr_mapping26.attr, |
| 676 | &dev_attr_mapping27.attr, |
| 677 | &dev_attr_mapping28.attr, |
| 678 | &dev_attr_mapping29.attr, |
| 679 | &dev_attr_mapping30.attr, |
| 680 | &dev_attr_mapping31.attr, |
| 681 | NULL, |
| 682 | }; |
| 683 | |
| 684 | struct attribute_group nd_mapping_attribute_group = { |
| 685 | .is_visible = mapping_visible, |
| 686 | .attrs = mapping_attributes, |
| 687 | }; |
| 688 | EXPORT_SYMBOL_GPL(nd_mapping_attribute_group); |
| 689 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 690 | int nd_blk_region_init(struct nd_region *nd_region) |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 691 | { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 692 | struct device *dev = &nd_region->dev; |
| 693 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 694 | |
| 695 | if (!is_nd_blk(dev)) |
| 696 | return 0; |
| 697 | |
| 698 | if (nd_region->ndr_mappings < 1) { |
| 699 | dev_err(dev, "invalid BLK region\n"); |
| 700 | return -ENXIO; |
| 701 | } |
| 702 | |
| 703 | return to_nd_blk_region(dev)->enable(nvdimm_bus, dev); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 704 | } |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 705 | |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 706 | /** |
| 707 | * nd_region_acquire_lane - allocate and lock a lane |
| 708 | * @nd_region: region id and number of lanes possible |
| 709 | * |
| 710 | * A lane correlates to a BLK-data-window and/or a log slot in the BTT. |
| 711 | * We optimize for the common case where there are 256 lanes, one |
| 712 | * per-cpu. For larger systems we need to lock to share lanes. For now |
| 713 | * this implementation assumes the cost of maintaining an allocator for |
| 714 | * free lanes is on the order of the lock hold time, so it implements a |
| 715 | * static lane = cpu % num_lanes mapping. |
| 716 | * |
| 717 | * In the case of a BTT instance on top of a BLK namespace a lane may be |
| 718 | * acquired recursively. We lock on the first instance. |
| 719 | * |
| 720 | * In the case of a BTT instance on top of PMEM, we only acquire a lane |
| 721 | * for the BTT metadata updates. |
| 722 | */ |
| 723 | unsigned int nd_region_acquire_lane(struct nd_region *nd_region) |
| 724 | { |
| 725 | unsigned int cpu, lane; |
| 726 | |
| 727 | cpu = get_cpu(); |
| 728 | if (nd_region->num_lanes < nr_cpu_ids) { |
| 729 | struct nd_percpu_lane *ndl_lock, *ndl_count; |
| 730 | |
| 731 | lane = cpu % nd_region->num_lanes; |
| 732 | ndl_count = per_cpu_ptr(nd_region->lane, cpu); |
| 733 | ndl_lock = per_cpu_ptr(nd_region->lane, lane); |
| 734 | if (ndl_count->count++ == 0) |
| 735 | spin_lock(&ndl_lock->lock); |
| 736 | } else |
| 737 | lane = cpu; |
| 738 | |
| 739 | return lane; |
| 740 | } |
| 741 | EXPORT_SYMBOL(nd_region_acquire_lane); |
| 742 | |
| 743 | void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane) |
| 744 | { |
| 745 | if (nd_region->num_lanes < nr_cpu_ids) { |
| 746 | unsigned int cpu = get_cpu(); |
| 747 | struct nd_percpu_lane *ndl_lock, *ndl_count; |
| 748 | |
| 749 | ndl_count = per_cpu_ptr(nd_region->lane, cpu); |
| 750 | ndl_lock = per_cpu_ptr(nd_region->lane, lane); |
| 751 | if (--ndl_count->count == 0) |
| 752 | spin_unlock(&ndl_lock->lock); |
| 753 | put_cpu(); |
| 754 | } |
| 755 | put_cpu(); |
| 756 | } |
| 757 | EXPORT_SYMBOL(nd_region_release_lane); |
| 758 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 759 | static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus, |
| 760 | struct nd_region_desc *ndr_desc, struct device_type *dev_type, |
| 761 | const char *caller) |
| 762 | { |
| 763 | struct nd_region *nd_region; |
| 764 | struct device *dev; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 765 | void *region_buf; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 766 | unsigned int i; |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 767 | int ro = 0; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 768 | |
| 769 | for (i = 0; i < ndr_desc->num_mappings; i++) { |
Dan Williams | 44c462e | 2016-09-19 16:38:50 -0700 | [diff] [blame] | 770 | struct nd_mapping_desc *mapping = &ndr_desc->mapping[i]; |
| 771 | struct nvdimm *nvdimm = mapping->nvdimm; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 772 | |
Dan Williams | 44c462e | 2016-09-19 16:38:50 -0700 | [diff] [blame] | 773 | if ((mapping->start | mapping->size) % SZ_4K) { |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 774 | dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n", |
| 775 | caller, dev_name(&nvdimm->dev), i); |
| 776 | |
| 777 | return NULL; |
| 778 | } |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 779 | |
| 780 | if (nvdimm->flags & NDD_UNARMED) |
| 781 | ro = 1; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 784 | if (dev_type == &nd_blk_device_type) { |
| 785 | struct nd_blk_region_desc *ndbr_desc; |
| 786 | struct nd_blk_region *ndbr; |
| 787 | |
| 788 | ndbr_desc = to_blk_region_desc(ndr_desc); |
| 789 | ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping) |
| 790 | * ndr_desc->num_mappings, |
| 791 | GFP_KERNEL); |
| 792 | if (ndbr) { |
| 793 | nd_region = &ndbr->nd_region; |
| 794 | ndbr->enable = ndbr_desc->enable; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 795 | ndbr->do_io = ndbr_desc->do_io; |
| 796 | } |
| 797 | region_buf = ndbr; |
| 798 | } else { |
| 799 | nd_region = kzalloc(sizeof(struct nd_region) |
| 800 | + sizeof(struct nd_mapping) |
| 801 | * ndr_desc->num_mappings, |
| 802 | GFP_KERNEL); |
| 803 | region_buf = nd_region; |
| 804 | } |
| 805 | |
| 806 | if (!region_buf) |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 807 | return NULL; |
| 808 | nd_region->id = ida_simple_get(®ion_ida, 0, 0, GFP_KERNEL); |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 809 | if (nd_region->id < 0) |
| 810 | goto err_id; |
| 811 | |
| 812 | nd_region->lane = alloc_percpu(struct nd_percpu_lane); |
| 813 | if (!nd_region->lane) |
| 814 | goto err_percpu; |
| 815 | |
| 816 | for (i = 0; i < nr_cpu_ids; i++) { |
| 817 | struct nd_percpu_lane *ndl; |
| 818 | |
| 819 | ndl = per_cpu_ptr(nd_region->lane, i); |
| 820 | spin_lock_init(&ndl->lock); |
| 821 | ndl->count = 0; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 822 | } |
| 823 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 824 | for (i = 0; i < ndr_desc->num_mappings; i++) { |
Dan Williams | 44c462e | 2016-09-19 16:38:50 -0700 | [diff] [blame] | 825 | struct nd_mapping_desc *mapping = &ndr_desc->mapping[i]; |
| 826 | struct nvdimm *nvdimm = mapping->nvdimm; |
| 827 | |
| 828 | nd_region->mapping[i].nvdimm = nvdimm; |
| 829 | nd_region->mapping[i].start = mapping->start; |
| 830 | nd_region->mapping[i].size = mapping->size; |
Dan Williams | ae8219f | 2016-09-19 16:04:21 -0700 | [diff] [blame] | 831 | INIT_LIST_HEAD(&nd_region->mapping[i].labels); |
| 832 | mutex_init(&nd_region->mapping[i].lock); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 833 | |
| 834 | get_device(&nvdimm->dev); |
| 835 | } |
| 836 | nd_region->ndr_mappings = ndr_desc->num_mappings; |
| 837 | nd_region->provider_data = ndr_desc->provider_data; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 838 | nd_region->nd_set = ndr_desc->nd_set; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 839 | nd_region->num_lanes = ndr_desc->num_lanes; |
Dan Williams | 004f1af | 2015-08-24 19:20:23 -0400 | [diff] [blame] | 840 | nd_region->flags = ndr_desc->flags; |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 841 | nd_region->ro = ro; |
Toshi Kani | 41d7a6d | 2015-06-19 12:18:33 -0600 | [diff] [blame] | 842 | nd_region->numa_node = ndr_desc->numa_node; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 843 | ida_init(&nd_region->ns_ida); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 844 | ida_init(&nd_region->btt_ida); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 845 | ida_init(&nd_region->pfn_ida); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 846 | ida_init(&nd_region->dax_ida); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 847 | dev = &nd_region->dev; |
| 848 | dev_set_name(dev, "region%d", nd_region->id); |
| 849 | dev->parent = &nvdimm_bus->dev; |
| 850 | dev->type = dev_type; |
| 851 | dev->groups = ndr_desc->attr_groups; |
| 852 | nd_region->ndr_size = resource_size(ndr_desc->res); |
| 853 | nd_region->ndr_start = ndr_desc->res->start; |
| 854 | nd_device_register(dev); |
| 855 | |
| 856 | return nd_region; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 857 | |
| 858 | err_percpu: |
| 859 | ida_simple_remove(®ion_ida, nd_region->id); |
| 860 | err_id: |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 861 | kfree(region_buf); |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 862 | return NULL; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus, |
| 866 | struct nd_region_desc *ndr_desc) |
| 867 | { |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 868 | ndr_desc->num_lanes = ND_MAX_LANES; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 869 | return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type, |
| 870 | __func__); |
| 871 | } |
| 872 | EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create); |
| 873 | |
| 874 | struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus, |
| 875 | struct nd_region_desc *ndr_desc) |
| 876 | { |
| 877 | if (ndr_desc->num_mappings > 1) |
| 878 | return NULL; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 879 | ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 880 | return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type, |
| 881 | __func__); |
| 882 | } |
| 883 | EXPORT_SYMBOL_GPL(nvdimm_blk_region_create); |
| 884 | |
| 885 | struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus, |
| 886 | struct nd_region_desc *ndr_desc) |
| 887 | { |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 888 | ndr_desc->num_lanes = ND_MAX_LANES; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 889 | return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type, |
| 890 | __func__); |
| 891 | } |
| 892 | EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create); |
Dan Williams | b354aba | 2016-05-17 20:24:16 -0700 | [diff] [blame] | 893 | |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 894 | /** |
| 895 | * nvdimm_flush - flush any posted write queues between the cpu and pmem media |
| 896 | * @nd_region: blk or interleaved pmem region |
| 897 | */ |
| 898 | void nvdimm_flush(struct nd_region *nd_region) |
| 899 | { |
| 900 | struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev); |
Dan Williams | 0c27af6 | 2016-05-27 09:23:01 -0700 | [diff] [blame] | 901 | int i, idx; |
| 902 | |
| 903 | /* |
| 904 | * Try to encourage some diversity in flush hint addresses |
| 905 | * across cpus assuming a limited number of flush hints. |
| 906 | */ |
| 907 | idx = this_cpu_read(flush_idx); |
| 908 | idx = this_cpu_add_return(flush_idx, hash_32(current->pid + idx, 8)); |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 909 | |
| 910 | /* |
| 911 | * The first wmb() is needed to 'sfence' all previous writes |
| 912 | * such that they are architecturally visible for the platform |
| 913 | * buffer flush. Note that we've already arranged for pmem |
| 914 | * writes to avoid the cache via arch_memcpy_to_pmem(). The |
| 915 | * final wmb() ensures ordering for the NVDIMM flush write. |
| 916 | */ |
| 917 | wmb(); |
| 918 | for (i = 0; i < nd_region->ndr_mappings; i++) |
| 919 | if (ndrd->flush_wpq[i][0]) |
Dan Williams | 0c27af6 | 2016-05-27 09:23:01 -0700 | [diff] [blame] | 920 | writeq(1, ndrd->flush_wpq[i][idx & ndrd->flush_mask]); |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 921 | wmb(); |
| 922 | } |
| 923 | EXPORT_SYMBOL_GPL(nvdimm_flush); |
| 924 | |
| 925 | /** |
| 926 | * nvdimm_has_flush - determine write flushing requirements |
| 927 | * @nd_region: blk or interleaved pmem region |
| 928 | * |
| 929 | * Returns 1 if writes require flushing |
| 930 | * Returns 0 if writes do not require flushing |
| 931 | * Returns -ENXIO if flushing capability can not be determined |
| 932 | */ |
| 933 | int nvdimm_has_flush(struct nd_region *nd_region) |
| 934 | { |
| 935 | struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev); |
| 936 | int i; |
| 937 | |
| 938 | /* no nvdimm == flushing capability unknown */ |
| 939 | if (nd_region->ndr_mappings == 0) |
| 940 | return -ENXIO; |
| 941 | |
| 942 | for (i = 0; i < nd_region->ndr_mappings; i++) |
| 943 | /* flush hints present, flushing required */ |
| 944 | if (ndrd->flush_wpq[i][0]) |
| 945 | return 1; |
| 946 | |
| 947 | /* |
| 948 | * The platform defines dimm devices without hints, assume |
| 949 | * platform persistence mechanism like ADR |
| 950 | */ |
| 951 | return 0; |
| 952 | } |
| 953 | EXPORT_SYMBOL_GPL(nvdimm_has_flush); |
| 954 | |
Dan Williams | b354aba | 2016-05-17 20:24:16 -0700 | [diff] [blame] | 955 | void __exit nd_region_devs_exit(void) |
| 956 | { |
| 957 | ida_destroy(®ion_ida); |
| 958 | } |