blob: 228c0e9f430e7b6a931162acda0571fadc7eed8d [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 Williams8c2f7e82015-06-25 04:20:04 -040017#include <linux/blkdev.h>
Dan Williams45def222015-04-26 19:26:48 -040018#include <linux/fcntl.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -040019#include <linux/async.h>
Dan Williams8c2f7e82015-06-25 04:20:04 -040020#include <linux/genhd.h>
Dan Williams62232e452015-06-08 14:27:06 -040021#include <linux/ndctl.h>
Dan Williams4d88a972015-05-31 14:41:48 -040022#include <linux/sched.h>
Dan Williams45def222015-04-26 19:26:48 -040023#include <linux/slab.h>
24#include <linux/fs.h>
25#include <linux/io.h>
Dan Williams62232e452015-06-08 14:27:06 -040026#include <linux/mm.h>
Dan Williams4d88a972015-05-31 14:41:48 -040027#include <linux/nd.h>
Dan Williams45def222015-04-26 19:26:48 -040028#include "nd-core.h"
Dan Williams4d88a972015-05-31 14:41:48 -040029#include "nd.h"
Dan Williams45def222015-04-26 19:26:48 -040030
Dan Williams62232e452015-06-08 14:27:06 -040031int nvdimm_major;
Dan Williams45def222015-04-26 19:26:48 -040032static int nvdimm_bus_major;
33static struct class *nd_class;
34
Dan Williams4d88a972015-05-31 14:41:48 -040035static int to_nd_device_type(struct device *dev)
36{
37 if (is_nvdimm(dev))
38 return ND_DEVICE_DIMM;
Dan Williams3d880022015-05-31 15:02:11 -040039 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));
Dan Williams4d88a972015-05-31 14:41:48 -040045
46 return 0;
47}
48
49static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
50{
Toshi Kani41d7a6d2015-06-19 12:18:33 -060051 /*
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);
Dan Williams4d88a972015-05-31 14:41:48 -040057 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
58 to_nd_device_type(dev));
59}
60
61static 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
Dan Williams3d880022015-05-31 15:02:11 -040068static 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
Dan Williamseaf96152015-05-01 13:11:27 -040079static 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
86static 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
Dan Williams4d88a972015-05-31 14:41:48 -040094static int nvdimm_bus_probe(struct device *dev)
95{
96 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -040097 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040098 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
99 int rc;
100
Dan Williams3d880022015-05-31 15:02:11 -0400101 if (!try_module_get(provider))
102 return -ENXIO;
103
Dan Williamseaf96152015-05-01 13:11:27 -0400104 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -0400105 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400106 if (rc == 0)
107 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400108 else
109 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400110 nvdimm_bus_probe_end(nvdimm_bus);
111
Dan Williams4d88a972015-05-31 14:41:48 -0400112 dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
113 dev_name(dev), rc);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400114
Dan Williams3d880022015-05-31 15:02:11 -0400115 if (rc != 0)
116 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400117 return rc;
118}
119
120static int nvdimm_bus_remove(struct device *dev)
121{
122 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -0400123 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400124 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
125 int rc;
126
127 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400128 nd_region_disable(nvdimm_bus, dev);
129
Dan Williams4d88a972015-05-31 14:41:48 -0400130 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
131 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400132 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400133 return rc;
134}
135
Dan Williams71999462016-02-18 10:29:49 -0800136void nd_device_notify(struct device *dev, enum nvdimm_event event)
137{
138 device_lock(dev);
139 if (dev->driver) {
140 struct nd_device_driver *nd_drv;
141
142 nd_drv = to_nd_device_driver(dev->driver);
143 if (nd_drv->notify)
144 nd_drv->notify(dev, event);
145 }
146 device_unlock(dev);
147}
148EXPORT_SYMBOL(nd_device_notify);
149
150void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
151{
152 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
153
154 if (!nvdimm_bus)
155 return;
156
157 /* caller is responsible for holding a reference on the device */
158 nd_device_notify(&nd_region->dev, event);
159}
160EXPORT_SYMBOL_GPL(nvdimm_region_notify);
161
Dan Williams4d88a972015-05-31 14:41:48 -0400162static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400163 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400164 .uevent = nvdimm_bus_uevent,
165 .match = nvdimm_bus_match,
166 .probe = nvdimm_bus_probe,
167 .remove = nvdimm_bus_remove,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400168};
169
Dan Williams4d88a972015-05-31 14:41:48 -0400170static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
171
172void nd_synchronize(void)
173{
174 async_synchronize_full_domain(&nd_async_domain);
175}
176EXPORT_SYMBOL_GPL(nd_synchronize);
177
178static void nd_async_device_register(void *d, async_cookie_t cookie)
179{
180 struct device *dev = d;
181
182 if (device_add(dev) != 0) {
183 dev_err(dev, "%s: failed\n", __func__);
184 put_device(dev);
185 }
186 put_device(dev);
187}
188
189static void nd_async_device_unregister(void *d, async_cookie_t cookie)
190{
191 struct device *dev = d;
192
Dan Williams0ba1c632015-05-30 12:35:36 -0400193 /* flush bus operations before delete */
194 nvdimm_bus_lock(dev);
195 nvdimm_bus_unlock(dev);
196
Dan Williams4d88a972015-05-31 14:41:48 -0400197 device_unregister(dev);
198 put_device(dev);
199}
200
Dan Williams8c2f7e82015-06-25 04:20:04 -0400201void __nd_device_register(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400202{
203 dev->bus = &nvdimm_bus_type;
Dan Williams4d88a972015-05-31 14:41:48 -0400204 get_device(dev);
205 async_schedule_domain(nd_async_device_register, dev,
206 &nd_async_domain);
207}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400208
209void nd_device_register(struct device *dev)
210{
211 device_initialize(dev);
212 __nd_device_register(dev);
213}
Dan Williams4d88a972015-05-31 14:41:48 -0400214EXPORT_SYMBOL(nd_device_register);
215
216void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
217{
218 switch (mode) {
219 case ND_ASYNC:
220 get_device(dev);
221 async_schedule_domain(nd_async_device_unregister, dev,
222 &nd_async_domain);
223 break;
224 case ND_SYNC:
225 nd_synchronize();
226 device_unregister(dev);
227 break;
228 }
229}
230EXPORT_SYMBOL(nd_device_unregister);
231
232/**
233 * __nd_driver_register() - register a region or a namespace driver
234 * @nd_drv: driver to register
235 * @owner: automatically set by nd_driver_register() macro
236 * @mod_name: automatically set by nd_driver_register() macro
237 */
238int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
239 const char *mod_name)
240{
241 struct device_driver *drv = &nd_drv->drv;
242
243 if (!nd_drv->type) {
244 pr_debug("driver type bitmask not set (%pf)\n",
245 __builtin_return_address(0));
246 return -EINVAL;
247 }
248
249 if (!nd_drv->probe || !nd_drv->remove) {
250 pr_debug("->probe() and ->remove() must be specified\n");
251 return -EINVAL;
252 }
253
254 drv->bus = &nvdimm_bus_type;
255 drv->owner = owner;
256 drv->mod_name = mod_name;
257
258 return driver_register(drv);
259}
260EXPORT_SYMBOL(__nd_driver_register);
261
Dan Williams58138822015-06-23 20:08:34 -0400262int nvdimm_revalidate_disk(struct gendisk *disk)
263{
264 struct device *dev = disk->driverfs_dev;
265 struct nd_region *nd_region = to_nd_region(dev->parent);
266 const char *pol = nd_region->ro ? "only" : "write";
267
268 if (nd_region->ro == get_disk_ro(disk))
269 return 0;
270
271 dev_info(dev, "%s read-%s, marking %s read-%s\n",
272 dev_name(&nd_region->dev), pol, disk->disk_name, pol);
273 set_disk_ro(disk, nd_region->ro);
274
275 return 0;
276
277}
278EXPORT_SYMBOL(nvdimm_revalidate_disk);
279
Dan Williams4d88a972015-05-31 14:41:48 -0400280static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
281 char *buf)
282{
283 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
284 to_nd_device_type(dev));
285}
286static DEVICE_ATTR_RO(modalias);
287
288static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
289 char *buf)
290{
291 return sprintf(buf, "%s\n", dev->type->name);
292}
293static DEVICE_ATTR_RO(devtype);
294
295static struct attribute *nd_device_attributes[] = {
296 &dev_attr_modalias.attr,
297 &dev_attr_devtype.attr,
298 NULL,
299};
300
301/**
302 * nd_device_attribute_group - generic attributes for all devices on an nd bus
303 */
304struct attribute_group nd_device_attribute_group = {
305 .attrs = nd_device_attributes,
306};
307EXPORT_SYMBOL_GPL(nd_device_attribute_group);
308
Toshi Kani74ae66c2015-06-19 12:18:34 -0600309static ssize_t numa_node_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
311{
312 return sprintf(buf, "%d\n", dev_to_node(dev));
313}
314static DEVICE_ATTR_RO(numa_node);
315
316static struct attribute *nd_numa_attributes[] = {
317 &dev_attr_numa_node.attr,
318 NULL,
319};
320
321static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
322 int n)
323{
324 if (!IS_ENABLED(CONFIG_NUMA))
325 return 0;
326
327 return a->mode;
328}
329
330/**
331 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
332 */
333struct attribute_group nd_numa_attribute_group = {
334 .attrs = nd_numa_attributes,
335 .is_visible = nd_numa_attr_visible,
336};
337EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
338
Dan Williams45def222015-04-26 19:26:48 -0400339int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
340{
341 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
342 struct device *dev;
343
344 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
345 "ndctl%d", nvdimm_bus->id);
346
347 if (IS_ERR(dev)) {
348 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
349 nvdimm_bus->id, PTR_ERR(dev));
350 return PTR_ERR(dev);
351 }
352 return 0;
353}
354
355void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
356{
357 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
358}
359
Dan Williams62232e452015-06-08 14:27:06 -0400360static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
361 [ND_CMD_IMPLEMENTED] = { },
362 [ND_CMD_SMART] = {
363 .out_num = 2,
364 .out_sizes = { 4, 8, },
365 },
366 [ND_CMD_SMART_THRESHOLD] = {
367 .out_num = 2,
368 .out_sizes = { 4, 8, },
369 },
370 [ND_CMD_DIMM_FLAGS] = {
371 .out_num = 2,
372 .out_sizes = { 4, 4 },
373 },
374 [ND_CMD_GET_CONFIG_SIZE] = {
375 .out_num = 3,
376 .out_sizes = { 4, 4, 4, },
377 },
378 [ND_CMD_GET_CONFIG_DATA] = {
379 .in_num = 2,
380 .in_sizes = { 4, 4, },
381 .out_num = 2,
382 .out_sizes = { 4, UINT_MAX, },
383 },
384 [ND_CMD_SET_CONFIG_DATA] = {
385 .in_num = 3,
386 .in_sizes = { 4, 4, UINT_MAX, },
387 .out_num = 1,
388 .out_sizes = { 4, },
389 },
390 [ND_CMD_VENDOR] = {
391 .in_num = 3,
392 .in_sizes = { 4, 4, UINT_MAX, },
393 .out_num = 3,
394 .out_sizes = { 4, 4, UINT_MAX, },
395 },
396};
397
398const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
399{
400 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
401 return &__nd_cmd_dimm_descs[cmd];
402 return NULL;
403}
404EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
405
406static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
407 [ND_CMD_IMPLEMENTED] = { },
408 [ND_CMD_ARS_CAP] = {
409 .in_num = 2,
410 .in_sizes = { 8, 8, },
Dan Williams4577b062016-02-17 13:08:58 -0800411 .out_num = 4,
412 .out_sizes = { 4, 4, 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400413 },
414 [ND_CMD_ARS_START] = {
Dan Williams4577b062016-02-17 13:08:58 -0800415 .in_num = 5,
416 .in_sizes = { 8, 8, 2, 1, 5, },
417 .out_num = 2,
418 .out_sizes = { 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400419 },
420 [ND_CMD_ARS_STATUS] = {
Dan Williams747ffe12016-02-19 15:21:14 -0800421 .out_num = 3,
422 .out_sizes = { 4, 4, UINT_MAX, },
Dan Williams62232e452015-06-08 14:27:06 -0400423 },
424};
425
426const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
427{
428 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
429 return &__nd_cmd_bus_descs[cmd];
430 return NULL;
431}
432EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
433
434u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
435 const struct nd_cmd_desc *desc, int idx, void *buf)
436{
437 if (idx >= desc->in_num)
438 return UINT_MAX;
439
440 if (desc->in_sizes[idx] < UINT_MAX)
441 return desc->in_sizes[idx];
442
443 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
444 struct nd_cmd_set_config_hdr *hdr = buf;
445
446 return hdr->in_length;
447 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
448 struct nd_cmd_vendor_hdr *hdr = buf;
449
450 return hdr->in_length;
451 }
452
453 return UINT_MAX;
454}
455EXPORT_SYMBOL_GPL(nd_cmd_in_size);
456
457u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
458 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
459 const u32 *out_field)
460{
461 if (idx >= desc->out_num)
462 return UINT_MAX;
463
464 if (desc->out_sizes[idx] < UINT_MAX)
465 return desc->out_sizes[idx];
466
467 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
468 return in_field[1];
469 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
470 return out_field[1];
Dan Williams747ffe12016-02-19 15:21:14 -0800471 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2)
472 return out_field[1] - 8;
Dan Williams62232e452015-06-08 14:27:06 -0400473
474 return UINT_MAX;
475}
476EXPORT_SYMBOL_GPL(nd_cmd_out_size);
477
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400478void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400479{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400480 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
481
Dan Williamseaf96152015-05-01 13:11:27 -0400482 do {
483 if (nvdimm_bus->probe_active == 0)
484 break;
485 nvdimm_bus_unlock(&nvdimm_bus->dev);
486 wait_event(nvdimm_bus->probe_wait,
487 nvdimm_bus->probe_active == 0);
488 nvdimm_bus_lock(&nvdimm_bus->dev);
489 } while (true);
490}
491
492/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800493static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
494 struct nvdimm *nvdimm, unsigned int cmd)
Dan Williamseaf96152015-05-01 13:11:27 -0400495{
Dan Williams87bf5722016-02-22 21:50:31 -0800496 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
497
498 /* ask the bus provider if it would like to block this request */
499 if (nd_desc->clear_to_send) {
500 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
501
502 if (rc)
503 return rc;
504 }
Dan Williamseaf96152015-05-01 13:11:27 -0400505
506 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
507 return 0;
508
Dan Williams87bf5722016-02-22 21:50:31 -0800509 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400510 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400511 if (atomic_read(&nvdimm->busy))
512 return -EBUSY;
513 return 0;
514}
515
Dan Williams62232e452015-06-08 14:27:06 -0400516static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
517 int read_only, unsigned int ioctl_cmd, unsigned long arg)
518{
519 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
520 size_t buf_len = 0, in_len = 0, out_len = 0;
521 static char out_env[ND_CMD_MAX_ENVELOPE];
522 static char in_env[ND_CMD_MAX_ENVELOPE];
523 const struct nd_cmd_desc *desc = NULL;
524 unsigned int cmd = _IOC_NR(ioctl_cmd);
525 void __user *p = (void __user *) arg;
526 struct device *dev = &nvdimm_bus->dev;
527 const char *cmd_name, *dimm_name;
528 unsigned long dsm_mask;
529 void *buf;
530 int rc, i;
531
532 if (nvdimm) {
533 desc = nd_cmd_dimm_desc(cmd);
534 cmd_name = nvdimm_cmd_name(cmd);
535 dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0;
536 dimm_name = dev_name(&nvdimm->dev);
537 } else {
538 desc = nd_cmd_bus_desc(cmd);
539 cmd_name = nvdimm_bus_cmd_name(cmd);
540 dsm_mask = nd_desc->dsm_mask;
541 dimm_name = "bus";
542 }
543
544 if (!desc || (desc->out_num + desc->in_num == 0) ||
545 !test_bit(cmd, &dsm_mask))
546 return -ENOTTY;
547
548 /* fail write commands (when read-only) */
549 if (read_only)
550 switch (ioctl_cmd) {
551 case ND_IOCTL_VENDOR:
552 case ND_IOCTL_SET_CONFIG_DATA:
553 case ND_IOCTL_ARS_START:
554 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
555 nvdimm ? nvdimm_cmd_name(cmd)
556 : nvdimm_bus_cmd_name(cmd));
557 return -EPERM;
558 default:
559 break;
560 }
561
562 /* process an input envelope */
563 for (i = 0; i < desc->in_num; i++) {
564 u32 in_size, copy;
565
566 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
567 if (in_size == UINT_MAX) {
568 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
569 __func__, dimm_name, cmd_name, i);
570 return -ENXIO;
571 }
Dan Williams62232e452015-06-08 14:27:06 -0400572 if (in_len < sizeof(in_env))
573 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
574 else
575 copy = 0;
576 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
577 return -EFAULT;
578 in_len += in_size;
579 }
580
581 /* process an output envelope */
582 for (i = 0; i < desc->out_num; i++) {
583 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
584 (u32 *) in_env, (u32 *) out_env);
585 u32 copy;
586
587 if (out_size == UINT_MAX) {
588 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
589 __func__, dimm_name, cmd_name, i);
590 return -EFAULT;
591 }
Dan Williams62232e452015-06-08 14:27:06 -0400592 if (out_len < sizeof(out_env))
593 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
594 else
595 copy = 0;
596 if (copy && copy_from_user(&out_env[out_len],
597 p + in_len + out_len, copy))
598 return -EFAULT;
599 out_len += out_size;
600 }
601
602 buf_len = out_len + in_len;
Dan Williams62232e452015-06-08 14:27:06 -0400603 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
604 dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
605 dimm_name, cmd_name, buf_len,
606 ND_IOCTL_MAX_BUFLEN);
607 return -EINVAL;
608 }
609
610 buf = vmalloc(buf_len);
611 if (!buf)
612 return -ENOMEM;
613
614 if (copy_from_user(buf, p, buf_len)) {
615 rc = -EFAULT;
616 goto out;
617 }
618
Dan Williamseaf96152015-05-01 13:11:27 -0400619 nvdimm_bus_lock(&nvdimm_bus->dev);
Dan Williams87bf5722016-02-22 21:50:31 -0800620 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, cmd);
Dan Williamseaf96152015-05-01 13:11:27 -0400621 if (rc)
622 goto out_unlock;
623
Dan Williamsaef25332016-02-12 17:01:11 -0800624 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, NULL);
Dan Williams62232e452015-06-08 14:27:06 -0400625 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -0400626 goto out_unlock;
Dan Williams62232e452015-06-08 14:27:06 -0400627 if (copy_to_user(p, buf, buf_len))
628 rc = -EFAULT;
Dan Williamseaf96152015-05-01 13:11:27 -0400629 out_unlock:
630 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -0400631 out:
632 vfree(buf);
633 return rc;
634}
635
Dan Williams45def222015-04-26 19:26:48 -0400636static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
637{
Dan Williams62232e452015-06-08 14:27:06 -0400638 long id = (long) file->private_data;
639 int rc = -ENXIO, read_only;
640 struct nvdimm_bus *nvdimm_bus;
641
642 read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
643 mutex_lock(&nvdimm_bus_list_mutex);
644 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
645 if (nvdimm_bus->id == id) {
646 rc = __nd_ioctl(nvdimm_bus, NULL, read_only, cmd, arg);
647 break;
648 }
649 }
650 mutex_unlock(&nvdimm_bus_list_mutex);
651
652 return rc;
653}
654
655static int match_dimm(struct device *dev, void *data)
656{
657 long id = (long) data;
658
659 if (is_nvdimm(dev)) {
660 struct nvdimm *nvdimm = to_nvdimm(dev);
661
662 return nvdimm->id == id;
663 }
664
665 return 0;
666}
667
668static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
669{
670 int rc = -ENXIO, read_only;
671 struct nvdimm_bus *nvdimm_bus;
672
673 read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
674 mutex_lock(&nvdimm_bus_list_mutex);
675 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
676 struct device *dev = device_find_child(&nvdimm_bus->dev,
677 file->private_data, match_dimm);
678 struct nvdimm *nvdimm;
679
680 if (!dev)
681 continue;
682
683 nvdimm = to_nvdimm(dev);
684 rc = __nd_ioctl(nvdimm_bus, nvdimm, read_only, cmd, arg);
685 put_device(dev);
686 break;
687 }
688 mutex_unlock(&nvdimm_bus_list_mutex);
689
690 return rc;
691}
692
693static int nd_open(struct inode *inode, struct file *file)
694{
695 long minor = iminor(inode);
696
697 file->private_data = (void *) minor;
698 return 0;
Dan Williams45def222015-04-26 19:26:48 -0400699}
700
701static const struct file_operations nvdimm_bus_fops = {
702 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -0400703 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -0400704 .unlocked_ioctl = nd_ioctl,
705 .compat_ioctl = nd_ioctl,
706 .llseek = noop_llseek,
707};
708
Dan Williams62232e452015-06-08 14:27:06 -0400709static const struct file_operations nvdimm_fops = {
710 .owner = THIS_MODULE,
711 .open = nd_open,
712 .unlocked_ioctl = nvdimm_ioctl,
713 .compat_ioctl = nvdimm_ioctl,
714 .llseek = noop_llseek,
715};
716
Dan Williams45def222015-04-26 19:26:48 -0400717int __init nvdimm_bus_init(void)
718{
719 int rc;
720
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400721 rc = bus_register(&nvdimm_bus_type);
722 if (rc)
723 return rc;
724
Dan Williams45def222015-04-26 19:26:48 -0400725 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
726 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -0400727 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -0400728 nvdimm_bus_major = rc;
729
Dan Williams62232e452015-06-08 14:27:06 -0400730 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
731 if (rc < 0)
732 goto err_dimm_chrdev;
733 nvdimm_major = rc;
734
Dan Williams45def222015-04-26 19:26:48 -0400735 nd_class = class_create(THIS_MODULE, "nd");
Axel Lindaa1dee2015-06-28 17:00:57 +0800736 if (IS_ERR(nd_class)) {
737 rc = PTR_ERR(nd_class);
Dan Williams45def222015-04-26 19:26:48 -0400738 goto err_class;
Axel Lindaa1dee2015-06-28 17:00:57 +0800739 }
Dan Williams45def222015-04-26 19:26:48 -0400740
741 return 0;
742
743 err_class:
Dan Williams62232e452015-06-08 14:27:06 -0400744 unregister_chrdev(nvdimm_major, "dimmctl");
745 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -0400746 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -0400747 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400748 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -0400749
750 return rc;
751}
752
Dan Williams4d88a972015-05-31 14:41:48 -0400753void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -0400754{
755 class_destroy(nd_class);
756 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -0400757 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400758 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -0400759}