]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/nvdimm/pfn_devs.c
libnvdimm, pfn: clean up pfn create parameters
[mirror_ubuntu-bionic-kernel.git] / drivers / nvdimm / pfn_devs.c
CommitLineData
e1455744
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 "pfn.h"
22#include "nd.h"
23
24static 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
36static struct device_type nd_pfn_device_type = {
37 .name = "nd_pfn",
38 .release = nd_pfn_release,
39};
40
41bool is_nd_pfn(struct device *dev)
42{
43 return dev ? dev->type == &nd_pfn_device_type : false;
44}
45EXPORT_SYMBOL(is_nd_pfn);
46
47struct 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}
54EXPORT_SYMBOL(to_nd_pfn);
55
56static 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
71static 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}
104static DEVICE_ATTR_RW(mode);
105
106static ssize_t uuid_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
108{
109 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
110
111 if (nd_pfn->uuid)
112 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
113 return sprintf(buf, "\n");
114}
115
116static ssize_t uuid_store(struct device *dev,
117 struct device_attribute *attr, const char *buf, size_t len)
118{
119 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
120 ssize_t rc;
121
122 device_lock(dev);
123 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
124 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
125 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
126 device_unlock(dev);
127
128 return rc ? rc : len;
129}
130static DEVICE_ATTR_RW(uuid);
131
132static ssize_t namespace_show(struct device *dev,
133 struct device_attribute *attr, char *buf)
134{
135 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
136 ssize_t rc;
137
138 nvdimm_bus_lock(dev);
139 rc = sprintf(buf, "%s\n", nd_pfn->ndns
140 ? dev_name(&nd_pfn->ndns->dev) : "");
141 nvdimm_bus_unlock(dev);
142 return rc;
143}
144
145static ssize_t namespace_store(struct device *dev,
146 struct device_attribute *attr, const char *buf, size_t len)
147{
148 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
149 ssize_t rc;
150
e1455744 151 device_lock(dev);
4ca8b57a 152 nvdimm_bus_lock(dev);
e1455744
DW
153 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
154 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
155 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
e1455744 156 nvdimm_bus_unlock(dev);
4ca8b57a 157 device_unlock(dev);
e1455744
DW
158
159 return rc;
160}
161static DEVICE_ATTR_RW(namespace);
162
163static struct attribute *nd_pfn_attributes[] = {
164 &dev_attr_mode.attr,
165 &dev_attr_namespace.attr,
166 &dev_attr_uuid.attr,
167 NULL,
168};
169
170static struct attribute_group nd_pfn_attribute_group = {
171 .attrs = nd_pfn_attributes,
172};
173
174static const struct attribute_group *nd_pfn_attribute_groups[] = {
175 &nd_pfn_attribute_group,
176 &nd_device_attribute_group,
177 &nd_numa_attribute_group,
178 NULL,
179};
180
181static struct device *__nd_pfn_create(struct nd_region *nd_region,
e1455744
DW
182 struct nd_namespace_common *ndns)
183{
184 struct nd_pfn *nd_pfn;
185 struct device *dev;
186
187 /* we can only create pages for contiguous ranged of pmem */
188 if (!is_nd_pmem(&nd_region->dev))
189 return NULL;
190
191 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
192 if (!nd_pfn)
193 return NULL;
194
195 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
196 if (nd_pfn->id < 0) {
197 kfree(nd_pfn);
198 return NULL;
199 }
200
f7c6ab80 201 nd_pfn->mode = PFN_MODE_NONE;
e1455744
DW
202 dev = &nd_pfn->dev;
203 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
204 dev->parent = &nd_region->dev;
205 dev->type = &nd_pfn_device_type;
206 dev->groups = nd_pfn_attribute_groups;
207 device_initialize(&nd_pfn->dev);
208 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
209 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
210 __func__, dev_name(ndns->claim));
211 put_device(dev);
212 return NULL;
213 }
214 return dev;
215}
216
217struct device *nd_pfn_create(struct nd_region *nd_region)
218{
f7c6ab80 219 struct device *dev = __nd_pfn_create(nd_region, NULL);
e1455744
DW
220
221 if (dev)
222 __nd_device_register(dev);
223 return dev;
224}
225
32ab0a3f 226int nd_pfn_validate(struct nd_pfn *nd_pfn)
e1455744
DW
227{
228 struct nd_namespace_common *ndns = nd_pfn->ndns;
229 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
230 struct nd_namespace_io *nsio;
231 u64 checksum, offset;
232
233 if (!pfn_sb || !ndns)
234 return -ENODEV;
235
236 if (!is_nd_pmem(nd_pfn->dev.parent))
237 return -ENODEV;
238
e1455744
DW
239 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
240 return -ENXIO;
241
242 if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
243 return -ENODEV;
244
245 checksum = le64_to_cpu(pfn_sb->checksum);
246 pfn_sb->checksum = 0;
247 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
248 return -ENODEV;
249 pfn_sb->checksum = cpu_to_le64(checksum);
250
251 switch (le32_to_cpu(pfn_sb->mode)) {
252 case PFN_MODE_RAM:
253 break;
254 case PFN_MODE_PMEM:
255 /* TODO: allocate from PMEM support */
256 return -ENOTTY;
257 default:
258 return -ENXIO;
259 }
260
261 if (!nd_pfn->uuid) {
262 /* from probe we allocate */
263 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
264 if (!nd_pfn->uuid)
265 return -ENOMEM;
266 } else {
267 /* from init we validate */
268 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
269 return -EINVAL;
270 }
271
272 /*
273 * These warnings are verbose because they can only trigger in
274 * the case where the physical address alignment of the
275 * namespace has changed since the pfn superblock was
276 * established.
277 */
278 offset = le64_to_cpu(pfn_sb->dataoff);
279 nsio = to_nd_namespace_io(&ndns->dev);
9f1e8cee 280 if (offset >= resource_size(&nsio->res)) {
e1455744
DW
281 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
282 dev_name(&ndns->dev));
283 return -EBUSY;
284 }
285
286 return 0;
287}
32ab0a3f 288EXPORT_SYMBOL(nd_pfn_validate);
e1455744
DW
289
290int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
291{
292 int rc;
293 struct device *dev;
294 struct nd_pfn *nd_pfn;
295 struct nd_pfn_sb *pfn_sb;
296 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
297
298 if (ndns->force_raw)
299 return -ENODEV;
300
301 nvdimm_bus_lock(&ndns->dev);
f7c6ab80 302 dev = __nd_pfn_create(nd_region, ndns);
e1455744
DW
303 nvdimm_bus_unlock(&ndns->dev);
304 if (!dev)
305 return -ENOMEM;
306 dev_set_drvdata(dev, drvdata);
307 pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
308 nd_pfn = to_nd_pfn(dev);
309 nd_pfn->pfn_sb = pfn_sb;
310 rc = nd_pfn_validate(nd_pfn);
311 nd_pfn->pfn_sb = NULL;
312 kfree(pfn_sb);
313 dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
314 rc == 0 ? dev_name(dev) : "<none>");
315 if (rc < 0) {
316 __nd_detach_ndns(dev, &nd_pfn->ndns);
317 put_device(dev);
318 } else
319 __nd_device_register(&nd_pfn->dev);
320
321 return rc;
322}
323EXPORT_SYMBOL(nd_pfn_probe);