pfn_devs.c 21 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
4
 */
5
#include <linux/memremap.h>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <linux/blkdev.h>
#include <linux/device.h>
#include <linux/genhd.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include "nd-core.h"
#include "pfn.h"
#include "nd.h"

static void nd_pfn_release(struct device *dev)
{
	struct nd_region *nd_region = to_nd_region(dev->parent);
	struct nd_pfn *nd_pfn = to_nd_pfn(dev);

22
	dev_dbg(dev, "trace\n");
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
	ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
	kfree(nd_pfn->uuid);
	kfree(nd_pfn);
}

static struct device_type nd_pfn_device_type = {
	.name = "nd_pfn",
	.release = nd_pfn_release,
};

bool is_nd_pfn(struct device *dev)
{
	return dev ? dev->type == &nd_pfn_device_type : false;
}
EXPORT_SYMBOL(is_nd_pfn);

struct nd_pfn *to_nd_pfn(struct device *dev)
{
	struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);

	WARN_ON(!is_nd_pfn(dev));
	return nd_pfn;
}
EXPORT_SYMBOL(to_nd_pfn);

static ssize_t mode_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
52
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
53
54
55
56
57
58
59
60
61
62
63
64
65
66

	switch (nd_pfn->mode) {
	case PFN_MODE_RAM:
		return sprintf(buf, "ram\n");
	case PFN_MODE_PMEM:
		return sprintf(buf, "pmem\n");
	default:
		return sprintf(buf, "none\n");
	}
}

static ssize_t mode_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t len)
{
67
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
68
69
	ssize_t rc = 0;

70
	nd_device_lock(dev);
71
72
73
74
75
76
77
78
	nvdimm_bus_lock(dev);
	if (dev->driver)
		rc = -EBUSY;
	else {
		size_t n = len - 1;

		if (strncmp(buf, "pmem\n", n) == 0
				|| strncmp(buf, "pmem", n) == 0) {
79
			nd_pfn->mode = PFN_MODE_PMEM;
80
81
82
83
84
85
86
87
88
		} else if (strncmp(buf, "ram\n", n) == 0
				|| strncmp(buf, "ram", n) == 0)
			nd_pfn->mode = PFN_MODE_RAM;
		else if (strncmp(buf, "none\n", n) == 0
				|| strncmp(buf, "none", n) == 0)
			nd_pfn->mode = PFN_MODE_NONE;
		else
			rc = -EINVAL;
	}
89
90
	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
			buf[len - 1] == '\n' ? "" : "\n");
91
	nvdimm_bus_unlock(dev);
92
	nd_device_unlock(dev);
93
94
95
96
97

	return rc ? rc : len;
}
static DEVICE_ATTR_RW(mode);

98
99
100
static ssize_t align_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
101
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
102

103
	return sprintf(buf, "%ld\n", nd_pfn->align);
104
105
}

106
static unsigned long *nd_pfn_supported_alignments(unsigned long *alignments)
107
108
{

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	alignments[0] = PAGE_SIZE;

	if (has_transparent_hugepage()) {
		alignments[1] = HPAGE_PMD_SIZE;
		if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
			alignments[2] = HPAGE_PUD_SIZE;
	}

	return alignments;
}

/*
 * Use pmd mapping if supported as default alignment
 */
static unsigned long nd_pfn_default_alignment(void)
{
125

126
127
128
	if (has_transparent_hugepage())
		return HPAGE_PMD_SIZE;
	return PAGE_SIZE;
129
130
}

131
132
133
static ssize_t align_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t len)
{
134
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
135
	unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
136
137
	ssize_t rc;

138
	nd_device_lock(dev);
139
	nvdimm_bus_lock(dev);
140
	rc = nd_size_select_store(dev, buf, &nd_pfn->align,
141
			nd_pfn_supported_alignments(aligns));
142
143
	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
			buf[len - 1] == '\n' ? "" : "\n");
144
	nvdimm_bus_unlock(dev);
145
	nd_device_unlock(dev);
146
147
148
149
150

	return rc ? rc : len;
}
static DEVICE_ATTR_RW(align);

151
152
153
static ssize_t uuid_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
154
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
155
156
157
158
159
160
161
162
163

	if (nd_pfn->uuid)
		return sprintf(buf, "%pUb\n", nd_pfn->uuid);
	return sprintf(buf, "\n");
}

