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