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 /* 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)
28 void hnae3_set_client_init_flag(struct hnae3_client *client,
29 struct hnae3_ae_dev *ae_dev,
32 if (!client || !ae_dev)
35 switch (client->type) {
36 case HNAE3_CLIENT_KNIC:
37 hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited);
39 case HNAE3_CLIENT_ROCE:
40 hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited);
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)
53 switch (client->type) {
54 case HNAE3_CLIENT_KNIC:
55 inited = hnae3_get_bit(ae_dev->flag,
56 HNAE3_KNIC_CLIENT_INITED_B);
58 case HNAE3_CLIENT_ROCE:
59 inited = hnae3_get_bit(ae_dev->flag,
60 HNAE3_ROCE_CLIENT_INITED_B);
69 static int hnae3_init_client_instance(struct hnae3_client *client,
70 struct hnae3_ae_dev *ae_dev)
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))) {
80 ret = ae_dev->ops->init_client_instance(client, ae_dev);
82 dev_err(&ae_dev->pdev->dev,
83 "fail to instantiate client, ret = %d\n", 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)))
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;
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)
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);
127 dev_err(&ae_dev->pdev->dev,
128 "match and instantiation failed for port, ret = %d\n",
133 mutex_unlock(&hnae3_common_lock);
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;
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) {
158 mutex_unlock(&hnae3_common_lock);
159 pr_err("client %s does not exist!\n", client->name);
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;
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);
198 dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
201 ae_dev->ops = ae_algo->ops;
203 ret = ae_algo->ops->init_ae_dev(ae_dev);
205 dev_err(&ae_dev->pdev->dev,
206 "init ae_dev error, ret = %d\n", ret);
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);
219 dev_err(&ae_dev->pdev->dev,
220 "match and instantiation failed, ret = %d\n",
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;
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))
247 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
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);
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;
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);
292 dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
296 ae_dev->ops = ae_algo->ops;
298 ret = ae_dev->ops->init_ae_dev(ae_dev);
300 dev_err(&ae_dev->pdev->dev,
301 "init ae_dev error, ret = %d\n", ret);
305 /* ae_dev init should set flag */
306 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
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);
316 dev_err(&ae_dev->pdev->dev,
317 "match and instantiation failed, ret = %d\n",
321 mutex_unlock(&hnae3_common_lock);
326 list_del(&ae_dev->node);
327 mutex_unlock(&hnae3_common_lock);
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;
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))
351 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
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);
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);