static ssize_t uuid_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t len)
{
164
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
165
166
	ssize_t rc;

167
	nd_device_lock(dev);
168
	rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
169
170
	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
			buf[len - 1] == '\n' ? "" : "\n");
171
	nd_device_unlock(dev);
172
173
174
175
176
177
178
179

	return rc ? rc : len;
}
static DEVICE_ATTR_RW(uuid);

static ssize_t namespace_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
180
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
181
182
183
184
185
186
187
188
189
190
191
192
	ssize_t rc;

	nvdimm_bus_lock(dev);
	rc = sprintf(buf, "%s\n", nd_pfn->ndns
			? dev_name(&nd_pfn->ndns->dev) : "");
	nvdimm_bus_unlock(dev);
	return rc;
}

static ssize_t namespace_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t len)
{
193
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
194
195
	ssize_t rc;

196
	nd_device_lock(dev);
197
	nvdimm_bus_lock(dev);
198
	rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
199
200
	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
			buf[len - 1] == '\n' ? "" : "\n");
201
	nvdimm_bus_unlock(dev);
202
	nd_device_unlock(dev);
203
204
205
206
207

	return rc;
}
static DEVICE_ATTR_RW(namespace);

208
209
210
static ssize_t resource_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
211
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
212
213
	ssize_t rc;

214
	nd_device_lock(dev);
215
216
217
218
219
220
221
222
223
224
225
226
227
	if (dev->driver) {
		struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
		u64 offset = __le64_to_cpu(pfn_sb->dataoff);
		struct nd_namespace_common *ndns = nd_pfn->ndns;
		u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
		struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);

		rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
				+ start_pad + offset);
	} else {
		/* no address to convey if the pfn instance is disabled */
		rc = -ENXIO;
	}
228
	nd_device_unlock(dev);
229
230
231
232
233
234
235
236

	return rc;
}
static DEVICE_ATTR_RO(resource);

static ssize_t size_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
237
	struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
238
239
	ssize_t rc;

240
	nd_device_lock(dev);
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
	if (dev->driver) {
		struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
		u64 offset = __le64_to_cpu(pfn_sb->dataoff);
		struct nd_namespace_common *ndns = nd_pfn->ndns;
		u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
		u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
		struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);

		rc = sprintf(buf, "%llu\n", (unsigned long long)
				resource_size(&nsio->res) - start_pad
				- end_trunc - offset);
	} else {
		/* no size to convey if the pfn instance is disabled */
		rc = -ENXIO;
	}
256
	nd_device_unlock(dev);
257
258
259
260
261

	return rc;
}
static DEVICE_ATTR_RO(size);

262
263
264
static ssize_t supported_alignments_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
265
266
267
268
	unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };

	return nd_size_select_show(0,
			nd_pfn_supported_alignments(aligns), buf);
269
270
271
}
static DEVICE_ATTR_RO(supported_alignments);

272
273
274
275
static struct attribute *nd_pfn_attributes[] = {
	&dev_attr_mode.attr,
	&dev_attr_namespace.attr,
	&dev_attr_uuid.attr,
276
	&dev_attr_align.attr,
277
278
	&dev_attr_resource.attr,
	&dev_attr_size.attr,
279
	&dev_attr_supported_alignments.attr,
280
281
282
	NULL,
};

283
284
285
286
287
288
289
static umode_t pfn_visible(struct kobject *kobj, struct attribute *a, int n)
{
	if (a == &dev_attr_resource.attr)
		return 0400;
	return a->mode;
}

290
struct attribute_group nd_pfn_attribute_group = {
291
	.attrs = nd_pfn_attributes,
292
	.is_visible = pfn_visible,
293
294
295
296
297
298
299
300
301
};

static const struct attribute_group *nd_pfn_attribute_groups[] = {
	&nd_pfn_attribute_group,
	&nd_device_attribute_group,
	&nd_numa_attribute_group,
	NULL,
};

302
struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
303
304
		struct nd_namespace_common *ndns)
{
305
	struct device *dev;
306

307
	if (!nd_pfn)
308
309
		return NULL;

310
	nd_pfn->mode = PFN_MODE_NONE;
311
	nd_pfn->align = nd_pfn_default_alignment();
312
313
314
	dev = &nd_pfn->dev;
	device_initialize(&nd_pfn->dev);
	if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
315
316
		dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
				dev_name(ndns->claim));
317
		put_device(dev);
318
		return NULL;
