blob: 9486acc08402db3a17079c0ec2589ce445bb23d2 [file] [log] [blame]
Dan Williams8c2f7e82015-06-25 04:20:04 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
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.
7 *
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.
12 */
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>
18#include <linux/fs.h>
19#include <linux/mm.h>
20#include "nd-core.h"
21#include "btt.h"
22#include "nd.h"
23
Dan Williams8c2f7e82015-06-25 04:20:04 -040024static void nd_btt_release(struct device *dev)
25{
26 struct nd_region *nd_region = to_nd_region(dev->parent);
27 struct nd_btt *nd_btt = to_nd_btt(dev);
28
Dan Williams426824d2018-03-05 16:39:31 -080029 dev_dbg(dev, "trace\n");
Dan Williamse1455742015-07-30 17:57:47 -040030 nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
Dan Williams8c2f7e82015-06-25 04:20:04 -040031 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
32 kfree(nd_btt->uuid);
33 kfree(nd_btt);
34}
35
36static struct device_type nd_btt_device_type = {
37 .name = "nd_btt",
38 .release = nd_btt_release,
39};
40
41bool is_nd_btt(struct device *dev)
42{
43 return dev->type == &nd_btt_device_type;
44}
45EXPORT_SYMBOL(is_nd_btt);
46
47struct nd_btt *to_nd_btt(struct device *dev)
48{
49 struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
50
51 WARN_ON(!is_nd_btt(dev));
52 return nd_btt;
53}
54EXPORT_SYMBOL(to_nd_btt);
55
Vishal Verma41cd8b72015-06-25 04:21:52 -040056static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
57 4096, 4104, 4160, 4224, 0 };
Dan Williams8c2f7e82015-06-25 04:20:04 -040058
59static ssize_t sector_size_show(struct device *dev,
60 struct device_attribute *attr, char *buf)
61{
62 struct nd_btt *nd_btt = to_nd_btt(dev);
63
Dan Williamsb2c48f92017-08-11 17:36:54 -070064 return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
Dan Williams8c2f7e82015-06-25 04:20:04 -040065}
66
67static ssize_t sector_size_store(struct device *dev,
68 struct device_attribute *attr, const char *buf, size_t len)
69{
70 struct nd_btt *nd_btt = to_nd_btt(dev);
71 ssize_t rc;
72
73 device_lock(dev);
74 nvdimm_bus_lock(dev);
Dan Williamsb2c48f92017-08-11 17:36:54 -070075 rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
Dan Williams8c2f7e82015-06-25 04:20:04 -040076 btt_lbasize_supported);
Dan Williams426824d2018-03-05 16:39:31 -080077 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
78 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -040079 nvdimm_bus_unlock(dev);
80 device_unlock(dev);
81
82 return rc ? rc : len;
83}
84static DEVICE_ATTR_RW(sector_size);
85
86static ssize_t uuid_show(struct device *dev,
87 struct device_attribute *attr, char *buf)
88{
89 struct nd_btt *nd_btt = to_nd_btt(dev);
90
91 if (nd_btt->uuid)
92 return sprintf(buf, "%pUb\n", nd_btt->uuid);
93 return sprintf(buf, "\n");
94}
95
96static ssize_t uuid_store(struct device *dev,
97 struct device_attribute *attr, const char *buf, size_t len)
98{
99 struct nd_btt *nd_btt = to_nd_btt(dev);
100 ssize_t rc;
101
102 device_lock(dev);
103 rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800104 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
105 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400106 device_unlock(dev);
107
108 return rc ? rc : len;
109}
110static DEVICE_ATTR_RW(uuid);
111
112static ssize_t namespace_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
114{
115 struct nd_btt *nd_btt = to_nd_btt(dev);
116 ssize_t rc;
117
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);
122 return rc;
123}
124
Dan Williams8c2f7e82015-06-25 04:20:04 -0400125static ssize_t namespace_store(struct device *dev,
126 struct device_attribute *attr, const char *buf, size_t len)
127{
Dan Williamse1455742015-07-30 17:57:47 -0400128 struct nd_btt *nd_btt = to_nd_btt(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400129 ssize_t rc;
130
Dan Williams8c2f7e82015-06-25 04:20:04 -0400131 device_lock(dev);
Axel Lin4be9c1f2015-09-16 21:24:47 +0800132 nvdimm_bus_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400133 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
Dan Williams426824d2018-03-05 16:39:31 -0800134 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
135 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400136 nvdimm_bus_unlock(dev);
Axel Lin4be9c1f2015-09-16 21:24:47 +0800137 device_unlock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400138
139 return rc;
140}
141static DEVICE_ATTR_RW(namespace);
142
Vishal Vermaabe8b4e2016-07-27 16:38:59 -0600143static ssize_t size_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
145{
146 struct nd_btt *nd_btt = to_nd_btt(dev);
147 ssize_t rc;
148
149 device_lock(dev);
150 if (dev->driver)
151 rc = sprintf(buf, "%llu\n", nd_btt->size);
152 else {
153 /* no size to convey if the btt instance is disabled */
154 rc = -ENXIO;
155 }
156 device_unlock(dev);
157
158 return rc;
159}
160static DEVICE_ATTR_RO(size);
161
Vishal Verma9dedc732019-02-27 17:06:27 -0700162static ssize_t log_zero_flags_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
164{
165 return sprintf(buf, "Y\n");
166}
167static DEVICE_ATTR_RO(log_zero_flags);
168
Dan Williams8c2f7e82015-06-25 04:20:04 -0400169static struct attribute *nd_btt_attributes[] = {
170 &dev_attr_sector_size.attr,
171 &dev_attr_namespace.attr,
172 &dev_attr_uuid.attr,
Vishal Vermaabe8b4e2016-07-27 16:38:59 -0600173 &dev_attr_size.attr,
Vishal Verma9dedc732019-02-27 17:06:27 -0700174 &dev_attr_log_zero_flags.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -0400175 NULL,
176};
177
178static struct attribute_group nd_btt_attribute_group = {
179 .attrs = nd_btt_attributes,
180};
181
182static const struct attribute_group *nd_btt_attribute_groups[] = {
183 &nd_btt_attribute_group,
184 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -0600185 &nd_numa_attribute_group,
Dan Williams8c2f7e82015-06-25 04:20:04 -0400186 NULL,
187};
188
189static struct device *__nd_btt_create(struct nd_region *nd_region,
190 unsigned long lbasize, u8 *uuid,
191 struct nd_namespace_common *ndns)
192{
193 struct nd_btt *nd_btt;
194 struct device *dev;
195
196 nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
197 if (!nd_btt)
198 return NULL;
199
200 nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
Aditya Pakki486fa922019-03-25 16:55:27 -0500201 if (nd_btt->id < 0)
202 goto out_nd_btt;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400203
204 nd_btt->lbasize = lbasize;
Aditya Pakki486fa922019-03-25 16:55:27 -0500205 if (uuid) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400206 uuid = kmemdup(uuid, 16, GFP_KERNEL);
Aditya Pakki486fa922019-03-25 16:55:27 -0500207 if (!uuid)
208 goto out_put_id;
209 }
Dan Williams8c2f7e82015-06-25 04:20:04 -0400210 nd_btt->uuid = uuid;
211 dev = &nd_btt->dev;
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);
Dan Williamse1455742015-07-30 17:57:47 -0400217 if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
Dan Williams426824d2018-03-05 16:39:31 -0800218 dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
219 dev_name(ndns->claim));
Dan Williams8c2f7e82015-06-25 04:20:04 -0400220 put_device(dev);
221 return NULL;
222 }
223 return dev;
Aditya Pakki486fa922019-03-25 16:55:27 -0500224
225out_put_id:
226 ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
227
228out_nd_btt:
229 kfree(nd_btt);
230 return NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400231}
232
233struct device *nd_btt_create(struct nd_region *nd_region)
234{
235 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
236
Markus Elfringd4c57252016-07-24 11:05:34 +0200237 __nd_device_register(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400238 return dev;
239}
240
Vishal Vermaab45e762015-07-29 14:58:08 -0600241/**
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
245 *
246 * Check consistency of the btt info block with itself by validating
Vishal Verma6ec68952015-07-29 14:58:09 -0600247 * the checksum, and with the parent namespace by verifying the
248 * parent_uuid contained in the info block with the one supplied in.
Vishal Vermaab45e762015-07-29 14:58:08 -0600249 *
250 * Returns:
251 * false for an invalid info block, true for a valid one
252 */
253bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
254{
Vishal Verma6ec68952015-07-29 14:58:09 -0600255 const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
Vishal Vermaab45e762015-07-29 14:58:08 -0600256 u64 checksum;
257
258 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
259 return false;
260
Christoph Hellwigef40dda2017-05-11 09:01:42 +0200261 if (!guid_is_null((guid_t *)&super->parent_uuid))
Vishal Verma6ec68952015-07-29 14:58:09 -0600262 if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
263 return false;
264
Vishal Vermaab45e762015-07-29 14:58:08 -0600265 checksum = le64_to_cpu(super->checksum);
266 super->checksum = 0;
Dan Williamse1455742015-07-30 17:57:47 -0400267 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
Vishal Vermaab45e762015-07-29 14:58:08 -0600268 return false;
269 super->checksum = cpu_to_le64(checksum);
270
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");
274
275 return true;
276}
277EXPORT_SYMBOL(nd_btt_arena_is_valid);
278
Vishal Verma14e49452017-06-28 14:25:00 -0600279int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
280 struct btt_sb *btt_sb)
281{
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))
288 return -ENXIO;
289 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
290 return -ENODEV;
291 if ((le16_to_cpu(btt_sb->version_major) != 2) ||
292 (le16_to_cpu(btt_sb->version_minor) != 0))
293 return -ENODEV;
294 } else {
295 /*
296 * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
297 * NVDIMM_CCLASS_BTT)
298 */
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))
303 return -ENXIO;
304 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
305 return -ENODEV;
306 if ((le16_to_cpu(btt_sb->version_major) != 1) ||
307 (le16_to_cpu(btt_sb->version_minor) != 1))
308 return -ENODEV;
309 }
310 return 0;
311}
312EXPORT_SYMBOL(nd_btt_version);
313
Dan Williams8c2f7e82015-06-25 04:20:04 -0400314static int __nd_btt_probe(struct nd_btt *nd_btt,
315 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
316{
Vishal Verma14e49452017-06-28 14:25:00 -0600317 int rc;
318
Dan Williams8c2f7e82015-06-25 04:20:04 -0400319 if (!btt_sb || !ndns || !nd_btt)
320 return -ENODEV;
321
Dan Williams8c2f7e82015-06-25 04:20:04 -0400322 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
323 return -ENXIO;
324
Vishal Verma14e49452017-06-28 14:25:00 -0600325 rc = nd_btt_version(nd_btt, ndns, btt_sb);
326 if (rc < 0)
327 return rc;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400328
Dan Williams8c2f7e82015-06-25 04:20:04 -0400329 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
330 nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
331 if (!nd_btt->uuid)
332 return -ENOMEM;
333
334 __nd_device_register(&nd_btt->dev);
335
336 return 0;
337}
338
Dan Williams200c79d2016-03-22 00:22:16 -0700339int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400340{
341 int rc;
Dan Williamse32bc722016-03-17 18:23:09 -0700342 struct device *btt_dev;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400343 struct btt_sb *btt_sb;
344 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
345
346 if (ndns->force_raw)
347 return -ENODEV;
348
Dan Williamsb3fde742017-06-04 10:18:39 +0900349 switch (ndns->claim_class) {
350 case NVDIMM_CCLASS_NONE:
351 case NVDIMM_CCLASS_BTT:
Vishal Verma14e49452017-06-28 14:25:00 -0600352 case NVDIMM_CCLASS_BTT2:
Dan Williamsb3fde742017-06-04 10:18:39 +0900353 break;
354 default:
355 return -ENODEV;
356 }
357
Dan Williams8c2f7e82015-06-25 04:20:04 -0400358 nvdimm_bus_lock(&ndns->dev);
Dan Williamse32bc722016-03-17 18:23:09 -0700359 btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400360 nvdimm_bus_unlock(&ndns->dev);
Dan Williamse32bc722016-03-17 18:23:09 -0700361 if (!btt_dev)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400362 return -ENOMEM;
Dan Williamse32bc722016-03-17 18:23:09 -0700363 btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
364 rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
Dan Williams426824d2018-03-05 16:39:31 -0800365 dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400366 if (rc < 0) {
Dan Williamse32bc722016-03-17 18:23:09 -0700367 struct nd_btt *nd_btt = to_nd_btt(btt_dev);
Dan Williamse1455742015-07-30 17:57:47 -0400368
Dan Williams452bae02017-04-28 22:05:14 -0700369 nd_detach_ndns(btt_dev, &nd_btt->ndns);
Dan Williamse32bc722016-03-17 18:23:09 -0700370 put_device(btt_dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400371 }
372
373 return rc;
374}
375EXPORT_SYMBOL(nd_btt_probe);