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 
450 	nic_dev->flags |= HINIC_INTF_UP;
451 
452 	if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
453 	    (HINIC_LINK_UP | HINIC_INTF_UP)) {
454 		netif_info(nic_dev, drv, netdev, "link + intf UP\n");
455 		netif_carrier_on(netdev);
456 		netif_tx_wake_all_queues(netdev);
457 	}
458 
459 	up(&nic_dev->mgmt_lock);
460 
461 	netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
462 	return 0;
463 
464 err_port_link:
465 	up(&nic_dev->mgmt_lock);
466 	ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
467 	if (ret)
468 		netif_warn(nic_dev, drv, netdev,
469 			   "Failed to revert func port state\n");
470 
471 err_func_port_state:
472 	ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
473 	if (ret)
474 		netif_warn(nic_dev, drv, netdev,
475 			   "Failed to revert port state\n");
476 err_port_state:
477 	free_rxqs(nic_dev);
478 	if (nic_dev->flags & HINIC_RSS_ENABLE) {
479 		hinic_rss_deinit(nic_dev);
480 		hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
481 	}
482 
483 err_create_rxqs:
484 	free_txqs(nic_dev);
485 
486 err_create_txqs:
487 	if (!(nic_dev->flags & HINIC_INTF_UP))
488 		hinic_hwdev_ifdown(nic_dev->hwdev);
489 	return err;
490 }
491 
492 int hinic_close(struct net_device *netdev)
493 {
494 	struct hinic_dev *nic_dev = netdev_priv(netdev);
495 	unsigned int flags;
496 
497 	down(&nic_dev->mgmt_lock);
498 
499 	flags = nic_dev->flags;
500 	nic_dev->flags &= ~HINIC_INTF_UP;
501 
502 	netif_carrier_off(netdev);
503 	netif_tx_disable(netdev);
504 
505 	update_nic_stats(nic_dev);
506 
507 	up(&nic_dev->mgmt_lock);
508 
509 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
510 		hinic_notify_all_vfs_link_changed(nic_dev->hwdev, 0);
511 
512 	hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
513 
514 	hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
515 
516 	if (nic_dev->flags & HINIC_RSS_ENABLE) {
517 		hinic_rss_deinit(nic_dev);
518 		hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
519 	}
520 
521 	free_rxqs(nic_dev);
522 	free_txqs(nic_dev);
523 
524 	if (flags & HINIC_INTF_UP)
525 		hinic_hwdev_ifdown(nic_dev->hwdev);
526 
527 	netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
528 	return 0;
529 }
530 
531 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
532 {
533 	struct hinic_dev *nic_dev = netdev_priv(netdev);
534 	int err;
535 
536 	netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
537 
538 	err = hinic_port_set_mtu(nic_dev, new_mtu);
539 	if (err)
540 		netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
541 	else
542 		netdev->mtu = new_mtu;
543 
544 	return err;
545 }
546 
547 /**
548  * change_mac_addr - change the main mac address of network device
549  * @netdev: network device
550  * @addr: mac address to set
551  *
552  * Return 0 - Success, negative - Failure
553  **/
554 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
555 {
556 	struct hinic_dev *nic_dev = netdev_priv(netdev);
557 	u16 vid = 0;
558 	int err;
559 
560 	if (!is_valid_ether_addr(addr))
561 		return -EADDRNOTAVAIL;
562 
563 	netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
564 		   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
565 
566 	down(&nic_dev->mgmt_lock);
567 
568 	do {
569 		err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
570 		if (err) {
571 			netif_err(nic_dev, drv, netdev,
572 				  "Failed to delete mac\n");
573 			break;
574 		}
575 
576 		err = hinic_port_add_mac(nic_dev, addr, vid);
577 		if (err) {
578 			netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
579 			break;
580 		}
581 
582 		vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
583 	} while (vid != VLAN_N_VID);
584 
585 	up(&nic_dev->mgmt_lock);
586 	return err;
587 }
588 
589 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
590 {
591 	unsigned char new_mac[ETH_ALEN];
592 	struct sockaddr *saddr = addr;
593 	int err;
594 
595 	memcpy(new_mac, saddr->sa_data, ETH_ALEN);
596 
597 	err = change_mac_addr(netdev, new_mac);
598 	if (!err)
599 		memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
600 
601 	return err;
602 }
603 
604 /**
605  * add_mac_addr - add mac address to network device
606  * @netdev: network device
607  * @addr: mac address to add
608  *
609  * Return 0 - Success, negative - Failure
610  **/
611 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
612 {
613 	struct hinic_dev *nic_dev = netdev_priv(netdev);
614 	u16 vid = 0;
615 	int err;
616 
617 	netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
618 		   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
619 
620 	down(&nic_dev->mgmt_lock);
621 
622 	do {
623 		err = hinic_port_add_mac(nic_dev, addr, vid);
624 		if (err) {
625 			netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
626 			break;
627 		}
628 
629 		vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
630 	} while (vid != VLAN_N_VID);
631 
632 	up(&nic_dev->mgmt_lock);
633 	return err;
634 }
635 
636 /**
637  * remove_mac_addr - remove mac address from network device
638  * @netdev: network device
639  * @addr: mac address to remove
640  *
641  * Return 0 - Success, negative - Failure
642  **/
643 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
644 {
645 	struct hinic_dev *nic_dev = netdev_priv(netdev);
646 	u16 vid = 0;
647 	int err;
648 
649 	if (!is_valid_ether_addr(addr))
650 		return -EADDRNOTAVAIL;
651 
652 	netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
653 		   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
654 
655 	down(&nic_dev->mgmt_lock);
656 
657 	do {
658 		err = hinic_port_del_mac(nic_dev, addr, vid);
659 		if (err) {
660 			netif_err(nic_dev, drv, netdev,
661 				  "Failed to delete mac\n");
662 			break;
663 		}
664 
665 		vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
666 	} while (vid != VLAN_N_VID);
667 
668 	up(&nic_dev->mgmt_lock);
669 	return err;
670 }
671 
672 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
673 				 __always_unused __be16 proto, u16 vid)
674 {
675 	struct hinic_dev *nic_dev = netdev_priv(netdev);
676 	int ret, err;
677 
678 	netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
679 
680 	down(&nic_dev->mgmt_lock);
681 
682 	err = hinic_port_add_vlan(nic_dev, vid);
683 	if (err) {
684 		netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
685 		goto err_vlan_add;
686 	}
687 
688 	err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
689 	if (err && err != HINIC_PF_SET_VF_ALREADY) {
690 		netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
691 		goto err_add_mac;
692 	}
693 
694 	bitmap_set(nic_dev->vlan_bitmap, vid, 1);
695 
696 	up(&nic_dev->mgmt_lock);
697 	return 0;
698 
699 err_add_mac:
700 	ret = hinic_port_del_vlan(nic_dev, vid);
701 	if (ret)
702 		netif_err(nic_dev, drv, netdev,
703 			  "Failed to revert by removing vlan\n");
704 
705 err_vlan_add:
706 	up(&nic_dev->mgmt_lock);
707 	return err;
708 }
709 
710 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
711 				  __always_unused __be16 proto, u16 vid)
712 {
713 	struct hinic_dev *nic_dev = netdev_priv(netdev);
714 	int err;
715 
716 	netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
717 
718 	down(&nic_dev->mgmt_lock);
719 
720 	err = hinic_port_del_vlan(nic_dev, vid);
721 	if (err) {
722 		netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
723 		goto err_del_vlan;
724 	}
725 
726 	bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
727 
728 	up(&nic_dev->mgmt_lock);
729 	return 0;
730 
731 err_del_vlan:
732 	up(&nic_dev->mgmt_lock);
733 	return err;
734 }
735 
736 static void set_rx_mode(struct work_struct *work)
737 {
738 	struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
739 	struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
740 
741 	hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
742 
743 	__dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
744 	__dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
745 }
746 
747 static void hinic_set_rx_mode(struct net_device *netdev)
748 {
749 	struct hinic_dev *nic_dev = netdev_priv(netdev);
750 	struct hinic_rx_mode_work *rx_mode_work;
751 	u32 rx_mode;
752 
753 	rx_mode_work = &nic_dev->rx_mode_work;
754 
755 	rx_mode = HINIC_RX_MODE_UC |
756 		  HINIC_RX_MODE_MC |
757 		  HINIC_RX_MODE_BC;
758 
759 	if (netdev->flags & IFF_PROMISC) {
760 		if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
761 			rx_mode |= HINIC_RX_MODE_PROMISC;
762 	} else if (netdev->flags & IFF_ALLMULTI) {
763 		rx_mode |= HINIC_RX_MODE_MC_ALL;
764 	}
765 
766 	rx_mode_work->rx_mode = rx_mode;
767 
768 	queue_work(nic_dev->workq, &rx_mode_work->work);
769 }
770 
771 static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
772 {
773 	struct hinic_dev *nic_dev = netdev_priv(netdev);
774 	u16 sw_pi, hw_ci, sw_ci;
775 	struct hinic_sq *sq;
776 	u16 num_sqs, q_id;
777 
778 	num_sqs = hinic_hwdev_num_qps(nic_dev->hwdev);
779 
780 	netif_err(nic_dev, drv, netdev, "Tx timeout\n");
781 
782 	for (q_id = 0; q_id < num_sqs; q_id++) {
783 		if (!netif_xmit_stopped(netdev_get_tx_queue(netdev, q_id)))
784 			continue;
785 
786 		sq = hinic_hwdev_get_sq(nic_dev->hwdev, q_id);
787 		sw_pi = atomic_read(&sq->wq->prod_idx) & sq->wq->mask;
788 		hw_ci = be16_to_cpu(*(u16 *)(sq->hw_ci_addr)) & sq->wq->mask;
789 		sw_ci = atomic_read(&sq->wq->cons_idx) & sq->wq->mask;
790 		netif_err(nic_dev, drv, netdev, "Txq%d: sw_pi: %d, hw_ci: %d, sw_ci: %d, napi->state: 0x%lx\n",
791 			  q_id, sw_pi, hw_ci, sw_ci,
792 			  nic_dev->txqs[q_id].napi.state);
793 	}
794 }
795 
796 static void hinic_get_stats64(struct net_device *netdev,
797 			      struct rtnl_link_stats64 *stats)
798 {
799 	struct hinic_dev *nic_dev = netdev_priv(netdev);
800 	struct hinic_rxq_stats *nic_rx_stats;
801 	struct hinic_txq_stats *nic_tx_stats;
802 
803 	nic_rx_stats = &nic_dev->rx_stats;
804 	nic_tx_stats = &nic_dev->tx_stats;
805 
806 	down(&nic_dev->mgmt_lock);
807 
808 	if (nic_dev->flags & HINIC_INTF_UP)
809 		update_nic_stats(nic_dev);
810 
811 	up(&nic_dev->mgmt_lock);
812 
813 	stats->rx_bytes   = nic_rx_stats->bytes;
814 	stats->rx_packets = nic_rx_stats->pkts;
815 	stats->rx_errors  = nic_rx_stats->errors;
816 
817 	stats->tx_bytes   = nic_tx_stats->bytes;
818 	stats->tx_packets = nic_tx_stats->pkts;
819 	stats->tx_errors  = nic_tx_stats->tx_dropped;
820 }
821 
822 static int hinic_set_features(struct net_device *netdev,
823 			      netdev_features_t features)
824 {
825 	struct hinic_dev *nic_dev = netdev_priv(netdev);
826 
827 	return set_features(nic_dev, nic_dev->netdev->features,
828 			    features, false);
829 }
830 
831 static netdev_features_t hinic_fix_features(struct net_device *netdev,
832 					    netdev_features_t features)
833 {
834 	struct hinic_dev *nic_dev = netdev_priv(netdev);
835 
836 	/* If Rx checksum is disabled, then LRO should also be disabled */
837 	if (!(features & NETIF_F_RXCSUM)) {
838 		netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
839 		features &= ~NETIF_F_LRO;
840 	}
841 
842 	return features;
843 }
844 
845 static const struct net_device_ops hinic_netdev_ops = {
846 	.ndo_open = hinic_open,
847 	.ndo_stop = hinic_close,
848 	.ndo_change_mtu = hinic_change_mtu,
849 	.ndo_set_mac_address = hinic_set_mac_addr,
850 	.ndo_validate_addr = eth_validate_addr,
851 	.ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
852 	.ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
853 	.ndo_set_rx_mode = hinic_set_rx_mode,
854 	.ndo_start_xmit = hinic_xmit_frame,
855 	.ndo_tx_timeout = hinic_tx_timeout,
856 	.ndo_get_stats64 = hinic_get_stats64,
857 	.ndo_fix_features = hinic_fix_features,
858 	.ndo_set_features = hinic_set_features,
859 	.ndo_set_vf_mac	= hinic_ndo_set_vf_mac,
860 	.ndo_set_vf_vlan = hinic_ndo_set_vf_vlan,
861 	.ndo_get_vf_config = hinic_ndo_get_vf_config,
862 	.ndo_set_vf_trust = hinic_ndo_set_vf_trust,
863 	.ndo_set_vf_rate = hinic_ndo_set_vf_bw,
864 	.ndo_set_vf_spoofchk = hinic_ndo_set_vf_spoofchk,
865 	.ndo_set_vf_link_state = hinic_ndo_set_vf_link_state,
866 };
867 
868 static const struct net_device_ops hinicvf_netdev_ops = {
869 	.ndo_open = hinic_open,
870 	.ndo_stop = hinic_close,
871 	.ndo_change_mtu = hinic_change_mtu,
872 	.ndo_set_mac_address = hinic_set_mac_addr,
873 	.ndo_validate_addr = eth_validate_addr,
874 	.ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
875 	.ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
876 	.ndo_set_rx_mode = hinic_set_rx_mode,
877 	.ndo_start_xmit = hinic_xmit_frame,
878 	.ndo_tx_timeout = hinic_tx_timeout,
879 	.ndo_get_stats64 = hinic_get_stats64,
880 	.ndo_fix_features = hinic_fix_features,
881 	.ndo_set_features = hinic_set_features,
882 };
883 
884 static void netdev_features_init(struct net_device *netdev)
885 {
886 	netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
887 			      NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
888 			      NETIF_F_RXCSUM | NETIF_F_LRO |
889 			      NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
890 
891 	netdev->vlan_features = netdev->hw_features;
892 
893 	netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
894 }
895 
896 static void hinic_refresh_nic_cfg(struct hinic_dev *nic_dev)
897 {
898 	struct hinic_nic_cfg *nic_cfg = &nic_dev->hwdev->func_to_io.nic_cfg;
899 	struct hinic_pause_config pause_info = {0};
900 	struct hinic_port_cap port_cap = {0};
901 
902 	if (hinic_port_get_cap(nic_dev, &port_cap))
903 		return;
904 
905 	mutex_lock(&nic_cfg->cfg_mutex);
906 	if (nic_cfg->pause_set || !port_cap.autoneg_state) {
907 		nic_cfg->auto_neg = port_cap.autoneg_state;
908 		pause_info.auto_neg = nic_cfg->auto_neg;
909 		pause_info.rx_pause = nic_cfg->rx_pause;
910 		pause_info.tx_pause = nic_cfg->tx_pause;
911 		hinic_set_hw_pause_info(nic_dev->hwdev, &pause_info);
912 	}
913 	mutex_unlock(&nic_cfg->cfg_mutex);
914 }
915 
916 /**
917  * link_status_event_handler - link event handler
918  * @handle: nic device for the handler
919  * @buf_in: input buffer
920  * @in_size: input size
921  * @buf_in: output buffer
922  * @out_size: returned output size
923  *
924  * Return 0 - Success, negative - Failure
925  **/
926 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
927 				      void *buf_out, u16 *out_size)
928 {
929 	struct hinic_port_link_status *link_status, *ret_link_status;
930 	struct hinic_dev *nic_dev = handle;
931 
932 	link_status = buf_in;
933 
934 	if (link_status->link == HINIC_LINK_STATE_UP) {
935 		down(&nic_dev->mgmt_lock);
936 
937 		nic_dev->flags |= HINIC_LINK_UP;
938 
939 		if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
940 		    (HINIC_LINK_UP | HINIC_INTF_UP)) {
941 			netif_carrier_on(nic_dev->netdev);
942 			netif_tx_wake_all_queues(nic_dev->netdev);
943 		}
944 
945 		up(&nic_dev->mgmt_lock);
946 
947 		if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
948 			hinic_refresh_nic_cfg(nic_dev);
949 
950 		netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
951 	} else {
952 		down(&nic_dev->mgmt_lock);
953 
954 		nic_dev->flags &= ~HINIC_LINK_UP;
955 
956 		netif_carrier_off(nic_dev->netdev);
957 		netif_tx_disable(nic_dev->netdev);
958 
959 		up(&nic_dev->mgmt_lock);
960 
961 		netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
962 	}
963 
964 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
965 		hinic_notify_all_vfs_link_changed(nic_dev->hwdev,
966 						  link_status->link);
967 
968 	ret_link_status = buf_out;
969 	ret_link_status->status = 0;
970 
971 	*out_size = sizeof(*ret_link_status);
972 }
973 
974 static int set_features(struct hinic_dev *nic_dev,
975 			netdev_features_t pre_features,
976 			netdev_features_t features, bool force_change)
977 {
978 	netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
979 	u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
980 	netdev_features_t failed_features = 0;
981 	int ret = 0;
982 	int err = 0;
983 
984 	if (changed & NETIF_F_TSO) {
985 		ret = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
986 					 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
987 		if (ret) {
988 			err = ret;
989 			failed_features |= NETIF_F_TSO;
990 		}
991 	}
992 
993 	if (changed & NETIF_F_RXCSUM) {
994 		ret = hinic_set_rx_csum_offload(nic_dev, csum_en);
995 		if (ret) {
996 			err = ret;
997 			failed_features |= NETIF_F_RXCSUM;
998 		}
999 	}
1000 
1001 	if (changed & NETIF_F_LRO) {
1002 		ret = hinic_set_rx_lro_state(nic_dev,
1003 					     !!(features & NETIF_F_LRO),
1004 					     HINIC_LRO_RX_TIMER_DEFAULT,
1005 					     HINIC_LRO_MAX_WQE_NUM_DEFAULT);
1006 		if (ret) {
1007 			err = ret;
1008 			failed_features |= NETIF_F_LRO;
1009 		}
1010 	}
1011 
1012 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1013 		ret = hinic_set_rx_vlan_offload(nic_dev,
1014 						!!(features &
1015 						   NETIF_F_HW_VLAN_CTAG_RX));
1016 		if (ret) {
1017 			err = ret;
1018 			failed_features |= NETIF_F_HW_VLAN_CTAG_RX;
1019 		}
1020 	}
1021 
1022 	if (err) {
1023 		nic_dev->netdev->features = features ^ failed_features;
1024 		return -EIO;
1025 	}
1026 
1027 	return 0;
1028 }
1029 
1030 static int hinic_init_intr_coalesce(struct hinic_dev *nic_dev)
1031 {
1032 	u64 size;
1033 	u16 i;
1034 
1035 	size = sizeof(struct hinic_intr_coal_info) * nic_dev->max_qps;
1036 	nic_dev->rx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1037 	if (!nic_dev->rx_intr_coalesce)
1038 		return -ENOMEM;
1039 	nic_dev->tx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1040 	if (!nic_dev->tx_intr_coalesce) {
1041 		kfree(nic_dev->rx_intr_coalesce);
1042 		return -ENOMEM;
1043 	}
1044 
1045 	for (i = 0; i < nic_dev->max_qps; i++) {
1046 		nic_dev->rx_intr_coalesce[i].pending_limt =
1047 			HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1048 		nic_dev->rx_intr_coalesce[i].coalesce_timer_cfg =
1049 			HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1050 		nic_dev->rx_intr_coalesce[i].resend_timer_cfg =
1051 			HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1052 		nic_dev->tx_intr_coalesce[i].pending_limt =
1053 			HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1054 		nic_dev->tx_intr_coalesce[i].coalesce_timer_cfg =
1055 			HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1056 		nic_dev->tx_intr_coalesce[i].resend_timer_cfg =
1057 			HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1058 	}
1059 
1060 	return 0;
1061 }
1062 
1063 static void hinic_free_intr_coalesce(struct hinic_dev *nic_dev)
1064 {
1065 	kfree(nic_dev->tx_intr_coalesce);
1066 	kfree(nic_dev->rx_intr_coalesce);
1067 }
1068 
1069 /**
1070  * nic_dev_init - Initialize the NIC device
1071  * @pdev: the NIC pci device
1072  *
1073  * Return 0 - Success, negative - Failure
1074  **/
1075 static int nic_dev_init(struct pci_dev *pdev)
1076 {
1077 	struct hinic_rx_mode_work *rx_mode_work;
1078 	struct hinic_txq_stats *tx_stats;
1079 	struct hinic_rxq_stats *rx_stats;
1080 	struct hinic_devlink_priv *priv;
1081 	struct hinic_dev *nic_dev;
1082 	struct net_device *netdev;
1083 	struct hinic_hwdev *hwdev;
1084 	struct devlink *devlink;
1085 	int err, num_qps;
1086 
1087 	hwdev = hinic_init_hwdev(pdev);
1088 	if (IS_ERR(hwdev)) {
1089 		dev_err(&pdev->dev, "Failed to initialize HW device\n");
1090 		return PTR_ERR(hwdev);
1091 	}
1092 
1093 	devlink = hinic_devlink_alloc();
1094 	if (!devlink) {
1095 		dev_err(&pdev->dev, "Hinic devlink alloc failed\n");
1096 		err = -ENOMEM;
1097 		goto err_devlink_alloc;
1098 	}
1099 
1100 	priv = devlink_priv(devlink);
1101 	priv->hwdev = hwdev;
1102 
1103 	num_qps = hinic_hwdev_num_qps(hwdev);
1104 	if (num_qps <= 0) {
1105 		dev_err(&pdev->dev, "Invalid number of QPS\n");
1106 		err = -EINVAL;
1107 		goto err_num_qps;
1108 	}
1109 
1110 	netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
1111 	if (!netdev) {
1112 		dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
1113 		err = -ENOMEM;
1114 		goto err_alloc_etherdev;
1115 	}
1116 
1117 	if (!HINIC_IS_VF(hwdev->hwif))
1118 		netdev->netdev_ops = &hinic_netdev_ops;
1119 	else
1120 		netdev->netdev_ops = &hinicvf_netdev_ops;
1121 
1122 	netdev->max_mtu = ETH_MAX_MTU;
1123 
1124 	nic_dev = netdev_priv(netdev);
1125 	nic_dev->netdev = netdev;
1126 	nic_dev->hwdev  = hwdev;
1127 	nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
1128 	nic_dev->flags = 0;
1129 	nic_dev->txqs = NULL;
1130 	nic_dev->rxqs = NULL;
1131 	nic_dev->tx_weight = tx_weight;
1132 	nic_dev->rx_weight = rx_weight;
1133 	nic_dev->sq_depth = HINIC_SQ_DEPTH;
1134 	nic_dev->rq_depth = HINIC_RQ_DEPTH;
1135 	nic_dev->sriov_info.hwdev = hwdev;
1136 	nic_dev->sriov_info.pdev = pdev;
1137 	nic_dev->max_qps = num_qps;
1138 	nic_dev->devlink = devlink;
1139 
1140 	hinic_set_ethtool_ops(netdev);
1141 
1142 	sema_init(&nic_dev->mgmt_lock, 1);
1143 
1144 	tx_stats = &nic_dev->tx_stats;
1145 	rx_stats = &nic_dev->rx_stats;
1146 
1147 	u64_stats_init(&tx_stats->syncp);
1148 	u64_stats_init(&rx_stats->syncp);
1149 
1150 	nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
1151 					    VLAN_BITMAP_SIZE(nic_dev),
1152 					    GFP_KERNEL);
1153 	if (!nic_dev->vlan_bitmap) {
1154 		err = -ENOMEM;
1155 		goto err_vlan_bitmap;
1156 	}
1157 
1158 	nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
1159 	if (!nic_dev->workq) {
1160 		err = -ENOMEM;
1161 		goto err_workq;
1162 	}
1163 
1164 	err = hinic_devlink_register(devlink, &pdev->dev);
1165 	if (err)
1166 		goto err_devlink_reg;
1167 
1168 	pci_set_drvdata(pdev, netdev);
1169 
1170 	err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
1171 	if (err) {
1172 		dev_err(&pdev->dev, "Failed to get mac address\n");
1173 		goto err_get_mac;
1174 	}
1175 
1176 	if (!is_valid_ether_addr(netdev->dev_addr)) {
1177 		if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1178 			dev_err(&pdev->dev, "Invalid MAC address\n");
1179 			err = -EIO;
1180 			goto err_add_mac;
1181 		}
1182 
1183 		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1184 			 netdev->dev_addr);
1185 		eth_hw_addr_random(netdev);
1186 	}
1187 
1188 	err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1189 	if (err && err != HINIC_PF_SET_VF_ALREADY) {
1190 		dev_err(&pdev->dev, "Failed to add mac\n");
1191 		goto err_add_mac;
1192 	}
1193 
1194 	err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1195 	if (err) {
1196 		dev_err(&pdev->dev, "Failed to set mtu\n");
1197 		goto err_set_mtu;
1198 	}
1199 
1200 	rx_mode_work = &nic_dev->rx_mode_work;
1201 	INIT_WORK(&rx_mode_work->work, set_rx_mode);
1202 
1203 	netdev_features_init(netdev);
1204 
1205 	netif_carrier_off(netdev);
1206 
1207 	hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1208 				nic_dev, link_status_event_handler);
1209 
1210 	err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1211 	if (err)
1212 		goto err_set_features;
1213 
1214 	/* enable pause and disable pfc by default */
1215 	err = hinic_dcb_set_pfc(nic_dev->hwdev, 0, 0);
1216 	if (err)
1217 		goto err_set_pfc;
1218 
1219 	SET_NETDEV_DEV(netdev, &pdev->dev);
1220 
1221 	err = hinic_init_intr_coalesce(nic_dev);
1222 	if (err) {
1223 		dev_err(&pdev->dev, "Failed to init_intr_coalesce\n");
1224 		goto err_init_intr;
1225 	}
1226 
1227 	err = register_netdev(netdev);
1228 	if (err) {
1229 		dev_err(&pdev->dev, "Failed to register netdev\n");
1230 		goto err_reg_netdev;
1231 	}
1232 
1233 	return 0;
1234 
1235 err_reg_netdev:
1236 	hinic_free_intr_coalesce(nic_dev);
1237 err_init_intr:
1238 err_set_pfc:
1239 err_set_features:
1240 	hinic_hwdev_cb_unregister(nic_dev->hwdev,
1241 				  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1242 	cancel_work_sync(&rx_mode_work->work);
1243 
1244 err_set_mtu:
1245 	hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1246 err_add_mac:
1247 err_get_mac:
1248 	pci_set_drvdata(pdev, NULL);
1249 err_devlink_reg:
1250 	destroy_workqueue(nic_dev->workq);
1251 
1252 err_workq:
1253 err_vlan_bitmap:
1254 	free_netdev(netdev);
1255 
1256 err_alloc_etherdev:
1257 err_num_qps:
1258 err_devlink_alloc:
1259 	hinic_free_hwdev(hwdev);
1260 	return err;
1261 }
1262 
1263 static int hinic_probe(struct pci_dev *pdev,
1264 		       const struct pci_device_id *id)
1265 {
1266 	int err = pci_enable_device(pdev);
1267 
1268 	if (err) {
1269 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
1270 		return err;
1271 	}
1272 
1273 	err = pci_request_regions(pdev, HINIC_DRV_NAME);
1274 	if (err) {
1275 		dev_err(&pdev->dev, "Failed to request PCI regions\n");
1276 		goto err_pci_regions;
1277 	}
1278 
1279 	pci_set_master(pdev);
1280 
1281 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1282 	if (err) {
1283 		dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1284 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1285 		if (err) {
1286 			dev_err(&pdev->dev, "Failed to set DMA mask\n");
1287 			goto err_dma_mask;
1288 		}
1289 	}
1290 
1291 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1292 	if (err) {
1293 		dev_warn(&pdev->dev,
1294 			 "Couldn't set 64-bit consistent DMA mask\n");
1295 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1296 		if (err) {
1297 			dev_err(&pdev->dev,
1298 				"Failed to set consistent DMA mask\n");
1299 			goto err_dma_consistent_mask;
1300 		}
1301 	}
1302 
1303 	err = nic_dev_init(pdev);
1304 	if (err) {
1305 		dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1306 		goto err_nic_dev_init;
1307 	}
1308 
1309 	dev_info(&pdev->dev, "HiNIC driver - probed\n");
1310 	return 0;
1311 
1312 err_nic_dev_init:
1313 err_dma_consistent_mask:
1314 err_dma_mask:
1315 	pci_release_regions(pdev);
1316 
1317 err_pci_regions:
1318 	pci_disable_device(pdev);
1319 	return err;
1320 }
1321 
1322 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT	15000
1323 
1324 static void wait_sriov_cfg_complete(struct hinic_dev *nic_dev)
1325 {
1326 	struct hinic_sriov_info *sriov_info = &nic_dev->sriov_info;
1327 	u32 loop_cnt = 0;
1328 
1329 	set_bit(HINIC_FUNC_REMOVE, &sriov_info->state);
1330 	usleep_range(9900, 10000);
1331 
1332 	while (loop_cnt < HINIC_WAIT_SRIOV_CFG_TIMEOUT) {
1333 		if (!test_bit(HINIC_SRIOV_ENABLE, &sriov_info->state) &&
1334 		    !test_bit(HINIC_SRIOV_DISABLE, &sriov_info->state))
1335 			return;
1336 
1337 		usleep_range(9900, 10000);
1338 		loop_cnt++;
1339 	}
1340 }
1341 
1342 static void hinic_remove(struct pci_dev *pdev)
1343 {
1344 	struct net_device *netdev = pci_get_drvdata(pdev);
1345 	struct hinic_dev *nic_dev = netdev_priv(netdev);
1346 	struct hinic_rx_mode_work *rx_mode_work;
1347 
1348 	if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1349 		wait_sriov_cfg_complete(nic_dev);
1350 		hinic_pci_sriov_disable(pdev);
1351 	}
1352 
1353 	unregister_netdev(netdev);
1354 
1355 	hinic_free_intr_coalesce(nic_dev);
1356 
1357 	hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1358 
1359 	hinic_hwdev_cb_unregister(nic_dev->hwdev,
1360 				  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1361 
1362 	rx_mode_work = &nic_dev->rx_mode_work;
1363 	cancel_work_sync(&rx_mode_work->work);
1364 
1365 	pci_set_drvdata(pdev, NULL);
1366 
1367 	hinic_devlink_unregister(nic_dev->devlink);
1368 
1369 	destroy_workqueue(nic_dev->workq);
1370 
1371 	hinic_devlink_free(nic_dev->devlink);
1372 
1373 	hinic_free_hwdev(nic_dev->hwdev);
1374 
1375 	free_netdev(netdev);
1376 
1377 	pci_release_regions(pdev);
1378 	pci_disable_device(pdev);
1379 
1380 	dev_info(&pdev->dev, "HiNIC driver - removed\n");
1381 }
1382 
1383 static void hinic_shutdown(struct pci_dev *pdev)
1384 {
1385 	pci_disable_device(pdev);
1386 }
1387 
1388 static const struct pci_device_id hinic_pci_table[] = {
1389 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1390 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1391 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1392 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1393 	{ PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_VF), 0},
1394 	{ 0, 0}
1395 };
1396 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1397 
1398 static struct pci_driver hinic_driver = {
1399 	.name           = HINIC_DRV_NAME,
1400 	.id_table       = hinic_pci_table,
1401 	.probe          = hinic_probe,
1402 	.remove         = hinic_remove,
1403 	.shutdown       = hinic_shutdown,
1404 	.sriov_configure = hinic_pci_sriov_configure,
1405 };
1406 
1407 module_pci_driver(hinic_driver);
1408