blob: 1f01e98c83c7ddcccecd50e11671e3b519b2c136 [file] [log] [blame]
Dan Williamsab68f262016-05-18 09:15:08 -07001/*
2 * Copyright(c) 2016 Intel Corporation. All rights reserved.
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 */
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 "dax.h"
20
21struct dax_pmem {
22 struct device *dev;
23 struct percpu_ref ref;
24 struct completion cmp;
25};
26
27struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
28{
29 return container_of(ref, struct dax_pmem, ref);
30}
31
32static void dax_pmem_percpu_release(struct percpu_ref *ref)
33{
34 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
35
36 dev_dbg(dax_pmem->dev, "%s\n", __func__);
37 complete(&dax_pmem->cmp);
38}
39
40static void dax_pmem_percpu_exit(void *data)
41{
42 struct percpu_ref *ref = data;
43 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
44
45 dev_dbg(dax_pmem->dev, "%s\n", __func__);
46 percpu_ref_exit(ref);
47 wait_for_completion(&dax_pmem->cmp);
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, "%s\n", __func__);
56 percpu_ref_kill(ref);
57}
58
59static int dax_pmem_probe(struct device *dev)
60{
61 int rc;
62 void *addr;
63 struct resource res;
64 struct nd_pfn_sb *pfn_sb;
65 struct dax_pmem *dax_pmem;
66 struct nd_region *nd_region;
67 struct nd_namespace_io *nsio;
68 struct dax_region *dax_region;
69 struct nd_namespace_common *ndns;
70 struct nd_dax *nd_dax = to_nd_dax(dev);
71 struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
72 struct vmem_altmap __altmap, *altmap = NULL;
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 /* parse the 'pfn' info block via ->rw_bytes */
80 devm_nsio_enable(dev, nsio);
81 altmap = nvdimm_setup_pfn(nd_pfn, &res, &__altmap);
82 if (IS_ERR(altmap))
83 return PTR_ERR(altmap);
84 devm_nsio_disable(dev, nsio);
85
86 pfn_sb = nd_pfn->pfn_sb;
87
88 if (!devm_request_mem_region(dev, nsio->res.start,
89 resource_size(&nsio->res), dev_name(dev))) {
90 dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
91 return -EBUSY;
92 }
93
94 dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
95 if (!dax_pmem)
96 return -ENOMEM;
97
98 dax_pmem->dev = dev;
99 init_completion(&dax_pmem->cmp);
100 rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
101 GFP_KERNEL);
102 if (rc)
103 return rc;
104
Sajjan, Vikas Cd1c8e0c2016-07-05 11:20:07 +0530105 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
106 &dax_pmem->ref);
107 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700108 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700109
110 addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
111 if (IS_ERR(addr))
112 return PTR_ERR(addr);
113
Sajjan, Vikas Cd1c8e0c2016-07-05 11:20:07 +0530114 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
115 &dax_pmem->ref);
116 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700117 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700118
Dan Williamsd0e58452016-08-25 15:17:14 -0700119 /* adjust the dax_region resource to the start of data */
120 res.start += le64_to_cpu(pfn_sb->dataoff);
121
Dan Williamsab68f262016-05-18 09:15:08 -0700122 nd_region = to_nd_region(dev->parent);
123 dax_region = alloc_dax_region(dev, nd_region->id, &res,
124 le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
125 if (!dax_region)
126 return -ENOMEM;
127
128 /* TODO: support for subdividing a dax region... */
129 rc = devm_create_dax_dev(dax_region, &res, 1);
130
131 /* child dax_dev instances now own the lifetime of the dax_region */
132 dax_region_put(dax_region);
133
134 return rc;
135}
136
137static struct nd_device_driver dax_pmem_driver = {
138 .probe = dax_pmem_probe,
139 .drv = {
140 .name = "dax_pmem",
141 },
142 .type = ND_DRIVER_DAX_PMEM,
143};
144
145static int __init dax_pmem_init(void)
146{
147 return nd_driver_register(&dax_pmem_driver);
148}
149module_init(dax_pmem_init);
150
151static void __exit dax_pmem_exit(void)
152{
153 driver_unregister(&dax_pmem_driver.drv);
154}
155module_exit(dax_pmem_exit);
156
157MODULE_LICENSE("GPL v2");
158MODULE_AUTHOR("Intel Corporation");
159MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);