319
320
321
322
323
324
325
326
	}
	return dev;
}

static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
{
	struct nd_pfn *nd_pfn;
	struct device *dev;
327
328
329
330
331
332
333
334
335
336
337
338
339
340

	nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
	if (!nd_pfn)
		return NULL;

	nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
	if (nd_pfn->id < 0) {
		kfree(nd_pfn);
		return NULL;
	}

	dev = &nd_pfn->dev;
	dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
	dev->groups = nd_pfn_attribute_groups;
341
342
343
344
	dev->type = &nd_pfn_device_type;
	dev->parent = &nd_region->dev;

	return nd_pfn;
345
346
347
348
}

struct device *nd_pfn_create(struct nd_region *nd_region)
{
349
350
351
	struct nd_pfn *nd_pfn;
	struct device *dev;

352
	if (!is_memory(&nd_region->dev))
353
		return NULL;
354

355
356
	nd_pfn = nd_pfn_alloc(nd_region);
	dev = nd_pfn_devinit(nd_pfn, NULL);
357

358
	__nd_device_register(dev);
359
360
361
	return dev;
}

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap
 * space associated with the namespace. If the memmap is set to DRAM, then
 * this is a no-op. Since the memmap area is freshly initialized during
 * probe, we have an opportunity to clear any badblocks in this area.
 */
static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
{
	struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
	struct nd_namespace_common *ndns = nd_pfn->ndns;
	void *zero_page = page_address(ZERO_PAGE(0));
	struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
	int num_bad, meta_num, rc, bb_present;
	sector_t first_bad, meta_start;
	struct nd_namespace_io *nsio;

	if (nd_pfn->mode != PFN_MODE_PMEM)
		return 0;

	nsio = to_nd_namespace_io(&ndns->dev);
	meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9;
	meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start;

	do {
		unsigned long zero_len;
		u64 nsoff;

		bb_present = badblocks_check(&nd_region->bb, meta_start,
				meta_num, &first_bad, &num_bad);
		if (bb_present) {
Christoph Hellwig's avatar
Christoph Hellwig committed
392
			dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %llx\n",
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
					num_bad, first_bad);
			nsoff = ALIGN_DOWN((nd_region->ndr_start
					+ (first_bad << 9)) - nsio->res.start,
					PAGE_SIZE);
			zero_len = ALIGN(num_bad << 9, PAGE_SIZE);
			while (zero_len) {
				unsigned long chunk = min(zero_len, PAGE_SIZE);

				rc = nvdimm_write_bytes(ndns, nsoff, zero_page,
							chunk, 0);
				if (rc)
					break;

				zero_len -= chunk;
				nsoff += chunk;
			}
			if (rc) {
				dev_err(&nd_pfn->dev,
Christoph Hellwig's avatar
Christoph Hellwig committed
411
					"error clearing %x badblocks at %llx\n",
412
413
414
415
416
417
418
419
420
					num_bad, first_bad);
				return rc;
			}
		}
	} while (bb_present);

	return 0;
}

421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
static bool nd_supported_alignment(unsigned long align)
{
	int i;
	unsigned long supported[MAX_NVDIMM_ALIGN] = { [0] = 0, };

	if (align == 0)
		return false;

	nd_pfn_supported_alignments(supported);
	for (i = 0; supported[i]; i++)
		if (align == supported[i])
			return true;
	return false;
}

436
437
438
439
440
441
442
443
444
/**
 * nd_pfn_validate - read and validate info-block
 * @nd_pfn: fsdax namespace runtime state / properties
 * @sig: 'devdax' or 'fsdax' signature
 *
 * Upon return the info-block buffer contents (->pfn_sb) are
 * indeterminate when validation fails, and a coherent info-block
 * otherwise.
 */
445
int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
446
447
{
	u64 checksum, offset;
448
	enum nd_pfn_mode mode;
449
	struct nd_namespace_io *nsio;
450
	unsigned long align, start_pad;
451
452
453
	struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
	struct nd_namespace_common *ndns = nd_pfn->ndns;
	const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
454
455
456
457

	if (!pfn_sb || !ndns)
		return -ENODEV;

458
	if (!is_memory(nd_pfn->dev.parent))
459
460
		return -ENODEV;

461
	if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
462
463
		return -ENXIO;

464
	if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
465
466
467
468
469
470
471
472
		return -ENODEV;

	checksum = le64_to_cpu(pfn_sb->checksum);
	pfn_sb->checksum = 0;
	if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
		return -ENODEV;
	pfn_sb->checksum = cpu_to_le64(checksum);

473
474
475
	if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
		return -ENODEV;

476
477
478
479
480
	if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
		pfn_sb->start_pad = 0;
		pfn_sb->end_trunc = 0;
	}

