1c0c050c5SMichael Chan /* Broadcom NetXtreme-C/E network driver.
2c0c050c5SMichael Chan  *
311f15ed3SMichael Chan  * Copyright (c) 2014-2016 Broadcom Corporation
48e202366SMichael Chan  * Copyright (c) 2016-2017 Broadcom Limited
5c0c050c5SMichael Chan  *
6c0c050c5SMichael Chan  * This program is free software; you can redistribute it and/or modify
7c0c050c5SMichael Chan  * it under the terms of the GNU General Public License as published by
8c0c050c5SMichael Chan  * the Free Software Foundation.
9c0c050c5SMichael Chan  */
10c0c050c5SMichael Chan 
113ebf6f0aSRob Swindell #include <linux/ctype.h>
128ddc9aaaSMichael Chan #include <linux/stringify.h>
13c0c050c5SMichael Chan #include <linux/ethtool.h>
14c0c050c5SMichael Chan #include <linux/interrupt.h>
15c0c050c5SMichael Chan #include <linux/pci.h>
16c0c050c5SMichael Chan #include <linux/etherdevice.h>
17c0c050c5SMichael Chan #include <linux/crc32.h>
18c0c050c5SMichael Chan #include <linux/firmware.h>
196c5657d0SVasundhara Volam #include <linux/utsname.h>
206c5657d0SVasundhara Volam #include <linux/time.h>
21c0c050c5SMichael Chan #include "bnxt_hsi.h"
22c0c050c5SMichael Chan #include "bnxt.h"
23f7dc1ea6SMichael Chan #include "bnxt_xdp.h"
24c0c050c5SMichael Chan #include "bnxt_ethtool.h"
25c0c050c5SMichael Chan #include "bnxt_nvm_defs.h"	/* NVRAM content constant and structure defs */
26c0c050c5SMichael Chan #include "bnxt_fw_hdr.h"	/* Firmware hdr constant and structure defs */
276c5657d0SVasundhara Volam #include "bnxt_coredump.h"
28c0c050c5SMichael Chan #define FLASH_NVRAM_TIMEOUT	((HWRM_CMD_TIMEOUT) * 100)
295ac67d8bSRob Swindell #define FLASH_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
305ac67d8bSRob Swindell #define INSTALL_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
31c0c050c5SMichael Chan 
32c0c050c5SMichael Chan static u32 bnxt_get_msglevel(struct net_device *dev)
33c0c050c5SMichael Chan {
34c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
35c0c050c5SMichael Chan 
36c0c050c5SMichael Chan 	return bp->msg_enable;
37c0c050c5SMichael Chan }
38c0c050c5SMichael Chan 
39c0c050c5SMichael Chan static void bnxt_set_msglevel(struct net_device *dev, u32 value)
40c0c050c5SMichael Chan {
41c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
42c0c050c5SMichael Chan 
43c0c050c5SMichael Chan 	bp->msg_enable = value;
44c0c050c5SMichael Chan }
45c0c050c5SMichael Chan 
46c0c050c5SMichael Chan static int bnxt_get_coalesce(struct net_device *dev,
47c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
48c0c050c5SMichael Chan {
49c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5018775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
5118775aa8SMichael Chan 	u16 mult;
52c0c050c5SMichael Chan 
53c0c050c5SMichael Chan 	memset(coal, 0, sizeof(*coal));
54c0c050c5SMichael Chan 
556a8788f2SAndy Gospodarek 	coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM;
566a8788f2SAndy Gospodarek 
5718775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
5818775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
5918775aa8SMichael Chan 	coal->rx_coalesce_usecs = hw_coal->coal_ticks;
6018775aa8SMichael Chan 	coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6118775aa8SMichael Chan 	coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
6218775aa8SMichael Chan 	coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
63c0c050c5SMichael Chan 
6418775aa8SMichael Chan 	hw_coal = &bp->tx_coal;
6518775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
6618775aa8SMichael Chan 	coal->tx_coalesce_usecs = hw_coal->coal_ticks;
6718775aa8SMichael Chan 	coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6818775aa8SMichael Chan 	coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
6918775aa8SMichael Chan 	coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
70dfc9c94aSMichael Chan 
7151f30785SMichael Chan 	coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
7251f30785SMichael Chan 
73c0c050c5SMichael Chan 	return 0;
74c0c050c5SMichael Chan }
75c0c050c5SMichael Chan 
76c0c050c5SMichael Chan static int bnxt_set_coalesce(struct net_device *dev,
77c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
78c0c050c5SMichael Chan {
79c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
8051f30785SMichael Chan 	bool update_stats = false;
8118775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
82c0c050c5SMichael Chan 	int rc = 0;
8318775aa8SMichael Chan 	u16 mult;
84c0c050c5SMichael Chan 
856a8788f2SAndy Gospodarek 	if (coal->use_adaptive_rx_coalesce) {
866a8788f2SAndy Gospodarek 		bp->flags |= BNXT_FLAG_DIM;
876a8788f2SAndy Gospodarek 	} else {
886a8788f2SAndy Gospodarek 		if (bp->flags & BNXT_FLAG_DIM) {
896a8788f2SAndy Gospodarek 			bp->flags &= ~(BNXT_FLAG_DIM);
906a8788f2SAndy Gospodarek 			goto reset_coalesce;
916a8788f2SAndy Gospodarek 		}
926a8788f2SAndy Gospodarek 	}
936a8788f2SAndy Gospodarek 
9418775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
9518775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
9618775aa8SMichael Chan 	hw_coal->coal_ticks = coal->rx_coalesce_usecs;
9718775aa8SMichael Chan 	hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult;
9818775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq;
9918775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult;
100c0c050c5SMichael Chan 
101de4a10efSAndy Gospodarek 	hw_coal = &bp->tx_coal;
10218775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
10318775aa8SMichael Chan 	hw_coal->coal_ticks = coal->tx_coalesce_usecs;
10418775aa8SMichael Chan 	hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult;
10518775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq;
10618775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult;
107dfc9c94aSMichael Chan 
10851f30785SMichael Chan 	if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
10951f30785SMichael Chan 		u32 stats_ticks = coal->stats_block_coalesce_usecs;
11051f30785SMichael Chan 
111adcc331eSMichael Chan 		/* Allow 0, which means disable. */
112adcc331eSMichael Chan 		if (stats_ticks)
11351f30785SMichael Chan 			stats_ticks = clamp_t(u32, stats_ticks,
11451f30785SMichael Chan 					      BNXT_MIN_STATS_COAL_TICKS,
11551f30785SMichael Chan 					      BNXT_MAX_STATS_COAL_TICKS);
11651f30785SMichael Chan 		stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
11751f30785SMichael Chan 		bp->stats_coal_ticks = stats_ticks;
118e795892eSMichael Chan 		if (bp->stats_coal_ticks)
119e795892eSMichael Chan 			bp->current_interval =
120e795892eSMichael Chan 				bp->stats_coal_ticks * HZ / 1000000;
121e795892eSMichael Chan 		else
122e795892eSMichael Chan 			bp->current_interval = BNXT_TIMER_INTERVAL;
12351f30785SMichael Chan 		update_stats = true;
12451f30785SMichael Chan 	}
12551f30785SMichael Chan 
1266a8788f2SAndy Gospodarek reset_coalesce:
12751f30785SMichael Chan 	if (netif_running(dev)) {
12851f30785SMichael Chan 		if (update_stats) {
12951f30785SMichael Chan 			rc = bnxt_close_nic(bp, true, false);
13051f30785SMichael Chan 			if (!rc)
13151f30785SMichael Chan 				rc = bnxt_open_nic(bp, true, false);
13251f30785SMichael Chan 		} else {
133c0c050c5SMichael Chan 			rc = bnxt_hwrm_set_coal(bp);
13451f30785SMichael Chan 		}
13551f30785SMichael Chan 	}
136c0c050c5SMichael Chan 
137c0c050c5SMichael Chan 	return rc;
138c0c050c5SMichael Chan }
139c0c050c5SMichael Chan 
14083eb5c5cSMichael Chan #define BNXT_NUM_STATS	22
141c0c050c5SMichael Chan 
1428ddc9aaaSMichael Chan #define BNXT_RX_STATS_ENTRY(counter)	\
1438ddc9aaaSMichael Chan 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
1448ddc9aaaSMichael Chan 
1458ddc9aaaSMichael Chan #define BNXT_TX_STATS_ENTRY(counter)	\
1468ddc9aaaSMichael Chan 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
1478ddc9aaaSMichael Chan 
14800db3cbaSVasundhara Volam #define BNXT_RX_STATS_EXT_ENTRY(counter)	\
14900db3cbaSVasundhara Volam 	{ BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
15000db3cbaSVasundhara Volam 
15136e53349SMichael Chan #define BNXT_TX_STATS_EXT_ENTRY(counter)	\
15236e53349SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter), __stringify(counter) }
15336e53349SMichael Chan 
15436e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRY(n)				\
15536e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_duration_us),	\
15636e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_transitions)
15736e53349SMichael Chan 
15836e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRY(n)				\
15936e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_duration_us),	\
16036e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_transitions)
16136e53349SMichael Chan 
16236e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRIES				\
16336e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(0),				\
16436e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(1),				\
16536e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(2),				\
16636e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(3),				\
16736e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(4),				\
16836e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(5),				\
16936e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(6),				\
17036e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(7)
17136e53349SMichael Chan 
17236e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRIES				\
17336e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(0),				\
17436e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(1),				\
17536e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(2),				\
17636e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(3),				\
17736e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(4),				\
17836e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(5),				\
17936e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(6),				\
18036e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(7)
18136e53349SMichael Chan 
18236e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRY(n)				\
18336e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bytes_cos##n),		\
18436e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_packets_cos##n)
18536e53349SMichael Chan 
18636e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRY(n)				\
18736e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_bytes_cos##n),		\
18836e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_packets_cos##n)
18936e53349SMichael Chan 
19036e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRIES				\
19136e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(0),				\
19236e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(1),				\
19336e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(2),				\
19436e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(3),				\
19536e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(4),				\
19636e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(5),				\
19736e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(6),				\
19836e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(7)				\
19936e53349SMichael Chan 
20036e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRIES				\
20136e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(0),				\
20236e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(1),				\
20336e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(2),				\
20436e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(3),				\
20536e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(4),				\
20636e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(5),				\
20736e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(6),				\
20836e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(7)				\
20936e53349SMichael Chan 
210e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRY(counter, n)		\
211e37fed79SMichael Chan 	{ BNXT_RX_STATS_EXT_OFFSET(counter##_cos0),	\
212e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
213e37fed79SMichael Chan 
214e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRY(counter, n)		\
215e37fed79SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter##_cos0),	\
216e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
217e37fed79SMichael Chan 
218e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRIES(counter)		\
219e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 0),		\
220e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 1),		\
221e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 2),		\
222e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 3),		\
223e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 4),		\
224e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 5),		\
225e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 6),		\
226e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 7)
227e37fed79SMichael Chan 
228e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRIES(counter)		\
229e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 0),		\
230e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 1),		\
231e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 2),		\
232e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 3),		\
233e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 4),		\
234e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 5),		\
235e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 6),		\
236e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 7)
237e37fed79SMichael Chan 
23855e4398dSVasundhara Volam #define BNXT_PCIE_STATS_ENTRY(counter)	\
23955e4398dSVasundhara Volam 	{ BNXT_PCIE_STATS_OFFSET(counter), __stringify(counter) }
24055e4398dSVasundhara Volam 
24120c1d28eSVasundhara Volam enum {
24220c1d28eSVasundhara Volam 	RX_TOTAL_DISCARDS,
24320c1d28eSVasundhara Volam 	TX_TOTAL_DISCARDS,
24420c1d28eSVasundhara Volam };
24520c1d28eSVasundhara Volam 
24620c1d28eSVasundhara Volam static struct {
24720c1d28eSVasundhara Volam 	u64			counter;
24820c1d28eSVasundhara Volam 	char			string[ETH_GSTRING_LEN];
24920c1d28eSVasundhara Volam } bnxt_sw_func_stats[] = {
25020c1d28eSVasundhara Volam 	{0, "rx_total_discard_pkts"},
25120c1d28eSVasundhara Volam 	{0, "tx_total_discard_pkts"},
25220c1d28eSVasundhara Volam };
25320c1d28eSVasundhara Volam 
2548ddc9aaaSMichael Chan static const struct {
2558ddc9aaaSMichael Chan 	long offset;
2568ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
2578ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
2588ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
2598ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
2608ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
2618ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
2628ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
2636fc92c33SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
2648ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
2658ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
2668ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
2678ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
2688ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
2698ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
2708ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
2718ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
2728ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
2738ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
2748ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
2758ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
2768ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
2778ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
2788ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
2798ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
2808ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
2818ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
2828ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
2838ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
284c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
285c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
286c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
287c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
288c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
289c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
290c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
291c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
2928ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
2938ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
2948ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
2958ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
2968ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
2978ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
298699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
299699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_err),
3008ddc9aaaSMichael Chan 
3018ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
3028ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
3038ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
3048ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
3058ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
3066fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
3078ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
3086fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
3098ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
3108ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
3118ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
3128ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
3138ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
3148ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
3158ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
3168ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
3178ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
3188ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
3198ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
3208ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
3218ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
3228ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
323c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
324c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
325c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
326c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
327c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
328c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
329c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
330c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
3318ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
3328ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
3338ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
3348ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
335699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
336699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
337699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_error),
3388ddc9aaaSMichael Chan };
3398ddc9aaaSMichael Chan 
34000db3cbaSVasundhara Volam static const struct {
34100db3cbaSVasundhara Volam 	long offset;
34200db3cbaSVasundhara Volam 	char string[ETH_GSTRING_LEN];
34300db3cbaSVasundhara Volam } bnxt_port_stats_ext_arr[] = {
34400db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
34500db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
34600db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
34700db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
34800db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
34936e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRIES,
35036e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRIES,
3514a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bits),
3524a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_buffer_passed_threshold),
3534a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_pcs_symbol_err),
3544a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_corrected_bits),
35536e53349SMichael Chan };
35636e53349SMichael Chan 
35736e53349SMichael Chan static const struct {
35836e53349SMichael Chan 	long offset;
35936e53349SMichael Chan 	char string[ETH_GSTRING_LEN];
36036e53349SMichael Chan } bnxt_tx_port_stats_ext_arr[] = {
36136e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRIES,
36236e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRIES,
36300db3cbaSVasundhara Volam };
36400db3cbaSVasundhara Volam 
365e37fed79SMichael Chan static const struct {
366e37fed79SMichael Chan 	long base_off;
367e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
368e37fed79SMichael Chan } bnxt_rx_bytes_pri_arr[] = {
369e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_bytes),
370e37fed79SMichael Chan };
371e37fed79SMichael Chan 
372e37fed79SMichael Chan static const struct {
373e37fed79SMichael Chan 	long base_off;
374e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
375e37fed79SMichael Chan } bnxt_rx_pkts_pri_arr[] = {
376e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_packets),
377e37fed79SMichael Chan };
378e37fed79SMichael Chan 
379e37fed79SMichael Chan static const struct {
380e37fed79SMichael Chan 	long base_off;
381e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
382e37fed79SMichael Chan } bnxt_tx_bytes_pri_arr[] = {
383e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_bytes),
384e37fed79SMichael Chan };
385e37fed79SMichael Chan 
386e37fed79SMichael Chan static const struct {
387e37fed79SMichael Chan 	long base_off;
388e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
389e37fed79SMichael Chan } bnxt_tx_pkts_pri_arr[] = {
390e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_packets),
391e37fed79SMichael Chan };
392e37fed79SMichael Chan 
39355e4398dSVasundhara Volam static const struct {
39455e4398dSVasundhara Volam 	long offset;
39555e4398dSVasundhara Volam 	char string[ETH_GSTRING_LEN];
39655e4398dSVasundhara Volam } bnxt_pcie_stats_arr[] = {
39755e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_pl_signal_integrity),
39855e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_dl_signal_integrity),
39955e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_tl_signal_integrity),
40055e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_link_integrity),
40155e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_tx_traffic_rate),
40255e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_rx_traffic_rate),
40355e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_tx_dllp_statistics),
40455e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_rx_dllp_statistics),
40555e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_equalization_time),
40655e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_ltssm_histogram[0]),
40755e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_ltssm_histogram[2]),
40855e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_recovery_histogram),
40955e4398dSVasundhara Volam };
41055e4398dSVasundhara Volam 
41120c1d28eSVasundhara Volam #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
4128ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
413e37fed79SMichael Chan #define BNXT_NUM_STATS_PRI			\
414e37fed79SMichael Chan 	(ARRAY_SIZE(bnxt_rx_bytes_pri_arr) +	\
415e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_rx_pkts_pri_arr) +	\
416e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_bytes_pri_arr) +	\
417e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_pkts_pri_arr))
41855e4398dSVasundhara Volam #define BNXT_NUM_PCIE_STATS ARRAY_SIZE(bnxt_pcie_stats_arr)
4198ddc9aaaSMichael Chan 
4205c8227d0SMichael Chan static int bnxt_get_num_stats(struct bnxt *bp)
421c0c050c5SMichael Chan {
4228ddc9aaaSMichael Chan 	int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
4238ddc9aaaSMichael Chan 
42420c1d28eSVasundhara Volam 	num_stats += BNXT_NUM_SW_FUNC_STATS;
42520c1d28eSVasundhara Volam 
4268ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS)
4278ddc9aaaSMichael Chan 		num_stats += BNXT_NUM_PORT_STATS;
4288ddc9aaaSMichael Chan 
429e37fed79SMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
43036e53349SMichael Chan 		num_stats += bp->fw_rx_stats_ext_size +
43136e53349SMichael Chan 			     bp->fw_tx_stats_ext_size;
432e37fed79SMichael Chan 		if (bp->pri2cos_valid)
433e37fed79SMichael Chan 			num_stats += BNXT_NUM_STATS_PRI;
434e37fed79SMichael Chan 	}
43500db3cbaSVasundhara Volam 
43655e4398dSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PCIE_STATS)
43755e4398dSVasundhara Volam 		num_stats += BNXT_NUM_PCIE_STATS;
43855e4398dSVasundhara Volam 
4398ddc9aaaSMichael Chan 	return num_stats;
4408ddc9aaaSMichael Chan }
4415c8227d0SMichael Chan 
4425c8227d0SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
4435c8227d0SMichael Chan {
4445c8227d0SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
4455c8227d0SMichael Chan 
4465c8227d0SMichael Chan 	switch (sset) {
4475c8227d0SMichael Chan 	case ETH_SS_STATS:
4485c8227d0SMichael Chan 		return bnxt_get_num_stats(bp);
449eb513658SMichael Chan 	case ETH_SS_TEST:
450eb513658SMichael Chan 		if (!bp->num_tests)
451eb513658SMichael Chan 			return -EOPNOTSUPP;
452eb513658SMichael Chan 		return bp->num_tests;
453c0c050c5SMichael Chan 	default:
454c0c050c5SMichael Chan 		return -EOPNOTSUPP;
455c0c050c5SMichael Chan 	}
456c0c050c5SMichael Chan }
457c0c050c5SMichael Chan 
458c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
459c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
460c0c050c5SMichael Chan {
461c0c050c5SMichael Chan 	u32 i, j = 0;
462c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
463c0c050c5SMichael Chan 	u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
464c0c050c5SMichael Chan 
465fd3ab1c7SMichael Chan 	if (!bp->bnapi) {
466fd3ab1c7SMichael Chan 		j += BNXT_NUM_STATS * bp->cp_nr_rings + BNXT_NUM_SW_FUNC_STATS;
467fd3ab1c7SMichael Chan 		goto skip_ring_stats;
468fd3ab1c7SMichael Chan 	}
469c0c050c5SMichael Chan 
47020c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
47120c1d28eSVasundhara Volam 		bnxt_sw_func_stats[i].counter = 0;
47220c1d28eSVasundhara Volam 
473c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
474c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
475c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
476c0c050c5SMichael Chan 		__le64 *hw_stats = (__le64 *)cpr->hw_stats;
477c0c050c5SMichael Chan 		int k;
478c0c050c5SMichael Chan 
479c0c050c5SMichael Chan 		for (k = 0; k < stat_fields; j++, k++)
480c0c050c5SMichael Chan 			buf[j] = le64_to_cpu(hw_stats[k]);
481c0c050c5SMichael Chan 		buf[j++] = cpr->rx_l4_csum_errors;
48283eb5c5cSMichael Chan 		buf[j++] = cpr->missed_irqs;
48320c1d28eSVasundhara Volam 
48420c1d28eSVasundhara Volam 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
48520c1d28eSVasundhara Volam 			le64_to_cpu(cpr->hw_stats->rx_discard_pkts);
48620c1d28eSVasundhara Volam 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
48720c1d28eSVasundhara Volam 			le64_to_cpu(cpr->hw_stats->tx_discard_pkts);
488c0c050c5SMichael Chan 	}
48920c1d28eSVasundhara Volam 
49020c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
49120c1d28eSVasundhara Volam 		buf[j] = bnxt_sw_func_stats[i].counter;
49220c1d28eSVasundhara Volam 
493fd3ab1c7SMichael Chan skip_ring_stats:
4948ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4958ddc9aaaSMichael Chan 		__le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
4968ddc9aaaSMichael Chan 
4978ddc9aaaSMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
4988ddc9aaaSMichael Chan 			buf[j] = le64_to_cpu(*(port_stats +
4998ddc9aaaSMichael Chan 					       bnxt_port_stats_arr[i].offset));
5008ddc9aaaSMichael Chan 		}
5018ddc9aaaSMichael Chan 	}
50200db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
50336e53349SMichael Chan 		__le64 *rx_port_stats_ext = (__le64 *)bp->hw_rx_port_stats_ext;
50436e53349SMichael Chan 		__le64 *tx_port_stats_ext = (__le64 *)bp->hw_tx_port_stats_ext;
50500db3cbaSVasundhara Volam 
50636e53349SMichael Chan 		for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
50736e53349SMichael Chan 			buf[j] = le64_to_cpu(*(rx_port_stats_ext +
50800db3cbaSVasundhara Volam 					    bnxt_port_stats_ext_arr[i].offset));
50900db3cbaSVasundhara Volam 		}
51036e53349SMichael Chan 		for (i = 0; i < bp->fw_tx_stats_ext_size; i++, j++) {
51136e53349SMichael Chan 			buf[j] = le64_to_cpu(*(tx_port_stats_ext +
51236e53349SMichael Chan 					bnxt_tx_port_stats_ext_arr[i].offset));
51336e53349SMichael Chan 		}
514e37fed79SMichael Chan 		if (bp->pri2cos_valid) {
515e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
516e37fed79SMichael Chan 				long n = bnxt_rx_bytes_pri_arr[i].base_off +
517e37fed79SMichael Chan 					 bp->pri2cos[i];
518e37fed79SMichael Chan 
519e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(rx_port_stats_ext + n));
520e37fed79SMichael Chan 			}
521e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
522e37fed79SMichael Chan 				long n = bnxt_rx_pkts_pri_arr[i].base_off +
523e37fed79SMichael Chan 					 bp->pri2cos[i];
524e37fed79SMichael Chan 
525e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(rx_port_stats_ext + n));
526e37fed79SMichael Chan 			}
527e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
528e37fed79SMichael Chan 				long n = bnxt_tx_bytes_pri_arr[i].base_off +
529e37fed79SMichael Chan 					 bp->pri2cos[i];
530e37fed79SMichael Chan 
531e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(tx_port_stats_ext + n));
532e37fed79SMichael Chan 			}
533e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
534e37fed79SMichael Chan 				long n = bnxt_tx_pkts_pri_arr[i].base_off +
535e37fed79SMichael Chan 					 bp->pri2cos[i];
536e37fed79SMichael Chan 
537e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(tx_port_stats_ext + n));
538e37fed79SMichael Chan 			}
539e37fed79SMichael Chan 		}
54000db3cbaSVasundhara Volam 	}
54155e4398dSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PCIE_STATS) {
54255e4398dSVasundhara Volam 		__le64 *pcie_stats = (__le64 *)bp->hw_pcie_stats;
54355e4398dSVasundhara Volam 
54455e4398dSVasundhara Volam 		for (i = 0; i < BNXT_NUM_PCIE_STATS; i++, j++) {
54555e4398dSVasundhara Volam 			buf[j] = le64_to_cpu(*(pcie_stats +
54655e4398dSVasundhara Volam 					       bnxt_pcie_stats_arr[i].offset));
54755e4398dSVasundhara Volam 		}
54855e4398dSVasundhara Volam 	}
549c0c050c5SMichael Chan }
550c0c050c5SMichael Chan 
551c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
552c0c050c5SMichael Chan {
553c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
554c0c050c5SMichael Chan 	u32 i;
555c0c050c5SMichael Chan 
556c0c050c5SMichael Chan 	switch (stringset) {
557c0c050c5SMichael Chan 	/* The number of strings must match BNXT_NUM_STATS defined above. */
558c0c050c5SMichael Chan 	case ETH_SS_STATS:
559c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
560c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_ucast_packets", i);
561c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
562c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_mcast_packets", i);
563c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
564c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_bcast_packets", i);
565c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
566c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_discards", i);
567c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
568c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_drops", i);
569c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
570c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_ucast_bytes", i);
571c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
572c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_mcast_bytes", i);
573c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
574c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_bcast_bytes", i);
575c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
576c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_ucast_packets", i);
577c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
578c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_mcast_packets", i);
579c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
580c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_bcast_packets", i);
581c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
582c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_discards", i);
583c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
584c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_drops", i);
585c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
586c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_ucast_bytes", i);
587c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
588c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_mcast_bytes", i);
589c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
590c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_bcast_bytes", i);
591c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
592c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_packets", i);
593c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
594c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_bytes", i);
595c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
596c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_events", i);
597c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
598c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_aborts", i);
599c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
600c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_l4_csum_errors", i);
601c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
60283eb5c5cSMichael Chan 			sprintf(buf, "[%d]: missed_irqs", i);
60383eb5c5cSMichael Chan 			buf += ETH_GSTRING_LEN;
604c0c050c5SMichael Chan 		}
60520c1d28eSVasundhara Volam 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
60620c1d28eSVasundhara Volam 			strcpy(buf, bnxt_sw_func_stats[i].string);
60720c1d28eSVasundhara Volam 			buf += ETH_GSTRING_LEN;
60820c1d28eSVasundhara Volam 		}
60920c1d28eSVasundhara Volam 
6108ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
6118ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
6128ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
6138ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
6148ddc9aaaSMichael Chan 			}
6158ddc9aaaSMichael Chan 		}
61600db3cbaSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
61736e53349SMichael Chan 			for (i = 0; i < bp->fw_rx_stats_ext_size; i++) {
61800db3cbaSVasundhara Volam 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
61900db3cbaSVasundhara Volam 				buf += ETH_GSTRING_LEN;
62000db3cbaSVasundhara Volam 			}
62136e53349SMichael Chan 			for (i = 0; i < bp->fw_tx_stats_ext_size; i++) {
62236e53349SMichael Chan 				strcpy(buf,
62336e53349SMichael Chan 				       bnxt_tx_port_stats_ext_arr[i].string);
62436e53349SMichael Chan 				buf += ETH_GSTRING_LEN;
62536e53349SMichael Chan 			}
626e37fed79SMichael Chan 			if (bp->pri2cos_valid) {
627e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
628e37fed79SMichael Chan 					strcpy(buf,
629e37fed79SMichael Chan 					       bnxt_rx_bytes_pri_arr[i].string);
630e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
631e37fed79SMichael Chan 				}
632e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
633e37fed79SMichael Chan 					strcpy(buf,
634e37fed79SMichael Chan 					       bnxt_rx_pkts_pri_arr[i].string);
635e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
636e37fed79SMichael Chan 				}
637e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
638e37fed79SMichael Chan 					strcpy(buf,
639e37fed79SMichael Chan 					       bnxt_tx_bytes_pri_arr[i].string);
640e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
641e37fed79SMichael Chan 				}
642e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
643e37fed79SMichael Chan 					strcpy(buf,
644e37fed79SMichael Chan 					       bnxt_tx_pkts_pri_arr[i].string);
645e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
646e37fed79SMichael Chan 				}
647e37fed79SMichael Chan 			}
64800db3cbaSVasundhara Volam 		}
64955e4398dSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PCIE_STATS) {
65055e4398dSVasundhara Volam 			for (i = 0; i < BNXT_NUM_PCIE_STATS; i++) {
65155e4398dSVasundhara Volam 				strcpy(buf, bnxt_pcie_stats_arr[i].string);
65255e4398dSVasundhara Volam 				buf += ETH_GSTRING_LEN;
65355e4398dSVasundhara Volam 			}
65455e4398dSVasundhara Volam 		}
655c0c050c5SMichael Chan 		break;
656eb513658SMichael Chan 	case ETH_SS_TEST:
657eb513658SMichael Chan 		if (bp->num_tests)
658eb513658SMichael Chan 			memcpy(buf, bp->test_info->string,
659eb513658SMichael Chan 			       bp->num_tests * ETH_GSTRING_LEN);
660eb513658SMichael Chan 		break;
661c0c050c5SMichael Chan 	default:
662c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
663c0c050c5SMichael Chan 			   stringset);
664c0c050c5SMichael Chan 		break;
665c0c050c5SMichael Chan 	}
666c0c050c5SMichael Chan }
667c0c050c5SMichael Chan 
668c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
669c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
670c0c050c5SMichael Chan {
671c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
672c0c050c5SMichael Chan 
673c0c050c5SMichael Chan 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
674c0c050c5SMichael Chan 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
675c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
676c0c050c5SMichael Chan 
677c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
678c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
679c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
680c0c050c5SMichael Chan }
681c0c050c5SMichael Chan 
682c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
683c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
684c0c050c5SMichael Chan {
685c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
686c0c050c5SMichael Chan 
687c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
688c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
689c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
690c0c050c5SMichael Chan 		return -EINVAL;
691c0c050c5SMichael Chan 
692c0c050c5SMichael Chan 	if (netif_running(dev))
693c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
694c0c050c5SMichael Chan 
695c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
696c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
697c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
698c0c050c5SMichael Chan 
699c0c050c5SMichael Chan 	if (netif_running(dev))
700c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
701c0c050c5SMichael Chan 
702c0c050c5SMichael Chan 	return 0;
703c0c050c5SMichael Chan }
704c0c050c5SMichael Chan 
705c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
706c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
707c0c050c5SMichael Chan {
708c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
709db4723b3SMichael Chan 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
710c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
711db4723b3SMichael Chan 	int max_tx_sch_inputs;
712db4723b3SMichael Chan 
713db4723b3SMichael Chan 	/* Get the most up-to-date max_tx_sch_inputs. */
714f1ca94deSMichael Chan 	if (BNXT_NEW_RM(bp))
715db4723b3SMichael Chan 		bnxt_hwrm_func_resc_qcaps(bp, false);
716db4723b3SMichael Chan 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
717c0c050c5SMichael Chan 
7186e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
719db4723b3SMichael Chan 	if (max_tx_sch_inputs)
720db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
721a79a5276SMichael Chan 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
722068c9ec6SMichael Chan 
72318d6e4e2SSatish Baddipadige 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
72418d6e4e2SSatish Baddipadige 		max_rx_rings = 0;
72518d6e4e2SSatish Baddipadige 		max_tx_rings = 0;
72618d6e4e2SSatish Baddipadige 	}
727db4723b3SMichael Chan 	if (max_tx_sch_inputs)
728db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
72918d6e4e2SSatish Baddipadige 
730c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
731c0c050c5SMichael Chan 	if (tcs > 1)
732c0c050c5SMichael Chan 		max_tx_rings /= tcs;
733c0c050c5SMichael Chan 
734c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
735c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
736c0c050c5SMichael Chan 	channel->max_other = 0;
737068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
738068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
73976595193SPrashant Sreedharan 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
74076595193SPrashant Sreedharan 			channel->combined_count--;
741068c9ec6SMichael Chan 	} else {
74276595193SPrashant Sreedharan 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
743c0c050c5SMichael Chan 			channel->rx_count = bp->rx_nr_rings;
744c0c050c5SMichael Chan 			channel->tx_count = bp->tx_nr_rings_per_tc;
745c0c050c5SMichael Chan 		}
746068c9ec6SMichael Chan 	}
74776595193SPrashant Sreedharan }
748c0c050c5SMichael Chan 
749c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
750c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
751c0c050c5SMichael Chan {
752c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
753d1e7925eSMichael Chan 	int req_tx_rings, req_rx_rings, tcs;
754068c9ec6SMichael Chan 	bool sh = false;
7555f449249SMichael Chan 	int tx_xdp = 0;
756d1e7925eSMichael Chan 	int rc = 0;
757c0c050c5SMichael Chan 
758068c9ec6SMichael Chan 	if (channel->other_count)
759c0c050c5SMichael Chan 		return -EINVAL;
760c0c050c5SMichael Chan 
761068c9ec6SMichael Chan 	if (!channel->combined_count &&
762068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
763068c9ec6SMichael Chan 		return -EINVAL;
764068c9ec6SMichael Chan 
765068c9ec6SMichael Chan 	if (channel->combined_count &&
766068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
767068c9ec6SMichael Chan 		return -EINVAL;
768068c9ec6SMichael Chan 
76976595193SPrashant Sreedharan 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
77076595193SPrashant Sreedharan 					    channel->tx_count))
77176595193SPrashant Sreedharan 		return -EINVAL;
77276595193SPrashant Sreedharan 
773068c9ec6SMichael Chan 	if (channel->combined_count)
774068c9ec6SMichael Chan 		sh = true;
775068c9ec6SMichael Chan 
776c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
777c0c050c5SMichael Chan 
778391be5c2SMichael Chan 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
779d1e7925eSMichael Chan 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
7805f449249SMichael Chan 	if (bp->tx_nr_rings_xdp) {
7815f449249SMichael Chan 		if (!sh) {
7825f449249SMichael Chan 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
7835f449249SMichael Chan 			return -EINVAL;
7845f449249SMichael Chan 		}
7855f449249SMichael Chan 		tx_xdp = req_rx_rings;
7865f449249SMichael Chan 	}
78798fdbe73SMichael Chan 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
788d1e7925eSMichael Chan 	if (rc) {
789d1e7925eSMichael Chan 		netdev_warn(dev, "Unable to allocate the requested rings\n");
790d1e7925eSMichael Chan 		return rc;
791391be5c2SMichael Chan 	}
792391be5c2SMichael Chan 
793c0c050c5SMichael Chan 	if (netif_running(dev)) {
794c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
795c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
796c0c050c5SMichael Chan 			 * before PF unload
797c0c050c5SMichael Chan 			 */
798c0c050c5SMichael Chan 		}
799c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
800c0c050c5SMichael Chan 		if (rc) {
801c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
802c0c050c5SMichael Chan 				   rc);
803c0c050c5SMichael Chan 			return rc;
804c0c050c5SMichael Chan 		}
805c0c050c5SMichael Chan 	}
806c0c050c5SMichael Chan 
807068c9ec6SMichael Chan 	if (sh) {
808068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
809d1e7925eSMichael Chan 		bp->rx_nr_rings = channel->combined_count;
810d1e7925eSMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
811068c9ec6SMichael Chan 	} else {
812068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
813c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
814c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
815068c9ec6SMichael Chan 	}
8165f449249SMichael Chan 	bp->tx_nr_rings_xdp = tx_xdp;
8175f449249SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
818c0c050c5SMichael Chan 	if (tcs > 1)
8195f449249SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
820068c9ec6SMichael Chan 
821068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
822068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
823068c9ec6SMichael Chan 
8242bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
8252bcfa6f6SMichael Chan 	netdev_update_features(dev);
826c0c050c5SMichael Chan 	if (netif_running(dev)) {
827c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
828c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
829c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
830c0c050c5SMichael Chan 			 * to renable
831c0c050c5SMichael Chan 			 */
832c0c050c5SMichael Chan 		}
833d8c09f19SMichael Chan 	} else {
8341b3f0b75SMichael Chan 		rc = bnxt_reserve_rings(bp, true);
835c0c050c5SMichael Chan 	}
836c0c050c5SMichael Chan 
837c0c050c5SMichael Chan 	return rc;
838c0c050c5SMichael Chan }
839c0c050c5SMichael Chan 
840c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
841c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
842c0c050c5SMichael Chan 			    u32 *rule_locs)
843c0c050c5SMichael Chan {
844c0c050c5SMichael Chan 	int i, j = 0;
845c0c050c5SMichael Chan 
846c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
847c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
848c0c050c5SMichael Chan 		struct hlist_head *head;
849c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
850c0c050c5SMichael Chan 
851c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
852c0c050c5SMichael Chan 		rcu_read_lock();
853c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
854c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
855c0c050c5SMichael Chan 				break;
856c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
857c0c050c5SMichael Chan 		}
858c0c050c5SMichael Chan 		rcu_read_unlock();
859c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
860c0c050c5SMichael Chan 			break;
861c0c050c5SMichael Chan 	}
862c0c050c5SMichael Chan 	cmd->rule_cnt = j;
863c0c050c5SMichael Chan 	return 0;
864c0c050c5SMichael Chan }
865c0c050c5SMichael Chan 
866c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
867c0c050c5SMichael Chan {
868c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
869c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
870c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
871c0c050c5SMichael Chan 	struct flow_keys *fkeys;
872c0c050c5SMichael Chan 	int i, rc = -EINVAL;
873c0c050c5SMichael Chan 
874b721cfafSstephen hemminger 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
875c0c050c5SMichael Chan 		return rc;
876c0c050c5SMichael Chan 
877c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
878c0c050c5SMichael Chan 		struct hlist_head *head;
879c0c050c5SMichael Chan 
880c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
881c0c050c5SMichael Chan 		rcu_read_lock();
882c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
883c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
884c0c050c5SMichael Chan 				goto fltr_found;
885c0c050c5SMichael Chan 		}
886c0c050c5SMichael Chan 		rcu_read_unlock();
887c0c050c5SMichael Chan 	}
888c0c050c5SMichael Chan 	return rc;
889c0c050c5SMichael Chan 
890c0c050c5SMichael Chan fltr_found:
891c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
892dda0e746SMichael Chan 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
893c0c050c5SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
894c0c050c5SMichael Chan 			fs->flow_type = TCP_V4_FLOW;
895c0c050c5SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
896c0c050c5SMichael Chan 			fs->flow_type = UDP_V4_FLOW;
897c0c050c5SMichael Chan 		else
898c0c050c5SMichael Chan 			goto fltr_err;
899c0c050c5SMichael Chan 
900c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
901c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
902c0c050c5SMichael Chan 
903c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
904c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
905c0c050c5SMichael Chan 
906c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
907c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
908c0c050c5SMichael Chan 
909c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
910c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
911dda0e746SMichael Chan 	} else {
912dda0e746SMichael Chan 		int i;
913dda0e746SMichael Chan 
914dda0e746SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
915dda0e746SMichael Chan 			fs->flow_type = TCP_V6_FLOW;
916dda0e746SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
917dda0e746SMichael Chan 			fs->flow_type = UDP_V6_FLOW;
918dda0e746SMichael Chan 		else
919dda0e746SMichael Chan 			goto fltr_err;
920dda0e746SMichael Chan 
921dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
922dda0e746SMichael Chan 			fkeys->addrs.v6addrs.src;
923dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
924dda0e746SMichael Chan 			fkeys->addrs.v6addrs.dst;
925dda0e746SMichael Chan 		for (i = 0; i < 4; i++) {
926dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
927dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
928dda0e746SMichael Chan 		}
929dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
930dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
931dda0e746SMichael Chan 
932dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
933dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
934dda0e746SMichael Chan 	}
935c0c050c5SMichael Chan 
936c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
937c0c050c5SMichael Chan 	rc = 0;
938c0c050c5SMichael Chan 
939c0c050c5SMichael Chan fltr_err:
940c0c050c5SMichael Chan 	rcu_read_unlock();
941c0c050c5SMichael Chan 
942c0c050c5SMichael Chan 	return rc;
943c0c050c5SMichael Chan }
944a011952aSMichael Chan #endif
945a011952aSMichael Chan 
946a011952aSMichael Chan static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
947a011952aSMichael Chan {
948a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
949a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
950a011952aSMichael Chan 	return 0;
951a011952aSMichael Chan }
952a011952aSMichael Chan 
953a011952aSMichael Chan static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
954a011952aSMichael Chan {
955a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
956a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
957a011952aSMichael Chan 	return 0;
958a011952aSMichael Chan }
959a011952aSMichael Chan 
960a011952aSMichael Chan static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
961a011952aSMichael Chan {
962a011952aSMichael Chan 	cmd->data = 0;
963a011952aSMichael Chan 	switch (cmd->flow_type) {
964a011952aSMichael Chan 	case TCP_V4_FLOW:
965a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
966a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
967a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
968a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
969a011952aSMichael Chan 		break;
970a011952aSMichael Chan 	case UDP_V4_FLOW:
971a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
972a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
973a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
974a011952aSMichael Chan 		/* fall through */
975a011952aSMichael Chan 	case SCTP_V4_FLOW:
976a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
977a011952aSMichael Chan 	case AH_V4_FLOW:
978a011952aSMichael Chan 	case ESP_V4_FLOW:
979a011952aSMichael Chan 	case IPV4_FLOW:
980a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
981a011952aSMichael Chan 		break;
982a011952aSMichael Chan 
983a011952aSMichael Chan 	case TCP_V6_FLOW:
984a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
985a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
986a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
987a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
988a011952aSMichael Chan 		break;
989a011952aSMichael Chan 	case UDP_V6_FLOW:
990a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
991a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
992a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
993a011952aSMichael Chan 		/* fall through */
994a011952aSMichael Chan 	case SCTP_V6_FLOW:
995a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
996a011952aSMichael Chan 	case AH_V6_FLOW:
997a011952aSMichael Chan 	case ESP_V6_FLOW:
998a011952aSMichael Chan 	case IPV6_FLOW:
999a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1000a011952aSMichael Chan 		break;
1001a011952aSMichael Chan 	}
1002a011952aSMichael Chan 	return 0;
1003a011952aSMichael Chan }
1004a011952aSMichael Chan 
1005a011952aSMichael Chan #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1006a011952aSMichael Chan #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
1007a011952aSMichael Chan 
1008a011952aSMichael Chan static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1009a011952aSMichael Chan {
1010a011952aSMichael Chan 	u32 rss_hash_cfg = bp->rss_hash_cfg;
1011a011952aSMichael Chan 	int tuple, rc = 0;
1012a011952aSMichael Chan 
1013a011952aSMichael Chan 	if (cmd->data == RXH_4TUPLE)
1014a011952aSMichael Chan 		tuple = 4;
1015a011952aSMichael Chan 	else if (cmd->data == RXH_2TUPLE)
1016a011952aSMichael Chan 		tuple = 2;
1017a011952aSMichael Chan 	else if (!cmd->data)
1018a011952aSMichael Chan 		tuple = 0;
1019a011952aSMichael Chan 	else
1020a011952aSMichael Chan 		return -EINVAL;
1021a011952aSMichael Chan 
1022a011952aSMichael Chan 	if (cmd->flow_type == TCP_V4_FLOW) {
1023a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1024a011952aSMichael Chan 		if (tuple == 4)
1025a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1026a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V4_FLOW) {
1027a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1028a011952aSMichael Chan 			return -EINVAL;
1029a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1030a011952aSMichael Chan 		if (tuple == 4)
1031a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1032a011952aSMichael Chan 	} else if (cmd->flow_type == TCP_V6_FLOW) {
1033a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1034a011952aSMichael Chan 		if (tuple == 4)
1035a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1036a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V6_FLOW) {
1037a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1038a011952aSMichael Chan 			return -EINVAL;
1039a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1040a011952aSMichael Chan 		if (tuple == 4)
1041a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1042a011952aSMichael Chan 	} else if (tuple == 4) {
1043a011952aSMichael Chan 		return -EINVAL;
1044a011952aSMichael Chan 	}
1045a011952aSMichael Chan 
1046a011952aSMichael Chan 	switch (cmd->flow_type) {
1047a011952aSMichael Chan 	case TCP_V4_FLOW:
1048a011952aSMichael Chan 	case UDP_V4_FLOW:
1049a011952aSMichael Chan 	case SCTP_V4_FLOW:
1050a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1051a011952aSMichael Chan 	case AH_V4_FLOW:
1052a011952aSMichael Chan 	case ESP_V4_FLOW:
1053a011952aSMichael Chan 	case IPV4_FLOW:
1054a011952aSMichael Chan 		if (tuple == 2)
1055a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1056a011952aSMichael Chan 		else if (!tuple)
1057a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1058a011952aSMichael Chan 		break;
1059a011952aSMichael Chan 
1060a011952aSMichael Chan 	case TCP_V6_FLOW:
1061a011952aSMichael Chan 	case UDP_V6_FLOW:
1062a011952aSMichael Chan 	case SCTP_V6_FLOW:
1063a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1064a011952aSMichael Chan 	case AH_V6_FLOW:
1065a011952aSMichael Chan 	case ESP_V6_FLOW:
1066a011952aSMichael Chan 	case IPV6_FLOW:
1067a011952aSMichael Chan 		if (tuple == 2)
1068a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1069a011952aSMichael Chan 		else if (!tuple)
1070a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1071a011952aSMichael Chan 		break;
1072a011952aSMichael Chan 	}
1073a011952aSMichael Chan 
1074a011952aSMichael Chan 	if (bp->rss_hash_cfg == rss_hash_cfg)
1075a011952aSMichael Chan 		return 0;
1076a011952aSMichael Chan 
1077a011952aSMichael Chan 	bp->rss_hash_cfg = rss_hash_cfg;
1078a011952aSMichael Chan 	if (netif_running(bp->dev)) {
1079a011952aSMichael Chan 		bnxt_close_nic(bp, false, false);
1080a011952aSMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1081a011952aSMichael Chan 	}
1082a011952aSMichael Chan 	return rc;
1083a011952aSMichael Chan }
1084c0c050c5SMichael Chan 
1085c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1086c0c050c5SMichael Chan 			  u32 *rule_locs)
1087c0c050c5SMichael Chan {
1088c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1089c0c050c5SMichael Chan 	int rc = 0;
1090c0c050c5SMichael Chan 
1091c0c050c5SMichael Chan 	switch (cmd->cmd) {
1092a011952aSMichael Chan #ifdef CONFIG_RFS_ACCEL
1093c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
1094c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
1095c0c050c5SMichael Chan 		break;
1096c0c050c5SMichael Chan 
1097c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
1098c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
1099c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
1100c0c050c5SMichael Chan 		break;
1101c0c050c5SMichael Chan 
1102c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
1103c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
1104c0c050c5SMichael Chan 		break;
1105c0c050c5SMichael Chan 
1106c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
1107c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
1108c0c050c5SMichael Chan 		break;
1109a011952aSMichael Chan #endif
1110a011952aSMichael Chan 
1111a011952aSMichael Chan 	case ETHTOOL_GRXFH:
1112a011952aSMichael Chan 		rc = bnxt_grxfh(bp, cmd);
1113a011952aSMichael Chan 		break;
1114c0c050c5SMichael Chan 
1115c0c050c5SMichael Chan 	default:
1116c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
1117c0c050c5SMichael Chan 		break;
1118c0c050c5SMichael Chan 	}
1119c0c050c5SMichael Chan 
1120c0c050c5SMichael Chan 	return rc;
1121c0c050c5SMichael Chan }
1122a011952aSMichael Chan 
1123a011952aSMichael Chan static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1124a011952aSMichael Chan {
1125a011952aSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1126a011952aSMichael Chan 	int rc;
1127a011952aSMichael Chan 
1128a011952aSMichael Chan 	switch (cmd->cmd) {
1129a011952aSMichael Chan 	case ETHTOOL_SRXFH:
1130a011952aSMichael Chan 		rc = bnxt_srxfh(bp, cmd);
1131a011952aSMichael Chan 		break;
1132a011952aSMichael Chan 
1133a011952aSMichael Chan 	default:
1134a011952aSMichael Chan 		rc = -EOPNOTSUPP;
1135a011952aSMichael Chan 		break;
1136a011952aSMichael Chan 	}
1137a011952aSMichael Chan 	return rc;
1138a011952aSMichael Chan }
1139c0c050c5SMichael Chan 
1140c0c050c5SMichael Chan static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
1141c0c050c5SMichael Chan {
1142c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
1143c0c050c5SMichael Chan }
1144c0c050c5SMichael Chan 
1145c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
1146c0c050c5SMichael Chan {
1147c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
1148c0c050c5SMichael Chan }
1149c0c050c5SMichael Chan 
1150c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1151c0c050c5SMichael Chan 			 u8 *hfunc)
1152c0c050c5SMichael Chan {
1153c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
11547991cb9cSMichael Chan 	struct bnxt_vnic_info *vnic;
1155c0c050c5SMichael Chan 	int i = 0;
1156c0c050c5SMichael Chan 
1157c0c050c5SMichael Chan 	if (hfunc)
1158c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
1159c0c050c5SMichael Chan 
11607991cb9cSMichael Chan 	if (!bp->vnic_info)
11617991cb9cSMichael Chan 		return 0;
11627991cb9cSMichael Chan 
11637991cb9cSMichael Chan 	vnic = &bp->vnic_info[0];
11647991cb9cSMichael Chan 	if (indir && vnic->rss_table) {
1165c0c050c5SMichael Chan 		for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
1166c0c050c5SMichael Chan 			indir[i] = le16_to_cpu(vnic->rss_table[i]);
11677991cb9cSMichael Chan 	}
1168c0c050c5SMichael Chan 
11697991cb9cSMichael Chan 	if (key && vnic->rss_hash_key)
1170c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
1171c0c050c5SMichael Chan 
1172c0c050c5SMichael Chan 	return 0;
1173c0c050c5SMichael Chan }
1174c0c050c5SMichael Chan 
1175c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
1176c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
1177c0c050c5SMichael Chan {
1178c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1179c0c050c5SMichael Chan 
1180c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1181c0c050c5SMichael Chan 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
1182431aa1ebSMichael Chan 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
1183c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
11845c8227d0SMichael Chan 	info->n_stats = bnxt_get_num_stats(bp);
1185eb513658SMichael Chan 	info->testinfo_len = bp->num_tests;
1186c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
1187c0c050c5SMichael Chan 	info->eedump_len = 0;
1188c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
1189c0c050c5SMichael Chan 	info->regdump_len = 0;
1190c0c050c5SMichael Chan }
1191c0c050c5SMichael Chan 
11928e202366SMichael Chan static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11938e202366SMichael Chan {
11948e202366SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
11958e202366SMichael Chan 
11968e202366SMichael Chan 	wol->supported = 0;
11978e202366SMichael Chan 	wol->wolopts = 0;
11988e202366SMichael Chan 	memset(&wol->sopass, 0, sizeof(wol->sopass));
11998e202366SMichael Chan 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
12008e202366SMichael Chan 		wol->supported = WAKE_MAGIC;
12018e202366SMichael Chan 		if (bp->wol)
12028e202366SMichael Chan 			wol->wolopts = WAKE_MAGIC;
12038e202366SMichael Chan 	}
12048e202366SMichael Chan }
12058e202366SMichael Chan 
12065282db6cSMichael Chan static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12075282db6cSMichael Chan {
12085282db6cSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
12095282db6cSMichael Chan 
12105282db6cSMichael Chan 	if (wol->wolopts & ~WAKE_MAGIC)
12115282db6cSMichael Chan 		return -EINVAL;
12125282db6cSMichael Chan 
12135282db6cSMichael Chan 	if (wol->wolopts & WAKE_MAGIC) {
12145282db6cSMichael Chan 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
12155282db6cSMichael Chan 			return -EINVAL;
12165282db6cSMichael Chan 		if (!bp->wol) {
12175282db6cSMichael Chan 			if (bnxt_hwrm_alloc_wol_fltr(bp))
12185282db6cSMichael Chan 				return -EBUSY;
12195282db6cSMichael Chan 			bp->wol = 1;
12205282db6cSMichael Chan 		}
12215282db6cSMichael Chan 	} else {
12225282db6cSMichael Chan 		if (bp->wol) {
12235282db6cSMichael Chan 			if (bnxt_hwrm_free_wol_fltr(bp))
12245282db6cSMichael Chan 				return -EBUSY;
12255282db6cSMichael Chan 			bp->wol = 0;
12265282db6cSMichael Chan 		}
12275282db6cSMichael Chan 	}
12285282db6cSMichael Chan 	return 0;
12295282db6cSMichael Chan }
12305282db6cSMichael Chan 
1231170ce013SMichael Chan u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
1232c0c050c5SMichael Chan {
1233c0c050c5SMichael Chan 	u32 speed_mask = 0;
1234c0c050c5SMichael Chan 
1235c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
1236c0c050c5SMichael Chan 	/* set the advertised speeds */
1237c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1238c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
1239c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1240c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
1241c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1242c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
1243c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1244c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
1245c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
12461c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
124727c4d578SMichael Chan 
124827c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
124927c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
125027c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
125127c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
125227c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
125327c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
125427c4d578SMichael Chan 
1255c0c050c5SMichael Chan 	return speed_mask;
1256c0c050c5SMichael Chan }
1257c0c050c5SMichael Chan 
125800c04a92SMichael Chan #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
125900c04a92SMichael Chan {									\
126000c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
126100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
126200c04a92SMichael Chan 						     100baseT_Full);	\
126300c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
126400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
126500c04a92SMichael Chan 						     1000baseT_Full);	\
126600c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
126700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
126800c04a92SMichael Chan 						     10000baseT_Full);	\
126900c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
127000c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
127100c04a92SMichael Chan 						     25000baseCR_Full);	\
127200c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
127300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
127400c04a92SMichael Chan 						     40000baseCR4_Full);\
127500c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
127600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
127700c04a92SMichael Chan 						     50000baseCR2_Full);\
127838a21b34SDeepak Khungar 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
127938a21b34SDeepak Khungar 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
128038a21b34SDeepak Khungar 						     100000baseCR4_Full);\
128100c04a92SMichael Chan 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
128200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
128300c04a92SMichael Chan 						     Pause);		\
128400c04a92SMichael Chan 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
128500c04a92SMichael Chan 			ethtool_link_ksettings_add_link_mode(		\
128600c04a92SMichael Chan 					lk_ksettings, name, Asym_Pause);\
128700c04a92SMichael Chan 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
128800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
128900c04a92SMichael Chan 						     Asym_Pause);	\
129000c04a92SMichael Chan 	}								\
129100c04a92SMichael Chan }
129200c04a92SMichael Chan 
129300c04a92SMichael Chan #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
129400c04a92SMichael Chan {									\
129500c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
129600c04a92SMichael Chan 						  100baseT_Full) ||	\
129700c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
129800c04a92SMichael Chan 						  100baseT_Half))	\
129900c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
130000c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
130100c04a92SMichael Chan 						  1000baseT_Full) ||	\
130200c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
130300c04a92SMichael Chan 						  1000baseT_Half))	\
130400c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
130500c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
130600c04a92SMichael Chan 						  10000baseT_Full))	\
130700c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
130800c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
130900c04a92SMichael Chan 						  25000baseCR_Full))	\
131000c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
131100c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
131200c04a92SMichael Chan 						  40000baseCR4_Full))	\
131300c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
131400c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
131500c04a92SMichael Chan 						  50000baseCR2_Full))	\
131600c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
131738a21b34SDeepak Khungar 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
131838a21b34SDeepak Khungar 						  100000baseCR4_Full))	\
131938a21b34SDeepak Khungar 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
132000c04a92SMichael Chan }
132100c04a92SMichael Chan 
132200c04a92SMichael Chan static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
132300c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
132427c4d578SMichael Chan {
132568515a18SMichael Chan 	u16 fw_speeds = link_info->advertising;
132627c4d578SMichael Chan 	u8 fw_pause = 0;
132727c4d578SMichael Chan 
132827c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
132927c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
133027c4d578SMichael Chan 
133100c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
133227c4d578SMichael Chan }
133327c4d578SMichael Chan 
133400c04a92SMichael Chan static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
133500c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
13363277360eSMichael Chan {
13373277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
13383277360eSMichael Chan 	u8 fw_pause = 0;
13393277360eSMichael Chan 
13403277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13413277360eSMichael Chan 		fw_pause = link_info->lp_pause;
13423277360eSMichael Chan 
134300c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
134400c04a92SMichael Chan 				lp_advertising);
13453277360eSMichael Chan }
13463277360eSMichael Chan 
134700c04a92SMichael Chan static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
134800c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
13494b32caccSMichael Chan {
13504b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
13514b32caccSMichael Chan 
135200c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
13534b32caccSMichael Chan 
135400c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
135500c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
135600c04a92SMichael Chan 					     Asym_Pause);
135793ed8117SMichael Chan 
135800c04a92SMichael Chan 	if (link_info->support_auto_speeds)
135900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
136000c04a92SMichael Chan 						     Autoneg);
136193ed8117SMichael Chan }
136293ed8117SMichael Chan 
1363c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1364c0c050c5SMichael Chan {
1365c0c050c5SMichael Chan 	switch (fw_link_speed) {
1366c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
1367c0c050c5SMichael Chan 		return SPEED_100;
1368c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
1369c0c050c5SMichael Chan 		return SPEED_1000;
1370c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
1371c0c050c5SMichael Chan 		return SPEED_2500;
1372c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
1373c0c050c5SMichael Chan 		return SPEED_10000;
1374c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
1375c0c050c5SMichael Chan 		return SPEED_20000;
1376c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
1377c0c050c5SMichael Chan 		return SPEED_25000;
1378c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
1379c0c050c5SMichael Chan 		return SPEED_40000;
1380c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
1381c0c050c5SMichael Chan 		return SPEED_50000;
138238a21b34SDeepak Khungar 	case BNXT_LINK_SPEED_100GB:
138338a21b34SDeepak Khungar 		return SPEED_100000;
1384c0c050c5SMichael Chan 	default:
1385c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
1386c0c050c5SMichael Chan 	}
1387c0c050c5SMichael Chan }
1388c0c050c5SMichael Chan 
138900c04a92SMichael Chan static int bnxt_get_link_ksettings(struct net_device *dev,
139000c04a92SMichael Chan 				   struct ethtool_link_ksettings *lk_ksettings)
1391c0c050c5SMichael Chan {
1392c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1393c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
139400c04a92SMichael Chan 	struct ethtool_link_settings *base = &lk_ksettings->base;
139500c04a92SMichael Chan 	u32 ethtool_speed;
1396c0c050c5SMichael Chan 
139700c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1398e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
139900c04a92SMichael Chan 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1400c0c050c5SMichael Chan 
140100c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1402b763499eSMichael Chan 	if (link_info->autoneg) {
140300c04a92SMichael Chan 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
140400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
140500c04a92SMichael Chan 						     advertising, Autoneg);
140600c04a92SMichael Chan 		base->autoneg = AUTONEG_ENABLE;
14073277360eSMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK)
140800c04a92SMichael Chan 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
140929c262feSMichael Chan 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
141029c262feSMichael Chan 		if (!netif_carrier_ok(dev))
141100c04a92SMichael Chan 			base->duplex = DUPLEX_UNKNOWN;
141229c262feSMichael Chan 		else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
141300c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
141429c262feSMichael Chan 		else
141500c04a92SMichael Chan 			base->duplex = DUPLEX_HALF;
1416c0c050c5SMichael Chan 	} else {
141700c04a92SMichael Chan 		base->autoneg = AUTONEG_DISABLE;
141829c262feSMichael Chan 		ethtool_speed =
141929c262feSMichael Chan 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
142000c04a92SMichael Chan 		base->duplex = DUPLEX_HALF;
142129c262feSMichael Chan 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
142200c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
1423c0c050c5SMichael Chan 	}
142400c04a92SMichael Chan 	base->speed = ethtool_speed;
1425c0c050c5SMichael Chan 
142600c04a92SMichael Chan 	base->port = PORT_NONE;
1427c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
142800c04a92SMichael Chan 		base->port = PORT_TP;
142900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
143000c04a92SMichael Chan 						     TP);
143100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
143200c04a92SMichael Chan 						     TP);
1433c0c050c5SMichael Chan 	} else {
143400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
143500c04a92SMichael Chan 						     FIBRE);
143600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
143700c04a92SMichael Chan 						     FIBRE);
1438c0c050c5SMichael Chan 
1439c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
144000c04a92SMichael Chan 			base->port = PORT_DA;
1441c0c050c5SMichael Chan 		else if (link_info->media_type ==
1442c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
144300c04a92SMichael Chan 			base->port = PORT_FIBRE;
1444c0c050c5SMichael Chan 	}
144500c04a92SMichael Chan 	base->phy_address = link_info->phy_addr;
1446e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1447c0c050c5SMichael Chan 
1448c0c050c5SMichael Chan 	return 0;
1449c0c050c5SMichael Chan }
1450c0c050c5SMichael Chan 
145138a21b34SDeepak Khungar static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed)
1452c0c050c5SMichael Chan {
14539d9cee08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
14549d9cee08SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
14559d9cee08SMichael Chan 	u16 support_spds = link_info->support_speeds;
14569d9cee08SMichael Chan 	u32 fw_speed = 0;
14579d9cee08SMichael Chan 
1458c0c050c5SMichael Chan 	switch (ethtool_speed) {
1459c0c050c5SMichael Chan 	case SPEED_100:
14609d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
14619d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
14629d9cee08SMichael Chan 		break;
1463c0c050c5SMichael Chan 	case SPEED_1000:
14649d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
14659d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
14669d9cee08SMichael Chan 		break;
1467c0c050c5SMichael Chan 	case SPEED_2500:
14689d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
14699d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
14709d9cee08SMichael Chan 		break;
1471c0c050c5SMichael Chan 	case SPEED_10000:
14729d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
14739d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
14749d9cee08SMichael Chan 		break;
1475c0c050c5SMichael Chan 	case SPEED_20000:
14769d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
14779d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
14789d9cee08SMichael Chan 		break;
1479c0c050c5SMichael Chan 	case SPEED_25000:
14809d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
14819d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
14829d9cee08SMichael Chan 		break;
1483c0c050c5SMichael Chan 	case SPEED_40000:
14849d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
14859d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
14869d9cee08SMichael Chan 		break;
1487c0c050c5SMichael Chan 	case SPEED_50000:
14889d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
14899d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
14909d9cee08SMichael Chan 		break;
149138a21b34SDeepak Khungar 	case SPEED_100000:
149238a21b34SDeepak Khungar 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB)
149338a21b34SDeepak Khungar 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB;
149438a21b34SDeepak Khungar 		break;
1495c0c050c5SMichael Chan 	default:
1496c0c050c5SMichael Chan 		netdev_err(dev, "unsupported speed!\n");
1497c0c050c5SMichael Chan 		break;
1498c0c050c5SMichael Chan 	}
14999d9cee08SMichael Chan 	return fw_speed;
1500c0c050c5SMichael Chan }
1501c0c050c5SMichael Chan 
1502939f7f0cSMichael Chan u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1503c0c050c5SMichael Chan {
1504c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
1505c0c050c5SMichael Chan 
1506c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
1507c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
1508c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
1509c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1510c0c050c5SMichael Chan 	}
1511c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
1512c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
1513c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1514c0c050c5SMichael Chan 	}
1515c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
1516c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1517c0c050c5SMichael Chan 
15181c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
15191c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
15201c49c421SMichael Chan 
1521c0c050c5SMichael Chan 	return fw_speed_mask;
1522c0c050c5SMichael Chan }
1523c0c050c5SMichael Chan 
152400c04a92SMichael Chan static int bnxt_set_link_ksettings(struct net_device *dev,
152500c04a92SMichael Chan 			   const struct ethtool_link_ksettings *lk_ksettings)
1526c0c050c5SMichael Chan {
1527c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1528c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
152900c04a92SMichael Chan 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1530c0c050c5SMichael Chan 	bool set_pause = false;
153168515a18SMichael Chan 	u16 fw_advertising = 0;
153268515a18SMichael Chan 	u32 speed;
153300c04a92SMichael Chan 	int rc = 0;
1534c0c050c5SMichael Chan 
1535567b2abeSSatish Baddipadige 	if (!BNXT_SINGLE_PF(bp))
153600c04a92SMichael Chan 		return -EOPNOTSUPP;
1537c0c050c5SMichael Chan 
1538e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
153900c04a92SMichael Chan 	if (base->autoneg == AUTONEG_ENABLE) {
154000c04a92SMichael Chan 		BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
154100c04a92SMichael Chan 					advertising);
1542c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1543c0c050c5SMichael Chan 		if (!fw_advertising)
154493ed8117SMichael Chan 			link_info->advertising = link_info->support_auto_speeds;
1545c0c050c5SMichael Chan 		else
1546c0c050c5SMichael Chan 			link_info->advertising = fw_advertising;
1547c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
1548c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
1549c0c050c5SMichael Chan 		 */
1550c0c050c5SMichael Chan 		set_pause = true;
1551c0c050c5SMichael Chan 	} else {
15529d9cee08SMichael Chan 		u16 fw_speed;
155303efbec0SMichael Chan 		u8 phy_type = link_info->phy_type;
15549d9cee08SMichael Chan 
155503efbec0SMichael Chan 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
155603efbec0SMichael Chan 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
155703efbec0SMichael Chan 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
155803efbec0SMichael Chan 			netdev_err(dev, "10GBase-T devices must autoneg\n");
155903efbec0SMichael Chan 			rc = -EINVAL;
156003efbec0SMichael Chan 			goto set_setting_exit;
156103efbec0SMichael Chan 		}
156200c04a92SMichael Chan 		if (base->duplex == DUPLEX_HALF) {
1563c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1564c0c050c5SMichael Chan 			rc = -EINVAL;
1565c0c050c5SMichael Chan 			goto set_setting_exit;
1566c0c050c5SMichael Chan 		}
156700c04a92SMichael Chan 		speed = base->speed;
15689d9cee08SMichael Chan 		fw_speed = bnxt_get_fw_speed(dev, speed);
15699d9cee08SMichael Chan 		if (!fw_speed) {
15709d9cee08SMichael Chan 			rc = -EINVAL;
15719d9cee08SMichael Chan 			goto set_setting_exit;
15729d9cee08SMichael Chan 		}
15739d9cee08SMichael Chan 		link_info->req_link_speed = fw_speed;
1574c0c050c5SMichael Chan 		link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1575b763499eSMichael Chan 		link_info->autoneg = 0;
1576c0c050c5SMichael Chan 		link_info->advertising = 0;
1577c0c050c5SMichael Chan 	}
1578c0c050c5SMichael Chan 
1579c0c050c5SMichael Chan 	if (netif_running(dev))
1580939f7f0cSMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1581c0c050c5SMichael Chan 
1582c0c050c5SMichael Chan set_setting_exit:
1583e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1584c0c050c5SMichael Chan 	return rc;
1585c0c050c5SMichael Chan }
1586c0c050c5SMichael Chan 
1587c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
1588c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
1589c0c050c5SMichael Chan {
1590c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1591c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1592c0c050c5SMichael Chan 
1593c0c050c5SMichael Chan 	if (BNXT_VF(bp))
1594c0c050c5SMichael Chan 		return;
1595b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
15963c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
15973c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1598c0c050c5SMichael Chan }
1599c0c050c5SMichael Chan 
1600c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
1601c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
1602c0c050c5SMichael Chan {
1603c0c050c5SMichael Chan 	int rc = 0;
1604c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1605c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1606c0c050c5SMichael Chan 
1607567b2abeSSatish Baddipadige 	if (!BNXT_SINGLE_PF(bp))
160875362a3fSMichael Chan 		return -EOPNOTSUPP;
1609c0c050c5SMichael Chan 
1610c0c050c5SMichael Chan 	if (epause->autoneg) {
1611b763499eSMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
1612b763499eSMichael Chan 			return -EINVAL;
1613b763499eSMichael Chan 
1614c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1615c9ee9516SMichael Chan 		if (bp->hwrm_spec_code >= 0x10201)
1616c9ee9516SMichael Chan 			link_info->req_flow_ctrl =
1617c9ee9516SMichael Chan 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1618c0c050c5SMichael Chan 	} else {
1619c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
1620c0c050c5SMichael Chan 		 * force a link change
1621c0c050c5SMichael Chan 		 */
1622c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1623c0c050c5SMichael Chan 			link_info->force_link_chng = true;
1624c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1625c9ee9516SMichael Chan 		link_info->req_flow_ctrl = 0;
1626c0c050c5SMichael Chan 	}
1627c0c050c5SMichael Chan 	if (epause->rx_pause)
1628c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1629c0c050c5SMichael Chan 
1630c0c050c5SMichael Chan 	if (epause->tx_pause)
1631c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1632c0c050c5SMichael Chan 
1633c0c050c5SMichael Chan 	if (netif_running(dev))
1634c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
1635c0c050c5SMichael Chan 	return rc;
1636c0c050c5SMichael Chan }
1637c0c050c5SMichael Chan 
1638c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
1639c0c050c5SMichael Chan {
1640c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1641c0c050c5SMichael Chan 
1642c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
1643c0c050c5SMichael Chan 	return bp->link_info.link_up;
1644c0c050c5SMichael Chan }
1645c0c050c5SMichael Chan 
16465ac67d8bSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
16475ac67d8bSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
16485ac67d8bSRob Swindell 				u32 *data_length);
16495ac67d8bSRob Swindell 
1650c0c050c5SMichael Chan static int bnxt_flash_nvram(struct net_device *dev,
1651c0c050c5SMichael Chan 			    u16 dir_type,
1652c0c050c5SMichael Chan 			    u16 dir_ordinal,
1653c0c050c5SMichael Chan 			    u16 dir_ext,
1654c0c050c5SMichael Chan 			    u16 dir_attr,
1655c0c050c5SMichael Chan 			    const u8 *data,
1656c0c050c5SMichael Chan 			    size_t data_len)
1657c0c050c5SMichael Chan {
1658c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1659c0c050c5SMichael Chan 	int rc;
1660c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
1661c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1662c0c050c5SMichael Chan 	u8 *kmem;
1663c0c050c5SMichael Chan 
1664c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1665c0c050c5SMichael Chan 
1666c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
1667c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
1668c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
1669c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
1670c0c050c5SMichael Chan 	req.dir_data_length = cpu_to_le32(data_len);
1671c0c050c5SMichael Chan 
1672c0c050c5SMichael Chan 	kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1673c0c050c5SMichael Chan 				  GFP_KERNEL);
1674c0c050c5SMichael Chan 	if (!kmem) {
1675c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1676c0c050c5SMichael Chan 			   (unsigned)data_len);
1677c0c050c5SMichael Chan 		return -ENOMEM;
1678c0c050c5SMichael Chan 	}
1679c0c050c5SMichael Chan 	memcpy(kmem, data, data_len);
1680c0c050c5SMichael Chan 	req.host_src_addr = cpu_to_le64(dma_handle);
1681c0c050c5SMichael Chan 
1682c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1683c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1684c0c050c5SMichael Chan 
16857c675421SVasundhara Volam 	if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) {
16867c675421SVasundhara Volam 		netdev_info(dev,
16877c675421SVasundhara Volam 			    "PF does not have admin privileges to flash the device\n");
16887c675421SVasundhara Volam 		rc = -EACCES;
16897c675421SVasundhara Volam 	} else if (rc) {
16907c675421SVasundhara Volam 		rc = -EIO;
16917c675421SVasundhara Volam 	}
1692c0c050c5SMichael Chan 	return rc;
1693c0c050c5SMichael Chan }
1694c0c050c5SMichael Chan 
1695d2d6318cSRob Swindell static int bnxt_firmware_reset(struct net_device *dev,
1696d2d6318cSRob Swindell 			       u16 dir_type)
1697d2d6318cSRob Swindell {
1698d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
16997c675421SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
17007c675421SVasundhara Volam 	int rc;
1701d2d6318cSRob Swindell 
1702d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1703d2d6318cSRob Swindell 
1704d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1705d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
1706d2d6318cSRob Swindell 	switch (dir_type) {
1707d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
1708d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
1709d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
1710d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1711d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
1712d2d6318cSRob Swindell 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1713d2d6318cSRob Swindell 		break;
1714d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
1715d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
1716d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
171708141e0bSRob Swindell 		/* Self-reset APE upon next PCIe reset: */
171808141e0bSRob Swindell 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1719d2d6318cSRob Swindell 		break;
1720d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
1721d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
1722d2d6318cSRob Swindell 		req.embedded_proc_type =
1723d2d6318cSRob Swindell 			FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1724d2d6318cSRob Swindell 		break;
1725d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
1726d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
1727d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1728d2d6318cSRob Swindell 		break;
172949f7972fSVasundhara Volam 	case BNXT_FW_RESET_CHIP:
173049f7972fSVasundhara Volam 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
173149f7972fSVasundhara Volam 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
173249f7972fSVasundhara Volam 		break;
17336502ad59SScott Branden 	case BNXT_FW_RESET_AP:
17346502ad59SScott Branden 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP;
17356502ad59SScott Branden 		break;
1736d2d6318cSRob Swindell 	default:
1737d2d6318cSRob Swindell 		return -EINVAL;
1738d2d6318cSRob Swindell 	}
1739d2d6318cSRob Swindell 
17407c675421SVasundhara Volam 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
17417c675421SVasundhara Volam 	if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) {
17427c675421SVasundhara Volam 		netdev_info(dev,
17437c675421SVasundhara Volam 			    "PF does not have admin privileges to reset the device\n");
17447c675421SVasundhara Volam 		rc = -EACCES;
17457c675421SVasundhara Volam 	} else if (rc) {
17467c675421SVasundhara Volam 		rc = -EIO;
17477c675421SVasundhara Volam 	}
17487c675421SVasundhara Volam 	return rc;
1749d2d6318cSRob Swindell }
1750d2d6318cSRob Swindell 
1751c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
1752c0c050c5SMichael Chan 			       u16 dir_type,
1753c0c050c5SMichael Chan 			       const u8 *fw_data,
1754c0c050c5SMichael Chan 			       size_t fw_size)
1755c0c050c5SMichael Chan {
1756c0c050c5SMichael Chan 	int	rc = 0;
1757c0c050c5SMichael Chan 	u16	code_type;
1758c0c050c5SMichael Chan 	u32	stored_crc;
1759c0c050c5SMichael Chan 	u32	calculated_crc;
1760c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1761c0c050c5SMichael Chan 
1762c0c050c5SMichael Chan 	switch (dir_type) {
1763c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1764c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1765c0c050c5SMichael Chan 		code_type = CODE_BOOT;
1766c0c050c5SMichael Chan 		break;
176793e0b4feSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
176893e0b4feSRob Swindell 		code_type = CODE_CHIMP_PATCH;
176993e0b4feSRob Swindell 		break;
17702731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
17712731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
17722731d70fSRob Swindell 		break;
177393e0b4feSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
177493e0b4feSRob Swindell 		code_type = CODE_APE_PATCH;
177593e0b4feSRob Swindell 		break;
177693e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
177793e0b4feSRob Swindell 		code_type = CODE_KONG_FW;
177893e0b4feSRob Swindell 		break;
177993e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
178093e0b4feSRob Swindell 		code_type = CODE_KONG_PATCH;
178193e0b4feSRob Swindell 		break;
178293e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
178393e0b4feSRob Swindell 		code_type = CODE_BONO_FW;
178493e0b4feSRob Swindell 		break;
178593e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
178693e0b4feSRob Swindell 		code_type = CODE_BONO_PATCH;
178793e0b4feSRob Swindell 		break;
1788c0c050c5SMichael Chan 	default:
1789c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
1790c0c050c5SMichael Chan 			   dir_type);
1791c0c050c5SMichael Chan 		return -EINVAL;
1792c0c050c5SMichael Chan 	}
1793c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
1794c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
1795c0c050c5SMichael Chan 			   (unsigned int)fw_size);
1796c0c050c5SMichael Chan 		return -EINVAL;
1797c0c050c5SMichael Chan 	}
1798c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1799c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
1800c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
1801c0c050c5SMichael Chan 		return -EINVAL;
1802c0c050c5SMichael Chan 	}
1803c0c050c5SMichael Chan 	if (header->code_type != code_type) {
1804c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1805c0c050c5SMichael Chan 			   code_type, header->code_type);
1806c0c050c5SMichael Chan 		return -EINVAL;
1807c0c050c5SMichael Chan 	}
1808c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
1809c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1810c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
1811c0c050c5SMichael Chan 		return -EINVAL;
1812c0c050c5SMichael Chan 	}
1813c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
1814c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1815c0c050c5SMichael Chan 					     sizeof(stored_crc)));
1816c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1817c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
1818c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1819c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
1820c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
1821c0c050c5SMichael Chan 		return -EINVAL;
1822c0c050c5SMichael Chan 	}
1823c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1824c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
1825d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
1826d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
1827d2d6318cSRob Swindell 
1828c0c050c5SMichael Chan 	return rc;
1829c0c050c5SMichael Chan }
1830c0c050c5SMichael Chan 
18315ac67d8bSRob Swindell static int bnxt_flash_microcode(struct net_device *dev,
18325ac67d8bSRob Swindell 				u16 dir_type,
18335ac67d8bSRob Swindell 				const u8 *fw_data,
18345ac67d8bSRob Swindell 				size_t fw_size)
18355ac67d8bSRob Swindell {
18365ac67d8bSRob Swindell 	struct bnxt_ucode_trailer *trailer;
18375ac67d8bSRob Swindell 	u32 calculated_crc;
18385ac67d8bSRob Swindell 	u32 stored_crc;
18395ac67d8bSRob Swindell 	int rc = 0;
18405ac67d8bSRob Swindell 
18415ac67d8bSRob Swindell 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
18425ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode file size: %u\n",
18435ac67d8bSRob Swindell 			   (unsigned int)fw_size);
18445ac67d8bSRob Swindell 		return -EINVAL;
18455ac67d8bSRob Swindell 	}
18465ac67d8bSRob Swindell 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
18475ac67d8bSRob Swindell 						sizeof(*trailer)));
18485ac67d8bSRob Swindell 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
18495ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
18505ac67d8bSRob Swindell 			   le32_to_cpu(trailer->sig));
18515ac67d8bSRob Swindell 		return -EINVAL;
18525ac67d8bSRob Swindell 	}
18535ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
18545ac67d8bSRob Swindell 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
18555ac67d8bSRob Swindell 			   dir_type, le16_to_cpu(trailer->dir_type));
18565ac67d8bSRob Swindell 		return -EINVAL;
18575ac67d8bSRob Swindell 	}
18585ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->trailer_length) <
18595ac67d8bSRob Swindell 		sizeof(struct bnxt_ucode_trailer)) {
18605ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
18615ac67d8bSRob Swindell 			   le16_to_cpu(trailer->trailer_length));
18625ac67d8bSRob Swindell 		return -EINVAL;
18635ac67d8bSRob Swindell 	}
18645ac67d8bSRob Swindell 
18655ac67d8bSRob Swindell 	/* Confirm the CRC32 checksum of the file: */
18665ac67d8bSRob Swindell 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
18675ac67d8bSRob Swindell 					     sizeof(stored_crc)));
18685ac67d8bSRob Swindell 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
18695ac67d8bSRob Swindell 	if (calculated_crc != stored_crc) {
18705ac67d8bSRob Swindell 		netdev_err(dev,
18715ac67d8bSRob Swindell 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
18725ac67d8bSRob Swindell 			   (unsigned long)stored_crc,
18735ac67d8bSRob Swindell 			   (unsigned long)calculated_crc);
18745ac67d8bSRob Swindell 		return -EINVAL;
18755ac67d8bSRob Swindell 	}
18765ac67d8bSRob Swindell 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
18775ac67d8bSRob Swindell 			      0, 0, fw_data, fw_size);
18785ac67d8bSRob Swindell 
18795ac67d8bSRob Swindell 	return rc;
18805ac67d8bSRob Swindell }
18815ac67d8bSRob Swindell 
1882c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1883c0c050c5SMichael Chan {
1884c0c050c5SMichael Chan 	switch (dir_type) {
1885c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
1886c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1887c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1888c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
1889c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
1890c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
1891c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
189293e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
189393e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
1894c0c050c5SMichael Chan 		return true;
1895c0c050c5SMichael Chan 	}
1896c0c050c5SMichael Chan 
1897c0c050c5SMichael Chan 	return false;
1898c0c050c5SMichael Chan }
1899c0c050c5SMichael Chan 
19005ac67d8bSRob Swindell static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
1901c0c050c5SMichael Chan {
1902c0c050c5SMichael Chan 	switch (dir_type) {
1903c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
1904c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
1905c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
1906c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
1907c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
1908c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
1909c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
1910c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1911c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1912c0c050c5SMichael Chan 		return true;
1913c0c050c5SMichael Chan 	}
1914c0c050c5SMichael Chan 
1915c0c050c5SMichael Chan 	return false;
1916c0c050c5SMichael Chan }
1917c0c050c5SMichael Chan 
1918c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
1919c0c050c5SMichael Chan {
1920c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
19215ac67d8bSRob Swindell 		bnxt_dir_type_is_other_exec_format(dir_type);
1922c0c050c5SMichael Chan }
1923c0c050c5SMichael Chan 
1924c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
1925c0c050c5SMichael Chan 					 u16 dir_type,
1926c0c050c5SMichael Chan 					 const char *filename)
1927c0c050c5SMichael Chan {
1928c0c050c5SMichael Chan 	const struct firmware  *fw;
1929c0c050c5SMichael Chan 	int			rc;
1930c0c050c5SMichael Chan 
1931c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
1932c0c050c5SMichael Chan 	if (rc != 0) {
1933c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
1934c0c050c5SMichael Chan 			   rc, filename);
1935c0c050c5SMichael Chan 		return rc;
1936c0c050c5SMichael Chan 	}
1937c0c050c5SMichael Chan 	if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1938c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
19395ac67d8bSRob Swindell 	else if (bnxt_dir_type_is_other_exec_format(dir_type) == true)
19405ac67d8bSRob Swindell 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
1941c0c050c5SMichael Chan 	else
1942c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1943c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
1944c0c050c5SMichael Chan 	release_firmware(fw);
1945c0c050c5SMichael Chan 	return rc;
1946c0c050c5SMichael Chan }
1947c0c050c5SMichael Chan 
1948c0c050c5SMichael Chan static int bnxt_flash_package_from_file(struct net_device *dev,
19495ac67d8bSRob Swindell 					char *filename, u32 install_type)
1950c0c050c5SMichael Chan {
19515ac67d8bSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
19525ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
19535ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_input install = {0};
19545ac67d8bSRob Swindell 	const struct firmware *fw;
19557c675421SVasundhara Volam 	int rc, hwrm_err = 0;
19565ac67d8bSRob Swindell 	u32 item_len;
19575ac67d8bSRob Swindell 	u16 index;
19585ac67d8bSRob Swindell 
19595ac67d8bSRob Swindell 	bnxt_hwrm_fw_set_time(bp);
19605ac67d8bSRob Swindell 
19615ac67d8bSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
19625ac67d8bSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
19635ac67d8bSRob Swindell 				 &index, &item_len, NULL) != 0) {
19645ac67d8bSRob Swindell 		netdev_err(dev, "PKG update area not created in nvram\n");
19655ac67d8bSRob Swindell 		return -ENOBUFS;
19665ac67d8bSRob Swindell 	}
19675ac67d8bSRob Swindell 
19685ac67d8bSRob Swindell 	rc = request_firmware(&fw, filename, &dev->dev);
19695ac67d8bSRob Swindell 	if (rc != 0) {
19705ac67d8bSRob Swindell 		netdev_err(dev, "PKG error %d requesting file: %s\n",
19715ac67d8bSRob Swindell 			   rc, filename);
19725ac67d8bSRob Swindell 		return rc;
19735ac67d8bSRob Swindell 	}
19745ac67d8bSRob Swindell 
19755ac67d8bSRob Swindell 	if (fw->size > item_len) {
19765ac67d8bSRob Swindell 		netdev_err(dev, "PKG insufficient update area in nvram: %lu",
19775ac67d8bSRob Swindell 			   (unsigned long)fw->size);
19785ac67d8bSRob Swindell 		rc = -EFBIG;
19795ac67d8bSRob Swindell 	} else {
19805ac67d8bSRob Swindell 		dma_addr_t dma_handle;
19815ac67d8bSRob Swindell 		u8 *kmem;
19825ac67d8bSRob Swindell 		struct hwrm_nvm_modify_input modify = {0};
19835ac67d8bSRob Swindell 
19845ac67d8bSRob Swindell 		bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
19855ac67d8bSRob Swindell 
19865ac67d8bSRob Swindell 		modify.dir_idx = cpu_to_le16(index);
19875ac67d8bSRob Swindell 		modify.len = cpu_to_le32(fw->size);
19885ac67d8bSRob Swindell 
19895ac67d8bSRob Swindell 		kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
19905ac67d8bSRob Swindell 					  &dma_handle, GFP_KERNEL);
19915ac67d8bSRob Swindell 		if (!kmem) {
19925ac67d8bSRob Swindell 			netdev_err(dev,
19935ac67d8bSRob Swindell 				   "dma_alloc_coherent failure, length = %u\n",
19945ac67d8bSRob Swindell 				   (unsigned int)fw->size);
19955ac67d8bSRob Swindell 			rc = -ENOMEM;
19965ac67d8bSRob Swindell 		} else {
19975ac67d8bSRob Swindell 			memcpy(kmem, fw->data, fw->size);
19985ac67d8bSRob Swindell 			modify.host_src_addr = cpu_to_le64(dma_handle);
19995ac67d8bSRob Swindell 
20007c675421SVasundhara Volam 			hwrm_err = hwrm_send_message(bp, &modify,
20017c675421SVasundhara Volam 						     sizeof(modify),
20025ac67d8bSRob Swindell 						     FLASH_PACKAGE_TIMEOUT);
20035ac67d8bSRob Swindell 			dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
20045ac67d8bSRob Swindell 					  dma_handle);
20055ac67d8bSRob Swindell 		}
20065ac67d8bSRob Swindell 	}
20075ac67d8bSRob Swindell 	release_firmware(fw);
20087c675421SVasundhara Volam 	if (rc || hwrm_err)
20097c675421SVasundhara Volam 		goto err_exit;
20105ac67d8bSRob Swindell 
20115ac67d8bSRob Swindell 	if ((install_type & 0xffff) == 0)
20125ac67d8bSRob Swindell 		install_type >>= 16;
20135ac67d8bSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
20145ac67d8bSRob Swindell 	install.install_type = cpu_to_le32(install_type);
20155ac67d8bSRob Swindell 
2016cb4d1d62SKshitij Soni 	mutex_lock(&bp->hwrm_cmd_lock);
20177c675421SVasundhara Volam 	hwrm_err = _hwrm_send_message(bp, &install, sizeof(install),
20185ac67d8bSRob Swindell 				      INSTALL_PACKAGE_TIMEOUT);
20197c675421SVasundhara Volam 	if (hwrm_err)
2020cb4d1d62SKshitij Soni 		goto flash_pkg_exit;
2021cb4d1d62SKshitij Soni 
2022cb4d1d62SKshitij Soni 	if (resp->error_code) {
2023cb4d1d62SKshitij Soni 		u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err;
2024cb4d1d62SKshitij Soni 
2025cb4d1d62SKshitij Soni 		if (error_code == NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
2026cb4d1d62SKshitij Soni 			install.flags |= cpu_to_le16(
2027cb4d1d62SKshitij Soni 			       NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
20287c675421SVasundhara Volam 			hwrm_err = _hwrm_send_message(bp, &install,
20297c675421SVasundhara Volam 						      sizeof(install),
2030cb4d1d62SKshitij Soni 						      INSTALL_PACKAGE_TIMEOUT);
20317c675421SVasundhara Volam 			if (hwrm_err)
2032cb4d1d62SKshitij Soni 				goto flash_pkg_exit;
2033cb4d1d62SKshitij Soni 		}
2034cb4d1d62SKshitij Soni 	}
20355ac67d8bSRob Swindell 
20365ac67d8bSRob Swindell 	if (resp->result) {
20375ac67d8bSRob Swindell 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
20385ac67d8bSRob Swindell 			   (s8)resp->result, (int)resp->problem_item);
2039cb4d1d62SKshitij Soni 		rc = -ENOPKG;
20405ac67d8bSRob Swindell 	}
2041cb4d1d62SKshitij Soni flash_pkg_exit:
2042cb4d1d62SKshitij Soni 	mutex_unlock(&bp->hwrm_cmd_lock);
20437c675421SVasundhara Volam err_exit:
20447c675421SVasundhara Volam 	if (hwrm_err == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) {
20457c675421SVasundhara Volam 		netdev_info(dev,
20467c675421SVasundhara Volam 			    "PF does not have admin privileges to flash the device\n");
20477c675421SVasundhara Volam 		rc = -EACCES;
20487c675421SVasundhara Volam 	} else if (hwrm_err) {
20497c675421SVasundhara Volam 		rc = -EOPNOTSUPP;
20507c675421SVasundhara Volam 	}
2051cb4d1d62SKshitij Soni 	return rc;
2052c0c050c5SMichael Chan }
2053c0c050c5SMichael Chan 
2054c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
2055c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
2056c0c050c5SMichael Chan {
2057c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
2058c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
2059c0c050c5SMichael Chan 		return -EINVAL;
2060c0c050c5SMichael Chan 	}
2061c0c050c5SMichael Chan 
20625ac67d8bSRob Swindell 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
20635ac67d8bSRob Swindell 	    flash->region > 0xffff)
20645ac67d8bSRob Swindell 		return bnxt_flash_package_from_file(dev, flash->data,
20655ac67d8bSRob Swindell 						    flash->region);
2066c0c050c5SMichael Chan 
2067c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
2068c0c050c5SMichael Chan }
2069c0c050c5SMichael Chan 
2070c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
2071c0c050c5SMichael Chan {
2072c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2073c0c050c5SMichael Chan 	int rc;
2074c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
2075c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
2076c0c050c5SMichael Chan 
2077c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
2078c0c050c5SMichael Chan 
2079c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2080c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2081c0c050c5SMichael Chan 	if (!rc) {
2082c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
2083c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
2084c0c050c5SMichael Chan 	}
2085c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2086c0c050c5SMichael Chan 	return rc;
2087c0c050c5SMichael Chan }
2088c0c050c5SMichael Chan 
2089c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
2090c0c050c5SMichael Chan {
20914cebbacaSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
20924cebbacaSMichael Chan 
20934cebbacaSMichael Chan 	if (BNXT_VF(bp))
20944cebbacaSMichael Chan 		return 0;
20954cebbacaSMichael Chan 
2096c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
2097c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
2098c0c050c5SMichael Chan 	 */
2099c0c050c5SMichael Chan 	return -1;
2100c0c050c5SMichael Chan }
2101c0c050c5SMichael Chan 
2102c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
2103c0c050c5SMichael Chan {
2104c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2105c0c050c5SMichael Chan 	int rc;
2106c0c050c5SMichael Chan 	u32 dir_entries;
2107c0c050c5SMichael Chan 	u32 entry_length;
2108c0c050c5SMichael Chan 	u8 *buf;
2109c0c050c5SMichael Chan 	size_t buflen;
2110c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2111c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
2112c0c050c5SMichael Chan 
2113c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
2114c0c050c5SMichael Chan 	if (rc != 0)
2115c0c050c5SMichael Chan 		return rc;
2116c0c050c5SMichael Chan 
2117c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
2118c0c050c5SMichael Chan 	if (len < 2)
2119c0c050c5SMichael Chan 		return -EINVAL;
2120c0c050c5SMichael Chan 
2121c0c050c5SMichael Chan 	*data++ = dir_entries;
2122c0c050c5SMichael Chan 	*data++ = entry_length;
2123c0c050c5SMichael Chan 	len -= 2;
2124c0c050c5SMichael Chan 	memset(data, 0xff, len);
2125c0c050c5SMichael Chan 
2126c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
2127c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
2128c0c050c5SMichael Chan 				 GFP_KERNEL);
2129c0c050c5SMichael Chan 	if (!buf) {
2130c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2131c0c050c5SMichael Chan 			   (unsigned)buflen);
2132c0c050c5SMichael Chan 		return -ENOMEM;
2133c0c050c5SMichael Chan 	}
2134c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
2135c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2136c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2137c0c050c5SMichael Chan 	if (rc == 0)
2138c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
2139c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
2140c0c050c5SMichael Chan 	return rc;
2141c0c050c5SMichael Chan }
2142c0c050c5SMichael Chan 
2143c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
2144c0c050c5SMichael Chan 			       u32 length, u8 *data)
2145c0c050c5SMichael Chan {
2146c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2147c0c050c5SMichael Chan 	int rc;
2148c0c050c5SMichael Chan 	u8 *buf;
2149c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2150c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
2151c0c050c5SMichael Chan 
2152e0ad8fc5SMichael Chan 	if (!length)
2153e0ad8fc5SMichael Chan 		return -EINVAL;
2154e0ad8fc5SMichael Chan 
2155c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
2156c0c050c5SMichael Chan 				 GFP_KERNEL);
2157c0c050c5SMichael Chan 	if (!buf) {
2158c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2159c0c050c5SMichael Chan 			   (unsigned)length);
2160c0c050c5SMichael Chan 		return -ENOMEM;
2161c0c050c5SMichael Chan 	}
2162c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
2163c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2164c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2165c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
2166c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
2167c0c050c5SMichael Chan 
2168c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2169c0c050c5SMichael Chan 	if (rc == 0)
2170c0c050c5SMichael Chan 		memcpy(data, buf, length);
2171c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
2172c0c050c5SMichael Chan 	return rc;
2173c0c050c5SMichael Chan }
2174c0c050c5SMichael Chan 
21753ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
21763ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
21773ebf6f0aSRob Swindell 				u32 *data_length)
21783ebf6f0aSRob Swindell {
21793ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
21803ebf6f0aSRob Swindell 	int rc;
21813ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
21823ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
21833ebf6f0aSRob Swindell 
21843ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
21853ebf6f0aSRob Swindell 	req.enables = 0;
21863ebf6f0aSRob Swindell 	req.dir_idx = 0;
21873ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
21883ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
21893ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
21903ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
2191cc72f3b1SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2192cc72f3b1SMichael Chan 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
21933ebf6f0aSRob Swindell 	if (rc == 0) {
21943ebf6f0aSRob Swindell 		if (index)
21953ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
21963ebf6f0aSRob Swindell 		if (item_length)
21973ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
21983ebf6f0aSRob Swindell 		if (data_length)
21993ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
22003ebf6f0aSRob Swindell 	}
2201cc72f3b1SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
22023ebf6f0aSRob Swindell 	return rc;
22033ebf6f0aSRob Swindell }
22043ebf6f0aSRob Swindell 
22053ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
22063ebf6f0aSRob Swindell {
22073ebf6f0aSRob Swindell 	char	*retval = NULL;
22083ebf6f0aSRob Swindell 	char	*p;
22093ebf6f0aSRob Swindell 	char	*value;
22103ebf6f0aSRob Swindell 	int	field = 0;
22113ebf6f0aSRob Swindell 
22123ebf6f0aSRob Swindell 	if (datalen < 1)
22133ebf6f0aSRob Swindell 		return NULL;
22143ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
22153ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
22163ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
22173ebf6f0aSRob Swindell 		field = 0;
22183ebf6f0aSRob Swindell 		retval = NULL;
22193ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
22203ebf6f0aSRob Swindell 			value = p;
22213ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
22223ebf6f0aSRob Swindell 				p++;
22233ebf6f0aSRob Swindell 			if (field == desired_field)
22243ebf6f0aSRob Swindell 				retval = value;
22253ebf6f0aSRob Swindell 			if (*p != '\t')
22263ebf6f0aSRob Swindell 				break;
22273ebf6f0aSRob Swindell 			*p = 0;
22283ebf6f0aSRob Swindell 			field++;
22293ebf6f0aSRob Swindell 			p++;
22303ebf6f0aSRob Swindell 		}
22313ebf6f0aSRob Swindell 		if (*p == 0)
22323ebf6f0aSRob Swindell 			break;
22333ebf6f0aSRob Swindell 		*p = 0;
22343ebf6f0aSRob Swindell 	}
22353ebf6f0aSRob Swindell 	return retval;
22363ebf6f0aSRob Swindell }
22373ebf6f0aSRob Swindell 
2238a60faa60SVasundhara Volam static void bnxt_get_pkgver(struct net_device *dev)
22393ebf6f0aSRob Swindell {
2240a60faa60SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
22413ebf6f0aSRob Swindell 	u16 index = 0;
2242a60faa60SVasundhara Volam 	char *pkgver;
2243a60faa60SVasundhara Volam 	u32 pkglen;
2244a60faa60SVasundhara Volam 	u8 *pkgbuf;
2245a60faa60SVasundhara Volam 	int len;
22463ebf6f0aSRob Swindell 
22473ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
22483ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2249a60faa60SVasundhara Volam 				 &index, NULL, &pkglen) != 0)
2250a60faa60SVasundhara Volam 		return;
22513ebf6f0aSRob Swindell 
2252a60faa60SVasundhara Volam 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
2253a60faa60SVasundhara Volam 	if (!pkgbuf) {
2254a60faa60SVasundhara Volam 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
2255a60faa60SVasundhara Volam 			pkglen);
2256a60faa60SVasundhara Volam 		return;
2257a60faa60SVasundhara Volam 	}
22583ebf6f0aSRob Swindell 
2259a60faa60SVasundhara Volam 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2260a60faa60SVasundhara Volam 		goto err;
2261a60faa60SVasundhara Volam 
2262a60faa60SVasundhara Volam 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2263a60faa60SVasundhara Volam 				   pkglen);
2264a60faa60SVasundhara Volam 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2265a60faa60SVasundhara Volam 		len = strlen(bp->fw_ver_str);
2266a60faa60SVasundhara Volam 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2267a60faa60SVasundhara Volam 			 "/pkg %s", pkgver);
2268a60faa60SVasundhara Volam 	}
2269a60faa60SVasundhara Volam err:
2270a60faa60SVasundhara Volam 	kfree(pkgbuf);
22713ebf6f0aSRob Swindell }
22723ebf6f0aSRob Swindell 
2273c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
2274c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2275c0c050c5SMichael Chan 			   u8 *data)
2276c0c050c5SMichael Chan {
2277c0c050c5SMichael Chan 	u32 index;
2278c0c050c5SMichael Chan 	u32 offset;
2279c0c050c5SMichael Chan 
2280c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
2281c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2282c0c050c5SMichael Chan 
2283c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
2284c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
2285c0c050c5SMichael Chan 
2286c0c050c5SMichael Chan 	if (index == 0) {
2287c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
2288c0c050c5SMichael Chan 		return -EINVAL;
2289c0c050c5SMichael Chan 	}
2290c0c050c5SMichael Chan 
2291c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2292c0c050c5SMichael Chan }
2293c0c050c5SMichael Chan 
2294c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2295c0c050c5SMichael Chan {
2296c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2297c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2298c0c050c5SMichael Chan 
2299c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2300c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2301c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2302c0c050c5SMichael Chan }
2303c0c050c5SMichael Chan 
2304c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
2305c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2306c0c050c5SMichael Chan 			   u8 *data)
2307c0c050c5SMichael Chan {
2308c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2309c0c050c5SMichael Chan 	u8 index, dir_op;
2310c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
2311c0c050c5SMichael Chan 
2312c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
2313c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2314c0c050c5SMichael Chan 		return -EINVAL;
2315c0c050c5SMichael Chan 	}
2316c0c050c5SMichael Chan 
2317c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
2318c0c050c5SMichael Chan 
2319c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
2320c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
2321c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
2322c0c050c5SMichael Chan 		if (index == 0)
2323c0c050c5SMichael Chan 			return -EINVAL;
2324c0c050c5SMichael Chan 		switch (dir_op) {
2325c0c050c5SMichael Chan 		case 0x0e: /* erase */
2326c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
2327c0c050c5SMichael Chan 				return -EINVAL;
2328c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
2329c0c050c5SMichael Chan 		default:
2330c0c050c5SMichael Chan 			return -EINVAL;
2331c0c050c5SMichael Chan 		}
2332c0c050c5SMichael Chan 	}
2333c0c050c5SMichael Chan 
2334c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
2335c0c050c5SMichael Chan 	if (bnxt_dir_type_is_executable(type) == true)
23365ac67d8bSRob Swindell 		return -EOPNOTSUPP;
2337c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
2338c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
2339c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
2340c0c050c5SMichael Chan 
2341c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2342c0c050c5SMichael Chan 				eeprom->len);
2343c0c050c5SMichael Chan }
2344c0c050c5SMichael Chan 
234572b34f04SMichael Chan static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
234672b34f04SMichael Chan {
234772b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
234872b34f04SMichael Chan 	struct ethtool_eee *eee = &bp->eee;
234972b34f04SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
235072b34f04SMichael Chan 	u32 advertising =
235172b34f04SMichael Chan 		 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
235272b34f04SMichael Chan 	int rc = 0;
235372b34f04SMichael Chan 
2354567b2abeSSatish Baddipadige 	if (!BNXT_SINGLE_PF(bp))
235575362a3fSMichael Chan 		return -EOPNOTSUPP;
235672b34f04SMichael Chan 
235772b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
235872b34f04SMichael Chan 		return -EOPNOTSUPP;
235972b34f04SMichael Chan 
236072b34f04SMichael Chan 	if (!edata->eee_enabled)
236172b34f04SMichael Chan 		goto eee_ok;
236272b34f04SMichael Chan 
236372b34f04SMichael Chan 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
236472b34f04SMichael Chan 		netdev_warn(dev, "EEE requires autoneg\n");
236572b34f04SMichael Chan 		return -EINVAL;
236672b34f04SMichael Chan 	}
236772b34f04SMichael Chan 	if (edata->tx_lpi_enabled) {
236872b34f04SMichael Chan 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
236972b34f04SMichael Chan 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
237072b34f04SMichael Chan 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
237172b34f04SMichael Chan 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
237272b34f04SMichael Chan 			return -EINVAL;
237372b34f04SMichael Chan 		} else if (!bp->lpi_tmr_hi) {
237472b34f04SMichael Chan 			edata->tx_lpi_timer = eee->tx_lpi_timer;
237572b34f04SMichael Chan 		}
237672b34f04SMichael Chan 	}
237772b34f04SMichael Chan 	if (!edata->advertised) {
237872b34f04SMichael Chan 		edata->advertised = advertising & eee->supported;
237972b34f04SMichael Chan 	} else if (edata->advertised & ~advertising) {
238072b34f04SMichael Chan 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
238172b34f04SMichael Chan 			    edata->advertised, advertising);
238272b34f04SMichael Chan 		return -EINVAL;
238372b34f04SMichael Chan 	}
238472b34f04SMichael Chan 
238572b34f04SMichael Chan 	eee->advertised = edata->advertised;
238672b34f04SMichael Chan 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
238772b34f04SMichael Chan 	eee->tx_lpi_timer = edata->tx_lpi_timer;
238872b34f04SMichael Chan eee_ok:
238972b34f04SMichael Chan 	eee->eee_enabled = edata->eee_enabled;
239072b34f04SMichael Chan 
239172b34f04SMichael Chan 	if (netif_running(dev))
239272b34f04SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
239372b34f04SMichael Chan 
239472b34f04SMichael Chan 	return rc;
239572b34f04SMichael Chan }
239672b34f04SMichael Chan 
239772b34f04SMichael Chan static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
239872b34f04SMichael Chan {
239972b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
240072b34f04SMichael Chan 
240172b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
240272b34f04SMichael Chan 		return -EOPNOTSUPP;
240372b34f04SMichael Chan 
240472b34f04SMichael Chan 	*edata = bp->eee;
240572b34f04SMichael Chan 	if (!bp->eee.eee_enabled) {
240672b34f04SMichael Chan 		/* Preserve tx_lpi_timer so that the last value will be used
240772b34f04SMichael Chan 		 * by default when it is re-enabled.
240872b34f04SMichael Chan 		 */
240972b34f04SMichael Chan 		edata->advertised = 0;
241072b34f04SMichael Chan 		edata->tx_lpi_enabled = 0;
241172b34f04SMichael Chan 	}
241272b34f04SMichael Chan 
241372b34f04SMichael Chan 	if (!bp->eee.eee_active)
241472b34f04SMichael Chan 		edata->lp_advertised = 0;
241572b34f04SMichael Chan 
241672b34f04SMichael Chan 	return 0;
241772b34f04SMichael Chan }
241872b34f04SMichael Chan 
241942ee18feSAjit Khaparde static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
242042ee18feSAjit Khaparde 					    u16 page_number, u16 start_addr,
242142ee18feSAjit Khaparde 					    u16 data_length, u8 *buf)
242242ee18feSAjit Khaparde {
242342ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_input req = {0};
242442ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
242542ee18feSAjit Khaparde 	int rc, byte_offset = 0;
242642ee18feSAjit Khaparde 
242742ee18feSAjit Khaparde 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
242842ee18feSAjit Khaparde 	req.i2c_slave_addr = i2c_addr;
242942ee18feSAjit Khaparde 	req.page_number = cpu_to_le16(page_number);
243042ee18feSAjit Khaparde 	req.port_id = cpu_to_le16(bp->pf.port_id);
243142ee18feSAjit Khaparde 	do {
243242ee18feSAjit Khaparde 		u16 xfer_size;
243342ee18feSAjit Khaparde 
243442ee18feSAjit Khaparde 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
243542ee18feSAjit Khaparde 		data_length -= xfer_size;
243642ee18feSAjit Khaparde 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
243742ee18feSAjit Khaparde 		req.data_length = xfer_size;
243842ee18feSAjit Khaparde 		req.enables = cpu_to_le32(start_addr + byte_offset ?
243942ee18feSAjit Khaparde 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
244042ee18feSAjit Khaparde 		mutex_lock(&bp->hwrm_cmd_lock);
244142ee18feSAjit Khaparde 		rc = _hwrm_send_message(bp, &req, sizeof(req),
244242ee18feSAjit Khaparde 					HWRM_CMD_TIMEOUT);
244342ee18feSAjit Khaparde 		if (!rc)
244442ee18feSAjit Khaparde 			memcpy(buf + byte_offset, output->data, xfer_size);
244542ee18feSAjit Khaparde 		mutex_unlock(&bp->hwrm_cmd_lock);
244642ee18feSAjit Khaparde 		byte_offset += xfer_size;
244742ee18feSAjit Khaparde 	} while (!rc && data_length > 0);
244842ee18feSAjit Khaparde 
244942ee18feSAjit Khaparde 	return rc;
245042ee18feSAjit Khaparde }
245142ee18feSAjit Khaparde 
245242ee18feSAjit Khaparde static int bnxt_get_module_info(struct net_device *dev,
245342ee18feSAjit Khaparde 				struct ethtool_modinfo *modinfo)
245442ee18feSAjit Khaparde {
24557328a23cSVasundhara Volam 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
245642ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
245742ee18feSAjit Khaparde 	int rc;
245842ee18feSAjit Khaparde 
245942ee18feSAjit Khaparde 	/* No point in going further if phy status indicates
246042ee18feSAjit Khaparde 	 * module is not inserted or if it is powered down or
246142ee18feSAjit Khaparde 	 * if it is of type 10GBase-T
246242ee18feSAjit Khaparde 	 */
246342ee18feSAjit Khaparde 	if (bp->link_info.module_status >
246442ee18feSAjit Khaparde 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
246542ee18feSAjit Khaparde 		return -EOPNOTSUPP;
246642ee18feSAjit Khaparde 
246742ee18feSAjit Khaparde 	/* This feature is not supported in older firmware versions */
246842ee18feSAjit Khaparde 	if (bp->hwrm_spec_code < 0x10202)
246942ee18feSAjit Khaparde 		return -EOPNOTSUPP;
247042ee18feSAjit Khaparde 
24717328a23cSVasundhara Volam 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
24727328a23cSVasundhara Volam 					      SFF_DIAG_SUPPORT_OFFSET + 1,
24737328a23cSVasundhara Volam 					      data);
247442ee18feSAjit Khaparde 	if (!rc) {
24757328a23cSVasundhara Volam 		u8 module_id = data[0];
24767328a23cSVasundhara Volam 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
247742ee18feSAjit Khaparde 
247842ee18feSAjit Khaparde 		switch (module_id) {
247942ee18feSAjit Khaparde 		case SFF_MODULE_ID_SFP:
248042ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8472;
248142ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
24827328a23cSVasundhara Volam 			if (!diag_supported)
24837328a23cSVasundhara Volam 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
248442ee18feSAjit Khaparde 			break;
248542ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP:
248642ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP_PLUS:
248742ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8436;
248842ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
248942ee18feSAjit Khaparde 			break;
249042ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP28:
249142ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8636;
249242ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
249342ee18feSAjit Khaparde 			break;
249442ee18feSAjit Khaparde 		default:
249542ee18feSAjit Khaparde 			rc = -EOPNOTSUPP;
249642ee18feSAjit Khaparde 			break;
249742ee18feSAjit Khaparde 		}
249842ee18feSAjit Khaparde 	}
249942ee18feSAjit Khaparde 	return rc;
250042ee18feSAjit Khaparde }
250142ee18feSAjit Khaparde 
250242ee18feSAjit Khaparde static int bnxt_get_module_eeprom(struct net_device *dev,
250342ee18feSAjit Khaparde 				  struct ethtool_eeprom *eeprom,
250442ee18feSAjit Khaparde 				  u8 *data)
250542ee18feSAjit Khaparde {
250642ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
250742ee18feSAjit Khaparde 	u16  start = eeprom->offset, length = eeprom->len;
2508f3ea3119SColin Ian King 	int rc = 0;
250942ee18feSAjit Khaparde 
251042ee18feSAjit Khaparde 	memset(data, 0, eeprom->len);
251142ee18feSAjit Khaparde 
251242ee18feSAjit Khaparde 	/* Read A0 portion of the EEPROM */
251342ee18feSAjit Khaparde 	if (start < ETH_MODULE_SFF_8436_LEN) {
251442ee18feSAjit Khaparde 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
251542ee18feSAjit Khaparde 			length = ETH_MODULE_SFF_8436_LEN - start;
251642ee18feSAjit Khaparde 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
251742ee18feSAjit Khaparde 						      start, length, data);
251842ee18feSAjit Khaparde 		if (rc)
251942ee18feSAjit Khaparde 			return rc;
252042ee18feSAjit Khaparde 		start += length;
252142ee18feSAjit Khaparde 		data += length;
252242ee18feSAjit Khaparde 		length = eeprom->len - length;
252342ee18feSAjit Khaparde 	}
252442ee18feSAjit Khaparde 
252542ee18feSAjit Khaparde 	/* Read A2 portion of the EEPROM */
252642ee18feSAjit Khaparde 	if (length) {
252742ee18feSAjit Khaparde 		start -= ETH_MODULE_SFF_8436_LEN;
2528dea521a2SChristophe JAILLET 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1,
2529dea521a2SChristophe JAILLET 						      start, length, data);
253042ee18feSAjit Khaparde 	}
253142ee18feSAjit Khaparde 	return rc;
253242ee18feSAjit Khaparde }
253342ee18feSAjit Khaparde 
2534ae8e98a6SDeepak Khungar static int bnxt_nway_reset(struct net_device *dev)
2535ae8e98a6SDeepak Khungar {
2536ae8e98a6SDeepak Khungar 	int rc = 0;
2537ae8e98a6SDeepak Khungar 
2538ae8e98a6SDeepak Khungar 	struct bnxt *bp = netdev_priv(dev);
2539ae8e98a6SDeepak Khungar 	struct bnxt_link_info *link_info = &bp->link_info;
2540ae8e98a6SDeepak Khungar 
2541ae8e98a6SDeepak Khungar 	if (!BNXT_SINGLE_PF(bp))
2542ae8e98a6SDeepak Khungar 		return -EOPNOTSUPP;
2543ae8e98a6SDeepak Khungar 
2544ae8e98a6SDeepak Khungar 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2545ae8e98a6SDeepak Khungar 		return -EINVAL;
2546ae8e98a6SDeepak Khungar 
2547ae8e98a6SDeepak Khungar 	if (netif_running(dev))
2548ae8e98a6SDeepak Khungar 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
2549ae8e98a6SDeepak Khungar 
2550ae8e98a6SDeepak Khungar 	return rc;
2551ae8e98a6SDeepak Khungar }
2552ae8e98a6SDeepak Khungar 
25535ad2cbeeSMichael Chan static int bnxt_set_phys_id(struct net_device *dev,
25545ad2cbeeSMichael Chan 			    enum ethtool_phys_id_state state)
25555ad2cbeeSMichael Chan {
25565ad2cbeeSMichael Chan 	struct hwrm_port_led_cfg_input req = {0};
25575ad2cbeeSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
25585ad2cbeeSMichael Chan 	struct bnxt_pf_info *pf = &bp->pf;
25595ad2cbeeSMichael Chan 	struct bnxt_led_cfg *led_cfg;
25605ad2cbeeSMichael Chan 	u8 led_state;
25615ad2cbeeSMichael Chan 	__le16 duration;
25625ad2cbeeSMichael Chan 	int i, rc;
25635ad2cbeeSMichael Chan 
25645ad2cbeeSMichael Chan 	if (!bp->num_leds || BNXT_VF(bp))
25655ad2cbeeSMichael Chan 		return -EOPNOTSUPP;
25665ad2cbeeSMichael Chan 
25675ad2cbeeSMichael Chan 	if (state == ETHTOOL_ID_ACTIVE) {
25685ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
25695ad2cbeeSMichael Chan 		duration = cpu_to_le16(500);
25705ad2cbeeSMichael Chan 	} else if (state == ETHTOOL_ID_INACTIVE) {
25715ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
25725ad2cbeeSMichael Chan 		duration = cpu_to_le16(0);
25735ad2cbeeSMichael Chan 	} else {
25745ad2cbeeSMichael Chan 		return -EINVAL;
25755ad2cbeeSMichael Chan 	}
25765ad2cbeeSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
25775ad2cbeeSMichael Chan 	req.port_id = cpu_to_le16(pf->port_id);
25785ad2cbeeSMichael Chan 	req.num_leds = bp->num_leds;
25795ad2cbeeSMichael Chan 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
25805ad2cbeeSMichael Chan 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
25815ad2cbeeSMichael Chan 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
25825ad2cbeeSMichael Chan 		led_cfg->led_id = bp->leds[i].led_id;
25835ad2cbeeSMichael Chan 		led_cfg->led_state = led_state;
25845ad2cbeeSMichael Chan 		led_cfg->led_blink_on = duration;
25855ad2cbeeSMichael Chan 		led_cfg->led_blink_off = duration;
25865ad2cbeeSMichael Chan 		led_cfg->led_group_id = bp->leds[i].led_group_id;
25875ad2cbeeSMichael Chan 	}
25885ad2cbeeSMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
25895ad2cbeeSMichael Chan 	if (rc)
25905ad2cbeeSMichael Chan 		rc = -EIO;
25915ad2cbeeSMichael Chan 	return rc;
25925ad2cbeeSMichael Chan }
25935ad2cbeeSMichael Chan 
259467fea463SMichael Chan static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
259567fea463SMichael Chan {
259667fea463SMichael Chan 	struct hwrm_selftest_irq_input req = {0};
259767fea463SMichael Chan 
259867fea463SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
259967fea463SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
260067fea463SMichael Chan }
260167fea463SMichael Chan 
260267fea463SMichael Chan static int bnxt_test_irq(struct bnxt *bp)
260367fea463SMichael Chan {
260467fea463SMichael Chan 	int i;
260567fea463SMichael Chan 
260667fea463SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
260767fea463SMichael Chan 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
260867fea463SMichael Chan 		int rc;
260967fea463SMichael Chan 
261067fea463SMichael Chan 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
261167fea463SMichael Chan 		if (rc)
261267fea463SMichael Chan 			return rc;
261367fea463SMichael Chan 	}
261467fea463SMichael Chan 	return 0;
261567fea463SMichael Chan }
261667fea463SMichael Chan 
2617f7dc1ea6SMichael Chan static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
2618f7dc1ea6SMichael Chan {
2619f7dc1ea6SMichael Chan 	struct hwrm_port_mac_cfg_input req = {0};
2620f7dc1ea6SMichael Chan 
2621f7dc1ea6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
2622f7dc1ea6SMichael Chan 
2623f7dc1ea6SMichael Chan 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
2624f7dc1ea6SMichael Chan 	if (enable)
2625f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
2626f7dc1ea6SMichael Chan 	else
2627f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
2628f7dc1ea6SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2629f7dc1ea6SMichael Chan }
2630f7dc1ea6SMichael Chan 
263156d37462SVasundhara Volam static int bnxt_query_force_speeds(struct bnxt *bp, u16 *force_speeds)
263256d37462SVasundhara Volam {
263356d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
263456d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_input req = {0};
263556d37462SVasundhara Volam 	int rc;
263656d37462SVasundhara Volam 
263756d37462SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
263856d37462SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
263956d37462SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
264056d37462SVasundhara Volam 	if (!rc)
264156d37462SVasundhara Volam 		*force_speeds = le16_to_cpu(resp->supported_speeds_force_mode);
264256d37462SVasundhara Volam 
264356d37462SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
264456d37462SVasundhara Volam 	return rc;
264556d37462SVasundhara Volam }
264656d37462SVasundhara Volam 
264791725d89SMichael Chan static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
264891725d89SMichael Chan 				    struct hwrm_port_phy_cfg_input *req)
264991725d89SMichael Chan {
265091725d89SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
265156d37462SVasundhara Volam 	u16 fw_advertising;
265291725d89SMichael Chan 	u16 fw_speed;
265391725d89SMichael Chan 	int rc;
265491725d89SMichael Chan 
265591725d89SMichael Chan 	if (!link_info->autoneg)
265691725d89SMichael Chan 		return 0;
265791725d89SMichael Chan 
265856d37462SVasundhara Volam 	rc = bnxt_query_force_speeds(bp, &fw_advertising);
265956d37462SVasundhara Volam 	if (rc)
266056d37462SVasundhara Volam 		return rc;
266156d37462SVasundhara Volam 
266291725d89SMichael Chan 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
266391725d89SMichael Chan 	if (netif_carrier_ok(bp->dev))
266491725d89SMichael Chan 		fw_speed = bp->link_info.link_speed;
266591725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
266691725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
266791725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
266891725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
266991725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
267091725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
267191725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
267291725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
267391725d89SMichael Chan 
267491725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(fw_speed);
267591725d89SMichael Chan 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
267691725d89SMichael Chan 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
267791725d89SMichael Chan 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
267891725d89SMichael Chan 	req->flags = 0;
267991725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(0);
268091725d89SMichael Chan 	return rc;
268191725d89SMichael Chan }
268291725d89SMichael Chan 
268355fd0cf3SMichael Chan static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
268491725d89SMichael Chan {
268591725d89SMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
268691725d89SMichael Chan 
268791725d89SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
268891725d89SMichael Chan 
268991725d89SMichael Chan 	if (enable) {
269091725d89SMichael Chan 		bnxt_disable_an_for_lpbk(bp, &req);
269155fd0cf3SMichael Chan 		if (ext)
269255fd0cf3SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
269355fd0cf3SMichael Chan 		else
269491725d89SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
269591725d89SMichael Chan 	} else {
269691725d89SMichael Chan 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
269791725d89SMichael Chan 	}
269891725d89SMichael Chan 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
269991725d89SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
270091725d89SMichael Chan }
270191725d89SMichael Chan 
2702e44758b7SMichael Chan static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2703f7dc1ea6SMichael Chan 			    u32 raw_cons, int pkt_size)
2704f7dc1ea6SMichael Chan {
2705e44758b7SMichael Chan 	struct bnxt_napi *bnapi = cpr->bnapi;
2706e44758b7SMichael Chan 	struct bnxt_rx_ring_info *rxr;
2707f7dc1ea6SMichael Chan 	struct bnxt_sw_rx_bd *rx_buf;
2708f7dc1ea6SMichael Chan 	struct rx_cmp *rxcmp;
2709f7dc1ea6SMichael Chan 	u16 cp_cons, cons;
2710f7dc1ea6SMichael Chan 	u8 *data;
2711f7dc1ea6SMichael Chan 	u32 len;
2712f7dc1ea6SMichael Chan 	int i;
2713f7dc1ea6SMichael Chan 
2714e44758b7SMichael Chan 	rxr = bnapi->rx_ring;
2715f7dc1ea6SMichael Chan 	cp_cons = RING_CMP(raw_cons);
2716f7dc1ea6SMichael Chan 	rxcmp = (struct rx_cmp *)
2717f7dc1ea6SMichael Chan 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2718f7dc1ea6SMichael Chan 	cons = rxcmp->rx_cmp_opaque;
2719f7dc1ea6SMichael Chan 	rx_buf = &rxr->rx_buf_ring[cons];
2720f7dc1ea6SMichael Chan 	data = rx_buf->data_ptr;
2721f7dc1ea6SMichael Chan 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
2722f7dc1ea6SMichael Chan 	if (len != pkt_size)
2723f7dc1ea6SMichael Chan 		return -EIO;
2724f7dc1ea6SMichael Chan 	i = ETH_ALEN;
2725f7dc1ea6SMichael Chan 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
2726f7dc1ea6SMichael Chan 		return -EIO;
2727f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2728f7dc1ea6SMichael Chan 	for (  ; i < pkt_size; i++) {
2729f7dc1ea6SMichael Chan 		if (data[i] != (u8)(i & 0xff))
2730f7dc1ea6SMichael Chan 			return -EIO;
2731f7dc1ea6SMichael Chan 	}
2732f7dc1ea6SMichael Chan 	return 0;
2733f7dc1ea6SMichael Chan }
2734f7dc1ea6SMichael Chan 
2735e44758b7SMichael Chan static int bnxt_poll_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2736e44758b7SMichael Chan 			      int pkt_size)
2737f7dc1ea6SMichael Chan {
2738f7dc1ea6SMichael Chan 	struct tx_cmp *txcmp;
2739f7dc1ea6SMichael Chan 	int rc = -EIO;
2740f7dc1ea6SMichael Chan 	u32 raw_cons;
2741f7dc1ea6SMichael Chan 	u32 cons;
2742f7dc1ea6SMichael Chan 	int i;
2743f7dc1ea6SMichael Chan 
2744f7dc1ea6SMichael Chan 	raw_cons = cpr->cp_raw_cons;
2745f7dc1ea6SMichael Chan 	for (i = 0; i < 200; i++) {
2746f7dc1ea6SMichael Chan 		cons = RING_CMP(raw_cons);
2747f7dc1ea6SMichael Chan 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2748f7dc1ea6SMichael Chan 
2749f7dc1ea6SMichael Chan 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
2750f7dc1ea6SMichael Chan 			udelay(5);
2751f7dc1ea6SMichael Chan 			continue;
2752f7dc1ea6SMichael Chan 		}
2753f7dc1ea6SMichael Chan 
2754f7dc1ea6SMichael Chan 		/* The valid test of the entry must be done first before
2755f7dc1ea6SMichael Chan 		 * reading any further.
2756f7dc1ea6SMichael Chan 		 */
2757f7dc1ea6SMichael Chan 		dma_rmb();
2758f7dc1ea6SMichael Chan 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
2759e44758b7SMichael Chan 			rc = bnxt_rx_loopback(bp, cpr, raw_cons, pkt_size);
2760f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2761f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2762f7dc1ea6SMichael Chan 			break;
2763f7dc1ea6SMichael Chan 		}
2764f7dc1ea6SMichael Chan 		raw_cons = NEXT_RAW_CMP(raw_cons);
2765f7dc1ea6SMichael Chan 	}
2766f7dc1ea6SMichael Chan 	cpr->cp_raw_cons = raw_cons;
2767f7dc1ea6SMichael Chan 	return rc;
2768f7dc1ea6SMichael Chan }
2769f7dc1ea6SMichael Chan 
2770f7dc1ea6SMichael Chan static int bnxt_run_loopback(struct bnxt *bp)
2771f7dc1ea6SMichael Chan {
2772f7dc1ea6SMichael Chan 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
277384404d5fSMichael Chan 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
2774e44758b7SMichael Chan 	struct bnxt_cp_ring_info *cpr;
2775f7dc1ea6SMichael Chan 	int pkt_size, i = 0;
2776f7dc1ea6SMichael Chan 	struct sk_buff *skb;
2777f7dc1ea6SMichael Chan 	dma_addr_t map;
2778f7dc1ea6SMichael Chan 	u8 *data;
2779f7dc1ea6SMichael Chan 	int rc;
2780f7dc1ea6SMichael Chan 
278184404d5fSMichael Chan 	cpr = &rxr->bnapi->cp_ring;
278284404d5fSMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
278384404d5fSMichael Chan 		cpr = cpr->cp_ring_arr[BNXT_RX_HDL];
2784f7dc1ea6SMichael Chan 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
2785f7dc1ea6SMichael Chan 	skb = netdev_alloc_skb(bp->dev, pkt_size);
2786f7dc1ea6SMichael Chan 	if (!skb)
2787f7dc1ea6SMichael Chan 		return -ENOMEM;
2788f7dc1ea6SMichael Chan 	data = skb_put(skb, pkt_size);
2789f7dc1ea6SMichael Chan 	eth_broadcast_addr(data);
2790f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2791f7dc1ea6SMichael Chan 	ether_addr_copy(&data[i], bp->dev->dev_addr);
2792f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2793f7dc1ea6SMichael Chan 	for ( ; i < pkt_size; i++)
2794f7dc1ea6SMichael Chan 		data[i] = (u8)(i & 0xff);
2795f7dc1ea6SMichael Chan 
2796f7dc1ea6SMichael Chan 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
2797f7dc1ea6SMichael Chan 			     PCI_DMA_TODEVICE);
2798f7dc1ea6SMichael Chan 	if (dma_mapping_error(&bp->pdev->dev, map)) {
2799f7dc1ea6SMichael Chan 		dev_kfree_skb(skb);
2800f7dc1ea6SMichael Chan 		return -EIO;
2801f7dc1ea6SMichael Chan 	}
2802f7dc1ea6SMichael Chan 	bnxt_xmit_xdp(bp, txr, map, pkt_size, 0);
2803f7dc1ea6SMichael Chan 
2804f7dc1ea6SMichael Chan 	/* Sync BD data before updating doorbell */
2805f7dc1ea6SMichael Chan 	wmb();
2806f7dc1ea6SMichael Chan 
2807697197e5SMichael Chan 	bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
2808e44758b7SMichael Chan 	rc = bnxt_poll_loopback(bp, cpr, pkt_size);
2809f7dc1ea6SMichael Chan 
2810f7dc1ea6SMichael Chan 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
2811f7dc1ea6SMichael Chan 	dev_kfree_skb(skb);
2812f7dc1ea6SMichael Chan 	return rc;
2813f7dc1ea6SMichael Chan }
2814f7dc1ea6SMichael Chan 
2815eb513658SMichael Chan static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
2816eb513658SMichael Chan {
2817eb513658SMichael Chan 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
2818eb513658SMichael Chan 	struct hwrm_selftest_exec_input req = {0};
2819eb513658SMichael Chan 	int rc;
2820eb513658SMichael Chan 
2821eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
2822eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2823eb513658SMichael Chan 	resp->test_success = 0;
2824eb513658SMichael Chan 	req.flags = test_mask;
2825eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
2826eb513658SMichael Chan 	*test_results = resp->test_success;
2827eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2828eb513658SMichael Chan 	return rc;
2829eb513658SMichael Chan }
2830eb513658SMichael Chan 
283155fd0cf3SMichael Chan #define BNXT_DRV_TESTS			4
2832f7dc1ea6SMichael Chan #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
283391725d89SMichael Chan #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
283455fd0cf3SMichael Chan #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
283555fd0cf3SMichael Chan #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
2836eb513658SMichael Chan 
2837eb513658SMichael Chan static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
2838eb513658SMichael Chan 			   u64 *buf)
2839eb513658SMichael Chan {
2840eb513658SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
284155fd0cf3SMichael Chan 	bool do_ext_lpbk = false;
2842eb513658SMichael Chan 	bool offline = false;
2843eb513658SMichael Chan 	u8 test_results = 0;
2844eb513658SMichael Chan 	u8 test_mask = 0;
2845d27e2ca1SMichael Chan 	int rc = 0, i;
2846eb513658SMichael Chan 
2847eb513658SMichael Chan 	if (!bp->num_tests || !BNXT_SINGLE_PF(bp))
2848eb513658SMichael Chan 		return;
2849eb513658SMichael Chan 	memset(buf, 0, sizeof(u64) * bp->num_tests);
2850eb513658SMichael Chan 	if (!netif_running(dev)) {
2851eb513658SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
2852eb513658SMichael Chan 		return;
2853eb513658SMichael Chan 	}
2854eb513658SMichael Chan 
285555fd0cf3SMichael Chan 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
285655fd0cf3SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_EXT_LPBK))
285755fd0cf3SMichael Chan 		do_ext_lpbk = true;
285855fd0cf3SMichael Chan 
2859eb513658SMichael Chan 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
2860eb513658SMichael Chan 		if (bp->pf.active_vfs) {
2861eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2862eb513658SMichael Chan 			netdev_warn(dev, "Offline tests cannot be run with active VFs\n");
2863eb513658SMichael Chan 			return;
2864eb513658SMichael Chan 		}
2865eb513658SMichael Chan 		offline = true;
2866eb513658SMichael Chan 	}
2867eb513658SMichael Chan 
2868eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2869eb513658SMichael Chan 		u8 bit_val = 1 << i;
2870eb513658SMichael Chan 
2871eb513658SMichael Chan 		if (!(bp->test_info->offline_mask & bit_val))
2872eb513658SMichael Chan 			test_mask |= bit_val;
2873eb513658SMichael Chan 		else if (offline)
2874eb513658SMichael Chan 			test_mask |= bit_val;
2875eb513658SMichael Chan 	}
2876eb513658SMichael Chan 	if (!offline) {
2877eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
2878eb513658SMichael Chan 	} else {
2879eb513658SMichael Chan 		rc = bnxt_close_nic(bp, false, false);
2880eb513658SMichael Chan 		if (rc)
2881eb513658SMichael Chan 			return;
2882eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
2883f7dc1ea6SMichael Chan 
2884f7dc1ea6SMichael Chan 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
2885f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, true);
2886f7dc1ea6SMichael Chan 		msleep(250);
2887f7dc1ea6SMichael Chan 		rc = bnxt_half_open_nic(bp);
2888f7dc1ea6SMichael Chan 		if (rc) {
2889f7dc1ea6SMichael Chan 			bnxt_hwrm_mac_loopback(bp, false);
2890f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2891f7dc1ea6SMichael Chan 			return;
2892f7dc1ea6SMichael Chan 		}
2893f7dc1ea6SMichael Chan 		if (bnxt_run_loopback(bp))
2894f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2895f7dc1ea6SMichael Chan 		else
2896f7dc1ea6SMichael Chan 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
2897f7dc1ea6SMichael Chan 
2898f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, false);
289955fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, true, false);
290091725d89SMichael Chan 		msleep(1000);
290191725d89SMichael Chan 		if (bnxt_run_loopback(bp)) {
290291725d89SMichael Chan 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
290391725d89SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
290491725d89SMichael Chan 		}
290555fd0cf3SMichael Chan 		if (do_ext_lpbk) {
290655fd0cf3SMichael Chan 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
290755fd0cf3SMichael Chan 			bnxt_hwrm_phy_loopback(bp, true, true);
290855fd0cf3SMichael Chan 			msleep(1000);
290955fd0cf3SMichael Chan 			if (bnxt_run_loopback(bp)) {
291055fd0cf3SMichael Chan 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
291155fd0cf3SMichael Chan 				etest->flags |= ETH_TEST_FL_FAILED;
291255fd0cf3SMichael Chan 			}
291355fd0cf3SMichael Chan 		}
291455fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, false, false);
291591725d89SMichael Chan 		bnxt_half_close_nic(bp);
2916d27e2ca1SMichael Chan 		rc = bnxt_open_nic(bp, false, true);
2917eb513658SMichael Chan 	}
2918d27e2ca1SMichael Chan 	if (rc || bnxt_test_irq(bp)) {
291967fea463SMichael Chan 		buf[BNXT_IRQ_TEST_IDX] = 1;
292067fea463SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
292167fea463SMichael Chan 	}
2922eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2923eb513658SMichael Chan 		u8 bit_val = 1 << i;
2924eb513658SMichael Chan 
2925eb513658SMichael Chan 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
2926eb513658SMichael Chan 			buf[i] = 1;
2927eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2928eb513658SMichael Chan 		}
2929eb513658SMichael Chan 	}
2930eb513658SMichael Chan }
2931eb513658SMichael Chan 
293249f7972fSVasundhara Volam static int bnxt_reset(struct net_device *dev, u32 *flags)
293349f7972fSVasundhara Volam {
293449f7972fSVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
293549f7972fSVasundhara Volam 	int rc = 0;
293649f7972fSVasundhara Volam 
293749f7972fSVasundhara Volam 	if (!BNXT_PF(bp)) {
293849f7972fSVasundhara Volam 		netdev_err(dev, "Reset is not supported from a VF\n");
293949f7972fSVasundhara Volam 		return -EOPNOTSUPP;
294049f7972fSVasundhara Volam 	}
294149f7972fSVasundhara Volam 
294249f7972fSVasundhara Volam 	if (pci_vfs_assigned(bp->pdev)) {
294349f7972fSVasundhara Volam 		netdev_err(dev,
294449f7972fSVasundhara Volam 			   "Reset not allowed when VFs are assigned to VMs\n");
294549f7972fSVasundhara Volam 		return -EBUSY;
294649f7972fSVasundhara Volam 	}
294749f7972fSVasundhara Volam 
294849f7972fSVasundhara Volam 	if (*flags == ETH_RESET_ALL) {
294949f7972fSVasundhara Volam 		/* This feature is not supported in older firmware versions */
295049f7972fSVasundhara Volam 		if (bp->hwrm_spec_code < 0x10803)
295149f7972fSVasundhara Volam 			return -EOPNOTSUPP;
295249f7972fSVasundhara Volam 
295349f7972fSVasundhara Volam 		rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_CHIP);
29542373d8d6SScott Branden 		if (!rc) {
295549f7972fSVasundhara Volam 			netdev_info(dev, "Reset request successful. Reload driver to complete reset\n");
29562373d8d6SScott Branden 			*flags = 0;
29572373d8d6SScott Branden 		}
29586502ad59SScott Branden 	} else if (*flags == ETH_RESET_AP) {
29596502ad59SScott Branden 		/* This feature is not supported in older firmware versions */
29606502ad59SScott Branden 		if (bp->hwrm_spec_code < 0x10803)
29616502ad59SScott Branden 			return -EOPNOTSUPP;
29626502ad59SScott Branden 
29636502ad59SScott Branden 		rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_AP);
29642373d8d6SScott Branden 		if (!rc) {
29656502ad59SScott Branden 			netdev_info(dev, "Reset Application Processor request successful.\n");
29662373d8d6SScott Branden 			*flags = 0;
29672373d8d6SScott Branden 		}
296849f7972fSVasundhara Volam 	} else {
296949f7972fSVasundhara Volam 		rc = -EINVAL;
297049f7972fSVasundhara Volam 	}
297149f7972fSVasundhara Volam 
297249f7972fSVasundhara Volam 	return rc;
297349f7972fSVasundhara Volam }
297449f7972fSVasundhara Volam 
29756c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
29766c5657d0SVasundhara Volam 				  struct bnxt_hwrm_dbg_dma_info *info)
29776c5657d0SVasundhara Volam {
29786c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
29796c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_input *cmn_req = msg;
29806c5657d0SVasundhara Volam 	__le16 *seq_ptr = msg + info->seq_off;
29816c5657d0SVasundhara Volam 	u16 seq = 0, len, segs_off;
29826c5657d0SVasundhara Volam 	void *resp = cmn_resp;
29836c5657d0SVasundhara Volam 	dma_addr_t dma_handle;
29846c5657d0SVasundhara Volam 	int rc, off = 0;
29856c5657d0SVasundhara Volam 	void *dma_buf;
29866c5657d0SVasundhara Volam 
29876c5657d0SVasundhara Volam 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
29886c5657d0SVasundhara Volam 				     GFP_KERNEL);
29896c5657d0SVasundhara Volam 	if (!dma_buf)
29906c5657d0SVasundhara Volam 		return -ENOMEM;
29916c5657d0SVasundhara Volam 
29926c5657d0SVasundhara Volam 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
29936c5657d0SVasundhara Volam 			    total_segments);
29946c5657d0SVasundhara Volam 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
29956c5657d0SVasundhara Volam 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
29966c5657d0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
29976c5657d0SVasundhara Volam 	while (1) {
29986c5657d0SVasundhara Volam 		*seq_ptr = cpu_to_le16(seq);
29996c5657d0SVasundhara Volam 		rc = _hwrm_send_message(bp, msg, msg_len, HWRM_CMD_TIMEOUT);
30006c5657d0SVasundhara Volam 		if (rc)
30016c5657d0SVasundhara Volam 			break;
30026c5657d0SVasundhara Volam 
30036c5657d0SVasundhara Volam 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
30046c5657d0SVasundhara Volam 		if (!seq &&
30056c5657d0SVasundhara Volam 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
30066c5657d0SVasundhara Volam 			info->segs = le16_to_cpu(*((__le16 *)(resp +
30076c5657d0SVasundhara Volam 							      segs_off)));
30086c5657d0SVasundhara Volam 			if (!info->segs) {
30096c5657d0SVasundhara Volam 				rc = -EIO;
30106c5657d0SVasundhara Volam 				break;
30116c5657d0SVasundhara Volam 			}
30126c5657d0SVasundhara Volam 
30136c5657d0SVasundhara Volam 			info->dest_buf_size = info->segs *
30146c5657d0SVasundhara Volam 					sizeof(struct coredump_segment_record);
30156c5657d0SVasundhara Volam 			info->dest_buf = kmalloc(info->dest_buf_size,
30166c5657d0SVasundhara Volam 						 GFP_KERNEL);
30176c5657d0SVasundhara Volam 			if (!info->dest_buf) {
30186c5657d0SVasundhara Volam 				rc = -ENOMEM;
30196c5657d0SVasundhara Volam 				break;
30206c5657d0SVasundhara Volam 			}
30216c5657d0SVasundhara Volam 		}
30226c5657d0SVasundhara Volam 
30236c5657d0SVasundhara Volam 		if (info->dest_buf)
30246c5657d0SVasundhara Volam 			memcpy(info->dest_buf + off, dma_buf, len);
30256c5657d0SVasundhara Volam 
30266c5657d0SVasundhara Volam 		if (cmn_req->req_type ==
30276c5657d0SVasundhara Volam 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
30286c5657d0SVasundhara Volam 			info->dest_buf_size += len;
30296c5657d0SVasundhara Volam 
30306c5657d0SVasundhara Volam 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
30316c5657d0SVasundhara Volam 			break;
30326c5657d0SVasundhara Volam 
30336c5657d0SVasundhara Volam 		seq++;
30346c5657d0SVasundhara Volam 		off += len;
30356c5657d0SVasundhara Volam 	}
30366c5657d0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
30376c5657d0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
30386c5657d0SVasundhara Volam 	return rc;
30396c5657d0SVasundhara Volam }
30406c5657d0SVasundhara Volam 
30416c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
30426c5657d0SVasundhara Volam 				       struct bnxt_coredump *coredump)
30436c5657d0SVasundhara Volam {
30446c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_list_input req = {0};
30456c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
30466c5657d0SVasundhara Volam 	int rc;
30476c5657d0SVasundhara Volam 
30486c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
30496c5657d0SVasundhara Volam 
30506c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_LIST_BUF_LEN;
30516c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
30526c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
30536c5657d0SVasundhara Volam 				     data_len);
30546c5657d0SVasundhara Volam 
30556c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
30566c5657d0SVasundhara Volam 	if (!rc) {
30576c5657d0SVasundhara Volam 		coredump->data = info.dest_buf;
30586c5657d0SVasundhara Volam 		coredump->data_size = info.dest_buf_size;
30596c5657d0SVasundhara Volam 		coredump->total_segs = info.segs;
30606c5657d0SVasundhara Volam 	}
30616c5657d0SVasundhara Volam 	return rc;
30626c5657d0SVasundhara Volam }
30636c5657d0SVasundhara Volam 
30646c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
30656c5657d0SVasundhara Volam 					   u16 segment_id)
30666c5657d0SVasundhara Volam {
30676c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_initiate_input req = {0};
30686c5657d0SVasundhara Volam 
30696c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
30706c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
30716c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
30726c5657d0SVasundhara Volam 
30736c5657d0SVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
30746c5657d0SVasundhara Volam }
30756c5657d0SVasundhara Volam 
30766c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
30776c5657d0SVasundhara Volam 					   u16 segment_id, u32 *seg_len,
30786c5657d0SVasundhara Volam 					   void *buf, u32 offset)
30796c5657d0SVasundhara Volam {
30806c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_retrieve_input req = {0};
30816c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
30826c5657d0SVasundhara Volam 	int rc;
30836c5657d0SVasundhara Volam 
30846c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
30856c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
30866c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
30876c5657d0SVasundhara Volam 
30886c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
30896c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
30906c5657d0SVasundhara Volam 				seq_no);
30916c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
30926c5657d0SVasundhara Volam 				     data_len);
30936c5657d0SVasundhara Volam 	if (buf)
30946c5657d0SVasundhara Volam 		info.dest_buf = buf + offset;
30956c5657d0SVasundhara Volam 
30966c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
30976c5657d0SVasundhara Volam 	if (!rc)
30986c5657d0SVasundhara Volam 		*seg_len = info.dest_buf_size;
30996c5657d0SVasundhara Volam 
31006c5657d0SVasundhara Volam 	return rc;
31016c5657d0SVasundhara Volam }
31026c5657d0SVasundhara Volam 
31036c5657d0SVasundhara Volam static void
31046c5657d0SVasundhara Volam bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
31056c5657d0SVasundhara Volam 			   struct bnxt_coredump_segment_hdr *seg_hdr,
31066c5657d0SVasundhara Volam 			   struct coredump_segment_record *seg_rec, u32 seg_len,
31076c5657d0SVasundhara Volam 			   int status, u32 duration, u32 instance)
31086c5657d0SVasundhara Volam {
31096c5657d0SVasundhara Volam 	memset(seg_hdr, 0, sizeof(*seg_hdr));
31108605212aSVasundhara Volam 	memcpy(seg_hdr->signature, "sEgM", 4);
31116c5657d0SVasundhara Volam 	if (seg_rec) {
31126c5657d0SVasundhara Volam 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
31136c5657d0SVasundhara Volam 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
31146c5657d0SVasundhara Volam 		seg_hdr->low_version = seg_rec->version_low;
31156c5657d0SVasundhara Volam 		seg_hdr->high_version = seg_rec->version_hi;
31166c5657d0SVasundhara Volam 	} else {
31176c5657d0SVasundhara Volam 		/* For hwrm_ver_get response Component id = 2
31186c5657d0SVasundhara Volam 		 * and Segment id = 0
31196c5657d0SVasundhara Volam 		 */
31206c5657d0SVasundhara Volam 		seg_hdr->component_id = cpu_to_le32(2);
31216c5657d0SVasundhara Volam 		seg_hdr->segment_id = 0;
31226c5657d0SVasundhara Volam 	}
31236c5657d0SVasundhara Volam 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
31246c5657d0SVasundhara Volam 	seg_hdr->length = cpu_to_le32(seg_len);
31256c5657d0SVasundhara Volam 	seg_hdr->status = cpu_to_le32(status);
31266c5657d0SVasundhara Volam 	seg_hdr->duration = cpu_to_le32(duration);
31276c5657d0SVasundhara Volam 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
31286c5657d0SVasundhara Volam 	seg_hdr->instance = cpu_to_le32(instance);
31296c5657d0SVasundhara Volam }
31306c5657d0SVasundhara Volam 
31316c5657d0SVasundhara Volam static void
31326c5657d0SVasundhara Volam bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
31336c5657d0SVasundhara Volam 			  time64_t start, s16 start_utc, u16 total_segs,
31346c5657d0SVasundhara Volam 			  int status)
31356c5657d0SVasundhara Volam {
31366c5657d0SVasundhara Volam 	time64_t end = ktime_get_real_seconds();
31376c5657d0SVasundhara Volam 	u32 os_ver_major = 0, os_ver_minor = 0;
31386c5657d0SVasundhara Volam 	struct tm tm;
31396c5657d0SVasundhara Volam 
31406c5657d0SVasundhara Volam 	time64_to_tm(start, 0, &tm);
31416c5657d0SVasundhara Volam 	memset(record, 0, sizeof(*record));
31428605212aSVasundhara Volam 	memcpy(record->signature, "cOrE", 4);
31436c5657d0SVasundhara Volam 	record->flags = 0;
31446c5657d0SVasundhara Volam 	record->low_version = 0;
31456c5657d0SVasundhara Volam 	record->high_version = 1;
31466c5657d0SVasundhara Volam 	record->asic_state = 0;
31473d46eee5SArnd Bergmann 	strlcpy(record->system_name, utsname()->nodename,
31483d46eee5SArnd Bergmann 		sizeof(record->system_name));
31498dc5ae2dSVasundhara Volam 	record->year = cpu_to_le16(tm.tm_year + 1900);
31508dc5ae2dSVasundhara Volam 	record->month = cpu_to_le16(tm.tm_mon + 1);
31516c5657d0SVasundhara Volam 	record->day = cpu_to_le16(tm.tm_mday);
31526c5657d0SVasundhara Volam 	record->hour = cpu_to_le16(tm.tm_hour);
31536c5657d0SVasundhara Volam 	record->minute = cpu_to_le16(tm.tm_min);
31546c5657d0SVasundhara Volam 	record->second = cpu_to_le16(tm.tm_sec);
31556c5657d0SVasundhara Volam 	record->utc_bias = cpu_to_le16(start_utc);
31566c5657d0SVasundhara Volam 	strcpy(record->commandline, "ethtool -w");
31576c5657d0SVasundhara Volam 	record->total_segments = cpu_to_le32(total_segs);
31586c5657d0SVasundhara Volam 
31596c5657d0SVasundhara Volam 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
31606c5657d0SVasundhara Volam 	record->os_ver_major = cpu_to_le32(os_ver_major);
31616c5657d0SVasundhara Volam 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
31626c5657d0SVasundhara Volam 
31638605212aSVasundhara Volam 	strlcpy(record->os_name, utsname()->sysname, 32);
31646c5657d0SVasundhara Volam 	time64_to_tm(end, 0, &tm);
31656c5657d0SVasundhara Volam 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
31666c5657d0SVasundhara Volam 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
31676c5657d0SVasundhara Volam 	record->end_day = cpu_to_le16(tm.tm_mday);
31686c5657d0SVasundhara Volam 	record->end_hour = cpu_to_le16(tm.tm_hour);
31696c5657d0SVasundhara Volam 	record->end_minute = cpu_to_le16(tm.tm_min);
31706c5657d0SVasundhara Volam 	record->end_second = cpu_to_le16(tm.tm_sec);
31716c5657d0SVasundhara Volam 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
31726c5657d0SVasundhara Volam 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
31736c5657d0SVasundhara Volam 				       bp->ver_resp.chip_rev << 8 |
31746c5657d0SVasundhara Volam 				       bp->ver_resp.chip_metal);
31756c5657d0SVasundhara Volam 	record->asic_id2 = 0;
31766c5657d0SVasundhara Volam 	record->coredump_status = cpu_to_le32(status);
31776c5657d0SVasundhara Volam 	record->ioctl_low_version = 0;
31786c5657d0SVasundhara Volam 	record->ioctl_high_version = 0;
31796c5657d0SVasundhara Volam }
31806c5657d0SVasundhara Volam 
31816c5657d0SVasundhara Volam static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
31826c5657d0SVasundhara Volam {
31836c5657d0SVasundhara Volam 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
31846c5657d0SVasundhara Volam 	struct coredump_segment_record *seg_record = NULL;
31856c5657d0SVasundhara Volam 	u32 offset = 0, seg_hdr_len, seg_record_len;
31866c5657d0SVasundhara Volam 	struct bnxt_coredump_segment_hdr seg_hdr;
31876c5657d0SVasundhara Volam 	struct bnxt_coredump coredump = {NULL};
31886c5657d0SVasundhara Volam 	time64_t start_time;
31896c5657d0SVasundhara Volam 	u16 start_utc;
31906c5657d0SVasundhara Volam 	int rc = 0, i;
31916c5657d0SVasundhara Volam 
31926c5657d0SVasundhara Volam 	start_time = ktime_get_real_seconds();
31936c5657d0SVasundhara Volam 	start_utc = sys_tz.tz_minuteswest * 60;
31946c5657d0SVasundhara Volam 	seg_hdr_len = sizeof(seg_hdr);
31956c5657d0SVasundhara Volam 
31966c5657d0SVasundhara Volam 	/* First segment should be hwrm_ver_get response */
31976c5657d0SVasundhara Volam 	*dump_len = seg_hdr_len + ver_get_resp_len;
31986c5657d0SVasundhara Volam 	if (buf) {
31996c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
32006c5657d0SVasundhara Volam 					   0, 0, 0);
32016c5657d0SVasundhara Volam 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
32026c5657d0SVasundhara Volam 		offset += seg_hdr_len;
32036c5657d0SVasundhara Volam 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
32046c5657d0SVasundhara Volam 		offset += ver_get_resp_len;
32056c5657d0SVasundhara Volam 	}
32066c5657d0SVasundhara Volam 
32076c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
32086c5657d0SVasundhara Volam 	if (rc) {
32096c5657d0SVasundhara Volam 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
32106c5657d0SVasundhara Volam 		goto err;
32116c5657d0SVasundhara Volam 	}
32126c5657d0SVasundhara Volam 
32136c5657d0SVasundhara Volam 	*dump_len += seg_hdr_len * coredump.total_segs;
32146c5657d0SVasundhara Volam 
32156c5657d0SVasundhara Volam 	seg_record = (struct coredump_segment_record *)coredump.data;
32166c5657d0SVasundhara Volam 	seg_record_len = sizeof(*seg_record);
32176c5657d0SVasundhara Volam 
32186c5657d0SVasundhara Volam 	for (i = 0; i < coredump.total_segs; i++) {
32196c5657d0SVasundhara Volam 		u16 comp_id = le16_to_cpu(seg_record->component_id);
32206c5657d0SVasundhara Volam 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
32216c5657d0SVasundhara Volam 		u32 duration = 0, seg_len = 0;
32226c5657d0SVasundhara Volam 		unsigned long start, end;
32236c5657d0SVasundhara Volam 
32246c5657d0SVasundhara Volam 		start = jiffies;
32256c5657d0SVasundhara Volam 
32266c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
32276c5657d0SVasundhara Volam 		if (rc) {
32286c5657d0SVasundhara Volam 			netdev_err(bp->dev,
32296c5657d0SVasundhara Volam 				   "Failed to initiate coredump for seg = %d\n",
32306c5657d0SVasundhara Volam 				   seg_record->segment_id);
32316c5657d0SVasundhara Volam 			goto next_seg;
32326c5657d0SVasundhara Volam 		}
32336c5657d0SVasundhara Volam 
32346c5657d0SVasundhara Volam 		/* Write segment data into the buffer */
32356c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
32366c5657d0SVasundhara Volam 						     &seg_len, buf,
32376c5657d0SVasundhara Volam 						     offset + seg_hdr_len);
32386c5657d0SVasundhara Volam 		if (rc)
32396c5657d0SVasundhara Volam 			netdev_err(bp->dev,
32406c5657d0SVasundhara Volam 				   "Failed to retrieve coredump for seg = %d\n",
32416c5657d0SVasundhara Volam 				   seg_record->segment_id);
32426c5657d0SVasundhara Volam 
32436c5657d0SVasundhara Volam next_seg:
32446c5657d0SVasundhara Volam 		end = jiffies;
32456c5657d0SVasundhara Volam 		duration = jiffies_to_msecs(end - start);
32466c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
32476c5657d0SVasundhara Volam 					   rc, duration, 0);
32486c5657d0SVasundhara Volam 
32496c5657d0SVasundhara Volam 		if (buf) {
32506c5657d0SVasundhara Volam 			/* Write segment header into the buffer */
32516c5657d0SVasundhara Volam 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
32526c5657d0SVasundhara Volam 			offset += seg_hdr_len + seg_len;
32536c5657d0SVasundhara Volam 		}
32546c5657d0SVasundhara Volam 
32556c5657d0SVasundhara Volam 		*dump_len += seg_len;
32566c5657d0SVasundhara Volam 		seg_record =
32576c5657d0SVasundhara Volam 			(struct coredump_segment_record *)((u8 *)seg_record +
32586c5657d0SVasundhara Volam 							   seg_record_len);
32596c5657d0SVasundhara Volam 	}
32606c5657d0SVasundhara Volam 
32616c5657d0SVasundhara Volam err:
32621bbf3aedSArnd Bergmann 	if (buf)
32631bbf3aedSArnd Bergmann 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
32646c5657d0SVasundhara Volam 					  start_utc, coredump.total_segs + 1,
32656c5657d0SVasundhara Volam 					  rc);
32666c5657d0SVasundhara Volam 	kfree(coredump.data);
32671bbf3aedSArnd Bergmann 	*dump_len += sizeof(struct bnxt_coredump_record);
32686c5657d0SVasundhara Volam 
32696c5657d0SVasundhara Volam 	return rc;
32706c5657d0SVasundhara Volam }
32716c5657d0SVasundhara Volam 
32726c5657d0SVasundhara Volam static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
32736c5657d0SVasundhara Volam {
32746c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
32756c5657d0SVasundhara Volam 
32766c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
32776c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
32786c5657d0SVasundhara Volam 
32796c5657d0SVasundhara Volam 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
32806c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_min_8b << 16 |
32816c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
32826c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_rsvd_8b;
32836c5657d0SVasundhara Volam 
32846c5657d0SVasundhara Volam 	return bnxt_get_coredump(bp, NULL, &dump->len);
32856c5657d0SVasundhara Volam }
32866c5657d0SVasundhara Volam 
32876c5657d0SVasundhara Volam static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
32886c5657d0SVasundhara Volam 			      void *buf)
32896c5657d0SVasundhara Volam {
32906c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
32916c5657d0SVasundhara Volam 
32926c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
32936c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
32946c5657d0SVasundhara Volam 
32956c5657d0SVasundhara Volam 	memset(buf, 0, dump->len);
32966c5657d0SVasundhara Volam 
32976c5657d0SVasundhara Volam 	return bnxt_get_coredump(bp, buf, &dump->len);
32986c5657d0SVasundhara Volam }
32996c5657d0SVasundhara Volam 
3300eb513658SMichael Chan void bnxt_ethtool_init(struct bnxt *bp)
3301eb513658SMichael Chan {
3302eb513658SMichael Chan 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3303eb513658SMichael Chan 	struct hwrm_selftest_qlist_input req = {0};
3304eb513658SMichael Chan 	struct bnxt_test_info *test_info;
3305431aa1ebSMichael Chan 	struct net_device *dev = bp->dev;
3306eb513658SMichael Chan 	int i, rc;
3307eb513658SMichael Chan 
3308691aa620SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PKG_VER))
3309a60faa60SVasundhara Volam 		bnxt_get_pkgver(dev);
3310431aa1ebSMichael Chan 
3311eb513658SMichael Chan 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp))
3312eb513658SMichael Chan 		return;
3313eb513658SMichael Chan 
3314eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3315eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3316eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3317eb513658SMichael Chan 	if (rc)
3318eb513658SMichael Chan 		goto ethtool_init_exit;
3319eb513658SMichael Chan 
3320eb513658SMichael Chan 	test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
3321eb513658SMichael Chan 	if (!test_info)
3322eb513658SMichael Chan 		goto ethtool_init_exit;
3323eb513658SMichael Chan 
3324eb513658SMichael Chan 	bp->test_info = test_info;
3325eb513658SMichael Chan 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
3326eb513658SMichael Chan 	if (bp->num_tests > BNXT_MAX_TEST)
3327eb513658SMichael Chan 		bp->num_tests = BNXT_MAX_TEST;
3328eb513658SMichael Chan 
3329eb513658SMichael Chan 	test_info->offline_mask = resp->offline_tests;
3330eb513658SMichael Chan 	test_info->timeout = le16_to_cpu(resp->test_timeout);
3331eb513658SMichael Chan 	if (!test_info->timeout)
3332eb513658SMichael Chan 		test_info->timeout = HWRM_CMD_TIMEOUT;
3333eb513658SMichael Chan 	for (i = 0; i < bp->num_tests; i++) {
3334eb513658SMichael Chan 		char *str = test_info->string[i];
3335eb513658SMichael Chan 		char *fw_str = resp->test0_name + i * 32;
3336eb513658SMichael Chan 
3337f7dc1ea6SMichael Chan 		if (i == BNXT_MACLPBK_TEST_IDX) {
3338f7dc1ea6SMichael Chan 			strcpy(str, "Mac loopback test (offline)");
333991725d89SMichael Chan 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
334091725d89SMichael Chan 			strcpy(str, "Phy loopback test (offline)");
334155fd0cf3SMichael Chan 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
334255fd0cf3SMichael Chan 			strcpy(str, "Ext loopback test (offline)");
334367fea463SMichael Chan 		} else if (i == BNXT_IRQ_TEST_IDX) {
334467fea463SMichael Chan 			strcpy(str, "Interrupt_test (offline)");
3345f7dc1ea6SMichael Chan 		} else {
3346eb513658SMichael Chan 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
3347eb513658SMichael Chan 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
3348eb513658SMichael Chan 			if (test_info->offline_mask & (1 << i))
3349eb513658SMichael Chan 				strncat(str, " (offline)",
3350eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3351eb513658SMichael Chan 			else
3352eb513658SMichael Chan 				strncat(str, " (online)",
3353eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3354eb513658SMichael Chan 		}
3355f7dc1ea6SMichael Chan 	}
3356eb513658SMichael Chan 
3357eb513658SMichael Chan ethtool_init_exit:
3358eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3359eb513658SMichael Chan }
3360eb513658SMichael Chan 
3361eb513658SMichael Chan void bnxt_ethtool_free(struct bnxt *bp)
3362eb513658SMichael Chan {
3363eb513658SMichael Chan 	kfree(bp->test_info);
3364eb513658SMichael Chan 	bp->test_info = NULL;
3365eb513658SMichael Chan }
3366eb513658SMichael Chan 
3367c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
336800c04a92SMichael Chan 	.get_link_ksettings	= bnxt_get_link_ksettings,
336900c04a92SMichael Chan 	.set_link_ksettings	= bnxt_set_link_ksettings,
3370c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
3371c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
3372c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
33738e202366SMichael Chan 	.get_wol		= bnxt_get_wol,
33745282db6cSMichael Chan 	.set_wol		= bnxt_set_wol,
3375c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
3376c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
3377c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
3378c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
3379c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
3380c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
3381c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
3382c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
3383c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
3384c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
3385c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
3386c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
3387a011952aSMichael Chan 	.set_rxnfc		= bnxt_set_rxnfc,
3388c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
3389c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
3390c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
3391c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
3392c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
3393c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
3394c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
3395c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
339672b34f04SMichael Chan 	.get_eee		= bnxt_get_eee,
339772b34f04SMichael Chan 	.set_eee		= bnxt_set_eee,
339842ee18feSAjit Khaparde 	.get_module_info	= bnxt_get_module_info,
339942ee18feSAjit Khaparde 	.get_module_eeprom	= bnxt_get_module_eeprom,
34005ad2cbeeSMichael Chan 	.nway_reset		= bnxt_nway_reset,
34015ad2cbeeSMichael Chan 	.set_phys_id		= bnxt_set_phys_id,
3402eb513658SMichael Chan 	.self_test		= bnxt_self_test,
340349f7972fSVasundhara Volam 	.reset			= bnxt_reset,
34046c5657d0SVasundhara Volam 	.get_dump_flag		= bnxt_get_dump_flag,
34056c5657d0SVasundhara Volam 	.get_dump_data		= bnxt_get_dump_data,
3406c0c050c5SMichael Chan };
3407