blob: fddc3f2a8f80cf9221a84b23f730dd98f65a518f [file] [log] [blame]
Dan Williams45def222015-04-26 19:26:48 -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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Dan Williams62232e452015-06-08 14:27:06 -040014#include <linux/vmalloc.h>
Dan Williams45def222015-04-26 19:26:48 -040015#include <linux/uaccess.h>
Dan Williams3d880022015-05-31 15:02:11 -040016#include <linux/module.h>
Dan Williams45def222015-04-26 19:26:48 -040017#include <linux/fcntl.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -040018#include <linux/async.h>
Dan Williams62232e452015-06-08 14:27:06 -040019#include <linux/ndctl.h>
Dan Williams4d88a972015-05-31 14:41:48 -040020#include <linux/sched.h>
Dan Williams45def222015-04-26 19:26:48 -040021#include <linux/slab.h>
22#include <linux/fs.h>
23#include <linux/io.h>
Dan Williams62232e452015-06-08 14:27:06 -040024#include <linux/mm.h>
Dan Williams4d88a972015-05-31 14:41:48 -040025#include <linux/nd.h>
Dan Williams45def222015-04-26 19:26:48 -040026#include "nd-core.h"
Dan Williams4d88a972015-05-31 14:41:48 -040027#include "nd.h"
Dan Williams45def222015-04-26 19:26:48 -040028
Dan Williams62232e452015-06-08 14:27:06 -040029int nvdimm_major;
Dan Williams45def222015-04-26 19:26:48 -040030static int nvdimm_bus_major;
31static struct class *nd_class;
32
Dan Williams4d88a972015-05-31 14:41:48 -040033static int to_nd_device_type(struct device *dev)
34{
35 if (is_nvdimm(dev))
36 return ND_DEVICE_DIMM;
Dan Williams3d880022015-05-31 15:02:11 -040037 else if (is_nd_pmem(dev))
38 return ND_DEVICE_REGION_PMEM;
39 else if (is_nd_blk(dev))
40 return ND_DEVICE_REGION_BLK;
41 else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent))
42 return nd_region_to_nstype(to_nd_region(dev->parent));
Dan Williams4d88a972015-05-31 14:41:48 -040043
44 return 0;
45}
46
47static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
48{
49 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
50 to_nd_device_type(dev));
51}
52
53static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
54{
55 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
56
57 return test_bit(to_nd_device_type(dev), &nd_drv->type);
58}
59
Dan Williams3d880022015-05-31 15:02:11 -040060static struct module *to_bus_provider(struct device *dev)
61{
62 /* pin bus providers while regions are enabled */
63 if (is_nd_pmem(dev) || is_nd_blk(dev)) {
64 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
65
66 return nvdimm_bus->module;
67 }
68 return NULL;
69}
70
Dan Williamseaf96152015-05-01 13:11:27 -040071static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
72{
73 nvdimm_bus_lock(&nvdimm_bus->dev);
74 nvdimm_bus->probe_active++;
75 nvdimm_bus_unlock(&nvdimm_bus->dev);
76}
77
78static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
79{
80 nvdimm_bus_lock(&nvdimm_bus->dev);
81 if (--nvdimm_bus->probe_active == 0)
82 wake_up(&nvdimm_bus->probe_wait);
83 nvdimm_bus_unlock(&nvdimm_bus->dev);
84}
85
Dan Williams4d88a972015-05-31 14:41:48 -040086static int nvdimm_bus_probe(struct device *dev)
87{
88 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -040089 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040090 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
91 int rc;
92
Dan Williams3d880022015-05-31 15:02:11 -040093 if (!try_module_get(provider))
94 return -ENXIO;
95
Dan Williamseaf96152015-05-01 13:11:27 -040096 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -040097 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -040098 if (rc == 0)
99 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400100 else
101 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400102 nvdimm_bus_probe_end(nvdimm_bus);
103
Dan Williams4d88a972015-05-31 14:41:48 -0400104 dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
105 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400106 if (rc != 0)
107 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400108 return rc;
109}
110
111static int nvdimm_bus_remove(struct device *dev)
112{
113 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -0400114 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400115 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
116 int rc;
117
118 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400119 nd_region_disable(nvdimm_bus, dev);
120
Dan Williams4d88a972015-05-31 14:41:48 -0400121 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
122 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400123 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400124 return rc;
125}
126
127static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400128 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400129 .uevent = nvdimm_bus_uevent,
130 .match = nvdimm_bus_match,
131 .probe = nvdimm_bus_probe,
132 .remove = nvdimm_bus_remove,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400133};
134
Dan Williams4d88a972015-05-31 14:41:48 -0400135static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
136
137void nd_synchronize(void)
138{
139 async_synchronize_full_domain(&nd_async_domain);
140}
141EXPORT_SYMBOL_GPL(nd_synchronize);
142
143static void nd_async_device_register(void *d, async_cookie_t cookie)
144{
145 struct device *dev = d;
146
147 if (device_add(dev) != 0) {
148 dev_err(dev, "%s: failed\n", __func__);
149 put_device(dev);
150 }
151 put_device(dev);
152}
153
154static void nd_async_device_unregister(void *d, async_cookie_t cookie)
155{
156 struct device *dev = d;
157
158 device_unregister(dev);
159 put_device(dev);
160}
161
162void nd_device_register(struct device *dev)
163{
164 dev->bus = &nvdimm_bus_type;
165 device_initialize(dev);
166 get_device(dev);
167 async_schedule_domain(nd_async_device_register, dev,
168 &nd_async_domain);
169}
170EXPORT_SYMBOL(nd_device_register);
171
172void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
173{
174 switch (mode) {
175 case ND_ASYNC:
176 get_device(dev);
177 async_schedule_domain(nd_async_device_unregister, dev,
178 &nd_async_domain);
179 break;
180 case ND_SYNC:
181 nd_synchronize();
182 device_unregister(dev);
183 break;
184 }
185}
186EXPORT_SYMBOL(nd_device_unregister);
187
188/**
189 * __nd_driver_register() - register a region or a namespace driver
190 * @nd_drv: driver to register
191 * @owner: automatically set by nd_driver_register() macro
192 * @mod_name: automatically set by nd_driver_register() macro
193 */
194int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
195 const char *mod_name)
196{
197 struct device_driver *drv = &nd_drv->drv;
198
199 if (!nd_drv->type) {
200 pr_debug("driver type bitmask not set (%pf)\n",
201 __builtin_return_address(0));
202 return -EINVAL;
203 }
204
205 if (!nd_drv->probe || !nd_drv->remove) {
206 pr_debug("->probe() and ->remove() must be specified\n");
207 return -EINVAL;
208 }
209
210 drv->bus = &nvdimm_bus_type;
211 drv->owner = owner;
212 drv->mod_name = mod_name;
213
214 return driver_register(drv);
215}
216EXPORT_SYMBOL(__nd_driver_register);
217
218static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
219 char *buf)
220{
221 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
222 to_nd_device_type(dev));
223}
224static DEVICE_ATTR_RO(modalias);
225
226static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
227 char *buf)
228{
229 return sprintf(buf, "%s\n", dev->type->name);
230}
231static DEVICE_ATTR_RO(devtype);
232
233static struct attribute *nd_device_attributes[] = {
234 &dev_attr_modalias.attr,
235 &dev_attr_devtype.attr,
236 NULL,
237};
238
239/**
240 * nd_device_attribute_group - generic attributes for all devices on an nd bus
241 */
242struct attribute_group nd_device_attribute_group = {
243 .attrs = nd_device_attributes,
244};
245EXPORT_SYMBOL_GPL(nd_device_attribute_group);
246
Dan Williams45def222015-04-26 19:26:48 -0400247int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
248{
249 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
250 struct device *dev;
251
252 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
253 "ndctl%d", nvdimm_bus->id);
254
255 if (IS_ERR(dev)) {
256 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
257 nvdimm_bus->id, PTR_ERR(dev));
258 return PTR_ERR(dev);
259 }
260 return 0;
261}
262
263void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
264{
265 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
266}
267
Dan Williams62232e452015-06-08 14:27:06 -0400268static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
269 [ND_CMD_IMPLEMENTED] = { },
270 [ND_CMD_SMART] = {
271 .out_num = 2,
272 .out_sizes = { 4, 8, },
273 },
274 [ND_CMD_SMART_THRESHOLD] = {
275 .out_num = 2,
276 .out_sizes = { 4, 8, },
277 },
278 [ND_CMD_DIMM_FLAGS] = {
279 .out_num = 2,
280 .out_sizes = { 4, 4 },
281 },
282 [ND_CMD_GET_CONFIG_SIZE] = {
283 .out_num = 3,
284 .out_sizes = { 4, 4, 4, },
285 },
286 [ND_CMD_GET_CONFIG_DATA] = {
287 .in_num = 2,
288 .in_sizes = { 4, 4, },
289 .out_num = 2,
290 .out_sizes = { 4, UINT_MAX, },
291 },
292 [ND_CMD_SET_CONFIG_DATA] = {
293 .in_num = 3,
294 .in_sizes = { 4, 4, UINT_MAX, },
295 .out_num = 1,
296 .out_sizes = { 4, },
297 },
298 [ND_CMD_VENDOR] = {
299 .in_num = 3,
300 .in_sizes = { 4, 4, UINT_MAX, },
301 .out_num = 3,
302 .out_sizes = { 4, 4, UINT_MAX, },
303 },
304};
305
306const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
307{
308 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
309 return &__nd_cmd_dimm_descs[cmd];
310 return NULL;
311}
312EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
313
314static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
315 [ND_CMD_IMPLEMENTED] = { },
316 [ND_CMD_ARS_CAP] = {
317 .in_num = 2,
318 .in_sizes = { 8, 8, },
319 .out_num = 2,
320 .out_sizes = { 4, 4, },
321 },
322 [ND_CMD_ARS_START] = {
323 .in_num = 4,
324 .in_sizes = { 8, 8, 2, 6, },
325 .out_num = 1,
326 .out_sizes = { 4, },
327 },
328 [ND_CMD_ARS_STATUS] = {
329 .out_num = 2,
330 .out_sizes = { 4, UINT_MAX, },
331 },
332};
333
334const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
335{
336 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
337 return &__nd_cmd_bus_descs[cmd];
338 return NULL;
339}
340EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
341
342u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
343 const struct nd_cmd_desc *desc, int idx, void *buf)
344{
345 if (idx >= desc->in_num)
346 return UINT_MAX;
347
348 if (desc->in_sizes[idx] < UINT_MAX)
349 return desc->in_sizes[idx];
350
351 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
352 struct nd_cmd_set_config_hdr *hdr = buf;
353
354 return hdr->in_length;
355 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
356 struct nd_cmd_vendor_hdr *hdr = buf;
357
358 return hdr->in_length;
359 }
360
361 return UINT_MAX;
362}
363EXPORT_SYMBOL_GPL(nd_cmd_in_size);
364
365u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
366 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
367 const u32 *out_field)
368{
369 if (idx >= desc->out_num)
370 return UINT_MAX;
371
372 if (desc->out_sizes[idx] < UINT_MAX)
373 return desc->out_sizes[idx];
374
375 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
376 return in_field[1];
377 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
378 return out_field[1];
379 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 1)
380 return ND_CMD_ARS_STATUS_MAX;
381
382 return UINT_MAX;
383}
384EXPORT_SYMBOL_GPL(nd_cmd_out_size);
385
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400386void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400387{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400388 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
389
Dan Williamseaf96152015-05-01 13:11:27 -0400390 do {
391 if (nvdimm_bus->probe_active == 0)
392 break;
393 nvdimm_bus_unlock(&nvdimm_bus->dev);
394 wait_event(nvdimm_bus->probe_wait,
395 nvdimm_bus->probe_active == 0);
396 nvdimm_bus_lock(&nvdimm_bus->dev);
397 } while (true);
398}
399
400/* set_config requires an idle interleave set */
401static int nd_cmd_clear_to_send(struct nvdimm *nvdimm, unsigned int cmd)
402{
403 struct nvdimm_bus *nvdimm_bus;
404
405 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
406 return 0;
407
408 nvdimm_bus = walk_to_nvdimm_bus(&nvdimm->dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400409 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400410
411 if (atomic_read(&nvdimm->busy))
412 return -EBUSY;
413 return 0;
414}
415
Dan Williams62232e452015-06-08 14:27:06 -0400416static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
417 int read_only, unsigned int ioctl_cmd, unsigned long arg)
418{
419 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
420 size_t buf_len = 0, in_len = 0, out_len = 0;
421 static char out_env[ND_CMD_MAX_ENVELOPE];
422 static char in_env[ND_CMD_MAX_ENVELOPE];
423 const struct nd_cmd_desc *desc = NULL;
424 unsigned int cmd = _IOC_NR(ioctl_cmd);
425 void __user *p = (void __user *) arg;
426 struct device *dev = &nvdimm_bus->dev;
427 const char *cmd_name, *dimm_name;
428 unsigned long dsm_mask;
429 void *buf;
430 int rc, i;
431
432 if (nvdimm) {
433 desc = nd_cmd_dimm_desc(cmd);
434 cmd_name = nvdimm_cmd_name(cmd);
435 dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0;
436 dimm_name = dev_name(&nvdimm->dev);
437 } else {
438 desc = nd_cmd_bus_desc(cmd);
439 cmd_name = nvdimm_bus_cmd_name(cmd);
440 dsm_mask = nd_desc->dsm_mask;
441 dimm_name = "bus";
442 }
443
444 if (!desc || (desc->out_num + desc->in_num == 0) ||
445 !test_bit(cmd, &dsm_mask))
446 return -ENOTTY;
447
448 /* fail write commands (when read-only) */
449 if (read_only)
450 switch (ioctl_cmd) {
451 case ND_IOCTL_VENDOR:
452 case ND_IOCTL_SET_CONFIG_DATA:
453 case ND_IOCTL_ARS_START:
454 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
455 nvdimm ? nvdimm_cmd_name(cmd)
456 : nvdimm_bus_cmd_name(cmd));
457 return -EPERM;
458 default:
459 break;
460 }
461
462 /* process an input envelope */
463 for (i = 0; i < desc->in_num; i++) {
464 u32 in_size, copy;
465
466 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
467 if (in_size == UINT_MAX) {
468 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
469 __func__, dimm_name, cmd_name, i);
470 return -ENXIO;
471 }
472 if (!access_ok(VERIFY_READ, p + in_len, in_size))
473 return -EFAULT;
474 if (in_len < sizeof(in_env))
475 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
476 else
477 copy = 0;
478 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
479 return -EFAULT;
480 in_len += in_size;
481 }
482
483 /* process an output envelope */
484 for (i = 0; i < desc->out_num; i++) {
485 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
486 (u32 *) in_env, (u32 *) out_env);
487 u32 copy;
488
489 if (out_size == UINT_MAX) {
490 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
491 __func__, dimm_name, cmd_name, i);
492 return -EFAULT;
493 }
494 if (!access_ok(VERIFY_WRITE, p + in_len + out_len, out_size))
495 return -EFAULT;
496 if (out_len < sizeof(out_env))
497 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
498 else
499 copy = 0;
500 if (copy && copy_from_user(&out_env[out_len],
501 p + in_len + out_len, copy))
502 return -EFAULT;
503 out_len += out_size;
504 }
505
506 buf_len = out_len + in_len;
507 if (!access_ok(VERIFY_WRITE, p, sizeof(buf_len)))
508 return -EFAULT;
509
510 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
511 dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
512 dimm_name, cmd_name, buf_len,
513 ND_IOCTL_MAX_BUFLEN);
514 return -EINVAL;
515 }
516
517 buf = vmalloc(buf_len);
518 if (!buf)
519 return -ENOMEM;
520
521 if (copy_from_user(buf, p, buf_len)) {
522 rc = -EFAULT;
523 goto out;
524 }
525
Dan Williamseaf96152015-05-01 13:11:27 -0400526 nvdimm_bus_lock(&nvdimm_bus->dev);
527 rc = nd_cmd_clear_to_send(nvdimm, cmd);
528 if (rc)
529 goto out_unlock;
530
Dan Williams62232e452015-06-08 14:27:06 -0400531 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len);
532 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -0400533 goto out_unlock;
Dan Williams62232e452015-06-08 14:27:06 -0400534 if (copy_to_user(p, buf, buf_len))
535 rc = -EFAULT;
Dan Williamseaf96152015-05-01 13:11:27 -0400536 out_unlock:
537 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -0400538 out:
539 vfree(buf);
540 return rc;
541}
542
Dan Williams45def222015-04-26 19:26:48 -0400543static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
544{
Dan Williams62232e452015-06-08 14:27:06 -0400545 long id = (long) file->private_data;
546 int rc = -ENXIO, read_only;
547 struct nvdimm_bus *nvdimm_bus;
548
549 read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
550 mutex_lock(&nvdimm_bus_list_mutex);
551 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
552 if (nvdimm_bus->id == id) {
553 rc = __nd_ioctl(nvdimm_bus, NULL, read_only, cmd, arg);
554 break;
555 }
556 }
557 mutex_unlock(&nvdimm_bus_list_mutex);
558
559 return rc;
560}
561
562static int match_dimm(struct device *dev, void *data)
563{
564 long id = (long) data;
565
566 if (is_nvdimm(dev)) {
567 struct nvdimm *nvdimm = to_nvdimm(dev);
568
569 return nvdimm->id == id;
570 }
571
572 return 0;
573}
574
575static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
576{
577 int rc = -ENXIO, read_only;
578 struct nvdimm_bus *nvdimm_bus;
579
580 read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
581 mutex_lock(&nvdimm_bus_list_mutex);
582 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
583 struct device *dev = device_find_child(&nvdimm_bus->dev,
584 file->private_data, match_dimm);
585 struct nvdimm *nvdimm;
586
587 if (!dev)
588 continue;
589
590 nvdimm = to_nvdimm(dev);
591 rc = __nd_ioctl(nvdimm_bus, nvdimm, read_only, cmd, arg);
592 put_device(dev);
593 break;
594 }
595 mutex_unlock(&nvdimm_bus_list_mutex);
596
597 return rc;
598}
599
600static int nd_open(struct inode *inode, struct file *file)
601{
602 long minor = iminor(inode);
603
604 file->private_data = (void *) minor;
605 return 0;
Dan Williams45def222015-04-26 19:26:48 -0400606}
607
608static const struct file_operations nvdimm_bus_fops = {
609 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -0400610 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -0400611 .unlocked_ioctl = nd_ioctl,
612 .compat_ioctl = nd_ioctl,
613 .llseek = noop_llseek,
614};
615
Dan Williams62232e452015-06-08 14:27:06 -0400616static const struct file_operations nvdimm_fops = {
617 .owner = THIS_MODULE,
618 .open = nd_open,
619 .unlocked_ioctl = nvdimm_ioctl,
620 .compat_ioctl = nvdimm_ioctl,
621 .llseek = noop_llseek,
622};
623
Dan Williams45def222015-04-26 19:26:48 -0400624int __init nvdimm_bus_init(void)
625{
626 int rc;
627
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400628 rc = bus_register(&nvdimm_bus_type);
629 if (rc)
630 return rc;
631
Dan Williams45def222015-04-26 19:26:48 -0400632 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
633 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -0400634 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -0400635 nvdimm_bus_major = rc;
636
Dan Williams62232e452015-06-08 14:27:06 -0400637 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
638 if (rc < 0)
639 goto err_dimm_chrdev;
640 nvdimm_major = rc;
641
Dan Williams45def222015-04-26 19:26:48 -0400642 nd_class = class_create(THIS_MODULE, "nd");
643 if (IS_ERR(nd_class))
644 goto err_class;
645
646 return 0;
647
648 err_class:
Dan Williams62232e452015-06-08 14:27:06 -0400649 unregister_chrdev(nvdimm_major, "dimmctl");
650 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -0400651 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -0400652 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400653 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -0400654
655 return rc;
656}
657
Dan Williams4d88a972015-05-31 14:41:48 -0400658void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -0400659{
660 class_destroy(nd_class);
661 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -0400662 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400663 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -0400664}