]>
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 | ||
cd03412a DW |
57 | static 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 | ||
e1455744 DW |
76 | static ssize_t mode_show(struct device *dev, |
77 | struct device_attribute *attr, char *buf) | |
78 | { | |
cd03412a | 79 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
80 | |
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 | ||
91 | static ssize_t mode_store(struct device *dev, | |
92 | struct device_attribute *attr, const char *buf, size_t len) | |
93 | { | |
cd03412a | 94 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
95 | 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) { | |
d2c0f041 | 106 | nd_pfn->mode = PFN_MODE_PMEM; |
e1455744 DW |
107 | } 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 | } | |
123 | static DEVICE_ATTR_RW(mode); | |
124 | ||
315c5625 DW |
125 | static ssize_t align_show(struct device *dev, |
126 | struct device_attribute *attr, char *buf) | |
127 | { | |
cd03412a | 128 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
315c5625 DW |
129 | |
130 | return sprintf(buf, "%lx\n", nd_pfn->align); | |
131 | } | |
132 | ||
133 | static 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 | ||
153 | static ssize_t align_store(struct device *dev, | |
154 | struct device_attribute *attr, const char *buf, size_t len) | |
155 | { | |
cd03412a | 156 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
315c5625 DW |
157 | 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 | } | |
169 | static DEVICE_ATTR_RW(align); | |
170 | ||
e1455744 DW |
171 | static ssize_t uuid_show(struct device *dev, |
172 | struct device_attribute *attr, char *buf) | |
173 | { | |
cd03412a | 174 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
175 | |
176 | if (nd_pfn->uuid) | |
177 | return sprintf(buf, "%pUb\n", nd_pfn->uuid); | |
178 | return sprintf(buf, "\n"); | |
179 | } | |
180 | ||
181 | static ssize_t uuid_store(struct device *dev, | |
182 | struct device_attribute *attr, const char *buf, size_t len) | |
183 | { | |
cd03412a | 184 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
185 | 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 | } | |
195 | static DEVICE_ATTR_RW(uuid); | |
196 | ||
197 | static ssize_t namespace_show(struct device *dev, | |
198 | struct device_attribute *attr, char *buf) | |
199 | { | |
cd03412a | 200 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
201 | 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 | ||
210 | static ssize_t namespace_store(struct device *dev, | |
211 | struct device_attribute *attr, const char *buf, size_t len) | |
212 | { | |
cd03412a | 213 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
e1455744 DW |
214 | ssize_t rc; |
215 | ||
e1455744 | 216 | device_lock(dev); |
4ca8b57a | 217 | nvdimm_bus_lock(dev); |
e1455744 DW |
218 | 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"); | |
e1455744 | 221 | nvdimm_bus_unlock(dev); |
4ca8b57a | 222 | device_unlock(dev); |
e1455744 DW |
223 | |
224 | return rc; | |
225 | } | |
226 | static DEVICE_ATTR_RW(namespace); | |
227 | ||
f6ed58c7 DW |
228 | static ssize_t resource_show(struct device *dev, |
229 | struct device_attribute *attr, char *buf) | |
230 | { | |
cd03412a | 231 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
f6ed58c7 DW |
232 | 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 | } | |
252 | static DEVICE_ATTR_RO(resource); | |
253 | ||
254 | static ssize_t size_show(struct device *dev, | |
255 | struct device_attribute *attr, char *buf) | |
256 | { | |
cd03412a | 257 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
f6ed58c7 DW |
258 | 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 | } | |
280 | static DEVICE_ATTR_RO(size); | |
281 | ||
e1455744 DW |
282 | static struct attribute *nd_pfn_attributes[] = { |
283 | &dev_attr_mode.attr, | |
284 | &dev_attr_namespace.attr, | |
285 | &dev_attr_uuid.attr, | |
315c5625 | 286 | &dev_attr_align.attr, |
f6ed58c7 DW |
287 | &dev_attr_resource.attr, |
288 | &dev_attr_size.attr, | |
e1455744 DW |
289 | NULL, |
290 | }; | |
291 | ||
cd03412a | 292 | struct attribute_group nd_pfn_attribute_group = { |
e1455744 DW |
293 | .attrs = nd_pfn_attributes, |
294 | }; | |
295 | ||
296 | static 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 | ||
cd03412a | 303 | struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn, |
e1455744 DW |
304 | struct nd_namespace_common *ndns) |
305 | { | |
cd03412a | 306 | struct device *dev = &nd_pfn->dev; |
e1455744 | 307 | |
cd03412a DW |
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); | |
e1455744 | 319 | return NULL; |
cd03412a DW |
320 | } |
321 | return dev; | |
322 | } | |
323 | ||
324 | static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region) | |
325 | { | |
326 | struct nd_pfn *nd_pfn; | |
327 | struct device *dev; | |
e1455744 DW |
328 | |
329 | 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 | ||
e1455744 DW |
339 | dev = &nd_pfn->dev; |
340 | dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id); | |
e1455744 | 341 | dev->groups = nd_pfn_attribute_groups; |
cd03412a DW |
342 | dev->type = &nd_pfn_device_type; |
343 | dev->parent = &nd_region->dev; | |
344 | ||
345 | return nd_pfn; | |
e1455744 DW |
346 | } |
347 | ||
348 | struct device *nd_pfn_create(struct nd_region *nd_region) | |
349 | { | |
cd03412a DW |
350 | struct nd_pfn *nd_pfn; |
351 | struct device *dev; | |
352 | ||
353 | 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); | |
e1455744 | 358 | |
cd03412a | 359 | __nd_device_register(dev); |
e1455744 DW |
360 | return dev; |
361 | } | |
362 | ||
32ab0a3f | 363 | int nd_pfn_validate(struct nd_pfn *nd_pfn) |
e1455744 | 364 | { |
e1455744 | 365 | u64 checksum, offset; |
a34d5e8a DW |
366 | 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); | |
e1455744 DW |
370 | |
371 | if (!pfn_sb || !ndns) | |
372 | return -ENODEV; | |
373 | ||
374 | if (!is_nd_pmem(nd_pfn->dev.parent)) | |
375 | return -ENODEV; | |
376 | ||
e1455744 DW |
377 | 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 | ||
a34d5e8a DW |
389 | if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0) |
390 | return -ENODEV; | |
391 | ||
cfe30b87 DW |
392 | if (__le16_to_cpu(pfn_sb->version_minor) < 1) { |
393 | pfn_sb->start_pad = 0; | |
394 | pfn_sb->end_trunc = 0; | |
395 | } | |
396 | ||
e1455744 DW |
397 | switch (le32_to_cpu(pfn_sb->mode)) { |
398 | case PFN_MODE_RAM: | |
e1455744 | 399 | case PFN_MODE_PMEM: |
45eb570a | 400 | break; |
e1455744 DW |
401 | default: |
402 | return -ENXIO; | |
403 | } | |
404 | ||
405 | if (!nd_pfn->uuid) { | |
406 | /* from probe we allocate */ | |
407 | nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL); | |
408 | if (!nd_pfn->uuid) | |
409 | return -ENOMEM; | |
410 | } else { | |
411 | /* from init we validate */ | |
412 | if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0) | |
e5670563 | 413 | return -ENODEV; |
e1455744 DW |
414 | } |
415 | ||
315c5625 DW |
416 | if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) { |
417 | dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n", | |
418 | nd_pfn->align, nvdimm_namespace_capacity(ndns)); | |
419 | return -EINVAL; | |
420 | } | |
421 | ||
e1455744 DW |
422 | /* |
423 | * These warnings are verbose because they can only trigger in | |
424 | * the case where the physical address alignment of the | |
425 | * namespace has changed since the pfn superblock was | |
426 | * established. | |
427 | */ | |
428 | offset = le64_to_cpu(pfn_sb->dataoff); | |
429 | nsio = to_nd_namespace_io(&ndns->dev); | |
9f1e8cee | 430 | if (offset >= resource_size(&nsio->res)) { |
e1455744 DW |
431 | dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n", |
432 | dev_name(&ndns->dev)); | |
433 | return -EBUSY; | |
434 | } | |
435 | ||
315c5625 DW |
436 | nd_pfn->align = 1UL << ilog2(offset); |
437 | if (!is_power_of_2(offset) || offset < PAGE_SIZE) { | |
438 | dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n", | |
439 | offset); | |
440 | return -ENXIO; | |
441 | } | |
442 | ||
e1455744 DW |
443 | return 0; |
444 | } | |
32ab0a3f | 445 | EXPORT_SYMBOL(nd_pfn_validate); |
e1455744 | 446 | |
200c79da | 447 | int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns) |
e1455744 DW |
448 | { |
449 | int rc; | |
e1455744 | 450 | struct nd_pfn *nd_pfn; |
bd032943 | 451 | struct device *pfn_dev; |
e1455744 DW |
452 | struct nd_pfn_sb *pfn_sb; |
453 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); | |
454 | ||
455 | if (ndns->force_raw) | |
456 | return -ENODEV; | |
457 | ||
458 | nvdimm_bus_lock(&ndns->dev); | |
cd03412a DW |
459 | nd_pfn = nd_pfn_alloc(nd_region); |
460 | pfn_dev = nd_pfn_devinit(nd_pfn, ndns); | |
e1455744 | 461 | nvdimm_bus_unlock(&ndns->dev); |
bd032943 | 462 | if (!pfn_dev) |
e1455744 | 463 | return -ENOMEM; |
bd032943 DW |
464 | pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL); |
465 | nd_pfn = to_nd_pfn(pfn_dev); | |
e1455744 DW |
466 | nd_pfn->pfn_sb = pfn_sb; |
467 | rc = nd_pfn_validate(nd_pfn); | |
bd032943 DW |
468 | dev_dbg(dev, "%s: pfn: %s\n", __func__, |
469 | rc == 0 ? dev_name(pfn_dev) : "<none>"); | |
e1455744 | 470 | if (rc < 0) { |
bd032943 DW |
471 | __nd_detach_ndns(pfn_dev, &nd_pfn->ndns); |
472 | put_device(pfn_dev); | |
e1455744 | 473 | } else |
bd032943 | 474 | __nd_device_register(pfn_dev); |
e1455744 DW |
475 | |
476 | return rc; | |
477 | } | |
478 | EXPORT_SYMBOL(nd_pfn_probe); | |
ac515c08 DW |
479 | |
480 | /* | |
481 | * We hotplug memory at section granularity, pad the reserved area from | |
482 | * the previous section base to the namespace base address. | |
483 | */ | |
484 | static unsigned long init_altmap_base(resource_size_t base) | |
485 | { | |
486 | unsigned long base_pfn = PHYS_PFN(base); | |
487 | ||
488 | return PFN_SECTION_ALIGN_DOWN(base_pfn); | |
489 | } | |
490 | ||
491 | static unsigned long init_altmap_reserve(resource_size_t base) | |
492 | { | |
493 | unsigned long reserve = PHYS_PFN(SZ_8K); | |
494 | unsigned long base_pfn = PHYS_PFN(base); | |
495 | ||
496 | reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn); | |
497 | return reserve; | |
498 | } | |
499 | ||
500 | static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn, | |
501 | struct resource *res, struct vmem_altmap *altmap) | |
502 | { | |
503 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; | |
504 | u64 offset = le64_to_cpu(pfn_sb->dataoff); | |
505 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); | |
506 | u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); | |
507 | struct nd_namespace_common *ndns = nd_pfn->ndns; | |
508 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); | |
509 | resource_size_t base = nsio->res.start + start_pad; | |
510 | struct vmem_altmap __altmap = { | |
511 | .base_pfn = init_altmap_base(base), | |
512 | .reserve = init_altmap_reserve(base), | |
513 | }; | |
514 | ||
515 | memcpy(res, &nsio->res, sizeof(*res)); | |
516 | res->start += start_pad; | |
517 | res->end -= end_trunc; | |
518 | ||
519 | nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode); | |
520 | if (nd_pfn->mode == PFN_MODE_RAM) { | |
521 | if (offset < SZ_8K) | |
522 | return ERR_PTR(-EINVAL); | |
523 | nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns); | |
524 | altmap = NULL; | |
525 | } else if (nd_pfn->mode == PFN_MODE_PMEM) { | |
526 | nd_pfn->npfns = (resource_size(res) - offset) / PAGE_SIZE; | |
527 | if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns) | |
528 | dev_info(&nd_pfn->dev, | |
529 | "number of pfns truncated from %lld to %ld\n", | |
530 | le64_to_cpu(nd_pfn->pfn_sb->npfns), | |
531 | nd_pfn->npfns); | |
532 | memcpy(altmap, &__altmap, sizeof(*altmap)); | |
533 | altmap->free = PHYS_PFN(offset - SZ_8K); | |
534 | altmap->alloc = 0; | |
535 | } else | |
536 | return ERR_PTR(-ENXIO); | |
537 | ||
538 | return altmap; | |
539 | } | |
540 | ||
541 | static int nd_pfn_init(struct nd_pfn *nd_pfn) | |
542 | { | |
543 | struct nd_namespace_common *ndns = nd_pfn->ndns; | |
544 | u32 start_pad = 0, end_trunc = 0; | |
545 | resource_size_t start, size; | |
546 | struct nd_namespace_io *nsio; | |
547 | struct nd_region *nd_region; | |
548 | struct nd_pfn_sb *pfn_sb; | |
549 | unsigned long npfns; | |
550 | phys_addr_t offset; | |
551 | u64 checksum; | |
552 | int rc; | |
553 | ||
554 | pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL); | |
555 | if (!pfn_sb) | |
556 | return -ENOMEM; | |
557 | ||
558 | nd_pfn->pfn_sb = pfn_sb; | |
559 | rc = nd_pfn_validate(nd_pfn); | |
560 | if (rc != -ENODEV) | |
561 | return rc; | |
562 | ||
563 | /* no info block, do init */; | |
564 | nd_region = to_nd_region(nd_pfn->dev.parent); | |
565 | if (nd_region->ro) { | |
566 | dev_info(&nd_pfn->dev, | |
567 | "%s is read-only, unable to init metadata\n", | |
568 | dev_name(&nd_region->dev)); | |
569 | return -ENXIO; | |
570 | } | |
571 | ||
572 | memset(pfn_sb, 0, sizeof(*pfn_sb)); | |
573 | ||
574 | /* | |
575 | * Check if pmem collides with 'System RAM' when section aligned and | |
576 | * trim it accordingly | |
577 | */ | |
578 | nsio = to_nd_namespace_io(&ndns->dev); | |
579 | start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start); | |
580 | size = resource_size(&nsio->res); | |
581 | if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, | |
582 | IORES_DESC_NONE) == REGION_MIXED) { | |
583 | start = nsio->res.start; | |
584 | start_pad = PHYS_SECTION_ALIGN_UP(start) - start; | |
585 | } | |
586 | ||
587 | start = nsio->res.start; | |
588 | size = PHYS_SECTION_ALIGN_UP(start + size) - start; | |
589 | if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, | |
590 | IORES_DESC_NONE) == REGION_MIXED) { | |
591 | size = resource_size(&nsio->res); | |
592 | end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size); | |
593 | } | |
594 | ||
595 | if (start_pad + end_trunc) | |
596 | dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n", | |
597 | dev_name(&ndns->dev), start_pad + end_trunc); | |
598 | ||
599 | /* | |
600 | * Note, we use 64 here for the standard size of struct page, | |
601 | * debugging options may cause it to be larger in which case the | |
602 | * implementation will limit the pfns advertised through | |
603 | * ->direct_access() to those that are included in the memmap. | |
604 | */ | |
605 | start += start_pad; | |
606 | size = resource_size(&nsio->res); | |
607 | npfns = (size - start_pad - end_trunc - SZ_8K) / SZ_4K; | |
608 | if (nd_pfn->mode == PFN_MODE_PMEM) | |
609 | offset = ALIGN(start + SZ_8K + 64 * npfns, nd_pfn->align) | |
610 | - start; | |
611 | else if (nd_pfn->mode == PFN_MODE_RAM) | |
612 | offset = ALIGN(start + SZ_8K, nd_pfn->align) - start; | |
613 | else | |
614 | return -ENXIO; | |
615 | ||
616 | if (offset + start_pad + end_trunc >= size) { | |
617 | dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n", | |
618 | dev_name(&ndns->dev)); | |
619 | return -ENXIO; | |
620 | } | |
621 | ||
622 | npfns = (size - offset - start_pad - end_trunc) / SZ_4K; | |
623 | pfn_sb->mode = cpu_to_le32(nd_pfn->mode); | |
624 | pfn_sb->dataoff = cpu_to_le64(offset); | |
625 | pfn_sb->npfns = cpu_to_le64(npfns); | |
626 | memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN); | |
627 | memcpy(pfn_sb->uuid, nd_pfn->uuid, 16); | |
628 | memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16); | |
629 | pfn_sb->version_major = cpu_to_le16(1); | |
630 | pfn_sb->version_minor = cpu_to_le16(1); | |
631 | pfn_sb->start_pad = cpu_to_le32(start_pad); | |
632 | pfn_sb->end_trunc = cpu_to_le32(end_trunc); | |
633 | checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb); | |
634 | pfn_sb->checksum = cpu_to_le64(checksum); | |
635 | ||
636 | return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)); | |
637 | } | |
638 | ||
639 | /* | |
640 | * Determine the effective resource range and vmem_altmap from an nd_pfn | |
641 | * instance. | |
642 | */ | |
643 | struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn, | |
644 | struct resource *res, struct vmem_altmap *altmap) | |
645 | { | |
646 | int rc; | |
647 | ||
648 | if (!nd_pfn->uuid || !nd_pfn->ndns) | |
649 | return ERR_PTR(-ENODEV); | |
650 | ||
651 | rc = nd_pfn_init(nd_pfn); | |
652 | if (rc) | |
653 | return ERR_PTR(rc); | |
654 | ||
655 | /* we need a valid pfn_sb before we can init a vmem_altmap */ | |
656 | return __nvdimm_setup_pfn(nd_pfn, res, altmap); | |
657 | } | |
658 | EXPORT_SYMBOL_GPL(nvdimm_setup_pfn); |