Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
3d88002e DW |
2 | /* |
3 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. | |
3d88002e | 4 | */ |
5212e11f | 5 | #include <linux/cpumask.h> |
3d88002e DW |
6 | #include <linux/module.h> |
7 | #include <linux/device.h> | |
8 | #include <linux/nd.h> | |
6a6bef90 | 9 | #include "nd-core.h" |
3d88002e DW |
10 | #include "nd.h" |
11 | ||
12 | static int nd_region_probe(struct device *dev) | |
13 | { | |
047fc8a1 | 14 | int err, rc; |
5212e11f | 15 | static unsigned long once; |
e5ae3b25 | 16 | struct nd_region_data *ndrd; |
3d88002e | 17 | struct nd_region *nd_region = to_nd_region(dev); |
3d88002e | 18 | |
5212e11f VV |
19 | if (nd_region->num_lanes > num_online_cpus() |
20 | && nd_region->num_lanes < num_possible_cpus() | |
21 | && !test_and_set_bit(0, &once)) { | |
60ce0f93 | 22 | dev_dbg(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n", |
5212e11f VV |
23 | num_online_cpus(), nd_region->num_lanes, |
24 | num_possible_cpus()); | |
60ce0f93 | 25 | dev_dbg(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n", |
5212e11f VV |
26 | nd_region->num_lanes); |
27 | } | |
28 | ||
e5ae3b25 DW |
29 | rc = nd_region_activate(nd_region); |
30 | if (rc) | |
31 | return rc; | |
32 | ||
047fc8a1 RZ |
33 | rc = nd_blk_region_init(nd_region); |
34 | if (rc) | |
35 | return rc; | |
36 | ||
c42adf87 | 37 | if (is_memory(&nd_region->dev)) { |
a4574f63 DW |
38 | struct range range = { |
39 | .start = nd_region->ndr_start, | |
40 | .end = nd_region->ndr_start + nd_region->ndr_size - 1, | |
41 | }; | |
6a6bef90 DJ |
42 | |
43 | if (devm_init_badblocks(dev, &nd_region->bb)) | |
44 | return -ENODEV; | |
975750a9 TK |
45 | nd_region->bb_state = sysfs_get_dirent(nd_region->dev.kobj.sd, |
46 | "badblocks"); | |
6aa734a2 | 47 | if (!nd_region->bb_state) |
975750a9 | 48 | dev_warn(&nd_region->dev, |
6aa734a2 | 49 | "'badblocks' notification disabled\n"); |
a4574f63 | 50 | nvdimm_badblocks_populate(nd_region, &nd_region->bb, &range); |
6a6bef90 DJ |
51 | } |
52 | ||
700cd033 DW |
53 | rc = nd_region_register_namespaces(nd_region, &err); |
54 | if (rc < 0) | |
55 | return rc; | |
56 | ||
57 | ndrd = dev_get_drvdata(dev); | |
58 | ndrd->ns_active = rc; | |
59 | ndrd->ns_count = rc + err; | |
60 | ||
61 | if (rc && err && rc == err) | |
62 | return -ENODEV; | |
63 | ||
8c2f7e86 | 64 | nd_region->btt_seed = nd_btt_create(nd_region); |
e1455744 | 65 | nd_region->pfn_seed = nd_pfn_create(nd_region); |
cd03412a | 66 | nd_region->dax_seed = nd_dax_create(nd_region); |
3d88002e DW |
67 | if (err == 0) |
68 | return 0; | |
69 | ||
3d88002e DW |
70 | /* |
71 | * Given multiple namespaces per region, we do not want to | |
72 | * disable all the successfully registered peer namespaces upon | |
73 | * a single registration failure. If userspace is missing a | |
74 | * namespace that it expects it can disable/re-enable the region | |
75 | * to retry discovery after correcting the failure. | |
76 | * <regionX>/namespaces returns the current | |
77 | * "<async-registered>/<total>" namespace count. | |
78 | */ | |
79 | dev_err(dev, "failed to register %d namespace%s, continuing...\n", | |
80 | err, err == 1 ? "" : "s"); | |
81 | return 0; | |
82 | } | |
83 | ||
84 | static int child_unregister(struct device *dev, void *data) | |
85 | { | |
86 | nd_device_unregister(dev, ND_SYNC); | |
87 | return 0; | |
88 | } | |
89 | ||
90 | static int nd_region_remove(struct device *dev) | |
91 | { | |
bf9bccc1 DW |
92 | struct nd_region *nd_region = to_nd_region(dev); |
93 | ||
a8f72022 DW |
94 | device_for_each_child(dev, NULL, child_unregister); |
95 | ||
3d88002e DW |
96 | /* flush attribute readers and disable */ |
97 | nvdimm_bus_lock(dev); | |
bf9bccc1 | 98 | nd_region->ns_seed = NULL; |
8c2f7e86 | 99 | nd_region->btt_seed = NULL; |
e1455744 | 100 | nd_region->pfn_seed = NULL; |
cd03412a | 101 | nd_region->dax_seed = NULL; |
3d88002e DW |
102 | dev_set_drvdata(dev, NULL); |
103 | nvdimm_bus_unlock(dev); | |
104 | ||
6aa734a2 | 105 | /* |
87a30e1f | 106 | * Note, this assumes nd_device_lock() context to not race |
6aa734a2 DW |
107 | * nd_region_notify() |
108 | */ | |
109 | sysfs_put(nd_region->bb_state); | |
110 | nd_region->bb_state = NULL; | |
111 | ||
3d88002e DW |
112 | return 0; |
113 | } | |
114 | ||
71999466 DW |
115 | static int child_notify(struct device *dev, void *data) |
116 | { | |
117 | nd_device_notify(dev, *(enum nvdimm_event *) data); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | static void nd_region_notify(struct device *dev, enum nvdimm_event event) | |
122 | { | |
6a6bef90 DJ |
123 | if (event == NVDIMM_REVALIDATE_POISON) { |
124 | struct nd_region *nd_region = to_nd_region(dev); | |
6a6bef90 | 125 | |
c42adf87 | 126 | if (is_memory(&nd_region->dev)) { |
a4574f63 DW |
127 | struct range range = { |
128 | .start = nd_region->ndr_start, | |
129 | .end = nd_region->ndr_start + | |
130 | nd_region->ndr_size - 1, | |
131 | }; | |
132 | ||
6a6bef90 | 133 | nvdimm_badblocks_populate(nd_region, |
a4574f63 | 134 | &nd_region->bb, &range); |
975750a9 TK |
135 | if (nd_region->bb_state) |
136 | sysfs_notify_dirent(nd_region->bb_state); | |
6a6bef90 DJ |
137 | } |
138 | } | |
71999466 DW |
139 | device_for_each_child(dev, &event, child_notify); |
140 | } | |
141 | ||
3d88002e DW |
142 | static struct nd_device_driver nd_region_driver = { |
143 | .probe = nd_region_probe, | |
144 | .remove = nd_region_remove, | |
71999466 | 145 | .notify = nd_region_notify, |
3d88002e DW |
146 | .drv = { |
147 | .name = "nd_region", | |
148 | }, | |
149 | .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM, | |
150 | }; | |
151 | ||
152 | int __init nd_region_init(void) | |
153 | { | |
154 | return nd_driver_register(&nd_region_driver); | |
155 | } | |
156 | ||
157 | void nd_region_exit(void) | |
158 | { | |
159 | driver_unregister(&nd_region_driver.drv); | |
160 | } | |
161 | ||
162 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM); | |
163 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK); |