1 /* 2 * Copyright(c) 2013-2016 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/memremap.h> 14 #include <linux/blkdev.h> 15 #include <linux/device.h> 16 #include <linux/genhd.h> 17 #include <linux/sizes.h> 18 #include <linux/slab.h> 19 #include <linux/fs.h> 20 #include <linux/mm.h> 21 #include "nd-core.h" 22 #include "pfn.h" 23 #include "nd.h" 24 25 static void nd_pfn_release(struct device *dev) 26 { 27 struct nd_region *nd_region = to_nd_region(dev->parent); 28 struct nd_pfn *nd_pfn = to_nd_pfn(dev); 29 30 dev_dbg(dev, "%s\n", __func__); 31 nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns); 32 ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id); 33 kfree(nd_pfn->uuid); 34 kfree(nd_pfn); 35 } 36 37 static struct device_type nd_pfn_device_type = { 38 .name = "nd_pfn", 39 .release = nd_pfn_release, 40 }; 41 42 bool is_nd_pfn(struct device *dev) 43 { 44 return dev ? dev->type == &nd_pfn_device_type : false; 45 } 46 EXPORT_SYMBOL(is_nd_pfn); 47 48 struct nd_pfn *to_nd_pfn(struct device *dev) 49 { 50 struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev); 51 52 WARN_ON(!is_nd_pfn(dev)); 53 return nd_pfn; 54 } 55 EXPORT_SYMBOL(to_nd_pfn); 56 57 static ssize_t mode_show(struct device *dev, 58 struct device_attribute *attr, char *buf) 59 { 60 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 61 62 switch (nd_pfn->mode) { 63 case PFN_MODE_RAM: 64 return sprintf(buf, "ram\n"); 65 case PFN_MODE_PMEM: 66 return sprintf(buf, "pmem\n"); 67 default: 68 return sprintf(buf, "none\n"); 69 } 70 } 71 72 static ssize_t mode_store(struct device *dev, 73 struct device_attribute *attr, const char *buf, size_t len) 74 { 75 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 76 ssize_t rc = 0; 77 78 device_lock(dev); 79 nvdimm_bus_lock(dev); 80 if (dev->driver) 81 rc = -EBUSY; 82 else { 83 size_t n = len - 1; 84 85 if (strncmp(buf, "pmem\n", n) == 0 86 || strncmp(buf, "pmem", n) == 0) { 87 nd_pfn->mode = PFN_MODE_PMEM; 88 } else if (strncmp(buf, "ram\n", n) == 0 89 || strncmp(buf, "ram", n) == 0) 90 nd_pfn->mode = PFN_MODE_RAM; 91 else if (strncmp(buf, "none\n", n) == 0 92 || strncmp(buf, "none", n) == 0) 93 nd_pfn->mode = PFN_MODE_NONE; 94 else 95 rc = -EINVAL; 96 } 97 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, 98 rc, buf, buf[len - 1] == '\n' ? "" : "\n"); 99 nvdimm_bus_unlock(dev); 100 device_unlock(dev); 101 102 return rc ? rc : len; 103 } 104 static DEVICE_ATTR_RW(mode); 105 106 static ssize_t align_show(struct device *dev, 107 struct device_attribute *attr, char *buf) 108 { 109 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 110 111 return sprintf(buf, "%ld\n", nd_pfn->align); 112 } 113 114 static const unsigned long *nd_pfn_supported_alignments(void) 115 { 116 /* 117 * This needs to be a non-static variable because the *_SIZE 118 * macros aren't always constants. 119 */ 120 const unsigned long supported_alignments[] = { 121 PAGE_SIZE, 122 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 123 HPAGE_PMD_SIZE, 124 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 125 HPAGE_PUD_SIZE, 126 #endif 127 #endif 128 0, 129 }; 130 static unsigned long data[ARRAY_SIZE(supported_alignments)]; 131 132 memcpy(data, supported_alignments, sizeof(data)); 133 134 return data; 135 } 136 137 static ssize_t align_store(struct device *dev, 138 struct device_attribute *attr, const char *buf, size_t len) 139 { 140 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 141 ssize_t rc; 142 143 device_lock(dev); 144 nvdimm_bus_lock(dev); 145 rc = nd_size_select_store(dev, buf, &nd_pfn->align, 146 nd_pfn_supported_alignments()); 147 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, 148 rc, buf, buf[len - 1] == '\n' ? "" : "\n"); 149 nvdimm_bus_unlock(dev); 150 device_unlock(dev); 151 152 return rc ? rc : len; 153 } 154 static DEVICE_ATTR_RW(align); 155 156 static ssize_t uuid_show(struct device *dev, 157 struct device_attribute *attr, char *buf) 158 { 159 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 160 161 if (nd_pfn->uuid) 162 return sprintf(buf, "%pUb\n", nd_pfn->uuid); 163 return sprintf(buf, "\n"); 164 } 165 166 static ssize_t uuid_store(struct device *dev, 167 struct device_attribute *attr, const char *buf, size_t len) 168 { 169 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 170 ssize_t rc; 171 172 device_lock(dev); 173 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len); 174 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, 175 rc, buf, buf[len - 1] == '\n' ? "" : "\n"); 176 device_unlock(dev); 177 178 return rc ? rc : len; 179 } 180 static DEVICE_ATTR_RW(uuid); 181 182 static ssize_t namespace_show(struct device *dev, 183 struct device_attribute *attr, char *buf) 184 { 185 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 186 ssize_t rc; 187 188 nvdimm_bus_lock(dev); 189 rc = sprintf(buf, "%s\n", nd_pfn->ndns 190 ? dev_name(&nd_pfn->ndns->dev) : ""); 191 nvdimm_bus_unlock(dev); 192 return rc; 193 } 194 195 static ssize_t namespace_store(struct device *dev, 196 struct device_attribute *attr, const char *buf, size_t len) 197 { 198 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 199 ssize_t rc; 200 201 device_lock(dev); 202 nvdimm_bus_lock(dev); 203 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len); 204 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, 205 rc, buf, buf[len - 1] == '\n' ? "" : "\n"); 206 nvdimm_bus_unlock(dev); 207 device_unlock(dev); 208 209 return rc; 210 } 211 static DEVICE_ATTR_RW(namespace); 212 213 static ssize_t resource_show(struct device *dev, 214 struct device_attribute *attr, char *buf) 215 { 216 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 217 ssize_t rc; 218 219 device_lock(dev); 220 if (dev->driver) { 221 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; 222 u64 offset = __le64_to_cpu(pfn_sb->dataoff); 223 struct nd_namespace_common *ndns = nd_pfn->ndns; 224 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); 225 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); 226 227 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start 228 + start_pad + offset); 229 } else { 230 /* no address to convey if the pfn instance is disabled */ 231 rc = -ENXIO; 232 } 233 device_unlock(dev); 234 235 return rc; 236 } 237 static DEVICE_ATTR_RO(resource); 238 239 static ssize_t size_show(struct device *dev, 240 struct device_attribute *attr, char *buf) 241 { 242 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); 243 ssize_t rc; 244 245 device_lock(dev); 246 if (dev->driver) { 247 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; 248 u64 offset = __le64_to_cpu(pfn_sb->dataoff); 249 struct nd_namespace_common *ndns = nd_pfn->ndns; 250 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); 251 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); 252 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); 253 254 rc = sprintf(buf, "%llu\n", (unsigned long long) 255 resource_size(&nsio->res) - start_pad 256 - end_trunc - offset); 257 } else { 258 /* no size to convey if the pfn instance is disabled */ 259 rc = -ENXIO; 260 } 261 device_unlock(dev); 262 263 return rc; 264 } 265 static DEVICE_ATTR_RO(size); 266 267 static ssize_t supported_alignments_show(struct device *dev, 268 struct device_attribute *attr, char *buf) 269 { 270 return nd_size_select_show(0, nd_pfn_supported_alignments(), buf); 271 } 272 static DEVICE_ATTR_RO(supported_alignments); 273 274 static struct attribute *nd_pfn_attributes[] = { 275 &dev_attr_mode.attr, 276 &dev_attr_namespace.attr, 277 &dev_attr_uuid.attr, 278 &dev_attr_align.attr, 279 &dev_attr_resource.attr, 280 &dev_attr_size.attr, 281 &dev_attr_supported_alignments.attr, 282 NULL, 283 }; 284 285 struct attribute_group nd_pfn_attribute_group = { 286 .attrs = nd_pfn_attributes, 287 }; 288 289 static const struct attribute_group *nd_pfn_attribute_groups[] = { 290 &nd_pfn_attribute_group, 291 &nd_device_attribute_group, 292 &nd_numa_attribute_group, 293 NULL, 294 }; 295 296 struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn, 297 struct nd_namespace_common *ndns) 298 { 299 struct device *dev = &nd_pfn->dev; 300 301 if (!nd_pfn) 302 return NULL; 303 304 nd_pfn->mode = PFN_MODE_NONE; 305 nd_pfn->align = PFN_DEFAULT_ALIGNMENT; 306 dev = &nd_pfn->dev; 307 device_initialize(&nd_pfn->dev); 308 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) { 309 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n", 310 __func__, dev_name(ndns->claim)); 311 put_device(dev); 312 return NULL; 313 } 314 return dev; 315 } 316 317 static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region) 318 { 319 struct nd_pfn *nd_pfn; 320 struct device *dev; 321 322 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL); 323 if (!nd_pfn) 324 return NULL; 325 326 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL); 327 if (nd_pfn->id < 0) { 328 kfree(nd_pfn); 329 return NULL; 330 } 331 332 dev = &nd_pfn->dev; 333 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id); 334 dev->groups = nd_pfn_attribute_groups; 335 dev->type = &nd_pfn_device_type; 336 dev->parent = &nd_region->dev; 337 338 return nd_pfn; 339 } 340 341 struct device *nd_pfn_create(struct nd_region *nd_region) 342 { 343 struct nd_pfn *nd_pfn; 344 struct device *dev; 345 346 if (!is_memory(&nd_region->dev)) 347 return NULL; 348 349 nd_pfn = nd_pfn_alloc(nd_region); 350 dev = nd_pfn_devinit(nd_pfn, NULL); 351 352 __nd_device_register(dev); 353 return dev; 354 } 355 356 int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig) 357 { 358 u64 checksum, offset; 359 unsigned long align; 360 enum nd_pfn_mode mode; 361 struct nd_namespace_io *nsio; 362 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; 363 struct nd_namespace_common *ndns = nd_pfn->ndns; 364 const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev); 365 366 if (!pfn_sb || !ndns) 367 return -ENODEV; 368 369 if (!is_memory(nd_pfn->dev.parent)) 370 return -ENODEV; 371 372 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0)) 373 return -ENXIO; 374 375 if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0) 376 return -ENODEV; 377 378 checksum = le64_to_cpu(pfn_sb->checksum); 379 pfn_sb->checksum = 0; 380 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb)) 381 return -ENODEV; 382 pfn_sb->checksum = cpu_to_le64(checksum); 383 384 if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0) 385 return -ENODEV; 386 387 if (__le16_to_cpu(pfn_sb->version_minor) < 1) { 388 pfn_sb->start_pad = 0; 389 pfn_sb->end_trunc = 0; 390 } 391 392 if (__le16_to_cpu(pfn_sb->version_minor) < 2) 393 pfn_sb->align = 0; 394 395 switch (le32_to_cpu(pfn_sb->mode)) { 396 case PFN_MODE_RAM: 397 case PFN_MODE_PMEM: 398 break; 399 default: 400 return -ENXIO; 401 } 402 403 align = le32_to_cpu(pfn_sb->align); 404 offset = le64_to_cpu(pfn_sb->dataoff); 405 if (align == 0) 406 align = 1UL << ilog2(offset); 407 mode = le32_to_cpu(pfn_sb->mode); 408 409 if (!nd_pfn->uuid) { 410 /* 411 * When probing a namepace via nd_pfn_probe() the uuid 412 * is NULL (see: nd_pfn_devinit()) we init settings from 413 * pfn_sb 414 */ 415 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL); 416 if (!nd_pfn->uuid) 417 return -ENOMEM; 418 nd_pfn->align = align; 419 nd_pfn->mode = mode; 420 } else { 421 /* 422 * When probing a pfn / dax instance we validate the 423 * live settings against the pfn_sb 424 */ 425 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0) 426 return -ENODEV; 427 428 /* 429 * If the uuid validates, but other settings mismatch 430 * return EINVAL because userspace has managed to change 431 * the configuration without specifying new 432 * identification. 433 */ 434 if (nd_pfn->align != align || nd_pfn->mode != mode) { 435 dev_err(&nd_pfn->dev, 436 "init failed, settings mismatch\n"); 437 dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n", 438 nd_pfn->align, align, nd_pfn->mode, 439 mode); 440 return -EINVAL; 441 } 442 } 443 444 if (align > nvdimm_namespace_capacity(ndns)) { 445 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n", 446 align, nvdimm_namespace_capacity(ndns)); 447 return -EINVAL; 448 } 449 450 /* 451 * These warnings are verbose because they can only trigger in 452 * the case where the physical address alignment of the 453 * namespace has changed since the pfn superblock was 454 * established. 455 */ 456 nsio = to_nd_namespace_io(&ndns->dev); 457 if (offset >= resource_size(&nsio->res)) { 458 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n", 459 dev_name(&ndns->dev)); 460 return -EBUSY; 461 } 462 463 if ((align && !IS_ALIGNED(offset, align)) 464 || !IS_ALIGNED(offset, PAGE_SIZE)) { 465 dev_err(&nd_pfn->dev, 466 "bad offset: %#llx dax disabled align: %#lx\n", 467 offset, align); 468 return -ENXIO; 469 } 470 471 return 0; 472 } 473 EXPORT_SYMBOL(nd_pfn_validate); 474 475 int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns) 476 { 477 int rc; 478 struct nd_pfn *nd_pfn; 479 struct device *pfn_dev; 480 struct nd_pfn_sb *pfn_sb; 481 struct nd_region *nd_region = to_nd_region(ndns->dev.parent); 482 483 if (ndns->force_raw) 484 return -ENODEV; 485 486 switch (ndns->claim_class) { 487 case NVDIMM_CCLASS_NONE: 488 case NVDIMM_CCLASS_PFN: 489 break; 490 default: 491 return -ENODEV; 492 } 493 494 nvdimm_bus_lock(&ndns->dev); 495 nd_pfn = nd_pfn_alloc(nd_region); 496 pfn_dev = nd_pfn_devinit(nd_pfn, ndns); 497 nvdimm_bus_unlock(&ndns->dev); 498 if (!pfn_dev) 499 return -ENOMEM; 500 pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL); 501 nd_pfn = to_nd_pfn(pfn_dev); 502 nd_pfn->pfn_sb = pfn_sb; 503 rc = nd_pfn_validate(nd_pfn, PFN_SIG); 504 dev_dbg(dev, "%s: pfn: %s\n", __func__, 505 rc == 0 ? dev_name(pfn_dev) : "<none>"); 506 if (rc < 0) { 507 nd_detach_ndns(pfn_dev, &nd_pfn->ndns); 508 put_device(pfn_dev); 509 } else 510 __nd_device_register(pfn_dev); 511 512 return rc; 513 } 514 EXPORT_SYMBOL(nd_pfn_probe); 515 516 /* 517 * We hotplug memory at section granularity, pad the reserved area from 518 * the previous section base to the namespace base address. 519 */ 520 static unsigned long init_altmap_base(resource_size_t base) 521 { 522 unsigned long base_pfn = PHYS_PFN(base); 523 524 return PFN_SECTION_ALIGN_DOWN(base_pfn); 525 } 526 527 static unsigned long init_altmap_reserve(resource_size_t base) 528 { 529 unsigned long reserve = PHYS_PFN(SZ_8K); 530 unsigned long base_pfn = PHYS_PFN(base); 531 532 reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn); 533 return reserve; 534 } 535 536 static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn, 537 struct resource *res, struct vmem_altmap *altmap) 538 { 539 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; 540 u64 offset = le64_to_cpu(pfn_sb->dataoff); 541 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); 542 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); 543 struct nd_namespace_common *ndns = nd_pfn->ndns; 544 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); 545 resource_size_t base = nsio->res.start + start_pad; 546 struct vmem_altmap __altmap = { 547 .base_pfn = init_altmap_base(base), 548 .reserve = init_altmap_reserve(base), 549 }; 550 551 memcpy(res, &nsio->res, sizeof(*res)); 552 res->start += start_pad; 553 res->end -= end_trunc; 554 555 if (nd_pfn->mode == PFN_MODE_RAM) { 556 if (offset < SZ_8K) 557 return ERR_PTR(-EINVAL); 558 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns); 559 altmap = NULL; 560 } else if (nd_pfn->mode == PFN_MODE_PMEM) { 561 nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res) 562 - offset) / PAGE_SIZE); 563 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns) 564 dev_info(&nd_pfn->dev, 565 "number of pfns truncated from %lld to %ld\n", 566 le64_to_cpu(nd_pfn->pfn_sb->npfns), 567 nd_pfn->npfns); 568 memcpy(altmap, &__altmap, sizeof(*altmap)); 569 altmap->free = PHYS_PFN(offset - SZ_8K); 570 altmap->alloc = 0; 571 } else 572 return ERR_PTR(-ENXIO); 573 574 return altmap; 575 } 576 577 static int nd_pfn_init(struct nd_pfn *nd_pfn) 578 { 579 u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0; 580 struct nd_namespace_common *ndns = nd_pfn->ndns; 581 u32 start_pad = 0, end_trunc = 0; 582 resource_size_t start, size; 583 struct nd_namespace_io *nsio; 584 struct nd_region *nd_region; 585 struct nd_pfn_sb *pfn_sb; 586 unsigned long npfns; 587 phys_addr_t offset; 588 const char *sig; 589 u64 checksum; 590 int rc; 591 592 pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL); 593 if (!pfn_sb) 594 return -ENOMEM; 595 596 nd_pfn->pfn_sb = pfn_sb; 597 if (is_nd_dax(&nd_pfn->dev)) 598 sig = DAX_SIG; 599 else 600 sig = PFN_SIG; 601 rc = nd_pfn_validate(nd_pfn, sig); 602 if (rc != -ENODEV) 603 return rc; 604 605 /* no info block, do init */; 606 nd_region = to_nd_region(nd_pfn->dev.parent); 607 if (nd_region->ro) { 608 dev_info(&nd_pfn->dev, 609 "%s is read-only, unable to init metadata\n", 610 dev_name(&nd_region->dev)); 611 return -ENXIO; 612 } 613 614 memset(pfn_sb, 0, sizeof(*pfn_sb)); 615 616 /* 617 * Check if pmem collides with 'System RAM' when section aligned and 618 * trim it accordingly 619 */ 620 nsio = to_nd_namespace_io(&ndns->dev); 621 start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start); 622 size = resource_size(&nsio->res); 623 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, 624 IORES_DESC_NONE) == REGION_MIXED) { 625 start = nsio->res.start; 626 start_pad = PHYS_SECTION_ALIGN_UP(start) - start; 627 } 628 629 start = nsio->res.start; 630 size = PHYS_SECTION_ALIGN_UP(start + size) - start; 631 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, 632 IORES_DESC_NONE) == REGION_MIXED) { 633 size = resource_size(&nsio->res); 634 end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size); 635 } 636 637 if (start_pad + end_trunc) 638 dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n", 639 dev_name(&ndns->dev), start_pad + end_trunc); 640 641 /* 642 * Note, we use 64 here for the standard size of struct page, 643 * debugging options may cause it to be larger in which case the 644 * implementation will limit the pfns advertised through 645 * ->direct_access() to those that are included in the memmap. 646 */ 647 start += start_pad; 648 size = resource_size(&nsio->res); 649 npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K) 650 / PAGE_SIZE); 651 if (nd_pfn->mode == PFN_MODE_PMEM) { 652 /* 653 * The altmap should be padded out to the block size used 654 * when populating the vmemmap. This *should* be equal to 655 * PMD_SIZE for most architectures. 656 */ 657 offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve, 658 max(nd_pfn->align, PMD_SIZE)) - start; 659 } else if (nd_pfn->mode == PFN_MODE_RAM) 660 offset = ALIGN(start + SZ_8K + dax_label_reserve, 661 nd_pfn->align) - start; 662 else 663 return -ENXIO; 664 665 if (offset + start_pad + end_trunc >= size) { 666 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n", 667 dev_name(&ndns->dev)); 668 return -ENXIO; 669 } 670 671 npfns = (size - offset - start_pad - end_trunc) / SZ_4K; 672 pfn_sb->mode = cpu_to_le32(nd_pfn->mode); 673 pfn_sb->dataoff = cpu_to_le64(offset); 674 pfn_sb->npfns = cpu_to_le64(npfns); 675 memcpy(pfn_sb->signature, sig, PFN_SIG_LEN); 676 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16); 677 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16); 678 pfn_sb->version_major = cpu_to_le16(1); 679 pfn_sb->version_minor = cpu_to_le16(2); 680 pfn_sb->start_pad = cpu_to_le32(start_pad); 681 pfn_sb->end_trunc = cpu_to_le32(end_trunc); 682 pfn_sb->align = cpu_to_le32(nd_pfn->align); 683 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb); 684 pfn_sb->checksum = cpu_to_le64(checksum); 685 686 return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0); 687 } 688 689 /* 690 * Determine the effective resource range and vmem_altmap from an nd_pfn 691 * instance. 692 */ 693 struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn, 694 struct resource *res, struct vmem_altmap *altmap) 695 { 696 int rc; 697 698 if (!nd_pfn->uuid || !nd_pfn->ndns) 699 return ERR_PTR(-ENODEV); 700 701 rc = nd_pfn_init(nd_pfn); 702 if (rc) 703 return ERR_PTR(rc); 704 705 /* we need a valid pfn_sb before we can init a vmem_altmap */ 706 return __nvdimm_setup_pfn(nd_pfn, res, altmap); 707 } 708 EXPORT_SYMBOL_GPL(nvdimm_setup_pfn); 709