Merge branch 'for-4.7/libnvdimm' into libnvdimm-for-next
[deliverable/linux.git] / drivers / nvdimm / bus.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/vmalloc.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/fcntl.h>
19 #include <linux/async.h>
20 #include <linux/genhd.h>
21 #include <linux/ndctl.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/fs.h>
25 #include <linux/io.h>
26 #include <linux/mm.h>
27 #include <linux/nd.h>
28 #include "nd-core.h"
29 #include "nd.h"
30
31 int nvdimm_major;
32 static int nvdimm_bus_major;
33 static struct class *nd_class;
34
35 static int to_nd_device_type(struct device *dev)
36 {
37 if (is_nvdimm(dev))
38 return ND_DEVICE_DIMM;
39 else if (is_nd_pmem(dev))
40 return ND_DEVICE_REGION_PMEM;
41 else if (is_nd_blk(dev))
42 return ND_DEVICE_REGION_BLK;
43 else if (is_nd_dax(dev))
44 return ND_DEVICE_DAX_PMEM;
45 else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent))
46 return nd_region_to_nstype(to_nd_region(dev->parent));
47
48 return 0;
49 }
50
51 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
52 {
53 /*
54 * Ensure that region devices always have their numa node set as
55 * early as possible.
56 */
57 if (is_nd_pmem(dev) || is_nd_blk(dev))
58 set_dev_node(dev, to_nd_region(dev)->numa_node);
59 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
60 to_nd_device_type(dev));
61 }
62
63 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
64 {
65 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
66
67 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
68 }
69
70 static struct module *to_bus_provider(struct device *dev)
71 {
72 /* pin bus providers while regions are enabled */
73 if (is_nd_pmem(dev) || is_nd_blk(dev)) {
74 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
75
76 return nvdimm_bus->module;
77 }
78 return NULL;
79 }
80
81 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
82 {
83 nvdimm_bus_lock(&nvdimm_bus->dev);
84 nvdimm_bus->probe_active++;
85 nvdimm_bus_unlock(&nvdimm_bus->dev);
86 }
87
88 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
89 {
90 nvdimm_bus_lock(&nvdimm_bus->dev);
91 if (--nvdimm_bus->probe_active == 0)
92 wake_up(&nvdimm_bus->probe_wait);
93 nvdimm_bus_unlock(&nvdimm_bus->dev);
94 }
95
96 static int nvdimm_bus_probe(struct device *dev)
97 {
98 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
99 struct module *provider = to_bus_provider(dev);
100 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
101 int rc;
102
103 if (!try_module_get(provider))
104 return -ENXIO;
105
106 nvdimm_bus_probe_start(nvdimm_bus);
107 rc = nd_drv->probe(dev);
108 if (rc == 0)
109 nd_region_probe_success(nvdimm_bus, dev);
110 else
111 nd_region_disable(nvdimm_bus, dev);
112 nvdimm_bus_probe_end(nvdimm_bus);
113
114 dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
115 dev_name(dev), rc);
116
117 if (rc != 0)
118 module_put(provider);
119 return rc;
120 }
121
122 static int nvdimm_bus_remove(struct device *dev)
123 {
124 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
125 struct module *provider = to_bus_provider(dev);
126 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
127 int rc;
128
129 rc = nd_drv->remove(dev);
130 nd_region_disable(nvdimm_bus, dev);
131
132 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
133 dev_name(dev), rc);
134 module_put(provider);
135 return rc;
136 }
137
138 void nd_device_notify(struct device *dev, enum nvdimm_event event)
139 {
140 device_lock(dev);
141 if (dev->driver) {
142 struct nd_device_driver *nd_drv;
143
144 nd_drv = to_nd_device_driver(dev->driver);
145 if (nd_drv->notify)
146 nd_drv->notify(dev, event);
147 }
148 device_unlock(dev);
149 }
150 EXPORT_SYMBOL(nd_device_notify);
151
152 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
153 {
154 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
155
156 if (!nvdimm_bus)
157 return;
158
159 /* caller is responsible for holding a reference on the device */
160 nd_device_notify(&nd_region->dev, event);
161 }
162 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
163
164 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
165 unsigned int len)
166 {
167 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
168 struct nvdimm_bus_descriptor *nd_desc;
169 struct nd_cmd_clear_error clear_err;
170 struct nd_cmd_ars_cap ars_cap;
171 u32 clear_err_unit, mask;
172 int cmd_rc, rc;
173
174 if (!nvdimm_bus)
175 return -ENXIO;
176
177 nd_desc = nvdimm_bus->nd_desc;
178 if (!nd_desc->ndctl)
179 return -ENXIO;
180
181 memset(&ars_cap, 0, sizeof(ars_cap));
182 ars_cap.address = phys;
183 ars_cap.length = len;
184 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
185 sizeof(ars_cap), &cmd_rc);
186 if (rc < 0)
187 return rc;
188 if (cmd_rc < 0)
189 return cmd_rc;
190 clear_err_unit = ars_cap.clear_err_unit;
191 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
192 return -ENXIO;
193
194 mask = clear_err_unit - 1;
195 if ((phys | len) & mask)
196 return -ENXIO;
197 memset(&clear_err, 0, sizeof(clear_err));
198 clear_err.address = phys;
199 clear_err.length = len;
200 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
201 sizeof(clear_err), &cmd_rc);
202 if (rc < 0)
203 return rc;
204 if (cmd_rc < 0)
205 return cmd_rc;
206 return clear_err.cleared;
207 }
208 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
209
210 static struct bus_type nvdimm_bus_type = {
211 .name = "nd",
212 .uevent = nvdimm_bus_uevent,
213 .match = nvdimm_bus_match,
214 .probe = nvdimm_bus_probe,
215 .remove = nvdimm_bus_remove,
216 };
217
218 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
219
220 void nd_synchronize(void)
221 {
222 async_synchronize_full_domain(&nd_async_domain);
223 }
224 EXPORT_SYMBOL_GPL(nd_synchronize);
225
226 static void nd_async_device_register(void *d, async_cookie_t cookie)
227 {
228 struct device *dev = d;
229
230 if (device_add(dev) != 0) {
231 dev_err(dev, "%s: failed\n", __func__);
232 put_device(dev);
233 }
234 put_device(dev);
235 }
236
237 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
238 {
239 struct device *dev = d;
240
241 /* flush bus operations before delete */
242 nvdimm_bus_lock(dev);
243 nvdimm_bus_unlock(dev);
244
245 device_unregister(dev);
246 put_device(dev);
247 }
248
249 void __nd_device_register(struct device *dev)
250 {
251 if (!dev)
252 return;
253 dev->bus = &nvdimm_bus_type;
254 get_device(dev);
255 async_schedule_domain(nd_async_device_register, dev,
256 &nd_async_domain);
257 }
258
259 void nd_device_register(struct device *dev)
260 {
261 device_initialize(dev);
262 __nd_device_register(dev);
263 }
264 EXPORT_SYMBOL(nd_device_register);
265
266 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
267 {
268 switch (mode) {
269 case ND_ASYNC:
270 get_device(dev);
271 async_schedule_domain(nd_async_device_unregister, dev,
272 &nd_async_domain);
273 break;
274 case ND_SYNC:
275 nd_synchronize();
276 device_unregister(dev);
277 break;
278 }
279 }
280 EXPORT_SYMBOL(nd_device_unregister);
281
282 /**
283 * __nd_driver_register() - register a region or a namespace driver
284 * @nd_drv: driver to register
285 * @owner: automatically set by nd_driver_register() macro
286 * @mod_name: automatically set by nd_driver_register() macro
287 */
288 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
289 const char *mod_name)
290 {
291 struct device_driver *drv = &nd_drv->drv;
292
293 if (!nd_drv->type) {
294 pr_debug("driver type bitmask not set (%pf)\n",
295 __builtin_return_address(0));
296 return -EINVAL;
297 }
298
299 if (!nd_drv->probe || !nd_drv->remove) {
300 pr_debug("->probe() and ->remove() must be specified\n");
301 return -EINVAL;
302 }
303
304 drv->bus = &nvdimm_bus_type;
305 drv->owner = owner;
306 drv->mod_name = mod_name;
307
308 return driver_register(drv);
309 }
310 EXPORT_SYMBOL(__nd_driver_register);
311
312 int nvdimm_revalidate_disk(struct gendisk *disk)
313 {
314 struct device *dev = disk->driverfs_dev;
315 struct nd_region *nd_region = to_nd_region(dev->parent);
316 const char *pol = nd_region->ro ? "only" : "write";
317
318 if (nd_region->ro == get_disk_ro(disk))
319 return 0;
320
321 dev_info(dev, "%s read-%s, marking %s read-%s\n",
322 dev_name(&nd_region->dev), pol, disk->disk_name, pol);
323 set_disk_ro(disk, nd_region->ro);
324
325 return 0;
326
327 }
328 EXPORT_SYMBOL(nvdimm_revalidate_disk);
329
330 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
331 char *buf)
332 {
333 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
334 to_nd_device_type(dev));
335 }
336 static DEVICE_ATTR_RO(modalias);
337
338 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
339 char *buf)
340 {
341 return sprintf(buf, "%s\n", dev->type->name);
342 }
343 static DEVICE_ATTR_RO(devtype);
344
345 static struct attribute *nd_device_attributes[] = {
346 &dev_attr_modalias.attr,
347 &dev_attr_devtype.attr,
348 NULL,
349 };
350
351 /**
352 * nd_device_attribute_group - generic attributes for all devices on an nd bus
353 */
354 struct attribute_group nd_device_attribute_group = {
355 .attrs = nd_device_attributes,
356 };
357 EXPORT_SYMBOL_GPL(nd_device_attribute_group);
358
359 static ssize_t numa_node_show(struct device *dev,
360 struct device_attribute *attr, char *buf)
361 {
362 return sprintf(buf, "%d\n", dev_to_node(dev));
363 }
364 static DEVICE_ATTR_RO(numa_node);
365
366 static struct attribute *nd_numa_attributes[] = {
367 &dev_attr_numa_node.attr,
368 NULL,
369 };
370
371 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
372 int n)
373 {
374 if (!IS_ENABLED(CONFIG_NUMA))
375 return 0;
376
377 return a->mode;
378 }
379
380 /**
381 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
382 */
383 struct attribute_group nd_numa_attribute_group = {
384 .attrs = nd_numa_attributes,
385 .is_visible = nd_numa_attr_visible,
386 };
387 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
388
389 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
390 {
391 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
392 struct device *dev;
393
394 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
395 "ndctl%d", nvdimm_bus->id);
396
397 if (IS_ERR(dev)) {
398 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
399 nvdimm_bus->id, PTR_ERR(dev));
400 return PTR_ERR(dev);
401 }
402 return 0;
403 }
404
405 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
406 {
407 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
408 }
409
410 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
411 [ND_CMD_IMPLEMENTED] = { },
412 [ND_CMD_SMART] = {
413 .out_num = 2,
414 .out_sizes = { 4, 128, },
415 },
416 [ND_CMD_SMART_THRESHOLD] = {
417 .out_num = 2,
418 .out_sizes = { 4, 8, },
419 },
420 [ND_CMD_DIMM_FLAGS] = {
421 .out_num = 2,
422 .out_sizes = { 4, 4 },
423 },
424 [ND_CMD_GET_CONFIG_SIZE] = {
425 .out_num = 3,
426 .out_sizes = { 4, 4, 4, },
427 },
428 [ND_CMD_GET_CONFIG_DATA] = {
429 .in_num = 2,
430 .in_sizes = { 4, 4, },
431 .out_num = 2,
432 .out_sizes = { 4, UINT_MAX, },
433 },
434 [ND_CMD_SET_CONFIG_DATA] = {
435 .in_num = 3,
436 .in_sizes = { 4, 4, UINT_MAX, },
437 .out_num = 1,
438 .out_sizes = { 4, },
439 },
440 [ND_CMD_VENDOR] = {
441 .in_num = 3,
442 .in_sizes = { 4, 4, UINT_MAX, },
443 .out_num = 3,
444 .out_sizes = { 4, 4, UINT_MAX, },
445 },
446 };
447
448 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
449 {
450 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
451 return &__nd_cmd_dimm_descs[cmd];
452 return NULL;
453 }
454 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
455
456 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
457 [ND_CMD_IMPLEMENTED] = { },
458 [ND_CMD_ARS_CAP] = {
459 .in_num = 2,
460 .in_sizes = { 8, 8, },
461 .out_num = 4,
462 .out_sizes = { 4, 4, 4, 4, },
463 },
464 [ND_CMD_ARS_START] = {
465 .in_num = 5,
466 .in_sizes = { 8, 8, 2, 1, 5, },
467 .out_num = 2,
468 .out_sizes = { 4, 4, },
469 },
470 [ND_CMD_ARS_STATUS] = {
471 .out_num = 3,
472 .out_sizes = { 4, 4, UINT_MAX, },
473 },
474 [ND_CMD_CLEAR_ERROR] = {
475 .in_num = 2,
476 .in_sizes = { 8, 8, },
477 .out_num = 3,
478 .out_sizes = { 4, 4, 8, },
479 },
480 };
481
482 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
483 {
484 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
485 return &__nd_cmd_bus_descs[cmd];
486 return NULL;
487 }
488 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
489
490 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
491 const struct nd_cmd_desc *desc, int idx, void *buf)
492 {
493 if (idx >= desc->in_num)
494 return UINT_MAX;
495
496 if (desc->in_sizes[idx] < UINT_MAX)
497 return desc->in_sizes[idx];
498
499 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
500 struct nd_cmd_set_config_hdr *hdr = buf;
501
502 return hdr->in_length;
503 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
504 struct nd_cmd_vendor_hdr *hdr = buf;
505
506 return hdr->in_length;
507 }
508
509 return UINT_MAX;
510 }
511 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
512
513 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
514 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
515 const u32 *out_field)
516 {
517 if (idx >= desc->out_num)
518 return UINT_MAX;
519
520 if (desc->out_sizes[idx] < UINT_MAX)
521 return desc->out_sizes[idx];
522
523 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
524 return in_field[1];
525 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
526 return out_field[1];
527 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2)
528 return out_field[1] - 8;
529
530 return UINT_MAX;
531 }
532 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
533
534 void wait_nvdimm_bus_probe_idle(struct device *dev)
535 {
536 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
537
538 do {
539 if (nvdimm_bus->probe_active == 0)
540 break;
541 nvdimm_bus_unlock(&nvdimm_bus->dev);
542 wait_event(nvdimm_bus->probe_wait,
543 nvdimm_bus->probe_active == 0);
544 nvdimm_bus_lock(&nvdimm_bus->dev);
545 } while (true);
546 }
547
548 static int pmem_active(struct device *dev, void *data)
549 {
550 if (is_nd_pmem(dev) && dev->driver)
551 return -EBUSY;
552 return 0;
553 }
554
555 /* set_config requires an idle interleave set */
556 static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
557 struct nvdimm *nvdimm, unsigned int cmd)
558 {
559 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
560
561 /* ask the bus provider if it would like to block this request */
562 if (nd_desc->clear_to_send) {
563 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
564
565 if (rc)
566 return rc;
567 }
568
569 /* require clear error to go through the pmem driver */
570 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
571 return device_for_each_child(&nvdimm_bus->dev, NULL,
572 pmem_active);
573
574 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
575 return 0;
576
577 /* prevent label manipulation while the kernel owns label updates */
578 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
579 if (atomic_read(&nvdimm->busy))
580 return -EBUSY;
581 return 0;
582 }
583
584 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
585 int read_only, unsigned int ioctl_cmd, unsigned long arg)
586 {
587 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
588 size_t buf_len = 0, in_len = 0, out_len = 0;
589 static char out_env[ND_CMD_MAX_ENVELOPE];
590 static char in_env[ND_CMD_MAX_ENVELOPE];
591 const struct nd_cmd_desc *desc = NULL;
592 unsigned int cmd = _IOC_NR(ioctl_cmd);
593 void __user *p = (void __user *) arg;
594 struct device *dev = &nvdimm_bus->dev;
595 const char *cmd_name, *dimm_name;
596 unsigned long dsm_mask;
597 void *buf;
598 int rc, i;
599
600 if (nvdimm) {
601 desc = nd_cmd_dimm_desc(cmd);
602 cmd_name = nvdimm_cmd_name(cmd);
603 dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0;
604 dimm_name = dev_name(&nvdimm->dev);
605 } else {
606 desc = nd_cmd_bus_desc(cmd);
607 cmd_name = nvdimm_bus_cmd_name(cmd);
608 dsm_mask = nd_desc->dsm_mask;
609 dimm_name = "bus";
610 }
611
612 if (!desc || (desc->out_num + desc->in_num == 0) ||
613 !test_bit(cmd, &dsm_mask))
614 return -ENOTTY;
615
616 /* fail write commands (when read-only) */
617 if (read_only)
618 switch (cmd) {
619 case ND_CMD_VENDOR:
620 case ND_CMD_SET_CONFIG_DATA:
621 case ND_CMD_ARS_START:
622 case ND_CMD_CLEAR_ERROR:
623 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
624 nvdimm ? nvdimm_cmd_name(cmd)
625 : nvdimm_bus_cmd_name(cmd));
626 return -EPERM;
627 default:
628 break;
629 }
630
631 /* process an input envelope */
632 for (i = 0; i < desc->in_num; i++) {
633 u32 in_size, copy;
634
635 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
636 if (in_size == UINT_MAX) {
637 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
638 __func__, dimm_name, cmd_name, i);
639 return -ENXIO;
640 }
641 if (in_len < sizeof(in_env))
642 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
643 else
644 copy = 0;
645 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
646 return -EFAULT;
647 in_len += in_size;
648 }
649
650 /* process an output envelope */
651 for (i = 0; i < desc->out_num; i++) {
652 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
653 (u32 *) in_env, (u32 *) out_env);
654 u32 copy;
655
656 if (out_size == UINT_MAX) {
657 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
658 __func__, dimm_name, cmd_name, i);
659 return -EFAULT;
660 }
661 if (out_len < sizeof(out_env))
662 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
663 else
664 copy = 0;
665 if (copy && copy_from_user(&out_env[out_len],
666 p + in_len + out_len, copy))
667 return -EFAULT;
668 out_len += out_size;
669 }
670
671 buf_len = out_len + in_len;
672 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
673 dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
674 dimm_name, cmd_name, buf_len,
675 ND_IOCTL_MAX_BUFLEN);
676 return -EINVAL;
677 }
678
679 buf = vmalloc(buf_len);
680 if (!buf)
681 return -ENOMEM;
682
683 if (copy_from_user(buf, p, buf_len)) {
684 rc = -EFAULT;
685 goto out;
686 }
687
688 nvdimm_bus_lock(&nvdimm_bus->dev);
689 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, cmd);
690 if (rc)
691 goto out_unlock;
692
693 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, NULL);
694 if (rc < 0)
695 goto out_unlock;
696 if (copy_to_user(p, buf, buf_len))
697 rc = -EFAULT;
698 out_unlock:
699 nvdimm_bus_unlock(&nvdimm_bus->dev);
700 out:
701 vfree(buf);
702 return rc;
703 }
704
705 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
706 {
707 long id = (long) file->private_data;
708 int rc = -ENXIO, ro;
709 struct nvdimm_bus *nvdimm_bus;
710
711 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
712 mutex_lock(&nvdimm_bus_list_mutex);
713 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
714 if (nvdimm_bus->id == id) {
715 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
716 break;
717 }
718 }
719 mutex_unlock(&nvdimm_bus_list_mutex);
720
721 return rc;
722 }
723
724 static int match_dimm(struct device *dev, void *data)
725 {
726 long id = (long) data;
727
728 if (is_nvdimm(dev)) {
729 struct nvdimm *nvdimm = to_nvdimm(dev);
730
731 return nvdimm->id == id;
732 }
733
734 return 0;
735 }
736
737 static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
738 {
739 int rc = -ENXIO, ro;
740 struct nvdimm_bus *nvdimm_bus;
741
742 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
743 mutex_lock(&nvdimm_bus_list_mutex);
744 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
745 struct device *dev = device_find_child(&nvdimm_bus->dev,
746 file->private_data, match_dimm);
747 struct nvdimm *nvdimm;
748
749 if (!dev)
750 continue;
751
752 nvdimm = to_nvdimm(dev);
753 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
754 put_device(dev);
755 break;
756 }
757 mutex_unlock(&nvdimm_bus_list_mutex);
758
759 return rc;
760 }
761
762 static int nd_open(struct inode *inode, struct file *file)
763 {
764 long minor = iminor(inode);
765
766 file->private_data = (void *) minor;
767 return 0;
768 }
769
770 static const struct file_operations nvdimm_bus_fops = {
771 .owner = THIS_MODULE,
772 .open = nd_open,
773 .unlocked_ioctl = nd_ioctl,
774 .compat_ioctl = nd_ioctl,
775 .llseek = noop_llseek,
776 };
777
778 static const struct file_operations nvdimm_fops = {
779 .owner = THIS_MODULE,
780 .open = nd_open,
781 .unlocked_ioctl = nvdimm_ioctl,
782 .compat_ioctl = nvdimm_ioctl,
783 .llseek = noop_llseek,
784 };
785
786 int __init nvdimm_bus_init(void)
787 {
788 int rc;
789
790 BUILD_BUG_ON(sizeof(struct nd_smart_payload) != 128);
791 BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload) != 8);
792
793 rc = bus_register(&nvdimm_bus_type);
794 if (rc)
795 return rc;
796
797 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
798 if (rc < 0)
799 goto err_bus_chrdev;
800 nvdimm_bus_major = rc;
801
802 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
803 if (rc < 0)
804 goto err_dimm_chrdev;
805 nvdimm_major = rc;
806
807 nd_class = class_create(THIS_MODULE, "nd");
808 if (IS_ERR(nd_class)) {
809 rc = PTR_ERR(nd_class);
810 goto err_class;
811 }
812
813 return 0;
814
815 err_class:
816 unregister_chrdev(nvdimm_major, "dimmctl");
817 err_dimm_chrdev:
818 unregister_chrdev(nvdimm_bus_major, "ndctl");
819 err_bus_chrdev:
820 bus_unregister(&nvdimm_bus_type);
821
822 return rc;
823 }
824
825 void nvdimm_bus_exit(void)
826 {
827 class_destroy(nd_class);
828 unregister_chrdev(nvdimm_bus_major, "ndctl");
829 unregister_chrdev(nvdimm_major, "dimmctl");
830 bus_unregister(&nvdimm_bus_type);
831 }
This page took 0.046916 seconds and 5 git commands to generate.