libnvdimm: Move nd_device_attribute_group to device_type
[linux-2.6-microblaze.git] / drivers / nvdimm / bus.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/libnvdimm.h>
7 #include <linux/sched/mm.h>
8 #include <linux/vmalloc.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
11 #include <linux/blkdev.h>
12 #include <linux/fcntl.h>
13 #include <linux/async.h>
14 #include <linux/genhd.h>
15 #include <linux/ndctl.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/cpu.h>
19 #include <linux/fs.h>
20 #include <linux/io.h>
21 #include <linux/mm.h>
22 #include <linux/nd.h>
23 #include "nd-core.h"
24 #include "nd.h"
25 #include "pfn.h"
26
27 int nvdimm_major;
28 static int nvdimm_bus_major;
29 struct class *nd_class;
30 static DEFINE_IDA(nd_ida);
31
32 static int to_nd_device_type(struct device *dev)
33 {
34         if (is_nvdimm(dev))
35                 return ND_DEVICE_DIMM;
36         else if (is_memory(dev))
37                 return ND_DEVICE_REGION_PMEM;
38         else if (is_nd_blk(dev))
39                 return ND_DEVICE_REGION_BLK;
40         else if (is_nd_dax(dev))
41                 return ND_DEVICE_DAX_PMEM;
42         else if (is_nd_region(dev->parent))
43                 return nd_region_to_nstype(to_nd_region(dev->parent));
44
45         return 0;
46 }
47
48 static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
49 {
50         return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
51                         to_nd_device_type(dev));
52 }
53
54 static struct module *to_bus_provider(struct device *dev)
55 {
56         /* pin bus providers while regions are enabled */
57         if (is_nd_region(dev)) {
58                 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
59
60                 return nvdimm_bus->nd_desc->module;
61         }
62         return NULL;
63 }
64
65 static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
66 {
67         nvdimm_bus_lock(&nvdimm_bus->dev);
68         nvdimm_bus->probe_active++;
69         nvdimm_bus_unlock(&nvdimm_bus->dev);
70 }
71
72 static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
73 {
74         nvdimm_bus_lock(&nvdimm_bus->dev);
75         if (--nvdimm_bus->probe_active == 0)
76                 wake_up(&nvdimm_bus->wait);
77         nvdimm_bus_unlock(&nvdimm_bus->dev);
78 }
79
80 static int nvdimm_bus_probe(struct device *dev)
81 {
82         struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
83         struct module *provider = to_bus_provider(dev);
84         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
85         int rc;
86
87         if (!try_module_get(provider))
88                 return -ENXIO;
89
90         dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
91                         dev->driver->name, dev_name(dev));
92
93         nvdimm_bus_probe_start(nvdimm_bus);
94         debug_nvdimm_lock(dev);
95         rc = nd_drv->probe(dev);
96         debug_nvdimm_unlock(dev);
97
98         if ((rc == 0 || rc == -EOPNOTSUPP) &&
99                         dev->parent && is_nd_region(dev->parent))
100                 nd_region_advance_seeds(to_nd_region(dev->parent), dev);
101         nvdimm_bus_probe_end(nvdimm_bus);
102
103         dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
104                         dev_name(dev), rc);
105
106         if (rc != 0)
107                 module_put(provider);
108         return rc;
109 }
110
111 static int nvdimm_bus_remove(struct device *dev)
112 {
113         struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
114         struct module *provider = to_bus_provider(dev);
115         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
116         int rc = 0;
117
118         if (nd_drv->remove) {
119                 debug_nvdimm_lock(dev);
120                 rc = nd_drv->remove(dev);
121                 debug_nvdimm_unlock(dev);
122         }
123
124         dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
125                         dev_name(dev), rc);
126         module_put(provider);
127         return rc;
128 }
129
130 static void nvdimm_bus_shutdown(struct device *dev)
131 {
132         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
133         struct nd_device_driver *nd_drv = NULL;
134
135         if (dev->driver)
136                 nd_drv = to_nd_device_driver(dev->driver);
137
138         if (nd_drv && nd_drv->shutdown) {
139                 nd_drv->shutdown(dev);
140                 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
141                                 dev->driver->name, dev_name(dev));
142         }
143 }
144
145 void nd_device_notify(struct device *dev, enum nvdimm_event event)
146 {
147         nd_device_lock(dev);
148         if (dev->driver) {
149                 struct nd_device_driver *nd_drv;
150
151                 nd_drv = to_nd_device_driver(dev->driver);
152                 if (nd_drv->notify)
153                         nd_drv->notify(dev, event);
154         }
155         nd_device_unlock(dev);
156 }
157 EXPORT_SYMBOL(nd_device_notify);
158
159 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
160 {
161         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
162
163         if (!nvdimm_bus)
164                 return;
165
166         /* caller is responsible for holding a reference on the device */
167         nd_device_notify(&nd_region->dev, event);
168 }
169 EXPORT_SYMBOL_GPL(nvdimm_region_notify);
170
171 struct clear_badblocks_context {
172         resource_size_t phys, cleared;
173 };
174
175 static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
176 {
177         struct clear_badblocks_context *ctx = data;
178         struct nd_region *nd_region;
179         resource_size_t ndr_end;
180         sector_t sector;
181
182         /* make sure device is a region */
183         if (!is_memory(dev))
184                 return 0;
185
186         nd_region = to_nd_region(dev);
187         ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
188
189         /* make sure we are in the region */
190         if (ctx->phys < nd_region->ndr_start
191                         || (ctx->phys + ctx->cleared) > ndr_end)
192                 return 0;
193
194         sector = (ctx->phys - nd_region->ndr_start) / 512;
195         badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
196
197         if (nd_region->bb_state)
198                 sysfs_notify_dirent(nd_region->bb_state);
199
200         return 0;
201 }
202
203 static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
204                 phys_addr_t phys, u64 cleared)
205 {
206         struct clear_badblocks_context ctx = {
207                 .phys = phys,
208                 .cleared = cleared,
209         };
210
211         device_for_each_child(&nvdimm_bus->dev, &ctx,
212                         nvdimm_clear_badblocks_region);
213 }
214
215 static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
216                 phys_addr_t phys, u64 cleared)
217 {
218         if (cleared > 0)
219                 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
220
221         if (cleared > 0 && cleared / 512)
222                 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
223 }
224
225 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
226                 unsigned int len)
227 {
228         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
229         struct nvdimm_bus_descriptor *nd_desc;
230         struct nd_cmd_clear_error clear_err;
231         struct nd_cmd_ars_cap ars_cap;
232         u32 clear_err_unit, mask;
233         unsigned int noio_flag;
234         int cmd_rc, rc;
235
236         if (!nvdimm_bus)
237                 return -ENXIO;
238
239         nd_desc = nvdimm_bus->nd_desc;
240         /*
241          * if ndctl does not exist, it's PMEM_LEGACY and
242          * we want to just pretend everything is handled.
243          */
244         if (!nd_desc->ndctl)
245                 return len;
246
247         memset(&ars_cap, 0, sizeof(ars_cap));
248         ars_cap.address = phys;
249         ars_cap.length = len;
250         noio_flag = memalloc_noio_save();
251         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
252                         sizeof(ars_cap), &cmd_rc);
253         memalloc_noio_restore(noio_flag);
254         if (rc < 0)
255                 return rc;
256         if (cmd_rc < 0)
257                 return cmd_rc;
258         clear_err_unit = ars_cap.clear_err_unit;
259         if (!clear_err_unit || !is_power_of_2(clear_err_unit))
260                 return -ENXIO;
261
262         mask = clear_err_unit - 1;
263         if ((phys | len) & mask)
264                 return -ENXIO;
265         memset(&clear_err, 0, sizeof(clear_err));
266         clear_err.address = phys;
267         clear_err.length = len;
268         noio_flag = memalloc_noio_save();
269         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
270                         sizeof(clear_err), &cmd_rc);
271         memalloc_noio_restore(noio_flag);
272         if (rc < 0)
273                 return rc;
274         if (cmd_rc < 0)
275                 return cmd_rc;
276
277         nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
278
279         return clear_err.cleared;
280 }
281 EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
282
283 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
284
285 static struct bus_type nvdimm_bus_type = {
286         .name = "nd",
287         .uevent = nvdimm_bus_uevent,
288         .match = nvdimm_bus_match,
289         .probe = nvdimm_bus_probe,
290         .remove = nvdimm_bus_remove,
291         .shutdown = nvdimm_bus_shutdown,
292 };
293
294 static void nvdimm_bus_release(struct device *dev)
295 {
296         struct nvdimm_bus *nvdimm_bus;
297
298         nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
299         ida_simple_remove(&nd_ida, nvdimm_bus->id);
300         kfree(nvdimm_bus);
301 }
302
303 bool is_nvdimm_bus(struct device *dev)
304 {
305         return dev->release == nvdimm_bus_release;
306 }
307
308 struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
309 {
310         struct device *dev;
311
312         for (dev = nd_dev; dev; dev = dev->parent)
313                 if (is_nvdimm_bus(dev))
314                         break;
315         dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
316         if (dev)
317                 return to_nvdimm_bus(dev);
318         return NULL;
319 }
320
321 struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
322 {
323         struct nvdimm_bus *nvdimm_bus;
324
325         nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
326         WARN_ON(!is_nvdimm_bus(dev));
327         return nvdimm_bus;
328 }
329 EXPORT_SYMBOL_GPL(to_nvdimm_bus);
330
331 struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm)
332 {
333         return to_nvdimm_bus(nvdimm->dev.parent);
334 }
335 EXPORT_SYMBOL_GPL(nvdimm_to_bus);
336
337 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
338                 struct nvdimm_bus_descriptor *nd_desc)
339 {
340         struct nvdimm_bus *nvdimm_bus;
341         int rc;
342
343         nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
344         if (!nvdimm_bus)
345                 return NULL;
346         INIT_LIST_HEAD(&nvdimm_bus->list);
347         INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
348         init_waitqueue_head(&nvdimm_bus->wait);
349         nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
350         if (nvdimm_bus->id < 0) {
351                 kfree(nvdimm_bus);
352                 return NULL;
353         }
354         mutex_init(&nvdimm_bus->reconfig_mutex);
355         badrange_init(&nvdimm_bus->badrange);
356         nvdimm_bus->nd_desc = nd_desc;
357         nvdimm_bus->dev.parent = parent;
358         nvdimm_bus->dev.release = nvdimm_bus_release;
359         nvdimm_bus->dev.groups = nd_desc->attr_groups;
360         nvdimm_bus->dev.bus = &nvdimm_bus_type;
361         nvdimm_bus->dev.of_node = nd_desc->of_node;
362         dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
363         rc = device_register(&nvdimm_bus->dev);
364         if (rc) {
365                 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
366                 goto err;
367         }
368
369         return nvdimm_bus;
370  err:
371         put_device(&nvdimm_bus->dev);
372         return NULL;
373 }
374 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
375
376 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
377 {
378         if (!nvdimm_bus)
379                 return;
380         device_unregister(&nvdimm_bus->dev);
381 }
382 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
383
384 static int child_unregister(struct device *dev, void *data)
385 {
386         /*
387          * the singular ndctl class device per bus needs to be
388          * "device_destroy"ed, so skip it here
389          *
390          * i.e. remove classless children
391          */
392         if (dev->class)
393                 return 0;
394
395         if (is_nvdimm(dev)) {
396                 struct nvdimm *nvdimm = to_nvdimm(dev);
397                 bool dev_put = false;
398
399                 /* We are shutting down. Make state frozen artificially. */
400                 nvdimm_bus_lock(dev);
401                 set_bit(NVDIMM_SECURITY_FROZEN, &nvdimm->sec.flags);
402                 if (test_and_clear_bit(NDD_WORK_PENDING, &nvdimm->flags))
403                         dev_put = true;
404                 nvdimm_bus_unlock(dev);
405                 cancel_delayed_work_sync(&nvdimm->dwork);
406                 if (dev_put)
407                         put_device(dev);
408         }
409         nd_device_unregister(dev, ND_SYNC);
410
411         return 0;
412 }
413
414 static void free_badrange_list(struct list_head *badrange_list)
415 {
416         struct badrange_entry *bre, *next;
417
418         list_for_each_entry_safe(bre, next, badrange_list, list) {
419                 list_del(&bre->list);
420                 kfree(bre);
421         }
422         list_del_init(badrange_list);
423 }
424
425 static int nd_bus_remove(struct device *dev)
426 {
427         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
428
429         mutex_lock(&nvdimm_bus_list_mutex);
430         list_del_init(&nvdimm_bus->list);
431         mutex_unlock(&nvdimm_bus_list_mutex);
432
433         wait_event(nvdimm_bus->wait,
434                         atomic_read(&nvdimm_bus->ioctl_active) == 0);
435
436         nd_synchronize();
437         device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
438
439         spin_lock(&nvdimm_bus->badrange.lock);
440         free_badrange_list(&nvdimm_bus->badrange.list);
441         spin_unlock(&nvdimm_bus->badrange.lock);
442
443         nvdimm_bus_destroy_ndctl(nvdimm_bus);
444
445         return 0;
446 }
447
448 static int nd_bus_probe(struct device *dev)
449 {
450         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
451         int rc;
452
453         rc = nvdimm_bus_create_ndctl(nvdimm_bus);
454         if (rc)
455                 return rc;
456
457         mutex_lock(&nvdimm_bus_list_mutex);
458         list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
459         mutex_unlock(&nvdimm_bus_list_mutex);
460
461         /* enable bus provider attributes to look up their local context */
462         dev_set_drvdata(dev, nvdimm_bus->nd_desc);
463
464         return 0;
465 }
466
467 static struct nd_device_driver nd_bus_driver = {
468         .probe = nd_bus_probe,
469         .remove = nd_bus_remove,
470         .drv = {
471                 .name = "nd_bus",
472                 .suppress_bind_attrs = true,
473                 .bus = &nvdimm_bus_type,
474                 .owner = THIS_MODULE,
475                 .mod_name = KBUILD_MODNAME,
476         },
477 };
478
479 static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
480 {
481         struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
482
483         if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
484                 return true;
485
486         return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
487 }
488
489 static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
490
491 void nd_synchronize(void)
492 {
493         async_synchronize_full_domain(&nd_async_domain);
494 }
495 EXPORT_SYMBOL_GPL(nd_synchronize);
496
497 static void nd_async_device_register(void *d, async_cookie_t cookie)
498 {
499         struct device *dev = d;
500
501         if (device_add(dev) != 0) {
502                 dev_err(dev, "%s: failed\n", __func__);
503                 put_device(dev);
504         }
505         put_device(dev);
506         if (dev->parent)
507                 put_device(dev->parent);
508 }
509
510 static void nd_async_device_unregister(void *d, async_cookie_t cookie)
511 {
512         struct device *dev = d;
513
514         /* flush bus operations before delete */
515         nvdimm_bus_lock(dev);
516         nvdimm_bus_unlock(dev);
517
518         device_unregister(dev);
519         put_device(dev);
520 }
521
522 void __nd_device_register(struct device *dev)
523 {
524         if (!dev)
525                 return;
526
527         /*
528          * Ensure that region devices always have their NUMA node set as
529          * early as possible. This way we are able to make certain that
530          * any memory associated with the creation and the creation
531          * itself of the region is associated with the correct node.
532          */
533         if (is_nd_region(dev))
534                 set_dev_node(dev, to_nd_region(dev)->numa_node);
535
536         dev->bus = &nvdimm_bus_type;
537         if (dev->parent) {
538                 get_device(dev->parent);
539                 if (dev_to_node(dev) == NUMA_NO_NODE)
540                         set_dev_node(dev, dev_to_node(dev->parent));
541         }
542         get_device(dev);
543
544         async_schedule_dev_domain(nd_async_device_register, dev,
545                                   &nd_async_domain);
546 }
547
548 void nd_device_register(struct device *dev)
549 {
550         device_initialize(dev);
551         __nd_device_register(dev);
552 }
553 EXPORT_SYMBOL(nd_device_register);
554
555 void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
556 {
557         bool killed;
558
559         switch (mode) {
560         case ND_ASYNC:
561                 /*
562                  * In the async case this is being triggered with the
563                  * device lock held and the unregistration work needs to
564                  * be moved out of line iff this is thread has won the
565                  * race to schedule the deletion.
566                  */
567                 if (!kill_device(dev))
568                         return;
569
570                 get_device(dev);
571                 async_schedule_domain(nd_async_device_unregister, dev,
572                                 &nd_async_domain);
573                 break;
574         case ND_SYNC:
575                 /*
576                  * In the sync case the device is being unregistered due
577                  * to a state change of the parent. Claim the kill state
578                  * to synchronize against other unregistration requests,
579                  * or otherwise let the async path handle it if the
580                  * unregistration was already queued.
581                  */
582                 nd_device_lock(dev);
583                 killed = kill_device(dev);
584                 nd_device_unlock(dev);
585
586                 if (!killed)
587                         return;
588
589                 nd_synchronize();
590                 device_unregister(dev);
591                 break;
592         }
593 }
594 EXPORT_SYMBOL(nd_device_unregister);
595
596 /**
597  * __nd_driver_register() - register a region or a namespace driver
598  * @nd_drv: driver to register
599  * @owner: automatically set by nd_driver_register() macro
600  * @mod_name: automatically set by nd_driver_register() macro
601  */
602 int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
603                 const char *mod_name)
604 {
605         struct device_driver *drv = &nd_drv->drv;
606
607         if (!nd_drv->type) {
608                 pr_debug("driver type bitmask not set (%ps)\n",
609                                 __builtin_return_address(0));
610                 return -EINVAL;
611         }
612
613         if (!nd_drv->probe) {
614                 pr_debug("%s ->probe() must be specified\n", mod_name);
615                 return -EINVAL;
616         }
617
618         drv->bus = &nvdimm_bus_type;
619         drv->owner = owner;
620         drv->mod_name = mod_name;
621
622         return driver_register(drv);
623 }
624 EXPORT_SYMBOL(__nd_driver_register);
625
626 int nvdimm_revalidate_disk(struct gendisk *disk)
627 {
628         struct device *dev = disk_to_dev(disk)->parent;
629         struct nd_region *nd_region = to_nd_region(dev->parent);
630         int disk_ro = get_disk_ro(disk);
631
632         /*
633          * Upgrade to read-only if the region is read-only preserve as
634          * read-only if the disk is already read-only.
635          */
636         if (disk_ro || nd_region->ro == disk_ro)
637                 return 0;
638
639         dev_info(dev, "%s read-only, marking %s read-only\n",
640                         dev_name(&nd_region->dev), disk->disk_name);
641         set_disk_ro(disk, 1);
642
643         return 0;
644
645 }
646 EXPORT_SYMBOL(nvdimm_revalidate_disk);
647
648 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
649                 char *buf)
650 {
651         return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
652                         to_nd_device_type(dev));
653 }
654 static DEVICE_ATTR_RO(modalias);
655
656 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
657                 char *buf)
658 {
659         return sprintf(buf, "%s\n", dev->type->name);
660 }
661 static DEVICE_ATTR_RO(devtype);
662
663 static struct attribute *nd_device_attributes[] = {
664         &dev_attr_modalias.attr,
665         &dev_attr_devtype.attr,
666         NULL,
667 };
668
669 /*
670  * nd_device_attribute_group - generic attributes for all devices on an nd bus
671  */
672 const struct attribute_group nd_device_attribute_group = {
673         .attrs = nd_device_attributes,
674 };
675
676 static ssize_t numa_node_show(struct device *dev,
677                 struct device_attribute *attr, char *buf)
678 {
679         return sprintf(buf, "%d\n", dev_to_node(dev));
680 }
681 static DEVICE_ATTR_RO(numa_node);
682
683 static struct attribute *nd_numa_attributes[] = {
684         &dev_attr_numa_node.attr,
685         NULL,
686 };
687
688 static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
689                 int n)
690 {
691         if (!IS_ENABLED(CONFIG_NUMA))
692                 return 0;
693
694         return a->mode;
695 }
696
697 /*
698  * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
699  */
700 struct attribute_group nd_numa_attribute_group = {
701         .attrs = nd_numa_attributes,
702         .is_visible = nd_numa_attr_visible,
703 };
704 EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
705
706 int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
707 {
708         dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
709         struct device *dev;
710
711         dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
712                         "ndctl%d", nvdimm_bus->id);
713
714         if (IS_ERR(dev))
715                 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
716                                 nvdimm_bus->id, PTR_ERR(dev));
717         return PTR_ERR_OR_ZERO(dev);
718 }
719
720 void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
721 {
722         device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
723 }
724
725 static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
726         [ND_CMD_IMPLEMENTED] = { },
727         [ND_CMD_SMART] = {
728                 .out_num = 2,
729                 .out_sizes = { 4, 128, },
730         },
731         [ND_CMD_SMART_THRESHOLD] = {
732                 .out_num = 2,
733                 .out_sizes = { 4, 8, },
734         },
735         [ND_CMD_DIMM_FLAGS] = {
736                 .out_num = 2,
737                 .out_sizes = { 4, 4 },
738         },
739         [ND_CMD_GET_CONFIG_SIZE] = {
740                 .out_num = 3,
741                 .out_sizes = { 4, 4, 4, },
742         },
743         [ND_CMD_GET_CONFIG_DATA] = {
744                 .in_num = 2,
745                 .in_sizes = { 4, 4, },
746                 .out_num = 2,
747                 .out_sizes = { 4, UINT_MAX, },
748         },
749         [ND_CMD_SET_CONFIG_DATA] = {
750                 .in_num = 3,
751                 .in_sizes = { 4, 4, UINT_MAX, },
752                 .out_num = 1,
753                 .out_sizes = { 4, },
754         },
755         [ND_CMD_VENDOR] = {
756                 .in_num = 3,
757                 .in_sizes = { 4, 4, UINT_MAX, },
758                 .out_num = 3,
759                 .out_sizes = { 4, 4, UINT_MAX, },
760         },
761         [ND_CMD_CALL] = {
762                 .in_num = 2,
763                 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
764                 .out_num = 1,
765                 .out_sizes = { UINT_MAX, },
766         },
767 };
768
769 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
770 {
771         if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
772                 return &__nd_cmd_dimm_descs[cmd];
773         return NULL;
774 }
775 EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
776
777 static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
778         [ND_CMD_IMPLEMENTED] = { },
779         [ND_CMD_ARS_CAP] = {
780                 .in_num = 2,
781                 .in_sizes = { 8, 8, },
782                 .out_num = 4,
783                 .out_sizes = { 4, 4, 4, 4, },
784         },
785         [ND_CMD_ARS_START] = {
786                 .in_num = 5,
787                 .in_sizes = { 8, 8, 2, 1, 5, },
788                 .out_num = 2,
789                 .out_sizes = { 4, 4, },
790         },
791         [ND_CMD_ARS_STATUS] = {
792                 .out_num = 3,
793                 .out_sizes = { 4, 4, UINT_MAX, },
794         },
795         [ND_CMD_CLEAR_ERROR] = {
796                 .in_num = 2,
797                 .in_sizes = { 8, 8, },
798                 .out_num = 3,
799                 .out_sizes = { 4, 4, 8, },
800         },
801         [ND_CMD_CALL] = {
802                 .in_num = 2,
803                 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
804                 .out_num = 1,
805                 .out_sizes = { UINT_MAX, },
806         },
807 };
808
809 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
810 {
811         if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
812                 return &__nd_cmd_bus_descs[cmd];
813         return NULL;
814 }
815 EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
816
817 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
818                 const struct nd_cmd_desc *desc, int idx, void *buf)
819 {
820         if (idx >= desc->in_num)
821                 return UINT_MAX;
822
823         if (desc->in_sizes[idx] < UINT_MAX)
824                 return desc->in_sizes[idx];
825
826         if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
827                 struct nd_cmd_set_config_hdr *hdr = buf;
828
829                 return hdr->in_length;
830         } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
831                 struct nd_cmd_vendor_hdr *hdr = buf;
832
833                 return hdr->in_length;
834         } else if (cmd == ND_CMD_CALL) {
835                 struct nd_cmd_pkg *pkg = buf;
836
837                 return pkg->nd_size_in;
838         }
839
840         return UINT_MAX;
841 }
842 EXPORT_SYMBOL_GPL(nd_cmd_in_size);
843
844 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
845                 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
846                 const u32 *out_field, unsigned long remainder)
847 {
848         if (idx >= desc->out_num)
849                 return UINT_MAX;
850
851         if (desc->out_sizes[idx] < UINT_MAX)
852                 return desc->out_sizes[idx];
853
854         if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
855                 return in_field[1];
856         else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
857                 return out_field[1];
858         else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
859                 /*
860                  * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
861                  * "Size of Output Buffer in bytes, including this
862                  * field."
863                  */
864                 if (out_field[1] < 4)
865                         return 0;
866                 /*
867                  * ACPI 6.1 is ambiguous if 'status' is included in the
868                  * output size. If we encounter an output size that
869                  * overshoots the remainder by 4 bytes, assume it was
870                  * including 'status'.
871                  */
872                 if (out_field[1] - 4 == remainder)
873                         return remainder;
874                 return out_field[1] - 8;
875         } else if (cmd == ND_CMD_CALL) {
876                 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
877
878                 return pkg->nd_size_out;
879         }
880
881
882         return UINT_MAX;
883 }
884 EXPORT_SYMBOL_GPL(nd_cmd_out_size);
885
886 void wait_nvdimm_bus_probe_idle(struct device *dev)
887 {
888         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
889
890         do {
891                 if (nvdimm_bus->probe_active == 0)
892                         break;
893                 nvdimm_bus_unlock(dev);
894                 nd_device_unlock(dev);
895                 wait_event(nvdimm_bus->wait,
896                                 nvdimm_bus->probe_active == 0);
897                 nd_device_lock(dev);
898                 nvdimm_bus_lock(dev);
899         } while (true);
900 }
901
902 static int nd_pmem_forget_poison_check(struct device *dev, void *data)
903 {
904         struct nd_cmd_clear_error *clear_err =
905                 (struct nd_cmd_clear_error *)data;
906         struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
907         struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
908         struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
909         struct nd_namespace_common *ndns = NULL;
910         struct nd_namespace_io *nsio;
911         resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
912
913         if (nd_dax || !dev->driver)
914                 return 0;
915
916         start = clear_err->address;
917         end = clear_err->address + clear_err->cleared - 1;
918
919         if (nd_btt || nd_pfn || nd_dax) {
920                 if (nd_btt)
921                         ndns = nd_btt->ndns;
922                 else if (nd_pfn)
923                         ndns = nd_pfn->ndns;
924                 else if (nd_dax)
925                         ndns = nd_dax->nd_pfn.ndns;
926
927                 if (!ndns)
928                         return 0;
929         } else
930                 ndns = to_ndns(dev);
931
932         nsio = to_nd_namespace_io(&ndns->dev);
933         pstart = nsio->res.start + offset;
934         pend = nsio->res.end - end_trunc;
935
936         if ((pstart >= start) && (pend <= end))
937                 return -EBUSY;
938
939         return 0;
940
941 }
942
943 static int nd_ns_forget_poison_check(struct device *dev, void *data)
944 {
945         return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
946 }
947
948 /* set_config requires an idle interleave set */
949 static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
950                 struct nvdimm *nvdimm, unsigned int cmd, void *data)
951 {
952         struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
953
954         /* ask the bus provider if it would like to block this request */
955         if (nd_desc->clear_to_send) {
956                 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd, data);
957
958                 if (rc)
959                         return rc;
960         }
961
962         /* require clear error to go through the pmem driver */
963         if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
964                 return device_for_each_child(&nvdimm_bus->dev, data,
965                                 nd_ns_forget_poison_check);
966
967         if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
968                 return 0;
969
970         /* prevent label manipulation while the kernel owns label updates */
971         wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
972         if (atomic_read(&nvdimm->busy))
973                 return -EBUSY;
974         return 0;
975 }
976
977 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
978                 int read_only, unsigned int ioctl_cmd, unsigned long arg)
979 {
980         struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
981         const struct nd_cmd_desc *desc = NULL;
982         unsigned int cmd = _IOC_NR(ioctl_cmd);
983         struct device *dev = &nvdimm_bus->dev;
984         void __user *p = (void __user *) arg;
985         char *out_env = NULL, *in_env = NULL;
986         const char *cmd_name, *dimm_name;
987         u32 in_len = 0, out_len = 0;
988         unsigned int func = cmd;
989         unsigned long cmd_mask;
990         struct nd_cmd_pkg pkg;
991         int rc, i, cmd_rc;
992         void *buf = NULL;
993         u64 buf_len = 0;
994
995         if (nvdimm) {
996                 desc = nd_cmd_dimm_desc(cmd);
997                 cmd_name = nvdimm_cmd_name(cmd);
998                 cmd_mask = nvdimm->cmd_mask;
999                 dimm_name = dev_name(&nvdimm->dev);
1000         } else {
1001                 desc = nd_cmd_bus_desc(cmd);
1002                 cmd_name = nvdimm_bus_cmd_name(cmd);
1003                 cmd_mask = nd_desc->cmd_mask;
1004                 dimm_name = "bus";
1005         }
1006
1007         if (cmd == ND_CMD_CALL) {
1008                 if (copy_from_user(&pkg, p, sizeof(pkg)))
1009                         return -EFAULT;
1010         }
1011
1012         if (!desc || (desc->out_num + desc->in_num == 0) ||
1013                         !test_bit(cmd, &cmd_mask))
1014                 return -ENOTTY;
1015
1016         /* fail write commands (when read-only) */
1017         if (read_only)
1018                 switch (cmd) {
1019                 case ND_CMD_VENDOR:
1020                 case ND_CMD_SET_CONFIG_DATA:
1021                 case ND_CMD_ARS_START:
1022                 case ND_CMD_CLEAR_ERROR:
1023                 case ND_CMD_CALL:
1024                         dev_dbg(dev, "'%s' command while read-only.\n",
1025                                         nvdimm ? nvdimm_cmd_name(cmd)
1026                                         : nvdimm_bus_cmd_name(cmd));
1027                         return -EPERM;
1028                 default:
1029                         break;
1030                 }
1031
1032         /* process an input envelope */
1033         in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1034         if (!in_env)
1035                 return -ENOMEM;
1036         for (i = 0; i < desc->in_num; i++) {
1037                 u32 in_size, copy;
1038
1039                 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
1040                 if (in_size == UINT_MAX) {
1041                         dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
1042                                         __func__, dimm_name, cmd_name, i);
1043                         rc = -ENXIO;
1044                         goto out;
1045                 }
1046                 if (in_len < ND_CMD_MAX_ENVELOPE)
1047                         copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
1048                 else
1049                         copy = 0;
1050                 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) {
1051                         rc = -EFAULT;
1052                         goto out;
1053                 }
1054                 in_len += in_size;
1055         }
1056
1057         if (cmd == ND_CMD_CALL) {
1058                 func = pkg.nd_command;
1059                 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1060                                 dimm_name, pkg.nd_command,
1061                                 in_len, out_len, buf_len);
1062         }
1063
1064         /* process an output envelope */
1065         out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1066         if (!out_env) {
1067                 rc = -ENOMEM;
1068                 goto out;
1069         }
1070
1071         for (i = 0; i < desc->out_num; i++) {
1072                 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
1073                                 (u32 *) in_env, (u32 *) out_env, 0);
1074                 u32 copy;
1075
1076                 if (out_size == UINT_MAX) {
1077                         dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1078                                         dimm_name, cmd_name, i);
1079                         rc = -EFAULT;
1080                         goto out;
1081                 }
1082                 if (out_len < ND_CMD_MAX_ENVELOPE)
1083                         copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
1084                 else
1085                         copy = 0;
1086                 if (copy && copy_from_user(&out_env[out_len],
1087                                         p + in_len + out_len, copy)) {
1088                         rc = -EFAULT;
1089                         goto out;
1090                 }
1091                 out_len += out_size;
1092         }
1093
1094         buf_len = (u64) out_len + (u64) in_len;
1095         if (buf_len > ND_IOCTL_MAX_BUFLEN) {
1096                 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1097                                 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
1098                 rc = -EINVAL;
1099                 goto out;
1100         }
1101
1102         buf = vmalloc(buf_len);
1103         if (!buf) {
1104                 rc = -ENOMEM;
1105                 goto out;
1106         }
1107
1108         if (copy_from_user(buf, p, buf_len)) {
1109                 rc = -EFAULT;
1110                 goto out;
1111         }
1112
1113         nd_device_lock(dev);
1114         nvdimm_bus_lock(dev);
1115         rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
1116         if (rc)
1117                 goto out_unlock;
1118
1119         rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
1120         if (rc < 0)
1121                 goto out_unlock;
1122
1123         if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1124                 struct nd_cmd_clear_error *clear_err = buf;
1125
1126                 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1127                                 clear_err->cleared);
1128         }
1129
1130         if (copy_to_user(p, buf, buf_len))
1131                 rc = -EFAULT;
1132
1133 out_unlock:
1134         nvdimm_bus_unlock(dev);
1135         nd_device_unlock(dev);
1136 out:
1137         kfree(in_env);
1138         kfree(out_env);
1139         vfree(buf);
1140         return rc;
1141 }
1142
1143 enum nd_ioctl_mode {
1144         BUS_IOCTL,
1145         DIMM_IOCTL,
1146 };
1147
1148 static int match_dimm(struct device *dev, void *data)
1149 {
1150         long id = (long) data;
1151
1152         if (is_nvdimm(dev)) {
1153                 struct nvdimm *nvdimm = to_nvdimm(dev);
1154
1155                 return nvdimm->id == id;
1156         }
1157
1158         return 0;
1159 }
1160
1161 static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1162                 enum nd_ioctl_mode mode)
1163
1164 {
1165         struct nvdimm_bus *nvdimm_bus, *found = NULL;
1166         long id = (long) file->private_data;
1167         struct nvdimm *nvdimm = NULL;
1168         int rc, ro;
1169
1170         ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
1171         mutex_lock(&nvdimm_bus_list_mutex);
1172         list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1173                 if (mode == DIMM_IOCTL) {
1174                         struct device *dev;
1175
1176                         dev = device_find_child(&nvdimm_bus->dev,
1177                                         file->private_data, match_dimm);
1178                         if (!dev)
1179                                 continue;
1180                         nvdimm = to_nvdimm(dev);
1181                         found = nvdimm_bus;
1182                 } else if (nvdimm_bus->id == id) {
1183                         found = nvdimm_bus;
1184                 }
1185
1186                 if (found) {
1187                         atomic_inc(&nvdimm_bus->ioctl_active);
1188                         break;
1189                 }
1190         }
1191         mutex_unlock(&nvdimm_bus_list_mutex);
1192
1193         if (!found)
1194                 return -ENXIO;
1195
1196         nvdimm_bus = found;
1197         rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
1198
1199         if (nvdimm)
1200                 put_device(&nvdimm->dev);
1201         if (atomic_dec_and_test(&nvdimm_bus->ioctl_active))
1202                 wake_up(&nvdimm_bus->wait);
1203
1204         return rc;
1205 }
1206
1207 static long bus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1208 {
1209         return nd_ioctl(file, cmd, arg, BUS_IOCTL);
1210 }
1211
1212 static long dimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1213 {
1214         return nd_ioctl(file, cmd, arg, DIMM_IOCTL);
1215 }
1216
1217 static int nd_open(struct inode *inode, struct file *file)
1218 {
1219         long minor = iminor(inode);
1220
1221         file->private_data = (void *) minor;
1222         return 0;
1223 }
1224
1225 static const struct file_operations nvdimm_bus_fops = {
1226         .owner = THIS_MODULE,
1227         .open = nd_open,
1228         .unlocked_ioctl = bus_ioctl,
1229         .compat_ioctl = bus_ioctl,
1230         .llseek = noop_llseek,
1231 };
1232
1233 static const struct file_operations nvdimm_fops = {
1234         .owner = THIS_MODULE,
1235         .open = nd_open,
1236         .unlocked_ioctl = dimm_ioctl,
1237         .compat_ioctl = dimm_ioctl,
1238         .llseek = noop_llseek,
1239 };
1240
1241 int __init nvdimm_bus_init(void)
1242 {
1243         int rc;
1244
1245         rc = bus_register(&nvdimm_bus_type);
1246         if (rc)
1247                 return rc;
1248
1249         rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1250         if (rc < 0)
1251                 goto err_bus_chrdev;
1252         nvdimm_bus_major = rc;
1253
1254         rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1255         if (rc < 0)
1256                 goto err_dimm_chrdev;
1257         nvdimm_major = rc;
1258
1259         nd_class = class_create(THIS_MODULE, "nd");
1260         if (IS_ERR(nd_class)) {
1261                 rc = PTR_ERR(nd_class);
1262                 goto err_class;
1263         }
1264
1265         rc = driver_register(&nd_bus_driver.drv);
1266         if (rc)
1267                 goto err_nd_bus;
1268
1269         return 0;
1270
1271  err_nd_bus:
1272         class_destroy(nd_class);
1273  err_class:
1274         unregister_chrdev(nvdimm_major, "dimmctl");
1275  err_dimm_chrdev:
1276         unregister_chrdev(nvdimm_bus_major, "ndctl");
1277  err_bus_chrdev:
1278         bus_unregister(&nvdimm_bus_type);
1279
1280         return rc;
1281 }
1282
1283 void nvdimm_bus_exit(void)
1284 {
1285         driver_unregister(&nd_bus_driver.drv);
1286         class_destroy(nd_class);
1287         unregister_chrdev(nvdimm_bus_major, "ndctl");
1288         unregister_chrdev(nvdimm_major, "dimmctl");
1289         bus_unregister(&nvdimm_bus_type);
1290         ida_destroy(&nd_ida);
1291 }