Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e1455744 | 2 | /* |
cd03412a | 3 | * Copyright(c) 2013-2016 Intel Corporation. All rights reserved. |
e1455744 | 4 | */ |
ac515c08 | 5 | #include <linux/memremap.h> |
e1455744 DW |
6 | #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 | ||
17 | static 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 | ||
426824d6 | 22 | dev_dbg(dev, "trace\n"); |
e1455744 DW |
23 | 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 | ||
29 | static struct device_type nd_pfn_device_type = { | |
30 | .name = "nd_pfn", | |
31 | .release = nd_pfn_release, | |
32 | }; | |
33 | ||
34 | bool is_nd_pfn(struct device *dev) | |
35 | { | |
36 | return dev ? dev->type == &nd_pfn_device_type : false; | |
37 | } | |
38 | EXPORT_SYMBOL(is_nd_pfn); | |
39 | ||
40 | struct 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 | } | |
47 | EXPORT_SYMBOL(to_nd_pfn); | |
48 | ||
49 | static ssize_t mode_show(struct device *dev, | |
50 | struct device_attribute *attr, char *buf) | |
51 | { | |
cd03412a | 52 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
53 | |
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 | ||
64 | static ssize_t mode_store(struct device *dev, | |
65 | struct device_attribute *attr, const char *buf, size_t len) | |
66 | { | |
cd03412a | 67 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
68 | ssize_t rc = 0; |
69 | ||
87a30e1f | 70 | nd_device_lock(dev); |
e1455744 DW |
71 | 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) { | |
d2c0f041 | 79 | nd_pfn->mode = PFN_MODE_PMEM; |
e1455744 DW |
80 | } 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 | } | |
426824d6 DW |
89 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
90 | buf[len - 1] == '\n' ? "" : "\n"); | |
e1455744 | 91 | nvdimm_bus_unlock(dev); |
87a30e1f | 92 | nd_device_unlock(dev); |
e1455744 DW |
93 | |
94 | return rc ? rc : len; | |
95 | } | |
96 | static DEVICE_ATTR_RW(mode); | |
97 | ||
315c5625 DW |
98 | static ssize_t align_show(struct device *dev, |
99 | struct device_attribute *attr, char *buf) | |
100 | { | |
cd03412a | 101 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
315c5625 | 102 | |
af7d9f0c | 103 | return sprintf(buf, "%ld\n", nd_pfn->align); |
315c5625 DW |
104 | } |
105 | ||
f5376699 | 106 | static unsigned long *nd_pfn_supported_alignments(unsigned long *alignments) |
1fdadbeb | 107 | { |
f5376699 AK |
108 | |
109 | alignments[0] = PAGE_SIZE; | |
110 | ||
111 | if (has_transparent_hugepage()) { | |
112 | alignments[1] = HPAGE_PMD_SIZE; | |
113 | if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)) | |
114 | alignments[2] = HPAGE_PUD_SIZE; | |
115 | } | |
116 | ||
117 | return alignments; | |
118 | } | |
119 | ||
120 | /* | |
121 | * Use pmd mapping if supported as default alignment | |
122 | */ | |
123 | static unsigned long nd_pfn_default_alignment(void) | |
124 | { | |
125 | ||
126 | if (has_transparent_hugepage()) | |
127 | return HPAGE_PMD_SIZE; | |
128 | return PAGE_SIZE; | |
1fdadbeb OH |
129 | } |
130 | ||
315c5625 DW |
131 | static ssize_t align_store(struct device *dev, |
132 | struct device_attribute *attr, const char *buf, size_t len) | |
133 | { | |
cd03412a | 134 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
f5376699 | 135 | unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, }; |
315c5625 DW |
136 | ssize_t rc; |
137 | ||
87a30e1f | 138 | nd_device_lock(dev); |
315c5625 | 139 | nvdimm_bus_lock(dev); |
f13d2b61 | 140 | rc = nd_size_select_store(dev, buf, &nd_pfn->align, |
f5376699 | 141 | nd_pfn_supported_alignments(aligns)); |
426824d6 DW |
142 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
143 | buf[len - 1] == '\n' ? "" : "\n"); | |
315c5625 | 144 | nvdimm_bus_unlock(dev); |
87a30e1f | 145 | nd_device_unlock(dev); |
315c5625 DW |
146 | |
147 | return rc ? rc : len; | |
148 | } | |
149 | static DEVICE_ATTR_RW(align); | |
150 | ||
e1455744 DW |
151 | static ssize_t uuid_show(struct device *dev, |
152 | struct device_attribute *attr, char *buf) | |
153 | { | |
cd03412a | 154 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
155 | |
156 | if (nd_pfn->uuid) | |
157 | return sprintf(buf, "%pUb\n", nd_pfn->uuid); | |
158 | return sprintf(buf, "\n"); | |
159 | } | |
160 | ||
161 | static ssize_t uuid_store(struct device *dev, | |
162 | struct device_attribute *attr, const char *buf, size_t len) | |
163 | { | |
cd03412a | 164 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
165 | ssize_t rc; |
166 | ||
87a30e1f | 167 | nd_device_lock(dev); |
e1455744 | 168 | rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len); |
426824d6 DW |
169 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
170 | buf[len - 1] == '\n' ? "" : "\n"); | |
87a30e1f | 171 | nd_device_unlock(dev); |
e1455744 DW |
172 | |
173 | return rc ? rc : len; | |
174 | } | |
175 | static DEVICE_ATTR_RW(uuid); | |
176 | ||
177 | static ssize_t namespace_show(struct device *dev, | |
178 | struct device_attribute *attr, char *buf) | |
179 | { | |
cd03412a | 180 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
181 | ssize_t rc; |
182 | ||
183 | nvdimm_bus_lock(dev); | |
184 | rc = sprintf(buf, "%s\n", nd_pfn->ndns | |
185 | ? dev_name(&nd_pfn->ndns->dev) : ""); | |
186 | nvdimm_bus_unlock(dev); | |
187 | return rc; | |
188 | } | |
189 | ||
190 | static ssize_t namespace_store(struct device *dev, | |
191 | struct device_attribute *attr, const char *buf, size_t len) | |
192 | { | |
cd03412a | 193 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
194 | ssize_t rc; |
195 | ||
87a30e1f | 196 | nd_device_lock(dev); |
4ca8b57a | 197 | nvdimm_bus_lock(dev); |
e1455744 | 198 | rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len); |
426824d6 DW |
199 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
200 | buf[len - 1] == '\n' ? "" : "\n"); | |
e1455744 | 201 | nvdimm_bus_unlock(dev); |
87a30e1f | 202 | nd_device_unlock(dev); |
e1455744 DW |
203 | |
204 | return rc; | |
205 | } | |
206 | static DEVICE_ATTR_RW(namespace); | |
207 | ||
f6ed58c7 DW |
208 | static ssize_t resource_show(struct device *dev, |
209 | struct device_attribute *attr, char *buf) | |
210 | { | |
cd03412a | 211 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
f6ed58c7 DW |
212 | ssize_t rc; |
213 | ||
87a30e1f | 214 | nd_device_lock(dev); |
f6ed58c7 DW |
215 | if (dev->driver) { |
216 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; | |
217 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); | |
218 | struct nd_namespace_common *ndns = nd_pfn->ndns; | |
219 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); | |
220 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); | |
221 | ||
222 | rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start | |
223 | + start_pad + offset); | |
224 | } else { | |
225 | /* no address to convey if the pfn instance is disabled */ | |
226 | rc = -ENXIO; | |
227 | } | |
87a30e1f | 228 | nd_device_unlock(dev); |
f6ed58c7 DW |
229 | |
230 | return rc; | |
231 | } | |
232 | static DEVICE_ATTR_RO(resource); | |
233 | ||
234 | static ssize_t size_show(struct device *dev, | |
235 | struct device_attribute *attr, char *buf) | |
236 | { | |
cd03412a | 237 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
f6ed58c7 DW |
238 | ssize_t rc; |
239 | ||
87a30e1f | 240 | nd_device_lock(dev); |
f6ed58c7 DW |
241 | if (dev->driver) { |
242 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; | |
243 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); | |
244 | struct nd_namespace_common *ndns = nd_pfn->ndns; | |
245 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); | |
246 | u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); | |
247 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); | |
248 | ||
249 | rc = sprintf(buf, "%llu\n", (unsigned long long) | |
250 | resource_size(&nsio->res) - start_pad | |
251 | - end_trunc - offset); | |
252 | } else { | |
253 | /* no size to convey if the pfn instance is disabled */ | |
254 | rc = -ENXIO; | |
255 | } | |
87a30e1f | 256 | nd_device_unlock(dev); |
f6ed58c7 DW |
257 | |
258 | return rc; | |
259 | } | |
260 | static DEVICE_ATTR_RO(size); | |
261 | ||
1fdadbeb OH |
262 | static ssize_t supported_alignments_show(struct device *dev, |
263 | struct device_attribute *attr, char *buf) | |
264 | { | |
f5376699 AK |
265 | unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, }; |
266 | ||
267 | return nd_size_select_show(0, | |
268 | nd_pfn_supported_alignments(aligns), buf); | |
1fdadbeb OH |
269 | } |
270 | static DEVICE_ATTR_RO(supported_alignments); | |
271 | ||
e1455744 DW |
272 | static struct attribute *nd_pfn_attributes[] = { |
273 | &dev_attr_mode.attr, | |
274 | &dev_attr_namespace.attr, | |
275 | &dev_attr_uuid.attr, | |
315c5625 | 276 | &dev_attr_align.attr, |
f6ed58c7 DW |
277 | &dev_attr_resource.attr, |
278 | &dev_attr_size.attr, | |
1fdadbeb | 279 | &dev_attr_supported_alignments.attr, |
e1455744 DW |
280 | NULL, |
281 | }; | |
282 | ||
26417ae4 DW |
283 | static umode_t pfn_visible(struct kobject *kobj, struct attribute *a, int n) |
284 | { | |
285 | if (a == &dev_attr_resource.attr) | |
286 | return 0400; | |
287 | return a->mode; | |
288 | } | |
289 | ||
cd03412a | 290 | struct attribute_group nd_pfn_attribute_group = { |
e1455744 | 291 | .attrs = nd_pfn_attributes, |
26417ae4 | 292 | .is_visible = pfn_visible, |
e1455744 DW |
293 | }; |
294 | ||
295 | static const struct attribute_group *nd_pfn_attribute_groups[] = { | |
296 | &nd_pfn_attribute_group, | |
297 | &nd_device_attribute_group, | |
298 | &nd_numa_attribute_group, | |
299 | NULL, | |
300 | }; | |
301 | ||
cd03412a | 302 | struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn, |
e1455744 DW |
303 | struct nd_namespace_common *ndns) |
304 | { | |
0cbfeef2 | 305 | struct device *dev; |
e1455744 | 306 | |
cd03412a | 307 | if (!nd_pfn) |
e1455744 DW |
308 | return NULL; |
309 | ||
cd03412a | 310 | nd_pfn->mode = PFN_MODE_NONE; |
f5376699 | 311 | nd_pfn->align = nd_pfn_default_alignment(); |
cd03412a DW |
312 | dev = &nd_pfn->dev; |
313 | device_initialize(&nd_pfn->dev); | |
314 | if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) { | |
426824d6 DW |
315 | dev_dbg(&ndns->dev, "failed, already claimed by %s\n", |
316 | dev_name(ndns->claim)); | |
cd03412a | 317 | put_device(dev); |
e1455744 | 318 | return NULL; |
cd03412a DW |
319 | } |
320 | return dev; | |
321 | } | |
322 | ||
323 | static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region) | |
324 | { | |
325 | struct nd_pfn *nd_pfn; | |
326 | struct device *dev; | |
e1455744 DW |
327 | |
328 | nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL); | |
329 | if (!nd_pfn) | |
330 | return NULL; | |
331 | ||
332 | nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL); | |
333 | if (nd_pfn->id < 0) { | |
334 | kfree(nd_pfn); | |
335 | return NULL; | |
336 | } | |
337 | ||
e1455744 DW |
338 | dev = &nd_pfn->dev; |
339 | dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id); | |
e1455744 | 340 | dev->groups = nd_pfn_attribute_groups; |
cd03412a DW |
341 | dev->type = &nd_pfn_device_type; |
342 | dev->parent = &nd_region->dev; | |
343 | ||
344 | return nd_pfn; | |
e1455744 DW |
345 | } |
346 | ||
347 | struct device *nd_pfn_create(struct nd_region *nd_region) | |
348 | { | |
cd03412a DW |
349 | struct nd_pfn *nd_pfn; |
350 | struct device *dev; | |
351 | ||
c9e582aa | 352 | if (!is_memory(&nd_region->dev)) |
cd03412a | 353 | return NULL; |
e1455744 | 354 | |
cd03412a DW |
355 | nd_pfn = nd_pfn_alloc(nd_region); |
356 | dev = nd_pfn_devinit(nd_pfn, NULL); | |
e1455744 | 357 | |
cd03412a | 358 | __nd_device_register(dev); |
e1455744 DW |
359 | return dev; |
360 | } | |
361 | ||
48af2f7e VV |
362 | /* |
363 | * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap | |
364 | * space associated with the namespace. If the memmap is set to DRAM, then | |
365 | * this is a no-op. Since the memmap area is freshly initialized during | |
366 | * probe, we have an opportunity to clear any badblocks in this area. | |
367 | */ | |
368 | static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn) | |
369 | { | |
370 | struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent); | |
371 | struct nd_namespace_common *ndns = nd_pfn->ndns; | |
372 | void *zero_page = page_address(ZERO_PAGE(0)); | |
373 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; | |
374 | int num_bad, meta_num, rc, bb_present; | |
375 | sector_t first_bad, meta_start; | |
376 | struct nd_namespace_io *nsio; | |
377 | ||
378 | if (nd_pfn->mode != PFN_MODE_PMEM) | |
379 | return 0; | |
380 | ||
381 | nsio = to_nd_namespace_io(&ndns->dev); | |
382 | meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9; | |
383 | meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start; | |
384 | ||
385 | do { | |
386 | unsigned long zero_len; | |
387 | u64 nsoff; | |
388 | ||
389 | bb_present = badblocks_check(&nd_region->bb, meta_start, | |
390 | meta_num, &first_bad, &num_bad); | |
391 | if (bb_present) { | |
72deb455 | 392 | dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %llx\n", |
48af2f7e VV |
393 | num_bad, first_bad); |
394 | nsoff = ALIGN_DOWN((nd_region->ndr_start | |
395 | + (first_bad << 9)) - nsio->res.start, | |
396 | PAGE_SIZE); | |
397 | zero_len = ALIGN(num_bad << 9, PAGE_SIZE); | |
398 | while (zero_len) { | |
399 | unsigned long chunk = min(zero_len, PAGE_SIZE); | |
400 | ||
401 | rc = nvdimm_write_bytes(ndns, nsoff, zero_page, | |
402 | chunk, 0); | |
403 | if (rc) | |
404 | break; | |
405 | ||
406 | zero_len -= chunk; | |
407 | nsoff += chunk; | |
408 | } | |
409 | if (rc) { | |
410 | dev_err(&nd_pfn->dev, | |
72deb455 | 411 | "error clearing %x badblocks at %llx\n", |
48af2f7e VV |
412 | num_bad, first_bad); |
413 | return rc; | |
414 | } | |
415 | } | |
416 | } while (bb_present); | |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
f5376699 AK |
421 | static bool nd_supported_alignment(unsigned long align) |
422 | { | |
423 | int i; | |
424 | unsigned long supported[MAX_NVDIMM_ALIGN] = { [0] = 0, }; | |
425 | ||
426 | if (align == 0) | |
427 | return false; | |
428 | ||
429 | nd_pfn_supported_alignments(supported); | |
430 | for (i = 0; supported[i]; i++) | |
431 | if (align == supported[i]) | |
432 | return true; | |
433 | return false; | |
434 | } | |
435 | ||
7e3e888d DW |
436 | /** |
437 | * nd_pfn_validate - read and validate info-block | |
438 | * @nd_pfn: fsdax namespace runtime state / properties | |
439 | * @sig: 'devdax' or 'fsdax' signature | |
440 | * | |
441 | * Upon return the info-block buffer contents (->pfn_sb) are | |
442 | * indeterminate when validation fails, and a coherent info-block | |
443 | * otherwise. | |
444 | */ | |
c5ed9268 | 445 | int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig) |
e1455744 | 446 | { |
e1455744 | 447 | u64 checksum, offset; |
1ee6667c | 448 | enum nd_pfn_mode mode; |
a34d5e8a | 449 | struct nd_namespace_io *nsio; |
19deaa21 | 450 | unsigned long align, start_pad; |
a34d5e8a DW |
451 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
452 | struct nd_namespace_common *ndns = nd_pfn->ndns; | |
453 | const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev); | |
e1455744 DW |
454 | |
455 | if (!pfn_sb || !ndns) | |
456 | return -ENODEV; | |
457 | ||
c9e582aa | 458 | if (!is_memory(nd_pfn->dev.parent)) |
e1455744 DW |
459 | return -ENODEV; |
460 | ||
3ae3d67b | 461 | if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0)) |
e1455744 DW |
462 | return -ENXIO; |
463 | ||
c5ed9268 | 464 | if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0) |
e1455744 DW |
465 | return -ENODEV; |
466 | ||
467 | checksum = le64_to_cpu(pfn_sb->checksum); | |
468 | pfn_sb->checksum = 0; | |
469 | if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb)) | |
470 | return -ENODEV; | |
471 | pfn_sb->checksum = cpu_to_le64(checksum); | |
472 | ||
a34d5e8a DW |
473 | if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0) |
474 | return -ENODEV; | |
475 | ||
cfe30b87 DW |
476 | if (__le16_to_cpu(pfn_sb->version_minor) < 1) { |
477 | pfn_sb->start_pad = 0; | |
478 | pfn_sb->end_trunc = 0; | |
479 | } | |
480 | ||
45a0dac0 DW |
481 | if (__le16_to_cpu(pfn_sb->version_minor) < 2) |
482 | pfn_sb->align = 0; | |
483 | ||
edbb52c2 AK |
484 | if (__le16_to_cpu(pfn_sb->version_minor) < 4) { |
485 | pfn_sb->page_struct_size = cpu_to_le16(64); | |
486 | pfn_sb->page_size = cpu_to_le32(PAGE_SIZE); | |
487 | } | |
488 | ||
e1455744 DW |
489 | switch (le32_to_cpu(pfn_sb->mode)) { |
490 | case PFN_MODE_RAM: | |
e1455744 | 491 | case PFN_MODE_PMEM: |
45eb570a | 492 | break; |
e1455744 DW |
493 | default: |
494 | return -ENXIO; | |
495 | } | |
496 | ||
1ee6667c DW |
497 | align = le32_to_cpu(pfn_sb->align); |
498 | offset = le64_to_cpu(pfn_sb->dataoff); | |
19deaa21 | 499 | start_pad = le32_to_cpu(pfn_sb->start_pad); |
1ee6667c DW |
500 | if (align == 0) |
501 | align = 1UL << ilog2(offset); | |
502 | mode = le32_to_cpu(pfn_sb->mode); | |
503 | ||
edbb52c2 AK |
504 | if ((le32_to_cpu(pfn_sb->page_size) > PAGE_SIZE) && |
505 | (mode == PFN_MODE_PMEM)) { | |
506 | dev_err(&nd_pfn->dev, | |
507 | "init failed, page size mismatch %d\n", | |
508 | le32_to_cpu(pfn_sb->page_size)); | |
509 | return -EOPNOTSUPP; | |
510 | } | |
511 | ||
512 | if ((le16_to_cpu(pfn_sb->page_struct_size) < sizeof(struct page)) && | |
513 | (mode == PFN_MODE_PMEM)) { | |
514 | dev_err(&nd_pfn->dev, | |
515 | "init failed, struct page size mismatch %d\n", | |
516 | le16_to_cpu(pfn_sb->page_struct_size)); | |
517 | return -EOPNOTSUPP; | |
518 | } | |
519 | ||
f5376699 AK |
520 | /* |
521 | * Check whether the we support the alignment. For Dax if the | |
522 | * superblock alignment is not matching, we won't initialize | |
523 | * the device. | |
524 | */ | |
525 | if (!nd_supported_alignment(align) && | |
526 | !memcmp(pfn_sb->signature, DAX_SIG, PFN_SIG_LEN)) { | |
527 | dev_err(&nd_pfn->dev, "init failed, alignment mismatch: " | |
528 | "%ld:%ld\n", nd_pfn->align, align); | |
529 | return -EOPNOTSUPP; | |
530 | } | |
531 | ||
e1455744 | 532 | if (!nd_pfn->uuid) { |
1ee6667c DW |
533 | /* |
534 | * When probing a namepace via nd_pfn_probe() the uuid | |
535 | * is NULL (see: nd_pfn_devinit()) we init settings from | |
536 | * pfn_sb | |
537 | */ | |
e1455744 DW |
538 | nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL); |
539 | if (!nd_pfn->uuid) | |
540 | return -ENOMEM; | |
1ee6667c DW |
541 | nd_pfn->align = align; |
542 | nd_pfn->mode = mode; | |
e1455744 | 543 | } else { |
1ee6667c DW |
544 | /* |
545 | * When probing a pfn / dax instance we validate the | |
546 | * live settings against the pfn_sb | |
547 | */ | |
e1455744 | 548 | if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0) |
e5670563 | 549 | return -ENODEV; |
1ee6667c DW |
550 | |
551 | /* | |
552 | * If the uuid validates, but other settings mismatch | |
553 | * return EINVAL because userspace has managed to change | |
554 | * the configuration without specifying new | |
555 | * identification. | |
556 | */ | |
557 | if (nd_pfn->align != align || nd_pfn->mode != mode) { | |
558 | dev_err(&nd_pfn->dev, | |
559 | "init failed, settings mismatch\n"); | |
560 | dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n", | |
561 | nd_pfn->align, align, nd_pfn->mode, | |
562 | mode); | |
563 | return -EINVAL; | |
564 | } | |
e1455744 DW |
565 | } |
566 | ||
1ee6667c | 567 | if (align > nvdimm_namespace_capacity(ndns)) { |
315c5625 | 568 | dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n", |
1ee6667c | 569 | align, nvdimm_namespace_capacity(ndns)); |
315c5625 DW |
570 | return -EINVAL; |
571 | } | |
572 | ||
e1455744 DW |
573 | /* |
574 | * These warnings are verbose because they can only trigger in | |
575 | * the case where the physical address alignment of the | |
576 | * namespace has changed since the pfn superblock was | |
577 | * established. | |
578 | */ | |
e1455744 | 579 | nsio = to_nd_namespace_io(&ndns->dev); |
9f1e8cee | 580 | if (offset >= resource_size(&nsio->res)) { |
e1455744 DW |
581 | dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n", |
582 | dev_name(&ndns->dev)); | |
583 | return -EBUSY; | |
584 | } | |
585 | ||
19deaa21 | 586 | if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align)) |
5e24c9fd | 587 | || !IS_ALIGNED(offset, PAGE_SIZE)) { |
1ee6667c DW |
588 | dev_err(&nd_pfn->dev, |
589 | "bad offset: %#llx dax disabled align: %#lx\n", | |
590 | offset, align); | |
315c5625 DW |
591 | return -ENXIO; |
592 | } | |
593 | ||
48af2f7e | 594 | return nd_pfn_clear_memmap_errors(nd_pfn); |
e1455744 | 595 | } |
32ab0a3f | 596 | EXPORT_SYMBOL(nd_pfn_validate); |
e1455744 | 597 | |
200c79da | 598 | int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns) |
e1455744 DW |
599 | { |
600 | int rc; | |
e1455744 | 601 | struct nd_pfn *nd_pfn; |
bd032943 | 602 | struct device *pfn_dev; |
e1455744 DW |
603 | struct nd_pfn_sb *pfn_sb; |
604 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); | |
605 | ||
606 | if (ndns->force_raw) | |
607 | return -ENODEV; | |
608 | ||
b3fde74e DW |
609 | switch (ndns->claim_class) { |
610 | case NVDIMM_CCLASS_NONE: | |
611 | case NVDIMM_CCLASS_PFN: | |
612 | break; | |
613 | default: | |
614 | return -ENODEV; | |
615 | } | |
616 | ||
e1455744 | 617 | nvdimm_bus_lock(&ndns->dev); |
cd03412a DW |
618 | nd_pfn = nd_pfn_alloc(nd_region); |
619 | pfn_dev = nd_pfn_devinit(nd_pfn, ndns); | |
e1455744 | 620 | nvdimm_bus_unlock(&ndns->dev); |
bd032943 | 621 | if (!pfn_dev) |
e1455744 | 622 | return -ENOMEM; |
7e3e888d | 623 | pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL); |
bd032943 | 624 | nd_pfn = to_nd_pfn(pfn_dev); |
e1455744 | 625 | nd_pfn->pfn_sb = pfn_sb; |
c5ed9268 | 626 | rc = nd_pfn_validate(nd_pfn, PFN_SIG); |
426824d6 | 627 | dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>"); |
e1455744 | 628 | if (rc < 0) { |
452bae0a | 629 | nd_detach_ndns(pfn_dev, &nd_pfn->ndns); |
bd032943 | 630 | put_device(pfn_dev); |
e1455744 | 631 | } else |
bd032943 | 632 | __nd_device_register(pfn_dev); |
e1455744 DW |
633 | |
634 | return rc; | |
635 | } | |
636 | EXPORT_SYMBOL(nd_pfn_probe); | |
ac515c08 | 637 | |
11a35810 DW |
638 | static u32 info_block_reserve(void) |
639 | { | |
640 | return ALIGN(SZ_8K, PAGE_SIZE); | |
641 | } | |
642 | ||
ac515c08 | 643 | /* |
a3619190 DW |
644 | * We hotplug memory at sub-section granularity, pad the reserved area |
645 | * from the previous section base to the namespace base address. | |
ac515c08 DW |
646 | */ |
647 | static unsigned long init_altmap_base(resource_size_t base) | |
648 | { | |
649 | unsigned long base_pfn = PHYS_PFN(base); | |
650 | ||
a3619190 | 651 | return SUBSECTION_ALIGN_DOWN(base_pfn); |
ac515c08 DW |
652 | } |
653 | ||
654 | static unsigned long init_altmap_reserve(resource_size_t base) | |
655 | { | |
11a35810 | 656 | unsigned long reserve = info_block_reserve() >> PAGE_SHIFT; |
ac515c08 DW |
657 | unsigned long base_pfn = PHYS_PFN(base); |
658 | ||
a3619190 | 659 | reserve += base_pfn - SUBSECTION_ALIGN_DOWN(base_pfn); |
ac515c08 DW |
660 | return reserve; |
661 | } | |
662 | ||
e8d51348 | 663 | static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap) |
ac515c08 | 664 | { |
e8d51348 CH |
665 | struct resource *res = &pgmap->res; |
666 | struct vmem_altmap *altmap = &pgmap->altmap; | |
ac515c08 DW |
667 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
668 | u64 offset = le64_to_cpu(pfn_sb->dataoff); | |
669 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); | |
670 | u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); | |
11a35810 | 671 | u32 reserve = info_block_reserve(); |
ac515c08 DW |
672 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
673 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); | |
674 | resource_size_t base = nsio->res.start + start_pad; | |
675 | struct vmem_altmap __altmap = { | |
676 | .base_pfn = init_altmap_base(base), | |
677 | .reserve = init_altmap_reserve(base), | |
678 | }; | |
679 | ||
680 | memcpy(res, &nsio->res, sizeof(*res)); | |
681 | res->start += start_pad; | |
682 | res->end -= end_trunc; | |
683 | ||
ac515c08 | 684 | if (nd_pfn->mode == PFN_MODE_RAM) { |
11a35810 | 685 | if (offset < reserve) |
e8d51348 | 686 | return -EINVAL; |
ac515c08 | 687 | nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns); |
ac515c08 | 688 | } else if (nd_pfn->mode == PFN_MODE_PMEM) { |
a3619190 | 689 | nd_pfn->npfns = PHYS_PFN((resource_size(res) - offset)); |
ac515c08 DW |
690 | if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns) |
691 | dev_info(&nd_pfn->dev, | |
692 | "number of pfns truncated from %lld to %ld\n", | |
693 | le64_to_cpu(nd_pfn->pfn_sb->npfns), | |
694 | nd_pfn->npfns); | |
695 | memcpy(altmap, &__altmap, sizeof(*altmap)); | |
11a35810 | 696 | altmap->free = PHYS_PFN(offset - reserve); |
ac515c08 | 697 | altmap->alloc = 0; |
514caf23 | 698 | pgmap->flags |= PGMAP_ALTMAP_VALID; |
ac515c08 | 699 | } else |
e8d51348 | 700 | return -ENXIO; |
ac515c08 | 701 | |
e8d51348 | 702 | return 0; |
ac515c08 DW |
703 | } |
704 | ||
705 | static int nd_pfn_init(struct nd_pfn *nd_pfn) | |
706 | { | |
707 | struct nd_namespace_common *ndns = nd_pfn->ndns; | |
ae86cbfe | 708 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
ac515c08 | 709 | resource_size_t start, size; |
ac515c08 | 710 | struct nd_region *nd_region; |
a3619190 | 711 | unsigned long npfns, align; |
ac515c08 | 712 | struct nd_pfn_sb *pfn_sb; |
ac515c08 | 713 | phys_addr_t offset; |
c5ed9268 | 714 | const char *sig; |
ac515c08 DW |
715 | u64 checksum; |
716 | int rc; | |
717 | ||
7e3e888d | 718 | pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL); |
ac515c08 DW |
719 | if (!pfn_sb) |
720 | return -ENOMEM; | |
721 | ||
722 | nd_pfn->pfn_sb = pfn_sb; | |
c5ed9268 DW |
723 | if (is_nd_dax(&nd_pfn->dev)) |
724 | sig = DAX_SIG; | |
725 | else | |
726 | sig = PFN_SIG; | |
7e3e888d | 727 | |
c5ed9268 | 728 | rc = nd_pfn_validate(nd_pfn, sig); |
ac515c08 DW |
729 | if (rc != -ENODEV) |
730 | return rc; | |
731 | ||
732 | /* no info block, do init */; | |
7e3e888d DW |
733 | memset(pfn_sb, 0, sizeof(*pfn_sb)); |
734 | ||
ac515c08 DW |
735 | nd_region = to_nd_region(nd_pfn->dev.parent); |
736 | if (nd_region->ro) { | |
737 | dev_info(&nd_pfn->dev, | |
738 | "%s is read-only, unable to init metadata\n", | |
739 | dev_name(&nd_region->dev)); | |
740 | return -ENXIO; | |
741 | } | |
742 | ||
ac515c08 DW |
743 | /* |
744 | * Note, we use 64 here for the standard size of struct page, | |
745 | * debugging options may cause it to be larger in which case the | |
746 | * implementation will limit the pfns advertised through | |
747 | * ->direct_access() to those that are included in the memmap. | |
748 | */ | |
a3619190 | 749 | start = nsio->res.start; |
ac515c08 | 750 | size = resource_size(&nsio->res); |
a3619190 DW |
751 | npfns = PHYS_PFN(size - SZ_8K); |
752 | align = max(nd_pfn->align, (1UL << SUBSECTION_SHIFT)); | |
594d6d96 | 753 | if (nd_pfn->mode == PFN_MODE_PMEM) { |
594d6d96 | 754 | /* |
0dd69643 OH |
755 | * The altmap should be padded out to the block size used |
756 | * when populating the vmemmap. This *should* be equal to | |
757 | * PMD_SIZE for most architectures. | |
e96f0bf2 AK |
758 | * |
759 | * Also make sure size of struct page is less than 64. We | |
760 | * want to make sure we use large enough size here so that | |
761 | * we don't have a dynamic reserve space depending on | |
762 | * struct page size. But we also want to make sure we notice | |
763 | * when we end up adding new elements to struct page. | |
594d6d96 | 764 | */ |
e96f0bf2 AK |
765 | BUILD_BUG_ON(sizeof(struct page) > MAX_STRUCT_PAGE_SIZE); |
766 | offset = ALIGN(start + SZ_8K + MAX_STRUCT_PAGE_SIZE * npfns, align) | |
767 | - start; | |
594d6d96 | 768 | } else if (nd_pfn->mode == PFN_MODE_RAM) |
a3619190 | 769 | offset = ALIGN(start + SZ_8K, align) - start; |
ac515c08 DW |
770 | else |
771 | return -ENXIO; | |
772 | ||
a3619190 | 773 | if (offset >= size) { |
ac515c08 DW |
774 | dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n", |
775 | dev_name(&ndns->dev)); | |
776 | return -ENXIO; | |
777 | } | |
778 | ||
a3619190 | 779 | npfns = PHYS_PFN(size - offset); |
ac515c08 DW |
780 | pfn_sb->mode = cpu_to_le32(nd_pfn->mode); |
781 | pfn_sb->dataoff = cpu_to_le64(offset); | |
782 | pfn_sb->npfns = cpu_to_le64(npfns); | |
c5ed9268 | 783 | memcpy(pfn_sb->signature, sig, PFN_SIG_LEN); |
ac515c08 DW |
784 | memcpy(pfn_sb->uuid, nd_pfn->uuid, 16); |
785 | memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16); | |
786 | pfn_sb->version_major = cpu_to_le16(1); | |
edbb52c2 | 787 | pfn_sb->version_minor = cpu_to_le16(4); |
45a0dac0 | 788 | pfn_sb->align = cpu_to_le32(nd_pfn->align); |
edbb52c2 AK |
789 | pfn_sb->page_struct_size = cpu_to_le16(MAX_STRUCT_PAGE_SIZE); |
790 | pfn_sb->page_size = cpu_to_le32(PAGE_SIZE); | |
ac515c08 DW |
791 | checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb); |
792 | pfn_sb->checksum = cpu_to_le64(checksum); | |
793 | ||
3ae3d67b | 794 | return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0); |
ac515c08 DW |
795 | } |
796 | ||
797 | /* | |
798 | * Determine the effective resource range and vmem_altmap from an nd_pfn | |
799 | * instance. | |
800 | */ | |
e8d51348 | 801 | int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap) |
ac515c08 DW |
802 | { |
803 | int rc; | |
804 | ||
805 | if (!nd_pfn->uuid || !nd_pfn->ndns) | |
e8d51348 | 806 | return -ENODEV; |
ac515c08 DW |
807 | |
808 | rc = nd_pfn_init(nd_pfn); | |
809 | if (rc) | |
e8d51348 | 810 | return rc; |
ac515c08 | 811 | |
e8d51348 CH |
812 | /* we need a valid pfn_sb before we can init a dev_pagemap */ |
813 | return __nvdimm_setup_pfn(nd_pfn, pgmap); | |
ac515c08 DW |
814 | } |
815 | EXPORT_SYMBOL_GPL(nvdimm_setup_pfn); |