blob: 3e7b11cf1aaec67f69ed4faf7d3a7b3c48215d5e [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
29static struct device_type nd_pfn_device_type = {
30 .name = "nd_pfn",
31 .release = nd_pfn_release,
32};
33
34bool is_nd_pfn(struct device *dev)
35{
36 return dev ? dev->type == &nd_pfn_device_type : false;
37}
38EXPORT_SYMBOL(is_nd_pfn);
39
40struct nd_pfn *to_nd_pfn(struct device *dev)
41{
42 struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
43
44 WARN_ON(!is_nd_pfn(dev));
45 return nd_pfn;
46}
47EXPORT_SYMBOL(to_nd_pfn);
48
49static ssize_t mode_show(struct device *dev,
50 struct device_attribute *attr, char *buf)
51{
Dan Williamscd034122016-03-11 10:15:36 -080052 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040053
54 switch (nd_pfn->mode) {
55 case PFN_MODE_RAM:
56 return sprintf(buf, "ram\n");
57 case PFN_MODE_PMEM:
58 return sprintf(buf, "pmem\n");
59 default:
60 return sprintf(buf, "none\n");
61 }
62}
63
64static ssize_t mode_store(struct device *dev,
65 struct device_attribute *attr, const char *buf, size_t len)
66{
Dan Williamscd034122016-03-11 10:15:36 -080067 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040068 ssize_t rc = 0;
69
Dan Williams87a30e12019-07-17 18:08:26 -070070 nd_device_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -040071 nvdimm_bus_lock(dev);
72 if (dev->driver)
73 rc = -EBUSY;
74 else {
75 size_t n = len - 1;
76
77 if (strncmp(buf, "pmem\n", n) == 0
78 || strncmp(buf, "pmem", n) == 0) {
Dan Williamsd2c0f042016-01-15 16:56:26 -080079 nd_pfn->mode = PFN_MODE_PMEM;
Dan Williamse1455742015-07-30 17:57:47 -040080 } else if (strncmp(buf, "ram\n", n) == 0
81 || strncmp(buf, "ram", n) == 0)
82 nd_pfn->mode = PFN_MODE_RAM;
83 else if (strncmp(buf, "none\n", n) == 0
84 || strncmp(buf, "none", n) == 0)
85 nd_pfn->mode = PFN_MODE_NONE;
86 else
87 rc = -EINVAL;
88 }
Dan Williams426824d2018-03-05 16:39:31 -080089 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
90 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -040091 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -070092 nd_device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -040093
94 return rc ? rc : len;
95}
96static DEVICE_ATTR_RW(mode);
97
Dan Williams315c5622015-12-10 14:45:23 -080098static ssize_t align_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
100{
Dan Williamscd034122016-03-11 10:15:36 -0800101 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800102
Dan Williamsaf7d9f02016-12-10 08:12:05 -0800103 return sprintf(buf, "%ld\n", nd_pfn->align);
Dan Williams315c5622015-12-10 14:45:23 -0800104}
105
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000106static const unsigned long *nd_pfn_supported_alignments(void)
107{
108 /*
109 * This needs to be a non-static variable because the *_SIZE
110 * macros aren't always constants.
111 */
112 const unsigned long supported_alignments[] = {
113 PAGE_SIZE,
114#ifdef CONFIG_TRANSPARENT_HUGEPAGE
115 HPAGE_PMD_SIZE,
116#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
117 HPAGE_PUD_SIZE,
118#endif
119#endif
120 0,
121 };
122 static unsigned long data[ARRAY_SIZE(supported_alignments)];
123
124 memcpy(data, supported_alignments, sizeof(data));
125
126 return data;
127}
128
Dan Williams315c5622015-12-10 14:45:23 -0800129static ssize_t align_store(struct device *dev,
130 struct device_attribute *attr, const char *buf, size_t len)
131{
Dan Williamscd034122016-03-11 10:15:36 -0800132 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800133 ssize_t rc;
134
Dan Williams87a30e12019-07-17 18:08:26 -0700135 nd_device_lock(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800136 nvdimm_bus_lock(dev);
Dan Williamsf13d2b62017-08-11 17:54:48 -0700137 rc = nd_size_select_store(dev, buf, &nd_pfn->align,
138 nd_pfn_supported_alignments());
Dan Williams426824d2018-03-05 16:39:31 -0800139 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
140 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams315c5622015-12-10 14:45:23 -0800141 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700142 nd_device_unlock(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800143
144 return rc ? rc : len;
145}
146static DEVICE_ATTR_RW(align);
147
Dan Williamse1455742015-07-30 17:57:47 -0400148static ssize_t uuid_show(struct device *dev,
149 struct device_attribute *attr, char *buf)
150{
Dan Williamscd034122016-03-11 10:15:36 -0800151 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400152
153 if (nd_pfn->uuid)
154 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
155 return sprintf(buf, "\n");
156}
157
158static ssize_t uuid_store(struct device *dev,
159 struct device_attribute *attr, const char *buf, size_t len)
160{
Dan Williamscd034122016-03-11 10:15:36 -0800161 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400162 ssize_t rc;
163
Dan Williams87a30e12019-07-17 18:08:26 -0700164 nd_device_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400165 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800166 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
167 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams87a30e12019-07-17 18:08:26 -0700168 nd_device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400169
170 return rc ? rc : len;
171}
172static DEVICE_ATTR_RW(uuid);
173
174static ssize_t namespace_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
176{
Dan Williamscd034122016-03-11 10:15:36 -0800177 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400178 ssize_t rc;
179
180 nvdimm_bus_lock(dev);
181 rc = sprintf(buf, "%s\n", nd_pfn->ndns
182 ? dev_name(&nd_pfn->ndns->dev) : "");
183 nvdimm_bus_unlock(dev);
184 return rc;
185}
186
187static ssize_t namespace_store(struct device *dev,
188 struct device_attribute *attr, const char *buf, size_t len)
189{
Dan Williamscd034122016-03-11 10:15:36 -0800190 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400191 ssize_t rc;
192
Dan Williams87a30e12019-07-17 18:08:26 -0700193 nd_device_lock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800194 nvdimm_bus_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400195 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800196 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
197 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -0400198 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700199 nd_device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400200
201 return rc;
202}
203static DEVICE_ATTR_RW(namespace);
204
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800205static ssize_t resource_show(struct device *dev,
206 struct device_attribute *attr, char *buf)
207{
Dan Williamscd034122016-03-11 10:15:36 -0800208 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800209 ssize_t rc;
210
Dan Williams87a30e12019-07-17 18:08:26 -0700211 nd_device_lock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800212 if (dev->driver) {
213 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
214 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
215 struct nd_namespace_common *ndns = nd_pfn->ndns;
216 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
217 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
218
219 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
220 + start_pad + offset);
221 } else {
222 /* no address to convey if the pfn instance is disabled */
223 rc = -ENXIO;
224 }
Dan Williams87a30e12019-07-17 18:08:26 -0700225 nd_device_unlock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800226
227 return rc;
228}
229static DEVICE_ATTR_RO(resource);
230
231static ssize_t size_show(struct device *dev,
232 struct device_attribute *attr, char *buf)
233{
Dan Williamscd034122016-03-11 10:15:36 -0800234 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800235 ssize_t rc;
236
Dan Williams87a30e12019-07-17 18:08:26 -0700237 nd_device_lock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800238 if (dev->driver) {
239 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
240 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
241 struct nd_namespace_common *ndns = nd_pfn->ndns;
242 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
243 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
244 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
245
246 rc = sprintf(buf, "%llu\n", (unsigned long long)
247 resource_size(&nsio->res) - start_pad
248 - end_trunc - offset);
249 } else {
250 /* no size to convey if the pfn instance is disabled */
251 rc = -ENXIO;
252 }
Dan Williams87a30e12019-07-17 18:08:26 -0700253 nd_device_unlock(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800254
255 return rc;
256}
257static DEVICE_ATTR_RO(size);
258
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000259static ssize_t supported_alignments_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
261{
262 return nd_size_select_show(0, nd_pfn_supported_alignments(), buf);
263}
264static DEVICE_ATTR_RO(supported_alignments);
265
Dan Williamse1455742015-07-30 17:57:47 -0400266static struct attribute *nd_pfn_attributes[] = {
267 &dev_attr_mode.attr,
268 &dev_attr_namespace.attr,
269 &dev_attr_uuid.attr,
Dan Williams315c5622015-12-10 14:45:23 -0800270 &dev_attr_align.attr,
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800271 &dev_attr_resource.attr,
272 &dev_attr_size.attr,
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000273 &dev_attr_supported_alignments.attr,
Dan Williamse1455742015-07-30 17:57:47 -0400274 NULL,
275};
276
Dan Williams26417ae2017-09-26 13:07:06 -0700277static umode_t pfn_visible(struct kobject *kobj, struct attribute *a, int n)
278{
279 if (a == &dev_attr_resource.attr)
280 return 0400;
281 return a->mode;
282}
283
Dan Williamscd034122016-03-11 10:15:36 -0800284struct attribute_group nd_pfn_attribute_group = {
Dan Williamse1455742015-07-30 17:57:47 -0400285 .attrs = nd_pfn_attributes,
Dan Williams26417ae2017-09-26 13:07:06 -0700286 .is_visible = pfn_visible,
Dan Williamse1455742015-07-30 17:57:47 -0400287};
288
289static const struct attribute_group *nd_pfn_attribute_groups[] = {
290 &nd_pfn_attribute_group,
291 &nd_device_attribute_group,
292 &nd_numa_attribute_group,
293 NULL,
294};
295
Dan Williamscd034122016-03-11 10:15:36 -0800296struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
Dan Williamse1455742015-07-30 17:57:47 -0400297 struct nd_namespace_common *ndns)
298{
Colin Ian King0cbfeef2018-02-05 14:08:52 +0000299 struct device *dev;
Dan Williamscd034122016-03-11 10:15:36 -0800300
301 if (!nd_pfn)
302 return NULL;
303
304 nd_pfn->mode = PFN_MODE_NONE;
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000305 nd_pfn->align = PFN_DEFAULT_ALIGNMENT;
Dan Williamscd034122016-03-11 10:15:36 -0800306 dev = &nd_pfn->dev;
307 device_initialize(&nd_pfn->dev);
308 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
Dan Williams426824d2018-03-05 16:39:31 -0800309 dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
310 dev_name(ndns->claim));
Dan Williamscd034122016-03-11 10:15:36 -0800311 put_device(dev);
312 return NULL;
313 }
314 return dev;
315}
316
317static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
318{
Dan Williamse1455742015-07-30 17:57:47 -0400319 struct nd_pfn *nd_pfn;
320 struct device *dev;
321
Dan Williamse1455742015-07-30 17:57:47 -0400322 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
323 if (!nd_pfn)
324 return NULL;
325
326 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
327 if (nd_pfn->id < 0) {
328 kfree(nd_pfn);
329 return NULL;
330 }
331
Dan Williamse1455742015-07-30 17:57:47 -0400332 dev = &nd_pfn->dev;
333 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
Dan Williamse1455742015-07-30 17:57:47 -0400334 dev->groups = nd_pfn_attribute_groups;
Dan Williamscd034122016-03-11 10:15:36 -0800335 dev->type = &nd_pfn_device_type;
336 dev->parent = &nd_region->dev;
337
338 return nd_pfn;
Dan Williamse1455742015-07-30 17:57:47 -0400339}
340
341struct device *nd_pfn_create(struct nd_region *nd_region)
342{
Dan Williamscd034122016-03-11 10:15:36 -0800343 struct nd_pfn *nd_pfn;
344 struct device *dev;
Dan Williamse1455742015-07-30 17:57:47 -0400345
Dan Williamsc9e582a2017-05-29 23:12:19 -0700346 if (!is_memory(&nd_region->dev))
Dan Williamscd034122016-03-11 10:15:36 -0800347 return NULL;
348
349 nd_pfn = nd_pfn_alloc(nd_region);
350 dev = nd_pfn_devinit(nd_pfn, NULL);
351
352 __nd_device_register(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400353 return dev;
354}
355
Vishal Verma48af2f72018-09-18 17:48:31 -0600356/*
357 * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap
358 * space associated with the namespace. If the memmap is set to DRAM, then
359 * this is a no-op. Since the memmap area is freshly initialized during
360 * probe, we have an opportunity to clear any badblocks in this area.
361 */
362static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
363{
364 struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
365 struct nd_namespace_common *ndns = nd_pfn->ndns;
366 void *zero_page = page_address(ZERO_PAGE(0));
367 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
368 int num_bad, meta_num, rc, bb_present;
369 sector_t first_bad, meta_start;
370 struct nd_namespace_io *nsio;
371
372 if (nd_pfn->mode != PFN_MODE_PMEM)
373 return 0;
374
375 nsio = to_nd_namespace_io(&ndns->dev);
376 meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9;
377 meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start;
378
379 do {
380 unsigned long zero_len;
381 u64 nsoff;
382
383 bb_present = badblocks_check(&nd_region->bb, meta_start,
384 meta_num, &first_bad, &num_bad);
385 if (bb_present) {
Christoph Hellwig72deb452019-04-05 18:08:59 +0200386 dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %llx\n",
Vishal Verma48af2f72018-09-18 17:48:31 -0600387 num_bad, first_bad);
388 nsoff = ALIGN_DOWN((nd_region->ndr_start
389 + (first_bad << 9)) - nsio->res.start,
390 PAGE_SIZE);
391 zero_len = ALIGN(num_bad << 9, PAGE_SIZE);
392 while (zero_len) {
393 unsigned long chunk = min(zero_len, PAGE_SIZE);
394
395 rc = nvdimm_write_bytes(ndns, nsoff, zero_page,
396 chunk, 0);
397 if (rc)
398 break;
399
400 zero_len -= chunk;
401 nsoff += chunk;
402 }
403 if (rc) {
404 dev_err(&nd_pfn->dev,
Christoph Hellwig72deb452019-04-05 18:08:59 +0200405 "error clearing %x badblocks at %llx\n",
Vishal Verma48af2f72018-09-18 17:48:31 -0600406 num_bad, first_bad);
407 return rc;
408 }
409 }
410 } while (bb_present);
411
412 return 0;
413}
414
Dan Williams7e3e8882019-07-18 15:58:36 -0700415/**
416 * nd_pfn_validate - read and validate info-block
417 * @nd_pfn: fsdax namespace runtime state / properties
418 * @sig: 'devdax' or 'fsdax' signature
419 *
420 * Upon return the info-block buffer contents (->pfn_sb) are
421 * indeterminate when validation fails, and a coherent info-block
422 * otherwise.
423 */
Dan Williamsc5ed9262016-05-18 14:50:12 -0700424int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
Dan Williamse1455742015-07-30 17:57:47 -0400425{
Dan Williamse1455742015-07-30 17:57:47 -0400426 u64 checksum, offset;
Dan Williams1ee66672016-06-23 17:50:39 -0700427 enum nd_pfn_mode mode;
Dan Williamsa34d5e82015-12-12 16:09:14 -0800428 struct nd_namespace_io *nsio;
Dan Williams19deaa22017-12-19 15:07:10 -0800429 unsigned long align, start_pad;
Dan Williamsa34d5e82015-12-12 16:09:14 -0800430 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
431 struct nd_namespace_common *ndns = nd_pfn->ndns;
432 const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
Dan Williamse1455742015-07-30 17:57:47 -0400433
434 if (!pfn_sb || !ndns)
435 return -ENODEV;
436
Dan Williamsc9e582a2017-05-29 23:12:19 -0700437 if (!is_memory(nd_pfn->dev.parent))
Dan Williamse1455742015-07-30 17:57:47 -0400438 return -ENODEV;
439
Vishal Verma3ae3d672017-05-10 15:01:30 -0600440 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
Dan Williamse1455742015-07-30 17:57:47 -0400441 return -ENXIO;
442
Dan Williamsc5ed9262016-05-18 14:50:12 -0700443 if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
Dan Williamse1455742015-07-30 17:57:47 -0400444 return -ENODEV;
445
446 checksum = le64_to_cpu(pfn_sb->checksum);
447 pfn_sb->checksum = 0;
448 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
449 return -ENODEV;
450 pfn_sb->checksum = cpu_to_le64(checksum);
451
Dan Williamsa34d5e82015-12-12 16:09:14 -0800452 if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
453 return -ENODEV;
454
Dan Williamscfe30b82016-03-03 09:38:00 -0800455 if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
456 pfn_sb->start_pad = 0;
457 pfn_sb->end_trunc = 0;
458 }
459
Dan Williams45a0dac2016-03-31 15:41:18 -0700460 if (__le16_to_cpu(pfn_sb->version_minor) < 2)
461 pfn_sb->align = 0;
462
Dan Williamse1455742015-07-30 17:57:47 -0400463 switch (le32_to_cpu(pfn_sb->mode)) {
464 case PFN_MODE_RAM:
Dan Williamse1455742015-07-30 17:57:47 -0400465 case PFN_MODE_PMEM:
Dan Williams45eb5702016-01-29 17:42:51 -0800466 break;
Dan Williamse1455742015-07-30 17:57:47 -0400467 default:
468 return -ENXIO;
469 }
470
Dan Williams1ee66672016-06-23 17:50:39 -0700471 align = le32_to_cpu(pfn_sb->align);
472 offset = le64_to_cpu(pfn_sb->dataoff);
Dan Williams19deaa22017-12-19 15:07:10 -0800473 start_pad = le32_to_cpu(pfn_sb->start_pad);
Dan Williams1ee66672016-06-23 17:50:39 -0700474 if (align == 0)
475 align = 1UL << ilog2(offset);
476 mode = le32_to_cpu(pfn_sb->mode);
477
Dan Williamse1455742015-07-30 17:57:47 -0400478 if (!nd_pfn->uuid) {
Dan Williams1ee66672016-06-23 17:50:39 -0700479 /*
480 * When probing a namepace via nd_pfn_probe() the uuid
481 * is NULL (see: nd_pfn_devinit()) we init settings from
482 * pfn_sb
483 */
Dan Williamse1455742015-07-30 17:57:47 -0400484 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
485 if (!nd_pfn->uuid)
486 return -ENOMEM;
Dan Williams1ee66672016-06-23 17:50:39 -0700487 nd_pfn->align = align;
488 nd_pfn->mode = mode;
Dan Williamse1455742015-07-30 17:57:47 -0400489 } else {
Dan Williams1ee66672016-06-23 17:50:39 -0700490 /*
491 * When probing a pfn / dax instance we validate the
492 * live settings against the pfn_sb
493 */
Dan Williamse1455742015-07-30 17:57:47 -0400494 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
Dan Williamse5670562016-04-07 19:59:27 -0700495 return -ENODEV;
Dan Williams1ee66672016-06-23 17:50:39 -0700496
497 /*
498 * If the uuid validates, but other settings mismatch
499 * return EINVAL because userspace has managed to change
500 * the configuration without specifying new
501 * identification.
502 */
503 if (nd_pfn->align != align || nd_pfn->mode != mode) {
504 dev_err(&nd_pfn->dev,
505 "init failed, settings mismatch\n");
506 dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
507 nd_pfn->align, align, nd_pfn->mode,
508 mode);
509 return -EINVAL;
510 }
Dan Williamse1455742015-07-30 17:57:47 -0400511 }
512
Dan Williams1ee66672016-06-23 17:50:39 -0700513 if (align > nvdimm_namespace_capacity(ndns)) {
Dan Williams315c5622015-12-10 14:45:23 -0800514 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
Dan Williams1ee66672016-06-23 17:50:39 -0700515 align, nvdimm_namespace_capacity(ndns));
Dan Williams315c5622015-12-10 14:45:23 -0800516 return -EINVAL;
517 }
518
Dan Williamse1455742015-07-30 17:57:47 -0400519 /*
520 * These warnings are verbose because they can only trigger in
521 * the case where the physical address alignment of the
522 * namespace has changed since the pfn superblock was
523 * established.
524 */
Dan Williamse1455742015-07-30 17:57:47 -0400525 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f1e8ce2015-12-10 15:14:20 -0800526 if (offset >= resource_size(&nsio->res)) {
Dan Williamse1455742015-07-30 17:57:47 -0400527 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
528 dev_name(&ndns->dev));
529 return -EBUSY;
530 }
531
Dan Williams19deaa22017-12-19 15:07:10 -0800532 if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align))
Dan Williams5e24c9f2016-05-21 11:01:41 -0700533 || !IS_ALIGNED(offset, PAGE_SIZE)) {
Dan Williams1ee66672016-06-23 17:50:39 -0700534 dev_err(&nd_pfn->dev,
535 "bad offset: %#llx dax disabled align: %#lx\n",
536 offset, align);
Dan Williams315c5622015-12-10 14:45:23 -0800537 return -ENXIO;
538 }
539
Vishal Verma48af2f72018-09-18 17:48:31 -0600540 return nd_pfn_clear_memmap_errors(nd_pfn);
Dan Williamse1455742015-07-30 17:57:47 -0400541}
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400542EXPORT_SYMBOL(nd_pfn_validate);
Dan Williamse1455742015-07-30 17:57:47 -0400543
Dan Williams200c79d2016-03-22 00:22:16 -0700544int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
Dan Williamse1455742015-07-30 17:57:47 -0400545{
546 int rc;
Dan Williamse1455742015-07-30 17:57:47 -0400547 struct nd_pfn *nd_pfn;
Dan Williamsbd032942016-03-17 18:16:15 -0700548 struct device *pfn_dev;
Dan Williamse1455742015-07-30 17:57:47 -0400549 struct nd_pfn_sb *pfn_sb;
550 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
551
552 if (ndns->force_raw)
553 return -ENODEV;
554
Dan Williamsb3fde742017-06-04 10:18:39 +0900555 switch (ndns->claim_class) {
556 case NVDIMM_CCLASS_NONE:
557 case NVDIMM_CCLASS_PFN:
558 break;
559 default:
560 return -ENODEV;
561 }
562
Dan Williamse1455742015-07-30 17:57:47 -0400563 nvdimm_bus_lock(&ndns->dev);
Dan Williamscd034122016-03-11 10:15:36 -0800564 nd_pfn = nd_pfn_alloc(nd_region);
565 pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
Dan Williamse1455742015-07-30 17:57:47 -0400566 nvdimm_bus_unlock(&ndns->dev);
Dan Williamsbd032942016-03-17 18:16:15 -0700567 if (!pfn_dev)
Dan Williamse1455742015-07-30 17:57:47 -0400568 return -ENOMEM;
Dan Williams7e3e8882019-07-18 15:58:36 -0700569 pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
Dan Williamsbd032942016-03-17 18:16:15 -0700570 nd_pfn = to_nd_pfn(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400571 nd_pfn->pfn_sb = pfn_sb;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700572 rc = nd_pfn_validate(nd_pfn, PFN_SIG);
Dan Williams426824d2018-03-05 16:39:31 -0800573 dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>");
Dan Williamse1455742015-07-30 17:57:47 -0400574 if (rc < 0) {
Dan Williams452bae02017-04-28 22:05:14 -0700575 nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
Dan Williamsbd032942016-03-17 18:16:15 -0700576 put_device(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400577 } else
Dan Williamsbd032942016-03-17 18:16:15 -0700578 __nd_device_register(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400579
580 return rc;
581}
582EXPORT_SYMBOL(nd_pfn_probe);
Dan Williamsac515c02016-03-22 00:29:43 -0700583
Dan Williams11a35812019-02-06 13:04:53 +1100584static u32 info_block_reserve(void)
585{
586 return ALIGN(SZ_8K, PAGE_SIZE);
587}
588
Dan Williamsac515c02016-03-22 00:29:43 -0700589/*
Dan Williamsa3619192019-07-18 15:58:40 -0700590 * We hotplug memory at sub-section granularity, pad the reserved area
591 * from the previous section base to the namespace base address.
Dan Williamsac515c02016-03-22 00:29:43 -0700592 */
593static unsigned long init_altmap_base(resource_size_t base)
594{
595 unsigned long base_pfn = PHYS_PFN(base);
596
Dan Williamsa3619192019-07-18 15:58:40 -0700597 return SUBSECTION_ALIGN_DOWN(base_pfn);
Dan Williamsac515c02016-03-22 00:29:43 -0700598}
599
600static unsigned long init_altmap_reserve(resource_size_t base)
601{
Dan Williams11a35812019-02-06 13:04:53 +1100602 unsigned long reserve = info_block_reserve() >> PAGE_SHIFT;
Dan Williamsac515c02016-03-22 00:29:43 -0700603 unsigned long base_pfn = PHYS_PFN(base);
604
Dan Williamsa3619192019-07-18 15:58:40 -0700605 reserve += base_pfn - SUBSECTION_ALIGN_DOWN(base_pfn);
Dan Williamsac515c02016-03-22 00:29:43 -0700606 return reserve;
607}
608
Christoph Hellwige8d51342017-12-29 08:54:05 +0100609static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
Dan Williamsac515c02016-03-22 00:29:43 -0700610{
Christoph Hellwige8d51342017-12-29 08:54:05 +0100611 struct resource *res = &pgmap->res;
612 struct vmem_altmap *altmap = &pgmap->altmap;
Dan Williamsac515c02016-03-22 00:29:43 -0700613 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
614 u64 offset = le64_to_cpu(pfn_sb->dataoff);
615 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
616 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
Dan Williams11a35812019-02-06 13:04:53 +1100617 u32 reserve = info_block_reserve();
Dan Williamsac515c02016-03-22 00:29:43 -0700618 struct nd_namespace_common *ndns = nd_pfn->ndns;
619 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
620 resource_size_t base = nsio->res.start + start_pad;
621 struct vmem_altmap __altmap = {
622 .base_pfn = init_altmap_base(base),
623 .reserve = init_altmap_reserve(base),
624 };
625
626 memcpy(res, &nsio->res, sizeof(*res));
627 res->start += start_pad;
628 res->end -= end_trunc;
629
Dan Williamsac515c02016-03-22 00:29:43 -0700630 if (nd_pfn->mode == PFN_MODE_RAM) {
Dan Williams11a35812019-02-06 13:04:53 +1100631 if (offset < reserve)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100632 return -EINVAL;
Dan Williamsac515c02016-03-22 00:29:43 -0700633 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
Dan Williamsac515c02016-03-22 00:29:43 -0700634 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
Dan Williamsa3619192019-07-18 15:58:40 -0700635 nd_pfn->npfns = PHYS_PFN((resource_size(res) - offset));
Dan Williamsac515c02016-03-22 00:29:43 -0700636 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
637 dev_info(&nd_pfn->dev,
638 "number of pfns truncated from %lld to %ld\n",
639 le64_to_cpu(nd_pfn->pfn_sb->npfns),
640 nd_pfn->npfns);
641 memcpy(altmap, &__altmap, sizeof(*altmap));
Dan Williams11a35812019-02-06 13:04:53 +1100642 altmap->free = PHYS_PFN(offset - reserve);
Dan Williamsac515c02016-03-22 00:29:43 -0700643 altmap->alloc = 0;
Christoph Hellwig514caf22019-06-26 14:27:13 +0200644 pgmap->flags |= PGMAP_ALTMAP_VALID;
Dan Williamsac515c02016-03-22 00:29:43 -0700645 } else
Christoph Hellwige8d51342017-12-29 08:54:05 +0100646 return -ENXIO;
Dan Williamsac515c02016-03-22 00:29:43 -0700647
Christoph Hellwige8d51342017-12-29 08:54:05 +0100648 return 0;
Dan Williamsac515c02016-03-22 00:29:43 -0700649}
650
651static int nd_pfn_init(struct nd_pfn *nd_pfn)
652{
653 struct nd_namespace_common *ndns = nd_pfn->ndns;
Dan Williamsae86cbf2018-11-24 10:47:04 -0800654 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
Dan Williamsac515c02016-03-22 00:29:43 -0700655 resource_size_t start, size;
Dan Williamsac515c02016-03-22 00:29:43 -0700656 struct nd_region *nd_region;
Dan Williamsa3619192019-07-18 15:58:40 -0700657 unsigned long npfns, align;
Dan Williamsac515c02016-03-22 00:29:43 -0700658 struct nd_pfn_sb *pfn_sb;
Dan Williamsac515c02016-03-22 00:29:43 -0700659 phys_addr_t offset;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700660 const char *sig;
Dan Williamsac515c02016-03-22 00:29:43 -0700661 u64 checksum;
662 int rc;
663
Dan Williams7e3e8882019-07-18 15:58:36 -0700664 pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
Dan Williamsac515c02016-03-22 00:29:43 -0700665 if (!pfn_sb)
666 return -ENOMEM;
667
668 nd_pfn->pfn_sb = pfn_sb;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700669 if (is_nd_dax(&nd_pfn->dev))
670 sig = DAX_SIG;
671 else
672 sig = PFN_SIG;
Dan Williams7e3e8882019-07-18 15:58:36 -0700673
Dan Williamsc5ed9262016-05-18 14:50:12 -0700674 rc = nd_pfn_validate(nd_pfn, sig);
Dan Williamsac515c02016-03-22 00:29:43 -0700675 if (rc != -ENODEV)
676 return rc;
677
678 /* no info block, do init */;
Dan Williams7e3e8882019-07-18 15:58:36 -0700679 memset(pfn_sb, 0, sizeof(*pfn_sb));
680
Dan Williamsac515c02016-03-22 00:29:43 -0700681 nd_region = to_nd_region(nd_pfn->dev.parent);
682 if (nd_region->ro) {
683 dev_info(&nd_pfn->dev,
684 "%s is read-only, unable to init metadata\n",
685 dev_name(&nd_region->dev));
686 return -ENXIO;
687 }
688
Dan Williamsac515c02016-03-22 00:29:43 -0700689 /*
690 * Note, we use 64 here for the standard size of struct page,
691 * debugging options may cause it to be larger in which case the
692 * implementation will limit the pfns advertised through
693 * ->direct_access() to those that are included in the memmap.
694 */
Dan Williamsa3619192019-07-18 15:58:40 -0700695 start = nsio->res.start;
Dan Williamsac515c02016-03-22 00:29:43 -0700696 size = resource_size(&nsio->res);
Dan Williamsa3619192019-07-18 15:58:40 -0700697 npfns = PHYS_PFN(size - SZ_8K);
698 align = max(nd_pfn->align, (1UL << SUBSECTION_SHIFT));
Dan Williams594d6d92016-05-18 09:59:34 -0700699 if (nd_pfn->mode == PFN_MODE_PMEM) {
Dan Williams594d6d92016-05-18 09:59:34 -0700700 /*
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000701 * The altmap should be padded out to the block size used
702 * when populating the vmemmap. This *should* be equal to
703 * PMD_SIZE for most architectures.
Dan Williams594d6d92016-05-18 09:59:34 -0700704 */
Dan Williamsa3619192019-07-18 15:58:40 -0700705 offset = ALIGN(start + SZ_8K + 64 * npfns, align) - start;
Dan Williams594d6d92016-05-18 09:59:34 -0700706 } else if (nd_pfn->mode == PFN_MODE_RAM)
Dan Williamsa3619192019-07-18 15:58:40 -0700707 offset = ALIGN(start + SZ_8K, align) - start;
Dan Williamsac515c02016-03-22 00:29:43 -0700708 else
709 return -ENXIO;
710
Dan Williamsa3619192019-07-18 15:58:40 -0700711 if (offset >= size) {
Dan Williamsac515c02016-03-22 00:29:43 -0700712 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
713 dev_name(&ndns->dev));
714 return -ENXIO;
715 }
716
Dan Williamsa3619192019-07-18 15:58:40 -0700717 npfns = PHYS_PFN(size - offset);
Dan Williamsac515c02016-03-22 00:29:43 -0700718 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
719 pfn_sb->dataoff = cpu_to_le64(offset);
720 pfn_sb->npfns = cpu_to_le64(npfns);
Dan Williamsc5ed9262016-05-18 14:50:12 -0700721 memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
Dan Williamsac515c02016-03-22 00:29:43 -0700722 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
723 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
724 pfn_sb->version_major = cpu_to_le16(1);
Dan Williams7e3e8882019-07-18 15:58:36 -0700725 pfn_sb->version_minor = cpu_to_le16(3);
Dan Williams45a0dac2016-03-31 15:41:18 -0700726 pfn_sb->align = cpu_to_le32(nd_pfn->align);
Dan Williamsac515c02016-03-22 00:29:43 -0700727 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
728 pfn_sb->checksum = cpu_to_le64(checksum);
729
Vishal Verma3ae3d672017-05-10 15:01:30 -0600730 return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
Dan Williamsac515c02016-03-22 00:29:43 -0700731}
732
733/*
734 * Determine the effective resource range and vmem_altmap from an nd_pfn
735 * instance.
736 */
Christoph Hellwige8d51342017-12-29 08:54:05 +0100737int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
Dan Williamsac515c02016-03-22 00:29:43 -0700738{
739 int rc;
740
741 if (!nd_pfn->uuid || !nd_pfn->ndns)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100742 return -ENODEV;
Dan Williamsac515c02016-03-22 00:29:43 -0700743
744 rc = nd_pfn_init(nd_pfn);
745 if (rc)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100746 return rc;
Dan Williamsac515c02016-03-22 00:29:43 -0700747
Christoph Hellwige8d51342017-12-29 08:54:05 +0100748 /* we need a valid pfn_sb before we can init a dev_pagemap */
749 return __nvdimm_setup_pfn(nd_pfn, pgmap);
Dan Williamsac515c02016-03-22 00:29:43 -0700750}
751EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);