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
);
19 if (nd_region
->num_lanes
> num_online_cpus()
20 && nd_region
->num_lanes
< num_possible_cpus()
21 && !test_and_set_bit(0, &once
)) {
22 dev_dbg(dev
, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n",
23 num_online_cpus(), nd_region
->num_lanes
,
25 dev_dbg(dev
, "setting nr_cpus=%d may yield better libnvdimm device performance\n",
26 nd_region
->num_lanes
);
29 rc
= nd_region_activate(nd_region
);
33 rc
= nd_blk_region_init(nd_region
);
37 if (is_memory(&nd_region
->dev
)) {
38 struct resource ndr_res
;
40 if (devm_init_badblocks(dev
, &nd_region
->bb
))
42 nd_region
->bb_state
= sysfs_get_dirent(nd_region
->dev
.kobj
.sd
,
44 if (!nd_region
->bb_state
)
45 dev_warn(&nd_region
->dev
,
46 "'badblocks' notification disabled\n");
47 ndr_res
.start
= nd_region
->ndr_start
;
48 ndr_res
.end
= nd_region
->ndr_start
+ nd_region
->ndr_size
- 1;
49 nvdimm_badblocks_populate(nd_region
, &nd_region
->bb
, &ndr_res
);
52 rc
= nd_region_register_namespaces(nd_region
, &err
);
56 ndrd
= dev_get_drvdata(dev
);
58 ndrd
->ns_count
= rc
+ err
;
60 if (rc
&& err
&& rc
== err
)
63 nd_region
->btt_seed
= nd_btt_create(nd_region
);
64 nd_region
->pfn_seed
= nd_pfn_create(nd_region
);
65 nd_region
->dax_seed
= nd_dax_create(nd_region
);
70 * Given multiple namespaces per region, we do not want to
71 * disable all the successfully registered peer namespaces upon
72 * a single registration failure. If userspace is missing a
73 * namespace that it expects it can disable/re-enable the region
74 * to retry discovery after correcting the failure.
75 * <regionX>/namespaces returns the current
76 * "<async-registered>/<total>" namespace count.
78 dev_err(dev
, "failed to register %d namespace%s, continuing...\n",
79 err
, err
== 1 ? "" : "s");
83 static int child_unregister(struct device
*dev
, void *data
)
85 nd_device_unregister(dev
, ND_SYNC
);
89 static int nd_region_remove(struct device
*dev
)
91 struct nd_region
*nd_region
= to_nd_region(dev
);
93 device_for_each_child(dev
, NULL
, child_unregister
);
95 /* flush attribute readers and disable */
97 nd_region
->ns_seed
= NULL
;
98 nd_region
->btt_seed
= NULL
;
99 nd_region
->pfn_seed
= NULL
;
100 nd_region
->dax_seed
= NULL
;
101 dev_set_drvdata(dev
, NULL
);
102 nvdimm_bus_unlock(dev
);
105 * Note, this assumes nd_device_lock() context to not race
108 sysfs_put(nd_region
->bb_state
);
109 nd_region
->bb_state
= NULL
;
114 static int child_notify(struct device
*dev
, void *data
)
116 nd_device_notify(dev
, *(enum nvdimm_event
*) data
);
120 static void nd_region_notify(struct device
*dev
, enum nvdimm_event event
)
122 if (event
== NVDIMM_REVALIDATE_POISON
) {
123 struct nd_region
*nd_region
= to_nd_region(dev
);
126 if (is_memory(&nd_region
->dev
)) {
127 res
.start
= nd_region
->ndr_start
;
128 res
.end
= nd_region
->ndr_start
+
129 nd_region
->ndr_size
- 1;
130 nvdimm_badblocks_populate(nd_region
,
131 &nd_region
->bb
, &res
);
132 if (nd_region
->bb_state
)
133 sysfs_notify_dirent(nd_region
->bb_state
);
136 device_for_each_child(dev
, &event
, child_notify
);
139 static struct nd_device_driver nd_region_driver
= {
140 .probe
= nd_region_probe
,
141 .remove
= nd_region_remove
,
142 .notify
= nd_region_notify
,
146 .type
= ND_DRIVER_REGION_BLK
| ND_DRIVER_REGION_PMEM
,
149 int __init
nd_region_init(void)
151 return nd_driver_register(&nd_region_driver
);
154 void nd_region_exit(void)
156 driver_unregister(&nd_region_driver
.drv
);
159 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM
);
160 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK
);