Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.git] / drivers / nvdimm / btt_devs.c
CommitLineData
8c2f7e86
DW
1/*
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
8c2f7e86
DW
24static 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
426824d6 29 dev_dbg(dev, "trace\n");
e1455744 30 nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
8c2f7e86
DW
31 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
41cd8b70
VV
56static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
57 4096, 4104, 4160, 4224, 0 };
8c2f7e86
DW
58
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
b2c48f9f 64 return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
8c2f7e86
DW
65}
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);
b2c48f9f 75 rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
8c2f7e86 76 btt_lbasize_supported);
426824d6
DW
77 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
78 buf[len - 1] == '\n' ? "" : "\n");
8c2f7e86
DW
79 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);
426824d6
DW
104 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
105 buf[len - 1] == '\n' ? "" : "\n");
8c2f7e86
DW
106 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
8c2f7e86
DW
125static ssize_t namespace_store(struct device *dev,
126 struct device_attribute *attr, const char *buf, size_t len)
127{
e1455744 128 struct nd_btt *nd_btt = to_nd_btt(dev);
8c2f7e86
DW
129 ssize_t rc;
130
8c2f7e86 131 device_lock(dev);
4be9c1fc 132 nvdimm_bus_lock(dev);
e1455744 133 rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
426824d6
DW
134 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
135 buf[len - 1] == '\n' ? "" : "\n");
8c2f7e86 136 nvdimm_bus_unlock(dev);
4be9c1fc 137 device_unlock(dev);
8c2f7e86
DW
138
139 return rc;
140}
141static DEVICE_ATTR_RW(namespace);
142
abe8b4e3
VV
143static 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
8c2f7e86
DW
162static struct attribute *nd_btt_attributes[] = {
163 &dev_attr_sector_size.attr,
164 &dev_attr_namespace.attr,
165 &dev_attr_uuid.attr,
abe8b4e3 166 &dev_attr_size.attr,
8c2f7e86
DW
167 NULL,
168};
169
170static struct attribute_group nd_btt_attribute_group = {
171 .attrs = nd_btt_attributes,
172};
173
174static const struct attribute_group *nd_btt_attribute_groups[] = {
175 &nd_btt_attribute_group,
176 &nd_device_attribute_group,
74ae66c3 177 &nd_numa_attribute_group,
8c2f7e86
DW
178 NULL,
179};
180
181static struct device *__nd_btt_create(struct nd_region *nd_region,
182 unsigned long lbasize, u8 *uuid,
183 struct nd_namespace_common *ndns)
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);
193 if (nd_btt->id < 0) {
194 kfree(nd_btt);
195 return NULL;
196 }
197
198 nd_btt->lbasize = lbasize;
199 if (uuid)
200 uuid = kmemdup(uuid, 16, GFP_KERNEL);
201 nd_btt->uuid = uuid;
202 dev = &nd_btt->dev;
203 dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
204 dev->parent = &nd_region->dev;
205 dev->type = &nd_btt_device_type;
206 dev->groups = nd_btt_attribute_groups;
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;
215}
216
217struct device *nd_btt_create(struct nd_region *nd_region)
218{
219 struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
220
d4c5725d 221 __nd_device_register(dev);
8c2f7e86
DW
222 return dev;
223}
224
ab45e763
VV
225/**
226 * nd_btt_arena_is_valid - check if the metadata layout is valid
227 * @nd_btt: device with BTT geometry and backing device info
228 * @super: pointer to the arena's info block being tested
229 *
230 * Check consistency of the btt info block with itself by validating
6ec68954
VV
231 * the checksum, and with the parent namespace by verifying the
232 * parent_uuid contained in the info block with the one supplied in.
ab45e763
VV
233 *
234 * Returns:
235 * false for an invalid info block, true for a valid one
236 */
237bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
238{
6ec68954 239 const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
ab45e763
VV
240 u64 checksum;
241
242 if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
243 return false;
244
ef40dda5 245 if (!guid_is_null((guid_t *)&super->parent_uuid))
6ec68954
VV
246 if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
247 return false;
248
ab45e763
VV
249 checksum = le64_to_cpu(super->checksum);
250 super->checksum = 0;
e1455744 251 if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
ab45e763
VV
252 return false;
253 super->checksum = cpu_to_le64(checksum);
254
255 /* TODO: figure out action for this */
256 if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
257 dev_info(&nd_btt->dev, "Found arena with an error flag\n");
258
259 return true;
260}
261EXPORT_SYMBOL(nd_btt_arena_is_valid);
262
14e49454
VV
263int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
264 struct btt_sb *btt_sb)
265{
266 if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
267 /* Probe/setup for BTT v2.0 */
268 nd_btt->initial_offset = 0;
269 nd_btt->version_major = 2;
270 nd_btt->version_minor = 0;
271 if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
272 return -ENXIO;
273 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
274 return -ENODEV;
275 if ((le16_to_cpu(btt_sb->version_major) != 2) ||
276 (le16_to_cpu(btt_sb->version_minor) != 0))
277 return -ENODEV;
278 } else {
279 /*
280 * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
281 * NVDIMM_CCLASS_BTT)
282 */
283 nd_btt->initial_offset = SZ_4K;
284 nd_btt->version_major = 1;
285 nd_btt->version_minor = 1;
286 if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
287 return -ENXIO;
288 if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
289 return -ENODEV;
290 if ((le16_to_cpu(btt_sb->version_major) != 1) ||
291 (le16_to_cpu(btt_sb->version_minor) != 1))
292 return -ENODEV;
293 }
294 return 0;
295}
296EXPORT_SYMBOL(nd_btt_version);
297
8c2f7e86
DW
298static int __nd_btt_probe(struct nd_btt *nd_btt,
299 struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
300{
14e49454
VV
301 int rc;
302
8c2f7e86
DW
303 if (!btt_sb || !ndns || !nd_btt)
304 return -ENODEV;
305
8c2f7e86
DW
306 if (nvdimm_namespace_capacity(ndns) < SZ_16M)
307 return -ENXIO;
308
14e49454
VV
309 rc = nd_btt_version(nd_btt, ndns, btt_sb);
310 if (rc < 0)
311 return rc;
8c2f7e86
DW
312
313 nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
314 nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
315 if (!nd_btt->uuid)
316 return -ENOMEM;
317
318 __nd_device_register(&nd_btt->dev);
319
320 return 0;
321}
322
200c79da 323int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
8c2f7e86
DW
324{
325 int rc;
e32bc729 326 struct device *btt_dev;
8c2f7e86
DW
327 struct btt_sb *btt_sb;
328 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
329
330 if (ndns->force_raw)
331 return -ENODEV;
332
b3fde74e
DW
333 switch (ndns->claim_class) {
334 case NVDIMM_CCLASS_NONE:
335 case NVDIMM_CCLASS_BTT:
14e49454 336 case NVDIMM_CCLASS_BTT2:
b3fde74e
DW
337 break;
338 default:
339 return -ENODEV;
340 }
341
8c2f7e86 342 nvdimm_bus_lock(&ndns->dev);
e32bc729 343 btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
8c2f7e86 344 nvdimm_bus_unlock(&ndns->dev);
e32bc729 345 if (!btt_dev)
8c2f7e86 346 return -ENOMEM;
e32bc729
DW
347 btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
348 rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
426824d6 349 dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
8c2f7e86 350 if (rc < 0) {
e32bc729 351 struct nd_btt *nd_btt = to_nd_btt(btt_dev);
e1455744 352
452bae0a 353 nd_detach_ndns(btt_dev, &nd_btt->ndns);
e32bc729 354 put_device(btt_dev);
8c2f7e86
DW
355 }
356
357 return rc;
358}
359EXPORT_SYMBOL(nd_btt_probe);