]>
Commit | Line | Data |
---|---|---|
e1455744 | 1 | /* |
cd03412a | 2 | * Copyright(c) 2013-2016 Intel Corporation. All rights reserved. |
e1455744 DW |
3 | * |
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 | */ | |
ac515c08 | 13 | #include <linux/memremap.h> |
e1455744 DW |
14 | #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 | ||
25 | static 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 | ||
37 | static struct device_type nd_pfn_device_type = { | |
38 | .name = "nd_pfn", | |
39 | .release = nd_pfn_release, | |
40 | }; | |
41 | ||
42 | bool is_nd_pfn(struct device *dev) | |
43 | { | |
44 | return dev ? dev->type == &nd_pfn_device_type : false; | |
45 | } | |
46 | EXPORT_SYMBOL(is_nd_pfn); | |
47 | ||
48 | struct 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 | } | |
55 | EXPORT_SYMBOL(to_nd_pfn); | |
56 | ||
57 | static ssize_t mode_show(struct device *dev, | |
58 | struct device_attribute *attr, char *buf) | |
59 | { | |
cd03412a | 60 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
61 | |
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 | ||
72 | static ssize_t mode_store(struct device *dev, | |
73 | struct device_attribute *attr, const char *buf, size_t len) | |
74 | { | |
cd03412a | 75 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
76 | 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) { | |
d2c0f041 | 87 | nd_pfn->mode = PFN_MODE_PMEM; |
e1455744 DW |
88 | } 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 | } | |
97 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
98 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
99 | nvdimm_bus_unlock(dev); | |
100 | device_unlock(dev); | |
101 | ||
102 | return rc ? rc : len; | |
103 | } | |
104 | static DEVICE_ATTR_RW(mode); | |
105 | ||
315c5625 DW |
106 | static ssize_t align_show(struct device *dev, |
107 | struct device_attribute *attr, char *buf) | |
108 | { | |
cd03412a | 109 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
315c5625 | 110 | |
af7d9f0c | 111 | return sprintf(buf, "%ld\n", nd_pfn->align); |
315c5625 DW |
112 | } |
113 | ||
1fdadbeb OH |
114 | static 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 | ||
315c5625 DW |
137 | static ssize_t align_store(struct device *dev, |
138 | struct device_attribute *attr, const char *buf, size_t len) | |
139 | { | |
cd03412a | 140 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
315c5625 DW |
141 | ssize_t rc; |
142 | ||
143 | device_lock(dev); | |
144 | nvdimm_bus_lock(dev); | |
f13d2b61 DW |
145 | rc = nd_size_select_store(dev, buf, &nd_pfn->align, |
146 | nd_pfn_supported_alignments()); | |
315c5625 DW |
147 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, |
148 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
149 | nvdimm_bus_unlock(dev); | |
150 | device_unlock(dev); | |
151 | ||
152 | return rc ? rc : len; | |
153 | } | |
154 | static DEVICE_ATTR_RW(align); | |
155 | ||
e1455744 DW |
156 | static ssize_t uuid_show(struct device *dev, |
157 | struct device_attribute *attr, char *buf) | |
158 | { | |
cd03412a | 159 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
160 | |
161 | if (nd_pfn->uuid) | |
162 | return sprintf(buf, "%pUb\n", nd_pfn->uuid); | |
163 | return sprintf(buf, "\n"); | |
164 | } | |
165 | ||
166 | static ssize_t uuid_store(struct device *dev, | |
167 | struct device_attribute *attr, const char *buf, size_t len) | |
168 | { | |
cd03412a | 169 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
170 | ssize_t rc; |
171 | ||
172 | device_lock(dev); | |
173 | rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len); | |
174 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
175 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
176 | device_unlock(dev); | |
177 | ||
178 | return rc ? rc : len; | |
179 | } | |
180 | static DEVICE_ATTR_RW(uuid); | |
181 | ||
182 | static ssize_t namespace_show(struct device *dev, | |
183 | struct device_attribute *attr, char *buf) | |
184 | { | |
cd03412a | 185 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
186 | 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 | ||
195 | static ssize_t namespace_store(struct device *dev, | |
196 | struct device_attribute *attr, const char *buf, size_t len) | |
197 | { | |
cd03412a | 198 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
199 | ssize_t rc; |
200 | ||
e1455744 | 201 | device_lock(dev); |
4ca8b57a | 202 | nvdimm_bus_lock(dev); |
e1455744 DW |
203 | rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len); |
204 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, | |
205 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
e1455744 | 206 | nvdimm_bus_unlock(dev); |
4ca8b57a | 207 | device_unlock(dev); |
e1455744 DW |
208 | |
209 | return rc; | |
210 | } | |
211 | static DEVICE_ATTR_RW(namespace); | |
212 | ||
f6ed58c7 DW |
213 | static ssize_t resource_show(struct device *dev, |
214 | struct device_attribute *attr, char *buf) | |
215 | { | |
cd03412a | 216 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
f6ed58c7 DW |
217 | 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 | } | |
237 | static DEVICE_ATTR_RO(resource); | |
238 | ||
239 | static ssize_t size_show(struct device *dev, | |
240 | struct device_attribute *attr, char *buf) | |
241 | { | |
cd03412a | 242 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
f6ed58c7 DW |
243 | 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 | } | |
265 | static DEVICE_ATTR_RO(size); | |
266 | ||
1fdadbeb OH |
267 | static 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 | } | |
272 | static DEVICE_ATTR_RO(supported_alignments); | |
273 | ||
e1455744 DW |
274 | static struct attribute *nd_pfn_attributes[] = { |
275 | &dev_attr_mode.attr, | |
276 | &dev_attr_namespace.attr, | |
277 | &dev_attr_uuid.attr, | |
315c5625 | 278 | &dev_attr_align.attr, |
f6ed58c7 DW |
279 | &dev_attr_resource.attr, |
280 | &dev_attr_size.attr, | |
1fdadbeb | 281 | &dev_attr_supported_alignments.attr, |
e1455744 DW |
282 | NULL, |
283 | }; | |
284 | ||
26417ae4 DW |
285 | static 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 | ||
cd03412a | 292 | struct attribute_group nd_pfn_attribute_group = { |
e1455744 | 293 | .attrs = nd_pfn_attributes, |
26417ae4 | 294 | .is_visible = pfn_visible, |
e1455744 DW |
295 | }; |
296 | ||
297 | static 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 | ||
cd03412a | 304 | struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn, |
e1455744 DW |
305 | struct nd_namespace_common *ndns) |
306 | { | |
cd03412a | 307 | struct device *dev = &nd_pfn->dev; |
e1455744 | 308 | |
cd03412a | 309 | if (!nd_pfn) |
e1455744 DW |
310 | return NULL; |
311 | ||
cd03412a | 312 | nd_pfn->mode = PFN_MODE_NONE; |
0dd69643 | 313 | nd_pfn->align = PFN_DEFAULT_ALIGNMENT; |
cd03412a DW |
314 | dev = &nd_pfn->dev; |
315 | device_initialize(&nd_pfn->dev); | |
316 | if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) { | |
317 | dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n", | |
318 | __func__, dev_name(ndns->claim)); | |
319 | put_device(dev); | |
e1455744 | 320 | return NULL; |
cd03412a DW |
321 | } |
322 | return dev; | |
323 | } | |
324 | ||
325 | static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region) | |
326 | { | |
327 | struct nd_pfn *nd_pfn; | |
328 | struct device *dev; | |
e1455744 DW |
329 | |
330 | 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 | ||
e1455744 DW |
340 | dev = &nd_pfn->dev; |
341 | dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id); | |
e1455744 | 342 | dev->groups = nd_pfn_attribute_groups; |
cd03412a DW |
343 | dev->type = &nd_pfn_device_type; |
344 | dev->parent = &nd_region->dev; | |
345 | ||
346 | return nd_pfn; | |
e1455744 DW |
347 | } |
348 | ||
349 | struct device *nd_pfn_create(struct nd_region *nd_region) | |
350 | { | |
cd03412a DW |
351 | struct nd_pfn *nd_pfn; |
352 | struct device *dev; | |
353 | ||
c9e582aa | 354 | if (!is_memory(&nd_region->dev)) |
cd03412a | 355 | return NULL; |
e1455744 | 356 | |
cd03412a DW |
357 | nd_pfn = nd_pfn_alloc(nd_region); |
358 | dev = nd_pfn_devinit(nd_pfn, NULL); | |
e1455744 | 359 | |
cd03412a | 360 | __nd_device_register(dev); |
e1455744 DW |
361 | return dev; |
362 | } | |
363 | ||
c5ed9268 | 364 | int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig) |
e1455744 | 365 | { |
e1455744 | 366 | u64 checksum, offset; |
1ee6667c | 367 | enum nd_pfn_mode mode; |
a34d5e8a | 368 | struct nd_namespace_io *nsio; |
19deaa21 | 369 | unsigned long align, start_pad; |
a34d5e8a DW |
370 | 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); | |
e1455744 DW |
373 | |
374 | if (!pfn_sb || !ndns) | |
375 | return -ENODEV; | |
376 | ||
c9e582aa | 377 | if (!is_memory(nd_pfn->dev.parent)) |
e1455744 DW |
378 | return -ENODEV; |
379 | ||
3ae3d67b | 380 | if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0)) |
e1455744 DW |
381 | return -ENXIO; |
382 | ||
c5ed9268 | 383 | if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0) |
e1455744 DW |
384 | 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 | ||
a34d5e8a DW |
392 | if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0) |
393 | return -ENODEV; | |
394 | ||
cfe30b87 DW |
395 | if (__le16_to_cpu(pfn_sb->version_minor) < 1) { |
396 | pfn_sb->start_pad = 0; | |
397 | pfn_sb->end_trunc = 0; | |
398 | } | |
399 | ||
45a0dac0 DW |
400 | if (__le16_to_cpu(pfn_sb->version_minor) < 2) |
401 | pfn_sb->align = 0; | |
402 | ||
e1455744 DW |
403 | switch (le32_to_cpu(pfn_sb->mode)) { |
404 | case PFN_MODE_RAM: | |
e1455744 | 405 | case PFN_MODE_PMEM: |
45eb570a | 406 | break; |
e1455744 DW |
407 | default: |
408 | return -ENXIO; | |
409 | } | |
410 | ||
1ee6667c DW |
411 | align = le32_to_cpu(pfn_sb->align); |
412 | offset = le64_to_cpu(pfn_sb->dataoff); | |
19deaa21 | 413 | start_pad = le32_to_cpu(pfn_sb->start_pad); |
1ee6667c DW |
414 | if (align == 0) |
415 | align = 1UL << ilog2(offset); | |
416 | mode = le32_to_cpu(pfn_sb->mode); | |
417 | ||
e1455744 | 418 | if (!nd_pfn->uuid) { |
1ee6667c DW |
419 | /* |
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 | */ | |
e1455744 DW |
424 | nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL); |
425 | if (!nd_pfn->uuid) | |
426 | return -ENOMEM; | |
1ee6667c DW |
427 | nd_pfn->align = align; |
428 | nd_pfn->mode = mode; | |
e1455744 | 429 | } else { |
1ee6667c DW |
430 | /* |
431 | * When probing a pfn / dax instance we validate the | |
432 | * live settings against the pfn_sb | |
433 | */ | |
e1455744 | 434 | if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0) |
e5670563 | 435 | return -ENODEV; |
1ee6667c DW |
436 | |
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 | } | |
e1455744 DW |
451 | } |
452 | ||
1ee6667c | 453 | if (align > nvdimm_namespace_capacity(ndns)) { |
315c5625 | 454 | dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n", |
1ee6667c | 455 | align, nvdimm_namespace_capacity(ndns)); |
315c5625 DW |
456 | return -EINVAL; |
457 | } | |
458 | ||
e1455744 DW |
459 | /* |
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 | */ | |
e1455744 | 465 | nsio = to_nd_namespace_io(&ndns->dev); |
9f1e8cee | 466 | if (offset >= resource_size(&nsio->res)) { |
e1455744 DW |
467 | dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n", |
468 | dev_name(&ndns->dev)); | |
469 | return -EBUSY; | |
470 | } | |
471 | ||
19deaa21 | 472 | if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align)) |
5e24c9fd | 473 | || !IS_ALIGNED(offset, PAGE_SIZE)) { |
1ee6667c DW |
474 | dev_err(&nd_pfn->dev, |
475 | "bad offset: %#llx dax disabled align: %#lx\n", | |
476 | offset, align); | |
315c5625 DW |
477 | return -ENXIO; |
478 | } | |
479 | ||
e1455744 DW |
480 | return 0; |
481 | } | |
32ab0a3f | 482 | EXPORT_SYMBOL(nd_pfn_validate); |
e1455744 | 483 | |
200c79da | 484 | int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns) |
e1455744 DW |
485 | { |
486 | int rc; | |
e1455744 | 487 | struct nd_pfn *nd_pfn; |
bd032943 | 488 | struct device *pfn_dev; |
e1455744 DW |
489 | 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 | ||
b3fde74e DW |
495 | switch (ndns->claim_class) { |
496 | case NVDIMM_CCLASS_NONE: | |
497 | case NVDIMM_CCLASS_PFN: | |
498 | break; | |
499 | default: | |
500 | return -ENODEV; | |
501 | } | |
502 | ||
e1455744 | 503 | nvdimm_bus_lock(&ndns->dev); |
cd03412a DW |
504 | nd_pfn = nd_pfn_alloc(nd_region); |
505 | pfn_dev = nd_pfn_devinit(nd_pfn, ndns); | |
e1455744 | 506 | nvdimm_bus_unlock(&ndns->dev); |
bd032943 | 507 | if (!pfn_dev) |
e1455744 | 508 | return -ENOMEM; |
bd032943 DW |
509 | pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL); |
510 | nd_pfn = to_nd_pfn(pfn_dev); | |
e1455744 | 511 | nd_pfn->pfn_sb = pfn_sb; |
c5ed9268 | 512 | rc = nd_pfn_validate(nd_pfn, PFN_SIG); |
bd032943 DW |
513 | dev_dbg(dev, "%s: pfn: %s\n", __func__, |
514 | rc == 0 ? dev_name(pfn_dev) : "<none>"); | |
e1455744 | 515 | if (rc < 0) { |
452bae0a | 516 | nd_detach_ndns(pfn_dev, &nd_pfn->ndns); |
bd032943 | 517 | put_device(pfn_dev); |
e1455744 | 518 | } else |
bd032943 | 519 | __nd_device_register(pfn_dev); |
e1455744 DW |
520 | |
521 | return rc; | |
522 | } | |
523 | EXPORT_SYMBOL(nd_pfn_probe); | |
ac515c08 DW |
524 | |
525 | /* | |
526 | * We hotplug memory at section granularity, pad the reserved area from | |
527 | * the previous section base to the namespace base address. | |
528 | */ | |
529 | static unsigned long init_altmap_base(resource_size_t base) | |
530 | { | |
531 | unsigned long base_pfn = PHYS_PFN(base); | |
532 | ||
533 | return PFN_SECTION_ALIGN_DOWN(base_pfn); | |
534 | } | |
535 | ||
536 | static unsigned long init_altmap_reserve(resource_size_t base) | |
537 | { | |
538 | unsigned long reserve = PHYS_PFN(SZ_8K); | |
539 | unsigned long base_pfn = PHYS_PFN(base); | |
540 | ||
541 | reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn); | |
542 | return reserve; | |
543 | } | |
544 | ||
545 | static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn, | |
546 | struct resource *res, struct vmem_altmap *altmap) | |
547 | { | |
548 | 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 | ||
ac515c08 DW |
564 | if (nd_pfn->mode == PFN_MODE_RAM) { |
565 | if (offset < SZ_8K) | |
566 | return ERR_PTR(-EINVAL); | |
567 | nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns); | |
568 | altmap = NULL; | |
569 | } else if (nd_pfn->mode == PFN_MODE_PMEM) { | |
d5483fed DW |
570 | nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res) |
571 | - offset) / PAGE_SIZE); | |
ac515c08 DW |
572 | 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; | |
580 | } else | |
581 | return ERR_PTR(-ENXIO); | |
582 | ||
583 | return altmap; | |
584 | } | |
585 | ||
41fce90f DW |
586 | static u64 phys_pmem_align_down(struct nd_pfn *nd_pfn, u64 phys) |
587 | { | |
588 | return min_t(u64, PHYS_SECTION_ALIGN_DOWN(phys), | |
589 | ALIGN_DOWN(phys, nd_pfn->align)); | |
590 | } | |
591 | ||
ac515c08 DW |
592 | static int nd_pfn_init(struct nd_pfn *nd_pfn) |
593 | { | |
52ac23b2 | 594 | u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0; |
ac515c08 DW |
595 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
596 | u32 start_pad = 0, end_trunc = 0; | |
597 | resource_size_t start, size; | |
598 | struct nd_namespace_io *nsio; | |
599 | struct nd_region *nd_region; | |
600 | struct nd_pfn_sb *pfn_sb; | |
601 | unsigned long npfns; | |
602 | phys_addr_t offset; | |
c5ed9268 | 603 | const char *sig; |
ac515c08 DW |
604 | u64 checksum; |
605 | int rc; | |
606 | ||
607 | pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL); | |
608 | if (!pfn_sb) | |
609 | return -ENOMEM; | |
610 | ||
611 | nd_pfn->pfn_sb = pfn_sb; | |
c5ed9268 DW |
612 | if (is_nd_dax(&nd_pfn->dev)) |
613 | sig = DAX_SIG; | |
614 | else | |
615 | sig = PFN_SIG; | |
616 | rc = nd_pfn_validate(nd_pfn, sig); | |
ac515c08 DW |
617 | if (rc != -ENODEV) |
618 | return rc; | |
619 | ||
620 | /* no info block, do init */; | |
621 | nd_region = to_nd_region(nd_pfn->dev.parent); | |
622 | if (nd_region->ro) { | |
623 | dev_info(&nd_pfn->dev, | |
624 | "%s is read-only, unable to init metadata\n", | |
625 | dev_name(&nd_region->dev)); | |
626 | return -ENXIO; | |
627 | } | |
628 | ||
629 | memset(pfn_sb, 0, sizeof(*pfn_sb)); | |
630 | ||
631 | /* | |
632 | * Check if pmem collides with 'System RAM' when section aligned and | |
633 | * trim it accordingly | |
634 | */ | |
635 | nsio = to_nd_namespace_io(&ndns->dev); | |
636 | start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start); | |
637 | size = resource_size(&nsio->res); | |
638 | if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, | |
639 | IORES_DESC_NONE) == REGION_MIXED) { | |
640 | start = nsio->res.start; | |
641 | start_pad = PHYS_SECTION_ALIGN_UP(start) - start; | |
642 | } | |
643 | ||
644 | start = nsio->res.start; | |
645 | size = PHYS_SECTION_ALIGN_UP(start + size) - start; | |
646 | if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, | |
41fce90f DW |
647 | IORES_DESC_NONE) == REGION_MIXED |
648 | || !IS_ALIGNED(start + resource_size(&nsio->res), | |
649 | nd_pfn->align)) { | |
ac515c08 | 650 | size = resource_size(&nsio->res); |
41fce90f DW |
651 | end_trunc = start + size - phys_pmem_align_down(nd_pfn, |
652 | start + size); | |
ac515c08 DW |
653 | } |
654 | ||
655 | if (start_pad + end_trunc) | |
41fce90f | 656 | dev_info(&nd_pfn->dev, "%s alignment collision, truncate %d bytes\n", |
ac515c08 DW |
657 | dev_name(&ndns->dev), start_pad + end_trunc); |
658 | ||
659 | /* | |
660 | * Note, we use 64 here for the standard size of struct page, | |
661 | * debugging options may cause it to be larger in which case the | |
662 | * implementation will limit the pfns advertised through | |
663 | * ->direct_access() to those that are included in the memmap. | |
664 | */ | |
665 | start += start_pad; | |
666 | size = resource_size(&nsio->res); | |
d5483fed DW |
667 | npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K) |
668 | / PAGE_SIZE); | |
594d6d96 | 669 | if (nd_pfn->mode == PFN_MODE_PMEM) { |
594d6d96 | 670 | /* |
0dd69643 OH |
671 | * The altmap should be padded out to the block size used |
672 | * when populating the vmemmap. This *should* be equal to | |
673 | * PMD_SIZE for most architectures. | |
594d6d96 | 674 | */ |
bfb34527 | 675 | offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve, |
0dd69643 | 676 | max(nd_pfn->align, PMD_SIZE)) - start; |
594d6d96 | 677 | } else if (nd_pfn->mode == PFN_MODE_RAM) |
52ac23b2 DW |
678 | offset = ALIGN(start + SZ_8K + dax_label_reserve, |
679 | nd_pfn->align) - start; | |
ac515c08 DW |
680 | else |
681 | return -ENXIO; | |
682 | ||
683 | if (offset + start_pad + end_trunc >= size) { | |
684 | dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n", | |
685 | dev_name(&ndns->dev)); | |
686 | return -ENXIO; | |
687 | } | |
688 | ||
689 | npfns = (size - offset - start_pad - end_trunc) / SZ_4K; | |
690 | pfn_sb->mode = cpu_to_le32(nd_pfn->mode); | |
691 | pfn_sb->dataoff = cpu_to_le64(offset); | |
692 | pfn_sb->npfns = cpu_to_le64(npfns); | |
c5ed9268 | 693 | memcpy(pfn_sb->signature, sig, PFN_SIG_LEN); |
ac515c08 DW |
694 | memcpy(pfn_sb->uuid, nd_pfn->uuid, 16); |
695 | memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16); | |
696 | pfn_sb->version_major = cpu_to_le16(1); | |
45a0dac0 | 697 | pfn_sb->version_minor = cpu_to_le16(2); |
ac515c08 DW |
698 | pfn_sb->start_pad = cpu_to_le32(start_pad); |
699 | pfn_sb->end_trunc = cpu_to_le32(end_trunc); | |
45a0dac0 | 700 | pfn_sb->align = cpu_to_le32(nd_pfn->align); |
ac515c08 DW |
701 | checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb); |
702 | pfn_sb->checksum = cpu_to_le64(checksum); | |
703 | ||
3ae3d67b | 704 | return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0); |
ac515c08 DW |
705 | } |
706 | ||
707 | /* | |
708 | * Determine the effective resource range and vmem_altmap from an nd_pfn | |
709 | * instance. | |
710 | */ | |
711 | struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn, | |
712 | struct resource *res, struct vmem_altmap *altmap) | |
713 | { | |
714 | int rc; | |
715 | ||
716 | if (!nd_pfn->uuid || !nd_pfn->ndns) | |
717 | return ERR_PTR(-ENODEV); | |
718 | ||
719 | rc = nd_pfn_init(nd_pfn); | |
720 | if (rc) | |
721 | return ERR_PTR(rc); | |
722 | ||
723 | /* we need a valid pfn_sb before we can init a vmem_altmap */ | |
724 | return __nvdimm_setup_pfn(nd_pfn, res, altmap); | |
725 | } | |
726 | EXPORT_SYMBOL_GPL(nvdimm_setup_pfn); |