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 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);
49 EXPORT_SYMBOL(hnae3_set_client_init_flag);
51 static int hnae3_get_client_init_flag(struct hnae3_client *client,
52 struct hnae3_ae_dev *ae_dev)
56 switch (client->type) {
57 case HNAE3_CLIENT_KNIC:
58 inited = hnae3_get_bit(ae_dev->flag,
59 HNAE3_KNIC_CLIENT_INITED_B);
61 case HNAE3_CLIENT_UNIC:
62 inited = hnae3_get_bit(ae_dev->flag,
63 HNAE3_UNIC_CLIENT_INITED_B);
65 case HNAE3_CLIENT_ROCE:
66 inited = hnae3_get_bit(ae_dev->flag,
67 HNAE3_ROCE_CLIENT_INITED_B);
76 static int hnae3_match_n_instantiate(struct hnae3_client *client,
77 struct hnae3_ae_dev *ae_dev, bool is_reg)
81 /* check if this client matches the type of ae_dev */
82 if (!(hnae3_client_match(client->type, ae_dev->dev_type) &&
83 hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
87 /* now, (un-)instantiate client by calling lower layer */
89 ret = ae_dev->ops->init_client_instance(client, ae_dev);
91 dev_err(&ae_dev->pdev->dev,
92 "fail to instantiate client, ret = %d\n", ret);
97 if (hnae3_get_client_init_flag(client, ae_dev)) {
98 ae_dev->ops->uninit_client_instance(client, ae_dev);
100 hnae3_set_client_init_flag(client, ae_dev, 0);
106 int hnae3_register_client(struct hnae3_client *client)
108 struct hnae3_client *client_tmp;
109 struct hnae3_ae_dev *ae_dev;
112 mutex_lock(&hnae3_common_lock);
113 /* one system should only have one client for every type */
114 list_for_each_entry(client_tmp, &hnae3_client_list, node) {
115 if (client_tmp->type == client->type)
119 list_add_tail(&client->node, &hnae3_client_list);
121 /* initialize the client on every matched port */
122 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
123 /* if the client could not be initialized on current port, for
124 * any error reasons, move on to next available port
126 ret = hnae3_match_n_instantiate(client, ae_dev, true);
128 dev_err(&ae_dev->pdev->dev,
129 "match and instantiation failed for port, ret = %d\n",
134 mutex_unlock(&hnae3_common_lock);
138 EXPORT_SYMBOL(hnae3_register_client);
140 void hnae3_unregister_client(struct hnae3_client *client)
142 struct hnae3_ae_dev *ae_dev;
144 mutex_lock(&hnae3_common_lock);
145 /* un-initialize the client on every matched port */
146 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
147 hnae3_match_n_instantiate(client, ae_dev, false);
150 list_del(&client->node);
151 mutex_unlock(&hnae3_common_lock);
153 EXPORT_SYMBOL(hnae3_unregister_client);
155 /* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
156 * @ae_algo: AE algorithm
157 * NOTE: the duplicated name will not be checked
159 void hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo)
161 const struct pci_device_id *id;
162 struct hnae3_ae_dev *ae_dev;
163 struct hnae3_client *client;
166 mutex_lock(&hnae3_common_lock);
168 list_add_tail(&ae_algo->node, &hnae3_ae_algo_list);
170 /* Check if this algo/ops matches the list of ae_devs */
171 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
172 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
176 /* ae_dev init should set flag */
177 ae_dev->ops = ae_algo->ops;
178 ret = ae_algo->ops->init_ae_dev(ae_dev);
180 dev_err(&ae_dev->pdev->dev,
181 "init ae_dev error, ret = %d\n", ret);
185 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
187 /* check the client list for the match with this ae_dev type and
188 * initialize the figure out client instance
190 list_for_each_entry(client, &hnae3_client_list, node) {
191 ret = hnae3_match_n_instantiate(client, ae_dev, true);
193 dev_err(&ae_dev->pdev->dev,
194 "match and instantiation failed, ret = %d\n",
199 mutex_unlock(&hnae3_common_lock);
201 EXPORT_SYMBOL(hnae3_register_ae_algo);
203 /* hnae3_unregister_ae_algo - unregisters a AE algorithm
204 * @ae_algo: the AE algorithm to unregister
206 void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo)
208 const struct pci_device_id *id;
209 struct hnae3_ae_dev *ae_dev;
210 struct hnae3_client *client;
212 mutex_lock(&hnae3_common_lock);
213 /* Check if there are matched ae_dev */
214 list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
215 if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
218 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
222 /* check the client list for the match with this ae_dev type and
223 * un-initialize the figure out client instance
225 list_for_each_entry(client, &hnae3_client_list, node)
226 hnae3_match_n_instantiate(client, ae_dev, false);
228 ae_algo->ops->uninit_ae_dev(ae_dev);
229 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
232 list_del(&ae_algo->node);
233 mutex_unlock(&hnae3_common_lock);
235 EXPORT_SYMBOL(hnae3_unregister_ae_algo);
237 /* hnae3_register_ae_dev - registers a AE device to hnae3 framework
238 * @ae_dev: the AE device
239 * NOTE: the duplicated name will not be checked
241 void hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev)
243 const struct pci_device_id *id;
244 struct hnae3_ae_algo *ae_algo;
245 struct hnae3_client *client;
248 mutex_lock(&hnae3_common_lock);
250 list_add_tail(&ae_dev->node, &hnae3_ae_dev_list);
252 /* Check if there are matched ae_algo */
253 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
254 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
258 ae_dev->ops = ae_algo->ops;
261 dev_err(&ae_dev->pdev->dev, "ae_dev ops are null\n");
265 /* ae_dev init should set flag */
266 ret = ae_dev->ops->init_ae_dev(ae_dev);
268 dev_err(&ae_dev->pdev->dev,
269 "init ae_dev error, ret = %d\n", ret);
273 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
277 /* check the client list for the match with this ae_dev type and
278 * initialize the figure out client instance
280 list_for_each_entry(client, &hnae3_client_list, node) {
281 ret = hnae3_match_n_instantiate(client, ae_dev, true);
283 dev_err(&ae_dev->pdev->dev,
284 "match and instantiation failed, ret = %d\n",
289 mutex_unlock(&hnae3_common_lock);
291 EXPORT_SYMBOL(hnae3_register_ae_dev);
293 /* hnae3_unregister_ae_dev - unregisters a AE device
294 * @ae_dev: the AE device to unregister
296 void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev)
298 const struct pci_device_id *id;
299 struct hnae3_ae_algo *ae_algo;
300 struct hnae3_client *client;
302 mutex_lock(&hnae3_common_lock);
303 /* Check if there are matched ae_algo */
304 list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
305 if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
308 id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
312 list_for_each_entry(client, &hnae3_client_list, node)
313 hnae3_match_n_instantiate(client, ae_dev, false);
315 ae_algo->ops->uninit_ae_dev(ae_dev);
316 hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
319 list_del(&ae_dev->node);
320 mutex_unlock(&hnae3_common_lock);
322 EXPORT_SYMBOL(hnae3_unregister_ae_dev);
324 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
325 MODULE_LICENSE("GPL");
326 MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");
327 MODULE_VERSION(HNAE3_MOD_VERSION);