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/genhd.h>
8 #include <linux/sizes.h>
9 #include <linux/slab.h>
16 static void nd_btt_release(struct device *dev)
18 struct nd_region *nd_region = to_nd_region(dev->parent);
19 struct nd_btt *nd_btt = to_nd_btt(dev);
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);
28 static struct device_type nd_btt_device_type = {
30 .release = nd_btt_release,
33 bool is_nd_btt(struct device *dev)
35 return dev->type == &nd_btt_device_type;
37 EXPORT_SYMBOL(is_nd_btt);
39 struct nd_btt *to_nd_btt(struct device *dev)
41 struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
43 WARN_ON(!is_nd_btt(dev));
46 EXPORT_SYMBOL(to_nd_btt);
48 static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
49 4096, 4104, 4160, 4224, 0 };
51 static ssize_t sector_size_show(struct device *dev,
52 struct device_attribute *attr, char *buf)
54 struct nd_btt *nd_btt = to_nd_btt(dev);
56 return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
59 static ssize_t sector_size_store(struct device *dev,
60 struct device_attribute *attr, const char *buf, size_t len)
62 struct nd_btt *nd_btt = to_nd_btt(dev);
67 rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
68 btt_lbasize_supported);
69 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
70 buf[len - 1] == '\n' ? "" : "\n");
71 nvdimm_bus_unlock(dev);
76 static DEVICE_ATTR_RW(sector_size);
78 static ssize_t uuid_show(struct device *dev,
79 struct device_attribute *attr, char *buf)
81 struct nd_btt *nd_btt = to_nd_btt(dev);
84 return sprintf(buf, "%pUb\n", nd_btt->uuid);
85 return sprintf(buf, "\n");
88 static ssize_t uuid_store(struct device *dev,
89 struct device_attribute *attr, const char *buf, size_t len)
91 struct nd_btt *nd_btt = to_nd_btt(dev);
95 rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
96 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
97 buf[len - 1] == '\n' ? "" : "\n");
100 return rc ? rc : len;
102 static DEVICE_ATTR_RW(uuid);
104 static ssize_t namespace_show(struct device *dev,
105 struct device_attribute *attr, char *buf)
107 struct nd_btt *nd_btt = to_nd_btt(dev);
110 nvdimm_bus_lock(dev);
111 rc = sprintf(buf, "%s\n", nd_btt->ndns
112 ? dev_name(&nd_btt->ndns->dev) : "");
113 nvdimm_bus_unlock(dev);
117 static ssize_t namespace_store(struct device *dev,
118 struct device_attribute *attr, const char *buf, size_t len)
120 struct nd_btt *nd_btt = to_nd_btt(dev);
124 nvdimm_bus_lock(dev);
125 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
126 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
127 buf[len - 1] == '\n' ? "" : "\n");
128 nvdimm_bus_unlock(dev);
133 static DEVICE_ATTR_RW(namespace);
135 static ssize_t size_show(struct device *dev,
136 struct device_attribute *attr, char *buf)
138 struct nd_btt *nd_btt = to_nd_btt(dev);
143 rc = sprintf(buf, "%llu\n", nd_btt->size);
145 /* no size to convey if the btt instance is disabled */
152 static DEVICE_ATTR_RO(size);
154 static ssize_t log_zero_flags_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
157 return sprintf(buf, "Y\n");
159 static DEVICE_ATTR_RO(log_zero_flags);
161 static struct attribute *nd_btt_attributes[] = {
162 &dev_attr_sector_size.attr,
163 &dev_attr_namespace.attr,
166 &dev_attr_log_zero_flags.attr,
170 static struct attribute_group nd_btt_attribute_group = {
171 .attrs = nd_btt_attributes,
174 static const struct attribute_group *nd_btt_attribute_groups[] = {
175 &nd_btt_attribute_group,
176 &nd_device_attribute_group,
177 &nd_numa_attribute_group,
181 static struct device *__nd_btt_create(struct nd_region *nd_region,
182 unsigned long lbasize, u8 *uuid,
183 struct nd_namespace_common *ndns)
185 struct nd_btt *nd_btt;
188 nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
192 nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
196 nd_btt->lbasize = lbasize;
198 uuid = kmemdup(uuid, 16, GFP_KERNEL);
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;
207 dev->groups = nd_btt_attribute_groups;
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));
218 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
225 struct device *nd_btt_create(struct nd_region *nd_region)
227 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
229 __nd_device_register(dev);
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
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.
243 * false for an invalid info block, true for a valid one
245 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
247 const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
250 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
253 if (!guid_is_null((guid_t *)&super->parent_uuid))
254 if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
257 checksum = le64_to_cpu(super->checksum);
259 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
261 super->checksum = cpu_to_le64(checksum);
263 /* TODO: figure out action for this */
264 if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
265 dev_info(&nd_btt->dev, "Found arena with an error flag\n");
269 EXPORT_SYMBOL(nd_btt_arena_is_valid);
271 int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
272 struct btt_sb *btt_sb)
274 if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
275 /* Probe/setup for BTT v2.0 */
276 nd_btt->initial_offset = 0;
277 nd_btt->version_major = 2;
278 nd_btt->version_minor = 0;
279 if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
281 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
283 if ((le16_to_cpu(btt_sb->version_major) != 2) ||
284 (le16_to_cpu(btt_sb->version_minor) != 0))
288 * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
291 nd_btt->initial_offset = SZ_4K;
292 nd_btt->version_major = 1;
293 nd_btt->version_minor = 1;
294 if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
296 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
298 if ((le16_to_cpu(btt_sb->version_major) != 1) ||
299 (le16_to_cpu(btt_sb->version_minor) != 1))
304 EXPORT_SYMBOL(nd_btt_version);
306 static int __nd_btt_probe(struct nd_btt *nd_btt,
307 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
311 if (!btt_sb || !ndns || !nd_btt)
314 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
317 rc = nd_btt_version(nd_btt, ndns, btt_sb);
321 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
322 nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
326 __nd_device_register(&nd_btt->dev);
331 int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
334 struct device *btt_dev;
335 struct btt_sb *btt_sb;
336 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
341 switch (ndns->claim_class) {
342 case NVDIMM_CCLASS_NONE:
343 case NVDIMM_CCLASS_BTT:
344 case NVDIMM_CCLASS_BTT2:
350 nvdimm_bus_lock(&ndns->dev);
351 btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
352 nvdimm_bus_unlock(&ndns->dev);
355 btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
356 rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
357 dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
359 struct nd_btt *nd_btt = to_nd_btt(btt_dev);
361 nd_detach_ndns(btt_dev, &nd_btt->ndns);
367 EXPORT_SYMBOL(nd_btt_probe);