1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
3 #include <linux/memremap.h>
4 #include <linux/module.h>
5 #include "../nvdimm/pfn.h"
6 #include "../nvdimm/nd.h"
7 #include "bus.h"
8
__dax_pmem_probe(struct device * dev)9 static struct dev_dax *__dax_pmem_probe(struct device *dev)
10 {
11 struct range range;
12 int rc, id, region_id;
13 resource_size_t offset;
14 struct nd_pfn_sb *pfn_sb;
15 struct dev_dax_data data;
16 struct nd_namespace_io *nsio;
17 struct dax_region *dax_region;
18 struct dev_pagemap pgmap = { };
19 struct nd_namespace_common *ndns;
20 struct nd_dax *nd_dax = to_nd_dax(dev);
21 struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
22 struct nd_region *nd_region = to_nd_region(dev->parent);
23
24 ndns = nvdimm_namespace_common_probe(dev);
25 if (IS_ERR(ndns))
26 return ERR_CAST(ndns);
27
28 /* parse the 'pfn' info block via ->rw_bytes */
29 rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
30 if (rc)
31 return ERR_PTR(rc);
32 rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
33 if (rc)
34 return ERR_PTR(rc);
35 devm_namespace_disable(dev, ndns);
36
37 /* reserve the metadata area, device-dax will reserve the data */
38 pfn_sb = nd_pfn->pfn_sb;
39 offset = le64_to_cpu(pfn_sb->dataoff);
40 nsio = to_nd_namespace_io(&ndns->dev);
41 if (!devm_request_mem_region(dev, nsio->res.start, offset,
42 dev_name(&ndns->dev))) {
43 dev_warn(dev, "could not reserve metadata\n");
44 return ERR_PTR(-EBUSY);
45 }
46
47 rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", ®ion_id, &id);
48 if (rc != 2)
49 return ERR_PTR(-EINVAL);
50
51 /* adjust the dax_region range to the start of data */
52 range = pgmap.range;
53 range.start += offset;
54 dax_region = alloc_dax_region(dev, region_id, &range,
55 nd_region->target_node, le32_to_cpu(pfn_sb->align),
56 IORESOURCE_DAX_STATIC);
57 if (!dax_region)
58 return ERR_PTR(-ENOMEM);
59
60 data = (struct dev_dax_data) {
61 .dax_region = dax_region,
62 .id = id,
63 .pgmap = &pgmap,
64 .size = range_len(&range),
65 .memmap_on_memory = false,
66 };
67
68 return devm_create_dev_dax(&data);
69 }
70
dax_pmem_probe(struct device * dev)71 static int dax_pmem_probe(struct device *dev)
72 {
73 return PTR_ERR_OR_ZERO(__dax_pmem_probe(dev));
74 }
75
76 static struct nd_device_driver dax_pmem_driver = {
77 .probe = dax_pmem_probe,
78 .drv = {
79 .name = "dax_pmem",
80 },
81 .type = ND_DRIVER_DAX_PMEM,
82 };
83
dax_pmem_init(void)84 static int __init dax_pmem_init(void)
85 {
86 return nd_driver_register(&dax_pmem_driver);
87 }
88 module_init(dax_pmem_init);
89
dax_pmem_exit(void)90 static void __exit dax_pmem_exit(void)
91 {
92 driver_unregister(&dax_pmem_driver.drv);
93 }
94 module_exit(dax_pmem_exit);
95
96 MODULE_DESCRIPTION("PMEM DAX: direct access to persistent memory");
97 MODULE_LICENSE("GPL v2");
98 MODULE_AUTHOR("Intel Corporation");
99 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);
100