481
482
483
	if (__le16_to_cpu(pfn_sb->version_minor) < 2)
		pfn_sb->align = 0;

484
485
486
487
488
	if (__le16_to_cpu(pfn_sb->version_minor) < 4) {
		pfn_sb->page_struct_size = cpu_to_le16(64);
		pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
	}

489
490
491
	switch (le32_to_cpu(pfn_sb->mode)) {
	case PFN_MODE_RAM:
	case PFN_MODE_PMEM:
492
		break;
493
494
495
496
	default:
		return -ENXIO;
	}

497
498
	align = le32_to_cpu(pfn_sb->align);
	offset = le64_to_cpu(pfn_sb->dataoff);
499
	start_pad = le32_to_cpu(pfn_sb->start_pad);
500
501
502
503
	if (align == 0)
		align = 1UL << ilog2(offset);
	mode = le32_to_cpu(pfn_sb->mode);

504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
	if ((le32_to_cpu(pfn_sb->page_size) > PAGE_SIZE) &&
			(mode == PFN_MODE_PMEM)) {
		dev_err(&nd_pfn->dev,
				"init failed, page size mismatch %d\n",
				le32_to_cpu(pfn_sb->page_size));
		return -EOPNOTSUPP;
	}

	if ((le16_to_cpu(pfn_sb->page_struct_size) < sizeof(struct page)) &&
			(mode == PFN_MODE_PMEM)) {
		dev_err(&nd_pfn->dev,
				"init failed, struct page size mismatch %d\n",
				le16_to_cpu(pfn_sb->page_struct_size));
		return -EOPNOTSUPP;
	}

520
521
522
523
524
525
526
527
528
529
530
531
	/*
	 * Check whether the we support the alignment. For Dax if the
	 * superblock alignment is not matching, we won't initialize
	 * the device.
	 */
	if (!nd_supported_alignment(align) &&
			!memcmp(pfn_sb->signature, DAX_SIG, PFN_SIG_LEN)) {
		dev_err(&nd_pfn->dev, "init failed, alignment mismatch: "
				"%ld:%ld\n", nd_pfn->align, align);
		return -EOPNOTSUPP;
	}

532
	if (!nd_pfn->uuid) {
533
534
535
536
537
		/*
		 * When probing a namepace via nd_pfn_probe() the uuid
		 * is NULL (see: nd_pfn_devinit()) we init settings from
		 * pfn_sb
		 */
538
539
540
		nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
		if (!nd_pfn->uuid)
			return -ENOMEM;
541
542
		nd_pfn->align = align;
		nd_pfn->mode = mode;
543
	} else {
544
545
546
547
		/*
		 * When probing a pfn / dax instance we validate the
		 * live settings against the pfn_sb
		 */
548
		if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
549
			return -ENODEV;
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

		/*
		 * If the uuid validates, but other settings mismatch
		 * return EINVAL because userspace has managed to change
		 * the configuration without specifying new
		 * identification.
		 */
		if (nd_pfn->align != align || nd_pfn->mode != mode) {
			dev_err(&nd_pfn->dev,
					"init failed, settings mismatch\n");
			dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
					nd_pfn->align, align, nd_pfn->mode,
					mode);
			return -EINVAL;
		}
565
566
	}

567
	if (align > nvdimm_namespace_capacity(ndns)) {
568
		dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
569
				align, nvdimm_namespace_capacity(ndns));
570
571
572
		return -EINVAL;
	}

573
574
575
576
577
578
579
	/*
	 * These warnings are verbose because they can only trigger in
	 * the case where the physical address alignment of the
	 * namespace has changed since the pfn superblock was
	 * established.
	 */
	nsio = to_nd_namespace_io(&ndns->dev);
580
	if (offset >= resource_size(&nsio->res)) {
581
582
583
584
585
		dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
				dev_name(&ndns->dev));
		return -EBUSY;
	}

586
	if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align))
587
			|| !IS_ALIGNED(offset, PAGE_SIZE)) {
588
589
590
		dev_err(&nd_pfn->dev,
				"bad offset: %#llx dax disabled align: %#lx\n",
				offset, align);
591
592
593
		return -ENXIO;
	}

