xref: /openbmc/linux/drivers/nvdimm/btt_devs.c (revision 9cf0666f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #include <linux/blkdev.h>
6 #include <linux/device.h>
7 #include <linux/genhd.h>
8 #include <linux/sizes.h>
9 #include <linux/slab.h>
10 #include <linux/fs.h>
11 #include <linux/mm.h>
12 #include "nd-core.h"
13 #include "btt.h"
14 #include "nd.h"
15 
16 static void nd_btt_release(struct device *dev)
17 {
18 	struct nd_region *nd_region = to_nd_region(dev->parent);
19 	struct nd_btt *nd_btt = to_nd_btt(dev);
20 
21 	dev_dbg(dev, "trace\n");
22 	nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
23 	ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
24 	kfree(nd_btt->uuid);
25 	kfree(nd_btt);
26 }
27 
28 struct nd_btt *to_nd_btt(struct device *dev)
29 {
30 	struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
31 
32 	WARN_ON(!is_nd_btt(dev));
33 	return nd_btt;
34 }
35 EXPORT_SYMBOL(to_nd_btt);
36 
37 static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
38 	4096, 4104, 4160, 4224, 0 };
39 
40 static ssize_t sector_size_show(struct device *dev,
41 		struct device_attribute *attr, char *buf)
42 {
43 	struct nd_btt *nd_btt = to_nd_btt(dev);
44 
45 	return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
46 }
47 
48 static ssize_t sector_size_store(struct device *dev,
49 		struct device_attribute *attr, const char *buf, size_t len)
50 {
51 	struct nd_btt *nd_btt = to_nd_btt(dev);
52 	ssize_t rc;
53 
54 	nd_device_lock(dev);
55 	nvdimm_bus_lock(dev);
56 	rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
57 			btt_lbasize_supported);
58 	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
59 			buf[len - 1] == '\n' ? "" : "\n");
60 	nvdimm_bus_unlock(dev);
61 	nd_device_unlock(dev);
62 
63 	return rc ? rc : len;
64 }
65 static DEVICE_ATTR_RW(sector_size);
66 
67 static ssize_t uuid_show(struct device *dev,
68 		struct device_attribute *attr, char *buf)
69 {
70 	struct nd_btt *nd_btt = to_nd_btt(dev);
71 
72 	if (nd_btt->uuid)
73 		return sprintf(buf, "%pUb\n", nd_btt->uuid);
74 	return sprintf(buf, "\n");
75 }
76 
77 static ssize_t uuid_store(struct device *dev,
78 		struct device_attribute *attr, const char *buf, size_t len)
79 {
80 	struct nd_btt *nd_btt = to_nd_btt(dev);
81 	ssize_t rc;
82 
83 	nd_device_lock(dev);
84 	rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
85 	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
86 			buf[len - 1] == '\n' ? "" : "\n");
87 	nd_device_unlock(dev);
88 
89 	return rc ? rc : len;
90 }
91 static DEVICE_ATTR_RW(uuid);
92 
93 static ssize_t namespace_show(struct device *dev,
94 		struct device_attribute *attr, char *buf)
95 {
96 	struct nd_btt *nd_btt = to_nd_btt(dev);
97 	ssize_t rc;
98 
99 	nvdimm_bus_lock(dev);
100 	rc = sprintf(buf, "%s\n", nd_btt->ndns
101 			? dev_name(&nd_btt->ndns->dev) : "");
102 	nvdimm_bus_unlock(dev);
103 	return rc;
104 }
105 
106 static ssize_t namespace_store(struct device *dev,
107 		struct device_attribute *attr, const char *buf, size_t len)
108 {
109 	struct nd_btt *nd_btt = to_nd_btt(dev);
110 	ssize_t rc;
111 
112 	nd_device_lock(dev);
113 	nvdimm_bus_lock(dev);
114 	rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
115 	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
116 			buf[len - 1] == '\n' ? "" : "\n");
117 	nvdimm_bus_unlock(dev);
118 	nd_device_unlock(dev);
119 
120 	return rc;
121 }
122 static DEVICE_ATTR_RW(namespace);
123 
124 static ssize_t size_show(struct device *dev,
125 		struct device_attribute *attr, char *buf)
126 {
127 	struct nd_btt *nd_btt = to_nd_btt(dev);
128 	ssize_t rc;
129 
130 	nd_device_lock(dev);
131 	if (dev->driver)
132 		rc = sprintf(buf, "%llu\n", nd_btt->size);
133 	else {
134 		/* no size to convey if the btt instance is disabled */
135 		rc = -ENXIO;
136 	}
137 	nd_device_unlock(dev);
138 
139 	return rc;
140 }
141 static DEVICE_ATTR_RO(size);
142 
143 static ssize_t log_zero_flags_show(struct device *dev,
144 		struct device_attribute *attr, char *buf)
145 {
146 	return sprintf(buf, "Y\n");
147 }
148 static DEVICE_ATTR_RO(log_zero_flags);
149 
150 static struct attribute *nd_btt_attributes[] = {
151 	&dev_attr_sector_size.attr,
152 	&dev_attr_namespace.attr,
153 	&dev_attr_uuid.attr,
154 	&dev_attr_size.attr,
155 	&dev_attr_log_zero_flags.attr,
156 	NULL,
157 };
158 
159 static struct attribute_group nd_btt_attribute_group = {
160 	.attrs = nd_btt_attributes,
161 };
162 
163 static const struct attribute_group *nd_btt_attribute_groups[] = {
164 	&nd_btt_attribute_group,
165 	&nd_device_attribute_group,
166 	&nd_numa_attribute_group,
167 	NULL,
168 };
169 
170 static const struct device_type nd_btt_device_type = {
171 	.name = "nd_btt",
172 	.release = nd_btt_release,
173 	.groups = nd_btt_attribute_groups,
174 };
175 
176 bool is_nd_btt(struct device *dev)
177 {
178 	return dev->type == &nd_btt_device_type;
179 }
180 EXPORT_SYMBOL(is_nd_btt);
181 
182 static struct device *__nd_btt_create(struct nd_region *nd_region,
183 				      unsigned long lbasize, uuid_t *uuid,
184 				      struct nd_namespace_common *ndns)
185 {
186 	struct nd_btt *nd_btt;
187 	struct device *dev;
188 
189 	nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
190 	if (!nd_btt)
191 		return NULL;
192 
193 	nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
194 	if (nd_btt->id < 0)
195 		goto out_nd_btt;
196 
197 	nd_btt->lbasize = lbasize;
198 	if (uuid) {
199 		uuid = kmemdup(uuid, 16, GFP_KERNEL);
200 		if (!uuid)
201 			goto out_put_id;
202 	}
203 	nd_btt->uuid = uuid;
204 	dev = &nd_btt->dev;
205 	dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
206 	dev->parent = &nd_region->dev;
207 	dev->type = &nd_btt_device_type;
208 	device_initialize(&nd_btt->dev);
209 	if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
210 		dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
211 				dev_name(ndns->claim));
212 		put_device(dev);
213 		return NULL;
214 	}
215 	return dev;
216 
217 out_put_id:
218 	ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
219 
220 out_nd_btt:
221 	kfree(nd_btt);
222 	return NULL;
223 }
224 
225 struct device *nd_btt_create(struct nd_region *nd_region)
226 {
227 	struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
228 
229 	__nd_device_register(dev);
230 	return dev;
231 }
232 
233 /**
234  * nd_btt_arena_is_valid - check if the metadata layout is valid
235  * @nd_btt:	device with BTT geometry and backing device info
236  * @super:	pointer to the arena's info block being tested
237  *
238  * Check consistency of the btt info block with itself by validating
239  * the checksum, and with the parent namespace by verifying the
240  * parent_uuid contained in the info block with the one supplied in.
241  *
242  * Returns:
243  * false for an invalid info block, true for a valid one
244  */
245 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
246 {
247 	const uuid_t *ns_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
248 	uuid_t parent_uuid;
249 	u64 checksum;
250 
251 	if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
252 		return false;
253 
254 	import_uuid(&parent_uuid, super->parent_uuid);
255 	if (!uuid_is_null(&parent_uuid))
256 		if (!uuid_equal(&parent_uuid, ns_uuid))
257 			return false;
258 
259 	checksum = le64_to_cpu(super->checksum);
260 	super->checksum = 0;
261 	if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
262 		return false;
263 	super->checksum = cpu_to_le64(checksum);
264 
265 	/* TODO: figure out action for this */
266 	if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
267 		dev_info(&nd_btt->dev, "Found arena with an error flag\n");
268 
269 	return true;
270 }
271 EXPORT_SYMBOL(nd_btt_arena_is_valid);
272 
273 int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
274 		struct btt_sb *btt_sb)
275 {
276 	if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
277 		/* Probe/setup for BTT v2.0 */
278 		nd_btt->initial_offset = 0;
279 		nd_btt->version_major = 2;
280 		nd_btt->version_minor = 0;
281 		if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
282 			return -ENXIO;
283 		if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
284 			return -ENODEV;
285 		if ((le16_to_cpu(btt_sb->version_major) != 2) ||
286 				(le16_to_cpu(btt_sb->version_minor) != 0))
287 			return -ENODEV;
288 	} else {
289 		/*
290 		 * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
291 		 * NVDIMM_CCLASS_BTT)
292 		 */
293 		nd_btt->initial_offset = SZ_4K;
294 		nd_btt->version_major = 1;
295 		nd_btt->version_minor = 1;
296 		if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
297 			return -ENXIO;
298 		if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
299 			return -ENODEV;
300 		if ((le16_to_cpu(btt_sb->version_major) != 1) ||
301 				(le16_to_cpu(btt_sb->version_minor) != 1))
302 			return -ENODEV;
303 	}
304 	return 0;
305 }
306 EXPORT_SYMBOL(nd_btt_version);
307 
308 static int __nd_btt_probe(struct nd_btt *nd_btt,
309 		struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
310 {
311 	int rc;
312 
313 	if (!btt_sb || !ndns || !nd_btt)
314 		return -ENODEV;
315 
316 	if (nvdimm_namespace_capacity(ndns) < SZ_16M)
317 		return -ENXIO;
318 
319 	rc = nd_btt_version(nd_btt, ndns, btt_sb);
320 	if (rc < 0)
321 		return rc;
322 
323 	nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
324 	nd_btt->uuid = kmemdup(&btt_sb->uuid, sizeof(uuid_t), GFP_KERNEL);
325 	if (!nd_btt->uuid)
326 		return -ENOMEM;
327 
328 	__nd_device_register(&nd_btt->dev);
329 
330 	return 0;
331 }
332 
333 int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
334 {
335 	int rc;
336 	struct device *btt_dev;
337 	struct btt_sb *btt_sb;
338 	struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
339 
340 	if (ndns->force_raw)
341 		return -ENODEV;
342 
343 	switch (ndns->claim_class) {
344 	case NVDIMM_CCLASS_NONE:
345 	case NVDIMM_CCLASS_BTT:
346 	case NVDIMM_CCLASS_BTT2:
347 		break;
348 	default:
349 		return -ENODEV;
350 	}
351 
352 	nvdimm_bus_lock(&ndns->dev);
353 	btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
354 	nvdimm_bus_unlock(&ndns->dev);
355 	if (!btt_dev)
356 		return -ENOMEM;
357 	btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
358 	rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
359 	dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
360 	if (rc < 0) {
361 		struct nd_btt *nd_btt = to_nd_btt(btt_dev);
362 
363 		nd_detach_ndns(btt_dev, &nd_btt->ndns);
364 		put_device(btt_dev);
365 	}
366 
367 	return rc;
368 }
369 EXPORT_SYMBOL(nd_btt_probe);
370