1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/blkdev.h>
6 #include <linux/device.h>
7 #include <linux/sizes.h>
8 #include <linux/slab.h>
15 static void nd_btt_release(struct device *dev)
17 struct nd_region *nd_region = to_nd_region(dev->parent);
18 struct nd_btt *nd_btt = to_nd_btt(dev);
20 dev_dbg(dev, "trace\n");
21 nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
22 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
27 struct nd_btt *to_nd_btt(struct device *dev)
29 struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
31 WARN_ON(!is_nd_btt(dev));
34 EXPORT_SYMBOL(to_nd_btt);
36 static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
37 4096, 4104, 4160, 4224, 0 };
39 static ssize_t sector_size_show(struct device *dev,
40 struct device_attribute *attr, char *buf)
42 struct nd_btt *nd_btt = to_nd_btt(dev);
44 return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
47 static ssize_t sector_size_store(struct device *dev,
48 struct device_attribute *attr, const char *buf, size_t len)
50 struct nd_btt *nd_btt = to_nd_btt(dev);
55 rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
56 btt_lbasize_supported);
57 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
58 buf[len - 1] == '\n' ? "" : "\n");
59 nvdimm_bus_unlock(dev);
64 static DEVICE_ATTR_RW(sector_size);
66 static ssize_t uuid_show(struct device *dev,
67 struct device_attribute *attr, char *buf)
69 struct nd_btt *nd_btt = to_nd_btt(dev);
72 return sprintf(buf, "%pUb\n", nd_btt->uuid);
73 return sprintf(buf, "\n");
76 static ssize_t uuid_store(struct device *dev,
77 struct device_attribute *attr, const char *buf, size_t len)
79 struct nd_btt *nd_btt = to_nd_btt(dev);
83 rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
84 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
85 buf[len - 1] == '\n' ? "" : "\n");
90 static DEVICE_ATTR_RW(uuid);
92 static ssize_t namespace_show(struct device *dev,
93 struct device_attribute *attr, char *buf)
95 struct nd_btt *nd_btt = to_nd_btt(dev);
99 rc = sprintf(buf, "%s\n", nd_btt->ndns
100 ? dev_name(&nd_btt->ndns->dev) : "");
101 nvdimm_bus_unlock(dev);
105 static ssize_t namespace_store(struct device *dev,
106 struct device_attribute *attr, const char *buf, size_t len)
108 struct nd_btt *nd_btt = to_nd_btt(dev);
112 nvdimm_bus_lock(dev);
113 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
114 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
115 buf[len - 1] == '\n' ? "" : "\n");
116 nvdimm_bus_unlock(dev);
121 static DEVICE_ATTR_RW(namespace);
123 static ssize_t size_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
126 struct nd_btt *nd_btt = to_nd_btt(dev);
131 rc = sprintf(buf, "%llu\n", nd_btt->size);
133 /* no size to convey if the btt instance is disabled */
140 static DEVICE_ATTR_RO(size);
142 static ssize_t log_zero_flags_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
145 return sprintf(buf, "Y\n");
147 static DEVICE_ATTR_RO(log_zero_flags);
149 static struct attribute *nd_btt_attributes[] = {
150 &dev_attr_sector_size.attr,
151 &dev_attr_namespace.attr,
154 &dev_attr_log_zero_flags.attr,
158 static struct attribute_group nd_btt_attribute_group = {
159 .attrs = nd_btt_attributes,
162 static const struct attribute_group *nd_btt_attribute_groups[] = {
163 &nd_btt_attribute_group,
164 &nd_device_attribute_group,
165 &nd_numa_attribute_group,
169 static const struct device_type nd_btt_device_type = {
171 .release = nd_btt_release,
172 .groups = nd_btt_attribute_groups,
175 bool is_nd_btt(struct device *dev)
177 return dev->type == &nd_btt_device_type;
179 EXPORT_SYMBOL(is_nd_btt);
181 static struct lock_class_key nvdimm_btt_key;
183 static struct device *__nd_btt_create(struct nd_region *nd_region,
184 unsigned long lbasize, uuid_t *uuid,
185 struct nd_namespace_common *ndns)
187 struct nd_btt *nd_btt;
190 nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
194 nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
198 nd_btt->lbasize = lbasize;
200 uuid = kmemdup(uuid, 16, GFP_KERNEL);
206 dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
207 dev->parent = &nd_region->dev;
208 dev->type = &nd_btt_device_type;
209 device_initialize(&nd_btt->dev);
210 lockdep_set_class(&nd_btt->dev.mutex, &nvdimm_btt_key);
211 if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
212 dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
213 dev_name(ndns->claim));
220 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
227 struct device *nd_btt_create(struct nd_region *nd_region)
229 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
231 nd_device_register(dev);
236 * nd_btt_arena_is_valid - check if the metadata layout is valid
237 * @nd_btt: device with BTT geometry and backing device info
238 * @super: pointer to the arena's info block being tested
240 * Check consistency of the btt info block with itself by validating
241 * the checksum, and with the parent namespace by verifying the
242 * parent_uuid contained in the info block with the one supplied in.
245 * false for an invalid info block, true for a valid one
247 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
249 const uuid_t *ns_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
253 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
256 import_uuid(&parent_uuid, super->parent_uuid);
257 if (!uuid_is_null(&parent_uuid))
258 if (!uuid_equal(&parent_uuid, ns_uuid))
261 checksum = le64_to_cpu(super->checksum);
263 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
265 super->checksum = cpu_to_le64(checksum);
267 /* TODO: figure out action for this */
268 if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
269 dev_info(&nd_btt->dev, "Found arena with an error flag\n");
273 EXPORT_SYMBOL(nd_btt_arena_is_valid);
275 int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
276 struct btt_sb *btt_sb)
278 if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
279 /* Probe/setup for BTT v2.0 */
280 nd_btt->initial_offset = 0;
281 nd_btt->version_major = 2;
282 nd_btt->version_minor = 0;
283 if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
285 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
287 if ((le16_to_cpu(btt_sb->version_major) != 2) ||
288 (le16_to_cpu(btt_sb->version_minor) != 0))
292 * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
295 nd_btt->initial_offset = SZ_4K;
296 nd_btt->version_major = 1;
297 nd_btt->version_minor = 1;
298 if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
300 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
302 if ((le16_to_cpu(btt_sb->version_major) != 1) ||
303 (le16_to_cpu(btt_sb->version_minor) != 1))
308 EXPORT_SYMBOL(nd_btt_version);
310 static int __nd_btt_probe(struct nd_btt *nd_btt,
311 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
315 if (!btt_sb || !ndns || !nd_btt)
318 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
321 rc = nd_btt_version(nd_btt, ndns, btt_sb);
325 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
326 nd_btt->uuid = kmemdup(&btt_sb->uuid, sizeof(uuid_t), GFP_KERNEL);
330 nd_device_register(&nd_btt->dev);
335 int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
338 struct device *btt_dev;
339 struct btt_sb *btt_sb;
340 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
345 switch (ndns->claim_class) {
346 case NVDIMM_CCLASS_NONE:
347 case NVDIMM_CCLASS_BTT:
348 case NVDIMM_CCLASS_BTT2:
354 nvdimm_bus_lock(&ndns->dev);
355 btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
356 nvdimm_bus_unlock(&ndns->dev);
359 btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
360 rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
361 dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
363 struct nd_btt *nd_btt = to_nd_btt(btt_dev);
365 nd_detach_ndns(btt_dev, &nd_btt->ndns);
371 EXPORT_SYMBOL(nd_btt_probe);