594
	return nd_pfn_clear_memmap_errors(nd_pfn);
595
}
596
EXPORT_SYMBOL(nd_pfn_validate);
597

598
int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
599
600
601
{
	int rc;
	struct nd_pfn *nd_pfn;
602
	struct device *pfn_dev;
603
604
605
606
607
608
	struct nd_pfn_sb *pfn_sb;
	struct nd_region *nd_region = to_nd_region(ndns->dev.parent);

	if (ndns->force_raw)
		return -ENODEV;

609
610
611
612
613
614
615
616
	switch (ndns->claim_class) {
	case NVDIMM_CCLASS_NONE:
	case NVDIMM_CCLASS_PFN:
		break;
	default:
		return -ENODEV;
	}

617
	nvdimm_bus_lock(&ndns->dev);
618
619
	nd_pfn = nd_pfn_alloc(nd_region);
	pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
620
	nvdimm_bus_unlock(&ndns->dev);
621
	if (!pfn_dev)
622
		return -ENOMEM;
623
	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
624
	nd_pfn = to_nd_pfn(pfn_dev);
625
	nd_pfn->pfn_sb = pfn_sb;
626
	rc = nd_pfn_validate(nd_pfn, PFN_SIG);
627
	dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>");
628
	if (rc < 0) {
629
		nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
630
		put_device(pfn_dev);
631
	} else
632
		__nd_device_register(pfn_dev);
633
634
635
636

	return rc;
}
EXPORT_SYMBOL(nd_pfn_probe);
637

638
639
640
641
642
static u32 info_block_reserve(void)
{
	return ALIGN(SZ_8K, PAGE_SIZE);
}

643
/*
644
645
 * We hotplug memory at sub-section granularity, pad the reserved area
 * from the previous section base to the namespace base address.
646
647
648
649
650
 */
static unsigned long init_altmap_base(resource_size_t base)
{
	unsigned long base_pfn = PHYS_PFN(base);

651
	return SUBSECTION_ALIGN_DOWN(base_pfn);
652
653
654
655
}

static unsigned long init_altmap_reserve(resource_size_t base)
{
656
	unsigned long reserve = info_block_reserve() >> PAGE_SHIFT;
657
658
	unsigned long base_pfn = PHYS_PFN(base);

659
	reserve += base_pfn - SUBSECTION_ALIGN_DOWN(base_pfn);
660
661
662
	return reserve;
}

663
static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
664
{
665
666
	struct resource *res = &pgmap->res;
	struct vmem_altmap *altmap = &pgmap->altmap;
667
668
669
670
	struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
	u64 offset = le64_to_cpu(pfn_sb->dataoff);
	u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
	u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
671
	u32 reserve = info_block_reserve();
672
673
674
	struct nd_namespace_common *ndns = nd_pfn->ndns;
	struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
	resource_size_t base = nsio->res.start + start_pad;
675
	resource_size_t end = nsio->res.end - end_trunc;
676
677
678
	struct vmem_altmap __altmap = {
		.base_pfn = init_altmap_base(base),
		.reserve = init_altmap_reserve(base),
679
		.end_pfn = PHYS_PFN(end),
680
681
682
683
684
685
686
	};

	memcpy(res, &nsio->res, sizeof(*res));
	res->start += start_pad;
	res->end -= end_trunc;

	if (nd_pfn->mode == PFN_MODE_RAM) {
687
		if (offset < reserve)
688
			return -EINVAL;
689
690
		nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
	} else if (nd_pfn->mode == PFN_MODE_PMEM) {
691
		nd_pfn->npfns = PHYS_PFN((resource_size(res) - offset));
692
693
694
695
696
697
		if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
			dev_info(&nd_pfn->dev,
					"number of pfns truncated from %lld to %ld\n",
					le64_to_cpu(nd_pfn->pfn_sb->npfns),
					nd_pfn->npfns);
		memcpy(altmap, &__altmap, sizeof(*altmap));
698
		altmap->free = PHYS_PFN(offset - reserve);
699
		altmap->alloc = 0;
700
		pgmap->flags |= PGMAP_ALTMAP_VALID;
701
	} else
702
		return -ENXIO;
703

704
	return 0;
705
706
707
708
709
}

