]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/nvdimm/pfn_devs.c
f9b674bc49db291f27b8504e803d2f0a47aa0740
[linux-beck.git] / drivers / nvdimm / pfn_devs.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/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 "pfn.h"
22 #include "nd.h"
23
24 static void nd_pfn_release(struct device *dev)
25 {
26         struct nd_region *nd_region = to_nd_region(dev->parent);
27         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
28
29         dev_dbg(dev, "%s\n", __func__);
30         nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
31         ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
32         kfree(nd_pfn->uuid);
33         kfree(nd_pfn);
34 }
35
36 static struct device_type nd_pfn_device_type = {
37         .name = "nd_pfn",
38         .release = nd_pfn_release,
39 };
40
41 bool is_nd_pfn(struct device *dev)
42 {
43         return dev ? dev->type == &nd_pfn_device_type : false;
44 }
45 EXPORT_SYMBOL(is_nd_pfn);
46
47 struct nd_pfn *to_nd_pfn(struct device *dev)
48 {
49         struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
50
51         WARN_ON(!is_nd_pfn(dev));
52         return nd_pfn;
53 }
54 EXPORT_SYMBOL(to_nd_pfn);
55
56 static ssize_t mode_show(struct device *dev,
57                 struct device_attribute *attr, char *buf)
58 {
59         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
60
61         switch (nd_pfn->mode) {
62         case PFN_MODE_RAM:
63                 return sprintf(buf, "ram\n");
64         case PFN_MODE_PMEM:
65                 return sprintf(buf, "pmem\n");
66         default:
67                 return sprintf(buf, "none\n");
68         }
69 }
70
71 static ssize_t mode_store(struct device *dev,
72                 struct device_attribute *attr, const char *buf, size_t len)
73 {
74         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
75         ssize_t rc = 0;
76
77         device_lock(dev);
78         nvdimm_bus_lock(dev);
79         if (dev->driver)
80                 rc = -EBUSY;
81         else {
82                 size_t n = len - 1;
83
84                 if (strncmp(buf, "pmem\n", n) == 0
85                                 || strncmp(buf, "pmem", n) == 0) {
86                         /* TODO: allocate from PMEM support */
87                         rc = -ENOTTY;
88                 } else if (strncmp(buf, "ram\n", n) == 0
89                                 || strncmp(buf, "ram", n) == 0)
90                         nd_pfn->mode = PFN_MODE_RAM;
91                 else if (strncmp(buf, "none\n", n) == 0
92                                 || strncmp(buf, "none", n) == 0)
93                         nd_pfn->mode = PFN_MODE_NONE;
94                 else
95                         rc = -EINVAL;
96         }
97         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
98                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
99         nvdimm_bus_unlock(dev);
100         device_unlock(dev);
101
102         return rc ? rc : len;
103 }
104 static DEVICE_ATTR_RW(mode);
105
106 static ssize_t align_show(struct device *dev,
107                 struct device_attribute *attr, char *buf)
108 {
109         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
110
111         return sprintf(buf, "%lx\n", nd_pfn->align);
112 }
113
114 static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
115 {
116         unsigned long val;
117         int rc;
118
119         rc = kstrtoul(buf, 0, &val);
120         if (rc)
121                 return rc;
122
123         if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G)
124                 return -EINVAL;
125
126         if (nd_pfn->dev.driver)
127                 return -EBUSY;
128         else
129                 nd_pfn->align = val;
130
131         return 0;
132 }
133
134 static ssize_t align_store(struct device *dev,
135                 struct device_attribute *attr, const char *buf, size_t len)
136 {
137         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
138         ssize_t rc;
139
140         device_lock(dev);
141         nvdimm_bus_lock(dev);
142         rc = __align_store(nd_pfn, buf);
143         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
144                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
145         nvdimm_bus_unlock(dev);
146         device_unlock(dev);
147
148         return rc ? rc : len;
149 }
150 static DEVICE_ATTR_RW(align);
151
152 static ssize_t uuid_show(struct device *dev,
153                 struct device_attribute *attr, char *buf)
154 {
155         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
156
157         if (nd_pfn->uuid)
158                 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
159         return sprintf(buf, "\n");
160 }
161
162 static ssize_t uuid_store(struct device *dev,
163                 struct device_attribute *attr, const char *buf, size_t len)
164 {
165         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
166         ssize_t rc;
167
168         device_lock(dev);
169         rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
170         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
171                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
172         device_unlock(dev);
173
174         return rc ? rc : len;
175 }
176 static DEVICE_ATTR_RW(uuid);
177
178 static ssize_t namespace_show(struct device *dev,
179                 struct device_attribute *attr, char *buf)
180 {
181         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
182         ssize_t rc;
183
184         nvdimm_bus_lock(dev);
185         rc = sprintf(buf, "%s\n", nd_pfn->ndns
186                         ? dev_name(&nd_pfn->ndns->dev) : "");
187         nvdimm_bus_unlock(dev);
188         return rc;
189 }
190
191 static ssize_t namespace_store(struct device *dev,
192                 struct device_attribute *attr, const char *buf, size_t len)
193 {
194         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
195         ssize_t rc;
196
197         device_lock(dev);
198         nvdimm_bus_lock(dev);
199         rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
200         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
201                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
202         nvdimm_bus_unlock(dev);
203         device_unlock(dev);
204
205         return rc;
206 }
207 static DEVICE_ATTR_RW(namespace);
208
209 static struct attribute *nd_pfn_attributes[] = {
210         &dev_attr_mode.attr,
211         &dev_attr_namespace.attr,
212         &dev_attr_uuid.attr,
213         &dev_attr_align.attr,
214         NULL,
215 };
216
217 static struct attribute_group nd_pfn_attribute_group = {
218         .attrs = nd_pfn_attributes,
219 };
220
221 static const struct attribute_group *nd_pfn_attribute_groups[] = {
222         &nd_pfn_attribute_group,
223         &nd_device_attribute_group,
224         &nd_numa_attribute_group,
225         NULL,
226 };
227
228 static struct device *__nd_pfn_create(struct nd_region *nd_region,
229                 struct nd_namespace_common *ndns)
230 {
231         struct nd_pfn *nd_pfn;
232         struct device *dev;
233
234         /* we can only create pages for contiguous ranged of pmem */
235         if (!is_nd_pmem(&nd_region->dev))
236                 return NULL;
237
238         nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
239         if (!nd_pfn)
240                 return NULL;
241
242         nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
243         if (nd_pfn->id < 0) {
244                 kfree(nd_pfn);
245                 return NULL;
246         }
247
248         nd_pfn->mode = PFN_MODE_NONE;
249         nd_pfn->align = HPAGE_SIZE;
250         dev = &nd_pfn->dev;
251         dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
252         dev->parent = &nd_region->dev;
253         dev->type = &nd_pfn_device_type;
254         dev->groups = nd_pfn_attribute_groups;
255         device_initialize(&nd_pfn->dev);
256         if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
257                 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
258                                 __func__, dev_name(ndns->claim));
259                 put_device(dev);
260                 return NULL;
261         }
262         return dev;
263 }
264
265 struct device *nd_pfn_create(struct nd_region *nd_region)
266 {
267         struct device *dev = __nd_pfn_create(nd_region, NULL);
268
269         if (dev)
270                 __nd_device_register(dev);
271         return dev;
272 }
273
274 int nd_pfn_validate(struct nd_pfn *nd_pfn)
275 {
276         u64 checksum, offset;
277         struct nd_namespace_io *nsio;
278         struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
279         struct nd_namespace_common *ndns = nd_pfn->ndns;
280         const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
281
282         if (!pfn_sb || !ndns)
283                 return -ENODEV;
284
285         if (!is_nd_pmem(nd_pfn->dev.parent))
286                 return -ENODEV;
287
288         if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
289                 return -ENXIO;
290
291         if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
292                 return -ENODEV;
293
294         checksum = le64_to_cpu(pfn_sb->checksum);
295         pfn_sb->checksum = 0;
296         if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
297                 return -ENODEV;
298         pfn_sb->checksum = cpu_to_le64(checksum);
299
300         if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
301                 return -ENODEV;
302
303         switch (le32_to_cpu(pfn_sb->mode)) {
304         case PFN_MODE_RAM:
305                 break;
306         case PFN_MODE_PMEM:
307                 /* TODO: allocate from PMEM support */
308                 return -ENOTTY;
309         default:
310                 return -ENXIO;
311         }
312
313         if (!nd_pfn->uuid) {
314                 /* from probe we allocate */
315                 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
316                 if (!nd_pfn->uuid)
317                         return -ENOMEM;
318         } else {
319                 /* from init we validate */
320                 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
321                         return -EINVAL;
322         }
323
324         if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) {
325                 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
326                                 nd_pfn->align, nvdimm_namespace_capacity(ndns));
327                 return -EINVAL;
328         }
329
330         /*
331          * These warnings are verbose because they can only trigger in
332          * the case where the physical address alignment of the
333          * namespace has changed since the pfn superblock was
334          * established.
335          */
336         offset = le64_to_cpu(pfn_sb->dataoff);
337         nsio = to_nd_namespace_io(&ndns->dev);
338         if (offset >= resource_size(&nsio->res)) {
339                 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
340                                 dev_name(&ndns->dev));
341                 return -EBUSY;
342         }
343
344         nd_pfn->align = 1UL << ilog2(offset);
345         if (!is_power_of_2(offset) || offset < PAGE_SIZE) {
346                 dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n",
347                                 offset);
348                 return -ENXIO;
349         }
350
351         return 0;
352 }
353 EXPORT_SYMBOL(nd_pfn_validate);
354
355 int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
356 {
357         int rc;
358         struct device *dev;
359         struct nd_pfn *nd_pfn;
360         struct nd_pfn_sb *pfn_sb;
361         struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
362
363         if (ndns->force_raw)
364                 return -ENODEV;
365
366         nvdimm_bus_lock(&ndns->dev);
367         dev = __nd_pfn_create(nd_region, ndns);
368         nvdimm_bus_unlock(&ndns->dev);
369         if (!dev)
370                 return -ENOMEM;
371         dev_set_drvdata(dev, drvdata);
372         pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
373         nd_pfn = to_nd_pfn(dev);
374         nd_pfn->pfn_sb = pfn_sb;
375         rc = nd_pfn_validate(nd_pfn);
376         nd_pfn->pfn_sb = NULL;
377         kfree(pfn_sb);
378         dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
379                         rc == 0 ? dev_name(dev) : "<none>");
380         if (rc < 0) {
381                 __nd_detach_ndns(dev, &nd_pfn->ndns);
382                 put_device(dev);
383         } else
384                 __nd_device_register(&nd_pfn->dev);
385
386         return rc;
387 }
388 EXPORT_SYMBOL(nd_pfn_probe);