blob: 5768a4749564a9b08869992a015e3c0a8796a5e8 [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;
Dan Williams18515942016-07-22 23:46:08 -070034static DEFINE_IDA(nd_ida);
Dan Williams45def222015-04-26 19:26:48 -040035
Dan Williams4d88a972015-05-31 14:41:48 -040036static int to_nd_device_type(struct device *dev)
37{
38 if (is_nvdimm(dev))
39 return ND_DEVICE_DIMM;
Dan Williams3d880022015-05-31 15:02:11 -040040 else if (is_nd_pmem(dev))
41 return ND_DEVICE_REGION_PMEM;
42 else if (is_nd_blk(dev))
43 return ND_DEVICE_REGION_BLK;
Dan Williamscd034122016-03-11 10:15:36 -080044 else if (is_nd_dax(dev))
45 return ND_DEVICE_DAX_PMEM;
Dan Williams3d880022015-05-31 15:02:11 -040046 else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent))
47 return nd_region_to_nstype(to_nd_region(dev->parent));
Dan Williams4d88a972015-05-31 14:41:48 -040048
49 return 0;
50}
51
52static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
53{
Toshi Kani41d7a6d2015-06-19 12:18:33 -060054 /*
55 * Ensure that region devices always have their numa node set as
56 * early as possible.
57 */
58 if (is_nd_pmem(dev) || is_nd_blk(dev))
59 set_dev_node(dev, to_nd_region(dev)->numa_node);
Dan Williams4d88a972015-05-31 14:41:48 -040060 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
61 to_nd_device_type(dev));
62}
63
Dan Williams3d880022015-05-31 15:02:11 -040064static struct module *to_bus_provider(struct device *dev)
65{
66 /* pin bus providers while regions are enabled */
67 if (is_nd_pmem(dev) || is_nd_blk(dev)) {
68 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
69
Dan Williamsbc9775d2016-07-21 20:03:19 -070070 return nvdimm_bus->nd_desc->module;
Dan Williams3d880022015-05-31 15:02:11 -040071 }
72 return NULL;
73}
74
Dan Williamseaf96152015-05-01 13:11:27 -040075static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
76{
77 nvdimm_bus_lock(&nvdimm_bus->dev);
78 nvdimm_bus->probe_active++;
79 nvdimm_bus_unlock(&nvdimm_bus->dev);
80}
81
82static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
83{
84 nvdimm_bus_lock(&nvdimm_bus->dev);
85 if (--nvdimm_bus->probe_active == 0)
86 wake_up(&nvdimm_bus->probe_wait);
87 nvdimm_bus_unlock(&nvdimm_bus->dev);
88}
89
Dan Williams4d88a972015-05-31 14:41:48 -040090static int nvdimm_bus_probe(struct device *dev)
91{
92 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -040093 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040094 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
95 int rc;
96
Dan Williams3d880022015-05-31 15:02:11 -040097 if (!try_module_get(provider))
98 return -ENXIO;
99
Dan Williamseaf96152015-05-01 13:11:27 -0400100 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -0400101 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400102 if (rc == 0)
103 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400104 else
105 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400106 nvdimm_bus_probe_end(nvdimm_bus);
107
Dan Williams4d88a972015-05-31 14:41:48 -0400108 dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
109 dev_name(dev), rc);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400110
Dan Williams3d880022015-05-31 15:02:11 -0400111 if (rc != 0)
112 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400113 return rc;
114}
115
116static int nvdimm_bus_remove(struct device *dev)
117{
118 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -0400119 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400120 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700121 int rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -0400122
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700123 if (nd_drv->remove)
124 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400125 nd_region_disable(nvdimm_bus, dev);
126
Dan Williams4d88a972015-05-31 14:41:48 -0400127 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
128 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400129 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400130 return rc;
131}
132
Dan Williams476f8482016-07-09 00:12:52 -0700133static void nvdimm_bus_shutdown(struct device *dev)
134{
135 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
136 struct nd_device_driver *nd_drv = NULL;
137
138 if (dev->driver)
139 nd_drv = to_nd_device_driver(dev->driver);
140
141 if (nd_drv && nd_drv->shutdown) {
142 nd_drv->shutdown(dev);
143 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
144 dev->driver->name, dev_name(dev));
145 }
146}
147
Dan Williams71999462016-02-18 10:29:49 -0800148void nd_device_notify(struct device *dev, enum nvdimm_event event)
149{
150 device_lock(dev);
151 if (dev->driver) {
152 struct nd_device_driver *nd_drv;
153
154 nd_drv = to_nd_device_driver(dev->driver);
155 if (nd_drv->notify)
156 nd_drv->notify(dev, event);
157 }
158 device_unlock(dev);
159}
160EXPORT_SYMBOL(nd_device_notify);
161
162void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
163{
164 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
165
166 if (!nvdimm_bus)
167 return;
168
169 /* caller is responsible for holding a reference on the device */
170 nd_device_notify(&nd_region->dev, event);
171}
172EXPORT_SYMBOL_GPL(nvdimm_region_notify);
173
Dan Williams59e64732016-03-08 07:16:07 -0800174long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
175 unsigned int len)
176{
177 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
178 struct nvdimm_bus_descriptor *nd_desc;
179 struct nd_cmd_clear_error clear_err;
180 struct nd_cmd_ars_cap ars_cap;
181 u32 clear_err_unit, mask;
182 int cmd_rc, rc;
183
184 if (!nvdimm_bus)
185 return -ENXIO;
186
187 nd_desc = nvdimm_bus->nd_desc;
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700188 /*
189 * if ndctl does not exist, it's PMEM_LEGACY and
190 * we want to just pretend everything is handled.
191 */
Dan Williams59e64732016-03-08 07:16:07 -0800192 if (!nd_desc->ndctl)
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700193 return len;
Dan Williams59e64732016-03-08 07:16:07 -0800194
195 memset(&ars_cap, 0, sizeof(ars_cap));
196 ars_cap.address = phys;
197 ars_cap.length = len;
198 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
199 sizeof(ars_cap), &cmd_rc);
200 if (rc < 0)
201 return rc;
202 if (cmd_rc < 0)
203 return cmd_rc;
204 clear_err_unit = ars_cap.clear_err_unit;
205 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
206 return -ENXIO;
207
208 mask = clear_err_unit - 1;
209 if ((phys | len) & mask)
210 return -ENXIO;
211 memset(&clear_err, 0, sizeof(clear_err));
212 clear_err.address = phys;
213 clear_err.length = len;
214 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
215 sizeof(clear_err), &cmd_rc);
216 if (rc < 0)
217 return rc;
218 if (cmd_rc < 0)
219 return cmd_rc;
Vishal Vermae0461142016-09-30 17:19:31 -0600220
Toshi Kanifa313fd2017-04-27 16:57:05 -0600221 if (clear_err.cleared > 0)
222 nvdimm_clear_from_poison_list(nvdimm_bus, phys,
223 clear_err.cleared);
224
Dan Williams59e64732016-03-08 07:16:07 -0800225 return clear_err.cleared;
226}
227EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
228
Dan Williams18515942016-07-22 23:46:08 -0700229static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
230
Dan Williams4d88a972015-05-31 14:41:48 -0400231static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400232 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400233 .uevent = nvdimm_bus_uevent,
234 .match = nvdimm_bus_match,
235 .probe = nvdimm_bus_probe,
236 .remove = nvdimm_bus_remove,
Dan Williams476f8482016-07-09 00:12:52 -0700237 .shutdown = nvdimm_bus_shutdown,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400238};
239
Dan Williams18515942016-07-22 23:46:08 -0700240static void nvdimm_bus_release(struct device *dev)
241{
242 struct nvdimm_bus *nvdimm_bus;
243
244 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
245 ida_simple_remove(&nd_ida, nvdimm_bus->id);
246 kfree(nvdimm_bus);
247}
248
249static bool is_nvdimm_bus(struct device *dev)
250{
251 return dev->release == nvdimm_bus_release;
252}
253
254struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
255{
256 struct device *dev;
257
258 for (dev = nd_dev; dev; dev = dev->parent)
259 if (is_nvdimm_bus(dev))
260 break;
261 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
262 if (dev)
263 return to_nvdimm_bus(dev);
264 return NULL;
265}
266
267struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
268{
269 struct nvdimm_bus *nvdimm_bus;
270
271 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
272 WARN_ON(!is_nvdimm_bus(dev));
273 return nvdimm_bus;
274}
275EXPORT_SYMBOL_GPL(to_nvdimm_bus);
276
277struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
278 struct nvdimm_bus_descriptor *nd_desc)
279{
280 struct nvdimm_bus *nvdimm_bus;
281 int rc;
282
283 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
284 if (!nvdimm_bus)
285 return NULL;
286 INIT_LIST_HEAD(&nvdimm_bus->list);
287 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
288 INIT_LIST_HEAD(&nvdimm_bus->poison_list);
289 init_waitqueue_head(&nvdimm_bus->probe_wait);
290 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
291 mutex_init(&nvdimm_bus->reconfig_mutex);
292 if (nvdimm_bus->id < 0) {
293 kfree(nvdimm_bus);
294 return NULL;
295 }
296 nvdimm_bus->nd_desc = nd_desc;
297 nvdimm_bus->dev.parent = parent;
298 nvdimm_bus->dev.release = nvdimm_bus_release;
299 nvdimm_bus->dev.groups = nd_desc->attr_groups;
300 nvdimm_bus->dev.bus = &nvdimm_bus_type;
301 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
302 rc = device_register(&nvdimm_bus->dev);
303 if (rc) {
304 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
305 goto err;
306 }
307
308 return nvdimm_bus;
309 err:
310 put_device(&nvdimm_bus->dev);
311 return NULL;
312}
313EXPORT_SYMBOL_GPL(nvdimm_bus_register);
314
315void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
316{
317 if (!nvdimm_bus)
318 return;
319 device_unregister(&nvdimm_bus->dev);
320}
321EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
322
323static int child_unregister(struct device *dev, void *data)
324{
325 /*
326 * the singular ndctl class device per bus needs to be
327 * "device_destroy"ed, so skip it here
328 *
329 * i.e. remove classless children
330 */
331 if (dev->class)
332 /* pass */;
333 else
334 nd_device_unregister(dev, ND_SYNC);
335 return 0;
336}
337
338static void free_poison_list(struct list_head *poison_list)
339{
340 struct nd_poison *pl, *next;
341
342 list_for_each_entry_safe(pl, next, poison_list, list) {
343 list_del(&pl->list);
344 kfree(pl);
345 }
346 list_del_init(poison_list);
347}
348
349static int nd_bus_remove(struct device *dev)
350{
351 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
352
353 mutex_lock(&nvdimm_bus_list_mutex);
354 list_del_init(&nvdimm_bus->list);
355 mutex_unlock(&nvdimm_bus_list_mutex);
356
357 nd_synchronize();
358 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
359
360 nvdimm_bus_lock(&nvdimm_bus->dev);
361 free_poison_list(&nvdimm_bus->poison_list);
362 nvdimm_bus_unlock(&nvdimm_bus->dev);
363
364 nvdimm_bus_destroy_ndctl(nvdimm_bus);
365
366 return 0;
367}
368
369static int nd_bus_probe(struct device *dev)
370{
371 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
372 int rc;
373
374 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
375 if (rc)
376 return rc;
377
378 mutex_lock(&nvdimm_bus_list_mutex);
379 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
380 mutex_unlock(&nvdimm_bus_list_mutex);
381
382 /* enable bus provider attributes to look up their local context */
383 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
384
385 return 0;
386}
387
388static struct nd_device_driver nd_bus_driver = {
389 .probe = nd_bus_probe,
390 .remove = nd_bus_remove,
391 .drv = {
392 .name = "nd_bus",
393 .suppress_bind_attrs = true,
394 .bus = &nvdimm_bus_type,
395 .owner = THIS_MODULE,
396 .mod_name = KBUILD_MODNAME,
397 },
398};
399
400static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
401{
402 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
403
404 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
405 return true;
406
407 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
408}
409
Dan Williams4d88a972015-05-31 14:41:48 -0400410static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
411
412void nd_synchronize(void)
413{
414 async_synchronize_full_domain(&nd_async_domain);
415}
416EXPORT_SYMBOL_GPL(nd_synchronize);
417
418static void nd_async_device_register(void *d, async_cookie_t cookie)
419{
420 struct device *dev = d;
421
422 if (device_add(dev) != 0) {
423 dev_err(dev, "%s: failed\n", __func__);
424 put_device(dev);
425 }
426 put_device(dev);
Alexander Duyck3e63a7f2018-09-25 13:53:02 -0700427 if (dev->parent)
428 put_device(dev->parent);
Dan Williams4d88a972015-05-31 14:41:48 -0400429}
430
431static void nd_async_device_unregister(void *d, async_cookie_t cookie)
432{
433 struct device *dev = d;
434
Dan Williams0ba1c632015-05-30 12:35:36 -0400435 /* flush bus operations before delete */
436 nvdimm_bus_lock(dev);
437 nvdimm_bus_unlock(dev);
438
Dan Williams4d88a972015-05-31 14:41:48 -0400439 device_unregister(dev);
440 put_device(dev);
441}
442
Dan Williams8c2f7e82015-06-25 04:20:04 -0400443void __nd_device_register(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400444{
Dan Williamscd034122016-03-11 10:15:36 -0800445 if (!dev)
446 return;
Dan Williams4d88a972015-05-31 14:41:48 -0400447 dev->bus = &nvdimm_bus_type;
Alexander Duyck3e63a7f2018-09-25 13:53:02 -0700448 if (dev->parent)
449 get_device(dev->parent);
Dan Williams4d88a972015-05-31 14:41:48 -0400450 get_device(dev);
451 async_schedule_domain(nd_async_device_register, dev,
452 &nd_async_domain);
453}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400454
455void nd_device_register(struct device *dev)
456{
457 device_initialize(dev);
458 __nd_device_register(dev);
459}
Dan Williams4d88a972015-05-31 14:41:48 -0400460EXPORT_SYMBOL(nd_device_register);
461
462void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
463{
464 switch (mode) {
465 case ND_ASYNC:
466 get_device(dev);
467 async_schedule_domain(nd_async_device_unregister, dev,
468 &nd_async_domain);
469 break;
470 case ND_SYNC:
471 nd_synchronize();
472 device_unregister(dev);
473 break;
474 }
475}
476EXPORT_SYMBOL(nd_device_unregister);
477
478/**
479 * __nd_driver_register() - register a region or a namespace driver
480 * @nd_drv: driver to register
481 * @owner: automatically set by nd_driver_register() macro
482 * @mod_name: automatically set by nd_driver_register() macro
483 */
484int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
485 const char *mod_name)
486{
487 struct device_driver *drv = &nd_drv->drv;
488
489 if (!nd_drv->type) {
490 pr_debug("driver type bitmask not set (%pf)\n",
491 __builtin_return_address(0));
492 return -EINVAL;
493 }
494
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700495 if (!nd_drv->probe) {
496 pr_debug("%s ->probe() must be specified\n", mod_name);
Dan Williams4d88a972015-05-31 14:41:48 -0400497 return -EINVAL;
498 }
499
500 drv->bus = &nvdimm_bus_type;
501 drv->owner = owner;
502 drv->mod_name = mod_name;
503
504 return driver_register(drv);
505}
506EXPORT_SYMBOL(__nd_driver_register);
507
Dan Williams58138822015-06-23 20:08:34 -0400508int nvdimm_revalidate_disk(struct gendisk *disk)
509{
Dan Williams52c44d92016-06-15 19:43:07 -0700510 struct device *dev = disk_to_dev(disk)->parent;
Dan Williams58138822015-06-23 20:08:34 -0400511 struct nd_region *nd_region = to_nd_region(dev->parent);
Robert Elliottf216d1e2018-05-31 18:36:36 -0500512 int disk_ro = get_disk_ro(disk);
Dan Williams58138822015-06-23 20:08:34 -0400513
Robert Elliottf216d1e2018-05-31 18:36:36 -0500514 /*
515 * Upgrade to read-only if the region is read-only preserve as
516 * read-only if the disk is already read-only.
517 */
518 if (disk_ro || nd_region->ro == disk_ro)
Dan Williams58138822015-06-23 20:08:34 -0400519 return 0;
520
Robert Elliottf216d1e2018-05-31 18:36:36 -0500521 dev_info(dev, "%s read-only, marking %s read-only\n",
522 dev_name(&nd_region->dev), disk->disk_name);
523 set_disk_ro(disk, 1);
Dan Williams58138822015-06-23 20:08:34 -0400524
525 return 0;
526
527}
528EXPORT_SYMBOL(nvdimm_revalidate_disk);
529
Dan Williams4d88a972015-05-31 14:41:48 -0400530static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
531 char *buf)
532{
533 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
534 to_nd_device_type(dev));
535}
536static DEVICE_ATTR_RO(modalias);
537
538static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
539 char *buf)
540{
541 return sprintf(buf, "%s\n", dev->type->name);
542}
543static DEVICE_ATTR_RO(devtype);
544
545static struct attribute *nd_device_attributes[] = {
546 &dev_attr_modalias.attr,
547 &dev_attr_devtype.attr,
548 NULL,
549};
550
551/**
552 * nd_device_attribute_group - generic attributes for all devices on an nd bus
553 */
554struct attribute_group nd_device_attribute_group = {
555 .attrs = nd_device_attributes,
556};
557EXPORT_SYMBOL_GPL(nd_device_attribute_group);
558
Toshi Kani74ae66c2015-06-19 12:18:34 -0600559static ssize_t numa_node_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
562 return sprintf(buf, "%d\n", dev_to_node(dev));
563}
564static DEVICE_ATTR_RO(numa_node);
565
566static struct attribute *nd_numa_attributes[] = {
567 &dev_attr_numa_node.attr,
568 NULL,
569};
570
571static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
572 int n)
573{
574 if (!IS_ENABLED(CONFIG_NUMA))
575 return 0;
576
577 return a->mode;
578}
579
580/**
581 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
582 */
583struct attribute_group nd_numa_attribute_group = {
584 .attrs = nd_numa_attributes,
585 .is_visible = nd_numa_attr_visible,
586};
587EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
588
Dan Williams45def222015-04-26 19:26:48 -0400589int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
590{
591 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
592 struct device *dev;
593
594 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
595 "ndctl%d", nvdimm_bus->id);
596
Dan Williams42588952016-05-27 13:28:31 -0700597 if (IS_ERR(dev))
Dan Williams45def222015-04-26 19:26:48 -0400598 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
599 nvdimm_bus->id, PTR_ERR(dev));
Dan Williams42588952016-05-27 13:28:31 -0700600 return PTR_ERR_OR_ZERO(dev);
Dan Williams45def222015-04-26 19:26:48 -0400601}
602
603void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
604{
605 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
606}
607
Dan Williams62232e452015-06-08 14:27:06 -0400608static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
609 [ND_CMD_IMPLEMENTED] = { },
610 [ND_CMD_SMART] = {
611 .out_num = 2,
Dan Williams21129112016-04-07 19:58:44 -0700612 .out_sizes = { 4, 128, },
Dan Williams62232e452015-06-08 14:27:06 -0400613 },
614 [ND_CMD_SMART_THRESHOLD] = {
615 .out_num = 2,
616 .out_sizes = { 4, 8, },
617 },
618 [ND_CMD_DIMM_FLAGS] = {
619 .out_num = 2,
620 .out_sizes = { 4, 4 },
621 },
622 [ND_CMD_GET_CONFIG_SIZE] = {
623 .out_num = 3,
624 .out_sizes = { 4, 4, 4, },
625 },
626 [ND_CMD_GET_CONFIG_DATA] = {
627 .in_num = 2,
628 .in_sizes = { 4, 4, },
629 .out_num = 2,
630 .out_sizes = { 4, UINT_MAX, },
631 },
632 [ND_CMD_SET_CONFIG_DATA] = {
633 .in_num = 3,
634 .in_sizes = { 4, 4, UINT_MAX, },
635 .out_num = 1,
636 .out_sizes = { 4, },
637 },
638 [ND_CMD_VENDOR] = {
639 .in_num = 3,
640 .in_sizes = { 4, 4, UINT_MAX, },
641 .out_num = 3,
642 .out_sizes = { 4, 4, UINT_MAX, },
643 },
Dan Williams31eca762016-04-28 16:23:43 -0700644 [ND_CMD_CALL] = {
645 .in_num = 2,
646 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
647 .out_num = 1,
648 .out_sizes = { UINT_MAX, },
649 },
Dan Williams62232e452015-06-08 14:27:06 -0400650};
651
652const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
653{
654 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
655 return &__nd_cmd_dimm_descs[cmd];
656 return NULL;
657}
658EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
659
660static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
661 [ND_CMD_IMPLEMENTED] = { },
662 [ND_CMD_ARS_CAP] = {
663 .in_num = 2,
664 .in_sizes = { 8, 8, },
Dan Williams4577b062016-02-17 13:08:58 -0800665 .out_num = 4,
666 .out_sizes = { 4, 4, 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400667 },
668 [ND_CMD_ARS_START] = {
Dan Williams4577b062016-02-17 13:08:58 -0800669 .in_num = 5,
670 .in_sizes = { 8, 8, 2, 1, 5, },
671 .out_num = 2,
672 .out_sizes = { 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400673 },
674 [ND_CMD_ARS_STATUS] = {
Dan Williams747ffe12016-02-19 15:21:14 -0800675 .out_num = 3,
676 .out_sizes = { 4, 4, UINT_MAX, },
Dan Williams62232e452015-06-08 14:27:06 -0400677 },
Dan Williamsd4f32362016-03-03 16:08:54 -0800678 [ND_CMD_CLEAR_ERROR] = {
679 .in_num = 2,
680 .in_sizes = { 8, 8, },
681 .out_num = 3,
682 .out_sizes = { 4, 4, 8, },
683 },
Dan Williams31eca762016-04-28 16:23:43 -0700684 [ND_CMD_CALL] = {
685 .in_num = 2,
686 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
687 .out_num = 1,
688 .out_sizes = { UINT_MAX, },
689 },
Dan Williams62232e452015-06-08 14:27:06 -0400690};
691
692const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
693{
694 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
695 return &__nd_cmd_bus_descs[cmd];
696 return NULL;
697}
698EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
699
700u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
701 const struct nd_cmd_desc *desc, int idx, void *buf)
702{
703 if (idx >= desc->in_num)
704 return UINT_MAX;
705
706 if (desc->in_sizes[idx] < UINT_MAX)
707 return desc->in_sizes[idx];
708
709 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
710 struct nd_cmd_set_config_hdr *hdr = buf;
711
712 return hdr->in_length;
713 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
714 struct nd_cmd_vendor_hdr *hdr = buf;
715
716 return hdr->in_length;
Dan Williams31eca762016-04-28 16:23:43 -0700717 } else if (cmd == ND_CMD_CALL) {
718 struct nd_cmd_pkg *pkg = buf;
719
720 return pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400721 }
722
723 return UINT_MAX;
724}
725EXPORT_SYMBOL_GPL(nd_cmd_in_size);
726
727u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
728 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800729 const u32 *out_field, unsigned long remainder)
Dan Williams62232e452015-06-08 14:27:06 -0400730{
731 if (idx >= desc->out_num)
732 return UINT_MAX;
733
734 if (desc->out_sizes[idx] < UINT_MAX)
735 return desc->out_sizes[idx];
736
737 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
738 return in_field[1];
739 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
740 return out_field[1];
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800741 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
742 /*
743 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
744 * "Size of Output Buffer in bytes, including this
745 * field."
746 */
747 if (out_field[1] < 4)
748 return 0;
749 /*
750 * ACPI 6.1 is ambiguous if 'status' is included in the
751 * output size. If we encounter an output size that
752 * overshoots the remainder by 4 bytes, assume it was
753 * including 'status'.
754 */
Vishal Verma05a085c2018-08-10 13:23:15 -0600755 if (out_field[1] - 4 == remainder)
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800756 return remainder;
Vishal Verma05a085c2018-08-10 13:23:15 -0600757 return out_field[1] - 8;
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800758 } else if (cmd == ND_CMD_CALL) {
Dan Williams31eca762016-04-28 16:23:43 -0700759 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
760
761 return pkg->nd_size_out;
762 }
763
Dan Williams62232e452015-06-08 14:27:06 -0400764
765 return UINT_MAX;
766}
767EXPORT_SYMBOL_GPL(nd_cmd_out_size);
768
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400769void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400770{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400771 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
772
Dan Williamseaf96152015-05-01 13:11:27 -0400773 do {
774 if (nvdimm_bus->probe_active == 0)
775 break;
776 nvdimm_bus_unlock(&nvdimm_bus->dev);
777 wait_event(nvdimm_bus->probe_wait,
778 nvdimm_bus->probe_active == 0);
779 nvdimm_bus_lock(&nvdimm_bus->dev);
780 } while (true);
781}
782
Dan Williamsd4f32362016-03-03 16:08:54 -0800783static int pmem_active(struct device *dev, void *data)
784{
785 if (is_nd_pmem(dev) && dev->driver)
786 return -EBUSY;
787 return 0;
788}
789
Dan Williamseaf96152015-05-01 13:11:27 -0400790/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800791static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
792 struct nvdimm *nvdimm, unsigned int cmd)
Dan Williamseaf96152015-05-01 13:11:27 -0400793{
Dan Williams87bf5722016-02-22 21:50:31 -0800794 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
795
796 /* ask the bus provider if it would like to block this request */
797 if (nd_desc->clear_to_send) {
798 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
799
800 if (rc)
801 return rc;
802 }
Dan Williamseaf96152015-05-01 13:11:27 -0400803
Dan Williamsd4f32362016-03-03 16:08:54 -0800804 /* require clear error to go through the pmem driver */
805 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
806 return device_for_each_child(&nvdimm_bus->dev, NULL,
807 pmem_active);
808
Dan Williamseaf96152015-05-01 13:11:27 -0400809 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
810 return 0;
811
Dan Williams87bf5722016-02-22 21:50:31 -0800812 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400813 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400814 if (atomic_read(&nvdimm->busy))
815 return -EBUSY;
816 return 0;
817}
818
Dan Williams62232e452015-06-08 14:27:06 -0400819static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
820 int read_only, unsigned int ioctl_cmd, unsigned long arg)
821{
822 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
Dan Williams62232e452015-06-08 14:27:06 -0400823 static char out_env[ND_CMD_MAX_ENVELOPE];
824 static char in_env[ND_CMD_MAX_ENVELOPE];
825 const struct nd_cmd_desc *desc = NULL;
826 unsigned int cmd = _IOC_NR(ioctl_cmd);
827 void __user *p = (void __user *) arg;
828 struct device *dev = &nvdimm_bus->dev;
829 const char *cmd_name, *dimm_name;
Dan Williams29c969c2018-02-23 14:05:38 -0800830 u32 in_len = 0, out_len = 0;
Dan Williamse3654ec2016-04-28 16:17:07 -0700831 unsigned long cmd_mask;
Dan Williams29c969c2018-02-23 14:05:38 -0800832 struct nd_cmd_pkg pkg;
833 u64 buf_len = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400834 void *buf;
835 int rc, i;
836
837 if (nvdimm) {
838 desc = nd_cmd_dimm_desc(cmd);
839 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700840 cmd_mask = nvdimm->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400841 dimm_name = dev_name(&nvdimm->dev);
842 } else {
843 desc = nd_cmd_bus_desc(cmd);
844 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700845 cmd_mask = nd_desc->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400846 dimm_name = "bus";
847 }
848
Dan Williams31eca762016-04-28 16:23:43 -0700849 if (cmd == ND_CMD_CALL) {
850 if (copy_from_user(&pkg, p, sizeof(pkg)))
851 return -EFAULT;
852 }
853
Dan Williams62232e452015-06-08 14:27:06 -0400854 if (!desc || (desc->out_num + desc->in_num == 0) ||
Dan Williamse3654ec2016-04-28 16:17:07 -0700855 !test_bit(cmd, &cmd_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400856 return -ENOTTY;
857
858 /* fail write commands (when read-only) */
859 if (read_only)
Jerry Hoemann07accfa2016-01-06 16:03:41 -0700860 switch (cmd) {
861 case ND_CMD_VENDOR:
862 case ND_CMD_SET_CONFIG_DATA:
863 case ND_CMD_ARS_START:
Dan Williamsd4f32362016-03-03 16:08:54 -0800864 case ND_CMD_CLEAR_ERROR:
Dan Williams31eca762016-04-28 16:23:43 -0700865 case ND_CMD_CALL:
Dan Williams62232e452015-06-08 14:27:06 -0400866 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
867 nvdimm ? nvdimm_cmd_name(cmd)
868 : nvdimm_bus_cmd_name(cmd));
869 return -EPERM;
870 default:
871 break;
872 }
873
874 /* process an input envelope */
875 for (i = 0; i < desc->in_num; i++) {
876 u32 in_size, copy;
877
878 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
879 if (in_size == UINT_MAX) {
880 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
881 __func__, dimm_name, cmd_name, i);
882 return -ENXIO;
883 }
Dan Williams62232e452015-06-08 14:27:06 -0400884 if (in_len < sizeof(in_env))
885 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
886 else
887 copy = 0;
888 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
889 return -EFAULT;
890 in_len += in_size;
891 }
892
Dan Williams31eca762016-04-28 16:23:43 -0700893 if (cmd == ND_CMD_CALL) {
Dan Williams29c969c2018-02-23 14:05:38 -0800894 dev_dbg(dev, "%s:%s, idx: %llu, in: %u, out: %u, len %llu\n",
Dan Williams31eca762016-04-28 16:23:43 -0700895 __func__, dimm_name, pkg.nd_command,
896 in_len, out_len, buf_len);
897
898 for (i = 0; i < ARRAY_SIZE(pkg.nd_reserved2); i++)
899 if (pkg.nd_reserved2[i])
900 return -EINVAL;
901 }
902
Dan Williams62232e452015-06-08 14:27:06 -0400903 /* process an output envelope */
904 for (i = 0; i < desc->out_num; i++) {
905 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800906 (u32 *) in_env, (u32 *) out_env, 0);
Dan Williams62232e452015-06-08 14:27:06 -0400907 u32 copy;
908
909 if (out_size == UINT_MAX) {
910 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
911 __func__, dimm_name, cmd_name, i);
912 return -EFAULT;
913 }
Dan Williams62232e452015-06-08 14:27:06 -0400914 if (out_len < sizeof(out_env))
915 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
916 else
917 copy = 0;
918 if (copy && copy_from_user(&out_env[out_len],
919 p + in_len + out_len, copy))
920 return -EFAULT;
921 out_len += out_size;
922 }
923
Dan Williams29c969c2018-02-23 14:05:38 -0800924 buf_len = (u64) out_len + (u64) in_len;
Dan Williams62232e452015-06-08 14:27:06 -0400925 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
Dan Williams29c969c2018-02-23 14:05:38 -0800926 dev_dbg(dev, "%s:%s cmd: %s buf_len: %llu > %d\n", __func__,
Dan Williams62232e452015-06-08 14:27:06 -0400927 dimm_name, cmd_name, buf_len,
928 ND_IOCTL_MAX_BUFLEN);
929 return -EINVAL;
930 }
931
932 buf = vmalloc(buf_len);
933 if (!buf)
934 return -ENOMEM;
935
936 if (copy_from_user(buf, p, buf_len)) {
937 rc = -EFAULT;
938 goto out;
939 }
940
Dan Williamseaf96152015-05-01 13:11:27 -0400941 nvdimm_bus_lock(&nvdimm_bus->dev);
Dan Williams87bf5722016-02-22 21:50:31 -0800942 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, cmd);
Dan Williamseaf96152015-05-01 13:11:27 -0400943 if (rc)
944 goto out_unlock;
945
Dan Williamsaef25332016-02-12 17:01:11 -0800946 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, NULL);
Dan Williams62232e452015-06-08 14:27:06 -0400947 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -0400948 goto out_unlock;
Dan Williams5ac50e72017-04-07 09:47:24 -0700949 nvdimm_bus_unlock(&nvdimm_bus->dev);
950
Dan Williams62232e452015-06-08 14:27:06 -0400951 if (copy_to_user(p, buf, buf_len))
952 rc = -EFAULT;
Dan Williams5ac50e72017-04-07 09:47:24 -0700953
954 vfree(buf);
955 return rc;
956
Dan Williamseaf96152015-05-01 13:11:27 -0400957 out_unlock:
958 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -0400959 out:
960 vfree(buf);
961 return rc;
962}
963
Dan Williams45def222015-04-26 19:26:48 -0400964static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
965{
Dan Williams62232e452015-06-08 14:27:06 -0400966 long id = (long) file->private_data;
Jerry Hoemann4dc0e7b2016-01-06 16:03:39 -0700967 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -0400968 struct nvdimm_bus *nvdimm_bus;
969
Jerry Hoemann4dc0e7b2016-01-06 16:03:39 -0700970 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -0400971 mutex_lock(&nvdimm_bus_list_mutex);
972 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
973 if (nvdimm_bus->id == id) {
Jerry Hoemann4dc0e7b2016-01-06 16:03:39 -0700974 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -0400975 break;
976 }
977 }
978 mutex_unlock(&nvdimm_bus_list_mutex);
979
980 return rc;
981}
982
983static int match_dimm(struct device *dev, void *data)
984{
985 long id = (long) data;
986
987 if (is_nvdimm(dev)) {
988 struct nvdimm *nvdimm = to_nvdimm(dev);
989
990 return nvdimm->id == id;
991 }
992
993 return 0;
994}
995
996static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
997{
Jerry Hoemann4dc0e7b2016-01-06 16:03:39 -0700998 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -0400999 struct nvdimm_bus *nvdimm_bus;
1000
Jerry Hoemann4dc0e7b2016-01-06 16:03:39 -07001001 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001002 mutex_lock(&nvdimm_bus_list_mutex);
1003 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1004 struct device *dev = device_find_child(&nvdimm_bus->dev,
1005 file->private_data, match_dimm);
1006 struct nvdimm *nvdimm;
1007
1008 if (!dev)
1009 continue;
1010
1011 nvdimm = to_nvdimm(dev);
Jerry Hoemann4dc0e7b2016-01-06 16:03:39 -07001012 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001013 put_device(dev);
1014 break;
1015 }
1016 mutex_unlock(&nvdimm_bus_list_mutex);
1017
1018 return rc;
1019}
1020
1021static int nd_open(struct inode *inode, struct file *file)
1022{
1023 long minor = iminor(inode);
1024
1025 file->private_data = (void *) minor;
1026 return 0;
Dan Williams45def222015-04-26 19:26:48 -04001027}
1028
1029static const struct file_operations nvdimm_bus_fops = {
1030 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -04001031 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -04001032 .unlocked_ioctl = nd_ioctl,
1033 .compat_ioctl = nd_ioctl,
1034 .llseek = noop_llseek,
1035};
1036
Dan Williams62232e452015-06-08 14:27:06 -04001037static const struct file_operations nvdimm_fops = {
1038 .owner = THIS_MODULE,
1039 .open = nd_open,
1040 .unlocked_ioctl = nvdimm_ioctl,
1041 .compat_ioctl = nvdimm_ioctl,
1042 .llseek = noop_llseek,
1043};
1044
Dan Williams45def222015-04-26 19:26:48 -04001045int __init nvdimm_bus_init(void)
1046{
1047 int rc;
1048
Dan Williamsbaa51272016-04-05 17:40:52 -07001049 BUILD_BUG_ON(sizeof(struct nd_smart_payload) != 128);
1050 BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload) != 8);
1051
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001052 rc = bus_register(&nvdimm_bus_type);
1053 if (rc)
1054 return rc;
1055
Dan Williams45def222015-04-26 19:26:48 -04001056 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1057 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -04001058 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -04001059 nvdimm_bus_major = rc;
1060
Dan Williams62232e452015-06-08 14:27:06 -04001061 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1062 if (rc < 0)
1063 goto err_dimm_chrdev;
1064 nvdimm_major = rc;
1065
Dan Williams45def222015-04-26 19:26:48 -04001066 nd_class = class_create(THIS_MODULE, "nd");
Axel Lindaa1dee2015-06-28 17:00:57 +08001067 if (IS_ERR(nd_class)) {
1068 rc = PTR_ERR(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001069 goto err_class;
Axel Lindaa1dee2015-06-28 17:00:57 +08001070 }
Dan Williams45def222015-04-26 19:26:48 -04001071
Dan Williams18515942016-07-22 23:46:08 -07001072 rc = driver_register(&nd_bus_driver.drv);
1073 if (rc)
1074 goto err_nd_bus;
1075
Dan Williams45def222015-04-26 19:26:48 -04001076 return 0;
1077
Dan Williams18515942016-07-22 23:46:08 -07001078 err_nd_bus:
1079 class_destroy(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001080 err_class:
Dan Williams62232e452015-06-08 14:27:06 -04001081 unregister_chrdev(nvdimm_major, "dimmctl");
1082 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -04001083 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001084 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001085 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -04001086
1087 return rc;
1088}
1089
Dan Williams4d88a972015-05-31 14:41:48 -04001090void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -04001091{
Dan Williams18515942016-07-22 23:46:08 -07001092 driver_unregister(&nd_bus_driver.drv);
Dan Williams45def222015-04-26 19:26:48 -04001093 class_destroy(nd_class);
1094 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001095 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001096 bus_unregister(&nvdimm_bus_type);
Dan Williams18515942016-07-22 23:46:08 -07001097 ida_destroy(&nd_ida);
Dan Williams45def222015-04-26 19:26:48 -04001098}