blob: 34db557dbad1923292c98473e5ed386d197355de [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williamse1455742015-07-30 17:57:47 -04002/*
Dan Williamscd034122016-03-11 10:15:36 -08003 * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
Dan Williamse1455742015-07-30 17:57:47 -04004 */
Dan Williamsac515c02016-03-22 00:29:43 -07005#include <linux/memremap.h>
Dan Williamse1455742015-07-30 17:57:47 -04006#include <linux/blkdev.h>
7#include <linux/device.h>
8#include <linux/genhd.h>
9#include <linux/sizes.h>
10#include <linux/slab.h>
11#include <linux/fs.h>
12#include <linux/mm.h>
13#include "nd-core.h"
14#include "pfn.h"
15#include "nd.h"
16
17static void nd_pfn_release(struct device *dev)
18{
19 struct nd_region *nd_region = to_nd_region(dev->parent);
20 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
21
Dan Williams426824d2018-03-05 16:39:31 -080022 dev_dbg(dev, "trace\n");
Dan Williamse1455742015-07-30 17:57:47 -040023 nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
24 ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
25 kfree(nd_pfn->uuid);
26 kfree(nd_pfn);
27}
28
Dan Williamse1455742015-07-30 17:57:47 -040029struct nd_pfn *to_nd_pfn(struct device *dev)
30{
31 struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
32
33 WARN_ON(!is_nd_pfn(dev));
34 return nd_pfn;
35}
36EXPORT_SYMBOL(to_nd_pfn);
37
38static ssize_t mode_show(struct device *dev,
39 struct device_attribute *attr, char *buf)
40{
Dan Williamscd034122016-03-11 10:15:36 -080041 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040042
43 switch (nd_pfn->mode) {
44 case PFN_MODE_RAM:
45 return sprintf(buf, "ram\n");
46 case PFN_MODE_PMEM:
47 return sprintf(buf, "pmem\n");
48 default:
49 return sprintf(buf, "none\n");
50 }
51}
52
53static ssize_t mode_store(struct device *dev,
54 struct device_attribute *attr, const char *buf, size_t len)
55{
Dan Williamscd034122016-03-11 10:15:36 -080056 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040057 ssize_t rc = 0;
58
Dan Williams87a30e12019-07-17 18:08:26 -070059 nd_device_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -040060 nvdimm_bus_lock(dev);
61 if (dev->driver)
62 rc = -EBUSY;
63 else {
64 size_t n = len - 1;
65
66 if (strncmp(buf, "pmem\n", n) == 0
67 || strncmp(buf, "pmem", n) == 0) {
Dan Williamsd2c0f042016-01-15 16:56:26 -080068 nd_pfn->mode = PFN_MODE_PMEM;
Dan Williamse1455742015-07-30 17:57:47 -040069 } else if (strncmp(buf, "ram\n", n) == 0
70 || strncmp(buf, "ram", n) == 0)
71 nd_pfn->mode = PFN_MODE_RAM;
72 else if (strncmp(buf, "none\n", n) == 0
73 || strncmp(buf, "none", n) == 0)
74 nd_pfn->mode = PFN_MODE_NONE;
75 else
76 rc = -EINVAL;
77 }
Dan Williams426824d2018-03-05 16:39:31 -080078 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
79 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -040080 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -070081 nd_device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -040082
83 return rc ? rc : len;
84}
85static DEVICE_ATTR_RW(mode);
86
Dan Williams315c5622015-12-10 14:45:23 -080087static ssize_t align_show(struct device *dev,
88 struct device_attribute *attr, char *buf)
89{
Dan Williamscd034122016-03-11 10:15:36 -080090 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williams315c5622015-12-10 14:45:23 -080091
Dan Williamsaf7d9f02016-12-10 08:12:05 -080092 return sprintf(buf, "%ld\n", nd_pfn->align);
Dan Williams315c5622015-12-10 14:45:23 -080093}
94
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +053095static unsigned long *nd_pfn_supported_alignments(unsigned long *alignments)
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +100096{
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +100097
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +053098 alignments[0] = PAGE_SIZE;
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +100099
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530100 if (has_transparent_hugepage()) {
101 alignments[1] = HPAGE_PMD_SIZE;
102 if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
103 alignments[2] = HPAGE_PUD_SIZE;
104 }
105
106 return alignments;
107}
108
109/*
110 * Use pmd mapping if supported as default alignment
111 */
112static unsigned long nd_pfn_default_alignment(void)
113{
114
115 if (has_transparent_hugepage())
116 return HPAGE_PMD_SIZE;
117 return PAGE_SIZE;
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000118}
119
Dan Williams315c5622015-12-10 14:45:23 -0800120static ssize_t align_store(struct device *dev,
121 struct device_attribute *attr, const char *buf, size_t len)
122{
Dan Williamscd034122016-03-11 10:15:36 -0800123 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530124 unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
Dan Williams315c5622015-12-10 14:45:23 -0800125 ssize_t rc;
126
Dan Williams87a30e12019-07-17 18:08:26 -0700127 nd_device_lock(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800128 nvdimm_bus_lock(dev);
Dan Williamsf13d2b62017-08-11 17:54:48 -0700129 rc = nd_size_select_store(dev, buf, &nd_pfn->align,
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530130 nd_pfn_supported_alignments(aligns));
Dan Williams426824d2018-03-05 16:39:31 -0800131 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
132 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams315c5622015-12-10 14:45:23 -0800133 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700134 nd_device_unlock(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800135
136 return rc ? rc : len;
137}
138static DEVICE_ATTR_RW(align);
139
Dan Williamse1455742015-07-30 17:57:47 -0400140static ssize_t uuid_show(struct device *dev,
141 struct device_attribute *attr, char *buf)
142{
Dan Williamscd034122016-03-11 10:15:36 -0800143 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400144
145 if (nd_pfn->uuid)
146 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
147 return sprintf(buf, "\n");
148}
149
150static ssize_t uuid_store(struct device *dev,
151 struct device_attribute *attr, const char *buf, size_t len)
152{
Dan Williamscd034122016-03-11 10:15:36 -0800153 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400154 ssize_t rc;
155
Dan Williams87a30e12019-07-17 18:08:26 -0700156 nd_device_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400157 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800158 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
159 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams87a30e12019-07-17 18:08:26 -0700160 nd_device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400161
162 return rc ? rc : len;
163}
164static DEVICE_ATTR_RW(uuid);
165
166static ssize_t namespace_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
168{
Dan Williamscd034122016-03-11 10:15:36 -0800169 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400170 ssize_t rc;
171
172 nvdimm_bus_lock(dev);
173 rc = sprintf(buf, "%s\n", nd_pfn->ndns
174 ? dev_name(&nd_pfn->ndns->dev) : "");
175 nvdimm_bus_unlock(dev);
176 return rc;
177}
178
179static ssize_t namespace_store(struct device *dev,
180 struct device_attribute *attr, const char *buf, size_t len)
181{
Dan Williamscd034122016-03-11 10:15:36 -0800182 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400183 ssize_t rc;
184
Dan Williams87a30e12019-07-17 18:08:26 -0700185 nd_device_lock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800186 nvdimm_bus_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400187 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800188 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
189 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -0400190 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700191 nd_device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400192
193 return rc;
194}
195static DEVICE_ATTR_RW(namespace);
196
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800197static ssize_t resource_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
199{
Dan Williamscd034122016-03-11 10:15:36 -0800200 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800201 ssize_t rc;
202
Dan Williams87a30e12019-07-17 18:08:26 -0700203 nd_device_lock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800204 if (dev->driver) {
205 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
206 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
207 struct nd_namespace_common *ndns = nd_pfn->ndns;
208 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
209 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
210
211 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
212 + start_pad + offset);
213 } else {
214 /* no address to convey if the pfn instance is disabled */
215 rc = -ENXIO;
216 }
Dan Williams87a30e12019-07-17 18:08:26 -0700217 nd_device_unlock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800218
219 return rc;
220}
Dan Williamsbfd2e912019-11-12 17:13:14 -0800221static DEVICE_ATTR(resource, 0400, resource_show, NULL);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800222
223static ssize_t size_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
Dan Williamscd034122016-03-11 10:15:36 -0800226 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800227 ssize_t rc;
228
Dan Williams87a30e12019-07-17 18:08:26 -0700229 nd_device_lock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800230 if (dev->driver) {
231 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
232 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
233 struct nd_namespace_common *ndns = nd_pfn->ndns;
234 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
235 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
236 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
237
238 rc = sprintf(buf, "%llu\n", (unsigned long long)
239 resource_size(&nsio->res) - start_pad
240 - end_trunc - offset);
241 } else {
242 /* no size to convey if the pfn instance is disabled */
243 rc = -ENXIO;
244 }
Dan Williams87a30e12019-07-17 18:08:26 -0700245 nd_device_unlock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800246
247 return rc;
248}
249static DEVICE_ATTR_RO(size);
250
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000251static ssize_t supported_alignments_show(struct device *dev,
252 struct device_attribute *attr, char *buf)
253{
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530254 unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
255
256 return nd_size_select_show(0,
257 nd_pfn_supported_alignments(aligns), buf);
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000258}
259static DEVICE_ATTR_RO(supported_alignments);
260
Dan Williamse1455742015-07-30 17:57:47 -0400261static struct attribute *nd_pfn_attributes[] = {
262 &dev_attr_mode.attr,
263 &dev_attr_namespace.attr,
264 &dev_attr_uuid.attr,
Dan Williams315c5622015-12-10 14:45:23 -0800265 &dev_attr_align.attr,
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800266 &dev_attr_resource.attr,
267 &dev_attr_size.attr,
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000268 &dev_attr_supported_alignments.attr,
Dan Williamse1455742015-07-30 17:57:47 -0400269 NULL,
270};
271
Dan Williams78c81cc2019-11-06 19:56:41 -0800272static struct attribute_group nd_pfn_attribute_group = {
Dan Williamse1455742015-07-30 17:57:47 -0400273 .attrs = nd_pfn_attributes,
274};
275
Dan Williams78c81cc2019-11-06 19:56:41 -0800276const struct attribute_group *nd_pfn_attribute_groups[] = {
Dan Williamse1455742015-07-30 17:57:47 -0400277 &nd_pfn_attribute_group,
278 &nd_device_attribute_group,
279 &nd_numa_attribute_group,
280 NULL,
281};
282
Dan Williams78c81cc2019-11-06 19:56:41 -0800283static const struct device_type nd_pfn_device_type = {
284 .name = "nd_pfn",
285 .release = nd_pfn_release,
286 .groups = nd_pfn_attribute_groups,
287};
288
289bool is_nd_pfn(struct device *dev)
290{
291 return dev ? dev->type == &nd_pfn_device_type : false;
292}
293EXPORT_SYMBOL(is_nd_pfn);
294
Dan Williamscd034122016-03-11 10:15:36 -0800295struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
Dan Williamse1455742015-07-30 17:57:47 -0400296 struct nd_namespace_common *ndns)
297{
Colin Ian King0cbfeef2018-02-05 14:08:52 +0000298 struct device *dev;
Dan Williamscd034122016-03-11 10:15:36 -0800299
300 if (!nd_pfn)
301 return NULL;
302
303 nd_pfn->mode = PFN_MODE_NONE;
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530304 nd_pfn->align = nd_pfn_default_alignment();
Dan Williamscd034122016-03-11 10:15:36 -0800305 dev = &nd_pfn->dev;
306 device_initialize(&nd_pfn->dev);
307 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
Dan Williams426824d2018-03-05 16:39:31 -0800308 dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
309 dev_name(ndns->claim));
Dan Williamscd034122016-03-11 10:15:36 -0800310 put_device(dev);
311 return NULL;
312 }
313 return dev;
314}
315
316static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
317{
Dan Williamse1455742015-07-30 17:57:47 -0400318 struct nd_pfn *nd_pfn;
319 struct device *dev;
320
Dan Williamse1455742015-07-30 17:57:47 -0400321 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
322 if (!nd_pfn)
323 return NULL;
324
325 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
326 if (nd_pfn->id < 0) {
327 kfree(nd_pfn);
328 return NULL;
329 }
330
Dan Williamse1455742015-07-30 17:57:47 -0400331 dev = &nd_pfn->dev;
332 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
Dan Williamscd034122016-03-11 10:15:36 -0800333 dev->type = &nd_pfn_device_type;
334 dev->parent = &nd_region->dev;
335
336 return nd_pfn;
Dan Williamse1455742015-07-30 17:57:47 -0400337}
338
339struct device *nd_pfn_create(struct nd_region *nd_region)
340{
Dan Williamscd034122016-03-11 10:15:36 -0800341 struct nd_pfn *nd_pfn;
342 struct device *dev;
Dan Williamse1455742015-07-30 17:57:47 -0400343
Dan Williamsc9e582a2017-05-29 23:12:19 -0700344 if (!is_memory(&nd_region->dev))
Dan Williamscd034122016-03-11 10:15:36 -0800345 return NULL;
346
347 nd_pfn = nd_pfn_alloc(nd_region);
348 dev = nd_pfn_devinit(nd_pfn, NULL);
349
350 __nd_device_register(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400351 return dev;
352}
353
Vishal Verma48af2f72018-09-18 17:48:31 -0600354/*
355 * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap
356 * space associated with the namespace. If the memmap is set to DRAM, then
357 * this is a no-op. Since the memmap area is freshly initialized during
358 * probe, we have an opportunity to clear any badblocks in this area.
359 */
360static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
361{
362 struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
363 struct nd_namespace_common *ndns = nd_pfn->ndns;
364 void *zero_page = page_address(ZERO_PAGE(0));
365 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
366 int num_bad, meta_num, rc, bb_present;
367 sector_t first_bad, meta_start;
368 struct nd_namespace_io *nsio;
369
370 if (nd_pfn->mode != PFN_MODE_PMEM)
371 return 0;
372
373 nsio = to_nd_namespace_io(&ndns->dev);
374 meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9;
375 meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start;
376
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530377 /*
378 * re-enable the namespace with correct size so that we can access
379 * the device memmap area.
380 */
381 devm_namespace_disable(&nd_pfn->dev, ndns);
382 rc = devm_namespace_enable(&nd_pfn->dev, ndns, le64_to_cpu(pfn_sb->dataoff));
383 if (rc)
384 return rc;
385
Vishal Verma48af2f72018-09-18 17:48:31 -0600386 do {
387 unsigned long zero_len;
388 u64 nsoff;
389
390 bb_present = badblocks_check(&nd_region->bb, meta_start,
391 meta_num, &first_bad, &num_bad);
392 if (bb_present) {
Christoph Hellwig72deb452019-04-05 18:08:59 +0200393 dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %llx\n",
Vishal Verma48af2f72018-09-18 17:48:31 -0600394 num_bad, first_bad);
395 nsoff = ALIGN_DOWN((nd_region->ndr_start
396 + (first_bad << 9)) - nsio->res.start,
397 PAGE_SIZE);
398 zero_len = ALIGN(num_bad << 9, PAGE_SIZE);
399 while (zero_len) {
400 unsigned long chunk = min(zero_len, PAGE_SIZE);
401
402 rc = nvdimm_write_bytes(ndns, nsoff, zero_page,
403 chunk, 0);
404 if (rc)
405 break;
406
407 zero_len -= chunk;
408 nsoff += chunk;
409 }
410 if (rc) {
411 dev_err(&nd_pfn->dev,
Christoph Hellwig72deb452019-04-05 18:08:59 +0200412 "error clearing %x badblocks at %llx\n",
Vishal Verma48af2f72018-09-18 17:48:31 -0600413 num_bad, first_bad);
414 return rc;
415 }
416 }
417 } while (bb_present);
418
419 return 0;
420}
421
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530422static bool nd_supported_alignment(unsigned long align)
423{
424 int i;
425 unsigned long supported[MAX_NVDIMM_ALIGN] = { [0] = 0, };
426
427 if (align == 0)
428 return false;
429
430 nd_pfn_supported_alignments(supported);
431 for (i = 0; supported[i]; i++)
432 if (align == supported[i])
433 return true;
434 return false;
435}
436
Dan Williams7e3e8882019-07-18 15:58:36 -0700437/**
438 * nd_pfn_validate - read and validate info-block
439 * @nd_pfn: fsdax namespace runtime state / properties
440 * @sig: 'devdax' or 'fsdax' signature
441 *
442 * Upon return the info-block buffer contents (->pfn_sb) are
443 * indeterminate when validation fails, and a coherent info-block
444 * otherwise.
445 */
Dan Williamsc5ed9262016-05-18 14:50:12 -0700446int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
Dan Williamse1455742015-07-30 17:57:47 -0400447{
Dan Williamse1455742015-07-30 17:57:47 -0400448 u64 checksum, offset;
Dan Williams6acd7d52020-02-27 21:39:23 -0800449 struct resource *res;
Dan Williams1ee66672016-06-23 17:50:39 -0700450 enum nd_pfn_mode mode;
Dan Williamsa34d5e82015-12-12 16:09:14 -0800451 struct nd_namespace_io *nsio;
Dan Williams19deaa22017-12-19 15:07:10 -0800452 unsigned long align, start_pad;
Dan Williamsa34d5e82015-12-12 16:09:14 -0800453 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
454 struct nd_namespace_common *ndns = nd_pfn->ndns;
455 const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
Dan Williamse1455742015-07-30 17:57:47 -0400456
457 if (!pfn_sb || !ndns)
458 return -ENODEV;
459
Dan Williamsc9e582a2017-05-29 23:12:19 -0700460 if (!is_memory(nd_pfn->dev.parent))
Dan Williamse1455742015-07-30 17:57:47 -0400461 return -ENODEV;
462
Vishal Verma3ae3d672017-05-10 15:01:30 -0600463 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
Dan Williamse1455742015-07-30 17:57:47 -0400464 return -ENXIO;
465
Dan Williamsc5ed9262016-05-18 14:50:12 -0700466 if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
Dan Williamse1455742015-07-30 17:57:47 -0400467 return -ENODEV;
468
469 checksum = le64_to_cpu(pfn_sb->checksum);
470 pfn_sb->checksum = 0;
471 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
472 return -ENODEV;
473 pfn_sb->checksum = cpu_to_le64(checksum);
474
Dan Williamsa34d5e82015-12-12 16:09:14 -0800475 if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
476 return -ENODEV;
477
Dan Williamscfe30b82016-03-03 09:38:00 -0800478 if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
479 pfn_sb->start_pad = 0;
480 pfn_sb->end_trunc = 0;
481 }
482
Dan Williams45a0dac2016-03-31 15:41:18 -0700483 if (__le16_to_cpu(pfn_sb->version_minor) < 2)
484 pfn_sb->align = 0;
485
Aneesh Kumar K.Vedbb52c2019-09-05 21:16:00 +0530486 if (__le16_to_cpu(pfn_sb->version_minor) < 4) {
487 pfn_sb->page_struct_size = cpu_to_le16(64);
488 pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
489 }
490
Dan Williamse1455742015-07-30 17:57:47 -0400491 switch (le32_to_cpu(pfn_sb->mode)) {
492 case PFN_MODE_RAM:
Dan Williamse1455742015-07-30 17:57:47 -0400493 case PFN_MODE_PMEM:
Dan Williams45eb5702016-01-29 17:42:51 -0800494 break;
Dan Williamse1455742015-07-30 17:57:47 -0400495 default:
496 return -ENXIO;
497 }
498
Dan Williams1ee66672016-06-23 17:50:39 -0700499 align = le32_to_cpu(pfn_sb->align);
500 offset = le64_to_cpu(pfn_sb->dataoff);
Dan Williams19deaa22017-12-19 15:07:10 -0800501 start_pad = le32_to_cpu(pfn_sb->start_pad);
Dan Williams1ee66672016-06-23 17:50:39 -0700502 if (align == 0)
503 align = 1UL << ilog2(offset);
504 mode = le32_to_cpu(pfn_sb->mode);
505
Aneesh Kumar K.Vedbb52c2019-09-05 21:16:00 +0530506 if ((le32_to_cpu(pfn_sb->page_size) > PAGE_SIZE) &&
507 (mode == PFN_MODE_PMEM)) {
508 dev_err(&nd_pfn->dev,
509 "init failed, page size mismatch %d\n",
510 le32_to_cpu(pfn_sb->page_size));
511 return -EOPNOTSUPP;
512 }
513
514 if ((le16_to_cpu(pfn_sb->page_struct_size) < sizeof(struct page)) &&
515 (mode == PFN_MODE_PMEM)) {
516 dev_err(&nd_pfn->dev,
517 "init failed, struct page size mismatch %d\n",
518 le16_to_cpu(pfn_sb->page_struct_size));
519 return -EOPNOTSUPP;
520 }
521
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530522 /*
523 * Check whether the we support the alignment. For Dax if the
524 * superblock alignment is not matching, we won't initialize
525 * the device.
526 */
527 if (!nd_supported_alignment(align) &&
528 !memcmp(pfn_sb->signature, DAX_SIG, PFN_SIG_LEN)) {
529 dev_err(&nd_pfn->dev, "init failed, alignment mismatch: "
530 "%ld:%ld\n", nd_pfn->align, align);
531 return -EOPNOTSUPP;
532 }
533
Dan Williamse1455742015-07-30 17:57:47 -0400534 if (!nd_pfn->uuid) {
Dan Williams1ee66672016-06-23 17:50:39 -0700535 /*
536 * When probing a namepace via nd_pfn_probe() the uuid
537 * is NULL (see: nd_pfn_devinit()) we init settings from
538 * pfn_sb
539 */
Dan Williamse1455742015-07-30 17:57:47 -0400540 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
541 if (!nd_pfn->uuid)
542 return -ENOMEM;
Dan Williams1ee66672016-06-23 17:50:39 -0700543 nd_pfn->align = align;
544 nd_pfn->mode = mode;
Dan Williamse1455742015-07-30 17:57:47 -0400545 } else {
Dan Williams1ee66672016-06-23 17:50:39 -0700546 /*
547 * When probing a pfn / dax instance we validate the
548 * live settings against the pfn_sb
549 */
Dan Williamse1455742015-07-30 17:57:47 -0400550 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
Dan Williamse5670562016-04-07 19:59:27 -0700551 return -ENODEV;
Dan Williams1ee66672016-06-23 17:50:39 -0700552
553 /*
554 * If the uuid validates, but other settings mismatch
555 * return EINVAL because userspace has managed to change
556 * the configuration without specifying new
557 * identification.
558 */
559 if (nd_pfn->align != align || nd_pfn->mode != mode) {
560 dev_err(&nd_pfn->dev,
561 "init failed, settings mismatch\n");
562 dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
563 nd_pfn->align, align, nd_pfn->mode,
564 mode);
Dan Williamsb2ba7e92020-02-27 21:31:45 -0800565 return -EOPNOTSUPP;
Dan Williams1ee66672016-06-23 17:50:39 -0700566 }
Dan Williamse1455742015-07-30 17:57:47 -0400567 }
568
Dan Williams1ee66672016-06-23 17:50:39 -0700569 if (align > nvdimm_namespace_capacity(ndns)) {
Dan Williams315c5622015-12-10 14:45:23 -0800570 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
Dan Williams1ee66672016-06-23 17:50:39 -0700571 align, nvdimm_namespace_capacity(ndns));
Dan Williamsb2ba7e92020-02-27 21:31:45 -0800572 return -EOPNOTSUPP;
Dan Williams315c5622015-12-10 14:45:23 -0800573 }
574
Dan Williamse1455742015-07-30 17:57:47 -0400575 /*
576 * These warnings are verbose because they can only trigger in
577 * the case where the physical address alignment of the
578 * namespace has changed since the pfn superblock was
579 * established.
580 */
Dan Williamse1455742015-07-30 17:57:47 -0400581 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams6acd7d52020-02-27 21:39:23 -0800582 res = &nsio->res;
583 if (offset >= resource_size(res)) {
Dan Williamse1455742015-07-30 17:57:47 -0400584 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
585 dev_name(&ndns->dev));
Dan Williamsb2ba7e92020-02-27 21:31:45 -0800586 return -EOPNOTSUPP;
Dan Williamse1455742015-07-30 17:57:47 -0400587 }
588
Dan Williams6acd7d52020-02-27 21:39:23 -0800589 if ((align && !IS_ALIGNED(res->start + offset + start_pad, align))
Dan Williams5e24c9f2016-05-21 11:01:41 -0700590 || !IS_ALIGNED(offset, PAGE_SIZE)) {
Dan Williams1ee66672016-06-23 17:50:39 -0700591 dev_err(&nd_pfn->dev,
592 "bad offset: %#llx dax disabled align: %#lx\n",
593 offset, align);
Dan Williamsb2ba7e92020-02-27 21:31:45 -0800594 return -EOPNOTSUPP;
Dan Williams315c5622015-12-10 14:45:23 -0800595 }
596
Dan Williams6acd7d52020-02-27 21:39:23 -0800597 if (!IS_ALIGNED(res->start + le32_to_cpu(pfn_sb->start_pad),
598 memremap_compat_align())) {
599 dev_err(&nd_pfn->dev, "resource start misaligned\n");
600 return -EOPNOTSUPP;
601 }
602
603 if (!IS_ALIGNED(res->end + 1 - le32_to_cpu(pfn_sb->end_trunc),
604 memremap_compat_align())) {
605 dev_err(&nd_pfn->dev, "resource end misaligned\n");
606 return -EOPNOTSUPP;
607 }
608
Aneesh Kumar K.Vc1f45d82019-11-01 08:57:28 +0530609 return 0;
Dan Williamse1455742015-07-30 17:57:47 -0400610}
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400611EXPORT_SYMBOL(nd_pfn_validate);
Dan Williamse1455742015-07-30 17:57:47 -0400612
Dan Williams200c79d2016-03-22 00:22:16 -0700613int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
Dan Williamse1455742015-07-30 17:57:47 -0400614{
615 int rc;
Dan Williamse1455742015-07-30 17:57:47 -0400616 struct nd_pfn *nd_pfn;
Dan Williamsbd032942016-03-17 18:16:15 -0700617 struct device *pfn_dev;
Dan Williamse1455742015-07-30 17:57:47 -0400618 struct nd_pfn_sb *pfn_sb;
619 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
620
621 if (ndns->force_raw)
622 return -ENODEV;
623
Dan Williamsb3fde742017-06-04 10:18:39 +0900624 switch (ndns->claim_class) {
625 case NVDIMM_CCLASS_NONE:
626 case NVDIMM_CCLASS_PFN:
627 break;
628 default:
629 return -ENODEV;
630 }
631
Dan Williamse1455742015-07-30 17:57:47 -0400632 nvdimm_bus_lock(&ndns->dev);
Dan Williamscd034122016-03-11 10:15:36 -0800633 nd_pfn = nd_pfn_alloc(nd_region);
634 pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
Dan Williamse1455742015-07-30 17:57:47 -0400635 nvdimm_bus_unlock(&ndns->dev);
Dan Williamsbd032942016-03-17 18:16:15 -0700636 if (!pfn_dev)
Dan Williamse1455742015-07-30 17:57:47 -0400637 return -ENOMEM;
Dan Williams7e3e8882019-07-18 15:58:36 -0700638 pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
Dan Williamsbd032942016-03-17 18:16:15 -0700639 nd_pfn = to_nd_pfn(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400640 nd_pfn->pfn_sb = pfn_sb;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700641 rc = nd_pfn_validate(nd_pfn, PFN_SIG);
Dan Williams426824d2018-03-05 16:39:31 -0800642 dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>");
Dan Williamse1455742015-07-30 17:57:47 -0400643 if (rc < 0) {
Dan Williams452bae02017-04-28 22:05:14 -0700644 nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
Dan Williamsbd032942016-03-17 18:16:15 -0700645 put_device(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400646 } else
Dan Williamsbd032942016-03-17 18:16:15 -0700647 __nd_device_register(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400648
649 return rc;
650}
651EXPORT_SYMBOL(nd_pfn_probe);
Dan Williamsac515c02016-03-22 00:29:43 -0700652
653/*
Dan Williamsa3619192019-07-18 15:58:40 -0700654 * We hotplug memory at sub-section granularity, pad the reserved area
655 * from the previous section base to the namespace base address.
Dan Williamsac515c02016-03-22 00:29:43 -0700656 */
657static unsigned long init_altmap_base(resource_size_t base)
658{
659 unsigned long base_pfn = PHYS_PFN(base);
660
Dan Williamsa3619192019-07-18 15:58:40 -0700661 return SUBSECTION_ALIGN_DOWN(base_pfn);
Dan Williamsac515c02016-03-22 00:29:43 -0700662}
663
664static unsigned long init_altmap_reserve(resource_size_t base)
665{
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530666 unsigned long reserve = nd_info_block_reserve() >> PAGE_SHIFT;
Dan Williamsac515c02016-03-22 00:29:43 -0700667 unsigned long base_pfn = PHYS_PFN(base);
668
Dan Williamsa3619192019-07-18 15:58:40 -0700669 reserve += base_pfn - SUBSECTION_ALIGN_DOWN(base_pfn);
Dan Williamsac515c02016-03-22 00:29:43 -0700670 return reserve;
671}
672
Christoph Hellwige8d51342017-12-29 08:54:05 +0100673static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
Dan Williamsac515c02016-03-22 00:29:43 -0700674{
Christoph Hellwige8d51342017-12-29 08:54:05 +0100675 struct resource *res = &pgmap->res;
676 struct vmem_altmap *altmap = &pgmap->altmap;
Dan Williamsac515c02016-03-22 00:29:43 -0700677 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
678 u64 offset = le64_to_cpu(pfn_sb->dataoff);
679 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
680 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530681 u32 reserve = nd_info_block_reserve();
Dan Williamsac515c02016-03-22 00:29:43 -0700682 struct nd_namespace_common *ndns = nd_pfn->ndns;
683 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
684 resource_size_t base = nsio->res.start + start_pad;
Aneesh Kumar K.Vcf387d92019-09-10 11:58:25 +0530685 resource_size_t end = nsio->res.end - end_trunc;
Dan Williamsac515c02016-03-22 00:29:43 -0700686 struct vmem_altmap __altmap = {
687 .base_pfn = init_altmap_base(base),
688 .reserve = init_altmap_reserve(base),
Aneesh Kumar K.Vcf387d92019-09-10 11:58:25 +0530689 .end_pfn = PHYS_PFN(end),
Dan Williamsac515c02016-03-22 00:29:43 -0700690 };
691
692 memcpy(res, &nsio->res, sizeof(*res));
693 res->start += start_pad;
694 res->end -= end_trunc;
695
Dan Williamsac515c02016-03-22 00:29:43 -0700696 if (nd_pfn->mode == PFN_MODE_RAM) {
Dan Williams11a35812019-02-06 13:04:53 +1100697 if (offset < reserve)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100698 return -EINVAL;
Dan Williamsac515c02016-03-22 00:29:43 -0700699 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
Dan Williamsac515c02016-03-22 00:29:43 -0700700 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
Dan Williamsa3619192019-07-18 15:58:40 -0700701 nd_pfn->npfns = PHYS_PFN((resource_size(res) - offset));
Dan Williamsac515c02016-03-22 00:29:43 -0700702 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
703 dev_info(&nd_pfn->dev,
704 "number of pfns truncated from %lld to %ld\n",
705 le64_to_cpu(nd_pfn->pfn_sb->npfns),
706 nd_pfn->npfns);
707 memcpy(altmap, &__altmap, sizeof(*altmap));
Dan Williams11a35812019-02-06 13:04:53 +1100708 altmap->free = PHYS_PFN(offset - reserve);
Dan Williamsac515c02016-03-22 00:29:43 -0700709 altmap->alloc = 0;
Christoph Hellwig514caf22019-06-26 14:27:13 +0200710 pgmap->flags |= PGMAP_ALTMAP_VALID;
Dan Williamsac515c02016-03-22 00:29:43 -0700711 } else
Christoph Hellwige8d51342017-12-29 08:54:05 +0100712 return -ENXIO;
Dan Williamsac515c02016-03-22 00:29:43 -0700713
Christoph Hellwige8d51342017-12-29 08:54:05 +0100714 return 0;
Dan Williamsac515c02016-03-22 00:29:43 -0700715}
716
717static int nd_pfn_init(struct nd_pfn *nd_pfn)
718{
719 struct nd_namespace_common *ndns = nd_pfn->ndns;
Dan Williamsae86cbf2018-11-24 10:47:04 -0800720 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
Dan Williamsac515c02016-03-22 00:29:43 -0700721 resource_size_t start, size;
Dan Williamsac515c02016-03-22 00:29:43 -0700722 struct nd_region *nd_region;
Dan Williamsa3619192019-07-18 15:58:40 -0700723 unsigned long npfns, align;
Jeff Moyer274b9242019-08-28 11:49:46 -0400724 u32 end_trunc;
Dan Williamsac515c02016-03-22 00:29:43 -0700725 struct nd_pfn_sb *pfn_sb;
Dan Williamsac515c02016-03-22 00:29:43 -0700726 phys_addr_t offset;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700727 const char *sig;
Dan Williamsac515c02016-03-22 00:29:43 -0700728 u64 checksum;
729 int rc;
730
Dan Williams7e3e8882019-07-18 15:58:36 -0700731 pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
Dan Williamsac515c02016-03-22 00:29:43 -0700732 if (!pfn_sb)
733 return -ENOMEM;
734
735 nd_pfn->pfn_sb = pfn_sb;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700736 if (is_nd_dax(&nd_pfn->dev))
737 sig = DAX_SIG;
738 else
739 sig = PFN_SIG;
Dan Williams7e3e8882019-07-18 15:58:36 -0700740
Dan Williamsc5ed9262016-05-18 14:50:12 -0700741 rc = nd_pfn_validate(nd_pfn, sig);
Aneesh Kumar K.Vc1f45d82019-11-01 08:57:28 +0530742 if (rc == 0)
743 return nd_pfn_clear_memmap_errors(nd_pfn);
Dan Williamsac515c02016-03-22 00:29:43 -0700744 if (rc != -ENODEV)
745 return rc;
746
747 /* no info block, do init */;
Dan Williams7e3e8882019-07-18 15:58:36 -0700748 memset(pfn_sb, 0, sizeof(*pfn_sb));
749
Dan Williamsac515c02016-03-22 00:29:43 -0700750 nd_region = to_nd_region(nd_pfn->dev.parent);
751 if (nd_region->ro) {
752 dev_info(&nd_pfn->dev,
753 "%s is read-only, unable to init metadata\n",
754 dev_name(&nd_region->dev));
755 return -ENXIO;
756 }
757
Dan Williamsac515c02016-03-22 00:29:43 -0700758 /*
759 * Note, we use 64 here for the standard size of struct page,
760 * debugging options may cause it to be larger in which case the
761 * implementation will limit the pfns advertised through
762 * ->direct_access() to those that are included in the memmap.
763 */
Dan Williamsa3619192019-07-18 15:58:40 -0700764 start = nsio->res.start;
Dan Williamsac515c02016-03-22 00:29:43 -0700765 size = resource_size(&nsio->res);
Dan Williamsa3619192019-07-18 15:58:40 -0700766 npfns = PHYS_PFN(size - SZ_8K);
Dan Williams6acd7d52020-02-27 21:39:23 -0800767 align = max(nd_pfn->align, memremap_compat_align());
768
769 /*
770 * When @start is misaligned fail namespace creation. See
771 * the 'struct nd_pfn_sb' commentary on why ->start_pad is not
772 * an option.
773 */
774 if (!IS_ALIGNED(start, memremap_compat_align())) {
775 dev_err(&nd_pfn->dev, "%s: start %pa misaligned to %#lx\n",
776 dev_name(&ndns->dev), &start,
777 memremap_compat_align());
778 return -EINVAL;
779 }
Jeff Moyer274b9242019-08-28 11:49:46 -0400780 end_trunc = start + size - ALIGN_DOWN(start + size, align);
Dan Williams594d6d92016-05-18 09:59:34 -0700781 if (nd_pfn->mode == PFN_MODE_PMEM) {
Dan Williams594d6d92016-05-18 09:59:34 -0700782 /*
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000783 * The altmap should be padded out to the block size used
784 * when populating the vmemmap. This *should* be equal to
785 * PMD_SIZE for most architectures.
Aneesh Kumar K.Ve96f0bf2019-09-05 21:15:59 +0530786 *
787 * Also make sure size of struct page is less than 64. We
788 * want to make sure we use large enough size here so that
789 * we don't have a dynamic reserve space depending on
790 * struct page size. But we also want to make sure we notice
791 * when we end up adding new elements to struct page.
Dan Williams594d6d92016-05-18 09:59:34 -0700792 */
Aneesh Kumar K.Ve96f0bf2019-09-05 21:15:59 +0530793 BUILD_BUG_ON(sizeof(struct page) > MAX_STRUCT_PAGE_SIZE);
794 offset = ALIGN(start + SZ_8K + MAX_STRUCT_PAGE_SIZE * npfns, align)
795 - start;
Dan Williams594d6d92016-05-18 09:59:34 -0700796 } else if (nd_pfn->mode == PFN_MODE_RAM)
Dan Williamsa3619192019-07-18 15:58:40 -0700797 offset = ALIGN(start + SZ_8K, align) - start;
Dan Williamsac515c02016-03-22 00:29:43 -0700798 else
799 return -ENXIO;
800
Dan Williamsa3619192019-07-18 15:58:40 -0700801 if (offset >= size) {
Dan Williamsac515c02016-03-22 00:29:43 -0700802 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
803 dev_name(&ndns->dev));
804 return -ENXIO;
805 }
806
Jeff Moyer274b9242019-08-28 11:49:46 -0400807 npfns = PHYS_PFN(size - offset - end_trunc);
Dan Williamsac515c02016-03-22 00:29:43 -0700808 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
809 pfn_sb->dataoff = cpu_to_le64(offset);
810 pfn_sb->npfns = cpu_to_le64(npfns);
Dan Williamsc5ed9262016-05-18 14:50:12 -0700811 memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
Dan Williamsac515c02016-03-22 00:29:43 -0700812 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
813 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
814 pfn_sb->version_major = cpu_to_le16(1);
Aneesh Kumar K.Vedbb52c2019-09-05 21:16:00 +0530815 pfn_sb->version_minor = cpu_to_le16(4);
Jeff Moyer274b9242019-08-28 11:49:46 -0400816 pfn_sb->end_trunc = cpu_to_le32(end_trunc);
Dan Williams45a0dac2016-03-31 15:41:18 -0700817 pfn_sb->align = cpu_to_le32(nd_pfn->align);
Aneesh Kumar K.Vedbb52c2019-09-05 21:16:00 +0530818 pfn_sb->page_struct_size = cpu_to_le16(MAX_STRUCT_PAGE_SIZE);
819 pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
Dan Williamsac515c02016-03-22 00:29:43 -0700820 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
821 pfn_sb->checksum = cpu_to_le64(checksum);
822
Aneesh Kumar K.Vc1f45d82019-11-01 08:57:28 +0530823 rc = nd_pfn_clear_memmap_errors(nd_pfn);
824 if (rc)
825 return rc;
826
Vishal Verma3ae3d672017-05-10 15:01:30 -0600827 return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
Dan Williamsac515c02016-03-22 00:29:43 -0700828}
829
830/*
831 * Determine the effective resource range and vmem_altmap from an nd_pfn
832 * instance.
833 */
Christoph Hellwige8d51342017-12-29 08:54:05 +0100834int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
Dan Williamsac515c02016-03-22 00:29:43 -0700835{
836 int rc;
837
838 if (!nd_pfn->uuid || !nd_pfn->ndns)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100839 return -ENODEV;
Dan Williamsac515c02016-03-22 00:29:43 -0700840
841 rc = nd_pfn_init(nd_pfn);
842 if (rc)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100843 return rc;
Dan Williamsac515c02016-03-22 00:29:43 -0700844
Christoph Hellwige8d51342017-12-29 08:54:05 +0100845 /* we need a valid pfn_sb before we can init a dev_pagemap */
846 return __nvdimm_setup_pfn(nd_pfn, pgmap);
Dan Williamsac515c02016-03-22 00:29:43 -0700847}
848EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);