blob: 71805a1aa0f3e5f002ee5d26339c53d49aadf951 [file] [log] [blame]
Dan Williamse1455742015-07-30 17:57:47 -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 "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
Dan Williamse1455742015-07-30 17:57:47 -0400151 device_lock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800152 nvdimm_bus_lock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400153 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");
Dan Williamse1455742015-07-30 17:57:47 -0400156 nvdimm_bus_unlock(dev);
Axel Lin4ca8b57a2015-09-16 21:25:38 +0800157 device_unlock(dev);
Dan Williamse1455742015-07-30 17:57:47 -0400158
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,
182 u8 *uuid, enum nd_pfn_mode mode,
183 struct nd_namespace_common *ndns)
184{
185 struct nd_pfn *nd_pfn;
186 struct device *dev;
187
188 /* we can only create pages for contiguous ranged of pmem */
189 if (!is_nd_pmem(&nd_region->dev))
190 return NULL;
191
192 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
193 if (!nd_pfn)
194 return NULL;
195
196 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
197 if (nd_pfn->id < 0) {
198 kfree(nd_pfn);
199 return NULL;
200 }
201
202 nd_pfn->mode = mode;
203 if (uuid)
204 uuid = kmemdup(uuid, 16, GFP_KERNEL);
205 nd_pfn->uuid = uuid;
206 dev = &nd_pfn->dev;
207 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
208 dev->parent = &nd_region->dev;
209 dev->type = &nd_pfn_device_type;
210 dev->groups = nd_pfn_attribute_groups;
211 device_initialize(&nd_pfn->dev);
212 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
213 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
214 __func__, dev_name(ndns->claim));
215 put_device(dev);
216 return NULL;
217 }
218 return dev;
219}
220
221struct device *nd_pfn_create(struct nd_region *nd_region)
222{
223 struct device *dev = __nd_pfn_create(nd_region, NULL, PFN_MODE_NONE,
224 NULL);
225
226 if (dev)
227 __nd_device_register(dev);
228 return dev;
229}
230
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400231int nd_pfn_validate(struct nd_pfn *nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -0400232{
233 struct nd_namespace_common *ndns = nd_pfn->ndns;
234 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
235 struct nd_namespace_io *nsio;
236 u64 checksum, offset;
237
238 if (!pfn_sb || !ndns)
239 return -ENODEV;
240
241 if (!is_nd_pmem(nd_pfn->dev.parent))
242 return -ENODEV;
243
244 /* section alignment for simple hotplug */
245 if (nvdimm_namespace_capacity(ndns) < ND_PFN_ALIGN)
246 return -ENODEV;
247
248 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
249 return -ENXIO;
250
251 if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
252 return -ENODEV;
253
254 checksum = le64_to_cpu(pfn_sb->checksum);
255 pfn_sb->checksum = 0;
256 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
257 return -ENODEV;
258 pfn_sb->checksum = cpu_to_le64(checksum);
259
260 switch (le32_to_cpu(pfn_sb->mode)) {
261 case PFN_MODE_RAM:
262 break;
263 case PFN_MODE_PMEM:
264 /* TODO: allocate from PMEM support */
265 return -ENOTTY;
266 default:
267 return -ENXIO;
268 }
269
270 if (!nd_pfn->uuid) {
271 /* from probe we allocate */
272 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
273 if (!nd_pfn->uuid)
274 return -ENOMEM;
275 } else {
276 /* from init we validate */
277 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
278 return -EINVAL;
279 }
280
281 /*
282 * These warnings are verbose because they can only trigger in
283 * the case where the physical address alignment of the
284 * namespace has changed since the pfn superblock was
285 * established.
286 */
287 offset = le64_to_cpu(pfn_sb->dataoff);
288 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400289 if (nsio->res.start & ND_PFN_MASK) {
Dan Williamse1455742015-07-30 17:57:47 -0400290 dev_err(&nd_pfn->dev,
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400291 "init failed: %s not section aligned\n",
292 dev_name(&ndns->dev));
Dan Williamse1455742015-07-30 17:57:47 -0400293 return -EBUSY;
294 } else if (offset >= resource_size(&nsio->res)) {
295 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
296 dev_name(&ndns->dev));
297 return -EBUSY;
298 }
299
300 return 0;
301}
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400302EXPORT_SYMBOL(nd_pfn_validate);
Dan Williamse1455742015-07-30 17:57:47 -0400303
304int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
305{
306 int rc;
307 struct device *dev;
308 struct nd_pfn *nd_pfn;
309 struct nd_pfn_sb *pfn_sb;
310 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
311
312 if (ndns->force_raw)
313 return -ENODEV;
314
315 nvdimm_bus_lock(&ndns->dev);
316 dev = __nd_pfn_create(nd_region, NULL, PFN_MODE_NONE, ndns);
317 nvdimm_bus_unlock(&ndns->dev);
318 if (!dev)
319 return -ENOMEM;
320 dev_set_drvdata(dev, drvdata);
321 pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
322 nd_pfn = to_nd_pfn(dev);
323 nd_pfn->pfn_sb = pfn_sb;
324 rc = nd_pfn_validate(nd_pfn);
325 nd_pfn->pfn_sb = NULL;
326 kfree(pfn_sb);
327 dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
328 rc == 0 ? dev_name(dev) : "<none>");
329 if (rc < 0) {
330 __nd_detach_ndns(dev, &nd_pfn->ndns);
331 put_device(dev);
332 } else
333 __nd_device_register(&nd_pfn->dev);
334
335 return rc;
336}
337EXPORT_SYMBOL(nd_pfn_probe);