1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/cpumask.h>
6 #include <linux/module.h>
7 #include <linux/device.h>
12 static int nd_region_probe(struct device *dev)
15 static unsigned long once;
16 struct nd_region_data *ndrd;
17 struct nd_region *nd_region = to_nd_region(dev);
18 struct range range = {
19 .start = nd_region->ndr_start,
20 .end = nd_region->ndr_start + nd_region->ndr_size - 1,
23 if (nd_region->num_lanes > num_online_cpus()
24 && nd_region->num_lanes < num_possible_cpus()
25 && !test_and_set_bit(0, &once)) {
26 dev_dbg(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n",
27 num_online_cpus(), nd_region->num_lanes,
29 dev_dbg(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n",
30 nd_region->num_lanes);
33 rc = nd_region_activate(nd_region);
37 if (devm_init_badblocks(dev, &nd_region->bb))
40 sysfs_get_dirent(nd_region->dev.kobj.sd, "badblocks");
41 if (!nd_region->bb_state)
42 dev_warn(dev, "'badblocks' notification disabled\n");
43 nvdimm_badblocks_populate(nd_region, &nd_region->bb, &range);
45 rc = nd_region_register_namespaces(nd_region, &err);
49 ndrd = dev_get_drvdata(dev);
51 ndrd->ns_count = rc + err;
53 if (rc && err && rc == err)
56 nd_region->btt_seed = nd_btt_create(nd_region);
57 nd_region->pfn_seed = nd_pfn_create(nd_region);
58 nd_region->dax_seed = nd_dax_create(nd_region);
63 * Given multiple namespaces per region, we do not want to
64 * disable all the successfully registered peer namespaces upon
65 * a single registration failure. If userspace is missing a
66 * namespace that it expects it can disable/re-enable the region
67 * to retry discovery after correcting the failure.
68 * <regionX>/namespaces returns the current
69 * "<async-registered>/<total>" namespace count.
71 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
72 err, err == 1 ? "" : "s");
76 static int child_unregister(struct device *dev, void *data)
78 nd_device_unregister(dev, ND_SYNC);
82 static void nd_region_remove(struct device *dev)
84 struct nd_region *nd_region = to_nd_region(dev);
86 device_for_each_child(dev, NULL, child_unregister);
88 /* flush attribute readers and disable */
90 nd_region->ns_seed = NULL;
91 nd_region->btt_seed = NULL;
92 nd_region->pfn_seed = NULL;
93 nd_region->dax_seed = NULL;
94 dev_set_drvdata(dev, NULL);
95 nvdimm_bus_unlock(dev);
98 * Note, this assumes device_lock() context to not race
101 sysfs_put(nd_region->bb_state);
102 nd_region->bb_state = NULL;
105 static int child_notify(struct device *dev, void *data)
107 nd_device_notify(dev, *(enum nvdimm_event *) data);
111 static void nd_region_notify(struct device *dev, enum nvdimm_event event)
113 if (event == NVDIMM_REVALIDATE_POISON) {
114 struct nd_region *nd_region = to_nd_region(dev);
116 if (is_memory(&nd_region->dev)) {
117 struct range range = {
118 .start = nd_region->ndr_start,
119 .end = nd_region->ndr_start +
120 nd_region->ndr_size - 1,
123 nvdimm_badblocks_populate(nd_region,
124 &nd_region->bb, &range);
125 if (nd_region->bb_state)
126 sysfs_notify_dirent(nd_region->bb_state);
129 device_for_each_child(dev, &event, child_notify);
132 static struct nd_device_driver nd_region_driver = {
133 .probe = nd_region_probe,
134 .remove = nd_region_remove,
135 .notify = nd_region_notify,
139 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
142 int __init nd_region_init(void)
144 return nd_driver_register(&nd_region_driver);
147 void nd_region_exit(void)
149 driver_unregister(&nd_region_driver.drv);
152 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);