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",
145ee79566eSMichael Chan 	"rx_drops",
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",
155ee79566eSMichael Chan 	"tx_discards",
156ee79566eSMichael Chan 	"tx_drops",
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 
29655e4398dSVasundhara Volam #define BNXT_PCIE_STATS_ENTRY(counter)	\
29755e4398dSVasundhara Volam 	{ BNXT_PCIE_STATS_OFFSET(counter), __stringify(counter) }
29855e4398dSVasundhara Volam 
29920c1d28eSVasundhara Volam enum {
30020c1d28eSVasundhara Volam 	RX_TOTAL_DISCARDS,
30120c1d28eSVasundhara Volam 	TX_TOTAL_DISCARDS,
30220c1d28eSVasundhara Volam };
30320c1d28eSVasundhara Volam 
30420c1d28eSVasundhara Volam static struct {
30520c1d28eSVasundhara Volam 	u64			counter;
30620c1d28eSVasundhara Volam 	char			string[ETH_GSTRING_LEN];
30720c1d28eSVasundhara Volam } bnxt_sw_func_stats[] = {
30820c1d28eSVasundhara Volam 	{0, "rx_total_discard_pkts"},
30920c1d28eSVasundhara Volam 	{0, "tx_total_discard_pkts"},
31020c1d28eSVasundhara Volam };
31120c1d28eSVasundhara Volam 
3123316d509SMichael Chan #define NUM_RING_RX_SW_STATS		ARRAY_SIZE(bnxt_rx_sw_stats_str)
3133316d509SMichael Chan #define NUM_RING_CMN_SW_STATS		ARRAY_SIZE(bnxt_cmn_sw_stats_str)
3143316d509SMichael Chan #define NUM_RING_RX_HW_STATS		ARRAY_SIZE(bnxt_ring_rx_stats_str)
3153316d509SMichael Chan #define NUM_RING_TX_HW_STATS		ARRAY_SIZE(bnxt_ring_tx_stats_str)
3163316d509SMichael Chan 
3178ddc9aaaSMichael Chan static const struct {
3188ddc9aaaSMichael Chan 	long offset;
3198ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
3208ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
3218ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
3228ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
3238ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
3248ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
3258ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
3266fc92c33SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
3278ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
3288ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
3298ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
3308ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
3318ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
3328ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
3338ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
3348ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
3358ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
3368ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
3378ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
3388ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
3398ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
3408ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
3418ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
3428ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
3438ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
3448ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
3458ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
3468ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
347c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
348c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
349c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
350c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
351c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
352c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
353c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
354c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
3558ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
3568ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
3578ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
3588ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
3598ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
3608ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
361699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
362699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_err),
3638ddc9aaaSMichael Chan 
3648ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
3658ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
3668ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
3678ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
3688ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
3696fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
3708ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
3716fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
3728ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
3738ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
3748ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
3758ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
3768ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
3778ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
3788ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
3798ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
3808ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
3818ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
3828ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
3838ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
3848ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
3858ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
386c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
387c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
388c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
389c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
390c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
391c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
392c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
393c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
3948ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
3958ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
3968ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
3978ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
398699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
399699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
400699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_error),
4018ddc9aaaSMichael Chan };
4028ddc9aaaSMichael Chan 
40300db3cbaSVasundhara Volam static const struct {
40400db3cbaSVasundhara Volam 	long offset;
40500db3cbaSVasundhara Volam 	char string[ETH_GSTRING_LEN];
40600db3cbaSVasundhara Volam } bnxt_port_stats_ext_arr[] = {
40700db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
40800db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
40900db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
41000db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
41100db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
41236e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRIES,
41336e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRIES,
4144a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bits),
4154a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_buffer_passed_threshold),
4164a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_pcs_symbol_err),
4174a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_corrected_bits),
4182792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES,
41936e53349SMichael Chan };
42036e53349SMichael Chan 
42136e53349SMichael Chan static const struct {
42236e53349SMichael Chan 	long offset;
42336e53349SMichael Chan 	char string[ETH_GSTRING_LEN];
42436e53349SMichael Chan } bnxt_tx_port_stats_ext_arr[] = {
42536e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRIES,
42636e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRIES,
42700db3cbaSVasundhara Volam };
42800db3cbaSVasundhara Volam 
429e37fed79SMichael Chan static const struct {
430e37fed79SMichael Chan 	long base_off;
431e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
432e37fed79SMichael Chan } bnxt_rx_bytes_pri_arr[] = {
433e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_bytes),
434e37fed79SMichael Chan };
435e37fed79SMichael Chan 
436e37fed79SMichael Chan static const struct {
437e37fed79SMichael Chan 	long base_off;
438e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
439e37fed79SMichael Chan } bnxt_rx_pkts_pri_arr[] = {
440e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_packets),
441e37fed79SMichael Chan };
442e37fed79SMichael Chan 
443e37fed79SMichael Chan static const struct {
444e37fed79SMichael Chan 	long base_off;
445e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
446e37fed79SMichael Chan } bnxt_tx_bytes_pri_arr[] = {
447e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_bytes),
448e37fed79SMichael Chan };
449e37fed79SMichael Chan 
450e37fed79SMichael Chan static const struct {
451e37fed79SMichael Chan 	long base_off;
452e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
453e37fed79SMichael Chan } bnxt_tx_pkts_pri_arr[] = {
454e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_packets),
455e37fed79SMichael Chan };
456e37fed79SMichael Chan 
45755e4398dSVasundhara Volam static const struct {
45855e4398dSVasundhara Volam 	long offset;
45955e4398dSVasundhara Volam 	char string[ETH_GSTRING_LEN];
46055e4398dSVasundhara Volam } bnxt_pcie_stats_arr[] = {
46155e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_pl_signal_integrity),
46255e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_dl_signal_integrity),
46355e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_tl_signal_integrity),
46455e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_link_integrity),
46555e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_tx_traffic_rate),
46655e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_rx_traffic_rate),
46755e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_tx_dllp_statistics),
46855e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_rx_dllp_statistics),
46955e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_equalization_time),
47055e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_ltssm_histogram[0]),
47155e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_ltssm_histogram[2]),
47255e4398dSVasundhara Volam 	BNXT_PCIE_STATS_ENTRY(pcie_recovery_histogram),
47355e4398dSVasundhara Volam };
47455e4398dSVasundhara Volam 
47520c1d28eSVasundhara Volam #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
4768ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
477e37fed79SMichael Chan #define BNXT_NUM_STATS_PRI			\
478e37fed79SMichael Chan 	(ARRAY_SIZE(bnxt_rx_bytes_pri_arr) +	\
479e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_rx_pkts_pri_arr) +	\
480e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_bytes_pri_arr) +	\
481e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_pkts_pri_arr))
48255e4398dSVasundhara Volam #define BNXT_NUM_PCIE_STATS ARRAY_SIZE(bnxt_pcie_stats_arr)
4838ddc9aaaSMichael Chan 
48478e7b866SMichael Chan static int bnxt_get_num_tpa_ring_stats(struct bnxt *bp)
48578e7b866SMichael Chan {
48678e7b866SMichael Chan 	if (BNXT_SUPPORTS_TPA(bp)) {
48778e7b866SMichael Chan 		if (bp->max_tpa_v2)
48878e7b866SMichael Chan 			return ARRAY_SIZE(bnxt_ring_tpa2_stats_str);
48978e7b866SMichael Chan 		return ARRAY_SIZE(bnxt_ring_tpa_stats_str);
49078e7b866SMichael Chan 	}
49178e7b866SMichael Chan 	return 0;
49278e7b866SMichael Chan }
49378e7b866SMichael Chan 
494ee79566eSMichael Chan static int bnxt_get_num_ring_stats(struct bnxt *bp)
495ee79566eSMichael Chan {
4963316d509SMichael Chan 	int rx, tx, cmn;
497125592fbSRajesh Ravi 	bool sh = false;
498125592fbSRajesh Ravi 
499125592fbSRajesh Ravi 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
500125592fbSRajesh Ravi 		sh = true;
501ee79566eSMichael Chan 
5023316d509SMichael Chan 	rx = NUM_RING_RX_HW_STATS + NUM_RING_RX_SW_STATS +
50378e7b866SMichael Chan 	     bnxt_get_num_tpa_ring_stats(bp);
5043316d509SMichael Chan 	tx = NUM_RING_TX_HW_STATS;
5053316d509SMichael Chan 	cmn = NUM_RING_CMN_SW_STATS;
506125592fbSRajesh Ravi 	if (sh)
5073316d509SMichael Chan 		return (rx + tx + cmn) * bp->cp_nr_rings;
508125592fbSRajesh Ravi 	else
509125592fbSRajesh Ravi 		return rx * bp->rx_nr_rings + tx * bp->tx_nr_rings +
510125592fbSRajesh Ravi 		       cmn * bp->cp_nr_rings;
511ee79566eSMichael Chan }
512ee79566eSMichael Chan 
5135c8227d0SMichael Chan static int bnxt_get_num_stats(struct bnxt *bp)
514c0c050c5SMichael Chan {
515ee79566eSMichael Chan 	int num_stats = bnxt_get_num_ring_stats(bp);
5168ddc9aaaSMichael Chan 
51720c1d28eSVasundhara Volam 	num_stats += BNXT_NUM_SW_FUNC_STATS;
51820c1d28eSVasundhara Volam 
5198ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS)
5208ddc9aaaSMichael Chan 		num_stats += BNXT_NUM_PORT_STATS;
5218ddc9aaaSMichael Chan 
522e37fed79SMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
52336e53349SMichael Chan 		num_stats += bp->fw_rx_stats_ext_size +
52436e53349SMichael Chan 			     bp->fw_tx_stats_ext_size;
525e37fed79SMichael Chan 		if (bp->pri2cos_valid)
526e37fed79SMichael Chan 			num_stats += BNXT_NUM_STATS_PRI;
527e37fed79SMichael Chan 	}
52800db3cbaSVasundhara Volam 
52955e4398dSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PCIE_STATS)
53055e4398dSVasundhara Volam 		num_stats += BNXT_NUM_PCIE_STATS;
53155e4398dSVasundhara Volam 
5328ddc9aaaSMichael Chan 	return num_stats;
5338ddc9aaaSMichael Chan }
5345c8227d0SMichael Chan 
5355c8227d0SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
5365c8227d0SMichael Chan {
5375c8227d0SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5385c8227d0SMichael Chan 
5395c8227d0SMichael Chan 	switch (sset) {
5405c8227d0SMichael Chan 	case ETH_SS_STATS:
5415c8227d0SMichael Chan 		return bnxt_get_num_stats(bp);
542eb513658SMichael Chan 	case ETH_SS_TEST:
543eb513658SMichael Chan 		if (!bp->num_tests)
544eb513658SMichael Chan 			return -EOPNOTSUPP;
545eb513658SMichael Chan 		return bp->num_tests;
546c0c050c5SMichael Chan 	default:
547c0c050c5SMichael Chan 		return -EOPNOTSUPP;
548c0c050c5SMichael Chan 	}
549c0c050c5SMichael Chan }
550c0c050c5SMichael Chan 
551125592fbSRajesh Ravi static bool is_rx_ring(struct bnxt *bp, int ring_num)
552125592fbSRajesh Ravi {
553125592fbSRajesh Ravi 	return ring_num < bp->rx_nr_rings;
554125592fbSRajesh Ravi }
555125592fbSRajesh Ravi 
556125592fbSRajesh Ravi static bool is_tx_ring(struct bnxt *bp, int ring_num)
557125592fbSRajesh Ravi {
558125592fbSRajesh Ravi 	int tx_base = 0;
559125592fbSRajesh Ravi 
560125592fbSRajesh Ravi 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
561125592fbSRajesh Ravi 		tx_base = bp->rx_nr_rings;
562125592fbSRajesh Ravi 
563125592fbSRajesh Ravi 	if (ring_num >= tx_base && ring_num < (tx_base + bp->tx_nr_rings))
564125592fbSRajesh Ravi 		return true;
565125592fbSRajesh Ravi 	return false;
566125592fbSRajesh Ravi }
567125592fbSRajesh Ravi 
568c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
569c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
570c0c050c5SMichael Chan {
571c0c050c5SMichael Chan 	u32 i, j = 0;
572c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
573125592fbSRajesh Ravi 	u32 tpa_stats;
574c0c050c5SMichael Chan 
575fd3ab1c7SMichael Chan 	if (!bp->bnapi) {
576ee79566eSMichael Chan 		j += bnxt_get_num_ring_stats(bp) + BNXT_NUM_SW_FUNC_STATS;
577fd3ab1c7SMichael Chan 		goto skip_ring_stats;
578fd3ab1c7SMichael Chan 	}
579c0c050c5SMichael Chan 
58020c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
58120c1d28eSVasundhara Volam 		bnxt_sw_func_stats[i].counter = 0;
58220c1d28eSVasundhara Volam 
583125592fbSRajesh Ravi 	tpa_stats = bnxt_get_num_tpa_ring_stats(bp);
584c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
585c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
586c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
587c0c050c5SMichael Chan 		__le64 *hw_stats = (__le64 *)cpr->hw_stats;
5889d8b5f05SMichael Chan 		u64 *sw;
589c0c050c5SMichael Chan 		int k;
590c0c050c5SMichael Chan 
591125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
592125592fbSRajesh Ravi 			for (k = 0; k < NUM_RING_RX_HW_STATS; j++, k++)
593125592fbSRajesh Ravi 				buf[j] = le64_to_cpu(hw_stats[k]);
594125592fbSRajesh Ravi 		}
595125592fbSRajesh Ravi 		if (is_tx_ring(bp, i)) {
596125592fbSRajesh Ravi 			k = NUM_RING_RX_HW_STATS;
597125592fbSRajesh Ravi 			for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
598125592fbSRajesh Ravi 			       j++, k++)
599125592fbSRajesh Ravi 				buf[j] = le64_to_cpu(hw_stats[k]);
600125592fbSRajesh Ravi 		}
601125592fbSRajesh Ravi 		if (!tpa_stats || !is_rx_ring(bp, i))
602125592fbSRajesh Ravi 			goto skip_tpa_ring_stats;
603125592fbSRajesh Ravi 
604125592fbSRajesh Ravi 		k = NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
605125592fbSRajesh Ravi 		for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS +
606125592fbSRajesh Ravi 			   tpa_stats; j++, k++)
607c0c050c5SMichael Chan 			buf[j] = le64_to_cpu(hw_stats[k]);
6089d8b5f05SMichael Chan 
609125592fbSRajesh Ravi skip_tpa_ring_stats:
6109d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.rx;
611125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
6123316d509SMichael Chan 			for (k = 0; k < NUM_RING_RX_SW_STATS; j++, k++)
6139d8b5f05SMichael Chan 				buf[j] = sw[k];
614125592fbSRajesh Ravi 		}
6159d8b5f05SMichael Chan 
6169d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.cmn;
6173316d509SMichael Chan 		for (k = 0; k < NUM_RING_CMN_SW_STATS; j++, k++)
6189d8b5f05SMichael Chan 			buf[j] = sw[k];
61920c1d28eSVasundhara Volam 
62020c1d28eSVasundhara Volam 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
62120c1d28eSVasundhara Volam 			le64_to_cpu(cpr->hw_stats->rx_discard_pkts);
62220c1d28eSVasundhara Volam 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
62320c1d28eSVasundhara Volam 			le64_to_cpu(cpr->hw_stats->tx_discard_pkts);
624c0c050c5SMichael Chan 	}
62520c1d28eSVasundhara Volam 
62620c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
62720c1d28eSVasundhara Volam 		buf[j] = bnxt_sw_func_stats[i].counter;
62820c1d28eSVasundhara Volam 
629fd3ab1c7SMichael Chan skip_ring_stats:
6308ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
6318ddc9aaaSMichael Chan 		__le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
6328ddc9aaaSMichael Chan 
6338ddc9aaaSMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
6348ddc9aaaSMichael Chan 			buf[j] = le64_to_cpu(*(port_stats +
6358ddc9aaaSMichael Chan 					       bnxt_port_stats_arr[i].offset));
6368ddc9aaaSMichael Chan 		}
6378ddc9aaaSMichael Chan 	}
63800db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
63936e53349SMichael Chan 		__le64 *rx_port_stats_ext = (__le64 *)bp->hw_rx_port_stats_ext;
64036e53349SMichael Chan 		__le64 *tx_port_stats_ext = (__le64 *)bp->hw_tx_port_stats_ext;
64100db3cbaSVasundhara Volam 
64236e53349SMichael Chan 		for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
64336e53349SMichael Chan 			buf[j] = le64_to_cpu(*(rx_port_stats_ext +
64400db3cbaSVasundhara Volam 					    bnxt_port_stats_ext_arr[i].offset));
64500db3cbaSVasundhara Volam 		}
64636e53349SMichael Chan 		for (i = 0; i < bp->fw_tx_stats_ext_size; i++, j++) {
64736e53349SMichael Chan 			buf[j] = le64_to_cpu(*(tx_port_stats_ext +
64836e53349SMichael Chan 					bnxt_tx_port_stats_ext_arr[i].offset));
64936e53349SMichael Chan 		}
650e37fed79SMichael Chan 		if (bp->pri2cos_valid) {
651e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
652e37fed79SMichael Chan 				long n = bnxt_rx_bytes_pri_arr[i].base_off +
653a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
654e37fed79SMichael Chan 
655e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(rx_port_stats_ext + n));
656e37fed79SMichael Chan 			}
657e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
658e37fed79SMichael Chan 				long n = bnxt_rx_pkts_pri_arr[i].base_off +
659a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
660e37fed79SMichael Chan 
661e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(rx_port_stats_ext + n));
662e37fed79SMichael Chan 			}
663e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
664e37fed79SMichael Chan 				long n = bnxt_tx_bytes_pri_arr[i].base_off +
665a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
666e37fed79SMichael Chan 
667e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(tx_port_stats_ext + n));
668e37fed79SMichael Chan 			}
669e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
670e37fed79SMichael Chan 				long n = bnxt_tx_pkts_pri_arr[i].base_off +
671a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
672e37fed79SMichael Chan 
673e37fed79SMichael Chan 				buf[j] = le64_to_cpu(*(tx_port_stats_ext + n));
674e37fed79SMichael Chan 			}
675e37fed79SMichael Chan 		}
67600db3cbaSVasundhara Volam 	}
67755e4398dSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PCIE_STATS) {
67855e4398dSVasundhara Volam 		__le64 *pcie_stats = (__le64 *)bp->hw_pcie_stats;
67955e4398dSVasundhara Volam 
68055e4398dSVasundhara Volam 		for (i = 0; i < BNXT_NUM_PCIE_STATS; i++, j++) {
68155e4398dSVasundhara Volam 			buf[j] = le64_to_cpu(*(pcie_stats +
68255e4398dSVasundhara Volam 					       bnxt_pcie_stats_arr[i].offset));
68355e4398dSVasundhara Volam 		}
68455e4398dSVasundhara Volam 	}
685c0c050c5SMichael Chan }
686c0c050c5SMichael Chan 
687c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
688c0c050c5SMichael Chan {
689c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
69078e7b866SMichael Chan 	static const char * const *str;
691ee79566eSMichael Chan 	u32 i, j, num_str;
692c0c050c5SMichael Chan 
693c0c050c5SMichael Chan 	switch (stringset) {
694c0c050c5SMichael Chan 	case ETH_SS_STATS:
695c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
696125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6973316d509SMichael Chan 				num_str = NUM_RING_RX_HW_STATS;
698ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
699ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
7003316d509SMichael Chan 						bnxt_ring_rx_stats_str[j]);
701c0c050c5SMichael Chan 					buf += ETH_GSTRING_LEN;
702ee79566eSMichael Chan 				}
703125592fbSRajesh Ravi 			}
704125592fbSRajesh Ravi 			if (is_tx_ring(bp, i)) {
7053316d509SMichael Chan 				num_str = NUM_RING_TX_HW_STATS;
7063316d509SMichael Chan 				for (j = 0; j < num_str; j++) {
7073316d509SMichael Chan 					sprintf(buf, "[%d]: %s", i,
7083316d509SMichael Chan 						bnxt_ring_tx_stats_str[j]);
7093316d509SMichael Chan 					buf += ETH_GSTRING_LEN;
7103316d509SMichael Chan 				}
711125592fbSRajesh Ravi 			}
7123316d509SMichael Chan 			num_str = bnxt_get_num_tpa_ring_stats(bp);
713125592fbSRajesh Ravi 			if (!num_str || !is_rx_ring(bp, i))
714ee79566eSMichael Chan 				goto skip_tpa_stats;
715ee79566eSMichael Chan 
7163316d509SMichael Chan 			if (bp->max_tpa_v2)
71778e7b866SMichael Chan 				str = bnxt_ring_tpa2_stats_str;
7183316d509SMichael Chan 			else
71978e7b866SMichael Chan 				str = bnxt_ring_tpa_stats_str;
7203316d509SMichael Chan 
721ee79566eSMichael Chan 			for (j = 0; j < num_str; j++) {
72278e7b866SMichael Chan 				sprintf(buf, "[%d]: %s", i, str[j]);
723c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
724ee79566eSMichael Chan 			}
725ee79566eSMichael Chan skip_tpa_stats:
726125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
7273316d509SMichael Chan 				num_str = NUM_RING_RX_SW_STATS;
728ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
729ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
7309d8b5f05SMichael Chan 						bnxt_rx_sw_stats_str[j]);
7319d8b5f05SMichael Chan 					buf += ETH_GSTRING_LEN;
7329d8b5f05SMichael Chan 				}
733125592fbSRajesh Ravi 			}
7343316d509SMichael Chan 			num_str = NUM_RING_CMN_SW_STATS;
7359d8b5f05SMichael Chan 			for (j = 0; j < num_str; j++) {
7369d8b5f05SMichael Chan 				sprintf(buf, "[%d]: %s", i,
7379d8b5f05SMichael Chan 					bnxt_cmn_sw_stats_str[j]);
738c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
739ee79566eSMichael Chan 			}
740c0c050c5SMichael Chan 		}
74120c1d28eSVasundhara Volam 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
74220c1d28eSVasundhara Volam 			strcpy(buf, bnxt_sw_func_stats[i].string);
74320c1d28eSVasundhara Volam 			buf += ETH_GSTRING_LEN;
74420c1d28eSVasundhara Volam 		}
74520c1d28eSVasundhara Volam 
7468ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
7478ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
7488ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
7498ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
7508ddc9aaaSMichael Chan 			}
7518ddc9aaaSMichael Chan 		}
75200db3cbaSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
75336e53349SMichael Chan 			for (i = 0; i < bp->fw_rx_stats_ext_size; i++) {
75400db3cbaSVasundhara Volam 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
75500db3cbaSVasundhara Volam 				buf += ETH_GSTRING_LEN;
75600db3cbaSVasundhara Volam 			}
75736e53349SMichael Chan 			for (i = 0; i < bp->fw_tx_stats_ext_size; i++) {
75836e53349SMichael Chan 				strcpy(buf,
75936e53349SMichael Chan 				       bnxt_tx_port_stats_ext_arr[i].string);
76036e53349SMichael Chan 				buf += ETH_GSTRING_LEN;
76136e53349SMichael Chan 			}
762e37fed79SMichael Chan 			if (bp->pri2cos_valid) {
763e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
764e37fed79SMichael Chan 					strcpy(buf,
765e37fed79SMichael Chan 					       bnxt_rx_bytes_pri_arr[i].string);
766e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
767e37fed79SMichael Chan 				}
768e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
769e37fed79SMichael Chan 					strcpy(buf,
770e37fed79SMichael Chan 					       bnxt_rx_pkts_pri_arr[i].string);
771e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
772e37fed79SMichael Chan 				}
773e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
774e37fed79SMichael Chan 					strcpy(buf,
775e37fed79SMichael Chan 					       bnxt_tx_bytes_pri_arr[i].string);
776e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
777e37fed79SMichael Chan 				}
778e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
779e37fed79SMichael Chan 					strcpy(buf,
780e37fed79SMichael Chan 					       bnxt_tx_pkts_pri_arr[i].string);
781e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
782e37fed79SMichael Chan 				}
783e37fed79SMichael Chan 			}
78400db3cbaSVasundhara Volam 		}
78555e4398dSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PCIE_STATS) {
78655e4398dSVasundhara Volam 			for (i = 0; i < BNXT_NUM_PCIE_STATS; i++) {
78755e4398dSVasundhara Volam 				strcpy(buf, bnxt_pcie_stats_arr[i].string);
78855e4398dSVasundhara Volam 				buf += ETH_GSTRING_LEN;
78955e4398dSVasundhara Volam 			}
79055e4398dSVasundhara Volam 		}
791c0c050c5SMichael Chan 		break;
792eb513658SMichael Chan 	case ETH_SS_TEST:
793eb513658SMichael Chan 		if (bp->num_tests)
794eb513658SMichael Chan 			memcpy(buf, bp->test_info->string,
795eb513658SMichael Chan 			       bp->num_tests * ETH_GSTRING_LEN);
796eb513658SMichael Chan 		break;
797c0c050c5SMichael Chan 	default:
798c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
799c0c050c5SMichael Chan 			   stringset);
800c0c050c5SMichael Chan 		break;
801c0c050c5SMichael Chan 	}
802c0c050c5SMichael Chan }
803c0c050c5SMichael Chan 
804c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
805c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
806c0c050c5SMichael Chan {
807c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
808c0c050c5SMichael Chan 
809c0c050c5SMichael Chan 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
810c0c050c5SMichael Chan 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
811c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
812c0c050c5SMichael Chan 
813c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
814c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
815c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
816c0c050c5SMichael Chan }
817c0c050c5SMichael Chan 
818c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
819c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
820c0c050c5SMichael Chan {
821c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
822c0c050c5SMichael Chan 
823c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
824c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
825c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
826c0c050c5SMichael Chan 		return -EINVAL;
827c0c050c5SMichael Chan 
828c0c050c5SMichael Chan 	if (netif_running(dev))
829c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
830c0c050c5SMichael Chan 
831c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
832c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
833c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
834c0c050c5SMichael Chan 
835c0c050c5SMichael Chan 	if (netif_running(dev))
836c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
837c0c050c5SMichael Chan 
838c0c050c5SMichael Chan 	return 0;
839c0c050c5SMichael Chan }
840c0c050c5SMichael Chan 
841c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
842c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
843c0c050c5SMichael Chan {
844c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
845db4723b3SMichael Chan 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
846c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
847db4723b3SMichael Chan 	int max_tx_sch_inputs;
848db4723b3SMichael Chan 
849db4723b3SMichael Chan 	/* Get the most up-to-date max_tx_sch_inputs. */
850f1ca94deSMichael Chan 	if (BNXT_NEW_RM(bp))
851db4723b3SMichael Chan 		bnxt_hwrm_func_resc_qcaps(bp, false);
852db4723b3SMichael Chan 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
853c0c050c5SMichael Chan 
8546e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
855db4723b3SMichael Chan 	if (max_tx_sch_inputs)
856db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
857a79a5276SMichael Chan 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
858068c9ec6SMichael Chan 
85918d6e4e2SSatish Baddipadige 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
86018d6e4e2SSatish Baddipadige 		max_rx_rings = 0;
86118d6e4e2SSatish Baddipadige 		max_tx_rings = 0;
86218d6e4e2SSatish Baddipadige 	}
863db4723b3SMichael Chan 	if (max_tx_sch_inputs)
864db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
86518d6e4e2SSatish Baddipadige 
866c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
867c0c050c5SMichael Chan 	if (tcs > 1)
868c0c050c5SMichael Chan 		max_tx_rings /= tcs;
869c0c050c5SMichael Chan 
870c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
871c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
872c0c050c5SMichael Chan 	channel->max_other = 0;
873068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
874068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
87576595193SPrashant Sreedharan 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
87676595193SPrashant Sreedharan 			channel->combined_count--;
877068c9ec6SMichael Chan 	} else {
87876595193SPrashant Sreedharan 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
879c0c050c5SMichael Chan 			channel->rx_count = bp->rx_nr_rings;
880c0c050c5SMichael Chan 			channel->tx_count = bp->tx_nr_rings_per_tc;
881c0c050c5SMichael Chan 		}
882068c9ec6SMichael Chan 	}
88376595193SPrashant Sreedharan }
884c0c050c5SMichael Chan 
885c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
886c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
887c0c050c5SMichael Chan {
888c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
889d1e7925eSMichael Chan 	int req_tx_rings, req_rx_rings, tcs;
890068c9ec6SMichael Chan 	bool sh = false;
8915f449249SMichael Chan 	int tx_xdp = 0;
892d1e7925eSMichael Chan 	int rc = 0;
893c0c050c5SMichael Chan 
894068c9ec6SMichael Chan 	if (channel->other_count)
895c0c050c5SMichael Chan 		return -EINVAL;
896c0c050c5SMichael Chan 
897068c9ec6SMichael Chan 	if (!channel->combined_count &&
898068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
899068c9ec6SMichael Chan 		return -EINVAL;
900068c9ec6SMichael Chan 
901068c9ec6SMichael Chan 	if (channel->combined_count &&
902068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
903068c9ec6SMichael Chan 		return -EINVAL;
904068c9ec6SMichael Chan 
90576595193SPrashant Sreedharan 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
90676595193SPrashant Sreedharan 					    channel->tx_count))
90776595193SPrashant Sreedharan 		return -EINVAL;
90876595193SPrashant Sreedharan 
909068c9ec6SMichael Chan 	if (channel->combined_count)
910068c9ec6SMichael Chan 		sh = true;
911068c9ec6SMichael Chan 
912c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
913c0c050c5SMichael Chan 
914391be5c2SMichael Chan 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
915d1e7925eSMichael Chan 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
9165f449249SMichael Chan 	if (bp->tx_nr_rings_xdp) {
9175f449249SMichael Chan 		if (!sh) {
9185f449249SMichael Chan 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
9195f449249SMichael Chan 			return -EINVAL;
9205f449249SMichael Chan 		}
9215f449249SMichael Chan 		tx_xdp = req_rx_rings;
9225f449249SMichael Chan 	}
92398fdbe73SMichael Chan 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
924d1e7925eSMichael Chan 	if (rc) {
925d1e7925eSMichael Chan 		netdev_warn(dev, "Unable to allocate the requested rings\n");
926d1e7925eSMichael Chan 		return rc;
927391be5c2SMichael Chan 	}
928391be5c2SMichael Chan 
929c0c050c5SMichael Chan 	if (netif_running(dev)) {
930c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
931c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
932c0c050c5SMichael Chan 			 * before PF unload
933c0c050c5SMichael Chan 			 */
934c0c050c5SMichael Chan 		}
935c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
936c0c050c5SMichael Chan 		if (rc) {
937c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
938c0c050c5SMichael Chan 				   rc);
939c0c050c5SMichael Chan 			return rc;
940c0c050c5SMichael Chan 		}
941c0c050c5SMichael Chan 	}
942c0c050c5SMichael Chan 
943068c9ec6SMichael Chan 	if (sh) {
944068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
945d1e7925eSMichael Chan 		bp->rx_nr_rings = channel->combined_count;
946d1e7925eSMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
947068c9ec6SMichael Chan 	} else {
948068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
949c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
950c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
951068c9ec6SMichael Chan 	}
9525f449249SMichael Chan 	bp->tx_nr_rings_xdp = tx_xdp;
9535f449249SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
954c0c050c5SMichael Chan 	if (tcs > 1)
9555f449249SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
956068c9ec6SMichael Chan 
957068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
958068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
959068c9ec6SMichael Chan 
9602bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
9612bcfa6f6SMichael Chan 	netdev_update_features(dev);
962c0c050c5SMichael Chan 	if (netif_running(dev)) {
963c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
964c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
965c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
966c0c050c5SMichael Chan 			 * to renable
967c0c050c5SMichael Chan 			 */
968c0c050c5SMichael Chan 		}
969d8c09f19SMichael Chan 	} else {
9701b3f0b75SMichael Chan 		rc = bnxt_reserve_rings(bp, true);
971c0c050c5SMichael Chan 	}
972c0c050c5SMichael Chan 
973c0c050c5SMichael Chan 	return rc;
974c0c050c5SMichael Chan }
975c0c050c5SMichael Chan 
976c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
977c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
978c0c050c5SMichael Chan 			    u32 *rule_locs)
979c0c050c5SMichael Chan {
980c0c050c5SMichael Chan 	int i, j = 0;
981c0c050c5SMichael Chan 
982c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
983c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
984c0c050c5SMichael Chan 		struct hlist_head *head;
985c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
986c0c050c5SMichael Chan 
987c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
988c0c050c5SMichael Chan 		rcu_read_lock();
989c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
990c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
991c0c050c5SMichael Chan 				break;
992c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
993c0c050c5SMichael Chan 		}
994c0c050c5SMichael Chan 		rcu_read_unlock();
995c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
996c0c050c5SMichael Chan 			break;
997c0c050c5SMichael Chan 	}
998c0c050c5SMichael Chan 	cmd->rule_cnt = j;
999c0c050c5SMichael Chan 	return 0;
1000c0c050c5SMichael Chan }
1001c0c050c5SMichael Chan 
1002c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1003c0c050c5SMichael Chan {
1004c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
1005c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1006c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
1007c0c050c5SMichael Chan 	struct flow_keys *fkeys;
1008c0c050c5SMichael Chan 	int i, rc = -EINVAL;
1009c0c050c5SMichael Chan 
1010b721cfafSstephen hemminger 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
1011c0c050c5SMichael Chan 		return rc;
1012c0c050c5SMichael Chan 
1013c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
1014c0c050c5SMichael Chan 		struct hlist_head *head;
1015c0c050c5SMichael Chan 
1016c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
1017c0c050c5SMichael Chan 		rcu_read_lock();
1018c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
1019c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
1020c0c050c5SMichael Chan 				goto fltr_found;
1021c0c050c5SMichael Chan 		}
1022c0c050c5SMichael Chan 		rcu_read_unlock();
1023c0c050c5SMichael Chan 	}
1024c0c050c5SMichael Chan 	return rc;
1025c0c050c5SMichael Chan 
1026c0c050c5SMichael Chan fltr_found:
1027c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
1028dda0e746SMichael Chan 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
1029c0c050c5SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1030c0c050c5SMichael Chan 			fs->flow_type = TCP_V4_FLOW;
1031c0c050c5SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1032c0c050c5SMichael Chan 			fs->flow_type = UDP_V4_FLOW;
1033c0c050c5SMichael Chan 		else
1034c0c050c5SMichael Chan 			goto fltr_err;
1035c0c050c5SMichael Chan 
1036c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
1037c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
1038c0c050c5SMichael Chan 
1039c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
1040c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
1041c0c050c5SMichael Chan 
1042c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
1043c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
1044c0c050c5SMichael Chan 
1045c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
1046c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
1047dda0e746SMichael Chan 	} else {
1048dda0e746SMichael Chan 		int i;
1049dda0e746SMichael Chan 
1050dda0e746SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1051dda0e746SMichael Chan 			fs->flow_type = TCP_V6_FLOW;
1052dda0e746SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1053dda0e746SMichael Chan 			fs->flow_type = UDP_V6_FLOW;
1054dda0e746SMichael Chan 		else
1055dda0e746SMichael Chan 			goto fltr_err;
1056dda0e746SMichael Chan 
1057dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
1058dda0e746SMichael Chan 			fkeys->addrs.v6addrs.src;
1059dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
1060dda0e746SMichael Chan 			fkeys->addrs.v6addrs.dst;
1061dda0e746SMichael Chan 		for (i = 0; i < 4; i++) {
1062dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
1063dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
1064dda0e746SMichael Chan 		}
1065dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
1066dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
1067dda0e746SMichael Chan 
1068dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
1069dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
1070dda0e746SMichael Chan 	}
1071c0c050c5SMichael Chan 
1072c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
1073c0c050c5SMichael Chan 	rc = 0;
1074c0c050c5SMichael Chan 
1075c0c050c5SMichael Chan fltr_err:
1076c0c050c5SMichael Chan 	rcu_read_unlock();
1077c0c050c5SMichael Chan 
1078c0c050c5SMichael Chan 	return rc;
1079c0c050c5SMichael Chan }
1080a011952aSMichael Chan #endif
1081a011952aSMichael Chan 
1082a011952aSMichael Chan static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
1083a011952aSMichael Chan {
1084a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
1085a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1086a011952aSMichael Chan 	return 0;
1087a011952aSMichael Chan }
1088a011952aSMichael Chan 
1089a011952aSMichael Chan static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
1090a011952aSMichael Chan {
1091a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
1092a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1093a011952aSMichael Chan 	return 0;
1094a011952aSMichael Chan }
1095a011952aSMichael Chan 
1096a011952aSMichael Chan static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1097a011952aSMichael Chan {
1098a011952aSMichael Chan 	cmd->data = 0;
1099a011952aSMichael Chan 	switch (cmd->flow_type) {
1100a011952aSMichael Chan 	case TCP_V4_FLOW:
1101a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
1102a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1103a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1104a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1105a011952aSMichael Chan 		break;
1106a011952aSMichael Chan 	case UDP_V4_FLOW:
1107a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
1108a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1109a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1110a011952aSMichael Chan 		/* fall through */
1111a011952aSMichael Chan 	case SCTP_V4_FLOW:
1112a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1113a011952aSMichael Chan 	case AH_V4_FLOW:
1114a011952aSMichael Chan 	case ESP_V4_FLOW:
1115a011952aSMichael Chan 	case IPV4_FLOW:
1116a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1117a011952aSMichael Chan 		break;
1118a011952aSMichael Chan 
1119a011952aSMichael Chan 	case TCP_V6_FLOW:
1120a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
1121a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1122a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1123a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1124a011952aSMichael Chan 		break;
1125a011952aSMichael Chan 	case UDP_V6_FLOW:
1126a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
1127a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1128a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1129a011952aSMichael Chan 		/* fall through */
1130a011952aSMichael Chan 	case SCTP_V6_FLOW:
1131a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1132a011952aSMichael Chan 	case AH_V6_FLOW:
1133a011952aSMichael Chan 	case ESP_V6_FLOW:
1134a011952aSMichael Chan 	case IPV6_FLOW:
1135a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1136a011952aSMichael Chan 		break;
1137a011952aSMichael Chan 	}
1138a011952aSMichael Chan 	return 0;
1139a011952aSMichael Chan }
1140a011952aSMichael Chan 
1141a011952aSMichael Chan #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1142a011952aSMichael Chan #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
1143a011952aSMichael Chan 
1144a011952aSMichael Chan static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1145a011952aSMichael Chan {
1146a011952aSMichael Chan 	u32 rss_hash_cfg = bp->rss_hash_cfg;
1147a011952aSMichael Chan 	int tuple, rc = 0;
1148a011952aSMichael Chan 
1149a011952aSMichael Chan 	if (cmd->data == RXH_4TUPLE)
1150a011952aSMichael Chan 		tuple = 4;
1151a011952aSMichael Chan 	else if (cmd->data == RXH_2TUPLE)
1152a011952aSMichael Chan 		tuple = 2;
1153a011952aSMichael Chan 	else if (!cmd->data)
1154a011952aSMichael Chan 		tuple = 0;
1155a011952aSMichael Chan 	else
1156a011952aSMichael Chan 		return -EINVAL;
1157a011952aSMichael Chan 
1158a011952aSMichael Chan 	if (cmd->flow_type == TCP_V4_FLOW) {
1159a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1160a011952aSMichael Chan 		if (tuple == 4)
1161a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1162a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V4_FLOW) {
1163a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1164a011952aSMichael Chan 			return -EINVAL;
1165a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1166a011952aSMichael Chan 		if (tuple == 4)
1167a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1168a011952aSMichael Chan 	} else if (cmd->flow_type == TCP_V6_FLOW) {
1169a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1170a011952aSMichael Chan 		if (tuple == 4)
1171a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1172a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V6_FLOW) {
1173a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1174a011952aSMichael Chan 			return -EINVAL;
1175a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1176a011952aSMichael Chan 		if (tuple == 4)
1177a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1178a011952aSMichael Chan 	} else if (tuple == 4) {
1179a011952aSMichael Chan 		return -EINVAL;
1180a011952aSMichael Chan 	}
1181a011952aSMichael Chan 
1182a011952aSMichael Chan 	switch (cmd->flow_type) {
1183a011952aSMichael Chan 	case TCP_V4_FLOW:
1184a011952aSMichael Chan 	case UDP_V4_FLOW:
1185a011952aSMichael Chan 	case SCTP_V4_FLOW:
1186a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1187a011952aSMichael Chan 	case AH_V4_FLOW:
1188a011952aSMichael Chan 	case ESP_V4_FLOW:
1189a011952aSMichael Chan 	case IPV4_FLOW:
1190a011952aSMichael Chan 		if (tuple == 2)
1191a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1192a011952aSMichael Chan 		else if (!tuple)
1193a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1194a011952aSMichael Chan 		break;
1195a011952aSMichael Chan 
1196a011952aSMichael Chan 	case TCP_V6_FLOW:
1197a011952aSMichael Chan 	case UDP_V6_FLOW:
1198a011952aSMichael Chan 	case SCTP_V6_FLOW:
1199a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1200a011952aSMichael Chan 	case AH_V6_FLOW:
1201a011952aSMichael Chan 	case ESP_V6_FLOW:
1202a011952aSMichael Chan 	case IPV6_FLOW:
1203a011952aSMichael Chan 		if (tuple == 2)
1204a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1205a011952aSMichael Chan 		else if (!tuple)
1206a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1207a011952aSMichael Chan 		break;
1208a011952aSMichael Chan 	}
1209a011952aSMichael Chan 
1210a011952aSMichael Chan 	if (bp->rss_hash_cfg == rss_hash_cfg)
1211a011952aSMichael Chan 		return 0;
1212a011952aSMichael Chan 
1213a011952aSMichael Chan 	bp->rss_hash_cfg = rss_hash_cfg;
1214a011952aSMichael Chan 	if (netif_running(bp->dev)) {
1215a011952aSMichael Chan 		bnxt_close_nic(bp, false, false);
1216a011952aSMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1217a011952aSMichael Chan 	}
1218a011952aSMichael Chan 	return rc;
1219a011952aSMichael Chan }
1220c0c050c5SMichael Chan 
1221c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1222c0c050c5SMichael Chan 			  u32 *rule_locs)
1223c0c050c5SMichael Chan {
1224c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1225c0c050c5SMichael Chan 	int rc = 0;
1226c0c050c5SMichael Chan 
1227c0c050c5SMichael Chan 	switch (cmd->cmd) {
1228a011952aSMichael Chan #ifdef CONFIG_RFS_ACCEL
1229c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
1230c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
1231c0c050c5SMichael Chan 		break;
1232c0c050c5SMichael Chan 
1233c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
1234c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
1235c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
1236c0c050c5SMichael Chan 		break;
1237c0c050c5SMichael Chan 
1238c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
1239c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
1240c0c050c5SMichael Chan 		break;
1241c0c050c5SMichael Chan 
1242c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
1243c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
1244c0c050c5SMichael Chan 		break;
1245a011952aSMichael Chan #endif
1246a011952aSMichael Chan 
1247a011952aSMichael Chan 	case ETHTOOL_GRXFH:
1248a011952aSMichael Chan 		rc = bnxt_grxfh(bp, cmd);
1249a011952aSMichael Chan 		break;
1250c0c050c5SMichael Chan 
1251c0c050c5SMichael Chan 	default:
1252c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
1253c0c050c5SMichael Chan 		break;
1254c0c050c5SMichael Chan 	}
1255c0c050c5SMichael Chan 
1256c0c050c5SMichael Chan 	return rc;
1257c0c050c5SMichael Chan }
1258a011952aSMichael Chan 
1259a011952aSMichael Chan static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1260a011952aSMichael Chan {
1261a011952aSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1262a011952aSMichael Chan 	int rc;
1263a011952aSMichael Chan 
1264a011952aSMichael Chan 	switch (cmd->cmd) {
1265a011952aSMichael Chan 	case ETHTOOL_SRXFH:
1266a011952aSMichael Chan 		rc = bnxt_srxfh(bp, cmd);
1267a011952aSMichael Chan 		break;
1268a011952aSMichael Chan 
1269a011952aSMichael Chan 	default:
1270a011952aSMichael Chan 		rc = -EOPNOTSUPP;
1271a011952aSMichael Chan 		break;
1272a011952aSMichael Chan 	}
1273a011952aSMichael Chan 	return rc;
1274a011952aSMichael Chan }
1275c0c050c5SMichael Chan 
1276c0c050c5SMichael Chan static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
1277c0c050c5SMichael Chan {
1278c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
1279c0c050c5SMichael Chan }
1280c0c050c5SMichael Chan 
1281c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
1282c0c050c5SMichael Chan {
1283c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
1284c0c050c5SMichael Chan }
1285c0c050c5SMichael Chan 
1286c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1287c0c050c5SMichael Chan 			 u8 *hfunc)
1288c0c050c5SMichael Chan {
1289c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
12907991cb9cSMichael Chan 	struct bnxt_vnic_info *vnic;
1291c0c050c5SMichael Chan 	int i = 0;
1292c0c050c5SMichael Chan 
1293c0c050c5SMichael Chan 	if (hfunc)
1294c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
1295c0c050c5SMichael Chan 
12967991cb9cSMichael Chan 	if (!bp->vnic_info)
12977991cb9cSMichael Chan 		return 0;
12987991cb9cSMichael Chan 
12997991cb9cSMichael Chan 	vnic = &bp->vnic_info[0];
13007991cb9cSMichael Chan 	if (indir && vnic->rss_table) {
1301c0c050c5SMichael Chan 		for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
1302c0c050c5SMichael Chan 			indir[i] = le16_to_cpu(vnic->rss_table[i]);
13037991cb9cSMichael Chan 	}
1304c0c050c5SMichael Chan 
13057991cb9cSMichael Chan 	if (key && vnic->rss_hash_key)
1306c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
1307c0c050c5SMichael Chan 
1308c0c050c5SMichael Chan 	return 0;
1309c0c050c5SMichael Chan }
1310c0c050c5SMichael Chan 
1311c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
1312c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
1313c0c050c5SMichael Chan {
1314c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1315c0c050c5SMichael Chan 
1316c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1317431aa1ebSMichael Chan 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
1318c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
13195c8227d0SMichael Chan 	info->n_stats = bnxt_get_num_stats(bp);
1320eb513658SMichael Chan 	info->testinfo_len = bp->num_tests;
1321c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
1322c0c050c5SMichael Chan 	info->eedump_len = 0;
1323c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
1324c0c050c5SMichael Chan 	info->regdump_len = 0;
1325c0c050c5SMichael Chan }
1326c0c050c5SMichael Chan 
13278e202366SMichael Chan static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13288e202366SMichael Chan {
13298e202366SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
13308e202366SMichael Chan 
13318e202366SMichael Chan 	wol->supported = 0;
13328e202366SMichael Chan 	wol->wolopts = 0;
13338e202366SMichael Chan 	memset(&wol->sopass, 0, sizeof(wol->sopass));
13348e202366SMichael Chan 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
13358e202366SMichael Chan 		wol->supported = WAKE_MAGIC;
13368e202366SMichael Chan 		if (bp->wol)
13378e202366SMichael Chan 			wol->wolopts = WAKE_MAGIC;
13388e202366SMichael Chan 	}
13398e202366SMichael Chan }
13408e202366SMichael Chan 
13415282db6cSMichael Chan static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13425282db6cSMichael Chan {
13435282db6cSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
13445282db6cSMichael Chan 
13455282db6cSMichael Chan 	if (wol->wolopts & ~WAKE_MAGIC)
13465282db6cSMichael Chan 		return -EINVAL;
13475282db6cSMichael Chan 
13485282db6cSMichael Chan 	if (wol->wolopts & WAKE_MAGIC) {
13495282db6cSMichael Chan 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
13505282db6cSMichael Chan 			return -EINVAL;
13515282db6cSMichael Chan 		if (!bp->wol) {
13525282db6cSMichael Chan 			if (bnxt_hwrm_alloc_wol_fltr(bp))
13535282db6cSMichael Chan 				return -EBUSY;
13545282db6cSMichael Chan 			bp->wol = 1;
13555282db6cSMichael Chan 		}
13565282db6cSMichael Chan 	} else {
13575282db6cSMichael Chan 		if (bp->wol) {
13585282db6cSMichael Chan 			if (bnxt_hwrm_free_wol_fltr(bp))
13595282db6cSMichael Chan 				return -EBUSY;
13605282db6cSMichael Chan 			bp->wol = 0;
13615282db6cSMichael Chan 		}
13625282db6cSMichael Chan 	}
13635282db6cSMichael Chan 	return 0;
13645282db6cSMichael Chan }
13655282db6cSMichael Chan 
1366170ce013SMichael Chan u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
1367c0c050c5SMichael Chan {
1368c0c050c5SMichael Chan 	u32 speed_mask = 0;
1369c0c050c5SMichael Chan 
1370c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
1371c0c050c5SMichael Chan 	/* set the advertised speeds */
1372c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1373c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
1374c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1375c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
1376c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1377c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
1378c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1379c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
1380c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
13811c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
138227c4d578SMichael Chan 
138327c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
138427c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
138527c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
138627c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
138727c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
138827c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
138927c4d578SMichael Chan 
1390c0c050c5SMichael Chan 	return speed_mask;
1391c0c050c5SMichael Chan }
1392c0c050c5SMichael Chan 
139300c04a92SMichael Chan #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
139400c04a92SMichael Chan {									\
139500c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
139600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
139700c04a92SMichael Chan 						     100baseT_Full);	\
139800c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
139900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
140000c04a92SMichael Chan 						     1000baseT_Full);	\
140100c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
140200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
140300c04a92SMichael Chan 						     10000baseT_Full);	\
140400c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
140500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
140600c04a92SMichael Chan 						     25000baseCR_Full);	\
140700c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
140800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
140900c04a92SMichael Chan 						     40000baseCR4_Full);\
141000c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
141100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
141200c04a92SMichael Chan 						     50000baseCR2_Full);\
141338a21b34SDeepak Khungar 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
141438a21b34SDeepak Khungar 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
141538a21b34SDeepak Khungar 						     100000baseCR4_Full);\
141600c04a92SMichael Chan 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
141700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
141800c04a92SMichael Chan 						     Pause);		\
141900c04a92SMichael Chan 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
142000c04a92SMichael Chan 			ethtool_link_ksettings_add_link_mode(		\
142100c04a92SMichael Chan 					lk_ksettings, name, Asym_Pause);\
142200c04a92SMichael Chan 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
142300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
142400c04a92SMichael Chan 						     Asym_Pause);	\
142500c04a92SMichael Chan 	}								\
142600c04a92SMichael Chan }
142700c04a92SMichael Chan 
142800c04a92SMichael Chan #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
142900c04a92SMichael Chan {									\
143000c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
143100c04a92SMichael Chan 						  100baseT_Full) ||	\
143200c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
143300c04a92SMichael Chan 						  100baseT_Half))	\
143400c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
143500c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
143600c04a92SMichael Chan 						  1000baseT_Full) ||	\
143700c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
143800c04a92SMichael Chan 						  1000baseT_Half))	\
143900c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
144000c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
144100c04a92SMichael Chan 						  10000baseT_Full))	\
144200c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
144300c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
144400c04a92SMichael Chan 						  25000baseCR_Full))	\
144500c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
144600c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
144700c04a92SMichael Chan 						  40000baseCR4_Full))	\
144800c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
144900c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
145000c04a92SMichael Chan 						  50000baseCR2_Full))	\
145100c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
145238a21b34SDeepak Khungar 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
145338a21b34SDeepak Khungar 						  100000baseCR4_Full))	\
145438a21b34SDeepak Khungar 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
145500c04a92SMichael Chan }
145600c04a92SMichael Chan 
145700c04a92SMichael Chan static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
145800c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
145927c4d578SMichael Chan {
146068515a18SMichael Chan 	u16 fw_speeds = link_info->advertising;
146127c4d578SMichael Chan 	u8 fw_pause = 0;
146227c4d578SMichael Chan 
146327c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
146427c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
146527c4d578SMichael Chan 
146600c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
146727c4d578SMichael Chan }
146827c4d578SMichael Chan 
146900c04a92SMichael Chan static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
147000c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
14713277360eSMichael Chan {
14723277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
14733277360eSMichael Chan 	u8 fw_pause = 0;
14743277360eSMichael Chan 
14753277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14763277360eSMichael Chan 		fw_pause = link_info->lp_pause;
14773277360eSMichael Chan 
147800c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
147900c04a92SMichael Chan 				lp_advertising);
14803277360eSMichael Chan }
14813277360eSMichael Chan 
148200c04a92SMichael Chan static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
148300c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
14844b32caccSMichael Chan {
14854b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
14864b32caccSMichael Chan 
148700c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
14884b32caccSMichael Chan 
148900c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
149000c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
149100c04a92SMichael Chan 					     Asym_Pause);
149293ed8117SMichael Chan 
149300c04a92SMichael Chan 	if (link_info->support_auto_speeds)
149400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
149500c04a92SMichael Chan 						     Autoneg);
149693ed8117SMichael Chan }
149793ed8117SMichael Chan 
1498c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1499c0c050c5SMichael Chan {
1500c0c050c5SMichael Chan 	switch (fw_link_speed) {
1501c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
1502c0c050c5SMichael Chan 		return SPEED_100;
1503c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
1504c0c050c5SMichael Chan 		return SPEED_1000;
1505c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
1506c0c050c5SMichael Chan 		return SPEED_2500;
1507c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
1508c0c050c5SMichael Chan 		return SPEED_10000;
1509c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
1510c0c050c5SMichael Chan 		return SPEED_20000;
1511c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
1512c0c050c5SMichael Chan 		return SPEED_25000;
1513c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
1514c0c050c5SMichael Chan 		return SPEED_40000;
1515c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
1516c0c050c5SMichael Chan 		return SPEED_50000;
151738a21b34SDeepak Khungar 	case BNXT_LINK_SPEED_100GB:
151838a21b34SDeepak Khungar 		return SPEED_100000;
1519c0c050c5SMichael Chan 	default:
1520c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
1521c0c050c5SMichael Chan 	}
1522c0c050c5SMichael Chan }
1523c0c050c5SMichael Chan 
152400c04a92SMichael Chan static int bnxt_get_link_ksettings(struct net_device *dev,
152500c04a92SMichael Chan 				   struct ethtool_link_ksettings *lk_ksettings)
1526c0c050c5SMichael Chan {
1527c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1528c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
152900c04a92SMichael Chan 	struct ethtool_link_settings *base = &lk_ksettings->base;
153000c04a92SMichael Chan 	u32 ethtool_speed;
1531c0c050c5SMichael Chan 
153200c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1533e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
153400c04a92SMichael Chan 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1535c0c050c5SMichael Chan 
153600c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1537b763499eSMichael Chan 	if (link_info->autoneg) {
153800c04a92SMichael Chan 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
153900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
154000c04a92SMichael Chan 						     advertising, Autoneg);
154100c04a92SMichael Chan 		base->autoneg = AUTONEG_ENABLE;
154200c04a92SMichael Chan 		base->duplex = DUPLEX_UNKNOWN;
154383d8f5e9SMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK) {
154483d8f5e9SMichael Chan 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
154583d8f5e9SMichael Chan 			if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
154600c04a92SMichael Chan 				base->duplex = DUPLEX_FULL;
154729c262feSMichael Chan 			else
154800c04a92SMichael Chan 				base->duplex = DUPLEX_HALF;
154983d8f5e9SMichael Chan 		}
155083d8f5e9SMichael Chan 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1551c0c050c5SMichael Chan 	} else {
155200c04a92SMichael Chan 		base->autoneg = AUTONEG_DISABLE;
155329c262feSMichael Chan 		ethtool_speed =
155429c262feSMichael Chan 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
155500c04a92SMichael Chan 		base->duplex = DUPLEX_HALF;
155629c262feSMichael Chan 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
155700c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
1558c0c050c5SMichael Chan 	}
155900c04a92SMichael Chan 	base->speed = ethtool_speed;
1560c0c050c5SMichael Chan 
156100c04a92SMichael Chan 	base->port = PORT_NONE;
1562c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
156300c04a92SMichael Chan 		base->port = PORT_TP;
156400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
156500c04a92SMichael Chan 						     TP);
156600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
156700c04a92SMichael Chan 						     TP);
1568c0c050c5SMichael Chan 	} else {
156900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
157000c04a92SMichael Chan 						     FIBRE);
157100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
157200c04a92SMichael Chan 						     FIBRE);
1573c0c050c5SMichael Chan 
1574c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
157500c04a92SMichael Chan 			base->port = PORT_DA;
1576c0c050c5SMichael Chan 		else if (link_info->media_type ==
1577c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
157800c04a92SMichael Chan 			base->port = PORT_FIBRE;
1579c0c050c5SMichael Chan 	}
158000c04a92SMichael Chan 	base->phy_address = link_info->phy_addr;
1581e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1582c0c050c5SMichael Chan 
1583c0c050c5SMichael Chan 	return 0;
1584c0c050c5SMichael Chan }
1585c0c050c5SMichael Chan 
158638a21b34SDeepak Khungar static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed)
1587c0c050c5SMichael Chan {
15889d9cee08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
15899d9cee08SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
15909d9cee08SMichael Chan 	u16 support_spds = link_info->support_speeds;
15919d9cee08SMichael Chan 	u32 fw_speed = 0;
15929d9cee08SMichael Chan 
1593c0c050c5SMichael Chan 	switch (ethtool_speed) {
1594c0c050c5SMichael Chan 	case SPEED_100:
15959d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
15969d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
15979d9cee08SMichael Chan 		break;
1598c0c050c5SMichael Chan 	case SPEED_1000:
15999d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
16009d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
16019d9cee08SMichael Chan 		break;
1602c0c050c5SMichael Chan 	case SPEED_2500:
16039d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
16049d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
16059d9cee08SMichael Chan 		break;
1606c0c050c5SMichael Chan 	case SPEED_10000:
16079d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
16089d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
16099d9cee08SMichael Chan 		break;
1610c0c050c5SMichael Chan 	case SPEED_20000:
16119d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
16129d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
16139d9cee08SMichael Chan 		break;
1614c0c050c5SMichael Chan 	case SPEED_25000:
16159d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
16169d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
16179d9cee08SMichael Chan 		break;
1618c0c050c5SMichael Chan 	case SPEED_40000:
16199d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
16209d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
16219d9cee08SMichael Chan 		break;
1622c0c050c5SMichael Chan 	case SPEED_50000:
16239d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
16249d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
16259d9cee08SMichael Chan 		break;
162638a21b34SDeepak Khungar 	case SPEED_100000:
162738a21b34SDeepak Khungar 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB)
162838a21b34SDeepak Khungar 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB;
162938a21b34SDeepak Khungar 		break;
1630c0c050c5SMichael Chan 	default:
1631c0c050c5SMichael Chan 		netdev_err(dev, "unsupported speed!\n");
1632c0c050c5SMichael Chan 		break;
1633c0c050c5SMichael Chan 	}
16349d9cee08SMichael Chan 	return fw_speed;
1635c0c050c5SMichael Chan }
1636c0c050c5SMichael Chan 
1637939f7f0cSMichael Chan u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1638c0c050c5SMichael Chan {
1639c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
1640c0c050c5SMichael Chan 
1641c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
1642c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
1643c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
1644c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1645c0c050c5SMichael Chan 	}
1646c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
1647c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
1648c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1649c0c050c5SMichael Chan 	}
1650c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
1651c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1652c0c050c5SMichael Chan 
16531c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
16541c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
16551c49c421SMichael Chan 
1656c0c050c5SMichael Chan 	return fw_speed_mask;
1657c0c050c5SMichael Chan }
1658c0c050c5SMichael Chan 
165900c04a92SMichael Chan static int bnxt_set_link_ksettings(struct net_device *dev,
166000c04a92SMichael Chan 			   const struct ethtool_link_ksettings *lk_ksettings)
1661c0c050c5SMichael Chan {
1662c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1663c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
166400c04a92SMichael Chan 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1665c0c050c5SMichael Chan 	bool set_pause = false;
166668515a18SMichael Chan 	u16 fw_advertising = 0;
166768515a18SMichael Chan 	u32 speed;
166800c04a92SMichael Chan 	int rc = 0;
1669c0c050c5SMichael Chan 
1670c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
167100c04a92SMichael Chan 		return -EOPNOTSUPP;
1672c0c050c5SMichael Chan 
1673e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
167400c04a92SMichael Chan 	if (base->autoneg == AUTONEG_ENABLE) {
167500c04a92SMichael Chan 		BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
167600c04a92SMichael Chan 					advertising);
1677c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1678c0c050c5SMichael Chan 		if (!fw_advertising)
167993ed8117SMichael Chan 			link_info->advertising = link_info->support_auto_speeds;
1680c0c050c5SMichael Chan 		else
1681c0c050c5SMichael Chan 			link_info->advertising = fw_advertising;
1682c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
1683c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
1684c0c050c5SMichael Chan 		 */
1685c0c050c5SMichael Chan 		set_pause = true;
1686c0c050c5SMichael Chan 	} else {
16879d9cee08SMichael Chan 		u16 fw_speed;
168803efbec0SMichael Chan 		u8 phy_type = link_info->phy_type;
16899d9cee08SMichael Chan 
169003efbec0SMichael Chan 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
169103efbec0SMichael Chan 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
169203efbec0SMichael Chan 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
169303efbec0SMichael Chan 			netdev_err(dev, "10GBase-T devices must autoneg\n");
169403efbec0SMichael Chan 			rc = -EINVAL;
169503efbec0SMichael Chan 			goto set_setting_exit;
169603efbec0SMichael Chan 		}
169700c04a92SMichael Chan 		if (base->duplex == DUPLEX_HALF) {
1698c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1699c0c050c5SMichael Chan 			rc = -EINVAL;
1700c0c050c5SMichael Chan 			goto set_setting_exit;
1701c0c050c5SMichael Chan 		}
170200c04a92SMichael Chan 		speed = base->speed;
17039d9cee08SMichael Chan 		fw_speed = bnxt_get_fw_speed(dev, speed);
17049d9cee08SMichael Chan 		if (!fw_speed) {
17059d9cee08SMichael Chan 			rc = -EINVAL;
17069d9cee08SMichael Chan 			goto set_setting_exit;
17079d9cee08SMichael Chan 		}
17089d9cee08SMichael Chan 		link_info->req_link_speed = fw_speed;
1709c0c050c5SMichael Chan 		link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1710b763499eSMichael Chan 		link_info->autoneg = 0;
1711c0c050c5SMichael Chan 		link_info->advertising = 0;
1712c0c050c5SMichael Chan 	}
1713c0c050c5SMichael Chan 
1714c0c050c5SMichael Chan 	if (netif_running(dev))
1715939f7f0cSMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1716c0c050c5SMichael Chan 
1717c0c050c5SMichael Chan set_setting_exit:
1718e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1719c0c050c5SMichael Chan 	return rc;
1720c0c050c5SMichael Chan }
1721c0c050c5SMichael Chan 
1722c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
1723c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
1724c0c050c5SMichael Chan {
1725c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1726c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1727c0c050c5SMichael Chan 
1728c0c050c5SMichael Chan 	if (BNXT_VF(bp))
1729c0c050c5SMichael Chan 		return;
1730b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
17313c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
17323c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1733c0c050c5SMichael Chan }
1734c0c050c5SMichael Chan 
1735c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
1736c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
1737c0c050c5SMichael Chan {
1738c0c050c5SMichael Chan 	int rc = 0;
1739c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1740c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1741c0c050c5SMichael Chan 
1742c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
174375362a3fSMichael Chan 		return -EOPNOTSUPP;
1744c0c050c5SMichael Chan 
1745c0c050c5SMichael Chan 	if (epause->autoneg) {
1746b763499eSMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
1747b763499eSMichael Chan 			return -EINVAL;
1748b763499eSMichael Chan 
1749c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1750c9ee9516SMichael Chan 		if (bp->hwrm_spec_code >= 0x10201)
1751c9ee9516SMichael Chan 			link_info->req_flow_ctrl =
1752c9ee9516SMichael Chan 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1753c0c050c5SMichael Chan 	} else {
1754c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
1755c0c050c5SMichael Chan 		 * force a link change
1756c0c050c5SMichael Chan 		 */
1757c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1758c0c050c5SMichael Chan 			link_info->force_link_chng = true;
1759c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1760c9ee9516SMichael Chan 		link_info->req_flow_ctrl = 0;
1761c0c050c5SMichael Chan 	}
1762c0c050c5SMichael Chan 	if (epause->rx_pause)
1763c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1764c0c050c5SMichael Chan 
1765c0c050c5SMichael Chan 	if (epause->tx_pause)
1766c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1767c0c050c5SMichael Chan 
1768163e9ef6SVasundhara Volam 	if (netif_running(dev)) {
1769163e9ef6SVasundhara Volam 		mutex_lock(&bp->link_lock);
1770c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
1771163e9ef6SVasundhara Volam 		mutex_unlock(&bp->link_lock);
1772163e9ef6SVasundhara Volam 	}
1773c0c050c5SMichael Chan 	return rc;
1774c0c050c5SMichael Chan }
1775c0c050c5SMichael Chan 
1776c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
1777c0c050c5SMichael Chan {
1778c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1779c0c050c5SMichael Chan 
1780c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
1781c0c050c5SMichael Chan 	return bp->link_info.link_up;
1782c0c050c5SMichael Chan }
1783c0c050c5SMichael Chan 
1784b3b0ddd0SMichael Chan static void bnxt_print_admin_err(struct bnxt *bp)
1785b3b0ddd0SMichael Chan {
1786b3b0ddd0SMichael Chan 	netdev_info(bp->dev, "PF does not have admin privileges to flash or reset the device\n");
1787b3b0ddd0SMichael Chan }
1788b3b0ddd0SMichael Chan 
17895ac67d8bSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
17905ac67d8bSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
17915ac67d8bSRob Swindell 				u32 *data_length);
17925ac67d8bSRob Swindell 
1793c0c050c5SMichael Chan static int bnxt_flash_nvram(struct net_device *dev,
1794c0c050c5SMichael Chan 			    u16 dir_type,
1795c0c050c5SMichael Chan 			    u16 dir_ordinal,
1796c0c050c5SMichael Chan 			    u16 dir_ext,
1797c0c050c5SMichael Chan 			    u16 dir_attr,
1798c0c050c5SMichael Chan 			    const u8 *data,
1799c0c050c5SMichael Chan 			    size_t data_len)
1800c0c050c5SMichael Chan {
1801c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1802c0c050c5SMichael Chan 	int rc;
1803c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
1804c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1805c0c050c5SMichael Chan 	u8 *kmem;
1806c0c050c5SMichael Chan 
1807c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1808c0c050c5SMichael Chan 
1809c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
1810c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
1811c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
1812c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
1813c0c050c5SMichael Chan 	req.dir_data_length = cpu_to_le32(data_len);
1814c0c050c5SMichael Chan 
1815c0c050c5SMichael Chan 	kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1816c0c050c5SMichael Chan 				  GFP_KERNEL);
1817c0c050c5SMichael Chan 	if (!kmem) {
1818c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1819c0c050c5SMichael Chan 			   (unsigned)data_len);
1820c0c050c5SMichael Chan 		return -ENOMEM;
1821c0c050c5SMichael Chan 	}
1822c0c050c5SMichael Chan 	memcpy(kmem, data, data_len);
1823c0c050c5SMichael Chan 	req.host_src_addr = cpu_to_le64(dma_handle);
1824c0c050c5SMichael Chan 
1825c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1826c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1827c0c050c5SMichael Chan 
1828d4f1420dSMichael Chan 	if (rc == -EACCES)
1829b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
1830c0c050c5SMichael Chan 	return rc;
1831c0c050c5SMichael Chan }
1832c0c050c5SMichael Chan 
183395fec034SEdwin Peer static int bnxt_hwrm_firmware_reset(struct net_device *dev, u8 proc_type,
183495fec034SEdwin Peer 				    u8 self_reset, u8 flags)
1835d2d6318cSRob Swindell {
1836d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
18377c675421SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
18387c675421SVasundhara Volam 	int rc;
1839d2d6318cSRob Swindell 
1840d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1841d2d6318cSRob Swindell 
184295fec034SEdwin Peer 	req.embedded_proc_type = proc_type;
184395fec034SEdwin Peer 	req.selfrst_status = self_reset;
184495fec034SEdwin Peer 	req.flags = flags;
184595fec034SEdwin Peer 
18468cec0940SEdwin Peer 	if (proc_type == FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP) {
18478cec0940SEdwin Peer 		rc = hwrm_send_message_silent(bp, &req, sizeof(req),
18488cec0940SEdwin Peer 					      HWRM_CMD_TIMEOUT);
18498cec0940SEdwin Peer 	} else {
185095fec034SEdwin Peer 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
185195fec034SEdwin Peer 		if (rc == -EACCES)
185295fec034SEdwin Peer 			bnxt_print_admin_err(bp);
18538cec0940SEdwin Peer 	}
185495fec034SEdwin Peer 	return rc;
185595fec034SEdwin Peer }
185695fec034SEdwin Peer 
185794f17e89SEdwin Peer static int bnxt_firmware_reset(struct net_device *dev,
185894f17e89SEdwin Peer 			       enum bnxt_nvm_directory_type dir_type)
185995fec034SEdwin Peer {
186095fec034SEdwin Peer 	u8 self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE;
186195fec034SEdwin Peer 	u8 proc_type, flags = 0;
186295fec034SEdwin Peer 
1863d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1864d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
1865d2d6318cSRob Swindell 	switch (dir_type) {
1866d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
1867d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
1868d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
186995fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1870d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
187195fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1872d2d6318cSRob Swindell 		break;
1873d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
1874d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
187595fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
187608141e0bSRob Swindell 		/* Self-reset APE upon next PCIe reset: */
187795fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1878d2d6318cSRob Swindell 		break;
1879d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
1880d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
188195fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1882d2d6318cSRob Swindell 		break;
1883d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
1884d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
188595fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1886d2d6318cSRob Swindell 		break;
1887d2d6318cSRob Swindell 	default:
1888d2d6318cSRob Swindell 		return -EINVAL;
1889d2d6318cSRob Swindell 	}
1890d2d6318cSRob Swindell 
189195fec034SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, proc_type, self_reset, flags);
1892d2d6318cSRob Swindell }
1893d2d6318cSRob Swindell 
189494f17e89SEdwin Peer static int bnxt_firmware_reset_chip(struct net_device *dev)
189594f17e89SEdwin Peer {
189694f17e89SEdwin Peer 	struct bnxt *bp = netdev_priv(dev);
189794f17e89SEdwin Peer 	u8 flags = 0;
189894f17e89SEdwin Peer 
189994f17e89SEdwin Peer 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
190094f17e89SEdwin Peer 		flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
190194f17e89SEdwin Peer 
190294f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev,
190394f17e89SEdwin Peer 					FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP,
190494f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP,
190594f17e89SEdwin Peer 					flags);
190694f17e89SEdwin Peer }
190794f17e89SEdwin Peer 
190894f17e89SEdwin Peer static int bnxt_firmware_reset_ap(struct net_device *dev)
190994f17e89SEdwin Peer {
191094f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP,
191194f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE,
191294f17e89SEdwin Peer 					0);
191394f17e89SEdwin Peer }
191494f17e89SEdwin Peer 
1915c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
1916c0c050c5SMichael Chan 			       u16 dir_type,
1917c0c050c5SMichael Chan 			       const u8 *fw_data,
1918c0c050c5SMichael Chan 			       size_t fw_size)
1919c0c050c5SMichael Chan {
1920c0c050c5SMichael Chan 	int	rc = 0;
1921c0c050c5SMichael Chan 	u16	code_type;
1922c0c050c5SMichael Chan 	u32	stored_crc;
1923c0c050c5SMichael Chan 	u32	calculated_crc;
1924c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1925c0c050c5SMichael Chan 
1926c0c050c5SMichael Chan 	switch (dir_type) {
1927c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1928c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1929c0c050c5SMichael Chan 		code_type = CODE_BOOT;
1930c0c050c5SMichael Chan 		break;
193193e0b4feSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
193293e0b4feSRob Swindell 		code_type = CODE_CHIMP_PATCH;
193393e0b4feSRob Swindell 		break;
19342731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
19352731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
19362731d70fSRob Swindell 		break;
193793e0b4feSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
193893e0b4feSRob Swindell 		code_type = CODE_APE_PATCH;
193993e0b4feSRob Swindell 		break;
194093e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
194193e0b4feSRob Swindell 		code_type = CODE_KONG_FW;
194293e0b4feSRob Swindell 		break;
194393e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
194493e0b4feSRob Swindell 		code_type = CODE_KONG_PATCH;
194593e0b4feSRob Swindell 		break;
194693e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
194793e0b4feSRob Swindell 		code_type = CODE_BONO_FW;
194893e0b4feSRob Swindell 		break;
194993e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
195093e0b4feSRob Swindell 		code_type = CODE_BONO_PATCH;
195193e0b4feSRob Swindell 		break;
1952c0c050c5SMichael Chan 	default:
1953c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
1954c0c050c5SMichael Chan 			   dir_type);
1955c0c050c5SMichael Chan 		return -EINVAL;
1956c0c050c5SMichael Chan 	}
1957c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
1958c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
1959c0c050c5SMichael Chan 			   (unsigned int)fw_size);
1960c0c050c5SMichael Chan 		return -EINVAL;
1961c0c050c5SMichael Chan 	}
1962c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1963c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
1964c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
1965c0c050c5SMichael Chan 		return -EINVAL;
1966c0c050c5SMichael Chan 	}
1967c0c050c5SMichael Chan 	if (header->code_type != code_type) {
1968c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1969c0c050c5SMichael Chan 			   code_type, header->code_type);
1970c0c050c5SMichael Chan 		return -EINVAL;
1971c0c050c5SMichael Chan 	}
1972c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
1973c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1974c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
1975c0c050c5SMichael Chan 		return -EINVAL;
1976c0c050c5SMichael Chan 	}
1977c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
1978c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1979c0c050c5SMichael Chan 					     sizeof(stored_crc)));
1980c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1981c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
1982c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1983c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
1984c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
1985c0c050c5SMichael Chan 		return -EINVAL;
1986c0c050c5SMichael Chan 	}
1987c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1988c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
1989d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
1990d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
1991d2d6318cSRob Swindell 
1992c0c050c5SMichael Chan 	return rc;
1993c0c050c5SMichael Chan }
1994c0c050c5SMichael Chan 
19955ac67d8bSRob Swindell static int bnxt_flash_microcode(struct net_device *dev,
19965ac67d8bSRob Swindell 				u16 dir_type,
19975ac67d8bSRob Swindell 				const u8 *fw_data,
19985ac67d8bSRob Swindell 				size_t fw_size)
19995ac67d8bSRob Swindell {
20005ac67d8bSRob Swindell 	struct bnxt_ucode_trailer *trailer;
20015ac67d8bSRob Swindell 	u32 calculated_crc;
20025ac67d8bSRob Swindell 	u32 stored_crc;
20035ac67d8bSRob Swindell 	int rc = 0;
20045ac67d8bSRob Swindell 
20055ac67d8bSRob Swindell 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
20065ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode file size: %u\n",
20075ac67d8bSRob Swindell 			   (unsigned int)fw_size);
20085ac67d8bSRob Swindell 		return -EINVAL;
20095ac67d8bSRob Swindell 	}
20105ac67d8bSRob Swindell 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
20115ac67d8bSRob Swindell 						sizeof(*trailer)));
20125ac67d8bSRob Swindell 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
20135ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
20145ac67d8bSRob Swindell 			   le32_to_cpu(trailer->sig));
20155ac67d8bSRob Swindell 		return -EINVAL;
20165ac67d8bSRob Swindell 	}
20175ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
20185ac67d8bSRob Swindell 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
20195ac67d8bSRob Swindell 			   dir_type, le16_to_cpu(trailer->dir_type));
20205ac67d8bSRob Swindell 		return -EINVAL;
20215ac67d8bSRob Swindell 	}
20225ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->trailer_length) <
20235ac67d8bSRob Swindell 		sizeof(struct bnxt_ucode_trailer)) {
20245ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
20255ac67d8bSRob Swindell 			   le16_to_cpu(trailer->trailer_length));
20265ac67d8bSRob Swindell 		return -EINVAL;
20275ac67d8bSRob Swindell 	}
20285ac67d8bSRob Swindell 
20295ac67d8bSRob Swindell 	/* Confirm the CRC32 checksum of the file: */
20305ac67d8bSRob Swindell 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
20315ac67d8bSRob Swindell 					     sizeof(stored_crc)));
20325ac67d8bSRob Swindell 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
20335ac67d8bSRob Swindell 	if (calculated_crc != stored_crc) {
20345ac67d8bSRob Swindell 		netdev_err(dev,
20355ac67d8bSRob Swindell 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
20365ac67d8bSRob Swindell 			   (unsigned long)stored_crc,
20375ac67d8bSRob Swindell 			   (unsigned long)calculated_crc);
20385ac67d8bSRob Swindell 		return -EINVAL;
20395ac67d8bSRob Swindell 	}
20405ac67d8bSRob Swindell 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
20415ac67d8bSRob Swindell 			      0, 0, fw_data, fw_size);
20425ac67d8bSRob Swindell 
20435ac67d8bSRob Swindell 	return rc;
20445ac67d8bSRob Swindell }
20455ac67d8bSRob Swindell 
2046c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
2047c0c050c5SMichael Chan {
2048c0c050c5SMichael Chan 	switch (dir_type) {
2049c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
2050c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2051c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2052c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
2053c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
2054c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
2055c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
205693e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
205793e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
2058c0c050c5SMichael Chan 		return true;
2059c0c050c5SMichael Chan 	}
2060c0c050c5SMichael Chan 
2061c0c050c5SMichael Chan 	return false;
2062c0c050c5SMichael Chan }
2063c0c050c5SMichael Chan 
20645ac67d8bSRob Swindell static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
2065c0c050c5SMichael Chan {
2066c0c050c5SMichael Chan 	switch (dir_type) {
2067c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
2068c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
2069c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
2070c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
2071c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
2072c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
2073c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
2074c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
2075c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
2076c0c050c5SMichael Chan 		return true;
2077c0c050c5SMichael Chan 	}
2078c0c050c5SMichael Chan 
2079c0c050c5SMichael Chan 	return false;
2080c0c050c5SMichael Chan }
2081c0c050c5SMichael Chan 
2082c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
2083c0c050c5SMichael Chan {
2084c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
20855ac67d8bSRob Swindell 		bnxt_dir_type_is_other_exec_format(dir_type);
2086c0c050c5SMichael Chan }
2087c0c050c5SMichael Chan 
2088c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
2089c0c050c5SMichael Chan 					 u16 dir_type,
2090c0c050c5SMichael Chan 					 const char *filename)
2091c0c050c5SMichael Chan {
2092c0c050c5SMichael Chan 	const struct firmware  *fw;
2093c0c050c5SMichael Chan 	int			rc;
2094c0c050c5SMichael Chan 
2095c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
2096c0c050c5SMichael Chan 	if (rc != 0) {
2097c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
2098c0c050c5SMichael Chan 			   rc, filename);
2099c0c050c5SMichael Chan 		return rc;
2100c0c050c5SMichael Chan 	}
2101ba425800SJason Yan 	if (bnxt_dir_type_is_ape_bin_format(dir_type))
2102c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
2103ba425800SJason Yan 	else if (bnxt_dir_type_is_other_exec_format(dir_type))
21045ac67d8bSRob Swindell 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
2105c0c050c5SMichael Chan 	else
2106c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2107c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
2108c0c050c5SMichael Chan 	release_firmware(fw);
2109c0c050c5SMichael Chan 	return rc;
2110c0c050c5SMichael Chan }
2111c0c050c5SMichael Chan 
2112d168f328SVasundhara Volam int bnxt_flash_package_from_file(struct net_device *dev, const char *filename,
2113d168f328SVasundhara Volam 				 u32 install_type)
2114c0c050c5SMichael Chan {
21155ac67d8bSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
21165ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
21175ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_input install = {0};
21185ac67d8bSRob Swindell 	const struct firmware *fw;
21195ac67d8bSRob Swindell 	u32 item_len;
212022630e28SEdwin Peer 	int rc = 0;
21215ac67d8bSRob Swindell 	u16 index;
21225ac67d8bSRob Swindell 
21235ac67d8bSRob Swindell 	bnxt_hwrm_fw_set_time(bp);
21245ac67d8bSRob Swindell 
212595ec1f47SVasundhara Volam 	rc = bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
21265ac67d8bSRob Swindell 				  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
212795ec1f47SVasundhara Volam 				  &index, &item_len, NULL);
212895ec1f47SVasundhara Volam 	if (rc) {
21295ac67d8bSRob Swindell 		netdev_err(dev, "PKG update area not created in nvram\n");
213095ec1f47SVasundhara Volam 		return rc;
21315ac67d8bSRob Swindell 	}
21325ac67d8bSRob Swindell 
21335ac67d8bSRob Swindell 	rc = request_firmware(&fw, filename, &dev->dev);
21345ac67d8bSRob Swindell 	if (rc != 0) {
21355ac67d8bSRob Swindell 		netdev_err(dev, "PKG error %d requesting file: %s\n",
21365ac67d8bSRob Swindell 			   rc, filename);
21375ac67d8bSRob Swindell 		return rc;
21385ac67d8bSRob Swindell 	}
21395ac67d8bSRob Swindell 
21405ac67d8bSRob Swindell 	if (fw->size > item_len) {
21419a005c38SJonathan Lemon 		netdev_err(dev, "PKG insufficient update area in nvram: %lu\n",
21425ac67d8bSRob Swindell 			   (unsigned long)fw->size);
21435ac67d8bSRob Swindell 		rc = -EFBIG;
21445ac67d8bSRob Swindell 	} else {
21455ac67d8bSRob Swindell 		dma_addr_t dma_handle;
21465ac67d8bSRob Swindell 		u8 *kmem;
21475ac67d8bSRob Swindell 		struct hwrm_nvm_modify_input modify = {0};
21485ac67d8bSRob Swindell 
21495ac67d8bSRob Swindell 		bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
21505ac67d8bSRob Swindell 
21515ac67d8bSRob Swindell 		modify.dir_idx = cpu_to_le16(index);
21525ac67d8bSRob Swindell 		modify.len = cpu_to_le32(fw->size);
21535ac67d8bSRob Swindell 
21545ac67d8bSRob Swindell 		kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
21555ac67d8bSRob Swindell 					  &dma_handle, GFP_KERNEL);
21565ac67d8bSRob Swindell 		if (!kmem) {
21575ac67d8bSRob Swindell 			netdev_err(dev,
21585ac67d8bSRob Swindell 				   "dma_alloc_coherent failure, length = %u\n",
21595ac67d8bSRob Swindell 				   (unsigned int)fw->size);
21605ac67d8bSRob Swindell 			rc = -ENOMEM;
21615ac67d8bSRob Swindell 		} else {
21625ac67d8bSRob Swindell 			memcpy(kmem, fw->data, fw->size);
21635ac67d8bSRob Swindell 			modify.host_src_addr = cpu_to_le64(dma_handle);
21645ac67d8bSRob Swindell 
216522630e28SEdwin Peer 			rc = hwrm_send_message(bp, &modify, sizeof(modify),
21665ac67d8bSRob Swindell 					       FLASH_PACKAGE_TIMEOUT);
21675ac67d8bSRob Swindell 			dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
21685ac67d8bSRob Swindell 					  dma_handle);
21695ac67d8bSRob Swindell 		}
21705ac67d8bSRob Swindell 	}
21715ac67d8bSRob Swindell 	release_firmware(fw);
217222630e28SEdwin Peer 	if (rc)
21737c675421SVasundhara Volam 		goto err_exit;
21745ac67d8bSRob Swindell 
21755ac67d8bSRob Swindell 	if ((install_type & 0xffff) == 0)
21765ac67d8bSRob Swindell 		install_type >>= 16;
21775ac67d8bSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
21785ac67d8bSRob Swindell 	install.install_type = cpu_to_le32(install_type);
21795ac67d8bSRob Swindell 
2180cb4d1d62SKshitij Soni 	mutex_lock(&bp->hwrm_cmd_lock);
218122630e28SEdwin Peer 	rc = _hwrm_send_message(bp, &install, sizeof(install),
21825ac67d8bSRob Swindell 				INSTALL_PACKAGE_TIMEOUT);
218322630e28SEdwin Peer 	if (rc) {
2184cb4d1d62SKshitij Soni 		u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err;
2185cb4d1d62SKshitij Soni 
2186dd2ebf34SVasundhara Volam 		if (resp->error_code && error_code ==
2187dd2ebf34SVasundhara Volam 		    NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
2188cb4d1d62SKshitij Soni 			install.flags |= cpu_to_le16(
2189cb4d1d62SKshitij Soni 			       NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
219022630e28SEdwin Peer 			rc = _hwrm_send_message(bp, &install, sizeof(install),
2191cb4d1d62SKshitij Soni 						INSTALL_PACKAGE_TIMEOUT);
2192dd2ebf34SVasundhara Volam 		}
219322630e28SEdwin Peer 		if (rc)
2194cb4d1d62SKshitij Soni 			goto flash_pkg_exit;
2195cb4d1d62SKshitij Soni 	}
21965ac67d8bSRob Swindell 
21975ac67d8bSRob Swindell 	if (resp->result) {
21985ac67d8bSRob Swindell 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
21995ac67d8bSRob Swindell 			   (s8)resp->result, (int)resp->problem_item);
2200cb4d1d62SKshitij Soni 		rc = -ENOPKG;
22015ac67d8bSRob Swindell 	}
2202cb4d1d62SKshitij Soni flash_pkg_exit:
2203cb4d1d62SKshitij Soni 	mutex_unlock(&bp->hwrm_cmd_lock);
22047c675421SVasundhara Volam err_exit:
220522630e28SEdwin Peer 	if (rc == -EACCES)
2206b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
2207cb4d1d62SKshitij Soni 	return rc;
2208c0c050c5SMichael Chan }
2209c0c050c5SMichael Chan 
2210c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
2211c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
2212c0c050c5SMichael Chan {
2213c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
2214c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
2215c0c050c5SMichael Chan 		return -EINVAL;
2216c0c050c5SMichael Chan 	}
2217c0c050c5SMichael Chan 
22185ac67d8bSRob Swindell 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
22195ac67d8bSRob Swindell 	    flash->region > 0xffff)
22205ac67d8bSRob Swindell 		return bnxt_flash_package_from_file(dev, flash->data,
22215ac67d8bSRob Swindell 						    flash->region);
2222c0c050c5SMichael Chan 
2223c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
2224c0c050c5SMichael Chan }
2225c0c050c5SMichael Chan 
2226c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
2227c0c050c5SMichael Chan {
2228c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2229c0c050c5SMichael Chan 	int rc;
2230c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
2231c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
2232c0c050c5SMichael Chan 
2233c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
2234c0c050c5SMichael Chan 
2235c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2236c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2237c0c050c5SMichael Chan 	if (!rc) {
2238c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
2239c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
2240c0c050c5SMichael Chan 	}
2241c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2242c0c050c5SMichael Chan 	return rc;
2243c0c050c5SMichael Chan }
2244c0c050c5SMichael Chan 
2245c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
2246c0c050c5SMichael Chan {
22474cebbacaSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
22484cebbacaSMichael Chan 
22494cebbacaSMichael Chan 	if (BNXT_VF(bp))
22504cebbacaSMichael Chan 		return 0;
22514cebbacaSMichael Chan 
2252c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
2253c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
2254c0c050c5SMichael Chan 	 */
2255c0c050c5SMichael Chan 	return -1;
2256c0c050c5SMichael Chan }
2257c0c050c5SMichael Chan 
2258c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
2259c0c050c5SMichael Chan {
2260c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2261c0c050c5SMichael Chan 	int rc;
2262c0c050c5SMichael Chan 	u32 dir_entries;
2263c0c050c5SMichael Chan 	u32 entry_length;
2264c0c050c5SMichael Chan 	u8 *buf;
2265c0c050c5SMichael Chan 	size_t buflen;
2266c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2267c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
2268c0c050c5SMichael Chan 
2269c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
2270c0c050c5SMichael Chan 	if (rc != 0)
2271c0c050c5SMichael Chan 		return rc;
2272c0c050c5SMichael Chan 
2273c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
2274c0c050c5SMichael Chan 	if (len < 2)
2275c0c050c5SMichael Chan 		return -EINVAL;
2276c0c050c5SMichael Chan 
2277c0c050c5SMichael Chan 	*data++ = dir_entries;
2278c0c050c5SMichael Chan 	*data++ = entry_length;
2279c0c050c5SMichael Chan 	len -= 2;
2280c0c050c5SMichael Chan 	memset(data, 0xff, len);
2281c0c050c5SMichael Chan 
2282c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
2283c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
2284c0c050c5SMichael Chan 				 GFP_KERNEL);
2285c0c050c5SMichael Chan 	if (!buf) {
2286c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2287c0c050c5SMichael Chan 			   (unsigned)buflen);
2288c0c050c5SMichael Chan 		return -ENOMEM;
2289c0c050c5SMichael Chan 	}
2290c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
2291c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2292c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2293c0c050c5SMichael Chan 	if (rc == 0)
2294c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
2295c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
2296c0c050c5SMichael Chan 	return rc;
2297c0c050c5SMichael Chan }
2298c0c050c5SMichael Chan 
2299c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
2300c0c050c5SMichael Chan 			       u32 length, u8 *data)
2301c0c050c5SMichael Chan {
2302c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2303c0c050c5SMichael Chan 	int rc;
2304c0c050c5SMichael Chan 	u8 *buf;
2305c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2306c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
2307c0c050c5SMichael Chan 
2308e0ad8fc5SMichael Chan 	if (!length)
2309e0ad8fc5SMichael Chan 		return -EINVAL;
2310e0ad8fc5SMichael Chan 
2311c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
2312c0c050c5SMichael Chan 				 GFP_KERNEL);
2313c0c050c5SMichael Chan 	if (!buf) {
2314c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2315c0c050c5SMichael Chan 			   (unsigned)length);
2316c0c050c5SMichael Chan 		return -ENOMEM;
2317c0c050c5SMichael Chan 	}
2318c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
2319c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2320c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2321c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
2322c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
2323c0c050c5SMichael Chan 
2324c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2325c0c050c5SMichael Chan 	if (rc == 0)
2326c0c050c5SMichael Chan 		memcpy(data, buf, length);
2327c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
2328c0c050c5SMichael Chan 	return rc;
2329c0c050c5SMichael Chan }
2330c0c050c5SMichael Chan 
23313ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
23323ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
23333ebf6f0aSRob Swindell 				u32 *data_length)
23343ebf6f0aSRob Swindell {
23353ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
23363ebf6f0aSRob Swindell 	int rc;
23373ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
23383ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
23393ebf6f0aSRob Swindell 
23403ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
23413ebf6f0aSRob Swindell 	req.enables = 0;
23423ebf6f0aSRob Swindell 	req.dir_idx = 0;
23433ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
23443ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
23453ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
23463ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
2347cc72f3b1SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2348cc72f3b1SMichael Chan 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
23493ebf6f0aSRob Swindell 	if (rc == 0) {
23503ebf6f0aSRob Swindell 		if (index)
23513ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
23523ebf6f0aSRob Swindell 		if (item_length)
23533ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
23543ebf6f0aSRob Swindell 		if (data_length)
23553ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
23563ebf6f0aSRob Swindell 	}
2357cc72f3b1SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
23583ebf6f0aSRob Swindell 	return rc;
23593ebf6f0aSRob Swindell }
23603ebf6f0aSRob Swindell 
23613ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
23623ebf6f0aSRob Swindell {
23633ebf6f0aSRob Swindell 	char	*retval = NULL;
23643ebf6f0aSRob Swindell 	char	*p;
23653ebf6f0aSRob Swindell 	char	*value;
23663ebf6f0aSRob Swindell 	int	field = 0;
23673ebf6f0aSRob Swindell 
23683ebf6f0aSRob Swindell 	if (datalen < 1)
23693ebf6f0aSRob Swindell 		return NULL;
23703ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
23713ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
23723ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
23733ebf6f0aSRob Swindell 		field = 0;
23743ebf6f0aSRob Swindell 		retval = NULL;
23753ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
23763ebf6f0aSRob Swindell 			value = p;
23773ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
23783ebf6f0aSRob Swindell 				p++;
23793ebf6f0aSRob Swindell 			if (field == desired_field)
23803ebf6f0aSRob Swindell 				retval = value;
23813ebf6f0aSRob Swindell 			if (*p != '\t')
23823ebf6f0aSRob Swindell 				break;
23833ebf6f0aSRob Swindell 			*p = 0;
23843ebf6f0aSRob Swindell 			field++;
23853ebf6f0aSRob Swindell 			p++;
23863ebf6f0aSRob Swindell 		}
23873ebf6f0aSRob Swindell 		if (*p == 0)
23883ebf6f0aSRob Swindell 			break;
23893ebf6f0aSRob Swindell 		*p = 0;
23903ebf6f0aSRob Swindell 	}
23913ebf6f0aSRob Swindell 	return retval;
23923ebf6f0aSRob Swindell }
23933ebf6f0aSRob Swindell 
2394a60faa60SVasundhara Volam static void bnxt_get_pkgver(struct net_device *dev)
23953ebf6f0aSRob Swindell {
2396a60faa60SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
23973ebf6f0aSRob Swindell 	u16 index = 0;
2398a60faa60SVasundhara Volam 	char *pkgver;
2399a60faa60SVasundhara Volam 	u32 pkglen;
2400a60faa60SVasundhara Volam 	u8 *pkgbuf;
2401a60faa60SVasundhara Volam 	int len;
24023ebf6f0aSRob Swindell 
24033ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
24043ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2405a60faa60SVasundhara Volam 				 &index, NULL, &pkglen) != 0)
2406a60faa60SVasundhara Volam 		return;
24073ebf6f0aSRob Swindell 
2408a60faa60SVasundhara Volam 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
2409a60faa60SVasundhara Volam 	if (!pkgbuf) {
2410a60faa60SVasundhara Volam 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
2411a60faa60SVasundhara Volam 			pkglen);
2412a60faa60SVasundhara Volam 		return;
2413a60faa60SVasundhara Volam 	}
24143ebf6f0aSRob Swindell 
2415a60faa60SVasundhara Volam 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2416a60faa60SVasundhara Volam 		goto err;
2417a60faa60SVasundhara Volam 
2418a60faa60SVasundhara Volam 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2419a60faa60SVasundhara Volam 				   pkglen);
2420a60faa60SVasundhara Volam 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2421a60faa60SVasundhara Volam 		len = strlen(bp->fw_ver_str);
2422a60faa60SVasundhara Volam 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2423a60faa60SVasundhara Volam 			 "/pkg %s", pkgver);
2424a60faa60SVasundhara Volam 	}
2425a60faa60SVasundhara Volam err:
2426a60faa60SVasundhara Volam 	kfree(pkgbuf);
24273ebf6f0aSRob Swindell }
24283ebf6f0aSRob Swindell 
2429c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
2430c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2431c0c050c5SMichael Chan 			   u8 *data)
2432c0c050c5SMichael Chan {
2433c0c050c5SMichael Chan 	u32 index;
2434c0c050c5SMichael Chan 	u32 offset;
2435c0c050c5SMichael Chan 
2436c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
2437c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2438c0c050c5SMichael Chan 
2439c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
2440c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
2441c0c050c5SMichael Chan 
2442c0c050c5SMichael Chan 	if (index == 0) {
2443c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
2444c0c050c5SMichael Chan 		return -EINVAL;
2445c0c050c5SMichael Chan 	}
2446c0c050c5SMichael Chan 
2447c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2448c0c050c5SMichael Chan }
2449c0c050c5SMichael Chan 
2450c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2451c0c050c5SMichael Chan {
2452c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2453c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2454c0c050c5SMichael Chan 
2455c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2456c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2457c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2458c0c050c5SMichael Chan }
2459c0c050c5SMichael Chan 
2460c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
2461c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2462c0c050c5SMichael Chan 			   u8 *data)
2463c0c050c5SMichael Chan {
2464c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2465c0c050c5SMichael Chan 	u8 index, dir_op;
2466c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
2467c0c050c5SMichael Chan 
2468c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
2469c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2470c0c050c5SMichael Chan 		return -EINVAL;
2471c0c050c5SMichael Chan 	}
2472c0c050c5SMichael Chan 
2473c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
2474c0c050c5SMichael Chan 
2475c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
2476c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
2477c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
2478c0c050c5SMichael Chan 		if (index == 0)
2479c0c050c5SMichael Chan 			return -EINVAL;
2480c0c050c5SMichael Chan 		switch (dir_op) {
2481c0c050c5SMichael Chan 		case 0x0e: /* erase */
2482c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
2483c0c050c5SMichael Chan 				return -EINVAL;
2484c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
2485c0c050c5SMichael Chan 		default:
2486c0c050c5SMichael Chan 			return -EINVAL;
2487c0c050c5SMichael Chan 		}
2488c0c050c5SMichael Chan 	}
2489c0c050c5SMichael Chan 
2490c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
2491ba425800SJason Yan 	if (bnxt_dir_type_is_executable(type))
24925ac67d8bSRob Swindell 		return -EOPNOTSUPP;
2493c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
2494c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
2495c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
2496c0c050c5SMichael Chan 
2497c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2498c0c050c5SMichael Chan 				eeprom->len);
2499c0c050c5SMichael Chan }
2500c0c050c5SMichael Chan 
250172b34f04SMichael Chan static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
250272b34f04SMichael Chan {
250372b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
250472b34f04SMichael Chan 	struct ethtool_eee *eee = &bp->eee;
250572b34f04SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
250672b34f04SMichael Chan 	u32 advertising =
250772b34f04SMichael Chan 		 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
250872b34f04SMichael Chan 	int rc = 0;
250972b34f04SMichael Chan 
2510c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
251175362a3fSMichael Chan 		return -EOPNOTSUPP;
251272b34f04SMichael Chan 
251372b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
251472b34f04SMichael Chan 		return -EOPNOTSUPP;
251572b34f04SMichael Chan 
251672b34f04SMichael Chan 	if (!edata->eee_enabled)
251772b34f04SMichael Chan 		goto eee_ok;
251872b34f04SMichael Chan 
251972b34f04SMichael Chan 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
252072b34f04SMichael Chan 		netdev_warn(dev, "EEE requires autoneg\n");
252172b34f04SMichael Chan 		return -EINVAL;
252272b34f04SMichael Chan 	}
252372b34f04SMichael Chan 	if (edata->tx_lpi_enabled) {
252472b34f04SMichael Chan 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
252572b34f04SMichael Chan 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
252672b34f04SMichael Chan 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
252772b34f04SMichael Chan 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
252872b34f04SMichael Chan 			return -EINVAL;
252972b34f04SMichael Chan 		} else if (!bp->lpi_tmr_hi) {
253072b34f04SMichael Chan 			edata->tx_lpi_timer = eee->tx_lpi_timer;
253172b34f04SMichael Chan 		}
253272b34f04SMichael Chan 	}
253372b34f04SMichael Chan 	if (!edata->advertised) {
253472b34f04SMichael Chan 		edata->advertised = advertising & eee->supported;
253572b34f04SMichael Chan 	} else if (edata->advertised & ~advertising) {
253672b34f04SMichael Chan 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
253772b34f04SMichael Chan 			    edata->advertised, advertising);
253872b34f04SMichael Chan 		return -EINVAL;
253972b34f04SMichael Chan 	}
254072b34f04SMichael Chan 
254172b34f04SMichael Chan 	eee->advertised = edata->advertised;
254272b34f04SMichael Chan 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
254372b34f04SMichael Chan 	eee->tx_lpi_timer = edata->tx_lpi_timer;
254472b34f04SMichael Chan eee_ok:
254572b34f04SMichael Chan 	eee->eee_enabled = edata->eee_enabled;
254672b34f04SMichael Chan 
254772b34f04SMichael Chan 	if (netif_running(dev))
254872b34f04SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
254972b34f04SMichael Chan 
255072b34f04SMichael Chan 	return rc;
255172b34f04SMichael Chan }
255272b34f04SMichael Chan 
255372b34f04SMichael Chan static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
255472b34f04SMichael Chan {
255572b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
255672b34f04SMichael Chan 
255772b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
255872b34f04SMichael Chan 		return -EOPNOTSUPP;
255972b34f04SMichael Chan 
256072b34f04SMichael Chan 	*edata = bp->eee;
256172b34f04SMichael Chan 	if (!bp->eee.eee_enabled) {
256272b34f04SMichael Chan 		/* Preserve tx_lpi_timer so that the last value will be used
256372b34f04SMichael Chan 		 * by default when it is re-enabled.
256472b34f04SMichael Chan 		 */
256572b34f04SMichael Chan 		edata->advertised = 0;
256672b34f04SMichael Chan 		edata->tx_lpi_enabled = 0;
256772b34f04SMichael Chan 	}
256872b34f04SMichael Chan 
256972b34f04SMichael Chan 	if (!bp->eee.eee_active)
257072b34f04SMichael Chan 		edata->lp_advertised = 0;
257172b34f04SMichael Chan 
257272b34f04SMichael Chan 	return 0;
257372b34f04SMichael Chan }
257472b34f04SMichael Chan 
257542ee18feSAjit Khaparde static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
257642ee18feSAjit Khaparde 					    u16 page_number, u16 start_addr,
257742ee18feSAjit Khaparde 					    u16 data_length, u8 *buf)
257842ee18feSAjit Khaparde {
257942ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_input req = {0};
258042ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
258142ee18feSAjit Khaparde 	int rc, byte_offset = 0;
258242ee18feSAjit Khaparde 
258342ee18feSAjit Khaparde 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
258442ee18feSAjit Khaparde 	req.i2c_slave_addr = i2c_addr;
258542ee18feSAjit Khaparde 	req.page_number = cpu_to_le16(page_number);
258642ee18feSAjit Khaparde 	req.port_id = cpu_to_le16(bp->pf.port_id);
258742ee18feSAjit Khaparde 	do {
258842ee18feSAjit Khaparde 		u16 xfer_size;
258942ee18feSAjit Khaparde 
259042ee18feSAjit Khaparde 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
259142ee18feSAjit Khaparde 		data_length -= xfer_size;
259242ee18feSAjit Khaparde 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
259342ee18feSAjit Khaparde 		req.data_length = xfer_size;
259442ee18feSAjit Khaparde 		req.enables = cpu_to_le32(start_addr + byte_offset ?
259542ee18feSAjit Khaparde 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
259642ee18feSAjit Khaparde 		mutex_lock(&bp->hwrm_cmd_lock);
259742ee18feSAjit Khaparde 		rc = _hwrm_send_message(bp, &req, sizeof(req),
259842ee18feSAjit Khaparde 					HWRM_CMD_TIMEOUT);
259942ee18feSAjit Khaparde 		if (!rc)
260042ee18feSAjit Khaparde 			memcpy(buf + byte_offset, output->data, xfer_size);
260142ee18feSAjit Khaparde 		mutex_unlock(&bp->hwrm_cmd_lock);
260242ee18feSAjit Khaparde 		byte_offset += xfer_size;
260342ee18feSAjit Khaparde 	} while (!rc && data_length > 0);
260442ee18feSAjit Khaparde 
260542ee18feSAjit Khaparde 	return rc;
260642ee18feSAjit Khaparde }
260742ee18feSAjit Khaparde 
260842ee18feSAjit Khaparde static int bnxt_get_module_info(struct net_device *dev,
260942ee18feSAjit Khaparde 				struct ethtool_modinfo *modinfo)
261042ee18feSAjit Khaparde {
26117328a23cSVasundhara Volam 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
261242ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
261342ee18feSAjit Khaparde 	int rc;
261442ee18feSAjit Khaparde 
261542ee18feSAjit Khaparde 	/* No point in going further if phy status indicates
261642ee18feSAjit Khaparde 	 * module is not inserted or if it is powered down or
261742ee18feSAjit Khaparde 	 * if it is of type 10GBase-T
261842ee18feSAjit Khaparde 	 */
261942ee18feSAjit Khaparde 	if (bp->link_info.module_status >
262042ee18feSAjit Khaparde 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
262142ee18feSAjit Khaparde 		return -EOPNOTSUPP;
262242ee18feSAjit Khaparde 
262342ee18feSAjit Khaparde 	/* This feature is not supported in older firmware versions */
262442ee18feSAjit Khaparde 	if (bp->hwrm_spec_code < 0x10202)
262542ee18feSAjit Khaparde 		return -EOPNOTSUPP;
262642ee18feSAjit Khaparde 
26277328a23cSVasundhara Volam 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
26287328a23cSVasundhara Volam 					      SFF_DIAG_SUPPORT_OFFSET + 1,
26297328a23cSVasundhara Volam 					      data);
263042ee18feSAjit Khaparde 	if (!rc) {
26317328a23cSVasundhara Volam 		u8 module_id = data[0];
26327328a23cSVasundhara Volam 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
263342ee18feSAjit Khaparde 
263442ee18feSAjit Khaparde 		switch (module_id) {
263542ee18feSAjit Khaparde 		case SFF_MODULE_ID_SFP:
263642ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8472;
263742ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
26387328a23cSVasundhara Volam 			if (!diag_supported)
26397328a23cSVasundhara Volam 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
264042ee18feSAjit Khaparde 			break;
264142ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP:
264242ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP_PLUS:
264342ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8436;
264442ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
264542ee18feSAjit Khaparde 			break;
264642ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP28:
264742ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8636;
264842ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
264942ee18feSAjit Khaparde 			break;
265042ee18feSAjit Khaparde 		default:
265142ee18feSAjit Khaparde 			rc = -EOPNOTSUPP;
265242ee18feSAjit Khaparde 			break;
265342ee18feSAjit Khaparde 		}
265442ee18feSAjit Khaparde 	}
265542ee18feSAjit Khaparde 	return rc;
265642ee18feSAjit Khaparde }
265742ee18feSAjit Khaparde 
265842ee18feSAjit Khaparde static int bnxt_get_module_eeprom(struct net_device *dev,
265942ee18feSAjit Khaparde 				  struct ethtool_eeprom *eeprom,
266042ee18feSAjit Khaparde 				  u8 *data)
266142ee18feSAjit Khaparde {
266242ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
266342ee18feSAjit Khaparde 	u16  start = eeprom->offset, length = eeprom->len;
2664f3ea3119SColin Ian King 	int rc = 0;
266542ee18feSAjit Khaparde 
266642ee18feSAjit Khaparde 	memset(data, 0, eeprom->len);
266742ee18feSAjit Khaparde 
266842ee18feSAjit Khaparde 	/* Read A0 portion of the EEPROM */
266942ee18feSAjit Khaparde 	if (start < ETH_MODULE_SFF_8436_LEN) {
267042ee18feSAjit Khaparde 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
267142ee18feSAjit Khaparde 			length = ETH_MODULE_SFF_8436_LEN - start;
267242ee18feSAjit Khaparde 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
267342ee18feSAjit Khaparde 						      start, length, data);
267442ee18feSAjit Khaparde 		if (rc)
267542ee18feSAjit Khaparde 			return rc;
267642ee18feSAjit Khaparde 		start += length;
267742ee18feSAjit Khaparde 		data += length;
267842ee18feSAjit Khaparde 		length = eeprom->len - length;
267942ee18feSAjit Khaparde 	}
268042ee18feSAjit Khaparde 
268142ee18feSAjit Khaparde 	/* Read A2 portion of the EEPROM */
268242ee18feSAjit Khaparde 	if (length) {
268342ee18feSAjit Khaparde 		start -= ETH_MODULE_SFF_8436_LEN;
2684dea521a2SChristophe JAILLET 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1,
2685dea521a2SChristophe JAILLET 						      start, length, data);
268642ee18feSAjit Khaparde 	}
268742ee18feSAjit Khaparde 	return rc;
268842ee18feSAjit Khaparde }
268942ee18feSAjit Khaparde 
2690ae8e98a6SDeepak Khungar static int bnxt_nway_reset(struct net_device *dev)
2691ae8e98a6SDeepak Khungar {
2692ae8e98a6SDeepak Khungar 	int rc = 0;
2693ae8e98a6SDeepak Khungar 
2694ae8e98a6SDeepak Khungar 	struct bnxt *bp = netdev_priv(dev);
2695ae8e98a6SDeepak Khungar 	struct bnxt_link_info *link_info = &bp->link_info;
2696ae8e98a6SDeepak Khungar 
2697c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
2698ae8e98a6SDeepak Khungar 		return -EOPNOTSUPP;
2699ae8e98a6SDeepak Khungar 
2700ae8e98a6SDeepak Khungar 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2701ae8e98a6SDeepak Khungar 		return -EINVAL;
2702ae8e98a6SDeepak Khungar 
2703ae8e98a6SDeepak Khungar 	if (netif_running(dev))
2704ae8e98a6SDeepak Khungar 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
2705ae8e98a6SDeepak Khungar 
2706ae8e98a6SDeepak Khungar 	return rc;
2707ae8e98a6SDeepak Khungar }
2708ae8e98a6SDeepak Khungar 
27095ad2cbeeSMichael Chan static int bnxt_set_phys_id(struct net_device *dev,
27105ad2cbeeSMichael Chan 			    enum ethtool_phys_id_state state)
27115ad2cbeeSMichael Chan {
27125ad2cbeeSMichael Chan 	struct hwrm_port_led_cfg_input req = {0};
27135ad2cbeeSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
27145ad2cbeeSMichael Chan 	struct bnxt_pf_info *pf = &bp->pf;
27155ad2cbeeSMichael Chan 	struct bnxt_led_cfg *led_cfg;
27165ad2cbeeSMichael Chan 	u8 led_state;
27175ad2cbeeSMichael Chan 	__le16 duration;
27189f90445cSVasundhara Volam 	int i;
27195ad2cbeeSMichael Chan 
27205ad2cbeeSMichael Chan 	if (!bp->num_leds || BNXT_VF(bp))
27215ad2cbeeSMichael Chan 		return -EOPNOTSUPP;
27225ad2cbeeSMichael Chan 
27235ad2cbeeSMichael Chan 	if (state == ETHTOOL_ID_ACTIVE) {
27245ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
27255ad2cbeeSMichael Chan 		duration = cpu_to_le16(500);
27265ad2cbeeSMichael Chan 	} else if (state == ETHTOOL_ID_INACTIVE) {
27275ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
27285ad2cbeeSMichael Chan 		duration = cpu_to_le16(0);
27295ad2cbeeSMichael Chan 	} else {
27305ad2cbeeSMichael Chan 		return -EINVAL;
27315ad2cbeeSMichael Chan 	}
27325ad2cbeeSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
27335ad2cbeeSMichael Chan 	req.port_id = cpu_to_le16(pf->port_id);
27345ad2cbeeSMichael Chan 	req.num_leds = bp->num_leds;
27355ad2cbeeSMichael Chan 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
27365ad2cbeeSMichael Chan 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
27375ad2cbeeSMichael Chan 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
27385ad2cbeeSMichael Chan 		led_cfg->led_id = bp->leds[i].led_id;
27395ad2cbeeSMichael Chan 		led_cfg->led_state = led_state;
27405ad2cbeeSMichael Chan 		led_cfg->led_blink_on = duration;
27415ad2cbeeSMichael Chan 		led_cfg->led_blink_off = duration;
27425ad2cbeeSMichael Chan 		led_cfg->led_group_id = bp->leds[i].led_group_id;
27435ad2cbeeSMichael Chan 	}
27449f90445cSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
27455ad2cbeeSMichael Chan }
27465ad2cbeeSMichael Chan 
274767fea463SMichael Chan static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
274867fea463SMichael Chan {
274967fea463SMichael Chan 	struct hwrm_selftest_irq_input req = {0};
275067fea463SMichael Chan 
275167fea463SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
275267fea463SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
275367fea463SMichael Chan }
275467fea463SMichael Chan 
275567fea463SMichael Chan static int bnxt_test_irq(struct bnxt *bp)
275667fea463SMichael Chan {
275767fea463SMichael Chan 	int i;
275867fea463SMichael Chan 
275967fea463SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
276067fea463SMichael Chan 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
276167fea463SMichael Chan 		int rc;
276267fea463SMichael Chan 
276367fea463SMichael Chan 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
276467fea463SMichael Chan 		if (rc)
276567fea463SMichael Chan 			return rc;
276667fea463SMichael Chan 	}
276767fea463SMichael Chan 	return 0;
276867fea463SMichael Chan }
276967fea463SMichael Chan 
2770f7dc1ea6SMichael Chan static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
2771f7dc1ea6SMichael Chan {
2772f7dc1ea6SMichael Chan 	struct hwrm_port_mac_cfg_input req = {0};
2773f7dc1ea6SMichael Chan 
2774f7dc1ea6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
2775f7dc1ea6SMichael Chan 
2776f7dc1ea6SMichael Chan 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
2777f7dc1ea6SMichael Chan 	if (enable)
2778f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
2779f7dc1ea6SMichael Chan 	else
2780f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
2781f7dc1ea6SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2782f7dc1ea6SMichael Chan }
2783f7dc1ea6SMichael Chan 
278456d37462SVasundhara Volam static int bnxt_query_force_speeds(struct bnxt *bp, u16 *force_speeds)
278556d37462SVasundhara Volam {
278656d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
278756d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_input req = {0};
278856d37462SVasundhara Volam 	int rc;
278956d37462SVasundhara Volam 
279056d37462SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
279156d37462SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
279256d37462SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
279356d37462SVasundhara Volam 	if (!rc)
279456d37462SVasundhara Volam 		*force_speeds = le16_to_cpu(resp->supported_speeds_force_mode);
279556d37462SVasundhara Volam 
279656d37462SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
279756d37462SVasundhara Volam 	return rc;
279856d37462SVasundhara Volam }
279956d37462SVasundhara Volam 
280091725d89SMichael Chan static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
280191725d89SMichael Chan 				    struct hwrm_port_phy_cfg_input *req)
280291725d89SMichael Chan {
280391725d89SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
280456d37462SVasundhara Volam 	u16 fw_advertising;
280591725d89SMichael Chan 	u16 fw_speed;
280691725d89SMichael Chan 	int rc;
280791725d89SMichael Chan 
28088a60efd1SMichael Chan 	if (!link_info->autoneg ||
28098a60efd1SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_AN_PHY_LPBK))
281091725d89SMichael Chan 		return 0;
281191725d89SMichael Chan 
281256d37462SVasundhara Volam 	rc = bnxt_query_force_speeds(bp, &fw_advertising);
281356d37462SVasundhara Volam 	if (rc)
281456d37462SVasundhara Volam 		return rc;
281556d37462SVasundhara Volam 
281691725d89SMichael Chan 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
281783d8f5e9SMichael Chan 	if (bp->link_info.link_up)
281891725d89SMichael Chan 		fw_speed = bp->link_info.link_speed;
281991725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
282091725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
282191725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
282291725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
282391725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
282491725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
282591725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
282691725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
282791725d89SMichael Chan 
282891725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(fw_speed);
282991725d89SMichael Chan 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
283091725d89SMichael Chan 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
283191725d89SMichael Chan 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
283291725d89SMichael Chan 	req->flags = 0;
283391725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(0);
283491725d89SMichael Chan 	return rc;
283591725d89SMichael Chan }
283691725d89SMichael Chan 
283755fd0cf3SMichael Chan static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
283891725d89SMichael Chan {
283991725d89SMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
284091725d89SMichael Chan 
284191725d89SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
284291725d89SMichael Chan 
284391725d89SMichael Chan 	if (enable) {
284491725d89SMichael Chan 		bnxt_disable_an_for_lpbk(bp, &req);
284555fd0cf3SMichael Chan 		if (ext)
284655fd0cf3SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
284755fd0cf3SMichael Chan 		else
284891725d89SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
284991725d89SMichael Chan 	} else {
285091725d89SMichael Chan 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
285191725d89SMichael Chan 	}
285291725d89SMichael Chan 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
285391725d89SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
285491725d89SMichael Chan }
285591725d89SMichael Chan 
2856e44758b7SMichael Chan static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2857f7dc1ea6SMichael Chan 			    u32 raw_cons, int pkt_size)
2858f7dc1ea6SMichael Chan {
2859e44758b7SMichael Chan 	struct bnxt_napi *bnapi = cpr->bnapi;
2860e44758b7SMichael Chan 	struct bnxt_rx_ring_info *rxr;
2861f7dc1ea6SMichael Chan 	struct bnxt_sw_rx_bd *rx_buf;
2862f7dc1ea6SMichael Chan 	struct rx_cmp *rxcmp;
2863f7dc1ea6SMichael Chan 	u16 cp_cons, cons;
2864f7dc1ea6SMichael Chan 	u8 *data;
2865f7dc1ea6SMichael Chan 	u32 len;
2866f7dc1ea6SMichael Chan 	int i;
2867f7dc1ea6SMichael Chan 
2868e44758b7SMichael Chan 	rxr = bnapi->rx_ring;
2869f7dc1ea6SMichael Chan 	cp_cons = RING_CMP(raw_cons);
2870f7dc1ea6SMichael Chan 	rxcmp = (struct rx_cmp *)
2871f7dc1ea6SMichael Chan 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2872f7dc1ea6SMichael Chan 	cons = rxcmp->rx_cmp_opaque;
2873f7dc1ea6SMichael Chan 	rx_buf = &rxr->rx_buf_ring[cons];
2874f7dc1ea6SMichael Chan 	data = rx_buf->data_ptr;
2875f7dc1ea6SMichael Chan 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
2876f7dc1ea6SMichael Chan 	if (len != pkt_size)
2877f7dc1ea6SMichael Chan 		return -EIO;
2878f7dc1ea6SMichael Chan 	i = ETH_ALEN;
2879f7dc1ea6SMichael Chan 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
2880f7dc1ea6SMichael Chan 		return -EIO;
2881f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2882f7dc1ea6SMichael Chan 	for (  ; i < pkt_size; i++) {
2883f7dc1ea6SMichael Chan 		if (data[i] != (u8)(i & 0xff))
2884f7dc1ea6SMichael Chan 			return -EIO;
2885f7dc1ea6SMichael Chan 	}
2886f7dc1ea6SMichael Chan 	return 0;
2887f7dc1ea6SMichael Chan }
2888f7dc1ea6SMichael Chan 
2889e44758b7SMichael Chan static int bnxt_poll_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2890e44758b7SMichael Chan 			      int pkt_size)
2891f7dc1ea6SMichael Chan {
2892f7dc1ea6SMichael Chan 	struct tx_cmp *txcmp;
2893f7dc1ea6SMichael Chan 	int rc = -EIO;
2894f7dc1ea6SMichael Chan 	u32 raw_cons;
2895f7dc1ea6SMichael Chan 	u32 cons;
2896f7dc1ea6SMichael Chan 	int i;
2897f7dc1ea6SMichael Chan 
2898f7dc1ea6SMichael Chan 	raw_cons = cpr->cp_raw_cons;
2899f7dc1ea6SMichael Chan 	for (i = 0; i < 200; i++) {
2900f7dc1ea6SMichael Chan 		cons = RING_CMP(raw_cons);
2901f7dc1ea6SMichael Chan 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2902f7dc1ea6SMichael Chan 
2903f7dc1ea6SMichael Chan 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
2904f7dc1ea6SMichael Chan 			udelay(5);
2905f7dc1ea6SMichael Chan 			continue;
2906f7dc1ea6SMichael Chan 		}
2907f7dc1ea6SMichael Chan 
2908f7dc1ea6SMichael Chan 		/* The valid test of the entry must be done first before
2909f7dc1ea6SMichael Chan 		 * reading any further.
2910f7dc1ea6SMichael Chan 		 */
2911f7dc1ea6SMichael Chan 		dma_rmb();
2912f7dc1ea6SMichael Chan 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
2913e44758b7SMichael Chan 			rc = bnxt_rx_loopback(bp, cpr, raw_cons, pkt_size);
2914f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2915f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2916f7dc1ea6SMichael Chan 			break;
2917f7dc1ea6SMichael Chan 		}
2918f7dc1ea6SMichael Chan 		raw_cons = NEXT_RAW_CMP(raw_cons);
2919f7dc1ea6SMichael Chan 	}
2920f7dc1ea6SMichael Chan 	cpr->cp_raw_cons = raw_cons;
2921f7dc1ea6SMichael Chan 	return rc;
2922f7dc1ea6SMichael Chan }
2923f7dc1ea6SMichael Chan 
2924f7dc1ea6SMichael Chan static int bnxt_run_loopback(struct bnxt *bp)
2925f7dc1ea6SMichael Chan {
2926f7dc1ea6SMichael Chan 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
292784404d5fSMichael Chan 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
2928e44758b7SMichael Chan 	struct bnxt_cp_ring_info *cpr;
2929f7dc1ea6SMichael Chan 	int pkt_size, i = 0;
2930f7dc1ea6SMichael Chan 	struct sk_buff *skb;
2931f7dc1ea6SMichael Chan 	dma_addr_t map;
2932f7dc1ea6SMichael Chan 	u8 *data;
2933f7dc1ea6SMichael Chan 	int rc;
2934f7dc1ea6SMichael Chan 
293584404d5fSMichael Chan 	cpr = &rxr->bnapi->cp_ring;
293684404d5fSMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
293784404d5fSMichael Chan 		cpr = cpr->cp_ring_arr[BNXT_RX_HDL];
2938f7dc1ea6SMichael Chan 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
2939f7dc1ea6SMichael Chan 	skb = netdev_alloc_skb(bp->dev, pkt_size);
2940f7dc1ea6SMichael Chan 	if (!skb)
2941f7dc1ea6SMichael Chan 		return -ENOMEM;
2942f7dc1ea6SMichael Chan 	data = skb_put(skb, pkt_size);
2943f7dc1ea6SMichael Chan 	eth_broadcast_addr(data);
2944f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2945f7dc1ea6SMichael Chan 	ether_addr_copy(&data[i], bp->dev->dev_addr);
2946f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2947f7dc1ea6SMichael Chan 	for ( ; i < pkt_size; i++)
2948f7dc1ea6SMichael Chan 		data[i] = (u8)(i & 0xff);
2949f7dc1ea6SMichael Chan 
2950f7dc1ea6SMichael Chan 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
2951f7dc1ea6SMichael Chan 			     PCI_DMA_TODEVICE);
2952f7dc1ea6SMichael Chan 	if (dma_mapping_error(&bp->pdev->dev, map)) {
2953f7dc1ea6SMichael Chan 		dev_kfree_skb(skb);
2954f7dc1ea6SMichael Chan 		return -EIO;
2955f7dc1ea6SMichael Chan 	}
2956c1ba92a8SMichael Chan 	bnxt_xmit_bd(bp, txr, map, pkt_size);
2957f7dc1ea6SMichael Chan 
2958f7dc1ea6SMichael Chan 	/* Sync BD data before updating doorbell */
2959f7dc1ea6SMichael Chan 	wmb();
2960f7dc1ea6SMichael Chan 
2961697197e5SMichael Chan 	bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
2962e44758b7SMichael Chan 	rc = bnxt_poll_loopback(bp, cpr, pkt_size);
2963f7dc1ea6SMichael Chan 
2964f7dc1ea6SMichael Chan 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
2965f7dc1ea6SMichael Chan 	dev_kfree_skb(skb);
2966f7dc1ea6SMichael Chan 	return rc;
2967f7dc1ea6SMichael Chan }
2968f7dc1ea6SMichael Chan 
2969eb513658SMichael Chan static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
2970eb513658SMichael Chan {
2971eb513658SMichael Chan 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
2972eb513658SMichael Chan 	struct hwrm_selftest_exec_input req = {0};
2973eb513658SMichael Chan 	int rc;
2974eb513658SMichael Chan 
2975eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
2976eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2977eb513658SMichael Chan 	resp->test_success = 0;
2978eb513658SMichael Chan 	req.flags = test_mask;
2979eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
2980eb513658SMichael Chan 	*test_results = resp->test_success;
2981eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2982eb513658SMichael Chan 	return rc;
2983eb513658SMichael Chan }
2984eb513658SMichael Chan 
298555fd0cf3SMichael Chan #define BNXT_DRV_TESTS			4
2986f7dc1ea6SMichael Chan #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
298791725d89SMichael Chan #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
298855fd0cf3SMichael Chan #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
298955fd0cf3SMichael Chan #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
2990eb513658SMichael Chan 
2991eb513658SMichael Chan static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
2992eb513658SMichael Chan 			   u64 *buf)
2993eb513658SMichael Chan {
2994eb513658SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
299555fd0cf3SMichael Chan 	bool do_ext_lpbk = false;
2996eb513658SMichael Chan 	bool offline = false;
2997eb513658SMichael Chan 	u8 test_results = 0;
2998eb513658SMichael Chan 	u8 test_mask = 0;
2999d27e2ca1SMichael Chan 	int rc = 0, i;
3000eb513658SMichael Chan 
3001eb513658SMichael Chan 	if (!bp->num_tests || !BNXT_SINGLE_PF(bp))
3002eb513658SMichael Chan 		return;
3003eb513658SMichael Chan 	memset(buf, 0, sizeof(u64) * bp->num_tests);
3004eb513658SMichael Chan 	if (!netif_running(dev)) {
3005eb513658SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
3006eb513658SMichael Chan 		return;
3007eb513658SMichael Chan 	}
3008eb513658SMichael Chan 
300955fd0cf3SMichael Chan 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
301055fd0cf3SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_EXT_LPBK))
301155fd0cf3SMichael Chan 		do_ext_lpbk = true;
301255fd0cf3SMichael Chan 
3013eb513658SMichael Chan 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
3014eb513658SMichael Chan 		if (bp->pf.active_vfs) {
3015eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3016eb513658SMichael Chan 			netdev_warn(dev, "Offline tests cannot be run with active VFs\n");
3017eb513658SMichael Chan 			return;
3018eb513658SMichael Chan 		}
3019eb513658SMichael Chan 		offline = true;
3020eb513658SMichael Chan 	}
3021eb513658SMichael Chan 
3022eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3023eb513658SMichael Chan 		u8 bit_val = 1 << i;
3024eb513658SMichael Chan 
3025eb513658SMichael Chan 		if (!(bp->test_info->offline_mask & bit_val))
3026eb513658SMichael Chan 			test_mask |= bit_val;
3027eb513658SMichael Chan 		else if (offline)
3028eb513658SMichael Chan 			test_mask |= bit_val;
3029eb513658SMichael Chan 	}
3030eb513658SMichael Chan 	if (!offline) {
3031eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3032eb513658SMichael Chan 	} else {
3033eb513658SMichael Chan 		rc = bnxt_close_nic(bp, false, false);
3034eb513658SMichael Chan 		if (rc)
3035eb513658SMichael Chan 			return;
3036eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3037f7dc1ea6SMichael Chan 
3038f7dc1ea6SMichael Chan 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
3039f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, true);
3040f7dc1ea6SMichael Chan 		msleep(250);
3041f7dc1ea6SMichael Chan 		rc = bnxt_half_open_nic(bp);
3042f7dc1ea6SMichael Chan 		if (rc) {
3043f7dc1ea6SMichael Chan 			bnxt_hwrm_mac_loopback(bp, false);
3044f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3045f7dc1ea6SMichael Chan 			return;
3046f7dc1ea6SMichael Chan 		}
3047f7dc1ea6SMichael Chan 		if (bnxt_run_loopback(bp))
3048f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3049f7dc1ea6SMichael Chan 		else
3050f7dc1ea6SMichael Chan 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
3051f7dc1ea6SMichael Chan 
3052f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, false);
305355fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, true, false);
305491725d89SMichael Chan 		msleep(1000);
305591725d89SMichael Chan 		if (bnxt_run_loopback(bp)) {
305691725d89SMichael Chan 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
305791725d89SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
305891725d89SMichael Chan 		}
305955fd0cf3SMichael Chan 		if (do_ext_lpbk) {
306055fd0cf3SMichael Chan 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
306155fd0cf3SMichael Chan 			bnxt_hwrm_phy_loopback(bp, true, true);
306255fd0cf3SMichael Chan 			msleep(1000);
306355fd0cf3SMichael Chan 			if (bnxt_run_loopback(bp)) {
306455fd0cf3SMichael Chan 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
306555fd0cf3SMichael Chan 				etest->flags |= ETH_TEST_FL_FAILED;
306655fd0cf3SMichael Chan 			}
306755fd0cf3SMichael Chan 		}
306855fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, false, false);
306991725d89SMichael Chan 		bnxt_half_close_nic(bp);
3070d27e2ca1SMichael Chan 		rc = bnxt_open_nic(bp, false, true);
3071eb513658SMichael Chan 	}
3072d27e2ca1SMichael Chan 	if (rc || bnxt_test_irq(bp)) {
307367fea463SMichael Chan 		buf[BNXT_IRQ_TEST_IDX] = 1;
307467fea463SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
307567fea463SMichael Chan 	}
3076eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3077eb513658SMichael Chan 		u8 bit_val = 1 << i;
3078eb513658SMichael Chan 
3079eb513658SMichael Chan 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
3080eb513658SMichael Chan 			buf[i] = 1;
3081eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3082eb513658SMichael Chan 		}
3083eb513658SMichael Chan 	}
3084eb513658SMichael Chan }
3085eb513658SMichael Chan 
308649f7972fSVasundhara Volam static int bnxt_reset(struct net_device *dev, u32 *flags)
308749f7972fSVasundhara Volam {
308849f7972fSVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
30898cec0940SEdwin Peer 	bool reload = false;
30907a13240eSEdwin Peer 	u32 req = *flags;
30917a13240eSEdwin Peer 
30927a13240eSEdwin Peer 	if (!req)
30937a13240eSEdwin Peer 		return -EINVAL;
309449f7972fSVasundhara Volam 
309549f7972fSVasundhara Volam 	if (!BNXT_PF(bp)) {
309649f7972fSVasundhara Volam 		netdev_err(dev, "Reset is not supported from a VF\n");
309749f7972fSVasundhara Volam 		return -EOPNOTSUPP;
309849f7972fSVasundhara Volam 	}
309949f7972fSVasundhara Volam 
31000a3f4e4fSVasundhara Volam 	if (pci_vfs_assigned(bp->pdev) &&
31010a3f4e4fSVasundhara Volam 	    !(bp->fw_cap & BNXT_FW_CAP_HOT_RESET)) {
310249f7972fSVasundhara Volam 		netdev_err(dev,
310349f7972fSVasundhara Volam 			   "Reset not allowed when VFs are assigned to VMs\n");
310449f7972fSVasundhara Volam 		return -EBUSY;
310549f7972fSVasundhara Volam 	}
310649f7972fSVasundhara Volam 
31077a13240eSEdwin Peer 	if ((req & BNXT_FW_RESET_CHIP) == BNXT_FW_RESET_CHIP) {
310849f7972fSVasundhara Volam 		/* This feature is not supported in older firmware versions */
31097a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
31107a13240eSEdwin Peer 			if (!bnxt_firmware_reset_chip(dev)) {
31117a13240eSEdwin Peer 				netdev_info(dev, "Firmware reset request successful.\n");
31120a3f4e4fSVasundhara Volam 				if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET))
31138cec0940SEdwin Peer 					reload = true;
31147a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_CHIP;
31152373d8d6SScott Branden 			}
31167a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_CHIP) {
31177a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
31187a13240eSEdwin Peer 		}
31197a13240eSEdwin Peer 	}
31207a13240eSEdwin Peer 
31217a13240eSEdwin Peer 	if (req & BNXT_FW_RESET_AP) {
31226502ad59SScott Branden 		/* This feature is not supported in older firmware versions */
31237a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
31247a13240eSEdwin Peer 			if (!bnxt_firmware_reset_ap(dev)) {
31257a13240eSEdwin Peer 				netdev_info(dev, "Reset application processor successful.\n");
31268cec0940SEdwin Peer 				reload = true;
31277a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_AP;
31282373d8d6SScott Branden 			}
31297a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_AP) {
31307a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
31317a13240eSEdwin Peer 		}
313249f7972fSVasundhara Volam 	}
313349f7972fSVasundhara Volam 
31348cec0940SEdwin Peer 	if (reload)
31358cec0940SEdwin Peer 		netdev_info(dev, "Reload driver to complete reset\n");
31368cec0940SEdwin Peer 
31377a13240eSEdwin Peer 	return 0;
313849f7972fSVasundhara Volam }
313949f7972fSVasundhara Volam 
31406c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
31416c5657d0SVasundhara Volam 				  struct bnxt_hwrm_dbg_dma_info *info)
31426c5657d0SVasundhara Volam {
31436c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
31446c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_input *cmn_req = msg;
31456c5657d0SVasundhara Volam 	__le16 *seq_ptr = msg + info->seq_off;
31466c5657d0SVasundhara Volam 	u16 seq = 0, len, segs_off;
31476c5657d0SVasundhara Volam 	void *resp = cmn_resp;
31486c5657d0SVasundhara Volam 	dma_addr_t dma_handle;
31496c5657d0SVasundhara Volam 	int rc, off = 0;
31506c5657d0SVasundhara Volam 	void *dma_buf;
31516c5657d0SVasundhara Volam 
31526c5657d0SVasundhara Volam 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
31536c5657d0SVasundhara Volam 				     GFP_KERNEL);
31546c5657d0SVasundhara Volam 	if (!dma_buf)
31556c5657d0SVasundhara Volam 		return -ENOMEM;
31566c5657d0SVasundhara Volam 
31576c5657d0SVasundhara Volam 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
31586c5657d0SVasundhara Volam 			    total_segments);
31596c5657d0SVasundhara Volam 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
31606c5657d0SVasundhara Volam 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
31616c5657d0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
31626c5657d0SVasundhara Volam 	while (1) {
31636c5657d0SVasundhara Volam 		*seq_ptr = cpu_to_le16(seq);
31645b306bdeSVasundhara Volam 		rc = _hwrm_send_message(bp, msg, msg_len,
31655b306bdeSVasundhara Volam 					HWRM_COREDUMP_TIMEOUT);
31666c5657d0SVasundhara Volam 		if (rc)
31676c5657d0SVasundhara Volam 			break;
31686c5657d0SVasundhara Volam 
31696c5657d0SVasundhara Volam 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
31706c5657d0SVasundhara Volam 		if (!seq &&
31716c5657d0SVasundhara Volam 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
31726c5657d0SVasundhara Volam 			info->segs = le16_to_cpu(*((__le16 *)(resp +
31736c5657d0SVasundhara Volam 							      segs_off)));
31746c5657d0SVasundhara Volam 			if (!info->segs) {
31756c5657d0SVasundhara Volam 				rc = -EIO;
31766c5657d0SVasundhara Volam 				break;
31776c5657d0SVasundhara Volam 			}
31786c5657d0SVasundhara Volam 
31796c5657d0SVasundhara Volam 			info->dest_buf_size = info->segs *
31806c5657d0SVasundhara Volam 					sizeof(struct coredump_segment_record);
31816c5657d0SVasundhara Volam 			info->dest_buf = kmalloc(info->dest_buf_size,
31826c5657d0SVasundhara Volam 						 GFP_KERNEL);
31836c5657d0SVasundhara Volam 			if (!info->dest_buf) {
31846c5657d0SVasundhara Volam 				rc = -ENOMEM;
31856c5657d0SVasundhara Volam 				break;
31866c5657d0SVasundhara Volam 			}
31876c5657d0SVasundhara Volam 		}
31886c5657d0SVasundhara Volam 
3189c74751f4SVasundhara Volam 		if (info->dest_buf) {
3190c74751f4SVasundhara Volam 			if ((info->seg_start + off + len) <=
3191c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
31926c5657d0SVasundhara Volam 				memcpy(info->dest_buf + off, dma_buf, len);
3193c74751f4SVasundhara Volam 			} else {
3194c74751f4SVasundhara Volam 				rc = -ENOBUFS;
3195c74751f4SVasundhara Volam 				break;
3196c74751f4SVasundhara Volam 			}
3197c74751f4SVasundhara Volam 		}
31986c5657d0SVasundhara Volam 
31996c5657d0SVasundhara Volam 		if (cmn_req->req_type ==
32006c5657d0SVasundhara Volam 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
32016c5657d0SVasundhara Volam 			info->dest_buf_size += len;
32026c5657d0SVasundhara Volam 
32036c5657d0SVasundhara Volam 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
32046c5657d0SVasundhara Volam 			break;
32056c5657d0SVasundhara Volam 
32066c5657d0SVasundhara Volam 		seq++;
32076c5657d0SVasundhara Volam 		off += len;
32086c5657d0SVasundhara Volam 	}
32096c5657d0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
32106c5657d0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
32116c5657d0SVasundhara Volam 	return rc;
32126c5657d0SVasundhara Volam }
32136c5657d0SVasundhara Volam 
32146c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
32156c5657d0SVasundhara Volam 				       struct bnxt_coredump *coredump)
32166c5657d0SVasundhara Volam {
32176c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_list_input req = {0};
32186c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
32196c5657d0SVasundhara Volam 	int rc;
32206c5657d0SVasundhara Volam 
32216c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
32226c5657d0SVasundhara Volam 
32236c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_LIST_BUF_LEN;
32246c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
32256c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
32266c5657d0SVasundhara Volam 				     data_len);
32276c5657d0SVasundhara Volam 
32286c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
32296c5657d0SVasundhara Volam 	if (!rc) {
32306c5657d0SVasundhara Volam 		coredump->data = info.dest_buf;
32316c5657d0SVasundhara Volam 		coredump->data_size = info.dest_buf_size;
32326c5657d0SVasundhara Volam 		coredump->total_segs = info.segs;
32336c5657d0SVasundhara Volam 	}
32346c5657d0SVasundhara Volam 	return rc;
32356c5657d0SVasundhara Volam }
32366c5657d0SVasundhara Volam 
32376c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
32386c5657d0SVasundhara Volam 					   u16 segment_id)
32396c5657d0SVasundhara Volam {
32406c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_initiate_input req = {0};
32416c5657d0SVasundhara Volam 
32426c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
32436c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
32446c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
32456c5657d0SVasundhara Volam 
324657a8730bSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_COREDUMP_TIMEOUT);
32476c5657d0SVasundhara Volam }
32486c5657d0SVasundhara Volam 
32496c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
32506c5657d0SVasundhara Volam 					   u16 segment_id, u32 *seg_len,
3251c74751f4SVasundhara Volam 					   void *buf, u32 buf_len, u32 offset)
32526c5657d0SVasundhara Volam {
32536c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_retrieve_input req = {0};
32546c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
32556c5657d0SVasundhara Volam 	int rc;
32566c5657d0SVasundhara Volam 
32576c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
32586c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
32596c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
32606c5657d0SVasundhara Volam 
32616c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
32626c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
32636c5657d0SVasundhara Volam 				seq_no);
32646c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
32656c5657d0SVasundhara Volam 				     data_len);
3266c74751f4SVasundhara Volam 	if (buf) {
32676c5657d0SVasundhara Volam 		info.dest_buf = buf + offset;
3268c74751f4SVasundhara Volam 		info.buf_len = buf_len;
3269c74751f4SVasundhara Volam 		info.seg_start = offset;
3270c74751f4SVasundhara Volam 	}
32716c5657d0SVasundhara Volam 
32726c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
32736c5657d0SVasundhara Volam 	if (!rc)
32746c5657d0SVasundhara Volam 		*seg_len = info.dest_buf_size;
32756c5657d0SVasundhara Volam 
32766c5657d0SVasundhara Volam 	return rc;
32776c5657d0SVasundhara Volam }
32786c5657d0SVasundhara Volam 
32796c5657d0SVasundhara Volam static void
32806c5657d0SVasundhara Volam bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
32816c5657d0SVasundhara Volam 			   struct bnxt_coredump_segment_hdr *seg_hdr,
32826c5657d0SVasundhara Volam 			   struct coredump_segment_record *seg_rec, u32 seg_len,
32836c5657d0SVasundhara Volam 			   int status, u32 duration, u32 instance)
32846c5657d0SVasundhara Volam {
32856c5657d0SVasundhara Volam 	memset(seg_hdr, 0, sizeof(*seg_hdr));
32868605212aSVasundhara Volam 	memcpy(seg_hdr->signature, "sEgM", 4);
32876c5657d0SVasundhara Volam 	if (seg_rec) {
32886c5657d0SVasundhara Volam 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
32896c5657d0SVasundhara Volam 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
32906c5657d0SVasundhara Volam 		seg_hdr->low_version = seg_rec->version_low;
32916c5657d0SVasundhara Volam 		seg_hdr->high_version = seg_rec->version_hi;
32926c5657d0SVasundhara Volam 	} else {
32936c5657d0SVasundhara Volam 		/* For hwrm_ver_get response Component id = 2
32946c5657d0SVasundhara Volam 		 * and Segment id = 0
32956c5657d0SVasundhara Volam 		 */
32966c5657d0SVasundhara Volam 		seg_hdr->component_id = cpu_to_le32(2);
32976c5657d0SVasundhara Volam 		seg_hdr->segment_id = 0;
32986c5657d0SVasundhara Volam 	}
32996c5657d0SVasundhara Volam 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
33006c5657d0SVasundhara Volam 	seg_hdr->length = cpu_to_le32(seg_len);
33016c5657d0SVasundhara Volam 	seg_hdr->status = cpu_to_le32(status);
33026c5657d0SVasundhara Volam 	seg_hdr->duration = cpu_to_le32(duration);
33036c5657d0SVasundhara Volam 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
33046c5657d0SVasundhara Volam 	seg_hdr->instance = cpu_to_le32(instance);
33056c5657d0SVasundhara Volam }
33066c5657d0SVasundhara Volam 
33076c5657d0SVasundhara Volam static void
33086c5657d0SVasundhara Volam bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
33096c5657d0SVasundhara Volam 			  time64_t start, s16 start_utc, u16 total_segs,
33106c5657d0SVasundhara Volam 			  int status)
33116c5657d0SVasundhara Volam {
33126c5657d0SVasundhara Volam 	time64_t end = ktime_get_real_seconds();
33136c5657d0SVasundhara Volam 	u32 os_ver_major = 0, os_ver_minor = 0;
33146c5657d0SVasundhara Volam 	struct tm tm;
33156c5657d0SVasundhara Volam 
33166c5657d0SVasundhara Volam 	time64_to_tm(start, 0, &tm);
33176c5657d0SVasundhara Volam 	memset(record, 0, sizeof(*record));
33188605212aSVasundhara Volam 	memcpy(record->signature, "cOrE", 4);
33196c5657d0SVasundhara Volam 	record->flags = 0;
33206c5657d0SVasundhara Volam 	record->low_version = 0;
33216c5657d0SVasundhara Volam 	record->high_version = 1;
33226c5657d0SVasundhara Volam 	record->asic_state = 0;
33233d46eee5SArnd Bergmann 	strlcpy(record->system_name, utsname()->nodename,
33243d46eee5SArnd Bergmann 		sizeof(record->system_name));
33258dc5ae2dSVasundhara Volam 	record->year = cpu_to_le16(tm.tm_year + 1900);
33268dc5ae2dSVasundhara Volam 	record->month = cpu_to_le16(tm.tm_mon + 1);
33276c5657d0SVasundhara Volam 	record->day = cpu_to_le16(tm.tm_mday);
33286c5657d0SVasundhara Volam 	record->hour = cpu_to_le16(tm.tm_hour);
33296c5657d0SVasundhara Volam 	record->minute = cpu_to_le16(tm.tm_min);
33306c5657d0SVasundhara Volam 	record->second = cpu_to_le16(tm.tm_sec);
33316c5657d0SVasundhara Volam 	record->utc_bias = cpu_to_le16(start_utc);
33326c5657d0SVasundhara Volam 	strcpy(record->commandline, "ethtool -w");
33336c5657d0SVasundhara Volam 	record->total_segments = cpu_to_le32(total_segs);
33346c5657d0SVasundhara Volam 
33356c5657d0SVasundhara Volam 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
33366c5657d0SVasundhara Volam 	record->os_ver_major = cpu_to_le32(os_ver_major);
33376c5657d0SVasundhara Volam 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
33386c5657d0SVasundhara Volam 
33398605212aSVasundhara Volam 	strlcpy(record->os_name, utsname()->sysname, 32);
33406c5657d0SVasundhara Volam 	time64_to_tm(end, 0, &tm);
33416c5657d0SVasundhara Volam 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
33426c5657d0SVasundhara Volam 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
33436c5657d0SVasundhara Volam 	record->end_day = cpu_to_le16(tm.tm_mday);
33446c5657d0SVasundhara Volam 	record->end_hour = cpu_to_le16(tm.tm_hour);
33456c5657d0SVasundhara Volam 	record->end_minute = cpu_to_le16(tm.tm_min);
33466c5657d0SVasundhara Volam 	record->end_second = cpu_to_le16(tm.tm_sec);
33476c5657d0SVasundhara Volam 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
33486c5657d0SVasundhara Volam 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
33496c5657d0SVasundhara Volam 				       bp->ver_resp.chip_rev << 8 |
33506c5657d0SVasundhara Volam 				       bp->ver_resp.chip_metal);
33516c5657d0SVasundhara Volam 	record->asic_id2 = 0;
33526c5657d0SVasundhara Volam 	record->coredump_status = cpu_to_le32(status);
33536c5657d0SVasundhara Volam 	record->ioctl_low_version = 0;
33546c5657d0SVasundhara Volam 	record->ioctl_high_version = 0;
33556c5657d0SVasundhara Volam }
33566c5657d0SVasundhara Volam 
33576c5657d0SVasundhara Volam static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
33586c5657d0SVasundhara Volam {
33596c5657d0SVasundhara Volam 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
3360c74751f4SVasundhara Volam 	u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
33616c5657d0SVasundhara Volam 	struct coredump_segment_record *seg_record = NULL;
33626c5657d0SVasundhara Volam 	struct bnxt_coredump_segment_hdr seg_hdr;
33636c5657d0SVasundhara Volam 	struct bnxt_coredump coredump = {NULL};
33646c5657d0SVasundhara Volam 	time64_t start_time;
33656c5657d0SVasundhara Volam 	u16 start_utc;
33666c5657d0SVasundhara Volam 	int rc = 0, i;
33676c5657d0SVasundhara Volam 
3368c74751f4SVasundhara Volam 	if (buf)
3369c74751f4SVasundhara Volam 		buf_len = *dump_len;
3370c74751f4SVasundhara Volam 
33716c5657d0SVasundhara Volam 	start_time = ktime_get_real_seconds();
33726c5657d0SVasundhara Volam 	start_utc = sys_tz.tz_minuteswest * 60;
33736c5657d0SVasundhara Volam 	seg_hdr_len = sizeof(seg_hdr);
33746c5657d0SVasundhara Volam 
33756c5657d0SVasundhara Volam 	/* First segment should be hwrm_ver_get response */
33766c5657d0SVasundhara Volam 	*dump_len = seg_hdr_len + ver_get_resp_len;
33776c5657d0SVasundhara Volam 	if (buf) {
33786c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
33796c5657d0SVasundhara Volam 					   0, 0, 0);
33806c5657d0SVasundhara Volam 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
33816c5657d0SVasundhara Volam 		offset += seg_hdr_len;
33826c5657d0SVasundhara Volam 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
33836c5657d0SVasundhara Volam 		offset += ver_get_resp_len;
33846c5657d0SVasundhara Volam 	}
33856c5657d0SVasundhara Volam 
33866c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
33876c5657d0SVasundhara Volam 	if (rc) {
33886c5657d0SVasundhara Volam 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
33896c5657d0SVasundhara Volam 		goto err;
33906c5657d0SVasundhara Volam 	}
33916c5657d0SVasundhara Volam 
33926c5657d0SVasundhara Volam 	*dump_len += seg_hdr_len * coredump.total_segs;
33936c5657d0SVasundhara Volam 
33946c5657d0SVasundhara Volam 	seg_record = (struct coredump_segment_record *)coredump.data;
33956c5657d0SVasundhara Volam 	seg_record_len = sizeof(*seg_record);
33966c5657d0SVasundhara Volam 
33976c5657d0SVasundhara Volam 	for (i = 0; i < coredump.total_segs; i++) {
33986c5657d0SVasundhara Volam 		u16 comp_id = le16_to_cpu(seg_record->component_id);
33996c5657d0SVasundhara Volam 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
34006c5657d0SVasundhara Volam 		u32 duration = 0, seg_len = 0;
34016c5657d0SVasundhara Volam 		unsigned long start, end;
34026c5657d0SVasundhara Volam 
3403c74751f4SVasundhara Volam 		if (buf && ((offset + seg_hdr_len) >
3404c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(buf_len))) {
3405c74751f4SVasundhara Volam 			rc = -ENOBUFS;
3406c74751f4SVasundhara Volam 			goto err;
3407c74751f4SVasundhara Volam 		}
3408c74751f4SVasundhara Volam 
34096c5657d0SVasundhara Volam 		start = jiffies;
34106c5657d0SVasundhara Volam 
34116c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
34126c5657d0SVasundhara Volam 		if (rc) {
34136c5657d0SVasundhara Volam 			netdev_err(bp->dev,
34146c5657d0SVasundhara Volam 				   "Failed to initiate coredump for seg = %d\n",
34156c5657d0SVasundhara Volam 				   seg_record->segment_id);
34166c5657d0SVasundhara Volam 			goto next_seg;
34176c5657d0SVasundhara Volam 		}
34186c5657d0SVasundhara Volam 
34196c5657d0SVasundhara Volam 		/* Write segment data into the buffer */
34206c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
3421c74751f4SVasundhara Volam 						     &seg_len, buf, buf_len,
34226c5657d0SVasundhara Volam 						     offset + seg_hdr_len);
3423c74751f4SVasundhara Volam 		if (rc && rc == -ENOBUFS)
3424c74751f4SVasundhara Volam 			goto err;
3425c74751f4SVasundhara Volam 		else if (rc)
34266c5657d0SVasundhara Volam 			netdev_err(bp->dev,
34276c5657d0SVasundhara Volam 				   "Failed to retrieve coredump for seg = %d\n",
34286c5657d0SVasundhara Volam 				   seg_record->segment_id);
34296c5657d0SVasundhara Volam 
34306c5657d0SVasundhara Volam next_seg:
34316c5657d0SVasundhara Volam 		end = jiffies;
34326c5657d0SVasundhara Volam 		duration = jiffies_to_msecs(end - start);
34336c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
34346c5657d0SVasundhara Volam 					   rc, duration, 0);
34356c5657d0SVasundhara Volam 
34366c5657d0SVasundhara Volam 		if (buf) {
34376c5657d0SVasundhara Volam 			/* Write segment header into the buffer */
34386c5657d0SVasundhara Volam 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
34396c5657d0SVasundhara Volam 			offset += seg_hdr_len + seg_len;
34406c5657d0SVasundhara Volam 		}
34416c5657d0SVasundhara Volam 
34426c5657d0SVasundhara Volam 		*dump_len += seg_len;
34436c5657d0SVasundhara Volam 		seg_record =
34446c5657d0SVasundhara Volam 			(struct coredump_segment_record *)((u8 *)seg_record +
34456c5657d0SVasundhara Volam 							   seg_record_len);
34466c5657d0SVasundhara Volam 	}
34476c5657d0SVasundhara Volam 
34486c5657d0SVasundhara Volam err:
34491bbf3aedSArnd Bergmann 	if (buf)
34501bbf3aedSArnd Bergmann 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
34516c5657d0SVasundhara Volam 					  start_utc, coredump.total_segs + 1,
34526c5657d0SVasundhara Volam 					  rc);
34536c5657d0SVasundhara Volam 	kfree(coredump.data);
34541bbf3aedSArnd Bergmann 	*dump_len += sizeof(struct bnxt_coredump_record);
3455c74751f4SVasundhara Volam 	if (rc == -ENOBUFS)
34569a005c38SJonathan Lemon 		netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
34576c5657d0SVasundhara Volam 	return rc;
34586c5657d0SVasundhara Volam }
34596c5657d0SVasundhara Volam 
34600b0eacf3SVasundhara Volam static int bnxt_set_dump(struct net_device *dev, struct ethtool_dump *dump)
34610b0eacf3SVasundhara Volam {
34620b0eacf3SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
34630b0eacf3SVasundhara Volam 
34640b0eacf3SVasundhara Volam 	if (dump->flag > BNXT_DUMP_CRASH) {
34650b0eacf3SVasundhara Volam 		netdev_info(dev, "Supports only Live(0) and Crash(1) dumps.\n");
34660b0eacf3SVasundhara Volam 		return -EINVAL;
34670b0eacf3SVasundhara Volam 	}
34680b0eacf3SVasundhara Volam 
34690b0eacf3SVasundhara Volam 	if (!IS_ENABLED(CONFIG_TEE_BNXT_FW) && dump->flag == BNXT_DUMP_CRASH) {
34700b0eacf3SVasundhara Volam 		netdev_info(dev, "Cannot collect crash dump as TEE_BNXT_FW config option is not enabled.\n");
34710b0eacf3SVasundhara Volam 		return -EOPNOTSUPP;
34720b0eacf3SVasundhara Volam 	}
34730b0eacf3SVasundhara Volam 
34740b0eacf3SVasundhara Volam 	bp->dump_flag = dump->flag;
34750b0eacf3SVasundhara Volam 	return 0;
34760b0eacf3SVasundhara Volam }
34770b0eacf3SVasundhara Volam 
34786c5657d0SVasundhara Volam static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
34796c5657d0SVasundhara Volam {
34806c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
34816c5657d0SVasundhara Volam 
34826c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
34836c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
34846c5657d0SVasundhara Volam 
34856c5657d0SVasundhara Volam 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
34866c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_min_8b << 16 |
34876c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
34886c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_rsvd_8b;
34896c5657d0SVasundhara Volam 
34900b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
34910b0eacf3SVasundhara Volam 	if (bp->dump_flag == BNXT_DUMP_CRASH)
34920b0eacf3SVasundhara Volam 		dump->len = BNXT_CRASH_DUMP_LEN;
34930b0eacf3SVasundhara Volam 	else
34940b0eacf3SVasundhara Volam 		bnxt_get_coredump(bp, NULL, &dump->len);
34950b0eacf3SVasundhara Volam 	return 0;
34966c5657d0SVasundhara Volam }
34976c5657d0SVasundhara Volam 
34986c5657d0SVasundhara Volam static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
34996c5657d0SVasundhara Volam 			      void *buf)
35006c5657d0SVasundhara Volam {
35016c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35026c5657d0SVasundhara Volam 
35036c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
35046c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
35056c5657d0SVasundhara Volam 
35066c5657d0SVasundhara Volam 	memset(buf, 0, dump->len);
35076c5657d0SVasundhara Volam 
35080b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
35090b0eacf3SVasundhara Volam 	if (dump->flag == BNXT_DUMP_CRASH) {
35100b0eacf3SVasundhara Volam #ifdef CONFIG_TEE_BNXT_FW
35110b0eacf3SVasundhara Volam 		return tee_bnxt_copy_coredump(buf, 0, dump->len);
35120b0eacf3SVasundhara Volam #endif
35130b0eacf3SVasundhara Volam 	} else {
35146c5657d0SVasundhara Volam 		return bnxt_get_coredump(bp, buf, &dump->len);
35156c5657d0SVasundhara Volam 	}
35166c5657d0SVasundhara Volam 
35170b0eacf3SVasundhara Volam 	return 0;
35180b0eacf3SVasundhara Volam }
35190b0eacf3SVasundhara Volam 
3520eb513658SMichael Chan void bnxt_ethtool_init(struct bnxt *bp)
3521eb513658SMichael Chan {
3522eb513658SMichael Chan 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3523eb513658SMichael Chan 	struct hwrm_selftest_qlist_input req = {0};
3524eb513658SMichael Chan 	struct bnxt_test_info *test_info;
3525431aa1ebSMichael Chan 	struct net_device *dev = bp->dev;
3526eb513658SMichael Chan 	int i, rc;
3527eb513658SMichael Chan 
3528691aa620SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PKG_VER))
3529a60faa60SVasundhara Volam 		bnxt_get_pkgver(dev);
3530431aa1ebSMichael Chan 
3531ba642ab7SMichael Chan 	bp->num_tests = 0;
3532eb513658SMichael Chan 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp))
3533eb513658SMichael Chan 		return;
3534eb513658SMichael Chan 
3535eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3536eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3537eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3538eb513658SMichael Chan 	if (rc)
3539eb513658SMichael Chan 		goto ethtool_init_exit;
3540eb513658SMichael Chan 
3541ba642ab7SMichael Chan 	test_info = bp->test_info;
3542ba642ab7SMichael Chan 	if (!test_info)
3543eb513658SMichael Chan 		test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
3544eb513658SMichael Chan 	if (!test_info)
3545eb513658SMichael Chan 		goto ethtool_init_exit;
3546eb513658SMichael Chan 
3547eb513658SMichael Chan 	bp->test_info = test_info;
3548eb513658SMichael Chan 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
3549eb513658SMichael Chan 	if (bp->num_tests > BNXT_MAX_TEST)
3550eb513658SMichael Chan 		bp->num_tests = BNXT_MAX_TEST;
3551eb513658SMichael Chan 
3552eb513658SMichael Chan 	test_info->offline_mask = resp->offline_tests;
3553eb513658SMichael Chan 	test_info->timeout = le16_to_cpu(resp->test_timeout);
3554eb513658SMichael Chan 	if (!test_info->timeout)
3555eb513658SMichael Chan 		test_info->timeout = HWRM_CMD_TIMEOUT;
3556eb513658SMichael Chan 	for (i = 0; i < bp->num_tests; i++) {
3557eb513658SMichael Chan 		char *str = test_info->string[i];
3558eb513658SMichael Chan 		char *fw_str = resp->test0_name + i * 32;
3559eb513658SMichael Chan 
3560f7dc1ea6SMichael Chan 		if (i == BNXT_MACLPBK_TEST_IDX) {
3561f7dc1ea6SMichael Chan 			strcpy(str, "Mac loopback test (offline)");
356291725d89SMichael Chan 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
356391725d89SMichael Chan 			strcpy(str, "Phy loopback test (offline)");
356455fd0cf3SMichael Chan 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
356555fd0cf3SMichael Chan 			strcpy(str, "Ext loopback test (offline)");
356667fea463SMichael Chan 		} else if (i == BNXT_IRQ_TEST_IDX) {
356767fea463SMichael Chan 			strcpy(str, "Interrupt_test (offline)");
3568f7dc1ea6SMichael Chan 		} else {
3569eb513658SMichael Chan 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
3570eb513658SMichael Chan 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
3571eb513658SMichael Chan 			if (test_info->offline_mask & (1 << i))
3572eb513658SMichael Chan 				strncat(str, " (offline)",
3573eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3574eb513658SMichael Chan 			else
3575eb513658SMichael Chan 				strncat(str, " (online)",
3576eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3577eb513658SMichael Chan 		}
3578f7dc1ea6SMichael Chan 	}
3579eb513658SMichael Chan 
3580eb513658SMichael Chan ethtool_init_exit:
3581eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3582eb513658SMichael Chan }
3583eb513658SMichael Chan 
3584eb513658SMichael Chan void bnxt_ethtool_free(struct bnxt *bp)
3585eb513658SMichael Chan {
3586eb513658SMichael Chan 	kfree(bp->test_info);
3587eb513658SMichael Chan 	bp->test_info = NULL;
3588eb513658SMichael Chan }
3589eb513658SMichael Chan 
3590c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
3591f704d243SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3592f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES |
3593f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USECS_IRQ |
3594f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
3595f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_STATS_BLOCK_USECS |
3596f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
359700c04a92SMichael Chan 	.get_link_ksettings	= bnxt_get_link_ksettings,
359800c04a92SMichael Chan 	.set_link_ksettings	= bnxt_set_link_ksettings,
3599c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
3600c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
3601c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
36028e202366SMichael Chan 	.get_wol		= bnxt_get_wol,
36035282db6cSMichael Chan 	.set_wol		= bnxt_set_wol,
3604c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
3605c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
3606c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
3607c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
3608c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
3609c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
3610c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
3611c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
3612c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
3613c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
3614c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
3615c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
3616a011952aSMichael Chan 	.set_rxnfc		= bnxt_set_rxnfc,
3617c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
3618c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
3619c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
3620c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
3621c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
3622c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
3623c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
3624c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
362572b34f04SMichael Chan 	.get_eee		= bnxt_get_eee,
362672b34f04SMichael Chan 	.set_eee		= bnxt_set_eee,
362742ee18feSAjit Khaparde 	.get_module_info	= bnxt_get_module_info,
362842ee18feSAjit Khaparde 	.get_module_eeprom	= bnxt_get_module_eeprom,
36295ad2cbeeSMichael Chan 	.nway_reset		= bnxt_nway_reset,
36305ad2cbeeSMichael Chan 	.set_phys_id		= bnxt_set_phys_id,
3631eb513658SMichael Chan 	.self_test		= bnxt_self_test,
363249f7972fSVasundhara Volam 	.reset			= bnxt_reset,
36330b0eacf3SVasundhara Volam 	.set_dump		= bnxt_set_dump,
36346c5657d0SVasundhara Volam 	.get_dump_flag		= bnxt_get_dump_flag,
36356c5657d0SVasundhara Volam 	.get_dump_data		= bnxt_get_dump_data,
3636c0c050c5SMichael Chan };
3637