xref: /linux/drivers/net/ethernet/huawei/hinic/hinic_main.c (revision 7bb377107c72a40ab7505341f8626c8eb79a0cb7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Huawei HiNIC PCI Express Linux driver
4  * Copyright(c) 2017 Huawei Technologies Co., Ltd
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/pci.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <linux/etherdevice.h>
15 #include <linux/netdevice.h>
16 #include <linux/slab.h>
17 #include <linux/if_vlan.h>
18 #include <linux/semaphore.h>
19 #include <linux/workqueue.h>
20 #include <net/ip.h>
21 #include <linux/bitops.h>
22 #include <linux/bitmap.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 
26 #include "hinic_hw_qp.h"
27 #include "hinic_hw_dev.h"
28 #include "hinic_port.h"
29 #include "hinic_tx.h"
30 #include "hinic_rx.h"
31 #include "hinic_dev.h"
32 #include "hinic_sriov.h"
33 
34 MODULE_AUTHOR("Huawei Technologies CO., Ltd");
35 MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
36 MODULE_LICENSE("GPL");
37 
38 static unsigned int tx_weight = 64;
39 module_param(tx_weight, uint, 0644);
40 MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
41 
42 static unsigned int rx_weight = 64;
43 module_param(rx_weight, uint, 0644);
44 MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
45 
46 #define HINIC_DEV_ID_QUAD_PORT_25GE         0x1822
47 #define HINIC_DEV_ID_DUAL_PORT_100GE        0x0200
48 #define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ   0x0205
49 #define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ    0x0210
50 #define HINIC_DEV_ID_VF    0x375e
51 
52 #define HINIC_WQ_NAME                   "hinic_dev"
53 
54 #define MSG_ENABLE_DEFAULT              (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
55 					 NETIF_MSG_IFUP |                  \
56 					 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
57 
58 #define HINIC_LRO_MAX_WQE_NUM_DEFAULT	8
59 
60 #define HINIC_LRO_RX_TIMER_DEFAULT	16
61 
62 #define VLAN_BITMAP_SIZE(nic_dev)       (ALIGN(VLAN_N_VID, 8) / 8)
63 
64 #define work_to_rx_mode_work(work)      \
65 		container_of(work, struct hinic_rx_mode_work, work)
66 
67 #define rx_mode_work_to_nic_dev(rx_mode_work) \
68 		container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
69 
70 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT	15000
71 
72 static int change_mac_addr(struct net_device *netdev, const u8 *addr);
73 
74 static int set_features(struct hinic_dev *nic_dev,
75 			netdev_features_t pre_features,
76 			netdev_features_t features, bool force_change);
77 
78 static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
79 {
80 	struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
81 	struct hinic_rxq_stats rx_stats;
82 
83 	u64_stats_init(&rx_stats.syncp);
84 
85 	hinic_rxq_get_stats(rxq, &rx_stats);
86 
87 	u64_stats_update_begin(&nic_rx_stats->syncp);
88 	nic_rx_stats->bytes += rx_stats.bytes;
89 	nic_rx_stats->pkts  += rx_stats.pkts;
90 	nic_rx_stats->errors += rx_stats.errors;
91 	nic_rx_stats->csum_errors += rx_stats.csum_errors;
92 	nic_rx_stats->other_errors += rx_stats.other_errors;
93 	u64_stats_update_end(&nic_rx_stats->syncp);
94 
95 	hinic_rxq_clean_stats(rxq);
96 }
97 
98 static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
99 {
100 	struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
101 	struct hinic_txq_stats tx_stats;
102 
103 	u64_stats_init(&tx_stats.syncp);
104 
105 	hinic_txq_get_stats(txq, &tx_stats);
106 
107 	u64_stats_update_begin(&nic_tx_stats->syncp);
108 	nic_tx_stats->bytes += tx_stats.bytes;
109 	nic_tx_stats->pkts += tx_stats.pkts;
110 	nic_tx_stats->tx_busy += tx_stats.tx_busy;
111 	nic_tx_stats->tx_wake += tx_stats.tx_wake;
112 	nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
113 	nic_tx_stats->big_frags_pkts += tx_stats.big_frags_pkts;
114 	u64_stats_update_end(&nic_tx_stats->syncp);
115 
116 	hinic_txq_clean_stats(txq);
117 }
118 
119 static void update_nic_stats(struct hinic_dev *nic_dev)
120 {
121 	int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
122 
123 	for (i = 0; i < num_qps; i++)
124 		update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
125 
126 	for (i = 0; i < num_qps; i++)
127 		update_tx_stats(nic_dev, &nic_dev->txqs[i]);
128 }
129 
130 /**
131  * create_txqs - Create the Logical Tx Queues of specific NIC device
132  * @nic_dev: the specific NIC device
133  *
134  * Return 0 - Success, negative - Failure
135  **/
136 static int create_txqs(struct hinic_dev *nic_dev)
137 {
138 	int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
139 	struct net_device *netdev = nic_dev->netdev;
140 	size_t txq_size;
141 
142 	if (nic_dev->txqs)
143 		return -EINVAL;
144 
145 	txq_size = num_txqs * sizeof(*nic_dev->txqs);
146 	nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
147 	if (!nic_dev->txqs)
148 		return -ENOMEM;
149 
150 	for (i = 0; i < num_txqs; i++) {
151 		struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
152 
153 		err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
154 		if (err) {
155 			netif_err(nic_dev, drv, netdev,
156 				  "Failed to init Txq\n");
157 			goto err_init_txq;
158 		}
159 	}
160 
161 	return 0;
162 
163 err_init_txq:
164 	for (j = 0; j < i; j++)
165 		hinic_clean_txq(&nic_dev->txqs[j]);
166 
167 	devm_kfree(&netdev->dev, nic_dev->txqs);
168 	return err;
169 }
170 
171 /**
172  * free_txqs - Free the Logical Tx Queues of specific NIC device
173  * @nic_dev: the specific NIC device
174  **/
175 static void free_txqs(struct hinic_dev *nic_dev)
176 {
177 	int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
178 	struct net_device *netdev = nic_dev->netdev;
179 
180 	if (!nic_dev->txqs)
181 		return;
182 
183 	for (i = 0; i < num_txqs; i++)
184 		hinic_clean_txq(&nic_dev->txqs[i]);
185 
186 	devm_kfree(&netdev->dev, nic_dev->txqs);
187 	nic_dev->txqs = NULL;
188 }
189 
190 /**
191  * create_txqs - Create the Logical Rx Queues of specific NIC device
192  * @nic_dev: the specific NIC device
193  *
194  * Return 0 - Success, negative - Failure
195  **/
196 static int create_rxqs(struct hinic_dev *nic_dev)
197 {
198 	int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
199 	struct net_device *netdev = nic_dev->netdev;
200 	size_t rxq_size;
201 
202 	if (nic_dev->rxqs)
203 		return -EINVAL;
204 
205 	rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
206 	nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
207 	if (!nic_dev->rxqs)
208 		return -ENOMEM;
209 
210 	for (i = 0; i < num_rxqs; i++) {
211 		struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
212 
213 		err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
214 		if (err) {
215 			netif_err(nic_dev, drv, netdev,
216 				  "Failed to init rxq\n");
217 			goto err_init_rxq;
218 		}
219 	}
220 
221 	return 0;
222 
223 err_init_rxq:
224 	for (j = 0; j < i; j++)
225 		hinic_clean_rxq(&nic_dev->rxqs[j]);
226 
227 	devm_kfree(&netdev->dev, nic_dev->rxqs);
228 	return err;
229 }
230 
231 /**
232  * free_txqs - Free the Logical Rx Queues of specific NIC device
233  * @nic_dev: the specific NIC device
234  **/
235 static void free_rxqs(struct hinic_dev *nic_dev)
236 {
237 	int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
238 	struct net_device *netdev = nic_dev->netdev;
239 
240 	if (!nic_dev->rxqs)
241 		return;
242 
243 	for (i = 0; i < num_rxqs; i++)
244 		hinic_clean_rxq(&nic_dev->rxqs[i]);
245 
246 	devm_kfree(&netdev->dev, nic_dev->rxqs);
247 	nic_dev->rxqs = NULL;
248 }
249 
250 static int hinic_configure_max_qnum(struct hinic_dev *nic_dev)
251 {
252 	int err;
253 
254 	err = hinic_set_max_qnum(nic_dev, nic_dev->hwdev->nic_cap.max_qps);
255 	if (err)
256 		return err;
257 
258 	return 0;
259 }
260 
261 static int hinic_rss_init(struct hinic_dev *nic_dev)
262 {
263 	u8 default_rss_key[HINIC_RSS_KEY_SIZE];
264 	u8 tmpl_idx = nic_dev->rss_tmpl_idx;
265 	u32 *indir_tbl;
266 	int err, i;
267 
268 	indir_tbl = kcalloc(HINIC_RSS_INDIR_SIZE, sizeof(u32), GFP_KERNEL);
269 	if (!indir_tbl)
270 		return -ENOMEM;
271 
272 	netdev_rss_key_fill(default_rss_key, sizeof(default_rss_key));
273 	for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++)
274 		indir_tbl[i] = ethtool_rxfh_indir_default(i, nic_dev->num_rss);
275 
276 	err = hinic_rss_set_template_tbl(nic_dev, tmpl_idx, default_rss_key);
277 	if (err)
278 		goto out;
279 
280 	err = hinic_rss_set_indir_tbl(nic_dev, tmpl_idx, indir_tbl);
281 	if (err)
282 		goto out;
283 
284 	err = hinic_set_rss_type(nic_dev, tmpl_idx, nic_dev->rss_type);
285 	if (err)
286 		goto out;
287 
288 	err = hinic_rss_set_hash_engine(nic_dev, tmpl_idx,
289 					nic_dev->rss_hash_engine);
290 	if (err)
291 		goto out;
292 
293 	err = hinic_rss_cfg(nic_dev, 1, tmpl_idx);
294 	if (err)
295 		goto out;
296 
297 out:
298 	kfree(indir_tbl);
299 	return err;
300 }
301 
302 static void hinic_rss_deinit(struct hinic_dev *nic_dev)
303 {
304 	hinic_rss_cfg(nic_dev, 0, nic_dev->rss_tmpl_idx);
305 }
306 
307 static void hinic_init_rss_parameters(struct hinic_dev *nic_dev)
308 {
309 	nic_dev->rss_hash_engine = HINIC_RSS_HASH_ENGINE_TYPE_XOR;
310 	nic_dev->rss_type.tcp_ipv6_ext = 1;
311 	nic_dev->rss_type.ipv6_ext = 1;
312 	nic_dev->rss_type.tcp_ipv6 = 1;
313 	nic_dev->rss_type.ipv6 = 1;
314 	nic_dev->rss_type.tcp_ipv4 = 1;
315 	nic_dev->rss_type.ipv4 = 1;
316 	nic_dev->rss_type.udp_ipv6 = 1;
317 	nic_dev->rss_type.udp_ipv4 = 1;
318 }
319 
320 static void hinic_enable_rss(struct hinic_dev *nic_dev)
321 {
322 	struct net_device *netdev = nic_dev->netdev;
323 	struct hinic_hwdev *hwdev = nic_dev->hwdev;
324 	struct hinic_hwif *hwif = hwdev->hwif;
325 	struct pci_dev *pdev = hwif->pdev;
326 	int i, node, err = 0;
327 	u16 num_cpus = 0;
328 
329 	nic_dev->max_qps = hinic_hwdev_max_num_qps(hwdev);
330 	if (nic_dev->max_qps <= 1) {
331 		nic_dev->flags &= ~HINIC_RSS_ENABLE;
332 		nic_dev->rss_limit = nic_dev->max_qps;
333 		nic_dev->num_qps = nic_dev->max_qps;
334 		nic_dev->num_rss = nic_dev->max_qps;
335 
336 		return;
337 	}
338 
339 	err = hinic_rss_template_alloc(nic_dev, &nic_dev->rss_tmpl_idx);
340 	if (err) {
341 		netif_err(nic_dev, drv, netdev,
342 			  "Failed to alloc tmpl_idx for rss, can't enable rss for this function\n");
343 		nic_dev->flags &= ~HINIC_RSS_ENABLE;
344 		nic_dev->max_qps = 1;
345 		nic_dev->rss_limit = nic_dev->max_qps;
346 		nic_dev->num_qps = nic_dev->max_qps;
347 		nic_dev->num_rss = nic_dev->max_qps;
348 
349 		return;
350 	}
351 
352 	nic_dev->flags |= HINIC_RSS_ENABLE;
353 
354 	for (i = 0; i < num_online_cpus(); i++) {
355 		node = cpu_to_node(i);
356 		if (node == dev_to_node(&pdev->dev))
357 			num_cpus++;
358 	}
359 
360 	if (!num_cpus)
361 		num_cpus = num_online_cpus();
362 
363 	nic_dev->num_qps = hinic_hwdev_num_qps(hwdev);
364 	nic_dev->num_qps = min_t(u16, nic_dev->num_qps, num_cpus);
365 
366 	nic_dev->rss_limit = nic_dev->num_qps;
367 	nic_dev->num_rss = nic_dev->num_qps;
368 
369 	hinic_init_rss_parameters(nic_dev);
370 	err = hinic_rss_init(nic_dev);
371 	if (err)
372 		netif_err(nic_dev, drv, netdev, "Failed to init rss\n");
373 }
374 
375 static int hinic_open(struct net_device *netdev)
376 {
377 	struct hinic_dev *nic_dev = netdev_priv(netdev);
378 	enum hinic_port_link_state link_state;
379 	int err, ret;
380 
381 	if (!(nic_dev->flags & HINIC_INTF_UP)) {
382 		err = hinic_hwdev_ifup(nic_dev->hwdev);
383 		if (err) {
384 			netif_err(nic_dev, drv, netdev,
385 				  "Failed - HW interface up\n");
386 			return err;
387 		}
388 	}
389 
390 	err = create_txqs(nic_dev);
391 	if (err) {
392 		netif_err(nic_dev, drv, netdev,
393 			  "Failed to create Tx queues\n");
394 		goto err_create_txqs;
395 	}
396 
397 	err = create_rxqs(nic_dev);
398 	if (err) {
399 		netif_err(nic_dev, drv, netdev,
400 			  "Failed to create Rx queues\n");
401 		goto err_create_rxqs;
402 	}
403 
404 	hinic_enable_rss(nic_dev);
405 
406 	err = hinic_configure_max_qnum(nic_dev);
407 	if (err) {
408 		netif_err(nic_dev, drv, nic_dev->netdev,
409 			  "Failed to configure the maximum number of queues\n");
410 		goto err_port_state;
411 	}
412 
413 	netif_set_real_num_tx_queues(netdev, nic_dev->num_qps);
414 	netif_set_real_num_rx_queues(netdev, nic_dev->num_qps);
415 
416 	err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
417 	if (err) {
418 		netif_err(nic_dev, drv, netdev,
419 			  "Failed to set port state\n");
420 		goto err_port_state;
421 	}
422 
423 	err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
424 	if (err) {
425 		netif_err(nic_dev, drv, netdev,
426 			  "Failed to set func port state\n");
427 		goto err_func_port_state;
428 	}
429 
430 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
431 		/* Wait up to 3 sec between port enable to link state */
432 		msleep(3000);
433 
434 	down(&nic_dev->mgmt_lock);
435 
436 	err = hinic_port_link_state(nic_dev, &link_state);
437 	if (err) {
438 		netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
439 		goto err_port_link;
440 	}
441 
442 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
443 		hinic_notify_all_vfs_link_changed(nic_dev->hwdev, link_state);
444 
445 	if (link_state == HINIC_LINK_STATE_UP)
446 		nic_dev->flags |= HINIC_LINK_UP;
447 
448 	nic_dev->flags |= HINIC_INTF_UP;
449 
450 	if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
451 	    (HINIC_LINK_UP | HINIC_INTF_UP)) {
452 		netif_info(nic_dev, drv, netdev, "link + intf UP\n");
453 		netif_carrier_on(netdev);
454 		netif_tx_wake_all_queues(netdev);
455 	}
456 
457 	up(&nic_dev->mgmt_lock);
458 
459 	netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
460 	return 0;
461 
462 err_port_link:
463 	up(&nic_dev->mgmt_lock);
464 	ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
465 	if (ret)
466 		netif_warn(nic_dev, drv, netdev,
467 			   "Failed to revert func port state\n");
468 
469 err_func_port_state:
470 	ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
471 	if (ret)
472 		netif_warn(nic_dev, drv, netdev,
473 			   "Failed to revert port state\n");
474 err_port_state:
475 	free_rxqs(nic_dev);
476 	if (nic_dev->flags & HINIC_RSS_ENABLE) {
477 		hinic_rss_deinit(nic_dev);
478 		hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
479 	}
480 
481 err_create_rxqs:
482 	free_txqs(nic_dev);
483 
484 err_create_txqs:
485 	if (!(nic_dev->flags & HINIC_INTF_UP))
486 		hinic_hwdev_ifdown(nic_dev->hwdev);
487 	return err;
488 }
489 
490 static int hinic_close(struct net_device *netdev)
491 {
492 	struct hinic_dev *nic_dev = netdev_priv(netdev);
493 	unsigned int flags;
494 	int err;
495 
496 	down(&nic_dev->mgmt_lock);
497 
498 	flags = nic_dev->flags;
499 	nic_dev->flags &= ~HINIC_INTF_UP;
500 
501 	netif_carrier_off(netdev);
502 	netif_tx_disable(netdev);
503 
504 	update_nic_stats(nic_dev);
505 
506 	up(&nic_dev->mgmt_lock);
507 
508 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
509 		hinic_notify_all_vfs_link_changed(nic_dev->hwdev, 0);
510 
511 	err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
512 	if (err) {
513 		netif_err(nic_dev, drv, netdev,
514 			  "Failed to set func port state\n");
515 		nic_dev->flags |= (flags & HINIC_INTF_UP);
516 		return err;
517 	}
518 
519 	err = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
520 	if (err) {
521 		netif_err(nic_dev, drv, netdev, "Failed to set port state\n");
522 		nic_dev->flags |= (flags & HINIC_INTF_UP);
523 		return err;
524 	}
525 
526 	if (nic_dev->flags & HINIC_RSS_ENABLE) {
527 		hinic_rss_deinit(nic_dev);
528 		hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
529 	}
530 
531 	free_rxqs(nic_dev);
532 	free_txqs(nic_dev);
533 
534 	if (flags & HINIC_INTF_UP)
535 		hinic_hwdev_ifdown(nic_dev->hwdev);
536 
537 	netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
538 	return 0;
539 }
540 
541 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
542 {
543 	struct hinic_dev *nic_dev = netdev_priv(netdev);
544 	int err;
545 
546 	netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
547 
548 	err = hinic_port_set_mtu(nic_dev, new_mtu);
549 	if (err)
550 		netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
551 	else
552 		netdev->mtu = new_mtu;
553 
554 	return err;
555 }
556 
557 /**
558  * change_mac_addr - change the main mac address of network device
559  * @netdev: network device
560  * @addr: mac address to set
561  *
562  * Return 0 - Success, negative - Failure
563  **/
564 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
565 {
566 	struct hinic_dev *nic_dev = netdev_priv(netdev);
567 	u16 vid = 0;
568 	int err;
569 
570 	if (!is_valid_ether_addr(addr))
571 		return -EADDRNOTAVAIL;
572 
573 	netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
574 		   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
575 
576 	down(&nic_dev->mgmt_lock);
577 
578 	do {
579 		err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
580 		if (err) {
581 			netif_err(nic_dev, drv, netdev,
582 				  "Failed to delete mac\n");
583 			break;
584 		}
585 
586 		err = hinic_port_add_mac(nic_dev, addr, vid);
587 		if (err) {
588 			netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
589 			break;
590 		}
591 
592 		vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
593 	} while (vid != VLAN_N_VID);
594 
595 	up(&nic_dev->mgmt_lock);
596 	return err;
597 }
598 
599 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
600 {
601 	unsigned char new_mac[ETH_ALEN];
602 	struct sockaddr *saddr = addr;
603 	int err;
604 
605 	memcpy(new_mac, saddr->sa_data, ETH_ALEN);
606 
607 	err = change_mac_addr(netdev, new_mac);
608 	if (!err)
609 		memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
610 
611 	return err;
612 }
613 
614 /**
615  * add_mac_addr - add mac address to network device
616  * @netdev: network device
617  * @addr: mac address to add
618  *
619  * Return 0 - Success, negative - Failure
620  **/
621 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
622 {
623 	struct hinic_dev *nic_dev = netdev_priv(netdev);
624 	u16 vid = 0;
625 	int err;
626 
627 	netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
628 		   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
629 
630 	down(&nic_dev->mgmt_lock);
631 
632 	do {
633 		err = hinic_port_add_mac(nic_dev, addr, vid);
634 		if (err) {
635 			netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
636 			break;
637 		}
638 
639 		vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
640 	} while (vid != VLAN_N_VID);
641 
642 	up(&nic_dev->mgmt_lock);
643 	return err;
644 }
645 
646 /**
647  * remove_mac_addr - remove mac address from network device
648  * @netdev: network device
649  * @addr: mac address to remove
650  *
651  * Return 0 - Success, negative - Failure
652  **/
653 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
654 {
655 	struct hinic_dev *nic_dev = netdev_priv(netdev);
656 	u16 vid = 0;
657 	int err;
658 
659 	if (!is_valid_ether_addr(addr))
660 		return -EADDRNOTAVAIL;
661 
662 	netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
663 		   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
664 
665 	down(&nic_dev->mgmt_lock);
666 
667 	do {
668 		err = hinic_port_del_mac(nic_dev, addr, vid);
669 		if (err) {
670 			netif_err(nic_dev, drv, netdev,
671 				  "Failed to delete mac\n");
672 			break;
673 		}
674 
675 		vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
676 	} while (vid != VLAN_N_VID);
677 
678 	up(&nic_dev->mgmt_lock);
679 	return err;
680 }
681 
682 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
683 				 __always_unused __be16 proto, u16 vid)
684 {
685 	struct hinic_dev *nic_dev = netdev_priv(netdev);
686 	int ret, err;
687 
688 	netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
689 
690 	down(&nic_dev->mgmt_lock);
691 
692 	err = hinic_port_add_vlan(nic_dev, vid);
693 	if (err) {
694 		netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
695 		goto err_vlan_add;
696 	}
697 
698 	err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
699 	if (err && err != HINIC_PF_SET_VF_ALREADY) {
700 		netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
701 		goto err_add_mac;
702 	}
703 
704 	bitmap_set(nic_dev->vlan_bitmap, vid, 1);
705 
706 	up(&nic_dev->mgmt_lock);
707 	return 0;
708 
709 err_add_mac:
710 	ret = hinic_port_del_vlan(nic_dev, vid);
711 	if (ret)
712 		netif_err(nic_dev, drv, netdev,
713 			  "Failed to revert by removing vlan\n");
714 
715 err_vlan_add:
716 	up(&nic_dev->mgmt_lock);
717 	return err;
718 }
719 
720 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
721 				  __always_unused __be16 proto, u16 vid)
722 {
723 	struct hinic_dev *nic_dev = netdev_priv(netdev);
724 	int err;
725 
726 	netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
727 
728 	down(&nic_dev->mgmt_lock);
729 
730 	err = hinic_port_del_vlan(nic_dev, vid);
731 	if (err) {
732 		netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
733 		goto err_del_vlan;
734 	}
735 
736 	bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
737 
738 	up(&nic_dev->mgmt_lock);
739 	return 0;
740 
741 err_del_vlan:
742 	up(&nic_dev->mgmt_lock);
743 	return err;
744 }
745 
746 static void set_rx_mode(struct work_struct *work)
747 {
748 	struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
749 	struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
750 
751 	hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
752 
753 	__dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
754 	__dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
755 }
756 
757 static void hinic_set_rx_mode(struct net_device *netdev)
758 {
759 	struct hinic_dev *nic_dev = netdev_priv(netdev);
760 	struct hinic_rx_mode_work *rx_mode_work;
761 	u32 rx_mode;
762 
763 	rx_mode_work = &nic_dev->rx_mode_work;
764 
765 	rx_mode = HINIC_RX_MODE_UC |
766 		  HINIC_RX_MODE_MC |
767 		  HINIC_RX_MODE_BC;
768 
769 	if (netdev->flags & IFF_PROMISC)
770 		rx_mode |= HINIC_RX_MODE_PROMISC;
771 	else if (netdev->flags & IFF_ALLMULTI)
772 		rx_mode |= HINIC_RX_MODE_MC_ALL;
773 
774 	rx_mode_work->rx_mode = rx_mode;
775 
776 	queue_work(nic_dev->workq, &rx_mode_work->work);
777 }
778 
779 static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
780 {
781 	struct hinic_dev *nic_dev = netdev_priv(netdev);
782 	u16 sw_pi, hw_ci, sw_ci;
783 	struct hinic_sq *sq;
784 	u16 num_sqs, q_id;
785 
786 	num_sqs = hinic_hwdev_num_qps(nic_dev->hwdev);
787 
788 	netif_err(nic_dev, drv, netdev, "Tx timeout\n");
789 
790 	for (q_id = 0; q_id < num_sqs; q_id++) {
791 		if (!netif_xmit_stopped(netdev_get_tx_queue(netdev, q_id)))
792 			continue;
793 
794 		sq = hinic_hwdev_get_sq(nic_dev->hwdev, q_id);
795 		sw_pi = atomic_read(&sq->wq->prod_idx) & sq->wq->mask;
796 		hw_ci = be16_to_cpu(*(u16 *)(sq->hw_ci_addr)) & sq->wq->mask;
797 		sw_ci = atomic_read(&sq->wq->cons_idx) & sq->wq->mask;
798 		netif_err(nic_dev, drv, netdev, "Txq%d: sw_pi: %d, hw_ci: %d, sw_ci: %d, napi->state: 0x%lx\n",
799 			  q_id, sw_pi, hw_ci, sw_ci,
800 			  nic_dev->txqs[q_id].napi.state);
801 	}
802 }
803 
804 static void hinic_get_stats64(struct net_device *netdev,
805 			      struct rtnl_link_stats64 *stats)
806 {
807 	struct hinic_dev *nic_dev = netdev_priv(netdev);
808 	struct hinic_rxq_stats *nic_rx_stats;
809 	struct hinic_txq_stats *nic_tx_stats;
810 
811 	nic_rx_stats = &nic_dev->rx_stats;
812 	nic_tx_stats = &nic_dev->tx_stats;
813 
814 	down(&nic_dev->mgmt_lock);
815 
816 	if (nic_dev->flags & HINIC_INTF_UP)
817 		update_nic_stats(nic_dev);
818 
819 	up(&nic_dev->mgmt_lock);
820 
821 	stats->rx_bytes   = nic_rx_stats->bytes;
822 	stats->rx_packets = nic_rx_stats->pkts;
823 	stats->rx_errors  = nic_rx_stats->errors;
824 
825 	stats->tx_bytes   = nic_tx_stats->bytes;
826 	stats->tx_packets = nic_tx_stats->pkts;
827 	stats->tx_errors  = nic_tx_stats->tx_dropped;
828 }
829 
830 static int hinic_set_features(struct net_device *netdev,
831 			      netdev_features_t features)
832 {
833 	struct hinic_dev *nic_dev = netdev_priv(netdev);
834 
835 	return set_features(nic_dev, nic_dev->netdev->features,
836 			    features, false);
837 }
838 
839 static netdev_features_t hinic_fix_features(struct net_device *netdev,
840 					    netdev_features_t features)
841 {
842 	struct hinic_dev *nic_dev = netdev_priv(netdev);
843 
844 	/* If Rx checksum is disabled, then LRO should also be disabled */
845 	if (!(features & NETIF_F_RXCSUM)) {
846 		netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
847 		features &= ~NETIF_F_LRO;
848 	}
849 
850 	return features;
851 }
852 
853 static const struct net_device_ops hinic_netdev_ops = {
854 	.ndo_open = hinic_open,
855 	.ndo_stop = hinic_close,
856 	.ndo_change_mtu = hinic_change_mtu,
857 	.ndo_set_mac_address = hinic_set_mac_addr,
858 	.ndo_validate_addr = eth_validate_addr,
859 	.ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
860 	.ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
861 	.ndo_set_rx_mode = hinic_set_rx_mode,
862 	.ndo_start_xmit = hinic_xmit_frame,
863 	.ndo_tx_timeout = hinic_tx_timeout,
864 	.ndo_get_stats64 = hinic_get_stats64,
865 	.ndo_fix_features = hinic_fix_features,
866 	.ndo_set_features = hinic_set_features,
867 	.ndo_set_vf_mac	= hinic_ndo_set_vf_mac,
868 	.ndo_set_vf_vlan = hinic_ndo_set_vf_vlan,
869 	.ndo_get_vf_config = hinic_ndo_get_vf_config,
870 	.ndo_set_vf_trust = hinic_ndo_set_vf_trust,
871 };
872 
873 static const struct net_device_ops hinicvf_netdev_ops = {
874 	.ndo_open = hinic_open,
875 	.ndo_stop = hinic_close,
876 	.ndo_change_mtu = hinic_change_mtu,
877 	.ndo_set_mac_address = hinic_set_mac_addr,
878 	.ndo_validate_addr = eth_validate_addr,
879 	.ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
880 	.ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
881 	.ndo_set_rx_mode = hinic_set_rx_mode,
882 	.ndo_start_xmit = hinic_xmit_frame,
883 	.ndo_tx_timeout = hinic_tx_timeout,
884 	.ndo_get_stats64 = hinic_get_stats64,
885 	.ndo_fix_features = hinic_fix_features,
886 	.ndo_set_features = hinic_set_features,
887 };
888 
889 static void netdev_features_init(struct net_device *netdev)
890 {
891 	netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
892 			      NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
893 			      NETIF_F_RXCSUM | NETIF_F_LRO |
894 			      NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
895 
896 	netdev->vlan_features = netdev->hw_features;
897 
898 	netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
899 }
900 
901 /**
902  * link_status_event_handler - link event handler
903  * @handle: nic device for the handler
904  * @buf_in: input buffer
905  * @in_size: input size
906  * @buf_in: output buffer
907  * @out_size: returned output size
908  *
909  * Return 0 - Success, negative - Failure
910  **/
911 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
912 				      void *buf_out, u16 *out_size)
913 {
914 	struct hinic_port_link_status *link_status, *ret_link_status;
915 	struct hinic_dev *nic_dev = handle;
916 
917 	link_status = buf_in;
918 
919 	if (link_status->link == HINIC_LINK_STATE_UP) {
920 		down(&nic_dev->mgmt_lock);
921 
922 		nic_dev->flags |= HINIC_LINK_UP;
923 
924 		if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
925 		    (HINIC_LINK_UP | HINIC_INTF_UP)) {
926 			netif_carrier_on(nic_dev->netdev);
927 			netif_tx_wake_all_queues(nic_dev->netdev);
928 		}
929 
930 		up(&nic_dev->mgmt_lock);
931 
932 		netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
933 	} else {
934 		down(&nic_dev->mgmt_lock);
935 
936 		nic_dev->flags &= ~HINIC_LINK_UP;
937 
938 		netif_carrier_off(nic_dev->netdev);
939 		netif_tx_disable(nic_dev->netdev);
940 
941 		up(&nic_dev->mgmt_lock);
942 
943 		netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
944 	}
945 
946 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
947 		hinic_notify_all_vfs_link_changed(nic_dev->hwdev,
948 						  link_status->link);
949 
950 	ret_link_status = buf_out;
951 	ret_link_status->status = 0;
952 
953 	*out_size = sizeof(*ret_link_status);
954 }
955 
956 static int set_features(struct hinic_dev *nic_dev,
957 			netdev_features_t pre_features,
958 			netdev_features_t features, bool force_change)
959 {
960 	netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
961 	u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
962 	int err = 0;
963 
964 	if (changed & NETIF_F_TSO)
965 		err = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
966 					 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
967 
968 	if (changed & NETIF_F_RXCSUM)
969 		err = hinic_set_rx_csum_offload(nic_dev, csum_en);
970 
971 	if (changed & NETIF_F_LRO) {
972 		err = hinic_set_rx_lro_state(nic_dev,
973 					     !!(features & NETIF_F_LRO),
974 					     HINIC_LRO_RX_TIMER_DEFAULT,
975 					     HINIC_LRO_MAX_WQE_NUM_DEFAULT);
976 	}
977 
978 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
979 		err = hinic_set_rx_vlan_offload(nic_dev,
980 						!!(features &
981 						   NETIF_F_HW_VLAN_CTAG_RX));
982 
983 	return err;
984 }
985 
986 /**
987  * nic_dev_init - Initialize the NIC device
988  * @pdev: the NIC pci device
989  *
990  * Return 0 - Success, negative - Failure
991  **/
992 static int nic_dev_init(struct pci_dev *pdev)
993 {
994 	struct hinic_rx_mode_work *rx_mode_work;
995 	struct hinic_txq_stats *tx_stats;
996 	struct hinic_rxq_stats *rx_stats;
997 	struct hinic_dev *nic_dev;
998 	struct net_device *netdev;
999 	struct hinic_hwdev *hwdev;
1000 	int err, num_qps;
1001 
1002 	hwdev = hinic_init_hwdev(pdev);
1003 	if (IS_ERR(hwdev)) {
1004 		dev_err(&pdev->dev, "Failed to initialize HW device\n");
1005 		return PTR_ERR(hwdev);
1006 	}
1007 
1008 	num_qps = hinic_hwdev_num_qps(hwdev);
1009 	if (num_qps <= 0) {
1010 		dev_err(&pdev->dev, "Invalid number of QPS\n");
1011 		err = -EINVAL;
1012 		goto err_num_qps;
1013 	}
1014 
1015 	netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
1016 	if (!netdev) {
1017 		dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
1018 		err = -ENOMEM;
1019 		goto err_alloc_etherdev;
1020 	}
1021 
1022 	hinic_set_ethtool_ops(netdev);
1023 
1024 	if (!HINIC_IS_VF(hwdev->hwif))
1025 		netdev->netdev_ops = &hinic_netdev_ops;
1026 	else
1027 		netdev->netdev_ops = &hinicvf_netdev_ops;
1028 
1029 	netdev->max_mtu = ETH_MAX_MTU;
1030 
1031 	nic_dev = netdev_priv(netdev);
1032 	nic_dev->netdev = netdev;
1033 	nic_dev->hwdev  = hwdev;
1034 	nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
1035 	nic_dev->flags = 0;
1036 	nic_dev->txqs = NULL;
1037 	nic_dev->rxqs = NULL;
1038 	nic_dev->tx_weight = tx_weight;
1039 	nic_dev->rx_weight = rx_weight;
1040 	nic_dev->sriov_info.hwdev = hwdev;
1041 	nic_dev->sriov_info.pdev = pdev;
1042 
1043 	sema_init(&nic_dev->mgmt_lock, 1);
1044 
1045 	tx_stats = &nic_dev->tx_stats;
1046 	rx_stats = &nic_dev->rx_stats;
1047 
1048 	u64_stats_init(&tx_stats->syncp);
1049 	u64_stats_init(&rx_stats->syncp);
1050 
1051 	nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
1052 					    VLAN_BITMAP_SIZE(nic_dev),
1053 					    GFP_KERNEL);
1054 	if (!nic_dev->vlan_bitmap) {
1055 		err = -ENOMEM;
1056 		goto err_vlan_bitmap;
1057 	}
1058 
1059 	nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
1060 	if (!nic_dev->workq) {
1061 		err = -ENOMEM;
1062 		goto err_workq;
1063 	}
1064 
1065 	pci_set_drvdata(pdev, netdev);
1066 
1067 	err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
1068 	if (err) {
1069 		dev_err(&pdev->dev, "Failed to get mac address\n");
1070 		goto err_get_mac;
1071 	}
1072 
1073 	if (!is_valid_ether_addr(netdev->dev_addr)) {
1074 		if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1075 			dev_err(&pdev->dev, "Invalid MAC address\n");
1076 			err = -EIO;
1077 			goto err_add_mac;
1078 		}
1079 
1080 		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1081 			 netdev->dev_addr);
1082 		eth_hw_addr_random(netdev);
1083 	}
1084 
1085 	err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1086 	if (err && err != HINIC_PF_SET_VF_ALREADY) {
1087 		dev_err(&pdev->dev, "Failed to add mac\n");
1088 		goto err_add_mac;
1089 	}
1090 
1091 	err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1092 	if (err) {
1093 		dev_err(&pdev->dev, "Failed to set mtu\n");
1094 		goto err_set_mtu;
1095 	}
1096 
1097 	rx_mode_work = &nic_dev->rx_mode_work;
1098 	INIT_WORK(&rx_mode_work->work, set_rx_mode);
1099 
1100 	netdev_features_init(netdev);
1101 
1102 	netif_carrier_off(netdev);
1103 
1104 	hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1105 				nic_dev, link_status_event_handler);
1106 
1107 	err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1108 	if (err)
1109 		goto err_set_features;
1110 
1111 	SET_NETDEV_DEV(netdev, &pdev->dev);
1112 
1113 	err = register_netdev(netdev);
1114 	if (err) {
1115 		dev_err(&pdev->dev, "Failed to register netdev\n");
1116 		goto err_reg_netdev;
1117 	}
1118 
1119 	return 0;
1120 
1121 err_reg_netdev:
1122 err_set_features:
1123 	hinic_hwdev_cb_unregister(nic_dev->hwdev,
1124 				  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1125 	cancel_work_sync(&rx_mode_work->work);
1126 
1127 err_set_mtu:
1128 err_get_mac:
1129 err_add_mac:
1130 	pci_set_drvdata(pdev, NULL);
1131 	destroy_workqueue(nic_dev->workq);
1132 
1133 err_workq:
1134 err_vlan_bitmap:
1135 	free_netdev(netdev);
1136 
1137 err_alloc_etherdev:
1138 err_num_qps:
1139 	hinic_free_hwdev(hwdev);
1140 	return err;
1141 }
1142 
1143 static int hinic_probe(struct pci_dev *pdev,
1144 		       const struct pci_device_id *id)
1145 {
1146 	int err = pci_enable_device(pdev);
1147 
1148 	if (err) {
1149 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
1150 		return err;
1151 	}
1152 
1153 	err = pci_request_regions(pdev, HINIC_DRV_NAME);
1154 	if (err) {
1155 		dev_err(&pdev->dev, "Failed to request PCI regions\n");
1156 		goto err_pci_regions;
1157 	}
1158 
1159 	pci_set_master(pdev);
1160 
1161 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1162 	if (err) {
1163 		dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1164 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1165 		if (err) {
1166 			dev_err(&pdev->dev, "Failed to set DMA mask\n");
1167 			goto err_dma_mask;
1168 		}
1169 	}
1170 
1171 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1172 	if (err) {
1173 		dev_warn(&pdev->dev,
1174 			 "Couldn't set 64-bit consistent DMA mask\n");
1175 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1176 		if (err) {
1177 			dev_err(&pdev->dev,
1178 				"Failed to set consistent DMA mask\n");
1179 			goto err_dma_consistent_mask;
1180 		}
1181 	}
1182 
1183 	err = nic_dev_init(pdev);
1184 	if (err) {
1185 		dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1186 		goto err_nic_dev_init;
1187 	}
1188 
1189 	dev_info(&pdev->dev, "HiNIC driver - probed\n");
1190 	return 0;
1191 
1192 err_nic_dev_init:
1193 err_dma_consistent_mask:
1194 err_dma_mask:
1195 	pci_release_regions(pdev);
1196 
1197 err_pci_regions:
1198 	pci_disable_device(pdev);
1199 	return err;
1200 }
1201 
1202 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT	15000
1203 
1204 static void wait_sriov_cfg_complete(struct hinic_dev *nic_dev)
1205 {
1206 	struct hinic_sriov_info *sriov_info = &nic_dev->sriov_info;
1207 	u32 loop_cnt = 0;
1208 
1209 	set_bit(HINIC_FUNC_REMOVE, &sriov_info->state);
1210 	usleep_range(9900, 10000);
1211 
1212 	while (loop_cnt < HINIC_WAIT_SRIOV_CFG_TIMEOUT) {
1213 		if (!test_bit(HINIC_SRIOV_ENABLE, &sriov_info->state) &&
1214 		    !test_bit(HINIC_SRIOV_DISABLE, &sriov_info->state))
1215 			return;
1216 
1217 		usleep_range(9900, 10000);
1218 		loop_cnt++;
1219 	}
1220 }
1221 
1222 static void hinic_remove(struct pci_dev *pdev)
1223 {
1224 	struct net_device *netdev = pci_get_drvdata(pdev);
1225 	struct hinic_dev *nic_dev = netdev_priv(netdev);
1226 	struct hinic_rx_mode_work *rx_mode_work;
1227 
1228 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1229 		wait_sriov_cfg_complete(nic_dev);
1230 		hinic_pci_sriov_disable(pdev);
1231 	}
1232 
1233 	unregister_netdev(netdev);
1234 
1235 	hinic_hwdev_cb_unregister(nic_dev->hwdev,
1236 				  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1237 
1238 	rx_mode_work = &nic_dev->rx_mode_work;
1239 	cancel_work_sync(&rx_mode_work->work);
1240 
1241 	pci_set_drvdata(pdev, NULL);
1242 
1243 	destroy_workqueue(nic_dev->workq);
1244 
1245 	hinic_vf_func_free(nic_dev->hwdev);
1246 
1247 	hinic_free_hwdev(nic_dev->hwdev);
1248 
1249 	free_netdev(netdev);
1250 
1251 	pci_release_regions(pdev);
1252 	pci_disable_device(pdev);
1253 
1254 	dev_info(&pdev->dev, "HiNIC driver - removed\n");
1255 }
1256 
1257 static void hinic_shutdown(struct pci_dev *pdev)
1258 {
1259 	pci_disable_device(pdev);
1260 }
1261 
1262 static const struct pci_device_id hinic_pci_table[] = {
1263 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1264 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1265 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1266 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1267 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_VF), 0},
1268 	{ 0, 0}
1269 };
1270 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1271 
1272 static struct pci_driver hinic_driver = {
1273 	.name           = HINIC_DRV_NAME,
1274 	.id_table       = hinic_pci_table,
1275 	.probe          = hinic_probe,
1276 	.remove         = hinic_remove,
1277 	.shutdown       = hinic_shutdown,
1278 	.sriov_configure = hinic_pci_sriov_configure,
1279 };
1280 
1281 module_pci_driver(hinic_driver);
1282