6bbd0a36994a46c667f7e2487eb1b2206b8e79a6
[cascardo/linux.git] / drivers / nvdimm / claim.c
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/device.h>
14 #include <linux/sizes.h>
15 #include <linux/pmem.h>
16 #include "nd-core.h"
17 #include "pfn.h"
18 #include "btt.h"
19 #include "nd.h"
20
21 void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns)
22 {
23         struct nd_namespace_common *ndns = *_ndns;
24
25         dev_WARN_ONCE(dev, !mutex_is_locked(&ndns->dev.mutex)
26                         || ndns->claim != dev,
27                         "%s: invalid claim\n", __func__);
28         ndns->claim = NULL;
29         *_ndns = NULL;
30         put_device(&ndns->dev);
31 }
32
33 void nd_detach_ndns(struct device *dev,
34                 struct nd_namespace_common **_ndns)
35 {
36         struct nd_namespace_common *ndns = *_ndns;
37
38         if (!ndns)
39                 return;
40         get_device(&ndns->dev);
41         device_lock(&ndns->dev);
42         __nd_detach_ndns(dev, _ndns);
43         device_unlock(&ndns->dev);
44         put_device(&ndns->dev);
45 }
46
47 bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
48                 struct nd_namespace_common **_ndns)
49 {
50         if (attach->claim)
51                 return false;
52         dev_WARN_ONCE(dev, !mutex_is_locked(&attach->dev.mutex)
53                         || *_ndns,
54                         "%s: invalid claim\n", __func__);
55         attach->claim = dev;
56         *_ndns = attach;
57         get_device(&attach->dev);
58         return true;
59 }
60
61 bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
62                 struct nd_namespace_common **_ndns)
63 {
64         bool claimed;
65
66         device_lock(&attach->dev);
67         claimed = __nd_attach_ndns(dev, attach, _ndns);
68         device_unlock(&attach->dev);
69         return claimed;
70 }
71
72 static int namespace_match(struct device *dev, void *data)
73 {
74         char *name = data;
75
76         return strcmp(name, dev_name(dev)) == 0;
77 }
78
79 static bool is_idle(struct device *dev, struct nd_namespace_common *ndns)
80 {
81         struct nd_region *nd_region = to_nd_region(dev->parent);
82         struct device *seed = NULL;
83
84         if (is_nd_btt(dev))
85                 seed = nd_region->btt_seed;
86         else if (is_nd_pfn(dev))
87                 seed = nd_region->pfn_seed;
88
89         if (seed == dev || ndns || dev->driver)
90                 return false;
91         return true;
92 }
93
94 static void nd_detach_and_reset(struct device *dev,
95                 struct nd_namespace_common **_ndns)
96 {
97         /* detach the namespace and destroy / reset the device */
98         nd_detach_ndns(dev, _ndns);
99         if (is_idle(dev, *_ndns)) {
100                 nd_device_unregister(dev, ND_ASYNC);
101         } else if (is_nd_btt(dev)) {
102                 struct nd_btt *nd_btt = to_nd_btt(dev);
103
104                 nd_btt->lbasize = 0;
105                 kfree(nd_btt->uuid);
106                 nd_btt->uuid = NULL;
107         } else if (is_nd_pfn(dev)) {
108                 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
109
110                 kfree(nd_pfn->uuid);
111                 nd_pfn->uuid = NULL;
112                 nd_pfn->mode = PFN_MODE_NONE;
113         }
114 }
115
116 ssize_t nd_namespace_store(struct device *dev,
117                 struct nd_namespace_common **_ndns, const char *buf,
118                 size_t len)
119 {
120         struct nd_namespace_common *ndns;
121         struct device *found;
122         char *name;
123
124         if (dev->driver) {
125                 dev_dbg(dev, "%s: -EBUSY\n", __func__);
126                 return -EBUSY;
127         }
128
129         name = kstrndup(buf, len, GFP_KERNEL);
130         if (!name)
131                 return -ENOMEM;
132         strim(name);
133
134         if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
135                 /* pass */;
136         else {
137                 len = -EINVAL;
138                 goto out;
139         }
140
141         ndns = *_ndns;
142         if (strcmp(name, "") == 0) {
143                 nd_detach_and_reset(dev, _ndns);
144                 goto out;
145         } else if (ndns) {
146                 dev_dbg(dev, "namespace already set to: %s\n",
147                                 dev_name(&ndns->dev));
148                 len = -EBUSY;
149                 goto out;
150         }
151
152         found = device_find_child(dev->parent, name, namespace_match);
153         if (!found) {
154                 dev_dbg(dev, "'%s' not found under %s\n", name,
155                                 dev_name(dev->parent));
156                 len = -ENODEV;
157                 goto out;
158         }
159
160         ndns = to_ndns(found);
161         if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
162                 dev_dbg(dev, "%s too small to host\n", name);
163                 len = -ENXIO;
164                 goto out_attach;
165         }
166
167         WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
168         if (!nd_attach_ndns(dev, ndns, _ndns)) {
169                 dev_dbg(dev, "%s already claimed\n",
170                                 dev_name(&ndns->dev));
171                 len = -EBUSY;
172         }
173
174  out_attach:
175         put_device(&ndns->dev); /* from device_find_child */
176  out:
177         kfree(name);
178         return len;
179 }
180
181 /*
182  * nd_sb_checksum: compute checksum for a generic info block
183  *
184  * Returns a fletcher64 checksum of everything in the given info block
185  * except the last field (since that's where the checksum lives).
186  */
187 u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
188 {
189         u64 sum;
190         __le64 sum_save;
191
192         BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
193         BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
194         BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
195
196         sum_save = nd_gen_sb->checksum;
197         nd_gen_sb->checksum = 0;
198         sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
199         nd_gen_sb->checksum = sum_save;
200         return sum;
201 }
202 EXPORT_SYMBOL(nd_sb_checksum);
203
204 static int nsio_rw_bytes(struct nd_namespace_common *ndns,
205                 resource_size_t offset, void *buf, size_t size, int rw)
206 {
207         struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
208
209         if (unlikely(offset + size > nsio->size)) {
210                 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
211                 return -EFAULT;
212         }
213
214         if (rw == READ) {
215                 unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
216
217                 if (unlikely(is_bad_pmem(&nsio->bb, offset / 512, sz_align)))
218                         return -EIO;
219                 return memcpy_from_pmem(buf, nsio->addr + offset, size);
220         } else {
221                 memcpy_to_pmem(nsio->addr + offset, buf, size);
222                 wmb_pmem();
223         }
224
225         return 0;
226 }
227
228 int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio)
229 {
230         struct resource *res = &nsio->res;
231         struct nd_namespace_common *ndns = &nsio->common;
232
233         nsio->size = resource_size(res);
234         if (!devm_request_mem_region(dev, res->start, resource_size(res),
235                                 dev_name(dev))) {
236                 dev_warn(dev, "could not reserve region %pR\n", res);
237                 return -EBUSY;
238         }
239
240         ndns->rw_bytes = nsio_rw_bytes;
241         if (devm_init_badblocks(dev, &nsio->bb))
242                 return -ENOMEM;
243         nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
244                         &nsio->res);
245
246         nsio->addr = devm_memremap(dev, res->start, resource_size(res),
247                         ARCH_MEMREMAP_PMEM);
248         if (IS_ERR(nsio->addr))
249                 return PTR_ERR(nsio->addr);
250         return 0;
251 }
252 EXPORT_SYMBOL_GPL(devm_nsio_enable);
253
254 void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio)
255 {
256         struct resource *res = &nsio->res;
257
258         devm_memunmap(dev, nsio->addr);
259         devm_exit_badblocks(dev, &nsio->bb);
260         devm_release_mem_region(dev, res->start, resource_size(res));
261 }
262 EXPORT_SYMBOL_GPL(devm_nsio_disable);