blob: 2248056d29e7e562cbb6e1ad898730e75f89fded [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
30 dev_dbg(dev, "%s\n", __func__);
31 nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
32 ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
33 kfree(nd_pfn->uuid);
34 kfree(nd_pfn);
35}
36
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
Dan Williamscd034122016-03-11 10:15:36 -080057static struct nd_pfn *to_nd_pfn_safe(struct device *dev)
58{
59 /*
60 * pfn device attributes are re-used by dax device instances, so we
61 * need to be careful to correct device-to-nd_pfn conversion.
62 */
63 if (is_nd_pfn(dev))
64 return to_nd_pfn(dev);
65
66 if (is_nd_dax(dev)) {
67 struct nd_dax *nd_dax = to_nd_dax(dev);
68
69 return &nd_dax->nd_pfn;
70 }
71
72 WARN_ON(1);
73 return NULL;
74}
75
Dan Williamse1455742015-07-30 17:57:47 -040076static ssize_t mode_show(struct device *dev,
77 struct device_attribute *attr, char *buf)
78{
Dan Williamscd034122016-03-11 10:15:36 -080079 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040080
81 switch (nd_pfn->mode) {
82 case PFN_MODE_RAM:
83 return sprintf(buf, "ram\n");
84 case PFN_MODE_PMEM:
85 return sprintf(buf, "pmem\n");
86 default:
87 return sprintf(buf, "none\n");
88 }
89}
90
91static ssize_t mode_store(struct device *dev,
92 struct device_attribute *attr, const char *buf, size_t len)
93{
Dan Williamscd034122016-03-11 10:15:36 -080094 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -040095 ssize_t rc = 0;
96
97 device_lock(dev);
98 nvdimm_bus_lock(dev);
99 if (dev->driver)
100 rc = -EBUSY;
101 else {
102 size_t n = len - 1;
103
104 if (strncmp(buf, "pmem\n", n) == 0
105 || strncmp(buf, "pmem", n) == 0) {
Dan Williamsd2c0f042016-01-15 16:56:26 -0800106 nd_pfn->mode = PFN_MODE_PMEM;
Dan Williamse1455742015-07-30 17:57:47 -0400107 } else if (strncmp(buf, "ram\n", n) == 0
108 || strncmp(buf, "ram", n) == 0)
109 nd_pfn->mode = PFN_MODE_RAM;
110 else if (strncmp(buf, "none\n", n) == 0
111 || strncmp(buf, "none", n) == 0)
112 nd_pfn->mode = PFN_MODE_NONE;
113 else
114 rc = -EINVAL;
115 }
116 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
117 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
118 nvdimm_bus_unlock(dev);
119 device_unlock(dev);
120
121 return rc ? rc : len;
122}
123static DEVICE_ATTR_RW(mode);
124
Dan Williams315c5622015-12-10 14:45:23 -0800125static ssize_t align_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
127{
Dan Williamscd034122016-03-11 10:15:36 -0800128 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800129
130 return sprintf(buf, "%lx\n", nd_pfn->align);
131}
132
133static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
134{
135 unsigned long val;
136 int rc;
137
138 rc = kstrtoul(buf, 0, &val);
139 if (rc)
140 return rc;
141
142 if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G)
143 return -EINVAL;
144
145 if (nd_pfn->dev.driver)
146 return -EBUSY;
147 else
148 nd_pfn->align = val;
149
150 return 0;
151}
152
153static ssize_t align_store(struct device *dev,
154 struct device_attribute *attr, const char *buf, size_t len)
155{
Dan Williamscd034122016-03-11 10:15:36 -0800156 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williams315c5622015-12-10 14:45:23 -0800157 ssize_t rc;
158
159 device_lock(dev);
160 nvdimm_bus_lock(dev);
161 rc = __align_store(nd_pfn, buf);
162 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
163 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
164 nvdimm_bus_unlock(dev);
165 device_unlock(dev);
166
167 return rc ? rc : len;
168}
169static DEVICE_ATTR_RW(align);
170
Dan Williamse1455742015-07-30 17:57:47 -0400171static ssize_t uuid_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
Dan Williamscd034122016-03-11 10:15:36 -0800174 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400175
176 if (nd_pfn->uuid)
177 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
178 return sprintf(buf, "\n");
179}
180
181static ssize_t uuid_store(struct device *dev,
182 struct device_attribute *attr, const char *buf, size_t len)
183{
Dan Williamscd034122016-03-11 10:15:36 -0800184 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400185 ssize_t rc;
186
187 device_lock(dev);
188 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
189 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
190 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
191 device_unlock(dev);
192
193 return rc ? rc : len;
194}
195static DEVICE_ATTR_RW(uuid);
196
197static ssize_t namespace_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 Williamse1455742015-07-30 17:57:47 -0400201 ssize_t rc;
202
203 nvdimm_bus_lock(dev);
204 rc = sprintf(buf, "%s\n", nd_pfn->ndns
205 ? dev_name(&nd_pfn->ndns->dev) : "");
206 nvdimm_bus_unlock(dev);
207 return rc;
208}
209
210static ssize_t namespace_store(struct device *dev,
211 struct device_attribute *attr, const char *buf, size_t len)
212{
Dan Williamscd034122016-03-11 10:15:36 -0800213 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400214 ssize_t rc;
215
Dan Williamse1455742015-07-30 17:57:47 -0400216 device_lock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800217 nvdimm_bus_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400218 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
219 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
220 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
Dan Williamse1455742015-07-30 17:57:47 -0400221 nvdimm_bus_unlock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800222 device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400223
224 return rc;
225}
226static DEVICE_ATTR_RW(namespace);
227
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800228static ssize_t resource_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
230{
Dan Williamscd034122016-03-11 10:15:36 -0800231 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800232 ssize_t rc;
233
234 device_lock(dev);
235 if (dev->driver) {
236 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
237 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
238 struct nd_namespace_common *ndns = nd_pfn->ndns;
239 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
240 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
241
242 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
243 + start_pad + offset);
244 } else {
245 /* no address to convey if the pfn instance is disabled */
246 rc = -ENXIO;
247 }
248 device_unlock(dev);
249
250 return rc;
251}
252static DEVICE_ATTR_RO(resource);
253
254static ssize_t size_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
256{
Dan Williamscd034122016-03-11 10:15:36 -0800257 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800258 ssize_t rc;
259
260 device_lock(dev);
261 if (dev->driver) {
262 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
263 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
264 struct nd_namespace_common *ndns = nd_pfn->ndns;
265 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
266 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
267 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
268
269 rc = sprintf(buf, "%llu\n", (unsigned long long)
270 resource_size(&nsio->res) - start_pad
271 - end_trunc - offset);
272 } else {
273 /* no size to convey if the pfn instance is disabled */
274 rc = -ENXIO;
275 }
276 device_unlock(dev);
277
278 return rc;
279}
280static DEVICE_ATTR_RO(size);
281
Dan Williamse1455742015-07-30 17:57:47 -0400282static struct attribute *nd_pfn_attributes[] = {
283 &dev_attr_mode.attr,
284 &dev_attr_namespace.attr,
285 &dev_attr_uuid.attr,
Dan Williams315c5622015-12-10 14:45:23 -0800286 &dev_attr_align.attr,
Dan Williamsf6ed58c2016-03-03 09:46:04 -0800287 &dev_attr_resource.attr,
288 &dev_attr_size.attr,
Dan Williamse1455742015-07-30 17:57:47 -0400289 NULL,
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,
294};
295
296static const struct attribute_group *nd_pfn_attribute_groups[] = {
297 &nd_pfn_attribute_group,
298 &nd_device_attribute_group,
299 &nd_numa_attribute_group,
300 NULL,
301};
302
Dan Williamscd034122016-03-11 10:15:36 -0800303struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
Dan Williamse1455742015-07-30 17:57:47 -0400304 struct nd_namespace_common *ndns)
305{
Dan Williamscd034122016-03-11 10:15:36 -0800306 struct device *dev = &nd_pfn->dev;
307
308 if (!nd_pfn)
309 return NULL;
310
311 nd_pfn->mode = PFN_MODE_NONE;
312 nd_pfn->align = HPAGE_SIZE;
313 dev = &nd_pfn->dev;
314 device_initialize(&nd_pfn->dev);
315 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
316 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
317 __func__, dev_name(ndns->claim));
318 put_device(dev);
319 return NULL;
320 }
321 return dev;
322}
323
324static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
325{
Dan Williamse1455742015-07-30 17:57:47 -0400326 struct nd_pfn *nd_pfn;
327 struct device *dev;
328
Dan Williamse1455742015-07-30 17:57:47 -0400329 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
330 if (!nd_pfn)
331 return NULL;
332
333 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
334 if (nd_pfn->id < 0) {
335 kfree(nd_pfn);
336 return NULL;
337 }
338
Dan Williamse1455742015-07-30 17:57:47 -0400339 dev = &nd_pfn->dev;
340 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
Dan Williamse1455742015-07-30 17:57:47 -0400341 dev->groups = nd_pfn_attribute_groups;
Dan Williamscd034122016-03-11 10:15:36 -0800342 dev->type = &nd_pfn_device_type;
343 dev->parent = &nd_region->dev;
344
345 return nd_pfn;
Dan Williamse1455742015-07-30 17:57:47 -0400346}
347
348struct device *nd_pfn_create(struct nd_region *nd_region)
349{
Dan Williamscd034122016-03-11 10:15:36 -0800350 struct nd_pfn *nd_pfn;
351 struct device *dev;
Dan Williamse1455742015-07-30 17:57:47 -0400352
Dan Williamscd034122016-03-11 10:15:36 -0800353 if (!is_nd_pmem(&nd_region->dev))
354 return NULL;
355
356 nd_pfn = nd_pfn_alloc(nd_region);
357 dev = nd_pfn_devinit(nd_pfn, NULL);
358
359 __nd_device_register(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400360 return dev;
361}
362
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400363int nd_pfn_validate(struct nd_pfn *nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -0400364{
Dan Williamse1455742015-07-30 17:57:47 -0400365 u64 checksum, offset;
Dan Williamsa34d5e82015-12-12 16:09:14 -0800366 struct nd_namespace_io *nsio;
367 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
368 struct nd_namespace_common *ndns = nd_pfn->ndns;
369 const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
Dan Williamse1455742015-07-30 17:57:47 -0400370
371 if (!pfn_sb || !ndns)
372 return -ENODEV;
373
374 if (!is_nd_pmem(nd_pfn->dev.parent))
375 return -ENODEV;
376
Dan Williamse1455742015-07-30 17:57:47 -0400377 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
378 return -ENXIO;
379
380 if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
381 return -ENODEV;
382
383 checksum = le64_to_cpu(pfn_sb->checksum);
384 pfn_sb->checksum = 0;
385 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
386 return -ENODEV;
387 pfn_sb->checksum = cpu_to_le64(checksum);
388
Dan Williamsa34d5e82015-12-12 16:09:14 -0800389 if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
390 return -ENODEV;
391
Dan Williamscfe30b82016-03-03 09:38:00 -0800392 if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
393 pfn_sb->start_pad = 0;
394 pfn_sb->end_trunc = 0;
395 }
396
Dan Williams45a0dac2016-03-31 15:41:18 -0700397 if (__le16_to_cpu(pfn_sb->version_minor) < 2)
398 pfn_sb->align = 0;
399
Dan Williamse1455742015-07-30 17:57:47 -0400400 switch (le32_to_cpu(pfn_sb->mode)) {
401 case PFN_MODE_RAM:
Dan Williamse1455742015-07-30 17:57:47 -0400402 case PFN_MODE_PMEM:
Dan Williams45eb5702016-01-29 17:42:51 -0800403 break;
Dan Williamse1455742015-07-30 17:57:47 -0400404 default:
405 return -ENXIO;
406 }
407
408 if (!nd_pfn->uuid) {
409 /* from probe we allocate */
410 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
411 if (!nd_pfn->uuid)
412 return -ENOMEM;
413 } else {
414 /* from init we validate */
415 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
Dan Williamse5670562016-04-07 19:59:27 -0700416 return -ENODEV;
Dan Williamse1455742015-07-30 17:57:47 -0400417 }
418
Dan Williams315c5622015-12-10 14:45:23 -0800419 if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) {
420 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
421 nd_pfn->align, nvdimm_namespace_capacity(ndns));
422 return -EINVAL;
423 }
424
Dan Williamse1455742015-07-30 17:57:47 -0400425 /*
426 * These warnings are verbose because they can only trigger in
427 * the case where the physical address alignment of the
428 * namespace has changed since the pfn superblock was
429 * established.
430 */
431 offset = le64_to_cpu(pfn_sb->dataoff);
432 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f1e8ce2015-12-10 15:14:20 -0800433 if (offset >= resource_size(&nsio->res)) {
Dan Williamse1455742015-07-30 17:57:47 -0400434 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
435 dev_name(&ndns->dev));
436 return -EBUSY;
437 }
438
Dan Williams45a0dac2016-03-31 15:41:18 -0700439 nd_pfn->align = le32_to_cpu(pfn_sb->align);
Dan Williams315c5622015-12-10 14:45:23 -0800440 if (!is_power_of_2(offset) || offset < PAGE_SIZE) {
441 dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n",
442 offset);
443 return -ENXIO;
444 }
445
Dan Williamse1455742015-07-30 17:57:47 -0400446 return 0;
447}
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400448EXPORT_SYMBOL(nd_pfn_validate);
Dan Williamse1455742015-07-30 17:57:47 -0400449
Dan Williams200c79d2016-03-22 00:22:16 -0700450int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
Dan Williamse1455742015-07-30 17:57:47 -0400451{
452 int rc;
Dan Williamse1455742015-07-30 17:57:47 -0400453 struct nd_pfn *nd_pfn;
Dan Williamsbd032942016-03-17 18:16:15 -0700454 struct device *pfn_dev;
Dan Williamse1455742015-07-30 17:57:47 -0400455 struct nd_pfn_sb *pfn_sb;
456 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
457
458 if (ndns->force_raw)
459 return -ENODEV;
460
461 nvdimm_bus_lock(&ndns->dev);
Dan Williamscd034122016-03-11 10:15:36 -0800462 nd_pfn = nd_pfn_alloc(nd_region);
463 pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
Dan Williamse1455742015-07-30 17:57:47 -0400464 nvdimm_bus_unlock(&ndns->dev);
Dan Williamsbd032942016-03-17 18:16:15 -0700465 if (!pfn_dev)
Dan Williamse1455742015-07-30 17:57:47 -0400466 return -ENOMEM;
Dan Williamsbd032942016-03-17 18:16:15 -0700467 pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
468 nd_pfn = to_nd_pfn(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400469 nd_pfn->pfn_sb = pfn_sb;
470 rc = nd_pfn_validate(nd_pfn);
Dan Williamsbd032942016-03-17 18:16:15 -0700471 dev_dbg(dev, "%s: pfn: %s\n", __func__,
472 rc == 0 ? dev_name(pfn_dev) : "<none>");
Dan Williamse1455742015-07-30 17:57:47 -0400473 if (rc < 0) {
Dan Williamsbd032942016-03-17 18:16:15 -0700474 __nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
475 put_device(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400476 } else
Dan Williamsbd032942016-03-17 18:16:15 -0700477 __nd_device_register(pfn_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400478
479 return rc;
480}
481EXPORT_SYMBOL(nd_pfn_probe);
Dan Williamsac515c02016-03-22 00:29:43 -0700482
483/*
484 * We hotplug memory at section granularity, pad the reserved area from
485 * the previous section base to the namespace base address.
486 */
487static unsigned long init_altmap_base(resource_size_t base)
488{
489 unsigned long base_pfn = PHYS_PFN(base);
490
491 return PFN_SECTION_ALIGN_DOWN(base_pfn);
492}
493
494static unsigned long init_altmap_reserve(resource_size_t base)
495{
496 unsigned long reserve = PHYS_PFN(SZ_8K);
497 unsigned long base_pfn = PHYS_PFN(base);
498
499 reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
500 return reserve;
501}
502
503static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
504 struct resource *res, struct vmem_altmap *altmap)
505{
506 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
507 u64 offset = le64_to_cpu(pfn_sb->dataoff);
508 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
509 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
510 struct nd_namespace_common *ndns = nd_pfn->ndns;
511 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
512 resource_size_t base = nsio->res.start + start_pad;
513 struct vmem_altmap __altmap = {
514 .base_pfn = init_altmap_base(base),
515 .reserve = init_altmap_reserve(base),
516 };
517
518 memcpy(res, &nsio->res, sizeof(*res));
519 res->start += start_pad;
520 res->end -= end_trunc;
521
522 nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode);
523 if (nd_pfn->mode == PFN_MODE_RAM) {
524 if (offset < SZ_8K)
525 return ERR_PTR(-EINVAL);
526 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
527 altmap = NULL;
528 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
529 nd_pfn->npfns = (resource_size(res) - offset) / PAGE_SIZE;
530 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
531 dev_info(&nd_pfn->dev,
532 "number of pfns truncated from %lld to %ld\n",
533 le64_to_cpu(nd_pfn->pfn_sb->npfns),
534 nd_pfn->npfns);
535 memcpy(altmap, &__altmap, sizeof(*altmap));
536 altmap->free = PHYS_PFN(offset - SZ_8K);
537 altmap->alloc = 0;
538 } else
539 return ERR_PTR(-ENXIO);
540
541 return altmap;
542}
543
544static int nd_pfn_init(struct nd_pfn *nd_pfn)
545{
Dan Williams52ac23b2016-03-31 09:37:11 -0700546 u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
Dan Williamsac515c02016-03-22 00:29:43 -0700547 struct nd_namespace_common *ndns = nd_pfn->ndns;
548 u32 start_pad = 0, end_trunc = 0;
549 resource_size_t start, size;
550 struct nd_namespace_io *nsio;
551 struct nd_region *nd_region;
552 struct nd_pfn_sb *pfn_sb;
553 unsigned long npfns;
554 phys_addr_t offset;
555 u64 checksum;
556 int rc;
557
558 pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
559 if (!pfn_sb)
560 return -ENOMEM;
561
562 nd_pfn->pfn_sb = pfn_sb;
563 rc = nd_pfn_validate(nd_pfn);
564 if (rc != -ENODEV)
565 return rc;
566
567 /* no info block, do init */;
568 nd_region = to_nd_region(nd_pfn->dev.parent);
569 if (nd_region->ro) {
570 dev_info(&nd_pfn->dev,
571 "%s is read-only, unable to init metadata\n",
572 dev_name(&nd_region->dev));
573 return -ENXIO;
574 }
575
576 memset(pfn_sb, 0, sizeof(*pfn_sb));
577
578 /*
579 * Check if pmem collides with 'System RAM' when section aligned and
580 * trim it accordingly
581 */
582 nsio = to_nd_namespace_io(&ndns->dev);
583 start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
584 size = resource_size(&nsio->res);
585 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
586 IORES_DESC_NONE) == REGION_MIXED) {
587 start = nsio->res.start;
588 start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
589 }
590
591 start = nsio->res.start;
592 size = PHYS_SECTION_ALIGN_UP(start + size) - start;
593 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
594 IORES_DESC_NONE) == REGION_MIXED) {
595 size = resource_size(&nsio->res);
596 end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size);
597 }
598
599 if (start_pad + end_trunc)
600 dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n",
601 dev_name(&ndns->dev), start_pad + end_trunc);
602
603 /*
604 * Note, we use 64 here for the standard size of struct page,
605 * debugging options may cause it to be larger in which case the
606 * implementation will limit the pfns advertised through
607 * ->direct_access() to those that are included in the memmap.
608 */
609 start += start_pad;
610 size = resource_size(&nsio->res);
611 npfns = (size - start_pad - end_trunc - SZ_8K) / SZ_4K;
Dan Williams594d6d92016-05-18 09:59:34 -0700612 if (nd_pfn->mode == PFN_MODE_PMEM) {
613 unsigned long memmap_size;
614
615 /*
616 * vmemmap_populate_hugepages() allocates the memmap array in
617 * HPAGE_SIZE chunks.
618 */
619 memmap_size = ALIGN(64 * npfns, HPAGE_SIZE);
620 offset = ALIGN(start + SZ_8K + memmap_size + dax_label_reserve,
Dan Williams52ac23b2016-03-31 09:37:11 -0700621 nd_pfn->align) - start;
Dan Williams594d6d92016-05-18 09:59:34 -0700622 } else if (nd_pfn->mode == PFN_MODE_RAM)
Dan Williams52ac23b2016-03-31 09:37:11 -0700623 offset = ALIGN(start + SZ_8K + dax_label_reserve,
624 nd_pfn->align) - start;
Dan Williamsac515c02016-03-22 00:29:43 -0700625 else
626 return -ENXIO;
627
628 if (offset + start_pad + end_trunc >= size) {
629 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
630 dev_name(&ndns->dev));
631 return -ENXIO;
632 }
633
634 npfns = (size - offset - start_pad - end_trunc) / SZ_4K;
635 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
636 pfn_sb->dataoff = cpu_to_le64(offset);
637 pfn_sb->npfns = cpu_to_le64(npfns);
638 memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN);
639 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
640 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
641 pfn_sb->version_major = cpu_to_le16(1);
Dan Williams45a0dac2016-03-31 15:41:18 -0700642 pfn_sb->version_minor = cpu_to_le16(2);
Dan Williamsac515c02016-03-22 00:29:43 -0700643 pfn_sb->start_pad = cpu_to_le32(start_pad);
644 pfn_sb->end_trunc = cpu_to_le32(end_trunc);
Dan Williams45a0dac2016-03-31 15:41:18 -0700645 pfn_sb->align = cpu_to_le32(nd_pfn->align);
Dan Williamsac515c02016-03-22 00:29:43 -0700646 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
647 pfn_sb->checksum = cpu_to_le64(checksum);
648
649 return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
650}
651
652/*
653 * Determine the effective resource range and vmem_altmap from an nd_pfn
654 * instance.
655 */
656struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
657 struct resource *res, struct vmem_altmap *altmap)
658{
659 int rc;
660
661 if (!nd_pfn->uuid || !nd_pfn->ndns)
662 return ERR_PTR(-ENODEV);
663
664 rc = nd_pfn_init(nd_pfn);
665 if (rc)
666 return ERR_PTR(rc);
667
668 /* we need a valid pfn_sb before we can init a vmem_altmap */
669 return __nvdimm_setup_pfn(nd_pfn, res, altmap);
670}
671EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);