blob: 3f7ad5bc443ee80416236143e0a0d58d8b138c04 [file] [log] [blame]
Dan Williamse1455742015-07-30 17:57:47 -04001/*
Dan Williamscd034122016-03-11 10:15:36 -08002 * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
Dan Williamse1455742015-07-30 17:57:47 -04003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
Dan Williamsac515c02016-03-22 00:29:43 -070013#include <linux/memremap.h>
Dan Williamse1455742015-07-30 17:57:47 -040014#include <linux/blkdev.h>
15#include <linux/device.h>
16#include <linux/genhd.h>
17#include <linux/sizes.h>
18#include <linux/slab.h>
19#include <linux/fs.h>
20#include <linux/mm.h>
21#include "nd-core.h"
22#include "pfn.h"
23#include "nd.h"
24
25static void nd_pfn_release(struct device *dev)
26{
27 struct nd_region *nd_region = to_nd_region(dev->parent);
28 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
29
Dan Williams426824d2018-03-05 16:39:31 -080030 dev_dbg(dev, "trace\n");
Dan Williamse1455742015-07-30 17:57:47 -040031 nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
32 ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
33 kfree(nd_pfn->uuid);
34 kfree(nd_pfn);
35}
36
37static struct device_type nd_pfn_device_type = {
38 .name = "nd_pfn",
39 .release = nd_pfn_release,
40};
41
42bool is_nd_pfn(struct device *dev)
43{
44 return dev ? dev->type == &nd_pfn_device_type : false;
45}
46EXPORT_SYMBOL(is_nd_pfn);
47
48struct nd_pfn *to_nd_pfn(struct device *dev)
49{
50 struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
51
52 WARN_ON(!is_nd_pfn(dev));
53 return nd_pfn;
54}
55EXPORT_SYMBOL(to_nd_pfn);
56
57static ssize_t mode_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
59{
Dan Williamscd034122016-03-11 10:15:36 -080060 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040061
62 switch (nd_pfn->mode) {
63 case PFN_MODE_RAM:
64 return sprintf(buf, "ram\n");
65 case PFN_MODE_PMEM:
66 return sprintf(buf, "pmem\n");
67 default:
68 return sprintf(buf, "none\n");
69 }
70}
71
72static ssize_t mode_store(struct device *dev,
73 struct device_attribute *attr, const char *buf, size_t len)
74{
Dan Williamscd034122016-03-11 10:15:36 -080075 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040076 ssize_t rc = 0;
77
78 device_lock(dev);
79 nvdimm_bus_lock(dev);
80 if (dev->driver)
81 rc = -EBUSY;
82 else {
83 size_t n = len - 1;
84
85 if (strncmp(buf, "pmem\n", n) == 0
86 || strncmp(buf, "pmem", n) == 0) {
Dan Williamsd2c0f042016-01-15 16:56:26 -080087 nd_pfn->mode = PFN_MODE_PMEM;
Dan Williamse1455742015-07-30 17:57:47 -040088 } else if (strncmp(buf, "ram\n", n) == 0
89 || strncmp(buf, "ram", n) == 0)
90 nd_pfn->mode = PFN_MODE_RAM;
91 else if (strncmp(buf, "none\n", n) == 0
92 || strncmp(buf, "none", n) == 0)
93 nd_pfn->mode = PFN_MODE_NONE;
94 else
95 rc = -EINVAL;
96 }
Dan Williams426824d2018-03-05 16:39:31 -080097 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
98 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -040099 nvdimm_bus_unlock(dev);
100 device_unlock(dev);
101
102 return rc ? rc : len;
103}
104static DEVICE_ATTR_RW(mode);
105
Dan Williams315c5622015-12-10 14:45:23 -0800106static ssize_t align_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
108{
Dan Williamscd034122016-03-11 10:15:36 -0800109 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800110
Dan Williamsaf7d9f02016-12-10 08:12:05 -0800111 return sprintf(buf, "%ld\n", nd_pfn->align);
Dan Williams315c5622015-12-10 14:45:23 -0800112}
113
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000114static const unsigned long *nd_pfn_supported_alignments(void)
115{
116 /*
117 * This needs to be a non-static variable because the *_SIZE
118 * macros aren't always constants.
119 */
120 const unsigned long supported_alignments[] = {
121 PAGE_SIZE,
122#ifdef CONFIG_TRANSPARENT_HUGEPAGE
123 HPAGE_PMD_SIZE,
124#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
125 HPAGE_PUD_SIZE,
126#endif
127#endif
128 0,
129 };
130 static unsigned long data[ARRAY_SIZE(supported_alignments)];
131
132 memcpy(data, supported_alignments, sizeof(data));
133
134 return data;
135}
136
Dan Williams315c5622015-12-10 14:45:23 -0800137static ssize_t align_store(struct device *dev,
138 struct device_attribute *attr, const char *buf, size_t len)
139{
Dan Williamscd034122016-03-11 10:15:36 -0800140 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800141 ssize_t rc;
142
143 device_lock(dev);
144 nvdimm_bus_lock(dev);
Dan Williamsf13d2b62017-08-11 17:54:48 -0700145 rc = nd_size_select_store(dev, buf, &nd_pfn->align,
146 nd_pfn_supported_alignments());
Dan Williams426824d2018-03-05 16:39:31 -0800147 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
148 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams315c5622015-12-10 14:45:23 -0800149 nvdimm_bus_unlock(dev);
150 device_unlock(dev);
151
152 return rc ? rc : len;
153}
154static DEVICE_ATTR_RW(align);
155
Dan Williamse1455742015-07-30 17:57:47 -0400156static ssize_t uuid_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
Dan Williamscd034122016-03-11 10:15:36 -0800159 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400160
161 if (nd_pfn->uuid)
162 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
163 return sprintf(buf, "\n");
164}
165
166static ssize_t uuid_store(struct device *dev,
167 struct device_attribute *attr, const char *buf, size_t len)
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 device_lock(dev);
173 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800174 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
175 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -0400176 device_unlock(dev);
177
178 return rc ? rc : len;
179}
180static DEVICE_ATTR_RW(uuid);
181
182static ssize_t namespace_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
Dan Williamscd034122016-03-11 10:15:36 -0800185 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400186 ssize_t rc;
187
188 nvdimm_bus_lock(dev);
189 rc = sprintf(buf, "%s\n", nd_pfn->ndns
190 ? dev_name(&nd_pfn->ndns->dev) : "");
191 nvdimm_bus_unlock(dev);
192 return rc;
193}
194
195static ssize_t namespace_store(struct device *dev,
196 struct device_attribute *attr, const char *buf, size_t len)
197{
Dan Williamscd034122016-03-11 10:15:36 -0800198 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400199 ssize_t rc;
200
Dan Williamse1455742015-07-30 17:57:47 -0400201 device_lock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800202 nvdimm_bus_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400203 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800204 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
205 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -0400206 nvdimm_bus_unlock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800207 device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400208
209 return rc;
210}
211static DEVICE_ATTR_RW(namespace);
212
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800213static ssize_t resource_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
Dan Williamscd034122016-03-11 10:15:36 -0800216 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800217 ssize_t rc;
218
219 device_lock(dev);
220 if (dev->driver) {
221 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
222 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
223 struct nd_namespace_common *ndns = nd_pfn->ndns;
224 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
225 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
226
227 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
228 + start_pad + offset);
229 } else {
230 /* no address to convey if the pfn instance is disabled */
231 rc = -ENXIO;
232 }
233 device_unlock(dev);
234
235 return rc;
236}
237static DEVICE_ATTR_RO(resource);
238
239static ssize_t size_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
Dan Williamscd034122016-03-11 10:15:36 -0800242 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800243 ssize_t rc;
244
245 device_lock(dev);
246 if (dev->driver) {
247 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
248 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
249 struct nd_namespace_common *ndns = nd_pfn->ndns;
250 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
251 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
252 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
253
254 rc = sprintf(buf, "%llu\n", (unsigned long long)
255 resource_size(&nsio->res) - start_pad
256 - end_trunc - offset);
257 } else {
258 /* no size to convey if the pfn instance is disabled */
259 rc = -ENXIO;
260 }
261 device_unlock(dev);
262
263 return rc;
264}
265static DEVICE_ATTR_RO(size);
266
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000267static ssize_t supported_alignments_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
269{
270 return nd_size_select_show(0, nd_pfn_supported_alignments(), buf);
271}
272static DEVICE_ATTR_RO(supported_alignments);
273
Dan Williamse1455742015-07-30 17:57:47 -0400274static struct attribute *nd_pfn_attributes[] = {
275 &dev_attr_mode.attr,
276 &dev_attr_namespace.attr,
277 &dev_attr_uuid.attr,
Dan Williams315c5622015-12-10 14:45:23 -0800278 &dev_attr_align.attr,
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800279 &dev_attr_resource.attr,
280 &dev_attr_size.attr,
Oliver O'Halloran1fdadbe2017-06-27 19:56:12 +1000281 &dev_attr_supported_alignments.attr,
Dan Williamse1455742015-07-30 17:57:47 -0400282 NULL,
283};
284
Dan Williams26417ae2017-09-26 13:07:06 -0700285static umode_t pfn_visible(struct kobject *kobj, struct attribute *a, int n)
286{
287 if (a == &dev_attr_resource.attr)
288 return 0400;
289 return a->mode;
290}
291
Dan Williamscd034122016-03-11 10:15:36 -0800292struct attribute_group nd_pfn_attribute_group = {
Dan Williamse1455742015-07-30 17:57:47 -0400293 .attrs = nd_pfn_attributes,
Dan Williams26417ae2017-09-26 13:07:06 -0700294 .is_visible = pfn_visible,
Dan Williamse1455742015-07-30 17:57:47 -0400295};
296
297static const struct attribute_group *nd_pfn_attribute_groups[] = {
298 &nd_pfn_attribute_group,
299 &nd_device_attribute_group,
300 &nd_numa_attribute_group,
301 NULL,
302};
303
Dan Williamscd034122016-03-11 10:15:36 -0800304struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
Dan Williamse1455742015-07-30 17:57:47 -0400305 struct nd_namespace_common *ndns)
306{
Colin Ian King0cbfeef2018-02-05 14:08:52 +0000307 struct device *dev;
Dan Williamscd034122016-03-11 10:15:36 -0800308
309 if (!nd_pfn)
310 return NULL;
311
312 nd_pfn->mode = PFN_MODE_NONE;
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000313 nd_pfn->align = PFN_DEFAULT_ALIGNMENT;
Dan Williamscd034122016-03-11 10:15:36 -0800314 dev = &nd_pfn->dev;
315 device_initialize(&nd_pfn->dev);
316 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
Dan Williams426824d2018-03-05 16:39:31 -0800317 dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
318 dev_name(ndns->claim));
Dan Williamscd034122016-03-11 10:15:36 -0800319 put_device(dev);
320 return NULL;
321 }
322 return dev;
323}
324
325static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
326{
Dan Williamse1455742015-07-30 17:57:47 -0400327 struct nd_pfn *nd_pfn;
328 struct device *dev;
329
Dan Williamse1455742015-07-30 17:57:47 -0400330 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
331 if (!nd_pfn)
332 return NULL;
333
334 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
335 if (nd_pfn->id < 0) {
336 kfree(nd_pfn);
337 return NULL;
338 }
339
Dan Williamse1455742015-07-30 17:57:47 -0400340 dev = &nd_pfn->dev;
341 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
Dan Williamse1455742015-07-30 17:57:47 -0400342 dev->groups = nd_pfn_attribute_groups;
Dan Williamscd034122016-03-11 10:15:36 -0800343 dev->type = &nd_pfn_device_type;
344 dev->parent = &nd_region->dev;
345
346 return nd_pfn;
Dan Williamse1455742015-07-30 17:57:47 -0400347}
348
349struct device *nd_pfn_create(struct nd_region *nd_region)
350{
Dan Williamscd034122016-03-11 10:15:36 -0800351 struct nd_pfn *nd_pfn;
352 struct device *dev;
Dan Williamse1455742015-07-30 17:57:47 -0400353
Dan Williamsc9e582a2017-05-29 23:12:19 -0700354 if (!is_memory(&nd_region->dev))
Dan Williamscd034122016-03-11 10:15:36 -0800355 return NULL;
356
357 nd_pfn = nd_pfn_alloc(nd_region);
358 dev = nd_pfn_devinit(nd_pfn, NULL);
359
360 __nd_device_register(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400361 return dev;
362}
363
Dan Williamsc5ed9262016-05-18 14:50:12 -0700364int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
Dan Williamse1455742015-07-30 17:57:47 -0400365{
Dan Williamse1455742015-07-30 17:57:47 -0400366 u64 checksum, offset;
Dan Williams1ee66672016-06-23 17:50:39 -0700367 enum nd_pfn_mode mode;
Dan Williamsa34d5e82015-12-12 16:09:14 -0800368 struct nd_namespace_io *nsio;
Dan Williams19deaa22017-12-19 15:07:10 -0800369 unsigned long align, start_pad;
Dan Williamsa34d5e82015-12-12 16:09:14 -0800370 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
371 struct nd_namespace_common *ndns = nd_pfn->ndns;
372 const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
Dan Williamse1455742015-07-30 17:57:47 -0400373
374 if (!pfn_sb || !ndns)
375 return -ENODEV;
376
Dan Williamsc9e582a2017-05-29 23:12:19 -0700377 if (!is_memory(nd_pfn->dev.parent))
Dan Williamse1455742015-07-30 17:57:47 -0400378 return -ENODEV;
379
Vishal Verma3ae3d672017-05-10 15:01:30 -0600380 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
Dan Williamse1455742015-07-30 17:57:47 -0400381 return -ENXIO;
382
Dan Williamsc5ed9262016-05-18 14:50:12 -0700383 if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
Dan Williamse1455742015-07-30 17:57:47 -0400384 return -ENODEV;
385
386 checksum = le64_to_cpu(pfn_sb->checksum);
387 pfn_sb->checksum = 0;
388 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
389 return -ENODEV;
390 pfn_sb->checksum = cpu_to_le64(checksum);
391
Dan Williamsa34d5e82015-12-12 16:09:14 -0800392 if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
393 return -ENODEV;
394
Dan Williamscfe30b82016-03-03 09:38:00 -0800395 if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
396 pfn_sb->start_pad = 0;
397 pfn_sb->end_trunc = 0;
398 }
399
Dan Williams45a0dac2016-03-31 15:41:18 -0700400 if (__le16_to_cpu(pfn_sb->version_minor) < 2)
401 pfn_sb->align = 0;
402
Dan Williamse1455742015-07-30 17:57:47 -0400403 switch (le32_to_cpu(pfn_sb->mode)) {
404 case PFN_MODE_RAM:
Dan Williamse1455742015-07-30 17:57:47 -0400405 case PFN_MODE_PMEM:
Dan Williams45eb5702016-01-29 17:42:51 -0800406 break;
Dan Williamse1455742015-07-30 17:57:47 -0400407 default:
408 return -ENXIO;
409 }
410
Dan Williams1ee66672016-06-23 17:50:39 -0700411 align = le32_to_cpu(pfn_sb->align);
412 offset = le64_to_cpu(pfn_sb->dataoff);
Dan Williams19deaa22017-12-19 15:07:10 -0800413 start_pad = le32_to_cpu(pfn_sb->start_pad);
Dan Williams1ee66672016-06-23 17:50:39 -0700414 if (align == 0)
415 align = 1UL << ilog2(offset);
416 mode = le32_to_cpu(pfn_sb->mode);
417
Dan Williamse1455742015-07-30 17:57:47 -0400418 if (!nd_pfn->uuid) {
Dan Williams1ee66672016-06-23 17:50:39 -0700419 /*
420 * When probing a namepace via nd_pfn_probe() the uuid
421 * is NULL (see: nd_pfn_devinit()) we init settings from
422 * pfn_sb
423 */
Dan Williamse1455742015-07-30 17:57:47 -0400424 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
425 if (!nd_pfn->uuid)
426 return -ENOMEM;
Dan Williams1ee66672016-06-23 17:50:39 -0700427 nd_pfn->align = align;
428 nd_pfn->mode = mode;
Dan Williamse1455742015-07-30 17:57:47 -0400429 } else {
Dan Williams1ee66672016-06-23 17:50:39 -0700430 /*
431 * When probing a pfn / dax instance we validate the
432 * live settings against the pfn_sb
433 */
Dan Williamse1455742015-07-30 17:57:47 -0400434 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
Dan Williamse5670562016-04-07 19:59:27 -0700435 return -ENODEV;
Dan Williams1ee66672016-06-23 17:50:39 -0700436
437 /*
438 * If the uuid validates, but other settings mismatch
439 * return EINVAL because userspace has managed to change
440 * the configuration without specifying new
441 * identification.
442 */
443 if (nd_pfn->align != align || nd_pfn->mode != mode) {
444 dev_err(&nd_pfn->dev,
445 "init failed, settings mismatch\n");
446 dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
447 nd_pfn->align, align, nd_pfn->mode,
448 mode);
449 return -EINVAL;
450 }
Dan Williamse1455742015-07-30 17:57:47 -0400451 }
452
Dan Williams1ee66672016-06-23 17:50:39 -0700453 if (align > nvdimm_namespace_capacity(ndns)) {
Dan Williams315c5622015-12-10 14:45:23 -0800454 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
Dan Williams1ee66672016-06-23 17:50:39 -0700455 align, nvdimm_namespace_capacity(ndns));
Dan Williams315c5622015-12-10 14:45:23 -0800456 return -EINVAL;
457 }
458
Dan Williamse1455742015-07-30 17:57:47 -0400459 /*
460 * These warnings are verbose because they can only trigger in
461 * the case where the physical address alignment of the
462 * namespace has changed since the pfn superblock was
463 * established.
464 */
Dan Williamse1455742015-07-30 17:57:47 -0400465 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f1e8ce2015-12-10 15:14:20 -0800466 if (offset >= resource_size(&nsio->res)) {
Dan Williamse1455742015-07-30 17:57:47 -0400467 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
468 dev_name(&ndns->dev));
469 return -EBUSY;
470 }
471
Dan Williams19deaa22017-12-19 15:07:10 -0800472 if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align))
Dan Williams5e24c9f2016-05-21 11:01:41 -0700473 || !IS_ALIGNED(offset, PAGE_SIZE)) {
Dan Williams1ee66672016-06-23 17:50:39 -0700474 dev_err(&nd_pfn->dev,
475 "bad offset: %#llx dax disabled align: %#lx\n",
476 offset, align);
Dan Williams315c5622015-12-10 14:45:23 -0800477 return -ENXIO;
478 }
479
Dan Williamse1455742015-07-30 17:57:47 -0400480 return 0;
481}
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400482EXPORT_SYMBOL(nd_pfn_validate);
Dan Williamse1455742015-07-30 17:57:47 -0400483
Dan Williams200c79d2016-03-22 00:22:16 -0700484int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
Dan Williamse1455742015-07-30 17:57:47 -0400485{
486 int rc;
Dan Williamse1455742015-07-30 17:57:47 -0400487 struct nd_pfn *nd_pfn;
Dan Williamsbd032942016-03-17 18:16:15 -0700488 struct device *pfn_dev;
Dan Williamse1455742015-07-30 17:57:47 -0400489 struct nd_pfn_sb *pfn_sb;
490 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
491
492 if (ndns->force_raw)
493 return -ENODEV;
494
Dan Williamsb3fde742017-06-04 10:18:39 +0900495 switch (ndns->claim_class) {
496 case NVDIMM_CCLASS_NONE:
497 case NVDIMM_CCLASS_PFN:
498 break;
499 default:
500 return -ENODEV;
501 }
502
Dan Williamse1455742015-07-30 17:57:47 -0400503 nvdimm_bus_lock(&ndns->dev);
Dan Williamscd034122016-03-11 10:15:36 -0800504 nd_pfn = nd_pfn_alloc(nd_region);
505 pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
Dan Williamse1455742015-07-30 17:57:47 -0400506 nvdimm_bus_unlock(&ndns->dev);
Dan Williamsbd032942016-03-17 18:16:15 -0700507 if (!pfn_dev)
Dan Williamse1455742015-07-30 17:57:47 -0400508 return -ENOMEM;
Dan Williamsbd032942016-03-17 18:16:15 -0700509 pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
510 nd_pfn = to_nd_pfn(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400511 nd_pfn->pfn_sb = pfn_sb;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700512 rc = nd_pfn_validate(nd_pfn, PFN_SIG);
Dan Williams426824d2018-03-05 16:39:31 -0800513 dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>");
Dan Williamse1455742015-07-30 17:57:47 -0400514 if (rc < 0) {
Dan Williams452bae02017-04-28 22:05:14 -0700515 nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
Dan Williamsbd032942016-03-17 18:16:15 -0700516 put_device(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400517 } else
Dan Williamsbd032942016-03-17 18:16:15 -0700518 __nd_device_register(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400519
520 return rc;
521}
522EXPORT_SYMBOL(nd_pfn_probe);
Dan Williamsac515c02016-03-22 00:29:43 -0700523
524/*
525 * We hotplug memory at section granularity, pad the reserved area from
526 * the previous section base to the namespace base address.
527 */
528static unsigned long init_altmap_base(resource_size_t base)
529{
530 unsigned long base_pfn = PHYS_PFN(base);
531
532 return PFN_SECTION_ALIGN_DOWN(base_pfn);
533}
534
535static unsigned long init_altmap_reserve(resource_size_t base)
536{
537 unsigned long reserve = PHYS_PFN(SZ_8K);
538 unsigned long base_pfn = PHYS_PFN(base);
539
540 reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
541 return reserve;
542}
543
Christoph Hellwige8d51342017-12-29 08:54:05 +0100544static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
Dan Williamsac515c02016-03-22 00:29:43 -0700545{
Christoph Hellwige8d51342017-12-29 08:54:05 +0100546 struct resource *res = &pgmap->res;
547 struct vmem_altmap *altmap = &pgmap->altmap;
Dan Williamsac515c02016-03-22 00:29:43 -0700548 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
549 u64 offset = le64_to_cpu(pfn_sb->dataoff);
550 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
551 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
552 struct nd_namespace_common *ndns = nd_pfn->ndns;
553 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
554 resource_size_t base = nsio->res.start + start_pad;
555 struct vmem_altmap __altmap = {
556 .base_pfn = init_altmap_base(base),
557 .reserve = init_altmap_reserve(base),
558 };
559
560 memcpy(res, &nsio->res, sizeof(*res));
561 res->start += start_pad;
562 res->end -= end_trunc;
563
Dan Williamsac515c02016-03-22 00:29:43 -0700564 if (nd_pfn->mode == PFN_MODE_RAM) {
565 if (offset < SZ_8K)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100566 return -EINVAL;
Dan Williamsac515c02016-03-22 00:29:43 -0700567 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100568 pgmap->altmap_valid = false;
Dan Williamsac515c02016-03-22 00:29:43 -0700569 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
Dan Williamsd5483fe2017-05-04 19:54:42 -0700570 nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res)
571 - offset) / PAGE_SIZE);
Dan Williamsac515c02016-03-22 00:29:43 -0700572 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
573 dev_info(&nd_pfn->dev,
574 "number of pfns truncated from %lld to %ld\n",
575 le64_to_cpu(nd_pfn->pfn_sb->npfns),
576 nd_pfn->npfns);
577 memcpy(altmap, &__altmap, sizeof(*altmap));
578 altmap->free = PHYS_PFN(offset - SZ_8K);
579 altmap->alloc = 0;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100580 pgmap->altmap_valid = true;
Dan Williamsac515c02016-03-22 00:29:43 -0700581 } else
Christoph Hellwige8d51342017-12-29 08:54:05 +0100582 return -ENXIO;
Dan Williamsac515c02016-03-22 00:29:43 -0700583
Christoph Hellwige8d51342017-12-29 08:54:05 +0100584 return 0;
Dan Williamsac515c02016-03-22 00:29:43 -0700585}
586
Dan Williams41fce902017-12-04 14:07:43 -0800587static u64 phys_pmem_align_down(struct nd_pfn *nd_pfn, u64 phys)
588{
589 return min_t(u64, PHYS_SECTION_ALIGN_DOWN(phys),
590 ALIGN_DOWN(phys, nd_pfn->align));
591}
592
Dan Williamsac515c02016-03-22 00:29:43 -0700593static int nd_pfn_init(struct nd_pfn *nd_pfn)
594{
Dan Williams52ac23b2016-03-31 09:37:11 -0700595 u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
Dan Williamsac515c02016-03-22 00:29:43 -0700596 struct nd_namespace_common *ndns = nd_pfn->ndns;
597 u32 start_pad = 0, end_trunc = 0;
598 resource_size_t start, size;
599 struct nd_namespace_io *nsio;
600 struct nd_region *nd_region;
601 struct nd_pfn_sb *pfn_sb;
602 unsigned long npfns;
603 phys_addr_t offset;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700604 const char *sig;
Dan Williamsac515c02016-03-22 00:29:43 -0700605 u64 checksum;
606 int rc;
607
608 pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
609 if (!pfn_sb)
610 return -ENOMEM;
611
612 nd_pfn->pfn_sb = pfn_sb;
Dan Williamsc5ed9262016-05-18 14:50:12 -0700613 if (is_nd_dax(&nd_pfn->dev))
614 sig = DAX_SIG;
615 else
616 sig = PFN_SIG;
617 rc = nd_pfn_validate(nd_pfn, sig);
Dan Williamsac515c02016-03-22 00:29:43 -0700618 if (rc != -ENODEV)
619 return rc;
620
621 /* no info block, do init */;
622 nd_region = to_nd_region(nd_pfn->dev.parent);
623 if (nd_region->ro) {
624 dev_info(&nd_pfn->dev,
625 "%s is read-only, unable to init metadata\n",
626 dev_name(&nd_region->dev));
627 return -ENXIO;
628 }
629
630 memset(pfn_sb, 0, sizeof(*pfn_sb));
631
632 /*
633 * Check if pmem collides with 'System RAM' when section aligned and
634 * trim it accordingly
635 */
636 nsio = to_nd_namespace_io(&ndns->dev);
637 start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
638 size = resource_size(&nsio->res);
639 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
640 IORES_DESC_NONE) == REGION_MIXED) {
641 start = nsio->res.start;
642 start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
643 }
644
645 start = nsio->res.start;
646 size = PHYS_SECTION_ALIGN_UP(start + size) - start;
647 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
Dan Williams41fce902017-12-04 14:07:43 -0800648 IORES_DESC_NONE) == REGION_MIXED
649 || !IS_ALIGNED(start + resource_size(&nsio->res),
650 nd_pfn->align)) {
Dan Williamsac515c02016-03-22 00:29:43 -0700651 size = resource_size(&nsio->res);
Dan Williams41fce902017-12-04 14:07:43 -0800652 end_trunc = start + size - phys_pmem_align_down(nd_pfn,
653 start + size);
Dan Williamsac515c02016-03-22 00:29:43 -0700654 }
655
656 if (start_pad + end_trunc)
Dan Williams41fce902017-12-04 14:07:43 -0800657 dev_info(&nd_pfn->dev, "%s alignment collision, truncate %d bytes\n",
Dan Williamsac515c02016-03-22 00:29:43 -0700658 dev_name(&ndns->dev), start_pad + end_trunc);
659
660 /*
661 * Note, we use 64 here for the standard size of struct page,
662 * debugging options may cause it to be larger in which case the
663 * implementation will limit the pfns advertised through
664 * ->direct_access() to those that are included in the memmap.
665 */
666 start += start_pad;
667 size = resource_size(&nsio->res);
Dan Williamsd5483fe2017-05-04 19:54:42 -0700668 npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
669 / PAGE_SIZE);
Dan Williams594d6d92016-05-18 09:59:34 -0700670 if (nd_pfn->mode == PFN_MODE_PMEM) {
Dan Williams594d6d92016-05-18 09:59:34 -0700671 /*
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000672 * The altmap should be padded out to the block size used
673 * when populating the vmemmap. This *should* be equal to
674 * PMD_SIZE for most architectures.
Dan Williams594d6d92016-05-18 09:59:34 -0700675 */
Dan Williamsbfb34522017-02-04 14:47:31 -0800676 offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve,
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000677 max(nd_pfn->align, PMD_SIZE)) - start;
Dan Williams594d6d92016-05-18 09:59:34 -0700678 } else if (nd_pfn->mode == PFN_MODE_RAM)
Dan Williams52ac23b2016-03-31 09:37:11 -0700679 offset = ALIGN(start + SZ_8K + dax_label_reserve,
680 nd_pfn->align) - start;
Dan Williamsac515c02016-03-22 00:29:43 -0700681 else
682 return -ENXIO;
683
684 if (offset + start_pad + end_trunc >= size) {
685 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
686 dev_name(&ndns->dev));
687 return -ENXIO;
688 }
689
690 npfns = (size - offset - start_pad - end_trunc) / SZ_4K;
691 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
692 pfn_sb->dataoff = cpu_to_le64(offset);
693 pfn_sb->npfns = cpu_to_le64(npfns);
Dan Williamsc5ed9262016-05-18 14:50:12 -0700694 memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
Dan Williamsac515c02016-03-22 00:29:43 -0700695 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
696 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
697 pfn_sb->version_major = cpu_to_le16(1);
Dan Williams45a0dac2016-03-31 15:41:18 -0700698 pfn_sb->version_minor = cpu_to_le16(2);
Dan Williamsac515c02016-03-22 00:29:43 -0700699 pfn_sb->start_pad = cpu_to_le32(start_pad);
700 pfn_sb->end_trunc = cpu_to_le32(end_trunc);
Dan Williams45a0dac2016-03-31 15:41:18 -0700701 pfn_sb->align = cpu_to_le32(nd_pfn->align);
Dan Williamsac515c02016-03-22 00:29:43 -0700702 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
703 pfn_sb->checksum = cpu_to_le64(checksum);
704
Vishal Verma3ae3d672017-05-10 15:01:30 -0600705 return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
Dan Williamsac515c02016-03-22 00:29:43 -0700706}
707
708/*
709 * Determine the effective resource range and vmem_altmap from an nd_pfn
710 * instance.
711 */
Christoph Hellwige8d51342017-12-29 08:54:05 +0100712int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
Dan Williamsac515c02016-03-22 00:29:43 -0700713{
714 int rc;
715
716 if (!nd_pfn->uuid || !nd_pfn->ndns)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100717 return -ENODEV;
Dan Williamsac515c02016-03-22 00:29:43 -0700718
719 rc = nd_pfn_init(nd_pfn);
720 if (rc)
Christoph Hellwige8d51342017-12-29 08:54:05 +0100721 return rc;
Dan Williamsac515c02016-03-22 00:29:43 -0700722
Christoph Hellwige8d51342017-12-29 08:54:05 +0100723 /* we need a valid pfn_sb before we can init a dev_pagemap */
724 return __nvdimm_setup_pfn(nd_pfn, pgmap);
Dan Williamsac515c02016-03-22 00:29:43 -0700725}
726EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);