blob: cb6fd64b13e38b9ec99baabeaba52e3f380ee85e [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 },
Dan Williamsd4f32362016-03-03 16:08:54 -0800424 [ND_CMD_CLEAR_ERROR] = {
425 .in_num = 2,
426 .in_sizes = { 8, 8, },
427 .out_num = 3,
428 .out_sizes = { 4, 4, 8, },
429 },
Dan Williams62232e452015-06-08 14:27:06 -0400430};
431
432const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
433{
434 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
435 return &__nd_cmd_bus_descs[cmd];
436 return NULL;
437}
438EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
439
440u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
441 const struct nd_cmd_desc *desc, int idx, void *buf)
442{
443 if (idx >= desc->in_num)
444 return UINT_MAX;
445
446 if (desc->in_sizes[idx] < UINT_MAX)
447 return desc->in_sizes[idx];
448
449 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
450 struct nd_cmd_set_config_hdr *hdr = buf;
451
452 return hdr->in_length;
453 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
454 struct nd_cmd_vendor_hdr *hdr = buf;
455
456 return hdr->in_length;
457 }
458
459 return UINT_MAX;
460}
461EXPORT_SYMBOL_GPL(nd_cmd_in_size);
462
463u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
464 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
465 const u32 *out_field)
466{
467 if (idx >= desc->out_num)
468 return UINT_MAX;
469
470 if (desc->out_sizes[idx] < UINT_MAX)
471 return desc->out_sizes[idx];
472
473 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
474 return in_field[1];
475 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
476 return out_field[1];
Dan Williams747ffe12016-02-19 15:21:14 -0800477 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2)
478 return out_field[1] - 8;
Dan Williams62232e452015-06-08 14:27:06 -0400479
480 return UINT_MAX;
481}
482EXPORT_SYMBOL_GPL(nd_cmd_out_size);
483
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400484void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400485{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400486 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
487
Dan Williamseaf96152015-05-01 13:11:27 -0400488 do {
489 if (nvdimm_bus->probe_active == 0)
490 break;
491 nvdimm_bus_unlock(&nvdimm_bus->dev);
492 wait_event(nvdimm_bus->probe_wait,
493 nvdimm_bus->probe_active == 0);
494 nvdimm_bus_lock(&nvdimm_bus->dev);
495 } while (true);
496}
497
Dan Williamsd4f32362016-03-03 16:08:54 -0800498static int pmem_active(struct device *dev, void *data)
499{
500 if (is_nd_pmem(dev) && dev->driver)
501 return -EBUSY;
502 return 0;
503}
504
Dan Williamseaf96152015-05-01 13:11:27 -0400505/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800506static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
507 struct nvdimm *nvdimm, unsigned int cmd)
Dan Williamseaf96152015-05-01 13:11:27 -0400508{
Dan Williams87bf5722016-02-22 21:50:31 -0800509 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
510
511 /* ask the bus provider if it would like to block this request */
512 if (nd_desc->clear_to_send) {
513 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
514
515 if (rc)
516 return rc;
517 }
Dan Williamseaf96152015-05-01 13:11:27 -0400518
Dan Williamsd4f32362016-03-03 16:08:54 -0800519 /* require clear error to go through the pmem driver */
520 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
521 return device_for_each_child(&nvdimm_bus->dev, NULL,
522 pmem_active);
523
Dan Williamseaf96152015-05-01 13:11:27 -0400524 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
525 return 0;
526
Dan Williams87bf5722016-02-22 21:50:31 -0800527 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400528 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400529 if (atomic_read(&nvdimm->busy))
530 return -EBUSY;
531 return 0;
532}
533
Dan Williams62232e452015-06-08 14:27:06 -0400534static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
535 int read_only, unsigned int ioctl_cmd, unsigned long arg)
536{
537 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
538 size_t buf_len = 0, in_len = 0, out_len = 0;
539 static char out_env[ND_CMD_MAX_ENVELOPE];
540 static char in_env[ND_CMD_MAX_ENVELOPE];
541 const struct nd_cmd_desc *desc = NULL;
542 unsigned int cmd = _IOC_NR(ioctl_cmd);
543 void __user *p = (void __user *) arg;
544 struct device *dev = &nvdimm_bus->dev;
545 const char *cmd_name, *dimm_name;
546 unsigned long dsm_mask;
547 void *buf;
548 int rc, i;
549
550 if (nvdimm) {
551 desc = nd_cmd_dimm_desc(cmd);
552 cmd_name = nvdimm_cmd_name(cmd);
553 dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0;
554 dimm_name = dev_name(&nvdimm->dev);
555 } else {
556 desc = nd_cmd_bus_desc(cmd);
557 cmd_name = nvdimm_bus_cmd_name(cmd);
558 dsm_mask = nd_desc->dsm_mask;
559 dimm_name = "bus";
560 }
561
562 if (!desc || (desc->out_num + desc->in_num == 0) ||
563 !test_bit(cmd, &dsm_mask))
564 return -ENOTTY;
565
566 /* fail write commands (when read-only) */
567 if (read_only)
Jerry Hoemann07accfa2016-01-06 16:03:41 -0700568 switch (cmd) {
569 case ND_CMD_VENDOR:
570 case ND_CMD_SET_CONFIG_DATA:
571 case ND_CMD_ARS_START:
Dan Williamsd4f32362016-03-03 16:08:54 -0800572 case ND_CMD_CLEAR_ERROR:
Dan Williams62232e452015-06-08 14:27:06 -0400573 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
574 nvdimm ? nvdimm_cmd_name(cmd)
575 : nvdimm_bus_cmd_name(cmd));
576 return -EPERM;
577 default:
578 break;
579 }
580
581 /* process an input envelope */
582 for (i = 0; i < desc->in_num; i++) {
583 u32 in_size, copy;
584
585 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
586 if (in_size == UINT_MAX) {
587 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
588 __func__, dimm_name, cmd_name, i);
589 return -ENXIO;
590 }
Dan Williams62232e452015-06-08 14:27:06 -0400591 if (in_len < sizeof(in_env))
592 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
593 else
594 copy = 0;
595 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
596 return -EFAULT;
597 in_len += in_size;
598 }
599
600 /* process an output envelope */
601 for (i = 0; i < desc->out_num; i++) {
602 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
603 (u32 *) in_env, (u32 *) out_env);
604 u32 copy;
605
606 if (out_size == UINT_MAX) {
607 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
608 __func__, dimm_name, cmd_name, i);
609 return -EFAULT;
610 }
Dan Williams62232e452015-06-08 14:27:06 -0400611 if (out_len < sizeof(out_env))
612 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
613 else
614 copy = 0;
615 if (copy && copy_from_user(&out_env[out_len],
616 p + in_len + out_len, copy))
617 return -EFAULT;
618 out_len += out_size;
619 }
620
621 buf_len = out_len + in_len;
Dan Williams62232e452015-06-08 14:27:06 -0400622 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
623 dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
624 dimm_name, cmd_name, buf_len,
625 ND_IOCTL_MAX_BUFLEN);
626 return -EINVAL;
627 }
628
629 buf = vmalloc(buf_len);
630 if (!buf)
631 return -ENOMEM;
632
633 if (copy_from_user(buf, p, buf_len)) {
634 rc = -EFAULT;
635 goto out;
636 }
637
Dan Williamseaf96152015-05-01 13:11:27 -0400638 nvdimm_bus_lock(&nvdimm_bus->dev);
Dan Williams87bf5722016-02-22 21:50:31 -0800639 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, cmd);
Dan Williamseaf96152015-05-01 13:11:27 -0400640 if (rc)
641 goto out_unlock;
642
Dan Williamsaef25332016-02-12 17:01:11 -0800643 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, NULL);
Dan Williams62232e452015-06-08 14:27:06 -0400644 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -0400645 goto out_unlock;
Dan Williams62232e452015-06-08 14:27:06 -0400646 if (copy_to_user(p, buf, buf_len))
647 rc = -EFAULT;
Dan Williamseaf96152015-05-01 13:11:27 -0400648 out_unlock:
649 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -0400650 out:
651 vfree(buf);
652 return rc;
653}
654
Dan Williams45def222015-04-26 19:26:48 -0400655static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
656{
Dan Williams62232e452015-06-08 14:27:06 -0400657 long id = (long) file->private_data;
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -0700658 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -0400659 struct nvdimm_bus *nvdimm_bus;
660
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -0700661 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -0400662 mutex_lock(&nvdimm_bus_list_mutex);
663 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
664 if (nvdimm_bus->id == id) {
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -0700665 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -0400666 break;
667 }
668 }
669 mutex_unlock(&nvdimm_bus_list_mutex);
670
671 return rc;
672}
673
674static int match_dimm(struct device *dev, void *data)
675{
676 long id = (long) data;
677
678 if (is_nvdimm(dev)) {
679 struct nvdimm *nvdimm = to_nvdimm(dev);
680
681 return nvdimm->id == id;
682 }
683
684 return 0;
685}
686
687static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
688{
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -0700689 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -0400690 struct nvdimm_bus *nvdimm_bus;
691
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -0700692 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -0400693 mutex_lock(&nvdimm_bus_list_mutex);
694 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
695 struct device *dev = device_find_child(&nvdimm_bus->dev,
696 file->private_data, match_dimm);
697 struct nvdimm *nvdimm;
698
699 if (!dev)
700 continue;
701
702 nvdimm = to_nvdimm(dev);
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -0700703 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -0400704 put_device(dev);
705 break;
706 }
707 mutex_unlock(&nvdimm_bus_list_mutex);
708
709 return rc;
710}
711
712static int nd_open(struct inode *inode, struct file *file)
713{
714 long minor = iminor(inode);
715
716 file->private_data = (void *) minor;
717 return 0;
Dan Williams45def222015-04-26 19:26:48 -0400718}
719
720static const struct file_operations nvdimm_bus_fops = {
721 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -0400722 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -0400723 .unlocked_ioctl = nd_ioctl,
724 .compat_ioctl = nd_ioctl,
725 .llseek = noop_llseek,
726};
727
Dan Williams62232e452015-06-08 14:27:06 -0400728static const struct file_operations nvdimm_fops = {
729 .owner = THIS_MODULE,
730 .open = nd_open,
731 .unlocked_ioctl = nvdimm_ioctl,
732 .compat_ioctl = nvdimm_ioctl,
733 .llseek = noop_llseek,
734};
735
Dan Williams45def222015-04-26 19:26:48 -0400736int __init nvdimm_bus_init(void)
737{
738 int rc;
739
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400740 rc = bus_register(&nvdimm_bus_type);
741 if (rc)
742 return rc;
743
Dan Williams45def222015-04-26 19:26:48 -0400744 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
745 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -0400746 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -0400747 nvdimm_bus_major = rc;
748
Dan Williams62232e452015-06-08 14:27:06 -0400749 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
750 if (rc < 0)
751 goto err_dimm_chrdev;
752 nvdimm_major = rc;
753
Dan Williams45def222015-04-26 19:26:48 -0400754 nd_class = class_create(THIS_MODULE, "nd");
Axel Lindaa1dee2015-06-28 17:00:57 +0800755 if (IS_ERR(nd_class)) {
756 rc = PTR_ERR(nd_class);
Dan Williams45def222015-04-26 19:26:48 -0400757 goto err_class;
Axel Lindaa1dee2015-06-28 17:00:57 +0800758 }
Dan Williams45def222015-04-26 19:26:48 -0400759
760 return 0;
761
762 err_class:
Dan Williams62232e452015-06-08 14:27:06 -0400763 unregister_chrdev(nvdimm_major, "dimmctl");
764 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -0400765 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -0400766 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400767 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -0400768
769 return rc;
770}
771
Dan Williams4d88a972015-05-31 14:41:48 -0400772void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -0400773{
774 class_destroy(nd_class);
775 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -0400776 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400777 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -0400778}