]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/nvdimm/bus.c
libnvdimm: control (ioctl) messages for nvdimm_bus and nvdimm devices
[mirror_ubuntu-artful-kernel.git] / drivers / nvdimm / bus.c
CommitLineData
45def22c
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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
62232e45 14#include <linux/vmalloc.h>
45def22c
DW
15#include <linux/uaccess.h>
16#include <linux/fcntl.h>
e6dfb2de 17#include <linux/async.h>
62232e45 18#include <linux/ndctl.h>
45def22c
DW
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/io.h>
62232e45 22#include <linux/mm.h>
45def22c
DW
23#include "nd-core.h"
24
62232e45 25int nvdimm_major;
45def22c
DW
26static int nvdimm_bus_major;
27static struct class *nd_class;
28
e6dfb2de
DW
29struct bus_type nvdimm_bus_type = {
30 .name = "nd",
31};
32
45def22c
DW
33int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
34{
35 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
36 struct device *dev;
37
38 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
39 "ndctl%d", nvdimm_bus->id);
40
41 if (IS_ERR(dev)) {
42 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
43 nvdimm_bus->id, PTR_ERR(dev));
44 return PTR_ERR(dev);
45 }
46 return 0;
47}
48
49void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
50{
51 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
52}
53
62232e45
DW
54static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
55 [ND_CMD_IMPLEMENTED] = { },
56 [ND_CMD_SMART] = {
57 .out_num = 2,
58 .out_sizes = { 4, 8, },
59 },
60 [ND_CMD_SMART_THRESHOLD] = {
61 .out_num = 2,
62 .out_sizes = { 4, 8, },
63 },
64 [ND_CMD_DIMM_FLAGS] = {
65 .out_num = 2,
66 .out_sizes = { 4, 4 },
67 },
68 [ND_CMD_GET_CONFIG_SIZE] = {
69 .out_num = 3,
70 .out_sizes = { 4, 4, 4, },
71 },
72 [ND_CMD_GET_CONFIG_DATA] = {
73 .in_num = 2,
74 .in_sizes = { 4, 4, },
75 .out_num = 2,
76 .out_sizes = { 4, UINT_MAX, },
77 },
78 [ND_CMD_SET_CONFIG_DATA] = {
79 .in_num = 3,
80 .in_sizes = { 4, 4, UINT_MAX, },
81 .out_num = 1,
82 .out_sizes = { 4, },
83 },
84 [ND_CMD_VENDOR] = {
85 .in_num = 3,
86 .in_sizes = { 4, 4, UINT_MAX, },
87 .out_num = 3,
88 .out_sizes = { 4, 4, UINT_MAX, },
89 },
90};
91
92const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
93{
94 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
95 return &__nd_cmd_dimm_descs[cmd];
96 return NULL;
97}
98EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
99
100static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
101 [ND_CMD_IMPLEMENTED] = { },
102 [ND_CMD_ARS_CAP] = {
103 .in_num = 2,
104 .in_sizes = { 8, 8, },
105 .out_num = 2,
106 .out_sizes = { 4, 4, },
107 },
108 [ND_CMD_ARS_START] = {
109 .in_num = 4,
110 .in_sizes = { 8, 8, 2, 6, },
111 .out_num = 1,
112 .out_sizes = { 4, },
113 },
114 [ND_CMD_ARS_STATUS] = {
115 .out_num = 2,
116 .out_sizes = { 4, UINT_MAX, },
117 },
118};
119
120const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
121{
122 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
123 return &__nd_cmd_bus_descs[cmd];
124 return NULL;
125}
126EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
127
128u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
129 const struct nd_cmd_desc *desc, int idx, void *buf)
130{
131 if (idx >= desc->in_num)
132 return UINT_MAX;
133
134 if (desc->in_sizes[idx] < UINT_MAX)
135 return desc->in_sizes[idx];
136
137 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
138 struct nd_cmd_set_config_hdr *hdr = buf;
139
140 return hdr->in_length;
141 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
142 struct nd_cmd_vendor_hdr *hdr = buf;
143
144 return hdr->in_length;
145 }
146
147 return UINT_MAX;
148}
149EXPORT_SYMBOL_GPL(nd_cmd_in_size);
150
151u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
152 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
153 const u32 *out_field)
154{
155 if (idx >= desc->out_num)
156 return UINT_MAX;
157
158 if (desc->out_sizes[idx] < UINT_MAX)
159 return desc->out_sizes[idx];
160
161 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
162 return in_field[1];
163 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
164 return out_field[1];
165 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 1)
166 return ND_CMD_ARS_STATUS_MAX;
167
168 return UINT_MAX;
169}
170EXPORT_SYMBOL_GPL(nd_cmd_out_size);
171
172static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
173 int read_only, unsigned int ioctl_cmd, unsigned long arg)
174{
175 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
176 size_t buf_len = 0, in_len = 0, out_len = 0;
177 static char out_env[ND_CMD_MAX_ENVELOPE];
178 static char in_env[ND_CMD_MAX_ENVELOPE];
179 const struct nd_cmd_desc *desc = NULL;
180 unsigned int cmd = _IOC_NR(ioctl_cmd);
181 void __user *p = (void __user *) arg;
182 struct device *dev = &nvdimm_bus->dev;
183 const char *cmd_name, *dimm_name;
184 unsigned long dsm_mask;
185 void *buf;
186 int rc, i;
187
188 if (nvdimm) {
189 desc = nd_cmd_dimm_desc(cmd);
190 cmd_name = nvdimm_cmd_name(cmd);
191 dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0;
192 dimm_name = dev_name(&nvdimm->dev);
193 } else {
194 desc = nd_cmd_bus_desc(cmd);
195 cmd_name = nvdimm_bus_cmd_name(cmd);
196 dsm_mask = nd_desc->dsm_mask;
197 dimm_name = "bus";
198 }
199
200 if (!desc || (desc->out_num + desc->in_num == 0) ||
201 !test_bit(cmd, &dsm_mask))
202 return -ENOTTY;
203
204 /* fail write commands (when read-only) */
205 if (read_only)
206 switch (ioctl_cmd) {
207 case ND_IOCTL_VENDOR:
208 case ND_IOCTL_SET_CONFIG_DATA:
209 case ND_IOCTL_ARS_START:
210 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
211 nvdimm ? nvdimm_cmd_name(cmd)
212 : nvdimm_bus_cmd_name(cmd));
213 return -EPERM;
214 default:
215 break;
216 }
217
218 /* process an input envelope */
219 for (i = 0; i < desc->in_num; i++) {
220 u32 in_size, copy;
221
222 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
223 if (in_size == UINT_MAX) {
224 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
225 __func__, dimm_name, cmd_name, i);
226 return -ENXIO;
227 }
228 if (!access_ok(VERIFY_READ, p + in_len, in_size))
229 return -EFAULT;
230 if (in_len < sizeof(in_env))
231 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
232 else
233 copy = 0;
234 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
235 return -EFAULT;
236 in_len += in_size;
237 }
238
239 /* process an output envelope */
240 for (i = 0; i < desc->out_num; i++) {
241 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
242 (u32 *) in_env, (u32 *) out_env);
243 u32 copy;
244
245 if (out_size == UINT_MAX) {
246 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
247 __func__, dimm_name, cmd_name, i);
248 return -EFAULT;
249 }
250 if (!access_ok(VERIFY_WRITE, p + in_len + out_len, out_size))
251 return -EFAULT;
252 if (out_len < sizeof(out_env))
253 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
254 else
255 copy = 0;
256 if (copy && copy_from_user(&out_env[out_len],
257 p + in_len + out_len, copy))
258 return -EFAULT;
259 out_len += out_size;
260 }
261
262 buf_len = out_len + in_len;
263 if (!access_ok(VERIFY_WRITE, p, sizeof(buf_len)))
264 return -EFAULT;
265
266 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
267 dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
268 dimm_name, cmd_name, buf_len,
269 ND_IOCTL_MAX_BUFLEN);
270 return -EINVAL;
271 }
272
273 buf = vmalloc(buf_len);
274 if (!buf)
275 return -ENOMEM;
276
277 if (copy_from_user(buf, p, buf_len)) {
278 rc = -EFAULT;
279 goto out;
280 }
281
282 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len);
283 if (rc < 0)
284 goto out;
285 if (copy_to_user(p, buf, buf_len))
286 rc = -EFAULT;
287 out:
288 vfree(buf);
289 return rc;
290}
291
45def22c
DW
292static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
293{
62232e45
DW
294 long id = (long) file->private_data;
295 int rc = -ENXIO, read_only;
296 struct nvdimm_bus *nvdimm_bus;
297
298 read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
299 mutex_lock(&nvdimm_bus_list_mutex);
300 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
301 if (nvdimm_bus->id == id) {
302 rc = __nd_ioctl(nvdimm_bus, NULL, read_only, cmd, arg);
303 break;
304 }
305 }
306 mutex_unlock(&nvdimm_bus_list_mutex);
307
308 return rc;
309}
310
311static int match_dimm(struct device *dev, void *data)
312{
313 long id = (long) data;
314
315 if (is_nvdimm(dev)) {
316 struct nvdimm *nvdimm = to_nvdimm(dev);
317
318 return nvdimm->id == id;
319 }
320
321 return 0;
322}
323
324static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
325{
326 int rc = -ENXIO, read_only;
327 struct nvdimm_bus *nvdimm_bus;
328
329 read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
330 mutex_lock(&nvdimm_bus_list_mutex);
331 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
332 struct device *dev = device_find_child(&nvdimm_bus->dev,
333 file->private_data, match_dimm);
334 struct nvdimm *nvdimm;
335
336 if (!dev)
337 continue;
338
339 nvdimm = to_nvdimm(dev);
340 rc = __nd_ioctl(nvdimm_bus, nvdimm, read_only, cmd, arg);
341 put_device(dev);
342 break;
343 }
344 mutex_unlock(&nvdimm_bus_list_mutex);
345
346 return rc;
347}
348
349static int nd_open(struct inode *inode, struct file *file)
350{
351 long minor = iminor(inode);
352
353 file->private_data = (void *) minor;
354 return 0;
45def22c
DW
355}
356
357static const struct file_operations nvdimm_bus_fops = {
358 .owner = THIS_MODULE,
62232e45 359 .open = nd_open,
45def22c
DW
360 .unlocked_ioctl = nd_ioctl,
361 .compat_ioctl = nd_ioctl,
362 .llseek = noop_llseek,
363};
364
62232e45
DW
365static const struct file_operations nvdimm_fops = {
366 .owner = THIS_MODULE,
367 .open = nd_open,
368 .unlocked_ioctl = nvdimm_ioctl,
369 .compat_ioctl = nvdimm_ioctl,
370 .llseek = noop_llseek,
371};
372
45def22c
DW
373int __init nvdimm_bus_init(void)
374{
375 int rc;
376
e6dfb2de
DW
377 rc = bus_register(&nvdimm_bus_type);
378 if (rc)
379 return rc;
380
45def22c
DW
381 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
382 if (rc < 0)
62232e45 383 goto err_bus_chrdev;
45def22c
DW
384 nvdimm_bus_major = rc;
385
62232e45
DW
386 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
387 if (rc < 0)
388 goto err_dimm_chrdev;
389 nvdimm_major = rc;
390
45def22c
DW
391 nd_class = class_create(THIS_MODULE, "nd");
392 if (IS_ERR(nd_class))
393 goto err_class;
394
395 return 0;
396
397 err_class:
62232e45
DW
398 unregister_chrdev(nvdimm_major, "dimmctl");
399 err_dimm_chrdev:
45def22c 400 unregister_chrdev(nvdimm_bus_major, "ndctl");
62232e45 401 err_bus_chrdev:
e6dfb2de 402 bus_unregister(&nvdimm_bus_type);
45def22c
DW
403
404 return rc;
405}
406
407void __exit nvdimm_bus_exit(void)
408{
409 class_destroy(nd_class);
410 unregister_chrdev(nvdimm_bus_major, "ndctl");
62232e45 411 unregister_chrdev(nvdimm_major, "dimmctl");
e6dfb2de 412 bus_unregister(&nvdimm_bus_type);
45def22c 413}