2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/blkdev.h>
14 #include <linux/device.h>
15 #include <linux/genhd.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
24 static void nd_btt_release(struct device *dev)
26 struct nd_region *nd_region = to_nd_region(dev->parent);
27 struct nd_btt *nd_btt = to_nd_btt(dev);
29 dev_dbg(dev, "trace\n");
30 nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
31 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
36 static struct device_type nd_btt_device_type = {
38 .release = nd_btt_release,
41 bool is_nd_btt(struct device *dev)
43 return dev->type == &nd_btt_device_type;
45 EXPORT_SYMBOL(is_nd_btt);
47 struct nd_btt *to_nd_btt(struct device *dev)
49 struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
51 WARN_ON(!is_nd_btt(dev));
54 EXPORT_SYMBOL(to_nd_btt);
56 static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
57 4096, 4104, 4160, 4224, 0 };
59 static ssize_t sector_size_show(struct device *dev,
60 struct device_attribute *attr, char *buf)
62 struct nd_btt *nd_btt = to_nd_btt(dev);
64 return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
67 static ssize_t sector_size_store(struct device *dev,
68 struct device_attribute *attr, const char *buf, size_t len)
70 struct nd_btt *nd_btt = to_nd_btt(dev);
75 rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
76 btt_lbasize_supported);
77 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
78 buf[len - 1] == '\n' ? "" : "\n");
79 nvdimm_bus_unlock(dev);
84 static DEVICE_ATTR_RW(sector_size);
86 static ssize_t uuid_show(struct device *dev,
87 struct device_attribute *attr, char *buf)
89 struct nd_btt *nd_btt = to_nd_btt(dev);
92 return sprintf(buf, "%pUb\n", nd_btt->uuid);
93 return sprintf(buf, "\n");
96 static ssize_t uuid_store(struct device *dev,
97 struct device_attribute *attr, const char *buf, size_t len)
99 struct nd_btt *nd_btt = to_nd_btt(dev);
103 rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
104 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
105 buf[len - 1] == '\n' ? "" : "\n");
108 return rc ? rc : len;
110 static DEVICE_ATTR_RW(uuid);
112 static ssize_t namespace_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
115 struct nd_btt *nd_btt = to_nd_btt(dev);
118 nvdimm_bus_lock(dev);
119 rc = sprintf(buf, "%s\n", nd_btt->ndns
120 ? dev_name(&nd_btt->ndns->dev) : "");
121 nvdimm_bus_unlock(dev);
125 static ssize_t namespace_store(struct device *dev,
126 struct device_attribute *attr, const char *buf, size_t len)
128 struct nd_btt *nd_btt = to_nd_btt(dev);
132 nvdimm_bus_lock(dev);
133 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
134 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
135 buf[len - 1] == '\n' ? "" : "\n");
136 nvdimm_bus_unlock(dev);
141 static DEVICE_ATTR_RW(namespace);
143 static ssize_t size_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
146 struct nd_btt *nd_btt = to_nd_btt(dev);
151 rc = sprintf(buf, "%llu\n", nd_btt->size);
153 /* no size to convey if the btt instance is disabled */
160 static DEVICE_ATTR_RO(size);
162 static ssize_t log_zero_flags_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
165 return sprintf(buf, "Y\n");
167 static DEVICE_ATTR_RO(log_zero_flags);
169 static struct attribute *nd_btt_attributes[] = {
170 &dev_attr_sector_size.attr,
171 &dev_attr_namespace.attr,
174 &dev_attr_log_zero_flags.attr,
178 static struct attribute_group nd_btt_attribute_group = {
179 .attrs = nd_btt_attributes,
182 static const struct attribute_group *nd_btt_attribute_groups[] = {
183 &nd_btt_attribute_group,
184 &nd_device_attribute_group,
185 &nd_numa_attribute_group,
189 static struct device *__nd_btt_create(struct nd_region *nd_region,
190 unsigned long lbasize, u8 *uuid,
191 struct nd_namespace_common *ndns)
193 struct nd_btt *nd_btt;
196 nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
200 nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
204 nd_btt->lbasize = lbasize;
206 uuid = kmemdup(uuid, 16, GFP_KERNEL);
212 dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
213 dev->parent = &nd_region->dev;
214 dev->type = &nd_btt_device_type;
215 dev->groups = nd_btt_attribute_groups;
216 device_initialize(&nd_btt->dev);
217 if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
218 dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
219 dev_name(ndns->claim));
226 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
233 struct device *nd_btt_create(struct nd_region *nd_region)
235 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
237 __nd_device_register(dev);
242 * nd_btt_arena_is_valid - check if the metadata layout is valid
243 * @nd_btt: device with BTT geometry and backing device info
244 * @super: pointer to the arena's info block being tested
246 * Check consistency of the btt info block with itself by validating
247 * the checksum, and with the parent namespace by verifying the
248 * parent_uuid contained in the info block with the one supplied in.
251 * false for an invalid info block, true for a valid one
253 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
255 const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
258 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
261 if (!guid_is_null((guid_t *)&super->parent_uuid))
262 if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
265 checksum = le64_to_cpu(super->checksum);
267 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
269 super->checksum = cpu_to_le64(checksum);
271 /* TODO: figure out action for this */
272 if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
273 dev_info(&nd_btt->dev, "Found arena with an error flag\n");
277 EXPORT_SYMBOL(nd_btt_arena_is_valid);
279 int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
280 struct btt_sb *btt_sb)
282 if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
283 /* Probe/setup for BTT v2.0 */
284 nd_btt->initial_offset = 0;
285 nd_btt->version_major = 2;
286 nd_btt->version_minor = 0;
287 if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
289 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
291 if ((le16_to_cpu(btt_sb->version_major) != 2) ||
292 (le16_to_cpu(btt_sb->version_minor) != 0))
296 * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
299 nd_btt->initial_offset = SZ_4K;
300 nd_btt->version_major = 1;
301 nd_btt->version_minor = 1;
302 if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
304 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
306 if ((le16_to_cpu(btt_sb->version_major) != 1) ||
307 (le16_to_cpu(btt_sb->version_minor) != 1))
312 EXPORT_SYMBOL(nd_btt_version);
314 static int __nd_btt_probe(struct nd_btt *nd_btt,
315 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
319 if (!btt_sb || !ndns || !nd_btt)
322 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
325 rc = nd_btt_version(nd_btt, ndns, btt_sb);
329 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
330 nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
334 __nd_device_register(&nd_btt->dev);
339 int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
342 struct device *btt_dev;
343 struct btt_sb *btt_sb;
344 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
349 switch (ndns->claim_class) {
350 case NVDIMM_CCLASS_NONE:
351 case NVDIMM_CCLASS_BTT:
352 case NVDIMM_CCLASS_BTT2:
358 nvdimm_bus_lock(&ndns->dev);
359 btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
360 nvdimm_bus_unlock(&ndns->dev);
363 btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
364 rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
365 dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
367 struct nd_btt *nd_btt = to_nd_btt(btt_dev);
369 nd_detach_ndns(btt_dev, &nd_btt->ndns);
375 EXPORT_SYMBOL(nd_btt_probe);