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