]>
Commit | Line | Data |
---|---|---|
e1455744 DW |
1 | /* |
2 | * Copyright(c) 2013-2015 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/device.h> | |
14 | #include <linux/sizes.h> | |
200c79da | 15 | #include <linux/pmem.h> |
e1455744 DW |
16 | #include "nd-core.h" |
17 | #include "pfn.h" | |
18 | #include "btt.h" | |
19 | #include "nd.h" | |
20 | ||
21 | void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns) | |
22 | { | |
23 | struct nd_namespace_common *ndns = *_ndns; | |
452bae0a | 24 | struct nvdimm_bus *nvdimm_bus; |
e1455744 | 25 | |
452bae0a DW |
26 | if (!ndns) |
27 | return; | |
28 | ||
29 | nvdimm_bus = walk_to_nvdimm_bus(&ndns->dev); | |
30 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); | |
9cf8bd52 | 31 | dev_WARN_ONCE(dev, ndns->claim != dev, "%s: invalid claim\n", __func__); |
e1455744 DW |
32 | ndns->claim = NULL; |
33 | *_ndns = NULL; | |
34 | put_device(&ndns->dev); | |
35 | } | |
36 | ||
37 | void nd_detach_ndns(struct device *dev, | |
38 | struct nd_namespace_common **_ndns) | |
39 | { | |
40 | struct nd_namespace_common *ndns = *_ndns; | |
41 | ||
42 | if (!ndns) | |
43 | return; | |
44 | get_device(&ndns->dev); | |
452bae0a | 45 | nvdimm_bus_lock(&ndns->dev); |
e1455744 | 46 | __nd_detach_ndns(dev, _ndns); |
452bae0a | 47 | nvdimm_bus_unlock(&ndns->dev); |
e1455744 DW |
48 | put_device(&ndns->dev); |
49 | } | |
50 | ||
51 | bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach, | |
52 | struct nd_namespace_common **_ndns) | |
53 | { | |
452bae0a DW |
54 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&attach->dev); |
55 | ||
e1455744 DW |
56 | if (attach->claim) |
57 | return false; | |
452bae0a | 58 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
9cf8bd52 | 59 | dev_WARN_ONCE(dev, *_ndns, "%s: invalid claim\n", __func__); |
e1455744 DW |
60 | attach->claim = dev; |
61 | *_ndns = attach; | |
62 | get_device(&attach->dev); | |
63 | return true; | |
64 | } | |
65 | ||
66 | bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach, | |
67 | struct nd_namespace_common **_ndns) | |
68 | { | |
69 | bool claimed; | |
70 | ||
452bae0a | 71 | nvdimm_bus_lock(&attach->dev); |
e1455744 | 72 | claimed = __nd_attach_ndns(dev, attach, _ndns); |
452bae0a | 73 | nvdimm_bus_unlock(&attach->dev); |
e1455744 DW |
74 | return claimed; |
75 | } | |
76 | ||
77 | static int namespace_match(struct device *dev, void *data) | |
78 | { | |
79 | char *name = data; | |
80 | ||
81 | return strcmp(name, dev_name(dev)) == 0; | |
82 | } | |
83 | ||
84 | static bool is_idle(struct device *dev, struct nd_namespace_common *ndns) | |
85 | { | |
86 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
87 | struct device *seed = NULL; | |
88 | ||
89 | if (is_nd_btt(dev)) | |
90 | seed = nd_region->btt_seed; | |
91 | else if (is_nd_pfn(dev)) | |
92 | seed = nd_region->pfn_seed; | |
cd03412a DW |
93 | else if (is_nd_dax(dev)) |
94 | seed = nd_region->dax_seed; | |
e1455744 DW |
95 | |
96 | if (seed == dev || ndns || dev->driver) | |
97 | return false; | |
98 | return true; | |
99 | } | |
100 | ||
03dca343 DW |
101 | struct nd_pfn *to_nd_pfn_safe(struct device *dev) |
102 | { | |
103 | /* | |
104 | * pfn device attributes are re-used by dax device instances, so we | |
105 | * need to be careful to correct device-to-nd_pfn conversion. | |
106 | */ | |
107 | if (is_nd_pfn(dev)) | |
108 | return to_nd_pfn(dev); | |
109 | ||
110 | if (is_nd_dax(dev)) { | |
111 | struct nd_dax *nd_dax = to_nd_dax(dev); | |
112 | ||
113 | return &nd_dax->nd_pfn; | |
114 | } | |
115 | ||
116 | WARN_ON(1); | |
117 | return NULL; | |
118 | } | |
119 | ||
e1455744 DW |
120 | static void nd_detach_and_reset(struct device *dev, |
121 | struct nd_namespace_common **_ndns) | |
122 | { | |
123 | /* detach the namespace and destroy / reset the device */ | |
452bae0a | 124 | __nd_detach_ndns(dev, _ndns); |
e1455744 DW |
125 | if (is_idle(dev, *_ndns)) { |
126 | nd_device_unregister(dev, ND_ASYNC); | |
127 | } else if (is_nd_btt(dev)) { | |
128 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
129 | ||
130 | nd_btt->lbasize = 0; | |
131 | kfree(nd_btt->uuid); | |
132 | nd_btt->uuid = NULL; | |
03dca343 DW |
133 | } else if (is_nd_pfn(dev) || is_nd_dax(dev)) { |
134 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); | |
e1455744 DW |
135 | |
136 | kfree(nd_pfn->uuid); | |
137 | nd_pfn->uuid = NULL; | |
138 | nd_pfn->mode = PFN_MODE_NONE; | |
139 | } | |
140 | } | |
141 | ||
142 | ssize_t nd_namespace_store(struct device *dev, | |
143 | struct nd_namespace_common **_ndns, const char *buf, | |
144 | size_t len) | |
145 | { | |
146 | struct nd_namespace_common *ndns; | |
147 | struct device *found; | |
148 | char *name; | |
149 | ||
150 | if (dev->driver) { | |
151 | dev_dbg(dev, "%s: -EBUSY\n", __func__); | |
152 | return -EBUSY; | |
153 | } | |
154 | ||
155 | name = kstrndup(buf, len, GFP_KERNEL); | |
156 | if (!name) | |
157 | return -ENOMEM; | |
158 | strim(name); | |
159 | ||
160 | if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0) | |
161 | /* pass */; | |
162 | else { | |
163 | len = -EINVAL; | |
164 | goto out; | |
165 | } | |
166 | ||
167 | ndns = *_ndns; | |
168 | if (strcmp(name, "") == 0) { | |
169 | nd_detach_and_reset(dev, _ndns); | |
170 | goto out; | |
171 | } else if (ndns) { | |
172 | dev_dbg(dev, "namespace already set to: %s\n", | |
173 | dev_name(&ndns->dev)); | |
174 | len = -EBUSY; | |
175 | goto out; | |
176 | } | |
177 | ||
178 | found = device_find_child(dev->parent, name, namespace_match); | |
179 | if (!found) { | |
180 | dev_dbg(dev, "'%s' not found under %s\n", name, | |
181 | dev_name(dev->parent)); | |
182 | len = -ENODEV; | |
183 | goto out; | |
184 | } | |
185 | ||
186 | ndns = to_ndns(found); | |
b3fde74e DW |
187 | |
188 | switch (ndns->claim_class) { | |
189 | case NVDIMM_CCLASS_NONE: | |
190 | break; | |
191 | case NVDIMM_CCLASS_BTT: | |
192 | if (!is_nd_btt(dev)) { | |
193 | len = -EBUSY; | |
194 | goto out_attach; | |
195 | } | |
196 | break; | |
197 | case NVDIMM_CCLASS_PFN: | |
198 | if (!is_nd_pfn(dev)) { | |
199 | len = -EBUSY; | |
200 | goto out_attach; | |
201 | } | |
202 | break; | |
203 | case NVDIMM_CCLASS_DAX: | |
204 | if (!is_nd_dax(dev)) { | |
205 | len = -EBUSY; | |
206 | goto out_attach; | |
207 | } | |
208 | break; | |
209 | default: | |
210 | len = -EBUSY; | |
211 | goto out_attach; | |
212 | break; | |
213 | } | |
214 | ||
e1455744 DW |
215 | if (__nvdimm_namespace_capacity(ndns) < SZ_16M) { |
216 | dev_dbg(dev, "%s too small to host\n", name); | |
217 | len = -ENXIO; | |
218 | goto out_attach; | |
219 | } | |
220 | ||
221 | WARN_ON_ONCE(!is_nvdimm_bus_locked(dev)); | |
452bae0a | 222 | if (!__nd_attach_ndns(dev, ndns, _ndns)) { |
e1455744 DW |
223 | dev_dbg(dev, "%s already claimed\n", |
224 | dev_name(&ndns->dev)); | |
225 | len = -EBUSY; | |
226 | } | |
227 | ||
228 | out_attach: | |
229 | put_device(&ndns->dev); /* from device_find_child */ | |
230 | out: | |
231 | kfree(name); | |
232 | return len; | |
233 | } | |
234 | ||
235 | /* | |
236 | * nd_sb_checksum: compute checksum for a generic info block | |
237 | * | |
238 | * Returns a fletcher64 checksum of everything in the given info block | |
239 | * except the last field (since that's where the checksum lives). | |
240 | */ | |
241 | u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb) | |
242 | { | |
243 | u64 sum; | |
244 | __le64 sum_save; | |
245 | ||
246 | BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K); | |
247 | BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K); | |
248 | BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K); | |
249 | ||
250 | sum_save = nd_gen_sb->checksum; | |
251 | nd_gen_sb->checksum = 0; | |
252 | sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1); | |
253 | nd_gen_sb->checksum = sum_save; | |
254 | return sum; | |
255 | } | |
256 | EXPORT_SYMBOL(nd_sb_checksum); | |
200c79da DW |
257 | |
258 | static int nsio_rw_bytes(struct nd_namespace_common *ndns, | |
3ae3d67b VV |
259 | resource_size_t offset, void *buf, size_t size, int rw, |
260 | unsigned long flags) | |
200c79da DW |
261 | { |
262 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); | |
82bf1037 DJ |
263 | unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512); |
264 | sector_t sector = offset >> 9; | |
265 | int rc = 0; | |
266 | ||
267 | if (unlikely(!size)) | |
268 | return 0; | |
200c79da DW |
269 | |
270 | if (unlikely(offset + size > nsio->size)) { | |
271 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); | |
272 | return -EFAULT; | |
273 | } | |
274 | ||
275 | if (rw == READ) { | |
82bf1037 | 276 | if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) |
200c79da | 277 | return -EIO; |
6abccd1b | 278 | return memcpy_mcsafe(buf, nsio->addr + offset, size); |
d37806dc FF |
279 | } |
280 | ||
281 | if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) { | |
a3e9af95 DW |
282 | /* |
283 | * FIXME: nsio_rw_bytes() may be called from atomic | |
284 | * context in the btt case and the ACPI DSM path for | |
285 | * clearing the error takes sleeping locks and allocates | |
286 | * memory. An explicit error clearing path, and support | |
287 | * for tracking badblocks in BTT metadata is needed to | |
288 | * work around this collision. | |
289 | */ | |
290 | if (IS_ALIGNED(offset, 512) && IS_ALIGNED(size, 512) | |
3ae3d67b VV |
291 | && !(flags & NVDIMM_IO_ATOMIC) |
292 | && !ndns->claim) { | |
d37806dc FF |
293 | long cleared; |
294 | ||
97681f9b TK |
295 | cleared = nvdimm_clear_poison(&ndns->dev, |
296 | nsio->res.start + offset, size); | |
868f036f | 297 | if (cleared < size) |
82bf1037 | 298 | rc = -EIO; |
868f036f DW |
299 | if (cleared > 0 && cleared / 512) { |
300 | cleared /= 512; | |
301 | badblocks_clear(&nsio->bb, sector, cleared); | |
d37806dc | 302 | } |
868f036f | 303 | invalidate_pmem(nsio->addr + offset, size); |
d37806dc FF |
304 | } else |
305 | rc = -EIO; | |
200c79da DW |
306 | } |
307 | ||
d37806dc FF |
308 | memcpy_to_pmem(nsio->addr + offset, buf, size); |
309 | nvdimm_flush(to_nd_region(ndns->dev.parent)); | |
310 | ||
82bf1037 | 311 | return rc; |
200c79da DW |
312 | } |
313 | ||
314 | int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio) | |
315 | { | |
316 | struct resource *res = &nsio->res; | |
317 | struct nd_namespace_common *ndns = &nsio->common; | |
318 | ||
319 | nsio->size = resource_size(res); | |
320 | if (!devm_request_mem_region(dev, res->start, resource_size(res), | |
450c6633 | 321 | dev_name(&ndns->dev))) { |
200c79da DW |
322 | dev_warn(dev, "could not reserve region %pR\n", res); |
323 | return -EBUSY; | |
324 | } | |
325 | ||
326 | ndns->rw_bytes = nsio_rw_bytes; | |
327 | if (devm_init_badblocks(dev, &nsio->bb)) | |
328 | return -ENOMEM; | |
329 | nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb, | |
330 | &nsio->res); | |
331 | ||
332 | nsio->addr = devm_memremap(dev, res->start, resource_size(res), | |
333 | ARCH_MEMREMAP_PMEM); | |
42588958 DW |
334 | |
335 | return PTR_ERR_OR_ZERO(nsio->addr); | |
200c79da DW |
336 | } |
337 | EXPORT_SYMBOL_GPL(devm_nsio_enable); | |
338 | ||
339 | void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio) | |
340 | { | |
341 | struct resource *res = &nsio->res; | |
342 | ||
343 | devm_memunmap(dev, nsio->addr); | |
344 | devm_exit_badblocks(dev, &nsio->bb); | |
345 | devm_release_mem_region(dev, res->start, resource_size(res)); | |
346 | } | |
347 | EXPORT_SYMBOL_GPL(devm_nsio_disable); |