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