]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/nvdimm/pfn_devs.c
libnvdimm, pfn: add 'align' attribute, default to HPAGE_SIZE
[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
315c5625
DW
106static 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
114static 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
134static 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}
150static DEVICE_ATTR_RW(align);
151
e1455744
DW
152static 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
162static 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}
176static DEVICE_ATTR_RW(uuid);
177
178static 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
191static 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
e1455744 197 device_lock(dev);
4ca8b57a 198 nvdimm_bus_lock(dev);
e1455744
DW
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");
e1455744 202 nvdimm_bus_unlock(dev);
4ca8b57a 203 device_unlock(dev);
e1455744
DW
204
205 return rc;
206}
207static DEVICE_ATTR_RW(namespace);
208
209static struct attribute *nd_pfn_attributes[] = {
210 &dev_attr_mode.attr,
211 &dev_attr_namespace.attr,
212 &dev_attr_uuid.attr,
315c5625 213 &dev_attr_align.attr,
e1455744
DW
214 NULL,
215};
216
217static struct attribute_group nd_pfn_attribute_group = {
218 .attrs = nd_pfn_attributes,
219};
220
221static 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
228static struct device *__nd_pfn_create(struct nd_region *nd_region,
e1455744
DW
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
f7c6ab80 248 nd_pfn->mode = PFN_MODE_NONE;
315c5625 249 nd_pfn->align = HPAGE_SIZE;
e1455744
DW
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
265struct device *nd_pfn_create(struct nd_region *nd_region)
266{
f7c6ab80 267 struct device *dev = __nd_pfn_create(nd_region, NULL);
e1455744
DW
268
269 if (dev)
270 __nd_device_register(dev);
271 return dev;
272}
273
32ab0a3f 274int nd_pfn_validate(struct nd_pfn *nd_pfn)
e1455744
DW
275{
276 struct nd_namespace_common *ndns = nd_pfn->ndns;
277 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
278 struct nd_namespace_io *nsio;
279 u64 checksum, offset;
280
281 if (!pfn_sb || !ndns)
282 return -ENODEV;
283
284 if (!is_nd_pmem(nd_pfn->dev.parent))
285 return -ENODEV;
286
e1455744
DW
287 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
288 return -ENXIO;
289
290 if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
291 return -ENODEV;
292
293 checksum = le64_to_cpu(pfn_sb->checksum);
294 pfn_sb->checksum = 0;
295 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
296 return -ENODEV;
297 pfn_sb->checksum = cpu_to_le64(checksum);
298
299 switch (le32_to_cpu(pfn_sb->mode)) {
300 case PFN_MODE_RAM:
301 break;
302 case PFN_MODE_PMEM:
303 /* TODO: allocate from PMEM support */
304 return -ENOTTY;
305 default:
306 return -ENXIO;
307 }
308
309 if (!nd_pfn->uuid) {
310 /* from probe we allocate */
311 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
312 if (!nd_pfn->uuid)
313 return -ENOMEM;
314 } else {
315 /* from init we validate */
316 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
317 return -EINVAL;
318 }
319
315c5625
DW
320 if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) {
321 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
322 nd_pfn->align, nvdimm_namespace_capacity(ndns));
323 return -EINVAL;
324 }
325
e1455744
DW
326 /*
327 * These warnings are verbose because they can only trigger in
328 * the case where the physical address alignment of the
329 * namespace has changed since the pfn superblock was
330 * established.
331 */
332 offset = le64_to_cpu(pfn_sb->dataoff);
333 nsio = to_nd_namespace_io(&ndns->dev);
9f1e8cee 334 if (offset >= resource_size(&nsio->res)) {
e1455744
DW
335 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
336 dev_name(&ndns->dev));
337 return -EBUSY;
338 }
339
315c5625
DW
340 nd_pfn->align = 1UL << ilog2(offset);
341 if (!is_power_of_2(offset) || offset < PAGE_SIZE) {
342 dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n",
343 offset);
344 return -ENXIO;
345 }
346
e1455744
DW
347 return 0;
348}
32ab0a3f 349EXPORT_SYMBOL(nd_pfn_validate);
e1455744
DW
350
351int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
352{
353 int rc;
354 struct device *dev;
355 struct nd_pfn *nd_pfn;
356 struct nd_pfn_sb *pfn_sb;
357 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
358
359 if (ndns->force_raw)
360 return -ENODEV;
361
362 nvdimm_bus_lock(&ndns->dev);
f7c6ab80 363 dev = __nd_pfn_create(nd_region, ndns);
e1455744
DW
364 nvdimm_bus_unlock(&ndns->dev);
365 if (!dev)
366 return -ENOMEM;
367 dev_set_drvdata(dev, drvdata);
368 pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
369 nd_pfn = to_nd_pfn(dev);
370 nd_pfn->pfn_sb = pfn_sb;
371 rc = nd_pfn_validate(nd_pfn);
372 nd_pfn->pfn_sb = NULL;
373 kfree(pfn_sb);
374 dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
375 rc == 0 ? dev_name(dev) : "<none>");
376 if (rc < 0) {
377 __nd_detach_ndns(dev, &nd_pfn->ndns);
378 put_device(dev);
379 } else
380 __nd_device_register(&nd_pfn->dev);
381
382 return rc;
383}
384EXPORT_SYMBOL(nd_pfn_probe);