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