]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/nvdimm/pfn_devs.c
libnvdimm: cleanup nvdimm_namespace_common_probe(), kill 'host'
[mirror_ubuntu-jammy-kernel.git] / drivers / nvdimm / pfn_devs.c
CommitLineData
e1455744
DW
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 */
ac515c08 13#include <linux/memremap.h>
e1455744
DW
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
25static 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
37static struct device_type nd_pfn_device_type = {
38 .name = "nd_pfn",
39 .release = nd_pfn_release,
40};
41
42bool is_nd_pfn(struct device *dev)
43{
44 return dev ? dev->type == &nd_pfn_device_type : false;
45}
46EXPORT_SYMBOL(is_nd_pfn);
47
48struct 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}
55EXPORT_SYMBOL(to_nd_pfn);
56
57static ssize_t mode_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
59{
60 struct nd_pfn *nd_pfn = to_nd_pfn(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
72static 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(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) {
d2c0f041 87 nd_pfn->mode = PFN_MODE_PMEM;
e1455744
DW
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}
104static DEVICE_ATTR_RW(mode);
105
315c5625
DW
106static ssize_t align_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
108{
109 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
110
111 return sprintf(buf, "%lx\n", nd_pfn->align);
112}
113
114static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
115{
116 unsigned long val;
117 int rc;
118
119 rc = kstrtoul(buf, 0, &val);
120 if (rc)
121 return rc;
122
123 if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G)
124 return -EINVAL;
125
126 if (nd_pfn->dev.driver)
127 return -EBUSY;
128 else
129 nd_pfn->align = val;
130
131 return 0;
132}
133
134static ssize_t align_store(struct device *dev,
135 struct device_attribute *attr, const char *buf, size_t len)
136{
137 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
138 ssize_t rc;
139
140 device_lock(dev);
141 nvdimm_bus_lock(dev);
142 rc = __align_store(nd_pfn, buf);
143 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
144 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
145 nvdimm_bus_unlock(dev);
146 device_unlock(dev);
147
148 return rc ? rc : len;
149}
150static DEVICE_ATTR_RW(align);
151
e1455744
DW
152static ssize_t uuid_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
154{
155 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
156
157 if (nd_pfn->uuid)
158 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
159 return sprintf(buf, "\n");
160}
161
162static ssize_t uuid_store(struct device *dev,
163 struct device_attribute *attr, const char *buf, size_t len)
164{
165 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
166 ssize_t rc;
167
168 device_lock(dev);
169 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
170 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
171 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
172 device_unlock(dev);
173
174 return rc ? rc : len;
175}
176static DEVICE_ATTR_RW(uuid);
177
178static ssize_t namespace_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
181 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
182 ssize_t rc;
183
184 nvdimm_bus_lock(dev);
185 rc = sprintf(buf, "%s\n", nd_pfn->ndns
186 ? dev_name(&nd_pfn->ndns->dev) : "");
187 nvdimm_bus_unlock(dev);
188 return rc;
189}
190
191static ssize_t namespace_store(struct device *dev,
192 struct device_attribute *attr, const char *buf, size_t len)
193{
194 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
195 ssize_t rc;
196
e1455744 197 device_lock(dev);
4ca8b57a 198 nvdimm_bus_lock(dev);
e1455744
DW
199 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
200 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
201 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
e1455744 202 nvdimm_bus_unlock(dev);
4ca8b57a 203 device_unlock(dev);
e1455744
DW
204
205 return rc;
206}
207static DEVICE_ATTR_RW(namespace);
208
f6ed58c7
DW
209static ssize_t resource_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
213 ssize_t rc;
214
215 device_lock(dev);
216 if (dev->driver) {
217 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
218 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
219 struct nd_namespace_common *ndns = nd_pfn->ndns;
220 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
221 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
222
223 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
224 + start_pad + offset);
225 } else {
226 /* no address to convey if the pfn instance is disabled */
227 rc = -ENXIO;
228 }
229 device_unlock(dev);
230
231 return rc;
232}
233static DEVICE_ATTR_RO(resource);
234
235static ssize_t size_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
237{
238 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
239 ssize_t rc;
240
241 device_lock(dev);
242 if (dev->driver) {
243 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
244 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
245 struct nd_namespace_common *ndns = nd_pfn->ndns;
246 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
247 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
248 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
249
250 rc = sprintf(buf, "%llu\n", (unsigned long long)
251 resource_size(&nsio->res) - start_pad
252 - end_trunc - offset);
253 } else {
254 /* no size to convey if the pfn instance is disabled */
255 rc = -ENXIO;
256 }
257 device_unlock(dev);
258
259 return rc;
260}
261static DEVICE_ATTR_RO(size);
262
e1455744
DW
263static struct attribute *nd_pfn_attributes[] = {
264 &dev_attr_mode.attr,
265 &dev_attr_namespace.attr,
266 &dev_attr_uuid.attr,
315c5625 267 &dev_attr_align.attr,
f6ed58c7
DW
268 &dev_attr_resource.attr,
269 &dev_attr_size.attr,
e1455744
DW
270 NULL,
271};
272
273static struct attribute_group nd_pfn_attribute_group = {
274 .attrs = nd_pfn_attributes,
275};
276
277static const struct attribute_group *nd_pfn_attribute_groups[] = {
278 &nd_pfn_attribute_group,
279 &nd_device_attribute_group,
280 &nd_numa_attribute_group,
281 NULL,
282};
283
284static struct device *__nd_pfn_create(struct nd_region *nd_region,
e1455744
DW
285 struct nd_namespace_common *ndns)
286{
287 struct nd_pfn *nd_pfn;
288 struct device *dev;
289
290 /* we can only create pages for contiguous ranged of pmem */
291 if (!is_nd_pmem(&nd_region->dev))
292 return NULL;
293
294 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
295 if (!nd_pfn)
296 return NULL;
297
298 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
299 if (nd_pfn->id < 0) {
300 kfree(nd_pfn);
301 return NULL;
302 }
303
f7c6ab80 304 nd_pfn->mode = PFN_MODE_NONE;
315c5625 305 nd_pfn->align = HPAGE_SIZE;
e1455744
DW
306 dev = &nd_pfn->dev;
307 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
308 dev->parent = &nd_region->dev;
309 dev->type = &nd_pfn_device_type;
310 dev->groups = nd_pfn_attribute_groups;
311 device_initialize(&nd_pfn->dev);
312 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
313 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
314 __func__, dev_name(ndns->claim));
315 put_device(dev);
316 return NULL;
317 }
318 return dev;
319}
320
321struct device *nd_pfn_create(struct nd_region *nd_region)
322{
f7c6ab80 323 struct device *dev = __nd_pfn_create(nd_region, NULL);
e1455744
DW
324
325 if (dev)
326 __nd_device_register(dev);
327 return dev;
328}
329
32ab0a3f 330int nd_pfn_validate(struct nd_pfn *nd_pfn)
e1455744 331{
e1455744 332 u64 checksum, offset;
a34d5e8a
DW
333 struct nd_namespace_io *nsio;
334 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
335 struct nd_namespace_common *ndns = nd_pfn->ndns;
336 const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
e1455744
DW
337
338 if (!pfn_sb || !ndns)
339 return -ENODEV;
340
341 if (!is_nd_pmem(nd_pfn->dev.parent))
342 return -ENODEV;
343
e1455744
DW
344 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
345 return -ENXIO;
346
347 if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
348 return -ENODEV;
349
350 checksum = le64_to_cpu(pfn_sb->checksum);
351 pfn_sb->checksum = 0;
352 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
353 return -ENODEV;
354 pfn_sb->checksum = cpu_to_le64(checksum);
355
a34d5e8a
DW
356 if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
357 return -ENODEV;
358
cfe30b87
DW
359 if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
360 pfn_sb->start_pad = 0;
361 pfn_sb->end_trunc = 0;
362 }
363
e1455744
DW
364 switch (le32_to_cpu(pfn_sb->mode)) {
365 case PFN_MODE_RAM:
e1455744 366 case PFN_MODE_PMEM:
45eb570a 367 break;
e1455744
DW
368 default:
369 return -ENXIO;
370 }
371
372 if (!nd_pfn->uuid) {
373 /* from probe we allocate */
374 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
375 if (!nd_pfn->uuid)
376 return -ENOMEM;
377 } else {
378 /* from init we validate */
379 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
e5670563 380 return -ENODEV;
e1455744
DW
381 }
382
315c5625
DW
383 if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) {
384 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
385 nd_pfn->align, nvdimm_namespace_capacity(ndns));
386 return -EINVAL;
387 }
388
e1455744
DW
389 /*
390 * These warnings are verbose because they can only trigger in
391 * the case where the physical address alignment of the
392 * namespace has changed since the pfn superblock was
393 * established.
394 */
395 offset = le64_to_cpu(pfn_sb->dataoff);
396 nsio = to_nd_namespace_io(&ndns->dev);
9f1e8cee 397 if (offset >= resource_size(&nsio->res)) {
e1455744
DW
398 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
399 dev_name(&ndns->dev));
400 return -EBUSY;
401 }
402
315c5625
DW
403 nd_pfn->align = 1UL << ilog2(offset);
404 if (!is_power_of_2(offset) || offset < PAGE_SIZE) {
405 dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n",
406 offset);
407 return -ENXIO;
408 }
409
e1455744
DW
410 return 0;
411}
32ab0a3f 412EXPORT_SYMBOL(nd_pfn_validate);
e1455744 413
200c79da 414int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
e1455744
DW
415{
416 int rc;
e1455744 417 struct nd_pfn *nd_pfn;
bd032943 418 struct device *pfn_dev;
e1455744
DW
419 struct nd_pfn_sb *pfn_sb;
420 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
421
422 if (ndns->force_raw)
423 return -ENODEV;
424
425 nvdimm_bus_lock(&ndns->dev);
bd032943 426 pfn_dev = __nd_pfn_create(nd_region, ndns);
e1455744 427 nvdimm_bus_unlock(&ndns->dev);
bd032943 428 if (!pfn_dev)
e1455744 429 return -ENOMEM;
bd032943
DW
430 pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
431 nd_pfn = to_nd_pfn(pfn_dev);
e1455744
DW
432 nd_pfn->pfn_sb = pfn_sb;
433 rc = nd_pfn_validate(nd_pfn);
bd032943
DW
434 dev_dbg(dev, "%s: pfn: %s\n", __func__,
435 rc == 0 ? dev_name(pfn_dev) : "<none>");
e1455744 436 if (rc < 0) {
bd032943
DW
437 __nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
438 put_device(pfn_dev);
e1455744 439 } else
bd032943 440 __nd_device_register(pfn_dev);
e1455744
DW
441
442 return rc;
443}
444EXPORT_SYMBOL(nd_pfn_probe);
ac515c08
DW
445
446/*
447 * We hotplug memory at section granularity, pad the reserved area from
448 * the previous section base to the namespace base address.
449 */
450static unsigned long init_altmap_base(resource_size_t base)
451{
452 unsigned long base_pfn = PHYS_PFN(base);
453
454 return PFN_SECTION_ALIGN_DOWN(base_pfn);
455}
456
457static unsigned long init_altmap_reserve(resource_size_t base)
458{
459 unsigned long reserve = PHYS_PFN(SZ_8K);
460 unsigned long base_pfn = PHYS_PFN(base);
461
462 reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
463 return reserve;
464}
465
466static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
467 struct resource *res, struct vmem_altmap *altmap)
468{
469 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
470 u64 offset = le64_to_cpu(pfn_sb->dataoff);
471 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
472 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
473 struct nd_namespace_common *ndns = nd_pfn->ndns;
474 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
475 resource_size_t base = nsio->res.start + start_pad;
476 struct vmem_altmap __altmap = {
477 .base_pfn = init_altmap_base(base),
478 .reserve = init_altmap_reserve(base),
479 };
480
481 memcpy(res, &nsio->res, sizeof(*res));
482 res->start += start_pad;
483 res->end -= end_trunc;
484
485 nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode);
486 if (nd_pfn->mode == PFN_MODE_RAM) {
487 if (offset < SZ_8K)
488 return ERR_PTR(-EINVAL);
489 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
490 altmap = NULL;
491 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
492 nd_pfn->npfns = (resource_size(res) - offset) / PAGE_SIZE;
493 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
494 dev_info(&nd_pfn->dev,
495 "number of pfns truncated from %lld to %ld\n",
496 le64_to_cpu(nd_pfn->pfn_sb->npfns),
497 nd_pfn->npfns);
498 memcpy(altmap, &__altmap, sizeof(*altmap));
499 altmap->free = PHYS_PFN(offset - SZ_8K);
500 altmap->alloc = 0;
501 } else
502 return ERR_PTR(-ENXIO);
503
504 return altmap;
505}
506
507static int nd_pfn_init(struct nd_pfn *nd_pfn)
508{
509 struct nd_namespace_common *ndns = nd_pfn->ndns;
510 u32 start_pad = 0, end_trunc = 0;
511 resource_size_t start, size;
512 struct nd_namespace_io *nsio;
513 struct nd_region *nd_region;
514 struct nd_pfn_sb *pfn_sb;
515 unsigned long npfns;
516 phys_addr_t offset;
517 u64 checksum;
518 int rc;
519
520 pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
521 if (!pfn_sb)
522 return -ENOMEM;
523
524 nd_pfn->pfn_sb = pfn_sb;
525 rc = nd_pfn_validate(nd_pfn);
526 if (rc != -ENODEV)
527 return rc;
528
529 /* no info block, do init */;
530 nd_region = to_nd_region(nd_pfn->dev.parent);
531 if (nd_region->ro) {
532 dev_info(&nd_pfn->dev,
533 "%s is read-only, unable to init metadata\n",
534 dev_name(&nd_region->dev));
535 return -ENXIO;
536 }
537
538 memset(pfn_sb, 0, sizeof(*pfn_sb));
539
540 /*
541 * Check if pmem collides with 'System RAM' when section aligned and
542 * trim it accordingly
543 */
544 nsio = to_nd_namespace_io(&ndns->dev);
545 start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
546 size = resource_size(&nsio->res);
547 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
548 IORES_DESC_NONE) == REGION_MIXED) {
549 start = nsio->res.start;
550 start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
551 }
552
553 start = nsio->res.start;
554 size = PHYS_SECTION_ALIGN_UP(start + size) - start;
555 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
556 IORES_DESC_NONE) == REGION_MIXED) {
557 size = resource_size(&nsio->res);
558 end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size);
559 }
560
561 if (start_pad + end_trunc)
562 dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n",
563 dev_name(&ndns->dev), start_pad + end_trunc);
564
565 /*
566 * Note, we use 64 here for the standard size of struct page,
567 * debugging options may cause it to be larger in which case the
568 * implementation will limit the pfns advertised through
569 * ->direct_access() to those that are included in the memmap.
570 */
571 start += start_pad;
572 size = resource_size(&nsio->res);
573 npfns = (size - start_pad - end_trunc - SZ_8K) / SZ_4K;
574 if (nd_pfn->mode == PFN_MODE_PMEM)
575 offset = ALIGN(start + SZ_8K + 64 * npfns, nd_pfn->align)
576 - start;
577 else if (nd_pfn->mode == PFN_MODE_RAM)
578 offset = ALIGN(start + SZ_8K, nd_pfn->align) - start;
579 else
580 return -ENXIO;
581
582 if (offset + start_pad + end_trunc >= size) {
583 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
584 dev_name(&ndns->dev));
585 return -ENXIO;
586 }
587
588 npfns = (size - offset - start_pad - end_trunc) / SZ_4K;
589 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
590 pfn_sb->dataoff = cpu_to_le64(offset);
591 pfn_sb->npfns = cpu_to_le64(npfns);
592 memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN);
593 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
594 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
595 pfn_sb->version_major = cpu_to_le16(1);
596 pfn_sb->version_minor = cpu_to_le16(1);
597 pfn_sb->start_pad = cpu_to_le32(start_pad);
598 pfn_sb->end_trunc = cpu_to_le32(end_trunc);
599 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
600 pfn_sb->checksum = cpu_to_le64(checksum);
601
602 return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
603}
604
605/*
606 * Determine the effective resource range and vmem_altmap from an nd_pfn
607 * instance.
608 */
609struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
610 struct resource *res, struct vmem_altmap *altmap)
611{
612 int rc;
613
614 if (!nd_pfn->uuid || !nd_pfn->ndns)
615 return ERR_PTR(-ENODEV);
616
617 rc = nd_pfn_init(nd_pfn);
618 if (rc)
619 return ERR_PTR(rc);
620
621 /* we need a valid pfn_sb before we can init a vmem_altmap */
622 return __nvdimm_setup_pfn(nd_pfn, res, altmap);
623}
624EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);