static int nd_pfn_init(struct nd_pfn *nd_pfn)
{
	struct nd_namespace_common *ndns = nd_pfn->ndns;
710
	struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
711
712
	resource_size_t start, size;
	struct nd_region *nd_region;
713
	unsigned long npfns, align;
714
	u32 end_trunc;
715
716
	struct nd_pfn_sb *pfn_sb;
	phys_addr_t offset;
717
	const char *sig;
718
719
720
	u64 checksum;
	int rc;

721
	pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
722
723
724
725
	if (!pfn_sb)
		return -ENOMEM;

	nd_pfn->pfn_sb = pfn_sb;
726
727
728
729
	if (is_nd_dax(&nd_pfn->dev))
		sig = DAX_SIG;
	else
		sig = PFN_SIG;
730

731
	rc = nd_pfn_validate(nd_pfn, sig);
732
733
734
735
	if (rc != -ENODEV)
		return rc;

	/* no info block, do init */;
736
737
	memset(pfn_sb, 0, sizeof(*pfn_sb));

738
739
740
741
742
743
744
745
746
747
748
749
750
751
	nd_region = to_nd_region(nd_pfn->dev.parent);
	if (nd_region->ro) {
		dev_info(&nd_pfn->dev,
				"%s is read-only, unable to init metadata\n",
				dev_name(&nd_region->dev));
		return -ENXIO;
	}

	/*
	 * Note, we use 64 here for the standard size of struct page,
	 * debugging options may cause it to be larger in which case the
	 * implementation will limit the pfns advertised through
	 * ->direct_access() to those that are included in the memmap.
	 */
752
	start = nsio->res.start;
753
	size = resource_size(&nsio->res);
754
755
	npfns = PHYS_PFN(size - SZ_8K);
	align = max(nd_pfn->align, (1UL << SUBSECTION_SHIFT));
756
	end_trunc = start + size - ALIGN_DOWN(start + size, align);
757
758
	if (nd_pfn->mode == PFN_MODE_PMEM) {
		/*
759
760
761
		 * The altmap should be padded out to the block size used
		 * when populating the vmemmap. This *should* be equal to
		 * PMD_SIZE for most architectures.
762
763
764
765
766
767
		 *
		 * Also make sure size of struct page is less than 64. We
		 * want to make sure we use large enough size here so that
		 * we don't have a dynamic reserve space depending on
		 * struct page size. But we also want to make sure we notice
		 * when we end up adding new elements to struct page.
768
		 */
769
770
771
		BUILD_BUG_ON(sizeof(struct page) > MAX_STRUCT_PAGE_SIZE);
		offset = ALIGN(start + SZ_8K + MAX_STRUCT_PAGE_SIZE * npfns, align)
			- start;
772
	} else if (nd_pfn->mode == PFN_MODE_RAM)
773
		offset = ALIGN(start + SZ_8K, align) - start;
774
775
776
	else
		return -ENXIO;

777
	if (offset >= size) {
778
779
780
781
782
		dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
				dev_name(&ndns->dev));
		return -ENXIO;
	}

783
	npfns = PHYS_PFN(size - offset - end_trunc);
784
785
786
	pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
	pfn_sb->dataoff = cpu_to_le64(offset);
	pfn_sb->npfns = cpu_to_le64(npfns);
787
	memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
788
789
790
	memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
	memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
	pfn_sb->version_major = cpu_to_le16(1);
791
	pfn_sb->version_minor = cpu_to_le16(4);
792
	pfn_sb->end_trunc = cpu_to_le32(end_trunc);
793
	pfn_sb->align = cpu_to_le32(nd_pfn->align);
794
795
	pfn_sb->page_struct_size = cpu_to_le16(MAX_STRUCT_PAGE_SIZE);
	pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
796
797
798
	checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
	pfn_sb->checksum = cpu_to_le64(checksum);

799
	return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
800
801
802
803
804
805
}

/*
 * Determine the effective resource range and vmem_altmap from an nd_pfn
 * instance.
 */
806
int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
807
808
809
810
{
	int rc;

	if (!nd_pfn->uuid || !nd_pfn->ndns)
811
		return -ENODEV;
812
813
814

	rc = nd_pfn_init(nd_pfn);
	if (rc)
815
		return rc;
816

817
818
	/* we need a valid pfn_sb before we can init a dev_pagemap */
	return __nvdimm_setup_pfn(nd_pfn, pgmap);
819
820
}
EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);