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",
17578e7b866SMichael Chan };
17678e7b866SMichael Chan 
1779d8b5f05SMichael Chan static const char * const bnxt_rx_sw_stats_str[] = {
178ee79566eSMichael Chan 	"rx_l4_csum_errors",
17919b3751fSMichael Chan 	"rx_buf_errors",
1809d8b5f05SMichael Chan };
1819d8b5f05SMichael Chan 
1829d8b5f05SMichael Chan static const char * const bnxt_cmn_sw_stats_str[] = {
183ee79566eSMichael Chan 	"missed_irqs",
184ee79566eSMichael Chan };
185c0c050c5SMichael Chan 
1868ddc9aaaSMichael Chan #define BNXT_RX_STATS_ENTRY(counter)	\
1878ddc9aaaSMichael Chan 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
1888ddc9aaaSMichael Chan 
1898ddc9aaaSMichael Chan #define BNXT_TX_STATS_ENTRY(counter)	\
1908ddc9aaaSMichael Chan 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
1918ddc9aaaSMichael Chan 
19200db3cbaSVasundhara Volam #define BNXT_RX_STATS_EXT_ENTRY(counter)	\
19300db3cbaSVasundhara Volam 	{ BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
19400db3cbaSVasundhara Volam 
19536e53349SMichael Chan #define BNXT_TX_STATS_EXT_ENTRY(counter)	\
19636e53349SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter), __stringify(counter) }
19736e53349SMichael Chan 
19836e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRY(n)				\
19936e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_duration_us),	\
20036e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_transitions)
20136e53349SMichael Chan 
20236e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRY(n)				\
20336e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_duration_us),	\
20436e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_transitions)
20536e53349SMichael Chan 
20636e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRIES				\
20736e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(0),				\
20836e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(1),				\
20936e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(2),				\
21036e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(3),				\
21136e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(4),				\
21236e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(5),				\
21336e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(6),				\
21436e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(7)
21536e53349SMichael Chan 
21636e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRIES				\
21736e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(0),				\
21836e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(1),				\
21936e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(2),				\
22036e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(3),				\
22136e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(4),				\
22236e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(5),				\
22336e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(6),				\
22436e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(7)
22536e53349SMichael Chan 
22636e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRY(n)				\
22736e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bytes_cos##n),		\
22836e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_packets_cos##n)
22936e53349SMichael Chan 
23036e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRY(n)				\
23136e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_bytes_cos##n),		\
23236e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_packets_cos##n)
23336e53349SMichael Chan 
23436e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRIES				\
23536e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(0),				\
23636e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(1),				\
23736e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(2),				\
23836e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(3),				\
23936e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(4),				\
24036e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(5),				\
24136e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(6),				\
24236e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(7)				\
24336e53349SMichael Chan 
24436e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRIES				\
24536e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(0),				\
24636e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(1),				\
24736e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(2),				\
24836e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(3),				\
24936e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(4),				\
25036e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(5),				\
25136e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(6),				\
25236e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(7)				\
25336e53349SMichael Chan 
2542792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(n)			\
2552792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_bytes_cos##n),	\
2562792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_packets_cos##n)
2572792b5b9SMichael Chan 
2582792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES				\
2592792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(0),				\
2602792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(1),				\
2612792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(2),				\
2622792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(3),				\
2632792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(4),				\
2642792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(5),				\
2652792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(6),				\
2662792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(7)
2672792b5b9SMichael Chan 
268e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRY(counter, n)		\
269e37fed79SMichael Chan 	{ BNXT_RX_STATS_EXT_OFFSET(counter##_cos0),	\
270e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
271e37fed79SMichael Chan 
272e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRY(counter, n)		\
273e37fed79SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter##_cos0),	\
274e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
275e37fed79SMichael Chan 
276e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRIES(counter)		\
277e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 0),		\
278e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 1),		\
279e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 2),		\
280e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 3),		\
281e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 4),		\
282e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 5),		\
283e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 6),		\
284e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 7)
285e37fed79SMichael Chan 
286e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRIES(counter)		\
287e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 0),		\
288e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 1),		\
289e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 2),		\
290e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 3),		\
291e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 4),		\
292e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 5),		\
293e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 6),		\
294e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 7)
295e37fed79SMichael Chan 
29620c1d28eSVasundhara Volam enum {
29720c1d28eSVasundhara Volam 	RX_TOTAL_DISCARDS,
29820c1d28eSVasundhara Volam 	TX_TOTAL_DISCARDS,
29920c1d28eSVasundhara Volam };
30020c1d28eSVasundhara Volam 
30120c1d28eSVasundhara Volam static struct {
30220c1d28eSVasundhara Volam 	u64			counter;
30320c1d28eSVasundhara Volam 	char			string[ETH_GSTRING_LEN];
30420c1d28eSVasundhara Volam } bnxt_sw_func_stats[] = {
30520c1d28eSVasundhara Volam 	{0, "rx_total_discard_pkts"},
30620c1d28eSVasundhara Volam 	{0, "tx_total_discard_pkts"},
30720c1d28eSVasundhara Volam };
30820c1d28eSVasundhara Volam 
3093316d509SMichael Chan #define NUM_RING_RX_SW_STATS		ARRAY_SIZE(bnxt_rx_sw_stats_str)
3103316d509SMichael Chan #define NUM_RING_CMN_SW_STATS		ARRAY_SIZE(bnxt_cmn_sw_stats_str)
3113316d509SMichael Chan #define NUM_RING_RX_HW_STATS		ARRAY_SIZE(bnxt_ring_rx_stats_str)
3123316d509SMichael Chan #define NUM_RING_TX_HW_STATS		ARRAY_SIZE(bnxt_ring_tx_stats_str)
3133316d509SMichael Chan 
3148ddc9aaaSMichael Chan static const struct {
3158ddc9aaaSMichael Chan 	long offset;
3168ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
3178ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
3188ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
3198ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
3208ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
3218ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
3228ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
3236fc92c33SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
3248ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
3258ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
3268ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
3278ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
3288ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
3298ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
3308ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
3318ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
3328ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
3338ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
3348ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
3358ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
3368ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
3378ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
3388ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
3398ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
3408ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
3418ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
3428ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
3438ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
344c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
345c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
346c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
347c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
348c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
349c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
350c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
351c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
3528ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
3538ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
3548ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
3558ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
3568ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
3578ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
358699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
359699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_err),
3608ddc9aaaSMichael Chan 
3618ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
3628ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
3638ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
3648ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
3658ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
3666fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
3678ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
3686fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
3698ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
3708ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
3718ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
3728ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
3738ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
3748ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
3758ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
3768ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
3778ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
3788ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
3798ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
3808ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
3818ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
3828ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
383c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
384c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
385c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
386c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
387c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
388c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
389c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
390c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
3918ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
3928ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
3938ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
3948ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
395699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
396699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
397699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_error),
3988ddc9aaaSMichael Chan };
3998ddc9aaaSMichael Chan 
40000db3cbaSVasundhara Volam static const struct {
40100db3cbaSVasundhara Volam 	long offset;
40200db3cbaSVasundhara Volam 	char string[ETH_GSTRING_LEN];
40300db3cbaSVasundhara Volam } bnxt_port_stats_ext_arr[] = {
40400db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
40500db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
40600db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
40700db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
40800db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
40936e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRIES,
41036e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRIES,
4114a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bits),
4124a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_buffer_passed_threshold),
4134a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_pcs_symbol_err),
4144a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_corrected_bits),
4152792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES,
41636e53349SMichael Chan };
41736e53349SMichael Chan 
41836e53349SMichael Chan static const struct {
41936e53349SMichael Chan 	long offset;
42036e53349SMichael Chan 	char string[ETH_GSTRING_LEN];
42136e53349SMichael Chan } bnxt_tx_port_stats_ext_arr[] = {
42236e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRIES,
42336e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRIES,
42400db3cbaSVasundhara Volam };
42500db3cbaSVasundhara Volam 
426e37fed79SMichael Chan static const struct {
427e37fed79SMichael Chan 	long base_off;
428e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
429e37fed79SMichael Chan } bnxt_rx_bytes_pri_arr[] = {
430e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_bytes),
431e37fed79SMichael Chan };
432e37fed79SMichael Chan 
433e37fed79SMichael Chan static const struct {
434e37fed79SMichael Chan 	long base_off;
435e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
436e37fed79SMichael Chan } bnxt_rx_pkts_pri_arr[] = {
437e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_packets),
438e37fed79SMichael Chan };
439e37fed79SMichael Chan 
440e37fed79SMichael Chan static const struct {
441e37fed79SMichael Chan 	long base_off;
442e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
443e37fed79SMichael Chan } bnxt_tx_bytes_pri_arr[] = {
444e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_bytes),
445e37fed79SMichael Chan };
446e37fed79SMichael Chan 
447e37fed79SMichael Chan static const struct {
448e37fed79SMichael Chan 	long base_off;
449e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
450e37fed79SMichael Chan } bnxt_tx_pkts_pri_arr[] = {
451e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_packets),
452e37fed79SMichael Chan };
453e37fed79SMichael Chan 
45420c1d28eSVasundhara Volam #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
4558ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
456e37fed79SMichael Chan #define BNXT_NUM_STATS_PRI			\
457e37fed79SMichael Chan 	(ARRAY_SIZE(bnxt_rx_bytes_pri_arr) +	\
458e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_rx_pkts_pri_arr) +	\
459e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_bytes_pri_arr) +	\
460e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_pkts_pri_arr))
4618ddc9aaaSMichael Chan 
46278e7b866SMichael Chan static int bnxt_get_num_tpa_ring_stats(struct bnxt *bp)
46378e7b866SMichael Chan {
46478e7b866SMichael Chan 	if (BNXT_SUPPORTS_TPA(bp)) {
46578e7b866SMichael Chan 		if (bp->max_tpa_v2)
46678e7b866SMichael Chan 			return ARRAY_SIZE(bnxt_ring_tpa2_stats_str);
46778e7b866SMichael Chan 		return ARRAY_SIZE(bnxt_ring_tpa_stats_str);
46878e7b866SMichael Chan 	}
46978e7b866SMichael Chan 	return 0;
47078e7b866SMichael Chan }
47178e7b866SMichael Chan 
472ee79566eSMichael Chan static int bnxt_get_num_ring_stats(struct bnxt *bp)
473ee79566eSMichael Chan {
4743316d509SMichael Chan 	int rx, tx, cmn;
475ee79566eSMichael Chan 
4763316d509SMichael Chan 	rx = NUM_RING_RX_HW_STATS + NUM_RING_RX_SW_STATS +
47778e7b866SMichael Chan 	     bnxt_get_num_tpa_ring_stats(bp);
4783316d509SMichael Chan 	tx = NUM_RING_TX_HW_STATS;
4793316d509SMichael Chan 	cmn = NUM_RING_CMN_SW_STATS;
480125592fbSRajesh Ravi 	return rx * bp->rx_nr_rings + tx * bp->tx_nr_rings +
481125592fbSRajesh Ravi 	       cmn * bp->cp_nr_rings;
482ee79566eSMichael Chan }
483ee79566eSMichael Chan 
4845c8227d0SMichael Chan static int bnxt_get_num_stats(struct bnxt *bp)
485c0c050c5SMichael Chan {
486ee79566eSMichael Chan 	int num_stats = bnxt_get_num_ring_stats(bp);
4878ddc9aaaSMichael Chan 
48820c1d28eSVasundhara Volam 	num_stats += BNXT_NUM_SW_FUNC_STATS;
48920c1d28eSVasundhara Volam 
4908ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS)
4918ddc9aaaSMichael Chan 		num_stats += BNXT_NUM_PORT_STATS;
4928ddc9aaaSMichael Chan 
493e37fed79SMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
49436e53349SMichael Chan 		num_stats += bp->fw_rx_stats_ext_size +
49536e53349SMichael Chan 			     bp->fw_tx_stats_ext_size;
496e37fed79SMichael Chan 		if (bp->pri2cos_valid)
497e37fed79SMichael Chan 			num_stats += BNXT_NUM_STATS_PRI;
498e37fed79SMichael Chan 	}
49900db3cbaSVasundhara Volam 
5008ddc9aaaSMichael Chan 	return num_stats;
5018ddc9aaaSMichael Chan }
5025c8227d0SMichael Chan 
5035c8227d0SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
5045c8227d0SMichael Chan {
5055c8227d0SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5065c8227d0SMichael Chan 
5075c8227d0SMichael Chan 	switch (sset) {
5085c8227d0SMichael Chan 	case ETH_SS_STATS:
5095c8227d0SMichael Chan 		return bnxt_get_num_stats(bp);
510eb513658SMichael Chan 	case ETH_SS_TEST:
511eb513658SMichael Chan 		if (!bp->num_tests)
512eb513658SMichael Chan 			return -EOPNOTSUPP;
513eb513658SMichael Chan 		return bp->num_tests;
514c0c050c5SMichael Chan 	default:
515c0c050c5SMichael Chan 		return -EOPNOTSUPP;
516c0c050c5SMichael Chan 	}
517c0c050c5SMichael Chan }
518c0c050c5SMichael Chan 
519125592fbSRajesh Ravi static bool is_rx_ring(struct bnxt *bp, int ring_num)
520125592fbSRajesh Ravi {
521125592fbSRajesh Ravi 	return ring_num < bp->rx_nr_rings;
522125592fbSRajesh Ravi }
523125592fbSRajesh Ravi 
524125592fbSRajesh Ravi static bool is_tx_ring(struct bnxt *bp, int ring_num)
525125592fbSRajesh Ravi {
526125592fbSRajesh Ravi 	int tx_base = 0;
527125592fbSRajesh Ravi 
528125592fbSRajesh Ravi 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
529125592fbSRajesh Ravi 		tx_base = bp->rx_nr_rings;
530125592fbSRajesh Ravi 
531125592fbSRajesh Ravi 	if (ring_num >= tx_base && ring_num < (tx_base + bp->tx_nr_rings))
532125592fbSRajesh Ravi 		return true;
533125592fbSRajesh Ravi 	return false;
534125592fbSRajesh Ravi }
535125592fbSRajesh Ravi 
536c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
537c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
538c0c050c5SMichael Chan {
539c0c050c5SMichael Chan 	u32 i, j = 0;
540c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
541125592fbSRajesh Ravi 	u32 tpa_stats;
542c0c050c5SMichael Chan 
543fd3ab1c7SMichael Chan 	if (!bp->bnapi) {
544ee79566eSMichael Chan 		j += bnxt_get_num_ring_stats(bp) + BNXT_NUM_SW_FUNC_STATS;
545fd3ab1c7SMichael Chan 		goto skip_ring_stats;
546fd3ab1c7SMichael Chan 	}
547c0c050c5SMichael Chan 
54820c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
54920c1d28eSVasundhara Volam 		bnxt_sw_func_stats[i].counter = 0;
55020c1d28eSVasundhara Volam 
551125592fbSRajesh Ravi 	tpa_stats = bnxt_get_num_tpa_ring_stats(bp);
552c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
553c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
554c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
555a0c30621SMichael Chan 		u64 *sw_stats = cpr->stats.sw_stats;
5569d8b5f05SMichael Chan 		u64 *sw;
557c0c050c5SMichael Chan 		int k;
558c0c050c5SMichael Chan 
559125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
560125592fbSRajesh Ravi 			for (k = 0; k < NUM_RING_RX_HW_STATS; j++, k++)
561a0c30621SMichael Chan 				buf[j] = sw_stats[k];
562125592fbSRajesh Ravi 		}
563125592fbSRajesh Ravi 		if (is_tx_ring(bp, i)) {
564125592fbSRajesh Ravi 			k = NUM_RING_RX_HW_STATS;
565125592fbSRajesh Ravi 			for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
566125592fbSRajesh Ravi 			       j++, k++)
567a0c30621SMichael Chan 				buf[j] = sw_stats[k];
568125592fbSRajesh Ravi 		}
569125592fbSRajesh Ravi 		if (!tpa_stats || !is_rx_ring(bp, i))
570125592fbSRajesh Ravi 			goto skip_tpa_ring_stats;
571125592fbSRajesh Ravi 
572125592fbSRajesh Ravi 		k = NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
573125592fbSRajesh Ravi 		for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS +
574125592fbSRajesh Ravi 			   tpa_stats; j++, k++)
575a0c30621SMichael Chan 			buf[j] = sw_stats[k];
5769d8b5f05SMichael Chan 
577125592fbSRajesh Ravi skip_tpa_ring_stats:
5789d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.rx;
579125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
5803316d509SMichael Chan 			for (k = 0; k < NUM_RING_RX_SW_STATS; j++, k++)
5819d8b5f05SMichael Chan 				buf[j] = sw[k];
582125592fbSRajesh Ravi 		}
5839d8b5f05SMichael Chan 
5849d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.cmn;
5853316d509SMichael Chan 		for (k = 0; k < NUM_RING_CMN_SW_STATS; j++, k++)
5869d8b5f05SMichael Chan 			buf[j] = sw[k];
58720c1d28eSVasundhara Volam 
58820c1d28eSVasundhara Volam 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
589a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, rx_discard_pkts);
59020c1d28eSVasundhara Volam 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
591a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, tx_discard_pkts);
592c0c050c5SMichael Chan 	}
59320c1d28eSVasundhara Volam 
59420c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
59520c1d28eSVasundhara Volam 		buf[j] = bnxt_sw_func_stats[i].counter;
59620c1d28eSVasundhara Volam 
597fd3ab1c7SMichael Chan skip_ring_stats:
5988ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
599a0c30621SMichael Chan 		u64 *port_stats = bp->port_stats.sw_stats;
6008ddc9aaaSMichael Chan 
601a0c30621SMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++)
602a0c30621SMichael Chan 			buf[j] = *(port_stats + bnxt_port_stats_arr[i].offset);
6038ddc9aaaSMichael Chan 	}
60400db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
605a0c30621SMichael Chan 		u64 *rx_port_stats_ext = bp->rx_port_stats_ext.sw_stats;
606a0c30621SMichael Chan 		u64 *tx_port_stats_ext = bp->tx_port_stats_ext.sw_stats;
60700db3cbaSVasundhara Volam 
60836e53349SMichael Chan 		for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
609a0c30621SMichael Chan 			buf[j] = *(rx_port_stats_ext +
610a0c30621SMichael Chan 				   bnxt_port_stats_ext_arr[i].offset);
61100db3cbaSVasundhara Volam 		}
61236e53349SMichael Chan 		for (i = 0; i < bp->fw_tx_stats_ext_size; i++, j++) {
613a0c30621SMichael Chan 			buf[j] = *(tx_port_stats_ext +
614a0c30621SMichael Chan 				   bnxt_tx_port_stats_ext_arr[i].offset);
61536e53349SMichael Chan 		}
616e37fed79SMichael Chan 		if (bp->pri2cos_valid) {
617e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
618e37fed79SMichael Chan 				long n = bnxt_rx_bytes_pri_arr[i].base_off +
619a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
620e37fed79SMichael Chan 
621a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
622e37fed79SMichael Chan 			}
623e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
624e37fed79SMichael Chan 				long n = bnxt_rx_pkts_pri_arr[i].base_off +
625a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
626e37fed79SMichael Chan 
627a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
628e37fed79SMichael Chan 			}
629e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
630e37fed79SMichael Chan 				long n = bnxt_tx_bytes_pri_arr[i].base_off +
631a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
632e37fed79SMichael Chan 
633a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
634e37fed79SMichael Chan 			}
635e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
636e37fed79SMichael Chan 				long n = bnxt_tx_pkts_pri_arr[i].base_off +
637a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
638e37fed79SMichael Chan 
639a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
640e37fed79SMichael Chan 			}
641e37fed79SMichael Chan 		}
64200db3cbaSVasundhara Volam 	}
643c0c050c5SMichael Chan }
644c0c050c5SMichael Chan 
645c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
646c0c050c5SMichael Chan {
647c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
64878e7b866SMichael Chan 	static const char * const *str;
649ee79566eSMichael Chan 	u32 i, j, num_str;
650c0c050c5SMichael Chan 
651c0c050c5SMichael Chan 	switch (stringset) {
652c0c050c5SMichael Chan 	case ETH_SS_STATS:
653c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
654125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6553316d509SMichael Chan 				num_str = NUM_RING_RX_HW_STATS;
656ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
657ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
6583316d509SMichael Chan 						bnxt_ring_rx_stats_str[j]);
659c0c050c5SMichael Chan 					buf += ETH_GSTRING_LEN;
660ee79566eSMichael Chan 				}
661125592fbSRajesh Ravi 			}
662125592fbSRajesh Ravi 			if (is_tx_ring(bp, i)) {
6633316d509SMichael Chan 				num_str = NUM_RING_TX_HW_STATS;
6643316d509SMichael Chan 				for (j = 0; j < num_str; j++) {
6653316d509SMichael Chan 					sprintf(buf, "[%d]: %s", i,
6663316d509SMichael Chan 						bnxt_ring_tx_stats_str[j]);
6673316d509SMichael Chan 					buf += ETH_GSTRING_LEN;
6683316d509SMichael Chan 				}
669125592fbSRajesh Ravi 			}
6703316d509SMichael Chan 			num_str = bnxt_get_num_tpa_ring_stats(bp);
671125592fbSRajesh Ravi 			if (!num_str || !is_rx_ring(bp, i))
672ee79566eSMichael Chan 				goto skip_tpa_stats;
673ee79566eSMichael Chan 
6743316d509SMichael Chan 			if (bp->max_tpa_v2)
67578e7b866SMichael Chan 				str = bnxt_ring_tpa2_stats_str;
6763316d509SMichael Chan 			else
67778e7b866SMichael Chan 				str = bnxt_ring_tpa_stats_str;
6783316d509SMichael Chan 
679ee79566eSMichael Chan 			for (j = 0; j < num_str; j++) {
68078e7b866SMichael Chan 				sprintf(buf, "[%d]: %s", i, str[j]);
681c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
682ee79566eSMichael Chan 			}
683ee79566eSMichael Chan skip_tpa_stats:
684125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6853316d509SMichael Chan 				num_str = NUM_RING_RX_SW_STATS;
686ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
687ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
6889d8b5f05SMichael Chan 						bnxt_rx_sw_stats_str[j]);
6899d8b5f05SMichael Chan 					buf += ETH_GSTRING_LEN;
6909d8b5f05SMichael Chan 				}
691125592fbSRajesh Ravi 			}
6923316d509SMichael Chan 			num_str = NUM_RING_CMN_SW_STATS;
6939d8b5f05SMichael Chan 			for (j = 0; j < num_str; j++) {
6949d8b5f05SMichael Chan 				sprintf(buf, "[%d]: %s", i,
6959d8b5f05SMichael Chan 					bnxt_cmn_sw_stats_str[j]);
696c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
697ee79566eSMichael Chan 			}
698c0c050c5SMichael Chan 		}
69920c1d28eSVasundhara Volam 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
70020c1d28eSVasundhara Volam 			strcpy(buf, bnxt_sw_func_stats[i].string);
70120c1d28eSVasundhara Volam 			buf += ETH_GSTRING_LEN;
70220c1d28eSVasundhara Volam 		}
70320c1d28eSVasundhara Volam 
7048ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
7058ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
7068ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
7078ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
7088ddc9aaaSMichael Chan 			}
7098ddc9aaaSMichael Chan 		}
71000db3cbaSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
71136e53349SMichael Chan 			for (i = 0; i < bp->fw_rx_stats_ext_size; i++) {
71200db3cbaSVasundhara Volam 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
71300db3cbaSVasundhara Volam 				buf += ETH_GSTRING_LEN;
71400db3cbaSVasundhara Volam 			}
71536e53349SMichael Chan 			for (i = 0; i < bp->fw_tx_stats_ext_size; i++) {
71636e53349SMichael Chan 				strcpy(buf,
71736e53349SMichael Chan 				       bnxt_tx_port_stats_ext_arr[i].string);
71836e53349SMichael Chan 				buf += ETH_GSTRING_LEN;
71936e53349SMichael Chan 			}
720e37fed79SMichael Chan 			if (bp->pri2cos_valid) {
721e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
722e37fed79SMichael Chan 					strcpy(buf,
723e37fed79SMichael Chan 					       bnxt_rx_bytes_pri_arr[i].string);
724e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
725e37fed79SMichael Chan 				}
726e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
727e37fed79SMichael Chan 					strcpy(buf,
728e37fed79SMichael Chan 					       bnxt_rx_pkts_pri_arr[i].string);
729e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
730e37fed79SMichael Chan 				}
731e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
732e37fed79SMichael Chan 					strcpy(buf,
733e37fed79SMichael Chan 					       bnxt_tx_bytes_pri_arr[i].string);
734e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
735e37fed79SMichael Chan 				}
736e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
737e37fed79SMichael Chan 					strcpy(buf,
738e37fed79SMichael Chan 					       bnxt_tx_pkts_pri_arr[i].string);
739e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
740e37fed79SMichael Chan 				}
741e37fed79SMichael Chan 			}
74200db3cbaSVasundhara Volam 		}
743c0c050c5SMichael Chan 		break;
744eb513658SMichael Chan 	case ETH_SS_TEST:
745eb513658SMichael Chan 		if (bp->num_tests)
746eb513658SMichael Chan 			memcpy(buf, bp->test_info->string,
747eb513658SMichael Chan 			       bp->num_tests * ETH_GSTRING_LEN);
748eb513658SMichael Chan 		break;
749c0c050c5SMichael Chan 	default:
750c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
751c0c050c5SMichael Chan 			   stringset);
752c0c050c5SMichael Chan 		break;
753c0c050c5SMichael Chan 	}
754c0c050c5SMichael Chan }
755c0c050c5SMichael Chan 
756c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
757c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
758c0c050c5SMichael Chan {
759c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
760c0c050c5SMichael Chan 
761c0c050c5SMichael Chan 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
762c0c050c5SMichael Chan 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
763c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
764c0c050c5SMichael Chan 
765c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
766c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
767c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
768c0c050c5SMichael Chan }
769c0c050c5SMichael Chan 
770c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
771c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
772c0c050c5SMichael Chan {
773c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
774c0c050c5SMichael Chan 
775c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
776c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
777c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
778c0c050c5SMichael Chan 		return -EINVAL;
779c0c050c5SMichael Chan 
780c0c050c5SMichael Chan 	if (netif_running(dev))
781c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
782c0c050c5SMichael Chan 
783c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
784c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
785c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
786c0c050c5SMichael Chan 
787c0c050c5SMichael Chan 	if (netif_running(dev))
788c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
789c0c050c5SMichael Chan 
790c0c050c5SMichael Chan 	return 0;
791c0c050c5SMichael Chan }
792c0c050c5SMichael Chan 
793c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
794c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
795c0c050c5SMichael Chan {
796c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
797db4723b3SMichael Chan 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
798c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
799db4723b3SMichael Chan 	int max_tx_sch_inputs;
800db4723b3SMichael Chan 
801db4723b3SMichael Chan 	/* Get the most up-to-date max_tx_sch_inputs. */
802c1c2d774SPavan Chebbi 	if (netif_running(dev) && BNXT_NEW_RM(bp))
803db4723b3SMichael Chan 		bnxt_hwrm_func_resc_qcaps(bp, false);
804db4723b3SMichael Chan 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
805c0c050c5SMichael Chan 
8066e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
807db4723b3SMichael Chan 	if (max_tx_sch_inputs)
808db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
809a79a5276SMichael Chan 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
810068c9ec6SMichael Chan 
81118d6e4e2SSatish Baddipadige 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
81218d6e4e2SSatish Baddipadige 		max_rx_rings = 0;
81318d6e4e2SSatish Baddipadige 		max_tx_rings = 0;
81418d6e4e2SSatish Baddipadige 	}
815db4723b3SMichael Chan 	if (max_tx_sch_inputs)
816db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
81718d6e4e2SSatish Baddipadige 
818c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
819c0c050c5SMichael Chan 	if (tcs > 1)
820c0c050c5SMichael Chan 		max_tx_rings /= tcs;
821c0c050c5SMichael Chan 
822c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
823c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
824c0c050c5SMichael Chan 	channel->max_other = 0;
825068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
826068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
82776595193SPrashant Sreedharan 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
82876595193SPrashant Sreedharan 			channel->combined_count--;
829068c9ec6SMichael Chan 	} else {
83076595193SPrashant Sreedharan 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
831c0c050c5SMichael Chan 			channel->rx_count = bp->rx_nr_rings;
832c0c050c5SMichael Chan 			channel->tx_count = bp->tx_nr_rings_per_tc;
833c0c050c5SMichael Chan 		}
834068c9ec6SMichael Chan 	}
83576595193SPrashant Sreedharan }
836c0c050c5SMichael Chan 
837c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
838c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
839c0c050c5SMichael Chan {
840c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
841d1e7925eSMichael Chan 	int req_tx_rings, req_rx_rings, tcs;
842068c9ec6SMichael Chan 	bool sh = false;
8435f449249SMichael Chan 	int tx_xdp = 0;
844d1e7925eSMichael Chan 	int rc = 0;
845c0c050c5SMichael Chan 
846068c9ec6SMichael Chan 	if (channel->other_count)
847c0c050c5SMichael Chan 		return -EINVAL;
848c0c050c5SMichael Chan 
849068c9ec6SMichael Chan 	if (!channel->combined_count &&
850068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
851068c9ec6SMichael Chan 		return -EINVAL;
852068c9ec6SMichael Chan 
853068c9ec6SMichael Chan 	if (channel->combined_count &&
854068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
855068c9ec6SMichael Chan 		return -EINVAL;
856068c9ec6SMichael Chan 
85776595193SPrashant Sreedharan 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
85876595193SPrashant Sreedharan 					    channel->tx_count))
85976595193SPrashant Sreedharan 		return -EINVAL;
86076595193SPrashant Sreedharan 
861068c9ec6SMichael Chan 	if (channel->combined_count)
862068c9ec6SMichael Chan 		sh = true;
863068c9ec6SMichael Chan 
864c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
865c0c050c5SMichael Chan 
866391be5c2SMichael Chan 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
867d1e7925eSMichael Chan 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
8685f449249SMichael Chan 	if (bp->tx_nr_rings_xdp) {
8695f449249SMichael Chan 		if (!sh) {
8705f449249SMichael Chan 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
8715f449249SMichael Chan 			return -EINVAL;
8725f449249SMichael Chan 		}
8735f449249SMichael Chan 		tx_xdp = req_rx_rings;
8745f449249SMichael Chan 	}
87598fdbe73SMichael Chan 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
876d1e7925eSMichael Chan 	if (rc) {
877d1e7925eSMichael Chan 		netdev_warn(dev, "Unable to allocate the requested rings\n");
878d1e7925eSMichael Chan 		return rc;
879391be5c2SMichael Chan 	}
880391be5c2SMichael Chan 
881bd3191b5SMichael Chan 	if (bnxt_get_nr_rss_ctxs(bp, req_rx_rings) !=
882bd3191b5SMichael Chan 	    bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) &&
883bd3191b5SMichael Chan 	    (dev->priv_flags & IFF_RXFH_CONFIGURED)) {
884bd3191b5SMichael Chan 		netdev_warn(dev, "RSS table size change required, RSS table entries must be default to proceed\n");
885bd3191b5SMichael Chan 		return -EINVAL;
886bd3191b5SMichael Chan 	}
887bd3191b5SMichael Chan 
888c0c050c5SMichael Chan 	if (netif_running(dev)) {
889c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
890c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
891c0c050c5SMichael Chan 			 * before PF unload
892c0c050c5SMichael Chan 			 */
893c0c050c5SMichael Chan 		}
894c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
895c0c050c5SMichael Chan 		if (rc) {
896c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
897c0c050c5SMichael Chan 				   rc);
898c0c050c5SMichael Chan 			return rc;
899c0c050c5SMichael Chan 		}
900c0c050c5SMichael Chan 	}
901c0c050c5SMichael Chan 
902068c9ec6SMichael Chan 	if (sh) {
903068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
904d1e7925eSMichael Chan 		bp->rx_nr_rings = channel->combined_count;
905d1e7925eSMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
906068c9ec6SMichael Chan 	} else {
907068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
908c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
909c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
910068c9ec6SMichael Chan 	}
9115f449249SMichael Chan 	bp->tx_nr_rings_xdp = tx_xdp;
9125f449249SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
913c0c050c5SMichael Chan 	if (tcs > 1)
9145f449249SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
915068c9ec6SMichael Chan 
916068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
917068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
918068c9ec6SMichael Chan 
9192bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
9202bcfa6f6SMichael Chan 	netdev_update_features(dev);
921c0c050c5SMichael Chan 	if (netif_running(dev)) {
922c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
923c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
924c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
925c0c050c5SMichael Chan 			 * to renable
926c0c050c5SMichael Chan 			 */
927c0c050c5SMichael Chan 		}
928d8c09f19SMichael Chan 	} else {
9291b3f0b75SMichael Chan 		rc = bnxt_reserve_rings(bp, true);
930c0c050c5SMichael Chan 	}
931c0c050c5SMichael Chan 
932c0c050c5SMichael Chan 	return rc;
933c0c050c5SMichael Chan }
934c0c050c5SMichael Chan 
935c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
936c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
937c0c050c5SMichael Chan 			    u32 *rule_locs)
938c0c050c5SMichael Chan {
939c0c050c5SMichael Chan 	int i, j = 0;
940c0c050c5SMichael Chan 
941c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
942c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
943c0c050c5SMichael Chan 		struct hlist_head *head;
944c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
945c0c050c5SMichael Chan 
946c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
947c0c050c5SMichael Chan 		rcu_read_lock();
948c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
949c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
950c0c050c5SMichael Chan 				break;
951c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
952c0c050c5SMichael Chan 		}
953c0c050c5SMichael Chan 		rcu_read_unlock();
954c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
955c0c050c5SMichael Chan 			break;
956c0c050c5SMichael Chan 	}
957c0c050c5SMichael Chan 	cmd->rule_cnt = j;
958c0c050c5SMichael Chan 	return 0;
959c0c050c5SMichael Chan }
960c0c050c5SMichael Chan 
961c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
962c0c050c5SMichael Chan {
963c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
964c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
965c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
966c0c050c5SMichael Chan 	struct flow_keys *fkeys;
967c0c050c5SMichael Chan 	int i, rc = -EINVAL;
968c0c050c5SMichael Chan 
969b721cfafSstephen hemminger 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
970c0c050c5SMichael Chan 		return rc;
971c0c050c5SMichael Chan 
972c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
973c0c050c5SMichael Chan 		struct hlist_head *head;
974c0c050c5SMichael Chan 
975c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
976c0c050c5SMichael Chan 		rcu_read_lock();
977c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
978c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
979c0c050c5SMichael Chan 				goto fltr_found;
980c0c050c5SMichael Chan 		}
981c0c050c5SMichael Chan 		rcu_read_unlock();
982c0c050c5SMichael Chan 	}
983c0c050c5SMichael Chan 	return rc;
984c0c050c5SMichael Chan 
985c0c050c5SMichael Chan fltr_found:
986c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
987dda0e746SMichael Chan 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
988c0c050c5SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
989c0c050c5SMichael Chan 			fs->flow_type = TCP_V4_FLOW;
990c0c050c5SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
991c0c050c5SMichael Chan 			fs->flow_type = UDP_V4_FLOW;
992c0c050c5SMichael Chan 		else
993c0c050c5SMichael Chan 			goto fltr_err;
994c0c050c5SMichael Chan 
995c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
996c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
997c0c050c5SMichael Chan 
998c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
999c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
1000c0c050c5SMichael Chan 
1001c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
1002c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
1003c0c050c5SMichael Chan 
1004c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
1005c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
1006dda0e746SMichael Chan 	} else {
1007dda0e746SMichael Chan 		int i;
1008dda0e746SMichael Chan 
1009dda0e746SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1010dda0e746SMichael Chan 			fs->flow_type = TCP_V6_FLOW;
1011dda0e746SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1012dda0e746SMichael Chan 			fs->flow_type = UDP_V6_FLOW;
1013dda0e746SMichael Chan 		else
1014dda0e746SMichael Chan 			goto fltr_err;
1015dda0e746SMichael Chan 
1016dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
1017dda0e746SMichael Chan 			fkeys->addrs.v6addrs.src;
1018dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
1019dda0e746SMichael Chan 			fkeys->addrs.v6addrs.dst;
1020dda0e746SMichael Chan 		for (i = 0; i < 4; i++) {
1021dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
1022dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
1023dda0e746SMichael Chan 		}
1024dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
1025dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
1026dda0e746SMichael Chan 
1027dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
1028dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
1029dda0e746SMichael Chan 	}
1030c0c050c5SMichael Chan 
1031c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
1032c0c050c5SMichael Chan 	rc = 0;
1033c0c050c5SMichael Chan 
1034c0c050c5SMichael Chan fltr_err:
1035c0c050c5SMichael Chan 	rcu_read_unlock();
1036c0c050c5SMichael Chan 
1037c0c050c5SMichael Chan 	return rc;
1038c0c050c5SMichael Chan }
1039a011952aSMichael Chan #endif
1040a011952aSMichael Chan 
1041a011952aSMichael Chan static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
1042a011952aSMichael Chan {
1043a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
1044a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1045a011952aSMichael Chan 	return 0;
1046a011952aSMichael Chan }
1047a011952aSMichael Chan 
1048a011952aSMichael Chan static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
1049a011952aSMichael Chan {
1050a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
1051a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1052a011952aSMichael Chan 	return 0;
1053a011952aSMichael Chan }
1054a011952aSMichael Chan 
1055a011952aSMichael Chan static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1056a011952aSMichael Chan {
1057a011952aSMichael Chan 	cmd->data = 0;
1058a011952aSMichael Chan 	switch (cmd->flow_type) {
1059a011952aSMichael Chan 	case TCP_V4_FLOW:
1060a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
1061a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1062a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1063a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1064a011952aSMichael Chan 		break;
1065a011952aSMichael Chan 	case UDP_V4_FLOW:
1066a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
1067a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1068a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1069df561f66SGustavo A. R. Silva 		fallthrough;
1070a011952aSMichael Chan 	case SCTP_V4_FLOW:
1071a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1072a011952aSMichael Chan 	case AH_V4_FLOW:
1073a011952aSMichael Chan 	case ESP_V4_FLOW:
1074a011952aSMichael Chan 	case IPV4_FLOW:
1075a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1076a011952aSMichael Chan 		break;
1077a011952aSMichael Chan 
1078a011952aSMichael Chan 	case TCP_V6_FLOW:
1079a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
1080a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1081a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1082a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1083a011952aSMichael Chan 		break;
1084a011952aSMichael Chan 	case UDP_V6_FLOW:
1085a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
1086a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1087a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1088df561f66SGustavo A. R. Silva 		fallthrough;
1089a011952aSMichael Chan 	case SCTP_V6_FLOW:
1090a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1091a011952aSMichael Chan 	case AH_V6_FLOW:
1092a011952aSMichael Chan 	case ESP_V6_FLOW:
1093a011952aSMichael Chan 	case IPV6_FLOW:
1094a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1095a011952aSMichael Chan 		break;
1096a011952aSMichael Chan 	}
1097a011952aSMichael Chan 	return 0;
1098a011952aSMichael Chan }
1099a011952aSMichael Chan 
1100a011952aSMichael Chan #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1101a011952aSMichael Chan #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
1102a011952aSMichael Chan 
1103a011952aSMichael Chan static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1104a011952aSMichael Chan {
1105a011952aSMichael Chan 	u32 rss_hash_cfg = bp->rss_hash_cfg;
1106a011952aSMichael Chan 	int tuple, rc = 0;
1107a011952aSMichael Chan 
1108a011952aSMichael Chan 	if (cmd->data == RXH_4TUPLE)
1109a011952aSMichael Chan 		tuple = 4;
1110a011952aSMichael Chan 	else if (cmd->data == RXH_2TUPLE)
1111a011952aSMichael Chan 		tuple = 2;
1112a011952aSMichael Chan 	else if (!cmd->data)
1113a011952aSMichael Chan 		tuple = 0;
1114a011952aSMichael Chan 	else
1115a011952aSMichael Chan 		return -EINVAL;
1116a011952aSMichael Chan 
1117a011952aSMichael Chan 	if (cmd->flow_type == TCP_V4_FLOW) {
1118a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1119a011952aSMichael Chan 		if (tuple == 4)
1120a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1121a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V4_FLOW) {
1122a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1123a011952aSMichael Chan 			return -EINVAL;
1124a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1125a011952aSMichael Chan 		if (tuple == 4)
1126a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1127a011952aSMichael Chan 	} else if (cmd->flow_type == TCP_V6_FLOW) {
1128a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1129a011952aSMichael Chan 		if (tuple == 4)
1130a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1131a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V6_FLOW) {
1132a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1133a011952aSMichael Chan 			return -EINVAL;
1134a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1135a011952aSMichael Chan 		if (tuple == 4)
1136a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1137a011952aSMichael Chan 	} else if (tuple == 4) {
1138a011952aSMichael Chan 		return -EINVAL;
1139a011952aSMichael Chan 	}
1140a011952aSMichael Chan 
1141a011952aSMichael Chan 	switch (cmd->flow_type) {
1142a011952aSMichael Chan 	case TCP_V4_FLOW:
1143a011952aSMichael Chan 	case UDP_V4_FLOW:
1144a011952aSMichael Chan 	case SCTP_V4_FLOW:
1145a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1146a011952aSMichael Chan 	case AH_V4_FLOW:
1147a011952aSMichael Chan 	case ESP_V4_FLOW:
1148a011952aSMichael Chan 	case IPV4_FLOW:
1149a011952aSMichael Chan 		if (tuple == 2)
1150a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1151a011952aSMichael Chan 		else if (!tuple)
1152a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1153a011952aSMichael Chan 		break;
1154a011952aSMichael Chan 
1155a011952aSMichael Chan 	case TCP_V6_FLOW:
1156a011952aSMichael Chan 	case UDP_V6_FLOW:
1157a011952aSMichael Chan 	case SCTP_V6_FLOW:
1158a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1159a011952aSMichael Chan 	case AH_V6_FLOW:
1160a011952aSMichael Chan 	case ESP_V6_FLOW:
1161a011952aSMichael Chan 	case IPV6_FLOW:
1162a011952aSMichael Chan 		if (tuple == 2)
1163a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1164a011952aSMichael Chan 		else if (!tuple)
1165a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1166a011952aSMichael Chan 		break;
1167a011952aSMichael Chan 	}
1168a011952aSMichael Chan 
1169a011952aSMichael Chan 	if (bp->rss_hash_cfg == rss_hash_cfg)
1170a011952aSMichael Chan 		return 0;
1171a011952aSMichael Chan 
1172a011952aSMichael Chan 	bp->rss_hash_cfg = rss_hash_cfg;
1173a011952aSMichael Chan 	if (netif_running(bp->dev)) {
1174a011952aSMichael Chan 		bnxt_close_nic(bp, false, false);
1175a011952aSMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1176a011952aSMichael Chan 	}
1177a011952aSMichael Chan 	return rc;
1178a011952aSMichael Chan }
1179c0c050c5SMichael Chan 
1180c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1181c0c050c5SMichael Chan 			  u32 *rule_locs)
1182c0c050c5SMichael Chan {
1183c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1184c0c050c5SMichael Chan 	int rc = 0;
1185c0c050c5SMichael Chan 
1186c0c050c5SMichael Chan 	switch (cmd->cmd) {
1187a011952aSMichael Chan #ifdef CONFIG_RFS_ACCEL
1188c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
1189c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
1190c0c050c5SMichael Chan 		break;
1191c0c050c5SMichael Chan 
1192c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
1193c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
1194c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
1195c0c050c5SMichael Chan 		break;
1196c0c050c5SMichael Chan 
1197c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
1198c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
1199c0c050c5SMichael Chan 		break;
1200c0c050c5SMichael Chan 
1201c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
1202c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
1203c0c050c5SMichael Chan 		break;
1204a011952aSMichael Chan #endif
1205a011952aSMichael Chan 
1206a011952aSMichael Chan 	case ETHTOOL_GRXFH:
1207a011952aSMichael Chan 		rc = bnxt_grxfh(bp, cmd);
1208a011952aSMichael Chan 		break;
1209c0c050c5SMichael Chan 
1210c0c050c5SMichael Chan 	default:
1211c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
1212c0c050c5SMichael Chan 		break;
1213c0c050c5SMichael Chan 	}
1214c0c050c5SMichael Chan 
1215c0c050c5SMichael Chan 	return rc;
1216c0c050c5SMichael Chan }
1217a011952aSMichael Chan 
1218a011952aSMichael Chan static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1219a011952aSMichael Chan {
1220a011952aSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1221a011952aSMichael Chan 	int rc;
1222a011952aSMichael Chan 
1223a011952aSMichael Chan 	switch (cmd->cmd) {
1224a011952aSMichael Chan 	case ETHTOOL_SRXFH:
1225a011952aSMichael Chan 		rc = bnxt_srxfh(bp, cmd);
1226a011952aSMichael Chan 		break;
1227a011952aSMichael Chan 
1228a011952aSMichael Chan 	default:
1229a011952aSMichael Chan 		rc = -EOPNOTSUPP;
1230a011952aSMichael Chan 		break;
1231a011952aSMichael Chan 	}
1232a011952aSMichael Chan 	return rc;
1233a011952aSMichael Chan }
1234c0c050c5SMichael Chan 
1235b73c1d08SMichael Chan u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
1236c0c050c5SMichael Chan {
1237b73c1d08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1238b73c1d08SMichael Chan 
1239b73c1d08SMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1240b73c1d08SMichael Chan 		return ALIGN(bp->rx_nr_rings, BNXT_RSS_TABLE_ENTRIES_P5);
1241c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
1242c0c050c5SMichael Chan }
1243c0c050c5SMichael Chan 
1244c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
1245c0c050c5SMichael Chan {
1246c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
1247c0c050c5SMichael Chan }
1248c0c050c5SMichael Chan 
1249c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1250c0c050c5SMichael Chan 			 u8 *hfunc)
1251c0c050c5SMichael Chan {
1252c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
12537991cb9cSMichael Chan 	struct bnxt_vnic_info *vnic;
1254adc38ac6SMichael Chan 	u32 i, tbl_size;
1255c0c050c5SMichael Chan 
1256c0c050c5SMichael Chan 	if (hfunc)
1257c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
1258c0c050c5SMichael Chan 
12597991cb9cSMichael Chan 	if (!bp->vnic_info)
12607991cb9cSMichael Chan 		return 0;
12617991cb9cSMichael Chan 
12627991cb9cSMichael Chan 	vnic = &bp->vnic_info[0];
1263adc38ac6SMichael Chan 	if (indir && bp->rss_indir_tbl) {
1264adc38ac6SMichael Chan 		tbl_size = bnxt_get_rxfh_indir_size(dev);
1265adc38ac6SMichael Chan 		for (i = 0; i < tbl_size; i++)
1266adc38ac6SMichael Chan 			indir[i] = bp->rss_indir_tbl[i];
12677991cb9cSMichael Chan 	}
1268c0c050c5SMichael Chan 
12697991cb9cSMichael Chan 	if (key && vnic->rss_hash_key)
1270c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
1271c0c050c5SMichael Chan 
1272c0c050c5SMichael Chan 	return 0;
1273c0c050c5SMichael Chan }
1274c0c050c5SMichael Chan 
1275bd3191b5SMichael Chan static int bnxt_set_rxfh(struct net_device *dev, const u32 *indir,
1276bd3191b5SMichael Chan 			 const u8 *key, const u8 hfunc)
1277bd3191b5SMichael Chan {
1278bd3191b5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1279bd3191b5SMichael Chan 	int rc = 0;
1280bd3191b5SMichael Chan 
1281bd3191b5SMichael Chan 	if (hfunc && hfunc != ETH_RSS_HASH_TOP)
1282bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1283bd3191b5SMichael Chan 
1284bd3191b5SMichael Chan 	if (key)
1285bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1286bd3191b5SMichael Chan 
1287bd3191b5SMichael Chan 	if (indir) {
1288bd3191b5SMichael Chan 		u32 i, pad, tbl_size = bnxt_get_rxfh_indir_size(dev);
1289bd3191b5SMichael Chan 
1290bd3191b5SMichael Chan 		for (i = 0; i < tbl_size; i++)
1291bd3191b5SMichael Chan 			bp->rss_indir_tbl[i] = indir[i];
1292bd3191b5SMichael Chan 		pad = bp->rss_indir_tbl_entries - tbl_size;
1293bd3191b5SMichael Chan 		if (pad)
1294bd3191b5SMichael Chan 			memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
1295bd3191b5SMichael Chan 	}
1296bd3191b5SMichael Chan 
1297bd3191b5SMichael Chan 	if (netif_running(bp->dev)) {
1298bd3191b5SMichael Chan 		bnxt_close_nic(bp, false, false);
1299bd3191b5SMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1300bd3191b5SMichael Chan 	}
1301bd3191b5SMichael Chan 	return rc;
1302bd3191b5SMichael Chan }
1303bd3191b5SMichael Chan 
1304c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
1305c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
1306c0c050c5SMichael Chan {
1307c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1308c0c050c5SMichael Chan 
1309c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1310431aa1ebSMichael Chan 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
1311c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
13125c8227d0SMichael Chan 	info->n_stats = bnxt_get_num_stats(bp);
1313eb513658SMichael Chan 	info->testinfo_len = bp->num_tests;
1314c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
1315c0c050c5SMichael Chan 	info->eedump_len = 0;
1316c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
1317c0c050c5SMichael Chan 	info->regdump_len = 0;
1318c0c050c5SMichael Chan }
1319c0c050c5SMichael Chan 
1320b5d600b0SVasundhara Volam static int bnxt_get_regs_len(struct net_device *dev)
1321b5d600b0SVasundhara Volam {
1322b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1323b5d600b0SVasundhara Volam 	int reg_len;
1324b5d600b0SVasundhara Volam 
1325b5d600b0SVasundhara Volam 	reg_len = BNXT_PXP_REG_LEN;
1326b5d600b0SVasundhara Volam 
1327b5d600b0SVasundhara Volam 	if (bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED)
1328b5d600b0SVasundhara Volam 		reg_len += sizeof(struct pcie_ctx_hw_stats);
1329b5d600b0SVasundhara Volam 
1330b5d600b0SVasundhara Volam 	return reg_len;
1331b5d600b0SVasundhara Volam }
1332b5d600b0SVasundhara Volam 
1333b5d600b0SVasundhara Volam static void bnxt_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1334b5d600b0SVasundhara Volam 			  void *_p)
1335b5d600b0SVasundhara Volam {
1336b5d600b0SVasundhara Volam 	struct pcie_ctx_hw_stats *hw_pcie_stats;
1337b5d600b0SVasundhara Volam 	struct hwrm_pcie_qstats_input req = {0};
1338b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1339b5d600b0SVasundhara Volam 	dma_addr_t hw_pcie_stats_addr;
1340b5d600b0SVasundhara Volam 	int rc;
1341b5d600b0SVasundhara Volam 
1342b5d600b0SVasundhara Volam 	regs->version = 0;
1343b5d600b0SVasundhara Volam 	bnxt_dbg_hwrm_rd_reg(bp, 0, BNXT_PXP_REG_LEN / 4, _p);
1344b5d600b0SVasundhara Volam 
1345b5d600b0SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED))
1346b5d600b0SVasundhara Volam 		return;
1347b5d600b0SVasundhara Volam 
1348b5d600b0SVasundhara Volam 	hw_pcie_stats = dma_alloc_coherent(&bp->pdev->dev,
1349b5d600b0SVasundhara Volam 					   sizeof(*hw_pcie_stats),
1350b5d600b0SVasundhara Volam 					   &hw_pcie_stats_addr, GFP_KERNEL);
1351b5d600b0SVasundhara Volam 	if (!hw_pcie_stats)
1352b5d600b0SVasundhara Volam 		return;
1353b5d600b0SVasundhara Volam 
1354b5d600b0SVasundhara Volam 	regs->version = 1;
1355b5d600b0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PCIE_QSTATS, -1, -1);
1356b5d600b0SVasundhara Volam 	req.pcie_stat_size = cpu_to_le16(sizeof(*hw_pcie_stats));
1357b5d600b0SVasundhara Volam 	req.pcie_stat_host_addr = cpu_to_le64(hw_pcie_stats_addr);
1358b5d600b0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
1359b5d600b0SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1360b5d600b0SVasundhara Volam 	if (!rc) {
1361b5d600b0SVasundhara Volam 		__le64 *src = (__le64 *)hw_pcie_stats;
1362b5d600b0SVasundhara Volam 		u64 *dst = (u64 *)(_p + BNXT_PXP_REG_LEN);
1363b5d600b0SVasundhara Volam 		int i;
1364b5d600b0SVasundhara Volam 
1365b5d600b0SVasundhara Volam 		for (i = 0; i < sizeof(*hw_pcie_stats) / sizeof(__le64); i++)
1366b5d600b0SVasundhara Volam 			dst[i] = le64_to_cpu(src[i]);
1367b5d600b0SVasundhara Volam 	}
1368b5d600b0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
1369b5d600b0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, sizeof(*hw_pcie_stats), hw_pcie_stats,
1370b5d600b0SVasundhara Volam 			  hw_pcie_stats_addr);
1371b5d600b0SVasundhara Volam }
1372b5d600b0SVasundhara Volam 
13738e202366SMichael Chan static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13748e202366SMichael Chan {
13758e202366SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
13768e202366SMichael Chan 
13778e202366SMichael Chan 	wol->supported = 0;
13788e202366SMichael Chan 	wol->wolopts = 0;
13798e202366SMichael Chan 	memset(&wol->sopass, 0, sizeof(wol->sopass));
13808e202366SMichael Chan 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
13818e202366SMichael Chan 		wol->supported = WAKE_MAGIC;
13828e202366SMichael Chan 		if (bp->wol)
13838e202366SMichael Chan 			wol->wolopts = WAKE_MAGIC;
13848e202366SMichael Chan 	}
13858e202366SMichael Chan }
13868e202366SMichael Chan 
13875282db6cSMichael Chan static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13885282db6cSMichael Chan {
13895282db6cSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
13905282db6cSMichael Chan 
13915282db6cSMichael Chan 	if (wol->wolopts & ~WAKE_MAGIC)
13925282db6cSMichael Chan 		return -EINVAL;
13935282db6cSMichael Chan 
13945282db6cSMichael Chan 	if (wol->wolopts & WAKE_MAGIC) {
13955282db6cSMichael Chan 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
13965282db6cSMichael Chan 			return -EINVAL;
13975282db6cSMichael Chan 		if (!bp->wol) {
13985282db6cSMichael Chan 			if (bnxt_hwrm_alloc_wol_fltr(bp))
13995282db6cSMichael Chan 				return -EBUSY;
14005282db6cSMichael Chan 			bp->wol = 1;
14015282db6cSMichael Chan 		}
14025282db6cSMichael Chan 	} else {
14035282db6cSMichael Chan 		if (bp->wol) {
14045282db6cSMichael Chan 			if (bnxt_hwrm_free_wol_fltr(bp))
14055282db6cSMichael Chan 				return -EBUSY;
14065282db6cSMichael Chan 			bp->wol = 0;
14075282db6cSMichael Chan 		}
14085282db6cSMichael Chan 	}
14095282db6cSMichael Chan 	return 0;
14105282db6cSMichael Chan }
14115282db6cSMichael Chan 
1412170ce013SMichael Chan u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
1413c0c050c5SMichael Chan {
1414c0c050c5SMichael Chan 	u32 speed_mask = 0;
1415c0c050c5SMichael Chan 
1416c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
1417c0c050c5SMichael Chan 	/* set the advertised speeds */
1418c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1419c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
1420c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1421c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
1422c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1423c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
1424c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1425c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
1426c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
14271c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
142827c4d578SMichael Chan 
142927c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
143027c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
143127c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
143227c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
143327c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
143427c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
143527c4d578SMichael Chan 
1436c0c050c5SMichael Chan 	return speed_mask;
1437c0c050c5SMichael Chan }
1438c0c050c5SMichael Chan 
143900c04a92SMichael Chan #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
144000c04a92SMichael Chan {									\
144100c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
144200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
144300c04a92SMichael Chan 						     100baseT_Full);	\
144400c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
144500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
144600c04a92SMichael Chan 						     1000baseT_Full);	\
144700c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
144800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
144900c04a92SMichael Chan 						     10000baseT_Full);	\
145000c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
145100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145200c04a92SMichael Chan 						     25000baseCR_Full);	\
145300c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
145400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145500c04a92SMichael Chan 						     40000baseCR4_Full);\
145600c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
145700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
145800c04a92SMichael Chan 						     50000baseCR2_Full);\
145938a21b34SDeepak Khungar 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
146038a21b34SDeepak Khungar 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146138a21b34SDeepak Khungar 						     100000baseCR4_Full);\
146200c04a92SMichael Chan 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
146300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
146400c04a92SMichael Chan 						     Pause);		\
146500c04a92SMichael Chan 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
146600c04a92SMichael Chan 			ethtool_link_ksettings_add_link_mode(		\
146700c04a92SMichael Chan 					lk_ksettings, name, Asym_Pause);\
146800c04a92SMichael Chan 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
146900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147000c04a92SMichael Chan 						     Asym_Pause);	\
147100c04a92SMichael Chan 	}								\
147200c04a92SMichael Chan }
147300c04a92SMichael Chan 
147400c04a92SMichael Chan #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
147500c04a92SMichael Chan {									\
147600c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
147700c04a92SMichael Chan 						  100baseT_Full) ||	\
147800c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
147900c04a92SMichael Chan 						  100baseT_Half))	\
148000c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
148100c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
148200c04a92SMichael Chan 						  1000baseT_Full) ||	\
148300c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
148400c04a92SMichael Chan 						  1000baseT_Half))	\
148500c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
148600c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
148700c04a92SMichael Chan 						  10000baseT_Full))	\
148800c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
148900c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149000c04a92SMichael Chan 						  25000baseCR_Full))	\
149100c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
149200c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149300c04a92SMichael Chan 						  40000baseCR4_Full))	\
149400c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
149500c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149600c04a92SMichael Chan 						  50000baseCR2_Full))	\
149700c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
149838a21b34SDeepak Khungar 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
149938a21b34SDeepak Khungar 						  100000baseCR4_Full))	\
150038a21b34SDeepak Khungar 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
150100c04a92SMichael Chan }
150200c04a92SMichael Chan 
150300c04a92SMichael Chan static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
150400c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
150527c4d578SMichael Chan {
150668515a18SMichael Chan 	u16 fw_speeds = link_info->advertising;
150727c4d578SMichael Chan 	u8 fw_pause = 0;
150827c4d578SMichael Chan 
150927c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
151027c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
151127c4d578SMichael Chan 
151200c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
151327c4d578SMichael Chan }
151427c4d578SMichael Chan 
151500c04a92SMichael Chan static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
151600c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15173277360eSMichael Chan {
15183277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
15193277360eSMichael Chan 	u8 fw_pause = 0;
15203277360eSMichael Chan 
15213277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
15223277360eSMichael Chan 		fw_pause = link_info->lp_pause;
15233277360eSMichael Chan 
152400c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
152500c04a92SMichael Chan 				lp_advertising);
15263277360eSMichael Chan }
15273277360eSMichael Chan 
152800c04a92SMichael Chan static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
152900c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15304b32caccSMichael Chan {
15314b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
15324b32caccSMichael Chan 
153300c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
15344b32caccSMichael Chan 
153500c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
153600c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
153700c04a92SMichael Chan 					     Asym_Pause);
153893ed8117SMichael Chan 
153900c04a92SMichael Chan 	if (link_info->support_auto_speeds)
154000c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
154100c04a92SMichael Chan 						     Autoneg);
154293ed8117SMichael Chan }
154393ed8117SMichael Chan 
1544c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1545c0c050c5SMichael Chan {
1546c0c050c5SMichael Chan 	switch (fw_link_speed) {
1547c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
1548c0c050c5SMichael Chan 		return SPEED_100;
1549c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
1550c0c050c5SMichael Chan 		return SPEED_1000;
1551c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
1552c0c050c5SMichael Chan 		return SPEED_2500;
1553c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
1554c0c050c5SMichael Chan 		return SPEED_10000;
1555c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
1556c0c050c5SMichael Chan 		return SPEED_20000;
1557c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
1558c0c050c5SMichael Chan 		return SPEED_25000;
1559c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
1560c0c050c5SMichael Chan 		return SPEED_40000;
1561c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
1562c0c050c5SMichael Chan 		return SPEED_50000;
156338a21b34SDeepak Khungar 	case BNXT_LINK_SPEED_100GB:
156438a21b34SDeepak Khungar 		return SPEED_100000;
1565c0c050c5SMichael Chan 	default:
1566c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
1567c0c050c5SMichael Chan 	}
1568c0c050c5SMichael Chan }
1569c0c050c5SMichael Chan 
157000c04a92SMichael Chan static int bnxt_get_link_ksettings(struct net_device *dev,
157100c04a92SMichael Chan 				   struct ethtool_link_ksettings *lk_ksettings)
1572c0c050c5SMichael Chan {
1573c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1574c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
157500c04a92SMichael Chan 	struct ethtool_link_settings *base = &lk_ksettings->base;
157600c04a92SMichael Chan 	u32 ethtool_speed;
1577c0c050c5SMichael Chan 
157800c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1579e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
158000c04a92SMichael Chan 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1581c0c050c5SMichael Chan 
158200c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1583b763499eSMichael Chan 	if (link_info->autoneg) {
158400c04a92SMichael Chan 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
158500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
158600c04a92SMichael Chan 						     advertising, Autoneg);
158700c04a92SMichael Chan 		base->autoneg = AUTONEG_ENABLE;
158800c04a92SMichael Chan 		base->duplex = DUPLEX_UNKNOWN;
158983d8f5e9SMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK) {
159083d8f5e9SMichael Chan 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
159183d8f5e9SMichael Chan 			if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
159200c04a92SMichael Chan 				base->duplex = DUPLEX_FULL;
159329c262feSMichael Chan 			else
159400c04a92SMichael Chan 				base->duplex = DUPLEX_HALF;
159583d8f5e9SMichael Chan 		}
159683d8f5e9SMichael Chan 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1597c0c050c5SMichael Chan 	} else {
159800c04a92SMichael Chan 		base->autoneg = AUTONEG_DISABLE;
159929c262feSMichael Chan 		ethtool_speed =
160029c262feSMichael Chan 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
160100c04a92SMichael Chan 		base->duplex = DUPLEX_HALF;
160229c262feSMichael Chan 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
160300c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
1604c0c050c5SMichael Chan 	}
160500c04a92SMichael Chan 	base->speed = ethtool_speed;
1606c0c050c5SMichael Chan 
160700c04a92SMichael Chan 	base->port = PORT_NONE;
1608c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
160900c04a92SMichael Chan 		base->port = PORT_TP;
161000c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
161100c04a92SMichael Chan 						     TP);
161200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
161300c04a92SMichael Chan 						     TP);
1614c0c050c5SMichael Chan 	} else {
161500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
161600c04a92SMichael Chan 						     FIBRE);
161700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
161800c04a92SMichael Chan 						     FIBRE);
1619c0c050c5SMichael Chan 
1620c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
162100c04a92SMichael Chan 			base->port = PORT_DA;
1622c0c050c5SMichael Chan 		else if (link_info->media_type ==
1623c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
162400c04a92SMichael Chan 			base->port = PORT_FIBRE;
1625c0c050c5SMichael Chan 	}
162600c04a92SMichael Chan 	base->phy_address = link_info->phy_addr;
1627e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1628c0c050c5SMichael Chan 
1629c0c050c5SMichael Chan 	return 0;
1630c0c050c5SMichael Chan }
1631c0c050c5SMichael Chan 
163238a21b34SDeepak Khungar static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed)
1633c0c050c5SMichael Chan {
16349d9cee08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
16359d9cee08SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
16369d9cee08SMichael Chan 	u16 support_spds = link_info->support_speeds;
16379d9cee08SMichael Chan 	u32 fw_speed = 0;
16389d9cee08SMichael Chan 
1639c0c050c5SMichael Chan 	switch (ethtool_speed) {
1640c0c050c5SMichael Chan 	case SPEED_100:
16419d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
16429d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
16439d9cee08SMichael Chan 		break;
1644c0c050c5SMichael Chan 	case SPEED_1000:
16459d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
16469d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
16479d9cee08SMichael Chan 		break;
1648c0c050c5SMichael Chan 	case SPEED_2500:
16499d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
16509d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
16519d9cee08SMichael Chan 		break;
1652c0c050c5SMichael Chan 	case SPEED_10000:
16539d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
16549d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
16559d9cee08SMichael Chan 		break;
1656c0c050c5SMichael Chan 	case SPEED_20000:
16579d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
16589d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
16599d9cee08SMichael Chan 		break;
1660c0c050c5SMichael Chan 	case SPEED_25000:
16619d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
16629d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
16639d9cee08SMichael Chan 		break;
1664c0c050c5SMichael Chan 	case SPEED_40000:
16659d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
16669d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
16679d9cee08SMichael Chan 		break;
1668c0c050c5SMichael Chan 	case SPEED_50000:
16699d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
16709d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
16719d9cee08SMichael Chan 		break;
167238a21b34SDeepak Khungar 	case SPEED_100000:
167338a21b34SDeepak Khungar 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB)
167438a21b34SDeepak Khungar 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB;
167538a21b34SDeepak Khungar 		break;
1676c0c050c5SMichael Chan 	default:
1677c0c050c5SMichael Chan 		netdev_err(dev, "unsupported speed!\n");
1678c0c050c5SMichael Chan 		break;
1679c0c050c5SMichael Chan 	}
16809d9cee08SMichael Chan 	return fw_speed;
1681c0c050c5SMichael Chan }
1682c0c050c5SMichael Chan 
1683939f7f0cSMichael Chan u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1684c0c050c5SMichael Chan {
1685c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
1686c0c050c5SMichael Chan 
1687c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
1688c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
1689c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
1690c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1691c0c050c5SMichael Chan 	}
1692c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
1693c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
1694c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1695c0c050c5SMichael Chan 	}
1696c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
1697c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1698c0c050c5SMichael Chan 
16991c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
17001c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
17011c49c421SMichael Chan 
1702c0c050c5SMichael Chan 	return fw_speed_mask;
1703c0c050c5SMichael Chan }
1704c0c050c5SMichael Chan 
170500c04a92SMichael Chan static int bnxt_set_link_ksettings(struct net_device *dev,
170600c04a92SMichael Chan 			   const struct ethtool_link_ksettings *lk_ksettings)
1707c0c050c5SMichael Chan {
1708c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1709c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
171000c04a92SMichael Chan 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1711c0c050c5SMichael Chan 	bool set_pause = false;
171268515a18SMichael Chan 	u16 fw_advertising = 0;
171368515a18SMichael Chan 	u32 speed;
171400c04a92SMichael Chan 	int rc = 0;
1715c0c050c5SMichael Chan 
1716c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
171700c04a92SMichael Chan 		return -EOPNOTSUPP;
1718c0c050c5SMichael Chan 
1719e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
172000c04a92SMichael Chan 	if (base->autoneg == AUTONEG_ENABLE) {
172100c04a92SMichael Chan 		BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
172200c04a92SMichael Chan 					advertising);
1723c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1724c0c050c5SMichael Chan 		if (!fw_advertising)
172593ed8117SMichael Chan 			link_info->advertising = link_info->support_auto_speeds;
1726c0c050c5SMichael Chan 		else
1727c0c050c5SMichael Chan 			link_info->advertising = fw_advertising;
1728c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
1729c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
1730c0c050c5SMichael Chan 		 */
1731c0c050c5SMichael Chan 		set_pause = true;
1732c0c050c5SMichael Chan 	} else {
17339d9cee08SMichael Chan 		u16 fw_speed;
173403efbec0SMichael Chan 		u8 phy_type = link_info->phy_type;
17359d9cee08SMichael Chan 
173603efbec0SMichael Chan 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
173703efbec0SMichael Chan 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
173803efbec0SMichael Chan 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
173903efbec0SMichael Chan 			netdev_err(dev, "10GBase-T devices must autoneg\n");
174003efbec0SMichael Chan 			rc = -EINVAL;
174103efbec0SMichael Chan 			goto set_setting_exit;
174203efbec0SMichael Chan 		}
174300c04a92SMichael Chan 		if (base->duplex == DUPLEX_HALF) {
1744c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1745c0c050c5SMichael Chan 			rc = -EINVAL;
1746c0c050c5SMichael Chan 			goto set_setting_exit;
1747c0c050c5SMichael Chan 		}
174800c04a92SMichael Chan 		speed = base->speed;
17499d9cee08SMichael Chan 		fw_speed = bnxt_get_fw_speed(dev, speed);
17509d9cee08SMichael Chan 		if (!fw_speed) {
17519d9cee08SMichael Chan 			rc = -EINVAL;
17529d9cee08SMichael Chan 			goto set_setting_exit;
17539d9cee08SMichael Chan 		}
17549d9cee08SMichael Chan 		link_info->req_link_speed = fw_speed;
1755c0c050c5SMichael Chan 		link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1756b763499eSMichael Chan 		link_info->autoneg = 0;
1757c0c050c5SMichael Chan 		link_info->advertising = 0;
1758c0c050c5SMichael Chan 	}
1759c0c050c5SMichael Chan 
1760c0c050c5SMichael Chan 	if (netif_running(dev))
1761939f7f0cSMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1762c0c050c5SMichael Chan 
1763c0c050c5SMichael Chan set_setting_exit:
1764e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1765c0c050c5SMichael Chan 	return rc;
1766c0c050c5SMichael Chan }
1767c0c050c5SMichael Chan 
1768c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
1769c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
1770c0c050c5SMichael Chan {
1771c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1772c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1773c0c050c5SMichael Chan 
1774c0c050c5SMichael Chan 	if (BNXT_VF(bp))
1775c0c050c5SMichael Chan 		return;
1776b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
17773c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
17783c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1779c0c050c5SMichael Chan }
1780c0c050c5SMichael Chan 
1781c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
1782c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
1783c0c050c5SMichael Chan {
1784c0c050c5SMichael Chan 	int rc = 0;
1785c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1786c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1787c0c050c5SMichael Chan 
1788c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
178975362a3fSMichael Chan 		return -EOPNOTSUPP;
1790c0c050c5SMichael Chan 
1791a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
1792c0c050c5SMichael Chan 	if (epause->autoneg) {
1793a5390690SMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
1794a5390690SMichael Chan 			rc = -EINVAL;
1795a5390690SMichael Chan 			goto pause_exit;
1796a5390690SMichael Chan 		}
1797b763499eSMichael Chan 
1798c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1799c9ee9516SMichael Chan 		if (bp->hwrm_spec_code >= 0x10201)
1800c9ee9516SMichael Chan 			link_info->req_flow_ctrl =
1801c9ee9516SMichael Chan 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1802c0c050c5SMichael Chan 	} else {
1803c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
1804c0c050c5SMichael Chan 		 * force a link change
1805c0c050c5SMichael Chan 		 */
1806c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1807c0c050c5SMichael Chan 			link_info->force_link_chng = true;
1808c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1809c9ee9516SMichael Chan 		link_info->req_flow_ctrl = 0;
1810c0c050c5SMichael Chan 	}
1811c0c050c5SMichael Chan 	if (epause->rx_pause)
1812c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1813c0c050c5SMichael Chan 
1814c0c050c5SMichael Chan 	if (epause->tx_pause)
1815c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1816c0c050c5SMichael Chan 
1817a5390690SMichael Chan 	if (netif_running(dev))
1818c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
1819a5390690SMichael Chan 
1820a5390690SMichael Chan pause_exit:
1821163e9ef6SVasundhara Volam 	mutex_unlock(&bp->link_lock);
1822c0c050c5SMichael Chan 	return rc;
1823c0c050c5SMichael Chan }
1824c0c050c5SMichael Chan 
1825c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
1826c0c050c5SMichael Chan {
1827c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1828c0c050c5SMichael Chan 
1829c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
1830c0c050c5SMichael Chan 	return bp->link_info.link_up;
1831c0c050c5SMichael Chan }
1832c0c050c5SMichael Chan 
1833b3b0ddd0SMichael Chan static void bnxt_print_admin_err(struct bnxt *bp)
1834b3b0ddd0SMichael Chan {
1835b3b0ddd0SMichael Chan 	netdev_info(bp->dev, "PF does not have admin privileges to flash or reset the device\n");
1836b3b0ddd0SMichael Chan }
1837b3b0ddd0SMichael Chan 
18385ac67d8bSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
18395ac67d8bSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
18405ac67d8bSRob Swindell 				u32 *data_length);
18415ac67d8bSRob Swindell 
1842c0c050c5SMichael Chan static int bnxt_flash_nvram(struct net_device *dev,
1843c0c050c5SMichael Chan 			    u16 dir_type,
1844c0c050c5SMichael Chan 			    u16 dir_ordinal,
1845c0c050c5SMichael Chan 			    u16 dir_ext,
1846c0c050c5SMichael Chan 			    u16 dir_attr,
1847c0c050c5SMichael Chan 			    const u8 *data,
1848c0c050c5SMichael Chan 			    size_t data_len)
1849c0c050c5SMichael Chan {
1850c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1851c0c050c5SMichael Chan 	int rc;
1852c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
1853c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1854c0c050c5SMichael Chan 	u8 *kmem;
1855c0c050c5SMichael Chan 
1856c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1857c0c050c5SMichael Chan 
1858c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
1859c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
1860c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
1861c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
1862c0c050c5SMichael Chan 	req.dir_data_length = cpu_to_le32(data_len);
1863c0c050c5SMichael Chan 
1864c0c050c5SMichael Chan 	kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1865c0c050c5SMichael Chan 				  GFP_KERNEL);
1866c0c050c5SMichael Chan 	if (!kmem) {
1867c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1868c0c050c5SMichael Chan 			   (unsigned)data_len);
1869c0c050c5SMichael Chan 		return -ENOMEM;
1870c0c050c5SMichael Chan 	}
1871c0c050c5SMichael Chan 	memcpy(kmem, data, data_len);
1872c0c050c5SMichael Chan 	req.host_src_addr = cpu_to_le64(dma_handle);
1873c0c050c5SMichael Chan 
1874c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1875c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1876c0c050c5SMichael Chan 
1877d4f1420dSMichael Chan 	if (rc == -EACCES)
1878b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
1879c0c050c5SMichael Chan 	return rc;
1880c0c050c5SMichael Chan }
1881c0c050c5SMichael Chan 
188295fec034SEdwin Peer static int bnxt_hwrm_firmware_reset(struct net_device *dev, u8 proc_type,
188395fec034SEdwin Peer 				    u8 self_reset, u8 flags)
1884d2d6318cSRob Swindell {
1885d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
18867c675421SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
18877c675421SVasundhara Volam 	int rc;
1888d2d6318cSRob Swindell 
1889d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1890d2d6318cSRob Swindell 
189195fec034SEdwin Peer 	req.embedded_proc_type = proc_type;
189295fec034SEdwin Peer 	req.selfrst_status = self_reset;
189395fec034SEdwin Peer 	req.flags = flags;
189495fec034SEdwin Peer 
18958cec0940SEdwin Peer 	if (proc_type == FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP) {
18968cec0940SEdwin Peer 		rc = hwrm_send_message_silent(bp, &req, sizeof(req),
18978cec0940SEdwin Peer 					      HWRM_CMD_TIMEOUT);
18988cec0940SEdwin Peer 	} else {
189995fec034SEdwin Peer 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
190095fec034SEdwin Peer 		if (rc == -EACCES)
190195fec034SEdwin Peer 			bnxt_print_admin_err(bp);
19028cec0940SEdwin Peer 	}
190395fec034SEdwin Peer 	return rc;
190495fec034SEdwin Peer }
190595fec034SEdwin Peer 
190694f17e89SEdwin Peer static int bnxt_firmware_reset(struct net_device *dev,
190794f17e89SEdwin Peer 			       enum bnxt_nvm_directory_type dir_type)
190895fec034SEdwin Peer {
190995fec034SEdwin Peer 	u8 self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE;
191095fec034SEdwin Peer 	u8 proc_type, flags = 0;
191195fec034SEdwin Peer 
1912d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1913d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
1914d2d6318cSRob Swindell 	switch (dir_type) {
1915d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
1916d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
1917d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
191895fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1919d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
192095fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1921d2d6318cSRob Swindell 		break;
1922d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
1923d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
192495fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
192508141e0bSRob Swindell 		/* Self-reset APE upon next PCIe reset: */
192695fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1927d2d6318cSRob Swindell 		break;
1928d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
1929d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
193095fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1931d2d6318cSRob Swindell 		break;
1932d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
1933d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
193495fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1935d2d6318cSRob Swindell 		break;
1936d2d6318cSRob Swindell 	default:
1937d2d6318cSRob Swindell 		return -EINVAL;
1938d2d6318cSRob Swindell 	}
1939d2d6318cSRob Swindell 
194095fec034SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, proc_type, self_reset, flags);
1941d2d6318cSRob Swindell }
1942d2d6318cSRob Swindell 
194394f17e89SEdwin Peer static int bnxt_firmware_reset_chip(struct net_device *dev)
194494f17e89SEdwin Peer {
194594f17e89SEdwin Peer 	struct bnxt *bp = netdev_priv(dev);
194694f17e89SEdwin Peer 	u8 flags = 0;
194794f17e89SEdwin Peer 
194894f17e89SEdwin Peer 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
194994f17e89SEdwin Peer 		flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
195094f17e89SEdwin Peer 
195194f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev,
195294f17e89SEdwin Peer 					FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP,
195394f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP,
195494f17e89SEdwin Peer 					flags);
195594f17e89SEdwin Peer }
195694f17e89SEdwin Peer 
195794f17e89SEdwin Peer static int bnxt_firmware_reset_ap(struct net_device *dev)
195894f17e89SEdwin Peer {
195994f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP,
196094f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE,
196194f17e89SEdwin Peer 					0);
196294f17e89SEdwin Peer }
196394f17e89SEdwin Peer 
1964c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
1965c0c050c5SMichael Chan 			       u16 dir_type,
1966c0c050c5SMichael Chan 			       const u8 *fw_data,
1967c0c050c5SMichael Chan 			       size_t fw_size)
1968c0c050c5SMichael Chan {
1969c0c050c5SMichael Chan 	int	rc = 0;
1970c0c050c5SMichael Chan 	u16	code_type;
1971c0c050c5SMichael Chan 	u32	stored_crc;
1972c0c050c5SMichael Chan 	u32	calculated_crc;
1973c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1974c0c050c5SMichael Chan 
1975c0c050c5SMichael Chan 	switch (dir_type) {
1976c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1977c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1978c0c050c5SMichael Chan 		code_type = CODE_BOOT;
1979c0c050c5SMichael Chan 		break;
198093e0b4feSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
198193e0b4feSRob Swindell 		code_type = CODE_CHIMP_PATCH;
198293e0b4feSRob Swindell 		break;
19832731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
19842731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
19852731d70fSRob Swindell 		break;
198693e0b4feSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
198793e0b4feSRob Swindell 		code_type = CODE_APE_PATCH;
198893e0b4feSRob Swindell 		break;
198993e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
199093e0b4feSRob Swindell 		code_type = CODE_KONG_FW;
199193e0b4feSRob Swindell 		break;
199293e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
199393e0b4feSRob Swindell 		code_type = CODE_KONG_PATCH;
199493e0b4feSRob Swindell 		break;
199593e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
199693e0b4feSRob Swindell 		code_type = CODE_BONO_FW;
199793e0b4feSRob Swindell 		break;
199893e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
199993e0b4feSRob Swindell 		code_type = CODE_BONO_PATCH;
200093e0b4feSRob Swindell 		break;
2001c0c050c5SMichael Chan 	default:
2002c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
2003c0c050c5SMichael Chan 			   dir_type);
2004c0c050c5SMichael Chan 		return -EINVAL;
2005c0c050c5SMichael Chan 	}
2006c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
2007c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
2008c0c050c5SMichael Chan 			   (unsigned int)fw_size);
2009c0c050c5SMichael Chan 		return -EINVAL;
2010c0c050c5SMichael Chan 	}
2011c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
2012c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
2013c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
2014c0c050c5SMichael Chan 		return -EINVAL;
2015c0c050c5SMichael Chan 	}
2016c0c050c5SMichael Chan 	if (header->code_type != code_type) {
2017c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
2018c0c050c5SMichael Chan 			   code_type, header->code_type);
2019c0c050c5SMichael Chan 		return -EINVAL;
2020c0c050c5SMichael Chan 	}
2021c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
2022c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
2023c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
2024c0c050c5SMichael Chan 		return -EINVAL;
2025c0c050c5SMichael Chan 	}
2026c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
2027c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
2028c0c050c5SMichael Chan 					     sizeof(stored_crc)));
2029c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
2030c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
2031c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
2032c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
2033c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
2034c0c050c5SMichael Chan 		return -EINVAL;
2035c0c050c5SMichael Chan 	}
2036c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2037c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
2038d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
2039d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
2040d2d6318cSRob Swindell 
2041c0c050c5SMichael Chan 	return rc;
2042c0c050c5SMichael Chan }
2043c0c050c5SMichael Chan 
20445ac67d8bSRob Swindell static int bnxt_flash_microcode(struct net_device *dev,
20455ac67d8bSRob Swindell 				u16 dir_type,
20465ac67d8bSRob Swindell 				const u8 *fw_data,
20475ac67d8bSRob Swindell 				size_t fw_size)
20485ac67d8bSRob Swindell {
20495ac67d8bSRob Swindell 	struct bnxt_ucode_trailer *trailer;
20505ac67d8bSRob Swindell 	u32 calculated_crc;
20515ac67d8bSRob Swindell 	u32 stored_crc;
20525ac67d8bSRob Swindell 	int rc = 0;
20535ac67d8bSRob Swindell 
20545ac67d8bSRob Swindell 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
20555ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode file size: %u\n",
20565ac67d8bSRob Swindell 			   (unsigned int)fw_size);
20575ac67d8bSRob Swindell 		return -EINVAL;
20585ac67d8bSRob Swindell 	}
20595ac67d8bSRob Swindell 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
20605ac67d8bSRob Swindell 						sizeof(*trailer)));
20615ac67d8bSRob Swindell 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
20625ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
20635ac67d8bSRob Swindell 			   le32_to_cpu(trailer->sig));
20645ac67d8bSRob Swindell 		return -EINVAL;
20655ac67d8bSRob Swindell 	}
20665ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
20675ac67d8bSRob Swindell 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
20685ac67d8bSRob Swindell 			   dir_type, le16_to_cpu(trailer->dir_type));
20695ac67d8bSRob Swindell 		return -EINVAL;
20705ac67d8bSRob Swindell 	}
20715ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->trailer_length) <
20725ac67d8bSRob Swindell 		sizeof(struct bnxt_ucode_trailer)) {
20735ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
20745ac67d8bSRob Swindell 			   le16_to_cpu(trailer->trailer_length));
20755ac67d8bSRob Swindell 		return -EINVAL;
20765ac67d8bSRob Swindell 	}
20775ac67d8bSRob Swindell 
20785ac67d8bSRob Swindell 	/* Confirm the CRC32 checksum of the file: */
20795ac67d8bSRob Swindell 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
20805ac67d8bSRob Swindell 					     sizeof(stored_crc)));
20815ac67d8bSRob Swindell 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
20825ac67d8bSRob Swindell 	if (calculated_crc != stored_crc) {
20835ac67d8bSRob Swindell 		netdev_err(dev,
20845ac67d8bSRob Swindell 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
20855ac67d8bSRob Swindell 			   (unsigned long)stored_crc,
20865ac67d8bSRob Swindell 			   (unsigned long)calculated_crc);
20875ac67d8bSRob Swindell 		return -EINVAL;
20885ac67d8bSRob Swindell 	}
20895ac67d8bSRob Swindell 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
20905ac67d8bSRob Swindell 			      0, 0, fw_data, fw_size);
20915ac67d8bSRob Swindell 
20925ac67d8bSRob Swindell 	return rc;
20935ac67d8bSRob Swindell }
20945ac67d8bSRob Swindell 
2095c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
2096c0c050c5SMichael Chan {
2097c0c050c5SMichael Chan 	switch (dir_type) {
2098c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
2099c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2100c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2101c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
2102c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
2103c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
2104c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
210593e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
210693e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
2107c0c050c5SMichael Chan 		return true;
2108c0c050c5SMichael Chan 	}
2109c0c050c5SMichael Chan 
2110c0c050c5SMichael Chan 	return false;
2111c0c050c5SMichael Chan }
2112c0c050c5SMichael Chan 
21135ac67d8bSRob Swindell static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
2114c0c050c5SMichael Chan {
2115c0c050c5SMichael Chan 	switch (dir_type) {
2116c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
2117c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
2118c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
2119c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
2120c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
2121c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
2122c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
2123c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
2124c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
2125c0c050c5SMichael Chan 		return true;
2126c0c050c5SMichael Chan 	}
2127c0c050c5SMichael Chan 
2128c0c050c5SMichael Chan 	return false;
2129c0c050c5SMichael Chan }
2130c0c050c5SMichael Chan 
2131c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
2132c0c050c5SMichael Chan {
2133c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
21345ac67d8bSRob Swindell 		bnxt_dir_type_is_other_exec_format(dir_type);
2135c0c050c5SMichael Chan }
2136c0c050c5SMichael Chan 
2137c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
2138c0c050c5SMichael Chan 					 u16 dir_type,
2139c0c050c5SMichael Chan 					 const char *filename)
2140c0c050c5SMichael Chan {
2141c0c050c5SMichael Chan 	const struct firmware  *fw;
2142c0c050c5SMichael Chan 	int			rc;
2143c0c050c5SMichael Chan 
2144c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
2145c0c050c5SMichael Chan 	if (rc != 0) {
2146c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
2147c0c050c5SMichael Chan 			   rc, filename);
2148c0c050c5SMichael Chan 		return rc;
2149c0c050c5SMichael Chan 	}
2150ba425800SJason Yan 	if (bnxt_dir_type_is_ape_bin_format(dir_type))
2151c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
2152ba425800SJason Yan 	else if (bnxt_dir_type_is_other_exec_format(dir_type))
21535ac67d8bSRob Swindell 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
2154c0c050c5SMichael Chan 	else
2155c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2156c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
2157c0c050c5SMichael Chan 	release_firmware(fw);
2158c0c050c5SMichael Chan 	return rc;
2159c0c050c5SMichael Chan }
2160c0c050c5SMichael Chan 
2161d168f328SVasundhara Volam int bnxt_flash_package_from_file(struct net_device *dev, const char *filename,
2162d168f328SVasundhara Volam 				 u32 install_type)
2163c0c050c5SMichael Chan {
21645ac67d8bSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
21655ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
21665ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_input install = {0};
21675ac67d8bSRob Swindell 	const struct firmware *fw;
21685ac67d8bSRob Swindell 	u32 item_len;
216922630e28SEdwin Peer 	int rc = 0;
21705ac67d8bSRob Swindell 	u16 index;
21715ac67d8bSRob Swindell 
21725ac67d8bSRob Swindell 	bnxt_hwrm_fw_set_time(bp);
21735ac67d8bSRob Swindell 
217495ec1f47SVasundhara Volam 	rc = bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
21755ac67d8bSRob Swindell 				  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
217695ec1f47SVasundhara Volam 				  &index, &item_len, NULL);
217795ec1f47SVasundhara Volam 	if (rc) {
21785ac67d8bSRob Swindell 		netdev_err(dev, "PKG update area not created in nvram\n");
217995ec1f47SVasundhara Volam 		return rc;
21805ac67d8bSRob Swindell 	}
21815ac67d8bSRob Swindell 
21825ac67d8bSRob Swindell 	rc = request_firmware(&fw, filename, &dev->dev);
21835ac67d8bSRob Swindell 	if (rc != 0) {
21845ac67d8bSRob Swindell 		netdev_err(dev, "PKG error %d requesting file: %s\n",
21855ac67d8bSRob Swindell 			   rc, filename);
21865ac67d8bSRob Swindell 		return rc;
21875ac67d8bSRob Swindell 	}
21885ac67d8bSRob Swindell 
21895ac67d8bSRob Swindell 	if (fw->size > item_len) {
21909a005c38SJonathan Lemon 		netdev_err(dev, "PKG insufficient update area in nvram: %lu\n",
21915ac67d8bSRob Swindell 			   (unsigned long)fw->size);
21925ac67d8bSRob Swindell 		rc = -EFBIG;
21935ac67d8bSRob Swindell 	} else {
21945ac67d8bSRob Swindell 		dma_addr_t dma_handle;
21955ac67d8bSRob Swindell 		u8 *kmem;
21965ac67d8bSRob Swindell 		struct hwrm_nvm_modify_input modify = {0};
21975ac67d8bSRob Swindell 
21985ac67d8bSRob Swindell 		bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
21995ac67d8bSRob Swindell 
22005ac67d8bSRob Swindell 		modify.dir_idx = cpu_to_le16(index);
22015ac67d8bSRob Swindell 		modify.len = cpu_to_le32(fw->size);
22025ac67d8bSRob Swindell 
22035ac67d8bSRob Swindell 		kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
22045ac67d8bSRob Swindell 					  &dma_handle, GFP_KERNEL);
22055ac67d8bSRob Swindell 		if (!kmem) {
22065ac67d8bSRob Swindell 			netdev_err(dev,
22075ac67d8bSRob Swindell 				   "dma_alloc_coherent failure, length = %u\n",
22085ac67d8bSRob Swindell 				   (unsigned int)fw->size);
22095ac67d8bSRob Swindell 			rc = -ENOMEM;
22105ac67d8bSRob Swindell 		} else {
22115ac67d8bSRob Swindell 			memcpy(kmem, fw->data, fw->size);
22125ac67d8bSRob Swindell 			modify.host_src_addr = cpu_to_le64(dma_handle);
22135ac67d8bSRob Swindell 
221422630e28SEdwin Peer 			rc = hwrm_send_message(bp, &modify, sizeof(modify),
22155ac67d8bSRob Swindell 					       FLASH_PACKAGE_TIMEOUT);
22165ac67d8bSRob Swindell 			dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
22175ac67d8bSRob Swindell 					  dma_handle);
22185ac67d8bSRob Swindell 		}
22195ac67d8bSRob Swindell 	}
22205ac67d8bSRob Swindell 	release_firmware(fw);
222122630e28SEdwin Peer 	if (rc)
22227c675421SVasundhara Volam 		goto err_exit;
22235ac67d8bSRob Swindell 
22245ac67d8bSRob Swindell 	if ((install_type & 0xffff) == 0)
22255ac67d8bSRob Swindell 		install_type >>= 16;
22265ac67d8bSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
22275ac67d8bSRob Swindell 	install.install_type = cpu_to_le32(install_type);
22285ac67d8bSRob Swindell 
2229cb4d1d62SKshitij Soni 	mutex_lock(&bp->hwrm_cmd_lock);
223022630e28SEdwin Peer 	rc = _hwrm_send_message(bp, &install, sizeof(install),
22315ac67d8bSRob Swindell 				INSTALL_PACKAGE_TIMEOUT);
223222630e28SEdwin Peer 	if (rc) {
2233cb4d1d62SKshitij Soni 		u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err;
2234cb4d1d62SKshitij Soni 
2235dd2ebf34SVasundhara Volam 		if (resp->error_code && error_code ==
2236dd2ebf34SVasundhara Volam 		    NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
2237cb4d1d62SKshitij Soni 			install.flags |= cpu_to_le16(
2238cb4d1d62SKshitij Soni 			       NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
223922630e28SEdwin Peer 			rc = _hwrm_send_message(bp, &install, sizeof(install),
2240cb4d1d62SKshitij Soni 						INSTALL_PACKAGE_TIMEOUT);
2241dd2ebf34SVasundhara Volam 		}
224222630e28SEdwin Peer 		if (rc)
2243cb4d1d62SKshitij Soni 			goto flash_pkg_exit;
2244cb4d1d62SKshitij Soni 	}
22455ac67d8bSRob Swindell 
22465ac67d8bSRob Swindell 	if (resp->result) {
22475ac67d8bSRob Swindell 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
22485ac67d8bSRob Swindell 			   (s8)resp->result, (int)resp->problem_item);
2249cb4d1d62SKshitij Soni 		rc = -ENOPKG;
22505ac67d8bSRob Swindell 	}
2251cb4d1d62SKshitij Soni flash_pkg_exit:
2252cb4d1d62SKshitij Soni 	mutex_unlock(&bp->hwrm_cmd_lock);
22537c675421SVasundhara Volam err_exit:
225422630e28SEdwin Peer 	if (rc == -EACCES)
2255b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
2256cb4d1d62SKshitij Soni 	return rc;
2257c0c050c5SMichael Chan }
2258c0c050c5SMichael Chan 
2259c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
2260c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
2261c0c050c5SMichael Chan {
2262c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
2263c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
2264c0c050c5SMichael Chan 		return -EINVAL;
2265c0c050c5SMichael Chan 	}
2266c0c050c5SMichael Chan 
22675ac67d8bSRob Swindell 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
22685ac67d8bSRob Swindell 	    flash->region > 0xffff)
22695ac67d8bSRob Swindell 		return bnxt_flash_package_from_file(dev, flash->data,
22705ac67d8bSRob Swindell 						    flash->region);
2271c0c050c5SMichael Chan 
2272c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
2273c0c050c5SMichael Chan }
2274c0c050c5SMichael Chan 
2275c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
2276c0c050c5SMichael Chan {
2277c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2278c0c050c5SMichael Chan 	int rc;
2279c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
2280c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
2281c0c050c5SMichael Chan 
2282c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
2283c0c050c5SMichael Chan 
2284c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2285c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2286c0c050c5SMichael Chan 	if (!rc) {
2287c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
2288c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
2289c0c050c5SMichael Chan 	}
2290c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2291c0c050c5SMichael Chan 	return rc;
2292c0c050c5SMichael Chan }
2293c0c050c5SMichael Chan 
2294c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
2295c0c050c5SMichael Chan {
22964cebbacaSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
22974cebbacaSMichael Chan 
22984cebbacaSMichael Chan 	if (BNXT_VF(bp))
22994cebbacaSMichael Chan 		return 0;
23004cebbacaSMichael Chan 
2301c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
2302c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
2303c0c050c5SMichael Chan 	 */
2304c0c050c5SMichael Chan 	return -1;
2305c0c050c5SMichael Chan }
2306c0c050c5SMichael Chan 
2307c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
2308c0c050c5SMichael Chan {
2309c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2310c0c050c5SMichael Chan 	int rc;
2311c0c050c5SMichael Chan 	u32 dir_entries;
2312c0c050c5SMichael Chan 	u32 entry_length;
2313c0c050c5SMichael Chan 	u8 *buf;
2314c0c050c5SMichael Chan 	size_t buflen;
2315c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2316c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
2317c0c050c5SMichael Chan 
2318c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
2319c0c050c5SMichael Chan 	if (rc != 0)
2320c0c050c5SMichael Chan 		return rc;
2321c0c050c5SMichael Chan 
2322dbbfa96aSVasundhara Volam 	if (!dir_entries || !entry_length)
2323dbbfa96aSVasundhara Volam 		return -EIO;
2324dbbfa96aSVasundhara Volam 
2325c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
2326c0c050c5SMichael Chan 	if (len < 2)
2327c0c050c5SMichael Chan 		return -EINVAL;
2328c0c050c5SMichael Chan 
2329c0c050c5SMichael Chan 	*data++ = dir_entries;
2330c0c050c5SMichael Chan 	*data++ = entry_length;
2331c0c050c5SMichael Chan 	len -= 2;
2332c0c050c5SMichael Chan 	memset(data, 0xff, len);
2333c0c050c5SMichael Chan 
2334c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
2335c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
2336c0c050c5SMichael Chan 				 GFP_KERNEL);
2337c0c050c5SMichael Chan 	if (!buf) {
2338c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2339c0c050c5SMichael Chan 			   (unsigned)buflen);
2340c0c050c5SMichael Chan 		return -ENOMEM;
2341c0c050c5SMichael Chan 	}
2342c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
2343c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2344c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2345c0c050c5SMichael Chan 	if (rc == 0)
2346c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
2347c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
2348c0c050c5SMichael Chan 	return rc;
2349c0c050c5SMichael Chan }
2350c0c050c5SMichael Chan 
2351c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
2352c0c050c5SMichael Chan 			       u32 length, u8 *data)
2353c0c050c5SMichael Chan {
2354c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2355c0c050c5SMichael Chan 	int rc;
2356c0c050c5SMichael Chan 	u8 *buf;
2357c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2358c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
2359c0c050c5SMichael Chan 
2360e0ad8fc5SMichael Chan 	if (!length)
2361e0ad8fc5SMichael Chan 		return -EINVAL;
2362e0ad8fc5SMichael Chan 
2363c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
2364c0c050c5SMichael Chan 				 GFP_KERNEL);
2365c0c050c5SMichael Chan 	if (!buf) {
2366c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2367c0c050c5SMichael Chan 			   (unsigned)length);
2368c0c050c5SMichael Chan 		return -ENOMEM;
2369c0c050c5SMichael Chan 	}
2370c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
2371c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2372c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2373c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
2374c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
2375c0c050c5SMichael Chan 
2376c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2377c0c050c5SMichael Chan 	if (rc == 0)
2378c0c050c5SMichael Chan 		memcpy(data, buf, length);
2379c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
2380c0c050c5SMichael Chan 	return rc;
2381c0c050c5SMichael Chan }
2382c0c050c5SMichael Chan 
23833ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
23843ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
23853ebf6f0aSRob Swindell 				u32 *data_length)
23863ebf6f0aSRob Swindell {
23873ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
23883ebf6f0aSRob Swindell 	int rc;
23893ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
23903ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
23913ebf6f0aSRob Swindell 
23923ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
23933ebf6f0aSRob Swindell 	req.enables = 0;
23943ebf6f0aSRob Swindell 	req.dir_idx = 0;
23953ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
23963ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
23973ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
23983ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
2399cc72f3b1SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2400cc72f3b1SMichael Chan 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
24013ebf6f0aSRob Swindell 	if (rc == 0) {
24023ebf6f0aSRob Swindell 		if (index)
24033ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
24043ebf6f0aSRob Swindell 		if (item_length)
24053ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
24063ebf6f0aSRob Swindell 		if (data_length)
24073ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
24083ebf6f0aSRob Swindell 	}
2409cc72f3b1SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
24103ebf6f0aSRob Swindell 	return rc;
24113ebf6f0aSRob Swindell }
24123ebf6f0aSRob Swindell 
24133ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
24143ebf6f0aSRob Swindell {
24153ebf6f0aSRob Swindell 	char	*retval = NULL;
24163ebf6f0aSRob Swindell 	char	*p;
24173ebf6f0aSRob Swindell 	char	*value;
24183ebf6f0aSRob Swindell 	int	field = 0;
24193ebf6f0aSRob Swindell 
24203ebf6f0aSRob Swindell 	if (datalen < 1)
24213ebf6f0aSRob Swindell 		return NULL;
24223ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
24233ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
24243ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
24253ebf6f0aSRob Swindell 		field = 0;
24263ebf6f0aSRob Swindell 		retval = NULL;
24273ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
24283ebf6f0aSRob Swindell 			value = p;
24293ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
24303ebf6f0aSRob Swindell 				p++;
24313ebf6f0aSRob Swindell 			if (field == desired_field)
24323ebf6f0aSRob Swindell 				retval = value;
24333ebf6f0aSRob Swindell 			if (*p != '\t')
24343ebf6f0aSRob Swindell 				break;
24353ebf6f0aSRob Swindell 			*p = 0;
24363ebf6f0aSRob Swindell 			field++;
24373ebf6f0aSRob Swindell 			p++;
24383ebf6f0aSRob Swindell 		}
24393ebf6f0aSRob Swindell 		if (*p == 0)
24403ebf6f0aSRob Swindell 			break;
24413ebf6f0aSRob Swindell 		*p = 0;
24423ebf6f0aSRob Swindell 	}
24433ebf6f0aSRob Swindell 	return retval;
24443ebf6f0aSRob Swindell }
24453ebf6f0aSRob Swindell 
2446a60faa60SVasundhara Volam static void bnxt_get_pkgver(struct net_device *dev)
24473ebf6f0aSRob Swindell {
2448a60faa60SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
24493ebf6f0aSRob Swindell 	u16 index = 0;
2450a60faa60SVasundhara Volam 	char *pkgver;
2451a60faa60SVasundhara Volam 	u32 pkglen;
2452a60faa60SVasundhara Volam 	u8 *pkgbuf;
2453a60faa60SVasundhara Volam 	int len;
24543ebf6f0aSRob Swindell 
24553ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
24563ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2457a60faa60SVasundhara Volam 				 &index, NULL, &pkglen) != 0)
2458a60faa60SVasundhara Volam 		return;
24593ebf6f0aSRob Swindell 
2460a60faa60SVasundhara Volam 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
2461a60faa60SVasundhara Volam 	if (!pkgbuf) {
2462a60faa60SVasundhara Volam 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
2463a60faa60SVasundhara Volam 			pkglen);
2464a60faa60SVasundhara Volam 		return;
2465a60faa60SVasundhara Volam 	}
24663ebf6f0aSRob Swindell 
2467a60faa60SVasundhara Volam 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2468a60faa60SVasundhara Volam 		goto err;
2469a60faa60SVasundhara Volam 
2470a60faa60SVasundhara Volam 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2471a60faa60SVasundhara Volam 				   pkglen);
2472a60faa60SVasundhara Volam 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2473a60faa60SVasundhara Volam 		len = strlen(bp->fw_ver_str);
2474a60faa60SVasundhara Volam 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2475a60faa60SVasundhara Volam 			 "/pkg %s", pkgver);
2476a60faa60SVasundhara Volam 	}
2477a60faa60SVasundhara Volam err:
2478a60faa60SVasundhara Volam 	kfree(pkgbuf);
24793ebf6f0aSRob Swindell }
24803ebf6f0aSRob Swindell 
2481c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
2482c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2483c0c050c5SMichael Chan 			   u8 *data)
2484c0c050c5SMichael Chan {
2485c0c050c5SMichael Chan 	u32 index;
2486c0c050c5SMichael Chan 	u32 offset;
2487c0c050c5SMichael Chan 
2488c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
2489c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2490c0c050c5SMichael Chan 
2491c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
2492c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
2493c0c050c5SMichael Chan 
2494c0c050c5SMichael Chan 	if (index == 0) {
2495c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
2496c0c050c5SMichael Chan 		return -EINVAL;
2497c0c050c5SMichael Chan 	}
2498c0c050c5SMichael Chan 
2499c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2500c0c050c5SMichael Chan }
2501c0c050c5SMichael Chan 
2502c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2503c0c050c5SMichael Chan {
2504c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2505c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2506c0c050c5SMichael Chan 
2507c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2508c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2509c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2510c0c050c5SMichael Chan }
2511c0c050c5SMichael Chan 
2512c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
2513c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2514c0c050c5SMichael Chan 			   u8 *data)
2515c0c050c5SMichael Chan {
2516c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2517c0c050c5SMichael Chan 	u8 index, dir_op;
2518c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
2519c0c050c5SMichael Chan 
2520c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
2521c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2522c0c050c5SMichael Chan 		return -EINVAL;
2523c0c050c5SMichael Chan 	}
2524c0c050c5SMichael Chan 
2525c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
2526c0c050c5SMichael Chan 
2527c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
2528c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
2529c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
2530c0c050c5SMichael Chan 		if (index == 0)
2531c0c050c5SMichael Chan 			return -EINVAL;
2532c0c050c5SMichael Chan 		switch (dir_op) {
2533c0c050c5SMichael Chan 		case 0x0e: /* erase */
2534c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
2535c0c050c5SMichael Chan 				return -EINVAL;
2536c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
2537c0c050c5SMichael Chan 		default:
2538c0c050c5SMichael Chan 			return -EINVAL;
2539c0c050c5SMichael Chan 		}
2540c0c050c5SMichael Chan 	}
2541c0c050c5SMichael Chan 
2542c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
2543ba425800SJason Yan 	if (bnxt_dir_type_is_executable(type))
25445ac67d8bSRob Swindell 		return -EOPNOTSUPP;
2545c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
2546c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
2547c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
2548c0c050c5SMichael Chan 
2549c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2550c0c050c5SMichael Chan 				eeprom->len);
2551c0c050c5SMichael Chan }
2552c0c050c5SMichael Chan 
255372b34f04SMichael Chan static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
255472b34f04SMichael Chan {
255572b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
255672b34f04SMichael Chan 	struct ethtool_eee *eee = &bp->eee;
255772b34f04SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
2558a5390690SMichael Chan 	u32 advertising;
255972b34f04SMichael Chan 	int rc = 0;
256072b34f04SMichael Chan 
2561c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
256275362a3fSMichael Chan 		return -EOPNOTSUPP;
256372b34f04SMichael Chan 
256472b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
256572b34f04SMichael Chan 		return -EOPNOTSUPP;
256672b34f04SMichael Chan 
2567a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
2568a5390690SMichael Chan 	advertising = _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
256972b34f04SMichael Chan 	if (!edata->eee_enabled)
257072b34f04SMichael Chan 		goto eee_ok;
257172b34f04SMichael Chan 
257272b34f04SMichael Chan 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
257372b34f04SMichael Chan 		netdev_warn(dev, "EEE requires autoneg\n");
2574a5390690SMichael Chan 		rc = -EINVAL;
2575a5390690SMichael Chan 		goto eee_exit;
257672b34f04SMichael Chan 	}
257772b34f04SMichael Chan 	if (edata->tx_lpi_enabled) {
257872b34f04SMichael Chan 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
257972b34f04SMichael Chan 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
258072b34f04SMichael Chan 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
258172b34f04SMichael Chan 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
2582a5390690SMichael Chan 			rc = -EINVAL;
2583a5390690SMichael Chan 			goto eee_exit;
258472b34f04SMichael Chan 		} else if (!bp->lpi_tmr_hi) {
258572b34f04SMichael Chan 			edata->tx_lpi_timer = eee->tx_lpi_timer;
258672b34f04SMichael Chan 		}
258772b34f04SMichael Chan 	}
258872b34f04SMichael Chan 	if (!edata->advertised) {
258972b34f04SMichael Chan 		edata->advertised = advertising & eee->supported;
259072b34f04SMichael Chan 	} else if (edata->advertised & ~advertising) {
259172b34f04SMichael Chan 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
259272b34f04SMichael Chan 			    edata->advertised, advertising);
2593a5390690SMichael Chan 		rc = -EINVAL;
2594a5390690SMichael Chan 		goto eee_exit;
259572b34f04SMichael Chan 	}
259672b34f04SMichael Chan 
259772b34f04SMichael Chan 	eee->advertised = edata->advertised;
259872b34f04SMichael Chan 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
259972b34f04SMichael Chan 	eee->tx_lpi_timer = edata->tx_lpi_timer;
260072b34f04SMichael Chan eee_ok:
260172b34f04SMichael Chan 	eee->eee_enabled = edata->eee_enabled;
260272b34f04SMichael Chan 
260372b34f04SMichael Chan 	if (netif_running(dev))
260472b34f04SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
260572b34f04SMichael Chan 
2606a5390690SMichael Chan eee_exit:
2607a5390690SMichael Chan 	mutex_unlock(&bp->link_lock);
260872b34f04SMichael Chan 	return rc;
260972b34f04SMichael Chan }
261072b34f04SMichael Chan 
261172b34f04SMichael Chan static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
261272b34f04SMichael Chan {
261372b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
261472b34f04SMichael Chan 
261572b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
261672b34f04SMichael Chan 		return -EOPNOTSUPP;
261772b34f04SMichael Chan 
261872b34f04SMichael Chan 	*edata = bp->eee;
261972b34f04SMichael Chan 	if (!bp->eee.eee_enabled) {
262072b34f04SMichael Chan 		/* Preserve tx_lpi_timer so that the last value will be used
262172b34f04SMichael Chan 		 * by default when it is re-enabled.
262272b34f04SMichael Chan 		 */
262372b34f04SMichael Chan 		edata->advertised = 0;
262472b34f04SMichael Chan 		edata->tx_lpi_enabled = 0;
262572b34f04SMichael Chan 	}
262672b34f04SMichael Chan 
262772b34f04SMichael Chan 	if (!bp->eee.eee_active)
262872b34f04SMichael Chan 		edata->lp_advertised = 0;
262972b34f04SMichael Chan 
263072b34f04SMichael Chan 	return 0;
263172b34f04SMichael Chan }
263272b34f04SMichael Chan 
263342ee18feSAjit Khaparde static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
263442ee18feSAjit Khaparde 					    u16 page_number, u16 start_addr,
263542ee18feSAjit Khaparde 					    u16 data_length, u8 *buf)
263642ee18feSAjit Khaparde {
263742ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_input req = {0};
263842ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
263942ee18feSAjit Khaparde 	int rc, byte_offset = 0;
264042ee18feSAjit Khaparde 
264142ee18feSAjit Khaparde 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
264242ee18feSAjit Khaparde 	req.i2c_slave_addr = i2c_addr;
264342ee18feSAjit Khaparde 	req.page_number = cpu_to_le16(page_number);
264442ee18feSAjit Khaparde 	req.port_id = cpu_to_le16(bp->pf.port_id);
264542ee18feSAjit Khaparde 	do {
264642ee18feSAjit Khaparde 		u16 xfer_size;
264742ee18feSAjit Khaparde 
264842ee18feSAjit Khaparde 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
264942ee18feSAjit Khaparde 		data_length -= xfer_size;
265042ee18feSAjit Khaparde 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
265142ee18feSAjit Khaparde 		req.data_length = xfer_size;
265242ee18feSAjit Khaparde 		req.enables = cpu_to_le32(start_addr + byte_offset ?
265342ee18feSAjit Khaparde 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
265442ee18feSAjit Khaparde 		mutex_lock(&bp->hwrm_cmd_lock);
265542ee18feSAjit Khaparde 		rc = _hwrm_send_message(bp, &req, sizeof(req),
265642ee18feSAjit Khaparde 					HWRM_CMD_TIMEOUT);
265742ee18feSAjit Khaparde 		if (!rc)
265842ee18feSAjit Khaparde 			memcpy(buf + byte_offset, output->data, xfer_size);
265942ee18feSAjit Khaparde 		mutex_unlock(&bp->hwrm_cmd_lock);
266042ee18feSAjit Khaparde 		byte_offset += xfer_size;
266142ee18feSAjit Khaparde 	} while (!rc && data_length > 0);
266242ee18feSAjit Khaparde 
266342ee18feSAjit Khaparde 	return rc;
266442ee18feSAjit Khaparde }
266542ee18feSAjit Khaparde 
266642ee18feSAjit Khaparde static int bnxt_get_module_info(struct net_device *dev,
266742ee18feSAjit Khaparde 				struct ethtool_modinfo *modinfo)
266842ee18feSAjit Khaparde {
26697328a23cSVasundhara Volam 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
267042ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
267142ee18feSAjit Khaparde 	int rc;
267242ee18feSAjit Khaparde 
267342ee18feSAjit Khaparde 	/* No point in going further if phy status indicates
267442ee18feSAjit Khaparde 	 * module is not inserted or if it is powered down or
267542ee18feSAjit Khaparde 	 * if it is of type 10GBase-T
267642ee18feSAjit Khaparde 	 */
267742ee18feSAjit Khaparde 	if (bp->link_info.module_status >
267842ee18feSAjit Khaparde 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
267942ee18feSAjit Khaparde 		return -EOPNOTSUPP;
268042ee18feSAjit Khaparde 
268142ee18feSAjit Khaparde 	/* This feature is not supported in older firmware versions */
268242ee18feSAjit Khaparde 	if (bp->hwrm_spec_code < 0x10202)
268342ee18feSAjit Khaparde 		return -EOPNOTSUPP;
268442ee18feSAjit Khaparde 
26857328a23cSVasundhara Volam 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
26867328a23cSVasundhara Volam 					      SFF_DIAG_SUPPORT_OFFSET + 1,
26877328a23cSVasundhara Volam 					      data);
268842ee18feSAjit Khaparde 	if (!rc) {
26897328a23cSVasundhara Volam 		u8 module_id = data[0];
26907328a23cSVasundhara Volam 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
269142ee18feSAjit Khaparde 
269242ee18feSAjit Khaparde 		switch (module_id) {
269342ee18feSAjit Khaparde 		case SFF_MODULE_ID_SFP:
269442ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8472;
269542ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
26967328a23cSVasundhara Volam 			if (!diag_supported)
26977328a23cSVasundhara Volam 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
269842ee18feSAjit Khaparde 			break;
269942ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP:
270042ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP_PLUS:
270142ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8436;
270242ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
270342ee18feSAjit Khaparde 			break;
270442ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP28:
270542ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8636;
270642ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
270742ee18feSAjit Khaparde 			break;
270842ee18feSAjit Khaparde 		default:
270942ee18feSAjit Khaparde 			rc = -EOPNOTSUPP;
271042ee18feSAjit Khaparde 			break;
271142ee18feSAjit Khaparde 		}
271242ee18feSAjit Khaparde 	}
271342ee18feSAjit Khaparde 	return rc;
271442ee18feSAjit Khaparde }
271542ee18feSAjit Khaparde 
271642ee18feSAjit Khaparde static int bnxt_get_module_eeprom(struct net_device *dev,
271742ee18feSAjit Khaparde 				  struct ethtool_eeprom *eeprom,
271842ee18feSAjit Khaparde 				  u8 *data)
271942ee18feSAjit Khaparde {
272042ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
272142ee18feSAjit Khaparde 	u16  start = eeprom->offset, length = eeprom->len;
2722f3ea3119SColin Ian King 	int rc = 0;
272342ee18feSAjit Khaparde 
272442ee18feSAjit Khaparde 	memset(data, 0, eeprom->len);
272542ee18feSAjit Khaparde 
272642ee18feSAjit Khaparde 	/* Read A0 portion of the EEPROM */
272742ee18feSAjit Khaparde 	if (start < ETH_MODULE_SFF_8436_LEN) {
272842ee18feSAjit Khaparde 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
272942ee18feSAjit Khaparde 			length = ETH_MODULE_SFF_8436_LEN - start;
273042ee18feSAjit Khaparde 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
273142ee18feSAjit Khaparde 						      start, length, data);
273242ee18feSAjit Khaparde 		if (rc)
273342ee18feSAjit Khaparde 			return rc;
273442ee18feSAjit Khaparde 		start += length;
273542ee18feSAjit Khaparde 		data += length;
273642ee18feSAjit Khaparde 		length = eeprom->len - length;
273742ee18feSAjit Khaparde 	}
273842ee18feSAjit Khaparde 
273942ee18feSAjit Khaparde 	/* Read A2 portion of the EEPROM */
274042ee18feSAjit Khaparde 	if (length) {
274142ee18feSAjit Khaparde 		start -= ETH_MODULE_SFF_8436_LEN;
2742dea521a2SChristophe JAILLET 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1,
2743dea521a2SChristophe JAILLET 						      start, length, data);
274442ee18feSAjit Khaparde 	}
274542ee18feSAjit Khaparde 	return rc;
274642ee18feSAjit Khaparde }
274742ee18feSAjit Khaparde 
2748ae8e98a6SDeepak Khungar static int bnxt_nway_reset(struct net_device *dev)
2749ae8e98a6SDeepak Khungar {
2750ae8e98a6SDeepak Khungar 	int rc = 0;
2751ae8e98a6SDeepak Khungar 
2752ae8e98a6SDeepak Khungar 	struct bnxt *bp = netdev_priv(dev);
2753ae8e98a6SDeepak Khungar 	struct bnxt_link_info *link_info = &bp->link_info;
2754ae8e98a6SDeepak Khungar 
2755c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
2756ae8e98a6SDeepak Khungar 		return -EOPNOTSUPP;
2757ae8e98a6SDeepak Khungar 
2758ae8e98a6SDeepak Khungar 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2759ae8e98a6SDeepak Khungar 		return -EINVAL;
2760ae8e98a6SDeepak Khungar 
2761ae8e98a6SDeepak Khungar 	if (netif_running(dev))
2762ae8e98a6SDeepak Khungar 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
2763ae8e98a6SDeepak Khungar 
2764ae8e98a6SDeepak Khungar 	return rc;
2765ae8e98a6SDeepak Khungar }
2766ae8e98a6SDeepak Khungar 
27675ad2cbeeSMichael Chan static int bnxt_set_phys_id(struct net_device *dev,
27685ad2cbeeSMichael Chan 			    enum ethtool_phys_id_state state)
27695ad2cbeeSMichael Chan {
27705ad2cbeeSMichael Chan 	struct hwrm_port_led_cfg_input req = {0};
27715ad2cbeeSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
27725ad2cbeeSMichael Chan 	struct bnxt_pf_info *pf = &bp->pf;
27735ad2cbeeSMichael Chan 	struct bnxt_led_cfg *led_cfg;
27745ad2cbeeSMichael Chan 	u8 led_state;
27755ad2cbeeSMichael Chan 	__le16 duration;
27769f90445cSVasundhara Volam 	int i;
27775ad2cbeeSMichael Chan 
27785ad2cbeeSMichael Chan 	if (!bp->num_leds || BNXT_VF(bp))
27795ad2cbeeSMichael Chan 		return -EOPNOTSUPP;
27805ad2cbeeSMichael Chan 
27815ad2cbeeSMichael Chan 	if (state == ETHTOOL_ID_ACTIVE) {
27825ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
27835ad2cbeeSMichael Chan 		duration = cpu_to_le16(500);
27845ad2cbeeSMichael Chan 	} else if (state == ETHTOOL_ID_INACTIVE) {
27855ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
27865ad2cbeeSMichael Chan 		duration = cpu_to_le16(0);
27875ad2cbeeSMichael Chan 	} else {
27885ad2cbeeSMichael Chan 		return -EINVAL;
27895ad2cbeeSMichael Chan 	}
27905ad2cbeeSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
27915ad2cbeeSMichael Chan 	req.port_id = cpu_to_le16(pf->port_id);
27925ad2cbeeSMichael Chan 	req.num_leds = bp->num_leds;
27935ad2cbeeSMichael Chan 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
27945ad2cbeeSMichael Chan 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
27955ad2cbeeSMichael Chan 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
27965ad2cbeeSMichael Chan 		led_cfg->led_id = bp->leds[i].led_id;
27975ad2cbeeSMichael Chan 		led_cfg->led_state = led_state;
27985ad2cbeeSMichael Chan 		led_cfg->led_blink_on = duration;
27995ad2cbeeSMichael Chan 		led_cfg->led_blink_off = duration;
28005ad2cbeeSMichael Chan 		led_cfg->led_group_id = bp->leds[i].led_group_id;
28015ad2cbeeSMichael Chan 	}
28029f90445cSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
28035ad2cbeeSMichael Chan }
28045ad2cbeeSMichael Chan 
280567fea463SMichael Chan static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
280667fea463SMichael Chan {
280767fea463SMichael Chan 	struct hwrm_selftest_irq_input req = {0};
280867fea463SMichael Chan 
280967fea463SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
281067fea463SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
281167fea463SMichael Chan }
281267fea463SMichael Chan 
281367fea463SMichael Chan static int bnxt_test_irq(struct bnxt *bp)
281467fea463SMichael Chan {
281567fea463SMichael Chan 	int i;
281667fea463SMichael Chan 
281767fea463SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
281867fea463SMichael Chan 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
281967fea463SMichael Chan 		int rc;
282067fea463SMichael Chan 
282167fea463SMichael Chan 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
282267fea463SMichael Chan 		if (rc)
282367fea463SMichael Chan 			return rc;
282467fea463SMichael Chan 	}
282567fea463SMichael Chan 	return 0;
282667fea463SMichael Chan }
282767fea463SMichael Chan 
2828f7dc1ea6SMichael Chan static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
2829f7dc1ea6SMichael Chan {
2830f7dc1ea6SMichael Chan 	struct hwrm_port_mac_cfg_input req = {0};
2831f7dc1ea6SMichael Chan 
2832f7dc1ea6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
2833f7dc1ea6SMichael Chan 
2834f7dc1ea6SMichael Chan 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
2835f7dc1ea6SMichael Chan 	if (enable)
2836f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
2837f7dc1ea6SMichael Chan 	else
2838f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
2839f7dc1ea6SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2840f7dc1ea6SMichael Chan }
2841f7dc1ea6SMichael Chan 
284256d37462SVasundhara Volam static int bnxt_query_force_speeds(struct bnxt *bp, u16 *force_speeds)
284356d37462SVasundhara Volam {
284456d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
284556d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_input req = {0};
284656d37462SVasundhara Volam 	int rc;
284756d37462SVasundhara Volam 
284856d37462SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
284956d37462SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
285056d37462SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
285156d37462SVasundhara Volam 	if (!rc)
285256d37462SVasundhara Volam 		*force_speeds = le16_to_cpu(resp->supported_speeds_force_mode);
285356d37462SVasundhara Volam 
285456d37462SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
285556d37462SVasundhara Volam 	return rc;
285656d37462SVasundhara Volam }
285756d37462SVasundhara Volam 
285891725d89SMichael Chan static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
285991725d89SMichael Chan 				    struct hwrm_port_phy_cfg_input *req)
286091725d89SMichael Chan {
286191725d89SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
286256d37462SVasundhara Volam 	u16 fw_advertising;
286391725d89SMichael Chan 	u16 fw_speed;
286491725d89SMichael Chan 	int rc;
286591725d89SMichael Chan 
28668a60efd1SMichael Chan 	if (!link_info->autoneg ||
28678a60efd1SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_AN_PHY_LPBK))
286891725d89SMichael Chan 		return 0;
286991725d89SMichael Chan 
287056d37462SVasundhara Volam 	rc = bnxt_query_force_speeds(bp, &fw_advertising);
287156d37462SVasundhara Volam 	if (rc)
287256d37462SVasundhara Volam 		return rc;
287356d37462SVasundhara Volam 
287491725d89SMichael Chan 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
287583d8f5e9SMichael Chan 	if (bp->link_info.link_up)
287691725d89SMichael Chan 		fw_speed = bp->link_info.link_speed;
287791725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
287891725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
287991725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
288091725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
288191725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
288291725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
288391725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
288491725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
288591725d89SMichael Chan 
288691725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(fw_speed);
288791725d89SMichael Chan 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
288891725d89SMichael Chan 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
288991725d89SMichael Chan 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
289091725d89SMichael Chan 	req->flags = 0;
289191725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(0);
289291725d89SMichael Chan 	return rc;
289391725d89SMichael Chan }
289491725d89SMichael Chan 
289555fd0cf3SMichael Chan static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
289691725d89SMichael Chan {
289791725d89SMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
289891725d89SMichael Chan 
289991725d89SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
290091725d89SMichael Chan 
290191725d89SMichael Chan 	if (enable) {
290291725d89SMichael Chan 		bnxt_disable_an_for_lpbk(bp, &req);
290355fd0cf3SMichael Chan 		if (ext)
290455fd0cf3SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
290555fd0cf3SMichael Chan 		else
290691725d89SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
290791725d89SMichael Chan 	} else {
290891725d89SMichael Chan 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
290991725d89SMichael Chan 	}
291091725d89SMichael Chan 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
291191725d89SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
291291725d89SMichael Chan }
291391725d89SMichael Chan 
2914e44758b7SMichael Chan static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2915f7dc1ea6SMichael Chan 			    u32 raw_cons, int pkt_size)
2916f7dc1ea6SMichael Chan {
2917e44758b7SMichael Chan 	struct bnxt_napi *bnapi = cpr->bnapi;
2918e44758b7SMichael Chan 	struct bnxt_rx_ring_info *rxr;
2919f7dc1ea6SMichael Chan 	struct bnxt_sw_rx_bd *rx_buf;
2920f7dc1ea6SMichael Chan 	struct rx_cmp *rxcmp;
2921f7dc1ea6SMichael Chan 	u16 cp_cons, cons;
2922f7dc1ea6SMichael Chan 	u8 *data;
2923f7dc1ea6SMichael Chan 	u32 len;
2924f7dc1ea6SMichael Chan 	int i;
2925f7dc1ea6SMichael Chan 
2926e44758b7SMichael Chan 	rxr = bnapi->rx_ring;
2927f7dc1ea6SMichael Chan 	cp_cons = RING_CMP(raw_cons);
2928f7dc1ea6SMichael Chan 	rxcmp = (struct rx_cmp *)
2929f7dc1ea6SMichael Chan 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2930f7dc1ea6SMichael Chan 	cons = rxcmp->rx_cmp_opaque;
2931f7dc1ea6SMichael Chan 	rx_buf = &rxr->rx_buf_ring[cons];
2932f7dc1ea6SMichael Chan 	data = rx_buf->data_ptr;
2933f7dc1ea6SMichael Chan 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
2934f7dc1ea6SMichael Chan 	if (len != pkt_size)
2935f7dc1ea6SMichael Chan 		return -EIO;
2936f7dc1ea6SMichael Chan 	i = ETH_ALEN;
2937f7dc1ea6SMichael Chan 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
2938f7dc1ea6SMichael Chan 		return -EIO;
2939f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2940f7dc1ea6SMichael Chan 	for (  ; i < pkt_size; i++) {
2941f7dc1ea6SMichael Chan 		if (data[i] != (u8)(i & 0xff))
2942f7dc1ea6SMichael Chan 			return -EIO;
2943f7dc1ea6SMichael Chan 	}
2944f7dc1ea6SMichael Chan 	return 0;
2945f7dc1ea6SMichael Chan }
2946f7dc1ea6SMichael Chan 
2947e44758b7SMichael Chan static int bnxt_poll_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2948e44758b7SMichael Chan 			      int pkt_size)
2949f7dc1ea6SMichael Chan {
2950f7dc1ea6SMichael Chan 	struct tx_cmp *txcmp;
2951f7dc1ea6SMichael Chan 	int rc = -EIO;
2952f7dc1ea6SMichael Chan 	u32 raw_cons;
2953f7dc1ea6SMichael Chan 	u32 cons;
2954f7dc1ea6SMichael Chan 	int i;
2955f7dc1ea6SMichael Chan 
2956f7dc1ea6SMichael Chan 	raw_cons = cpr->cp_raw_cons;
2957f7dc1ea6SMichael Chan 	for (i = 0; i < 200; i++) {
2958f7dc1ea6SMichael Chan 		cons = RING_CMP(raw_cons);
2959f7dc1ea6SMichael Chan 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2960f7dc1ea6SMichael Chan 
2961f7dc1ea6SMichael Chan 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
2962f7dc1ea6SMichael Chan 			udelay(5);
2963f7dc1ea6SMichael Chan 			continue;
2964f7dc1ea6SMichael Chan 		}
2965f7dc1ea6SMichael Chan 
2966f7dc1ea6SMichael Chan 		/* The valid test of the entry must be done first before
2967f7dc1ea6SMichael Chan 		 * reading any further.
2968f7dc1ea6SMichael Chan 		 */
2969f7dc1ea6SMichael Chan 		dma_rmb();
2970f7dc1ea6SMichael Chan 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
2971e44758b7SMichael Chan 			rc = bnxt_rx_loopback(bp, cpr, raw_cons, pkt_size);
2972f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2973f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2974f7dc1ea6SMichael Chan 			break;
2975f7dc1ea6SMichael Chan 		}
2976f7dc1ea6SMichael Chan 		raw_cons = NEXT_RAW_CMP(raw_cons);
2977f7dc1ea6SMichael Chan 	}
2978f7dc1ea6SMichael Chan 	cpr->cp_raw_cons = raw_cons;
2979f7dc1ea6SMichael Chan 	return rc;
2980f7dc1ea6SMichael Chan }
2981f7dc1ea6SMichael Chan 
2982f7dc1ea6SMichael Chan static int bnxt_run_loopback(struct bnxt *bp)
2983f7dc1ea6SMichael Chan {
2984f7dc1ea6SMichael Chan 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
298584404d5fSMichael Chan 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
2986e44758b7SMichael Chan 	struct bnxt_cp_ring_info *cpr;
2987f7dc1ea6SMichael Chan 	int pkt_size, i = 0;
2988f7dc1ea6SMichael Chan 	struct sk_buff *skb;
2989f7dc1ea6SMichael Chan 	dma_addr_t map;
2990f7dc1ea6SMichael Chan 	u8 *data;
2991f7dc1ea6SMichael Chan 	int rc;
2992f7dc1ea6SMichael Chan 
299384404d5fSMichael Chan 	cpr = &rxr->bnapi->cp_ring;
299484404d5fSMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
299584404d5fSMichael Chan 		cpr = cpr->cp_ring_arr[BNXT_RX_HDL];
2996f7dc1ea6SMichael Chan 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
2997f7dc1ea6SMichael Chan 	skb = netdev_alloc_skb(bp->dev, pkt_size);
2998f7dc1ea6SMichael Chan 	if (!skb)
2999f7dc1ea6SMichael Chan 		return -ENOMEM;
3000f7dc1ea6SMichael Chan 	data = skb_put(skb, pkt_size);
3001f7dc1ea6SMichael Chan 	eth_broadcast_addr(data);
3002f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3003f7dc1ea6SMichael Chan 	ether_addr_copy(&data[i], bp->dev->dev_addr);
3004f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3005f7dc1ea6SMichael Chan 	for ( ; i < pkt_size; i++)
3006f7dc1ea6SMichael Chan 		data[i] = (u8)(i & 0xff);
3007f7dc1ea6SMichael Chan 
3008f7dc1ea6SMichael Chan 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
3009f7dc1ea6SMichael Chan 			     PCI_DMA_TODEVICE);
3010f7dc1ea6SMichael Chan 	if (dma_mapping_error(&bp->pdev->dev, map)) {
3011f7dc1ea6SMichael Chan 		dev_kfree_skb(skb);
3012f7dc1ea6SMichael Chan 		return -EIO;
3013f7dc1ea6SMichael Chan 	}
3014c1ba92a8SMichael Chan 	bnxt_xmit_bd(bp, txr, map, pkt_size);
3015f7dc1ea6SMichael Chan 
3016f7dc1ea6SMichael Chan 	/* Sync BD data before updating doorbell */
3017f7dc1ea6SMichael Chan 	wmb();
3018f7dc1ea6SMichael Chan 
3019697197e5SMichael Chan 	bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
3020e44758b7SMichael Chan 	rc = bnxt_poll_loopback(bp, cpr, pkt_size);
3021f7dc1ea6SMichael Chan 
3022f7dc1ea6SMichael Chan 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
3023f7dc1ea6SMichael Chan 	dev_kfree_skb(skb);
3024f7dc1ea6SMichael Chan 	return rc;
3025f7dc1ea6SMichael Chan }
3026f7dc1ea6SMichael Chan 
3027eb513658SMichael Chan static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
3028eb513658SMichael Chan {
3029eb513658SMichael Chan 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
3030eb513658SMichael Chan 	struct hwrm_selftest_exec_input req = {0};
3031eb513658SMichael Chan 	int rc;
3032eb513658SMichael Chan 
3033eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
3034eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3035eb513658SMichael Chan 	resp->test_success = 0;
3036eb513658SMichael Chan 	req.flags = test_mask;
3037eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
3038eb513658SMichael Chan 	*test_results = resp->test_success;
3039eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3040eb513658SMichael Chan 	return rc;
3041eb513658SMichael Chan }
3042eb513658SMichael Chan 
304355fd0cf3SMichael Chan #define BNXT_DRV_TESTS			4
3044f7dc1ea6SMichael Chan #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
304591725d89SMichael Chan #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
304655fd0cf3SMichael Chan #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
304755fd0cf3SMichael Chan #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
3048eb513658SMichael Chan 
3049eb513658SMichael Chan static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
3050eb513658SMichael Chan 			   u64 *buf)
3051eb513658SMichael Chan {
3052eb513658SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
305355fd0cf3SMichael Chan 	bool do_ext_lpbk = false;
3054eb513658SMichael Chan 	bool offline = false;
3055eb513658SMichael Chan 	u8 test_results = 0;
3056eb513658SMichael Chan 	u8 test_mask = 0;
3057d27e2ca1SMichael Chan 	int rc = 0, i;
3058eb513658SMichael Chan 
3059eb513658SMichael Chan 	if (!bp->num_tests || !BNXT_SINGLE_PF(bp))
3060eb513658SMichael Chan 		return;
3061eb513658SMichael Chan 	memset(buf, 0, sizeof(u64) * bp->num_tests);
3062eb513658SMichael Chan 	if (!netif_running(dev)) {
3063eb513658SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
3064eb513658SMichael Chan 		return;
3065eb513658SMichael Chan 	}
3066eb513658SMichael Chan 
306755fd0cf3SMichael Chan 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
306855fd0cf3SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_EXT_LPBK))
306955fd0cf3SMichael Chan 		do_ext_lpbk = true;
307055fd0cf3SMichael Chan 
3071eb513658SMichael Chan 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
3072eb513658SMichael Chan 		if (bp->pf.active_vfs) {
3073eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3074eb513658SMichael Chan 			netdev_warn(dev, "Offline tests cannot be run with active VFs\n");
3075eb513658SMichael Chan 			return;
3076eb513658SMichael Chan 		}
3077eb513658SMichael Chan 		offline = true;
3078eb513658SMichael Chan 	}
3079eb513658SMichael Chan 
3080eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3081eb513658SMichael Chan 		u8 bit_val = 1 << i;
3082eb513658SMichael Chan 
3083eb513658SMichael Chan 		if (!(bp->test_info->offline_mask & bit_val))
3084eb513658SMichael Chan 			test_mask |= bit_val;
3085eb513658SMichael Chan 		else if (offline)
3086eb513658SMichael Chan 			test_mask |= bit_val;
3087eb513658SMichael Chan 	}
3088eb513658SMichael Chan 	if (!offline) {
3089eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3090eb513658SMichael Chan 	} else {
3091eb513658SMichael Chan 		rc = bnxt_close_nic(bp, false, false);
3092eb513658SMichael Chan 		if (rc)
3093eb513658SMichael Chan 			return;
3094eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3095f7dc1ea6SMichael Chan 
3096f7dc1ea6SMichael Chan 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
3097f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, true);
3098f7dc1ea6SMichael Chan 		msleep(250);
3099f7dc1ea6SMichael Chan 		rc = bnxt_half_open_nic(bp);
3100f7dc1ea6SMichael Chan 		if (rc) {
3101f7dc1ea6SMichael Chan 			bnxt_hwrm_mac_loopback(bp, false);
3102f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3103f7dc1ea6SMichael Chan 			return;
3104f7dc1ea6SMichael Chan 		}
3105f7dc1ea6SMichael Chan 		if (bnxt_run_loopback(bp))
3106f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3107f7dc1ea6SMichael Chan 		else
3108f7dc1ea6SMichael Chan 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
3109f7dc1ea6SMichael Chan 
3110f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, false);
311155fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, true, false);
311291725d89SMichael Chan 		msleep(1000);
311391725d89SMichael Chan 		if (bnxt_run_loopback(bp)) {
311491725d89SMichael Chan 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
311591725d89SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
311691725d89SMichael Chan 		}
311755fd0cf3SMichael Chan 		if (do_ext_lpbk) {
311855fd0cf3SMichael Chan 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
311955fd0cf3SMichael Chan 			bnxt_hwrm_phy_loopback(bp, true, true);
312055fd0cf3SMichael Chan 			msleep(1000);
312155fd0cf3SMichael Chan 			if (bnxt_run_loopback(bp)) {
312255fd0cf3SMichael Chan 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
312355fd0cf3SMichael Chan 				etest->flags |= ETH_TEST_FL_FAILED;
312455fd0cf3SMichael Chan 			}
312555fd0cf3SMichael Chan 		}
312655fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, false, false);
312791725d89SMichael Chan 		bnxt_half_close_nic(bp);
3128d27e2ca1SMichael Chan 		rc = bnxt_open_nic(bp, false, true);
3129eb513658SMichael Chan 	}
3130d27e2ca1SMichael Chan 	if (rc || bnxt_test_irq(bp)) {
313167fea463SMichael Chan 		buf[BNXT_IRQ_TEST_IDX] = 1;
313267fea463SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
313367fea463SMichael Chan 	}
3134eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3135eb513658SMichael Chan 		u8 bit_val = 1 << i;
3136eb513658SMichael Chan 
3137eb513658SMichael Chan 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
3138eb513658SMichael Chan 			buf[i] = 1;
3139eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3140eb513658SMichael Chan 		}
3141eb513658SMichael Chan 	}
3142eb513658SMichael Chan }
3143eb513658SMichael Chan 
314449f7972fSVasundhara Volam static int bnxt_reset(struct net_device *dev, u32 *flags)
314549f7972fSVasundhara Volam {
314649f7972fSVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
31478cec0940SEdwin Peer 	bool reload = false;
31487a13240eSEdwin Peer 	u32 req = *flags;
31497a13240eSEdwin Peer 
31507a13240eSEdwin Peer 	if (!req)
31517a13240eSEdwin Peer 		return -EINVAL;
315249f7972fSVasundhara Volam 
315349f7972fSVasundhara Volam 	if (!BNXT_PF(bp)) {
315449f7972fSVasundhara Volam 		netdev_err(dev, "Reset is not supported from a VF\n");
315549f7972fSVasundhara Volam 		return -EOPNOTSUPP;
315649f7972fSVasundhara Volam 	}
315749f7972fSVasundhara Volam 
31580a3f4e4fSVasundhara Volam 	if (pci_vfs_assigned(bp->pdev) &&
31590a3f4e4fSVasundhara Volam 	    !(bp->fw_cap & BNXT_FW_CAP_HOT_RESET)) {
316049f7972fSVasundhara Volam 		netdev_err(dev,
316149f7972fSVasundhara Volam 			   "Reset not allowed when VFs are assigned to VMs\n");
316249f7972fSVasundhara Volam 		return -EBUSY;
316349f7972fSVasundhara Volam 	}
316449f7972fSVasundhara Volam 
31657a13240eSEdwin Peer 	if ((req & BNXT_FW_RESET_CHIP) == BNXT_FW_RESET_CHIP) {
316649f7972fSVasundhara Volam 		/* This feature is not supported in older firmware versions */
31677a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
31687a13240eSEdwin Peer 			if (!bnxt_firmware_reset_chip(dev)) {
31697a13240eSEdwin Peer 				netdev_info(dev, "Firmware reset request successful.\n");
31700a3f4e4fSVasundhara Volam 				if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET))
31718cec0940SEdwin Peer 					reload = true;
31727a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_CHIP;
31732373d8d6SScott Branden 			}
31747a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_CHIP) {
31757a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
31767a13240eSEdwin Peer 		}
31777a13240eSEdwin Peer 	}
31787a13240eSEdwin Peer 
31797a13240eSEdwin Peer 	if (req & BNXT_FW_RESET_AP) {
31806502ad59SScott Branden 		/* This feature is not supported in older firmware versions */
31817a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
31827a13240eSEdwin Peer 			if (!bnxt_firmware_reset_ap(dev)) {
31837a13240eSEdwin Peer 				netdev_info(dev, "Reset application processor successful.\n");
31848cec0940SEdwin Peer 				reload = true;
31857a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_AP;
31862373d8d6SScott Branden 			}
31877a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_AP) {
31887a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
31897a13240eSEdwin Peer 		}
319049f7972fSVasundhara Volam 	}
319149f7972fSVasundhara Volam 
31928cec0940SEdwin Peer 	if (reload)
31938cec0940SEdwin Peer 		netdev_info(dev, "Reload driver to complete reset\n");
31948cec0940SEdwin Peer 
31957a13240eSEdwin Peer 	return 0;
319649f7972fSVasundhara Volam }
319749f7972fSVasundhara Volam 
31986c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
31996c5657d0SVasundhara Volam 				  struct bnxt_hwrm_dbg_dma_info *info)
32006c5657d0SVasundhara Volam {
32016c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
32026c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_input *cmn_req = msg;
32036c5657d0SVasundhara Volam 	__le16 *seq_ptr = msg + info->seq_off;
32046c5657d0SVasundhara Volam 	u16 seq = 0, len, segs_off;
32056c5657d0SVasundhara Volam 	void *resp = cmn_resp;
32066c5657d0SVasundhara Volam 	dma_addr_t dma_handle;
32076c5657d0SVasundhara Volam 	int rc, off = 0;
32086c5657d0SVasundhara Volam 	void *dma_buf;
32096c5657d0SVasundhara Volam 
32106c5657d0SVasundhara Volam 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
32116c5657d0SVasundhara Volam 				     GFP_KERNEL);
32126c5657d0SVasundhara Volam 	if (!dma_buf)
32136c5657d0SVasundhara Volam 		return -ENOMEM;
32146c5657d0SVasundhara Volam 
32156c5657d0SVasundhara Volam 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
32166c5657d0SVasundhara Volam 			    total_segments);
32176c5657d0SVasundhara Volam 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
32186c5657d0SVasundhara Volam 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
32196c5657d0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
32206c5657d0SVasundhara Volam 	while (1) {
32216c5657d0SVasundhara Volam 		*seq_ptr = cpu_to_le16(seq);
32225b306bdeSVasundhara Volam 		rc = _hwrm_send_message(bp, msg, msg_len,
32235b306bdeSVasundhara Volam 					HWRM_COREDUMP_TIMEOUT);
32246c5657d0SVasundhara Volam 		if (rc)
32256c5657d0SVasundhara Volam 			break;
32266c5657d0SVasundhara Volam 
32276c5657d0SVasundhara Volam 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
32286c5657d0SVasundhara Volam 		if (!seq &&
32296c5657d0SVasundhara Volam 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
32306c5657d0SVasundhara Volam 			info->segs = le16_to_cpu(*((__le16 *)(resp +
32316c5657d0SVasundhara Volam 							      segs_off)));
32326c5657d0SVasundhara Volam 			if (!info->segs) {
32336c5657d0SVasundhara Volam 				rc = -EIO;
32346c5657d0SVasundhara Volam 				break;
32356c5657d0SVasundhara Volam 			}
32366c5657d0SVasundhara Volam 
32376c5657d0SVasundhara Volam 			info->dest_buf_size = info->segs *
32386c5657d0SVasundhara Volam 					sizeof(struct coredump_segment_record);
32396c5657d0SVasundhara Volam 			info->dest_buf = kmalloc(info->dest_buf_size,
32406c5657d0SVasundhara Volam 						 GFP_KERNEL);
32416c5657d0SVasundhara Volam 			if (!info->dest_buf) {
32426c5657d0SVasundhara Volam 				rc = -ENOMEM;
32436c5657d0SVasundhara Volam 				break;
32446c5657d0SVasundhara Volam 			}
32456c5657d0SVasundhara Volam 		}
32466c5657d0SVasundhara Volam 
3247c74751f4SVasundhara Volam 		if (info->dest_buf) {
3248c74751f4SVasundhara Volam 			if ((info->seg_start + off + len) <=
3249c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
32506c5657d0SVasundhara Volam 				memcpy(info->dest_buf + off, dma_buf, len);
3251c74751f4SVasundhara Volam 			} else {
3252c74751f4SVasundhara Volam 				rc = -ENOBUFS;
3253c74751f4SVasundhara Volam 				break;
3254c74751f4SVasundhara Volam 			}
3255c74751f4SVasundhara Volam 		}
32566c5657d0SVasundhara Volam 
32576c5657d0SVasundhara Volam 		if (cmn_req->req_type ==
32586c5657d0SVasundhara Volam 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
32596c5657d0SVasundhara Volam 			info->dest_buf_size += len;
32606c5657d0SVasundhara Volam 
32616c5657d0SVasundhara Volam 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
32626c5657d0SVasundhara Volam 			break;
32636c5657d0SVasundhara Volam 
32646c5657d0SVasundhara Volam 		seq++;
32656c5657d0SVasundhara Volam 		off += len;
32666c5657d0SVasundhara Volam 	}
32676c5657d0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
32686c5657d0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
32696c5657d0SVasundhara Volam 	return rc;
32706c5657d0SVasundhara Volam }
32716c5657d0SVasundhara Volam 
32726c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
32736c5657d0SVasundhara Volam 				       struct bnxt_coredump *coredump)
32746c5657d0SVasundhara Volam {
32756c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_list_input req = {0};
32766c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
32776c5657d0SVasundhara Volam 	int rc;
32786c5657d0SVasundhara Volam 
32796c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
32806c5657d0SVasundhara Volam 
32816c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_LIST_BUF_LEN;
32826c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
32836c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
32846c5657d0SVasundhara Volam 				     data_len);
32856c5657d0SVasundhara Volam 
32866c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
32876c5657d0SVasundhara Volam 	if (!rc) {
32886c5657d0SVasundhara Volam 		coredump->data = info.dest_buf;
32896c5657d0SVasundhara Volam 		coredump->data_size = info.dest_buf_size;
32906c5657d0SVasundhara Volam 		coredump->total_segs = info.segs;
32916c5657d0SVasundhara Volam 	}
32926c5657d0SVasundhara Volam 	return rc;
32936c5657d0SVasundhara Volam }
32946c5657d0SVasundhara Volam 
32956c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
32966c5657d0SVasundhara Volam 					   u16 segment_id)
32976c5657d0SVasundhara Volam {
32986c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_initiate_input req = {0};
32996c5657d0SVasundhara Volam 
33006c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
33016c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
33026c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
33036c5657d0SVasundhara Volam 
330457a8730bSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_COREDUMP_TIMEOUT);
33056c5657d0SVasundhara Volam }
33066c5657d0SVasundhara Volam 
33076c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
33086c5657d0SVasundhara Volam 					   u16 segment_id, u32 *seg_len,
3309c74751f4SVasundhara Volam 					   void *buf, u32 buf_len, u32 offset)
33106c5657d0SVasundhara Volam {
33116c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_retrieve_input req = {0};
33126c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
33136c5657d0SVasundhara Volam 	int rc;
33146c5657d0SVasundhara Volam 
33156c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
33166c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
33176c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
33186c5657d0SVasundhara Volam 
33196c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
33206c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
33216c5657d0SVasundhara Volam 				seq_no);
33226c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
33236c5657d0SVasundhara Volam 				     data_len);
3324c74751f4SVasundhara Volam 	if (buf) {
33256c5657d0SVasundhara Volam 		info.dest_buf = buf + offset;
3326c74751f4SVasundhara Volam 		info.buf_len = buf_len;
3327c74751f4SVasundhara Volam 		info.seg_start = offset;
3328c74751f4SVasundhara Volam 	}
33296c5657d0SVasundhara Volam 
33306c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
33316c5657d0SVasundhara Volam 	if (!rc)
33326c5657d0SVasundhara Volam 		*seg_len = info.dest_buf_size;
33336c5657d0SVasundhara Volam 
33346c5657d0SVasundhara Volam 	return rc;
33356c5657d0SVasundhara Volam }
33366c5657d0SVasundhara Volam 
33376c5657d0SVasundhara Volam static void
33386c5657d0SVasundhara Volam bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
33396c5657d0SVasundhara Volam 			   struct bnxt_coredump_segment_hdr *seg_hdr,
33406c5657d0SVasundhara Volam 			   struct coredump_segment_record *seg_rec, u32 seg_len,
33416c5657d0SVasundhara Volam 			   int status, u32 duration, u32 instance)
33426c5657d0SVasundhara Volam {
33436c5657d0SVasundhara Volam 	memset(seg_hdr, 0, sizeof(*seg_hdr));
33448605212aSVasundhara Volam 	memcpy(seg_hdr->signature, "sEgM", 4);
33456c5657d0SVasundhara Volam 	if (seg_rec) {
33466c5657d0SVasundhara Volam 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
33476c5657d0SVasundhara Volam 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
33486c5657d0SVasundhara Volam 		seg_hdr->low_version = seg_rec->version_low;
33496c5657d0SVasundhara Volam 		seg_hdr->high_version = seg_rec->version_hi;
33506c5657d0SVasundhara Volam 	} else {
33516c5657d0SVasundhara Volam 		/* For hwrm_ver_get response Component id = 2
33526c5657d0SVasundhara Volam 		 * and Segment id = 0
33536c5657d0SVasundhara Volam 		 */
33546c5657d0SVasundhara Volam 		seg_hdr->component_id = cpu_to_le32(2);
33556c5657d0SVasundhara Volam 		seg_hdr->segment_id = 0;
33566c5657d0SVasundhara Volam 	}
33576c5657d0SVasundhara Volam 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
33586c5657d0SVasundhara Volam 	seg_hdr->length = cpu_to_le32(seg_len);
33596c5657d0SVasundhara Volam 	seg_hdr->status = cpu_to_le32(status);
33606c5657d0SVasundhara Volam 	seg_hdr->duration = cpu_to_le32(duration);
33616c5657d0SVasundhara Volam 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
33626c5657d0SVasundhara Volam 	seg_hdr->instance = cpu_to_le32(instance);
33636c5657d0SVasundhara Volam }
33646c5657d0SVasundhara Volam 
33656c5657d0SVasundhara Volam static void
33666c5657d0SVasundhara Volam bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
33676c5657d0SVasundhara Volam 			  time64_t start, s16 start_utc, u16 total_segs,
33686c5657d0SVasundhara Volam 			  int status)
33696c5657d0SVasundhara Volam {
33706c5657d0SVasundhara Volam 	time64_t end = ktime_get_real_seconds();
33716c5657d0SVasundhara Volam 	u32 os_ver_major = 0, os_ver_minor = 0;
33726c5657d0SVasundhara Volam 	struct tm tm;
33736c5657d0SVasundhara Volam 
33746c5657d0SVasundhara Volam 	time64_to_tm(start, 0, &tm);
33756c5657d0SVasundhara Volam 	memset(record, 0, sizeof(*record));
33768605212aSVasundhara Volam 	memcpy(record->signature, "cOrE", 4);
33776c5657d0SVasundhara Volam 	record->flags = 0;
33786c5657d0SVasundhara Volam 	record->low_version = 0;
33796c5657d0SVasundhara Volam 	record->high_version = 1;
33806c5657d0SVasundhara Volam 	record->asic_state = 0;
33813d46eee5SArnd Bergmann 	strlcpy(record->system_name, utsname()->nodename,
33823d46eee5SArnd Bergmann 		sizeof(record->system_name));
33838dc5ae2dSVasundhara Volam 	record->year = cpu_to_le16(tm.tm_year + 1900);
33848dc5ae2dSVasundhara Volam 	record->month = cpu_to_le16(tm.tm_mon + 1);
33856c5657d0SVasundhara Volam 	record->day = cpu_to_le16(tm.tm_mday);
33866c5657d0SVasundhara Volam 	record->hour = cpu_to_le16(tm.tm_hour);
33876c5657d0SVasundhara Volam 	record->minute = cpu_to_le16(tm.tm_min);
33886c5657d0SVasundhara Volam 	record->second = cpu_to_le16(tm.tm_sec);
33896c5657d0SVasundhara Volam 	record->utc_bias = cpu_to_le16(start_utc);
33906c5657d0SVasundhara Volam 	strcpy(record->commandline, "ethtool -w");
33916c5657d0SVasundhara Volam 	record->total_segments = cpu_to_le32(total_segs);
33926c5657d0SVasundhara Volam 
33936c5657d0SVasundhara Volam 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
33946c5657d0SVasundhara Volam 	record->os_ver_major = cpu_to_le32(os_ver_major);
33956c5657d0SVasundhara Volam 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
33966c5657d0SVasundhara Volam 
33978605212aSVasundhara Volam 	strlcpy(record->os_name, utsname()->sysname, 32);
33986c5657d0SVasundhara Volam 	time64_to_tm(end, 0, &tm);
33996c5657d0SVasundhara Volam 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
34006c5657d0SVasundhara Volam 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
34016c5657d0SVasundhara Volam 	record->end_day = cpu_to_le16(tm.tm_mday);
34026c5657d0SVasundhara Volam 	record->end_hour = cpu_to_le16(tm.tm_hour);
34036c5657d0SVasundhara Volam 	record->end_minute = cpu_to_le16(tm.tm_min);
34046c5657d0SVasundhara Volam 	record->end_second = cpu_to_le16(tm.tm_sec);
34056c5657d0SVasundhara Volam 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
34066c5657d0SVasundhara Volam 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
34076c5657d0SVasundhara Volam 				       bp->ver_resp.chip_rev << 8 |
34086c5657d0SVasundhara Volam 				       bp->ver_resp.chip_metal);
34096c5657d0SVasundhara Volam 	record->asic_id2 = 0;
34106c5657d0SVasundhara Volam 	record->coredump_status = cpu_to_le32(status);
34116c5657d0SVasundhara Volam 	record->ioctl_low_version = 0;
34126c5657d0SVasundhara Volam 	record->ioctl_high_version = 0;
34136c5657d0SVasundhara Volam }
34146c5657d0SVasundhara Volam 
34156c5657d0SVasundhara Volam static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
34166c5657d0SVasundhara Volam {
34176c5657d0SVasundhara Volam 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
3418c74751f4SVasundhara Volam 	u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
34196c5657d0SVasundhara Volam 	struct coredump_segment_record *seg_record = NULL;
34206c5657d0SVasundhara Volam 	struct bnxt_coredump_segment_hdr seg_hdr;
34216c5657d0SVasundhara Volam 	struct bnxt_coredump coredump = {NULL};
34226c5657d0SVasundhara Volam 	time64_t start_time;
34236c5657d0SVasundhara Volam 	u16 start_utc;
34246c5657d0SVasundhara Volam 	int rc = 0, i;
34256c5657d0SVasundhara Volam 
3426c74751f4SVasundhara Volam 	if (buf)
3427c74751f4SVasundhara Volam 		buf_len = *dump_len;
3428c74751f4SVasundhara Volam 
34296c5657d0SVasundhara Volam 	start_time = ktime_get_real_seconds();
34306c5657d0SVasundhara Volam 	start_utc = sys_tz.tz_minuteswest * 60;
34316c5657d0SVasundhara Volam 	seg_hdr_len = sizeof(seg_hdr);
34326c5657d0SVasundhara Volam 
34336c5657d0SVasundhara Volam 	/* First segment should be hwrm_ver_get response */
34346c5657d0SVasundhara Volam 	*dump_len = seg_hdr_len + ver_get_resp_len;
34356c5657d0SVasundhara Volam 	if (buf) {
34366c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
34376c5657d0SVasundhara Volam 					   0, 0, 0);
34386c5657d0SVasundhara Volam 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
34396c5657d0SVasundhara Volam 		offset += seg_hdr_len;
34406c5657d0SVasundhara Volam 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
34416c5657d0SVasundhara Volam 		offset += ver_get_resp_len;
34426c5657d0SVasundhara Volam 	}
34436c5657d0SVasundhara Volam 
34446c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
34456c5657d0SVasundhara Volam 	if (rc) {
34466c5657d0SVasundhara Volam 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
34476c5657d0SVasundhara Volam 		goto err;
34486c5657d0SVasundhara Volam 	}
34496c5657d0SVasundhara Volam 
34506c5657d0SVasundhara Volam 	*dump_len += seg_hdr_len * coredump.total_segs;
34516c5657d0SVasundhara Volam 
34526c5657d0SVasundhara Volam 	seg_record = (struct coredump_segment_record *)coredump.data;
34536c5657d0SVasundhara Volam 	seg_record_len = sizeof(*seg_record);
34546c5657d0SVasundhara Volam 
34556c5657d0SVasundhara Volam 	for (i = 0; i < coredump.total_segs; i++) {
34566c5657d0SVasundhara Volam 		u16 comp_id = le16_to_cpu(seg_record->component_id);
34576c5657d0SVasundhara Volam 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
34586c5657d0SVasundhara Volam 		u32 duration = 0, seg_len = 0;
34596c5657d0SVasundhara Volam 		unsigned long start, end;
34606c5657d0SVasundhara Volam 
3461c74751f4SVasundhara Volam 		if (buf && ((offset + seg_hdr_len) >
3462c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(buf_len))) {
3463c74751f4SVasundhara Volam 			rc = -ENOBUFS;
3464c74751f4SVasundhara Volam 			goto err;
3465c74751f4SVasundhara Volam 		}
3466c74751f4SVasundhara Volam 
34676c5657d0SVasundhara Volam 		start = jiffies;
34686c5657d0SVasundhara Volam 
34696c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
34706c5657d0SVasundhara Volam 		if (rc) {
34716c5657d0SVasundhara Volam 			netdev_err(bp->dev,
34726c5657d0SVasundhara Volam 				   "Failed to initiate coredump for seg = %d\n",
34736c5657d0SVasundhara Volam 				   seg_record->segment_id);
34746c5657d0SVasundhara Volam 			goto next_seg;
34756c5657d0SVasundhara Volam 		}
34766c5657d0SVasundhara Volam 
34776c5657d0SVasundhara Volam 		/* Write segment data into the buffer */
34786c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
3479c74751f4SVasundhara Volam 						     &seg_len, buf, buf_len,
34806c5657d0SVasundhara Volam 						     offset + seg_hdr_len);
3481c74751f4SVasundhara Volam 		if (rc && rc == -ENOBUFS)
3482c74751f4SVasundhara Volam 			goto err;
3483c74751f4SVasundhara Volam 		else if (rc)
34846c5657d0SVasundhara Volam 			netdev_err(bp->dev,
34856c5657d0SVasundhara Volam 				   "Failed to retrieve coredump for seg = %d\n",
34866c5657d0SVasundhara Volam 				   seg_record->segment_id);
34876c5657d0SVasundhara Volam 
34886c5657d0SVasundhara Volam next_seg:
34896c5657d0SVasundhara Volam 		end = jiffies;
34906c5657d0SVasundhara Volam 		duration = jiffies_to_msecs(end - start);
34916c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
34926c5657d0SVasundhara Volam 					   rc, duration, 0);
34936c5657d0SVasundhara Volam 
34946c5657d0SVasundhara Volam 		if (buf) {
34956c5657d0SVasundhara Volam 			/* Write segment header into the buffer */
34966c5657d0SVasundhara Volam 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
34976c5657d0SVasundhara Volam 			offset += seg_hdr_len + seg_len;
34986c5657d0SVasundhara Volam 		}
34996c5657d0SVasundhara Volam 
35006c5657d0SVasundhara Volam 		*dump_len += seg_len;
35016c5657d0SVasundhara Volam 		seg_record =
35026c5657d0SVasundhara Volam 			(struct coredump_segment_record *)((u8 *)seg_record +
35036c5657d0SVasundhara Volam 							   seg_record_len);
35046c5657d0SVasundhara Volam 	}
35056c5657d0SVasundhara Volam 
35066c5657d0SVasundhara Volam err:
35071bbf3aedSArnd Bergmann 	if (buf)
35081bbf3aedSArnd Bergmann 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
35096c5657d0SVasundhara Volam 					  start_utc, coredump.total_segs + 1,
35106c5657d0SVasundhara Volam 					  rc);
35116c5657d0SVasundhara Volam 	kfree(coredump.data);
35121bbf3aedSArnd Bergmann 	*dump_len += sizeof(struct bnxt_coredump_record);
3513c74751f4SVasundhara Volam 	if (rc == -ENOBUFS)
35149a005c38SJonathan Lemon 		netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
35156c5657d0SVasundhara Volam 	return rc;
35166c5657d0SVasundhara Volam }
35176c5657d0SVasundhara Volam 
35180b0eacf3SVasundhara Volam static int bnxt_set_dump(struct net_device *dev, struct ethtool_dump *dump)
35190b0eacf3SVasundhara Volam {
35200b0eacf3SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35210b0eacf3SVasundhara Volam 
35220b0eacf3SVasundhara Volam 	if (dump->flag > BNXT_DUMP_CRASH) {
35230b0eacf3SVasundhara Volam 		netdev_info(dev, "Supports only Live(0) and Crash(1) dumps.\n");
35240b0eacf3SVasundhara Volam 		return -EINVAL;
35250b0eacf3SVasundhara Volam 	}
35260b0eacf3SVasundhara Volam 
35270b0eacf3SVasundhara Volam 	if (!IS_ENABLED(CONFIG_TEE_BNXT_FW) && dump->flag == BNXT_DUMP_CRASH) {
35280b0eacf3SVasundhara Volam 		netdev_info(dev, "Cannot collect crash dump as TEE_BNXT_FW config option is not enabled.\n");
35290b0eacf3SVasundhara Volam 		return -EOPNOTSUPP;
35300b0eacf3SVasundhara Volam 	}
35310b0eacf3SVasundhara Volam 
35320b0eacf3SVasundhara Volam 	bp->dump_flag = dump->flag;
35330b0eacf3SVasundhara Volam 	return 0;
35340b0eacf3SVasundhara Volam }
35350b0eacf3SVasundhara Volam 
35366c5657d0SVasundhara Volam static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
35376c5657d0SVasundhara Volam {
35386c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35396c5657d0SVasundhara Volam 
35406c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
35416c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
35426c5657d0SVasundhara Volam 
35436c5657d0SVasundhara Volam 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
35446c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_min_8b << 16 |
35456c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
35466c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_rsvd_8b;
35476c5657d0SVasundhara Volam 
35480b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
35490b0eacf3SVasundhara Volam 	if (bp->dump_flag == BNXT_DUMP_CRASH)
35500b0eacf3SVasundhara Volam 		dump->len = BNXT_CRASH_DUMP_LEN;
35510b0eacf3SVasundhara Volam 	else
35520b0eacf3SVasundhara Volam 		bnxt_get_coredump(bp, NULL, &dump->len);
35530b0eacf3SVasundhara Volam 	return 0;
35546c5657d0SVasundhara Volam }
35556c5657d0SVasundhara Volam 
35566c5657d0SVasundhara Volam static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
35576c5657d0SVasundhara Volam 			      void *buf)
35586c5657d0SVasundhara Volam {
35596c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35606c5657d0SVasundhara Volam 
35616c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
35626c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
35636c5657d0SVasundhara Volam 
35646c5657d0SVasundhara Volam 	memset(buf, 0, dump->len);
35656c5657d0SVasundhara Volam 
35660b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
35670b0eacf3SVasundhara Volam 	if (dump->flag == BNXT_DUMP_CRASH) {
35680b0eacf3SVasundhara Volam #ifdef CONFIG_TEE_BNXT_FW
35690b0eacf3SVasundhara Volam 		return tee_bnxt_copy_coredump(buf, 0, dump->len);
35700b0eacf3SVasundhara Volam #endif
35710b0eacf3SVasundhara Volam 	} else {
35726c5657d0SVasundhara Volam 		return bnxt_get_coredump(bp, buf, &dump->len);
35736c5657d0SVasundhara Volam 	}
35746c5657d0SVasundhara Volam 
35750b0eacf3SVasundhara Volam 	return 0;
35760b0eacf3SVasundhara Volam }
35770b0eacf3SVasundhara Volam 
3578eb513658SMichael Chan void bnxt_ethtool_init(struct bnxt *bp)
3579eb513658SMichael Chan {
3580eb513658SMichael Chan 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3581eb513658SMichael Chan 	struct hwrm_selftest_qlist_input req = {0};
3582eb513658SMichael Chan 	struct bnxt_test_info *test_info;
3583431aa1ebSMichael Chan 	struct net_device *dev = bp->dev;
3584eb513658SMichael Chan 	int i, rc;
3585eb513658SMichael Chan 
3586691aa620SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PKG_VER))
3587a60faa60SVasundhara Volam 		bnxt_get_pkgver(dev);
3588431aa1ebSMichael Chan 
3589ba642ab7SMichael Chan 	bp->num_tests = 0;
3590eb513658SMichael Chan 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp))
3591eb513658SMichael Chan 		return;
3592eb513658SMichael Chan 
3593eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3594eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3595eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3596eb513658SMichael Chan 	if (rc)
3597eb513658SMichael Chan 		goto ethtool_init_exit;
3598eb513658SMichael Chan 
3599ba642ab7SMichael Chan 	test_info = bp->test_info;
3600ba642ab7SMichael Chan 	if (!test_info)
3601eb513658SMichael Chan 		test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
3602eb513658SMichael Chan 	if (!test_info)
3603eb513658SMichael Chan 		goto ethtool_init_exit;
3604eb513658SMichael Chan 
3605eb513658SMichael Chan 	bp->test_info = test_info;
3606eb513658SMichael Chan 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
3607eb513658SMichael Chan 	if (bp->num_tests > BNXT_MAX_TEST)
3608eb513658SMichael Chan 		bp->num_tests = BNXT_MAX_TEST;
3609eb513658SMichael Chan 
3610eb513658SMichael Chan 	test_info->offline_mask = resp->offline_tests;
3611eb513658SMichael Chan 	test_info->timeout = le16_to_cpu(resp->test_timeout);
3612eb513658SMichael Chan 	if (!test_info->timeout)
3613eb513658SMichael Chan 		test_info->timeout = HWRM_CMD_TIMEOUT;
3614eb513658SMichael Chan 	for (i = 0; i < bp->num_tests; i++) {
3615eb513658SMichael Chan 		char *str = test_info->string[i];
3616eb513658SMichael Chan 		char *fw_str = resp->test0_name + i * 32;
3617eb513658SMichael Chan 
3618f7dc1ea6SMichael Chan 		if (i == BNXT_MACLPBK_TEST_IDX) {
3619f7dc1ea6SMichael Chan 			strcpy(str, "Mac loopback test (offline)");
362091725d89SMichael Chan 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
362191725d89SMichael Chan 			strcpy(str, "Phy loopback test (offline)");
362255fd0cf3SMichael Chan 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
362355fd0cf3SMichael Chan 			strcpy(str, "Ext loopback test (offline)");
362467fea463SMichael Chan 		} else if (i == BNXT_IRQ_TEST_IDX) {
362567fea463SMichael Chan 			strcpy(str, "Interrupt_test (offline)");
3626f7dc1ea6SMichael Chan 		} else {
3627eb513658SMichael Chan 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
3628eb513658SMichael Chan 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
3629eb513658SMichael Chan 			if (test_info->offline_mask & (1 << i))
3630eb513658SMichael Chan 				strncat(str, " (offline)",
3631eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3632eb513658SMichael Chan 			else
3633eb513658SMichael Chan 				strncat(str, " (online)",
3634eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3635eb513658SMichael Chan 		}
3636f7dc1ea6SMichael Chan 	}
3637eb513658SMichael Chan 
3638eb513658SMichael Chan ethtool_init_exit:
3639eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3640eb513658SMichael Chan }
3641eb513658SMichael Chan 
3642eb513658SMichael Chan void bnxt_ethtool_free(struct bnxt *bp)
3643eb513658SMichael Chan {
3644eb513658SMichael Chan 	kfree(bp->test_info);
3645eb513658SMichael Chan 	bp->test_info = NULL;
3646eb513658SMichael Chan }
3647eb513658SMichael Chan 
3648c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
3649f704d243SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3650f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES |
3651f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USECS_IRQ |
3652f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
3653f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_STATS_BLOCK_USECS |
3654f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
365500c04a92SMichael Chan 	.get_link_ksettings	= bnxt_get_link_ksettings,
365600c04a92SMichael Chan 	.set_link_ksettings	= bnxt_set_link_ksettings,
3657c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
3658c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
3659c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
3660b5d600b0SVasundhara Volam 	.get_regs_len		= bnxt_get_regs_len,
3661b5d600b0SVasundhara Volam 	.get_regs		= bnxt_get_regs,
36628e202366SMichael Chan 	.get_wol		= bnxt_get_wol,
36635282db6cSMichael Chan 	.set_wol		= bnxt_set_wol,
3664c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
3665c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
3666c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
3667c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
3668c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
3669c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
3670c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
3671c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
3672c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
3673c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
3674c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
3675c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
3676a011952aSMichael Chan 	.set_rxnfc		= bnxt_set_rxnfc,
3677c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
3678c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
3679c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
3680bd3191b5SMichael Chan 	.set_rxfh		= bnxt_set_rxfh,
3681c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
3682c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
3683c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
3684c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
3685c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
368672b34f04SMichael Chan 	.get_eee		= bnxt_get_eee,
368772b34f04SMichael Chan 	.set_eee		= bnxt_set_eee,
368842ee18feSAjit Khaparde 	.get_module_info	= bnxt_get_module_info,
368942ee18feSAjit Khaparde 	.get_module_eeprom	= bnxt_get_module_eeprom,
36905ad2cbeeSMichael Chan 	.nway_reset		= bnxt_nway_reset,
36915ad2cbeeSMichael Chan 	.set_phys_id		= bnxt_set_phys_id,
3692eb513658SMichael Chan 	.self_test		= bnxt_self_test,
369349f7972fSVasundhara Volam 	.reset			= bnxt_reset,
36940b0eacf3SVasundhara Volam 	.set_dump		= bnxt_set_dump,
36956c5657d0SVasundhara Volam 	.get_dump_flag		= bnxt_get_dump_flag,
36966c5657d0SVasundhara Volam 	.get_dump_data		= bnxt_get_dump_data,
3697c0c050c5SMichael Chan };
3698