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