WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / ethernet / hisilicon / hns3 / hnae3.c
blobeef1b2764d34ae60499cb3ce110c4390aba138eb
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
4 #include <linux/list.h>
5 #include <linux/spinlock.h>
7 #include "hnae3.h"
9 static LIST_HEAD(hnae3_ae_algo_list);
10 static LIST_HEAD(hnae3_client_list);
11 static LIST_HEAD(hnae3_ae_dev_list);
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.
17 static DEFINE_MUTEX(hnae3_common_lock);
19 static bool hnae3_client_match(enum hnae3_client_type client_type)
21 if (client_type == HNAE3_CLIENT_KNIC ||
22 client_type == HNAE3_CLIENT_ROCE)
23 return true;
25 return false;
28 void hnae3_set_client_init_flag(struct hnae3_client *client,
29 struct hnae3_ae_dev *ae_dev,
30 unsigned int inited)
32 if (!client || !ae_dev)
33 return;
35 switch (client->type) {
36 case HNAE3_CLIENT_KNIC:
37 hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited);
38 break;
39 case HNAE3_CLIENT_ROCE:
40 hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited);
41 break;
42 default:
43 break;
46 EXPORT_SYMBOL(hnae3_set_client_init_flag);
48 static int hnae3_get_client_init_flag(struct hnae3_client *client,
49 struct hnae3_ae_dev *ae_dev)
51 int inited = 0;
53 switch (client->type) {
54 case HNAE3_CLIENT_KNIC:
55 inited = hnae3_get_bit(ae_dev->flag,
56 HNAE3_KNIC_CLIENT_INITED_B);
57 break;
58 case HNAE3_CLIENT_ROCE:
59 inited = hnae3_get_bit(ae_dev->flag,
60 HNAE3_ROCE_CLIENT_INITED_B);
61 break;
62 default:
63 break;
66 return inited;
69 static int hnae3_init_client_instance(struct hnae3_client *client,
70 struct hnae3_ae_dev *ae_dev)
72 int ret;
74 /* check if this client matches the type of ae_dev */
75 if (!(hnae3_client_match(client->type) &&
76 hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
77 return 0;
80 ret = ae_dev->ops->init_client_instance(client, ae_dev);
81 if (ret)
82 dev_err(&ae_dev->pdev->dev,
83 "fail to instantiate client, ret = %d\n", ret);
85 return ret;
88 static void hnae3_uninit_client_instance(struct hnae3_client *client,
89 struct hnae3_ae_dev *ae_dev)
91 /* check if this client matches the type of ae_dev */
92 if (!(hnae3_client_match(client->type) &&
93 hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B)))
94 return;
96 if (hnae3_get_client_init_flag(client, ae_dev)) {
97 ae_dev->ops->uninit_client_instance(client, ae_dev);
99 hnae3_set_client_init_flag(client, ae_dev, 0);
103 int hnae3_register_client(struct hnae3_client *client)
105 struct hnae3_client *client_tmp;
106 struct hnae3_ae_dev *ae_dev;
108 if (!client)
109 return -ENODEV;
111 mutex_lock(&hnae3_common_lock);
112 /* one system should only have one client for every type */
113 list_for_each_entry(client_tmp, &hnae3_client_list, node) {
114 if (client_tmp->type == client->type)
115 goto exit;
118 list_add_tail(&client->node, &hnae3_client_list);
120 /* initialize the client on every matched port */
121 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
122 /* if the client could not be initialized on current port, for
123 * any error reasons, move on to next available port
125 int ret = hnae3_init_client_instance(client, ae_dev);
126 if (ret)
127 dev_err(&ae_dev->pdev->dev,
128 "match and instantiation failed for port, ret = %d\n",
129 ret);
132 exit:
133 mutex_unlock(&hnae3_common_lock);
135 return 0;
137 EXPORT_SYMBOL(hnae3_register_client);
139 void hnae3_unregister_client(struct hnae3_client *client)
141 struct hnae3_client *client_tmp;
142 struct hnae3_ae_dev *ae_dev;
143 bool existed = false;
145 if (!client)
146 return;
148 mutex_lock(&hnae3_common_lock);
149 /* one system should only have one client for every type */
150 list_for_each_entry(client_tmp, &hnae3_client_list, node) {
151 if (client_tmp->type == client->type) {
152 existed = true;
153 break;
157 if (!existed) {
158 mutex_unlock(&hnae3_common_lock);
159 pr_err("client %s does not exist!\n", client->name);
160 return;
163 /* un-initialize the client on every matched port */
164 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
165 hnae3_uninit_client_instance(client, ae_dev);
168 list_del(&client->node);
169 mutex_unlock(&hnae3_common_lock);
171 EXPORT_SYMBOL(hnae3_unregister_client);
173 /* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
174 * @ae_algo: AE algorithm
175 * NOTE: the duplicated name will not be checked
177 void hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo)
179 const struct pci_device_id *id;
180 struct hnae3_ae_dev *ae_dev;
181 struct hnae3_client *client;
182 int ret;
184 if (!ae_algo)
185 return;
187 mutex_lock(&hnae3_common_lock);
189 list_add_tail(&ae_algo->node, &hnae3_ae_algo_list);
191 /* Check if this algo/ops matches the list of ae_devs */
192 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
193 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
194 if (!id)
195 continue;
197 if (!ae_algo->ops) {
198 dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
199 continue;
201 ae_dev->ops = ae_algo->ops;
203 ret = ae_algo->ops->init_ae_dev(ae_dev);
204 if (ret) {
205 dev_err(&ae_dev->pdev->dev,
206 "init ae_dev error, ret = %d\n", ret);
207 continue;
210 /* ae_dev init should set flag */
211 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
213 /* check the client list for the match with this ae_dev type and
214 * initialize the figure out client instance
216 list_for_each_entry(client, &hnae3_client_list, node) {
217 ret = hnae3_init_client_instance(client, ae_dev);
218 if (ret)
219 dev_err(&ae_dev->pdev->dev,
220 "match and instantiation failed, ret = %d\n",
221 ret);
225 mutex_unlock(&hnae3_common_lock);
227 EXPORT_SYMBOL(hnae3_register_ae_algo);
229 /* hnae3_unregister_ae_algo - unregisters a AE algorithm
230 * @ae_algo: the AE algorithm to unregister
232 void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo)
234 const struct pci_device_id *id;
235 struct hnae3_ae_dev *ae_dev;
236 struct hnae3_client *client;
238 if (!ae_algo)
239 return;
241 mutex_lock(&hnae3_common_lock);
242 /* Check if there are matched ae_dev */
243 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
244 if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
245 continue;
247 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
248 if (!id)
249 continue;
251 /* check the client list for the match with this ae_dev type and
252 * un-initialize the figure out client instance
254 list_for_each_entry(client, &hnae3_client_list, node)
255 hnae3_uninit_client_instance(client, ae_dev);
257 ae_algo->ops->uninit_ae_dev(ae_dev);
258 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
259 ae_dev->ops = NULL;
262 list_del(&ae_algo->node);
263 mutex_unlock(&hnae3_common_lock);
265 EXPORT_SYMBOL(hnae3_unregister_ae_algo);
267 /* hnae3_register_ae_dev - registers a AE device to hnae3 framework
268 * @ae_dev: the AE device
269 * NOTE: the duplicated name will not be checked
271 int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev)
273 const struct pci_device_id *id;
274 struct hnae3_ae_algo *ae_algo;
275 struct hnae3_client *client;
276 int ret;
278 if (!ae_dev)
279 return -ENODEV;
281 mutex_lock(&hnae3_common_lock);
283 list_add_tail(&ae_dev->node, &hnae3_ae_dev_list);
285 /* Check if there are matched ae_algo */
286 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
287 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
288 if (!id)
289 continue;
291 if (!ae_algo->ops) {
292 dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
293 ret = -EOPNOTSUPP;
294 goto out_err;
296 ae_dev->ops = ae_algo->ops;
298 ret = ae_dev->ops->init_ae_dev(ae_dev);
299 if (ret) {
300 dev_err(&ae_dev->pdev->dev,
301 "init ae_dev error, ret = %d\n", ret);
302 goto out_err;
305 /* ae_dev init should set flag */
306 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
307 break;
310 /* check the client list for the match with this ae_dev type and
311 * initialize the figure out client instance
313 list_for_each_entry(client, &hnae3_client_list, node) {
314 ret = hnae3_init_client_instance(client, ae_dev);
315 if (ret)
316 dev_err(&ae_dev->pdev->dev,
317 "match and instantiation failed, ret = %d\n",
318 ret);
321 mutex_unlock(&hnae3_common_lock);
323 return 0;
325 out_err:
326 list_del(&ae_dev->node);
327 mutex_unlock(&hnae3_common_lock);
329 return ret;
331 EXPORT_SYMBOL(hnae3_register_ae_dev);
333 /* hnae3_unregister_ae_dev - unregisters a AE device
334 * @ae_dev: the AE device to unregister
336 void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev)
338 const struct pci_device_id *id;
339 struct hnae3_ae_algo *ae_algo;
340 struct hnae3_client *client;
342 if (!ae_dev)
343 return;
345 mutex_lock(&hnae3_common_lock);
346 /* Check if there are matched ae_algo */
347 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
348 if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
349 continue;
351 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
352 if (!id)
353 continue;
355 list_for_each_entry(client, &hnae3_client_list, node)
356 hnae3_uninit_client_instance(client, ae_dev);
358 ae_algo->ops->uninit_ae_dev(ae_dev);
359 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
360 ae_dev->ops = NULL;
363 list_del(&ae_dev->node);
364 mutex_unlock(&hnae3_common_lock);
366 EXPORT_SYMBOL(hnae3_unregister_ae_dev);
368 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
369 MODULE_LICENSE("GPL");
370 MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");
371 MODULE_VERSION(HNAE3_MOD_VERSION);