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>
148b277589SMichael Chan #include <linux/linkmode.h>
15c0c050c5SMichael Chan #include <linux/interrupt.h>
16c0c050c5SMichael Chan #include <linux/pci.h>
17c0c050c5SMichael Chan #include <linux/etherdevice.h>
18c0c050c5SMichael Chan #include <linux/crc32.h>
19c0c050c5SMichael Chan #include <linux/firmware.h>
206c5657d0SVasundhara Volam #include <linux/utsname.h>
216c5657d0SVasundhara Volam #include <linux/time.h>
22c0c050c5SMichael Chan #include "bnxt_hsi.h"
23c0c050c5SMichael Chan #include "bnxt.h"
24f7dc1ea6SMichael Chan #include "bnxt_xdp.h"
25c0c050c5SMichael Chan #include "bnxt_ethtool.h"
26c0c050c5SMichael Chan #include "bnxt_nvm_defs.h"	/* NVRAM content constant and structure defs */
27c0c050c5SMichael Chan #include "bnxt_fw_hdr.h"	/* Firmware hdr constant and structure defs */
286c5657d0SVasundhara Volam #include "bnxt_coredump.h"
29c0c050c5SMichael Chan #define FLASH_NVRAM_TIMEOUT	((HWRM_CMD_TIMEOUT) * 100)
305ac67d8bSRob Swindell #define FLASH_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
315ac67d8bSRob Swindell #define INSTALL_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
32c0c050c5SMichael Chan 
33c0c050c5SMichael Chan static u32 bnxt_get_msglevel(struct net_device *dev)
34c0c050c5SMichael Chan {
35c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
36c0c050c5SMichael Chan 
37c0c050c5SMichael Chan 	return bp->msg_enable;
38c0c050c5SMichael Chan }
39c0c050c5SMichael Chan 
40c0c050c5SMichael Chan static void bnxt_set_msglevel(struct net_device *dev, u32 value)
41c0c050c5SMichael Chan {
42c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
43c0c050c5SMichael Chan 
44c0c050c5SMichael Chan 	bp->msg_enable = value;
45c0c050c5SMichael Chan }
46c0c050c5SMichael Chan 
47c0c050c5SMichael Chan static int bnxt_get_coalesce(struct net_device *dev,
48c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
49c0c050c5SMichael Chan {
50c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5118775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
5218775aa8SMichael Chan 	u16 mult;
53c0c050c5SMichael Chan 
54c0c050c5SMichael Chan 	memset(coal, 0, sizeof(*coal));
55c0c050c5SMichael Chan 
566a8788f2SAndy Gospodarek 	coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM;
576a8788f2SAndy Gospodarek 
5818775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
5918775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
6018775aa8SMichael Chan 	coal->rx_coalesce_usecs = hw_coal->coal_ticks;
6118775aa8SMichael Chan 	coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6218775aa8SMichael Chan 	coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
6318775aa8SMichael Chan 	coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
64c0c050c5SMichael Chan 
6518775aa8SMichael Chan 	hw_coal = &bp->tx_coal;
6618775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
6718775aa8SMichael Chan 	coal->tx_coalesce_usecs = hw_coal->coal_ticks;
6818775aa8SMichael Chan 	coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6918775aa8SMichael Chan 	coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
7018775aa8SMichael Chan 	coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
71dfc9c94aSMichael Chan 
7251f30785SMichael Chan 	coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
7351f30785SMichael Chan 
74c0c050c5SMichael Chan 	return 0;
75c0c050c5SMichael Chan }
76c0c050c5SMichael Chan 
77c0c050c5SMichael Chan static int bnxt_set_coalesce(struct net_device *dev,
78c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
79c0c050c5SMichael Chan {
80c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
8151f30785SMichael Chan 	bool update_stats = false;
8218775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
83c0c050c5SMichael Chan 	int rc = 0;
8418775aa8SMichael Chan 	u16 mult;
85c0c050c5SMichael Chan 
866a8788f2SAndy Gospodarek 	if (coal->use_adaptive_rx_coalesce) {
876a8788f2SAndy Gospodarek 		bp->flags |= BNXT_FLAG_DIM;
886a8788f2SAndy Gospodarek 	} else {
896a8788f2SAndy Gospodarek 		if (bp->flags & BNXT_FLAG_DIM) {
906a8788f2SAndy Gospodarek 			bp->flags &= ~(BNXT_FLAG_DIM);
916a8788f2SAndy Gospodarek 			goto reset_coalesce;
926a8788f2SAndy Gospodarek 		}
936a8788f2SAndy Gospodarek 	}
946a8788f2SAndy Gospodarek 
9518775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
9618775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
9718775aa8SMichael Chan 	hw_coal->coal_ticks = coal->rx_coalesce_usecs;
9818775aa8SMichael Chan 	hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult;
9918775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq;
10018775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult;
101c0c050c5SMichael Chan 
102de4a10efSAndy Gospodarek 	hw_coal = &bp->tx_coal;
10318775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
10418775aa8SMichael Chan 	hw_coal->coal_ticks = coal->tx_coalesce_usecs;
10518775aa8SMichael Chan 	hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult;
10618775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq;
10718775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult;
108dfc9c94aSMichael Chan 
10951f30785SMichael Chan 	if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
11051f30785SMichael Chan 		u32 stats_ticks = coal->stats_block_coalesce_usecs;
11151f30785SMichael Chan 
112adcc331eSMichael Chan 		/* Allow 0, which means disable. */
113adcc331eSMichael Chan 		if (stats_ticks)
11451f30785SMichael Chan 			stats_ticks = clamp_t(u32, stats_ticks,
11551f30785SMichael Chan 					      BNXT_MIN_STATS_COAL_TICKS,
11651f30785SMichael Chan 					      BNXT_MAX_STATS_COAL_TICKS);
11751f30785SMichael Chan 		stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
11851f30785SMichael Chan 		bp->stats_coal_ticks = stats_ticks;
119e795892eSMichael Chan 		if (bp->stats_coal_ticks)
120e795892eSMichael Chan 			bp->current_interval =
121e795892eSMichael Chan 				bp->stats_coal_ticks * HZ / 1000000;
122e795892eSMichael Chan 		else
123e795892eSMichael Chan 			bp->current_interval = BNXT_TIMER_INTERVAL;
12451f30785SMichael Chan 		update_stats = true;
12551f30785SMichael Chan 	}
12651f30785SMichael Chan 
1276a8788f2SAndy Gospodarek reset_coalesce:
12851f30785SMichael Chan 	if (netif_running(dev)) {
12951f30785SMichael Chan 		if (update_stats) {
13051f30785SMichael Chan 			rc = bnxt_close_nic(bp, true, false);
13151f30785SMichael Chan 			if (!rc)
13251f30785SMichael Chan 				rc = bnxt_open_nic(bp, true, false);
13351f30785SMichael Chan 		} else {
134c0c050c5SMichael Chan 			rc = bnxt_hwrm_set_coal(bp);
13551f30785SMichael Chan 		}
13651f30785SMichael Chan 	}
137c0c050c5SMichael Chan 
138c0c050c5SMichael Chan 	return rc;
139c0c050c5SMichael Chan }
140c0c050c5SMichael Chan 
1413316d509SMichael Chan static const char * const bnxt_ring_rx_stats_str[] = {
142ee79566eSMichael Chan 	"rx_ucast_packets",
143ee79566eSMichael Chan 	"rx_mcast_packets",
144ee79566eSMichael Chan 	"rx_bcast_packets",
145ee79566eSMichael Chan 	"rx_discards",
146bfc6e5fbSMichael Chan 	"rx_errors",
147ee79566eSMichael Chan 	"rx_ucast_bytes",
148ee79566eSMichael Chan 	"rx_mcast_bytes",
149ee79566eSMichael Chan 	"rx_bcast_bytes",
1503316d509SMichael Chan };
1513316d509SMichael Chan 
1523316d509SMichael Chan static const char * const bnxt_ring_tx_stats_str[] = {
153ee79566eSMichael Chan 	"tx_ucast_packets",
154ee79566eSMichael Chan 	"tx_mcast_packets",
155ee79566eSMichael Chan 	"tx_bcast_packets",
156bfc6e5fbSMichael Chan 	"tx_errors",
157ee79566eSMichael Chan 	"tx_discards",
158ee79566eSMichael Chan 	"tx_ucast_bytes",
159ee79566eSMichael Chan 	"tx_mcast_bytes",
160ee79566eSMichael Chan 	"tx_bcast_bytes",
161ee79566eSMichael Chan };
162ee79566eSMichael Chan 
163ee79566eSMichael Chan static const char * const bnxt_ring_tpa_stats_str[] = {
164ee79566eSMichael Chan 	"tpa_packets",
165ee79566eSMichael Chan 	"tpa_bytes",
166ee79566eSMichael Chan 	"tpa_events",
167ee79566eSMichael Chan 	"tpa_aborts",
168ee79566eSMichael Chan };
169ee79566eSMichael Chan 
17078e7b866SMichael Chan static const char * const bnxt_ring_tpa2_stats_str[] = {
17178e7b866SMichael Chan 	"rx_tpa_eligible_pkt",
17278e7b866SMichael Chan 	"rx_tpa_eligible_bytes",
17378e7b866SMichael Chan 	"rx_tpa_pkt",
17478e7b866SMichael Chan 	"rx_tpa_bytes",
17578e7b866SMichael Chan 	"rx_tpa_errors",
1769d6b648cSMichael Chan 	"rx_tpa_events",
17778e7b866SMichael Chan };
17878e7b866SMichael Chan 
1799d8b5f05SMichael Chan static const char * const bnxt_rx_sw_stats_str[] = {
180ee79566eSMichael Chan 	"rx_l4_csum_errors",
1818a27d4b9SMichael Chan 	"rx_resets",
18219b3751fSMichael Chan 	"rx_buf_errors",
1839d8b5f05SMichael Chan };
1849d8b5f05SMichael Chan 
1859d8b5f05SMichael Chan static const char * const bnxt_cmn_sw_stats_str[] = {
186ee79566eSMichael Chan 	"missed_irqs",
187ee79566eSMichael Chan };
188c0c050c5SMichael Chan 
1898ddc9aaaSMichael Chan #define BNXT_RX_STATS_ENTRY(counter)	\
1908ddc9aaaSMichael Chan 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
1918ddc9aaaSMichael Chan 
1928ddc9aaaSMichael Chan #define BNXT_TX_STATS_ENTRY(counter)	\
1938ddc9aaaSMichael Chan 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
1948ddc9aaaSMichael Chan 
19500db3cbaSVasundhara Volam #define BNXT_RX_STATS_EXT_ENTRY(counter)	\
19600db3cbaSVasundhara Volam 	{ BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
19700db3cbaSVasundhara Volam 
19836e53349SMichael Chan #define BNXT_TX_STATS_EXT_ENTRY(counter)	\
19936e53349SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter), __stringify(counter) }
20036e53349SMichael Chan 
20136e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRY(n)				\
20236e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_duration_us),	\
20336e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_transitions)
20436e53349SMichael Chan 
20536e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRY(n)				\
20636e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_duration_us),	\
20736e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_transitions)
20836e53349SMichael Chan 
20936e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRIES				\
21036e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(0),				\
21136e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(1),				\
21236e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(2),				\
21336e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(3),				\
21436e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(4),				\
21536e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(5),				\
21636e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(6),				\
21736e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(7)
21836e53349SMichael Chan 
21936e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRIES				\
22036e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(0),				\
22136e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(1),				\
22236e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(2),				\
22336e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(3),				\
22436e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(4),				\
22536e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(5),				\
22636e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(6),				\
22736e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(7)
22836e53349SMichael Chan 
22936e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRY(n)				\
23036e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bytes_cos##n),		\
23136e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_packets_cos##n)
23236e53349SMichael Chan 
23336e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRY(n)				\
23436e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_bytes_cos##n),		\
23536e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_packets_cos##n)
23636e53349SMichael Chan 
23736e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRIES				\
23836e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(0),				\
23936e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(1),				\
24036e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(2),				\
24136e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(3),				\
24236e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(4),				\
24336e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(5),				\
24436e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(6),				\
24536e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(7)				\
24636e53349SMichael Chan 
24736e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRIES				\
24836e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(0),				\
24936e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(1),				\
25036e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(2),				\
25136e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(3),				\
25236e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(4),				\
25336e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(5),				\
25436e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(6),				\
25536e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(7)				\
25636e53349SMichael Chan 
2572792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(n)			\
2582792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_bytes_cos##n),	\
2592792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_packets_cos##n)
2602792b5b9SMichael Chan 
2612792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES				\
2622792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(0),				\
2632792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(1),				\
2642792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(2),				\
2652792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(3),				\
2662792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(4),				\
2672792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(5),				\
2682792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(6),				\
2692792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(7)
2702792b5b9SMichael Chan 
271e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRY(counter, n)		\
272e37fed79SMichael Chan 	{ BNXT_RX_STATS_EXT_OFFSET(counter##_cos0),	\
273e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
274e37fed79SMichael Chan 
275e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRY(counter, n)		\
276e37fed79SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter##_cos0),	\
277e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
278e37fed79SMichael Chan 
279e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRIES(counter)		\
280e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 0),		\
281e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 1),		\
282e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 2),		\
283e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 3),		\
284e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 4),		\
285e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 5),		\
286e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 6),		\
287e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 7)
288e37fed79SMichael Chan 
289e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRIES(counter)		\
290e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 0),		\
291e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 1),		\
292e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 2),		\
293e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 3),		\
294e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 4),		\
295e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 5),		\
296e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 6),		\
297e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 7)
298e37fed79SMichael Chan 
29920c1d28eSVasundhara Volam enum {
30020c1d28eSVasundhara Volam 	RX_TOTAL_DISCARDS,
30120c1d28eSVasundhara Volam 	TX_TOTAL_DISCARDS,
30220c1d28eSVasundhara Volam };
30320c1d28eSVasundhara Volam 
30420c1d28eSVasundhara Volam static struct {
30520c1d28eSVasundhara Volam 	u64			counter;
30620c1d28eSVasundhara Volam 	char			string[ETH_GSTRING_LEN];
30720c1d28eSVasundhara Volam } bnxt_sw_func_stats[] = {
30820c1d28eSVasundhara Volam 	{0, "rx_total_discard_pkts"},
30920c1d28eSVasundhara Volam 	{0, "tx_total_discard_pkts"},
31020c1d28eSVasundhara Volam };
31120c1d28eSVasundhara Volam 
3123316d509SMichael Chan #define NUM_RING_RX_SW_STATS		ARRAY_SIZE(bnxt_rx_sw_stats_str)
3133316d509SMichael Chan #define NUM_RING_CMN_SW_STATS		ARRAY_SIZE(bnxt_cmn_sw_stats_str)
3143316d509SMichael Chan #define NUM_RING_RX_HW_STATS		ARRAY_SIZE(bnxt_ring_rx_stats_str)
3153316d509SMichael Chan #define NUM_RING_TX_HW_STATS		ARRAY_SIZE(bnxt_ring_tx_stats_str)
3163316d509SMichael Chan 
3178ddc9aaaSMichael Chan static const struct {
3188ddc9aaaSMichael Chan 	long offset;
3198ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
3208ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
3218ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
3228ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
3238ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
3248ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
3258ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
3266fc92c33SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
3278ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
3288ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
3298ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
3308ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
3318ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
3328ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
3338ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
3348ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
3358ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
3368ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
3378ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
3388ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
3398ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
3408ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
3418ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
3428ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
3438ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
3448ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
3458ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
3468ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
347c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
348c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
349c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
350c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
351c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
352c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
353c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
354c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
3558ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
3568ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
3578ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
3588ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
3598ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
3608ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
361699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
362699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_err),
3638ddc9aaaSMichael Chan 
3648ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
3658ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
3668ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
3678ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
3688ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
3696fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
3708ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
3716fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
3728ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
3738ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
3748ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
3758ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
3768ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
3778ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
3788ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
3798ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
3808ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
3818ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
3828ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
3838ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
3848ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
3858ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
386c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
387c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
388c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
389c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
390c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
391c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
392c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
393c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
3948ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
3958ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
3968ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
3978ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
398699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
399699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
400699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_error),
4018ddc9aaaSMichael Chan };
4028ddc9aaaSMichael Chan 
40300db3cbaSVasundhara Volam static const struct {
40400db3cbaSVasundhara Volam 	long offset;
40500db3cbaSVasundhara Volam 	char string[ETH_GSTRING_LEN];
40600db3cbaSVasundhara Volam } bnxt_port_stats_ext_arr[] = {
40700db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
40800db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
40900db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
41000db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
41100db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
41236e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRIES,
41336e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRIES,
4144a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bits),
4154a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_buffer_passed_threshold),
4164a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_pcs_symbol_err),
4174a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_corrected_bits),
4182792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES,
41936e53349SMichael Chan };
42036e53349SMichael Chan 
42136e53349SMichael Chan static const struct {
42236e53349SMichael Chan 	long offset;
42336e53349SMichael Chan 	char string[ETH_GSTRING_LEN];
42436e53349SMichael Chan } bnxt_tx_port_stats_ext_arr[] = {
42536e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRIES,
42636e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRIES,
42700db3cbaSVasundhara Volam };
42800db3cbaSVasundhara Volam 
429e37fed79SMichael Chan static const struct {
430e37fed79SMichael Chan 	long base_off;
431e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
432e37fed79SMichael Chan } bnxt_rx_bytes_pri_arr[] = {
433e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_bytes),
434e37fed79SMichael Chan };
435e37fed79SMichael Chan 
436e37fed79SMichael Chan static const struct {
437e37fed79SMichael Chan 	long base_off;
438e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
439e37fed79SMichael Chan } bnxt_rx_pkts_pri_arr[] = {
440e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_packets),
441e37fed79SMichael Chan };
442e37fed79SMichael Chan 
443e37fed79SMichael Chan static const struct {
444e37fed79SMichael Chan 	long base_off;
445e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
446e37fed79SMichael Chan } bnxt_tx_bytes_pri_arr[] = {
447e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_bytes),
448e37fed79SMichael Chan };
449e37fed79SMichael Chan 
450e37fed79SMichael Chan static const struct {
451e37fed79SMichael Chan 	long base_off;
452e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
453e37fed79SMichael Chan } bnxt_tx_pkts_pri_arr[] = {
454e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_packets),
455e37fed79SMichael Chan };
456e37fed79SMichael Chan 
45720c1d28eSVasundhara Volam #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
4588ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
459e37fed79SMichael Chan #define BNXT_NUM_STATS_PRI			\
460e37fed79SMichael Chan 	(ARRAY_SIZE(bnxt_rx_bytes_pri_arr) +	\
461e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_rx_pkts_pri_arr) +	\
462e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_bytes_pri_arr) +	\
463e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_pkts_pri_arr))
4648ddc9aaaSMichael Chan 
46578e7b866SMichael Chan static int bnxt_get_num_tpa_ring_stats(struct bnxt *bp)
46678e7b866SMichael Chan {
46778e7b866SMichael Chan 	if (BNXT_SUPPORTS_TPA(bp)) {
4689d6b648cSMichael Chan 		if (bp->max_tpa_v2) {
4699d6b648cSMichael Chan 			if (BNXT_CHIP_P5_THOR(bp))
4709d6b648cSMichael Chan 				return BNXT_NUM_TPA_RING_STATS_P5;
4719d6b648cSMichael Chan 			return BNXT_NUM_TPA_RING_STATS_P5_SR2;
4729d6b648cSMichael Chan 		}
4739d6b648cSMichael Chan 		return BNXT_NUM_TPA_RING_STATS;
47478e7b866SMichael Chan 	}
47578e7b866SMichael Chan 	return 0;
47678e7b866SMichael Chan }
47778e7b866SMichael Chan 
478ee79566eSMichael Chan static int bnxt_get_num_ring_stats(struct bnxt *bp)
479ee79566eSMichael Chan {
4803316d509SMichael Chan 	int rx, tx, cmn;
481ee79566eSMichael Chan 
4823316d509SMichael Chan 	rx = NUM_RING_RX_HW_STATS + NUM_RING_RX_SW_STATS +
48378e7b866SMichael Chan 	     bnxt_get_num_tpa_ring_stats(bp);
4843316d509SMichael Chan 	tx = NUM_RING_TX_HW_STATS;
4853316d509SMichael Chan 	cmn = NUM_RING_CMN_SW_STATS;
486125592fbSRajesh Ravi 	return rx * bp->rx_nr_rings + tx * bp->tx_nr_rings +
487125592fbSRajesh Ravi 	       cmn * bp->cp_nr_rings;
488ee79566eSMichael Chan }
489ee79566eSMichael Chan 
4905c8227d0SMichael Chan static int bnxt_get_num_stats(struct bnxt *bp)
491c0c050c5SMichael Chan {
492ee79566eSMichael Chan 	int num_stats = bnxt_get_num_ring_stats(bp);
4938ddc9aaaSMichael Chan 
49420c1d28eSVasundhara Volam 	num_stats += BNXT_NUM_SW_FUNC_STATS;
49520c1d28eSVasundhara Volam 
4968ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS)
4978ddc9aaaSMichael Chan 		num_stats += BNXT_NUM_PORT_STATS;
4988ddc9aaaSMichael Chan 
499e37fed79SMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
50036e53349SMichael Chan 		num_stats += bp->fw_rx_stats_ext_size +
50136e53349SMichael Chan 			     bp->fw_tx_stats_ext_size;
502e37fed79SMichael Chan 		if (bp->pri2cos_valid)
503e37fed79SMichael Chan 			num_stats += BNXT_NUM_STATS_PRI;
504e37fed79SMichael Chan 	}
50500db3cbaSVasundhara Volam 
5068ddc9aaaSMichael Chan 	return num_stats;
5078ddc9aaaSMichael Chan }
5085c8227d0SMichael Chan 
5095c8227d0SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
5105c8227d0SMichael Chan {
5115c8227d0SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5125c8227d0SMichael Chan 
5135c8227d0SMichael Chan 	switch (sset) {
5145c8227d0SMichael Chan 	case ETH_SS_STATS:
5155c8227d0SMichael Chan 		return bnxt_get_num_stats(bp);
516eb513658SMichael Chan 	case ETH_SS_TEST:
517eb513658SMichael Chan 		if (!bp->num_tests)
518eb513658SMichael Chan 			return -EOPNOTSUPP;
519eb513658SMichael Chan 		return bp->num_tests;
520c0c050c5SMichael Chan 	default:
521c0c050c5SMichael Chan 		return -EOPNOTSUPP;
522c0c050c5SMichael Chan 	}
523c0c050c5SMichael Chan }
524c0c050c5SMichael Chan 
525125592fbSRajesh Ravi static bool is_rx_ring(struct bnxt *bp, int ring_num)
526125592fbSRajesh Ravi {
527125592fbSRajesh Ravi 	return ring_num < bp->rx_nr_rings;
528125592fbSRajesh Ravi }
529125592fbSRajesh Ravi 
530125592fbSRajesh Ravi static bool is_tx_ring(struct bnxt *bp, int ring_num)
531125592fbSRajesh Ravi {
532125592fbSRajesh Ravi 	int tx_base = 0;
533125592fbSRajesh Ravi 
534125592fbSRajesh Ravi 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
535125592fbSRajesh Ravi 		tx_base = bp->rx_nr_rings;
536125592fbSRajesh Ravi 
537125592fbSRajesh Ravi 	if (ring_num >= tx_base && ring_num < (tx_base + bp->tx_nr_rings))
538125592fbSRajesh Ravi 		return true;
539125592fbSRajesh Ravi 	return false;
540125592fbSRajesh Ravi }
541125592fbSRajesh Ravi 
542c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
543c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
544c0c050c5SMichael Chan {
545c0c050c5SMichael Chan 	u32 i, j = 0;
546c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
547125592fbSRajesh Ravi 	u32 tpa_stats;
548c0c050c5SMichael Chan 
549fd3ab1c7SMichael Chan 	if (!bp->bnapi) {
550ee79566eSMichael Chan 		j += bnxt_get_num_ring_stats(bp) + BNXT_NUM_SW_FUNC_STATS;
551fd3ab1c7SMichael Chan 		goto skip_ring_stats;
552fd3ab1c7SMichael Chan 	}
553c0c050c5SMichael Chan 
55420c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
55520c1d28eSVasundhara Volam 		bnxt_sw_func_stats[i].counter = 0;
55620c1d28eSVasundhara Volam 
557125592fbSRajesh Ravi 	tpa_stats = bnxt_get_num_tpa_ring_stats(bp);
558c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
559c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
560c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
561a0c30621SMichael Chan 		u64 *sw_stats = cpr->stats.sw_stats;
5629d8b5f05SMichael Chan 		u64 *sw;
563c0c050c5SMichael Chan 		int k;
564c0c050c5SMichael Chan 
565125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
566125592fbSRajesh Ravi 			for (k = 0; k < NUM_RING_RX_HW_STATS; j++, k++)
567a0c30621SMichael Chan 				buf[j] = sw_stats[k];
568125592fbSRajesh Ravi 		}
569125592fbSRajesh Ravi 		if (is_tx_ring(bp, i)) {
570125592fbSRajesh Ravi 			k = NUM_RING_RX_HW_STATS;
571125592fbSRajesh Ravi 			for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
572125592fbSRajesh Ravi 			       j++, k++)
573a0c30621SMichael Chan 				buf[j] = sw_stats[k];
574125592fbSRajesh Ravi 		}
575125592fbSRajesh Ravi 		if (!tpa_stats || !is_rx_ring(bp, i))
576125592fbSRajesh Ravi 			goto skip_tpa_ring_stats;
577125592fbSRajesh Ravi 
578125592fbSRajesh Ravi 		k = NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
579125592fbSRajesh Ravi 		for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS +
580125592fbSRajesh Ravi 			   tpa_stats; j++, k++)
581a0c30621SMichael Chan 			buf[j] = sw_stats[k];
5829d8b5f05SMichael Chan 
583125592fbSRajesh Ravi skip_tpa_ring_stats:
5849d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.rx;
585125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
5863316d509SMichael Chan 			for (k = 0; k < NUM_RING_RX_SW_STATS; j++, k++)
5879d8b5f05SMichael Chan 				buf[j] = sw[k];
588125592fbSRajesh Ravi 		}
5899d8b5f05SMichael Chan 
5909d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.cmn;
5913316d509SMichael Chan 		for (k = 0; k < NUM_RING_CMN_SW_STATS; j++, k++)
5929d8b5f05SMichael Chan 			buf[j] = sw[k];
59320c1d28eSVasundhara Volam 
59420c1d28eSVasundhara Volam 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
595a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, rx_discard_pkts);
59620c1d28eSVasundhara Volam 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
597a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, tx_discard_pkts);
598c0c050c5SMichael Chan 	}
59920c1d28eSVasundhara Volam 
60020c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
60120c1d28eSVasundhara Volam 		buf[j] = bnxt_sw_func_stats[i].counter;
60220c1d28eSVasundhara Volam 
603fd3ab1c7SMichael Chan skip_ring_stats:
6048ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
605a0c30621SMichael Chan 		u64 *port_stats = bp->port_stats.sw_stats;
6068ddc9aaaSMichael Chan 
607a0c30621SMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++)
608a0c30621SMichael Chan 			buf[j] = *(port_stats + bnxt_port_stats_arr[i].offset);
6098ddc9aaaSMichael Chan 	}
61000db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
611a0c30621SMichael Chan 		u64 *rx_port_stats_ext = bp->rx_port_stats_ext.sw_stats;
612a0c30621SMichael Chan 		u64 *tx_port_stats_ext = bp->tx_port_stats_ext.sw_stats;
61300db3cbaSVasundhara Volam 
61436e53349SMichael Chan 		for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
615a0c30621SMichael Chan 			buf[j] = *(rx_port_stats_ext +
616a0c30621SMichael Chan 				   bnxt_port_stats_ext_arr[i].offset);
61700db3cbaSVasundhara Volam 		}
61836e53349SMichael Chan 		for (i = 0; i < bp->fw_tx_stats_ext_size; i++, j++) {
619a0c30621SMichael Chan 			buf[j] = *(tx_port_stats_ext +
620a0c30621SMichael Chan 				   bnxt_tx_port_stats_ext_arr[i].offset);
62136e53349SMichael Chan 		}
622e37fed79SMichael Chan 		if (bp->pri2cos_valid) {
623e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
624e37fed79SMichael Chan 				long n = bnxt_rx_bytes_pri_arr[i].base_off +
625a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
626e37fed79SMichael Chan 
627a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
628e37fed79SMichael Chan 			}
629e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
630e37fed79SMichael Chan 				long n = bnxt_rx_pkts_pri_arr[i].base_off +
631a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
632e37fed79SMichael Chan 
633a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
634e37fed79SMichael Chan 			}
635e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
636e37fed79SMichael Chan 				long n = bnxt_tx_bytes_pri_arr[i].base_off +
637a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
638e37fed79SMichael Chan 
639a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
640e37fed79SMichael Chan 			}
641e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
642e37fed79SMichael Chan 				long n = bnxt_tx_pkts_pri_arr[i].base_off +
643a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
644e37fed79SMichael Chan 
645a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
646e37fed79SMichael Chan 			}
647e37fed79SMichael Chan 		}
64800db3cbaSVasundhara Volam 	}
649c0c050c5SMichael Chan }
650c0c050c5SMichael Chan 
651c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
652c0c050c5SMichael Chan {
653c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
65478e7b866SMichael Chan 	static const char * const *str;
655ee79566eSMichael Chan 	u32 i, j, num_str;
656c0c050c5SMichael Chan 
657c0c050c5SMichael Chan 	switch (stringset) {
658c0c050c5SMichael Chan 	case ETH_SS_STATS:
659c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
660125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6613316d509SMichael Chan 				num_str = NUM_RING_RX_HW_STATS;
662ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
663ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
6643316d509SMichael Chan 						bnxt_ring_rx_stats_str[j]);
665c0c050c5SMichael Chan 					buf += ETH_GSTRING_LEN;
666ee79566eSMichael Chan 				}
667125592fbSRajesh Ravi 			}
668125592fbSRajesh Ravi 			if (is_tx_ring(bp, i)) {
6693316d509SMichael Chan 				num_str = NUM_RING_TX_HW_STATS;
6703316d509SMichael Chan 				for (j = 0; j < num_str; j++) {
6713316d509SMichael Chan 					sprintf(buf, "[%d]: %s", i,
6723316d509SMichael Chan 						bnxt_ring_tx_stats_str[j]);
6733316d509SMichael Chan 					buf += ETH_GSTRING_LEN;
6743316d509SMichael Chan 				}
675125592fbSRajesh Ravi 			}
6763316d509SMichael Chan 			num_str = bnxt_get_num_tpa_ring_stats(bp);
677125592fbSRajesh Ravi 			if (!num_str || !is_rx_ring(bp, i))
678ee79566eSMichael Chan 				goto skip_tpa_stats;
679ee79566eSMichael Chan 
6803316d509SMichael Chan 			if (bp->max_tpa_v2)
68178e7b866SMichael Chan 				str = bnxt_ring_tpa2_stats_str;
6823316d509SMichael Chan 			else
68378e7b866SMichael Chan 				str = bnxt_ring_tpa_stats_str;
6843316d509SMichael Chan 
685ee79566eSMichael Chan 			for (j = 0; j < num_str; j++) {
68678e7b866SMichael Chan 				sprintf(buf, "[%d]: %s", i, str[j]);
687c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
688ee79566eSMichael Chan 			}
689ee79566eSMichael Chan skip_tpa_stats:
690125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6913316d509SMichael Chan 				num_str = NUM_RING_RX_SW_STATS;
692ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
693ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
6949d8b5f05SMichael Chan 						bnxt_rx_sw_stats_str[j]);
6959d8b5f05SMichael Chan 					buf += ETH_GSTRING_LEN;
6969d8b5f05SMichael Chan 				}
697125592fbSRajesh Ravi 			}
6983316d509SMichael Chan 			num_str = NUM_RING_CMN_SW_STATS;
6999d8b5f05SMichael Chan 			for (j = 0; j < num_str; j++) {
7009d8b5f05SMichael Chan 				sprintf(buf, "[%d]: %s", i,
7019d8b5f05SMichael Chan 					bnxt_cmn_sw_stats_str[j]);
702c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
703ee79566eSMichael Chan 			}
704c0c050c5SMichael Chan 		}
70520c1d28eSVasundhara Volam 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
70620c1d28eSVasundhara Volam 			strcpy(buf, bnxt_sw_func_stats[i].string);
70720c1d28eSVasundhara Volam 			buf += ETH_GSTRING_LEN;
70820c1d28eSVasundhara Volam 		}
70920c1d28eSVasundhara Volam 
7108ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
7118ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
7128ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
7138ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
7148ddc9aaaSMichael Chan 			}
7158ddc9aaaSMichael Chan 		}
71600db3cbaSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
71736e53349SMichael Chan 			for (i = 0; i < bp->fw_rx_stats_ext_size; i++) {
71800db3cbaSVasundhara Volam 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
71900db3cbaSVasundhara Volam 				buf += ETH_GSTRING_LEN;
72000db3cbaSVasundhara Volam 			}
72136e53349SMichael Chan 			for (i = 0; i < bp->fw_tx_stats_ext_size; i++) {
72236e53349SMichael Chan 				strcpy(buf,
72336e53349SMichael Chan 				       bnxt_tx_port_stats_ext_arr[i].string);
72436e53349SMichael Chan 				buf += ETH_GSTRING_LEN;
72536e53349SMichael Chan 			}
726e37fed79SMichael Chan 			if (bp->pri2cos_valid) {
727e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
728e37fed79SMichael Chan 					strcpy(buf,
729e37fed79SMichael Chan 					       bnxt_rx_bytes_pri_arr[i].string);
730e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
731e37fed79SMichael Chan 				}
732e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
733e37fed79SMichael Chan 					strcpy(buf,
734e37fed79SMichael Chan 					       bnxt_rx_pkts_pri_arr[i].string);
735e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
736e37fed79SMichael Chan 				}
737e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
738e37fed79SMichael Chan 					strcpy(buf,
739e37fed79SMichael Chan 					       bnxt_tx_bytes_pri_arr[i].string);
740e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
741e37fed79SMichael Chan 				}
742e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
743e37fed79SMichael Chan 					strcpy(buf,
744e37fed79SMichael Chan 					       bnxt_tx_pkts_pri_arr[i].string);
745e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
746e37fed79SMichael Chan 				}
747e37fed79SMichael Chan 			}
74800db3cbaSVasundhara Volam 		}
749c0c050c5SMichael Chan 		break;
750eb513658SMichael Chan 	case ETH_SS_TEST:
751eb513658SMichael Chan 		if (bp->num_tests)
752eb513658SMichael Chan 			memcpy(buf, bp->test_info->string,
753eb513658SMichael Chan 			       bp->num_tests * ETH_GSTRING_LEN);
754eb513658SMichael Chan 		break;
755c0c050c5SMichael Chan 	default:
756c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
757c0c050c5SMichael Chan 			   stringset);
758c0c050c5SMichael Chan 		break;
759c0c050c5SMichael Chan 	}
760c0c050c5SMichael Chan }
761c0c050c5SMichael Chan 
762c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
763c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
764c0c050c5SMichael Chan {
765c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
766c0c050c5SMichael Chan 
767c0c050c5SMichael Chan 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
768c0c050c5SMichael Chan 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
769c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
770c0c050c5SMichael Chan 
771c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
772c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
773c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
774c0c050c5SMichael Chan }
775c0c050c5SMichael Chan 
776c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
777c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
778c0c050c5SMichael Chan {
779c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
780c0c050c5SMichael Chan 
781c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
782c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
783c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
784c0c050c5SMichael Chan 		return -EINVAL;
785c0c050c5SMichael Chan 
786c0c050c5SMichael Chan 	if (netif_running(dev))
787c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
788c0c050c5SMichael Chan 
789c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
790c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
791c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
792c0c050c5SMichael Chan 
793c0c050c5SMichael Chan 	if (netif_running(dev))
794c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
795c0c050c5SMichael Chan 
796c0c050c5SMichael Chan 	return 0;
797c0c050c5SMichael Chan }
798c0c050c5SMichael Chan 
799c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
800c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
801c0c050c5SMichael Chan {
802c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
803db4723b3SMichael Chan 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
804c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
8054301304bSMichael Chan 	int max_tx_sch_inputs, tx_grps;
806db4723b3SMichael Chan 
807db4723b3SMichael Chan 	/* Get the most up-to-date max_tx_sch_inputs. */
808c1c2d774SPavan Chebbi 	if (netif_running(dev) && BNXT_NEW_RM(bp))
809db4723b3SMichael Chan 		bnxt_hwrm_func_resc_qcaps(bp, false);
810db4723b3SMichael Chan 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
811c0c050c5SMichael Chan 
8126e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
813db4723b3SMichael Chan 	if (max_tx_sch_inputs)
814db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
8154301304bSMichael Chan 
8164301304bSMichael Chan 	tcs = netdev_get_num_tc(dev);
8174301304bSMichael Chan 	tx_grps = max(tcs, 1);
8184301304bSMichael Chan 	if (bp->tx_nr_rings_xdp)
8194301304bSMichael Chan 		tx_grps++;
8204301304bSMichael Chan 	max_tx_rings /= tx_grps;
821a79a5276SMichael Chan 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
822068c9ec6SMichael Chan 
82318d6e4e2SSatish Baddipadige 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
82418d6e4e2SSatish Baddipadige 		max_rx_rings = 0;
82518d6e4e2SSatish Baddipadige 		max_tx_rings = 0;
82618d6e4e2SSatish Baddipadige 	}
827db4723b3SMichael Chan 	if (max_tx_sch_inputs)
828db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
82918d6e4e2SSatish Baddipadige 
830c0c050c5SMichael Chan 	if (tcs > 1)
831c0c050c5SMichael Chan 		max_tx_rings /= tcs;
832c0c050c5SMichael Chan 
833c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
834c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
835c0c050c5SMichael Chan 	channel->max_other = 0;
836068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
837068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
83876595193SPrashant Sreedharan 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
83976595193SPrashant Sreedharan 			channel->combined_count--;
840068c9ec6SMichael Chan 	} else {
84176595193SPrashant Sreedharan 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
842c0c050c5SMichael Chan 			channel->rx_count = bp->rx_nr_rings;
843c0c050c5SMichael Chan 			channel->tx_count = bp->tx_nr_rings_per_tc;
844c0c050c5SMichael Chan 		}
845068c9ec6SMichael Chan 	}
84676595193SPrashant Sreedharan }
847c0c050c5SMichael Chan 
848c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
849c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
850c0c050c5SMichael Chan {
851c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
852d1e7925eSMichael Chan 	int req_tx_rings, req_rx_rings, tcs;
853068c9ec6SMichael Chan 	bool sh = false;
8545f449249SMichael Chan 	int tx_xdp = 0;
855d1e7925eSMichael Chan 	int rc = 0;
856c0c050c5SMichael Chan 
857068c9ec6SMichael Chan 	if (channel->other_count)
858c0c050c5SMichael Chan 		return -EINVAL;
859c0c050c5SMichael Chan 
860068c9ec6SMichael Chan 	if (!channel->combined_count &&
861068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
862068c9ec6SMichael Chan 		return -EINVAL;
863068c9ec6SMichael Chan 
864068c9ec6SMichael Chan 	if (channel->combined_count &&
865068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
866068c9ec6SMichael Chan 		return -EINVAL;
867068c9ec6SMichael Chan 
86876595193SPrashant Sreedharan 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
86976595193SPrashant Sreedharan 					    channel->tx_count))
87076595193SPrashant Sreedharan 		return -EINVAL;
87176595193SPrashant Sreedharan 
872068c9ec6SMichael Chan 	if (channel->combined_count)
873068c9ec6SMichael Chan 		sh = true;
874068c9ec6SMichael Chan 
875c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
876c0c050c5SMichael Chan 
877391be5c2SMichael Chan 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
878d1e7925eSMichael Chan 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
8795f449249SMichael Chan 	if (bp->tx_nr_rings_xdp) {
8805f449249SMichael Chan 		if (!sh) {
8815f449249SMichael Chan 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
8825f449249SMichael Chan 			return -EINVAL;
8835f449249SMichael Chan 		}
8845f449249SMichael Chan 		tx_xdp = req_rx_rings;
8855f449249SMichael Chan 	}
88698fdbe73SMichael Chan 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
887d1e7925eSMichael Chan 	if (rc) {
888d1e7925eSMichael Chan 		netdev_warn(dev, "Unable to allocate the requested rings\n");
889d1e7925eSMichael Chan 		return rc;
890391be5c2SMichael Chan 	}
891391be5c2SMichael Chan 
892bd3191b5SMichael Chan 	if (bnxt_get_nr_rss_ctxs(bp, req_rx_rings) !=
893bd3191b5SMichael Chan 	    bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) &&
894bd3191b5SMichael Chan 	    (dev->priv_flags & IFF_RXFH_CONFIGURED)) {
895bd3191b5SMichael Chan 		netdev_warn(dev, "RSS table size change required, RSS table entries must be default to proceed\n");
896bd3191b5SMichael Chan 		return -EINVAL;
897bd3191b5SMichael Chan 	}
898bd3191b5SMichael Chan 
899c0c050c5SMichael Chan 	if (netif_running(dev)) {
900c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
901c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
902c0c050c5SMichael Chan 			 * before PF unload
903c0c050c5SMichael Chan 			 */
904c0c050c5SMichael Chan 		}
905c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
906c0c050c5SMichael Chan 		if (rc) {
907c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
908c0c050c5SMichael Chan 				   rc);
909c0c050c5SMichael Chan 			return rc;
910c0c050c5SMichael Chan 		}
911c0c050c5SMichael Chan 	}
912c0c050c5SMichael Chan 
913068c9ec6SMichael Chan 	if (sh) {
914068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
915d1e7925eSMichael Chan 		bp->rx_nr_rings = channel->combined_count;
916d1e7925eSMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
917068c9ec6SMichael Chan 	} else {
918068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
919c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
920c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
921068c9ec6SMichael Chan 	}
9225f449249SMichael Chan 	bp->tx_nr_rings_xdp = tx_xdp;
9235f449249SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
924c0c050c5SMichael Chan 	if (tcs > 1)
9255f449249SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
926068c9ec6SMichael Chan 
927068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
928068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
929068c9ec6SMichael Chan 
9302bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
9312bcfa6f6SMichael Chan 	netdev_update_features(dev);
932c0c050c5SMichael Chan 	if (netif_running(dev)) {
933c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
934c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
935c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
936c0c050c5SMichael Chan 			 * to renable
937c0c050c5SMichael Chan 			 */
938c0c050c5SMichael Chan 		}
939d8c09f19SMichael Chan 	} else {
9401b3f0b75SMichael Chan 		rc = bnxt_reserve_rings(bp, true);
941c0c050c5SMichael Chan 	}
942c0c050c5SMichael Chan 
943c0c050c5SMichael Chan 	return rc;
944c0c050c5SMichael Chan }
945c0c050c5SMichael Chan 
946c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
947c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
948c0c050c5SMichael Chan 			    u32 *rule_locs)
949c0c050c5SMichael Chan {
950c0c050c5SMichael Chan 	int i, j = 0;
951c0c050c5SMichael Chan 
952c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
953c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
954c0c050c5SMichael Chan 		struct hlist_head *head;
955c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
956c0c050c5SMichael Chan 
957c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
958c0c050c5SMichael Chan 		rcu_read_lock();
959c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
960c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
961c0c050c5SMichael Chan 				break;
962c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
963c0c050c5SMichael Chan 		}
964c0c050c5SMichael Chan 		rcu_read_unlock();
965c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
966c0c050c5SMichael Chan 			break;
967c0c050c5SMichael Chan 	}
968c0c050c5SMichael Chan 	cmd->rule_cnt = j;
969c0c050c5SMichael Chan 	return 0;
970c0c050c5SMichael Chan }
971c0c050c5SMichael Chan 
972c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
973c0c050c5SMichael Chan {
974c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
975c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
976c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
977c0c050c5SMichael Chan 	struct flow_keys *fkeys;
978c0c050c5SMichael Chan 	int i, rc = -EINVAL;
979c0c050c5SMichael Chan 
980b721cfafSstephen hemminger 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
981c0c050c5SMichael Chan 		return rc;
982c0c050c5SMichael Chan 
983c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
984c0c050c5SMichael Chan 		struct hlist_head *head;
985c0c050c5SMichael Chan 
986c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
987c0c050c5SMichael Chan 		rcu_read_lock();
988c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
989c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
990c0c050c5SMichael Chan 				goto fltr_found;
991c0c050c5SMichael Chan 		}
992c0c050c5SMichael Chan 		rcu_read_unlock();
993c0c050c5SMichael Chan 	}
994c0c050c5SMichael Chan 	return rc;
995c0c050c5SMichael Chan 
996c0c050c5SMichael Chan fltr_found:
997c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
998dda0e746SMichael Chan 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
999c0c050c5SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1000c0c050c5SMichael Chan 			fs->flow_type = TCP_V4_FLOW;
1001c0c050c5SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1002c0c050c5SMichael Chan 			fs->flow_type = UDP_V4_FLOW;
1003c0c050c5SMichael Chan 		else
1004c0c050c5SMichael Chan 			goto fltr_err;
1005c0c050c5SMichael Chan 
1006c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
1007c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
1008c0c050c5SMichael Chan 
1009c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
1010c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
1011c0c050c5SMichael Chan 
1012c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
1013c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
1014c0c050c5SMichael Chan 
1015c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
1016c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
1017dda0e746SMichael Chan 	} else {
1018dda0e746SMichael Chan 		int i;
1019dda0e746SMichael Chan 
1020dda0e746SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1021dda0e746SMichael Chan 			fs->flow_type = TCP_V6_FLOW;
1022dda0e746SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1023dda0e746SMichael Chan 			fs->flow_type = UDP_V6_FLOW;
1024dda0e746SMichael Chan 		else
1025dda0e746SMichael Chan 			goto fltr_err;
1026dda0e746SMichael Chan 
1027dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
1028dda0e746SMichael Chan 			fkeys->addrs.v6addrs.src;
1029dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
1030dda0e746SMichael Chan 			fkeys->addrs.v6addrs.dst;
1031dda0e746SMichael Chan 		for (i = 0; i < 4; i++) {
1032dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
1033dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
1034dda0e746SMichael Chan 		}
1035dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
1036dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
1037dda0e746SMichael Chan 
1038dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
1039dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
1040dda0e746SMichael Chan 	}
1041c0c050c5SMichael Chan 
1042c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
1043c0c050c5SMichael Chan 	rc = 0;
1044c0c050c5SMichael Chan 
1045c0c050c5SMichael Chan fltr_err:
1046c0c050c5SMichael Chan 	rcu_read_unlock();
1047c0c050c5SMichael Chan 
1048c0c050c5SMichael Chan 	return rc;
1049c0c050c5SMichael Chan }
1050a011952aSMichael Chan #endif
1051a011952aSMichael Chan 
1052a011952aSMichael Chan static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
1053a011952aSMichael Chan {
1054a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
1055a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1056a011952aSMichael Chan 	return 0;
1057a011952aSMichael Chan }
1058a011952aSMichael Chan 
1059a011952aSMichael Chan static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
1060a011952aSMichael Chan {
1061a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
1062a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1063a011952aSMichael Chan 	return 0;
1064a011952aSMichael Chan }
1065a011952aSMichael Chan 
1066a011952aSMichael Chan static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1067a011952aSMichael Chan {
1068a011952aSMichael Chan 	cmd->data = 0;
1069a011952aSMichael Chan 	switch (cmd->flow_type) {
1070a011952aSMichael Chan 	case TCP_V4_FLOW:
1071a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
1072a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1073a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1074a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1075a011952aSMichael Chan 		break;
1076a011952aSMichael Chan 	case UDP_V4_FLOW:
1077a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
1078a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1079a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1080df561f66SGustavo A. R. Silva 		fallthrough;
1081a011952aSMichael Chan 	case SCTP_V4_FLOW:
1082a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1083a011952aSMichael Chan 	case AH_V4_FLOW:
1084a011952aSMichael Chan 	case ESP_V4_FLOW:
1085a011952aSMichael Chan 	case IPV4_FLOW:
1086a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1087a011952aSMichael Chan 		break;
1088a011952aSMichael Chan 
1089a011952aSMichael Chan 	case TCP_V6_FLOW:
1090a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
1091a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1092a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1093a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1094a011952aSMichael Chan 		break;
1095a011952aSMichael Chan 	case UDP_V6_FLOW:
1096a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
1097a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1098a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1099df561f66SGustavo A. R. Silva 		fallthrough;
1100a011952aSMichael Chan 	case SCTP_V6_FLOW:
1101a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1102a011952aSMichael Chan 	case AH_V6_FLOW:
1103a011952aSMichael Chan 	case ESP_V6_FLOW:
1104a011952aSMichael Chan 	case IPV6_FLOW:
1105a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1106a011952aSMichael Chan 		break;
1107a011952aSMichael Chan 	}
1108a011952aSMichael Chan 	return 0;
1109a011952aSMichael Chan }
1110a011952aSMichael Chan 
1111a011952aSMichael Chan #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1112a011952aSMichael Chan #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
1113a011952aSMichael Chan 
1114a011952aSMichael Chan static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1115a011952aSMichael Chan {
1116a011952aSMichael Chan 	u32 rss_hash_cfg = bp->rss_hash_cfg;
1117a011952aSMichael Chan 	int tuple, rc = 0;
1118a011952aSMichael Chan 
1119a011952aSMichael Chan 	if (cmd->data == RXH_4TUPLE)
1120a011952aSMichael Chan 		tuple = 4;
1121a011952aSMichael Chan 	else if (cmd->data == RXH_2TUPLE)
1122a011952aSMichael Chan 		tuple = 2;
1123a011952aSMichael Chan 	else if (!cmd->data)
1124a011952aSMichael Chan 		tuple = 0;
1125a011952aSMichael Chan 	else
1126a011952aSMichael Chan 		return -EINVAL;
1127a011952aSMichael Chan 
1128a011952aSMichael Chan 	if (cmd->flow_type == TCP_V4_FLOW) {
1129a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1130a011952aSMichael Chan 		if (tuple == 4)
1131a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1132a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V4_FLOW) {
1133a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1134a011952aSMichael Chan 			return -EINVAL;
1135a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1136a011952aSMichael Chan 		if (tuple == 4)
1137a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1138a011952aSMichael Chan 	} else if (cmd->flow_type == TCP_V6_FLOW) {
1139a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1140a011952aSMichael Chan 		if (tuple == 4)
1141a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1142a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V6_FLOW) {
1143a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1144a011952aSMichael Chan 			return -EINVAL;
1145a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1146a011952aSMichael Chan 		if (tuple == 4)
1147a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1148a011952aSMichael Chan 	} else if (tuple == 4) {
1149a011952aSMichael Chan 		return -EINVAL;
1150a011952aSMichael Chan 	}
1151a011952aSMichael Chan 
1152a011952aSMichael Chan 	switch (cmd->flow_type) {
1153a011952aSMichael Chan 	case TCP_V4_FLOW:
1154a011952aSMichael Chan 	case UDP_V4_FLOW:
1155a011952aSMichael Chan 	case SCTP_V4_FLOW:
1156a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1157a011952aSMichael Chan 	case AH_V4_FLOW:
1158a011952aSMichael Chan 	case ESP_V4_FLOW:
1159a011952aSMichael Chan 	case IPV4_FLOW:
1160a011952aSMichael Chan 		if (tuple == 2)
1161a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1162a011952aSMichael Chan 		else if (!tuple)
1163a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1164a011952aSMichael Chan 		break;
1165a011952aSMichael Chan 
1166a011952aSMichael Chan 	case TCP_V6_FLOW:
1167a011952aSMichael Chan 	case UDP_V6_FLOW:
1168a011952aSMichael Chan 	case SCTP_V6_FLOW:
1169a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1170a011952aSMichael Chan 	case AH_V6_FLOW:
1171a011952aSMichael Chan 	case ESP_V6_FLOW:
1172a011952aSMichael Chan 	case IPV6_FLOW:
1173a011952aSMichael Chan 		if (tuple == 2)
1174a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1175a011952aSMichael Chan 		else if (!tuple)
1176a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1177a011952aSMichael Chan 		break;
1178a011952aSMichael Chan 	}
1179a011952aSMichael Chan 
1180a011952aSMichael Chan 	if (bp->rss_hash_cfg == rss_hash_cfg)
1181a011952aSMichael Chan 		return 0;
1182a011952aSMichael Chan 
1183a011952aSMichael Chan 	bp->rss_hash_cfg = rss_hash_cfg;
1184a011952aSMichael Chan 	if (netif_running(bp->dev)) {
1185a011952aSMichael Chan 		bnxt_close_nic(bp, false, false);
1186a011952aSMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1187a011952aSMichael Chan 	}
1188a011952aSMichael Chan 	return rc;
1189a011952aSMichael Chan }
1190c0c050c5SMichael Chan 
1191c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1192c0c050c5SMichael Chan 			  u32 *rule_locs)
1193c0c050c5SMichael Chan {
1194c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1195c0c050c5SMichael Chan 	int rc = 0;
1196c0c050c5SMichael Chan 
1197c0c050c5SMichael Chan 	switch (cmd->cmd) {
1198a011952aSMichael Chan #ifdef CONFIG_RFS_ACCEL
1199c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
1200c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
1201c0c050c5SMichael Chan 		break;
1202c0c050c5SMichael Chan 
1203c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
1204c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
1205c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
1206c0c050c5SMichael Chan 		break;
1207c0c050c5SMichael Chan 
1208c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
1209c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
1210c0c050c5SMichael Chan 		break;
1211c0c050c5SMichael Chan 
1212c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
1213c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
1214c0c050c5SMichael Chan 		break;
1215a011952aSMichael Chan #endif
1216a011952aSMichael Chan 
1217a011952aSMichael Chan 	case ETHTOOL_GRXFH:
1218a011952aSMichael Chan 		rc = bnxt_grxfh(bp, cmd);
1219a011952aSMichael Chan 		break;
1220c0c050c5SMichael Chan 
1221c0c050c5SMichael Chan 	default:
1222c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
1223c0c050c5SMichael Chan 		break;
1224c0c050c5SMichael Chan 	}
1225c0c050c5SMichael Chan 
1226c0c050c5SMichael Chan 	return rc;
1227c0c050c5SMichael Chan }
1228a011952aSMichael Chan 
1229a011952aSMichael Chan static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1230a011952aSMichael Chan {
1231a011952aSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1232a011952aSMichael Chan 	int rc;
1233a011952aSMichael Chan 
1234a011952aSMichael Chan 	switch (cmd->cmd) {
1235a011952aSMichael Chan 	case ETHTOOL_SRXFH:
1236a011952aSMichael Chan 		rc = bnxt_srxfh(bp, cmd);
1237a011952aSMichael Chan 		break;
1238a011952aSMichael Chan 
1239a011952aSMichael Chan 	default:
1240a011952aSMichael Chan 		rc = -EOPNOTSUPP;
1241a011952aSMichael Chan 		break;
1242a011952aSMichael Chan 	}
1243a011952aSMichael Chan 	return rc;
1244a011952aSMichael Chan }
1245c0c050c5SMichael Chan 
1246b73c1d08SMichael Chan u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
1247c0c050c5SMichael Chan {
1248b73c1d08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1249b73c1d08SMichael Chan 
1250b73c1d08SMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1251b73c1d08SMichael Chan 		return ALIGN(bp->rx_nr_rings, BNXT_RSS_TABLE_ENTRIES_P5);
1252c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
1253c0c050c5SMichael Chan }
1254c0c050c5SMichael Chan 
1255c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
1256c0c050c5SMichael Chan {
1257c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
1258c0c050c5SMichael Chan }
1259c0c050c5SMichael Chan 
1260c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1261c0c050c5SMichael Chan 			 u8 *hfunc)
1262c0c050c5SMichael Chan {
1263c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
12647991cb9cSMichael Chan 	struct bnxt_vnic_info *vnic;
1265adc38ac6SMichael Chan 	u32 i, tbl_size;
1266c0c050c5SMichael Chan 
1267c0c050c5SMichael Chan 	if (hfunc)
1268c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
1269c0c050c5SMichael Chan 
12707991cb9cSMichael Chan 	if (!bp->vnic_info)
12717991cb9cSMichael Chan 		return 0;
12727991cb9cSMichael Chan 
12737991cb9cSMichael Chan 	vnic = &bp->vnic_info[0];
1274adc38ac6SMichael Chan 	if (indir && bp->rss_indir_tbl) {
1275adc38ac6SMichael Chan 		tbl_size = bnxt_get_rxfh_indir_size(dev);
1276adc38ac6SMichael Chan 		for (i = 0; i < tbl_size; i++)
1277adc38ac6SMichael Chan 			indir[i] = bp->rss_indir_tbl[i];
12787991cb9cSMichael Chan 	}
1279c0c050c5SMichael Chan 
12807991cb9cSMichael Chan 	if (key && vnic->rss_hash_key)
1281c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
1282c0c050c5SMichael Chan 
1283c0c050c5SMichael Chan 	return 0;
1284c0c050c5SMichael Chan }
1285c0c050c5SMichael Chan 
1286bd3191b5SMichael Chan static int bnxt_set_rxfh(struct net_device *dev, const u32 *indir,
1287bd3191b5SMichael Chan 			 const u8 *key, const u8 hfunc)
1288bd3191b5SMichael Chan {
1289bd3191b5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1290bd3191b5SMichael Chan 	int rc = 0;
1291bd3191b5SMichael Chan 
1292bd3191b5SMichael Chan 	if (hfunc && hfunc != ETH_RSS_HASH_TOP)
1293bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1294bd3191b5SMichael Chan 
1295bd3191b5SMichael Chan 	if (key)
1296bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1297bd3191b5SMichael Chan 
1298bd3191b5SMichael Chan 	if (indir) {
1299bd3191b5SMichael Chan 		u32 i, pad, tbl_size = bnxt_get_rxfh_indir_size(dev);
1300bd3191b5SMichael Chan 
1301bd3191b5SMichael Chan 		for (i = 0; i < tbl_size; i++)
1302bd3191b5SMichael Chan 			bp->rss_indir_tbl[i] = indir[i];
1303bd3191b5SMichael Chan 		pad = bp->rss_indir_tbl_entries - tbl_size;
1304bd3191b5SMichael Chan 		if (pad)
1305bd3191b5SMichael Chan 			memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
1306bd3191b5SMichael Chan 	}
1307bd3191b5SMichael Chan 
1308bd3191b5SMichael Chan 	if (netif_running(bp->dev)) {
1309bd3191b5SMichael Chan 		bnxt_close_nic(bp, false, false);
1310bd3191b5SMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1311bd3191b5SMichael Chan 	}
1312bd3191b5SMichael Chan 	return rc;
1313bd3191b5SMichael Chan }
1314bd3191b5SMichael Chan 
1315c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
1316c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
1317c0c050c5SMichael Chan {
1318c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1319c0c050c5SMichael Chan 
1320c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1321431aa1ebSMichael Chan 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
1322c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
13235c8227d0SMichael Chan 	info->n_stats = bnxt_get_num_stats(bp);
1324eb513658SMichael Chan 	info->testinfo_len = bp->num_tests;
1325c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
1326c0c050c5SMichael Chan 	info->eedump_len = 0;
1327c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
1328c0c050c5SMichael Chan 	info->regdump_len = 0;
1329c0c050c5SMichael Chan }
1330c0c050c5SMichael Chan 
1331b5d600b0SVasundhara Volam static int bnxt_get_regs_len(struct net_device *dev)
1332b5d600b0SVasundhara Volam {
1333b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1334b5d600b0SVasundhara Volam 	int reg_len;
1335b5d600b0SVasundhara Volam 
1336f0f47b2fSVasundhara Volam 	if (!BNXT_PF(bp))
1337f0f47b2fSVasundhara Volam 		return -EOPNOTSUPP;
1338f0f47b2fSVasundhara Volam 
1339b5d600b0SVasundhara Volam 	reg_len = BNXT_PXP_REG_LEN;
1340b5d600b0SVasundhara Volam 
1341b5d600b0SVasundhara Volam 	if (bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED)
1342b5d600b0SVasundhara Volam 		reg_len += sizeof(struct pcie_ctx_hw_stats);
1343b5d600b0SVasundhara Volam 
1344b5d600b0SVasundhara Volam 	return reg_len;
1345b5d600b0SVasundhara Volam }
1346b5d600b0SVasundhara Volam 
1347b5d600b0SVasundhara Volam static void bnxt_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1348b5d600b0SVasundhara Volam 			  void *_p)
1349b5d600b0SVasundhara Volam {
1350b5d600b0SVasundhara Volam 	struct pcie_ctx_hw_stats *hw_pcie_stats;
1351b5d600b0SVasundhara Volam 	struct hwrm_pcie_qstats_input req = {0};
1352b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1353b5d600b0SVasundhara Volam 	dma_addr_t hw_pcie_stats_addr;
1354b5d600b0SVasundhara Volam 	int rc;
1355b5d600b0SVasundhara Volam 
1356b5d600b0SVasundhara Volam 	regs->version = 0;
1357b5d600b0SVasundhara Volam 	bnxt_dbg_hwrm_rd_reg(bp, 0, BNXT_PXP_REG_LEN / 4, _p);
1358b5d600b0SVasundhara Volam 
1359b5d600b0SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED))
1360b5d600b0SVasundhara Volam 		return;
1361b5d600b0SVasundhara Volam 
1362b5d600b0SVasundhara Volam 	hw_pcie_stats = dma_alloc_coherent(&bp->pdev->dev,
1363b5d600b0SVasundhara Volam 					   sizeof(*hw_pcie_stats),
1364b5d600b0SVasundhara Volam 					   &hw_pcie_stats_addr, GFP_KERNEL);
1365b5d600b0SVasundhara Volam 	if (!hw_pcie_stats)
1366b5d600b0SVasundhara Volam 		return;
1367b5d600b0SVasundhara Volam 
1368b5d600b0SVasundhara Volam 	regs->version = 1;
1369b5d600b0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PCIE_QSTATS, -1, -1);
1370b5d600b0SVasundhara Volam 	req.pcie_stat_size = cpu_to_le16(sizeof(*hw_pcie_stats));
1371b5d600b0SVasundhara Volam 	req.pcie_stat_host_addr = cpu_to_le64(hw_pcie_stats_addr);
1372b5d600b0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
1373b5d600b0SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1374b5d600b0SVasundhara Volam 	if (!rc) {
1375b5d600b0SVasundhara Volam 		__le64 *src = (__le64 *)hw_pcie_stats;
1376b5d600b0SVasundhara Volam 		u64 *dst = (u64 *)(_p + BNXT_PXP_REG_LEN);
1377b5d600b0SVasundhara Volam 		int i;
1378b5d600b0SVasundhara Volam 
1379b5d600b0SVasundhara Volam 		for (i = 0; i < sizeof(*hw_pcie_stats) / sizeof(__le64); i++)
1380b5d600b0SVasundhara Volam 			dst[i] = le64_to_cpu(src[i]);
1381b5d600b0SVasundhara Volam 	}
1382b5d600b0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
1383b5d600b0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, sizeof(*hw_pcie_stats), hw_pcie_stats,
1384b5d600b0SVasundhara Volam 			  hw_pcie_stats_addr);
1385b5d600b0SVasundhara Volam }
1386b5d600b0SVasundhara Volam 
13878e202366SMichael Chan static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13888e202366SMichael Chan {
13898e202366SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
13908e202366SMichael Chan 
13918e202366SMichael Chan 	wol->supported = 0;
13928e202366SMichael Chan 	wol->wolopts = 0;
13938e202366SMichael Chan 	memset(&wol->sopass, 0, sizeof(wol->sopass));
13948e202366SMichael Chan 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
13958e202366SMichael Chan 		wol->supported = WAKE_MAGIC;
13968e202366SMichael Chan 		if (bp->wol)
13978e202366SMichael Chan 			wol->wolopts = WAKE_MAGIC;
13988e202366SMichael Chan 	}
13998e202366SMichael Chan }
14008e202366SMichael Chan 
14015282db6cSMichael Chan static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
14025282db6cSMichael Chan {
14035282db6cSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
14045282db6cSMichael Chan 
14055282db6cSMichael Chan 	if (wol->wolopts & ~WAKE_MAGIC)
14065282db6cSMichael Chan 		return -EINVAL;
14075282db6cSMichael Chan 
14085282db6cSMichael Chan 	if (wol->wolopts & WAKE_MAGIC) {
14095282db6cSMichael Chan 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
14105282db6cSMichael Chan 			return -EINVAL;
14115282db6cSMichael Chan 		if (!bp->wol) {
14125282db6cSMichael Chan 			if (bnxt_hwrm_alloc_wol_fltr(bp))
14135282db6cSMichael Chan 				return -EBUSY;
14145282db6cSMichael Chan 			bp->wol = 1;
14155282db6cSMichael Chan 		}
14165282db6cSMichael Chan 	} else {
14175282db6cSMichael Chan 		if (bp->wol) {
14185282db6cSMichael Chan 			if (bnxt_hwrm_free_wol_fltr(bp))
14195282db6cSMichael Chan 				return -EBUSY;
14205282db6cSMichael Chan 			bp->wol = 0;
14215282db6cSMichael Chan 		}
14225282db6cSMichael Chan 	}
14235282db6cSMichael Chan 	return 0;
14245282db6cSMichael Chan }
14255282db6cSMichael Chan 
1426170ce013SMichael Chan u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
1427c0c050c5SMichael Chan {
1428c0c050c5SMichael Chan 	u32 speed_mask = 0;
1429c0c050c5SMichael Chan 
1430c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
1431c0c050c5SMichael Chan 	/* set the advertised speeds */
1432c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1433c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
1434c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1435c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
1436c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1437c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
1438c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1439c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
1440c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
14411c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
144227c4d578SMichael Chan 
144327c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
144427c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
144527c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
144627c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
144727c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
144827c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
144927c4d578SMichael Chan 
1450c0c050c5SMichael Chan 	return speed_mask;
1451c0c050c5SMichael Chan }
1452c0c050c5SMichael Chan 
145300c04a92SMichael Chan #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
145400c04a92SMichael Chan {									\
145500c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
145600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145700c04a92SMichael Chan 						     100baseT_Full);	\
145800c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
145900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146000c04a92SMichael Chan 						     1000baseT_Full);	\
146100c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
146200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146300c04a92SMichael Chan 						     10000baseT_Full);	\
146400c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
146500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146600c04a92SMichael Chan 						     25000baseCR_Full);	\
146700c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
146800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146900c04a92SMichael Chan 						     40000baseCR4_Full);\
147000c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
147100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147200c04a92SMichael Chan 						     50000baseCR2_Full);\
147338a21b34SDeepak Khungar 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
147438a21b34SDeepak Khungar 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147538a21b34SDeepak Khungar 						     100000baseCR4_Full);\
147600c04a92SMichael Chan 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
147700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147800c04a92SMichael Chan 						     Pause);		\
147900c04a92SMichael Chan 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
148000c04a92SMichael Chan 			ethtool_link_ksettings_add_link_mode(		\
148100c04a92SMichael Chan 					lk_ksettings, name, Asym_Pause);\
148200c04a92SMichael Chan 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
148300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
148400c04a92SMichael Chan 						     Asym_Pause);	\
148500c04a92SMichael Chan 	}								\
148600c04a92SMichael Chan }
148700c04a92SMichael Chan 
148800c04a92SMichael Chan #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
148900c04a92SMichael Chan {									\
149000c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149100c04a92SMichael Chan 						  100baseT_Full) ||	\
149200c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149300c04a92SMichael Chan 						  100baseT_Half))	\
149400c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
149500c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149600c04a92SMichael Chan 						  1000baseT_Full) ||	\
149700c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149800c04a92SMichael Chan 						  1000baseT_Half))	\
149900c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
150000c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
150100c04a92SMichael Chan 						  10000baseT_Full))	\
150200c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
150300c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
150400c04a92SMichael Chan 						  25000baseCR_Full))	\
150500c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
150600c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
150700c04a92SMichael Chan 						  40000baseCR4_Full))	\
150800c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
150900c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
151000c04a92SMichael Chan 						  50000baseCR2_Full))	\
151100c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
151238a21b34SDeepak Khungar 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
151338a21b34SDeepak Khungar 						  100000baseCR4_Full))	\
151438a21b34SDeepak Khungar 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
151500c04a92SMichael Chan }
151600c04a92SMichael Chan 
1517532262baSEdwin Peer #define BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, name)	\
1518532262baSEdwin Peer {									\
1519532262baSEdwin Peer 	if ((fw_speeds) & BNXT_LINK_PAM4_SPEED_MSK_50GB)		\
1520532262baSEdwin Peer 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1521532262baSEdwin Peer 						     50000baseCR_Full);	\
1522532262baSEdwin Peer 	if ((fw_speeds) & BNXT_LINK_PAM4_SPEED_MSK_100GB)		\
1523532262baSEdwin Peer 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1524532262baSEdwin Peer 						     100000baseCR2_Full);\
1525532262baSEdwin Peer 	if ((fw_speeds) & BNXT_LINK_PAM4_SPEED_MSK_200GB)		\
1526532262baSEdwin Peer 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1527532262baSEdwin Peer 						     200000baseCR4_Full);\
1528532262baSEdwin Peer }
1529532262baSEdwin Peer 
1530532262baSEdwin Peer #define BNXT_ETHTOOL_TO_FW_PAM4_SPDS(fw_speeds, lk_ksettings, name)	\
1531532262baSEdwin Peer {									\
1532532262baSEdwin Peer 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1533532262baSEdwin Peer 						  50000baseCR_Full))	\
1534532262baSEdwin Peer 		(fw_speeds) |= BNXT_LINK_PAM4_SPEED_MSK_50GB;		\
1535532262baSEdwin Peer 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1536532262baSEdwin Peer 						  100000baseCR2_Full))	\
1537532262baSEdwin Peer 		(fw_speeds) |= BNXT_LINK_PAM4_SPEED_MSK_100GB;		\
1538532262baSEdwin Peer 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1539532262baSEdwin Peer 						  200000baseCR4_Full))	\
1540532262baSEdwin Peer 		(fw_speeds) |= BNXT_LINK_PAM4_SPEED_MSK_200GB;		\
1541532262baSEdwin Peer }
1542532262baSEdwin Peer 
15438b277589SMichael Chan static void bnxt_fw_to_ethtool_advertised_fec(struct bnxt_link_info *link_info,
15448b277589SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15458b277589SMichael Chan {
15468b277589SMichael Chan 	u16 fec_cfg = link_info->fec_cfg;
15478b277589SMichael Chan 
15488b277589SMichael Chan 	if ((fec_cfg & BNXT_FEC_NONE) || !(fec_cfg & BNXT_FEC_AUTONEG)) {
15498b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
15508b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15518b277589SMichael Chan 		return;
15528b277589SMichael Chan 	}
15538b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_BASE_R)
15548b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
15558b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15568b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_RS)
15578b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
15588b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15598b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_LLRS)
15608b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
15618b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15628b277589SMichael Chan }
15638b277589SMichael Chan 
156400c04a92SMichael Chan static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
156500c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
156627c4d578SMichael Chan {
156768515a18SMichael Chan 	u16 fw_speeds = link_info->advertising;
156827c4d578SMichael Chan 	u8 fw_pause = 0;
156927c4d578SMichael Chan 
157027c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
157127c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
157227c4d578SMichael Chan 
157300c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
1574532262baSEdwin Peer 	fw_speeds = link_info->advertising_pam4;
1575532262baSEdwin Peer 	BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, advertising);
15768b277589SMichael Chan 	bnxt_fw_to_ethtool_advertised_fec(link_info, lk_ksettings);
157727c4d578SMichael Chan }
157827c4d578SMichael Chan 
157900c04a92SMichael Chan static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
158000c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15813277360eSMichael Chan {
15823277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
15833277360eSMichael Chan 	u8 fw_pause = 0;
15843277360eSMichael Chan 
15853277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
15863277360eSMichael Chan 		fw_pause = link_info->lp_pause;
15873277360eSMichael Chan 
158800c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
158900c04a92SMichael Chan 				lp_advertising);
1590532262baSEdwin Peer 	fw_speeds = link_info->lp_auto_pam4_link_speeds;
1591532262baSEdwin Peer 	BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, lp_advertising);
15923277360eSMichael Chan }
15933277360eSMichael Chan 
15948b277589SMichael Chan static void bnxt_fw_to_ethtool_support_fec(struct bnxt_link_info *link_info,
15958b277589SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15968b277589SMichael Chan {
15978b277589SMichael Chan 	u16 fec_cfg = link_info->fec_cfg;
15988b277589SMichael Chan 
15998b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_NONE) {
16008b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
16018b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16028b277589SMichael Chan 		return;
16038b277589SMichael Chan 	}
16048b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_BASE_R_CAP)
16058b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
16068b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16078b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_RS_CAP)
16088b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
16098b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16108b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_LLRS_CAP)
16118b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
16128b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16138b277589SMichael Chan }
16148b277589SMichael Chan 
161500c04a92SMichael Chan static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
161600c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
16174b32caccSMichael Chan {
16184b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
16194b32caccSMichael Chan 
162000c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
1621532262baSEdwin Peer 	fw_speeds = link_info->support_pam4_speeds;
1622532262baSEdwin Peer 	BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, supported);
16234b32caccSMichael Chan 
162400c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
162500c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
162600c04a92SMichael Chan 					     Asym_Pause);
162793ed8117SMichael Chan 
1628532262baSEdwin Peer 	if (link_info->support_auto_speeds ||
1629532262baSEdwin Peer 	    link_info->support_pam4_auto_speeds)
163000c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
163100c04a92SMichael Chan 						     Autoneg);
16328b277589SMichael Chan 	bnxt_fw_to_ethtool_support_fec(link_info, lk_ksettings);
163393ed8117SMichael Chan }
163493ed8117SMichael Chan 
1635c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1636c0c050c5SMichael Chan {
1637c0c050c5SMichael Chan 	switch (fw_link_speed) {
1638c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
1639c0c050c5SMichael Chan 		return SPEED_100;
1640c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
1641c0c050c5SMichael Chan 		return SPEED_1000;
1642c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
1643c0c050c5SMichael Chan 		return SPEED_2500;
1644c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
1645c0c050c5SMichael Chan 		return SPEED_10000;
1646c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
1647c0c050c5SMichael Chan 		return SPEED_20000;
1648c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
1649c0c050c5SMichael Chan 		return SPEED_25000;
1650c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
1651c0c050c5SMichael Chan 		return SPEED_40000;
1652c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
1653c0c050c5SMichael Chan 		return SPEED_50000;
165438a21b34SDeepak Khungar 	case BNXT_LINK_SPEED_100GB:
165538a21b34SDeepak Khungar 		return SPEED_100000;
1656c0c050c5SMichael Chan 	default:
1657c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
1658c0c050c5SMichael Chan 	}
1659c0c050c5SMichael Chan }
1660c0c050c5SMichael Chan 
166100c04a92SMichael Chan static int bnxt_get_link_ksettings(struct net_device *dev,
166200c04a92SMichael Chan 				   struct ethtool_link_ksettings *lk_ksettings)
1663c0c050c5SMichael Chan {
1664c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1665c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
166600c04a92SMichael Chan 	struct ethtool_link_settings *base = &lk_ksettings->base;
166700c04a92SMichael Chan 	u32 ethtool_speed;
1668c0c050c5SMichael Chan 
166900c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1670e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
167100c04a92SMichael Chan 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1672c0c050c5SMichael Chan 
167300c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1674b763499eSMichael Chan 	if (link_info->autoneg) {
167500c04a92SMichael Chan 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
167600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
167700c04a92SMichael Chan 						     advertising, Autoneg);
167800c04a92SMichael Chan 		base->autoneg = AUTONEG_ENABLE;
167900c04a92SMichael Chan 		base->duplex = DUPLEX_UNKNOWN;
168083d8f5e9SMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK) {
168183d8f5e9SMichael Chan 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
168283d8f5e9SMichael Chan 			if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
168300c04a92SMichael Chan 				base->duplex = DUPLEX_FULL;
168429c262feSMichael Chan 			else
168500c04a92SMichael Chan 				base->duplex = DUPLEX_HALF;
168683d8f5e9SMichael Chan 		}
168783d8f5e9SMichael Chan 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1688c0c050c5SMichael Chan 	} else {
168900c04a92SMichael Chan 		base->autoneg = AUTONEG_DISABLE;
169029c262feSMichael Chan 		ethtool_speed =
169129c262feSMichael Chan 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
169200c04a92SMichael Chan 		base->duplex = DUPLEX_HALF;
169329c262feSMichael Chan 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
169400c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
1695c0c050c5SMichael Chan 	}
169600c04a92SMichael Chan 	base->speed = ethtool_speed;
1697c0c050c5SMichael Chan 
169800c04a92SMichael Chan 	base->port = PORT_NONE;
1699c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
170000c04a92SMichael Chan 		base->port = PORT_TP;
170100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
170200c04a92SMichael Chan 						     TP);
170300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
170400c04a92SMichael Chan 						     TP);
1705c0c050c5SMichael Chan 	} else {
170600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
170700c04a92SMichael Chan 						     FIBRE);
170800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
170900c04a92SMichael Chan 						     FIBRE);
1710c0c050c5SMichael Chan 
1711c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
171200c04a92SMichael Chan 			base->port = PORT_DA;
1713c0c050c5SMichael Chan 		else if (link_info->media_type ==
1714c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
171500c04a92SMichael Chan 			base->port = PORT_FIBRE;
1716c0c050c5SMichael Chan 	}
171700c04a92SMichael Chan 	base->phy_address = link_info->phy_addr;
1718e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1719c0c050c5SMichael Chan 
1720c0c050c5SMichael Chan 	return 0;
1721c0c050c5SMichael Chan }
1722c0c050c5SMichael Chan 
1723f00530bfSEdwin Peer static int bnxt_force_link_speed(struct net_device *dev, u32 ethtool_speed)
1724c0c050c5SMichael Chan {
17259d9cee08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
17269d9cee08SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1727532262baSEdwin Peer 	u16 support_pam4_spds = link_info->support_pam4_speeds;
17289d9cee08SMichael Chan 	u16 support_spds = link_info->support_speeds;
1729532262baSEdwin Peer 	u8 sig_mode = BNXT_SIG_MODE_NRZ;
1730f00530bfSEdwin Peer 	u16 fw_speed = 0;
17319d9cee08SMichael Chan 
1732c0c050c5SMichael Chan 	switch (ethtool_speed) {
1733c0c050c5SMichael Chan 	case SPEED_100:
17349d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
1735f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_100MB;
17369d9cee08SMichael Chan 		break;
1737c0c050c5SMichael Chan 	case SPEED_1000:
17389d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
1739f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
17409d9cee08SMichael Chan 		break;
1741c0c050c5SMichael Chan 	case SPEED_2500:
17429d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
1743f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_2_5GB;
17449d9cee08SMichael Chan 		break;
1745c0c050c5SMichael Chan 	case SPEED_10000:
17469d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
1747f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
17489d9cee08SMichael Chan 		break;
1749c0c050c5SMichael Chan 	case SPEED_20000:
17509d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
1751f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_20GB;
17529d9cee08SMichael Chan 		break;
1753c0c050c5SMichael Chan 	case SPEED_25000:
17549d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
1755f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
17569d9cee08SMichael Chan 		break;
1757c0c050c5SMichael Chan 	case SPEED_40000:
17589d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
1759f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
17609d9cee08SMichael Chan 		break;
1761c0c050c5SMichael Chan 	case SPEED_50000:
1762532262baSEdwin Peer 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB) {
1763f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
1764532262baSEdwin Peer 		} else if (support_pam4_spds & BNXT_LINK_PAM4_SPEED_MSK_50GB) {
1765532262baSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_PAM4_LINK_SPEED_50GB;
1766532262baSEdwin Peer 			sig_mode = BNXT_SIG_MODE_PAM4;
1767532262baSEdwin Peer 		}
17689d9cee08SMichael Chan 		break;
176938a21b34SDeepak Khungar 	case SPEED_100000:
1770532262baSEdwin Peer 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB) {
1771f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_100GB;
1772532262baSEdwin Peer 		} else if (support_pam4_spds & BNXT_LINK_PAM4_SPEED_MSK_100GB) {
1773532262baSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_PAM4_LINK_SPEED_100GB;
1774532262baSEdwin Peer 			sig_mode = BNXT_SIG_MODE_PAM4;
1775532262baSEdwin Peer 		}
1776532262baSEdwin Peer 		break;
1777532262baSEdwin Peer 	case SPEED_200000:
1778532262baSEdwin Peer 		if (support_pam4_spds & BNXT_LINK_PAM4_SPEED_MSK_200GB) {
1779532262baSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_PAM4_LINK_SPEED_200GB;
1780532262baSEdwin Peer 			sig_mode = BNXT_SIG_MODE_PAM4;
1781532262baSEdwin Peer 		}
1782c0c050c5SMichael Chan 		break;
1783c0c050c5SMichael Chan 	}
1784f00530bfSEdwin Peer 
1785f00530bfSEdwin Peer 	if (!fw_speed) {
1786f00530bfSEdwin Peer 		netdev_err(dev, "unsupported speed!\n");
1787f00530bfSEdwin Peer 		return -EINVAL;
1788f00530bfSEdwin Peer 	}
1789f00530bfSEdwin Peer 
1790745b5c65SEdwin Peer 	if (link_info->req_link_speed == fw_speed &&
1791745b5c65SEdwin Peer 	    link_info->req_signal_mode == sig_mode &&
1792745b5c65SEdwin Peer 	    link_info->autoneg == 0)
1793745b5c65SEdwin Peer 		return -EALREADY;
1794745b5c65SEdwin Peer 
1795f00530bfSEdwin Peer 	link_info->req_link_speed = fw_speed;
1796532262baSEdwin Peer 	link_info->req_signal_mode = sig_mode;
1797f00530bfSEdwin Peer 	link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1798f00530bfSEdwin Peer 	link_info->autoneg = 0;
1799f00530bfSEdwin Peer 	link_info->advertising = 0;
1800532262baSEdwin Peer 	link_info->advertising_pam4 = 0;
1801f00530bfSEdwin Peer 
1802f00530bfSEdwin Peer 	return 0;
1803c0c050c5SMichael Chan }
1804c0c050c5SMichael Chan 
1805939f7f0cSMichael Chan u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1806c0c050c5SMichael Chan {
1807c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
1808c0c050c5SMichael Chan 
1809c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
1810c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
1811c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
1812c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1813c0c050c5SMichael Chan 	}
1814c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
1815c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
1816c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1817c0c050c5SMichael Chan 	}
1818c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
1819c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1820c0c050c5SMichael Chan 
18211c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
18221c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
18231c49c421SMichael Chan 
1824c0c050c5SMichael Chan 	return fw_speed_mask;
1825c0c050c5SMichael Chan }
1826c0c050c5SMichael Chan 
182700c04a92SMichael Chan static int bnxt_set_link_ksettings(struct net_device *dev,
182800c04a92SMichael Chan 			   const struct ethtool_link_ksettings *lk_ksettings)
1829c0c050c5SMichael Chan {
1830c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1831c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
183200c04a92SMichael Chan 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1833c0c050c5SMichael Chan 	bool set_pause = false;
183468515a18SMichael Chan 	u32 speed;
183500c04a92SMichael Chan 	int rc = 0;
1836c0c050c5SMichael Chan 
1837c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
183800c04a92SMichael Chan 		return -EOPNOTSUPP;
1839c0c050c5SMichael Chan 
1840e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
184100c04a92SMichael Chan 	if (base->autoneg == AUTONEG_ENABLE) {
1842532262baSEdwin Peer 		link_info->advertising = 0;
1843532262baSEdwin Peer 		link_info->advertising_pam4 = 0;
1844532262baSEdwin Peer 		BNXT_ETHTOOL_TO_FW_SPDS(link_info->advertising, lk_ksettings,
184500c04a92SMichael Chan 					advertising);
1846532262baSEdwin Peer 		BNXT_ETHTOOL_TO_FW_PAM4_SPDS(link_info->advertising_pam4,
1847532262baSEdwin Peer 					     lk_ksettings, advertising);
1848c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1849532262baSEdwin Peer 		if (!link_info->advertising && !link_info->advertising_pam4) {
185093ed8117SMichael Chan 			link_info->advertising = link_info->support_auto_speeds;
1851532262baSEdwin Peer 			link_info->advertising_pam4 =
1852532262baSEdwin Peer 				link_info->support_pam4_auto_speeds;
1853532262baSEdwin Peer 		}
1854c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
1855c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
1856c0c050c5SMichael Chan 		 */
1857c0c050c5SMichael Chan 		set_pause = true;
1858c0c050c5SMichael Chan 	} else {
185903efbec0SMichael Chan 		u8 phy_type = link_info->phy_type;
18609d9cee08SMichael Chan 
186103efbec0SMichael Chan 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
186203efbec0SMichael Chan 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
186303efbec0SMichael Chan 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
186403efbec0SMichael Chan 			netdev_err(dev, "10GBase-T devices must autoneg\n");
186503efbec0SMichael Chan 			rc = -EINVAL;
186603efbec0SMichael Chan 			goto set_setting_exit;
186703efbec0SMichael Chan 		}
186800c04a92SMichael Chan 		if (base->duplex == DUPLEX_HALF) {
1869c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1870c0c050c5SMichael Chan 			rc = -EINVAL;
1871c0c050c5SMichael Chan 			goto set_setting_exit;
1872c0c050c5SMichael Chan 		}
187300c04a92SMichael Chan 		speed = base->speed;
1874f00530bfSEdwin Peer 		rc = bnxt_force_link_speed(dev, speed);
1875745b5c65SEdwin Peer 		if (rc) {
1876745b5c65SEdwin Peer 			if (rc == -EALREADY)
1877745b5c65SEdwin Peer 				rc = 0;
18789d9cee08SMichael Chan 			goto set_setting_exit;
18799d9cee08SMichael Chan 		}
1880745b5c65SEdwin Peer 	}
1881c0c050c5SMichael Chan 
1882c0c050c5SMichael Chan 	if (netif_running(dev))
1883939f7f0cSMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1884c0c050c5SMichael Chan 
1885c0c050c5SMichael Chan set_setting_exit:
1886e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1887c0c050c5SMichael Chan 	return rc;
1888c0c050c5SMichael Chan }
1889c0c050c5SMichael Chan 
18908b277589SMichael Chan static int bnxt_get_fecparam(struct net_device *dev,
18918b277589SMichael Chan 			     struct ethtool_fecparam *fec)
18928b277589SMichael Chan {
18938b277589SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
18948b277589SMichael Chan 	struct bnxt_link_info *link_info;
18958b277589SMichael Chan 	u8 active_fec;
18968b277589SMichael Chan 	u16 fec_cfg;
18978b277589SMichael Chan 
18988b277589SMichael Chan 	link_info = &bp->link_info;
18998b277589SMichael Chan 	fec_cfg = link_info->fec_cfg;
19008b277589SMichael Chan 	active_fec = link_info->active_fec_sig_mode &
19018b277589SMichael Chan 		     PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
19028b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_NONE) {
19038b277589SMichael Chan 		fec->fec = ETHTOOL_FEC_NONE;
19048b277589SMichael Chan 		fec->active_fec = ETHTOOL_FEC_NONE;
19058b277589SMichael Chan 		return 0;
19068b277589SMichael Chan 	}
19078b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_AUTONEG)
19088b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_AUTO;
19098b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_BASE_R)
19108b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_BASER;
19118b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_RS)
19128b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_RS;
19138b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_LLRS)
19148b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_LLRS;
19158b277589SMichael Chan 
19168b277589SMichael Chan 	switch (active_fec) {
19178b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
19188b277589SMichael Chan 		fec->active_fec |= ETHTOOL_FEC_BASER;
19198b277589SMichael Chan 		break;
19208b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
19218b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
19228b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
19238b277589SMichael Chan 		fec->active_fec |= ETHTOOL_FEC_RS;
19248b277589SMichael Chan 		break;
19258b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
19268b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
19278b277589SMichael Chan 		fec->active_fec |= ETHTOOL_FEC_LLRS;
19288b277589SMichael Chan 		break;
19298b277589SMichael Chan 	}
19308b277589SMichael Chan 	return 0;
19318b277589SMichael Chan }
19328b277589SMichael Chan 
1933ccd6a9dcSMichael Chan static u32 bnxt_ethtool_forced_fec_to_fw(struct bnxt_link_info *link_info,
1934ccd6a9dcSMichael Chan 					 u32 fec)
1935ccd6a9dcSMichael Chan {
1936ccd6a9dcSMichael Chan 	u32 fw_fec = PORT_PHY_CFG_REQ_FLAGS_FEC_AUTONEG_DISABLE;
1937ccd6a9dcSMichael Chan 
1938ccd6a9dcSMichael Chan 	if (fec & ETHTOOL_FEC_BASER)
1939ccd6a9dcSMichael Chan 		fw_fec |= BNXT_FEC_BASE_R_ON(link_info);
1940ccd6a9dcSMichael Chan 	else if (fec & ETHTOOL_FEC_RS)
1941ccd6a9dcSMichael Chan 		fw_fec |= BNXT_FEC_RS_ON(link_info);
1942ccd6a9dcSMichael Chan 	else if (fec & ETHTOOL_FEC_LLRS)
1943ccd6a9dcSMichael Chan 		fw_fec |= BNXT_FEC_LLRS_ON;
1944ccd6a9dcSMichael Chan 	return fw_fec;
1945ccd6a9dcSMichael Chan }
1946ccd6a9dcSMichael Chan 
1947ccd6a9dcSMichael Chan static int bnxt_set_fecparam(struct net_device *dev,
1948ccd6a9dcSMichael Chan 			     struct ethtool_fecparam *fecparam)
1949ccd6a9dcSMichael Chan {
1950ccd6a9dcSMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
1951ccd6a9dcSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1952ccd6a9dcSMichael Chan 	struct bnxt_link_info *link_info;
1953ccd6a9dcSMichael Chan 	u32 new_cfg, fec = fecparam->fec;
1954ccd6a9dcSMichael Chan 	u16 fec_cfg;
1955ccd6a9dcSMichael Chan 	int rc;
1956ccd6a9dcSMichael Chan 
1957ccd6a9dcSMichael Chan 	link_info = &bp->link_info;
1958ccd6a9dcSMichael Chan 	fec_cfg = link_info->fec_cfg;
1959ccd6a9dcSMichael Chan 	if (fec_cfg & BNXT_FEC_NONE)
1960ccd6a9dcSMichael Chan 		return -EOPNOTSUPP;
1961ccd6a9dcSMichael Chan 
1962ccd6a9dcSMichael Chan 	if (fec & ETHTOOL_FEC_OFF) {
1963ccd6a9dcSMichael Chan 		new_cfg = PORT_PHY_CFG_REQ_FLAGS_FEC_AUTONEG_DISABLE |
1964ccd6a9dcSMichael Chan 			  BNXT_FEC_ALL_OFF(link_info);
1965ccd6a9dcSMichael Chan 		goto apply_fec;
1966ccd6a9dcSMichael Chan 	}
1967ccd6a9dcSMichael Chan 	if (((fec & ETHTOOL_FEC_AUTO) && !(fec_cfg & BNXT_FEC_AUTONEG_CAP)) ||
1968ccd6a9dcSMichael Chan 	    ((fec & ETHTOOL_FEC_RS) && !(fec_cfg & BNXT_FEC_ENC_RS_CAP)) ||
1969ccd6a9dcSMichael Chan 	    ((fec & ETHTOOL_FEC_LLRS) && !(fec_cfg & BNXT_FEC_ENC_LLRS_CAP)) ||
1970ccd6a9dcSMichael Chan 	    ((fec & ETHTOOL_FEC_BASER) && !(fec_cfg & BNXT_FEC_ENC_BASE_R_CAP)))
1971ccd6a9dcSMichael Chan 		return -EINVAL;
1972ccd6a9dcSMichael Chan 
1973ccd6a9dcSMichael Chan 	if (fec & ETHTOOL_FEC_AUTO) {
1974ccd6a9dcSMichael Chan 		if (!link_info->autoneg)
1975ccd6a9dcSMichael Chan 			return -EINVAL;
1976ccd6a9dcSMichael Chan 		new_cfg = PORT_PHY_CFG_REQ_FLAGS_FEC_AUTONEG_ENABLE;
1977ccd6a9dcSMichael Chan 	} else {
1978ccd6a9dcSMichael Chan 		new_cfg = bnxt_ethtool_forced_fec_to_fw(link_info, fec);
1979ccd6a9dcSMichael Chan 	}
1980ccd6a9dcSMichael Chan 
1981ccd6a9dcSMichael Chan apply_fec:
1982ccd6a9dcSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
1983ccd6a9dcSMichael Chan 	req.flags = cpu_to_le32(new_cfg | PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
1984ccd6a9dcSMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1985ccd6a9dcSMichael Chan 	/* update current settings */
1986ccd6a9dcSMichael Chan 	if (!rc) {
1987ccd6a9dcSMichael Chan 		mutex_lock(&bp->link_lock);
1988ccd6a9dcSMichael Chan 		bnxt_update_link(bp, false);
1989ccd6a9dcSMichael Chan 		mutex_unlock(&bp->link_lock);
1990ccd6a9dcSMichael Chan 	}
1991ccd6a9dcSMichael Chan 	return rc;
1992ccd6a9dcSMichael Chan }
1993ccd6a9dcSMichael Chan 
1994c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
1995c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
1996c0c050c5SMichael Chan {
1997c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1998c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1999c0c050c5SMichael Chan 
2000c0c050c5SMichael Chan 	if (BNXT_VF(bp))
2001c0c050c5SMichael Chan 		return;
2002b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
20033c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
20043c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
2005c0c050c5SMichael Chan }
2006c0c050c5SMichael Chan 
2007423cffcfSJakub Kicinski static void bnxt_get_pause_stats(struct net_device *dev,
2008423cffcfSJakub Kicinski 				 struct ethtool_pause_stats *epstat)
2009423cffcfSJakub Kicinski {
2010423cffcfSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
2011423cffcfSJakub Kicinski 	u64 *rx, *tx;
2012423cffcfSJakub Kicinski 
2013423cffcfSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS))
2014423cffcfSJakub Kicinski 		return;
2015423cffcfSJakub Kicinski 
2016423cffcfSJakub Kicinski 	rx = bp->port_stats.sw_stats;
2017423cffcfSJakub Kicinski 	tx = bp->port_stats.sw_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
2018423cffcfSJakub Kicinski 
2019423cffcfSJakub Kicinski 	epstat->rx_pause_frames = BNXT_GET_RX_PORT_STATS64(rx, rx_pause_frames);
2020423cffcfSJakub Kicinski 	epstat->tx_pause_frames = BNXT_GET_TX_PORT_STATS64(tx, tx_pause_frames);
2021423cffcfSJakub Kicinski }
2022423cffcfSJakub Kicinski 
2023c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
2024c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
2025c0c050c5SMichael Chan {
2026c0c050c5SMichael Chan 	int rc = 0;
2027c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2028c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
2029c0c050c5SMichael Chan 
2030c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
203175362a3fSMichael Chan 		return -EOPNOTSUPP;
2032c0c050c5SMichael Chan 
2033a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
2034c0c050c5SMichael Chan 	if (epause->autoneg) {
2035a5390690SMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
2036a5390690SMichael Chan 			rc = -EINVAL;
2037a5390690SMichael Chan 			goto pause_exit;
2038a5390690SMichael Chan 		}
2039b763499eSMichael Chan 
2040c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
2041c9ee9516SMichael Chan 		if (bp->hwrm_spec_code >= 0x10201)
2042c9ee9516SMichael Chan 			link_info->req_flow_ctrl =
2043c9ee9516SMichael Chan 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
2044c0c050c5SMichael Chan 	} else {
2045c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
2046c0c050c5SMichael Chan 		 * force a link change
2047c0c050c5SMichael Chan 		 */
2048c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
2049c0c050c5SMichael Chan 			link_info->force_link_chng = true;
2050c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
2051c9ee9516SMichael Chan 		link_info->req_flow_ctrl = 0;
2052c0c050c5SMichael Chan 	}
2053c0c050c5SMichael Chan 	if (epause->rx_pause)
2054c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
2055c0c050c5SMichael Chan 
2056c0c050c5SMichael Chan 	if (epause->tx_pause)
2057c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
2058c0c050c5SMichael Chan 
2059a5390690SMichael Chan 	if (netif_running(dev))
2060c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
2061a5390690SMichael Chan 
2062a5390690SMichael Chan pause_exit:
2063163e9ef6SVasundhara Volam 	mutex_unlock(&bp->link_lock);
2064c0c050c5SMichael Chan 	return rc;
2065c0c050c5SMichael Chan }
2066c0c050c5SMichael Chan 
2067c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
2068c0c050c5SMichael Chan {
2069c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2070c0c050c5SMichael Chan 
2071c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
2072c0c050c5SMichael Chan 	return bp->link_info.link_up;
2073c0c050c5SMichael Chan }
2074c0c050c5SMichael Chan 
20754933f675SVasundhara Volam int bnxt_hwrm_nvm_get_dev_info(struct bnxt *bp,
20764933f675SVasundhara Volam 			       struct hwrm_nvm_get_dev_info_output *nvm_dev_info)
20774933f675SVasundhara Volam {
20784933f675SVasundhara Volam 	struct hwrm_nvm_get_dev_info_output *resp = bp->hwrm_cmd_resp_addr;
20794933f675SVasundhara Volam 	struct hwrm_nvm_get_dev_info_input req = {0};
20804933f675SVasundhara Volam 	int rc;
20814933f675SVasundhara Volam 
20820ae0a779SVasundhara Volam 	if (BNXT_VF(bp))
20830ae0a779SVasundhara Volam 		return -EOPNOTSUPP;
20840ae0a779SVasundhara Volam 
20854933f675SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DEV_INFO, -1, -1);
20864933f675SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
20874933f675SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
20884933f675SVasundhara Volam 	if (!rc)
20894933f675SVasundhara Volam 		memcpy(nvm_dev_info, resp, sizeof(*resp));
20904933f675SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
20914933f675SVasundhara Volam 	return rc;
20924933f675SVasundhara Volam }
20934933f675SVasundhara Volam 
2094b3b0ddd0SMichael Chan static void bnxt_print_admin_err(struct bnxt *bp)
2095b3b0ddd0SMichael Chan {
2096b3b0ddd0SMichael Chan 	netdev_info(bp->dev, "PF does not have admin privileges to flash or reset the device\n");
2097b3b0ddd0SMichael Chan }
2098b3b0ddd0SMichael Chan 
20995ac67d8bSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
21005ac67d8bSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
21015ac67d8bSRob Swindell 				u32 *data_length);
21025ac67d8bSRob Swindell 
210393ff3435SPavan Chebbi static int __bnxt_flash_nvram(struct net_device *dev, u16 dir_type,
210493ff3435SPavan Chebbi 			      u16 dir_ordinal, u16 dir_ext, u16 dir_attr,
210593ff3435SPavan Chebbi 			      u32 dir_item_len, const u8 *data,
2106c0c050c5SMichael Chan 			      size_t data_len)
2107c0c050c5SMichael Chan {
2108c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2109c0c050c5SMichael Chan 	int rc;
2110c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
2111c0c050c5SMichael Chan 	dma_addr_t dma_handle;
211293ff3435SPavan Chebbi 	u8 *kmem = NULL;
2113c0c050c5SMichael Chan 
2114c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
2115c0c050c5SMichael Chan 
2116c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
2117c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
2118c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
2119c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
212093ff3435SPavan Chebbi 	req.dir_item_length = cpu_to_le32(dir_item_len);
212193ff3435SPavan Chebbi 	if (data_len && data) {
2122c0c050c5SMichael Chan 		req.dir_data_length = cpu_to_le32(data_len);
2123c0c050c5SMichael Chan 
2124c0c050c5SMichael Chan 		kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
2125c0c050c5SMichael Chan 					  GFP_KERNEL);
212693ff3435SPavan Chebbi 		if (!kmem)
2127c0c050c5SMichael Chan 			return -ENOMEM;
212893ff3435SPavan Chebbi 
2129c0c050c5SMichael Chan 		memcpy(kmem, data, data_len);
2130c0c050c5SMichael Chan 		req.host_src_addr = cpu_to_le64(dma_handle);
213193ff3435SPavan Chebbi 	}
2132c0c050c5SMichael Chan 
213393ff3435SPavan Chebbi 	rc = _hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
213493ff3435SPavan Chebbi 	if (kmem)
2135c0c050c5SMichael Chan 		dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
2136c0c050c5SMichael Chan 
2137d4f1420dSMichael Chan 	if (rc == -EACCES)
2138b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
2139c0c050c5SMichael Chan 	return rc;
2140c0c050c5SMichael Chan }
2141c0c050c5SMichael Chan 
214293ff3435SPavan Chebbi static int bnxt_flash_nvram(struct net_device *dev, u16 dir_type,
214393ff3435SPavan Chebbi 			    u16 dir_ordinal, u16 dir_ext, u16 dir_attr,
214493ff3435SPavan Chebbi 			    const u8 *data, size_t data_len)
214593ff3435SPavan Chebbi {
214693ff3435SPavan Chebbi 	struct bnxt *bp = netdev_priv(dev);
214793ff3435SPavan Chebbi 	int rc;
214893ff3435SPavan Chebbi 
214993ff3435SPavan Chebbi 	mutex_lock(&bp->hwrm_cmd_lock);
215093ff3435SPavan Chebbi 	rc = __bnxt_flash_nvram(dev, dir_type, dir_ordinal, dir_ext, dir_attr,
215193ff3435SPavan Chebbi 				0, data, data_len);
215293ff3435SPavan Chebbi 	mutex_unlock(&bp->hwrm_cmd_lock);
215393ff3435SPavan Chebbi 	return rc;
215493ff3435SPavan Chebbi }
215593ff3435SPavan Chebbi 
215695fec034SEdwin Peer static int bnxt_hwrm_firmware_reset(struct net_device *dev, u8 proc_type,
215795fec034SEdwin Peer 				    u8 self_reset, u8 flags)
2158d2d6318cSRob Swindell {
2159d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
21607c675421SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
21617c675421SVasundhara Volam 	int rc;
2162d2d6318cSRob Swindell 
2163d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
2164d2d6318cSRob Swindell 
216595fec034SEdwin Peer 	req.embedded_proc_type = proc_type;
216695fec034SEdwin Peer 	req.selfrst_status = self_reset;
216795fec034SEdwin Peer 	req.flags = flags;
216895fec034SEdwin Peer 
21698cec0940SEdwin Peer 	if (proc_type == FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP) {
21708cec0940SEdwin Peer 		rc = hwrm_send_message_silent(bp, &req, sizeof(req),
21718cec0940SEdwin Peer 					      HWRM_CMD_TIMEOUT);
21728cec0940SEdwin Peer 	} else {
217395fec034SEdwin Peer 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
217495fec034SEdwin Peer 		if (rc == -EACCES)
217595fec034SEdwin Peer 			bnxt_print_admin_err(bp);
21768cec0940SEdwin Peer 	}
217795fec034SEdwin Peer 	return rc;
217895fec034SEdwin Peer }
217995fec034SEdwin Peer 
218094f17e89SEdwin Peer static int bnxt_firmware_reset(struct net_device *dev,
218194f17e89SEdwin Peer 			       enum bnxt_nvm_directory_type dir_type)
218295fec034SEdwin Peer {
218395fec034SEdwin Peer 	u8 self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE;
218495fec034SEdwin Peer 	u8 proc_type, flags = 0;
218595fec034SEdwin Peer 
2186d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
2187d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
2188d2d6318cSRob Swindell 	switch (dir_type) {
2189d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
2190d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
2191d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
219295fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
2193d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
219495fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
2195d2d6318cSRob Swindell 		break;
2196d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
2197d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
219895fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
219908141e0bSRob Swindell 		/* Self-reset APE upon next PCIe reset: */
220095fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
2201d2d6318cSRob Swindell 		break;
2202d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
2203d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
220495fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
2205d2d6318cSRob Swindell 		break;
2206d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
2207d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
220895fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
2209d2d6318cSRob Swindell 		break;
2210d2d6318cSRob Swindell 	default:
2211d2d6318cSRob Swindell 		return -EINVAL;
2212d2d6318cSRob Swindell 	}
2213d2d6318cSRob Swindell 
221495fec034SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, proc_type, self_reset, flags);
2215d2d6318cSRob Swindell }
2216d2d6318cSRob Swindell 
221794f17e89SEdwin Peer static int bnxt_firmware_reset_chip(struct net_device *dev)
221894f17e89SEdwin Peer {
221994f17e89SEdwin Peer 	struct bnxt *bp = netdev_priv(dev);
222094f17e89SEdwin Peer 	u8 flags = 0;
222194f17e89SEdwin Peer 
222294f17e89SEdwin Peer 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
222394f17e89SEdwin Peer 		flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
222494f17e89SEdwin Peer 
222594f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev,
222694f17e89SEdwin Peer 					FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP,
222794f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP,
222894f17e89SEdwin Peer 					flags);
222994f17e89SEdwin Peer }
223094f17e89SEdwin Peer 
223194f17e89SEdwin Peer static int bnxt_firmware_reset_ap(struct net_device *dev)
223294f17e89SEdwin Peer {
223394f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP,
223494f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE,
223594f17e89SEdwin Peer 					0);
223694f17e89SEdwin Peer }
223794f17e89SEdwin Peer 
2238c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
2239c0c050c5SMichael Chan 			       u16 dir_type,
2240c0c050c5SMichael Chan 			       const u8 *fw_data,
2241c0c050c5SMichael Chan 			       size_t fw_size)
2242c0c050c5SMichael Chan {
2243c0c050c5SMichael Chan 	int	rc = 0;
2244c0c050c5SMichael Chan 	u16	code_type;
2245c0c050c5SMichael Chan 	u32	stored_crc;
2246c0c050c5SMichael Chan 	u32	calculated_crc;
2247c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
2248c0c050c5SMichael Chan 
2249c0c050c5SMichael Chan 	switch (dir_type) {
2250c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2251c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2252c0c050c5SMichael Chan 		code_type = CODE_BOOT;
2253c0c050c5SMichael Chan 		break;
225493e0b4feSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
225593e0b4feSRob Swindell 		code_type = CODE_CHIMP_PATCH;
225693e0b4feSRob Swindell 		break;
22572731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
22582731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
22592731d70fSRob Swindell 		break;
226093e0b4feSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
226193e0b4feSRob Swindell 		code_type = CODE_APE_PATCH;
226293e0b4feSRob Swindell 		break;
226393e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
226493e0b4feSRob Swindell 		code_type = CODE_KONG_FW;
226593e0b4feSRob Swindell 		break;
226693e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
226793e0b4feSRob Swindell 		code_type = CODE_KONG_PATCH;
226893e0b4feSRob Swindell 		break;
226993e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
227093e0b4feSRob Swindell 		code_type = CODE_BONO_FW;
227193e0b4feSRob Swindell 		break;
227293e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
227393e0b4feSRob Swindell 		code_type = CODE_BONO_PATCH;
227493e0b4feSRob Swindell 		break;
2275c0c050c5SMichael Chan 	default:
2276c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
2277c0c050c5SMichael Chan 			   dir_type);
2278c0c050c5SMichael Chan 		return -EINVAL;
2279c0c050c5SMichael Chan 	}
2280c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
2281c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
2282c0c050c5SMichael Chan 			   (unsigned int)fw_size);
2283c0c050c5SMichael Chan 		return -EINVAL;
2284c0c050c5SMichael Chan 	}
2285c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
2286c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
2287c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
2288c0c050c5SMichael Chan 		return -EINVAL;
2289c0c050c5SMichael Chan 	}
2290c0c050c5SMichael Chan 	if (header->code_type != code_type) {
2291c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
2292c0c050c5SMichael Chan 			   code_type, header->code_type);
2293c0c050c5SMichael Chan 		return -EINVAL;
2294c0c050c5SMichael Chan 	}
2295c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
2296c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
2297c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
2298c0c050c5SMichael Chan 		return -EINVAL;
2299c0c050c5SMichael Chan 	}
2300c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
2301c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
2302c0c050c5SMichael Chan 					     sizeof(stored_crc)));
2303c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
2304c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
2305c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
2306c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
2307c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
2308c0c050c5SMichael Chan 		return -EINVAL;
2309c0c050c5SMichael Chan 	}
2310c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2311c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
2312d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
2313d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
2314d2d6318cSRob Swindell 
2315c0c050c5SMichael Chan 	return rc;
2316c0c050c5SMichael Chan }
2317c0c050c5SMichael Chan 
23185ac67d8bSRob Swindell static int bnxt_flash_microcode(struct net_device *dev,
23195ac67d8bSRob Swindell 				u16 dir_type,
23205ac67d8bSRob Swindell 				const u8 *fw_data,
23215ac67d8bSRob Swindell 				size_t fw_size)
23225ac67d8bSRob Swindell {
23235ac67d8bSRob Swindell 	struct bnxt_ucode_trailer *trailer;
23245ac67d8bSRob Swindell 	u32 calculated_crc;
23255ac67d8bSRob Swindell 	u32 stored_crc;
23265ac67d8bSRob Swindell 	int rc = 0;
23275ac67d8bSRob Swindell 
23285ac67d8bSRob Swindell 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
23295ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode file size: %u\n",
23305ac67d8bSRob Swindell 			   (unsigned int)fw_size);
23315ac67d8bSRob Swindell 		return -EINVAL;
23325ac67d8bSRob Swindell 	}
23335ac67d8bSRob Swindell 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
23345ac67d8bSRob Swindell 						sizeof(*trailer)));
23355ac67d8bSRob Swindell 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
23365ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
23375ac67d8bSRob Swindell 			   le32_to_cpu(trailer->sig));
23385ac67d8bSRob Swindell 		return -EINVAL;
23395ac67d8bSRob Swindell 	}
23405ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
23415ac67d8bSRob Swindell 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
23425ac67d8bSRob Swindell 			   dir_type, le16_to_cpu(trailer->dir_type));
23435ac67d8bSRob Swindell 		return -EINVAL;
23445ac67d8bSRob Swindell 	}
23455ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->trailer_length) <
23465ac67d8bSRob Swindell 		sizeof(struct bnxt_ucode_trailer)) {
23475ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
23485ac67d8bSRob Swindell 			   le16_to_cpu(trailer->trailer_length));
23495ac67d8bSRob Swindell 		return -EINVAL;
23505ac67d8bSRob Swindell 	}
23515ac67d8bSRob Swindell 
23525ac67d8bSRob Swindell 	/* Confirm the CRC32 checksum of the file: */
23535ac67d8bSRob Swindell 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
23545ac67d8bSRob Swindell 					     sizeof(stored_crc)));
23555ac67d8bSRob Swindell 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
23565ac67d8bSRob Swindell 	if (calculated_crc != stored_crc) {
23575ac67d8bSRob Swindell 		netdev_err(dev,
23585ac67d8bSRob Swindell 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
23595ac67d8bSRob Swindell 			   (unsigned long)stored_crc,
23605ac67d8bSRob Swindell 			   (unsigned long)calculated_crc);
23615ac67d8bSRob Swindell 		return -EINVAL;
23625ac67d8bSRob Swindell 	}
23635ac67d8bSRob Swindell 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
23645ac67d8bSRob Swindell 			      0, 0, fw_data, fw_size);
23655ac67d8bSRob Swindell 
23665ac67d8bSRob Swindell 	return rc;
23675ac67d8bSRob Swindell }
23685ac67d8bSRob Swindell 
2369c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
2370c0c050c5SMichael Chan {
2371c0c050c5SMichael Chan 	switch (dir_type) {
2372c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
2373c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2374c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2375c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
2376c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
2377c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
2378c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
237993e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
238093e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
2381c0c050c5SMichael Chan 		return true;
2382c0c050c5SMichael Chan 	}
2383c0c050c5SMichael Chan 
2384c0c050c5SMichael Chan 	return false;
2385c0c050c5SMichael Chan }
2386c0c050c5SMichael Chan 
23875ac67d8bSRob Swindell static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
2388c0c050c5SMichael Chan {
2389c0c050c5SMichael Chan 	switch (dir_type) {
2390c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
2391c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
2392c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
2393c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
2394c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
2395c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
2396c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
2397c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
2398c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
2399c0c050c5SMichael Chan 		return true;
2400c0c050c5SMichael Chan 	}
2401c0c050c5SMichael Chan 
2402c0c050c5SMichael Chan 	return false;
2403c0c050c5SMichael Chan }
2404c0c050c5SMichael Chan 
2405c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
2406c0c050c5SMichael Chan {
2407c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
24085ac67d8bSRob Swindell 		bnxt_dir_type_is_other_exec_format(dir_type);
2409c0c050c5SMichael Chan }
2410c0c050c5SMichael Chan 
2411c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
2412c0c050c5SMichael Chan 					 u16 dir_type,
2413c0c050c5SMichael Chan 					 const char *filename)
2414c0c050c5SMichael Chan {
2415c0c050c5SMichael Chan 	const struct firmware  *fw;
2416c0c050c5SMichael Chan 	int			rc;
2417c0c050c5SMichael Chan 
2418c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
2419c0c050c5SMichael Chan 	if (rc != 0) {
2420c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
2421c0c050c5SMichael Chan 			   rc, filename);
2422c0c050c5SMichael Chan 		return rc;
2423c0c050c5SMichael Chan 	}
2424ba425800SJason Yan 	if (bnxt_dir_type_is_ape_bin_format(dir_type))
2425c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
2426ba425800SJason Yan 	else if (bnxt_dir_type_is_other_exec_format(dir_type))
24275ac67d8bSRob Swindell 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
2428c0c050c5SMichael Chan 	else
2429c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2430c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
2431c0c050c5SMichael Chan 	release_firmware(fw);
2432c0c050c5SMichael Chan 	return rc;
2433c0c050c5SMichael Chan }
2434c0c050c5SMichael Chan 
2435b44cfd4fSJacob Keller int bnxt_flash_package_from_fw_obj(struct net_device *dev, const struct firmware *fw,
2436d168f328SVasundhara Volam 				   u32 install_type)
2437c0c050c5SMichael Chan {
24385ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_input install = {0};
2439a9094ba6SMichael Chan 	struct hwrm_nvm_install_update_output resp = {0};
2440a9094ba6SMichael Chan 	struct hwrm_nvm_modify_input modify = {0};
2441a9094ba6SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2442a9094ba6SMichael Chan 	dma_addr_t dma_handle;
2443a9094ba6SMichael Chan 	u8 *kmem = NULL;
24445ac67d8bSRob Swindell 	u32 item_len;
244522630e28SEdwin Peer 	int rc = 0;
24465ac67d8bSRob Swindell 	u16 index;
24475ac67d8bSRob Swindell 
24485ac67d8bSRob Swindell 	bnxt_hwrm_fw_set_time(bp);
24495ac67d8bSRob Swindell 
2450a9094ba6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
2451a9094ba6SMichael Chan 
2452a9094ba6SMichael Chan 	kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size, &dma_handle,
2453a9094ba6SMichael Chan 				  GFP_KERNEL);
2454a9094ba6SMichael Chan 	if (!kmem)
2455a9094ba6SMichael Chan 		return -ENOMEM;
2456a9094ba6SMichael Chan 
2457a9094ba6SMichael Chan 	modify.host_src_addr = cpu_to_le64(dma_handle);
2458a9094ba6SMichael Chan 
2459a9094ba6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
2460a9094ba6SMichael Chan 	if ((install_type & 0xffff) == 0)
2461a9094ba6SMichael Chan 		install_type >>= 16;
2462a9094ba6SMichael Chan 	install.install_type = cpu_to_le32(install_type);
2463a9094ba6SMichael Chan 
2464*2e5fb428SPavan Chebbi 	do {
246595ec1f47SVasundhara Volam 		rc = bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
2466*2e5fb428SPavan Chebbi 					  BNX_DIR_ORDINAL_FIRST,
2467*2e5fb428SPavan Chebbi 					  BNX_DIR_EXT_NONE,
246895ec1f47SVasundhara Volam 					  &index, &item_len, NULL);
246995ec1f47SVasundhara Volam 		if (rc) {
24705ac67d8bSRob Swindell 			netdev_err(dev, "PKG update area not created in nvram\n");
2471*2e5fb428SPavan Chebbi 			break;
24725ac67d8bSRob Swindell 		}
24735ac67d8bSRob Swindell 		if (fw->size > item_len) {
24749a005c38SJonathan Lemon 			netdev_err(dev, "PKG insufficient update area in nvram: %lu\n",
24755ac67d8bSRob Swindell 				   (unsigned long)fw->size);
24765ac67d8bSRob Swindell 			rc = -EFBIG;
2477*2e5fb428SPavan Chebbi 			break;
2478*2e5fb428SPavan Chebbi 		}
2479*2e5fb428SPavan Chebbi 
24805ac67d8bSRob Swindell 		modify.dir_idx = cpu_to_le16(index);
24815ac67d8bSRob Swindell 		modify.len = cpu_to_le32(fw->size);
24825ac67d8bSRob Swindell 
24835ac67d8bSRob Swindell 		memcpy(kmem, fw->data, fw->size);
248422630e28SEdwin Peer 		rc = hwrm_send_message(bp, &modify, sizeof(modify),
24855ac67d8bSRob Swindell 				       FLASH_PACKAGE_TIMEOUT);
248622630e28SEdwin Peer 		if (rc)
2487*2e5fb428SPavan Chebbi 			break;
24885ac67d8bSRob Swindell 
2489cb4d1d62SKshitij Soni 		mutex_lock(&bp->hwrm_cmd_lock);
249022630e28SEdwin Peer 		rc = _hwrm_send_message(bp, &install, sizeof(install),
24915ac67d8bSRob Swindell 					INSTALL_PACKAGE_TIMEOUT);
2492a9094ba6SMichael Chan 		memcpy(&resp, bp->hwrm_cmd_resp_addr, sizeof(resp));
2493cb4d1d62SKshitij Soni 
2494*2e5fb428SPavan Chebbi 		if (rc && ((struct hwrm_err_output *)&resp)->cmd_err ==
2495dd2ebf34SVasundhara Volam 		    NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
2496*2e5fb428SPavan Chebbi 			install.flags |=
2497*2e5fb428SPavan Chebbi 				cpu_to_le16(NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
2498*2e5fb428SPavan Chebbi 
249922630e28SEdwin Peer 			rc = _hwrm_send_message(bp, &install, sizeof(install),
2500cb4d1d62SKshitij Soni 						INSTALL_PACKAGE_TIMEOUT);
2501a9094ba6SMichael Chan 			memcpy(&resp, bp->hwrm_cmd_resp_addr, sizeof(resp));
2502dd2ebf34SVasundhara Volam 		}
2503*2e5fb428SPavan Chebbi 		mutex_unlock(&bp->hwrm_cmd_lock);
2504*2e5fb428SPavan Chebbi 	} while (false);
25055ac67d8bSRob Swindell 
2506*2e5fb428SPavan Chebbi 	dma_free_coherent(&bp->pdev->dev, fw->size, kmem, dma_handle);
2507a9094ba6SMichael Chan 	if (resp.result) {
25085ac67d8bSRob Swindell 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
2509a9094ba6SMichael Chan 			   (s8)resp.result, (int)resp.problem_item);
2510cb4d1d62SKshitij Soni 		rc = -ENOPKG;
25115ac67d8bSRob Swindell 	}
251222630e28SEdwin Peer 	if (rc == -EACCES)
2513b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
2514cb4d1d62SKshitij Soni 	return rc;
2515c0c050c5SMichael Chan }
2516c0c050c5SMichael Chan 
2517b44cfd4fSJacob Keller static int bnxt_flash_package_from_file(struct net_device *dev, const char *filename,
2518b44cfd4fSJacob Keller 					u32 install_type)
2519b44cfd4fSJacob Keller {
2520b44cfd4fSJacob Keller 	const struct firmware *fw;
2521b44cfd4fSJacob Keller 	int rc;
2522b44cfd4fSJacob Keller 
2523b44cfd4fSJacob Keller 	rc = request_firmware(&fw, filename, &dev->dev);
2524b44cfd4fSJacob Keller 	if (rc != 0) {
2525b44cfd4fSJacob Keller 		netdev_err(dev, "PKG error %d requesting file: %s\n",
2526b44cfd4fSJacob Keller 			   rc, filename);
2527b44cfd4fSJacob Keller 		return rc;
2528b44cfd4fSJacob Keller 	}
2529b44cfd4fSJacob Keller 
2530b44cfd4fSJacob Keller 	rc = bnxt_flash_package_from_fw_obj(dev, fw, install_type);
2531b44cfd4fSJacob Keller 
2532b44cfd4fSJacob Keller 	release_firmware(fw);
2533b44cfd4fSJacob Keller 
2534b44cfd4fSJacob Keller 	return rc;
2535b44cfd4fSJacob Keller }
2536b44cfd4fSJacob Keller 
2537c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
2538c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
2539c0c050c5SMichael Chan {
2540c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
2541c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
2542c0c050c5SMichael Chan 		return -EINVAL;
2543c0c050c5SMichael Chan 	}
2544c0c050c5SMichael Chan 
25455ac67d8bSRob Swindell 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
25465ac67d8bSRob Swindell 	    flash->region > 0xffff)
25475ac67d8bSRob Swindell 		return bnxt_flash_package_from_file(dev, flash->data,
25485ac67d8bSRob Swindell 						    flash->region);
2549c0c050c5SMichael Chan 
2550c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
2551c0c050c5SMichael Chan }
2552c0c050c5SMichael Chan 
2553c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
2554c0c050c5SMichael Chan {
2555c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2556c0c050c5SMichael Chan 	int rc;
2557c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
2558c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
2559c0c050c5SMichael Chan 
2560c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
2561c0c050c5SMichael Chan 
2562c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2563c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2564c0c050c5SMichael Chan 	if (!rc) {
2565c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
2566c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
2567c0c050c5SMichael Chan 	}
2568c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2569c0c050c5SMichael Chan 	return rc;
2570c0c050c5SMichael Chan }
2571c0c050c5SMichael Chan 
2572c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
2573c0c050c5SMichael Chan {
25744cebbacaSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
25754cebbacaSMichael Chan 
25764cebbacaSMichael Chan 	if (BNXT_VF(bp))
25774cebbacaSMichael Chan 		return 0;
25784cebbacaSMichael Chan 
2579c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
2580c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
2581c0c050c5SMichael Chan 	 */
2582c0c050c5SMichael Chan 	return -1;
2583c0c050c5SMichael Chan }
2584c0c050c5SMichael Chan 
2585c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
2586c0c050c5SMichael Chan {
2587c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2588c0c050c5SMichael Chan 	int rc;
2589c0c050c5SMichael Chan 	u32 dir_entries;
2590c0c050c5SMichael Chan 	u32 entry_length;
2591c0c050c5SMichael Chan 	u8 *buf;
2592c0c050c5SMichael Chan 	size_t buflen;
2593c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2594c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
2595c0c050c5SMichael Chan 
2596c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
2597c0c050c5SMichael Chan 	if (rc != 0)
2598c0c050c5SMichael Chan 		return rc;
2599c0c050c5SMichael Chan 
2600dbbfa96aSVasundhara Volam 	if (!dir_entries || !entry_length)
2601dbbfa96aSVasundhara Volam 		return -EIO;
2602dbbfa96aSVasundhara Volam 
2603c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
2604c0c050c5SMichael Chan 	if (len < 2)
2605c0c050c5SMichael Chan 		return -EINVAL;
2606c0c050c5SMichael Chan 
2607c0c050c5SMichael Chan 	*data++ = dir_entries;
2608c0c050c5SMichael Chan 	*data++ = entry_length;
2609c0c050c5SMichael Chan 	len -= 2;
2610c0c050c5SMichael Chan 	memset(data, 0xff, len);
2611c0c050c5SMichael Chan 
2612c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
2613c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
2614c0c050c5SMichael Chan 				 GFP_KERNEL);
2615c0c050c5SMichael Chan 	if (!buf) {
2616c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2617c0c050c5SMichael Chan 			   (unsigned)buflen);
2618c0c050c5SMichael Chan 		return -ENOMEM;
2619c0c050c5SMichael Chan 	}
2620c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
2621c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2622c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2623c0c050c5SMichael Chan 	if (rc == 0)
2624c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
2625c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
2626c0c050c5SMichael Chan 	return rc;
2627c0c050c5SMichael Chan }
2628c0c050c5SMichael Chan 
2629c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
2630c0c050c5SMichael Chan 			       u32 length, u8 *data)
2631c0c050c5SMichael Chan {
2632c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2633c0c050c5SMichael Chan 	int rc;
2634c0c050c5SMichael Chan 	u8 *buf;
2635c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2636c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
2637c0c050c5SMichael Chan 
2638e0ad8fc5SMichael Chan 	if (!length)
2639e0ad8fc5SMichael Chan 		return -EINVAL;
2640e0ad8fc5SMichael Chan 
2641c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
2642c0c050c5SMichael Chan 				 GFP_KERNEL);
2643c0c050c5SMichael Chan 	if (!buf) {
2644c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2645c0c050c5SMichael Chan 			   (unsigned)length);
2646c0c050c5SMichael Chan 		return -ENOMEM;
2647c0c050c5SMichael Chan 	}
2648c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
2649c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2650c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2651c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
2652c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
2653c0c050c5SMichael Chan 
2654c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2655c0c050c5SMichael Chan 	if (rc == 0)
2656c0c050c5SMichael Chan 		memcpy(data, buf, length);
2657c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
2658c0c050c5SMichael Chan 	return rc;
2659c0c050c5SMichael Chan }
2660c0c050c5SMichael Chan 
26613ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
26623ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
26633ebf6f0aSRob Swindell 				u32 *data_length)
26643ebf6f0aSRob Swindell {
26653ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
26663ebf6f0aSRob Swindell 	int rc;
26673ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
26683ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
26693ebf6f0aSRob Swindell 
26703ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
26713ebf6f0aSRob Swindell 	req.enables = 0;
26723ebf6f0aSRob Swindell 	req.dir_idx = 0;
26733ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
26743ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
26753ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
26763ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
2677cc72f3b1SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2678cc72f3b1SMichael Chan 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
26793ebf6f0aSRob Swindell 	if (rc == 0) {
26803ebf6f0aSRob Swindell 		if (index)
26813ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
26823ebf6f0aSRob Swindell 		if (item_length)
26833ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
26843ebf6f0aSRob Swindell 		if (data_length)
26853ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
26863ebf6f0aSRob Swindell 	}
2687cc72f3b1SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
26883ebf6f0aSRob Swindell 	return rc;
26893ebf6f0aSRob Swindell }
26903ebf6f0aSRob Swindell 
26913ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
26923ebf6f0aSRob Swindell {
26933ebf6f0aSRob Swindell 	char	*retval = NULL;
26943ebf6f0aSRob Swindell 	char	*p;
26953ebf6f0aSRob Swindell 	char	*value;
26963ebf6f0aSRob Swindell 	int	field = 0;
26973ebf6f0aSRob Swindell 
26983ebf6f0aSRob Swindell 	if (datalen < 1)
26993ebf6f0aSRob Swindell 		return NULL;
27003ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
27013ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
27023ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
27033ebf6f0aSRob Swindell 		field = 0;
27043ebf6f0aSRob Swindell 		retval = NULL;
27053ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
27063ebf6f0aSRob Swindell 			value = p;
27073ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
27083ebf6f0aSRob Swindell 				p++;
27093ebf6f0aSRob Swindell 			if (field == desired_field)
27103ebf6f0aSRob Swindell 				retval = value;
27113ebf6f0aSRob Swindell 			if (*p != '\t')
27123ebf6f0aSRob Swindell 				break;
27133ebf6f0aSRob Swindell 			*p = 0;
27143ebf6f0aSRob Swindell 			field++;
27153ebf6f0aSRob Swindell 			p++;
27163ebf6f0aSRob Swindell 		}
27173ebf6f0aSRob Swindell 		if (*p == 0)
27183ebf6f0aSRob Swindell 			break;
27193ebf6f0aSRob Swindell 		*p = 0;
27203ebf6f0aSRob Swindell 	}
27213ebf6f0aSRob Swindell 	return retval;
27223ebf6f0aSRob Swindell }
27233ebf6f0aSRob Swindell 
2724a60faa60SVasundhara Volam static void bnxt_get_pkgver(struct net_device *dev)
27253ebf6f0aSRob Swindell {
2726a60faa60SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
27273ebf6f0aSRob Swindell 	u16 index = 0;
2728a60faa60SVasundhara Volam 	char *pkgver;
2729a60faa60SVasundhara Volam 	u32 pkglen;
2730a60faa60SVasundhara Volam 	u8 *pkgbuf;
2731a60faa60SVasundhara Volam 	int len;
27323ebf6f0aSRob Swindell 
27333ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
27343ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2735a60faa60SVasundhara Volam 				 &index, NULL, &pkglen) != 0)
2736a60faa60SVasundhara Volam 		return;
27373ebf6f0aSRob Swindell 
2738a60faa60SVasundhara Volam 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
2739a60faa60SVasundhara Volam 	if (!pkgbuf) {
2740a60faa60SVasundhara Volam 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
2741a60faa60SVasundhara Volam 			pkglen);
2742a60faa60SVasundhara Volam 		return;
2743a60faa60SVasundhara Volam 	}
27443ebf6f0aSRob Swindell 
2745a60faa60SVasundhara Volam 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2746a60faa60SVasundhara Volam 		goto err;
2747a60faa60SVasundhara Volam 
2748a60faa60SVasundhara Volam 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2749a60faa60SVasundhara Volam 				   pkglen);
2750a60faa60SVasundhara Volam 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2751a60faa60SVasundhara Volam 		len = strlen(bp->fw_ver_str);
2752a60faa60SVasundhara Volam 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2753a60faa60SVasundhara Volam 			 "/pkg %s", pkgver);
2754a60faa60SVasundhara Volam 	}
2755a60faa60SVasundhara Volam err:
2756a60faa60SVasundhara Volam 	kfree(pkgbuf);
27573ebf6f0aSRob Swindell }
27583ebf6f0aSRob Swindell 
2759c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
2760c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2761c0c050c5SMichael Chan 			   u8 *data)
2762c0c050c5SMichael Chan {
2763c0c050c5SMichael Chan 	u32 index;
2764c0c050c5SMichael Chan 	u32 offset;
2765c0c050c5SMichael Chan 
2766c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
2767c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2768c0c050c5SMichael Chan 
2769c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
2770c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
2771c0c050c5SMichael Chan 
2772c0c050c5SMichael Chan 	if (index == 0) {
2773c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
2774c0c050c5SMichael Chan 		return -EINVAL;
2775c0c050c5SMichael Chan 	}
2776c0c050c5SMichael Chan 
2777c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2778c0c050c5SMichael Chan }
2779c0c050c5SMichael Chan 
2780c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2781c0c050c5SMichael Chan {
2782c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2783c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2784c0c050c5SMichael Chan 
2785c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2786c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2787c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2788c0c050c5SMichael Chan }
2789c0c050c5SMichael Chan 
2790c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
2791c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2792c0c050c5SMichael Chan 			   u8 *data)
2793c0c050c5SMichael Chan {
2794c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2795c0c050c5SMichael Chan 	u8 index, dir_op;
2796c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
2797c0c050c5SMichael Chan 
2798c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
2799c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2800c0c050c5SMichael Chan 		return -EINVAL;
2801c0c050c5SMichael Chan 	}
2802c0c050c5SMichael Chan 
2803c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
2804c0c050c5SMichael Chan 
2805c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
2806c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
2807c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
2808c0c050c5SMichael Chan 		if (index == 0)
2809c0c050c5SMichael Chan 			return -EINVAL;
2810c0c050c5SMichael Chan 		switch (dir_op) {
2811c0c050c5SMichael Chan 		case 0x0e: /* erase */
2812c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
2813c0c050c5SMichael Chan 				return -EINVAL;
2814c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
2815c0c050c5SMichael Chan 		default:
2816c0c050c5SMichael Chan 			return -EINVAL;
2817c0c050c5SMichael Chan 		}
2818c0c050c5SMichael Chan 	}
2819c0c050c5SMichael Chan 
2820c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
2821ba425800SJason Yan 	if (bnxt_dir_type_is_executable(type))
28225ac67d8bSRob Swindell 		return -EOPNOTSUPP;
2823c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
2824c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
2825c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
2826c0c050c5SMichael Chan 
2827c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2828c0c050c5SMichael Chan 				eeprom->len);
2829c0c050c5SMichael Chan }
2830c0c050c5SMichael Chan 
283172b34f04SMichael Chan static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
283272b34f04SMichael Chan {
283372b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
283472b34f04SMichael Chan 	struct ethtool_eee *eee = &bp->eee;
283572b34f04SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
2836a5390690SMichael Chan 	u32 advertising;
283772b34f04SMichael Chan 	int rc = 0;
283872b34f04SMichael Chan 
2839c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
284075362a3fSMichael Chan 		return -EOPNOTSUPP;
284172b34f04SMichael Chan 
284272b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
284372b34f04SMichael Chan 		return -EOPNOTSUPP;
284472b34f04SMichael Chan 
2845a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
2846a5390690SMichael Chan 	advertising = _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
284772b34f04SMichael Chan 	if (!edata->eee_enabled)
284872b34f04SMichael Chan 		goto eee_ok;
284972b34f04SMichael Chan 
285072b34f04SMichael Chan 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
285172b34f04SMichael Chan 		netdev_warn(dev, "EEE requires autoneg\n");
2852a5390690SMichael Chan 		rc = -EINVAL;
2853a5390690SMichael Chan 		goto eee_exit;
285472b34f04SMichael Chan 	}
285572b34f04SMichael Chan 	if (edata->tx_lpi_enabled) {
285672b34f04SMichael Chan 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
285772b34f04SMichael Chan 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
285872b34f04SMichael Chan 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
285972b34f04SMichael Chan 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
2860a5390690SMichael Chan 			rc = -EINVAL;
2861a5390690SMichael Chan 			goto eee_exit;
286272b34f04SMichael Chan 		} else if (!bp->lpi_tmr_hi) {
286372b34f04SMichael Chan 			edata->tx_lpi_timer = eee->tx_lpi_timer;
286472b34f04SMichael Chan 		}
286572b34f04SMichael Chan 	}
286672b34f04SMichael Chan 	if (!edata->advertised) {
286772b34f04SMichael Chan 		edata->advertised = advertising & eee->supported;
286872b34f04SMichael Chan 	} else if (edata->advertised & ~advertising) {
286972b34f04SMichael Chan 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
287072b34f04SMichael Chan 			    edata->advertised, advertising);
2871a5390690SMichael Chan 		rc = -EINVAL;
2872a5390690SMichael Chan 		goto eee_exit;
287372b34f04SMichael Chan 	}
287472b34f04SMichael Chan 
287572b34f04SMichael Chan 	eee->advertised = edata->advertised;
287672b34f04SMichael Chan 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
287772b34f04SMichael Chan 	eee->tx_lpi_timer = edata->tx_lpi_timer;
287872b34f04SMichael Chan eee_ok:
287972b34f04SMichael Chan 	eee->eee_enabled = edata->eee_enabled;
288072b34f04SMichael Chan 
288172b34f04SMichael Chan 	if (netif_running(dev))
288272b34f04SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
288372b34f04SMichael Chan 
2884a5390690SMichael Chan eee_exit:
2885a5390690SMichael Chan 	mutex_unlock(&bp->link_lock);
288672b34f04SMichael Chan 	return rc;
288772b34f04SMichael Chan }
288872b34f04SMichael Chan 
288972b34f04SMichael Chan static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
289072b34f04SMichael Chan {
289172b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
289272b34f04SMichael Chan 
289372b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
289472b34f04SMichael Chan 		return -EOPNOTSUPP;
289572b34f04SMichael Chan 
289672b34f04SMichael Chan 	*edata = bp->eee;
289772b34f04SMichael Chan 	if (!bp->eee.eee_enabled) {
289872b34f04SMichael Chan 		/* Preserve tx_lpi_timer so that the last value will be used
289972b34f04SMichael Chan 		 * by default when it is re-enabled.
290072b34f04SMichael Chan 		 */
290172b34f04SMichael Chan 		edata->advertised = 0;
290272b34f04SMichael Chan 		edata->tx_lpi_enabled = 0;
290372b34f04SMichael Chan 	}
290472b34f04SMichael Chan 
290572b34f04SMichael Chan 	if (!bp->eee.eee_active)
290672b34f04SMichael Chan 		edata->lp_advertised = 0;
290772b34f04SMichael Chan 
290872b34f04SMichael Chan 	return 0;
290972b34f04SMichael Chan }
291072b34f04SMichael Chan 
291142ee18feSAjit Khaparde static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
291242ee18feSAjit Khaparde 					    u16 page_number, u16 start_addr,
291342ee18feSAjit Khaparde 					    u16 data_length, u8 *buf)
291442ee18feSAjit Khaparde {
291542ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_input req = {0};
291642ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
291742ee18feSAjit Khaparde 	int rc, byte_offset = 0;
291842ee18feSAjit Khaparde 
291942ee18feSAjit Khaparde 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
292042ee18feSAjit Khaparde 	req.i2c_slave_addr = i2c_addr;
292142ee18feSAjit Khaparde 	req.page_number = cpu_to_le16(page_number);
292242ee18feSAjit Khaparde 	req.port_id = cpu_to_le16(bp->pf.port_id);
292342ee18feSAjit Khaparde 	do {
292442ee18feSAjit Khaparde 		u16 xfer_size;
292542ee18feSAjit Khaparde 
292642ee18feSAjit Khaparde 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
292742ee18feSAjit Khaparde 		data_length -= xfer_size;
292842ee18feSAjit Khaparde 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
292942ee18feSAjit Khaparde 		req.data_length = xfer_size;
293042ee18feSAjit Khaparde 		req.enables = cpu_to_le32(start_addr + byte_offset ?
293142ee18feSAjit Khaparde 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
293242ee18feSAjit Khaparde 		mutex_lock(&bp->hwrm_cmd_lock);
293342ee18feSAjit Khaparde 		rc = _hwrm_send_message(bp, &req, sizeof(req),
293442ee18feSAjit Khaparde 					HWRM_CMD_TIMEOUT);
293542ee18feSAjit Khaparde 		if (!rc)
293642ee18feSAjit Khaparde 			memcpy(buf + byte_offset, output->data, xfer_size);
293742ee18feSAjit Khaparde 		mutex_unlock(&bp->hwrm_cmd_lock);
293842ee18feSAjit Khaparde 		byte_offset += xfer_size;
293942ee18feSAjit Khaparde 	} while (!rc && data_length > 0);
294042ee18feSAjit Khaparde 
294142ee18feSAjit Khaparde 	return rc;
294242ee18feSAjit Khaparde }
294342ee18feSAjit Khaparde 
294442ee18feSAjit Khaparde static int bnxt_get_module_info(struct net_device *dev,
294542ee18feSAjit Khaparde 				struct ethtool_modinfo *modinfo)
294642ee18feSAjit Khaparde {
29477328a23cSVasundhara Volam 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
294842ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
294942ee18feSAjit Khaparde 	int rc;
295042ee18feSAjit Khaparde 
295142ee18feSAjit Khaparde 	/* No point in going further if phy status indicates
295242ee18feSAjit Khaparde 	 * module is not inserted or if it is powered down or
295342ee18feSAjit Khaparde 	 * if it is of type 10GBase-T
295442ee18feSAjit Khaparde 	 */
295542ee18feSAjit Khaparde 	if (bp->link_info.module_status >
295642ee18feSAjit Khaparde 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
295742ee18feSAjit Khaparde 		return -EOPNOTSUPP;
295842ee18feSAjit Khaparde 
295942ee18feSAjit Khaparde 	/* This feature is not supported in older firmware versions */
296042ee18feSAjit Khaparde 	if (bp->hwrm_spec_code < 0x10202)
296142ee18feSAjit Khaparde 		return -EOPNOTSUPP;
296242ee18feSAjit Khaparde 
29637328a23cSVasundhara Volam 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
29647328a23cSVasundhara Volam 					      SFF_DIAG_SUPPORT_OFFSET + 1,
29657328a23cSVasundhara Volam 					      data);
296642ee18feSAjit Khaparde 	if (!rc) {
29677328a23cSVasundhara Volam 		u8 module_id = data[0];
29687328a23cSVasundhara Volam 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
296942ee18feSAjit Khaparde 
297042ee18feSAjit Khaparde 		switch (module_id) {
297142ee18feSAjit Khaparde 		case SFF_MODULE_ID_SFP:
297242ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8472;
297342ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
29747328a23cSVasundhara Volam 			if (!diag_supported)
29757328a23cSVasundhara Volam 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
297642ee18feSAjit Khaparde 			break;
297742ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP:
297842ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP_PLUS:
297942ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8436;
298042ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
298142ee18feSAjit Khaparde 			break;
298242ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP28:
298342ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8636;
298442ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
298542ee18feSAjit Khaparde 			break;
298642ee18feSAjit Khaparde 		default:
298742ee18feSAjit Khaparde 			rc = -EOPNOTSUPP;
298842ee18feSAjit Khaparde 			break;
298942ee18feSAjit Khaparde 		}
299042ee18feSAjit Khaparde 	}
299142ee18feSAjit Khaparde 	return rc;
299242ee18feSAjit Khaparde }
299342ee18feSAjit Khaparde 
299442ee18feSAjit Khaparde static int bnxt_get_module_eeprom(struct net_device *dev,
299542ee18feSAjit Khaparde 				  struct ethtool_eeprom *eeprom,
299642ee18feSAjit Khaparde 				  u8 *data)
299742ee18feSAjit Khaparde {
299842ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
299942ee18feSAjit Khaparde 	u16  start = eeprom->offset, length = eeprom->len;
3000f3ea3119SColin Ian King 	int rc = 0;
300142ee18feSAjit Khaparde 
300242ee18feSAjit Khaparde 	memset(data, 0, eeprom->len);
300342ee18feSAjit Khaparde 
300442ee18feSAjit Khaparde 	/* Read A0 portion of the EEPROM */
300542ee18feSAjit Khaparde 	if (start < ETH_MODULE_SFF_8436_LEN) {
300642ee18feSAjit Khaparde 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
300742ee18feSAjit Khaparde 			length = ETH_MODULE_SFF_8436_LEN - start;
300842ee18feSAjit Khaparde 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
300942ee18feSAjit Khaparde 						      start, length, data);
301042ee18feSAjit Khaparde 		if (rc)
301142ee18feSAjit Khaparde 			return rc;
301242ee18feSAjit Khaparde 		start += length;
301342ee18feSAjit Khaparde 		data += length;
301442ee18feSAjit Khaparde 		length = eeprom->len - length;
301542ee18feSAjit Khaparde 	}
301642ee18feSAjit Khaparde 
301742ee18feSAjit Khaparde 	/* Read A2 portion of the EEPROM */
301842ee18feSAjit Khaparde 	if (length) {
301942ee18feSAjit Khaparde 		start -= ETH_MODULE_SFF_8436_LEN;
30204260330bSEdwin Peer 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 0,
3021dea521a2SChristophe JAILLET 						      start, length, data);
302242ee18feSAjit Khaparde 	}
302342ee18feSAjit Khaparde 	return rc;
302442ee18feSAjit Khaparde }
302542ee18feSAjit Khaparde 
3026ae8e98a6SDeepak Khungar static int bnxt_nway_reset(struct net_device *dev)
3027ae8e98a6SDeepak Khungar {
3028ae8e98a6SDeepak Khungar 	int rc = 0;
3029ae8e98a6SDeepak Khungar 
3030ae8e98a6SDeepak Khungar 	struct bnxt *bp = netdev_priv(dev);
3031ae8e98a6SDeepak Khungar 	struct bnxt_link_info *link_info = &bp->link_info;
3032ae8e98a6SDeepak Khungar 
3033c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
3034ae8e98a6SDeepak Khungar 		return -EOPNOTSUPP;
3035ae8e98a6SDeepak Khungar 
3036ae8e98a6SDeepak Khungar 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
3037ae8e98a6SDeepak Khungar 		return -EINVAL;
3038ae8e98a6SDeepak Khungar 
3039ae8e98a6SDeepak Khungar 	if (netif_running(dev))
3040ae8e98a6SDeepak Khungar 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
3041ae8e98a6SDeepak Khungar 
3042ae8e98a6SDeepak Khungar 	return rc;
3043ae8e98a6SDeepak Khungar }
3044ae8e98a6SDeepak Khungar 
30455ad2cbeeSMichael Chan static int bnxt_set_phys_id(struct net_device *dev,
30465ad2cbeeSMichael Chan 			    enum ethtool_phys_id_state state)
30475ad2cbeeSMichael Chan {
30485ad2cbeeSMichael Chan 	struct hwrm_port_led_cfg_input req = {0};
30495ad2cbeeSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
30505ad2cbeeSMichael Chan 	struct bnxt_pf_info *pf = &bp->pf;
30515ad2cbeeSMichael Chan 	struct bnxt_led_cfg *led_cfg;
30525ad2cbeeSMichael Chan 	u8 led_state;
30535ad2cbeeSMichael Chan 	__le16 duration;
30549f90445cSVasundhara Volam 	int i;
30555ad2cbeeSMichael Chan 
30565ad2cbeeSMichael Chan 	if (!bp->num_leds || BNXT_VF(bp))
30575ad2cbeeSMichael Chan 		return -EOPNOTSUPP;
30585ad2cbeeSMichael Chan 
30595ad2cbeeSMichael Chan 	if (state == ETHTOOL_ID_ACTIVE) {
30605ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
30615ad2cbeeSMichael Chan 		duration = cpu_to_le16(500);
30625ad2cbeeSMichael Chan 	} else if (state == ETHTOOL_ID_INACTIVE) {
30635ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
30645ad2cbeeSMichael Chan 		duration = cpu_to_le16(0);
30655ad2cbeeSMichael Chan 	} else {
30665ad2cbeeSMichael Chan 		return -EINVAL;
30675ad2cbeeSMichael Chan 	}
30685ad2cbeeSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
30695ad2cbeeSMichael Chan 	req.port_id = cpu_to_le16(pf->port_id);
30705ad2cbeeSMichael Chan 	req.num_leds = bp->num_leds;
30715ad2cbeeSMichael Chan 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
30725ad2cbeeSMichael Chan 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
30735ad2cbeeSMichael Chan 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
30745ad2cbeeSMichael Chan 		led_cfg->led_id = bp->leds[i].led_id;
30755ad2cbeeSMichael Chan 		led_cfg->led_state = led_state;
30765ad2cbeeSMichael Chan 		led_cfg->led_blink_on = duration;
30775ad2cbeeSMichael Chan 		led_cfg->led_blink_off = duration;
30785ad2cbeeSMichael Chan 		led_cfg->led_group_id = bp->leds[i].led_group_id;
30795ad2cbeeSMichael Chan 	}
30809f90445cSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
30815ad2cbeeSMichael Chan }
30825ad2cbeeSMichael Chan 
308367fea463SMichael Chan static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
308467fea463SMichael Chan {
308567fea463SMichael Chan 	struct hwrm_selftest_irq_input req = {0};
308667fea463SMichael Chan 
308767fea463SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
308867fea463SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
308967fea463SMichael Chan }
309067fea463SMichael Chan 
309167fea463SMichael Chan static int bnxt_test_irq(struct bnxt *bp)
309267fea463SMichael Chan {
309367fea463SMichael Chan 	int i;
309467fea463SMichael Chan 
309567fea463SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
309667fea463SMichael Chan 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
309767fea463SMichael Chan 		int rc;
309867fea463SMichael Chan 
309967fea463SMichael Chan 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
310067fea463SMichael Chan 		if (rc)
310167fea463SMichael Chan 			return rc;
310267fea463SMichael Chan 	}
310367fea463SMichael Chan 	return 0;
310467fea463SMichael Chan }
310567fea463SMichael Chan 
3106f7dc1ea6SMichael Chan static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
3107f7dc1ea6SMichael Chan {
3108f7dc1ea6SMichael Chan 	struct hwrm_port_mac_cfg_input req = {0};
3109f7dc1ea6SMichael Chan 
3110f7dc1ea6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
3111f7dc1ea6SMichael Chan 
3112f7dc1ea6SMichael Chan 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
3113f7dc1ea6SMichael Chan 	if (enable)
3114f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
3115f7dc1ea6SMichael Chan 	else
3116f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
3117f7dc1ea6SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3118f7dc1ea6SMichael Chan }
3119f7dc1ea6SMichael Chan 
312056d37462SVasundhara Volam static int bnxt_query_force_speeds(struct bnxt *bp, u16 *force_speeds)
312156d37462SVasundhara Volam {
312256d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
312356d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_input req = {0};
312456d37462SVasundhara Volam 	int rc;
312556d37462SVasundhara Volam 
312656d37462SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
312756d37462SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
312856d37462SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
312956d37462SVasundhara Volam 	if (!rc)
313056d37462SVasundhara Volam 		*force_speeds = le16_to_cpu(resp->supported_speeds_force_mode);
313156d37462SVasundhara Volam 
313256d37462SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
313356d37462SVasundhara Volam 	return rc;
313456d37462SVasundhara Volam }
313556d37462SVasundhara Volam 
313691725d89SMichael Chan static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
313791725d89SMichael Chan 				    struct hwrm_port_phy_cfg_input *req)
313891725d89SMichael Chan {
313991725d89SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
314056d37462SVasundhara Volam 	u16 fw_advertising;
314191725d89SMichael Chan 	u16 fw_speed;
314291725d89SMichael Chan 	int rc;
314391725d89SMichael Chan 
31448a60efd1SMichael Chan 	if (!link_info->autoneg ||
31458a60efd1SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_AN_PHY_LPBK))
314691725d89SMichael Chan 		return 0;
314791725d89SMichael Chan 
314856d37462SVasundhara Volam 	rc = bnxt_query_force_speeds(bp, &fw_advertising);
314956d37462SVasundhara Volam 	if (rc)
315056d37462SVasundhara Volam 		return rc;
315156d37462SVasundhara Volam 
315291725d89SMichael Chan 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
315383d8f5e9SMichael Chan 	if (bp->link_info.link_up)
315491725d89SMichael Chan 		fw_speed = bp->link_info.link_speed;
315591725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
315691725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
315791725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
315891725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
315991725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
316091725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
316191725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
316291725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
316391725d89SMichael Chan 
316491725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(fw_speed);
316591725d89SMichael Chan 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
316691725d89SMichael Chan 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
316791725d89SMichael Chan 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
316891725d89SMichael Chan 	req->flags = 0;
316991725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(0);
317091725d89SMichael Chan 	return rc;
317191725d89SMichael Chan }
317291725d89SMichael Chan 
317355fd0cf3SMichael Chan static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
317491725d89SMichael Chan {
317591725d89SMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
317691725d89SMichael Chan 
317791725d89SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
317891725d89SMichael Chan 
317991725d89SMichael Chan 	if (enable) {
318091725d89SMichael Chan 		bnxt_disable_an_for_lpbk(bp, &req);
318155fd0cf3SMichael Chan 		if (ext)
318255fd0cf3SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
318355fd0cf3SMichael Chan 		else
318491725d89SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
318591725d89SMichael Chan 	} else {
318691725d89SMichael Chan 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
318791725d89SMichael Chan 	}
318891725d89SMichael Chan 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
318991725d89SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
319091725d89SMichael Chan }
319191725d89SMichael Chan 
3192e44758b7SMichael Chan static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3193f7dc1ea6SMichael Chan 			    u32 raw_cons, int pkt_size)
3194f7dc1ea6SMichael Chan {
3195e44758b7SMichael Chan 	struct bnxt_napi *bnapi = cpr->bnapi;
3196e44758b7SMichael Chan 	struct bnxt_rx_ring_info *rxr;
3197f7dc1ea6SMichael Chan 	struct bnxt_sw_rx_bd *rx_buf;
3198f7dc1ea6SMichael Chan 	struct rx_cmp *rxcmp;
3199f7dc1ea6SMichael Chan 	u16 cp_cons, cons;
3200f7dc1ea6SMichael Chan 	u8 *data;
3201f7dc1ea6SMichael Chan 	u32 len;
3202f7dc1ea6SMichael Chan 	int i;
3203f7dc1ea6SMichael Chan 
3204e44758b7SMichael Chan 	rxr = bnapi->rx_ring;
3205f7dc1ea6SMichael Chan 	cp_cons = RING_CMP(raw_cons);
3206f7dc1ea6SMichael Chan 	rxcmp = (struct rx_cmp *)
3207f7dc1ea6SMichael Chan 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3208f7dc1ea6SMichael Chan 	cons = rxcmp->rx_cmp_opaque;
3209f7dc1ea6SMichael Chan 	rx_buf = &rxr->rx_buf_ring[cons];
3210f7dc1ea6SMichael Chan 	data = rx_buf->data_ptr;
3211f7dc1ea6SMichael Chan 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
3212f7dc1ea6SMichael Chan 	if (len != pkt_size)
3213f7dc1ea6SMichael Chan 		return -EIO;
3214f7dc1ea6SMichael Chan 	i = ETH_ALEN;
3215f7dc1ea6SMichael Chan 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
3216f7dc1ea6SMichael Chan 		return -EIO;
3217f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3218f7dc1ea6SMichael Chan 	for (  ; i < pkt_size; i++) {
3219f7dc1ea6SMichael Chan 		if (data[i] != (u8)(i & 0xff))
3220f7dc1ea6SMichael Chan 			return -EIO;
3221f7dc1ea6SMichael Chan 	}
3222f7dc1ea6SMichael Chan 	return 0;
3223f7dc1ea6SMichael Chan }
3224f7dc1ea6SMichael Chan 
3225e44758b7SMichael Chan static int bnxt_poll_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3226e44758b7SMichael Chan 			      int pkt_size)
3227f7dc1ea6SMichael Chan {
3228f7dc1ea6SMichael Chan 	struct tx_cmp *txcmp;
3229f7dc1ea6SMichael Chan 	int rc = -EIO;
3230f7dc1ea6SMichael Chan 	u32 raw_cons;
3231f7dc1ea6SMichael Chan 	u32 cons;
3232f7dc1ea6SMichael Chan 	int i;
3233f7dc1ea6SMichael Chan 
3234f7dc1ea6SMichael Chan 	raw_cons = cpr->cp_raw_cons;
3235f7dc1ea6SMichael Chan 	for (i = 0; i < 200; i++) {
3236f7dc1ea6SMichael Chan 		cons = RING_CMP(raw_cons);
3237f7dc1ea6SMichael Chan 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3238f7dc1ea6SMichael Chan 
3239f7dc1ea6SMichael Chan 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
3240f7dc1ea6SMichael Chan 			udelay(5);
3241f7dc1ea6SMichael Chan 			continue;
3242f7dc1ea6SMichael Chan 		}
3243f7dc1ea6SMichael Chan 
3244f7dc1ea6SMichael Chan 		/* The valid test of the entry must be done first before
3245f7dc1ea6SMichael Chan 		 * reading any further.
3246f7dc1ea6SMichael Chan 		 */
3247f7dc1ea6SMichael Chan 		dma_rmb();
3248f7dc1ea6SMichael Chan 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
3249e44758b7SMichael Chan 			rc = bnxt_rx_loopback(bp, cpr, raw_cons, pkt_size);
3250f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
3251f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
3252f7dc1ea6SMichael Chan 			break;
3253f7dc1ea6SMichael Chan 		}
3254f7dc1ea6SMichael Chan 		raw_cons = NEXT_RAW_CMP(raw_cons);
3255f7dc1ea6SMichael Chan 	}
3256f7dc1ea6SMichael Chan 	cpr->cp_raw_cons = raw_cons;
3257f7dc1ea6SMichael Chan 	return rc;
3258f7dc1ea6SMichael Chan }
3259f7dc1ea6SMichael Chan 
3260f7dc1ea6SMichael Chan static int bnxt_run_loopback(struct bnxt *bp)
3261f7dc1ea6SMichael Chan {
3262f7dc1ea6SMichael Chan 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
326384404d5fSMichael Chan 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
3264e44758b7SMichael Chan 	struct bnxt_cp_ring_info *cpr;
3265f7dc1ea6SMichael Chan 	int pkt_size, i = 0;
3266f7dc1ea6SMichael Chan 	struct sk_buff *skb;
3267f7dc1ea6SMichael Chan 	dma_addr_t map;
3268f7dc1ea6SMichael Chan 	u8 *data;
3269f7dc1ea6SMichael Chan 	int rc;
3270f7dc1ea6SMichael Chan 
327184404d5fSMichael Chan 	cpr = &rxr->bnapi->cp_ring;
327284404d5fSMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
327384404d5fSMichael Chan 		cpr = cpr->cp_ring_arr[BNXT_RX_HDL];
3274f7dc1ea6SMichael Chan 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
3275f7dc1ea6SMichael Chan 	skb = netdev_alloc_skb(bp->dev, pkt_size);
3276f7dc1ea6SMichael Chan 	if (!skb)
3277f7dc1ea6SMichael Chan 		return -ENOMEM;
3278f7dc1ea6SMichael Chan 	data = skb_put(skb, pkt_size);
3279f7dc1ea6SMichael Chan 	eth_broadcast_addr(data);
3280f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3281f7dc1ea6SMichael Chan 	ether_addr_copy(&data[i], bp->dev->dev_addr);
3282f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3283f7dc1ea6SMichael Chan 	for ( ; i < pkt_size; i++)
3284f7dc1ea6SMichael Chan 		data[i] = (u8)(i & 0xff);
3285f7dc1ea6SMichael Chan 
3286f7dc1ea6SMichael Chan 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
3287f7dc1ea6SMichael Chan 			     PCI_DMA_TODEVICE);
3288f7dc1ea6SMichael Chan 	if (dma_mapping_error(&bp->pdev->dev, map)) {
3289f7dc1ea6SMichael Chan 		dev_kfree_skb(skb);
3290f7dc1ea6SMichael Chan 		return -EIO;
3291f7dc1ea6SMichael Chan 	}
3292c1ba92a8SMichael Chan 	bnxt_xmit_bd(bp, txr, map, pkt_size);
3293f7dc1ea6SMichael Chan 
3294f7dc1ea6SMichael Chan 	/* Sync BD data before updating doorbell */
3295f7dc1ea6SMichael Chan 	wmb();
3296f7dc1ea6SMichael Chan 
3297697197e5SMichael Chan 	bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
3298e44758b7SMichael Chan 	rc = bnxt_poll_loopback(bp, cpr, pkt_size);
3299f7dc1ea6SMichael Chan 
3300f7dc1ea6SMichael Chan 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
3301f7dc1ea6SMichael Chan 	dev_kfree_skb(skb);
3302f7dc1ea6SMichael Chan 	return rc;
3303f7dc1ea6SMichael Chan }
3304f7dc1ea6SMichael Chan 
3305eb513658SMichael Chan static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
3306eb513658SMichael Chan {
3307eb513658SMichael Chan 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
3308eb513658SMichael Chan 	struct hwrm_selftest_exec_input req = {0};
3309eb513658SMichael Chan 	int rc;
3310eb513658SMichael Chan 
3311eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
3312eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3313eb513658SMichael Chan 	resp->test_success = 0;
3314eb513658SMichael Chan 	req.flags = test_mask;
3315eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
3316eb513658SMichael Chan 	*test_results = resp->test_success;
3317eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3318eb513658SMichael Chan 	return rc;
3319eb513658SMichael Chan }
3320eb513658SMichael Chan 
332155fd0cf3SMichael Chan #define BNXT_DRV_TESTS			4
3322f7dc1ea6SMichael Chan #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
332391725d89SMichael Chan #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
332455fd0cf3SMichael Chan #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
332555fd0cf3SMichael Chan #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
3326eb513658SMichael Chan 
3327eb513658SMichael Chan static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
3328eb513658SMichael Chan 			   u64 *buf)
3329eb513658SMichael Chan {
3330eb513658SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
333155fd0cf3SMichael Chan 	bool do_ext_lpbk = false;
3332eb513658SMichael Chan 	bool offline = false;
3333eb513658SMichael Chan 	u8 test_results = 0;
3334eb513658SMichael Chan 	u8 test_mask = 0;
3335d27e2ca1SMichael Chan 	int rc = 0, i;
3336eb513658SMichael Chan 
33376896cb35SVasundhara Volam 	if (!bp->num_tests || !BNXT_PF(bp))
3338eb513658SMichael Chan 		return;
3339eb513658SMichael Chan 	memset(buf, 0, sizeof(u64) * bp->num_tests);
3340eb513658SMichael Chan 	if (!netif_running(dev)) {
3341eb513658SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
3342eb513658SMichael Chan 		return;
3343eb513658SMichael Chan 	}
3344eb513658SMichael Chan 
334555fd0cf3SMichael Chan 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
334655fd0cf3SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_EXT_LPBK))
334755fd0cf3SMichael Chan 		do_ext_lpbk = true;
334855fd0cf3SMichael Chan 
3349eb513658SMichael Chan 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
33506896cb35SVasundhara Volam 		if (bp->pf.active_vfs || !BNXT_SINGLE_PF(bp)) {
3351eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
33526896cb35SVasundhara Volam 			netdev_warn(dev, "Offline tests cannot be run with active VFs or on shared PF\n");
3353eb513658SMichael Chan 			return;
3354eb513658SMichael Chan 		}
3355eb513658SMichael Chan 		offline = true;
3356eb513658SMichael Chan 	}
3357eb513658SMichael Chan 
3358eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3359eb513658SMichael Chan 		u8 bit_val = 1 << i;
3360eb513658SMichael Chan 
3361eb513658SMichael Chan 		if (!(bp->test_info->offline_mask & bit_val))
3362eb513658SMichael Chan 			test_mask |= bit_val;
3363eb513658SMichael Chan 		else if (offline)
3364eb513658SMichael Chan 			test_mask |= bit_val;
3365eb513658SMichael Chan 	}
3366eb513658SMichael Chan 	if (!offline) {
3367eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3368eb513658SMichael Chan 	} else {
3369eb513658SMichael Chan 		rc = bnxt_close_nic(bp, false, false);
3370eb513658SMichael Chan 		if (rc)
3371eb513658SMichael Chan 			return;
3372eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3373f7dc1ea6SMichael Chan 
3374f7dc1ea6SMichael Chan 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
3375f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, true);
3376f7dc1ea6SMichael Chan 		msleep(250);
3377f7dc1ea6SMichael Chan 		rc = bnxt_half_open_nic(bp);
3378f7dc1ea6SMichael Chan 		if (rc) {
3379f7dc1ea6SMichael Chan 			bnxt_hwrm_mac_loopback(bp, false);
3380f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3381f7dc1ea6SMichael Chan 			return;
3382f7dc1ea6SMichael Chan 		}
3383f7dc1ea6SMichael Chan 		if (bnxt_run_loopback(bp))
3384f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3385f7dc1ea6SMichael Chan 		else
3386f7dc1ea6SMichael Chan 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
3387f7dc1ea6SMichael Chan 
3388f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, false);
338955fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, true, false);
339091725d89SMichael Chan 		msleep(1000);
339191725d89SMichael Chan 		if (bnxt_run_loopback(bp)) {
339291725d89SMichael Chan 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
339391725d89SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
339491725d89SMichael Chan 		}
339555fd0cf3SMichael Chan 		if (do_ext_lpbk) {
339655fd0cf3SMichael Chan 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
339755fd0cf3SMichael Chan 			bnxt_hwrm_phy_loopback(bp, true, true);
339855fd0cf3SMichael Chan 			msleep(1000);
339955fd0cf3SMichael Chan 			if (bnxt_run_loopback(bp)) {
340055fd0cf3SMichael Chan 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
340155fd0cf3SMichael Chan 				etest->flags |= ETH_TEST_FL_FAILED;
340255fd0cf3SMichael Chan 			}
340355fd0cf3SMichael Chan 		}
340455fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, false, false);
340591725d89SMichael Chan 		bnxt_half_close_nic(bp);
3406d27e2ca1SMichael Chan 		rc = bnxt_open_nic(bp, false, true);
3407eb513658SMichael Chan 	}
3408d27e2ca1SMichael Chan 	if (rc || bnxt_test_irq(bp)) {
340967fea463SMichael Chan 		buf[BNXT_IRQ_TEST_IDX] = 1;
341067fea463SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
341167fea463SMichael Chan 	}
3412eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3413eb513658SMichael Chan 		u8 bit_val = 1 << i;
3414eb513658SMichael Chan 
3415eb513658SMichael Chan 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
3416eb513658SMichael Chan 			buf[i] = 1;
3417eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3418eb513658SMichael Chan 		}
3419eb513658SMichael Chan 	}
3420eb513658SMichael Chan }
3421eb513658SMichael Chan 
342249f7972fSVasundhara Volam static int bnxt_reset(struct net_device *dev, u32 *flags)
342349f7972fSVasundhara Volam {
342449f7972fSVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
34258cec0940SEdwin Peer 	bool reload = false;
34267a13240eSEdwin Peer 	u32 req = *flags;
34277a13240eSEdwin Peer 
34287a13240eSEdwin Peer 	if (!req)
34297a13240eSEdwin Peer 		return -EINVAL;
343049f7972fSVasundhara Volam 
343149f7972fSVasundhara Volam 	if (!BNXT_PF(bp)) {
343249f7972fSVasundhara Volam 		netdev_err(dev, "Reset is not supported from a VF\n");
343349f7972fSVasundhara Volam 		return -EOPNOTSUPP;
343449f7972fSVasundhara Volam 	}
343549f7972fSVasundhara Volam 
34360a3f4e4fSVasundhara Volam 	if (pci_vfs_assigned(bp->pdev) &&
34370a3f4e4fSVasundhara Volam 	    !(bp->fw_cap & BNXT_FW_CAP_HOT_RESET)) {
343849f7972fSVasundhara Volam 		netdev_err(dev,
343949f7972fSVasundhara Volam 			   "Reset not allowed when VFs are assigned to VMs\n");
344049f7972fSVasundhara Volam 		return -EBUSY;
344149f7972fSVasundhara Volam 	}
344249f7972fSVasundhara Volam 
34437a13240eSEdwin Peer 	if ((req & BNXT_FW_RESET_CHIP) == BNXT_FW_RESET_CHIP) {
344449f7972fSVasundhara Volam 		/* This feature is not supported in older firmware versions */
34457a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
34467a13240eSEdwin Peer 			if (!bnxt_firmware_reset_chip(dev)) {
34477a13240eSEdwin Peer 				netdev_info(dev, "Firmware reset request successful.\n");
34480a3f4e4fSVasundhara Volam 				if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET))
34498cec0940SEdwin Peer 					reload = true;
34507a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_CHIP;
34512373d8d6SScott Branden 			}
34527a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_CHIP) {
34537a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
34547a13240eSEdwin Peer 		}
34557a13240eSEdwin Peer 	}
34567a13240eSEdwin Peer 
34577a13240eSEdwin Peer 	if (req & BNXT_FW_RESET_AP) {
34586502ad59SScott Branden 		/* This feature is not supported in older firmware versions */
34597a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
34607a13240eSEdwin Peer 			if (!bnxt_firmware_reset_ap(dev)) {
34617a13240eSEdwin Peer 				netdev_info(dev, "Reset application processor successful.\n");
34628cec0940SEdwin Peer 				reload = true;
34637a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_AP;
34642373d8d6SScott Branden 			}
34657a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_AP) {
34667a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
34677a13240eSEdwin Peer 		}
346849f7972fSVasundhara Volam 	}
346949f7972fSVasundhara Volam 
34708cec0940SEdwin Peer 	if (reload)
34718cec0940SEdwin Peer 		netdev_info(dev, "Reload driver to complete reset\n");
34728cec0940SEdwin Peer 
34737a13240eSEdwin Peer 	return 0;
347449f7972fSVasundhara Volam }
347549f7972fSVasundhara Volam 
34766c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
34776c5657d0SVasundhara Volam 				  struct bnxt_hwrm_dbg_dma_info *info)
34786c5657d0SVasundhara Volam {
34796c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
34806c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_input *cmn_req = msg;
34816c5657d0SVasundhara Volam 	__le16 *seq_ptr = msg + info->seq_off;
34826c5657d0SVasundhara Volam 	u16 seq = 0, len, segs_off;
34836c5657d0SVasundhara Volam 	void *resp = cmn_resp;
34846c5657d0SVasundhara Volam 	dma_addr_t dma_handle;
34856c5657d0SVasundhara Volam 	int rc, off = 0;
34866c5657d0SVasundhara Volam 	void *dma_buf;
34876c5657d0SVasundhara Volam 
34886c5657d0SVasundhara Volam 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
34896c5657d0SVasundhara Volam 				     GFP_KERNEL);
34906c5657d0SVasundhara Volam 	if (!dma_buf)
34916c5657d0SVasundhara Volam 		return -ENOMEM;
34926c5657d0SVasundhara Volam 
34936c5657d0SVasundhara Volam 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
34946c5657d0SVasundhara Volam 			    total_segments);
34956c5657d0SVasundhara Volam 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
34966c5657d0SVasundhara Volam 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
34976c5657d0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
34986c5657d0SVasundhara Volam 	while (1) {
34996c5657d0SVasundhara Volam 		*seq_ptr = cpu_to_le16(seq);
35005b306bdeSVasundhara Volam 		rc = _hwrm_send_message(bp, msg, msg_len,
35015b306bdeSVasundhara Volam 					HWRM_COREDUMP_TIMEOUT);
35026c5657d0SVasundhara Volam 		if (rc)
35036c5657d0SVasundhara Volam 			break;
35046c5657d0SVasundhara Volam 
35056c5657d0SVasundhara Volam 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
35066c5657d0SVasundhara Volam 		if (!seq &&
35076c5657d0SVasundhara Volam 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
35086c5657d0SVasundhara Volam 			info->segs = le16_to_cpu(*((__le16 *)(resp +
35096c5657d0SVasundhara Volam 							      segs_off)));
35106c5657d0SVasundhara Volam 			if (!info->segs) {
35116c5657d0SVasundhara Volam 				rc = -EIO;
35126c5657d0SVasundhara Volam 				break;
35136c5657d0SVasundhara Volam 			}
35146c5657d0SVasundhara Volam 
35156c5657d0SVasundhara Volam 			info->dest_buf_size = info->segs *
35166c5657d0SVasundhara Volam 					sizeof(struct coredump_segment_record);
35176c5657d0SVasundhara Volam 			info->dest_buf = kmalloc(info->dest_buf_size,
35186c5657d0SVasundhara Volam 						 GFP_KERNEL);
35196c5657d0SVasundhara Volam 			if (!info->dest_buf) {
35206c5657d0SVasundhara Volam 				rc = -ENOMEM;
35216c5657d0SVasundhara Volam 				break;
35226c5657d0SVasundhara Volam 			}
35236c5657d0SVasundhara Volam 		}
35246c5657d0SVasundhara Volam 
3525c74751f4SVasundhara Volam 		if (info->dest_buf) {
3526c74751f4SVasundhara Volam 			if ((info->seg_start + off + len) <=
3527c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
35286c5657d0SVasundhara Volam 				memcpy(info->dest_buf + off, dma_buf, len);
3529c74751f4SVasundhara Volam 			} else {
3530c74751f4SVasundhara Volam 				rc = -ENOBUFS;
3531c74751f4SVasundhara Volam 				break;
3532c74751f4SVasundhara Volam 			}
3533c74751f4SVasundhara Volam 		}
35346c5657d0SVasundhara Volam 
35356c5657d0SVasundhara Volam 		if (cmn_req->req_type ==
35366c5657d0SVasundhara Volam 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
35376c5657d0SVasundhara Volam 			info->dest_buf_size += len;
35386c5657d0SVasundhara Volam 
35396c5657d0SVasundhara Volam 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
35406c5657d0SVasundhara Volam 			break;
35416c5657d0SVasundhara Volam 
35426c5657d0SVasundhara Volam 		seq++;
35436c5657d0SVasundhara Volam 		off += len;
35446c5657d0SVasundhara Volam 	}
35456c5657d0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
35466c5657d0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
35476c5657d0SVasundhara Volam 	return rc;
35486c5657d0SVasundhara Volam }
35496c5657d0SVasundhara Volam 
35506c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
35516c5657d0SVasundhara Volam 				       struct bnxt_coredump *coredump)
35526c5657d0SVasundhara Volam {
35536c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_list_input req = {0};
35546c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
35556c5657d0SVasundhara Volam 	int rc;
35566c5657d0SVasundhara Volam 
35576c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
35586c5657d0SVasundhara Volam 
35596c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_LIST_BUF_LEN;
35606c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
35616c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
35626c5657d0SVasundhara Volam 				     data_len);
35636c5657d0SVasundhara Volam 
35646c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
35656c5657d0SVasundhara Volam 	if (!rc) {
35666c5657d0SVasundhara Volam 		coredump->data = info.dest_buf;
35676c5657d0SVasundhara Volam 		coredump->data_size = info.dest_buf_size;
35686c5657d0SVasundhara Volam 		coredump->total_segs = info.segs;
35696c5657d0SVasundhara Volam 	}
35706c5657d0SVasundhara Volam 	return rc;
35716c5657d0SVasundhara Volam }
35726c5657d0SVasundhara Volam 
35736c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
35746c5657d0SVasundhara Volam 					   u16 segment_id)
35756c5657d0SVasundhara Volam {
35766c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_initiate_input req = {0};
35776c5657d0SVasundhara Volam 
35786c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
35796c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
35806c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
35816c5657d0SVasundhara Volam 
358257a8730bSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_COREDUMP_TIMEOUT);
35836c5657d0SVasundhara Volam }
35846c5657d0SVasundhara Volam 
35856c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
35866c5657d0SVasundhara Volam 					   u16 segment_id, u32 *seg_len,
3587c74751f4SVasundhara Volam 					   void *buf, u32 buf_len, u32 offset)
35886c5657d0SVasundhara Volam {
35896c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_retrieve_input req = {0};
35906c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
35916c5657d0SVasundhara Volam 	int rc;
35926c5657d0SVasundhara Volam 
35936c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
35946c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
35956c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
35966c5657d0SVasundhara Volam 
35976c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
35986c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
35996c5657d0SVasundhara Volam 				seq_no);
36006c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
36016c5657d0SVasundhara Volam 				     data_len);
3602c74751f4SVasundhara Volam 	if (buf) {
36036c5657d0SVasundhara Volam 		info.dest_buf = buf + offset;
3604c74751f4SVasundhara Volam 		info.buf_len = buf_len;
3605c74751f4SVasundhara Volam 		info.seg_start = offset;
3606c74751f4SVasundhara Volam 	}
36076c5657d0SVasundhara Volam 
36086c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
36096c5657d0SVasundhara Volam 	if (!rc)
36106c5657d0SVasundhara Volam 		*seg_len = info.dest_buf_size;
36116c5657d0SVasundhara Volam 
36126c5657d0SVasundhara Volam 	return rc;
36136c5657d0SVasundhara Volam }
36146c5657d0SVasundhara Volam 
36156c5657d0SVasundhara Volam static void
36166c5657d0SVasundhara Volam bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
36176c5657d0SVasundhara Volam 			   struct bnxt_coredump_segment_hdr *seg_hdr,
36186c5657d0SVasundhara Volam 			   struct coredump_segment_record *seg_rec, u32 seg_len,
36196c5657d0SVasundhara Volam 			   int status, u32 duration, u32 instance)
36206c5657d0SVasundhara Volam {
36216c5657d0SVasundhara Volam 	memset(seg_hdr, 0, sizeof(*seg_hdr));
36228605212aSVasundhara Volam 	memcpy(seg_hdr->signature, "sEgM", 4);
36236c5657d0SVasundhara Volam 	if (seg_rec) {
36246c5657d0SVasundhara Volam 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
36256c5657d0SVasundhara Volam 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
36266c5657d0SVasundhara Volam 		seg_hdr->low_version = seg_rec->version_low;
36276c5657d0SVasundhara Volam 		seg_hdr->high_version = seg_rec->version_hi;
36286c5657d0SVasundhara Volam 	} else {
36296c5657d0SVasundhara Volam 		/* For hwrm_ver_get response Component id = 2
36306c5657d0SVasundhara Volam 		 * and Segment id = 0
36316c5657d0SVasundhara Volam 		 */
36326c5657d0SVasundhara Volam 		seg_hdr->component_id = cpu_to_le32(2);
36336c5657d0SVasundhara Volam 		seg_hdr->segment_id = 0;
36346c5657d0SVasundhara Volam 	}
36356c5657d0SVasundhara Volam 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
36366c5657d0SVasundhara Volam 	seg_hdr->length = cpu_to_le32(seg_len);
36376c5657d0SVasundhara Volam 	seg_hdr->status = cpu_to_le32(status);
36386c5657d0SVasundhara Volam 	seg_hdr->duration = cpu_to_le32(duration);
36396c5657d0SVasundhara Volam 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
36406c5657d0SVasundhara Volam 	seg_hdr->instance = cpu_to_le32(instance);
36416c5657d0SVasundhara Volam }
36426c5657d0SVasundhara Volam 
36436c5657d0SVasundhara Volam static void
36446c5657d0SVasundhara Volam bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
36456c5657d0SVasundhara Volam 			  time64_t start, s16 start_utc, u16 total_segs,
36466c5657d0SVasundhara Volam 			  int status)
36476c5657d0SVasundhara Volam {
36486c5657d0SVasundhara Volam 	time64_t end = ktime_get_real_seconds();
36496c5657d0SVasundhara Volam 	u32 os_ver_major = 0, os_ver_minor = 0;
36506c5657d0SVasundhara Volam 	struct tm tm;
36516c5657d0SVasundhara Volam 
36526c5657d0SVasundhara Volam 	time64_to_tm(start, 0, &tm);
36536c5657d0SVasundhara Volam 	memset(record, 0, sizeof(*record));
36548605212aSVasundhara Volam 	memcpy(record->signature, "cOrE", 4);
36556c5657d0SVasundhara Volam 	record->flags = 0;
36566c5657d0SVasundhara Volam 	record->low_version = 0;
36576c5657d0SVasundhara Volam 	record->high_version = 1;
36586c5657d0SVasundhara Volam 	record->asic_state = 0;
36593d46eee5SArnd Bergmann 	strlcpy(record->system_name, utsname()->nodename,
36603d46eee5SArnd Bergmann 		sizeof(record->system_name));
36618dc5ae2dSVasundhara Volam 	record->year = cpu_to_le16(tm.tm_year + 1900);
36628dc5ae2dSVasundhara Volam 	record->month = cpu_to_le16(tm.tm_mon + 1);
36636c5657d0SVasundhara Volam 	record->day = cpu_to_le16(tm.tm_mday);
36646c5657d0SVasundhara Volam 	record->hour = cpu_to_le16(tm.tm_hour);
36656c5657d0SVasundhara Volam 	record->minute = cpu_to_le16(tm.tm_min);
36666c5657d0SVasundhara Volam 	record->second = cpu_to_le16(tm.tm_sec);
36676c5657d0SVasundhara Volam 	record->utc_bias = cpu_to_le16(start_utc);
36686c5657d0SVasundhara Volam 	strcpy(record->commandline, "ethtool -w");
36696c5657d0SVasundhara Volam 	record->total_segments = cpu_to_le32(total_segs);
36706c5657d0SVasundhara Volam 
36716c5657d0SVasundhara Volam 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
36726c5657d0SVasundhara Volam 	record->os_ver_major = cpu_to_le32(os_ver_major);
36736c5657d0SVasundhara Volam 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
36746c5657d0SVasundhara Volam 
36758605212aSVasundhara Volam 	strlcpy(record->os_name, utsname()->sysname, 32);
36766c5657d0SVasundhara Volam 	time64_to_tm(end, 0, &tm);
36776c5657d0SVasundhara Volam 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
36786c5657d0SVasundhara Volam 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
36796c5657d0SVasundhara Volam 	record->end_day = cpu_to_le16(tm.tm_mday);
36806c5657d0SVasundhara Volam 	record->end_hour = cpu_to_le16(tm.tm_hour);
36816c5657d0SVasundhara Volam 	record->end_minute = cpu_to_le16(tm.tm_min);
36826c5657d0SVasundhara Volam 	record->end_second = cpu_to_le16(tm.tm_sec);
36836c5657d0SVasundhara Volam 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
36846c5657d0SVasundhara Volam 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
36856c5657d0SVasundhara Volam 				       bp->ver_resp.chip_rev << 8 |
36866c5657d0SVasundhara Volam 				       bp->ver_resp.chip_metal);
36876c5657d0SVasundhara Volam 	record->asic_id2 = 0;
36886c5657d0SVasundhara Volam 	record->coredump_status = cpu_to_le32(status);
36896c5657d0SVasundhara Volam 	record->ioctl_low_version = 0;
36906c5657d0SVasundhara Volam 	record->ioctl_high_version = 0;
36916c5657d0SVasundhara Volam }
36926c5657d0SVasundhara Volam 
36936c5657d0SVasundhara Volam static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
36946c5657d0SVasundhara Volam {
36956c5657d0SVasundhara Volam 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
3696c74751f4SVasundhara Volam 	u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
36976c5657d0SVasundhara Volam 	struct coredump_segment_record *seg_record = NULL;
36986c5657d0SVasundhara Volam 	struct bnxt_coredump_segment_hdr seg_hdr;
36996c5657d0SVasundhara Volam 	struct bnxt_coredump coredump = {NULL};
37006c5657d0SVasundhara Volam 	time64_t start_time;
37016c5657d0SVasundhara Volam 	u16 start_utc;
37026c5657d0SVasundhara Volam 	int rc = 0, i;
37036c5657d0SVasundhara Volam 
3704c74751f4SVasundhara Volam 	if (buf)
3705c74751f4SVasundhara Volam 		buf_len = *dump_len;
3706c74751f4SVasundhara Volam 
37076c5657d0SVasundhara Volam 	start_time = ktime_get_real_seconds();
37086c5657d0SVasundhara Volam 	start_utc = sys_tz.tz_minuteswest * 60;
37096c5657d0SVasundhara Volam 	seg_hdr_len = sizeof(seg_hdr);
37106c5657d0SVasundhara Volam 
37116c5657d0SVasundhara Volam 	/* First segment should be hwrm_ver_get response */
37126c5657d0SVasundhara Volam 	*dump_len = seg_hdr_len + ver_get_resp_len;
37136c5657d0SVasundhara Volam 	if (buf) {
37146c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
37156c5657d0SVasundhara Volam 					   0, 0, 0);
37166c5657d0SVasundhara Volam 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
37176c5657d0SVasundhara Volam 		offset += seg_hdr_len;
37186c5657d0SVasundhara Volam 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
37196c5657d0SVasundhara Volam 		offset += ver_get_resp_len;
37206c5657d0SVasundhara Volam 	}
37216c5657d0SVasundhara Volam 
37226c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
37236c5657d0SVasundhara Volam 	if (rc) {
37246c5657d0SVasundhara Volam 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
37256c5657d0SVasundhara Volam 		goto err;
37266c5657d0SVasundhara Volam 	}
37276c5657d0SVasundhara Volam 
37286c5657d0SVasundhara Volam 	*dump_len += seg_hdr_len * coredump.total_segs;
37296c5657d0SVasundhara Volam 
37306c5657d0SVasundhara Volam 	seg_record = (struct coredump_segment_record *)coredump.data;
37316c5657d0SVasundhara Volam 	seg_record_len = sizeof(*seg_record);
37326c5657d0SVasundhara Volam 
37336c5657d0SVasundhara Volam 	for (i = 0; i < coredump.total_segs; i++) {
37346c5657d0SVasundhara Volam 		u16 comp_id = le16_to_cpu(seg_record->component_id);
37356c5657d0SVasundhara Volam 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
37366c5657d0SVasundhara Volam 		u32 duration = 0, seg_len = 0;
37376c5657d0SVasundhara Volam 		unsigned long start, end;
37386c5657d0SVasundhara Volam 
3739c74751f4SVasundhara Volam 		if (buf && ((offset + seg_hdr_len) >
3740c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(buf_len))) {
3741c74751f4SVasundhara Volam 			rc = -ENOBUFS;
3742c74751f4SVasundhara Volam 			goto err;
3743c74751f4SVasundhara Volam 		}
3744c74751f4SVasundhara Volam 
37456c5657d0SVasundhara Volam 		start = jiffies;
37466c5657d0SVasundhara Volam 
37476c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
37486c5657d0SVasundhara Volam 		if (rc) {
37496c5657d0SVasundhara Volam 			netdev_err(bp->dev,
37506c5657d0SVasundhara Volam 				   "Failed to initiate coredump for seg = %d\n",
37516c5657d0SVasundhara Volam 				   seg_record->segment_id);
37526c5657d0SVasundhara Volam 			goto next_seg;
37536c5657d0SVasundhara Volam 		}
37546c5657d0SVasundhara Volam 
37556c5657d0SVasundhara Volam 		/* Write segment data into the buffer */
37566c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
3757c74751f4SVasundhara Volam 						     &seg_len, buf, buf_len,
37586c5657d0SVasundhara Volam 						     offset + seg_hdr_len);
3759c74751f4SVasundhara Volam 		if (rc && rc == -ENOBUFS)
3760c74751f4SVasundhara Volam 			goto err;
3761c74751f4SVasundhara Volam 		else if (rc)
37626c5657d0SVasundhara Volam 			netdev_err(bp->dev,
37636c5657d0SVasundhara Volam 				   "Failed to retrieve coredump for seg = %d\n",
37646c5657d0SVasundhara Volam 				   seg_record->segment_id);
37656c5657d0SVasundhara Volam 
37666c5657d0SVasundhara Volam next_seg:
37676c5657d0SVasundhara Volam 		end = jiffies;
37686c5657d0SVasundhara Volam 		duration = jiffies_to_msecs(end - start);
37696c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
37706c5657d0SVasundhara Volam 					   rc, duration, 0);
37716c5657d0SVasundhara Volam 
37726c5657d0SVasundhara Volam 		if (buf) {
37736c5657d0SVasundhara Volam 			/* Write segment header into the buffer */
37746c5657d0SVasundhara Volam 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
37756c5657d0SVasundhara Volam 			offset += seg_hdr_len + seg_len;
37766c5657d0SVasundhara Volam 		}
37776c5657d0SVasundhara Volam 
37786c5657d0SVasundhara Volam 		*dump_len += seg_len;
37796c5657d0SVasundhara Volam 		seg_record =
37806c5657d0SVasundhara Volam 			(struct coredump_segment_record *)((u8 *)seg_record +
37816c5657d0SVasundhara Volam 							   seg_record_len);
37826c5657d0SVasundhara Volam 	}
37836c5657d0SVasundhara Volam 
37846c5657d0SVasundhara Volam err:
37851bbf3aedSArnd Bergmann 	if (buf)
37861bbf3aedSArnd Bergmann 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
37876c5657d0SVasundhara Volam 					  start_utc, coredump.total_segs + 1,
37886c5657d0SVasundhara Volam 					  rc);
37896c5657d0SVasundhara Volam 	kfree(coredump.data);
37901bbf3aedSArnd Bergmann 	*dump_len += sizeof(struct bnxt_coredump_record);
3791c74751f4SVasundhara Volam 	if (rc == -ENOBUFS)
37929a005c38SJonathan Lemon 		netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
37936c5657d0SVasundhara Volam 	return rc;
37946c5657d0SVasundhara Volam }
37956c5657d0SVasundhara Volam 
37960b0eacf3SVasundhara Volam static int bnxt_set_dump(struct net_device *dev, struct ethtool_dump *dump)
37970b0eacf3SVasundhara Volam {
37980b0eacf3SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
37990b0eacf3SVasundhara Volam 
38000b0eacf3SVasundhara Volam 	if (dump->flag > BNXT_DUMP_CRASH) {
38010b0eacf3SVasundhara Volam 		netdev_info(dev, "Supports only Live(0) and Crash(1) dumps.\n");
38020b0eacf3SVasundhara Volam 		return -EINVAL;
38030b0eacf3SVasundhara Volam 	}
38040b0eacf3SVasundhara Volam 
38050b0eacf3SVasundhara Volam 	if (!IS_ENABLED(CONFIG_TEE_BNXT_FW) && dump->flag == BNXT_DUMP_CRASH) {
38060b0eacf3SVasundhara Volam 		netdev_info(dev, "Cannot collect crash dump as TEE_BNXT_FW config option is not enabled.\n");
38070b0eacf3SVasundhara Volam 		return -EOPNOTSUPP;
38080b0eacf3SVasundhara Volam 	}
38090b0eacf3SVasundhara Volam 
38100b0eacf3SVasundhara Volam 	bp->dump_flag = dump->flag;
38110b0eacf3SVasundhara Volam 	return 0;
38120b0eacf3SVasundhara Volam }
38130b0eacf3SVasundhara Volam 
38146c5657d0SVasundhara Volam static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
38156c5657d0SVasundhara Volam {
38166c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
38176c5657d0SVasundhara Volam 
38186c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
38196c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
38206c5657d0SVasundhara Volam 
38216c5657d0SVasundhara Volam 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
38226c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_min_8b << 16 |
38236c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
38246c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_rsvd_8b;
38256c5657d0SVasundhara Volam 
38260b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
38270b0eacf3SVasundhara Volam 	if (bp->dump_flag == BNXT_DUMP_CRASH)
38280b0eacf3SVasundhara Volam 		dump->len = BNXT_CRASH_DUMP_LEN;
38290b0eacf3SVasundhara Volam 	else
38300b0eacf3SVasundhara Volam 		bnxt_get_coredump(bp, NULL, &dump->len);
38310b0eacf3SVasundhara Volam 	return 0;
38326c5657d0SVasundhara Volam }
38336c5657d0SVasundhara Volam 
38346c5657d0SVasundhara Volam static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
38356c5657d0SVasundhara Volam 			      void *buf)
38366c5657d0SVasundhara Volam {
38376c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
38386c5657d0SVasundhara Volam 
38396c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
38406c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
38416c5657d0SVasundhara Volam 
38426c5657d0SVasundhara Volam 	memset(buf, 0, dump->len);
38436c5657d0SVasundhara Volam 
38440b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
38450b0eacf3SVasundhara Volam 	if (dump->flag == BNXT_DUMP_CRASH) {
38460b0eacf3SVasundhara Volam #ifdef CONFIG_TEE_BNXT_FW
38470b0eacf3SVasundhara Volam 		return tee_bnxt_copy_coredump(buf, 0, dump->len);
38480b0eacf3SVasundhara Volam #endif
38490b0eacf3SVasundhara Volam 	} else {
38506c5657d0SVasundhara Volam 		return bnxt_get_coredump(bp, buf, &dump->len);
38516c5657d0SVasundhara Volam 	}
38526c5657d0SVasundhara Volam 
38530b0eacf3SVasundhara Volam 	return 0;
38540b0eacf3SVasundhara Volam }
38550b0eacf3SVasundhara Volam 
3856eb513658SMichael Chan void bnxt_ethtool_init(struct bnxt *bp)
3857eb513658SMichael Chan {
3858eb513658SMichael Chan 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3859eb513658SMichael Chan 	struct hwrm_selftest_qlist_input req = {0};
3860eb513658SMichael Chan 	struct bnxt_test_info *test_info;
3861431aa1ebSMichael Chan 	struct net_device *dev = bp->dev;
3862eb513658SMichael Chan 	int i, rc;
3863eb513658SMichael Chan 
3864691aa620SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PKG_VER))
3865a60faa60SVasundhara Volam 		bnxt_get_pkgver(dev);
3866431aa1ebSMichael Chan 
3867ba642ab7SMichael Chan 	bp->num_tests = 0;
38686896cb35SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_PF(bp))
3869eb513658SMichael Chan 		return;
3870eb513658SMichael Chan 
3871eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3872eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3873eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3874eb513658SMichael Chan 	if (rc)
3875eb513658SMichael Chan 		goto ethtool_init_exit;
3876eb513658SMichael Chan 
3877ba642ab7SMichael Chan 	test_info = bp->test_info;
3878ba642ab7SMichael Chan 	if (!test_info)
3879eb513658SMichael Chan 		test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
3880eb513658SMichael Chan 	if (!test_info)
3881eb513658SMichael Chan 		goto ethtool_init_exit;
3882eb513658SMichael Chan 
3883eb513658SMichael Chan 	bp->test_info = test_info;
3884eb513658SMichael Chan 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
3885eb513658SMichael Chan 	if (bp->num_tests > BNXT_MAX_TEST)
3886eb513658SMichael Chan 		bp->num_tests = BNXT_MAX_TEST;
3887eb513658SMichael Chan 
3888eb513658SMichael Chan 	test_info->offline_mask = resp->offline_tests;
3889eb513658SMichael Chan 	test_info->timeout = le16_to_cpu(resp->test_timeout);
3890eb513658SMichael Chan 	if (!test_info->timeout)
3891eb513658SMichael Chan 		test_info->timeout = HWRM_CMD_TIMEOUT;
3892eb513658SMichael Chan 	for (i = 0; i < bp->num_tests; i++) {
3893eb513658SMichael Chan 		char *str = test_info->string[i];
3894eb513658SMichael Chan 		char *fw_str = resp->test0_name + i * 32;
3895eb513658SMichael Chan 
3896f7dc1ea6SMichael Chan 		if (i == BNXT_MACLPBK_TEST_IDX) {
3897f7dc1ea6SMichael Chan 			strcpy(str, "Mac loopback test (offline)");
389891725d89SMichael Chan 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
389991725d89SMichael Chan 			strcpy(str, "Phy loopback test (offline)");
390055fd0cf3SMichael Chan 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
390155fd0cf3SMichael Chan 			strcpy(str, "Ext loopback test (offline)");
390267fea463SMichael Chan 		} else if (i == BNXT_IRQ_TEST_IDX) {
390367fea463SMichael Chan 			strcpy(str, "Interrupt_test (offline)");
3904f7dc1ea6SMichael Chan 		} else {
3905eb513658SMichael Chan 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
3906eb513658SMichael Chan 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
3907eb513658SMichael Chan 			if (test_info->offline_mask & (1 << i))
3908eb513658SMichael Chan 				strncat(str, " (offline)",
3909eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3910eb513658SMichael Chan 			else
3911eb513658SMichael Chan 				strncat(str, " (online)",
3912eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3913eb513658SMichael Chan 		}
3914f7dc1ea6SMichael Chan 	}
3915eb513658SMichael Chan 
3916eb513658SMichael Chan ethtool_init_exit:
3917eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3918eb513658SMichael Chan }
3919eb513658SMichael Chan 
3920eb513658SMichael Chan void bnxt_ethtool_free(struct bnxt *bp)
3921eb513658SMichael Chan {
3922eb513658SMichael Chan 	kfree(bp->test_info);
3923eb513658SMichael Chan 	bp->test_info = NULL;
3924eb513658SMichael Chan }
3925eb513658SMichael Chan 
3926c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
3927f704d243SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3928f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES |
3929f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USECS_IRQ |
3930f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
3931f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_STATS_BLOCK_USECS |
3932f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
393300c04a92SMichael Chan 	.get_link_ksettings	= bnxt_get_link_ksettings,
393400c04a92SMichael Chan 	.set_link_ksettings	= bnxt_set_link_ksettings,
39358b277589SMichael Chan 	.get_fecparam		= bnxt_get_fecparam,
3936ccd6a9dcSMichael Chan 	.set_fecparam		= bnxt_set_fecparam,
3937423cffcfSJakub Kicinski 	.get_pause_stats	= bnxt_get_pause_stats,
3938c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
3939c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
3940c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
3941b5d600b0SVasundhara Volam 	.get_regs_len		= bnxt_get_regs_len,
3942b5d600b0SVasundhara Volam 	.get_regs		= bnxt_get_regs,
39438e202366SMichael Chan 	.get_wol		= bnxt_get_wol,
39445282db6cSMichael Chan 	.set_wol		= bnxt_set_wol,
3945c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
3946c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
3947c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
3948c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
3949c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
3950c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
3951c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
3952c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
3953c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
3954c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
3955c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
3956c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
3957a011952aSMichael Chan 	.set_rxnfc		= bnxt_set_rxnfc,
3958c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
3959c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
3960c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
3961bd3191b5SMichael Chan 	.set_rxfh		= bnxt_set_rxfh,
3962c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
3963c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
3964c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
3965c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
3966c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
396772b34f04SMichael Chan 	.get_eee		= bnxt_get_eee,
396872b34f04SMichael Chan 	.set_eee		= bnxt_set_eee,
396942ee18feSAjit Khaparde 	.get_module_info	= bnxt_get_module_info,
397042ee18feSAjit Khaparde 	.get_module_eeprom	= bnxt_get_module_eeprom,
39715ad2cbeeSMichael Chan 	.nway_reset		= bnxt_nway_reset,
39725ad2cbeeSMichael Chan 	.set_phys_id		= bnxt_set_phys_id,
3973eb513658SMichael Chan 	.self_test		= bnxt_self_test,
397449f7972fSVasundhara Volam 	.reset			= bnxt_reset,
39750b0eacf3SVasundhara Volam 	.set_dump		= bnxt_set_dump,
39766c5657d0SVasundhara Volam 	.get_dump_flag		= bnxt_get_dump_flag,
39776c5657d0SVasundhara Volam 	.get_dump_data		= bnxt_get_dump_data,
3978c0c050c5SMichael Chan };
3979