hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/drivers/net/ethernet/hisilicon/hns3/hnae3.c
....@@ -37,28 +37,25 @@
3737 */
3838 static DEFINE_MUTEX(hnae3_common_lock);
3939
40
-static bool hnae3_client_match(enum hnae3_client_type client_type,
41
- enum hnae3_dev_type dev_type)
40
+static bool hnae3_client_match(enum hnae3_client_type client_type)
4241 {
43
- if ((dev_type == HNAE3_DEV_KNIC) && (client_type == HNAE3_CLIENT_KNIC ||
44
- client_type == HNAE3_CLIENT_ROCE))
45
- return true;
46
-
47
- if (dev_type == HNAE3_DEV_UNIC && client_type == HNAE3_CLIENT_UNIC)
42
+ if (client_type == HNAE3_CLIENT_KNIC ||
43
+ client_type == HNAE3_CLIENT_ROCE)
4844 return true;
4945
5046 return false;
5147 }
5248
5349 void hnae3_set_client_init_flag(struct hnae3_client *client,
54
- struct hnae3_ae_dev *ae_dev, int inited)
50
+ struct hnae3_ae_dev *ae_dev,
51
+ unsigned int inited)
5552 {
53
+ if (!client || !ae_dev)
54
+ return;
55
+
5656 switch (client->type) {
5757 case HNAE3_CLIENT_KNIC:
5858 hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited);
59
- break;
60
- case HNAE3_CLIENT_UNIC:
61
- hnae3_set_bit(ae_dev->flag, HNAE3_UNIC_CLIENT_INITED_B, inited);
6259 break;
6360 case HNAE3_CLIENT_ROCE:
6461 hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited);
....@@ -70,7 +67,7 @@
7067 EXPORT_SYMBOL(hnae3_set_client_init_flag);
7168
7269 static int hnae3_get_client_init_flag(struct hnae3_client *client,
73
- struct hnae3_ae_dev *ae_dev)
70
+ struct hnae3_ae_dev *ae_dev)
7471 {
7572 int inited = 0;
7673
....@@ -78,10 +75,6 @@
7875 case HNAE3_CLIENT_KNIC:
7976 inited = hnae3_get_bit(ae_dev->flag,
8077 HNAE3_KNIC_CLIENT_INITED_B);
81
- break;
82
- case HNAE3_CLIENT_UNIC:
83
- inited = hnae3_get_bit(ae_dev->flag,
84
- HNAE3_UNIC_CLIENT_INITED_B);
8578 break;
8679 case HNAE3_CLIENT_ROCE:
8780 inited = hnae3_get_bit(ae_dev->flag,
....@@ -94,41 +87,47 @@
9487 return inited;
9588 }
9689
97
-static int hnae3_match_n_instantiate(struct hnae3_client *client,
98
- struct hnae3_ae_dev *ae_dev, bool is_reg)
90
+static int hnae3_init_client_instance(struct hnae3_client *client,
91
+ struct hnae3_ae_dev *ae_dev)
9992 {
10093 int ret;
10194
10295 /* check if this client matches the type of ae_dev */
103
- if (!(hnae3_client_match(client->type, ae_dev->dev_type) &&
96
+ if (!(hnae3_client_match(client->type) &&
10497 hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
10598 return 0;
10699 }
107100
108
- /* now, (un-)instantiate client by calling lower layer */
109
- if (is_reg) {
110
- ret = ae_dev->ops->init_client_instance(client, ae_dev);
111
- if (ret)
112
- dev_err(&ae_dev->pdev->dev,
113
- "fail to instantiate client, ret = %d\n", ret);
101
+ ret = ae_dev->ops->init_client_instance(client, ae_dev);
102
+ if (ret)
103
+ dev_err(&ae_dev->pdev->dev,
104
+ "fail to instantiate client, ret = %d\n", ret);
114105
115
- return ret;
116
- }
106
+ return ret;
107
+}
108
+
109
+static void hnae3_uninit_client_instance(struct hnae3_client *client,
110
+ struct hnae3_ae_dev *ae_dev)
111
+{
112
+ /* check if this client matches the type of ae_dev */
113
+ if (!(hnae3_client_match(client->type) &&
114
+ hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B)))
115
+ return;
117116
118117 if (hnae3_get_client_init_flag(client, ae_dev)) {
119118 ae_dev->ops->uninit_client_instance(client, ae_dev);
120119
121120 hnae3_set_client_init_flag(client, ae_dev, 0);
122121 }
123
-
124
- return 0;
125122 }
126123
127124 int hnae3_register_client(struct hnae3_client *client)
128125 {
129126 struct hnae3_client *client_tmp;
130127 struct hnae3_ae_dev *ae_dev;
131
- int ret = 0;
128
+
129
+ if (!client)
130
+ return -ENODEV;
132131
133132 mutex_lock(&hnae3_common_lock);
134133 /* one system should only have one client for every type */
....@@ -144,7 +143,7 @@
144143 /* if the client could not be initialized on current port, for
145144 * any error reasons, move on to next available port
146145 */
147
- ret = hnae3_match_n_instantiate(client, ae_dev, true);
146
+ int ret = hnae3_init_client_instance(client, ae_dev);
148147 if (ret)
149148 dev_err(&ae_dev->pdev->dev,
150149 "match and instantiation failed for port, ret = %d\n",
....@@ -160,12 +159,31 @@
160159
161160 void hnae3_unregister_client(struct hnae3_client *client)
162161 {
162
+ struct hnae3_client *client_tmp;
163163 struct hnae3_ae_dev *ae_dev;
164
+ bool existed = false;
165
+
166
+ if (!client)
167
+ return;
164168
165169 mutex_lock(&hnae3_common_lock);
170
+ /* one system should only have one client for every type */
171
+ list_for_each_entry(client_tmp, &hnae3_client_list, node) {
172
+ if (client_tmp->type == client->type) {
173
+ existed = true;
174
+ break;
175
+ }
176
+ }
177
+
178
+ if (!existed) {
179
+ mutex_unlock(&hnae3_common_lock);
180
+ pr_err("client %s does not exist!\n", client->name);
181
+ return;
182
+ }
183
+
166184 /* un-initialize the client on every matched port */
167185 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
168
- hnae3_match_n_instantiate(client, ae_dev, false);
186
+ hnae3_uninit_client_instance(client, ae_dev);
169187 }
170188
171189 list_del(&client->node);
....@@ -182,7 +200,10 @@
182200 const struct pci_device_id *id;
183201 struct hnae3_ae_dev *ae_dev;
184202 struct hnae3_client *client;
185
- int ret = 0;
203
+ int ret;
204
+
205
+ if (!ae_algo)
206
+ return;
186207
187208 mutex_lock(&hnae3_common_lock);
188209
....@@ -214,7 +235,7 @@
214235 * initialize the figure out client instance
215236 */
216237 list_for_each_entry(client, &hnae3_client_list, node) {
217
- ret = hnae3_match_n_instantiate(client, ae_dev, true);
238
+ ret = hnae3_init_client_instance(client, ae_dev);
218239 if (ret)
219240 dev_err(&ae_dev->pdev->dev,
220241 "match and instantiation failed, ret = %d\n",
....@@ -235,6 +256,9 @@
235256 struct hnae3_ae_dev *ae_dev;
236257 struct hnae3_client *client;
237258
259
+ if (!ae_algo)
260
+ return;
261
+
238262 mutex_lock(&hnae3_common_lock);
239263 /* Check if there are matched ae_dev */
240264 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
....@@ -249,7 +273,7 @@
249273 * un-initialize the figure out client instance
250274 */
251275 list_for_each_entry(client, &hnae3_client_list, node)
252
- hnae3_match_n_instantiate(client, ae_dev, false);
276
+ hnae3_uninit_client_instance(client, ae_dev);
253277
254278 ae_algo->ops->uninit_ae_dev(ae_dev);
255279 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
....@@ -270,7 +294,10 @@
270294 const struct pci_device_id *id;
271295 struct hnae3_ae_algo *ae_algo;
272296 struct hnae3_client *client;
273
- int ret = 0;
297
+ int ret;
298
+
299
+ if (!ae_dev)
300
+ return -ENODEV;
274301
275302 mutex_lock(&hnae3_common_lock);
276303
....@@ -305,7 +332,7 @@
305332 * initialize the figure out client instance
306333 */
307334 list_for_each_entry(client, &hnae3_client_list, node) {
308
- ret = hnae3_match_n_instantiate(client, ae_dev, true);
335
+ ret = hnae3_init_client_instance(client, ae_dev);
309336 if (ret)
310337 dev_err(&ae_dev->pdev->dev,
311338 "match and instantiation failed, ret = %d\n",
....@@ -333,6 +360,9 @@
333360 struct hnae3_ae_algo *ae_algo;
334361 struct hnae3_client *client;
335362
363
+ if (!ae_dev)
364
+ return;
365
+
336366 mutex_lock(&hnae3_common_lock);
337367 /* Check if there are matched ae_algo */
338368 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
....@@ -344,7 +374,7 @@
344374 continue;
345375
346376 list_for_each_entry(client, &hnae3_client_list, node)
347
- hnae3_match_n_instantiate(client, ae_dev, false);
377
+ hnae3_uninit_client_instance(client, ae_dev);
348378
349379 ae_algo->ops->uninit_ae_dev(ae_dev);
350380 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);