2 * Copyright (c) 2016-2017 Hisilicon Limited.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/list.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
16 static LIST_HEAD(hnae3_ae_algo_list);
17 static LIST_HEAD(hnae3_client_list);
18 static LIST_HEAD(hnae3_ae_dev_list);
20 /* we are keeping things simple and using single lock for all the
21 * list. This is a non-critical code so other updations, if happen
22 * in parallel, can wait.
24 static DEFINE_MUTEX(hnae3_common_lock);
26 static bool hnae3_client_match(enum hnae3_client_type client_type,
27 enum hnae3_dev_type dev_type)
29 if ((dev_type == HNAE3_DEV_KNIC) && (client_type == HNAE3_CLIENT_KNIC ||
30 client_type == HNAE3_CLIENT_ROCE))
33 if (dev_type == HNAE3_DEV_UNIC && client_type == HNAE3_CLIENT_UNIC)
39 static int hnae3_match_n_instantiate(struct hnae3_client *client,
40 struct hnae3_ae_dev *ae_dev, bool is_reg)
44 /* check if this client matches the type of ae_dev */
45 if (!(hnae3_client_match(client->type, ae_dev->dev_type) &&
46 hnae_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
50 /* now, (un-)instantiate client by calling lower layer */
52 ret = ae_dev->ops->init_client_instance(client, ae_dev);
54 dev_err(&ae_dev->pdev->dev,
55 "fail to instantiate client\n");
59 ae_dev->ops->uninit_client_instance(client, ae_dev);
63 int hnae3_register_client(struct hnae3_client *client)
65 struct hnae3_client *client_tmp;
66 struct hnae3_ae_dev *ae_dev;
69 mutex_lock(&hnae3_common_lock);
70 /* one system should only have one client for every type */
71 list_for_each_entry(client_tmp, &hnae3_client_list, node) {
72 if (client_tmp->type == client->type)
76 list_add_tail(&client->node, &hnae3_client_list);
78 /* initialize the client on every matched port */
79 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
80 /* if the client could not be initialized on current port, for
81 * any error reasons, move on to next available port
83 ret = hnae3_match_n_instantiate(client, ae_dev, true);
85 dev_err(&ae_dev->pdev->dev,
86 "match and instantiation failed for port\n");
90 mutex_unlock(&hnae3_common_lock);
94 EXPORT_SYMBOL(hnae3_register_client);
96 void hnae3_unregister_client(struct hnae3_client *client)
98 struct hnae3_ae_dev *ae_dev;
100 mutex_lock(&hnae3_common_lock);
101 /* un-initialize the client on every matched port */
102 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
103 hnae3_match_n_instantiate(client, ae_dev, false);
106 list_del(&client->node);
107 mutex_unlock(&hnae3_common_lock);
109 EXPORT_SYMBOL(hnae3_unregister_client);
111 /* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
112 * @ae_algo: AE algorithm
113 * NOTE: the duplicated name will not be checked
115 int hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo)
117 const struct pci_device_id *id;
118 struct hnae3_ae_dev *ae_dev;
119 struct hnae3_client *client;
122 mutex_lock(&hnae3_common_lock);
124 list_add_tail(&ae_algo->node, &hnae3_ae_algo_list);
126 /* Check if this algo/ops matches the list of ae_devs */
127 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
128 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
132 /* ae_dev init should set flag */
133 ae_dev->ops = ae_algo->ops;
134 ret = ae_algo->ops->init_ae_dev(ae_dev);
136 dev_err(&ae_dev->pdev->dev, "init ae_dev error.\n");
140 hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
142 /* check the client list for the match with this ae_dev type and
143 * initialize the figure out client instance
145 list_for_each_entry(client, &hnae3_client_list, node) {
146 ret = hnae3_match_n_instantiate(client, ae_dev, true);
148 dev_err(&ae_dev->pdev->dev,
149 "match and instantiation failed\n");
153 mutex_unlock(&hnae3_common_lock);
157 EXPORT_SYMBOL(hnae3_register_ae_algo);
159 /* hnae3_unregister_ae_algo - unregisters a AE algorithm
160 * @ae_algo: the AE algorithm to unregister
162 void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo)
164 const struct pci_device_id *id;
165 struct hnae3_ae_dev *ae_dev;
166 struct hnae3_client *client;
168 mutex_lock(&hnae3_common_lock);
169 /* Check if there are matched ae_dev */
170 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
171 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
175 /* check the client list for the match with this ae_dev type and
176 * un-initialize the figure out client instance
178 list_for_each_entry(client, &hnae3_client_list, node)
179 hnae3_match_n_instantiate(client, ae_dev, false);
181 ae_algo->ops->uninit_ae_dev(ae_dev);
182 hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
185 list_del(&ae_algo->node);
186 mutex_unlock(&hnae3_common_lock);
188 EXPORT_SYMBOL(hnae3_unregister_ae_algo);
190 /* hnae3_register_ae_dev - registers a AE device to hnae3 framework
191 * @ae_dev: the AE device
192 * NOTE: the duplicated name will not be checked
194 int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev)
196 const struct pci_device_id *id;
197 struct hnae3_ae_algo *ae_algo;
198 struct hnae3_client *client;
201 mutex_lock(&hnae3_common_lock);
202 list_add_tail(&ae_dev->node, &hnae3_ae_dev_list);
204 /* Check if there are matched ae_algo */
205 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
206 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
210 ae_dev->ops = ae_algo->ops;
213 dev_err(&ae_dev->pdev->dev, "ae_dev ops are null\n");
217 /* ae_dev init should set flag */
218 ret = ae_dev->ops->init_ae_dev(ae_dev);
220 dev_err(&ae_dev->pdev->dev, "init ae_dev error\n");
224 hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
228 /* check the client list for the match with this ae_dev type and
229 * initialize the figure out client instance
231 list_for_each_entry(client, &hnae3_client_list, node) {
232 ret = hnae3_match_n_instantiate(client, ae_dev, true);
234 dev_err(&ae_dev->pdev->dev,
235 "match and instantiation failed\n");
239 mutex_unlock(&hnae3_common_lock);
243 EXPORT_SYMBOL(hnae3_register_ae_dev);
245 /* hnae3_unregister_ae_dev - unregisters a AE device
246 * @ae_dev: the AE device to unregister
248 void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev)
250 const struct pci_device_id *id;
251 struct hnae3_ae_algo *ae_algo;
252 struct hnae3_client *client;
254 mutex_lock(&hnae3_common_lock);
255 /* Check if there are matched ae_algo */
256 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
257 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
261 list_for_each_entry(client, &hnae3_client_list, node)
262 hnae3_match_n_instantiate(client, ae_dev, false);
264 ae_algo->ops->uninit_ae_dev(ae_dev);
265 hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
268 list_del(&ae_dev->node);
269 mutex_unlock(&hnae3_common_lock);
271 EXPORT_SYMBOL(hnae3_unregister_ae_dev);
273 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
274 MODULE_LICENSE("GPL");
275 MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");