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