blob: 73c6ce93a0d9204227818465a707db9f7d5806fb [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
Dan Williamsccdb07f2016-08-06 16:05:06 -070027static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
Dan Williamsab68f262016-05-18 09:15:08 -070028{
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);
Dan Williamsab68f262016-05-18 09:15:08 -070047}
48
49static void dax_pmem_percpu_kill(void *data)
50{
51 struct percpu_ref *ref = data;
52 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
53
54 dev_dbg(dax_pmem->dev, "%s\n", __func__);
55 percpu_ref_kill(ref);
Dan Williams52e73eb2016-10-27 17:04:05 -070056 wait_for_completion(&dax_pmem->cmp);
Dan Williamsab68f262016-05-18 09:15:08 -070057}
58
59static int dax_pmem_probe(struct device *dev)
60{
61 int rc;
62 void *addr;
63 struct resource res;
Dan Williamsd76911e2016-07-19 17:51:40 -070064 struct dax_dev *dax_dev;
Dan Williamsab68f262016-05-18 09:15:08 -070065 struct nd_pfn_sb *pfn_sb;
66 struct dax_pmem *dax_pmem;
67 struct nd_region *nd_region;
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 struct vmem_altmap __altmap, *altmap = NULL;
74
75 ndns = nvdimm_namespace_common_probe(dev);
76 if (IS_ERR(ndns))
77 return PTR_ERR(ndns);
78 nsio = to_nd_namespace_io(&ndns->dev);
79
80 /* parse the 'pfn' info block via ->rw_bytes */
Dan Williams6a84fb42016-10-28 14:34:51 -070081 rc = devm_nsio_enable(dev, nsio);
82 if (rc)
83 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -070084 altmap = nvdimm_setup_pfn(nd_pfn, &res, &__altmap);
85 if (IS_ERR(altmap))
86 return PTR_ERR(altmap);
87 devm_nsio_disable(dev, nsio);
88
89 pfn_sb = nd_pfn->pfn_sb;
90
91 if (!devm_request_mem_region(dev, nsio->res.start,
92 resource_size(&nsio->res), dev_name(dev))) {
93 dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
94 return -EBUSY;
95 }
96
97 dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
98 if (!dax_pmem)
99 return -ENOMEM;
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
Sajjan, Vikas Cd1c8e0c2016-07-05 11:20:07 +0530108 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
109 &dax_pmem->ref);
110 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700111 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700112
113 addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
114 if (IS_ERR(addr))
115 return PTR_ERR(addr);
116
Sajjan, Vikas Cd1c8e0c2016-07-05 11:20:07 +0530117 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
118 &dax_pmem->ref);
119 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700120 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700121
Dan Williamsd0e58452016-08-25 15:17:14 -0700122 /* adjust the dax_region resource to the start of data */
123 res.start += le64_to_cpu(pfn_sb->dataoff);
124
Dan Williamsab68f262016-05-18 09:15:08 -0700125 nd_region = to_nd_region(dev->parent);
126 dax_region = alloc_dax_region(dev, nd_region->id, &res,
127 le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
128 if (!dax_region)
129 return -ENOMEM;
130
131 /* TODO: support for subdividing a dax region... */
Dan Williamsd76911e2016-07-19 17:51:40 -0700132 dax_dev = devm_create_dax_dev(dax_region, &res, 1);
Dan Williamsab68f262016-05-18 09:15:08 -0700133
134 /* child dax_dev instances now own the lifetime of the dax_region */
135 dax_region_put(dax_region);
136
Dan Williamsd76911e2016-07-19 17:51:40 -0700137 return PTR_ERR_OR_ZERO(dax_dev);
Dan Williamsab68f262016-05-18 09:15:08 -0700138}
139
140static struct nd_device_driver dax_pmem_driver = {
141 .probe = dax_pmem_probe,
142 .drv = {
143 .name = "dax_pmem",
144 },
145 .type = ND_DRIVER_DAX_PMEM,
146};
147
148static int __init dax_pmem_init(void)
149{
150 return nd_driver_register(&dax_pmem_driver);
151}
152module_init(dax_pmem_init);
153
154static void __exit dax_pmem_exit(void)
155{
156 driver_unregister(&dax_pmem_driver.drv);
157}
158module_exit(dax_pmem_exit);
159
160MODULE_LICENSE("GPL v2");
161MODULE_AUTHOR("Intel Corporation");
162MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);