]>
Commit | Line | Data |
---|---|---|
ab68f262 DW |
1 | /* |
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" | |
7b6be844 | 19 | #include "device-dax.h" |
ab68f262 DW |
20 | |
21 | struct dax_pmem { | |
22 | struct device *dev; | |
23 | struct percpu_ref ref; | |
e8d51348 | 24 | struct dev_pagemap pgmap; |
ab68f262 DW |
25 | struct completion cmp; |
26 | }; | |
27 | ||
ccdb07f6 | 28 | static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref) |
ab68f262 DW |
29 | { |
30 | return container_of(ref, struct dax_pmem, ref); | |
31 | } | |
32 | ||
33 | static void dax_pmem_percpu_release(struct percpu_ref *ref) | |
34 | { | |
35 | struct dax_pmem *dax_pmem = to_dax_pmem(ref); | |
36 | ||
6daaca52 | 37 | dev_dbg(dax_pmem->dev, "trace\n"); |
ab68f262 DW |
38 | complete(&dax_pmem->cmp); |
39 | } | |
40 | ||
41 | static 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 | ||
6daaca52 | 46 | dev_dbg(dax_pmem->dev, "trace\n"); |
71389703 | 47 | wait_for_completion(&dax_pmem->cmp); |
ab68f262 | 48 | percpu_ref_exit(ref); |
ab68f262 DW |
49 | } |
50 | ||
a95c90f1 | 51 | static void dax_pmem_percpu_kill(struct percpu_ref *ref) |
ab68f262 | 52 | { |
ab68f262 DW |
53 | struct dax_pmem *dax_pmem = to_dax_pmem(ref); |
54 | ||
6daaca52 | 55 | dev_dbg(dax_pmem->dev, "trace\n"); |
ab68f262 DW |
56 | percpu_ref_kill(ref); |
57 | } | |
58 | ||
59 | static int dax_pmem_probe(struct device *dev) | |
60 | { | |
ab68f262 DW |
61 | void *addr; |
62 | struct resource res; | |
bbb3be17 | 63 | int rc, id, region_id; |
ab68f262 | 64 | struct nd_pfn_sb *pfn_sb; |
5f0694b3 | 65 | struct dev_dax *dev_dax; |
ab68f262 | 66 | struct dax_pmem *dax_pmem; |
ab68f262 DW |
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; | |
ab68f262 DW |
72 | |
73 | ndns = nvdimm_namespace_common_probe(dev); | |
74 | if (IS_ERR(ndns)) | |
75 | return PTR_ERR(ndns); | |
76 | nsio = to_nd_namespace_io(&ndns->dev); | |
77 | ||
e8d51348 CH |
78 | dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL); |
79 | if (!dax_pmem) | |
80 | return -ENOMEM; | |
81 | ||
ab68f262 | 82 | /* parse the 'pfn' info block via ->rw_bytes */ |
6a84fb4b DW |
83 | rc = devm_nsio_enable(dev, nsio); |
84 | if (rc) | |
85 | return rc; | |
e8d51348 CH |
86 | rc = nvdimm_setup_pfn(nd_pfn, &dax_pmem->pgmap); |
87 | if (rc) | |
88 | return rc; | |
ab68f262 DW |
89 | devm_nsio_disable(dev, nsio); |
90 | ||
91 | pfn_sb = nd_pfn->pfn_sb; | |
92 | ||
93 | if (!devm_request_mem_region(dev, nsio->res.start, | |
450c6633 DW |
94 | resource_size(&nsio->res), |
95 | dev_name(&ndns->dev))) { | |
ab68f262 DW |
96 | dev_warn(dev, "could not reserve region %pR\n", &nsio->res); |
97 | return -EBUSY; | |
98 | } | |
99 | ||
ab68f262 DW |
100 | dax_pmem->dev = dev; |
101 | init_completion(&dax_pmem->cmp); | |
102 | rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0, | |
103 | GFP_KERNEL); | |
104 | if (rc) | |
105 | return rc; | |
106 | ||
b7751410 SH |
107 | rc = devm_add_action(dev, dax_pmem_percpu_exit, &dax_pmem->ref); |
108 | if (rc) { | |
109 | percpu_ref_exit(&dax_pmem->ref); | |
ab68f262 | 110 | return rc; |
b7751410 | 111 | } |
ab68f262 | 112 | |
e8d51348 | 113 | dax_pmem->pgmap.ref = &dax_pmem->ref; |
a95c90f1 | 114 | dax_pmem->pgmap.kill = dax_pmem_percpu_kill; |
e8d51348 | 115 | addr = devm_memremap_pages(dev, &dax_pmem->pgmap); |
a95c90f1 | 116 | if (IS_ERR(addr)) |
ab68f262 | 117 | return PTR_ERR(addr); |
ab68f262 | 118 | |
d0e58455 | 119 | /* adjust the dax_region resource to the start of data */ |
e8d51348 | 120 | memcpy(&res, &dax_pmem->pgmap.res, sizeof(res)); |
d0e58455 DW |
121 | res.start += le64_to_cpu(pfn_sb->dataoff); |
122 | ||
bbb3be17 DW |
123 | rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", ®ion_id, &id); |
124 | if (rc != 2) | |
125 | return -EINVAL; | |
126 | ||
127 | dax_region = alloc_dax_region(dev, region_id, &res, | |
ab68f262 DW |
128 | le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP); |
129 | if (!dax_region) | |
130 | return -ENOMEM; | |
131 | ||
132 | /* TODO: support for subdividing a dax region... */ | |
bbb3be17 | 133 | dev_dax = devm_create_dev_dax(dax_region, id, &res, 1); |
ab68f262 | 134 | |
5f0694b3 | 135 | /* child dev_dax instances now own the lifetime of the dax_region */ |
ab68f262 DW |
136 | dax_region_put(dax_region); |
137 | ||
5f0694b3 | 138 | return PTR_ERR_OR_ZERO(dev_dax); |
ab68f262 DW |
139 | } |
140 | ||
141 | static struct nd_device_driver dax_pmem_driver = { | |
142 | .probe = dax_pmem_probe, | |
143 | .drv = { | |
144 | .name = "dax_pmem", | |
145 | }, | |
146 | .type = ND_DRIVER_DAX_PMEM, | |
147 | }; | |
148 | ||
d812b0d5 | 149 | module_nd_driver(dax_pmem_driver); |
ab68f262 DW |
150 | |
151 | MODULE_LICENSE("GPL v2"); | |
152 | MODULE_AUTHOR("Intel Corporation"); | |
153 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM); |