1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
4 #include <linux/list.h>
5 #include <linux/spinlock.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 void hnae3_unregister_ae_algo_prepare(struct hnae3_ae_algo *ae_algo)
15 const struct pci_device_id *pci_id;
16 struct hnae3_ae_dev *ae_dev;
21 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
22 if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
25 pci_id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
28 if (IS_ENABLED(CONFIG_PCI_IOV)) {
29 device_lock(&ae_dev->pdev->dev);
30 pci_disable_sriov(ae_dev->pdev);
31 device_unlock(&ae_dev->pdev->dev);
35 EXPORT_SYMBOL(hnae3_unregister_ae_algo_prepare);
37 /* we are keeping things simple and using single lock for all the
38 * list. This is a non-critical code so other updations, if happen
39 * in parallel, can wait.
41 static DEFINE_MUTEX(hnae3_common_lock);
43 /* ensure the drivers being unloaded one by one */
44 static DEFINE_MUTEX(hnae3_unload_lock);
46 void hnae3_acquire_unload_lock(void)
48 mutex_lock(&hnae3_unload_lock);
50 EXPORT_SYMBOL(hnae3_acquire_unload_lock);
52 void hnae3_release_unload_lock(void)
54 mutex_unlock(&hnae3_unload_lock);
56 EXPORT_SYMBOL(hnae3_release_unload_lock);
58 static bool hnae3_client_match(enum hnae3_client_type client_type)
60 if (client_type == HNAE3_CLIENT_KNIC ||
61 client_type == HNAE3_CLIENT_ROCE)
67 void hnae3_set_client_init_flag(struct hnae3_client *client,
68 struct hnae3_ae_dev *ae_dev,
71 if (!client || !ae_dev)
74 switch (client->type) {
75 case HNAE3_CLIENT_KNIC:
76 hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited);
78 case HNAE3_CLIENT_ROCE:
79 hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited);
85 EXPORT_SYMBOL(hnae3_set_client_init_flag);
87 static int hnae3_get_client_init_flag(struct hnae3_client *client,
88 struct hnae3_ae_dev *ae_dev)
92 switch (client->type) {
93 case HNAE3_CLIENT_KNIC:
94 inited = hnae3_get_bit(ae_dev->flag,
95 HNAE3_KNIC_CLIENT_INITED_B);
97 case HNAE3_CLIENT_ROCE:
98 inited = hnae3_get_bit(ae_dev->flag,
99 HNAE3_ROCE_CLIENT_INITED_B);
108 static int hnae3_init_client_instance(struct hnae3_client *client,
109 struct hnae3_ae_dev *ae_dev)
113 /* check if this client matches the type of ae_dev */
114 if (!(hnae3_client_match(client->type) &&
115 hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
119 ret = ae_dev->ops->init_client_instance(client, ae_dev);
121 dev_err(&ae_dev->pdev->dev,
122 "fail to instantiate client, ret = %d\n", ret);
127 static void hnae3_uninit_client_instance(struct hnae3_client *client,
128 struct hnae3_ae_dev *ae_dev)
130 /* check if this client matches the type of ae_dev */
131 if (!(hnae3_client_match(client->type) &&
132 hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B)))
135 if (hnae3_get_client_init_flag(client, ae_dev)) {
136 ae_dev->ops->uninit_client_instance(client, ae_dev);
138 hnae3_set_client_init_flag(client, ae_dev, 0);
142 int hnae3_register_client(struct hnae3_client *client)
144 struct hnae3_client *client_tmp;
145 struct hnae3_ae_dev *ae_dev;
150 mutex_lock(&hnae3_common_lock);
151 /* one system should only have one client for every type */
152 list_for_each_entry(client_tmp, &hnae3_client_list, node) {
153 if (client_tmp->type == client->type)
157 list_add_tail(&client->node, &hnae3_client_list);
159 /* initialize the client on every matched port */
160 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
161 /* if the client could not be initialized on current port, for
162 * any error reasons, move on to next available port
164 int ret = hnae3_init_client_instance(client, ae_dev);
166 dev_err(&ae_dev->pdev->dev,
167 "match and instantiation failed for port, ret = %d\n",
172 mutex_unlock(&hnae3_common_lock);
176 EXPORT_SYMBOL(hnae3_register_client);
178 void hnae3_unregister_client(struct hnae3_client *client)
180 struct hnae3_client *client_tmp;
181 struct hnae3_ae_dev *ae_dev;
182 bool existed = false;
187 mutex_lock(&hnae3_common_lock);
188 /* one system should only have one client for every type */
189 list_for_each_entry(client_tmp, &hnae3_client_list, node) {
190 if (client_tmp->type == client->type) {
197 mutex_unlock(&hnae3_common_lock);
198 pr_err("client %s does not exist!\n", client->name);
202 /* un-initialize the client on every matched port */
203 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
204 hnae3_uninit_client_instance(client, ae_dev);
207 list_del(&client->node);
208 mutex_unlock(&hnae3_common_lock);
210 EXPORT_SYMBOL(hnae3_unregister_client);
212 /* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
213 * @ae_algo: AE algorithm
214 * NOTE: the duplicated name will not be checked
216 void hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo)
218 const struct pci_device_id *id;
219 struct hnae3_ae_dev *ae_dev;
220 struct hnae3_client *client;
226 mutex_lock(&hnae3_common_lock);
228 list_add_tail(&ae_algo->node, &hnae3_ae_algo_list);
230 /* Check if this algo/ops matches the list of ae_devs */
231 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
232 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
237 dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
240 ae_dev->ops = ae_algo->ops;
242 ret = ae_algo->ops->init_ae_dev(ae_dev);
244 dev_err(&ae_dev->pdev->dev,
245 "init ae_dev error, ret = %d\n", ret);
249 /* ae_dev init should set flag */
250 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
252 /* check the client list for the match with this ae_dev type and
253 * initialize the figure out client instance
255 list_for_each_entry(client, &hnae3_client_list, node) {
256 ret = hnae3_init_client_instance(client, ae_dev);
258 dev_err(&ae_dev->pdev->dev,
259 "match and instantiation failed, ret = %d\n",
264 mutex_unlock(&hnae3_common_lock);
266 EXPORT_SYMBOL(hnae3_register_ae_algo);
268 /* hnae3_unregister_ae_algo - unregisters a AE algorithm
269 * @ae_algo: the AE algorithm to unregister
271 void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo)
273 const struct pci_device_id *id;
274 struct hnae3_ae_dev *ae_dev;
275 struct hnae3_client *client;
280 mutex_lock(&hnae3_common_lock);
281 /* Check if there are matched ae_dev */
282 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
283 if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
286 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
290 /* check the client list for the match with this ae_dev type and
291 * un-initialize the figure out client instance
293 list_for_each_entry(client, &hnae3_client_list, node)
294 hnae3_uninit_client_instance(client, ae_dev);
296 ae_algo->ops->uninit_ae_dev(ae_dev);
297 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
301 list_del(&ae_algo->node);
302 mutex_unlock(&hnae3_common_lock);
304 EXPORT_SYMBOL(hnae3_unregister_ae_algo);
306 /* hnae3_register_ae_dev - registers a AE device to hnae3 framework
307 * @ae_dev: the AE device
308 * NOTE: the duplicated name will not be checked
310 int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev)
312 const struct pci_device_id *id;
313 struct hnae3_ae_algo *ae_algo;
314 struct hnae3_client *client;
320 mutex_lock(&hnae3_common_lock);
322 list_add_tail(&ae_dev->node, &hnae3_ae_dev_list);
324 /* Check if there are matched ae_algo */
325 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
326 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
331 dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
335 ae_dev->ops = ae_algo->ops;
337 ret = ae_dev->ops->init_ae_dev(ae_dev);
339 dev_err(&ae_dev->pdev->dev,
340 "init ae_dev error, ret = %d\n", ret);
344 /* ae_dev init should set flag */
345 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
349 /* check the client list for the match with this ae_dev type and
350 * initialize the figure out client instance
352 list_for_each_entry(client, &hnae3_client_list, node) {
353 ret = hnae3_init_client_instance(client, ae_dev);
355 dev_err(&ae_dev->pdev->dev,
356 "match and instantiation failed, ret = %d\n",
360 mutex_unlock(&hnae3_common_lock);
365 list_del(&ae_dev->node);
366 mutex_unlock(&hnae3_common_lock);
370 EXPORT_SYMBOL(hnae3_register_ae_dev);
372 /* hnae3_unregister_ae_dev - unregisters a AE device
373 * @ae_dev: the AE device to unregister
375 void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev)
377 const struct pci_device_id *id;
378 struct hnae3_ae_algo *ae_algo;
379 struct hnae3_client *client;
384 mutex_lock(&hnae3_common_lock);
385 /* Check if there are matched ae_algo */
386 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
387 if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
390 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
394 list_for_each_entry(client, &hnae3_client_list, node)
395 hnae3_uninit_client_instance(client, ae_dev);
397 ae_algo->ops->uninit_ae_dev(ae_dev);
398 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
402 list_del(&ae_dev->node);
403 mutex_unlock(&hnae3_common_lock);
405 EXPORT_SYMBOL(hnae3_unregister_ae_dev);
407 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
408 MODULE_LICENSE("GPL");
409 MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");
410 MODULE_VERSION(HNAE3_MOD_VERSION);