1 /* 2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * General Public License for more details. 12 */ 13 #include <linux/scatterlist.h> 14 #include <linux/highmem.h> 15 #include <linux/sched.h> 16 #include <linux/slab.h> 17 #include <linux/sort.h> 18 #include <linux/io.h> 19 #include <linux/nd.h> 20 #include "nd-core.h" 21 #include "nd.h" 22 23 static DEFINE_IDA(region_ida); 24 25 static void nd_region_release(struct device *dev) 26 { 27 struct nd_region *nd_region = to_nd_region(dev); 28 u16 i; 29 30 for (i = 0; i < nd_region->ndr_mappings; i++) { 31 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 32 struct nvdimm *nvdimm = nd_mapping->nvdimm; 33 34 put_device(&nvdimm->dev); 35 } 36 free_percpu(nd_region->lane); 37 ida_simple_remove(®ion_ida, nd_region->id); 38 if (is_nd_blk(dev)) 39 kfree(to_nd_blk_region(dev)); 40 else 41 kfree(nd_region); 42 } 43 44 static struct device_type nd_blk_device_type = { 45 .name = "nd_blk", 46 .release = nd_region_release, 47 }; 48 49 static struct device_type nd_pmem_device_type = { 50 .name = "nd_pmem", 51 .release = nd_region_release, 52 }; 53 54 static struct device_type nd_volatile_device_type = { 55 .name = "nd_volatile", 56 .release = nd_region_release, 57 }; 58 59 bool is_nd_pmem(struct device *dev) 60 { 61 return dev ? dev->type == &nd_pmem_device_type : false; 62 } 63 64 bool is_nd_blk(struct device *dev) 65 { 66 return dev ? dev->type == &nd_blk_device_type : false; 67 } 68 69 struct nd_region *to_nd_region(struct device *dev) 70 { 71 struct nd_region *nd_region = container_of(dev, struct nd_region, dev); 72 73 WARN_ON(dev->type->release != nd_region_release); 74 return nd_region; 75 } 76 EXPORT_SYMBOL_GPL(to_nd_region); 77 78 struct nd_blk_region *to_nd_blk_region(struct device *dev) 79 { 80 struct nd_region *nd_region = to_nd_region(dev); 81 82 WARN_ON(!is_nd_blk(dev)); 83 return container_of(nd_region, struct nd_blk_region, nd_region); 84 } 85 EXPORT_SYMBOL_GPL(to_nd_blk_region); 86 87 void *nd_region_provider_data(struct nd_region *nd_region) 88 { 89 return nd_region->provider_data; 90 } 91 EXPORT_SYMBOL_GPL(nd_region_provider_data); 92 93 void *nd_blk_region_provider_data(struct nd_blk_region *ndbr) 94 { 95 return ndbr->blk_provider_data; 96 } 97 EXPORT_SYMBOL_GPL(nd_blk_region_provider_data); 98 99 void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data) 100 { 101 ndbr->blk_provider_data = data; 102 } 103 EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data); 104 105 /** 106 * nd_region_to_nstype() - region to an integer namespace type 107 * @nd_region: region-device to interrogate 108 * 109 * This is the 'nstype' attribute of a region as well, an input to the 110 * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match 111 * namespace devices with namespace drivers. 112 */ 113 int nd_region_to_nstype(struct nd_region *nd_region) 114 { 115 if (is_nd_pmem(&nd_region->dev)) { 116 u16 i, alias; 117 118 for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) { 119 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 120 struct nvdimm *nvdimm = nd_mapping->nvdimm; 121 122 if (nvdimm->flags & NDD_ALIASING) 123 alias++; 124 } 125 if (alias) 126 return ND_DEVICE_NAMESPACE_PMEM; 127 else 128 return ND_DEVICE_NAMESPACE_IO; 129 } else if (is_nd_blk(&nd_region->dev)) { 130 return ND_DEVICE_NAMESPACE_BLK; 131 } 132 133 return 0; 134 } 135 EXPORT_SYMBOL(nd_region_to_nstype); 136 137 static int is_uuid_busy(struct device *dev, void *data) 138 { 139 struct nd_region *nd_region = to_nd_region(dev->parent); 140 u8 *uuid = data; 141 142 switch (nd_region_to_nstype(nd_region)) { 143 case ND_DEVICE_NAMESPACE_PMEM: { 144 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 145 146 if (!nspm->uuid) 147 break; 148 if (memcmp(uuid, nspm->uuid, NSLABEL_UUID_LEN) == 0) 149 return -EBUSY; 150 break; 151 } 152 case ND_DEVICE_NAMESPACE_BLK: { 153 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 154 155 if (!nsblk->uuid) 156 break; 157 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) == 0) 158 return -EBUSY; 159 break; 160 } 161 default: 162 break; 163 } 164 165 return 0; 166 } 167 168 static int is_namespace_uuid_busy(struct device *dev, void *data) 169 { 170 if (is_nd_pmem(dev) || is_nd_blk(dev)) 171 return device_for_each_child(dev, data, is_uuid_busy); 172 return 0; 173 } 174 175 /** 176 * nd_is_uuid_unique - verify that no other namespace has @uuid 177 * @dev: any device on a nvdimm_bus 178 * @uuid: uuid to check 179 */ 180 bool nd_is_uuid_unique(struct device *dev, u8 *uuid) 181 { 182 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 183 184 if (!nvdimm_bus) 185 return false; 186 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev)); 187 if (device_for_each_child(&nvdimm_bus->dev, uuid, 188 is_namespace_uuid_busy) != 0) 189 return false; 190 return true; 191 } 192 193 static ssize_t size_show(struct device *dev, 194 struct device_attribute *attr, char *buf) 195 { 196 struct nd_region *nd_region = to_nd_region(dev); 197 unsigned long long size = 0; 198 199 if (is_nd_pmem(dev)) { 200 size = nd_region->ndr_size; 201 } else if (nd_region->ndr_mappings == 1) { 202 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 203 204 size = nd_mapping->size; 205 } 206 207 return sprintf(buf, "%llu\n", size); 208 } 209 static DEVICE_ATTR_RO(size); 210 211 static ssize_t mappings_show(struct device *dev, 212 struct device_attribute *attr, char *buf) 213 { 214 struct nd_region *nd_region = to_nd_region(dev); 215 216 return sprintf(buf, "%d\n", nd_region->ndr_mappings); 217 } 218 static DEVICE_ATTR_RO(mappings); 219 220 static ssize_t nstype_show(struct device *dev, 221 struct device_attribute *attr, char *buf) 222 { 223 struct nd_region *nd_region = to_nd_region(dev); 224 225 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); 226 } 227 static DEVICE_ATTR_RO(nstype); 228 229 static ssize_t set_cookie_show(struct device *dev, 230 struct device_attribute *attr, char *buf) 231 { 232 struct nd_region *nd_region = to_nd_region(dev); 233 struct nd_interleave_set *nd_set = nd_region->nd_set; 234 235 if (is_nd_pmem(dev) && nd_set) 236 /* pass, should be precluded by region_visible */; 237 else 238 return -ENXIO; 239 240 return sprintf(buf, "%#llx\n", nd_set->cookie); 241 } 242 static DEVICE_ATTR_RO(set_cookie); 243 244 resource_size_t nd_region_available_dpa(struct nd_region *nd_region) 245 { 246 resource_size_t blk_max_overlap = 0, available, overlap; 247 int i; 248 249 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); 250 251 retry: 252 available = 0; 253 overlap = blk_max_overlap; 254 for (i = 0; i < nd_region->ndr_mappings; i++) { 255 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 256 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 257 258 /* if a dimm is disabled the available capacity is zero */ 259 if (!ndd) 260 return 0; 261 262 if (is_nd_pmem(&nd_region->dev)) { 263 available += nd_pmem_available_dpa(nd_region, 264 nd_mapping, &overlap); 265 if (overlap > blk_max_overlap) { 266 blk_max_overlap = overlap; 267 goto retry; 268 } 269 } else if (is_nd_blk(&nd_region->dev)) { 270 available += nd_blk_available_dpa(nd_mapping); 271 } 272 } 273 274 return available; 275 } 276 277 static ssize_t available_size_show(struct device *dev, 278 struct device_attribute *attr, char *buf) 279 { 280 struct nd_region *nd_region = to_nd_region(dev); 281 unsigned long long available = 0; 282 283 /* 284 * Flush in-flight updates and grab a snapshot of the available 285 * size. Of course, this value is potentially invalidated the 286 * memory nvdimm_bus_lock() is dropped, but that's userspace's 287 * problem to not race itself. 288 */ 289 nvdimm_bus_lock(dev); 290 wait_nvdimm_bus_probe_idle(dev); 291 available = nd_region_available_dpa(nd_region); 292 nvdimm_bus_unlock(dev); 293 294 return sprintf(buf, "%llu\n", available); 295 } 296 static DEVICE_ATTR_RO(available_size); 297 298 static ssize_t init_namespaces_show(struct device *dev, 299 struct device_attribute *attr, char *buf) 300 { 301 struct nd_region_namespaces *num_ns = dev_get_drvdata(dev); 302 ssize_t rc; 303 304 nvdimm_bus_lock(dev); 305 if (num_ns) 306 rc = sprintf(buf, "%d/%d\n", num_ns->active, num_ns->count); 307 else 308 rc = -ENXIO; 309 nvdimm_bus_unlock(dev); 310 311 return rc; 312 } 313 static DEVICE_ATTR_RO(init_namespaces); 314 315 static ssize_t namespace_seed_show(struct device *dev, 316 struct device_attribute *attr, char *buf) 317 { 318 struct nd_region *nd_region = to_nd_region(dev); 319 ssize_t rc; 320 321 nvdimm_bus_lock(dev); 322 if (nd_region->ns_seed) 323 rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed)); 324 else 325 rc = sprintf(buf, "\n"); 326 nvdimm_bus_unlock(dev); 327 return rc; 328 } 329 static DEVICE_ATTR_RO(namespace_seed); 330 331 static ssize_t btt_seed_show(struct device *dev, 332 struct device_attribute *attr, char *buf) 333 { 334 struct nd_region *nd_region = to_nd_region(dev); 335 ssize_t rc; 336 337 nvdimm_bus_lock(dev); 338 if (nd_region->btt_seed) 339 rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed)); 340 else 341 rc = sprintf(buf, "\n"); 342 nvdimm_bus_unlock(dev); 343 344 return rc; 345 } 346 static DEVICE_ATTR_RO(btt_seed); 347 348 static ssize_t read_only_show(struct device *dev, 349 struct device_attribute *attr, char *buf) 350 { 351 struct nd_region *nd_region = to_nd_region(dev); 352 353 return sprintf(buf, "%d\n", nd_region->ro); 354 } 355 356 static ssize_t read_only_store(struct device *dev, 357 struct device_attribute *attr, const char *buf, size_t len) 358 { 359 bool ro; 360 int rc = strtobool(buf, &ro); 361 struct nd_region *nd_region = to_nd_region(dev); 362 363 if (rc) 364 return rc; 365 366 nd_region->ro = ro; 367 return len; 368 } 369 static DEVICE_ATTR_RW(read_only); 370 371 static struct attribute *nd_region_attributes[] = { 372 &dev_attr_size.attr, 373 &dev_attr_nstype.attr, 374 &dev_attr_mappings.attr, 375 &dev_attr_btt_seed.attr, 376 &dev_attr_read_only.attr, 377 &dev_attr_set_cookie.attr, 378 &dev_attr_available_size.attr, 379 &dev_attr_namespace_seed.attr, 380 &dev_attr_init_namespaces.attr, 381 NULL, 382 }; 383 384 static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n) 385 { 386 struct device *dev = container_of(kobj, typeof(*dev), kobj); 387 struct nd_region *nd_region = to_nd_region(dev); 388 struct nd_interleave_set *nd_set = nd_region->nd_set; 389 int type = nd_region_to_nstype(nd_region); 390 391 if (a != &dev_attr_set_cookie.attr 392 && a != &dev_attr_available_size.attr) 393 return a->mode; 394 395 if ((type == ND_DEVICE_NAMESPACE_PMEM 396 || type == ND_DEVICE_NAMESPACE_BLK) 397 && a == &dev_attr_available_size.attr) 398 return a->mode; 399 else if (is_nd_pmem(dev) && nd_set) 400 return a->mode; 401 402 return 0; 403 } 404 405 struct attribute_group nd_region_attribute_group = { 406 .attrs = nd_region_attributes, 407 .is_visible = region_visible, 408 }; 409 EXPORT_SYMBOL_GPL(nd_region_attribute_group); 410 411 u64 nd_region_interleave_set_cookie(struct nd_region *nd_region) 412 { 413 struct nd_interleave_set *nd_set = nd_region->nd_set; 414 415 if (nd_set) 416 return nd_set->cookie; 417 return 0; 418 } 419 420 /* 421 * Upon successful probe/remove, take/release a reference on the 422 * associated interleave set (if present), and plant new btt + namespace 423 * seeds. Also, on the removal of a BLK region, notify the provider to 424 * disable the region. 425 */ 426 static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus, 427 struct device *dev, bool probe) 428 { 429 struct nd_region *nd_region; 430 431 if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) { 432 int i; 433 434 nd_region = to_nd_region(dev); 435 for (i = 0; i < nd_region->ndr_mappings; i++) { 436 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 437 struct nvdimm_drvdata *ndd = nd_mapping->ndd; 438 struct nvdimm *nvdimm = nd_mapping->nvdimm; 439 440 kfree(nd_mapping->labels); 441 nd_mapping->labels = NULL; 442 put_ndd(ndd); 443 nd_mapping->ndd = NULL; 444 if (ndd) 445 atomic_dec(&nvdimm->busy); 446 } 447 448 if (is_nd_pmem(dev)) 449 return; 450 451 to_nd_blk_region(dev)->disable(nvdimm_bus, dev); 452 } 453 if (dev->parent && is_nd_blk(dev->parent) && probe) { 454 nd_region = to_nd_region(dev->parent); 455 nvdimm_bus_lock(dev); 456 if (nd_region->ns_seed == dev) 457 nd_region_create_blk_seed(nd_region); 458 nvdimm_bus_unlock(dev); 459 } 460 if (is_nd_btt(dev) && probe) { 461 nd_region = to_nd_region(dev->parent); 462 nvdimm_bus_lock(dev); 463 if (nd_region->btt_seed == dev) 464 nd_region_create_btt_seed(nd_region); 465 nvdimm_bus_unlock(dev); 466 } 467 } 468 469 void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev) 470 { 471 nd_region_notify_driver_action(nvdimm_bus, dev, true); 472 } 473 474 void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev) 475 { 476 nd_region_notify_driver_action(nvdimm_bus, dev, false); 477 } 478 479 static ssize_t mappingN(struct device *dev, char *buf, int n) 480 { 481 struct nd_region *nd_region = to_nd_region(dev); 482 struct nd_mapping *nd_mapping; 483 struct nvdimm *nvdimm; 484 485 if (n >= nd_region->ndr_mappings) 486 return -ENXIO; 487 nd_mapping = &nd_region->mapping[n]; 488 nvdimm = nd_mapping->nvdimm; 489 490 return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev), 491 nd_mapping->start, nd_mapping->size); 492 } 493 494 #define REGION_MAPPING(idx) \ 495 static ssize_t mapping##idx##_show(struct device *dev, \ 496 struct device_attribute *attr, char *buf) \ 497 { \ 498 return mappingN(dev, buf, idx); \ 499 } \ 500 static DEVICE_ATTR_RO(mapping##idx) 501 502 /* 503 * 32 should be enough for a while, even in the presence of socket 504 * interleave a 32-way interleave set is a degenerate case. 505 */ 506 REGION_MAPPING(0); 507 REGION_MAPPING(1); 508 REGION_MAPPING(2); 509 REGION_MAPPING(3); 510 REGION_MAPPING(4); 511 REGION_MAPPING(5); 512 REGION_MAPPING(6); 513 REGION_MAPPING(7); 514 REGION_MAPPING(8); 515 REGION_MAPPING(9); 516 REGION_MAPPING(10); 517 REGION_MAPPING(11); 518 REGION_MAPPING(12); 519 REGION_MAPPING(13); 520 REGION_MAPPING(14); 521 REGION_MAPPING(15); 522 REGION_MAPPING(16); 523 REGION_MAPPING(17); 524 REGION_MAPPING(18); 525 REGION_MAPPING(19); 526 REGION_MAPPING(20); 527 REGION_MAPPING(21); 528 REGION_MAPPING(22); 529 REGION_MAPPING(23); 530 REGION_MAPPING(24); 531 REGION_MAPPING(25); 532 REGION_MAPPING(26); 533 REGION_MAPPING(27); 534 REGION_MAPPING(28); 535 REGION_MAPPING(29); 536 REGION_MAPPING(30); 537 REGION_MAPPING(31); 538 539 static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n) 540 { 541 struct device *dev = container_of(kobj, struct device, kobj); 542 struct nd_region *nd_region = to_nd_region(dev); 543 544 if (n < nd_region->ndr_mappings) 545 return a->mode; 546 return 0; 547 } 548 549 static struct attribute *mapping_attributes[] = { 550 &dev_attr_mapping0.attr, 551 &dev_attr_mapping1.attr, 552 &dev_attr_mapping2.attr, 553 &dev_attr_mapping3.attr, 554 &dev_attr_mapping4.attr, 555 &dev_attr_mapping5.attr, 556 &dev_attr_mapping6.attr, 557 &dev_attr_mapping7.attr, 558 &dev_attr_mapping8.attr, 559 &dev_attr_mapping9.attr, 560 &dev_attr_mapping10.attr, 561 &dev_attr_mapping11.attr, 562 &dev_attr_mapping12.attr, 563 &dev_attr_mapping13.attr, 564 &dev_attr_mapping14.attr, 565 &dev_attr_mapping15.attr, 566 &dev_attr_mapping16.attr, 567 &dev_attr_mapping17.attr, 568 &dev_attr_mapping18.attr, 569 &dev_attr_mapping19.attr, 570 &dev_attr_mapping20.attr, 571 &dev_attr_mapping21.attr, 572 &dev_attr_mapping22.attr, 573 &dev_attr_mapping23.attr, 574 &dev_attr_mapping24.attr, 575 &dev_attr_mapping25.attr, 576 &dev_attr_mapping26.attr, 577 &dev_attr_mapping27.attr, 578 &dev_attr_mapping28.attr, 579 &dev_attr_mapping29.attr, 580 &dev_attr_mapping30.attr, 581 &dev_attr_mapping31.attr, 582 NULL, 583 }; 584 585 struct attribute_group nd_mapping_attribute_group = { 586 .is_visible = mapping_visible, 587 .attrs = mapping_attributes, 588 }; 589 EXPORT_SYMBOL_GPL(nd_mapping_attribute_group); 590 591 int nd_blk_region_init(struct nd_region *nd_region) 592 { 593 struct device *dev = &nd_region->dev; 594 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 595 596 if (!is_nd_blk(dev)) 597 return 0; 598 599 if (nd_region->ndr_mappings < 1) { 600 dev_err(dev, "invalid BLK region\n"); 601 return -ENXIO; 602 } 603 604 return to_nd_blk_region(dev)->enable(nvdimm_bus, dev); 605 } 606 607 /** 608 * nd_region_acquire_lane - allocate and lock a lane 609 * @nd_region: region id and number of lanes possible 610 * 611 * A lane correlates to a BLK-data-window and/or a log slot in the BTT. 612 * We optimize for the common case where there are 256 lanes, one 613 * per-cpu. For larger systems we need to lock to share lanes. For now 614 * this implementation assumes the cost of maintaining an allocator for 615 * free lanes is on the order of the lock hold time, so it implements a 616 * static lane = cpu % num_lanes mapping. 617 * 618 * In the case of a BTT instance on top of a BLK namespace a lane may be 619 * acquired recursively. We lock on the first instance. 620 * 621 * In the case of a BTT instance on top of PMEM, we only acquire a lane 622 * for the BTT metadata updates. 623 */ 624 unsigned int nd_region_acquire_lane(struct nd_region *nd_region) 625 { 626 unsigned int cpu, lane; 627 628 cpu = get_cpu(); 629 if (nd_region->num_lanes < nr_cpu_ids) { 630 struct nd_percpu_lane *ndl_lock, *ndl_count; 631 632 lane = cpu % nd_region->num_lanes; 633 ndl_count = per_cpu_ptr(nd_region->lane, cpu); 634 ndl_lock = per_cpu_ptr(nd_region->lane, lane); 635 if (ndl_count->count++ == 0) 636 spin_lock(&ndl_lock->lock); 637 } else 638 lane = cpu; 639 640 return lane; 641 } 642 EXPORT_SYMBOL(nd_region_acquire_lane); 643 644 void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane) 645 { 646 if (nd_region->num_lanes < nr_cpu_ids) { 647 unsigned int cpu = get_cpu(); 648 struct nd_percpu_lane *ndl_lock, *ndl_count; 649 650 ndl_count = per_cpu_ptr(nd_region->lane, cpu); 651 ndl_lock = per_cpu_ptr(nd_region->lane, lane); 652 if (--ndl_count->count == 0) 653 spin_unlock(&ndl_lock->lock); 654 put_cpu(); 655 } 656 put_cpu(); 657 } 658 EXPORT_SYMBOL(nd_region_release_lane); 659 660 static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus, 661 struct nd_region_desc *ndr_desc, struct device_type *dev_type, 662 const char *caller) 663 { 664 struct nd_region *nd_region; 665 struct device *dev; 666 void *region_buf; 667 unsigned int i; 668 int ro = 0; 669 670 for (i = 0; i < ndr_desc->num_mappings; i++) { 671 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i]; 672 struct nvdimm *nvdimm = nd_mapping->nvdimm; 673 674 if ((nd_mapping->start | nd_mapping->size) % SZ_4K) { 675 dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n", 676 caller, dev_name(&nvdimm->dev), i); 677 678 return NULL; 679 } 680 681 if (nvdimm->flags & NDD_UNARMED) 682 ro = 1; 683 } 684 685 if (dev_type == &nd_blk_device_type) { 686 struct nd_blk_region_desc *ndbr_desc; 687 struct nd_blk_region *ndbr; 688 689 ndbr_desc = to_blk_region_desc(ndr_desc); 690 ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping) 691 * ndr_desc->num_mappings, 692 GFP_KERNEL); 693 if (ndbr) { 694 nd_region = &ndbr->nd_region; 695 ndbr->enable = ndbr_desc->enable; 696 ndbr->disable = ndbr_desc->disable; 697 ndbr->do_io = ndbr_desc->do_io; 698 } 699 region_buf = ndbr; 700 } else { 701 nd_region = kzalloc(sizeof(struct nd_region) 702 + sizeof(struct nd_mapping) 703 * ndr_desc->num_mappings, 704 GFP_KERNEL); 705 region_buf = nd_region; 706 } 707 708 if (!region_buf) 709 return NULL; 710 nd_region->id = ida_simple_get(®ion_ida, 0, 0, GFP_KERNEL); 711 if (nd_region->id < 0) 712 goto err_id; 713 714 nd_region->lane = alloc_percpu(struct nd_percpu_lane); 715 if (!nd_region->lane) 716 goto err_percpu; 717 718 for (i = 0; i < nr_cpu_ids; i++) { 719 struct nd_percpu_lane *ndl; 720 721 ndl = per_cpu_ptr(nd_region->lane, i); 722 spin_lock_init(&ndl->lock); 723 ndl->count = 0; 724 } 725 726 memcpy(nd_region->mapping, ndr_desc->nd_mapping, 727 sizeof(struct nd_mapping) * ndr_desc->num_mappings); 728 for (i = 0; i < ndr_desc->num_mappings; i++) { 729 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i]; 730 struct nvdimm *nvdimm = nd_mapping->nvdimm; 731 732 get_device(&nvdimm->dev); 733 } 734 nd_region->ndr_mappings = ndr_desc->num_mappings; 735 nd_region->provider_data = ndr_desc->provider_data; 736 nd_region->nd_set = ndr_desc->nd_set; 737 nd_region->num_lanes = ndr_desc->num_lanes; 738 nd_region->ro = ro; 739 nd_region->numa_node = ndr_desc->numa_node; 740 ida_init(&nd_region->ns_ida); 741 ida_init(&nd_region->btt_ida); 742 dev = &nd_region->dev; 743 dev_set_name(dev, "region%d", nd_region->id); 744 dev->parent = &nvdimm_bus->dev; 745 dev->type = dev_type; 746 dev->groups = ndr_desc->attr_groups; 747 nd_region->ndr_size = resource_size(ndr_desc->res); 748 nd_region->ndr_start = ndr_desc->res->start; 749 nd_device_register(dev); 750 751 return nd_region; 752 753 err_percpu: 754 ida_simple_remove(®ion_ida, nd_region->id); 755 err_id: 756 kfree(region_buf); 757 return NULL; 758 } 759 760 struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus, 761 struct nd_region_desc *ndr_desc) 762 { 763 ndr_desc->num_lanes = ND_MAX_LANES; 764 return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type, 765 __func__); 766 } 767 EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create); 768 769 struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus, 770 struct nd_region_desc *ndr_desc) 771 { 772 if (ndr_desc->num_mappings > 1) 773 return NULL; 774 ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES); 775 return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type, 776 __func__); 777 } 778 EXPORT_SYMBOL_GPL(nvdimm_blk_region_create); 779 780 struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus, 781 struct nd_region_desc *ndr_desc) 782 { 783 ndr_desc->num_lanes = ND_MAX_LANES; 784 return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type, 785 __func__); 786 } 787 EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create); 788