xref: /openbmc/linux/drivers/net/ethernet/hisilicon/hns3/hnae3.c (revision 023e41632e065d49bcbe31b3c4b336217f96a271)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include <linux/list.h>
5 #include <linux/spinlock.h>
6 
7 #include "hnae3.h"
8 
9 static LIST_HEAD(hnae3_ae_algo_list);
10 static LIST_HEAD(hnae3_client_list);
11 static LIST_HEAD(hnae3_ae_dev_list);
12 
13 /* we are keeping things simple and using single lock for all the
14  * list. This is a non-critical code so other updations, if happen
15  * in parallel, can wait.
16  */
17 static DEFINE_MUTEX(hnae3_common_lock);
18 
19 static bool hnae3_client_match(enum hnae3_client_type client_type,
20 			       enum hnae3_dev_type dev_type)
21 {
22 	if ((dev_type == HNAE3_DEV_KNIC) && (client_type == HNAE3_CLIENT_KNIC ||
23 					     client_type == HNAE3_CLIENT_ROCE))
24 		return true;
25 
26 	if (dev_type == HNAE3_DEV_UNIC && client_type == HNAE3_CLIENT_UNIC)
27 		return true;
28 
29 	return false;
30 }
31 
32 void hnae3_set_client_init_flag(struct hnae3_client *client,
33 				struct hnae3_ae_dev *ae_dev, int inited)
34 {
35 	if (!client || !ae_dev)
36 		return;
37 
38 	switch (client->type) {
39 	case HNAE3_CLIENT_KNIC:
40 		hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited);
41 		break;
42 	case HNAE3_CLIENT_UNIC:
43 		hnae3_set_bit(ae_dev->flag, HNAE3_UNIC_CLIENT_INITED_B, inited);
44 		break;
45 	case HNAE3_CLIENT_ROCE:
46 		hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited);
47 		break;
48 	default:
49 		break;
50 	}
51 }
52 EXPORT_SYMBOL(hnae3_set_client_init_flag);
53 
54 static int hnae3_get_client_init_flag(struct hnae3_client *client,
55 				       struct hnae3_ae_dev *ae_dev)
56 {
57 	int inited = 0;
58 
59 	switch (client->type) {
60 	case HNAE3_CLIENT_KNIC:
61 		inited = hnae3_get_bit(ae_dev->flag,
62 				       HNAE3_KNIC_CLIENT_INITED_B);
63 		break;
64 	case HNAE3_CLIENT_UNIC:
65 		inited = hnae3_get_bit(ae_dev->flag,
66 				       HNAE3_UNIC_CLIENT_INITED_B);
67 		break;
68 	case HNAE3_CLIENT_ROCE:
69 		inited = hnae3_get_bit(ae_dev->flag,
70 				       HNAE3_ROCE_CLIENT_INITED_B);
71 		break;
72 	default:
73 		break;
74 	}
75 
76 	return inited;
77 }
78 
79 static int hnae3_match_n_instantiate(struct hnae3_client *client,
80 				     struct hnae3_ae_dev *ae_dev, bool is_reg)
81 {
82 	int ret;
83 
84 	/* check if this client matches the type of ae_dev */
85 	if (!(hnae3_client_match(client->type, ae_dev->dev_type) &&
86 	      hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
87 		return 0;
88 	}
89 
90 	/* now, (un-)instantiate client by calling lower layer */
91 	if (is_reg) {
92 		ret = ae_dev->ops->init_client_instance(client, ae_dev);
93 		if (ret)
94 			dev_err(&ae_dev->pdev->dev,
95 				"fail to instantiate client, ret = %d\n", ret);
96 
97 		return ret;
98 	}
99 
100 	if (hnae3_get_client_init_flag(client, ae_dev)) {
101 		ae_dev->ops->uninit_client_instance(client, ae_dev);
102 
103 		hnae3_set_client_init_flag(client, ae_dev, 0);
104 	}
105 
106 	return 0;
107 }
108 
109 int hnae3_register_client(struct hnae3_client *client)
110 {
111 	struct hnae3_client *client_tmp;
112 	struct hnae3_ae_dev *ae_dev;
113 	int ret = 0;
114 
115 	if (!client)
116 		return -ENODEV;
117 
118 	mutex_lock(&hnae3_common_lock);
119 	/* one system should only have one client for every type */
120 	list_for_each_entry(client_tmp, &hnae3_client_list, node) {
121 		if (client_tmp->type == client->type)
122 			goto exit;
123 	}
124 
125 	list_add_tail(&client->node, &hnae3_client_list);
126 
127 	/* initialize the client on every matched port */
128 	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
129 		/* if the client could not be initialized on current port, for
130 		 * any error reasons, move on to next available port
131 		 */
132 		ret = hnae3_match_n_instantiate(client, ae_dev, true);
133 		if (ret)
134 			dev_err(&ae_dev->pdev->dev,
135 				"match and instantiation failed for port, ret = %d\n",
136 				ret);
137 	}
138 
139 exit:
140 	mutex_unlock(&hnae3_common_lock);
141 
142 	return 0;
143 }
144 EXPORT_SYMBOL(hnae3_register_client);
145 
146 void hnae3_unregister_client(struct hnae3_client *client)
147 {
148 	struct hnae3_ae_dev *ae_dev;
149 
150 	if (!client)
151 		return;
152 
153 	mutex_lock(&hnae3_common_lock);
154 	/* un-initialize the client on every matched port */
155 	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
156 		hnae3_match_n_instantiate(client, ae_dev, false);
157 	}
158 
159 	list_del(&client->node);
160 	mutex_unlock(&hnae3_common_lock);
161 }
162 EXPORT_SYMBOL(hnae3_unregister_client);
163 
164 /* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
165  * @ae_algo: AE algorithm
166  * NOTE: the duplicated name will not be checked
167  */
168 void hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo)
169 {
170 	const struct pci_device_id *id;
171 	struct hnae3_ae_dev *ae_dev;
172 	struct hnae3_client *client;
173 	int ret = 0;
174 
175 	if (!ae_algo)
176 		return;
177 
178 	mutex_lock(&hnae3_common_lock);
179 
180 	list_add_tail(&ae_algo->node, &hnae3_ae_algo_list);
181 
182 	/* Check if this algo/ops matches the list of ae_devs */
183 	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
184 		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
185 		if (!id)
186 			continue;
187 
188 		if (!ae_algo->ops) {
189 			dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
190 			continue;
191 		}
192 		ae_dev->ops = ae_algo->ops;
193 
194 		ret = ae_algo->ops->init_ae_dev(ae_dev);
195 		if (ret) {
196 			dev_err(&ae_dev->pdev->dev,
197 				"init ae_dev error, ret = %d\n", ret);
198 			continue;
199 		}
200 
201 		/* ae_dev init should set flag */
202 		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
203 
204 		/* check the client list for the match with this ae_dev type and
205 		 * initialize the figure out client instance
206 		 */
207 		list_for_each_entry(client, &hnae3_client_list, node) {
208 			ret = hnae3_match_n_instantiate(client, ae_dev, true);
209 			if (ret)
210 				dev_err(&ae_dev->pdev->dev,
211 					"match and instantiation failed, ret = %d\n",
212 					ret);
213 		}
214 	}
215 
216 	mutex_unlock(&hnae3_common_lock);
217 }
218 EXPORT_SYMBOL(hnae3_register_ae_algo);
219 
220 /* hnae3_unregister_ae_algo - unregisters a AE algorithm
221  * @ae_algo: the AE algorithm to unregister
222  */
223 void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo)
224 {
225 	const struct pci_device_id *id;
226 	struct hnae3_ae_dev *ae_dev;
227 	struct hnae3_client *client;
228 
229 	if (!ae_algo)
230 		return;
231 
232 	mutex_lock(&hnae3_common_lock);
233 	/* Check if there are matched ae_dev */
234 	list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
235 		if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
236 			continue;
237 
238 		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
239 		if (!id)
240 			continue;
241 
242 		/* check the client list for the match with this ae_dev type and
243 		 * un-initialize the figure out client instance
244 		 */
245 		list_for_each_entry(client, &hnae3_client_list, node)
246 			hnae3_match_n_instantiate(client, ae_dev, false);
247 
248 		ae_algo->ops->uninit_ae_dev(ae_dev);
249 		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
250 	}
251 
252 	list_del(&ae_algo->node);
253 	mutex_unlock(&hnae3_common_lock);
254 }
255 EXPORT_SYMBOL(hnae3_unregister_ae_algo);
256 
257 /* hnae3_register_ae_dev - registers a AE device to hnae3 framework
258  * @ae_dev: the AE device
259  * NOTE: the duplicated name will not be checked
260  */
261 int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev)
262 {
263 	const struct pci_device_id *id;
264 	struct hnae3_ae_algo *ae_algo;
265 	struct hnae3_client *client;
266 	int ret = 0;
267 
268 	if (!ae_dev)
269 		return -ENODEV;
270 
271 	mutex_lock(&hnae3_common_lock);
272 
273 	list_add_tail(&ae_dev->node, &hnae3_ae_dev_list);
274 
275 	/* Check if there are matched ae_algo */
276 	list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
277 		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
278 		if (!id)
279 			continue;
280 
281 		if (!ae_algo->ops) {
282 			dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
283 			ret = -EOPNOTSUPP;
284 			goto out_err;
285 		}
286 		ae_dev->ops = ae_algo->ops;
287 
288 		ret = ae_dev->ops->init_ae_dev(ae_dev);
289 		if (ret) {
290 			dev_err(&ae_dev->pdev->dev,
291 				"init ae_dev error, ret = %d\n", ret);
292 			goto out_err;
293 		}
294 
295 		/* ae_dev init should set flag */
296 		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
297 		break;
298 	}
299 
300 	/* check the client list for the match with this ae_dev type and
301 	 * initialize the figure out client instance
302 	 */
303 	list_for_each_entry(client, &hnae3_client_list, node) {
304 		ret = hnae3_match_n_instantiate(client, ae_dev, true);
305 		if (ret)
306 			dev_err(&ae_dev->pdev->dev,
307 				"match and instantiation failed, ret = %d\n",
308 				ret);
309 	}
310 
311 	mutex_unlock(&hnae3_common_lock);
312 
313 	return 0;
314 
315 out_err:
316 	list_del(&ae_dev->node);
317 	mutex_unlock(&hnae3_common_lock);
318 
319 	return ret;
320 }
321 EXPORT_SYMBOL(hnae3_register_ae_dev);
322 
323 /* hnae3_unregister_ae_dev - unregisters a AE device
324  * @ae_dev: the AE device to unregister
325  */
326 void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev)
327 {
328 	const struct pci_device_id *id;
329 	struct hnae3_ae_algo *ae_algo;
330 	struct hnae3_client *client;
331 
332 	if (!ae_dev)
333 		return;
334 
335 	mutex_lock(&hnae3_common_lock);
336 	/* Check if there are matched ae_algo */
337 	list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
338 		if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
339 			continue;
340 
341 		id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
342 		if (!id)
343 			continue;
344 
345 		list_for_each_entry(client, &hnae3_client_list, node)
346 			hnae3_match_n_instantiate(client, ae_dev, false);
347 
348 		ae_algo->ops->uninit_ae_dev(ae_dev);
349 		hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
350 	}
351 
352 	list_del(&ae_dev->node);
353 	mutex_unlock(&hnae3_common_lock);
354 }
355 EXPORT_SYMBOL(hnae3_unregister_ae_dev);
356 
357 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
358 MODULE_LICENSE("GPL");
359 MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");
360 MODULE_VERSION(HNAE3_MOD_VERSION);
361