1c0c050c5SMichael Chan /* Broadcom NetXtreme-C/E network driver.
2c0c050c5SMichael Chan  *
311f15ed3SMichael Chan  * Copyright (c) 2014-2016 Broadcom Corporation
48e202366SMichael Chan  * Copyright (c) 2016-2017 Broadcom Limited
5c0c050c5SMichael Chan  *
6c0c050c5SMichael Chan  * This program is free software; you can redistribute it and/or modify
7c0c050c5SMichael Chan  * it under the terms of the GNU General Public License as published by
8c0c050c5SMichael Chan  * the Free Software Foundation.
9c0c050c5SMichael Chan  */
10c0c050c5SMichael Chan 
113ebf6f0aSRob Swindell #include <linux/ctype.h>
128ddc9aaaSMichael Chan #include <linux/stringify.h>
13c0c050c5SMichael Chan #include <linux/ethtool.h>
14c0c050c5SMichael Chan #include <linux/interrupt.h>
15c0c050c5SMichael Chan #include <linux/pci.h>
16c0c050c5SMichael Chan #include <linux/etherdevice.h>
17c0c050c5SMichael Chan #include <linux/crc32.h>
18c0c050c5SMichael Chan #include <linux/firmware.h>
196c5657d0SVasundhara Volam #include <linux/utsname.h>
206c5657d0SVasundhara Volam #include <linux/time.h>
21c0c050c5SMichael Chan #include "bnxt_hsi.h"
22c0c050c5SMichael Chan #include "bnxt.h"
23f7dc1ea6SMichael Chan #include "bnxt_xdp.h"
24c0c050c5SMichael Chan #include "bnxt_ethtool.h"
25c0c050c5SMichael Chan #include "bnxt_nvm_defs.h"	/* NVRAM content constant and structure defs */
26c0c050c5SMichael Chan #include "bnxt_fw_hdr.h"	/* Firmware hdr constant and structure defs */
276c5657d0SVasundhara Volam #include "bnxt_coredump.h"
28c0c050c5SMichael Chan #define FLASH_NVRAM_TIMEOUT	((HWRM_CMD_TIMEOUT) * 100)
295ac67d8bSRob Swindell #define FLASH_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
305ac67d8bSRob Swindell #define INSTALL_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
31c0c050c5SMichael Chan 
32c0c050c5SMichael Chan static u32 bnxt_get_msglevel(struct net_device *dev)
33c0c050c5SMichael Chan {
34c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
35c0c050c5SMichael Chan 
36c0c050c5SMichael Chan 	return bp->msg_enable;
37c0c050c5SMichael Chan }
38c0c050c5SMichael Chan 
39c0c050c5SMichael Chan static void bnxt_set_msglevel(struct net_device *dev, u32 value)
40c0c050c5SMichael Chan {
41c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
42c0c050c5SMichael Chan 
43c0c050c5SMichael Chan 	bp->msg_enable = value;
44c0c050c5SMichael Chan }
45c0c050c5SMichael Chan 
46c0c050c5SMichael Chan static int bnxt_get_coalesce(struct net_device *dev,
47c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
48c0c050c5SMichael Chan {
49c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5018775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
5118775aa8SMichael Chan 	u16 mult;
52c0c050c5SMichael Chan 
53c0c050c5SMichael Chan 	memset(coal, 0, sizeof(*coal));
54c0c050c5SMichael Chan 
556a8788f2SAndy Gospodarek 	coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM;
566a8788f2SAndy Gospodarek 
5718775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
5818775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
5918775aa8SMichael Chan 	coal->rx_coalesce_usecs = hw_coal->coal_ticks;
6018775aa8SMichael Chan 	coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6118775aa8SMichael Chan 	coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
6218775aa8SMichael Chan 	coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
63c0c050c5SMichael Chan 
6418775aa8SMichael Chan 	hw_coal = &bp->tx_coal;
6518775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
6618775aa8SMichael Chan 	coal->tx_coalesce_usecs = hw_coal->coal_ticks;
6718775aa8SMichael Chan 	coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6818775aa8SMichael Chan 	coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
6918775aa8SMichael Chan 	coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
70dfc9c94aSMichael Chan 
7151f30785SMichael Chan 	coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
7251f30785SMichael Chan 
73c0c050c5SMichael Chan 	return 0;
74c0c050c5SMichael Chan }
75c0c050c5SMichael Chan 
76c0c050c5SMichael Chan static int bnxt_set_coalesce(struct net_device *dev,
77c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
78c0c050c5SMichael Chan {
79c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
8051f30785SMichael Chan 	bool update_stats = false;
8118775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
82c0c050c5SMichael Chan 	int rc = 0;
8318775aa8SMichael Chan 	u16 mult;
84c0c050c5SMichael Chan 
856a8788f2SAndy Gospodarek 	if (coal->use_adaptive_rx_coalesce) {
866a8788f2SAndy Gospodarek 		bp->flags |= BNXT_FLAG_DIM;
876a8788f2SAndy Gospodarek 	} else {
886a8788f2SAndy Gospodarek 		if (bp->flags & BNXT_FLAG_DIM) {
896a8788f2SAndy Gospodarek 			bp->flags &= ~(BNXT_FLAG_DIM);
906a8788f2SAndy Gospodarek 			goto reset_coalesce;
916a8788f2SAndy Gospodarek 		}
926a8788f2SAndy Gospodarek 	}
936a8788f2SAndy Gospodarek 
9418775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
9518775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
9618775aa8SMichael Chan 	hw_coal->coal_ticks = coal->rx_coalesce_usecs;
9718775aa8SMichael Chan 	hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult;
9818775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq;
9918775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult;
100c0c050c5SMichael Chan 
101de4a10efSAndy Gospodarek 	hw_coal = &bp->tx_coal;
10218775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
10318775aa8SMichael Chan 	hw_coal->coal_ticks = coal->tx_coalesce_usecs;
10418775aa8SMichael Chan 	hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult;
10518775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq;
10618775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult;
107dfc9c94aSMichael Chan 
10851f30785SMichael Chan 	if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
10951f30785SMichael Chan 		u32 stats_ticks = coal->stats_block_coalesce_usecs;
11051f30785SMichael Chan 
111adcc331eSMichael Chan 		/* Allow 0, which means disable. */
112adcc331eSMichael Chan 		if (stats_ticks)
11351f30785SMichael Chan 			stats_ticks = clamp_t(u32, stats_ticks,
11451f30785SMichael Chan 					      BNXT_MIN_STATS_COAL_TICKS,
11551f30785SMichael Chan 					      BNXT_MAX_STATS_COAL_TICKS);
11651f30785SMichael Chan 		stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
11751f30785SMichael Chan 		bp->stats_coal_ticks = stats_ticks;
118e795892eSMichael Chan 		if (bp->stats_coal_ticks)
119e795892eSMichael Chan 			bp->current_interval =
120e795892eSMichael Chan 				bp->stats_coal_ticks * HZ / 1000000;
121e795892eSMichael Chan 		else
122e795892eSMichael Chan 			bp->current_interval = BNXT_TIMER_INTERVAL;
12351f30785SMichael Chan 		update_stats = true;
12451f30785SMichael Chan 	}
12551f30785SMichael Chan 
1266a8788f2SAndy Gospodarek reset_coalesce:
12751f30785SMichael Chan 	if (netif_running(dev)) {
12851f30785SMichael Chan 		if (update_stats) {
12951f30785SMichael Chan 			rc = bnxt_close_nic(bp, true, false);
13051f30785SMichael Chan 			if (!rc)
13151f30785SMichael Chan 				rc = bnxt_open_nic(bp, true, false);
13251f30785SMichael Chan 		} else {
133c0c050c5SMichael Chan 			rc = bnxt_hwrm_set_coal(bp);
13451f30785SMichael Chan 		}
13551f30785SMichael Chan 	}
136c0c050c5SMichael Chan 
137c0c050c5SMichael Chan 	return rc;
138c0c050c5SMichael Chan }
139c0c050c5SMichael Chan 
1403316d509SMichael Chan static const char * const bnxt_ring_rx_stats_str[] = {
141ee79566eSMichael Chan 	"rx_ucast_packets",
142ee79566eSMichael Chan 	"rx_mcast_packets",
143ee79566eSMichael Chan 	"rx_bcast_packets",
144ee79566eSMichael Chan 	"rx_discards",
145bfc6e5fbSMichael Chan 	"rx_errors",
146ee79566eSMichael Chan 	"rx_ucast_bytes",
147ee79566eSMichael Chan 	"rx_mcast_bytes",
148ee79566eSMichael Chan 	"rx_bcast_bytes",
1493316d509SMichael Chan };
1503316d509SMichael Chan 
1513316d509SMichael Chan static const char * const bnxt_ring_tx_stats_str[] = {
152ee79566eSMichael Chan 	"tx_ucast_packets",
153ee79566eSMichael Chan 	"tx_mcast_packets",
154ee79566eSMichael Chan 	"tx_bcast_packets",
155bfc6e5fbSMichael Chan 	"tx_errors",
156ee79566eSMichael Chan 	"tx_discards",
157ee79566eSMichael Chan 	"tx_ucast_bytes",
158ee79566eSMichael Chan 	"tx_mcast_bytes",
159ee79566eSMichael Chan 	"tx_bcast_bytes",
160ee79566eSMichael Chan };
161ee79566eSMichael Chan 
162ee79566eSMichael Chan static const char * const bnxt_ring_tpa_stats_str[] = {
163ee79566eSMichael Chan 	"tpa_packets",
164ee79566eSMichael Chan 	"tpa_bytes",
165ee79566eSMichael Chan 	"tpa_events",
166ee79566eSMichael Chan 	"tpa_aborts",
167ee79566eSMichael Chan };
168ee79566eSMichael Chan 
16978e7b866SMichael Chan static const char * const bnxt_ring_tpa2_stats_str[] = {
17078e7b866SMichael Chan 	"rx_tpa_eligible_pkt",
17178e7b866SMichael Chan 	"rx_tpa_eligible_bytes",
17278e7b866SMichael Chan 	"rx_tpa_pkt",
17378e7b866SMichael Chan 	"rx_tpa_bytes",
17478e7b866SMichael Chan 	"rx_tpa_errors",
1759d6b648cSMichael Chan 	"rx_tpa_events",
17678e7b866SMichael Chan };
17778e7b866SMichael Chan 
1789d8b5f05SMichael Chan static const char * const bnxt_rx_sw_stats_str[] = {
179ee79566eSMichael Chan 	"rx_l4_csum_errors",
18019b3751fSMichael Chan 	"rx_buf_errors",
1819d8b5f05SMichael Chan };
1829d8b5f05SMichael Chan 
1839d8b5f05SMichael Chan static const char * const bnxt_cmn_sw_stats_str[] = {
184ee79566eSMichael Chan 	"missed_irqs",
185ee79566eSMichael Chan };
186c0c050c5SMichael Chan 
1878ddc9aaaSMichael Chan #define BNXT_RX_STATS_ENTRY(counter)	\
1888ddc9aaaSMichael Chan 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
1898ddc9aaaSMichael Chan 
1908ddc9aaaSMichael Chan #define BNXT_TX_STATS_ENTRY(counter)	\
1918ddc9aaaSMichael Chan 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
1928ddc9aaaSMichael Chan 
19300db3cbaSVasundhara Volam #define BNXT_RX_STATS_EXT_ENTRY(counter)	\
19400db3cbaSVasundhara Volam 	{ BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
19500db3cbaSVasundhara Volam 
19636e53349SMichael Chan #define BNXT_TX_STATS_EXT_ENTRY(counter)	\
19736e53349SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter), __stringify(counter) }
19836e53349SMichael Chan 
19936e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRY(n)				\
20036e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_duration_us),	\
20136e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_transitions)
20236e53349SMichael Chan 
20336e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRY(n)				\
20436e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_duration_us),	\
20536e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_transitions)
20636e53349SMichael Chan 
20736e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRIES				\
20836e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(0),				\
20936e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(1),				\
21036e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(2),				\
21136e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(3),				\
21236e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(4),				\
21336e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(5),				\
21436e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(6),				\
21536e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(7)
21636e53349SMichael Chan 
21736e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRIES				\
21836e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(0),				\
21936e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(1),				\
22036e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(2),				\
22136e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(3),				\
22236e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(4),				\
22336e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(5),				\
22436e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(6),				\
22536e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(7)
22636e53349SMichael Chan 
22736e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRY(n)				\
22836e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bytes_cos##n),		\
22936e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_packets_cos##n)
23036e53349SMichael Chan 
23136e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRY(n)				\
23236e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_bytes_cos##n),		\
23336e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_packets_cos##n)
23436e53349SMichael Chan 
23536e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRIES				\
23636e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(0),				\
23736e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(1),				\
23836e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(2),				\
23936e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(3),				\
24036e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(4),				\
24136e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(5),				\
24236e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(6),				\
24336e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(7)				\
24436e53349SMichael Chan 
24536e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRIES				\
24636e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(0),				\
24736e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(1),				\
24836e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(2),				\
24936e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(3),				\
25036e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(4),				\
25136e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(5),				\
25236e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(6),				\
25336e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(7)				\
25436e53349SMichael Chan 
2552792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(n)			\
2562792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_bytes_cos##n),	\
2572792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_packets_cos##n)
2582792b5b9SMichael Chan 
2592792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES				\
2602792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(0),				\
2612792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(1),				\
2622792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(2),				\
2632792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(3),				\
2642792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(4),				\
2652792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(5),				\
2662792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(6),				\
2672792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(7)
2682792b5b9SMichael Chan 
269e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRY(counter, n)		\
270e37fed79SMichael Chan 	{ BNXT_RX_STATS_EXT_OFFSET(counter##_cos0),	\
271e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
272e37fed79SMichael Chan 
273e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRY(counter, n)		\
274e37fed79SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter##_cos0),	\
275e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
276e37fed79SMichael Chan 
277e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRIES(counter)		\
278e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 0),		\
279e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 1),		\
280e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 2),		\
281e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 3),		\
282e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 4),		\
283e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 5),		\
284e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 6),		\
285e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 7)
286e37fed79SMichael Chan 
287e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRIES(counter)		\
288e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 0),		\
289e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 1),		\
290e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 2),		\
291e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 3),		\
292e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 4),		\
293e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 5),		\
294e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 6),		\
295e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 7)
296e37fed79SMichael Chan 
29720c1d28eSVasundhara Volam enum {
29820c1d28eSVasundhara Volam 	RX_TOTAL_DISCARDS,
29920c1d28eSVasundhara Volam 	TX_TOTAL_DISCARDS,
30020c1d28eSVasundhara Volam };
30120c1d28eSVasundhara Volam 
30220c1d28eSVasundhara Volam static struct {
30320c1d28eSVasundhara Volam 	u64			counter;
30420c1d28eSVasundhara Volam 	char			string[ETH_GSTRING_LEN];
30520c1d28eSVasundhara Volam } bnxt_sw_func_stats[] = {
30620c1d28eSVasundhara Volam 	{0, "rx_total_discard_pkts"},
30720c1d28eSVasundhara Volam 	{0, "tx_total_discard_pkts"},
30820c1d28eSVasundhara Volam };
30920c1d28eSVasundhara Volam 
3103316d509SMichael Chan #define NUM_RING_RX_SW_STATS		ARRAY_SIZE(bnxt_rx_sw_stats_str)
3113316d509SMichael Chan #define NUM_RING_CMN_SW_STATS		ARRAY_SIZE(bnxt_cmn_sw_stats_str)
3123316d509SMichael Chan #define NUM_RING_RX_HW_STATS		ARRAY_SIZE(bnxt_ring_rx_stats_str)
3133316d509SMichael Chan #define NUM_RING_TX_HW_STATS		ARRAY_SIZE(bnxt_ring_tx_stats_str)
3143316d509SMichael Chan 
3158ddc9aaaSMichael Chan static const struct {
3168ddc9aaaSMichael Chan 	long offset;
3178ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
3188ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
3198ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
3208ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
3218ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
3228ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
3238ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
3246fc92c33SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
3258ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
3268ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
3278ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
3288ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
3298ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
3308ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
3318ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
3328ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
3338ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
3348ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
3358ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
3368ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
3378ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
3388ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
3398ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
3408ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
3418ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
3428ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
3438ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
3448ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
345c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
346c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
347c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
348c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
349c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
350c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
351c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
352c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
3538ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
3548ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
3558ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
3568ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
3578ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
3588ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
359699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
360699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_err),
3618ddc9aaaSMichael Chan 
3628ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
3638ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
3648ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
3658ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
3668ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
3676fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
3688ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
3696fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
3708ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
3718ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
3728ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
3738ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
3748ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
3758ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
3768ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
3778ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
3788ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
3798ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
3808ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
3818ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
3828ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
3838ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
384c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
385c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
386c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
387c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
388c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
389c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
390c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
391c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
3928ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
3938ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
3948ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
3958ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
396699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
397699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
398699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_error),
3998ddc9aaaSMichael Chan };
4008ddc9aaaSMichael Chan 
40100db3cbaSVasundhara Volam static const struct {
40200db3cbaSVasundhara Volam 	long offset;
40300db3cbaSVasundhara Volam 	char string[ETH_GSTRING_LEN];
40400db3cbaSVasundhara Volam } bnxt_port_stats_ext_arr[] = {
40500db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
40600db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
40700db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
40800db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
40900db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
41036e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRIES,
41136e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRIES,
4124a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bits),
4134a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_buffer_passed_threshold),
4144a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_pcs_symbol_err),
4154a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_corrected_bits),
4162792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES,
41736e53349SMichael Chan };
41836e53349SMichael Chan 
41936e53349SMichael Chan static const struct {
42036e53349SMichael Chan 	long offset;
42136e53349SMichael Chan 	char string[ETH_GSTRING_LEN];
42236e53349SMichael Chan } bnxt_tx_port_stats_ext_arr[] = {
42336e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRIES,
42436e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRIES,
42500db3cbaSVasundhara Volam };
42600db3cbaSVasundhara Volam 
427e37fed79SMichael Chan static const struct {
428e37fed79SMichael Chan 	long base_off;
429e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
430e37fed79SMichael Chan } bnxt_rx_bytes_pri_arr[] = {
431e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_bytes),
432e37fed79SMichael Chan };
433e37fed79SMichael Chan 
434e37fed79SMichael Chan static const struct {
435e37fed79SMichael Chan 	long base_off;
436e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
437e37fed79SMichael Chan } bnxt_rx_pkts_pri_arr[] = {
438e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_packets),
439e37fed79SMichael Chan };
440e37fed79SMichael Chan 
441e37fed79SMichael Chan static const struct {
442e37fed79SMichael Chan 	long base_off;
443e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
444e37fed79SMichael Chan } bnxt_tx_bytes_pri_arr[] = {
445e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_bytes),
446e37fed79SMichael Chan };
447e37fed79SMichael Chan 
448e37fed79SMichael Chan static const struct {
449e37fed79SMichael Chan 	long base_off;
450e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
451e37fed79SMichael Chan } bnxt_tx_pkts_pri_arr[] = {
452e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_packets),
453e37fed79SMichael Chan };
454e37fed79SMichael Chan 
45520c1d28eSVasundhara Volam #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
4568ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
457e37fed79SMichael Chan #define BNXT_NUM_STATS_PRI			\
458e37fed79SMichael Chan 	(ARRAY_SIZE(bnxt_rx_bytes_pri_arr) +	\
459e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_rx_pkts_pri_arr) +	\
460e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_bytes_pri_arr) +	\
461e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_pkts_pri_arr))
4628ddc9aaaSMichael Chan 
46378e7b866SMichael Chan static int bnxt_get_num_tpa_ring_stats(struct bnxt *bp)
46478e7b866SMichael Chan {
46578e7b866SMichael Chan 	if (BNXT_SUPPORTS_TPA(bp)) {
4669d6b648cSMichael Chan 		if (bp->max_tpa_v2) {
4679d6b648cSMichael Chan 			if (BNXT_CHIP_P5_THOR(bp))
4689d6b648cSMichael Chan 				return BNXT_NUM_TPA_RING_STATS_P5;
4699d6b648cSMichael Chan 			return BNXT_NUM_TPA_RING_STATS_P5_SR2;
4709d6b648cSMichael Chan 		}
4719d6b648cSMichael Chan 		return BNXT_NUM_TPA_RING_STATS;
47278e7b866SMichael Chan 	}
47378e7b866SMichael Chan 	return 0;
47478e7b866SMichael Chan }
47578e7b866SMichael Chan 
476ee79566eSMichael Chan static int bnxt_get_num_ring_stats(struct bnxt *bp)
477ee79566eSMichael Chan {
4783316d509SMichael Chan 	int rx, tx, cmn;
479ee79566eSMichael Chan 
4803316d509SMichael Chan 	rx = NUM_RING_RX_HW_STATS + NUM_RING_RX_SW_STATS +
48178e7b866SMichael Chan 	     bnxt_get_num_tpa_ring_stats(bp);
4823316d509SMichael Chan 	tx = NUM_RING_TX_HW_STATS;
4833316d509SMichael Chan 	cmn = NUM_RING_CMN_SW_STATS;
484125592fbSRajesh Ravi 	return rx * bp->rx_nr_rings + tx * bp->tx_nr_rings +
485125592fbSRajesh Ravi 	       cmn * bp->cp_nr_rings;
486ee79566eSMichael Chan }
487ee79566eSMichael Chan 
4885c8227d0SMichael Chan static int bnxt_get_num_stats(struct bnxt *bp)
489c0c050c5SMichael Chan {
490ee79566eSMichael Chan 	int num_stats = bnxt_get_num_ring_stats(bp);
4918ddc9aaaSMichael Chan 
49220c1d28eSVasundhara Volam 	num_stats += BNXT_NUM_SW_FUNC_STATS;
49320c1d28eSVasundhara Volam 
4948ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS)
4958ddc9aaaSMichael Chan 		num_stats += BNXT_NUM_PORT_STATS;
4968ddc9aaaSMichael Chan 
497e37fed79SMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
49836e53349SMichael Chan 		num_stats += bp->fw_rx_stats_ext_size +
49936e53349SMichael Chan 			     bp->fw_tx_stats_ext_size;
500e37fed79SMichael Chan 		if (bp->pri2cos_valid)
501e37fed79SMichael Chan 			num_stats += BNXT_NUM_STATS_PRI;
502e37fed79SMichael Chan 	}
50300db3cbaSVasundhara Volam 
5048ddc9aaaSMichael Chan 	return num_stats;
5058ddc9aaaSMichael Chan }
5065c8227d0SMichael Chan 
5075c8227d0SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
5085c8227d0SMichael Chan {
5095c8227d0SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5105c8227d0SMichael Chan 
5115c8227d0SMichael Chan 	switch (sset) {
5125c8227d0SMichael Chan 	case ETH_SS_STATS:
5135c8227d0SMichael Chan 		return bnxt_get_num_stats(bp);
514eb513658SMichael Chan 	case ETH_SS_TEST:
515eb513658SMichael Chan 		if (!bp->num_tests)
516eb513658SMichael Chan 			return -EOPNOTSUPP;
517eb513658SMichael Chan 		return bp->num_tests;
518c0c050c5SMichael Chan 	default:
519c0c050c5SMichael Chan 		return -EOPNOTSUPP;
520c0c050c5SMichael Chan 	}
521c0c050c5SMichael Chan }
522c0c050c5SMichael Chan 
523125592fbSRajesh Ravi static bool is_rx_ring(struct bnxt *bp, int ring_num)
524125592fbSRajesh Ravi {
525125592fbSRajesh Ravi 	return ring_num < bp->rx_nr_rings;
526125592fbSRajesh Ravi }
527125592fbSRajesh Ravi 
528125592fbSRajesh Ravi static bool is_tx_ring(struct bnxt *bp, int ring_num)
529125592fbSRajesh Ravi {
530125592fbSRajesh Ravi 	int tx_base = 0;
531125592fbSRajesh Ravi 
532125592fbSRajesh Ravi 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
533125592fbSRajesh Ravi 		tx_base = bp->rx_nr_rings;
534125592fbSRajesh Ravi 
535125592fbSRajesh Ravi 	if (ring_num >= tx_base && ring_num < (tx_base + bp->tx_nr_rings))
536125592fbSRajesh Ravi 		return true;
537125592fbSRajesh Ravi 	return false;
538125592fbSRajesh Ravi }
539125592fbSRajesh Ravi 
540c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
541c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
542c0c050c5SMichael Chan {
543c0c050c5SMichael Chan 	u32 i, j = 0;
544c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
545125592fbSRajesh Ravi 	u32 tpa_stats;
546c0c050c5SMichael Chan 
547fd3ab1c7SMichael Chan 	if (!bp->bnapi) {
548ee79566eSMichael Chan 		j += bnxt_get_num_ring_stats(bp) + BNXT_NUM_SW_FUNC_STATS;
549fd3ab1c7SMichael Chan 		goto skip_ring_stats;
550fd3ab1c7SMichael Chan 	}
551c0c050c5SMichael Chan 
55220c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
55320c1d28eSVasundhara Volam 		bnxt_sw_func_stats[i].counter = 0;
55420c1d28eSVasundhara Volam 
555125592fbSRajesh Ravi 	tpa_stats = bnxt_get_num_tpa_ring_stats(bp);
556c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
557c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
558c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
559a0c30621SMichael Chan 		u64 *sw_stats = cpr->stats.sw_stats;
5609d8b5f05SMichael Chan 		u64 *sw;
561c0c050c5SMichael Chan 		int k;
562c0c050c5SMichael Chan 
563125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
564125592fbSRajesh Ravi 			for (k = 0; k < NUM_RING_RX_HW_STATS; j++, k++)
565a0c30621SMichael Chan 				buf[j] = sw_stats[k];
566125592fbSRajesh Ravi 		}
567125592fbSRajesh Ravi 		if (is_tx_ring(bp, i)) {
568125592fbSRajesh Ravi 			k = NUM_RING_RX_HW_STATS;
569125592fbSRajesh Ravi 			for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
570125592fbSRajesh Ravi 			       j++, k++)
571a0c30621SMichael Chan 				buf[j] = sw_stats[k];
572125592fbSRajesh Ravi 		}
573125592fbSRajesh Ravi 		if (!tpa_stats || !is_rx_ring(bp, i))
574125592fbSRajesh Ravi 			goto skip_tpa_ring_stats;
575125592fbSRajesh Ravi 
576125592fbSRajesh Ravi 		k = NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
577125592fbSRajesh Ravi 		for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS +
578125592fbSRajesh Ravi 			   tpa_stats; j++, k++)
579a0c30621SMichael Chan 			buf[j] = sw_stats[k];
5809d8b5f05SMichael Chan 
581125592fbSRajesh Ravi skip_tpa_ring_stats:
5829d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.rx;
583125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
5843316d509SMichael Chan 			for (k = 0; k < NUM_RING_RX_SW_STATS; j++, k++)
5859d8b5f05SMichael Chan 				buf[j] = sw[k];
586125592fbSRajesh Ravi 		}
5879d8b5f05SMichael Chan 
5889d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.cmn;
5893316d509SMichael Chan 		for (k = 0; k < NUM_RING_CMN_SW_STATS; j++, k++)
5909d8b5f05SMichael Chan 			buf[j] = sw[k];
59120c1d28eSVasundhara Volam 
59220c1d28eSVasundhara Volam 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
593a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, rx_discard_pkts);
59420c1d28eSVasundhara Volam 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
595a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, tx_discard_pkts);
596c0c050c5SMichael Chan 	}
59720c1d28eSVasundhara Volam 
59820c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
59920c1d28eSVasundhara Volam 		buf[j] = bnxt_sw_func_stats[i].counter;
60020c1d28eSVasundhara Volam 
601fd3ab1c7SMichael Chan skip_ring_stats:
6028ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
603a0c30621SMichael Chan 		u64 *port_stats = bp->port_stats.sw_stats;
6048ddc9aaaSMichael Chan 
605a0c30621SMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++)
606a0c30621SMichael Chan 			buf[j] = *(port_stats + bnxt_port_stats_arr[i].offset);
6078ddc9aaaSMichael Chan 	}
60800db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
609a0c30621SMichael Chan 		u64 *rx_port_stats_ext = bp->rx_port_stats_ext.sw_stats;
610a0c30621SMichael Chan 		u64 *tx_port_stats_ext = bp->tx_port_stats_ext.sw_stats;
61100db3cbaSVasundhara Volam 
61236e53349SMichael Chan 		for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
613a0c30621SMichael Chan 			buf[j] = *(rx_port_stats_ext +
614a0c30621SMichael Chan 				   bnxt_port_stats_ext_arr[i].offset);
61500db3cbaSVasundhara Volam 		}
61636e53349SMichael Chan 		for (i = 0; i < bp->fw_tx_stats_ext_size; i++, j++) {
617a0c30621SMichael Chan 			buf[j] = *(tx_port_stats_ext +
618a0c30621SMichael Chan 				   bnxt_tx_port_stats_ext_arr[i].offset);
61936e53349SMichael Chan 		}
620e37fed79SMichael Chan 		if (bp->pri2cos_valid) {
621e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
622e37fed79SMichael Chan 				long n = bnxt_rx_bytes_pri_arr[i].base_off +
623a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
624e37fed79SMichael Chan 
625a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
626e37fed79SMichael Chan 			}
627e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
628e37fed79SMichael Chan 				long n = bnxt_rx_pkts_pri_arr[i].base_off +
629a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
630e37fed79SMichael Chan 
631a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
632e37fed79SMichael Chan 			}
633e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
634e37fed79SMichael Chan 				long n = bnxt_tx_bytes_pri_arr[i].base_off +
635a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
636e37fed79SMichael Chan 
637a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
638e37fed79SMichael Chan 			}
639e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
640e37fed79SMichael Chan 				long n = bnxt_tx_pkts_pri_arr[i].base_off +
641a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
642e37fed79SMichael Chan 
643a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
644e37fed79SMichael Chan 			}
645e37fed79SMichael Chan 		}
64600db3cbaSVasundhara Volam 	}
647c0c050c5SMichael Chan }
648c0c050c5SMichael Chan 
649c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
650c0c050c5SMichael Chan {
651c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
65278e7b866SMichael Chan 	static const char * const *str;
653ee79566eSMichael Chan 	u32 i, j, num_str;
654c0c050c5SMichael Chan 
655c0c050c5SMichael Chan 	switch (stringset) {
656c0c050c5SMichael Chan 	case ETH_SS_STATS:
657c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
658125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6593316d509SMichael Chan 				num_str = NUM_RING_RX_HW_STATS;
660ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
661ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
6623316d509SMichael Chan 						bnxt_ring_rx_stats_str[j]);
663c0c050c5SMichael Chan 					buf += ETH_GSTRING_LEN;
664ee79566eSMichael Chan 				}
665125592fbSRajesh Ravi 			}
666125592fbSRajesh Ravi 			if (is_tx_ring(bp, i)) {
6673316d509SMichael Chan 				num_str = NUM_RING_TX_HW_STATS;
6683316d509SMichael Chan 				for (j = 0; j < num_str; j++) {
6693316d509SMichael Chan 					sprintf(buf, "[%d]: %s", i,
6703316d509SMichael Chan 						bnxt_ring_tx_stats_str[j]);
6713316d509SMichael Chan 					buf += ETH_GSTRING_LEN;
6723316d509SMichael Chan 				}
673125592fbSRajesh Ravi 			}
6743316d509SMichael Chan 			num_str = bnxt_get_num_tpa_ring_stats(bp);
675125592fbSRajesh Ravi 			if (!num_str || !is_rx_ring(bp, i))
676ee79566eSMichael Chan 				goto skip_tpa_stats;
677ee79566eSMichael Chan 
6783316d509SMichael Chan 			if (bp->max_tpa_v2)
67978e7b866SMichael Chan 				str = bnxt_ring_tpa2_stats_str;
6803316d509SMichael Chan 			else
68178e7b866SMichael Chan 				str = bnxt_ring_tpa_stats_str;
6823316d509SMichael Chan 
683ee79566eSMichael Chan 			for (j = 0; j < num_str; j++) {
68478e7b866SMichael Chan 				sprintf(buf, "[%d]: %s", i, str[j]);
685c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
686ee79566eSMichael Chan 			}
687ee79566eSMichael Chan skip_tpa_stats:
688125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6893316d509SMichael Chan 				num_str = NUM_RING_RX_SW_STATS;
690ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
691ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
6929d8b5f05SMichael Chan 						bnxt_rx_sw_stats_str[j]);
6939d8b5f05SMichael Chan 					buf += ETH_GSTRING_LEN;
6949d8b5f05SMichael Chan 				}
695125592fbSRajesh Ravi 			}
6963316d509SMichael Chan 			num_str = NUM_RING_CMN_SW_STATS;
6979d8b5f05SMichael Chan 			for (j = 0; j < num_str; j++) {
6989d8b5f05SMichael Chan 				sprintf(buf, "[%d]: %s", i,
6999d8b5f05SMichael Chan 					bnxt_cmn_sw_stats_str[j]);
700c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
701ee79566eSMichael Chan 			}
702c0c050c5SMichael Chan 		}
70320c1d28eSVasundhara Volam 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
70420c1d28eSVasundhara Volam 			strcpy(buf, bnxt_sw_func_stats[i].string);
70520c1d28eSVasundhara Volam 			buf += ETH_GSTRING_LEN;
70620c1d28eSVasundhara Volam 		}
70720c1d28eSVasundhara Volam 
7088ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
7098ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
7108ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
7118ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
7128ddc9aaaSMichael Chan 			}
7138ddc9aaaSMichael Chan 		}
71400db3cbaSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
71536e53349SMichael Chan 			for (i = 0; i < bp->fw_rx_stats_ext_size; i++) {
71600db3cbaSVasundhara Volam 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
71700db3cbaSVasundhara Volam 				buf += ETH_GSTRING_LEN;
71800db3cbaSVasundhara Volam 			}
71936e53349SMichael Chan 			for (i = 0; i < bp->fw_tx_stats_ext_size; i++) {
72036e53349SMichael Chan 				strcpy(buf,
72136e53349SMichael Chan 				       bnxt_tx_port_stats_ext_arr[i].string);
72236e53349SMichael Chan 				buf += ETH_GSTRING_LEN;
72336e53349SMichael Chan 			}
724e37fed79SMichael Chan 			if (bp->pri2cos_valid) {
725e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
726e37fed79SMichael Chan 					strcpy(buf,
727e37fed79SMichael Chan 					       bnxt_rx_bytes_pri_arr[i].string);
728e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
729e37fed79SMichael Chan 				}
730e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
731e37fed79SMichael Chan 					strcpy(buf,
732e37fed79SMichael Chan 					       bnxt_rx_pkts_pri_arr[i].string);
733e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
734e37fed79SMichael Chan 				}
735e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
736e37fed79SMichael Chan 					strcpy(buf,
737e37fed79SMichael Chan 					       bnxt_tx_bytes_pri_arr[i].string);
738e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
739e37fed79SMichael Chan 				}
740e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
741e37fed79SMichael Chan 					strcpy(buf,
742e37fed79SMichael Chan 					       bnxt_tx_pkts_pri_arr[i].string);
743e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
744e37fed79SMichael Chan 				}
745e37fed79SMichael Chan 			}
74600db3cbaSVasundhara Volam 		}
747c0c050c5SMichael Chan 		break;
748eb513658SMichael Chan 	case ETH_SS_TEST:
749eb513658SMichael Chan 		if (bp->num_tests)
750eb513658SMichael Chan 			memcpy(buf, bp->test_info->string,
751eb513658SMichael Chan 			       bp->num_tests * ETH_GSTRING_LEN);
752eb513658SMichael Chan 		break;
753c0c050c5SMichael Chan 	default:
754c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
755c0c050c5SMichael Chan 			   stringset);
756c0c050c5SMichael Chan 		break;
757c0c050c5SMichael Chan 	}
758c0c050c5SMichael Chan }
759c0c050c5SMichael Chan 
760c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
761c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
762c0c050c5SMichael Chan {
763c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
764c0c050c5SMichael Chan 
765c0c050c5SMichael Chan 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
766c0c050c5SMichael Chan 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
767c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
768c0c050c5SMichael Chan 
769c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
770c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
771c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
772c0c050c5SMichael Chan }
773c0c050c5SMichael Chan 
774c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
775c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
776c0c050c5SMichael Chan {
777c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
778c0c050c5SMichael Chan 
779c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
780c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
781c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
782c0c050c5SMichael Chan 		return -EINVAL;
783c0c050c5SMichael Chan 
784c0c050c5SMichael Chan 	if (netif_running(dev))
785c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
786c0c050c5SMichael Chan 
787c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
788c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
789c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
790c0c050c5SMichael Chan 
791c0c050c5SMichael Chan 	if (netif_running(dev))
792c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
793c0c050c5SMichael Chan 
794c0c050c5SMichael Chan 	return 0;
795c0c050c5SMichael Chan }
796c0c050c5SMichael Chan 
797c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
798c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
799c0c050c5SMichael Chan {
800c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
801db4723b3SMichael Chan 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
802c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
803db4723b3SMichael Chan 	int max_tx_sch_inputs;
804db4723b3SMichael Chan 
805db4723b3SMichael Chan 	/* Get the most up-to-date max_tx_sch_inputs. */
806c1c2d774SPavan Chebbi 	if (netif_running(dev) && BNXT_NEW_RM(bp))
807db4723b3SMichael Chan 		bnxt_hwrm_func_resc_qcaps(bp, false);
808db4723b3SMichael Chan 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
809c0c050c5SMichael Chan 
8106e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
811db4723b3SMichael Chan 	if (max_tx_sch_inputs)
812db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
813a79a5276SMichael Chan 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
814068c9ec6SMichael Chan 
81518d6e4e2SSatish Baddipadige 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
81618d6e4e2SSatish Baddipadige 		max_rx_rings = 0;
81718d6e4e2SSatish Baddipadige 		max_tx_rings = 0;
81818d6e4e2SSatish Baddipadige 	}
819db4723b3SMichael Chan 	if (max_tx_sch_inputs)
820db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
82118d6e4e2SSatish Baddipadige 
822c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
823c0c050c5SMichael Chan 	if (tcs > 1)
824c0c050c5SMichael Chan 		max_tx_rings /= tcs;
825c0c050c5SMichael Chan 
826c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
827c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
828c0c050c5SMichael Chan 	channel->max_other = 0;
829068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
830068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
83176595193SPrashant Sreedharan 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
83276595193SPrashant Sreedharan 			channel->combined_count--;
833068c9ec6SMichael Chan 	} else {
83476595193SPrashant Sreedharan 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
835c0c050c5SMichael Chan 			channel->rx_count = bp->rx_nr_rings;
836c0c050c5SMichael Chan 			channel->tx_count = bp->tx_nr_rings_per_tc;
837c0c050c5SMichael Chan 		}
838068c9ec6SMichael Chan 	}
83976595193SPrashant Sreedharan }
840c0c050c5SMichael Chan 
841c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
842c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
843c0c050c5SMichael Chan {
844c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
845d1e7925eSMichael Chan 	int req_tx_rings, req_rx_rings, tcs;
846068c9ec6SMichael Chan 	bool sh = false;
8475f449249SMichael Chan 	int tx_xdp = 0;
848d1e7925eSMichael Chan 	int rc = 0;
849c0c050c5SMichael Chan 
850068c9ec6SMichael Chan 	if (channel->other_count)
851c0c050c5SMichael Chan 		return -EINVAL;
852c0c050c5SMichael Chan 
853068c9ec6SMichael Chan 	if (!channel->combined_count &&
854068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
855068c9ec6SMichael Chan 		return -EINVAL;
856068c9ec6SMichael Chan 
857068c9ec6SMichael Chan 	if (channel->combined_count &&
858068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
859068c9ec6SMichael Chan 		return -EINVAL;
860068c9ec6SMichael Chan 
86176595193SPrashant Sreedharan 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
86276595193SPrashant Sreedharan 					    channel->tx_count))
86376595193SPrashant Sreedharan 		return -EINVAL;
86476595193SPrashant Sreedharan 
865068c9ec6SMichael Chan 	if (channel->combined_count)
866068c9ec6SMichael Chan 		sh = true;
867068c9ec6SMichael Chan 
868c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
869c0c050c5SMichael Chan 
870391be5c2SMichael Chan 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
871d1e7925eSMichael Chan 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
8725f449249SMichael Chan 	if (bp->tx_nr_rings_xdp) {
8735f449249SMichael Chan 		if (!sh) {
8745f449249SMichael Chan 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
8755f449249SMichael Chan 			return -EINVAL;
8765f449249SMichael Chan 		}
8775f449249SMichael Chan 		tx_xdp = req_rx_rings;
8785f449249SMichael Chan 	}
87998fdbe73SMichael Chan 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
880d1e7925eSMichael Chan 	if (rc) {
881d1e7925eSMichael Chan 		netdev_warn(dev, "Unable to allocate the requested rings\n");
882d1e7925eSMichael Chan 		return rc;
883391be5c2SMichael Chan 	}
884391be5c2SMichael Chan 
885bd3191b5SMichael Chan 	if (bnxt_get_nr_rss_ctxs(bp, req_rx_rings) !=
886bd3191b5SMichael Chan 	    bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) &&
887bd3191b5SMichael Chan 	    (dev->priv_flags & IFF_RXFH_CONFIGURED)) {
888bd3191b5SMichael Chan 		netdev_warn(dev, "RSS table size change required, RSS table entries must be default to proceed\n");
889bd3191b5SMichael Chan 		return -EINVAL;
890bd3191b5SMichael Chan 	}
891bd3191b5SMichael Chan 
892c0c050c5SMichael Chan 	if (netif_running(dev)) {
893c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
894c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
895c0c050c5SMichael Chan 			 * before PF unload
896c0c050c5SMichael Chan 			 */
897c0c050c5SMichael Chan 		}
898c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
899c0c050c5SMichael Chan 		if (rc) {
900c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
901c0c050c5SMichael Chan 				   rc);
902c0c050c5SMichael Chan 			return rc;
903c0c050c5SMichael Chan 		}
904c0c050c5SMichael Chan 	}
905c0c050c5SMichael Chan 
906068c9ec6SMichael Chan 	if (sh) {
907068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
908d1e7925eSMichael Chan 		bp->rx_nr_rings = channel->combined_count;
909d1e7925eSMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
910068c9ec6SMichael Chan 	} else {
911068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
912c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
913c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
914068c9ec6SMichael Chan 	}
9155f449249SMichael Chan 	bp->tx_nr_rings_xdp = tx_xdp;
9165f449249SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
917c0c050c5SMichael Chan 	if (tcs > 1)
9185f449249SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
919068c9ec6SMichael Chan 
920068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
921068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
922068c9ec6SMichael Chan 
9232bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
9242bcfa6f6SMichael Chan 	netdev_update_features(dev);
925c0c050c5SMichael Chan 	if (netif_running(dev)) {
926c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
927c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
928c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
929c0c050c5SMichael Chan 			 * to renable
930c0c050c5SMichael Chan 			 */
931c0c050c5SMichael Chan 		}
932d8c09f19SMichael Chan 	} else {
9331b3f0b75SMichael Chan 		rc = bnxt_reserve_rings(bp, true);
934c0c050c5SMichael Chan 	}
935c0c050c5SMichael Chan 
936c0c050c5SMichael Chan 	return rc;
937c0c050c5SMichael Chan }
938c0c050c5SMichael Chan 
939c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
940c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
941c0c050c5SMichael Chan 			    u32 *rule_locs)
942c0c050c5SMichael Chan {
943c0c050c5SMichael Chan 	int i, j = 0;
944c0c050c5SMichael Chan 
945c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
946c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
947c0c050c5SMichael Chan 		struct hlist_head *head;
948c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
949c0c050c5SMichael Chan 
950c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
951c0c050c5SMichael Chan 		rcu_read_lock();
952c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
953c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
954c0c050c5SMichael Chan 				break;
955c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
956c0c050c5SMichael Chan 		}
957c0c050c5SMichael Chan 		rcu_read_unlock();
958c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
959c0c050c5SMichael Chan 			break;
960c0c050c5SMichael Chan 	}
961c0c050c5SMichael Chan 	cmd->rule_cnt = j;
962c0c050c5SMichael Chan 	return 0;
963c0c050c5SMichael Chan }
964c0c050c5SMichael Chan 
965c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
966c0c050c5SMichael Chan {
967c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
968c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
969c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
970c0c050c5SMichael Chan 	struct flow_keys *fkeys;
971c0c050c5SMichael Chan 	int i, rc = -EINVAL;
972c0c050c5SMichael Chan 
973b721cfafSstephen hemminger 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
974c0c050c5SMichael Chan 		return rc;
975c0c050c5SMichael Chan 
976c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
977c0c050c5SMichael Chan 		struct hlist_head *head;
978c0c050c5SMichael Chan 
979c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
980c0c050c5SMichael Chan 		rcu_read_lock();
981c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
982c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
983c0c050c5SMichael Chan 				goto fltr_found;
984c0c050c5SMichael Chan 		}
985c0c050c5SMichael Chan 		rcu_read_unlock();
986c0c050c5SMichael Chan 	}
987c0c050c5SMichael Chan 	return rc;
988c0c050c5SMichael Chan 
989c0c050c5SMichael Chan fltr_found:
990c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
991dda0e746SMichael Chan 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
992c0c050c5SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
993c0c050c5SMichael Chan 			fs->flow_type = TCP_V4_FLOW;
994c0c050c5SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
995c0c050c5SMichael Chan 			fs->flow_type = UDP_V4_FLOW;
996c0c050c5SMichael Chan 		else
997c0c050c5SMichael Chan 			goto fltr_err;
998c0c050c5SMichael Chan 
999c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
1000c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
1001c0c050c5SMichael Chan 
1002c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
1003c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
1004c0c050c5SMichael Chan 
1005c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
1006c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
1007c0c050c5SMichael Chan 
1008c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
1009c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
1010dda0e746SMichael Chan 	} else {
1011dda0e746SMichael Chan 		int i;
1012dda0e746SMichael Chan 
1013dda0e746SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1014dda0e746SMichael Chan 			fs->flow_type = TCP_V6_FLOW;
1015dda0e746SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1016dda0e746SMichael Chan 			fs->flow_type = UDP_V6_FLOW;
1017dda0e746SMichael Chan 		else
1018dda0e746SMichael Chan 			goto fltr_err;
1019dda0e746SMichael Chan 
1020dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
1021dda0e746SMichael Chan 			fkeys->addrs.v6addrs.src;
1022dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
1023dda0e746SMichael Chan 			fkeys->addrs.v6addrs.dst;
1024dda0e746SMichael Chan 		for (i = 0; i < 4; i++) {
1025dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
1026dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
1027dda0e746SMichael Chan 		}
1028dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
1029dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
1030dda0e746SMichael Chan 
1031dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
1032dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
1033dda0e746SMichael Chan 	}
1034c0c050c5SMichael Chan 
1035c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
1036c0c050c5SMichael Chan 	rc = 0;
1037c0c050c5SMichael Chan 
1038c0c050c5SMichael Chan fltr_err:
1039c0c050c5SMichael Chan 	rcu_read_unlock();
1040c0c050c5SMichael Chan 
1041c0c050c5SMichael Chan 	return rc;
1042c0c050c5SMichael Chan }
1043a011952aSMichael Chan #endif
1044a011952aSMichael Chan 
1045a011952aSMichael Chan static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
1046a011952aSMichael Chan {
1047a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
1048a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1049a011952aSMichael Chan 	return 0;
1050a011952aSMichael Chan }
1051a011952aSMichael Chan 
1052a011952aSMichael Chan static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
1053a011952aSMichael Chan {
1054a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
1055a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1056a011952aSMichael Chan 	return 0;
1057a011952aSMichael Chan }
1058a011952aSMichael Chan 
1059a011952aSMichael Chan static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1060a011952aSMichael Chan {
1061a011952aSMichael Chan 	cmd->data = 0;
1062a011952aSMichael Chan 	switch (cmd->flow_type) {
1063a011952aSMichael Chan 	case TCP_V4_FLOW:
1064a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
1065a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1066a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1067a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1068a011952aSMichael Chan 		break;
1069a011952aSMichael Chan 	case UDP_V4_FLOW:
1070a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
1071a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1072a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1073df561f66SGustavo A. R. Silva 		fallthrough;
1074a011952aSMichael Chan 	case SCTP_V4_FLOW:
1075a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1076a011952aSMichael Chan 	case AH_V4_FLOW:
1077a011952aSMichael Chan 	case ESP_V4_FLOW:
1078a011952aSMichael Chan 	case IPV4_FLOW:
1079a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1080a011952aSMichael Chan 		break;
1081a011952aSMichael Chan 
1082a011952aSMichael Chan 	case TCP_V6_FLOW:
1083a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
1084a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1085a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1086a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1087a011952aSMichael Chan 		break;
1088a011952aSMichael Chan 	case UDP_V6_FLOW:
1089a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
1090a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1091a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1092df561f66SGustavo A. R. Silva 		fallthrough;
1093a011952aSMichael Chan 	case SCTP_V6_FLOW:
1094a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1095a011952aSMichael Chan 	case AH_V6_FLOW:
1096a011952aSMichael Chan 	case ESP_V6_FLOW:
1097a011952aSMichael Chan 	case IPV6_FLOW:
1098a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1099a011952aSMichael Chan 		break;
1100a011952aSMichael Chan 	}
1101a011952aSMichael Chan 	return 0;
1102a011952aSMichael Chan }
1103a011952aSMichael Chan 
1104a011952aSMichael Chan #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1105a011952aSMichael Chan #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
1106a011952aSMichael Chan 
1107a011952aSMichael Chan static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1108a011952aSMichael Chan {
1109a011952aSMichael Chan 	u32 rss_hash_cfg = bp->rss_hash_cfg;
1110a011952aSMichael Chan 	int tuple, rc = 0;
1111a011952aSMichael Chan 
1112a011952aSMichael Chan 	if (cmd->data == RXH_4TUPLE)
1113a011952aSMichael Chan 		tuple = 4;
1114a011952aSMichael Chan 	else if (cmd->data == RXH_2TUPLE)
1115a011952aSMichael Chan 		tuple = 2;
1116a011952aSMichael Chan 	else if (!cmd->data)
1117a011952aSMichael Chan 		tuple = 0;
1118a011952aSMichael Chan 	else
1119a011952aSMichael Chan 		return -EINVAL;
1120a011952aSMichael Chan 
1121a011952aSMichael Chan 	if (cmd->flow_type == TCP_V4_FLOW) {
1122a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1123a011952aSMichael Chan 		if (tuple == 4)
1124a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1125a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V4_FLOW) {
1126a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1127a011952aSMichael Chan 			return -EINVAL;
1128a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1129a011952aSMichael Chan 		if (tuple == 4)
1130a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1131a011952aSMichael Chan 	} else if (cmd->flow_type == TCP_V6_FLOW) {
1132a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1133a011952aSMichael Chan 		if (tuple == 4)
1134a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1135a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V6_FLOW) {
1136a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1137a011952aSMichael Chan 			return -EINVAL;
1138a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1139a011952aSMichael Chan 		if (tuple == 4)
1140a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1141a011952aSMichael Chan 	} else if (tuple == 4) {
1142a011952aSMichael Chan 		return -EINVAL;
1143a011952aSMichael Chan 	}
1144a011952aSMichael Chan 
1145a011952aSMichael Chan 	switch (cmd->flow_type) {
1146a011952aSMichael Chan 	case TCP_V4_FLOW:
1147a011952aSMichael Chan 	case UDP_V4_FLOW:
1148a011952aSMichael Chan 	case SCTP_V4_FLOW:
1149a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1150a011952aSMichael Chan 	case AH_V4_FLOW:
1151a011952aSMichael Chan 	case ESP_V4_FLOW:
1152a011952aSMichael Chan 	case IPV4_FLOW:
1153a011952aSMichael Chan 		if (tuple == 2)
1154a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1155a011952aSMichael Chan 		else if (!tuple)
1156a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1157a011952aSMichael Chan 		break;
1158a011952aSMichael Chan 
1159a011952aSMichael Chan 	case TCP_V6_FLOW:
1160a011952aSMichael Chan 	case UDP_V6_FLOW:
1161a011952aSMichael Chan 	case SCTP_V6_FLOW:
1162a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1163a011952aSMichael Chan 	case AH_V6_FLOW:
1164a011952aSMichael Chan 	case ESP_V6_FLOW:
1165a011952aSMichael Chan 	case IPV6_FLOW:
1166a011952aSMichael Chan 		if (tuple == 2)
1167a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1168a011952aSMichael Chan 		else if (!tuple)
1169a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1170a011952aSMichael Chan 		break;
1171a011952aSMichael Chan 	}
1172a011952aSMichael Chan 
1173a011952aSMichael Chan 	if (bp->rss_hash_cfg == rss_hash_cfg)
1174a011952aSMichael Chan 		return 0;
1175a011952aSMichael Chan 
1176a011952aSMichael Chan 	bp->rss_hash_cfg = rss_hash_cfg;
1177a011952aSMichael Chan 	if (netif_running(bp->dev)) {
1178a011952aSMichael Chan 		bnxt_close_nic(bp, false, false);
1179a011952aSMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1180a011952aSMichael Chan 	}
1181a011952aSMichael Chan 	return rc;
1182a011952aSMichael Chan }
1183c0c050c5SMichael Chan 
1184c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1185c0c050c5SMichael Chan 			  u32 *rule_locs)
1186c0c050c5SMichael Chan {
1187c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1188c0c050c5SMichael Chan 	int rc = 0;
1189c0c050c5SMichael Chan 
1190c0c050c5SMichael Chan 	switch (cmd->cmd) {
1191a011952aSMichael Chan #ifdef CONFIG_RFS_ACCEL
1192c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
1193c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
1194c0c050c5SMichael Chan 		break;
1195c0c050c5SMichael Chan 
1196c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
1197c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
1198c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
1199c0c050c5SMichael Chan 		break;
1200c0c050c5SMichael Chan 
1201c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
1202c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
1203c0c050c5SMichael Chan 		break;
1204c0c050c5SMichael Chan 
1205c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
1206c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
1207c0c050c5SMichael Chan 		break;
1208a011952aSMichael Chan #endif
1209a011952aSMichael Chan 
1210a011952aSMichael Chan 	case ETHTOOL_GRXFH:
1211a011952aSMichael Chan 		rc = bnxt_grxfh(bp, cmd);
1212a011952aSMichael Chan 		break;
1213c0c050c5SMichael Chan 
1214c0c050c5SMichael Chan 	default:
1215c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
1216c0c050c5SMichael Chan 		break;
1217c0c050c5SMichael Chan 	}
1218c0c050c5SMichael Chan 
1219c0c050c5SMichael Chan 	return rc;
1220c0c050c5SMichael Chan }
1221a011952aSMichael Chan 
1222a011952aSMichael Chan static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1223a011952aSMichael Chan {
1224a011952aSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1225a011952aSMichael Chan 	int rc;
1226a011952aSMichael Chan 
1227a011952aSMichael Chan 	switch (cmd->cmd) {
1228a011952aSMichael Chan 	case ETHTOOL_SRXFH:
1229a011952aSMichael Chan 		rc = bnxt_srxfh(bp, cmd);
1230a011952aSMichael Chan 		break;
1231a011952aSMichael Chan 
1232a011952aSMichael Chan 	default:
1233a011952aSMichael Chan 		rc = -EOPNOTSUPP;
1234a011952aSMichael Chan 		break;
1235a011952aSMichael Chan 	}
1236a011952aSMichael Chan 	return rc;
1237a011952aSMichael Chan }
1238c0c050c5SMichael Chan 
1239b73c1d08SMichael Chan u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
1240c0c050c5SMichael Chan {
1241b73c1d08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1242b73c1d08SMichael Chan 
1243b73c1d08SMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1244b73c1d08SMichael Chan 		return ALIGN(bp->rx_nr_rings, BNXT_RSS_TABLE_ENTRIES_P5);
1245c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
1246c0c050c5SMichael Chan }
1247c0c050c5SMichael Chan 
1248c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
1249c0c050c5SMichael Chan {
1250c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
1251c0c050c5SMichael Chan }
1252c0c050c5SMichael Chan 
1253c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1254c0c050c5SMichael Chan 			 u8 *hfunc)
1255c0c050c5SMichael Chan {
1256c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
12577991cb9cSMichael Chan 	struct bnxt_vnic_info *vnic;
1258adc38ac6SMichael Chan 	u32 i, tbl_size;
1259c0c050c5SMichael Chan 
1260c0c050c5SMichael Chan 	if (hfunc)
1261c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
1262c0c050c5SMichael Chan 
12637991cb9cSMichael Chan 	if (!bp->vnic_info)
12647991cb9cSMichael Chan 		return 0;
12657991cb9cSMichael Chan 
12667991cb9cSMichael Chan 	vnic = &bp->vnic_info[0];
1267adc38ac6SMichael Chan 	if (indir && bp->rss_indir_tbl) {
1268adc38ac6SMichael Chan 		tbl_size = bnxt_get_rxfh_indir_size(dev);
1269adc38ac6SMichael Chan 		for (i = 0; i < tbl_size; i++)
1270adc38ac6SMichael Chan 			indir[i] = bp->rss_indir_tbl[i];
12717991cb9cSMichael Chan 	}
1272c0c050c5SMichael Chan 
12737991cb9cSMichael Chan 	if (key && vnic->rss_hash_key)
1274c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
1275c0c050c5SMichael Chan 
1276c0c050c5SMichael Chan 	return 0;
1277c0c050c5SMichael Chan }
1278c0c050c5SMichael Chan 
1279bd3191b5SMichael Chan static int bnxt_set_rxfh(struct net_device *dev, const u32 *indir,
1280bd3191b5SMichael Chan 			 const u8 *key, const u8 hfunc)
1281bd3191b5SMichael Chan {
1282bd3191b5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1283bd3191b5SMichael Chan 	int rc = 0;
1284bd3191b5SMichael Chan 
1285bd3191b5SMichael Chan 	if (hfunc && hfunc != ETH_RSS_HASH_TOP)
1286bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1287bd3191b5SMichael Chan 
1288bd3191b5SMichael Chan 	if (key)
1289bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1290bd3191b5SMichael Chan 
1291bd3191b5SMichael Chan 	if (indir) {
1292bd3191b5SMichael Chan 		u32 i, pad, tbl_size = bnxt_get_rxfh_indir_size(dev);
1293bd3191b5SMichael Chan 
1294bd3191b5SMichael Chan 		for (i = 0; i < tbl_size; i++)
1295bd3191b5SMichael Chan 			bp->rss_indir_tbl[i] = indir[i];
1296bd3191b5SMichael Chan 		pad = bp->rss_indir_tbl_entries - tbl_size;
1297bd3191b5SMichael Chan 		if (pad)
1298bd3191b5SMichael Chan 			memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
1299bd3191b5SMichael Chan 	}
1300bd3191b5SMichael Chan 
1301bd3191b5SMichael Chan 	if (netif_running(bp->dev)) {
1302bd3191b5SMichael Chan 		bnxt_close_nic(bp, false, false);
1303bd3191b5SMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1304bd3191b5SMichael Chan 	}
1305bd3191b5SMichael Chan 	return rc;
1306bd3191b5SMichael Chan }
1307bd3191b5SMichael Chan 
1308c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
1309c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
1310c0c050c5SMichael Chan {
1311c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1312c0c050c5SMichael Chan 
1313c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1314431aa1ebSMichael Chan 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
1315c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
13165c8227d0SMichael Chan 	info->n_stats = bnxt_get_num_stats(bp);
1317eb513658SMichael Chan 	info->testinfo_len = bp->num_tests;
1318c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
1319c0c050c5SMichael Chan 	info->eedump_len = 0;
1320c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
1321c0c050c5SMichael Chan 	info->regdump_len = 0;
1322c0c050c5SMichael Chan }
1323c0c050c5SMichael Chan 
1324b5d600b0SVasundhara Volam static int bnxt_get_regs_len(struct net_device *dev)
1325b5d600b0SVasundhara Volam {
1326b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1327b5d600b0SVasundhara Volam 	int reg_len;
1328b5d600b0SVasundhara Volam 
1329f0f47b2fSVasundhara Volam 	if (!BNXT_PF(bp))
1330f0f47b2fSVasundhara Volam 		return -EOPNOTSUPP;
1331f0f47b2fSVasundhara Volam 
1332b5d600b0SVasundhara Volam 	reg_len = BNXT_PXP_REG_LEN;
1333b5d600b0SVasundhara Volam 
1334b5d600b0SVasundhara Volam 	if (bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED)
1335b5d600b0SVasundhara Volam 		reg_len += sizeof(struct pcie_ctx_hw_stats);
1336b5d600b0SVasundhara Volam 
1337b5d600b0SVasundhara Volam 	return reg_len;
1338b5d600b0SVasundhara Volam }
1339b5d600b0SVasundhara Volam 
1340b5d600b0SVasundhara Volam static void bnxt_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1341b5d600b0SVasundhara Volam 			  void *_p)
1342b5d600b0SVasundhara Volam {
1343b5d600b0SVasundhara Volam 	struct pcie_ctx_hw_stats *hw_pcie_stats;
1344b5d600b0SVasundhara Volam 	struct hwrm_pcie_qstats_input req = {0};
1345b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1346b5d600b0SVasundhara Volam 	dma_addr_t hw_pcie_stats_addr;
1347b5d600b0SVasundhara Volam 	int rc;
1348b5d600b0SVasundhara Volam 
1349b5d600b0SVasundhara Volam 	regs->version = 0;
1350b5d600b0SVasundhara Volam 	bnxt_dbg_hwrm_rd_reg(bp, 0, BNXT_PXP_REG_LEN / 4, _p);
1351b5d600b0SVasundhara Volam 
1352b5d600b0SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED))
1353b5d600b0SVasundhara Volam 		return;
1354b5d600b0SVasundhara Volam 
1355b5d600b0SVasundhara Volam 	hw_pcie_stats = dma_alloc_coherent(&bp->pdev->dev,
1356b5d600b0SVasundhara Volam 					   sizeof(*hw_pcie_stats),
1357b5d600b0SVasundhara Volam 					   &hw_pcie_stats_addr, GFP_KERNEL);
1358b5d600b0SVasundhara Volam 	if (!hw_pcie_stats)
1359b5d600b0SVasundhara Volam 		return;
1360b5d600b0SVasundhara Volam 
1361b5d600b0SVasundhara Volam 	regs->version = 1;
1362b5d600b0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PCIE_QSTATS, -1, -1);
1363b5d600b0SVasundhara Volam 	req.pcie_stat_size = cpu_to_le16(sizeof(*hw_pcie_stats));
1364b5d600b0SVasundhara Volam 	req.pcie_stat_host_addr = cpu_to_le64(hw_pcie_stats_addr);
1365b5d600b0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
1366b5d600b0SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1367b5d600b0SVasundhara Volam 	if (!rc) {
1368b5d600b0SVasundhara Volam 		__le64 *src = (__le64 *)hw_pcie_stats;
1369b5d600b0SVasundhara Volam 		u64 *dst = (u64 *)(_p + BNXT_PXP_REG_LEN);
1370b5d600b0SVasundhara Volam 		int i;
1371b5d600b0SVasundhara Volam 
1372b5d600b0SVasundhara Volam 		for (i = 0; i < sizeof(*hw_pcie_stats) / sizeof(__le64); i++)
1373b5d600b0SVasundhara Volam 			dst[i] = le64_to_cpu(src[i]);
1374b5d600b0SVasundhara Volam 	}
1375b5d600b0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
1376b5d600b0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, sizeof(*hw_pcie_stats), hw_pcie_stats,
1377b5d600b0SVasundhara Volam 			  hw_pcie_stats_addr);
1378b5d600b0SVasundhara Volam }
1379b5d600b0SVasundhara Volam 
13808e202366SMichael Chan static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13818e202366SMichael Chan {
13828e202366SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
13838e202366SMichael Chan 
13848e202366SMichael Chan 	wol->supported = 0;
13858e202366SMichael Chan 	wol->wolopts = 0;
13868e202366SMichael Chan 	memset(&wol->sopass, 0, sizeof(wol->sopass));
13878e202366SMichael Chan 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
13888e202366SMichael Chan 		wol->supported = WAKE_MAGIC;
13898e202366SMichael Chan 		if (bp->wol)
13908e202366SMichael Chan 			wol->wolopts = WAKE_MAGIC;
13918e202366SMichael Chan 	}
13928e202366SMichael Chan }
13938e202366SMichael Chan 
13945282db6cSMichael Chan static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13955282db6cSMichael Chan {
13965282db6cSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
13975282db6cSMichael Chan 
13985282db6cSMichael Chan 	if (wol->wolopts & ~WAKE_MAGIC)
13995282db6cSMichael Chan 		return -EINVAL;
14005282db6cSMichael Chan 
14015282db6cSMichael Chan 	if (wol->wolopts & WAKE_MAGIC) {
14025282db6cSMichael Chan 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
14035282db6cSMichael Chan 			return -EINVAL;
14045282db6cSMichael Chan 		if (!bp->wol) {
14055282db6cSMichael Chan 			if (bnxt_hwrm_alloc_wol_fltr(bp))
14065282db6cSMichael Chan 				return -EBUSY;
14075282db6cSMichael Chan 			bp->wol = 1;
14085282db6cSMichael Chan 		}
14095282db6cSMichael Chan 	} else {
14105282db6cSMichael Chan 		if (bp->wol) {
14115282db6cSMichael Chan 			if (bnxt_hwrm_free_wol_fltr(bp))
14125282db6cSMichael Chan 				return -EBUSY;
14135282db6cSMichael Chan 			bp->wol = 0;
14145282db6cSMichael Chan 		}
14155282db6cSMichael Chan 	}
14165282db6cSMichael Chan 	return 0;
14175282db6cSMichael Chan }
14185282db6cSMichael Chan 
1419170ce013SMichael Chan u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
1420c0c050c5SMichael Chan {
1421c0c050c5SMichael Chan 	u32 speed_mask = 0;
1422c0c050c5SMichael Chan 
1423c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
1424c0c050c5SMichael Chan 	/* set the advertised speeds */
1425c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1426c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
1427c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1428c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
1429c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1430c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
1431c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1432c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
1433c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
14341c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
143527c4d578SMichael Chan 
143627c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
143727c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
143827c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
143927c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
144027c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
144127c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
144227c4d578SMichael Chan 
1443c0c050c5SMichael Chan 	return speed_mask;
1444c0c050c5SMichael Chan }
1445c0c050c5SMichael Chan 
144600c04a92SMichael Chan #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
144700c04a92SMichael Chan {									\
144800c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
144900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145000c04a92SMichael Chan 						     100baseT_Full);	\
145100c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
145200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145300c04a92SMichael Chan 						     1000baseT_Full);	\
145400c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
145500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145600c04a92SMichael Chan 						     10000baseT_Full);	\
145700c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
145800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145900c04a92SMichael Chan 						     25000baseCR_Full);	\
146000c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
146100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146200c04a92SMichael Chan 						     40000baseCR4_Full);\
146300c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
146400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146500c04a92SMichael Chan 						     50000baseCR2_Full);\
146638a21b34SDeepak Khungar 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
146738a21b34SDeepak Khungar 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146838a21b34SDeepak Khungar 						     100000baseCR4_Full);\
146900c04a92SMichael Chan 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
147000c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147100c04a92SMichael Chan 						     Pause);		\
147200c04a92SMichael Chan 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
147300c04a92SMichael Chan 			ethtool_link_ksettings_add_link_mode(		\
147400c04a92SMichael Chan 					lk_ksettings, name, Asym_Pause);\
147500c04a92SMichael Chan 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
147600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147700c04a92SMichael Chan 						     Asym_Pause);	\
147800c04a92SMichael Chan 	}								\
147900c04a92SMichael Chan }
148000c04a92SMichael Chan 
148100c04a92SMichael Chan #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
148200c04a92SMichael Chan {									\
148300c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
148400c04a92SMichael Chan 						  100baseT_Full) ||	\
148500c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
148600c04a92SMichael Chan 						  100baseT_Half))	\
148700c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
148800c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
148900c04a92SMichael Chan 						  1000baseT_Full) ||	\
149000c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149100c04a92SMichael Chan 						  1000baseT_Half))	\
149200c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
149300c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149400c04a92SMichael Chan 						  10000baseT_Full))	\
149500c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
149600c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149700c04a92SMichael Chan 						  25000baseCR_Full))	\
149800c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
149900c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
150000c04a92SMichael Chan 						  40000baseCR4_Full))	\
150100c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
150200c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
150300c04a92SMichael Chan 						  50000baseCR2_Full))	\
150400c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
150538a21b34SDeepak Khungar 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
150638a21b34SDeepak Khungar 						  100000baseCR4_Full))	\
150738a21b34SDeepak Khungar 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
150800c04a92SMichael Chan }
150900c04a92SMichael Chan 
151000c04a92SMichael Chan static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
151100c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
151227c4d578SMichael Chan {
151368515a18SMichael Chan 	u16 fw_speeds = link_info->advertising;
151427c4d578SMichael Chan 	u8 fw_pause = 0;
151527c4d578SMichael Chan 
151627c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
151727c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
151827c4d578SMichael Chan 
151900c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
152027c4d578SMichael Chan }
152127c4d578SMichael Chan 
152200c04a92SMichael Chan static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
152300c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15243277360eSMichael Chan {
15253277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
15263277360eSMichael Chan 	u8 fw_pause = 0;
15273277360eSMichael Chan 
15283277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
15293277360eSMichael Chan 		fw_pause = link_info->lp_pause;
15303277360eSMichael Chan 
153100c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
153200c04a92SMichael Chan 				lp_advertising);
15333277360eSMichael Chan }
15343277360eSMichael Chan 
153500c04a92SMichael Chan static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
153600c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15374b32caccSMichael Chan {
15384b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
15394b32caccSMichael Chan 
154000c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
15414b32caccSMichael Chan 
154200c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
154300c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
154400c04a92SMichael Chan 					     Asym_Pause);
154593ed8117SMichael Chan 
154600c04a92SMichael Chan 	if (link_info->support_auto_speeds)
154700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
154800c04a92SMichael Chan 						     Autoneg);
154993ed8117SMichael Chan }
155093ed8117SMichael Chan 
1551c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1552c0c050c5SMichael Chan {
1553c0c050c5SMichael Chan 	switch (fw_link_speed) {
1554c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
1555c0c050c5SMichael Chan 		return SPEED_100;
1556c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
1557c0c050c5SMichael Chan 		return SPEED_1000;
1558c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
1559c0c050c5SMichael Chan 		return SPEED_2500;
1560c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
1561c0c050c5SMichael Chan 		return SPEED_10000;
1562c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
1563c0c050c5SMichael Chan 		return SPEED_20000;
1564c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
1565c0c050c5SMichael Chan 		return SPEED_25000;
1566c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
1567c0c050c5SMichael Chan 		return SPEED_40000;
1568c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
1569c0c050c5SMichael Chan 		return SPEED_50000;
157038a21b34SDeepak Khungar 	case BNXT_LINK_SPEED_100GB:
157138a21b34SDeepak Khungar 		return SPEED_100000;
1572c0c050c5SMichael Chan 	default:
1573c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
1574c0c050c5SMichael Chan 	}
1575c0c050c5SMichael Chan }
1576c0c050c5SMichael Chan 
157700c04a92SMichael Chan static int bnxt_get_link_ksettings(struct net_device *dev,
157800c04a92SMichael Chan 				   struct ethtool_link_ksettings *lk_ksettings)
1579c0c050c5SMichael Chan {
1580c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1581c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
158200c04a92SMichael Chan 	struct ethtool_link_settings *base = &lk_ksettings->base;
158300c04a92SMichael Chan 	u32 ethtool_speed;
1584c0c050c5SMichael Chan 
158500c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1586e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
158700c04a92SMichael Chan 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1588c0c050c5SMichael Chan 
158900c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1590b763499eSMichael Chan 	if (link_info->autoneg) {
159100c04a92SMichael Chan 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
159200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
159300c04a92SMichael Chan 						     advertising, Autoneg);
159400c04a92SMichael Chan 		base->autoneg = AUTONEG_ENABLE;
159500c04a92SMichael Chan 		base->duplex = DUPLEX_UNKNOWN;
159683d8f5e9SMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK) {
159783d8f5e9SMichael Chan 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
159883d8f5e9SMichael Chan 			if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
159900c04a92SMichael Chan 				base->duplex = DUPLEX_FULL;
160029c262feSMichael Chan 			else
160100c04a92SMichael Chan 				base->duplex = DUPLEX_HALF;
160283d8f5e9SMichael Chan 		}
160383d8f5e9SMichael Chan 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1604c0c050c5SMichael Chan 	} else {
160500c04a92SMichael Chan 		base->autoneg = AUTONEG_DISABLE;
160629c262feSMichael Chan 		ethtool_speed =
160729c262feSMichael Chan 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
160800c04a92SMichael Chan 		base->duplex = DUPLEX_HALF;
160929c262feSMichael Chan 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
161000c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
1611c0c050c5SMichael Chan 	}
161200c04a92SMichael Chan 	base->speed = ethtool_speed;
1613c0c050c5SMichael Chan 
161400c04a92SMichael Chan 	base->port = PORT_NONE;
1615c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
161600c04a92SMichael Chan 		base->port = PORT_TP;
161700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
161800c04a92SMichael Chan 						     TP);
161900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
162000c04a92SMichael Chan 						     TP);
1621c0c050c5SMichael Chan 	} else {
162200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
162300c04a92SMichael Chan 						     FIBRE);
162400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
162500c04a92SMichael Chan 						     FIBRE);
1626c0c050c5SMichael Chan 
1627c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
162800c04a92SMichael Chan 			base->port = PORT_DA;
1629c0c050c5SMichael Chan 		else if (link_info->media_type ==
1630c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
163100c04a92SMichael Chan 			base->port = PORT_FIBRE;
1632c0c050c5SMichael Chan 	}
163300c04a92SMichael Chan 	base->phy_address = link_info->phy_addr;
1634e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1635c0c050c5SMichael Chan 
1636c0c050c5SMichael Chan 	return 0;
1637c0c050c5SMichael Chan }
1638c0c050c5SMichael Chan 
163938a21b34SDeepak Khungar static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed)
1640c0c050c5SMichael Chan {
16419d9cee08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
16429d9cee08SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
16439d9cee08SMichael Chan 	u16 support_spds = link_info->support_speeds;
16449d9cee08SMichael Chan 	u32 fw_speed = 0;
16459d9cee08SMichael Chan 
1646c0c050c5SMichael Chan 	switch (ethtool_speed) {
1647c0c050c5SMichael Chan 	case SPEED_100:
16489d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
16499d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
16509d9cee08SMichael Chan 		break;
1651c0c050c5SMichael Chan 	case SPEED_1000:
16529d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
16539d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
16549d9cee08SMichael Chan 		break;
1655c0c050c5SMichael Chan 	case SPEED_2500:
16569d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
16579d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
16589d9cee08SMichael Chan 		break;
1659c0c050c5SMichael Chan 	case SPEED_10000:
16609d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
16619d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
16629d9cee08SMichael Chan 		break;
1663c0c050c5SMichael Chan 	case SPEED_20000:
16649d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
16659d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
16669d9cee08SMichael Chan 		break;
1667c0c050c5SMichael Chan 	case SPEED_25000:
16689d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
16699d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
16709d9cee08SMichael Chan 		break;
1671c0c050c5SMichael Chan 	case SPEED_40000:
16729d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
16739d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
16749d9cee08SMichael Chan 		break;
1675c0c050c5SMichael Chan 	case SPEED_50000:
16769d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
16779d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
16789d9cee08SMichael Chan 		break;
167938a21b34SDeepak Khungar 	case SPEED_100000:
168038a21b34SDeepak Khungar 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB)
168138a21b34SDeepak Khungar 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB;
168238a21b34SDeepak Khungar 		break;
1683c0c050c5SMichael Chan 	default:
1684c0c050c5SMichael Chan 		netdev_err(dev, "unsupported speed!\n");
1685c0c050c5SMichael Chan 		break;
1686c0c050c5SMichael Chan 	}
16879d9cee08SMichael Chan 	return fw_speed;
1688c0c050c5SMichael Chan }
1689c0c050c5SMichael Chan 
1690939f7f0cSMichael Chan u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1691c0c050c5SMichael Chan {
1692c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
1693c0c050c5SMichael Chan 
1694c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
1695c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
1696c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
1697c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1698c0c050c5SMichael Chan 	}
1699c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
1700c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
1701c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1702c0c050c5SMichael Chan 	}
1703c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
1704c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1705c0c050c5SMichael Chan 
17061c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
17071c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
17081c49c421SMichael Chan 
1709c0c050c5SMichael Chan 	return fw_speed_mask;
1710c0c050c5SMichael Chan }
1711c0c050c5SMichael Chan 
171200c04a92SMichael Chan static int bnxt_set_link_ksettings(struct net_device *dev,
171300c04a92SMichael Chan 			   const struct ethtool_link_ksettings *lk_ksettings)
1714c0c050c5SMichael Chan {
1715c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1716c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
171700c04a92SMichael Chan 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1718c0c050c5SMichael Chan 	bool set_pause = false;
171968515a18SMichael Chan 	u16 fw_advertising = 0;
172068515a18SMichael Chan 	u32 speed;
172100c04a92SMichael Chan 	int rc = 0;
1722c0c050c5SMichael Chan 
1723c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
172400c04a92SMichael Chan 		return -EOPNOTSUPP;
1725c0c050c5SMichael Chan 
1726e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
172700c04a92SMichael Chan 	if (base->autoneg == AUTONEG_ENABLE) {
172800c04a92SMichael Chan 		BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
172900c04a92SMichael Chan 					advertising);
1730c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1731c0c050c5SMichael Chan 		if (!fw_advertising)
173293ed8117SMichael Chan 			link_info->advertising = link_info->support_auto_speeds;
1733c0c050c5SMichael Chan 		else
1734c0c050c5SMichael Chan 			link_info->advertising = fw_advertising;
1735c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
1736c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
1737c0c050c5SMichael Chan 		 */
1738c0c050c5SMichael Chan 		set_pause = true;
1739c0c050c5SMichael Chan 	} else {
17409d9cee08SMichael Chan 		u16 fw_speed;
174103efbec0SMichael Chan 		u8 phy_type = link_info->phy_type;
17429d9cee08SMichael Chan 
174303efbec0SMichael Chan 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
174403efbec0SMichael Chan 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
174503efbec0SMichael Chan 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
174603efbec0SMichael Chan 			netdev_err(dev, "10GBase-T devices must autoneg\n");
174703efbec0SMichael Chan 			rc = -EINVAL;
174803efbec0SMichael Chan 			goto set_setting_exit;
174903efbec0SMichael Chan 		}
175000c04a92SMichael Chan 		if (base->duplex == DUPLEX_HALF) {
1751c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1752c0c050c5SMichael Chan 			rc = -EINVAL;
1753c0c050c5SMichael Chan 			goto set_setting_exit;
1754c0c050c5SMichael Chan 		}
175500c04a92SMichael Chan 		speed = base->speed;
17569d9cee08SMichael Chan 		fw_speed = bnxt_get_fw_speed(dev, speed);
17579d9cee08SMichael Chan 		if (!fw_speed) {
17589d9cee08SMichael Chan 			rc = -EINVAL;
17599d9cee08SMichael Chan 			goto set_setting_exit;
17609d9cee08SMichael Chan 		}
17619d9cee08SMichael Chan 		link_info->req_link_speed = fw_speed;
1762c0c050c5SMichael Chan 		link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1763b763499eSMichael Chan 		link_info->autoneg = 0;
1764c0c050c5SMichael Chan 		link_info->advertising = 0;
1765c0c050c5SMichael Chan 	}
1766c0c050c5SMichael Chan 
1767c0c050c5SMichael Chan 	if (netif_running(dev))
1768939f7f0cSMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1769c0c050c5SMichael Chan 
1770c0c050c5SMichael Chan set_setting_exit:
1771e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1772c0c050c5SMichael Chan 	return rc;
1773c0c050c5SMichael Chan }
1774c0c050c5SMichael Chan 
1775c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
1776c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
1777c0c050c5SMichael Chan {
1778c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1779c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1780c0c050c5SMichael Chan 
1781c0c050c5SMichael Chan 	if (BNXT_VF(bp))
1782c0c050c5SMichael Chan 		return;
1783b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
17843c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
17853c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1786c0c050c5SMichael Chan }
1787c0c050c5SMichael Chan 
1788423cffcfSJakub Kicinski static void bnxt_get_pause_stats(struct net_device *dev,
1789423cffcfSJakub Kicinski 				 struct ethtool_pause_stats *epstat)
1790423cffcfSJakub Kicinski {
1791423cffcfSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
1792423cffcfSJakub Kicinski 	u64 *rx, *tx;
1793423cffcfSJakub Kicinski 
1794423cffcfSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS))
1795423cffcfSJakub Kicinski 		return;
1796423cffcfSJakub Kicinski 
1797423cffcfSJakub Kicinski 	rx = bp->port_stats.sw_stats;
1798423cffcfSJakub Kicinski 	tx = bp->port_stats.sw_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
1799423cffcfSJakub Kicinski 
1800423cffcfSJakub Kicinski 	epstat->rx_pause_frames = BNXT_GET_RX_PORT_STATS64(rx, rx_pause_frames);
1801423cffcfSJakub Kicinski 	epstat->tx_pause_frames = BNXT_GET_TX_PORT_STATS64(tx, tx_pause_frames);
1802423cffcfSJakub Kicinski }
1803423cffcfSJakub Kicinski 
1804c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
1805c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
1806c0c050c5SMichael Chan {
1807c0c050c5SMichael Chan 	int rc = 0;
1808c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1809c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1810c0c050c5SMichael Chan 
1811c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
181275362a3fSMichael Chan 		return -EOPNOTSUPP;
1813c0c050c5SMichael Chan 
1814a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
1815c0c050c5SMichael Chan 	if (epause->autoneg) {
1816a5390690SMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
1817a5390690SMichael Chan 			rc = -EINVAL;
1818a5390690SMichael Chan 			goto pause_exit;
1819a5390690SMichael Chan 		}
1820b763499eSMichael Chan 
1821c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1822c9ee9516SMichael Chan 		if (bp->hwrm_spec_code >= 0x10201)
1823c9ee9516SMichael Chan 			link_info->req_flow_ctrl =
1824c9ee9516SMichael Chan 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1825c0c050c5SMichael Chan 	} else {
1826c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
1827c0c050c5SMichael Chan 		 * force a link change
1828c0c050c5SMichael Chan 		 */
1829c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1830c0c050c5SMichael Chan 			link_info->force_link_chng = true;
1831c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1832c9ee9516SMichael Chan 		link_info->req_flow_ctrl = 0;
1833c0c050c5SMichael Chan 	}
1834c0c050c5SMichael Chan 	if (epause->rx_pause)
1835c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1836c0c050c5SMichael Chan 
1837c0c050c5SMichael Chan 	if (epause->tx_pause)
1838c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1839c0c050c5SMichael Chan 
1840a5390690SMichael Chan 	if (netif_running(dev))
1841c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
1842a5390690SMichael Chan 
1843a5390690SMichael Chan pause_exit:
1844163e9ef6SVasundhara Volam 	mutex_unlock(&bp->link_lock);
1845c0c050c5SMichael Chan 	return rc;
1846c0c050c5SMichael Chan }
1847c0c050c5SMichael Chan 
1848c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
1849c0c050c5SMichael Chan {
1850c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1851c0c050c5SMichael Chan 
1852c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
1853c0c050c5SMichael Chan 	return bp->link_info.link_up;
1854c0c050c5SMichael Chan }
1855c0c050c5SMichael Chan 
1856b3b0ddd0SMichael Chan static void bnxt_print_admin_err(struct bnxt *bp)
1857b3b0ddd0SMichael Chan {
1858b3b0ddd0SMichael Chan 	netdev_info(bp->dev, "PF does not have admin privileges to flash or reset the device\n");
1859b3b0ddd0SMichael Chan }
1860b3b0ddd0SMichael Chan 
18615ac67d8bSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
18625ac67d8bSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
18635ac67d8bSRob Swindell 				u32 *data_length);
18645ac67d8bSRob Swindell 
1865c0c050c5SMichael Chan static int bnxt_flash_nvram(struct net_device *dev,
1866c0c050c5SMichael Chan 			    u16 dir_type,
1867c0c050c5SMichael Chan 			    u16 dir_ordinal,
1868c0c050c5SMichael Chan 			    u16 dir_ext,
1869c0c050c5SMichael Chan 			    u16 dir_attr,
1870c0c050c5SMichael Chan 			    const u8 *data,
1871c0c050c5SMichael Chan 			    size_t data_len)
1872c0c050c5SMichael Chan {
1873c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1874c0c050c5SMichael Chan 	int rc;
1875c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
1876c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1877c0c050c5SMichael Chan 	u8 *kmem;
1878c0c050c5SMichael Chan 
1879c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1880c0c050c5SMichael Chan 
1881c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
1882c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
1883c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
1884c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
1885c0c050c5SMichael Chan 	req.dir_data_length = cpu_to_le32(data_len);
1886c0c050c5SMichael Chan 
1887c0c050c5SMichael Chan 	kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1888c0c050c5SMichael Chan 				  GFP_KERNEL);
1889c0c050c5SMichael Chan 	if (!kmem) {
1890c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1891c0c050c5SMichael Chan 			   (unsigned)data_len);
1892c0c050c5SMichael Chan 		return -ENOMEM;
1893c0c050c5SMichael Chan 	}
1894c0c050c5SMichael Chan 	memcpy(kmem, data, data_len);
1895c0c050c5SMichael Chan 	req.host_src_addr = cpu_to_le64(dma_handle);
1896c0c050c5SMichael Chan 
1897c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1898c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1899c0c050c5SMichael Chan 
1900d4f1420dSMichael Chan 	if (rc == -EACCES)
1901b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
1902c0c050c5SMichael Chan 	return rc;
1903c0c050c5SMichael Chan }
1904c0c050c5SMichael Chan 
190595fec034SEdwin Peer static int bnxt_hwrm_firmware_reset(struct net_device *dev, u8 proc_type,
190695fec034SEdwin Peer 				    u8 self_reset, u8 flags)
1907d2d6318cSRob Swindell {
1908d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
19097c675421SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
19107c675421SVasundhara Volam 	int rc;
1911d2d6318cSRob Swindell 
1912d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1913d2d6318cSRob Swindell 
191495fec034SEdwin Peer 	req.embedded_proc_type = proc_type;
191595fec034SEdwin Peer 	req.selfrst_status = self_reset;
191695fec034SEdwin Peer 	req.flags = flags;
191795fec034SEdwin Peer 
19188cec0940SEdwin Peer 	if (proc_type == FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP) {
19198cec0940SEdwin Peer 		rc = hwrm_send_message_silent(bp, &req, sizeof(req),
19208cec0940SEdwin Peer 					      HWRM_CMD_TIMEOUT);
19218cec0940SEdwin Peer 	} else {
192295fec034SEdwin Peer 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
192395fec034SEdwin Peer 		if (rc == -EACCES)
192495fec034SEdwin Peer 			bnxt_print_admin_err(bp);
19258cec0940SEdwin Peer 	}
192695fec034SEdwin Peer 	return rc;
192795fec034SEdwin Peer }
192895fec034SEdwin Peer 
192994f17e89SEdwin Peer static int bnxt_firmware_reset(struct net_device *dev,
193094f17e89SEdwin Peer 			       enum bnxt_nvm_directory_type dir_type)
193195fec034SEdwin Peer {
193295fec034SEdwin Peer 	u8 self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE;
193395fec034SEdwin Peer 	u8 proc_type, flags = 0;
193495fec034SEdwin Peer 
1935d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1936d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
1937d2d6318cSRob Swindell 	switch (dir_type) {
1938d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
1939d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
1940d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
194195fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1942d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
194395fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1944d2d6318cSRob Swindell 		break;
1945d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
1946d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
194795fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
194808141e0bSRob Swindell 		/* Self-reset APE upon next PCIe reset: */
194995fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1950d2d6318cSRob Swindell 		break;
1951d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
1952d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
195395fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1954d2d6318cSRob Swindell 		break;
1955d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
1956d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
195795fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1958d2d6318cSRob Swindell 		break;
1959d2d6318cSRob Swindell 	default:
1960d2d6318cSRob Swindell 		return -EINVAL;
1961d2d6318cSRob Swindell 	}
1962d2d6318cSRob Swindell 
196395fec034SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, proc_type, self_reset, flags);
1964d2d6318cSRob Swindell }
1965d2d6318cSRob Swindell 
196694f17e89SEdwin Peer static int bnxt_firmware_reset_chip(struct net_device *dev)
196794f17e89SEdwin Peer {
196894f17e89SEdwin Peer 	struct bnxt *bp = netdev_priv(dev);
196994f17e89SEdwin Peer 	u8 flags = 0;
197094f17e89SEdwin Peer 
197194f17e89SEdwin Peer 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
197294f17e89SEdwin Peer 		flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
197394f17e89SEdwin Peer 
197494f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev,
197594f17e89SEdwin Peer 					FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP,
197694f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP,
197794f17e89SEdwin Peer 					flags);
197894f17e89SEdwin Peer }
197994f17e89SEdwin Peer 
198094f17e89SEdwin Peer static int bnxt_firmware_reset_ap(struct net_device *dev)
198194f17e89SEdwin Peer {
198294f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP,
198394f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE,
198494f17e89SEdwin Peer 					0);
198594f17e89SEdwin Peer }
198694f17e89SEdwin Peer 
1987c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
1988c0c050c5SMichael Chan 			       u16 dir_type,
1989c0c050c5SMichael Chan 			       const u8 *fw_data,
1990c0c050c5SMichael Chan 			       size_t fw_size)
1991c0c050c5SMichael Chan {
1992c0c050c5SMichael Chan 	int	rc = 0;
1993c0c050c5SMichael Chan 	u16	code_type;
1994c0c050c5SMichael Chan 	u32	stored_crc;
1995c0c050c5SMichael Chan 	u32	calculated_crc;
1996c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1997c0c050c5SMichael Chan 
1998c0c050c5SMichael Chan 	switch (dir_type) {
1999c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2000c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2001c0c050c5SMichael Chan 		code_type = CODE_BOOT;
2002c0c050c5SMichael Chan 		break;
200393e0b4feSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
200493e0b4feSRob Swindell 		code_type = CODE_CHIMP_PATCH;
200593e0b4feSRob Swindell 		break;
20062731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
20072731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
20082731d70fSRob Swindell 		break;
200993e0b4feSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
201093e0b4feSRob Swindell 		code_type = CODE_APE_PATCH;
201193e0b4feSRob Swindell 		break;
201293e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
201393e0b4feSRob Swindell 		code_type = CODE_KONG_FW;
201493e0b4feSRob Swindell 		break;
201593e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
201693e0b4feSRob Swindell 		code_type = CODE_KONG_PATCH;
201793e0b4feSRob Swindell 		break;
201893e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
201993e0b4feSRob Swindell 		code_type = CODE_BONO_FW;
202093e0b4feSRob Swindell 		break;
202193e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
202293e0b4feSRob Swindell 		code_type = CODE_BONO_PATCH;
202393e0b4feSRob Swindell 		break;
2024c0c050c5SMichael Chan 	default:
2025c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
2026c0c050c5SMichael Chan 			   dir_type);
2027c0c050c5SMichael Chan 		return -EINVAL;
2028c0c050c5SMichael Chan 	}
2029c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
2030c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
2031c0c050c5SMichael Chan 			   (unsigned int)fw_size);
2032c0c050c5SMichael Chan 		return -EINVAL;
2033c0c050c5SMichael Chan 	}
2034c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
2035c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
2036c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
2037c0c050c5SMichael Chan 		return -EINVAL;
2038c0c050c5SMichael Chan 	}
2039c0c050c5SMichael Chan 	if (header->code_type != code_type) {
2040c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
2041c0c050c5SMichael Chan 			   code_type, header->code_type);
2042c0c050c5SMichael Chan 		return -EINVAL;
2043c0c050c5SMichael Chan 	}
2044c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
2045c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
2046c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
2047c0c050c5SMichael Chan 		return -EINVAL;
2048c0c050c5SMichael Chan 	}
2049c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
2050c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
2051c0c050c5SMichael Chan 					     sizeof(stored_crc)));
2052c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
2053c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
2054c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
2055c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
2056c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
2057c0c050c5SMichael Chan 		return -EINVAL;
2058c0c050c5SMichael Chan 	}
2059c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2060c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
2061d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
2062d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
2063d2d6318cSRob Swindell 
2064c0c050c5SMichael Chan 	return rc;
2065c0c050c5SMichael Chan }
2066c0c050c5SMichael Chan 
20675ac67d8bSRob Swindell static int bnxt_flash_microcode(struct net_device *dev,
20685ac67d8bSRob Swindell 				u16 dir_type,
20695ac67d8bSRob Swindell 				const u8 *fw_data,
20705ac67d8bSRob Swindell 				size_t fw_size)
20715ac67d8bSRob Swindell {
20725ac67d8bSRob Swindell 	struct bnxt_ucode_trailer *trailer;
20735ac67d8bSRob Swindell 	u32 calculated_crc;
20745ac67d8bSRob Swindell 	u32 stored_crc;
20755ac67d8bSRob Swindell 	int rc = 0;
20765ac67d8bSRob Swindell 
20775ac67d8bSRob Swindell 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
20785ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode file size: %u\n",
20795ac67d8bSRob Swindell 			   (unsigned int)fw_size);
20805ac67d8bSRob Swindell 		return -EINVAL;
20815ac67d8bSRob Swindell 	}
20825ac67d8bSRob Swindell 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
20835ac67d8bSRob Swindell 						sizeof(*trailer)));
20845ac67d8bSRob Swindell 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
20855ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
20865ac67d8bSRob Swindell 			   le32_to_cpu(trailer->sig));
20875ac67d8bSRob Swindell 		return -EINVAL;
20885ac67d8bSRob Swindell 	}
20895ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
20905ac67d8bSRob Swindell 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
20915ac67d8bSRob Swindell 			   dir_type, le16_to_cpu(trailer->dir_type));
20925ac67d8bSRob Swindell 		return -EINVAL;
20935ac67d8bSRob Swindell 	}
20945ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->trailer_length) <
20955ac67d8bSRob Swindell 		sizeof(struct bnxt_ucode_trailer)) {
20965ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
20975ac67d8bSRob Swindell 			   le16_to_cpu(trailer->trailer_length));
20985ac67d8bSRob Swindell 		return -EINVAL;
20995ac67d8bSRob Swindell 	}
21005ac67d8bSRob Swindell 
21015ac67d8bSRob Swindell 	/* Confirm the CRC32 checksum of the file: */
21025ac67d8bSRob Swindell 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
21035ac67d8bSRob Swindell 					     sizeof(stored_crc)));
21045ac67d8bSRob Swindell 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
21055ac67d8bSRob Swindell 	if (calculated_crc != stored_crc) {
21065ac67d8bSRob Swindell 		netdev_err(dev,
21075ac67d8bSRob Swindell 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
21085ac67d8bSRob Swindell 			   (unsigned long)stored_crc,
21095ac67d8bSRob Swindell 			   (unsigned long)calculated_crc);
21105ac67d8bSRob Swindell 		return -EINVAL;
21115ac67d8bSRob Swindell 	}
21125ac67d8bSRob Swindell 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
21135ac67d8bSRob Swindell 			      0, 0, fw_data, fw_size);
21145ac67d8bSRob Swindell 
21155ac67d8bSRob Swindell 	return rc;
21165ac67d8bSRob Swindell }
21175ac67d8bSRob Swindell 
2118c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
2119c0c050c5SMichael Chan {
2120c0c050c5SMichael Chan 	switch (dir_type) {
2121c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
2122c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2123c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2124c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
2125c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
2126c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
2127c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
212893e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
212993e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
2130c0c050c5SMichael Chan 		return true;
2131c0c050c5SMichael Chan 	}
2132c0c050c5SMichael Chan 
2133c0c050c5SMichael Chan 	return false;
2134c0c050c5SMichael Chan }
2135c0c050c5SMichael Chan 
21365ac67d8bSRob Swindell static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
2137c0c050c5SMichael Chan {
2138c0c050c5SMichael Chan 	switch (dir_type) {
2139c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
2140c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
2141c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
2142c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
2143c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
2144c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
2145c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
2146c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
2147c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
2148c0c050c5SMichael Chan 		return true;
2149c0c050c5SMichael Chan 	}
2150c0c050c5SMichael Chan 
2151c0c050c5SMichael Chan 	return false;
2152c0c050c5SMichael Chan }
2153c0c050c5SMichael Chan 
2154c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
2155c0c050c5SMichael Chan {
2156c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
21575ac67d8bSRob Swindell 		bnxt_dir_type_is_other_exec_format(dir_type);
2158c0c050c5SMichael Chan }
2159c0c050c5SMichael Chan 
2160c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
2161c0c050c5SMichael Chan 					 u16 dir_type,
2162c0c050c5SMichael Chan 					 const char *filename)
2163c0c050c5SMichael Chan {
2164c0c050c5SMichael Chan 	const struct firmware  *fw;
2165c0c050c5SMichael Chan 	int			rc;
2166c0c050c5SMichael Chan 
2167c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
2168c0c050c5SMichael Chan 	if (rc != 0) {
2169c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
2170c0c050c5SMichael Chan 			   rc, filename);
2171c0c050c5SMichael Chan 		return rc;
2172c0c050c5SMichael Chan 	}
2173ba425800SJason Yan 	if (bnxt_dir_type_is_ape_bin_format(dir_type))
2174c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
2175ba425800SJason Yan 	else if (bnxt_dir_type_is_other_exec_format(dir_type))
21765ac67d8bSRob Swindell 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
2177c0c050c5SMichael Chan 	else
2178c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2179c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
2180c0c050c5SMichael Chan 	release_firmware(fw);
2181c0c050c5SMichael Chan 	return rc;
2182c0c050c5SMichael Chan }
2183c0c050c5SMichael Chan 
2184d168f328SVasundhara Volam int bnxt_flash_package_from_file(struct net_device *dev, const char *filename,
2185d168f328SVasundhara Volam 				 u32 install_type)
2186c0c050c5SMichael Chan {
21875ac67d8bSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
21885ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
21895ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_input install = {0};
21905ac67d8bSRob Swindell 	const struct firmware *fw;
21915ac67d8bSRob Swindell 	u32 item_len;
219222630e28SEdwin Peer 	int rc = 0;
21935ac67d8bSRob Swindell 	u16 index;
21945ac67d8bSRob Swindell 
21955ac67d8bSRob Swindell 	bnxt_hwrm_fw_set_time(bp);
21965ac67d8bSRob Swindell 
219795ec1f47SVasundhara Volam 	rc = bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
21985ac67d8bSRob Swindell 				  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
219995ec1f47SVasundhara Volam 				  &index, &item_len, NULL);
220095ec1f47SVasundhara Volam 	if (rc) {
22015ac67d8bSRob Swindell 		netdev_err(dev, "PKG update area not created in nvram\n");
220295ec1f47SVasundhara Volam 		return rc;
22035ac67d8bSRob Swindell 	}
22045ac67d8bSRob Swindell 
22055ac67d8bSRob Swindell 	rc = request_firmware(&fw, filename, &dev->dev);
22065ac67d8bSRob Swindell 	if (rc != 0) {
22075ac67d8bSRob Swindell 		netdev_err(dev, "PKG error %d requesting file: %s\n",
22085ac67d8bSRob Swindell 			   rc, filename);
22095ac67d8bSRob Swindell 		return rc;
22105ac67d8bSRob Swindell 	}
22115ac67d8bSRob Swindell 
22125ac67d8bSRob Swindell 	if (fw->size > item_len) {
22139a005c38SJonathan Lemon 		netdev_err(dev, "PKG insufficient update area in nvram: %lu\n",
22145ac67d8bSRob Swindell 			   (unsigned long)fw->size);
22155ac67d8bSRob Swindell 		rc = -EFBIG;
22165ac67d8bSRob Swindell 	} else {
22175ac67d8bSRob Swindell 		dma_addr_t dma_handle;
22185ac67d8bSRob Swindell 		u8 *kmem;
22195ac67d8bSRob Swindell 		struct hwrm_nvm_modify_input modify = {0};
22205ac67d8bSRob Swindell 
22215ac67d8bSRob Swindell 		bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
22225ac67d8bSRob Swindell 
22235ac67d8bSRob Swindell 		modify.dir_idx = cpu_to_le16(index);
22245ac67d8bSRob Swindell 		modify.len = cpu_to_le32(fw->size);
22255ac67d8bSRob Swindell 
22265ac67d8bSRob Swindell 		kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
22275ac67d8bSRob Swindell 					  &dma_handle, GFP_KERNEL);
22285ac67d8bSRob Swindell 		if (!kmem) {
22295ac67d8bSRob Swindell 			netdev_err(dev,
22305ac67d8bSRob Swindell 				   "dma_alloc_coherent failure, length = %u\n",
22315ac67d8bSRob Swindell 				   (unsigned int)fw->size);
22325ac67d8bSRob Swindell 			rc = -ENOMEM;
22335ac67d8bSRob Swindell 		} else {
22345ac67d8bSRob Swindell 			memcpy(kmem, fw->data, fw->size);
22355ac67d8bSRob Swindell 			modify.host_src_addr = cpu_to_le64(dma_handle);
22365ac67d8bSRob Swindell 
223722630e28SEdwin Peer 			rc = hwrm_send_message(bp, &modify, sizeof(modify),
22385ac67d8bSRob Swindell 					       FLASH_PACKAGE_TIMEOUT);
22395ac67d8bSRob Swindell 			dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
22405ac67d8bSRob Swindell 					  dma_handle);
22415ac67d8bSRob Swindell 		}
22425ac67d8bSRob Swindell 	}
22435ac67d8bSRob Swindell 	release_firmware(fw);
224422630e28SEdwin Peer 	if (rc)
22457c675421SVasundhara Volam 		goto err_exit;
22465ac67d8bSRob Swindell 
22475ac67d8bSRob Swindell 	if ((install_type & 0xffff) == 0)
22485ac67d8bSRob Swindell 		install_type >>= 16;
22495ac67d8bSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
22505ac67d8bSRob Swindell 	install.install_type = cpu_to_le32(install_type);
22515ac67d8bSRob Swindell 
2252cb4d1d62SKshitij Soni 	mutex_lock(&bp->hwrm_cmd_lock);
225322630e28SEdwin Peer 	rc = _hwrm_send_message(bp, &install, sizeof(install),
22545ac67d8bSRob Swindell 				INSTALL_PACKAGE_TIMEOUT);
225522630e28SEdwin Peer 	if (rc) {
2256cb4d1d62SKshitij Soni 		u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err;
2257cb4d1d62SKshitij Soni 
2258dd2ebf34SVasundhara Volam 		if (resp->error_code && error_code ==
2259dd2ebf34SVasundhara Volam 		    NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
2260cb4d1d62SKshitij Soni 			install.flags |= cpu_to_le16(
2261cb4d1d62SKshitij Soni 			       NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
226222630e28SEdwin Peer 			rc = _hwrm_send_message(bp, &install, sizeof(install),
2263cb4d1d62SKshitij Soni 						INSTALL_PACKAGE_TIMEOUT);
2264dd2ebf34SVasundhara Volam 		}
226522630e28SEdwin Peer 		if (rc)
2266cb4d1d62SKshitij Soni 			goto flash_pkg_exit;
2267cb4d1d62SKshitij Soni 	}
22685ac67d8bSRob Swindell 
22695ac67d8bSRob Swindell 	if (resp->result) {
22705ac67d8bSRob Swindell 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
22715ac67d8bSRob Swindell 			   (s8)resp->result, (int)resp->problem_item);
2272cb4d1d62SKshitij Soni 		rc = -ENOPKG;
22735ac67d8bSRob Swindell 	}
2274cb4d1d62SKshitij Soni flash_pkg_exit:
2275cb4d1d62SKshitij Soni 	mutex_unlock(&bp->hwrm_cmd_lock);
22767c675421SVasundhara Volam err_exit:
227722630e28SEdwin Peer 	if (rc == -EACCES)
2278b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
2279cb4d1d62SKshitij Soni 	return rc;
2280c0c050c5SMichael Chan }
2281c0c050c5SMichael Chan 
2282c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
2283c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
2284c0c050c5SMichael Chan {
2285c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
2286c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
2287c0c050c5SMichael Chan 		return -EINVAL;
2288c0c050c5SMichael Chan 	}
2289c0c050c5SMichael Chan 
22905ac67d8bSRob Swindell 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
22915ac67d8bSRob Swindell 	    flash->region > 0xffff)
22925ac67d8bSRob Swindell 		return bnxt_flash_package_from_file(dev, flash->data,
22935ac67d8bSRob Swindell 						    flash->region);
2294c0c050c5SMichael Chan 
2295c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
2296c0c050c5SMichael Chan }
2297c0c050c5SMichael Chan 
2298c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
2299c0c050c5SMichael Chan {
2300c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2301c0c050c5SMichael Chan 	int rc;
2302c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
2303c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
2304c0c050c5SMichael Chan 
2305c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
2306c0c050c5SMichael Chan 
2307c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2308c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2309c0c050c5SMichael Chan 	if (!rc) {
2310c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
2311c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
2312c0c050c5SMichael Chan 	}
2313c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2314c0c050c5SMichael Chan 	return rc;
2315c0c050c5SMichael Chan }
2316c0c050c5SMichael Chan 
2317c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
2318c0c050c5SMichael Chan {
23194cebbacaSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
23204cebbacaSMichael Chan 
23214cebbacaSMichael Chan 	if (BNXT_VF(bp))
23224cebbacaSMichael Chan 		return 0;
23234cebbacaSMichael Chan 
2324c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
2325c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
2326c0c050c5SMichael Chan 	 */
2327c0c050c5SMichael Chan 	return -1;
2328c0c050c5SMichael Chan }
2329c0c050c5SMichael Chan 
2330c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
2331c0c050c5SMichael Chan {
2332c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2333c0c050c5SMichael Chan 	int rc;
2334c0c050c5SMichael Chan 	u32 dir_entries;
2335c0c050c5SMichael Chan 	u32 entry_length;
2336c0c050c5SMichael Chan 	u8 *buf;
2337c0c050c5SMichael Chan 	size_t buflen;
2338c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2339c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
2340c0c050c5SMichael Chan 
2341c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
2342c0c050c5SMichael Chan 	if (rc != 0)
2343c0c050c5SMichael Chan 		return rc;
2344c0c050c5SMichael Chan 
2345dbbfa96aSVasundhara Volam 	if (!dir_entries || !entry_length)
2346dbbfa96aSVasundhara Volam 		return -EIO;
2347dbbfa96aSVasundhara Volam 
2348c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
2349c0c050c5SMichael Chan 	if (len < 2)
2350c0c050c5SMichael Chan 		return -EINVAL;
2351c0c050c5SMichael Chan 
2352c0c050c5SMichael Chan 	*data++ = dir_entries;
2353c0c050c5SMichael Chan 	*data++ = entry_length;
2354c0c050c5SMichael Chan 	len -= 2;
2355c0c050c5SMichael Chan 	memset(data, 0xff, len);
2356c0c050c5SMichael Chan 
2357c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
2358c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
2359c0c050c5SMichael Chan 				 GFP_KERNEL);
2360c0c050c5SMichael Chan 	if (!buf) {
2361c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2362c0c050c5SMichael Chan 			   (unsigned)buflen);
2363c0c050c5SMichael Chan 		return -ENOMEM;
2364c0c050c5SMichael Chan 	}
2365c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
2366c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2367c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2368c0c050c5SMichael Chan 	if (rc == 0)
2369c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
2370c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
2371c0c050c5SMichael Chan 	return rc;
2372c0c050c5SMichael Chan }
2373c0c050c5SMichael Chan 
2374c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
2375c0c050c5SMichael Chan 			       u32 length, u8 *data)
2376c0c050c5SMichael Chan {
2377c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2378c0c050c5SMichael Chan 	int rc;
2379c0c050c5SMichael Chan 	u8 *buf;
2380c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2381c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
2382c0c050c5SMichael Chan 
2383e0ad8fc5SMichael Chan 	if (!length)
2384e0ad8fc5SMichael Chan 		return -EINVAL;
2385e0ad8fc5SMichael Chan 
2386c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
2387c0c050c5SMichael Chan 				 GFP_KERNEL);
2388c0c050c5SMichael Chan 	if (!buf) {
2389c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2390c0c050c5SMichael Chan 			   (unsigned)length);
2391c0c050c5SMichael Chan 		return -ENOMEM;
2392c0c050c5SMichael Chan 	}
2393c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
2394c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2395c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2396c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
2397c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
2398c0c050c5SMichael Chan 
2399c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2400c0c050c5SMichael Chan 	if (rc == 0)
2401c0c050c5SMichael Chan 		memcpy(data, buf, length);
2402c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
2403c0c050c5SMichael Chan 	return rc;
2404c0c050c5SMichael Chan }
2405c0c050c5SMichael Chan 
24063ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
24073ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
24083ebf6f0aSRob Swindell 				u32 *data_length)
24093ebf6f0aSRob Swindell {
24103ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
24113ebf6f0aSRob Swindell 	int rc;
24123ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
24133ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
24143ebf6f0aSRob Swindell 
24153ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
24163ebf6f0aSRob Swindell 	req.enables = 0;
24173ebf6f0aSRob Swindell 	req.dir_idx = 0;
24183ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
24193ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
24203ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
24213ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
2422cc72f3b1SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2423cc72f3b1SMichael Chan 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
24243ebf6f0aSRob Swindell 	if (rc == 0) {
24253ebf6f0aSRob Swindell 		if (index)
24263ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
24273ebf6f0aSRob Swindell 		if (item_length)
24283ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
24293ebf6f0aSRob Swindell 		if (data_length)
24303ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
24313ebf6f0aSRob Swindell 	}
2432cc72f3b1SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
24333ebf6f0aSRob Swindell 	return rc;
24343ebf6f0aSRob Swindell }
24353ebf6f0aSRob Swindell 
24363ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
24373ebf6f0aSRob Swindell {
24383ebf6f0aSRob Swindell 	char	*retval = NULL;
24393ebf6f0aSRob Swindell 	char	*p;
24403ebf6f0aSRob Swindell 	char	*value;
24413ebf6f0aSRob Swindell 	int	field = 0;
24423ebf6f0aSRob Swindell 
24433ebf6f0aSRob Swindell 	if (datalen < 1)
24443ebf6f0aSRob Swindell 		return NULL;
24453ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
24463ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
24473ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
24483ebf6f0aSRob Swindell 		field = 0;
24493ebf6f0aSRob Swindell 		retval = NULL;
24503ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
24513ebf6f0aSRob Swindell 			value = p;
24523ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
24533ebf6f0aSRob Swindell 				p++;
24543ebf6f0aSRob Swindell 			if (field == desired_field)
24553ebf6f0aSRob Swindell 				retval = value;
24563ebf6f0aSRob Swindell 			if (*p != '\t')
24573ebf6f0aSRob Swindell 				break;
24583ebf6f0aSRob Swindell 			*p = 0;
24593ebf6f0aSRob Swindell 			field++;
24603ebf6f0aSRob Swindell 			p++;
24613ebf6f0aSRob Swindell 		}
24623ebf6f0aSRob Swindell 		if (*p == 0)
24633ebf6f0aSRob Swindell 			break;
24643ebf6f0aSRob Swindell 		*p = 0;
24653ebf6f0aSRob Swindell 	}
24663ebf6f0aSRob Swindell 	return retval;
24673ebf6f0aSRob Swindell }
24683ebf6f0aSRob Swindell 
2469a60faa60SVasundhara Volam static void bnxt_get_pkgver(struct net_device *dev)
24703ebf6f0aSRob Swindell {
2471a60faa60SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
24723ebf6f0aSRob Swindell 	u16 index = 0;
2473a60faa60SVasundhara Volam 	char *pkgver;
2474a60faa60SVasundhara Volam 	u32 pkglen;
2475a60faa60SVasundhara Volam 	u8 *pkgbuf;
2476a60faa60SVasundhara Volam 	int len;
24773ebf6f0aSRob Swindell 
24783ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
24793ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2480a60faa60SVasundhara Volam 				 &index, NULL, &pkglen) != 0)
2481a60faa60SVasundhara Volam 		return;
24823ebf6f0aSRob Swindell 
2483a60faa60SVasundhara Volam 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
2484a60faa60SVasundhara Volam 	if (!pkgbuf) {
2485a60faa60SVasundhara Volam 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
2486a60faa60SVasundhara Volam 			pkglen);
2487a60faa60SVasundhara Volam 		return;
2488a60faa60SVasundhara Volam 	}
24893ebf6f0aSRob Swindell 
2490a60faa60SVasundhara Volam 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2491a60faa60SVasundhara Volam 		goto err;
2492a60faa60SVasundhara Volam 
2493a60faa60SVasundhara Volam 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2494a60faa60SVasundhara Volam 				   pkglen);
2495a60faa60SVasundhara Volam 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2496a60faa60SVasundhara Volam 		len = strlen(bp->fw_ver_str);
2497a60faa60SVasundhara Volam 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2498a60faa60SVasundhara Volam 			 "/pkg %s", pkgver);
2499a60faa60SVasundhara Volam 	}
2500a60faa60SVasundhara Volam err:
2501a60faa60SVasundhara Volam 	kfree(pkgbuf);
25023ebf6f0aSRob Swindell }
25033ebf6f0aSRob Swindell 
2504c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
2505c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2506c0c050c5SMichael Chan 			   u8 *data)
2507c0c050c5SMichael Chan {
2508c0c050c5SMichael Chan 	u32 index;
2509c0c050c5SMichael Chan 	u32 offset;
2510c0c050c5SMichael Chan 
2511c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
2512c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2513c0c050c5SMichael Chan 
2514c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
2515c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
2516c0c050c5SMichael Chan 
2517c0c050c5SMichael Chan 	if (index == 0) {
2518c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
2519c0c050c5SMichael Chan 		return -EINVAL;
2520c0c050c5SMichael Chan 	}
2521c0c050c5SMichael Chan 
2522c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2523c0c050c5SMichael Chan }
2524c0c050c5SMichael Chan 
2525c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2526c0c050c5SMichael Chan {
2527c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2528c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2529c0c050c5SMichael Chan 
2530c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2531c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2532c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2533c0c050c5SMichael Chan }
2534c0c050c5SMichael Chan 
2535c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
2536c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2537c0c050c5SMichael Chan 			   u8 *data)
2538c0c050c5SMichael Chan {
2539c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2540c0c050c5SMichael Chan 	u8 index, dir_op;
2541c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
2542c0c050c5SMichael Chan 
2543c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
2544c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2545c0c050c5SMichael Chan 		return -EINVAL;
2546c0c050c5SMichael Chan 	}
2547c0c050c5SMichael Chan 
2548c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
2549c0c050c5SMichael Chan 
2550c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
2551c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
2552c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
2553c0c050c5SMichael Chan 		if (index == 0)
2554c0c050c5SMichael Chan 			return -EINVAL;
2555c0c050c5SMichael Chan 		switch (dir_op) {
2556c0c050c5SMichael Chan 		case 0x0e: /* erase */
2557c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
2558c0c050c5SMichael Chan 				return -EINVAL;
2559c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
2560c0c050c5SMichael Chan 		default:
2561c0c050c5SMichael Chan 			return -EINVAL;
2562c0c050c5SMichael Chan 		}
2563c0c050c5SMichael Chan 	}
2564c0c050c5SMichael Chan 
2565c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
2566ba425800SJason Yan 	if (bnxt_dir_type_is_executable(type))
25675ac67d8bSRob Swindell 		return -EOPNOTSUPP;
2568c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
2569c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
2570c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
2571c0c050c5SMichael Chan 
2572c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2573c0c050c5SMichael Chan 				eeprom->len);
2574c0c050c5SMichael Chan }
2575c0c050c5SMichael Chan 
257672b34f04SMichael Chan static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
257772b34f04SMichael Chan {
257872b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
257972b34f04SMichael Chan 	struct ethtool_eee *eee = &bp->eee;
258072b34f04SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
2581a5390690SMichael Chan 	u32 advertising;
258272b34f04SMichael Chan 	int rc = 0;
258372b34f04SMichael Chan 
2584c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
258575362a3fSMichael Chan 		return -EOPNOTSUPP;
258672b34f04SMichael Chan 
258772b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
258872b34f04SMichael Chan 		return -EOPNOTSUPP;
258972b34f04SMichael Chan 
2590a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
2591a5390690SMichael Chan 	advertising = _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
259272b34f04SMichael Chan 	if (!edata->eee_enabled)
259372b34f04SMichael Chan 		goto eee_ok;
259472b34f04SMichael Chan 
259572b34f04SMichael Chan 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
259672b34f04SMichael Chan 		netdev_warn(dev, "EEE requires autoneg\n");
2597a5390690SMichael Chan 		rc = -EINVAL;
2598a5390690SMichael Chan 		goto eee_exit;
259972b34f04SMichael Chan 	}
260072b34f04SMichael Chan 	if (edata->tx_lpi_enabled) {
260172b34f04SMichael Chan 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
260272b34f04SMichael Chan 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
260372b34f04SMichael Chan 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
260472b34f04SMichael Chan 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
2605a5390690SMichael Chan 			rc = -EINVAL;
2606a5390690SMichael Chan 			goto eee_exit;
260772b34f04SMichael Chan 		} else if (!bp->lpi_tmr_hi) {
260872b34f04SMichael Chan 			edata->tx_lpi_timer = eee->tx_lpi_timer;
260972b34f04SMichael Chan 		}
261072b34f04SMichael Chan 	}
261172b34f04SMichael Chan 	if (!edata->advertised) {
261272b34f04SMichael Chan 		edata->advertised = advertising & eee->supported;
261372b34f04SMichael Chan 	} else if (edata->advertised & ~advertising) {
261472b34f04SMichael Chan 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
261572b34f04SMichael Chan 			    edata->advertised, advertising);
2616a5390690SMichael Chan 		rc = -EINVAL;
2617a5390690SMichael Chan 		goto eee_exit;
261872b34f04SMichael Chan 	}
261972b34f04SMichael Chan 
262072b34f04SMichael Chan 	eee->advertised = edata->advertised;
262172b34f04SMichael Chan 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
262272b34f04SMichael Chan 	eee->tx_lpi_timer = edata->tx_lpi_timer;
262372b34f04SMichael Chan eee_ok:
262472b34f04SMichael Chan 	eee->eee_enabled = edata->eee_enabled;
262572b34f04SMichael Chan 
262672b34f04SMichael Chan 	if (netif_running(dev))
262772b34f04SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
262872b34f04SMichael Chan 
2629a5390690SMichael Chan eee_exit:
2630a5390690SMichael Chan 	mutex_unlock(&bp->link_lock);
263172b34f04SMichael Chan 	return rc;
263272b34f04SMichael Chan }
263372b34f04SMichael Chan 
263472b34f04SMichael Chan static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
263572b34f04SMichael Chan {
263672b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
263772b34f04SMichael Chan 
263872b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
263972b34f04SMichael Chan 		return -EOPNOTSUPP;
264072b34f04SMichael Chan 
264172b34f04SMichael Chan 	*edata = bp->eee;
264272b34f04SMichael Chan 	if (!bp->eee.eee_enabled) {
264372b34f04SMichael Chan 		/* Preserve tx_lpi_timer so that the last value will be used
264472b34f04SMichael Chan 		 * by default when it is re-enabled.
264572b34f04SMichael Chan 		 */
264672b34f04SMichael Chan 		edata->advertised = 0;
264772b34f04SMichael Chan 		edata->tx_lpi_enabled = 0;
264872b34f04SMichael Chan 	}
264972b34f04SMichael Chan 
265072b34f04SMichael Chan 	if (!bp->eee.eee_active)
265172b34f04SMichael Chan 		edata->lp_advertised = 0;
265272b34f04SMichael Chan 
265372b34f04SMichael Chan 	return 0;
265472b34f04SMichael Chan }
265572b34f04SMichael Chan 
265642ee18feSAjit Khaparde static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
265742ee18feSAjit Khaparde 					    u16 page_number, u16 start_addr,
265842ee18feSAjit Khaparde 					    u16 data_length, u8 *buf)
265942ee18feSAjit Khaparde {
266042ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_input req = {0};
266142ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
266242ee18feSAjit Khaparde 	int rc, byte_offset = 0;
266342ee18feSAjit Khaparde 
266442ee18feSAjit Khaparde 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
266542ee18feSAjit Khaparde 	req.i2c_slave_addr = i2c_addr;
266642ee18feSAjit Khaparde 	req.page_number = cpu_to_le16(page_number);
266742ee18feSAjit Khaparde 	req.port_id = cpu_to_le16(bp->pf.port_id);
266842ee18feSAjit Khaparde 	do {
266942ee18feSAjit Khaparde 		u16 xfer_size;
267042ee18feSAjit Khaparde 
267142ee18feSAjit Khaparde 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
267242ee18feSAjit Khaparde 		data_length -= xfer_size;
267342ee18feSAjit Khaparde 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
267442ee18feSAjit Khaparde 		req.data_length = xfer_size;
267542ee18feSAjit Khaparde 		req.enables = cpu_to_le32(start_addr + byte_offset ?
267642ee18feSAjit Khaparde 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
267742ee18feSAjit Khaparde 		mutex_lock(&bp->hwrm_cmd_lock);
267842ee18feSAjit Khaparde 		rc = _hwrm_send_message(bp, &req, sizeof(req),
267942ee18feSAjit Khaparde 					HWRM_CMD_TIMEOUT);
268042ee18feSAjit Khaparde 		if (!rc)
268142ee18feSAjit Khaparde 			memcpy(buf + byte_offset, output->data, xfer_size);
268242ee18feSAjit Khaparde 		mutex_unlock(&bp->hwrm_cmd_lock);
268342ee18feSAjit Khaparde 		byte_offset += xfer_size;
268442ee18feSAjit Khaparde 	} while (!rc && data_length > 0);
268542ee18feSAjit Khaparde 
268642ee18feSAjit Khaparde 	return rc;
268742ee18feSAjit Khaparde }
268842ee18feSAjit Khaparde 
268942ee18feSAjit Khaparde static int bnxt_get_module_info(struct net_device *dev,
269042ee18feSAjit Khaparde 				struct ethtool_modinfo *modinfo)
269142ee18feSAjit Khaparde {
26927328a23cSVasundhara Volam 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
269342ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
269442ee18feSAjit Khaparde 	int rc;
269542ee18feSAjit Khaparde 
269642ee18feSAjit Khaparde 	/* No point in going further if phy status indicates
269742ee18feSAjit Khaparde 	 * module is not inserted or if it is powered down or
269842ee18feSAjit Khaparde 	 * if it is of type 10GBase-T
269942ee18feSAjit Khaparde 	 */
270042ee18feSAjit Khaparde 	if (bp->link_info.module_status >
270142ee18feSAjit Khaparde 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
270242ee18feSAjit Khaparde 		return -EOPNOTSUPP;
270342ee18feSAjit Khaparde 
270442ee18feSAjit Khaparde 	/* This feature is not supported in older firmware versions */
270542ee18feSAjit Khaparde 	if (bp->hwrm_spec_code < 0x10202)
270642ee18feSAjit Khaparde 		return -EOPNOTSUPP;
270742ee18feSAjit Khaparde 
27087328a23cSVasundhara Volam 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
27097328a23cSVasundhara Volam 					      SFF_DIAG_SUPPORT_OFFSET + 1,
27107328a23cSVasundhara Volam 					      data);
271142ee18feSAjit Khaparde 	if (!rc) {
27127328a23cSVasundhara Volam 		u8 module_id = data[0];
27137328a23cSVasundhara Volam 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
271442ee18feSAjit Khaparde 
271542ee18feSAjit Khaparde 		switch (module_id) {
271642ee18feSAjit Khaparde 		case SFF_MODULE_ID_SFP:
271742ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8472;
271842ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
27197328a23cSVasundhara Volam 			if (!diag_supported)
27207328a23cSVasundhara Volam 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
272142ee18feSAjit Khaparde 			break;
272242ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP:
272342ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP_PLUS:
272442ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8436;
272542ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
272642ee18feSAjit Khaparde 			break;
272742ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP28:
272842ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8636;
272942ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
273042ee18feSAjit Khaparde 			break;
273142ee18feSAjit Khaparde 		default:
273242ee18feSAjit Khaparde 			rc = -EOPNOTSUPP;
273342ee18feSAjit Khaparde 			break;
273442ee18feSAjit Khaparde 		}
273542ee18feSAjit Khaparde 	}
273642ee18feSAjit Khaparde 	return rc;
273742ee18feSAjit Khaparde }
273842ee18feSAjit Khaparde 
273942ee18feSAjit Khaparde static int bnxt_get_module_eeprom(struct net_device *dev,
274042ee18feSAjit Khaparde 				  struct ethtool_eeprom *eeprom,
274142ee18feSAjit Khaparde 				  u8 *data)
274242ee18feSAjit Khaparde {
274342ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
274442ee18feSAjit Khaparde 	u16  start = eeprom->offset, length = eeprom->len;
2745f3ea3119SColin Ian King 	int rc = 0;
274642ee18feSAjit Khaparde 
274742ee18feSAjit Khaparde 	memset(data, 0, eeprom->len);
274842ee18feSAjit Khaparde 
274942ee18feSAjit Khaparde 	/* Read A0 portion of the EEPROM */
275042ee18feSAjit Khaparde 	if (start < ETH_MODULE_SFF_8436_LEN) {
275142ee18feSAjit Khaparde 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
275242ee18feSAjit Khaparde 			length = ETH_MODULE_SFF_8436_LEN - start;
275342ee18feSAjit Khaparde 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
275442ee18feSAjit Khaparde 						      start, length, data);
275542ee18feSAjit Khaparde 		if (rc)
275642ee18feSAjit Khaparde 			return rc;
275742ee18feSAjit Khaparde 		start += length;
275842ee18feSAjit Khaparde 		data += length;
275942ee18feSAjit Khaparde 		length = eeprom->len - length;
276042ee18feSAjit Khaparde 	}
276142ee18feSAjit Khaparde 
276242ee18feSAjit Khaparde 	/* Read A2 portion of the EEPROM */
276342ee18feSAjit Khaparde 	if (length) {
276442ee18feSAjit Khaparde 		start -= ETH_MODULE_SFF_8436_LEN;
2765dea521a2SChristophe JAILLET 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1,
2766dea521a2SChristophe JAILLET 						      start, length, data);
276742ee18feSAjit Khaparde 	}
276842ee18feSAjit Khaparde 	return rc;
276942ee18feSAjit Khaparde }
277042ee18feSAjit Khaparde 
2771ae8e98a6SDeepak Khungar static int bnxt_nway_reset(struct net_device *dev)
2772ae8e98a6SDeepak Khungar {
2773ae8e98a6SDeepak Khungar 	int rc = 0;
2774ae8e98a6SDeepak Khungar 
2775ae8e98a6SDeepak Khungar 	struct bnxt *bp = netdev_priv(dev);
2776ae8e98a6SDeepak Khungar 	struct bnxt_link_info *link_info = &bp->link_info;
2777ae8e98a6SDeepak Khungar 
2778c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
2779ae8e98a6SDeepak Khungar 		return -EOPNOTSUPP;
2780ae8e98a6SDeepak Khungar 
2781ae8e98a6SDeepak Khungar 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2782ae8e98a6SDeepak Khungar 		return -EINVAL;
2783ae8e98a6SDeepak Khungar 
2784ae8e98a6SDeepak Khungar 	if (netif_running(dev))
2785ae8e98a6SDeepak Khungar 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
2786ae8e98a6SDeepak Khungar 
2787ae8e98a6SDeepak Khungar 	return rc;
2788ae8e98a6SDeepak Khungar }
2789ae8e98a6SDeepak Khungar 
27905ad2cbeeSMichael Chan static int bnxt_set_phys_id(struct net_device *dev,
27915ad2cbeeSMichael Chan 			    enum ethtool_phys_id_state state)
27925ad2cbeeSMichael Chan {
27935ad2cbeeSMichael Chan 	struct hwrm_port_led_cfg_input req = {0};
27945ad2cbeeSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
27955ad2cbeeSMichael Chan 	struct bnxt_pf_info *pf = &bp->pf;
27965ad2cbeeSMichael Chan 	struct bnxt_led_cfg *led_cfg;
27975ad2cbeeSMichael Chan 	u8 led_state;
27985ad2cbeeSMichael Chan 	__le16 duration;
27999f90445cSVasundhara Volam 	int i;
28005ad2cbeeSMichael Chan 
28015ad2cbeeSMichael Chan 	if (!bp->num_leds || BNXT_VF(bp))
28025ad2cbeeSMichael Chan 		return -EOPNOTSUPP;
28035ad2cbeeSMichael Chan 
28045ad2cbeeSMichael Chan 	if (state == ETHTOOL_ID_ACTIVE) {
28055ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
28065ad2cbeeSMichael Chan 		duration = cpu_to_le16(500);
28075ad2cbeeSMichael Chan 	} else if (state == ETHTOOL_ID_INACTIVE) {
28085ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
28095ad2cbeeSMichael Chan 		duration = cpu_to_le16(0);
28105ad2cbeeSMichael Chan 	} else {
28115ad2cbeeSMichael Chan 		return -EINVAL;
28125ad2cbeeSMichael Chan 	}
28135ad2cbeeSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
28145ad2cbeeSMichael Chan 	req.port_id = cpu_to_le16(pf->port_id);
28155ad2cbeeSMichael Chan 	req.num_leds = bp->num_leds;
28165ad2cbeeSMichael Chan 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
28175ad2cbeeSMichael Chan 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
28185ad2cbeeSMichael Chan 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
28195ad2cbeeSMichael Chan 		led_cfg->led_id = bp->leds[i].led_id;
28205ad2cbeeSMichael Chan 		led_cfg->led_state = led_state;
28215ad2cbeeSMichael Chan 		led_cfg->led_blink_on = duration;
28225ad2cbeeSMichael Chan 		led_cfg->led_blink_off = duration;
28235ad2cbeeSMichael Chan 		led_cfg->led_group_id = bp->leds[i].led_group_id;
28245ad2cbeeSMichael Chan 	}
28259f90445cSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
28265ad2cbeeSMichael Chan }
28275ad2cbeeSMichael Chan 
282867fea463SMichael Chan static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
282967fea463SMichael Chan {
283067fea463SMichael Chan 	struct hwrm_selftest_irq_input req = {0};
283167fea463SMichael Chan 
283267fea463SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
283367fea463SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
283467fea463SMichael Chan }
283567fea463SMichael Chan 
283667fea463SMichael Chan static int bnxt_test_irq(struct bnxt *bp)
283767fea463SMichael Chan {
283867fea463SMichael Chan 	int i;
283967fea463SMichael Chan 
284067fea463SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
284167fea463SMichael Chan 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
284267fea463SMichael Chan 		int rc;
284367fea463SMichael Chan 
284467fea463SMichael Chan 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
284567fea463SMichael Chan 		if (rc)
284667fea463SMichael Chan 			return rc;
284767fea463SMichael Chan 	}
284867fea463SMichael Chan 	return 0;
284967fea463SMichael Chan }
285067fea463SMichael Chan 
2851f7dc1ea6SMichael Chan static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
2852f7dc1ea6SMichael Chan {
2853f7dc1ea6SMichael Chan 	struct hwrm_port_mac_cfg_input req = {0};
2854f7dc1ea6SMichael Chan 
2855f7dc1ea6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
2856f7dc1ea6SMichael Chan 
2857f7dc1ea6SMichael Chan 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
2858f7dc1ea6SMichael Chan 	if (enable)
2859f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
2860f7dc1ea6SMichael Chan 	else
2861f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
2862f7dc1ea6SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2863f7dc1ea6SMichael Chan }
2864f7dc1ea6SMichael Chan 
286556d37462SVasundhara Volam static int bnxt_query_force_speeds(struct bnxt *bp, u16 *force_speeds)
286656d37462SVasundhara Volam {
286756d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
286856d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_input req = {0};
286956d37462SVasundhara Volam 	int rc;
287056d37462SVasundhara Volam 
287156d37462SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
287256d37462SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
287356d37462SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
287456d37462SVasundhara Volam 	if (!rc)
287556d37462SVasundhara Volam 		*force_speeds = le16_to_cpu(resp->supported_speeds_force_mode);
287656d37462SVasundhara Volam 
287756d37462SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
287856d37462SVasundhara Volam 	return rc;
287956d37462SVasundhara Volam }
288056d37462SVasundhara Volam 
288191725d89SMichael Chan static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
288291725d89SMichael Chan 				    struct hwrm_port_phy_cfg_input *req)
288391725d89SMichael Chan {
288491725d89SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
288556d37462SVasundhara Volam 	u16 fw_advertising;
288691725d89SMichael Chan 	u16 fw_speed;
288791725d89SMichael Chan 	int rc;
288891725d89SMichael Chan 
28898a60efd1SMichael Chan 	if (!link_info->autoneg ||
28908a60efd1SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_AN_PHY_LPBK))
289191725d89SMichael Chan 		return 0;
289291725d89SMichael Chan 
289356d37462SVasundhara Volam 	rc = bnxt_query_force_speeds(bp, &fw_advertising);
289456d37462SVasundhara Volam 	if (rc)
289556d37462SVasundhara Volam 		return rc;
289656d37462SVasundhara Volam 
289791725d89SMichael Chan 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
289883d8f5e9SMichael Chan 	if (bp->link_info.link_up)
289991725d89SMichael Chan 		fw_speed = bp->link_info.link_speed;
290091725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
290191725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
290291725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
290391725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
290491725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
290591725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
290691725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
290791725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
290891725d89SMichael Chan 
290991725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(fw_speed);
291091725d89SMichael Chan 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
291191725d89SMichael Chan 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
291291725d89SMichael Chan 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
291391725d89SMichael Chan 	req->flags = 0;
291491725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(0);
291591725d89SMichael Chan 	return rc;
291691725d89SMichael Chan }
291791725d89SMichael Chan 
291855fd0cf3SMichael Chan static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
291991725d89SMichael Chan {
292091725d89SMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
292191725d89SMichael Chan 
292291725d89SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
292391725d89SMichael Chan 
292491725d89SMichael Chan 	if (enable) {
292591725d89SMichael Chan 		bnxt_disable_an_for_lpbk(bp, &req);
292655fd0cf3SMichael Chan 		if (ext)
292755fd0cf3SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
292855fd0cf3SMichael Chan 		else
292991725d89SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
293091725d89SMichael Chan 	} else {
293191725d89SMichael Chan 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
293291725d89SMichael Chan 	}
293391725d89SMichael Chan 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
293491725d89SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
293591725d89SMichael Chan }
293691725d89SMichael Chan 
2937e44758b7SMichael Chan static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2938f7dc1ea6SMichael Chan 			    u32 raw_cons, int pkt_size)
2939f7dc1ea6SMichael Chan {
2940e44758b7SMichael Chan 	struct bnxt_napi *bnapi = cpr->bnapi;
2941e44758b7SMichael Chan 	struct bnxt_rx_ring_info *rxr;
2942f7dc1ea6SMichael Chan 	struct bnxt_sw_rx_bd *rx_buf;
2943f7dc1ea6SMichael Chan 	struct rx_cmp *rxcmp;
2944f7dc1ea6SMichael Chan 	u16 cp_cons, cons;
2945f7dc1ea6SMichael Chan 	u8 *data;
2946f7dc1ea6SMichael Chan 	u32 len;
2947f7dc1ea6SMichael Chan 	int i;
2948f7dc1ea6SMichael Chan 
2949e44758b7SMichael Chan 	rxr = bnapi->rx_ring;
2950f7dc1ea6SMichael Chan 	cp_cons = RING_CMP(raw_cons);
2951f7dc1ea6SMichael Chan 	rxcmp = (struct rx_cmp *)
2952f7dc1ea6SMichael Chan 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2953f7dc1ea6SMichael Chan 	cons = rxcmp->rx_cmp_opaque;
2954f7dc1ea6SMichael Chan 	rx_buf = &rxr->rx_buf_ring[cons];
2955f7dc1ea6SMichael Chan 	data = rx_buf->data_ptr;
2956f7dc1ea6SMichael Chan 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
2957f7dc1ea6SMichael Chan 	if (len != pkt_size)
2958f7dc1ea6SMichael Chan 		return -EIO;
2959f7dc1ea6SMichael Chan 	i = ETH_ALEN;
2960f7dc1ea6SMichael Chan 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
2961f7dc1ea6SMichael Chan 		return -EIO;
2962f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2963f7dc1ea6SMichael Chan 	for (  ; i < pkt_size; i++) {
2964f7dc1ea6SMichael Chan 		if (data[i] != (u8)(i & 0xff))
2965f7dc1ea6SMichael Chan 			return -EIO;
2966f7dc1ea6SMichael Chan 	}
2967f7dc1ea6SMichael Chan 	return 0;
2968f7dc1ea6SMichael Chan }
2969f7dc1ea6SMichael Chan 
2970e44758b7SMichael Chan static int bnxt_poll_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2971e44758b7SMichael Chan 			      int pkt_size)
2972f7dc1ea6SMichael Chan {
2973f7dc1ea6SMichael Chan 	struct tx_cmp *txcmp;
2974f7dc1ea6SMichael Chan 	int rc = -EIO;
2975f7dc1ea6SMichael Chan 	u32 raw_cons;
2976f7dc1ea6SMichael Chan 	u32 cons;
2977f7dc1ea6SMichael Chan 	int i;
2978f7dc1ea6SMichael Chan 
2979f7dc1ea6SMichael Chan 	raw_cons = cpr->cp_raw_cons;
2980f7dc1ea6SMichael Chan 	for (i = 0; i < 200; i++) {
2981f7dc1ea6SMichael Chan 		cons = RING_CMP(raw_cons);
2982f7dc1ea6SMichael Chan 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2983f7dc1ea6SMichael Chan 
2984f7dc1ea6SMichael Chan 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
2985f7dc1ea6SMichael Chan 			udelay(5);
2986f7dc1ea6SMichael Chan 			continue;
2987f7dc1ea6SMichael Chan 		}
2988f7dc1ea6SMichael Chan 
2989f7dc1ea6SMichael Chan 		/* The valid test of the entry must be done first before
2990f7dc1ea6SMichael Chan 		 * reading any further.
2991f7dc1ea6SMichael Chan 		 */
2992f7dc1ea6SMichael Chan 		dma_rmb();
2993f7dc1ea6SMichael Chan 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
2994e44758b7SMichael Chan 			rc = bnxt_rx_loopback(bp, cpr, raw_cons, pkt_size);
2995f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2996f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2997f7dc1ea6SMichael Chan 			break;
2998f7dc1ea6SMichael Chan 		}
2999f7dc1ea6SMichael Chan 		raw_cons = NEXT_RAW_CMP(raw_cons);
3000f7dc1ea6SMichael Chan 	}
3001f7dc1ea6SMichael Chan 	cpr->cp_raw_cons = raw_cons;
3002f7dc1ea6SMichael Chan 	return rc;
3003f7dc1ea6SMichael Chan }
3004f7dc1ea6SMichael Chan 
3005f7dc1ea6SMichael Chan static int bnxt_run_loopback(struct bnxt *bp)
3006f7dc1ea6SMichael Chan {
3007f7dc1ea6SMichael Chan 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
300884404d5fSMichael Chan 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
3009e44758b7SMichael Chan 	struct bnxt_cp_ring_info *cpr;
3010f7dc1ea6SMichael Chan 	int pkt_size, i = 0;
3011f7dc1ea6SMichael Chan 	struct sk_buff *skb;
3012f7dc1ea6SMichael Chan 	dma_addr_t map;
3013f7dc1ea6SMichael Chan 	u8 *data;
3014f7dc1ea6SMichael Chan 	int rc;
3015f7dc1ea6SMichael Chan 
301684404d5fSMichael Chan 	cpr = &rxr->bnapi->cp_ring;
301784404d5fSMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
301884404d5fSMichael Chan 		cpr = cpr->cp_ring_arr[BNXT_RX_HDL];
3019f7dc1ea6SMichael Chan 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
3020f7dc1ea6SMichael Chan 	skb = netdev_alloc_skb(bp->dev, pkt_size);
3021f7dc1ea6SMichael Chan 	if (!skb)
3022f7dc1ea6SMichael Chan 		return -ENOMEM;
3023f7dc1ea6SMichael Chan 	data = skb_put(skb, pkt_size);
3024f7dc1ea6SMichael Chan 	eth_broadcast_addr(data);
3025f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3026f7dc1ea6SMichael Chan 	ether_addr_copy(&data[i], bp->dev->dev_addr);
3027f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3028f7dc1ea6SMichael Chan 	for ( ; i < pkt_size; i++)
3029f7dc1ea6SMichael Chan 		data[i] = (u8)(i & 0xff);
3030f7dc1ea6SMichael Chan 
3031f7dc1ea6SMichael Chan 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
3032f7dc1ea6SMichael Chan 			     PCI_DMA_TODEVICE);
3033f7dc1ea6SMichael Chan 	if (dma_mapping_error(&bp->pdev->dev, map)) {
3034f7dc1ea6SMichael Chan 		dev_kfree_skb(skb);
3035f7dc1ea6SMichael Chan 		return -EIO;
3036f7dc1ea6SMichael Chan 	}
3037c1ba92a8SMichael Chan 	bnxt_xmit_bd(bp, txr, map, pkt_size);
3038f7dc1ea6SMichael Chan 
3039f7dc1ea6SMichael Chan 	/* Sync BD data before updating doorbell */
3040f7dc1ea6SMichael Chan 	wmb();
3041f7dc1ea6SMichael Chan 
3042697197e5SMichael Chan 	bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
3043e44758b7SMichael Chan 	rc = bnxt_poll_loopback(bp, cpr, pkt_size);
3044f7dc1ea6SMichael Chan 
3045f7dc1ea6SMichael Chan 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
3046f7dc1ea6SMichael Chan 	dev_kfree_skb(skb);
3047f7dc1ea6SMichael Chan 	return rc;
3048f7dc1ea6SMichael Chan }
3049f7dc1ea6SMichael Chan 
3050eb513658SMichael Chan static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
3051eb513658SMichael Chan {
3052eb513658SMichael Chan 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
3053eb513658SMichael Chan 	struct hwrm_selftest_exec_input req = {0};
3054eb513658SMichael Chan 	int rc;
3055eb513658SMichael Chan 
3056eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
3057eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3058eb513658SMichael Chan 	resp->test_success = 0;
3059eb513658SMichael Chan 	req.flags = test_mask;
3060eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
3061eb513658SMichael Chan 	*test_results = resp->test_success;
3062eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3063eb513658SMichael Chan 	return rc;
3064eb513658SMichael Chan }
3065eb513658SMichael Chan 
306655fd0cf3SMichael Chan #define BNXT_DRV_TESTS			4
3067f7dc1ea6SMichael Chan #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
306891725d89SMichael Chan #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
306955fd0cf3SMichael Chan #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
307055fd0cf3SMichael Chan #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
3071eb513658SMichael Chan 
3072eb513658SMichael Chan static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
3073eb513658SMichael Chan 			   u64 *buf)
3074eb513658SMichael Chan {
3075eb513658SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
307655fd0cf3SMichael Chan 	bool do_ext_lpbk = false;
3077eb513658SMichael Chan 	bool offline = false;
3078eb513658SMichael Chan 	u8 test_results = 0;
3079eb513658SMichael Chan 	u8 test_mask = 0;
3080d27e2ca1SMichael Chan 	int rc = 0, i;
3081eb513658SMichael Chan 
3082eb513658SMichael Chan 	if (!bp->num_tests || !BNXT_SINGLE_PF(bp))
3083eb513658SMichael Chan 		return;
3084eb513658SMichael Chan 	memset(buf, 0, sizeof(u64) * bp->num_tests);
3085eb513658SMichael Chan 	if (!netif_running(dev)) {
3086eb513658SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
3087eb513658SMichael Chan 		return;
3088eb513658SMichael Chan 	}
3089eb513658SMichael Chan 
309055fd0cf3SMichael Chan 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
309155fd0cf3SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_EXT_LPBK))
309255fd0cf3SMichael Chan 		do_ext_lpbk = true;
309355fd0cf3SMichael Chan 
3094eb513658SMichael Chan 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
3095eb513658SMichael Chan 		if (bp->pf.active_vfs) {
3096eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3097eb513658SMichael Chan 			netdev_warn(dev, "Offline tests cannot be run with active VFs\n");
3098eb513658SMichael Chan 			return;
3099eb513658SMichael Chan 		}
3100eb513658SMichael Chan 		offline = true;
3101eb513658SMichael Chan 	}
3102eb513658SMichael Chan 
3103eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3104eb513658SMichael Chan 		u8 bit_val = 1 << i;
3105eb513658SMichael Chan 
3106eb513658SMichael Chan 		if (!(bp->test_info->offline_mask & bit_val))
3107eb513658SMichael Chan 			test_mask |= bit_val;
3108eb513658SMichael Chan 		else if (offline)
3109eb513658SMichael Chan 			test_mask |= bit_val;
3110eb513658SMichael Chan 	}
3111eb513658SMichael Chan 	if (!offline) {
3112eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3113eb513658SMichael Chan 	} else {
3114eb513658SMichael Chan 		rc = bnxt_close_nic(bp, false, false);
3115eb513658SMichael Chan 		if (rc)
3116eb513658SMichael Chan 			return;
3117eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3118f7dc1ea6SMichael Chan 
3119f7dc1ea6SMichael Chan 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
3120f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, true);
3121f7dc1ea6SMichael Chan 		msleep(250);
3122f7dc1ea6SMichael Chan 		rc = bnxt_half_open_nic(bp);
3123f7dc1ea6SMichael Chan 		if (rc) {
3124f7dc1ea6SMichael Chan 			bnxt_hwrm_mac_loopback(bp, false);
3125f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3126f7dc1ea6SMichael Chan 			return;
3127f7dc1ea6SMichael Chan 		}
3128f7dc1ea6SMichael Chan 		if (bnxt_run_loopback(bp))
3129f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3130f7dc1ea6SMichael Chan 		else
3131f7dc1ea6SMichael Chan 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
3132f7dc1ea6SMichael Chan 
3133f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, false);
313455fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, true, false);
313591725d89SMichael Chan 		msleep(1000);
313691725d89SMichael Chan 		if (bnxt_run_loopback(bp)) {
313791725d89SMichael Chan 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
313891725d89SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
313991725d89SMichael Chan 		}
314055fd0cf3SMichael Chan 		if (do_ext_lpbk) {
314155fd0cf3SMichael Chan 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
314255fd0cf3SMichael Chan 			bnxt_hwrm_phy_loopback(bp, true, true);
314355fd0cf3SMichael Chan 			msleep(1000);
314455fd0cf3SMichael Chan 			if (bnxt_run_loopback(bp)) {
314555fd0cf3SMichael Chan 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
314655fd0cf3SMichael Chan 				etest->flags |= ETH_TEST_FL_FAILED;
314755fd0cf3SMichael Chan 			}
314855fd0cf3SMichael Chan 		}
314955fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, false, false);
315091725d89SMichael Chan 		bnxt_half_close_nic(bp);
3151d27e2ca1SMichael Chan 		rc = bnxt_open_nic(bp, false, true);
3152eb513658SMichael Chan 	}
3153d27e2ca1SMichael Chan 	if (rc || bnxt_test_irq(bp)) {
315467fea463SMichael Chan 		buf[BNXT_IRQ_TEST_IDX] = 1;
315567fea463SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
315667fea463SMichael Chan 	}
3157eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3158eb513658SMichael Chan 		u8 bit_val = 1 << i;
3159eb513658SMichael Chan 
3160eb513658SMichael Chan 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
3161eb513658SMichael Chan 			buf[i] = 1;
3162eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3163eb513658SMichael Chan 		}
3164eb513658SMichael Chan 	}
3165eb513658SMichael Chan }
3166eb513658SMichael Chan 
316749f7972fSVasundhara Volam static int bnxt_reset(struct net_device *dev, u32 *flags)
316849f7972fSVasundhara Volam {
316949f7972fSVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
31708cec0940SEdwin Peer 	bool reload = false;
31717a13240eSEdwin Peer 	u32 req = *flags;
31727a13240eSEdwin Peer 
31737a13240eSEdwin Peer 	if (!req)
31747a13240eSEdwin Peer 		return -EINVAL;
317549f7972fSVasundhara Volam 
317649f7972fSVasundhara Volam 	if (!BNXT_PF(bp)) {
317749f7972fSVasundhara Volam 		netdev_err(dev, "Reset is not supported from a VF\n");
317849f7972fSVasundhara Volam 		return -EOPNOTSUPP;
317949f7972fSVasundhara Volam 	}
318049f7972fSVasundhara Volam 
31810a3f4e4fSVasundhara Volam 	if (pci_vfs_assigned(bp->pdev) &&
31820a3f4e4fSVasundhara Volam 	    !(bp->fw_cap & BNXT_FW_CAP_HOT_RESET)) {
318349f7972fSVasundhara Volam 		netdev_err(dev,
318449f7972fSVasundhara Volam 			   "Reset not allowed when VFs are assigned to VMs\n");
318549f7972fSVasundhara Volam 		return -EBUSY;
318649f7972fSVasundhara Volam 	}
318749f7972fSVasundhara Volam 
31887a13240eSEdwin Peer 	if ((req & BNXT_FW_RESET_CHIP) == BNXT_FW_RESET_CHIP) {
318949f7972fSVasundhara Volam 		/* This feature is not supported in older firmware versions */
31907a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
31917a13240eSEdwin Peer 			if (!bnxt_firmware_reset_chip(dev)) {
31927a13240eSEdwin Peer 				netdev_info(dev, "Firmware reset request successful.\n");
31930a3f4e4fSVasundhara Volam 				if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET))
31948cec0940SEdwin Peer 					reload = true;
31957a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_CHIP;
31962373d8d6SScott Branden 			}
31977a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_CHIP) {
31987a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
31997a13240eSEdwin Peer 		}
32007a13240eSEdwin Peer 	}
32017a13240eSEdwin Peer 
32027a13240eSEdwin Peer 	if (req & BNXT_FW_RESET_AP) {
32036502ad59SScott Branden 		/* This feature is not supported in older firmware versions */
32047a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
32057a13240eSEdwin Peer 			if (!bnxt_firmware_reset_ap(dev)) {
32067a13240eSEdwin Peer 				netdev_info(dev, "Reset application processor successful.\n");
32078cec0940SEdwin Peer 				reload = true;
32087a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_AP;
32092373d8d6SScott Branden 			}
32107a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_AP) {
32117a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
32127a13240eSEdwin Peer 		}
321349f7972fSVasundhara Volam 	}
321449f7972fSVasundhara Volam 
32158cec0940SEdwin Peer 	if (reload)
32168cec0940SEdwin Peer 		netdev_info(dev, "Reload driver to complete reset\n");
32178cec0940SEdwin Peer 
32187a13240eSEdwin Peer 	return 0;
321949f7972fSVasundhara Volam }
322049f7972fSVasundhara Volam 
32216c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
32226c5657d0SVasundhara Volam 				  struct bnxt_hwrm_dbg_dma_info *info)
32236c5657d0SVasundhara Volam {
32246c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
32256c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_input *cmn_req = msg;
32266c5657d0SVasundhara Volam 	__le16 *seq_ptr = msg + info->seq_off;
32276c5657d0SVasundhara Volam 	u16 seq = 0, len, segs_off;
32286c5657d0SVasundhara Volam 	void *resp = cmn_resp;
32296c5657d0SVasundhara Volam 	dma_addr_t dma_handle;
32306c5657d0SVasundhara Volam 	int rc, off = 0;
32316c5657d0SVasundhara Volam 	void *dma_buf;
32326c5657d0SVasundhara Volam 
32336c5657d0SVasundhara Volam 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
32346c5657d0SVasundhara Volam 				     GFP_KERNEL);
32356c5657d0SVasundhara Volam 	if (!dma_buf)
32366c5657d0SVasundhara Volam 		return -ENOMEM;
32376c5657d0SVasundhara Volam 
32386c5657d0SVasundhara Volam 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
32396c5657d0SVasundhara Volam 			    total_segments);
32406c5657d0SVasundhara Volam 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
32416c5657d0SVasundhara Volam 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
32426c5657d0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
32436c5657d0SVasundhara Volam 	while (1) {
32446c5657d0SVasundhara Volam 		*seq_ptr = cpu_to_le16(seq);
32455b306bdeSVasundhara Volam 		rc = _hwrm_send_message(bp, msg, msg_len,
32465b306bdeSVasundhara Volam 					HWRM_COREDUMP_TIMEOUT);
32476c5657d0SVasundhara Volam 		if (rc)
32486c5657d0SVasundhara Volam 			break;
32496c5657d0SVasundhara Volam 
32506c5657d0SVasundhara Volam 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
32516c5657d0SVasundhara Volam 		if (!seq &&
32526c5657d0SVasundhara Volam 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
32536c5657d0SVasundhara Volam 			info->segs = le16_to_cpu(*((__le16 *)(resp +
32546c5657d0SVasundhara Volam 							      segs_off)));
32556c5657d0SVasundhara Volam 			if (!info->segs) {
32566c5657d0SVasundhara Volam 				rc = -EIO;
32576c5657d0SVasundhara Volam 				break;
32586c5657d0SVasundhara Volam 			}
32596c5657d0SVasundhara Volam 
32606c5657d0SVasundhara Volam 			info->dest_buf_size = info->segs *
32616c5657d0SVasundhara Volam 					sizeof(struct coredump_segment_record);
32626c5657d0SVasundhara Volam 			info->dest_buf = kmalloc(info->dest_buf_size,
32636c5657d0SVasundhara Volam 						 GFP_KERNEL);
32646c5657d0SVasundhara Volam 			if (!info->dest_buf) {
32656c5657d0SVasundhara Volam 				rc = -ENOMEM;
32666c5657d0SVasundhara Volam 				break;
32676c5657d0SVasundhara Volam 			}
32686c5657d0SVasundhara Volam 		}
32696c5657d0SVasundhara Volam 
3270c74751f4SVasundhara Volam 		if (info->dest_buf) {
3271c74751f4SVasundhara Volam 			if ((info->seg_start + off + len) <=
3272c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
32736c5657d0SVasundhara Volam 				memcpy(info->dest_buf + off, dma_buf, len);
3274c74751f4SVasundhara Volam 			} else {
3275c74751f4SVasundhara Volam 				rc = -ENOBUFS;
3276c74751f4SVasundhara Volam 				break;
3277c74751f4SVasundhara Volam 			}
3278c74751f4SVasundhara Volam 		}
32796c5657d0SVasundhara Volam 
32806c5657d0SVasundhara Volam 		if (cmn_req->req_type ==
32816c5657d0SVasundhara Volam 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
32826c5657d0SVasundhara Volam 			info->dest_buf_size += len;
32836c5657d0SVasundhara Volam 
32846c5657d0SVasundhara Volam 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
32856c5657d0SVasundhara Volam 			break;
32866c5657d0SVasundhara Volam 
32876c5657d0SVasundhara Volam 		seq++;
32886c5657d0SVasundhara Volam 		off += len;
32896c5657d0SVasundhara Volam 	}
32906c5657d0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
32916c5657d0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
32926c5657d0SVasundhara Volam 	return rc;
32936c5657d0SVasundhara Volam }
32946c5657d0SVasundhara Volam 
32956c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
32966c5657d0SVasundhara Volam 				       struct bnxt_coredump *coredump)
32976c5657d0SVasundhara Volam {
32986c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_list_input req = {0};
32996c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
33006c5657d0SVasundhara Volam 	int rc;
33016c5657d0SVasundhara Volam 
33026c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
33036c5657d0SVasundhara Volam 
33046c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_LIST_BUF_LEN;
33056c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
33066c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
33076c5657d0SVasundhara Volam 				     data_len);
33086c5657d0SVasundhara Volam 
33096c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
33106c5657d0SVasundhara Volam 	if (!rc) {
33116c5657d0SVasundhara Volam 		coredump->data = info.dest_buf;
33126c5657d0SVasundhara Volam 		coredump->data_size = info.dest_buf_size;
33136c5657d0SVasundhara Volam 		coredump->total_segs = info.segs;
33146c5657d0SVasundhara Volam 	}
33156c5657d0SVasundhara Volam 	return rc;
33166c5657d0SVasundhara Volam }
33176c5657d0SVasundhara Volam 
33186c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
33196c5657d0SVasundhara Volam 					   u16 segment_id)
33206c5657d0SVasundhara Volam {
33216c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_initiate_input req = {0};
33226c5657d0SVasundhara Volam 
33236c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
33246c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
33256c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
33266c5657d0SVasundhara Volam 
332757a8730bSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_COREDUMP_TIMEOUT);
33286c5657d0SVasundhara Volam }
33296c5657d0SVasundhara Volam 
33306c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
33316c5657d0SVasundhara Volam 					   u16 segment_id, u32 *seg_len,
3332c74751f4SVasundhara Volam 					   void *buf, u32 buf_len, u32 offset)
33336c5657d0SVasundhara Volam {
33346c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_retrieve_input req = {0};
33356c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
33366c5657d0SVasundhara Volam 	int rc;
33376c5657d0SVasundhara Volam 
33386c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
33396c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
33406c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
33416c5657d0SVasundhara Volam 
33426c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
33436c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
33446c5657d0SVasundhara Volam 				seq_no);
33456c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
33466c5657d0SVasundhara Volam 				     data_len);
3347c74751f4SVasundhara Volam 	if (buf) {
33486c5657d0SVasundhara Volam 		info.dest_buf = buf + offset;
3349c74751f4SVasundhara Volam 		info.buf_len = buf_len;
3350c74751f4SVasundhara Volam 		info.seg_start = offset;
3351c74751f4SVasundhara Volam 	}
33526c5657d0SVasundhara Volam 
33536c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
33546c5657d0SVasundhara Volam 	if (!rc)
33556c5657d0SVasundhara Volam 		*seg_len = info.dest_buf_size;
33566c5657d0SVasundhara Volam 
33576c5657d0SVasundhara Volam 	return rc;
33586c5657d0SVasundhara Volam }
33596c5657d0SVasundhara Volam 
33606c5657d0SVasundhara Volam static void
33616c5657d0SVasundhara Volam bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
33626c5657d0SVasundhara Volam 			   struct bnxt_coredump_segment_hdr *seg_hdr,
33636c5657d0SVasundhara Volam 			   struct coredump_segment_record *seg_rec, u32 seg_len,
33646c5657d0SVasundhara Volam 			   int status, u32 duration, u32 instance)
33656c5657d0SVasundhara Volam {
33666c5657d0SVasundhara Volam 	memset(seg_hdr, 0, sizeof(*seg_hdr));
33678605212aSVasundhara Volam 	memcpy(seg_hdr->signature, "sEgM", 4);
33686c5657d0SVasundhara Volam 	if (seg_rec) {
33696c5657d0SVasundhara Volam 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
33706c5657d0SVasundhara Volam 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
33716c5657d0SVasundhara Volam 		seg_hdr->low_version = seg_rec->version_low;
33726c5657d0SVasundhara Volam 		seg_hdr->high_version = seg_rec->version_hi;
33736c5657d0SVasundhara Volam 	} else {
33746c5657d0SVasundhara Volam 		/* For hwrm_ver_get response Component id = 2
33756c5657d0SVasundhara Volam 		 * and Segment id = 0
33766c5657d0SVasundhara Volam 		 */
33776c5657d0SVasundhara Volam 		seg_hdr->component_id = cpu_to_le32(2);
33786c5657d0SVasundhara Volam 		seg_hdr->segment_id = 0;
33796c5657d0SVasundhara Volam 	}
33806c5657d0SVasundhara Volam 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
33816c5657d0SVasundhara Volam 	seg_hdr->length = cpu_to_le32(seg_len);
33826c5657d0SVasundhara Volam 	seg_hdr->status = cpu_to_le32(status);
33836c5657d0SVasundhara Volam 	seg_hdr->duration = cpu_to_le32(duration);
33846c5657d0SVasundhara Volam 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
33856c5657d0SVasundhara Volam 	seg_hdr->instance = cpu_to_le32(instance);
33866c5657d0SVasundhara Volam }
33876c5657d0SVasundhara Volam 
33886c5657d0SVasundhara Volam static void
33896c5657d0SVasundhara Volam bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
33906c5657d0SVasundhara Volam 			  time64_t start, s16 start_utc, u16 total_segs,
33916c5657d0SVasundhara Volam 			  int status)
33926c5657d0SVasundhara Volam {
33936c5657d0SVasundhara Volam 	time64_t end = ktime_get_real_seconds();
33946c5657d0SVasundhara Volam 	u32 os_ver_major = 0, os_ver_minor = 0;
33956c5657d0SVasundhara Volam 	struct tm tm;
33966c5657d0SVasundhara Volam 
33976c5657d0SVasundhara Volam 	time64_to_tm(start, 0, &tm);
33986c5657d0SVasundhara Volam 	memset(record, 0, sizeof(*record));
33998605212aSVasundhara Volam 	memcpy(record->signature, "cOrE", 4);
34006c5657d0SVasundhara Volam 	record->flags = 0;
34016c5657d0SVasundhara Volam 	record->low_version = 0;
34026c5657d0SVasundhara Volam 	record->high_version = 1;
34036c5657d0SVasundhara Volam 	record->asic_state = 0;
34043d46eee5SArnd Bergmann 	strlcpy(record->system_name, utsname()->nodename,
34053d46eee5SArnd Bergmann 		sizeof(record->system_name));
34068dc5ae2dSVasundhara Volam 	record->year = cpu_to_le16(tm.tm_year + 1900);
34078dc5ae2dSVasundhara Volam 	record->month = cpu_to_le16(tm.tm_mon + 1);
34086c5657d0SVasundhara Volam 	record->day = cpu_to_le16(tm.tm_mday);
34096c5657d0SVasundhara Volam 	record->hour = cpu_to_le16(tm.tm_hour);
34106c5657d0SVasundhara Volam 	record->minute = cpu_to_le16(tm.tm_min);
34116c5657d0SVasundhara Volam 	record->second = cpu_to_le16(tm.tm_sec);
34126c5657d0SVasundhara Volam 	record->utc_bias = cpu_to_le16(start_utc);
34136c5657d0SVasundhara Volam 	strcpy(record->commandline, "ethtool -w");
34146c5657d0SVasundhara Volam 	record->total_segments = cpu_to_le32(total_segs);
34156c5657d0SVasundhara Volam 
34166c5657d0SVasundhara Volam 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
34176c5657d0SVasundhara Volam 	record->os_ver_major = cpu_to_le32(os_ver_major);
34186c5657d0SVasundhara Volam 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
34196c5657d0SVasundhara Volam 
34208605212aSVasundhara Volam 	strlcpy(record->os_name, utsname()->sysname, 32);
34216c5657d0SVasundhara Volam 	time64_to_tm(end, 0, &tm);
34226c5657d0SVasundhara Volam 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
34236c5657d0SVasundhara Volam 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
34246c5657d0SVasundhara Volam 	record->end_day = cpu_to_le16(tm.tm_mday);
34256c5657d0SVasundhara Volam 	record->end_hour = cpu_to_le16(tm.tm_hour);
34266c5657d0SVasundhara Volam 	record->end_minute = cpu_to_le16(tm.tm_min);
34276c5657d0SVasundhara Volam 	record->end_second = cpu_to_le16(tm.tm_sec);
34286c5657d0SVasundhara Volam 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
34296c5657d0SVasundhara Volam 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
34306c5657d0SVasundhara Volam 				       bp->ver_resp.chip_rev << 8 |
34316c5657d0SVasundhara Volam 				       bp->ver_resp.chip_metal);
34326c5657d0SVasundhara Volam 	record->asic_id2 = 0;
34336c5657d0SVasundhara Volam 	record->coredump_status = cpu_to_le32(status);
34346c5657d0SVasundhara Volam 	record->ioctl_low_version = 0;
34356c5657d0SVasundhara Volam 	record->ioctl_high_version = 0;
34366c5657d0SVasundhara Volam }
34376c5657d0SVasundhara Volam 
34386c5657d0SVasundhara Volam static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
34396c5657d0SVasundhara Volam {
34406c5657d0SVasundhara Volam 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
3441c74751f4SVasundhara Volam 	u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
34426c5657d0SVasundhara Volam 	struct coredump_segment_record *seg_record = NULL;
34436c5657d0SVasundhara Volam 	struct bnxt_coredump_segment_hdr seg_hdr;
34446c5657d0SVasundhara Volam 	struct bnxt_coredump coredump = {NULL};
34456c5657d0SVasundhara Volam 	time64_t start_time;
34466c5657d0SVasundhara Volam 	u16 start_utc;
34476c5657d0SVasundhara Volam 	int rc = 0, i;
34486c5657d0SVasundhara Volam 
3449c74751f4SVasundhara Volam 	if (buf)
3450c74751f4SVasundhara Volam 		buf_len = *dump_len;
3451c74751f4SVasundhara Volam 
34526c5657d0SVasundhara Volam 	start_time = ktime_get_real_seconds();
34536c5657d0SVasundhara Volam 	start_utc = sys_tz.tz_minuteswest * 60;
34546c5657d0SVasundhara Volam 	seg_hdr_len = sizeof(seg_hdr);
34556c5657d0SVasundhara Volam 
34566c5657d0SVasundhara Volam 	/* First segment should be hwrm_ver_get response */
34576c5657d0SVasundhara Volam 	*dump_len = seg_hdr_len + ver_get_resp_len;
34586c5657d0SVasundhara Volam 	if (buf) {
34596c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
34606c5657d0SVasundhara Volam 					   0, 0, 0);
34616c5657d0SVasundhara Volam 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
34626c5657d0SVasundhara Volam 		offset += seg_hdr_len;
34636c5657d0SVasundhara Volam 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
34646c5657d0SVasundhara Volam 		offset += ver_get_resp_len;
34656c5657d0SVasundhara Volam 	}
34666c5657d0SVasundhara Volam 
34676c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
34686c5657d0SVasundhara Volam 	if (rc) {
34696c5657d0SVasundhara Volam 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
34706c5657d0SVasundhara Volam 		goto err;
34716c5657d0SVasundhara Volam 	}
34726c5657d0SVasundhara Volam 
34736c5657d0SVasundhara Volam 	*dump_len += seg_hdr_len * coredump.total_segs;
34746c5657d0SVasundhara Volam 
34756c5657d0SVasundhara Volam 	seg_record = (struct coredump_segment_record *)coredump.data;
34766c5657d0SVasundhara Volam 	seg_record_len = sizeof(*seg_record);
34776c5657d0SVasundhara Volam 
34786c5657d0SVasundhara Volam 	for (i = 0; i < coredump.total_segs; i++) {
34796c5657d0SVasundhara Volam 		u16 comp_id = le16_to_cpu(seg_record->component_id);
34806c5657d0SVasundhara Volam 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
34816c5657d0SVasundhara Volam 		u32 duration = 0, seg_len = 0;
34826c5657d0SVasundhara Volam 		unsigned long start, end;
34836c5657d0SVasundhara Volam 
3484c74751f4SVasundhara Volam 		if (buf && ((offset + seg_hdr_len) >
3485c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(buf_len))) {
3486c74751f4SVasundhara Volam 			rc = -ENOBUFS;
3487c74751f4SVasundhara Volam 			goto err;
3488c74751f4SVasundhara Volam 		}
3489c74751f4SVasundhara Volam 
34906c5657d0SVasundhara Volam 		start = jiffies;
34916c5657d0SVasundhara Volam 
34926c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
34936c5657d0SVasundhara Volam 		if (rc) {
34946c5657d0SVasundhara Volam 			netdev_err(bp->dev,
34956c5657d0SVasundhara Volam 				   "Failed to initiate coredump for seg = %d\n",
34966c5657d0SVasundhara Volam 				   seg_record->segment_id);
34976c5657d0SVasundhara Volam 			goto next_seg;
34986c5657d0SVasundhara Volam 		}
34996c5657d0SVasundhara Volam 
35006c5657d0SVasundhara Volam 		/* Write segment data into the buffer */
35016c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
3502c74751f4SVasundhara Volam 						     &seg_len, buf, buf_len,
35036c5657d0SVasundhara Volam 						     offset + seg_hdr_len);
3504c74751f4SVasundhara Volam 		if (rc && rc == -ENOBUFS)
3505c74751f4SVasundhara Volam 			goto err;
3506c74751f4SVasundhara Volam 		else if (rc)
35076c5657d0SVasundhara Volam 			netdev_err(bp->dev,
35086c5657d0SVasundhara Volam 				   "Failed to retrieve coredump for seg = %d\n",
35096c5657d0SVasundhara Volam 				   seg_record->segment_id);
35106c5657d0SVasundhara Volam 
35116c5657d0SVasundhara Volam next_seg:
35126c5657d0SVasundhara Volam 		end = jiffies;
35136c5657d0SVasundhara Volam 		duration = jiffies_to_msecs(end - start);
35146c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
35156c5657d0SVasundhara Volam 					   rc, duration, 0);
35166c5657d0SVasundhara Volam 
35176c5657d0SVasundhara Volam 		if (buf) {
35186c5657d0SVasundhara Volam 			/* Write segment header into the buffer */
35196c5657d0SVasundhara Volam 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
35206c5657d0SVasundhara Volam 			offset += seg_hdr_len + seg_len;
35216c5657d0SVasundhara Volam 		}
35226c5657d0SVasundhara Volam 
35236c5657d0SVasundhara Volam 		*dump_len += seg_len;
35246c5657d0SVasundhara Volam 		seg_record =
35256c5657d0SVasundhara Volam 			(struct coredump_segment_record *)((u8 *)seg_record +
35266c5657d0SVasundhara Volam 							   seg_record_len);
35276c5657d0SVasundhara Volam 	}
35286c5657d0SVasundhara Volam 
35296c5657d0SVasundhara Volam err:
35301bbf3aedSArnd Bergmann 	if (buf)
35311bbf3aedSArnd Bergmann 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
35326c5657d0SVasundhara Volam 					  start_utc, coredump.total_segs + 1,
35336c5657d0SVasundhara Volam 					  rc);
35346c5657d0SVasundhara Volam 	kfree(coredump.data);
35351bbf3aedSArnd Bergmann 	*dump_len += sizeof(struct bnxt_coredump_record);
3536c74751f4SVasundhara Volam 	if (rc == -ENOBUFS)
35379a005c38SJonathan Lemon 		netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
35386c5657d0SVasundhara Volam 	return rc;
35396c5657d0SVasundhara Volam }
35406c5657d0SVasundhara Volam 
35410b0eacf3SVasundhara Volam static int bnxt_set_dump(struct net_device *dev, struct ethtool_dump *dump)
35420b0eacf3SVasundhara Volam {
35430b0eacf3SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35440b0eacf3SVasundhara Volam 
35450b0eacf3SVasundhara Volam 	if (dump->flag > BNXT_DUMP_CRASH) {
35460b0eacf3SVasundhara Volam 		netdev_info(dev, "Supports only Live(0) and Crash(1) dumps.\n");
35470b0eacf3SVasundhara Volam 		return -EINVAL;
35480b0eacf3SVasundhara Volam 	}
35490b0eacf3SVasundhara Volam 
35500b0eacf3SVasundhara Volam 	if (!IS_ENABLED(CONFIG_TEE_BNXT_FW) && dump->flag == BNXT_DUMP_CRASH) {
35510b0eacf3SVasundhara Volam 		netdev_info(dev, "Cannot collect crash dump as TEE_BNXT_FW config option is not enabled.\n");
35520b0eacf3SVasundhara Volam 		return -EOPNOTSUPP;
35530b0eacf3SVasundhara Volam 	}
35540b0eacf3SVasundhara Volam 
35550b0eacf3SVasundhara Volam 	bp->dump_flag = dump->flag;
35560b0eacf3SVasundhara Volam 	return 0;
35570b0eacf3SVasundhara Volam }
35580b0eacf3SVasundhara Volam 
35596c5657d0SVasundhara Volam static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
35606c5657d0SVasundhara Volam {
35616c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35626c5657d0SVasundhara Volam 
35636c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
35646c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
35656c5657d0SVasundhara Volam 
35666c5657d0SVasundhara Volam 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
35676c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_min_8b << 16 |
35686c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
35696c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_rsvd_8b;
35706c5657d0SVasundhara Volam 
35710b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
35720b0eacf3SVasundhara Volam 	if (bp->dump_flag == BNXT_DUMP_CRASH)
35730b0eacf3SVasundhara Volam 		dump->len = BNXT_CRASH_DUMP_LEN;
35740b0eacf3SVasundhara Volam 	else
35750b0eacf3SVasundhara Volam 		bnxt_get_coredump(bp, NULL, &dump->len);
35760b0eacf3SVasundhara Volam 	return 0;
35776c5657d0SVasundhara Volam }
35786c5657d0SVasundhara Volam 
35796c5657d0SVasundhara Volam static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
35806c5657d0SVasundhara Volam 			      void *buf)
35816c5657d0SVasundhara Volam {
35826c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35836c5657d0SVasundhara Volam 
35846c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
35856c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
35866c5657d0SVasundhara Volam 
35876c5657d0SVasundhara Volam 	memset(buf, 0, dump->len);
35886c5657d0SVasundhara Volam 
35890b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
35900b0eacf3SVasundhara Volam 	if (dump->flag == BNXT_DUMP_CRASH) {
35910b0eacf3SVasundhara Volam #ifdef CONFIG_TEE_BNXT_FW
35920b0eacf3SVasundhara Volam 		return tee_bnxt_copy_coredump(buf, 0, dump->len);
35930b0eacf3SVasundhara Volam #endif
35940b0eacf3SVasundhara Volam 	} else {
35956c5657d0SVasundhara Volam 		return bnxt_get_coredump(bp, buf, &dump->len);
35966c5657d0SVasundhara Volam 	}
35976c5657d0SVasundhara Volam 
35980b0eacf3SVasundhara Volam 	return 0;
35990b0eacf3SVasundhara Volam }
36000b0eacf3SVasundhara Volam 
3601eb513658SMichael Chan void bnxt_ethtool_init(struct bnxt *bp)
3602eb513658SMichael Chan {
3603eb513658SMichael Chan 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3604eb513658SMichael Chan 	struct hwrm_selftest_qlist_input req = {0};
3605eb513658SMichael Chan 	struct bnxt_test_info *test_info;
3606431aa1ebSMichael Chan 	struct net_device *dev = bp->dev;
3607eb513658SMichael Chan 	int i, rc;
3608eb513658SMichael Chan 
3609691aa620SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PKG_VER))
3610a60faa60SVasundhara Volam 		bnxt_get_pkgver(dev);
3611431aa1ebSMichael Chan 
3612ba642ab7SMichael Chan 	bp->num_tests = 0;
3613eb513658SMichael Chan 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp))
3614eb513658SMichael Chan 		return;
3615eb513658SMichael Chan 
3616eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3617eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3618eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3619eb513658SMichael Chan 	if (rc)
3620eb513658SMichael Chan 		goto ethtool_init_exit;
3621eb513658SMichael Chan 
3622ba642ab7SMichael Chan 	test_info = bp->test_info;
3623ba642ab7SMichael Chan 	if (!test_info)
3624eb513658SMichael Chan 		test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
3625eb513658SMichael Chan 	if (!test_info)
3626eb513658SMichael Chan 		goto ethtool_init_exit;
3627eb513658SMichael Chan 
3628eb513658SMichael Chan 	bp->test_info = test_info;
3629eb513658SMichael Chan 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
3630eb513658SMichael Chan 	if (bp->num_tests > BNXT_MAX_TEST)
3631eb513658SMichael Chan 		bp->num_tests = BNXT_MAX_TEST;
3632eb513658SMichael Chan 
3633eb513658SMichael Chan 	test_info->offline_mask = resp->offline_tests;
3634eb513658SMichael Chan 	test_info->timeout = le16_to_cpu(resp->test_timeout);
3635eb513658SMichael Chan 	if (!test_info->timeout)
3636eb513658SMichael Chan 		test_info->timeout = HWRM_CMD_TIMEOUT;
3637eb513658SMichael Chan 	for (i = 0; i < bp->num_tests; i++) {
3638eb513658SMichael Chan 		char *str = test_info->string[i];
3639eb513658SMichael Chan 		char *fw_str = resp->test0_name + i * 32;
3640eb513658SMichael Chan 
3641f7dc1ea6SMichael Chan 		if (i == BNXT_MACLPBK_TEST_IDX) {
3642f7dc1ea6SMichael Chan 			strcpy(str, "Mac loopback test (offline)");
364391725d89SMichael Chan 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
364491725d89SMichael Chan 			strcpy(str, "Phy loopback test (offline)");
364555fd0cf3SMichael Chan 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
364655fd0cf3SMichael Chan 			strcpy(str, "Ext loopback test (offline)");
364767fea463SMichael Chan 		} else if (i == BNXT_IRQ_TEST_IDX) {
364867fea463SMichael Chan 			strcpy(str, "Interrupt_test (offline)");
3649f7dc1ea6SMichael Chan 		} else {
3650eb513658SMichael Chan 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
3651eb513658SMichael Chan 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
3652eb513658SMichael Chan 			if (test_info->offline_mask & (1 << i))
3653eb513658SMichael Chan 				strncat(str, " (offline)",
3654eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3655eb513658SMichael Chan 			else
3656eb513658SMichael Chan 				strncat(str, " (online)",
3657eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3658eb513658SMichael Chan 		}
3659f7dc1ea6SMichael Chan 	}
3660eb513658SMichael Chan 
3661eb513658SMichael Chan ethtool_init_exit:
3662eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3663eb513658SMichael Chan }
3664eb513658SMichael Chan 
3665eb513658SMichael Chan void bnxt_ethtool_free(struct bnxt *bp)
3666eb513658SMichael Chan {
3667eb513658SMichael Chan 	kfree(bp->test_info);
3668eb513658SMichael Chan 	bp->test_info = NULL;
3669eb513658SMichael Chan }
3670eb513658SMichael Chan 
3671c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
3672f704d243SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3673f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES |
3674f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USECS_IRQ |
3675f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
3676f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_STATS_BLOCK_USECS |
3677f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
367800c04a92SMichael Chan 	.get_link_ksettings	= bnxt_get_link_ksettings,
367900c04a92SMichael Chan 	.set_link_ksettings	= bnxt_set_link_ksettings,
3680423cffcfSJakub Kicinski 	.get_pause_stats	= bnxt_get_pause_stats,
3681c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
3682c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
3683c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
3684b5d600b0SVasundhara Volam 	.get_regs_len		= bnxt_get_regs_len,
3685b5d600b0SVasundhara Volam 	.get_regs		= bnxt_get_regs,
36868e202366SMichael Chan 	.get_wol		= bnxt_get_wol,
36875282db6cSMichael Chan 	.set_wol		= bnxt_set_wol,
3688c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
3689c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
3690c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
3691c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
3692c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
3693c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
3694c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
3695c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
3696c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
3697c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
3698c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
3699c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
3700a011952aSMichael Chan 	.set_rxnfc		= bnxt_set_rxnfc,
3701c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
3702c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
3703c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
3704bd3191b5SMichael Chan 	.set_rxfh		= bnxt_set_rxfh,
3705c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
3706c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
3707c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
3708c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
3709c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
371072b34f04SMichael Chan 	.get_eee		= bnxt_get_eee,
371172b34f04SMichael Chan 	.set_eee		= bnxt_set_eee,
371242ee18feSAjit Khaparde 	.get_module_info	= bnxt_get_module_info,
371342ee18feSAjit Khaparde 	.get_module_eeprom	= bnxt_get_module_eeprom,
37145ad2cbeeSMichael Chan 	.nway_reset		= bnxt_nway_reset,
37155ad2cbeeSMichael Chan 	.set_phys_id		= bnxt_set_phys_id,
3716eb513658SMichael Chan 	.self_test		= bnxt_self_test,
371749f7972fSVasundhara Volam 	.reset			= bnxt_reset,
37180b0eacf3SVasundhara Volam 	.set_dump		= bnxt_set_dump,
37196c5657d0SVasundhara Volam 	.get_dump_flag		= bnxt_get_dump_flag,
37206c5657d0SVasundhara Volam 	.get_dump_data		= bnxt_get_dump_data,
3721c0c050c5SMichael Chan };
3722