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
,
20 enum hnae3_dev_type dev_type
)
22 if ((dev_type
== HNAE3_DEV_KNIC
) && (client_type
== HNAE3_CLIENT_KNIC
||
23 client_type
== HNAE3_CLIENT_ROCE
))
26 if (dev_type
== HNAE3_DEV_UNIC
&& client_type
== HNAE3_CLIENT_UNIC
)
32 static void hnae3_set_client_init_flag(struct hnae3_client
*client
,
33 struct hnae3_ae_dev
*ae_dev
, int inited
)
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_UNIC
:
40 hnae3_set_bit(ae_dev
->flag
, HNAE3_UNIC_CLIENT_INITED_B
, inited
);
42 case HNAE3_CLIENT_ROCE
:
43 hnae3_set_bit(ae_dev
->flag
, HNAE3_ROCE_CLIENT_INITED_B
, inited
);
50 static int hnae3_get_client_init_flag(struct hnae3_client
*client
,
51 struct hnae3_ae_dev
*ae_dev
)
55 switch (client
->type
) {
56 case HNAE3_CLIENT_KNIC
:
57 inited
= hnae3_get_bit(ae_dev
->flag
,
58 HNAE3_KNIC_CLIENT_INITED_B
);
60 case HNAE3_CLIENT_UNIC
:
61 inited
= hnae3_get_bit(ae_dev
->flag
,
62 HNAE3_UNIC_CLIENT_INITED_B
);
64 case HNAE3_CLIENT_ROCE
:
65 inited
= hnae3_get_bit(ae_dev
->flag
,
66 HNAE3_ROCE_CLIENT_INITED_B
);
75 static int hnae3_match_n_instantiate(struct hnae3_client
*client
,
76 struct hnae3_ae_dev
*ae_dev
, bool is_reg
)
80 /* check if this client matches the type of ae_dev */
81 if (!(hnae3_client_match(client
->type
, ae_dev
->dev_type
) &&
82 hnae3_get_bit(ae_dev
->flag
, HNAE3_DEV_INITED_B
))) {
86 /* now, (un-)instantiate client by calling lower layer */
88 ret
= ae_dev
->ops
->init_client_instance(client
, ae_dev
);
90 dev_err(&ae_dev
->pdev
->dev
,
91 "fail to instantiate client, ret = %d\n", ret
);
95 hnae3_set_client_init_flag(client
, ae_dev
, 1);
99 if (hnae3_get_client_init_flag(client
, ae_dev
)) {
100 ae_dev
->ops
->uninit_client_instance(client
, ae_dev
);
102 hnae3_set_client_init_flag(client
, ae_dev
, 0);
108 int hnae3_register_client(struct hnae3_client
*client
)
110 struct hnae3_client
*client_tmp
;
111 struct hnae3_ae_dev
*ae_dev
;
114 mutex_lock(&hnae3_common_lock
);
115 /* one system should only have one client for every type */
116 list_for_each_entry(client_tmp
, &hnae3_client_list
, node
) {
117 if (client_tmp
->type
== client
->type
)
121 list_add_tail(&client
->node
, &hnae3_client_list
);
123 /* initialize the client on every matched port */
124 list_for_each_entry(ae_dev
, &hnae3_ae_dev_list
, node
) {
125 /* if the client could not be initialized on current port, for
126 * any error reasons, move on to next available port
128 ret
= hnae3_match_n_instantiate(client
, ae_dev
, true);
130 dev_err(&ae_dev
->pdev
->dev
,
131 "match and instantiation failed for port, ret = %d\n",
136 mutex_unlock(&hnae3_common_lock
);
140 EXPORT_SYMBOL(hnae3_register_client
);
142 void hnae3_unregister_client(struct hnae3_client
*client
)
144 struct hnae3_ae_dev
*ae_dev
;
146 mutex_lock(&hnae3_common_lock
);
147 /* un-initialize the client on every matched port */
148 list_for_each_entry(ae_dev
, &hnae3_ae_dev_list
, node
) {
149 hnae3_match_n_instantiate(client
, ae_dev
, false);
152 list_del(&client
->node
);
153 mutex_unlock(&hnae3_common_lock
);
155 EXPORT_SYMBOL(hnae3_unregister_client
);
157 /* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
158 * @ae_algo: AE algorithm
159 * NOTE: the duplicated name will not be checked
161 void hnae3_register_ae_algo(struct hnae3_ae_algo
*ae_algo
)
163 const struct pci_device_id
*id
;
164 struct hnae3_ae_dev
*ae_dev
;
165 struct hnae3_client
*client
;
168 mutex_lock(&hnae3_common_lock
);
170 list_add_tail(&ae_algo
->node
, &hnae3_ae_algo_list
);
172 /* Check if this algo/ops matches the list of ae_devs */
173 list_for_each_entry(ae_dev
, &hnae3_ae_dev_list
, node
) {
174 id
= pci_match_id(ae_algo
->pdev_id_table
, ae_dev
->pdev
);
178 /* ae_dev init should set flag */
179 ae_dev
->ops
= ae_algo
->ops
;
180 ret
= ae_algo
->ops
->init_ae_dev(ae_dev
);
182 dev_err(&ae_dev
->pdev
->dev
,
183 "init ae_dev error, ret = %d\n", ret
);
187 hnae3_set_bit(ae_dev
->flag
, HNAE3_DEV_INITED_B
, 1);
189 /* check the client list for the match with this ae_dev type and
190 * initialize the figure out client instance
192 list_for_each_entry(client
, &hnae3_client_list
, node
) {
193 ret
= hnae3_match_n_instantiate(client
, ae_dev
, true);
195 dev_err(&ae_dev
->pdev
->dev
,
196 "match and instantiation failed, ret = %d\n",
201 mutex_unlock(&hnae3_common_lock
);
203 EXPORT_SYMBOL(hnae3_register_ae_algo
);
205 /* hnae3_unregister_ae_algo - unregisters a AE algorithm
206 * @ae_algo: the AE algorithm to unregister
208 void hnae3_unregister_ae_algo(struct hnae3_ae_algo
*ae_algo
)
210 const struct pci_device_id
*id
;
211 struct hnae3_ae_dev
*ae_dev
;
212 struct hnae3_client
*client
;
214 mutex_lock(&hnae3_common_lock
);
215 /* Check if there are matched ae_dev */
216 list_for_each_entry(ae_dev
, &hnae3_ae_dev_list
, node
) {
217 if (!hnae3_get_bit(ae_dev
->flag
, HNAE3_DEV_INITED_B
))
220 id
= pci_match_id(ae_algo
->pdev_id_table
, ae_dev
->pdev
);
224 /* check the client list for the match with this ae_dev type and
225 * un-initialize the figure out client instance
227 list_for_each_entry(client
, &hnae3_client_list
, node
)
228 hnae3_match_n_instantiate(client
, ae_dev
, false);
230 ae_algo
->ops
->uninit_ae_dev(ae_dev
);
231 hnae3_set_bit(ae_dev
->flag
, HNAE3_DEV_INITED_B
, 0);
234 list_del(&ae_algo
->node
);
235 mutex_unlock(&hnae3_common_lock
);
237 EXPORT_SYMBOL(hnae3_unregister_ae_algo
);
239 /* hnae3_register_ae_dev - registers a AE device to hnae3 framework
240 * @ae_dev: the AE device
241 * NOTE: the duplicated name will not be checked
243 void hnae3_register_ae_dev(struct hnae3_ae_dev
*ae_dev
)
245 const struct pci_device_id
*id
;
246 struct hnae3_ae_algo
*ae_algo
;
247 struct hnae3_client
*client
;
250 mutex_lock(&hnae3_common_lock
);
252 list_add_tail(&ae_dev
->node
, &hnae3_ae_dev_list
);
254 /* Check if there are matched ae_algo */
255 list_for_each_entry(ae_algo
, &hnae3_ae_algo_list
, node
) {
256 id
= pci_match_id(ae_algo
->pdev_id_table
, ae_dev
->pdev
);
260 ae_dev
->ops
= ae_algo
->ops
;
263 dev_err(&ae_dev
->pdev
->dev
, "ae_dev ops are null\n");
267 /* ae_dev init should set flag */
268 ret
= ae_dev
->ops
->init_ae_dev(ae_dev
);
270 dev_err(&ae_dev
->pdev
->dev
,
271 "init ae_dev error, ret = %d\n", ret
);
275 hnae3_set_bit(ae_dev
->flag
, HNAE3_DEV_INITED_B
, 1);
279 /* check the client list for the match with this ae_dev type and
280 * initialize the figure out client instance
282 list_for_each_entry(client
, &hnae3_client_list
, node
) {
283 ret
= hnae3_match_n_instantiate(client
, ae_dev
, true);
285 dev_err(&ae_dev
->pdev
->dev
,
286 "match and instantiation failed, ret = %d\n",
291 mutex_unlock(&hnae3_common_lock
);
293 EXPORT_SYMBOL(hnae3_register_ae_dev
);
295 /* hnae3_unregister_ae_dev - unregisters a AE device
296 * @ae_dev: the AE device to unregister
298 void hnae3_unregister_ae_dev(struct hnae3_ae_dev
*ae_dev
)
300 const struct pci_device_id
*id
;
301 struct hnae3_ae_algo
*ae_algo
;
302 struct hnae3_client
*client
;
304 mutex_lock(&hnae3_common_lock
);
305 /* Check if there are matched ae_algo */
306 list_for_each_entry(ae_algo
, &hnae3_ae_algo_list
, node
) {
307 if (!hnae3_get_bit(ae_dev
->flag
, HNAE3_DEV_INITED_B
))
310 id
= pci_match_id(ae_algo
->pdev_id_table
, ae_dev
->pdev
);
314 list_for_each_entry(client
, &hnae3_client_list
, node
)
315 hnae3_match_n_instantiate(client
, ae_dev
, false);
317 ae_algo
->ops
->uninit_ae_dev(ae_dev
);
318 hnae3_set_bit(ae_dev
->flag
, HNAE3_DEV_INITED_B
, 0);
321 list_del(&ae_dev
->node
);
322 mutex_unlock(&hnae3_common_lock
);
324 EXPORT_SYMBOL(hnae3_unregister_ae_dev
);
326 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
327 MODULE_LICENSE("GPL");
328 MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");
329 MODULE_VERSION(HNAE3_MOD_VERSION
);