1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/percpu-refcount.h>
14#include <linux/memremap.h>
15#include <linux/module.h>
16#include <linux/pfn_t.h>
17#include "../nvdimm/pfn.h"
18#include "../nvdimm/nd.h"
19#include "device-dax.h"
20
21struct dax_pmem {
22 struct device *dev;
23 struct percpu_ref ref;
24 struct dev_pagemap pgmap;
25 struct completion cmp;
26};
27
28static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
29{
30 return container_of(ref, struct dax_pmem, ref);
31}
32
33static void dax_pmem_percpu_release(struct percpu_ref *ref)
34{
35 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
36
37 dev_dbg(dax_pmem->dev, "trace\n");
38 complete(&dax_pmem->cmp);
39}
40
41static void dax_pmem_percpu_exit(void *data)
42{
43 struct percpu_ref *ref = data;
44 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
45
46 dev_dbg(dax_pmem->dev, "trace\n");
47 percpu_ref_exit(ref);
48}
49
50static void dax_pmem_percpu_kill(void *data)
51{
52 struct percpu_ref *ref = data;
53 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
54
55 dev_dbg(dax_pmem->dev, "trace\n");
56 percpu_ref_kill(ref);
57 wait_for_completion(&dax_pmem->cmp);
58}
59
60static int dax_pmem_probe(struct device *dev)
61{
62 void *addr;
63 struct resource res;
64 int rc, id, region_id;
65 struct nd_pfn_sb *pfn_sb;
66 struct dev_dax *dev_dax;
67 struct dax_pmem *dax_pmem;
68 struct nd_namespace_io *nsio;
69 struct dax_region *dax_region;
70 struct nd_namespace_common *ndns;
71 struct nd_dax *nd_dax = to_nd_dax(dev);
72 struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
73
74 ndns = nvdimm_namespace_common_probe(dev);
75 if (IS_ERR(ndns))
76 return PTR_ERR(ndns);
77 nsio = to_nd_namespace_io(&ndns->dev);
78
79 dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
80 if (!dax_pmem)
81 return -ENOMEM;
82
83
84 rc = devm_nsio_enable(dev, nsio);
85 if (rc)
86 return rc;
87 rc = nvdimm_setup_pfn(nd_pfn, &dax_pmem->pgmap);
88 if (rc)
89 return rc;
90 devm_nsio_disable(dev, nsio);
91
92 pfn_sb = nd_pfn->pfn_sb;
93
94 if (!devm_request_mem_region(dev, nsio->res.start,
95 resource_size(&nsio->res),
96 dev_name(&ndns->dev))) {
97 dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
98 return -EBUSY;
99 }
100
101 dax_pmem->dev = dev;
102 init_completion(&dax_pmem->cmp);
103 rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
104 GFP_KERNEL);
105 if (rc)
106 return rc;
107
108 rc = devm_add_action(dev, dax_pmem_percpu_exit, &dax_pmem->ref);
109 if (rc) {
110 percpu_ref_exit(&dax_pmem->ref);
111 return rc;
112 }
113
114 dax_pmem->pgmap.type = MEMORY_DEVICE_DEV_DAX;
115 dax_pmem->pgmap.ref = &dax_pmem->ref;
116 addr = devm_memremap_pages(dev, &dax_pmem->pgmap);
117 if (IS_ERR(addr)) {
118 devm_remove_action(dev, dax_pmem_percpu_exit, &dax_pmem->ref);
119 percpu_ref_exit(&dax_pmem->ref);
120 return PTR_ERR(addr);
121 }
122
123 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
124 &dax_pmem->ref);
125 if (rc)
126 return rc;
127
128
129 memcpy(&res, &dax_pmem->pgmap.res, sizeof(res));
130 res.start += le64_to_cpu(pfn_sb->dataoff);
131
132 rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", ®ion_id, &id);
133 if (rc != 2)
134 return -EINVAL;
135
136 dax_region = alloc_dax_region(dev, region_id, &res,
137 le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
138 if (!dax_region)
139 return -ENOMEM;
140
141
142 dev_dax = devm_create_dev_dax(dax_region, id, &res, 1);
143
144
145 dax_region_put(dax_region);
146
147 return PTR_ERR_OR_ZERO(dev_dax);
148}
149
150static struct nd_device_driver dax_pmem_driver = {
151 .probe = dax_pmem_probe,
152 .drv = {
153 .name = "dax_pmem",
154 },
155 .type = ND_DRIVER_DAX_PMEM,
156};
157
158module_nd_driver(dax_pmem_driver);
159
160MODULE_LICENSE("GPL v2");
161MODULE_AUTHOR("Intel Corporation");
162MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);
163