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