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