1c0c050c5SMichael Chan /* Broadcom NetXtreme-C/E network driver.
2c0c050c5SMichael Chan  *
311f15ed3SMichael Chan  * Copyright (c) 2014-2016 Broadcom Corporation
48e202366SMichael Chan  * Copyright (c) 2016-2017 Broadcom Limited
5c0c050c5SMichael Chan  *
6c0c050c5SMichael Chan  * This program is free software; you can redistribute it and/or modify
7c0c050c5SMichael Chan  * it under the terms of the GNU General Public License as published by
8c0c050c5SMichael Chan  * the Free Software Foundation.
9c0c050c5SMichael Chan  */
10c0c050c5SMichael Chan 
113ebf6f0aSRob Swindell #include <linux/ctype.h>
128ddc9aaaSMichael Chan #include <linux/stringify.h>
13c0c050c5SMichael Chan #include <linux/ethtool.h>
148b277589SMichael Chan #include <linux/linkmode.h>
15c0c050c5SMichael Chan #include <linux/interrupt.h>
16c0c050c5SMichael Chan #include <linux/pci.h>
17c0c050c5SMichael Chan #include <linux/etherdevice.h>
18c0c050c5SMichael Chan #include <linux/crc32.h>
19c0c050c5SMichael Chan #include <linux/firmware.h>
206c5657d0SVasundhara Volam #include <linux/utsname.h>
216c5657d0SVasundhara Volam #include <linux/time.h>
22118612d5SMichael Chan #include <linux/ptp_clock_kernel.h>
23118612d5SMichael Chan #include <linux/net_tstamp.h>
24118612d5SMichael Chan #include <linux/timecounter.h>
25c0c050c5SMichael Chan #include "bnxt_hsi.h"
26c0c050c5SMichael Chan #include "bnxt.h"
27*3c8c20dbSEdwin Peer #include "bnxt_hwrm.h"
28f7dc1ea6SMichael Chan #include "bnxt_xdp.h"
29118612d5SMichael Chan #include "bnxt_ptp.h"
30c0c050c5SMichael Chan #include "bnxt_ethtool.h"
31c0c050c5SMichael Chan #include "bnxt_nvm_defs.h"	/* NVRAM content constant and structure defs */
32c0c050c5SMichael Chan #include "bnxt_fw_hdr.h"	/* Firmware hdr constant and structure defs */
336c5657d0SVasundhara Volam #include "bnxt_coredump.h"
34c0c050c5SMichael Chan #define FLASH_NVRAM_TIMEOUT	((HWRM_CMD_TIMEOUT) * 100)
355ac67d8bSRob Swindell #define FLASH_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
365ac67d8bSRob Swindell #define INSTALL_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
37c0c050c5SMichael Chan 
38c0c050c5SMichael Chan static u32 bnxt_get_msglevel(struct net_device *dev)
39c0c050c5SMichael Chan {
40c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
41c0c050c5SMichael Chan 
42c0c050c5SMichael Chan 	return bp->msg_enable;
43c0c050c5SMichael Chan }
44c0c050c5SMichael Chan 
45c0c050c5SMichael Chan static void bnxt_set_msglevel(struct net_device *dev, u32 value)
46c0c050c5SMichael Chan {
47c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
48c0c050c5SMichael Chan 
49c0c050c5SMichael Chan 	bp->msg_enable = value;
50c0c050c5SMichael Chan }
51c0c050c5SMichael Chan 
52c0c050c5SMichael Chan static int bnxt_get_coalesce(struct net_device *dev,
53f3ccfda1SYufeng Mo 			     struct ethtool_coalesce *coal,
54f3ccfda1SYufeng Mo 			     struct kernel_ethtool_coalesce *kernel_coal,
55f3ccfda1SYufeng Mo 			     struct netlink_ext_ack *extack)
56c0c050c5SMichael Chan {
57c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5818775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
5918775aa8SMichael Chan 	u16 mult;
60c0c050c5SMichael Chan 
61c0c050c5SMichael Chan 	memset(coal, 0, sizeof(*coal));
62c0c050c5SMichael Chan 
636a8788f2SAndy Gospodarek 	coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM;
646a8788f2SAndy Gospodarek 
6518775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
6618775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
6718775aa8SMichael Chan 	coal->rx_coalesce_usecs = hw_coal->coal_ticks;
6818775aa8SMichael Chan 	coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6918775aa8SMichael Chan 	coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
7018775aa8SMichael Chan 	coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
71c0c050c5SMichael Chan 
7218775aa8SMichael Chan 	hw_coal = &bp->tx_coal;
7318775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
7418775aa8SMichael Chan 	coal->tx_coalesce_usecs = hw_coal->coal_ticks;
7518775aa8SMichael Chan 	coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult;
7618775aa8SMichael Chan 	coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
7718775aa8SMichael Chan 	coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
78dfc9c94aSMichael Chan 
7951f30785SMichael Chan 	coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
8051f30785SMichael Chan 
81c0c050c5SMichael Chan 	return 0;
82c0c050c5SMichael Chan }
83c0c050c5SMichael Chan 
84c0c050c5SMichael Chan static int bnxt_set_coalesce(struct net_device *dev,
85f3ccfda1SYufeng Mo 			     struct ethtool_coalesce *coal,
86f3ccfda1SYufeng Mo 			     struct kernel_ethtool_coalesce *kernel_coal,
87f3ccfda1SYufeng Mo 			     struct netlink_ext_ack *extack)
88c0c050c5SMichael Chan {
89c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
9051f30785SMichael Chan 	bool update_stats = false;
9118775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
92c0c050c5SMichael Chan 	int rc = 0;
9318775aa8SMichael Chan 	u16 mult;
94c0c050c5SMichael Chan 
956a8788f2SAndy Gospodarek 	if (coal->use_adaptive_rx_coalesce) {
966a8788f2SAndy Gospodarek 		bp->flags |= BNXT_FLAG_DIM;
976a8788f2SAndy Gospodarek 	} else {
986a8788f2SAndy Gospodarek 		if (bp->flags & BNXT_FLAG_DIM) {
996a8788f2SAndy Gospodarek 			bp->flags &= ~(BNXT_FLAG_DIM);
1006a8788f2SAndy Gospodarek 			goto reset_coalesce;
1016a8788f2SAndy Gospodarek 		}
1026a8788f2SAndy Gospodarek 	}
1036a8788f2SAndy Gospodarek 
10418775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
10518775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
10618775aa8SMichael Chan 	hw_coal->coal_ticks = coal->rx_coalesce_usecs;
10718775aa8SMichael Chan 	hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult;
10818775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq;
10918775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult;
110c0c050c5SMichael Chan 
111de4a10efSAndy Gospodarek 	hw_coal = &bp->tx_coal;
11218775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
11318775aa8SMichael Chan 	hw_coal->coal_ticks = coal->tx_coalesce_usecs;
11418775aa8SMichael Chan 	hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult;
11518775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq;
11618775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult;
117dfc9c94aSMichael Chan 
11851f30785SMichael Chan 	if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
11951f30785SMichael Chan 		u32 stats_ticks = coal->stats_block_coalesce_usecs;
12051f30785SMichael Chan 
121adcc331eSMichael Chan 		/* Allow 0, which means disable. */
122adcc331eSMichael Chan 		if (stats_ticks)
12351f30785SMichael Chan 			stats_ticks = clamp_t(u32, stats_ticks,
12451f30785SMichael Chan 					      BNXT_MIN_STATS_COAL_TICKS,
12551f30785SMichael Chan 					      BNXT_MAX_STATS_COAL_TICKS);
12651f30785SMichael Chan 		stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
12751f30785SMichael Chan 		bp->stats_coal_ticks = stats_ticks;
128e795892eSMichael Chan 		if (bp->stats_coal_ticks)
129e795892eSMichael Chan 			bp->current_interval =
130e795892eSMichael Chan 				bp->stats_coal_ticks * HZ / 1000000;
131e795892eSMichael Chan 		else
132e795892eSMichael Chan 			bp->current_interval = BNXT_TIMER_INTERVAL;
13351f30785SMichael Chan 		update_stats = true;
13451f30785SMichael Chan 	}
13551f30785SMichael Chan 
1366a8788f2SAndy Gospodarek reset_coalesce:
13751f30785SMichael Chan 	if (netif_running(dev)) {
13851f30785SMichael Chan 		if (update_stats) {
13951f30785SMichael Chan 			rc = bnxt_close_nic(bp, true, false);
14051f30785SMichael Chan 			if (!rc)
14151f30785SMichael Chan 				rc = bnxt_open_nic(bp, true, false);
14251f30785SMichael Chan 		} else {
143c0c050c5SMichael Chan 			rc = bnxt_hwrm_set_coal(bp);
14451f30785SMichael Chan 		}
14551f30785SMichael Chan 	}
146c0c050c5SMichael Chan 
147c0c050c5SMichael Chan 	return rc;
148c0c050c5SMichael Chan }
149c0c050c5SMichael Chan 
1503316d509SMichael Chan static const char * const bnxt_ring_rx_stats_str[] = {
151ee79566eSMichael Chan 	"rx_ucast_packets",
152ee79566eSMichael Chan 	"rx_mcast_packets",
153ee79566eSMichael Chan 	"rx_bcast_packets",
154ee79566eSMichael Chan 	"rx_discards",
155bfc6e5fbSMichael Chan 	"rx_errors",
156ee79566eSMichael Chan 	"rx_ucast_bytes",
157ee79566eSMichael Chan 	"rx_mcast_bytes",
158ee79566eSMichael Chan 	"rx_bcast_bytes",
1593316d509SMichael Chan };
1603316d509SMichael Chan 
1613316d509SMichael Chan static const char * const bnxt_ring_tx_stats_str[] = {
162ee79566eSMichael Chan 	"tx_ucast_packets",
163ee79566eSMichael Chan 	"tx_mcast_packets",
164ee79566eSMichael Chan 	"tx_bcast_packets",
165bfc6e5fbSMichael Chan 	"tx_errors",
166ee79566eSMichael Chan 	"tx_discards",
167ee79566eSMichael Chan 	"tx_ucast_bytes",
168ee79566eSMichael Chan 	"tx_mcast_bytes",
169ee79566eSMichael Chan 	"tx_bcast_bytes",
170ee79566eSMichael Chan };
171ee79566eSMichael Chan 
172ee79566eSMichael Chan static const char * const bnxt_ring_tpa_stats_str[] = {
173ee79566eSMichael Chan 	"tpa_packets",
174ee79566eSMichael Chan 	"tpa_bytes",
175ee79566eSMichael Chan 	"tpa_events",
176ee79566eSMichael Chan 	"tpa_aborts",
177ee79566eSMichael Chan };
178ee79566eSMichael Chan 
17978e7b866SMichael Chan static const char * const bnxt_ring_tpa2_stats_str[] = {
18078e7b866SMichael Chan 	"rx_tpa_eligible_pkt",
18178e7b866SMichael Chan 	"rx_tpa_eligible_bytes",
18278e7b866SMichael Chan 	"rx_tpa_pkt",
18378e7b866SMichael Chan 	"rx_tpa_bytes",
18478e7b866SMichael Chan 	"rx_tpa_errors",
1859d6b648cSMichael Chan 	"rx_tpa_events",
18678e7b866SMichael Chan };
18778e7b866SMichael Chan 
1889d8b5f05SMichael Chan static const char * const bnxt_rx_sw_stats_str[] = {
189ee79566eSMichael Chan 	"rx_l4_csum_errors",
1908a27d4b9SMichael Chan 	"rx_resets",
19119b3751fSMichael Chan 	"rx_buf_errors",
1929d8b5f05SMichael Chan };
1939d8b5f05SMichael Chan 
1949d8b5f05SMichael Chan static const char * const bnxt_cmn_sw_stats_str[] = {
195ee79566eSMichael Chan 	"missed_irqs",
196ee79566eSMichael Chan };
197c0c050c5SMichael Chan 
1988ddc9aaaSMichael Chan #define BNXT_RX_STATS_ENTRY(counter)	\
1998ddc9aaaSMichael Chan 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
2008ddc9aaaSMichael Chan 
2018ddc9aaaSMichael Chan #define BNXT_TX_STATS_ENTRY(counter)	\
2028ddc9aaaSMichael Chan 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
2038ddc9aaaSMichael Chan 
20400db3cbaSVasundhara Volam #define BNXT_RX_STATS_EXT_ENTRY(counter)	\
20500db3cbaSVasundhara Volam 	{ BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
20600db3cbaSVasundhara Volam 
20736e53349SMichael Chan #define BNXT_TX_STATS_EXT_ENTRY(counter)	\
20836e53349SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter), __stringify(counter) }
20936e53349SMichael Chan 
21036e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRY(n)				\
21136e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_duration_us),	\
21236e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_transitions)
21336e53349SMichael Chan 
21436e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRY(n)				\
21536e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_duration_us),	\
21636e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_transitions)
21736e53349SMichael Chan 
21836e53349SMichael Chan #define BNXT_RX_STATS_EXT_PFC_ENTRIES				\
21936e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(0),				\
22036e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(1),				\
22136e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(2),				\
22236e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(3),				\
22336e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(4),				\
22436e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(5),				\
22536e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(6),				\
22636e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRY(7)
22736e53349SMichael Chan 
22836e53349SMichael Chan #define BNXT_TX_STATS_EXT_PFC_ENTRIES				\
22936e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(0),				\
23036e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(1),				\
23136e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(2),				\
23236e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(3),				\
23336e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(4),				\
23436e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(5),				\
23536e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(6),				\
23636e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRY(7)
23736e53349SMichael Chan 
23836e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRY(n)				\
23936e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bytes_cos##n),		\
24036e53349SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_packets_cos##n)
24136e53349SMichael Chan 
24236e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRY(n)				\
24336e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_bytes_cos##n),		\
24436e53349SMichael Chan 	BNXT_TX_STATS_EXT_ENTRY(tx_packets_cos##n)
24536e53349SMichael Chan 
24636e53349SMichael Chan #define BNXT_RX_STATS_EXT_COS_ENTRIES				\
24736e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(0),				\
24836e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(1),				\
24936e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(2),				\
25036e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(3),				\
25136e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(4),				\
25236e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(5),				\
25336e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(6),				\
25436e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRY(7)				\
25536e53349SMichael Chan 
25636e53349SMichael Chan #define BNXT_TX_STATS_EXT_COS_ENTRIES				\
25736e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(0),				\
25836e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(1),				\
25936e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(2),				\
26036e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(3),				\
26136e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(4),				\
26236e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(5),				\
26336e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(6),				\
26436e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRY(7)				\
26536e53349SMichael Chan 
2662792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(n)			\
2672792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_bytes_cos##n),	\
2682792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_packets_cos##n)
2692792b5b9SMichael Chan 
2702792b5b9SMichael Chan #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES				\
2712792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(0),				\
2722792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(1),				\
2732792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(2),				\
2742792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(3),				\
2752792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(4),				\
2762792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(5),				\
2772792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(6),				\
2782792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(7)
2792792b5b9SMichael Chan 
280e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRY(counter, n)		\
281e37fed79SMichael Chan 	{ BNXT_RX_STATS_EXT_OFFSET(counter##_cos0),	\
282e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
283e37fed79SMichael Chan 
284e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRY(counter, n)		\
285e37fed79SMichael Chan 	{ BNXT_TX_STATS_EXT_OFFSET(counter##_cos0),	\
286e37fed79SMichael Chan 	  __stringify(counter##_pri##n) }
287e37fed79SMichael Chan 
288e37fed79SMichael Chan #define BNXT_RX_STATS_PRI_ENTRIES(counter)		\
289e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 0),		\
290e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 1),		\
291e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 2),		\
292e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 3),		\
293e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 4),		\
294e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 5),		\
295e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 6),		\
296e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRY(counter, 7)
297e37fed79SMichael Chan 
298e37fed79SMichael Chan #define BNXT_TX_STATS_PRI_ENTRIES(counter)		\
299e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 0),		\
300e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 1),		\
301e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 2),		\
302e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 3),		\
303e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 4),		\
304e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 5),		\
305e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 6),		\
306e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRY(counter, 7)
307e37fed79SMichael Chan 
30820c1d28eSVasundhara Volam enum {
30920c1d28eSVasundhara Volam 	RX_TOTAL_DISCARDS,
31020c1d28eSVasundhara Volam 	TX_TOTAL_DISCARDS,
31140bedf7cSJakub Kicinski 	RX_NETPOLL_DISCARDS,
31220c1d28eSVasundhara Volam };
31320c1d28eSVasundhara Volam 
31420c1d28eSVasundhara Volam static struct {
31520c1d28eSVasundhara Volam 	u64			counter;
31620c1d28eSVasundhara Volam 	char			string[ETH_GSTRING_LEN];
31720c1d28eSVasundhara Volam } bnxt_sw_func_stats[] = {
31820c1d28eSVasundhara Volam 	{0, "rx_total_discard_pkts"},
31920c1d28eSVasundhara Volam 	{0, "tx_total_discard_pkts"},
32040bedf7cSJakub Kicinski 	{0, "rx_total_netpoll_discards"},
32120c1d28eSVasundhara Volam };
32220c1d28eSVasundhara Volam 
3233316d509SMichael Chan #define NUM_RING_RX_SW_STATS		ARRAY_SIZE(bnxt_rx_sw_stats_str)
3243316d509SMichael Chan #define NUM_RING_CMN_SW_STATS		ARRAY_SIZE(bnxt_cmn_sw_stats_str)
3253316d509SMichael Chan #define NUM_RING_RX_HW_STATS		ARRAY_SIZE(bnxt_ring_rx_stats_str)
3263316d509SMichael Chan #define NUM_RING_TX_HW_STATS		ARRAY_SIZE(bnxt_ring_tx_stats_str)
3273316d509SMichael Chan 
3288ddc9aaaSMichael Chan static const struct {
3298ddc9aaaSMichael Chan 	long offset;
3308ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
3318ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
3328ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
3338ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
3348ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
3358ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
3368ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
3376fc92c33SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
3388ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
3398ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
3408ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
3418ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
3428ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
3438ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
3448ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
3458ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
3468ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
3478ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
3488ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
3498ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
3508ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
3518ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
3528ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
3538ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
3548ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
3558ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
3568ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
3578ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
358c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
359c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
360c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
361c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
362c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
363c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
364c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
365c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
3668ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
3678ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
3688ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
3698ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
3708ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
3718ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
372699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
373699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_err),
3748ddc9aaaSMichael Chan 
3758ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
3768ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
3778ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
3788ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
3798ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
3806fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
3818ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
3826fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
3838ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
3848ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
3858ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
3868ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
3878ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
3888ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
3898ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
3908ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
3918ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
3928ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
3938ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
3948ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
3958ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
3968ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
397c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
398c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
399c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
400c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
401c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
402c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
403c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
404c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
4058ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
4068ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
4078ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
4088ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
409699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
410699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
411699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_error),
4128ddc9aaaSMichael Chan };
4138ddc9aaaSMichael Chan 
41400db3cbaSVasundhara Volam static const struct {
41500db3cbaSVasundhara Volam 	long offset;
41600db3cbaSVasundhara Volam 	char string[ETH_GSTRING_LEN];
41700db3cbaSVasundhara Volam } bnxt_port_stats_ext_arr[] = {
41800db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
41900db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
42000db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
42100db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
42200db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
42336e53349SMichael Chan 	BNXT_RX_STATS_EXT_COS_ENTRIES,
42436e53349SMichael Chan 	BNXT_RX_STATS_EXT_PFC_ENTRIES,
4254a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_bits),
4264a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_buffer_passed_threshold),
4274a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_pcs_symbol_err),
4284a50ddc2SMichael Chan 	BNXT_RX_STATS_EXT_ENTRY(rx_corrected_bits),
4292792b5b9SMichael Chan 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES,
43036e53349SMichael Chan };
43136e53349SMichael Chan 
43236e53349SMichael Chan static const struct {
43336e53349SMichael Chan 	long offset;
43436e53349SMichael Chan 	char string[ETH_GSTRING_LEN];
43536e53349SMichael Chan } bnxt_tx_port_stats_ext_arr[] = {
43636e53349SMichael Chan 	BNXT_TX_STATS_EXT_COS_ENTRIES,
43736e53349SMichael Chan 	BNXT_TX_STATS_EXT_PFC_ENTRIES,
43800db3cbaSVasundhara Volam };
43900db3cbaSVasundhara Volam 
440e37fed79SMichael Chan static const struct {
441e37fed79SMichael Chan 	long base_off;
442e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
443e37fed79SMichael Chan } bnxt_rx_bytes_pri_arr[] = {
444e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_bytes),
445e37fed79SMichael Chan };
446e37fed79SMichael Chan 
447e37fed79SMichael Chan static const struct {
448e37fed79SMichael Chan 	long base_off;
449e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
450e37fed79SMichael Chan } bnxt_rx_pkts_pri_arr[] = {
451e37fed79SMichael Chan 	BNXT_RX_STATS_PRI_ENTRIES(rx_packets),
452e37fed79SMichael Chan };
453e37fed79SMichael Chan 
454e37fed79SMichael Chan static const struct {
455e37fed79SMichael Chan 	long base_off;
456e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
457e37fed79SMichael Chan } bnxt_tx_bytes_pri_arr[] = {
458e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_bytes),
459e37fed79SMichael Chan };
460e37fed79SMichael Chan 
461e37fed79SMichael Chan static const struct {
462e37fed79SMichael Chan 	long base_off;
463e37fed79SMichael Chan 	char string[ETH_GSTRING_LEN];
464e37fed79SMichael Chan } bnxt_tx_pkts_pri_arr[] = {
465e37fed79SMichael Chan 	BNXT_TX_STATS_PRI_ENTRIES(tx_packets),
466e37fed79SMichael Chan };
467e37fed79SMichael Chan 
46820c1d28eSVasundhara Volam #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
4698ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
470e37fed79SMichael Chan #define BNXT_NUM_STATS_PRI			\
471e37fed79SMichael Chan 	(ARRAY_SIZE(bnxt_rx_bytes_pri_arr) +	\
472e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_rx_pkts_pri_arr) +	\
473e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_bytes_pri_arr) +	\
474e37fed79SMichael Chan 	 ARRAY_SIZE(bnxt_tx_pkts_pri_arr))
4758ddc9aaaSMichael Chan 
47678e7b866SMichael Chan static int bnxt_get_num_tpa_ring_stats(struct bnxt *bp)
47778e7b866SMichael Chan {
47878e7b866SMichael Chan 	if (BNXT_SUPPORTS_TPA(bp)) {
4799d6b648cSMichael Chan 		if (bp->max_tpa_v2) {
4809d6b648cSMichael Chan 			if (BNXT_CHIP_P5_THOR(bp))
4819d6b648cSMichael Chan 				return BNXT_NUM_TPA_RING_STATS_P5;
4829d6b648cSMichael Chan 			return BNXT_NUM_TPA_RING_STATS_P5_SR2;
4839d6b648cSMichael Chan 		}
4849d6b648cSMichael Chan 		return BNXT_NUM_TPA_RING_STATS;
48578e7b866SMichael Chan 	}
48678e7b866SMichael Chan 	return 0;
48778e7b866SMichael Chan }
48878e7b866SMichael Chan 
489ee79566eSMichael Chan static int bnxt_get_num_ring_stats(struct bnxt *bp)
490ee79566eSMichael Chan {
4913316d509SMichael Chan 	int rx, tx, cmn;
492ee79566eSMichael Chan 
4933316d509SMichael Chan 	rx = NUM_RING_RX_HW_STATS + NUM_RING_RX_SW_STATS +
49478e7b866SMichael Chan 	     bnxt_get_num_tpa_ring_stats(bp);
4953316d509SMichael Chan 	tx = NUM_RING_TX_HW_STATS;
4963316d509SMichael Chan 	cmn = NUM_RING_CMN_SW_STATS;
497125592fbSRajesh Ravi 	return rx * bp->rx_nr_rings + tx * bp->tx_nr_rings +
498125592fbSRajesh Ravi 	       cmn * bp->cp_nr_rings;
499ee79566eSMichael Chan }
500ee79566eSMichael Chan 
5015c8227d0SMichael Chan static int bnxt_get_num_stats(struct bnxt *bp)
502c0c050c5SMichael Chan {
503ee79566eSMichael Chan 	int num_stats = bnxt_get_num_ring_stats(bp);
5048ddc9aaaSMichael Chan 
50520c1d28eSVasundhara Volam 	num_stats += BNXT_NUM_SW_FUNC_STATS;
50620c1d28eSVasundhara Volam 
5078ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS)
5088ddc9aaaSMichael Chan 		num_stats += BNXT_NUM_PORT_STATS;
5098ddc9aaaSMichael Chan 
510e37fed79SMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
51136e53349SMichael Chan 		num_stats += bp->fw_rx_stats_ext_size +
51236e53349SMichael Chan 			     bp->fw_tx_stats_ext_size;
513e37fed79SMichael Chan 		if (bp->pri2cos_valid)
514e37fed79SMichael Chan 			num_stats += BNXT_NUM_STATS_PRI;
515e37fed79SMichael Chan 	}
51600db3cbaSVasundhara Volam 
5178ddc9aaaSMichael Chan 	return num_stats;
5188ddc9aaaSMichael Chan }
5195c8227d0SMichael Chan 
5205c8227d0SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
5215c8227d0SMichael Chan {
5225c8227d0SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5235c8227d0SMichael Chan 
5245c8227d0SMichael Chan 	switch (sset) {
5255c8227d0SMichael Chan 	case ETH_SS_STATS:
5265c8227d0SMichael Chan 		return bnxt_get_num_stats(bp);
527eb513658SMichael Chan 	case ETH_SS_TEST:
528eb513658SMichael Chan 		if (!bp->num_tests)
529eb513658SMichael Chan 			return -EOPNOTSUPP;
530eb513658SMichael Chan 		return bp->num_tests;
531c0c050c5SMichael Chan 	default:
532c0c050c5SMichael Chan 		return -EOPNOTSUPP;
533c0c050c5SMichael Chan 	}
534c0c050c5SMichael Chan }
535c0c050c5SMichael Chan 
536125592fbSRajesh Ravi static bool is_rx_ring(struct bnxt *bp, int ring_num)
537125592fbSRajesh Ravi {
538125592fbSRajesh Ravi 	return ring_num < bp->rx_nr_rings;
539125592fbSRajesh Ravi }
540125592fbSRajesh Ravi 
541125592fbSRajesh Ravi static bool is_tx_ring(struct bnxt *bp, int ring_num)
542125592fbSRajesh Ravi {
543125592fbSRajesh Ravi 	int tx_base = 0;
544125592fbSRajesh Ravi 
545125592fbSRajesh Ravi 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
546125592fbSRajesh Ravi 		tx_base = bp->rx_nr_rings;
547125592fbSRajesh Ravi 
548125592fbSRajesh Ravi 	if (ring_num >= tx_base && ring_num < (tx_base + bp->tx_nr_rings))
549125592fbSRajesh Ravi 		return true;
550125592fbSRajesh Ravi 	return false;
551125592fbSRajesh Ravi }
552125592fbSRajesh Ravi 
553c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
554c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
555c0c050c5SMichael Chan {
556c0c050c5SMichael Chan 	u32 i, j = 0;
557c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
558125592fbSRajesh Ravi 	u32 tpa_stats;
559c0c050c5SMichael Chan 
560fd3ab1c7SMichael Chan 	if (!bp->bnapi) {
561ee79566eSMichael Chan 		j += bnxt_get_num_ring_stats(bp) + BNXT_NUM_SW_FUNC_STATS;
562fd3ab1c7SMichael Chan 		goto skip_ring_stats;
563fd3ab1c7SMichael Chan 	}
564c0c050c5SMichael Chan 
56520c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
56620c1d28eSVasundhara Volam 		bnxt_sw_func_stats[i].counter = 0;
56720c1d28eSVasundhara Volam 
568125592fbSRajesh Ravi 	tpa_stats = bnxt_get_num_tpa_ring_stats(bp);
569c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
570c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
571c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
572a0c30621SMichael Chan 		u64 *sw_stats = cpr->stats.sw_stats;
5739d8b5f05SMichael Chan 		u64 *sw;
574c0c050c5SMichael Chan 		int k;
575c0c050c5SMichael Chan 
576125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
577125592fbSRajesh Ravi 			for (k = 0; k < NUM_RING_RX_HW_STATS; j++, k++)
578a0c30621SMichael Chan 				buf[j] = sw_stats[k];
579125592fbSRajesh Ravi 		}
580125592fbSRajesh Ravi 		if (is_tx_ring(bp, i)) {
581125592fbSRajesh Ravi 			k = NUM_RING_RX_HW_STATS;
582125592fbSRajesh Ravi 			for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
583125592fbSRajesh Ravi 			       j++, k++)
584a0c30621SMichael Chan 				buf[j] = sw_stats[k];
585125592fbSRajesh Ravi 		}
586125592fbSRajesh Ravi 		if (!tpa_stats || !is_rx_ring(bp, i))
587125592fbSRajesh Ravi 			goto skip_tpa_ring_stats;
588125592fbSRajesh Ravi 
589125592fbSRajesh Ravi 		k = NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS;
590125592fbSRajesh Ravi 		for (; k < NUM_RING_RX_HW_STATS + NUM_RING_TX_HW_STATS +
591125592fbSRajesh Ravi 			   tpa_stats; j++, k++)
592a0c30621SMichael Chan 			buf[j] = sw_stats[k];
5939d8b5f05SMichael Chan 
594125592fbSRajesh Ravi skip_tpa_ring_stats:
5959d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.rx;
596125592fbSRajesh Ravi 		if (is_rx_ring(bp, i)) {
5973316d509SMichael Chan 			for (k = 0; k < NUM_RING_RX_SW_STATS; j++, k++)
5989d8b5f05SMichael Chan 				buf[j] = sw[k];
599125592fbSRajesh Ravi 		}
6009d8b5f05SMichael Chan 
6019d8b5f05SMichael Chan 		sw = (u64 *)&cpr->sw_stats.cmn;
6023316d509SMichael Chan 		for (k = 0; k < NUM_RING_CMN_SW_STATS; j++, k++)
6039d8b5f05SMichael Chan 			buf[j] = sw[k];
60420c1d28eSVasundhara Volam 
60520c1d28eSVasundhara Volam 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
606a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, rx_discard_pkts);
60720c1d28eSVasundhara Volam 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
608a0c30621SMichael Chan 			BNXT_GET_RING_STATS64(sw_stats, tx_discard_pkts);
60940bedf7cSJakub Kicinski 		bnxt_sw_func_stats[RX_NETPOLL_DISCARDS].counter +=
61040bedf7cSJakub Kicinski 			cpr->sw_stats.rx.rx_netpoll_discards;
611c0c050c5SMichael Chan 	}
61220c1d28eSVasundhara Volam 
61320c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
61420c1d28eSVasundhara Volam 		buf[j] = bnxt_sw_func_stats[i].counter;
61520c1d28eSVasundhara Volam 
616fd3ab1c7SMichael Chan skip_ring_stats:
6178ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
618a0c30621SMichael Chan 		u64 *port_stats = bp->port_stats.sw_stats;
6198ddc9aaaSMichael Chan 
620a0c30621SMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++)
621a0c30621SMichael Chan 			buf[j] = *(port_stats + bnxt_port_stats_arr[i].offset);
6228ddc9aaaSMichael Chan 	}
62300db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
624a0c30621SMichael Chan 		u64 *rx_port_stats_ext = bp->rx_port_stats_ext.sw_stats;
625a0c30621SMichael Chan 		u64 *tx_port_stats_ext = bp->tx_port_stats_ext.sw_stats;
62600db3cbaSVasundhara Volam 
62736e53349SMichael Chan 		for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
628a0c30621SMichael Chan 			buf[j] = *(rx_port_stats_ext +
629a0c30621SMichael Chan 				   bnxt_port_stats_ext_arr[i].offset);
63000db3cbaSVasundhara Volam 		}
63136e53349SMichael Chan 		for (i = 0; i < bp->fw_tx_stats_ext_size; i++, j++) {
632a0c30621SMichael Chan 			buf[j] = *(tx_port_stats_ext +
633a0c30621SMichael Chan 				   bnxt_tx_port_stats_ext_arr[i].offset);
63436e53349SMichael Chan 		}
635e37fed79SMichael Chan 		if (bp->pri2cos_valid) {
636e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
637e37fed79SMichael Chan 				long n = bnxt_rx_bytes_pri_arr[i].base_off +
638a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
639e37fed79SMichael Chan 
640a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
641e37fed79SMichael Chan 			}
642e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
643e37fed79SMichael Chan 				long n = bnxt_rx_pkts_pri_arr[i].base_off +
644a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
645e37fed79SMichael Chan 
646a0c30621SMichael Chan 				buf[j] = *(rx_port_stats_ext + n);
647e37fed79SMichael Chan 			}
648e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
649e37fed79SMichael Chan 				long n = bnxt_tx_bytes_pri_arr[i].base_off +
650a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
651e37fed79SMichael Chan 
652a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
653e37fed79SMichael Chan 			}
654e37fed79SMichael Chan 			for (i = 0; i < 8; i++, j++) {
655e37fed79SMichael Chan 				long n = bnxt_tx_pkts_pri_arr[i].base_off +
656a24ec322SMichael Chan 					 bp->pri2cos_idx[i];
657e37fed79SMichael Chan 
658a0c30621SMichael Chan 				buf[j] = *(tx_port_stats_ext + n);
659e37fed79SMichael Chan 			}
660e37fed79SMichael Chan 		}
66100db3cbaSVasundhara Volam 	}
662c0c050c5SMichael Chan }
663c0c050c5SMichael Chan 
664c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
665c0c050c5SMichael Chan {
666c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
66778e7b866SMichael Chan 	static const char * const *str;
668ee79566eSMichael Chan 	u32 i, j, num_str;
669c0c050c5SMichael Chan 
670c0c050c5SMichael Chan 	switch (stringset) {
671c0c050c5SMichael Chan 	case ETH_SS_STATS:
672c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
673125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
6743316d509SMichael Chan 				num_str = NUM_RING_RX_HW_STATS;
675ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
676ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
6773316d509SMichael Chan 						bnxt_ring_rx_stats_str[j]);
678c0c050c5SMichael Chan 					buf += ETH_GSTRING_LEN;
679ee79566eSMichael Chan 				}
680125592fbSRajesh Ravi 			}
681125592fbSRajesh Ravi 			if (is_tx_ring(bp, i)) {
6823316d509SMichael Chan 				num_str = NUM_RING_TX_HW_STATS;
6833316d509SMichael Chan 				for (j = 0; j < num_str; j++) {
6843316d509SMichael Chan 					sprintf(buf, "[%d]: %s", i,
6853316d509SMichael Chan 						bnxt_ring_tx_stats_str[j]);
6863316d509SMichael Chan 					buf += ETH_GSTRING_LEN;
6873316d509SMichael Chan 				}
688125592fbSRajesh Ravi 			}
6893316d509SMichael Chan 			num_str = bnxt_get_num_tpa_ring_stats(bp);
690125592fbSRajesh Ravi 			if (!num_str || !is_rx_ring(bp, i))
691ee79566eSMichael Chan 				goto skip_tpa_stats;
692ee79566eSMichael Chan 
6933316d509SMichael Chan 			if (bp->max_tpa_v2)
69478e7b866SMichael Chan 				str = bnxt_ring_tpa2_stats_str;
6953316d509SMichael Chan 			else
69678e7b866SMichael Chan 				str = bnxt_ring_tpa_stats_str;
6973316d509SMichael Chan 
698ee79566eSMichael Chan 			for (j = 0; j < num_str; j++) {
69978e7b866SMichael Chan 				sprintf(buf, "[%d]: %s", i, str[j]);
700c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
701ee79566eSMichael Chan 			}
702ee79566eSMichael Chan skip_tpa_stats:
703125592fbSRajesh Ravi 			if (is_rx_ring(bp, i)) {
7043316d509SMichael Chan 				num_str = NUM_RING_RX_SW_STATS;
705ee79566eSMichael Chan 				for (j = 0; j < num_str; j++) {
706ee79566eSMichael Chan 					sprintf(buf, "[%d]: %s", i,
7079d8b5f05SMichael Chan 						bnxt_rx_sw_stats_str[j]);
7089d8b5f05SMichael Chan 					buf += ETH_GSTRING_LEN;
7099d8b5f05SMichael Chan 				}
710125592fbSRajesh Ravi 			}
7113316d509SMichael Chan 			num_str = NUM_RING_CMN_SW_STATS;
7129d8b5f05SMichael Chan 			for (j = 0; j < num_str; j++) {
7139d8b5f05SMichael Chan 				sprintf(buf, "[%d]: %s", i,
7149d8b5f05SMichael Chan 					bnxt_cmn_sw_stats_str[j]);
715c0c050c5SMichael Chan 				buf += ETH_GSTRING_LEN;
716ee79566eSMichael Chan 			}
717c0c050c5SMichael Chan 		}
71820c1d28eSVasundhara Volam 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
71920c1d28eSVasundhara Volam 			strcpy(buf, bnxt_sw_func_stats[i].string);
72020c1d28eSVasundhara Volam 			buf += ETH_GSTRING_LEN;
72120c1d28eSVasundhara Volam 		}
72220c1d28eSVasundhara Volam 
7238ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
7248ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
7258ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
7268ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
7278ddc9aaaSMichael Chan 			}
7288ddc9aaaSMichael Chan 		}
72900db3cbaSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
73036e53349SMichael Chan 			for (i = 0; i < bp->fw_rx_stats_ext_size; i++) {
73100db3cbaSVasundhara Volam 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
73200db3cbaSVasundhara Volam 				buf += ETH_GSTRING_LEN;
73300db3cbaSVasundhara Volam 			}
73436e53349SMichael Chan 			for (i = 0; i < bp->fw_tx_stats_ext_size; i++) {
73536e53349SMichael Chan 				strcpy(buf,
73636e53349SMichael Chan 				       bnxt_tx_port_stats_ext_arr[i].string);
73736e53349SMichael Chan 				buf += ETH_GSTRING_LEN;
73836e53349SMichael Chan 			}
739e37fed79SMichael Chan 			if (bp->pri2cos_valid) {
740e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
741e37fed79SMichael Chan 					strcpy(buf,
742e37fed79SMichael Chan 					       bnxt_rx_bytes_pri_arr[i].string);
743e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
744e37fed79SMichael Chan 				}
745e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
746e37fed79SMichael Chan 					strcpy(buf,
747e37fed79SMichael Chan 					       bnxt_rx_pkts_pri_arr[i].string);
748e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
749e37fed79SMichael Chan 				}
750e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
751e37fed79SMichael Chan 					strcpy(buf,
752e37fed79SMichael Chan 					       bnxt_tx_bytes_pri_arr[i].string);
753e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
754e37fed79SMichael Chan 				}
755e37fed79SMichael Chan 				for (i = 0; i < 8; i++) {
756e37fed79SMichael Chan 					strcpy(buf,
757e37fed79SMichael Chan 					       bnxt_tx_pkts_pri_arr[i].string);
758e37fed79SMichael Chan 					buf += ETH_GSTRING_LEN;
759e37fed79SMichael Chan 				}
760e37fed79SMichael Chan 			}
76100db3cbaSVasundhara Volam 		}
762c0c050c5SMichael Chan 		break;
763eb513658SMichael Chan 	case ETH_SS_TEST:
764eb513658SMichael Chan 		if (bp->num_tests)
765eb513658SMichael Chan 			memcpy(buf, bp->test_info->string,
766eb513658SMichael Chan 			       bp->num_tests * ETH_GSTRING_LEN);
767eb513658SMichael Chan 		break;
768c0c050c5SMichael Chan 	default:
769c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
770c0c050c5SMichael Chan 			   stringset);
771c0c050c5SMichael Chan 		break;
772c0c050c5SMichael Chan 	}
773c0c050c5SMichael Chan }
774c0c050c5SMichael Chan 
775c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
776c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
777c0c050c5SMichael Chan {
778c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
779c0c050c5SMichael Chan 
780c1129b51SMichael Chan 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
781c1129b51SMichael Chan 		ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
782c0c050c5SMichael Chan 		ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
783c1129b51SMichael Chan 	} else {
784c1129b51SMichael Chan 		ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
785c1129b51SMichael Chan 		ering->rx_jumbo_max_pending = 0;
786c1129b51SMichael Chan 	}
787c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
788c0c050c5SMichael Chan 
789c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
790c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
791c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
792c0c050c5SMichael Chan }
793c0c050c5SMichael Chan 
794c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
795c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
796c0c050c5SMichael Chan {
797c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
798c0c050c5SMichael Chan 
799c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
800c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
801c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
802c0c050c5SMichael Chan 		return -EINVAL;
803c0c050c5SMichael Chan 
804c0c050c5SMichael Chan 	if (netif_running(dev))
805c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
806c0c050c5SMichael Chan 
807c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
808c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
809c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
810c0c050c5SMichael Chan 
811c0c050c5SMichael Chan 	if (netif_running(dev))
812c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
813c0c050c5SMichael Chan 
814c0c050c5SMichael Chan 	return 0;
815c0c050c5SMichael Chan }
816c0c050c5SMichael Chan 
817c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
818c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
819c0c050c5SMichael Chan {
820c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
821db4723b3SMichael Chan 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
822c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
8234301304bSMichael Chan 	int max_tx_sch_inputs, tx_grps;
824db4723b3SMichael Chan 
825db4723b3SMichael Chan 	/* Get the most up-to-date max_tx_sch_inputs. */
826c1c2d774SPavan Chebbi 	if (netif_running(dev) && BNXT_NEW_RM(bp))
827db4723b3SMichael Chan 		bnxt_hwrm_func_resc_qcaps(bp, false);
828db4723b3SMichael Chan 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
829c0c050c5SMichael Chan 
8306e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
831db4723b3SMichael Chan 	if (max_tx_sch_inputs)
832db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
8334301304bSMichael Chan 
8344301304bSMichael Chan 	tcs = netdev_get_num_tc(dev);
8354301304bSMichael Chan 	tx_grps = max(tcs, 1);
8364301304bSMichael Chan 	if (bp->tx_nr_rings_xdp)
8374301304bSMichael Chan 		tx_grps++;
8384301304bSMichael Chan 	max_tx_rings /= tx_grps;
839a79a5276SMichael Chan 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
840068c9ec6SMichael Chan 
84118d6e4e2SSatish Baddipadige 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
84218d6e4e2SSatish Baddipadige 		max_rx_rings = 0;
84318d6e4e2SSatish Baddipadige 		max_tx_rings = 0;
84418d6e4e2SSatish Baddipadige 	}
845db4723b3SMichael Chan 	if (max_tx_sch_inputs)
846db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
84718d6e4e2SSatish Baddipadige 
848c0c050c5SMichael Chan 	if (tcs > 1)
849c0c050c5SMichael Chan 		max_tx_rings /= tcs;
850c0c050c5SMichael Chan 
851c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
852c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
853c0c050c5SMichael Chan 	channel->max_other = 0;
854068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
855068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
85676595193SPrashant Sreedharan 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
85776595193SPrashant Sreedharan 			channel->combined_count--;
858068c9ec6SMichael Chan 	} else {
85976595193SPrashant Sreedharan 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
860c0c050c5SMichael Chan 			channel->rx_count = bp->rx_nr_rings;
861c0c050c5SMichael Chan 			channel->tx_count = bp->tx_nr_rings_per_tc;
862c0c050c5SMichael Chan 		}
863068c9ec6SMichael Chan 	}
86476595193SPrashant Sreedharan }
865c0c050c5SMichael Chan 
866c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
867c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
868c0c050c5SMichael Chan {
869c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
870d1e7925eSMichael Chan 	int req_tx_rings, req_rx_rings, tcs;
871068c9ec6SMichael Chan 	bool sh = false;
8725f449249SMichael Chan 	int tx_xdp = 0;
873d1e7925eSMichael Chan 	int rc = 0;
874c0c050c5SMichael Chan 
875068c9ec6SMichael Chan 	if (channel->other_count)
876c0c050c5SMichael Chan 		return -EINVAL;
877c0c050c5SMichael Chan 
878068c9ec6SMichael Chan 	if (!channel->combined_count &&
879068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
880068c9ec6SMichael Chan 		return -EINVAL;
881068c9ec6SMichael Chan 
882068c9ec6SMichael Chan 	if (channel->combined_count &&
883068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
884068c9ec6SMichael Chan 		return -EINVAL;
885068c9ec6SMichael Chan 
88676595193SPrashant Sreedharan 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
88776595193SPrashant Sreedharan 					    channel->tx_count))
88876595193SPrashant Sreedharan 		return -EINVAL;
88976595193SPrashant Sreedharan 
890068c9ec6SMichael Chan 	if (channel->combined_count)
891068c9ec6SMichael Chan 		sh = true;
892068c9ec6SMichael Chan 
893c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
894c0c050c5SMichael Chan 
895391be5c2SMichael Chan 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
896d1e7925eSMichael Chan 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
8975f449249SMichael Chan 	if (bp->tx_nr_rings_xdp) {
8985f449249SMichael Chan 		if (!sh) {
8995f449249SMichael Chan 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
9005f449249SMichael Chan 			return -EINVAL;
9015f449249SMichael Chan 		}
9025f449249SMichael Chan 		tx_xdp = req_rx_rings;
9035f449249SMichael Chan 	}
90498fdbe73SMichael Chan 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
905d1e7925eSMichael Chan 	if (rc) {
906d1e7925eSMichael Chan 		netdev_warn(dev, "Unable to allocate the requested rings\n");
907d1e7925eSMichael Chan 		return rc;
908391be5c2SMichael Chan 	}
909391be5c2SMichael Chan 
910bd3191b5SMichael Chan 	if (bnxt_get_nr_rss_ctxs(bp, req_rx_rings) !=
911bd3191b5SMichael Chan 	    bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) &&
912bd3191b5SMichael Chan 	    (dev->priv_flags & IFF_RXFH_CONFIGURED)) {
913bd3191b5SMichael Chan 		netdev_warn(dev, "RSS table size change required, RSS table entries must be default to proceed\n");
914bd3191b5SMichael Chan 		return -EINVAL;
915bd3191b5SMichael Chan 	}
916bd3191b5SMichael Chan 
917c0c050c5SMichael Chan 	if (netif_running(dev)) {
918c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
919c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
920c0c050c5SMichael Chan 			 * before PF unload
921c0c050c5SMichael Chan 			 */
922c0c050c5SMichael Chan 		}
923c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
924c0c050c5SMichael Chan 		if (rc) {
925c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
926c0c050c5SMichael Chan 				   rc);
927c0c050c5SMichael Chan 			return rc;
928c0c050c5SMichael Chan 		}
929c0c050c5SMichael Chan 	}
930c0c050c5SMichael Chan 
931068c9ec6SMichael Chan 	if (sh) {
932068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
933d1e7925eSMichael Chan 		bp->rx_nr_rings = channel->combined_count;
934d1e7925eSMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
935068c9ec6SMichael Chan 	} else {
936068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
937c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
938c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
939068c9ec6SMichael Chan 	}
9405f449249SMichael Chan 	bp->tx_nr_rings_xdp = tx_xdp;
9415f449249SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
942c0c050c5SMichael Chan 	if (tcs > 1)
9435f449249SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
944068c9ec6SMichael Chan 
945068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
946068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
947068c9ec6SMichael Chan 
9482bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
9492bcfa6f6SMichael Chan 	netdev_update_features(dev);
950c0c050c5SMichael Chan 	if (netif_running(dev)) {
951c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
952c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
953c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
954c0c050c5SMichael Chan 			 * to renable
955c0c050c5SMichael Chan 			 */
956c0c050c5SMichael Chan 		}
957d8c09f19SMichael Chan 	} else {
9581b3f0b75SMichael Chan 		rc = bnxt_reserve_rings(bp, true);
959c0c050c5SMichael Chan 	}
960c0c050c5SMichael Chan 
961c0c050c5SMichael Chan 	return rc;
962c0c050c5SMichael Chan }
963c0c050c5SMichael Chan 
964c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
965c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
966c0c050c5SMichael Chan 			    u32 *rule_locs)
967c0c050c5SMichael Chan {
968c0c050c5SMichael Chan 	int i, j = 0;
969c0c050c5SMichael Chan 
970c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
971c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
972c0c050c5SMichael Chan 		struct hlist_head *head;
973c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
974c0c050c5SMichael Chan 
975c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
976c0c050c5SMichael Chan 		rcu_read_lock();
977c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
978c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
979c0c050c5SMichael Chan 				break;
980c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
981c0c050c5SMichael Chan 		}
982c0c050c5SMichael Chan 		rcu_read_unlock();
983c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
984c0c050c5SMichael Chan 			break;
985c0c050c5SMichael Chan 	}
986c0c050c5SMichael Chan 	cmd->rule_cnt = j;
987c0c050c5SMichael Chan 	return 0;
988c0c050c5SMichael Chan }
989c0c050c5SMichael Chan 
990c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
991c0c050c5SMichael Chan {
992c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
993c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
994c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
995c0c050c5SMichael Chan 	struct flow_keys *fkeys;
996c0c050c5SMichael Chan 	int i, rc = -EINVAL;
997c0c050c5SMichael Chan 
998b721cfafSstephen hemminger 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
999c0c050c5SMichael Chan 		return rc;
1000c0c050c5SMichael Chan 
1001c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
1002c0c050c5SMichael Chan 		struct hlist_head *head;
1003c0c050c5SMichael Chan 
1004c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
1005c0c050c5SMichael Chan 		rcu_read_lock();
1006c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
1007c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
1008c0c050c5SMichael Chan 				goto fltr_found;
1009c0c050c5SMichael Chan 		}
1010c0c050c5SMichael Chan 		rcu_read_unlock();
1011c0c050c5SMichael Chan 	}
1012c0c050c5SMichael Chan 	return rc;
1013c0c050c5SMichael Chan 
1014c0c050c5SMichael Chan fltr_found:
1015c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
1016dda0e746SMichael Chan 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
1017c0c050c5SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1018c0c050c5SMichael Chan 			fs->flow_type = TCP_V4_FLOW;
1019c0c050c5SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1020c0c050c5SMichael Chan 			fs->flow_type = UDP_V4_FLOW;
1021c0c050c5SMichael Chan 		else
1022c0c050c5SMichael Chan 			goto fltr_err;
1023c0c050c5SMichael Chan 
1024c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
1025c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
1026c0c050c5SMichael Chan 
1027c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
1028c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
1029c0c050c5SMichael Chan 
1030c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
1031c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
1032c0c050c5SMichael Chan 
1033c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
1034c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
1035dda0e746SMichael Chan 	} else {
1036dda0e746SMichael Chan 		int i;
1037dda0e746SMichael Chan 
1038dda0e746SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
1039dda0e746SMichael Chan 			fs->flow_type = TCP_V6_FLOW;
1040dda0e746SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
1041dda0e746SMichael Chan 			fs->flow_type = UDP_V6_FLOW;
1042dda0e746SMichael Chan 		else
1043dda0e746SMichael Chan 			goto fltr_err;
1044dda0e746SMichael Chan 
1045dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
1046dda0e746SMichael Chan 			fkeys->addrs.v6addrs.src;
1047dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
1048dda0e746SMichael Chan 			fkeys->addrs.v6addrs.dst;
1049dda0e746SMichael Chan 		for (i = 0; i < 4; i++) {
1050dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
1051dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
1052dda0e746SMichael Chan 		}
1053dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
1054dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
1055dda0e746SMichael Chan 
1056dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
1057dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
1058dda0e746SMichael Chan 	}
1059c0c050c5SMichael Chan 
1060c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
1061c0c050c5SMichael Chan 	rc = 0;
1062c0c050c5SMichael Chan 
1063c0c050c5SMichael Chan fltr_err:
1064c0c050c5SMichael Chan 	rcu_read_unlock();
1065c0c050c5SMichael Chan 
1066c0c050c5SMichael Chan 	return rc;
1067c0c050c5SMichael Chan }
1068a011952aSMichael Chan #endif
1069a011952aSMichael Chan 
1070a011952aSMichael Chan static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
1071a011952aSMichael Chan {
1072a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
1073a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1074a011952aSMichael Chan 	return 0;
1075a011952aSMichael Chan }
1076a011952aSMichael Chan 
1077a011952aSMichael Chan static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
1078a011952aSMichael Chan {
1079a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
1080a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
1081a011952aSMichael Chan 	return 0;
1082a011952aSMichael Chan }
1083a011952aSMichael Chan 
1084a011952aSMichael Chan static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1085a011952aSMichael Chan {
1086a011952aSMichael Chan 	cmd->data = 0;
1087a011952aSMichael Chan 	switch (cmd->flow_type) {
1088a011952aSMichael Chan 	case TCP_V4_FLOW:
1089a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
1090a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1091a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1092a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1093a011952aSMichael Chan 		break;
1094a011952aSMichael Chan 	case UDP_V4_FLOW:
1095a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
1096a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1097a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1098df561f66SGustavo A. R. Silva 		fallthrough;
1099a011952aSMichael Chan 	case SCTP_V4_FLOW:
1100a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1101a011952aSMichael Chan 	case AH_V4_FLOW:
1102a011952aSMichael Chan 	case ESP_V4_FLOW:
1103a011952aSMichael Chan 	case IPV4_FLOW:
1104a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
1105a011952aSMichael Chan 		break;
1106a011952aSMichael Chan 
1107a011952aSMichael Chan 	case TCP_V6_FLOW:
1108a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
1109a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1110a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1111a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1112a011952aSMichael Chan 		break;
1113a011952aSMichael Chan 	case UDP_V6_FLOW:
1114a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
1115a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1116a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1117df561f66SGustavo A. R. Silva 		fallthrough;
1118a011952aSMichael Chan 	case SCTP_V6_FLOW:
1119a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1120a011952aSMichael Chan 	case AH_V6_FLOW:
1121a011952aSMichael Chan 	case ESP_V6_FLOW:
1122a011952aSMichael Chan 	case IPV6_FLOW:
1123a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
1124a011952aSMichael Chan 		break;
1125a011952aSMichael Chan 	}
1126a011952aSMichael Chan 	return 0;
1127a011952aSMichael Chan }
1128a011952aSMichael Chan 
1129a011952aSMichael Chan #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1130a011952aSMichael Chan #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
1131a011952aSMichael Chan 
1132a011952aSMichael Chan static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1133a011952aSMichael Chan {
1134a011952aSMichael Chan 	u32 rss_hash_cfg = bp->rss_hash_cfg;
1135a011952aSMichael Chan 	int tuple, rc = 0;
1136a011952aSMichael Chan 
1137a011952aSMichael Chan 	if (cmd->data == RXH_4TUPLE)
1138a011952aSMichael Chan 		tuple = 4;
1139a011952aSMichael Chan 	else if (cmd->data == RXH_2TUPLE)
1140a011952aSMichael Chan 		tuple = 2;
1141a011952aSMichael Chan 	else if (!cmd->data)
1142a011952aSMichael Chan 		tuple = 0;
1143a011952aSMichael Chan 	else
1144a011952aSMichael Chan 		return -EINVAL;
1145a011952aSMichael Chan 
1146a011952aSMichael Chan 	if (cmd->flow_type == TCP_V4_FLOW) {
1147a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1148a011952aSMichael Chan 		if (tuple == 4)
1149a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1150a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V4_FLOW) {
1151a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1152a011952aSMichael Chan 			return -EINVAL;
1153a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1154a011952aSMichael Chan 		if (tuple == 4)
1155a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1156a011952aSMichael Chan 	} else if (cmd->flow_type == TCP_V6_FLOW) {
1157a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1158a011952aSMichael Chan 		if (tuple == 4)
1159a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1160a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V6_FLOW) {
1161a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1162a011952aSMichael Chan 			return -EINVAL;
1163a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1164a011952aSMichael Chan 		if (tuple == 4)
1165a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1166a011952aSMichael Chan 	} else if (tuple == 4) {
1167a011952aSMichael Chan 		return -EINVAL;
1168a011952aSMichael Chan 	}
1169a011952aSMichael Chan 
1170a011952aSMichael Chan 	switch (cmd->flow_type) {
1171a011952aSMichael Chan 	case TCP_V4_FLOW:
1172a011952aSMichael Chan 	case UDP_V4_FLOW:
1173a011952aSMichael Chan 	case SCTP_V4_FLOW:
1174a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
1175a011952aSMichael Chan 	case AH_V4_FLOW:
1176a011952aSMichael Chan 	case ESP_V4_FLOW:
1177a011952aSMichael Chan 	case IPV4_FLOW:
1178a011952aSMichael Chan 		if (tuple == 2)
1179a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1180a011952aSMichael Chan 		else if (!tuple)
1181a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1182a011952aSMichael Chan 		break;
1183a011952aSMichael Chan 
1184a011952aSMichael Chan 	case TCP_V6_FLOW:
1185a011952aSMichael Chan 	case UDP_V6_FLOW:
1186a011952aSMichael Chan 	case SCTP_V6_FLOW:
1187a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
1188a011952aSMichael Chan 	case AH_V6_FLOW:
1189a011952aSMichael Chan 	case ESP_V6_FLOW:
1190a011952aSMichael Chan 	case IPV6_FLOW:
1191a011952aSMichael Chan 		if (tuple == 2)
1192a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1193a011952aSMichael Chan 		else if (!tuple)
1194a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1195a011952aSMichael Chan 		break;
1196a011952aSMichael Chan 	}
1197a011952aSMichael Chan 
1198a011952aSMichael Chan 	if (bp->rss_hash_cfg == rss_hash_cfg)
1199a011952aSMichael Chan 		return 0;
1200a011952aSMichael Chan 
1201a011952aSMichael Chan 	bp->rss_hash_cfg = rss_hash_cfg;
1202a011952aSMichael Chan 	if (netif_running(bp->dev)) {
1203a011952aSMichael Chan 		bnxt_close_nic(bp, false, false);
1204a011952aSMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1205a011952aSMichael Chan 	}
1206a011952aSMichael Chan 	return rc;
1207a011952aSMichael Chan }
1208c0c050c5SMichael Chan 
1209c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1210c0c050c5SMichael Chan 			  u32 *rule_locs)
1211c0c050c5SMichael Chan {
1212c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1213c0c050c5SMichael Chan 	int rc = 0;
1214c0c050c5SMichael Chan 
1215c0c050c5SMichael Chan 	switch (cmd->cmd) {
1216a011952aSMichael Chan #ifdef CONFIG_RFS_ACCEL
1217c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
1218c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
1219c0c050c5SMichael Chan 		break;
1220c0c050c5SMichael Chan 
1221c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
1222c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
1223c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
1224c0c050c5SMichael Chan 		break;
1225c0c050c5SMichael Chan 
1226c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
1227c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
1228c0c050c5SMichael Chan 		break;
1229c0c050c5SMichael Chan 
1230c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
1231c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
1232c0c050c5SMichael Chan 		break;
1233a011952aSMichael Chan #endif
1234a011952aSMichael Chan 
1235a011952aSMichael Chan 	case ETHTOOL_GRXFH:
1236a011952aSMichael Chan 		rc = bnxt_grxfh(bp, cmd);
1237a011952aSMichael Chan 		break;
1238c0c050c5SMichael Chan 
1239c0c050c5SMichael Chan 	default:
1240c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
1241c0c050c5SMichael Chan 		break;
1242c0c050c5SMichael Chan 	}
1243c0c050c5SMichael Chan 
1244c0c050c5SMichael Chan 	return rc;
1245c0c050c5SMichael Chan }
1246a011952aSMichael Chan 
1247a011952aSMichael Chan static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1248a011952aSMichael Chan {
1249a011952aSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1250a011952aSMichael Chan 	int rc;
1251a011952aSMichael Chan 
1252a011952aSMichael Chan 	switch (cmd->cmd) {
1253a011952aSMichael Chan 	case ETHTOOL_SRXFH:
1254a011952aSMichael Chan 		rc = bnxt_srxfh(bp, cmd);
1255a011952aSMichael Chan 		break;
1256a011952aSMichael Chan 
1257a011952aSMichael Chan 	default:
1258a011952aSMichael Chan 		rc = -EOPNOTSUPP;
1259a011952aSMichael Chan 		break;
1260a011952aSMichael Chan 	}
1261a011952aSMichael Chan 	return rc;
1262a011952aSMichael Chan }
1263c0c050c5SMichael Chan 
1264b73c1d08SMichael Chan u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
1265c0c050c5SMichael Chan {
1266b73c1d08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1267b73c1d08SMichael Chan 
1268b73c1d08SMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1269b73c1d08SMichael Chan 		return ALIGN(bp->rx_nr_rings, BNXT_RSS_TABLE_ENTRIES_P5);
1270c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
1271c0c050c5SMichael Chan }
1272c0c050c5SMichael Chan 
1273c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
1274c0c050c5SMichael Chan {
1275c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
1276c0c050c5SMichael Chan }
1277c0c050c5SMichael Chan 
1278c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1279c0c050c5SMichael Chan 			 u8 *hfunc)
1280c0c050c5SMichael Chan {
1281c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
12827991cb9cSMichael Chan 	struct bnxt_vnic_info *vnic;
1283adc38ac6SMichael Chan 	u32 i, tbl_size;
1284c0c050c5SMichael Chan 
1285c0c050c5SMichael Chan 	if (hfunc)
1286c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
1287c0c050c5SMichael Chan 
12887991cb9cSMichael Chan 	if (!bp->vnic_info)
12897991cb9cSMichael Chan 		return 0;
12907991cb9cSMichael Chan 
12917991cb9cSMichael Chan 	vnic = &bp->vnic_info[0];
1292adc38ac6SMichael Chan 	if (indir && bp->rss_indir_tbl) {
1293adc38ac6SMichael Chan 		tbl_size = bnxt_get_rxfh_indir_size(dev);
1294adc38ac6SMichael Chan 		for (i = 0; i < tbl_size; i++)
1295adc38ac6SMichael Chan 			indir[i] = bp->rss_indir_tbl[i];
12967991cb9cSMichael Chan 	}
1297c0c050c5SMichael Chan 
12987991cb9cSMichael Chan 	if (key && vnic->rss_hash_key)
1299c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
1300c0c050c5SMichael Chan 
1301c0c050c5SMichael Chan 	return 0;
1302c0c050c5SMichael Chan }
1303c0c050c5SMichael Chan 
1304bd3191b5SMichael Chan static int bnxt_set_rxfh(struct net_device *dev, const u32 *indir,
1305bd3191b5SMichael Chan 			 const u8 *key, const u8 hfunc)
1306bd3191b5SMichael Chan {
1307bd3191b5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1308bd3191b5SMichael Chan 	int rc = 0;
1309bd3191b5SMichael Chan 
1310bd3191b5SMichael Chan 	if (hfunc && hfunc != ETH_RSS_HASH_TOP)
1311bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1312bd3191b5SMichael Chan 
1313bd3191b5SMichael Chan 	if (key)
1314bd3191b5SMichael Chan 		return -EOPNOTSUPP;
1315bd3191b5SMichael Chan 
1316bd3191b5SMichael Chan 	if (indir) {
1317bd3191b5SMichael Chan 		u32 i, pad, tbl_size = bnxt_get_rxfh_indir_size(dev);
1318bd3191b5SMichael Chan 
1319bd3191b5SMichael Chan 		for (i = 0; i < tbl_size; i++)
1320bd3191b5SMichael Chan 			bp->rss_indir_tbl[i] = indir[i];
1321bd3191b5SMichael Chan 		pad = bp->rss_indir_tbl_entries - tbl_size;
1322bd3191b5SMichael Chan 		if (pad)
1323bd3191b5SMichael Chan 			memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
1324bd3191b5SMichael Chan 	}
1325bd3191b5SMichael Chan 
1326bd3191b5SMichael Chan 	if (netif_running(bp->dev)) {
1327bd3191b5SMichael Chan 		bnxt_close_nic(bp, false, false);
1328bd3191b5SMichael Chan 		rc = bnxt_open_nic(bp, false, false);
1329bd3191b5SMichael Chan 	}
1330bd3191b5SMichael Chan 	return rc;
1331bd3191b5SMichael Chan }
1332bd3191b5SMichael Chan 
1333c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
1334c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
1335c0c050c5SMichael Chan {
1336c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1337c0c050c5SMichael Chan 
1338c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1339431aa1ebSMichael Chan 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
1340c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
13415c8227d0SMichael Chan 	info->n_stats = bnxt_get_num_stats(bp);
1342eb513658SMichael Chan 	info->testinfo_len = bp->num_tests;
1343c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
1344c0c050c5SMichael Chan 	info->eedump_len = 0;
1345c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
1346c0c050c5SMichael Chan 	info->regdump_len = 0;
1347c0c050c5SMichael Chan }
1348c0c050c5SMichael Chan 
1349b5d600b0SVasundhara Volam static int bnxt_get_regs_len(struct net_device *dev)
1350b5d600b0SVasundhara Volam {
1351b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1352b5d600b0SVasundhara Volam 	int reg_len;
1353b5d600b0SVasundhara Volam 
1354f0f47b2fSVasundhara Volam 	if (!BNXT_PF(bp))
1355f0f47b2fSVasundhara Volam 		return -EOPNOTSUPP;
1356f0f47b2fSVasundhara Volam 
1357b5d600b0SVasundhara Volam 	reg_len = BNXT_PXP_REG_LEN;
1358b5d600b0SVasundhara Volam 
1359b5d600b0SVasundhara Volam 	if (bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED)
1360b5d600b0SVasundhara Volam 		reg_len += sizeof(struct pcie_ctx_hw_stats);
1361b5d600b0SVasundhara Volam 
1362b5d600b0SVasundhara Volam 	return reg_len;
1363b5d600b0SVasundhara Volam }
1364b5d600b0SVasundhara Volam 
1365b5d600b0SVasundhara Volam static void bnxt_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1366b5d600b0SVasundhara Volam 			  void *_p)
1367b5d600b0SVasundhara Volam {
1368b5d600b0SVasundhara Volam 	struct pcie_ctx_hw_stats *hw_pcie_stats;
1369b5d600b0SVasundhara Volam 	struct hwrm_pcie_qstats_input req = {0};
1370b5d600b0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
1371b5d600b0SVasundhara Volam 	dma_addr_t hw_pcie_stats_addr;
1372b5d600b0SVasundhara Volam 	int rc;
1373b5d600b0SVasundhara Volam 
1374b5d600b0SVasundhara Volam 	regs->version = 0;
1375b5d600b0SVasundhara Volam 	bnxt_dbg_hwrm_rd_reg(bp, 0, BNXT_PXP_REG_LEN / 4, _p);
1376b5d600b0SVasundhara Volam 
1377b5d600b0SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED))
1378b5d600b0SVasundhara Volam 		return;
1379b5d600b0SVasundhara Volam 
1380b5d600b0SVasundhara Volam 	hw_pcie_stats = dma_alloc_coherent(&bp->pdev->dev,
1381b5d600b0SVasundhara Volam 					   sizeof(*hw_pcie_stats),
1382b5d600b0SVasundhara Volam 					   &hw_pcie_stats_addr, GFP_KERNEL);
1383b5d600b0SVasundhara Volam 	if (!hw_pcie_stats)
1384b5d600b0SVasundhara Volam 		return;
1385b5d600b0SVasundhara Volam 
1386b5d600b0SVasundhara Volam 	regs->version = 1;
1387b5d600b0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PCIE_QSTATS, -1, -1);
1388b5d600b0SVasundhara Volam 	req.pcie_stat_size = cpu_to_le16(sizeof(*hw_pcie_stats));
1389b5d600b0SVasundhara Volam 	req.pcie_stat_host_addr = cpu_to_le64(hw_pcie_stats_addr);
1390b5d600b0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
1391b5d600b0SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1392b5d600b0SVasundhara Volam 	if (!rc) {
1393b5d600b0SVasundhara Volam 		__le64 *src = (__le64 *)hw_pcie_stats;
1394b5d600b0SVasundhara Volam 		u64 *dst = (u64 *)(_p + BNXT_PXP_REG_LEN);
1395b5d600b0SVasundhara Volam 		int i;
1396b5d600b0SVasundhara Volam 
1397b5d600b0SVasundhara Volam 		for (i = 0; i < sizeof(*hw_pcie_stats) / sizeof(__le64); i++)
1398b5d600b0SVasundhara Volam 			dst[i] = le64_to_cpu(src[i]);
1399b5d600b0SVasundhara Volam 	}
1400b5d600b0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
1401b5d600b0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, sizeof(*hw_pcie_stats), hw_pcie_stats,
1402b5d600b0SVasundhara Volam 			  hw_pcie_stats_addr);
1403b5d600b0SVasundhara Volam }
1404b5d600b0SVasundhara Volam 
14058e202366SMichael Chan static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
14068e202366SMichael Chan {
14078e202366SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
14088e202366SMichael Chan 
14098e202366SMichael Chan 	wol->supported = 0;
14108e202366SMichael Chan 	wol->wolopts = 0;
14118e202366SMichael Chan 	memset(&wol->sopass, 0, sizeof(wol->sopass));
14128e202366SMichael Chan 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
14138e202366SMichael Chan 		wol->supported = WAKE_MAGIC;
14148e202366SMichael Chan 		if (bp->wol)
14158e202366SMichael Chan 			wol->wolopts = WAKE_MAGIC;
14168e202366SMichael Chan 	}
14178e202366SMichael Chan }
14188e202366SMichael Chan 
14195282db6cSMichael Chan static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
14205282db6cSMichael Chan {
14215282db6cSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
14225282db6cSMichael Chan 
14235282db6cSMichael Chan 	if (wol->wolopts & ~WAKE_MAGIC)
14245282db6cSMichael Chan 		return -EINVAL;
14255282db6cSMichael Chan 
14265282db6cSMichael Chan 	if (wol->wolopts & WAKE_MAGIC) {
14275282db6cSMichael Chan 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
14285282db6cSMichael Chan 			return -EINVAL;
14295282db6cSMichael Chan 		if (!bp->wol) {
14305282db6cSMichael Chan 			if (bnxt_hwrm_alloc_wol_fltr(bp))
14315282db6cSMichael Chan 				return -EBUSY;
14325282db6cSMichael Chan 			bp->wol = 1;
14335282db6cSMichael Chan 		}
14345282db6cSMichael Chan 	} else {
14355282db6cSMichael Chan 		if (bp->wol) {
14365282db6cSMichael Chan 			if (bnxt_hwrm_free_wol_fltr(bp))
14375282db6cSMichael Chan 				return -EBUSY;
14385282db6cSMichael Chan 			bp->wol = 0;
14395282db6cSMichael Chan 		}
14405282db6cSMichael Chan 	}
14415282db6cSMichael Chan 	return 0;
14425282db6cSMichael Chan }
14435282db6cSMichael Chan 
1444170ce013SMichael Chan u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
1445c0c050c5SMichael Chan {
1446c0c050c5SMichael Chan 	u32 speed_mask = 0;
1447c0c050c5SMichael Chan 
1448c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
1449c0c050c5SMichael Chan 	/* set the advertised speeds */
1450c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1451c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
1452c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1453c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
1454c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1455c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
1456c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1457c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
1458c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
14591c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
146027c4d578SMichael Chan 
146127c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
146227c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
146327c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
146427c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
146527c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
146627c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
146727c4d578SMichael Chan 
1468c0c050c5SMichael Chan 	return speed_mask;
1469c0c050c5SMichael Chan }
1470c0c050c5SMichael Chan 
147100c04a92SMichael Chan #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
147200c04a92SMichael Chan {									\
147300c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
147400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147500c04a92SMichael Chan 						     100baseT_Full);	\
147600c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
147700c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
147800c04a92SMichael Chan 						     1000baseT_Full);	\
147900c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
148000c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
148100c04a92SMichael Chan 						     10000baseT_Full);	\
148200c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
148300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
148400c04a92SMichael Chan 						     25000baseCR_Full);	\
148500c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
148600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
148700c04a92SMichael Chan 						     40000baseCR4_Full);\
148800c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
148900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
149000c04a92SMichael Chan 						     50000baseCR2_Full);\
149138a21b34SDeepak Khungar 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
149238a21b34SDeepak Khungar 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
149338a21b34SDeepak Khungar 						     100000baseCR4_Full);\
149400c04a92SMichael Chan 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
149500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
149600c04a92SMichael Chan 						     Pause);		\
149700c04a92SMichael Chan 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
149800c04a92SMichael Chan 			ethtool_link_ksettings_add_link_mode(		\
149900c04a92SMichael Chan 					lk_ksettings, name, Asym_Pause);\
150000c04a92SMichael Chan 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
150100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
150200c04a92SMichael Chan 						     Asym_Pause);	\
150300c04a92SMichael Chan 	}								\
150400c04a92SMichael Chan }
150500c04a92SMichael Chan 
150600c04a92SMichael Chan #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
150700c04a92SMichael Chan {									\
150800c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
150900c04a92SMichael Chan 						  100baseT_Full) ||	\
151000c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
151100c04a92SMichael Chan 						  100baseT_Half))	\
151200c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
151300c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
151400c04a92SMichael Chan 						  1000baseT_Full) ||	\
151500c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
151600c04a92SMichael Chan 						  1000baseT_Half))	\
151700c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
151800c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
151900c04a92SMichael Chan 						  10000baseT_Full))	\
152000c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
152100c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
152200c04a92SMichael Chan 						  25000baseCR_Full))	\
152300c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
152400c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
152500c04a92SMichael Chan 						  40000baseCR4_Full))	\
152600c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
152700c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
152800c04a92SMichael Chan 						  50000baseCR2_Full))	\
152900c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
153038a21b34SDeepak Khungar 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
153138a21b34SDeepak Khungar 						  100000baseCR4_Full))	\
153238a21b34SDeepak Khungar 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
153300c04a92SMichael Chan }
153400c04a92SMichael Chan 
1535532262baSEdwin Peer #define BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, name)	\
1536532262baSEdwin Peer {									\
1537532262baSEdwin Peer 	if ((fw_speeds) & BNXT_LINK_PAM4_SPEED_MSK_50GB)		\
1538532262baSEdwin Peer 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1539532262baSEdwin Peer 						     50000baseCR_Full);	\
1540532262baSEdwin Peer 	if ((fw_speeds) & BNXT_LINK_PAM4_SPEED_MSK_100GB)		\
1541532262baSEdwin Peer 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1542532262baSEdwin Peer 						     100000baseCR2_Full);\
1543532262baSEdwin Peer 	if ((fw_speeds) & BNXT_LINK_PAM4_SPEED_MSK_200GB)		\
1544532262baSEdwin Peer 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1545532262baSEdwin Peer 						     200000baseCR4_Full);\
1546532262baSEdwin Peer }
1547532262baSEdwin Peer 
1548532262baSEdwin Peer #define BNXT_ETHTOOL_TO_FW_PAM4_SPDS(fw_speeds, lk_ksettings, name)	\
1549532262baSEdwin Peer {									\
1550532262baSEdwin Peer 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1551532262baSEdwin Peer 						  50000baseCR_Full))	\
1552532262baSEdwin Peer 		(fw_speeds) |= BNXT_LINK_PAM4_SPEED_MSK_50GB;		\
1553532262baSEdwin Peer 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1554532262baSEdwin Peer 						  100000baseCR2_Full))	\
1555532262baSEdwin Peer 		(fw_speeds) |= BNXT_LINK_PAM4_SPEED_MSK_100GB;		\
1556532262baSEdwin Peer 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1557532262baSEdwin Peer 						  200000baseCR4_Full))	\
1558532262baSEdwin Peer 		(fw_speeds) |= BNXT_LINK_PAM4_SPEED_MSK_200GB;		\
1559532262baSEdwin Peer }
1560532262baSEdwin Peer 
15618b277589SMichael Chan static void bnxt_fw_to_ethtool_advertised_fec(struct bnxt_link_info *link_info,
15628b277589SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15638b277589SMichael Chan {
15648b277589SMichael Chan 	u16 fec_cfg = link_info->fec_cfg;
15658b277589SMichael Chan 
15668b277589SMichael Chan 	if ((fec_cfg & BNXT_FEC_NONE) || !(fec_cfg & BNXT_FEC_AUTONEG)) {
15678b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
15688b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15698b277589SMichael Chan 		return;
15708b277589SMichael Chan 	}
15718b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_BASE_R)
15728b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
15738b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15748b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_RS)
15758b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
15768b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15778b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_LLRS)
15788b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
15798b277589SMichael Chan 				 lk_ksettings->link_modes.advertising);
15808b277589SMichael Chan }
15818b277589SMichael Chan 
158200c04a92SMichael Chan static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
158300c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
158427c4d578SMichael Chan {
158568515a18SMichael Chan 	u16 fw_speeds = link_info->advertising;
158627c4d578SMichael Chan 	u8 fw_pause = 0;
158727c4d578SMichael Chan 
158827c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
158927c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
159027c4d578SMichael Chan 
159100c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
1592532262baSEdwin Peer 	fw_speeds = link_info->advertising_pam4;
1593532262baSEdwin Peer 	BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, advertising);
15948b277589SMichael Chan 	bnxt_fw_to_ethtool_advertised_fec(link_info, lk_ksettings);
159527c4d578SMichael Chan }
159627c4d578SMichael Chan 
159700c04a92SMichael Chan static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
159800c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
15993277360eSMichael Chan {
16003277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
16013277360eSMichael Chan 	u8 fw_pause = 0;
16023277360eSMichael Chan 
16033277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
16043277360eSMichael Chan 		fw_pause = link_info->lp_pause;
16053277360eSMichael Chan 
160600c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
160700c04a92SMichael Chan 				lp_advertising);
1608532262baSEdwin Peer 	fw_speeds = link_info->lp_auto_pam4_link_speeds;
1609532262baSEdwin Peer 	BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, lp_advertising);
16103277360eSMichael Chan }
16113277360eSMichael Chan 
16128b277589SMichael Chan static void bnxt_fw_to_ethtool_support_fec(struct bnxt_link_info *link_info,
16138b277589SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
16148b277589SMichael Chan {
16158b277589SMichael Chan 	u16 fec_cfg = link_info->fec_cfg;
16168b277589SMichael Chan 
16178b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_NONE) {
16188b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
16198b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16208b277589SMichael Chan 		return;
16218b277589SMichael Chan 	}
16228b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_BASE_R_CAP)
16238b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
16248b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16258b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_RS_CAP)
16268b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
16278b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16288b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_LLRS_CAP)
16298b277589SMichael Chan 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
16308b277589SMichael Chan 				 lk_ksettings->link_modes.supported);
16318b277589SMichael Chan }
16328b277589SMichael Chan 
163300c04a92SMichael Chan static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
163400c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
16354b32caccSMichael Chan {
16364b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
16374b32caccSMichael Chan 
163800c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
1639532262baSEdwin Peer 	fw_speeds = link_info->support_pam4_speeds;
1640532262baSEdwin Peer 	BNXT_FW_TO_ETHTOOL_PAM4_SPDS(fw_speeds, lk_ksettings, supported);
16414b32caccSMichael Chan 
164200c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
164300c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
164400c04a92SMichael Chan 					     Asym_Pause);
164593ed8117SMichael Chan 
1646532262baSEdwin Peer 	if (link_info->support_auto_speeds ||
1647532262baSEdwin Peer 	    link_info->support_pam4_auto_speeds)
164800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
164900c04a92SMichael Chan 						     Autoneg);
16508b277589SMichael Chan 	bnxt_fw_to_ethtool_support_fec(link_info, lk_ksettings);
165193ed8117SMichael Chan }
165293ed8117SMichael Chan 
1653c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1654c0c050c5SMichael Chan {
1655c0c050c5SMichael Chan 	switch (fw_link_speed) {
1656c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
1657c0c050c5SMichael Chan 		return SPEED_100;
1658c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
1659c0c050c5SMichael Chan 		return SPEED_1000;
1660c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
1661c0c050c5SMichael Chan 		return SPEED_2500;
1662c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
1663c0c050c5SMichael Chan 		return SPEED_10000;
1664c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
1665c0c050c5SMichael Chan 		return SPEED_20000;
1666c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
1667c0c050c5SMichael Chan 		return SPEED_25000;
1668c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
1669c0c050c5SMichael Chan 		return SPEED_40000;
1670c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
1671c0c050c5SMichael Chan 		return SPEED_50000;
167238a21b34SDeepak Khungar 	case BNXT_LINK_SPEED_100GB:
167338a21b34SDeepak Khungar 		return SPEED_100000;
1674c0c050c5SMichael Chan 	default:
1675c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
1676c0c050c5SMichael Chan 	}
1677c0c050c5SMichael Chan }
1678c0c050c5SMichael Chan 
167900c04a92SMichael Chan static int bnxt_get_link_ksettings(struct net_device *dev,
168000c04a92SMichael Chan 				   struct ethtool_link_ksettings *lk_ksettings)
1681c0c050c5SMichael Chan {
1682c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1683c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
168400c04a92SMichael Chan 	struct ethtool_link_settings *base = &lk_ksettings->base;
168500c04a92SMichael Chan 	u32 ethtool_speed;
1686c0c050c5SMichael Chan 
168700c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1688e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
168900c04a92SMichael Chan 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1690c0c050c5SMichael Chan 
169100c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1692b763499eSMichael Chan 	if (link_info->autoneg) {
169300c04a92SMichael Chan 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
169400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
169500c04a92SMichael Chan 						     advertising, Autoneg);
169600c04a92SMichael Chan 		base->autoneg = AUTONEG_ENABLE;
169700c04a92SMichael Chan 		base->duplex = DUPLEX_UNKNOWN;
169883d8f5e9SMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK) {
169983d8f5e9SMichael Chan 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
170083d8f5e9SMichael Chan 			if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
170100c04a92SMichael Chan 				base->duplex = DUPLEX_FULL;
170229c262feSMichael Chan 			else
170300c04a92SMichael Chan 				base->duplex = DUPLEX_HALF;
170483d8f5e9SMichael Chan 		}
170583d8f5e9SMichael Chan 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1706c0c050c5SMichael Chan 	} else {
170700c04a92SMichael Chan 		base->autoneg = AUTONEG_DISABLE;
170829c262feSMichael Chan 		ethtool_speed =
170929c262feSMichael Chan 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
171000c04a92SMichael Chan 		base->duplex = DUPLEX_HALF;
171129c262feSMichael Chan 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
171200c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
1713c0c050c5SMichael Chan 	}
171400c04a92SMichael Chan 	base->speed = ethtool_speed;
1715c0c050c5SMichael Chan 
171600c04a92SMichael Chan 	base->port = PORT_NONE;
1717c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
171800c04a92SMichael Chan 		base->port = PORT_TP;
171900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
172000c04a92SMichael Chan 						     TP);
172100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
172200c04a92SMichael Chan 						     TP);
1723c0c050c5SMichael Chan 	} else {
172400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
172500c04a92SMichael Chan 						     FIBRE);
172600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
172700c04a92SMichael Chan 						     FIBRE);
1728c0c050c5SMichael Chan 
1729c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
173000c04a92SMichael Chan 			base->port = PORT_DA;
1731c0c050c5SMichael Chan 		else if (link_info->media_type ==
1732c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
173300c04a92SMichael Chan 			base->port = PORT_FIBRE;
1734c0c050c5SMichael Chan 	}
173500c04a92SMichael Chan 	base->phy_address = link_info->phy_addr;
1736e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1737c0c050c5SMichael Chan 
1738c0c050c5SMichael Chan 	return 0;
1739c0c050c5SMichael Chan }
1740c0c050c5SMichael Chan 
1741f00530bfSEdwin Peer static int bnxt_force_link_speed(struct net_device *dev, u32 ethtool_speed)
1742c0c050c5SMichael Chan {
17439d9cee08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
17449d9cee08SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1745532262baSEdwin Peer 	u16 support_pam4_spds = link_info->support_pam4_speeds;
17469d9cee08SMichael Chan 	u16 support_spds = link_info->support_speeds;
1747532262baSEdwin Peer 	u8 sig_mode = BNXT_SIG_MODE_NRZ;
1748f00530bfSEdwin Peer 	u16 fw_speed = 0;
17499d9cee08SMichael Chan 
1750c0c050c5SMichael Chan 	switch (ethtool_speed) {
1751c0c050c5SMichael Chan 	case SPEED_100:
17529d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
1753f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_100MB;
17549d9cee08SMichael Chan 		break;
1755c0c050c5SMichael Chan 	case SPEED_1000:
17569d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
1757f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
17589d9cee08SMichael Chan 		break;
1759c0c050c5SMichael Chan 	case SPEED_2500:
17609d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
1761f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_2_5GB;
17629d9cee08SMichael Chan 		break;
1763c0c050c5SMichael Chan 	case SPEED_10000:
17649d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
1765f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
17669d9cee08SMichael Chan 		break;
1767c0c050c5SMichael Chan 	case SPEED_20000:
17689d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
1769f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_20GB;
17709d9cee08SMichael Chan 		break;
1771c0c050c5SMichael Chan 	case SPEED_25000:
17729d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
1773f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
17749d9cee08SMichael Chan 		break;
1775c0c050c5SMichael Chan 	case SPEED_40000:
17769d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
1777f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
17789d9cee08SMichael Chan 		break;
1779c0c050c5SMichael Chan 	case SPEED_50000:
1780532262baSEdwin Peer 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB) {
1781f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
1782532262baSEdwin Peer 		} else if (support_pam4_spds & BNXT_LINK_PAM4_SPEED_MSK_50GB) {
1783532262baSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_PAM4_LINK_SPEED_50GB;
1784532262baSEdwin Peer 			sig_mode = BNXT_SIG_MODE_PAM4;
1785532262baSEdwin Peer 		}
17869d9cee08SMichael Chan 		break;
178738a21b34SDeepak Khungar 	case SPEED_100000:
1788532262baSEdwin Peer 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB) {
1789f00530bfSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_100GB;
1790532262baSEdwin Peer 		} else if (support_pam4_spds & BNXT_LINK_PAM4_SPEED_MSK_100GB) {
1791532262baSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_PAM4_LINK_SPEED_100GB;
1792532262baSEdwin Peer 			sig_mode = BNXT_SIG_MODE_PAM4;
1793532262baSEdwin Peer 		}
1794532262baSEdwin Peer 		break;
1795532262baSEdwin Peer 	case SPEED_200000:
1796532262baSEdwin Peer 		if (support_pam4_spds & BNXT_LINK_PAM4_SPEED_MSK_200GB) {
1797532262baSEdwin Peer 			fw_speed = PORT_PHY_CFG_REQ_FORCE_PAM4_LINK_SPEED_200GB;
1798532262baSEdwin Peer 			sig_mode = BNXT_SIG_MODE_PAM4;
1799532262baSEdwin Peer 		}
1800c0c050c5SMichael Chan 		break;
1801c0c050c5SMichael Chan 	}
1802f00530bfSEdwin Peer 
1803f00530bfSEdwin Peer 	if (!fw_speed) {
1804f00530bfSEdwin Peer 		netdev_err(dev, "unsupported speed!\n");
1805f00530bfSEdwin Peer 		return -EINVAL;
1806f00530bfSEdwin Peer 	}
1807f00530bfSEdwin Peer 
1808745b5c65SEdwin Peer 	if (link_info->req_link_speed == fw_speed &&
1809745b5c65SEdwin Peer 	    link_info->req_signal_mode == sig_mode &&
1810745b5c65SEdwin Peer 	    link_info->autoneg == 0)
1811745b5c65SEdwin Peer 		return -EALREADY;
1812745b5c65SEdwin Peer 
1813f00530bfSEdwin Peer 	link_info->req_link_speed = fw_speed;
1814532262baSEdwin Peer 	link_info->req_signal_mode = sig_mode;
1815f00530bfSEdwin Peer 	link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1816f00530bfSEdwin Peer 	link_info->autoneg = 0;
1817f00530bfSEdwin Peer 	link_info->advertising = 0;
1818532262baSEdwin Peer 	link_info->advertising_pam4 = 0;
1819f00530bfSEdwin Peer 
1820f00530bfSEdwin Peer 	return 0;
1821c0c050c5SMichael Chan }
1822c0c050c5SMichael Chan 
1823939f7f0cSMichael Chan u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1824c0c050c5SMichael Chan {
1825c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
1826c0c050c5SMichael Chan 
1827c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
1828c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
1829c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
1830c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1831c0c050c5SMichael Chan 	}
1832c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
1833c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
1834c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1835c0c050c5SMichael Chan 	}
1836c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
1837c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1838c0c050c5SMichael Chan 
18391c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
18401c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
18411c49c421SMichael Chan 
1842c0c050c5SMichael Chan 	return fw_speed_mask;
1843c0c050c5SMichael Chan }
1844c0c050c5SMichael Chan 
184500c04a92SMichael Chan static int bnxt_set_link_ksettings(struct net_device *dev,
184600c04a92SMichael Chan 			   const struct ethtool_link_ksettings *lk_ksettings)
1847c0c050c5SMichael Chan {
1848c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1849c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
185000c04a92SMichael Chan 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1851c0c050c5SMichael Chan 	bool set_pause = false;
185268515a18SMichael Chan 	u32 speed;
185300c04a92SMichael Chan 	int rc = 0;
1854c0c050c5SMichael Chan 
1855c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
185600c04a92SMichael Chan 		return -EOPNOTSUPP;
1857c0c050c5SMichael Chan 
1858e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
185900c04a92SMichael Chan 	if (base->autoneg == AUTONEG_ENABLE) {
1860532262baSEdwin Peer 		link_info->advertising = 0;
1861532262baSEdwin Peer 		link_info->advertising_pam4 = 0;
1862532262baSEdwin Peer 		BNXT_ETHTOOL_TO_FW_SPDS(link_info->advertising, lk_ksettings,
186300c04a92SMichael Chan 					advertising);
1864532262baSEdwin Peer 		BNXT_ETHTOOL_TO_FW_PAM4_SPDS(link_info->advertising_pam4,
1865532262baSEdwin Peer 					     lk_ksettings, advertising);
1866c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1867532262baSEdwin Peer 		if (!link_info->advertising && !link_info->advertising_pam4) {
186893ed8117SMichael Chan 			link_info->advertising = link_info->support_auto_speeds;
1869532262baSEdwin Peer 			link_info->advertising_pam4 =
1870532262baSEdwin Peer 				link_info->support_pam4_auto_speeds;
1871532262baSEdwin Peer 		}
1872c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
1873c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
1874c0c050c5SMichael Chan 		 */
1875c0c050c5SMichael Chan 		set_pause = true;
1876c0c050c5SMichael Chan 	} else {
187703efbec0SMichael Chan 		u8 phy_type = link_info->phy_type;
18789d9cee08SMichael Chan 
187903efbec0SMichael Chan 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
188003efbec0SMichael Chan 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
188103efbec0SMichael Chan 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
188203efbec0SMichael Chan 			netdev_err(dev, "10GBase-T devices must autoneg\n");
188303efbec0SMichael Chan 			rc = -EINVAL;
188403efbec0SMichael Chan 			goto set_setting_exit;
188503efbec0SMichael Chan 		}
188600c04a92SMichael Chan 		if (base->duplex == DUPLEX_HALF) {
1887c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1888c0c050c5SMichael Chan 			rc = -EINVAL;
1889c0c050c5SMichael Chan 			goto set_setting_exit;
1890c0c050c5SMichael Chan 		}
189100c04a92SMichael Chan 		speed = base->speed;
1892f00530bfSEdwin Peer 		rc = bnxt_force_link_speed(dev, speed);
1893745b5c65SEdwin Peer 		if (rc) {
1894745b5c65SEdwin Peer 			if (rc == -EALREADY)
1895745b5c65SEdwin Peer 				rc = 0;
18969d9cee08SMichael Chan 			goto set_setting_exit;
18979d9cee08SMichael Chan 		}
1898745b5c65SEdwin Peer 	}
1899c0c050c5SMichael Chan 
1900c0c050c5SMichael Chan 	if (netif_running(dev))
1901939f7f0cSMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1902c0c050c5SMichael Chan 
1903c0c050c5SMichael Chan set_setting_exit:
1904e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1905c0c050c5SMichael Chan 	return rc;
1906c0c050c5SMichael Chan }
1907c0c050c5SMichael Chan 
19088b277589SMichael Chan static int bnxt_get_fecparam(struct net_device *dev,
19098b277589SMichael Chan 			     struct ethtool_fecparam *fec)
19108b277589SMichael Chan {
19118b277589SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
19128b277589SMichael Chan 	struct bnxt_link_info *link_info;
19138b277589SMichael Chan 	u8 active_fec;
19148b277589SMichael Chan 	u16 fec_cfg;
19158b277589SMichael Chan 
19168b277589SMichael Chan 	link_info = &bp->link_info;
19178b277589SMichael Chan 	fec_cfg = link_info->fec_cfg;
19188b277589SMichael Chan 	active_fec = link_info->active_fec_sig_mode &
19198b277589SMichael Chan 		     PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
19208b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_NONE) {
19218b277589SMichael Chan 		fec->fec = ETHTOOL_FEC_NONE;
19228b277589SMichael Chan 		fec->active_fec = ETHTOOL_FEC_NONE;
19238b277589SMichael Chan 		return 0;
19248b277589SMichael Chan 	}
19258b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_AUTONEG)
19268b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_AUTO;
19278b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_BASE_R)
19288b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_BASER;
19298b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_RS)
19308b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_RS;
19318b277589SMichael Chan 	if (fec_cfg & BNXT_FEC_ENC_LLRS)
19328b277589SMichael Chan 		fec->fec |= ETHTOOL_FEC_LLRS;
19338b277589SMichael Chan 
19348b277589SMichael Chan 	switch (active_fec) {
19358b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
19368b277589SMichael Chan 		fec->active_fec |= ETHTOOL_FEC_BASER;
19378b277589SMichael Chan 		break;
19388b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
19398b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
19408b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
19418b277589SMichael Chan 		fec->active_fec |= ETHTOOL_FEC_RS;
19428b277589SMichael Chan 		break;
19438b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
19448b277589SMichael Chan 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
19458b277589SMichael Chan 		fec->active_fec |= ETHTOOL_FEC_LLRS;
19468b277589SMichael Chan 		break;
19478b277589SMichael Chan 	}
19488b277589SMichael Chan 	return 0;
19498b277589SMichael Chan }
19508b277589SMichael Chan 
1951c9ca5c3aSJakub Kicinski static void bnxt_get_fec_stats(struct net_device *dev,
1952c9ca5c3aSJakub Kicinski 			       struct ethtool_fec_stats *fec_stats)
1953c9ca5c3aSJakub Kicinski {
1954c9ca5c3aSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
1955c9ca5c3aSJakub Kicinski 	u64 *rx;
1956c9ca5c3aSJakub Kicinski 
1957c9ca5c3aSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
1958c9ca5c3aSJakub Kicinski 		return;
1959c9ca5c3aSJakub Kicinski 
1960c9ca5c3aSJakub Kicinski 	rx = bp->rx_port_stats_ext.sw_stats;
1961c9ca5c3aSJakub Kicinski 	fec_stats->corrected_bits.total =
1962c9ca5c3aSJakub Kicinski 		*(rx + BNXT_RX_STATS_EXT_OFFSET(rx_corrected_bits));
1963c9ca5c3aSJakub Kicinski }
1964c9ca5c3aSJakub Kicinski 
1965ccd6a9dcSMichael Chan static u32 bnxt_ethtool_forced_fec_to_fw(struct bnxt_link_info *link_info,
1966ccd6a9dcSMichael Chan 					 u32 fec)
1967ccd6a9dcSMichael Chan {
1968ccd6a9dcSMichael Chan 	u32 fw_fec = PORT_PHY_CFG_REQ_FLAGS_FEC_AUTONEG_DISABLE;
1969ccd6a9dcSMichael Chan 
1970ccd6a9dcSMichael Chan 	if (fec & ETHTOOL_FEC_BASER)
1971ccd6a9dcSMichael Chan 		fw_fec |= BNXT_FEC_BASE_R_ON(link_info);
1972ccd6a9dcSMichael Chan 	else if (fec & ETHTOOL_FEC_RS)
1973ccd6a9dcSMichael Chan 		fw_fec |= BNXT_FEC_RS_ON(link_info);
1974ccd6a9dcSMichael Chan 	else if (fec & ETHTOOL_FEC_LLRS)
1975ccd6a9dcSMichael Chan 		fw_fec |= BNXT_FEC_LLRS_ON;
1976ccd6a9dcSMichael Chan 	return fw_fec;
1977ccd6a9dcSMichael Chan }
1978ccd6a9dcSMichael Chan 
1979ccd6a9dcSMichael Chan static int bnxt_set_fecparam(struct net_device *dev,
1980ccd6a9dcSMichael Chan 			     struct ethtool_fecparam *fecparam)
1981ccd6a9dcSMichael Chan {
1982ccd6a9dcSMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
1983ccd6a9dcSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1984ccd6a9dcSMichael Chan 	struct bnxt_link_info *link_info;
1985ccd6a9dcSMichael Chan 	u32 new_cfg, fec = fecparam->fec;
1986ccd6a9dcSMichael Chan 	u16 fec_cfg;
1987ccd6a9dcSMichael Chan 	int rc;
1988ccd6a9dcSMichael Chan 
1989ccd6a9dcSMichael Chan 	link_info = &bp->link_info;
1990ccd6a9dcSMichael Chan 	fec_cfg = link_info->fec_cfg;
1991ccd6a9dcSMichael Chan 	if (fec_cfg & BNXT_FEC_NONE)
1992ccd6a9dcSMichael Chan 		return -EOPNOTSUPP;
1993ccd6a9dcSMichael Chan 
1994ccd6a9dcSMichael Chan 	if (fec & ETHTOOL_FEC_OFF) {
1995ccd6a9dcSMichael Chan 		new_cfg = PORT_PHY_CFG_REQ_FLAGS_FEC_AUTONEG_DISABLE |
1996ccd6a9dcSMichael Chan 			  BNXT_FEC_ALL_OFF(link_info);
1997ccd6a9dcSMichael Chan 		goto apply_fec;
1998ccd6a9dcSMichael Chan 	}
1999ccd6a9dcSMichael Chan 	if (((fec & ETHTOOL_FEC_AUTO) && !(fec_cfg & BNXT_FEC_AUTONEG_CAP)) ||
2000ccd6a9dcSMichael Chan 	    ((fec & ETHTOOL_FEC_RS) && !(fec_cfg & BNXT_FEC_ENC_RS_CAP)) ||
2001ccd6a9dcSMichael Chan 	    ((fec & ETHTOOL_FEC_LLRS) && !(fec_cfg & BNXT_FEC_ENC_LLRS_CAP)) ||
2002ccd6a9dcSMichael Chan 	    ((fec & ETHTOOL_FEC_BASER) && !(fec_cfg & BNXT_FEC_ENC_BASE_R_CAP)))
2003ccd6a9dcSMichael Chan 		return -EINVAL;
2004ccd6a9dcSMichael Chan 
2005ccd6a9dcSMichael Chan 	if (fec & ETHTOOL_FEC_AUTO) {
2006ccd6a9dcSMichael Chan 		if (!link_info->autoneg)
2007ccd6a9dcSMichael Chan 			return -EINVAL;
2008ccd6a9dcSMichael Chan 		new_cfg = PORT_PHY_CFG_REQ_FLAGS_FEC_AUTONEG_ENABLE;
2009ccd6a9dcSMichael Chan 	} else {
2010ccd6a9dcSMichael Chan 		new_cfg = bnxt_ethtool_forced_fec_to_fw(link_info, fec);
2011ccd6a9dcSMichael Chan 	}
2012ccd6a9dcSMichael Chan 
2013ccd6a9dcSMichael Chan apply_fec:
2014ccd6a9dcSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
2015ccd6a9dcSMichael Chan 	req.flags = cpu_to_le32(new_cfg | PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
2016ccd6a9dcSMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2017ccd6a9dcSMichael Chan 	/* update current settings */
2018ccd6a9dcSMichael Chan 	if (!rc) {
2019ccd6a9dcSMichael Chan 		mutex_lock(&bp->link_lock);
2020ccd6a9dcSMichael Chan 		bnxt_update_link(bp, false);
2021ccd6a9dcSMichael Chan 		mutex_unlock(&bp->link_lock);
2022ccd6a9dcSMichael Chan 	}
2023ccd6a9dcSMichael Chan 	return rc;
2024ccd6a9dcSMichael Chan }
2025ccd6a9dcSMichael Chan 
2026c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
2027c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
2028c0c050c5SMichael Chan {
2029c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2030c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
2031c0c050c5SMichael Chan 
2032c0c050c5SMichael Chan 	if (BNXT_VF(bp))
2033c0c050c5SMichael Chan 		return;
2034b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
20353c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
20363c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
2037c0c050c5SMichael Chan }
2038c0c050c5SMichael Chan 
2039423cffcfSJakub Kicinski static void bnxt_get_pause_stats(struct net_device *dev,
2040423cffcfSJakub Kicinski 				 struct ethtool_pause_stats *epstat)
2041423cffcfSJakub Kicinski {
2042423cffcfSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
2043423cffcfSJakub Kicinski 	u64 *rx, *tx;
2044423cffcfSJakub Kicinski 
2045423cffcfSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS))
2046423cffcfSJakub Kicinski 		return;
2047423cffcfSJakub Kicinski 
2048423cffcfSJakub Kicinski 	rx = bp->port_stats.sw_stats;
2049423cffcfSJakub Kicinski 	tx = bp->port_stats.sw_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
2050423cffcfSJakub Kicinski 
2051423cffcfSJakub Kicinski 	epstat->rx_pause_frames = BNXT_GET_RX_PORT_STATS64(rx, rx_pause_frames);
2052423cffcfSJakub Kicinski 	epstat->tx_pause_frames = BNXT_GET_TX_PORT_STATS64(tx, tx_pause_frames);
2053423cffcfSJakub Kicinski }
2054423cffcfSJakub Kicinski 
2055c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
2056c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
2057c0c050c5SMichael Chan {
2058c0c050c5SMichael Chan 	int rc = 0;
2059c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2060c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
2061c0c050c5SMichael Chan 
2062c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
206375362a3fSMichael Chan 		return -EOPNOTSUPP;
2064c0c050c5SMichael Chan 
2065a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
2066c0c050c5SMichael Chan 	if (epause->autoneg) {
2067a5390690SMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
2068a5390690SMichael Chan 			rc = -EINVAL;
2069a5390690SMichael Chan 			goto pause_exit;
2070a5390690SMichael Chan 		}
2071b763499eSMichael Chan 
2072c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
2073c9ee9516SMichael Chan 		if (bp->hwrm_spec_code >= 0x10201)
2074c9ee9516SMichael Chan 			link_info->req_flow_ctrl =
2075c9ee9516SMichael Chan 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
2076c0c050c5SMichael Chan 	} else {
2077c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
2078c0c050c5SMichael Chan 		 * force a link change
2079c0c050c5SMichael Chan 		 */
2080c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
2081c0c050c5SMichael Chan 			link_info->force_link_chng = true;
2082c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
2083c9ee9516SMichael Chan 		link_info->req_flow_ctrl = 0;
2084c0c050c5SMichael Chan 	}
2085c0c050c5SMichael Chan 	if (epause->rx_pause)
2086c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
2087c0c050c5SMichael Chan 
2088c0c050c5SMichael Chan 	if (epause->tx_pause)
2089c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
2090c0c050c5SMichael Chan 
2091a5390690SMichael Chan 	if (netif_running(dev))
2092c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
2093a5390690SMichael Chan 
2094a5390690SMichael Chan pause_exit:
2095163e9ef6SVasundhara Volam 	mutex_unlock(&bp->link_lock);
2096c0c050c5SMichael Chan 	return rc;
2097c0c050c5SMichael Chan }
2098c0c050c5SMichael Chan 
2099c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
2100c0c050c5SMichael Chan {
2101c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2102c0c050c5SMichael Chan 
2103c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
2104c0c050c5SMichael Chan 	return bp->link_info.link_up;
2105c0c050c5SMichael Chan }
2106c0c050c5SMichael Chan 
21074933f675SVasundhara Volam int bnxt_hwrm_nvm_get_dev_info(struct bnxt *bp,
21084933f675SVasundhara Volam 			       struct hwrm_nvm_get_dev_info_output *nvm_dev_info)
21094933f675SVasundhara Volam {
21104933f675SVasundhara Volam 	struct hwrm_nvm_get_dev_info_output *resp = bp->hwrm_cmd_resp_addr;
21114933f675SVasundhara Volam 	struct hwrm_nvm_get_dev_info_input req = {0};
21124933f675SVasundhara Volam 	int rc;
21134933f675SVasundhara Volam 
21140ae0a779SVasundhara Volam 	if (BNXT_VF(bp))
21150ae0a779SVasundhara Volam 		return -EOPNOTSUPP;
21160ae0a779SVasundhara Volam 
21174933f675SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DEV_INFO, -1, -1);
21184933f675SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
21194933f675SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
21204933f675SVasundhara Volam 	if (!rc)
21214933f675SVasundhara Volam 		memcpy(nvm_dev_info, resp, sizeof(*resp));
21224933f675SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
21234933f675SVasundhara Volam 	return rc;
21244933f675SVasundhara Volam }
21254933f675SVasundhara Volam 
2126b3b0ddd0SMichael Chan static void bnxt_print_admin_err(struct bnxt *bp)
2127b3b0ddd0SMichael Chan {
2128b3b0ddd0SMichael Chan 	netdev_info(bp->dev, "PF does not have admin privileges to flash or reset the device\n");
2129b3b0ddd0SMichael Chan }
2130b3b0ddd0SMichael Chan 
21315ac67d8bSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
21325ac67d8bSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
21335ac67d8bSRob Swindell 				u32 *data_length);
21345ac67d8bSRob Swindell 
213593ff3435SPavan Chebbi static int __bnxt_flash_nvram(struct net_device *dev, u16 dir_type,
213693ff3435SPavan Chebbi 			      u16 dir_ordinal, u16 dir_ext, u16 dir_attr,
213793ff3435SPavan Chebbi 			      u32 dir_item_len, const u8 *data,
2138c0c050c5SMichael Chan 			      size_t data_len)
2139c0c050c5SMichael Chan {
2140c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2141c0c050c5SMichael Chan 	int rc;
2142c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
2143c0c050c5SMichael Chan 	dma_addr_t dma_handle;
214493ff3435SPavan Chebbi 	u8 *kmem = NULL;
2145c0c050c5SMichael Chan 
2146c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
2147c0c050c5SMichael Chan 
2148c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
2149c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
2150c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
2151c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
215293ff3435SPavan Chebbi 	req.dir_item_length = cpu_to_le32(dir_item_len);
215393ff3435SPavan Chebbi 	if (data_len && data) {
2154c0c050c5SMichael Chan 		req.dir_data_length = cpu_to_le32(data_len);
2155c0c050c5SMichael Chan 
2156c0c050c5SMichael Chan 		kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
2157c0c050c5SMichael Chan 					  GFP_KERNEL);
215893ff3435SPavan Chebbi 		if (!kmem)
2159c0c050c5SMichael Chan 			return -ENOMEM;
216093ff3435SPavan Chebbi 
2161c0c050c5SMichael Chan 		memcpy(kmem, data, data_len);
2162c0c050c5SMichael Chan 		req.host_src_addr = cpu_to_le64(dma_handle);
216393ff3435SPavan Chebbi 	}
2164c0c050c5SMichael Chan 
216593ff3435SPavan Chebbi 	rc = _hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
216693ff3435SPavan Chebbi 	if (kmem)
2167c0c050c5SMichael Chan 		dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
2168c0c050c5SMichael Chan 
2169d4f1420dSMichael Chan 	if (rc == -EACCES)
2170b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
2171c0c050c5SMichael Chan 	return rc;
2172c0c050c5SMichael Chan }
2173c0c050c5SMichael Chan 
217493ff3435SPavan Chebbi static int bnxt_flash_nvram(struct net_device *dev, u16 dir_type,
217593ff3435SPavan Chebbi 			    u16 dir_ordinal, u16 dir_ext, u16 dir_attr,
217693ff3435SPavan Chebbi 			    const u8 *data, size_t data_len)
217793ff3435SPavan Chebbi {
217893ff3435SPavan Chebbi 	struct bnxt *bp = netdev_priv(dev);
217993ff3435SPavan Chebbi 	int rc;
218093ff3435SPavan Chebbi 
218193ff3435SPavan Chebbi 	mutex_lock(&bp->hwrm_cmd_lock);
218293ff3435SPavan Chebbi 	rc = __bnxt_flash_nvram(dev, dir_type, dir_ordinal, dir_ext, dir_attr,
218393ff3435SPavan Chebbi 				0, data, data_len);
218493ff3435SPavan Chebbi 	mutex_unlock(&bp->hwrm_cmd_lock);
218593ff3435SPavan Chebbi 	return rc;
218693ff3435SPavan Chebbi }
218793ff3435SPavan Chebbi 
218895fec034SEdwin Peer static int bnxt_hwrm_firmware_reset(struct net_device *dev, u8 proc_type,
218995fec034SEdwin Peer 				    u8 self_reset, u8 flags)
2190d2d6318cSRob Swindell {
2191d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
21927c675421SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
21937c675421SVasundhara Volam 	int rc;
2194d2d6318cSRob Swindell 
2195d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
2196d2d6318cSRob Swindell 
219795fec034SEdwin Peer 	req.embedded_proc_type = proc_type;
219895fec034SEdwin Peer 	req.selfrst_status = self_reset;
219995fec034SEdwin Peer 	req.flags = flags;
220095fec034SEdwin Peer 
22018cec0940SEdwin Peer 	if (proc_type == FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP) {
22028cec0940SEdwin Peer 		rc = hwrm_send_message_silent(bp, &req, sizeof(req),
22038cec0940SEdwin Peer 					      HWRM_CMD_TIMEOUT);
22048cec0940SEdwin Peer 	} else {
220595fec034SEdwin Peer 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
220695fec034SEdwin Peer 		if (rc == -EACCES)
220795fec034SEdwin Peer 			bnxt_print_admin_err(bp);
22088cec0940SEdwin Peer 	}
220995fec034SEdwin Peer 	return rc;
221095fec034SEdwin Peer }
221195fec034SEdwin Peer 
221294f17e89SEdwin Peer static int bnxt_firmware_reset(struct net_device *dev,
221394f17e89SEdwin Peer 			       enum bnxt_nvm_directory_type dir_type)
221495fec034SEdwin Peer {
221595fec034SEdwin Peer 	u8 self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE;
221695fec034SEdwin Peer 	u8 proc_type, flags = 0;
221795fec034SEdwin Peer 
2218d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
2219d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
2220d2d6318cSRob Swindell 	switch (dir_type) {
2221d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
2222d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
2223d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
222495fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
2225d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
222695fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
2227d2d6318cSRob Swindell 		break;
2228d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
2229d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
223095fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
223108141e0bSRob Swindell 		/* Self-reset APE upon next PCIe reset: */
223295fec034SEdwin Peer 		self_reset = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
2233d2d6318cSRob Swindell 		break;
2234d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
2235d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
223695fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
2237d2d6318cSRob Swindell 		break;
2238d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
2239d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
224095fec034SEdwin Peer 		proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
2241d2d6318cSRob Swindell 		break;
2242d2d6318cSRob Swindell 	default:
2243d2d6318cSRob Swindell 		return -EINVAL;
2244d2d6318cSRob Swindell 	}
2245d2d6318cSRob Swindell 
224695fec034SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, proc_type, self_reset, flags);
2247d2d6318cSRob Swindell }
2248d2d6318cSRob Swindell 
224994f17e89SEdwin Peer static int bnxt_firmware_reset_chip(struct net_device *dev)
225094f17e89SEdwin Peer {
225194f17e89SEdwin Peer 	struct bnxt *bp = netdev_priv(dev);
225294f17e89SEdwin Peer 	u8 flags = 0;
225394f17e89SEdwin Peer 
225494f17e89SEdwin Peer 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
225594f17e89SEdwin Peer 		flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
225694f17e89SEdwin Peer 
225794f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev,
225894f17e89SEdwin Peer 					FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP,
225994f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP,
226094f17e89SEdwin Peer 					flags);
226194f17e89SEdwin Peer }
226294f17e89SEdwin Peer 
226394f17e89SEdwin Peer static int bnxt_firmware_reset_ap(struct net_device *dev)
226494f17e89SEdwin Peer {
226594f17e89SEdwin Peer 	return bnxt_hwrm_firmware_reset(dev, FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP,
226694f17e89SEdwin Peer 					FW_RESET_REQ_SELFRST_STATUS_SELFRSTNONE,
226794f17e89SEdwin Peer 					0);
226894f17e89SEdwin Peer }
226994f17e89SEdwin Peer 
2270c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
2271c0c050c5SMichael Chan 			       u16 dir_type,
2272c0c050c5SMichael Chan 			       const u8 *fw_data,
2273c0c050c5SMichael Chan 			       size_t fw_size)
2274c0c050c5SMichael Chan {
2275c0c050c5SMichael Chan 	int	rc = 0;
2276c0c050c5SMichael Chan 	u16	code_type;
2277c0c050c5SMichael Chan 	u32	stored_crc;
2278c0c050c5SMichael Chan 	u32	calculated_crc;
2279c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
2280c0c050c5SMichael Chan 
2281c0c050c5SMichael Chan 	switch (dir_type) {
2282c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2283c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2284c0c050c5SMichael Chan 		code_type = CODE_BOOT;
2285c0c050c5SMichael Chan 		break;
228693e0b4feSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
228793e0b4feSRob Swindell 		code_type = CODE_CHIMP_PATCH;
228893e0b4feSRob Swindell 		break;
22892731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
22902731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
22912731d70fSRob Swindell 		break;
229293e0b4feSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
229393e0b4feSRob Swindell 		code_type = CODE_APE_PATCH;
229493e0b4feSRob Swindell 		break;
229593e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
229693e0b4feSRob Swindell 		code_type = CODE_KONG_FW;
229793e0b4feSRob Swindell 		break;
229893e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
229993e0b4feSRob Swindell 		code_type = CODE_KONG_PATCH;
230093e0b4feSRob Swindell 		break;
230193e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
230293e0b4feSRob Swindell 		code_type = CODE_BONO_FW;
230393e0b4feSRob Swindell 		break;
230493e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
230593e0b4feSRob Swindell 		code_type = CODE_BONO_PATCH;
230693e0b4feSRob Swindell 		break;
2307c0c050c5SMichael Chan 	default:
2308c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
2309c0c050c5SMichael Chan 			   dir_type);
2310c0c050c5SMichael Chan 		return -EINVAL;
2311c0c050c5SMichael Chan 	}
2312c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
2313c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
2314c0c050c5SMichael Chan 			   (unsigned int)fw_size);
2315c0c050c5SMichael Chan 		return -EINVAL;
2316c0c050c5SMichael Chan 	}
2317c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
2318c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
2319c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
2320c0c050c5SMichael Chan 		return -EINVAL;
2321c0c050c5SMichael Chan 	}
2322c0c050c5SMichael Chan 	if (header->code_type != code_type) {
2323c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
2324c0c050c5SMichael Chan 			   code_type, header->code_type);
2325c0c050c5SMichael Chan 		return -EINVAL;
2326c0c050c5SMichael Chan 	}
2327c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
2328c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
2329c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
2330c0c050c5SMichael Chan 		return -EINVAL;
2331c0c050c5SMichael Chan 	}
2332c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
2333c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
2334c0c050c5SMichael Chan 					     sizeof(stored_crc)));
2335c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
2336c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
2337c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
2338c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
2339c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
2340c0c050c5SMichael Chan 		return -EINVAL;
2341c0c050c5SMichael Chan 	}
2342c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2343c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
2344d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
2345d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
2346d2d6318cSRob Swindell 
2347c0c050c5SMichael Chan 	return rc;
2348c0c050c5SMichael Chan }
2349c0c050c5SMichael Chan 
23505ac67d8bSRob Swindell static int bnxt_flash_microcode(struct net_device *dev,
23515ac67d8bSRob Swindell 				u16 dir_type,
23525ac67d8bSRob Swindell 				const u8 *fw_data,
23535ac67d8bSRob Swindell 				size_t fw_size)
23545ac67d8bSRob Swindell {
23555ac67d8bSRob Swindell 	struct bnxt_ucode_trailer *trailer;
23565ac67d8bSRob Swindell 	u32 calculated_crc;
23575ac67d8bSRob Swindell 	u32 stored_crc;
23585ac67d8bSRob Swindell 	int rc = 0;
23595ac67d8bSRob Swindell 
23605ac67d8bSRob Swindell 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
23615ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode file size: %u\n",
23625ac67d8bSRob Swindell 			   (unsigned int)fw_size);
23635ac67d8bSRob Swindell 		return -EINVAL;
23645ac67d8bSRob Swindell 	}
23655ac67d8bSRob Swindell 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
23665ac67d8bSRob Swindell 						sizeof(*trailer)));
23675ac67d8bSRob Swindell 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
23685ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
23695ac67d8bSRob Swindell 			   le32_to_cpu(trailer->sig));
23705ac67d8bSRob Swindell 		return -EINVAL;
23715ac67d8bSRob Swindell 	}
23725ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
23735ac67d8bSRob Swindell 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
23745ac67d8bSRob Swindell 			   dir_type, le16_to_cpu(trailer->dir_type));
23755ac67d8bSRob Swindell 		return -EINVAL;
23765ac67d8bSRob Swindell 	}
23775ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->trailer_length) <
23785ac67d8bSRob Swindell 		sizeof(struct bnxt_ucode_trailer)) {
23795ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
23805ac67d8bSRob Swindell 			   le16_to_cpu(trailer->trailer_length));
23815ac67d8bSRob Swindell 		return -EINVAL;
23825ac67d8bSRob Swindell 	}
23835ac67d8bSRob Swindell 
23845ac67d8bSRob Swindell 	/* Confirm the CRC32 checksum of the file: */
23855ac67d8bSRob Swindell 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
23865ac67d8bSRob Swindell 					     sizeof(stored_crc)));
23875ac67d8bSRob Swindell 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
23885ac67d8bSRob Swindell 	if (calculated_crc != stored_crc) {
23895ac67d8bSRob Swindell 		netdev_err(dev,
23905ac67d8bSRob Swindell 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
23915ac67d8bSRob Swindell 			   (unsigned long)stored_crc,
23925ac67d8bSRob Swindell 			   (unsigned long)calculated_crc);
23935ac67d8bSRob Swindell 		return -EINVAL;
23945ac67d8bSRob Swindell 	}
23955ac67d8bSRob Swindell 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
23965ac67d8bSRob Swindell 			      0, 0, fw_data, fw_size);
23975ac67d8bSRob Swindell 
23985ac67d8bSRob Swindell 	return rc;
23995ac67d8bSRob Swindell }
24005ac67d8bSRob Swindell 
2401c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
2402c0c050c5SMichael Chan {
2403c0c050c5SMichael Chan 	switch (dir_type) {
2404c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
2405c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
2406c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
2407c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
2408c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
2409c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
2410c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
241193e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
241293e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
2413c0c050c5SMichael Chan 		return true;
2414c0c050c5SMichael Chan 	}
2415c0c050c5SMichael Chan 
2416c0c050c5SMichael Chan 	return false;
2417c0c050c5SMichael Chan }
2418c0c050c5SMichael Chan 
24195ac67d8bSRob Swindell static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
2420c0c050c5SMichael Chan {
2421c0c050c5SMichael Chan 	switch (dir_type) {
2422c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
2423c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
2424c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
2425c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
2426c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
2427c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
2428c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
2429c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
2430c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
2431c0c050c5SMichael Chan 		return true;
2432c0c050c5SMichael Chan 	}
2433c0c050c5SMichael Chan 
2434c0c050c5SMichael Chan 	return false;
2435c0c050c5SMichael Chan }
2436c0c050c5SMichael Chan 
2437c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
2438c0c050c5SMichael Chan {
2439c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
24405ac67d8bSRob Swindell 		bnxt_dir_type_is_other_exec_format(dir_type);
2441c0c050c5SMichael Chan }
2442c0c050c5SMichael Chan 
2443c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
2444c0c050c5SMichael Chan 					 u16 dir_type,
2445c0c050c5SMichael Chan 					 const char *filename)
2446c0c050c5SMichael Chan {
2447c0c050c5SMichael Chan 	const struct firmware  *fw;
2448c0c050c5SMichael Chan 	int			rc;
2449c0c050c5SMichael Chan 
2450c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
2451c0c050c5SMichael Chan 	if (rc != 0) {
2452c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
2453c0c050c5SMichael Chan 			   rc, filename);
2454c0c050c5SMichael Chan 		return rc;
2455c0c050c5SMichael Chan 	}
2456ba425800SJason Yan 	if (bnxt_dir_type_is_ape_bin_format(dir_type))
2457c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
2458ba425800SJason Yan 	else if (bnxt_dir_type_is_other_exec_format(dir_type))
24595ac67d8bSRob Swindell 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
2460c0c050c5SMichael Chan 	else
2461c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
2462c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
2463c0c050c5SMichael Chan 	release_firmware(fw);
2464c0c050c5SMichael Chan 	return rc;
2465c0c050c5SMichael Chan }
2466c0c050c5SMichael Chan 
2467a86b313eSMichael Chan #define BNXT_PKG_DMA_SIZE	0x40000
2468a86b313eSMichael Chan #define BNXT_NVM_MORE_FLAG	(cpu_to_le16(NVM_MODIFY_REQ_FLAGS_BATCH_MODE))
2469a86b313eSMichael Chan #define BNXT_NVM_LAST_FLAG	(cpu_to_le16(NVM_MODIFY_REQ_FLAGS_BATCH_LAST))
2470a86b313eSMichael Chan 
2471b44cfd4fSJacob Keller int bnxt_flash_package_from_fw_obj(struct net_device *dev, const struct firmware *fw,
2472d168f328SVasundhara Volam 				   u32 install_type)
2473c0c050c5SMichael Chan {
24745ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_input install = {0};
2475a9094ba6SMichael Chan 	struct hwrm_nvm_install_update_output resp = {0};
2476a9094ba6SMichael Chan 	struct hwrm_nvm_modify_input modify = {0};
2477a9094ba6SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
24781432c3f6SPavan Chebbi 	bool defrag_attempted = false;
2479a9094ba6SMichael Chan 	dma_addr_t dma_handle;
2480a9094ba6SMichael Chan 	u8 *kmem = NULL;
2481a86b313eSMichael Chan 	u32 modify_len;
24825ac67d8bSRob Swindell 	u32 item_len;
248322630e28SEdwin Peer 	int rc = 0;
24845ac67d8bSRob Swindell 	u16 index;
24855ac67d8bSRob Swindell 
24865ac67d8bSRob Swindell 	bnxt_hwrm_fw_set_time(bp);
24875ac67d8bSRob Swindell 
2488a9094ba6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
2489a9094ba6SMichael Chan 
2490a86b313eSMichael Chan 	/* Try allocating a large DMA buffer first.  Older fw will
2491a86b313eSMichael Chan 	 * cause excessive NVRAM erases when using small blocks.
2492a86b313eSMichael Chan 	 */
2493a86b313eSMichael Chan 	modify_len = roundup_pow_of_two(fw->size);
2494a86b313eSMichael Chan 	modify_len = min_t(u32, modify_len, BNXT_PKG_DMA_SIZE);
2495a86b313eSMichael Chan 	while (1) {
2496a86b313eSMichael Chan 		kmem = dma_alloc_coherent(&bp->pdev->dev, modify_len,
2497a86b313eSMichael Chan 					  &dma_handle, GFP_KERNEL);
2498a86b313eSMichael Chan 		if (!kmem && modify_len > PAGE_SIZE)
2499a86b313eSMichael Chan 			modify_len /= 2;
2500a86b313eSMichael Chan 		else
2501a86b313eSMichael Chan 			break;
2502a86b313eSMichael Chan 	}
2503a9094ba6SMichael Chan 	if (!kmem)
2504a9094ba6SMichael Chan 		return -ENOMEM;
2505a9094ba6SMichael Chan 
2506a9094ba6SMichael Chan 	modify.host_src_addr = cpu_to_le64(dma_handle);
2507a9094ba6SMichael Chan 
2508a9094ba6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
2509a9094ba6SMichael Chan 	if ((install_type & 0xffff) == 0)
2510a9094ba6SMichael Chan 		install_type >>= 16;
2511a9094ba6SMichael Chan 	install.install_type = cpu_to_le32(install_type);
2512a9094ba6SMichael Chan 
25132e5fb428SPavan Chebbi 	do {
2514a86b313eSMichael Chan 		u32 copied = 0, len = modify_len;
2515a86b313eSMichael Chan 
251695ec1f47SVasundhara Volam 		rc = bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
25172e5fb428SPavan Chebbi 					  BNX_DIR_ORDINAL_FIRST,
25182e5fb428SPavan Chebbi 					  BNX_DIR_EXT_NONE,
251995ec1f47SVasundhara Volam 					  &index, &item_len, NULL);
252095ec1f47SVasundhara Volam 		if (rc) {
25215ac67d8bSRob Swindell 			netdev_err(dev, "PKG update area not created in nvram\n");
25222e5fb428SPavan Chebbi 			break;
25235ac67d8bSRob Swindell 		}
25245ac67d8bSRob Swindell 		if (fw->size > item_len) {
25259a005c38SJonathan Lemon 			netdev_err(dev, "PKG insufficient update area in nvram: %lu\n",
25265ac67d8bSRob Swindell 				   (unsigned long)fw->size);
25275ac67d8bSRob Swindell 			rc = -EFBIG;
25282e5fb428SPavan Chebbi 			break;
25292e5fb428SPavan Chebbi 		}
25302e5fb428SPavan Chebbi 
25315ac67d8bSRob Swindell 		modify.dir_idx = cpu_to_le16(index);
25325ac67d8bSRob Swindell 
2533a86b313eSMichael Chan 		if (fw->size > modify_len)
2534a86b313eSMichael Chan 			modify.flags = BNXT_NVM_MORE_FLAG;
2535a86b313eSMichael Chan 		while (copied < fw->size) {
2536a86b313eSMichael Chan 			u32 balance = fw->size - copied;
2537a86b313eSMichael Chan 
2538a86b313eSMichael Chan 			if (balance <= modify_len) {
2539a86b313eSMichael Chan 				len = balance;
2540a86b313eSMichael Chan 				if (copied)
2541a86b313eSMichael Chan 					modify.flags |= BNXT_NVM_LAST_FLAG;
2542a86b313eSMichael Chan 			}
2543a86b313eSMichael Chan 			memcpy(kmem, fw->data + copied, len);
2544a86b313eSMichael Chan 			modify.len = cpu_to_le32(len);
2545a86b313eSMichael Chan 			modify.offset = cpu_to_le32(copied);
254622630e28SEdwin Peer 			rc = hwrm_send_message(bp, &modify, sizeof(modify),
25475ac67d8bSRob Swindell 					       FLASH_PACKAGE_TIMEOUT);
254822630e28SEdwin Peer 			if (rc)
2549a86b313eSMichael Chan 				goto pkg_abort;
2550a86b313eSMichael Chan 			copied += len;
2551a86b313eSMichael Chan 		}
2552cb4d1d62SKshitij Soni 		mutex_lock(&bp->hwrm_cmd_lock);
25531432c3f6SPavan Chebbi 		rc = _hwrm_send_message_silent(bp, &install, sizeof(install),
25545ac67d8bSRob Swindell 					       INSTALL_PACKAGE_TIMEOUT);
2555a9094ba6SMichael Chan 		memcpy(&resp, bp->hwrm_cmd_resp_addr, sizeof(resp));
2556cb4d1d62SKshitij Soni 
25571432c3f6SPavan Chebbi 		if (defrag_attempted) {
25581432c3f6SPavan Chebbi 			/* We have tried to defragment already in the previous
25591432c3f6SPavan Chebbi 			 * iteration. Return with the result for INSTALL_UPDATE
25601432c3f6SPavan Chebbi 			 */
25611432c3f6SPavan Chebbi 			mutex_unlock(&bp->hwrm_cmd_lock);
25621432c3f6SPavan Chebbi 			break;
25631432c3f6SPavan Chebbi 		}
25641432c3f6SPavan Chebbi 
25652e5fb428SPavan Chebbi 		if (rc && ((struct hwrm_err_output *)&resp)->cmd_err ==
2566dd2ebf34SVasundhara Volam 		    NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
256768748775SPavan Chebbi 			install.flags =
25682e5fb428SPavan Chebbi 				cpu_to_le16(NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
25692e5fb428SPavan Chebbi 
25701432c3f6SPavan Chebbi 			rc = _hwrm_send_message_silent(bp, &install,
25711432c3f6SPavan Chebbi 						       sizeof(install),
2572cb4d1d62SKshitij Soni 						       INSTALL_PACKAGE_TIMEOUT);
2573a9094ba6SMichael Chan 			memcpy(&resp, bp->hwrm_cmd_resp_addr, sizeof(resp));
25741432c3f6SPavan Chebbi 
25751432c3f6SPavan Chebbi 			if (rc && ((struct hwrm_err_output *)&resp)->cmd_err ==
25761432c3f6SPavan Chebbi 			    NVM_INSTALL_UPDATE_CMD_ERR_CODE_NO_SPACE) {
25771432c3f6SPavan Chebbi 				/* FW has cleared NVM area, driver will create
25781432c3f6SPavan Chebbi 				 * UPDATE directory and try the flash again
25791432c3f6SPavan Chebbi 				 */
25801432c3f6SPavan Chebbi 				defrag_attempted = true;
258168748775SPavan Chebbi 				install.flags = 0;
25821432c3f6SPavan Chebbi 				rc = __bnxt_flash_nvram(bp->dev,
25831432c3f6SPavan Chebbi 							BNX_DIR_TYPE_UPDATE,
25841432c3f6SPavan Chebbi 							BNX_DIR_ORDINAL_FIRST,
25851432c3f6SPavan Chebbi 							0, 0, item_len, NULL,
25861432c3f6SPavan Chebbi 							0);
25871432c3f6SPavan Chebbi 			} else if (rc) {
25881432c3f6SPavan Chebbi 				netdev_err(dev, "HWRM_NVM_INSTALL_UPDATE failure rc :%x\n", rc);
25891432c3f6SPavan Chebbi 			}
25901432c3f6SPavan Chebbi 		} else if (rc) {
25911432c3f6SPavan Chebbi 			netdev_err(dev, "HWRM_NVM_INSTALL_UPDATE failure rc :%x\n", rc);
2592dd2ebf34SVasundhara Volam 		}
25932e5fb428SPavan Chebbi 		mutex_unlock(&bp->hwrm_cmd_lock);
25941432c3f6SPavan Chebbi 	} while (defrag_attempted && !rc);
25955ac67d8bSRob Swindell 
2596a86b313eSMichael Chan pkg_abort:
2597a86b313eSMichael Chan 	dma_free_coherent(&bp->pdev->dev, modify_len, kmem, dma_handle);
2598a9094ba6SMichael Chan 	if (resp.result) {
25995ac67d8bSRob Swindell 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
2600a9094ba6SMichael Chan 			   (s8)resp.result, (int)resp.problem_item);
2601cb4d1d62SKshitij Soni 		rc = -ENOPKG;
26025ac67d8bSRob Swindell 	}
260322630e28SEdwin Peer 	if (rc == -EACCES)
2604b3b0ddd0SMichael Chan 		bnxt_print_admin_err(bp);
2605cb4d1d62SKshitij Soni 	return rc;
2606c0c050c5SMichael Chan }
2607c0c050c5SMichael Chan 
2608b44cfd4fSJacob Keller static int bnxt_flash_package_from_file(struct net_device *dev, const char *filename,
2609b44cfd4fSJacob Keller 					u32 install_type)
2610b44cfd4fSJacob Keller {
2611b44cfd4fSJacob Keller 	const struct firmware *fw;
2612b44cfd4fSJacob Keller 	int rc;
2613b44cfd4fSJacob Keller 
2614b44cfd4fSJacob Keller 	rc = request_firmware(&fw, filename, &dev->dev);
2615b44cfd4fSJacob Keller 	if (rc != 0) {
2616b44cfd4fSJacob Keller 		netdev_err(dev, "PKG error %d requesting file: %s\n",
2617b44cfd4fSJacob Keller 			   rc, filename);
2618b44cfd4fSJacob Keller 		return rc;
2619b44cfd4fSJacob Keller 	}
2620b44cfd4fSJacob Keller 
2621b44cfd4fSJacob Keller 	rc = bnxt_flash_package_from_fw_obj(dev, fw, install_type);
2622b44cfd4fSJacob Keller 
2623b44cfd4fSJacob Keller 	release_firmware(fw);
2624b44cfd4fSJacob Keller 
2625b44cfd4fSJacob Keller 	return rc;
2626b44cfd4fSJacob Keller }
2627b44cfd4fSJacob Keller 
2628c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
2629c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
2630c0c050c5SMichael Chan {
2631c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
2632c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
2633c0c050c5SMichael Chan 		return -EINVAL;
2634c0c050c5SMichael Chan 	}
2635c0c050c5SMichael Chan 
26365ac67d8bSRob Swindell 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
26375ac67d8bSRob Swindell 	    flash->region > 0xffff)
26385ac67d8bSRob Swindell 		return bnxt_flash_package_from_file(dev, flash->data,
26395ac67d8bSRob Swindell 						    flash->region);
2640c0c050c5SMichael Chan 
2641c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
2642c0c050c5SMichael Chan }
2643c0c050c5SMichael Chan 
2644c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
2645c0c050c5SMichael Chan {
2646c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2647c0c050c5SMichael Chan 	int rc;
2648c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
2649c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
2650c0c050c5SMichael Chan 
2651c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
2652c0c050c5SMichael Chan 
2653c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2654c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2655c0c050c5SMichael Chan 	if (!rc) {
2656c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
2657c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
2658c0c050c5SMichael Chan 	}
2659c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2660c0c050c5SMichael Chan 	return rc;
2661c0c050c5SMichael Chan }
2662c0c050c5SMichael Chan 
2663c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
2664c0c050c5SMichael Chan {
26654cebbacaSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
26664cebbacaSMichael Chan 
26674cebbacaSMichael Chan 	if (BNXT_VF(bp))
26684cebbacaSMichael Chan 		return 0;
26694cebbacaSMichael Chan 
2670c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
2671c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
2672c0c050c5SMichael Chan 	 */
2673c0c050c5SMichael Chan 	return -1;
2674c0c050c5SMichael Chan }
2675c0c050c5SMichael Chan 
2676c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
2677c0c050c5SMichael Chan {
2678c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2679c0c050c5SMichael Chan 	int rc;
2680c0c050c5SMichael Chan 	u32 dir_entries;
2681c0c050c5SMichael Chan 	u32 entry_length;
2682c0c050c5SMichael Chan 	u8 *buf;
2683c0c050c5SMichael Chan 	size_t buflen;
2684c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2685c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
2686c0c050c5SMichael Chan 
2687c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
2688c0c050c5SMichael Chan 	if (rc != 0)
2689c0c050c5SMichael Chan 		return rc;
2690c0c050c5SMichael Chan 
2691dbbfa96aSVasundhara Volam 	if (!dir_entries || !entry_length)
2692dbbfa96aSVasundhara Volam 		return -EIO;
2693dbbfa96aSVasundhara Volam 
2694c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
2695c0c050c5SMichael Chan 	if (len < 2)
2696c0c050c5SMichael Chan 		return -EINVAL;
2697c0c050c5SMichael Chan 
2698c0c050c5SMichael Chan 	*data++ = dir_entries;
2699c0c050c5SMichael Chan 	*data++ = entry_length;
2700c0c050c5SMichael Chan 	len -= 2;
2701c0c050c5SMichael Chan 	memset(data, 0xff, len);
2702c0c050c5SMichael Chan 
2703c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
2704c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
2705c0c050c5SMichael Chan 				 GFP_KERNEL);
2706c0c050c5SMichael Chan 	if (!buf) {
2707c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2708c0c050c5SMichael Chan 			   (unsigned)buflen);
2709c0c050c5SMichael Chan 		return -ENOMEM;
2710c0c050c5SMichael Chan 	}
2711c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
2712c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2713c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2714c0c050c5SMichael Chan 	if (rc == 0)
2715c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
2716c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
2717c0c050c5SMichael Chan 	return rc;
2718c0c050c5SMichael Chan }
2719c0c050c5SMichael Chan 
2720c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
2721c0c050c5SMichael Chan 			       u32 length, u8 *data)
2722c0c050c5SMichael Chan {
2723c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2724c0c050c5SMichael Chan 	int rc;
2725c0c050c5SMichael Chan 	u8 *buf;
2726c0c050c5SMichael Chan 	dma_addr_t dma_handle;
2727c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
2728c0c050c5SMichael Chan 
2729e0ad8fc5SMichael Chan 	if (!length)
2730e0ad8fc5SMichael Chan 		return -EINVAL;
2731e0ad8fc5SMichael Chan 
2732c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
2733c0c050c5SMichael Chan 				 GFP_KERNEL);
2734c0c050c5SMichael Chan 	if (!buf) {
2735c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2736c0c050c5SMichael Chan 			   (unsigned)length);
2737c0c050c5SMichael Chan 		return -ENOMEM;
2738c0c050c5SMichael Chan 	}
2739c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
2740c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
2741c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2742c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
2743c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
2744c0c050c5SMichael Chan 
2745c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2746c0c050c5SMichael Chan 	if (rc == 0)
2747c0c050c5SMichael Chan 		memcpy(data, buf, length);
2748c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
2749c0c050c5SMichael Chan 	return rc;
2750c0c050c5SMichael Chan }
2751c0c050c5SMichael Chan 
27523ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
27533ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
27543ebf6f0aSRob Swindell 				u32 *data_length)
27553ebf6f0aSRob Swindell {
27563ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
27573ebf6f0aSRob Swindell 	int rc;
27583ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
27593ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
27603ebf6f0aSRob Swindell 
27613ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
27623ebf6f0aSRob Swindell 	req.enables = 0;
27633ebf6f0aSRob Swindell 	req.dir_idx = 0;
27643ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
27653ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
27663ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
27673ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
2768cc72f3b1SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2769cc72f3b1SMichael Chan 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
27703ebf6f0aSRob Swindell 	if (rc == 0) {
27713ebf6f0aSRob Swindell 		if (index)
27723ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
27733ebf6f0aSRob Swindell 		if (item_length)
27743ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
27753ebf6f0aSRob Swindell 		if (data_length)
27763ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
27773ebf6f0aSRob Swindell 	}
2778cc72f3b1SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
27793ebf6f0aSRob Swindell 	return rc;
27803ebf6f0aSRob Swindell }
27813ebf6f0aSRob Swindell 
27823ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
27833ebf6f0aSRob Swindell {
27843ebf6f0aSRob Swindell 	char	*retval = NULL;
27853ebf6f0aSRob Swindell 	char	*p;
27863ebf6f0aSRob Swindell 	char	*value;
27873ebf6f0aSRob Swindell 	int	field = 0;
27883ebf6f0aSRob Swindell 
27893ebf6f0aSRob Swindell 	if (datalen < 1)
27903ebf6f0aSRob Swindell 		return NULL;
27913ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
27923ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
27933ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
27943ebf6f0aSRob Swindell 		field = 0;
27953ebf6f0aSRob Swindell 		retval = NULL;
27963ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
27973ebf6f0aSRob Swindell 			value = p;
27983ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
27993ebf6f0aSRob Swindell 				p++;
28003ebf6f0aSRob Swindell 			if (field == desired_field)
28013ebf6f0aSRob Swindell 				retval = value;
28023ebf6f0aSRob Swindell 			if (*p != '\t')
28033ebf6f0aSRob Swindell 				break;
28043ebf6f0aSRob Swindell 			*p = 0;
28053ebf6f0aSRob Swindell 			field++;
28063ebf6f0aSRob Swindell 			p++;
28073ebf6f0aSRob Swindell 		}
28083ebf6f0aSRob Swindell 		if (*p == 0)
28093ebf6f0aSRob Swindell 			break;
28103ebf6f0aSRob Swindell 		*p = 0;
28113ebf6f0aSRob Swindell 	}
28123ebf6f0aSRob Swindell 	return retval;
28133ebf6f0aSRob Swindell }
28143ebf6f0aSRob Swindell 
2815a60faa60SVasundhara Volam static void bnxt_get_pkgver(struct net_device *dev)
28163ebf6f0aSRob Swindell {
2817a60faa60SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
28183ebf6f0aSRob Swindell 	u16 index = 0;
2819a60faa60SVasundhara Volam 	char *pkgver;
2820a60faa60SVasundhara Volam 	u32 pkglen;
2821a60faa60SVasundhara Volam 	u8 *pkgbuf;
2822a60faa60SVasundhara Volam 	int len;
28233ebf6f0aSRob Swindell 
28243ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
28253ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2826a60faa60SVasundhara Volam 				 &index, NULL, &pkglen) != 0)
2827a60faa60SVasundhara Volam 		return;
28283ebf6f0aSRob Swindell 
2829a60faa60SVasundhara Volam 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
2830a60faa60SVasundhara Volam 	if (!pkgbuf) {
2831a60faa60SVasundhara Volam 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
2832a60faa60SVasundhara Volam 			pkglen);
2833a60faa60SVasundhara Volam 		return;
2834a60faa60SVasundhara Volam 	}
28353ebf6f0aSRob Swindell 
2836a60faa60SVasundhara Volam 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2837a60faa60SVasundhara Volam 		goto err;
2838a60faa60SVasundhara Volam 
2839a60faa60SVasundhara Volam 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2840a60faa60SVasundhara Volam 				   pkglen);
2841a60faa60SVasundhara Volam 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2842a60faa60SVasundhara Volam 		len = strlen(bp->fw_ver_str);
2843a60faa60SVasundhara Volam 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2844a60faa60SVasundhara Volam 			 "/pkg %s", pkgver);
2845a60faa60SVasundhara Volam 	}
2846a60faa60SVasundhara Volam err:
2847a60faa60SVasundhara Volam 	kfree(pkgbuf);
28483ebf6f0aSRob Swindell }
28493ebf6f0aSRob Swindell 
2850c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
2851c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2852c0c050c5SMichael Chan 			   u8 *data)
2853c0c050c5SMichael Chan {
2854c0c050c5SMichael Chan 	u32 index;
2855c0c050c5SMichael Chan 	u32 offset;
2856c0c050c5SMichael Chan 
2857c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
2858c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2859c0c050c5SMichael Chan 
2860c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
2861c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
2862c0c050c5SMichael Chan 
2863c0c050c5SMichael Chan 	if (index == 0) {
2864c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
2865c0c050c5SMichael Chan 		return -EINVAL;
2866c0c050c5SMichael Chan 	}
2867c0c050c5SMichael Chan 
2868c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2869c0c050c5SMichael Chan }
2870c0c050c5SMichael Chan 
2871c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2872c0c050c5SMichael Chan {
2873c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2874c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2875c0c050c5SMichael Chan 
2876c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2877c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2878c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2879c0c050c5SMichael Chan }
2880c0c050c5SMichael Chan 
2881c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
2882c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2883c0c050c5SMichael Chan 			   u8 *data)
2884c0c050c5SMichael Chan {
2885c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2886c0c050c5SMichael Chan 	u8 index, dir_op;
2887c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
2888c0c050c5SMichael Chan 
2889c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
2890c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2891c0c050c5SMichael Chan 		return -EINVAL;
2892c0c050c5SMichael Chan 	}
2893c0c050c5SMichael Chan 
2894c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
2895c0c050c5SMichael Chan 
2896c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
2897c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
2898c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
2899c0c050c5SMichael Chan 		if (index == 0)
2900c0c050c5SMichael Chan 			return -EINVAL;
2901c0c050c5SMichael Chan 		switch (dir_op) {
2902c0c050c5SMichael Chan 		case 0x0e: /* erase */
2903c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
2904c0c050c5SMichael Chan 				return -EINVAL;
2905c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
2906c0c050c5SMichael Chan 		default:
2907c0c050c5SMichael Chan 			return -EINVAL;
2908c0c050c5SMichael Chan 		}
2909c0c050c5SMichael Chan 	}
2910c0c050c5SMichael Chan 
2911c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
2912ba425800SJason Yan 	if (bnxt_dir_type_is_executable(type))
29135ac67d8bSRob Swindell 		return -EOPNOTSUPP;
2914c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
2915c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
2916c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
2917c0c050c5SMichael Chan 
2918c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2919c0c050c5SMichael Chan 				eeprom->len);
2920c0c050c5SMichael Chan }
2921c0c050c5SMichael Chan 
292272b34f04SMichael Chan static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
292372b34f04SMichael Chan {
292472b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
292572b34f04SMichael Chan 	struct ethtool_eee *eee = &bp->eee;
292672b34f04SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
2927a5390690SMichael Chan 	u32 advertising;
292872b34f04SMichael Chan 	int rc = 0;
292972b34f04SMichael Chan 
2930c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
293175362a3fSMichael Chan 		return -EOPNOTSUPP;
293272b34f04SMichael Chan 
2933b0d28207SMichael Chan 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
293472b34f04SMichael Chan 		return -EOPNOTSUPP;
293572b34f04SMichael Chan 
2936a5390690SMichael Chan 	mutex_lock(&bp->link_lock);
2937a5390690SMichael Chan 	advertising = _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
293872b34f04SMichael Chan 	if (!edata->eee_enabled)
293972b34f04SMichael Chan 		goto eee_ok;
294072b34f04SMichael Chan 
294172b34f04SMichael Chan 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
294272b34f04SMichael Chan 		netdev_warn(dev, "EEE requires autoneg\n");
2943a5390690SMichael Chan 		rc = -EINVAL;
2944a5390690SMichael Chan 		goto eee_exit;
294572b34f04SMichael Chan 	}
294672b34f04SMichael Chan 	if (edata->tx_lpi_enabled) {
294772b34f04SMichael Chan 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
294872b34f04SMichael Chan 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
294972b34f04SMichael Chan 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
295072b34f04SMichael Chan 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
2951a5390690SMichael Chan 			rc = -EINVAL;
2952a5390690SMichael Chan 			goto eee_exit;
295372b34f04SMichael Chan 		} else if (!bp->lpi_tmr_hi) {
295472b34f04SMichael Chan 			edata->tx_lpi_timer = eee->tx_lpi_timer;
295572b34f04SMichael Chan 		}
295672b34f04SMichael Chan 	}
295772b34f04SMichael Chan 	if (!edata->advertised) {
295872b34f04SMichael Chan 		edata->advertised = advertising & eee->supported;
295972b34f04SMichael Chan 	} else if (edata->advertised & ~advertising) {
296072b34f04SMichael Chan 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
296172b34f04SMichael Chan 			    edata->advertised, advertising);
2962a5390690SMichael Chan 		rc = -EINVAL;
2963a5390690SMichael Chan 		goto eee_exit;
296472b34f04SMichael Chan 	}
296572b34f04SMichael Chan 
296672b34f04SMichael Chan 	eee->advertised = edata->advertised;
296772b34f04SMichael Chan 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
296872b34f04SMichael Chan 	eee->tx_lpi_timer = edata->tx_lpi_timer;
296972b34f04SMichael Chan eee_ok:
297072b34f04SMichael Chan 	eee->eee_enabled = edata->eee_enabled;
297172b34f04SMichael Chan 
297272b34f04SMichael Chan 	if (netif_running(dev))
297372b34f04SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
297472b34f04SMichael Chan 
2975a5390690SMichael Chan eee_exit:
2976a5390690SMichael Chan 	mutex_unlock(&bp->link_lock);
297772b34f04SMichael Chan 	return rc;
297872b34f04SMichael Chan }
297972b34f04SMichael Chan 
298072b34f04SMichael Chan static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
298172b34f04SMichael Chan {
298272b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
298372b34f04SMichael Chan 
2984b0d28207SMichael Chan 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
298572b34f04SMichael Chan 		return -EOPNOTSUPP;
298672b34f04SMichael Chan 
298772b34f04SMichael Chan 	*edata = bp->eee;
298872b34f04SMichael Chan 	if (!bp->eee.eee_enabled) {
298972b34f04SMichael Chan 		/* Preserve tx_lpi_timer so that the last value will be used
299072b34f04SMichael Chan 		 * by default when it is re-enabled.
299172b34f04SMichael Chan 		 */
299272b34f04SMichael Chan 		edata->advertised = 0;
299372b34f04SMichael Chan 		edata->tx_lpi_enabled = 0;
299472b34f04SMichael Chan 	}
299572b34f04SMichael Chan 
299672b34f04SMichael Chan 	if (!bp->eee.eee_active)
299772b34f04SMichael Chan 		edata->lp_advertised = 0;
299872b34f04SMichael Chan 
299972b34f04SMichael Chan 	return 0;
300072b34f04SMichael Chan }
300172b34f04SMichael Chan 
300242ee18feSAjit Khaparde static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
300342ee18feSAjit Khaparde 					    u16 page_number, u16 start_addr,
300442ee18feSAjit Khaparde 					    u16 data_length, u8 *buf)
300542ee18feSAjit Khaparde {
300642ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_input req = {0};
300742ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
300842ee18feSAjit Khaparde 	int rc, byte_offset = 0;
300942ee18feSAjit Khaparde 
301042ee18feSAjit Khaparde 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
301142ee18feSAjit Khaparde 	req.i2c_slave_addr = i2c_addr;
301242ee18feSAjit Khaparde 	req.page_number = cpu_to_le16(page_number);
301342ee18feSAjit Khaparde 	req.port_id = cpu_to_le16(bp->pf.port_id);
301442ee18feSAjit Khaparde 	do {
301542ee18feSAjit Khaparde 		u16 xfer_size;
301642ee18feSAjit Khaparde 
301742ee18feSAjit Khaparde 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
301842ee18feSAjit Khaparde 		data_length -= xfer_size;
301942ee18feSAjit Khaparde 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
302042ee18feSAjit Khaparde 		req.data_length = xfer_size;
302142ee18feSAjit Khaparde 		req.enables = cpu_to_le32(start_addr + byte_offset ?
302242ee18feSAjit Khaparde 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
302342ee18feSAjit Khaparde 		mutex_lock(&bp->hwrm_cmd_lock);
302442ee18feSAjit Khaparde 		rc = _hwrm_send_message(bp, &req, sizeof(req),
302542ee18feSAjit Khaparde 					HWRM_CMD_TIMEOUT);
302642ee18feSAjit Khaparde 		if (!rc)
302742ee18feSAjit Khaparde 			memcpy(buf + byte_offset, output->data, xfer_size);
302842ee18feSAjit Khaparde 		mutex_unlock(&bp->hwrm_cmd_lock);
302942ee18feSAjit Khaparde 		byte_offset += xfer_size;
303042ee18feSAjit Khaparde 	} while (!rc && data_length > 0);
303142ee18feSAjit Khaparde 
303242ee18feSAjit Khaparde 	return rc;
303342ee18feSAjit Khaparde }
303442ee18feSAjit Khaparde 
303542ee18feSAjit Khaparde static int bnxt_get_module_info(struct net_device *dev,
303642ee18feSAjit Khaparde 				struct ethtool_modinfo *modinfo)
303742ee18feSAjit Khaparde {
30387328a23cSVasundhara Volam 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
303942ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
304042ee18feSAjit Khaparde 	int rc;
304142ee18feSAjit Khaparde 
304242ee18feSAjit Khaparde 	/* No point in going further if phy status indicates
304342ee18feSAjit Khaparde 	 * module is not inserted or if it is powered down or
304442ee18feSAjit Khaparde 	 * if it is of type 10GBase-T
304542ee18feSAjit Khaparde 	 */
304642ee18feSAjit Khaparde 	if (bp->link_info.module_status >
304742ee18feSAjit Khaparde 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
304842ee18feSAjit Khaparde 		return -EOPNOTSUPP;
304942ee18feSAjit Khaparde 
305042ee18feSAjit Khaparde 	/* This feature is not supported in older firmware versions */
305142ee18feSAjit Khaparde 	if (bp->hwrm_spec_code < 0x10202)
305242ee18feSAjit Khaparde 		return -EOPNOTSUPP;
305342ee18feSAjit Khaparde 
30547328a23cSVasundhara Volam 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
30557328a23cSVasundhara Volam 					      SFF_DIAG_SUPPORT_OFFSET + 1,
30567328a23cSVasundhara Volam 					      data);
305742ee18feSAjit Khaparde 	if (!rc) {
30587328a23cSVasundhara Volam 		u8 module_id = data[0];
30597328a23cSVasundhara Volam 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
306042ee18feSAjit Khaparde 
306142ee18feSAjit Khaparde 		switch (module_id) {
306242ee18feSAjit Khaparde 		case SFF_MODULE_ID_SFP:
306342ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8472;
306442ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
30657328a23cSVasundhara Volam 			if (!diag_supported)
30667328a23cSVasundhara Volam 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
306742ee18feSAjit Khaparde 			break;
306842ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP:
306942ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP_PLUS:
307042ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8436;
307142ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
307242ee18feSAjit Khaparde 			break;
307342ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP28:
307442ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8636;
307542ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
307642ee18feSAjit Khaparde 			break;
307742ee18feSAjit Khaparde 		default:
307842ee18feSAjit Khaparde 			rc = -EOPNOTSUPP;
307942ee18feSAjit Khaparde 			break;
308042ee18feSAjit Khaparde 		}
308142ee18feSAjit Khaparde 	}
308242ee18feSAjit Khaparde 	return rc;
308342ee18feSAjit Khaparde }
308442ee18feSAjit Khaparde 
308542ee18feSAjit Khaparde static int bnxt_get_module_eeprom(struct net_device *dev,
308642ee18feSAjit Khaparde 				  struct ethtool_eeprom *eeprom,
308742ee18feSAjit Khaparde 				  u8 *data)
308842ee18feSAjit Khaparde {
308942ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
309042ee18feSAjit Khaparde 	u16  start = eeprom->offset, length = eeprom->len;
3091f3ea3119SColin Ian King 	int rc = 0;
309242ee18feSAjit Khaparde 
309342ee18feSAjit Khaparde 	memset(data, 0, eeprom->len);
309442ee18feSAjit Khaparde 
309542ee18feSAjit Khaparde 	/* Read A0 portion of the EEPROM */
309642ee18feSAjit Khaparde 	if (start < ETH_MODULE_SFF_8436_LEN) {
309742ee18feSAjit Khaparde 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
309842ee18feSAjit Khaparde 			length = ETH_MODULE_SFF_8436_LEN - start;
309942ee18feSAjit Khaparde 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
310042ee18feSAjit Khaparde 						      start, length, data);
310142ee18feSAjit Khaparde 		if (rc)
310242ee18feSAjit Khaparde 			return rc;
310342ee18feSAjit Khaparde 		start += length;
310442ee18feSAjit Khaparde 		data += length;
310542ee18feSAjit Khaparde 		length = eeprom->len - length;
310642ee18feSAjit Khaparde 	}
310742ee18feSAjit Khaparde 
310842ee18feSAjit Khaparde 	/* Read A2 portion of the EEPROM */
310942ee18feSAjit Khaparde 	if (length) {
311042ee18feSAjit Khaparde 		start -= ETH_MODULE_SFF_8436_LEN;
31114260330bSEdwin Peer 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 0,
3112dea521a2SChristophe JAILLET 						      start, length, data);
311342ee18feSAjit Khaparde 	}
311442ee18feSAjit Khaparde 	return rc;
311542ee18feSAjit Khaparde }
311642ee18feSAjit Khaparde 
3117ae8e98a6SDeepak Khungar static int bnxt_nway_reset(struct net_device *dev)
3118ae8e98a6SDeepak Khungar {
3119ae8e98a6SDeepak Khungar 	int rc = 0;
3120ae8e98a6SDeepak Khungar 
3121ae8e98a6SDeepak Khungar 	struct bnxt *bp = netdev_priv(dev);
3122ae8e98a6SDeepak Khungar 	struct bnxt_link_info *link_info = &bp->link_info;
3123ae8e98a6SDeepak Khungar 
3124c7e457f4SMichael Chan 	if (!BNXT_PHY_CFG_ABLE(bp))
3125ae8e98a6SDeepak Khungar 		return -EOPNOTSUPP;
3126ae8e98a6SDeepak Khungar 
3127ae8e98a6SDeepak Khungar 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
3128ae8e98a6SDeepak Khungar 		return -EINVAL;
3129ae8e98a6SDeepak Khungar 
3130ae8e98a6SDeepak Khungar 	if (netif_running(dev))
3131ae8e98a6SDeepak Khungar 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
3132ae8e98a6SDeepak Khungar 
3133ae8e98a6SDeepak Khungar 	return rc;
3134ae8e98a6SDeepak Khungar }
3135ae8e98a6SDeepak Khungar 
31365ad2cbeeSMichael Chan static int bnxt_set_phys_id(struct net_device *dev,
31375ad2cbeeSMichael Chan 			    enum ethtool_phys_id_state state)
31385ad2cbeeSMichael Chan {
31395ad2cbeeSMichael Chan 	struct hwrm_port_led_cfg_input req = {0};
31405ad2cbeeSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
31415ad2cbeeSMichael Chan 	struct bnxt_pf_info *pf = &bp->pf;
31425ad2cbeeSMichael Chan 	struct bnxt_led_cfg *led_cfg;
31435ad2cbeeSMichael Chan 	u8 led_state;
31445ad2cbeeSMichael Chan 	__le16 duration;
31459f90445cSVasundhara Volam 	int i;
31465ad2cbeeSMichael Chan 
31475ad2cbeeSMichael Chan 	if (!bp->num_leds || BNXT_VF(bp))
31485ad2cbeeSMichael Chan 		return -EOPNOTSUPP;
31495ad2cbeeSMichael Chan 
31505ad2cbeeSMichael Chan 	if (state == ETHTOOL_ID_ACTIVE) {
31515ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
31525ad2cbeeSMichael Chan 		duration = cpu_to_le16(500);
31535ad2cbeeSMichael Chan 	} else if (state == ETHTOOL_ID_INACTIVE) {
31545ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
31555ad2cbeeSMichael Chan 		duration = cpu_to_le16(0);
31565ad2cbeeSMichael Chan 	} else {
31575ad2cbeeSMichael Chan 		return -EINVAL;
31585ad2cbeeSMichael Chan 	}
31595ad2cbeeSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
31605ad2cbeeSMichael Chan 	req.port_id = cpu_to_le16(pf->port_id);
31615ad2cbeeSMichael Chan 	req.num_leds = bp->num_leds;
31625ad2cbeeSMichael Chan 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
31635ad2cbeeSMichael Chan 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
31645ad2cbeeSMichael Chan 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
31655ad2cbeeSMichael Chan 		led_cfg->led_id = bp->leds[i].led_id;
31665ad2cbeeSMichael Chan 		led_cfg->led_state = led_state;
31675ad2cbeeSMichael Chan 		led_cfg->led_blink_on = duration;
31685ad2cbeeSMichael Chan 		led_cfg->led_blink_off = duration;
31695ad2cbeeSMichael Chan 		led_cfg->led_group_id = bp->leds[i].led_group_id;
31705ad2cbeeSMichael Chan 	}
31719f90445cSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
31725ad2cbeeSMichael Chan }
31735ad2cbeeSMichael Chan 
317467fea463SMichael Chan static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
317567fea463SMichael Chan {
317667fea463SMichael Chan 	struct hwrm_selftest_irq_input req = {0};
317767fea463SMichael Chan 
317867fea463SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
317967fea463SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
318067fea463SMichael Chan }
318167fea463SMichael Chan 
318267fea463SMichael Chan static int bnxt_test_irq(struct bnxt *bp)
318367fea463SMichael Chan {
318467fea463SMichael Chan 	int i;
318567fea463SMichael Chan 
318667fea463SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
318767fea463SMichael Chan 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
318867fea463SMichael Chan 		int rc;
318967fea463SMichael Chan 
319067fea463SMichael Chan 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
319167fea463SMichael Chan 		if (rc)
319267fea463SMichael Chan 			return rc;
319367fea463SMichael Chan 	}
319467fea463SMichael Chan 	return 0;
319567fea463SMichael Chan }
319667fea463SMichael Chan 
3197f7dc1ea6SMichael Chan static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
3198f7dc1ea6SMichael Chan {
3199f7dc1ea6SMichael Chan 	struct hwrm_port_mac_cfg_input req = {0};
3200f7dc1ea6SMichael Chan 
3201f7dc1ea6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
3202f7dc1ea6SMichael Chan 
3203f7dc1ea6SMichael Chan 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
3204f7dc1ea6SMichael Chan 	if (enable)
3205f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
3206f7dc1ea6SMichael Chan 	else
3207f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
3208f7dc1ea6SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3209f7dc1ea6SMichael Chan }
3210f7dc1ea6SMichael Chan 
321156d37462SVasundhara Volam static int bnxt_query_force_speeds(struct bnxt *bp, u16 *force_speeds)
321256d37462SVasundhara Volam {
321356d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
321456d37462SVasundhara Volam 	struct hwrm_port_phy_qcaps_input req = {0};
321556d37462SVasundhara Volam 	int rc;
321656d37462SVasundhara Volam 
321756d37462SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
321856d37462SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
321956d37462SVasundhara Volam 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
322056d37462SVasundhara Volam 	if (!rc)
322156d37462SVasundhara Volam 		*force_speeds = le16_to_cpu(resp->supported_speeds_force_mode);
322256d37462SVasundhara Volam 
322356d37462SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
322456d37462SVasundhara Volam 	return rc;
322556d37462SVasundhara Volam }
322656d37462SVasundhara Volam 
322791725d89SMichael Chan static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
322891725d89SMichael Chan 				    struct hwrm_port_phy_cfg_input *req)
322991725d89SMichael Chan {
323091725d89SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
323156d37462SVasundhara Volam 	u16 fw_advertising;
323291725d89SMichael Chan 	u16 fw_speed;
323391725d89SMichael Chan 	int rc;
323491725d89SMichael Chan 
32358a60efd1SMichael Chan 	if (!link_info->autoneg ||
3236b0d28207SMichael Chan 	    (bp->phy_flags & BNXT_PHY_FL_AN_PHY_LPBK))
323791725d89SMichael Chan 		return 0;
323891725d89SMichael Chan 
323956d37462SVasundhara Volam 	rc = bnxt_query_force_speeds(bp, &fw_advertising);
324056d37462SVasundhara Volam 	if (rc)
324156d37462SVasundhara Volam 		return rc;
324256d37462SVasundhara Volam 
324391725d89SMichael Chan 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
324483d8f5e9SMichael Chan 	if (bp->link_info.link_up)
324591725d89SMichael Chan 		fw_speed = bp->link_info.link_speed;
324691725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
324791725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
324891725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
324991725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
325091725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
325191725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
325291725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
325391725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
325491725d89SMichael Chan 
325591725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(fw_speed);
325691725d89SMichael Chan 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
325791725d89SMichael Chan 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
325891725d89SMichael Chan 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
325991725d89SMichael Chan 	req->flags = 0;
326091725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(0);
326191725d89SMichael Chan 	return rc;
326291725d89SMichael Chan }
326391725d89SMichael Chan 
326455fd0cf3SMichael Chan static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
326591725d89SMichael Chan {
326691725d89SMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
326791725d89SMichael Chan 
326891725d89SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
326991725d89SMichael Chan 
327091725d89SMichael Chan 	if (enable) {
327191725d89SMichael Chan 		bnxt_disable_an_for_lpbk(bp, &req);
327255fd0cf3SMichael Chan 		if (ext)
327355fd0cf3SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
327455fd0cf3SMichael Chan 		else
327591725d89SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
327691725d89SMichael Chan 	} else {
327791725d89SMichael Chan 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
327891725d89SMichael Chan 	}
327991725d89SMichael Chan 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
328091725d89SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
328191725d89SMichael Chan }
328291725d89SMichael Chan 
3283e44758b7SMichael Chan static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3284f7dc1ea6SMichael Chan 			    u32 raw_cons, int pkt_size)
3285f7dc1ea6SMichael Chan {
3286e44758b7SMichael Chan 	struct bnxt_napi *bnapi = cpr->bnapi;
3287e44758b7SMichael Chan 	struct bnxt_rx_ring_info *rxr;
3288f7dc1ea6SMichael Chan 	struct bnxt_sw_rx_bd *rx_buf;
3289f7dc1ea6SMichael Chan 	struct rx_cmp *rxcmp;
3290f7dc1ea6SMichael Chan 	u16 cp_cons, cons;
3291f7dc1ea6SMichael Chan 	u8 *data;
3292f7dc1ea6SMichael Chan 	u32 len;
3293f7dc1ea6SMichael Chan 	int i;
3294f7dc1ea6SMichael Chan 
3295e44758b7SMichael Chan 	rxr = bnapi->rx_ring;
3296f7dc1ea6SMichael Chan 	cp_cons = RING_CMP(raw_cons);
3297f7dc1ea6SMichael Chan 	rxcmp = (struct rx_cmp *)
3298f7dc1ea6SMichael Chan 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3299f7dc1ea6SMichael Chan 	cons = rxcmp->rx_cmp_opaque;
3300f7dc1ea6SMichael Chan 	rx_buf = &rxr->rx_buf_ring[cons];
3301f7dc1ea6SMichael Chan 	data = rx_buf->data_ptr;
3302f7dc1ea6SMichael Chan 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
3303f7dc1ea6SMichael Chan 	if (len != pkt_size)
3304f7dc1ea6SMichael Chan 		return -EIO;
3305f7dc1ea6SMichael Chan 	i = ETH_ALEN;
3306f7dc1ea6SMichael Chan 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
3307f7dc1ea6SMichael Chan 		return -EIO;
3308f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3309f7dc1ea6SMichael Chan 	for (  ; i < pkt_size; i++) {
3310f7dc1ea6SMichael Chan 		if (data[i] != (u8)(i & 0xff))
3311f7dc1ea6SMichael Chan 			return -EIO;
3312f7dc1ea6SMichael Chan 	}
3313f7dc1ea6SMichael Chan 	return 0;
3314f7dc1ea6SMichael Chan }
3315f7dc1ea6SMichael Chan 
3316e44758b7SMichael Chan static int bnxt_poll_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3317e44758b7SMichael Chan 			      int pkt_size)
3318f7dc1ea6SMichael Chan {
3319f7dc1ea6SMichael Chan 	struct tx_cmp *txcmp;
3320f7dc1ea6SMichael Chan 	int rc = -EIO;
3321f7dc1ea6SMichael Chan 	u32 raw_cons;
3322f7dc1ea6SMichael Chan 	u32 cons;
3323f7dc1ea6SMichael Chan 	int i;
3324f7dc1ea6SMichael Chan 
3325f7dc1ea6SMichael Chan 	raw_cons = cpr->cp_raw_cons;
3326f7dc1ea6SMichael Chan 	for (i = 0; i < 200; i++) {
3327f7dc1ea6SMichael Chan 		cons = RING_CMP(raw_cons);
3328f7dc1ea6SMichael Chan 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3329f7dc1ea6SMichael Chan 
3330f7dc1ea6SMichael Chan 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
3331f7dc1ea6SMichael Chan 			udelay(5);
3332f7dc1ea6SMichael Chan 			continue;
3333f7dc1ea6SMichael Chan 		}
3334f7dc1ea6SMichael Chan 
3335f7dc1ea6SMichael Chan 		/* The valid test of the entry must be done first before
3336f7dc1ea6SMichael Chan 		 * reading any further.
3337f7dc1ea6SMichael Chan 		 */
3338f7dc1ea6SMichael Chan 		dma_rmb();
3339f7dc1ea6SMichael Chan 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
3340e44758b7SMichael Chan 			rc = bnxt_rx_loopback(bp, cpr, raw_cons, pkt_size);
3341f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
3342f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
3343f7dc1ea6SMichael Chan 			break;
3344f7dc1ea6SMichael Chan 		}
3345f7dc1ea6SMichael Chan 		raw_cons = NEXT_RAW_CMP(raw_cons);
3346f7dc1ea6SMichael Chan 	}
3347f7dc1ea6SMichael Chan 	cpr->cp_raw_cons = raw_cons;
3348f7dc1ea6SMichael Chan 	return rc;
3349f7dc1ea6SMichael Chan }
3350f7dc1ea6SMichael Chan 
3351f7dc1ea6SMichael Chan static int bnxt_run_loopback(struct bnxt *bp)
3352f7dc1ea6SMichael Chan {
3353f7dc1ea6SMichael Chan 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
335484404d5fSMichael Chan 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
3355e44758b7SMichael Chan 	struct bnxt_cp_ring_info *cpr;
3356f7dc1ea6SMichael Chan 	int pkt_size, i = 0;
3357f7dc1ea6SMichael Chan 	struct sk_buff *skb;
3358f7dc1ea6SMichael Chan 	dma_addr_t map;
3359f7dc1ea6SMichael Chan 	u8 *data;
3360f7dc1ea6SMichael Chan 	int rc;
3361f7dc1ea6SMichael Chan 
336284404d5fSMichael Chan 	cpr = &rxr->bnapi->cp_ring;
336384404d5fSMichael Chan 	if (bp->flags & BNXT_FLAG_CHIP_P5)
336484404d5fSMichael Chan 		cpr = cpr->cp_ring_arr[BNXT_RX_HDL];
3365f7dc1ea6SMichael Chan 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
3366f7dc1ea6SMichael Chan 	skb = netdev_alloc_skb(bp->dev, pkt_size);
3367f7dc1ea6SMichael Chan 	if (!skb)
3368f7dc1ea6SMichael Chan 		return -ENOMEM;
3369f7dc1ea6SMichael Chan 	data = skb_put(skb, pkt_size);
3370f7dc1ea6SMichael Chan 	eth_broadcast_addr(data);
3371f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3372f7dc1ea6SMichael Chan 	ether_addr_copy(&data[i], bp->dev->dev_addr);
3373f7dc1ea6SMichael Chan 	i += ETH_ALEN;
3374f7dc1ea6SMichael Chan 	for ( ; i < pkt_size; i++)
3375f7dc1ea6SMichael Chan 		data[i] = (u8)(i & 0xff);
3376f7dc1ea6SMichael Chan 
3377f7dc1ea6SMichael Chan 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
3378df70303dSChristophe JAILLET 			     DMA_TO_DEVICE);
3379f7dc1ea6SMichael Chan 	if (dma_mapping_error(&bp->pdev->dev, map)) {
3380f7dc1ea6SMichael Chan 		dev_kfree_skb(skb);
3381f7dc1ea6SMichael Chan 		return -EIO;
3382f7dc1ea6SMichael Chan 	}
3383c1ba92a8SMichael Chan 	bnxt_xmit_bd(bp, txr, map, pkt_size);
3384f7dc1ea6SMichael Chan 
3385f7dc1ea6SMichael Chan 	/* Sync BD data before updating doorbell */
3386f7dc1ea6SMichael Chan 	wmb();
3387f7dc1ea6SMichael Chan 
3388697197e5SMichael Chan 	bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
3389e44758b7SMichael Chan 	rc = bnxt_poll_loopback(bp, cpr, pkt_size);
3390f7dc1ea6SMichael Chan 
3391df70303dSChristophe JAILLET 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, DMA_TO_DEVICE);
3392f7dc1ea6SMichael Chan 	dev_kfree_skb(skb);
3393f7dc1ea6SMichael Chan 	return rc;
3394f7dc1ea6SMichael Chan }
3395f7dc1ea6SMichael Chan 
3396eb513658SMichael Chan static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
3397eb513658SMichael Chan {
3398eb513658SMichael Chan 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
3399eb513658SMichael Chan 	struct hwrm_selftest_exec_input req = {0};
3400eb513658SMichael Chan 	int rc;
3401eb513658SMichael Chan 
3402eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
3403eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3404eb513658SMichael Chan 	resp->test_success = 0;
3405eb513658SMichael Chan 	req.flags = test_mask;
3406eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
3407eb513658SMichael Chan 	*test_results = resp->test_success;
3408eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3409eb513658SMichael Chan 	return rc;
3410eb513658SMichael Chan }
3411eb513658SMichael Chan 
341255fd0cf3SMichael Chan #define BNXT_DRV_TESTS			4
3413f7dc1ea6SMichael Chan #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
341491725d89SMichael Chan #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
341555fd0cf3SMichael Chan #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
341655fd0cf3SMichael Chan #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
3417eb513658SMichael Chan 
3418eb513658SMichael Chan static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
3419eb513658SMichael Chan 			   u64 *buf)
3420eb513658SMichael Chan {
3421eb513658SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
342255fd0cf3SMichael Chan 	bool do_ext_lpbk = false;
3423eb513658SMichael Chan 	bool offline = false;
3424eb513658SMichael Chan 	u8 test_results = 0;
3425eb513658SMichael Chan 	u8 test_mask = 0;
3426d27e2ca1SMichael Chan 	int rc = 0, i;
3427eb513658SMichael Chan 
34286896cb35SVasundhara Volam 	if (!bp->num_tests || !BNXT_PF(bp))
3429eb513658SMichael Chan 		return;
3430eb513658SMichael Chan 	memset(buf, 0, sizeof(u64) * bp->num_tests);
3431eb513658SMichael Chan 	if (!netif_running(dev)) {
3432eb513658SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
3433eb513658SMichael Chan 		return;
3434eb513658SMichael Chan 	}
3435eb513658SMichael Chan 
343655fd0cf3SMichael Chan 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
3437b0d28207SMichael Chan 	    (bp->phy_flags & BNXT_PHY_FL_EXT_LPBK))
343855fd0cf3SMichael Chan 		do_ext_lpbk = true;
343955fd0cf3SMichael Chan 
3440eb513658SMichael Chan 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
34416896cb35SVasundhara Volam 		if (bp->pf.active_vfs || !BNXT_SINGLE_PF(bp)) {
3442eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
34436896cb35SVasundhara Volam 			netdev_warn(dev, "Offline tests cannot be run with active VFs or on shared PF\n");
3444eb513658SMichael Chan 			return;
3445eb513658SMichael Chan 		}
3446eb513658SMichael Chan 		offline = true;
3447eb513658SMichael Chan 	}
3448eb513658SMichael Chan 
3449eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3450eb513658SMichael Chan 		u8 bit_val = 1 << i;
3451eb513658SMichael Chan 
3452eb513658SMichael Chan 		if (!(bp->test_info->offline_mask & bit_val))
3453eb513658SMichael Chan 			test_mask |= bit_val;
3454eb513658SMichael Chan 		else if (offline)
3455eb513658SMichael Chan 			test_mask |= bit_val;
3456eb513658SMichael Chan 	}
3457eb513658SMichael Chan 	if (!offline) {
3458eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3459eb513658SMichael Chan 	} else {
3460eb513658SMichael Chan 		rc = bnxt_close_nic(bp, false, false);
3461eb513658SMichael Chan 		if (rc)
3462eb513658SMichael Chan 			return;
3463eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
3464f7dc1ea6SMichael Chan 
3465f7dc1ea6SMichael Chan 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
3466f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, true);
3467f7dc1ea6SMichael Chan 		msleep(250);
3468f7dc1ea6SMichael Chan 		rc = bnxt_half_open_nic(bp);
3469f7dc1ea6SMichael Chan 		if (rc) {
3470f7dc1ea6SMichael Chan 			bnxt_hwrm_mac_loopback(bp, false);
3471f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3472f7dc1ea6SMichael Chan 			return;
3473f7dc1ea6SMichael Chan 		}
3474f7dc1ea6SMichael Chan 		if (bnxt_run_loopback(bp))
3475f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3476f7dc1ea6SMichael Chan 		else
3477f7dc1ea6SMichael Chan 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
3478f7dc1ea6SMichael Chan 
3479f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, false);
348055fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, true, false);
348191725d89SMichael Chan 		msleep(1000);
348291725d89SMichael Chan 		if (bnxt_run_loopback(bp)) {
348391725d89SMichael Chan 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
348491725d89SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
348591725d89SMichael Chan 		}
348655fd0cf3SMichael Chan 		if (do_ext_lpbk) {
348755fd0cf3SMichael Chan 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
348855fd0cf3SMichael Chan 			bnxt_hwrm_phy_loopback(bp, true, true);
348955fd0cf3SMichael Chan 			msleep(1000);
349055fd0cf3SMichael Chan 			if (bnxt_run_loopback(bp)) {
349155fd0cf3SMichael Chan 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
349255fd0cf3SMichael Chan 				etest->flags |= ETH_TEST_FL_FAILED;
349355fd0cf3SMichael Chan 			}
349455fd0cf3SMichael Chan 		}
349555fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, false, false);
349691725d89SMichael Chan 		bnxt_half_close_nic(bp);
3497d27e2ca1SMichael Chan 		rc = bnxt_open_nic(bp, false, true);
3498eb513658SMichael Chan 	}
3499d27e2ca1SMichael Chan 	if (rc || bnxt_test_irq(bp)) {
350067fea463SMichael Chan 		buf[BNXT_IRQ_TEST_IDX] = 1;
350167fea463SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
350267fea463SMichael Chan 	}
3503eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
3504eb513658SMichael Chan 		u8 bit_val = 1 << i;
3505eb513658SMichael Chan 
3506eb513658SMichael Chan 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
3507eb513658SMichael Chan 			buf[i] = 1;
3508eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
3509eb513658SMichael Chan 		}
3510eb513658SMichael Chan 	}
3511eb513658SMichael Chan }
3512eb513658SMichael Chan 
351349f7972fSVasundhara Volam static int bnxt_reset(struct net_device *dev, u32 *flags)
351449f7972fSVasundhara Volam {
351549f7972fSVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
35168cec0940SEdwin Peer 	bool reload = false;
35177a13240eSEdwin Peer 	u32 req = *flags;
35187a13240eSEdwin Peer 
35197a13240eSEdwin Peer 	if (!req)
35207a13240eSEdwin Peer 		return -EINVAL;
352149f7972fSVasundhara Volam 
352249f7972fSVasundhara Volam 	if (!BNXT_PF(bp)) {
352349f7972fSVasundhara Volam 		netdev_err(dev, "Reset is not supported from a VF\n");
352449f7972fSVasundhara Volam 		return -EOPNOTSUPP;
352549f7972fSVasundhara Volam 	}
352649f7972fSVasundhara Volam 
35270a3f4e4fSVasundhara Volam 	if (pci_vfs_assigned(bp->pdev) &&
35280a3f4e4fSVasundhara Volam 	    !(bp->fw_cap & BNXT_FW_CAP_HOT_RESET)) {
352949f7972fSVasundhara Volam 		netdev_err(dev,
353049f7972fSVasundhara Volam 			   "Reset not allowed when VFs are assigned to VMs\n");
353149f7972fSVasundhara Volam 		return -EBUSY;
353249f7972fSVasundhara Volam 	}
353349f7972fSVasundhara Volam 
35347a13240eSEdwin Peer 	if ((req & BNXT_FW_RESET_CHIP) == BNXT_FW_RESET_CHIP) {
353549f7972fSVasundhara Volam 		/* This feature is not supported in older firmware versions */
35367a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
35377a13240eSEdwin Peer 			if (!bnxt_firmware_reset_chip(dev)) {
35387a13240eSEdwin Peer 				netdev_info(dev, "Firmware reset request successful.\n");
35390a3f4e4fSVasundhara Volam 				if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET))
35408cec0940SEdwin Peer 					reload = true;
35417a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_CHIP;
35422373d8d6SScott Branden 			}
35437a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_CHIP) {
35447a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
35457a13240eSEdwin Peer 		}
35467a13240eSEdwin Peer 	}
35477a13240eSEdwin Peer 
35487a13240eSEdwin Peer 	if (req & BNXT_FW_RESET_AP) {
35496502ad59SScott Branden 		/* This feature is not supported in older firmware versions */
35507a13240eSEdwin Peer 		if (bp->hwrm_spec_code >= 0x10803) {
35517a13240eSEdwin Peer 			if (!bnxt_firmware_reset_ap(dev)) {
35527a13240eSEdwin Peer 				netdev_info(dev, "Reset application processor successful.\n");
35538cec0940SEdwin Peer 				reload = true;
35547a13240eSEdwin Peer 				*flags &= ~BNXT_FW_RESET_AP;
35552373d8d6SScott Branden 			}
35567a13240eSEdwin Peer 		} else if (req == BNXT_FW_RESET_AP) {
35577a13240eSEdwin Peer 			return -EOPNOTSUPP; /* only request, fail hard */
35587a13240eSEdwin Peer 		}
355949f7972fSVasundhara Volam 	}
356049f7972fSVasundhara Volam 
35618cec0940SEdwin Peer 	if (reload)
35628cec0940SEdwin Peer 		netdev_info(dev, "Reload driver to complete reset\n");
35638cec0940SEdwin Peer 
35647a13240eSEdwin Peer 	return 0;
356549f7972fSVasundhara Volam }
356649f7972fSVasundhara Volam 
35676c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
35686c5657d0SVasundhara Volam 				  struct bnxt_hwrm_dbg_dma_info *info)
35696c5657d0SVasundhara Volam {
35706c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
35716c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_input *cmn_req = msg;
35726c5657d0SVasundhara Volam 	__le16 *seq_ptr = msg + info->seq_off;
35736c5657d0SVasundhara Volam 	u16 seq = 0, len, segs_off;
35746c5657d0SVasundhara Volam 	void *resp = cmn_resp;
35756c5657d0SVasundhara Volam 	dma_addr_t dma_handle;
35766c5657d0SVasundhara Volam 	int rc, off = 0;
35776c5657d0SVasundhara Volam 	void *dma_buf;
35786c5657d0SVasundhara Volam 
35796c5657d0SVasundhara Volam 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
35806c5657d0SVasundhara Volam 				     GFP_KERNEL);
35816c5657d0SVasundhara Volam 	if (!dma_buf)
35826c5657d0SVasundhara Volam 		return -ENOMEM;
35836c5657d0SVasundhara Volam 
35846c5657d0SVasundhara Volam 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
35856c5657d0SVasundhara Volam 			    total_segments);
35866c5657d0SVasundhara Volam 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
35876c5657d0SVasundhara Volam 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
35886c5657d0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
35896c5657d0SVasundhara Volam 	while (1) {
35906c5657d0SVasundhara Volam 		*seq_ptr = cpu_to_le16(seq);
35915b306bdeSVasundhara Volam 		rc = _hwrm_send_message(bp, msg, msg_len,
35925b306bdeSVasundhara Volam 					HWRM_COREDUMP_TIMEOUT);
35936c5657d0SVasundhara Volam 		if (rc)
35946c5657d0SVasundhara Volam 			break;
35956c5657d0SVasundhara Volam 
35966c5657d0SVasundhara Volam 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
35976c5657d0SVasundhara Volam 		if (!seq &&
35986c5657d0SVasundhara Volam 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
35996c5657d0SVasundhara Volam 			info->segs = le16_to_cpu(*((__le16 *)(resp +
36006c5657d0SVasundhara Volam 							      segs_off)));
36016c5657d0SVasundhara Volam 			if (!info->segs) {
36026c5657d0SVasundhara Volam 				rc = -EIO;
36036c5657d0SVasundhara Volam 				break;
36046c5657d0SVasundhara Volam 			}
36056c5657d0SVasundhara Volam 
36066c5657d0SVasundhara Volam 			info->dest_buf_size = info->segs *
36076c5657d0SVasundhara Volam 					sizeof(struct coredump_segment_record);
36086c5657d0SVasundhara Volam 			info->dest_buf = kmalloc(info->dest_buf_size,
36096c5657d0SVasundhara Volam 						 GFP_KERNEL);
36106c5657d0SVasundhara Volam 			if (!info->dest_buf) {
36116c5657d0SVasundhara Volam 				rc = -ENOMEM;
36126c5657d0SVasundhara Volam 				break;
36136c5657d0SVasundhara Volam 			}
36146c5657d0SVasundhara Volam 		}
36156c5657d0SVasundhara Volam 
3616c74751f4SVasundhara Volam 		if (info->dest_buf) {
3617c74751f4SVasundhara Volam 			if ((info->seg_start + off + len) <=
3618c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
36196c5657d0SVasundhara Volam 				memcpy(info->dest_buf + off, dma_buf, len);
3620c74751f4SVasundhara Volam 			} else {
3621c74751f4SVasundhara Volam 				rc = -ENOBUFS;
3622c74751f4SVasundhara Volam 				break;
3623c74751f4SVasundhara Volam 			}
3624c74751f4SVasundhara Volam 		}
36256c5657d0SVasundhara Volam 
36266c5657d0SVasundhara Volam 		if (cmn_req->req_type ==
36276c5657d0SVasundhara Volam 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
36286c5657d0SVasundhara Volam 			info->dest_buf_size += len;
36296c5657d0SVasundhara Volam 
36306c5657d0SVasundhara Volam 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
36316c5657d0SVasundhara Volam 			break;
36326c5657d0SVasundhara Volam 
36336c5657d0SVasundhara Volam 		seq++;
36346c5657d0SVasundhara Volam 		off += len;
36356c5657d0SVasundhara Volam 	}
36366c5657d0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
36376c5657d0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
36386c5657d0SVasundhara Volam 	return rc;
36396c5657d0SVasundhara Volam }
36406c5657d0SVasundhara Volam 
36416c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
36426c5657d0SVasundhara Volam 				       struct bnxt_coredump *coredump)
36436c5657d0SVasundhara Volam {
36446c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_list_input req = {0};
36456c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
36466c5657d0SVasundhara Volam 	int rc;
36476c5657d0SVasundhara Volam 
36486c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
36496c5657d0SVasundhara Volam 
36506c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_LIST_BUF_LEN;
36516c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
36526c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
36536c5657d0SVasundhara Volam 				     data_len);
36546c5657d0SVasundhara Volam 
36556c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
36566c5657d0SVasundhara Volam 	if (!rc) {
36576c5657d0SVasundhara Volam 		coredump->data = info.dest_buf;
36586c5657d0SVasundhara Volam 		coredump->data_size = info.dest_buf_size;
36596c5657d0SVasundhara Volam 		coredump->total_segs = info.segs;
36606c5657d0SVasundhara Volam 	}
36616c5657d0SVasundhara Volam 	return rc;
36626c5657d0SVasundhara Volam }
36636c5657d0SVasundhara Volam 
36646c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
36656c5657d0SVasundhara Volam 					   u16 segment_id)
36666c5657d0SVasundhara Volam {
36676c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_initiate_input req = {0};
36686c5657d0SVasundhara Volam 
36696c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
36706c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
36716c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
36726c5657d0SVasundhara Volam 
367357a8730bSVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_COREDUMP_TIMEOUT);
36746c5657d0SVasundhara Volam }
36756c5657d0SVasundhara Volam 
36766c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
36776c5657d0SVasundhara Volam 					   u16 segment_id, u32 *seg_len,
3678c74751f4SVasundhara Volam 					   void *buf, u32 buf_len, u32 offset)
36796c5657d0SVasundhara Volam {
36806c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_retrieve_input req = {0};
36816c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
36826c5657d0SVasundhara Volam 	int rc;
36836c5657d0SVasundhara Volam 
36846c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
36856c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
36866c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
36876c5657d0SVasundhara Volam 
36886c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
36896c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
36906c5657d0SVasundhara Volam 				seq_no);
36916c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
36926c5657d0SVasundhara Volam 				     data_len);
3693c74751f4SVasundhara Volam 	if (buf) {
36946c5657d0SVasundhara Volam 		info.dest_buf = buf + offset;
3695c74751f4SVasundhara Volam 		info.buf_len = buf_len;
3696c74751f4SVasundhara Volam 		info.seg_start = offset;
3697c74751f4SVasundhara Volam 	}
36986c5657d0SVasundhara Volam 
36996c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
37006c5657d0SVasundhara Volam 	if (!rc)
37016c5657d0SVasundhara Volam 		*seg_len = info.dest_buf_size;
37026c5657d0SVasundhara Volam 
37036c5657d0SVasundhara Volam 	return rc;
37046c5657d0SVasundhara Volam }
37056c5657d0SVasundhara Volam 
37066c5657d0SVasundhara Volam static void
37076c5657d0SVasundhara Volam bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
37086c5657d0SVasundhara Volam 			   struct bnxt_coredump_segment_hdr *seg_hdr,
37096c5657d0SVasundhara Volam 			   struct coredump_segment_record *seg_rec, u32 seg_len,
37106c5657d0SVasundhara Volam 			   int status, u32 duration, u32 instance)
37116c5657d0SVasundhara Volam {
37126c5657d0SVasundhara Volam 	memset(seg_hdr, 0, sizeof(*seg_hdr));
37138605212aSVasundhara Volam 	memcpy(seg_hdr->signature, "sEgM", 4);
37146c5657d0SVasundhara Volam 	if (seg_rec) {
37156c5657d0SVasundhara Volam 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
37166c5657d0SVasundhara Volam 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
37176c5657d0SVasundhara Volam 		seg_hdr->low_version = seg_rec->version_low;
37186c5657d0SVasundhara Volam 		seg_hdr->high_version = seg_rec->version_hi;
37196c5657d0SVasundhara Volam 	} else {
37206c5657d0SVasundhara Volam 		/* For hwrm_ver_get response Component id = 2
37216c5657d0SVasundhara Volam 		 * and Segment id = 0
37226c5657d0SVasundhara Volam 		 */
37236c5657d0SVasundhara Volam 		seg_hdr->component_id = cpu_to_le32(2);
37246c5657d0SVasundhara Volam 		seg_hdr->segment_id = 0;
37256c5657d0SVasundhara Volam 	}
37266c5657d0SVasundhara Volam 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
37276c5657d0SVasundhara Volam 	seg_hdr->length = cpu_to_le32(seg_len);
37286c5657d0SVasundhara Volam 	seg_hdr->status = cpu_to_le32(status);
37296c5657d0SVasundhara Volam 	seg_hdr->duration = cpu_to_le32(duration);
37306c5657d0SVasundhara Volam 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
37316c5657d0SVasundhara Volam 	seg_hdr->instance = cpu_to_le32(instance);
37326c5657d0SVasundhara Volam }
37336c5657d0SVasundhara Volam 
37346c5657d0SVasundhara Volam static void
37356c5657d0SVasundhara Volam bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
37366c5657d0SVasundhara Volam 			  time64_t start, s16 start_utc, u16 total_segs,
37376c5657d0SVasundhara Volam 			  int status)
37386c5657d0SVasundhara Volam {
37396c5657d0SVasundhara Volam 	time64_t end = ktime_get_real_seconds();
37406c5657d0SVasundhara Volam 	u32 os_ver_major = 0, os_ver_minor = 0;
37416c5657d0SVasundhara Volam 	struct tm tm;
37426c5657d0SVasundhara Volam 
37436c5657d0SVasundhara Volam 	time64_to_tm(start, 0, &tm);
37446c5657d0SVasundhara Volam 	memset(record, 0, sizeof(*record));
37458605212aSVasundhara Volam 	memcpy(record->signature, "cOrE", 4);
37466c5657d0SVasundhara Volam 	record->flags = 0;
37476c5657d0SVasundhara Volam 	record->low_version = 0;
37486c5657d0SVasundhara Volam 	record->high_version = 1;
37496c5657d0SVasundhara Volam 	record->asic_state = 0;
37503d46eee5SArnd Bergmann 	strlcpy(record->system_name, utsname()->nodename,
37513d46eee5SArnd Bergmann 		sizeof(record->system_name));
37528dc5ae2dSVasundhara Volam 	record->year = cpu_to_le16(tm.tm_year + 1900);
37538dc5ae2dSVasundhara Volam 	record->month = cpu_to_le16(tm.tm_mon + 1);
37546c5657d0SVasundhara Volam 	record->day = cpu_to_le16(tm.tm_mday);
37556c5657d0SVasundhara Volam 	record->hour = cpu_to_le16(tm.tm_hour);
37566c5657d0SVasundhara Volam 	record->minute = cpu_to_le16(tm.tm_min);
37576c5657d0SVasundhara Volam 	record->second = cpu_to_le16(tm.tm_sec);
37586c5657d0SVasundhara Volam 	record->utc_bias = cpu_to_le16(start_utc);
37596c5657d0SVasundhara Volam 	strcpy(record->commandline, "ethtool -w");
37606c5657d0SVasundhara Volam 	record->total_segments = cpu_to_le32(total_segs);
37616c5657d0SVasundhara Volam 
37626c5657d0SVasundhara Volam 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
37636c5657d0SVasundhara Volam 	record->os_ver_major = cpu_to_le32(os_ver_major);
37646c5657d0SVasundhara Volam 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
37656c5657d0SVasundhara Volam 
37668605212aSVasundhara Volam 	strlcpy(record->os_name, utsname()->sysname, 32);
37676c5657d0SVasundhara Volam 	time64_to_tm(end, 0, &tm);
37686c5657d0SVasundhara Volam 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
37696c5657d0SVasundhara Volam 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
37706c5657d0SVasundhara Volam 	record->end_day = cpu_to_le16(tm.tm_mday);
37716c5657d0SVasundhara Volam 	record->end_hour = cpu_to_le16(tm.tm_hour);
37726c5657d0SVasundhara Volam 	record->end_minute = cpu_to_le16(tm.tm_min);
37736c5657d0SVasundhara Volam 	record->end_second = cpu_to_le16(tm.tm_sec);
37746c5657d0SVasundhara Volam 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
37756c5657d0SVasundhara Volam 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
37766c5657d0SVasundhara Volam 				       bp->ver_resp.chip_rev << 8 |
37776c5657d0SVasundhara Volam 				       bp->ver_resp.chip_metal);
37786c5657d0SVasundhara Volam 	record->asic_id2 = 0;
37796c5657d0SVasundhara Volam 	record->coredump_status = cpu_to_le32(status);
37806c5657d0SVasundhara Volam 	record->ioctl_low_version = 0;
37816c5657d0SVasundhara Volam 	record->ioctl_high_version = 0;
37826c5657d0SVasundhara Volam }
37836c5657d0SVasundhara Volam 
37846c5657d0SVasundhara Volam static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
37856c5657d0SVasundhara Volam {
37866c5657d0SVasundhara Volam 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
3787c74751f4SVasundhara Volam 	u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
37886c5657d0SVasundhara Volam 	struct coredump_segment_record *seg_record = NULL;
37896c5657d0SVasundhara Volam 	struct bnxt_coredump_segment_hdr seg_hdr;
37906c5657d0SVasundhara Volam 	struct bnxt_coredump coredump = {NULL};
37916c5657d0SVasundhara Volam 	time64_t start_time;
37926c5657d0SVasundhara Volam 	u16 start_utc;
37936c5657d0SVasundhara Volam 	int rc = 0, i;
37946c5657d0SVasundhara Volam 
3795c74751f4SVasundhara Volam 	if (buf)
3796c74751f4SVasundhara Volam 		buf_len = *dump_len;
3797c74751f4SVasundhara Volam 
37986c5657d0SVasundhara Volam 	start_time = ktime_get_real_seconds();
37996c5657d0SVasundhara Volam 	start_utc = sys_tz.tz_minuteswest * 60;
38006c5657d0SVasundhara Volam 	seg_hdr_len = sizeof(seg_hdr);
38016c5657d0SVasundhara Volam 
38026c5657d0SVasundhara Volam 	/* First segment should be hwrm_ver_get response */
38036c5657d0SVasundhara Volam 	*dump_len = seg_hdr_len + ver_get_resp_len;
38046c5657d0SVasundhara Volam 	if (buf) {
38056c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
38066c5657d0SVasundhara Volam 					   0, 0, 0);
38076c5657d0SVasundhara Volam 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
38086c5657d0SVasundhara Volam 		offset += seg_hdr_len;
38096c5657d0SVasundhara Volam 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
38106c5657d0SVasundhara Volam 		offset += ver_get_resp_len;
38116c5657d0SVasundhara Volam 	}
38126c5657d0SVasundhara Volam 
38136c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
38146c5657d0SVasundhara Volam 	if (rc) {
38156c5657d0SVasundhara Volam 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
38166c5657d0SVasundhara Volam 		goto err;
38176c5657d0SVasundhara Volam 	}
38186c5657d0SVasundhara Volam 
38196c5657d0SVasundhara Volam 	*dump_len += seg_hdr_len * coredump.total_segs;
38206c5657d0SVasundhara Volam 
38216c5657d0SVasundhara Volam 	seg_record = (struct coredump_segment_record *)coredump.data;
38226c5657d0SVasundhara Volam 	seg_record_len = sizeof(*seg_record);
38236c5657d0SVasundhara Volam 
38246c5657d0SVasundhara Volam 	for (i = 0; i < coredump.total_segs; i++) {
38256c5657d0SVasundhara Volam 		u16 comp_id = le16_to_cpu(seg_record->component_id);
38266c5657d0SVasundhara Volam 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
38276c5657d0SVasundhara Volam 		u32 duration = 0, seg_len = 0;
38286c5657d0SVasundhara Volam 		unsigned long start, end;
38296c5657d0SVasundhara Volam 
3830c74751f4SVasundhara Volam 		if (buf && ((offset + seg_hdr_len) >
3831c74751f4SVasundhara Volam 			    BNXT_COREDUMP_BUF_LEN(buf_len))) {
3832c74751f4SVasundhara Volam 			rc = -ENOBUFS;
3833c74751f4SVasundhara Volam 			goto err;
3834c74751f4SVasundhara Volam 		}
3835c74751f4SVasundhara Volam 
38366c5657d0SVasundhara Volam 		start = jiffies;
38376c5657d0SVasundhara Volam 
38386c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
38396c5657d0SVasundhara Volam 		if (rc) {
38406c5657d0SVasundhara Volam 			netdev_err(bp->dev,
38416c5657d0SVasundhara Volam 				   "Failed to initiate coredump for seg = %d\n",
38426c5657d0SVasundhara Volam 				   seg_record->segment_id);
38436c5657d0SVasundhara Volam 			goto next_seg;
38446c5657d0SVasundhara Volam 		}
38456c5657d0SVasundhara Volam 
38466c5657d0SVasundhara Volam 		/* Write segment data into the buffer */
38476c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
3848c74751f4SVasundhara Volam 						     &seg_len, buf, buf_len,
38496c5657d0SVasundhara Volam 						     offset + seg_hdr_len);
3850c74751f4SVasundhara Volam 		if (rc && rc == -ENOBUFS)
3851c74751f4SVasundhara Volam 			goto err;
3852c74751f4SVasundhara Volam 		else if (rc)
38536c5657d0SVasundhara Volam 			netdev_err(bp->dev,
38546c5657d0SVasundhara Volam 				   "Failed to retrieve coredump for seg = %d\n",
38556c5657d0SVasundhara Volam 				   seg_record->segment_id);
38566c5657d0SVasundhara Volam 
38576c5657d0SVasundhara Volam next_seg:
38586c5657d0SVasundhara Volam 		end = jiffies;
38596c5657d0SVasundhara Volam 		duration = jiffies_to_msecs(end - start);
38606c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
38616c5657d0SVasundhara Volam 					   rc, duration, 0);
38626c5657d0SVasundhara Volam 
38636c5657d0SVasundhara Volam 		if (buf) {
38646c5657d0SVasundhara Volam 			/* Write segment header into the buffer */
38656c5657d0SVasundhara Volam 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
38666c5657d0SVasundhara Volam 			offset += seg_hdr_len + seg_len;
38676c5657d0SVasundhara Volam 		}
38686c5657d0SVasundhara Volam 
38696c5657d0SVasundhara Volam 		*dump_len += seg_len;
38706c5657d0SVasundhara Volam 		seg_record =
38716c5657d0SVasundhara Volam 			(struct coredump_segment_record *)((u8 *)seg_record +
38726c5657d0SVasundhara Volam 							   seg_record_len);
38736c5657d0SVasundhara Volam 	}
38746c5657d0SVasundhara Volam 
38756c5657d0SVasundhara Volam err:
38761bbf3aedSArnd Bergmann 	if (buf)
38771bbf3aedSArnd Bergmann 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
38786c5657d0SVasundhara Volam 					  start_utc, coredump.total_segs + 1,
38796c5657d0SVasundhara Volam 					  rc);
38806c5657d0SVasundhara Volam 	kfree(coredump.data);
38811bbf3aedSArnd Bergmann 	*dump_len += sizeof(struct bnxt_coredump_record);
3882c74751f4SVasundhara Volam 	if (rc == -ENOBUFS)
38839a005c38SJonathan Lemon 		netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
38846c5657d0SVasundhara Volam 	return rc;
38856c5657d0SVasundhara Volam }
38866c5657d0SVasundhara Volam 
38870b0eacf3SVasundhara Volam static int bnxt_set_dump(struct net_device *dev, struct ethtool_dump *dump)
38880b0eacf3SVasundhara Volam {
38890b0eacf3SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
38900b0eacf3SVasundhara Volam 
38910b0eacf3SVasundhara Volam 	if (dump->flag > BNXT_DUMP_CRASH) {
38920b0eacf3SVasundhara Volam 		netdev_info(dev, "Supports only Live(0) and Crash(1) dumps.\n");
38930b0eacf3SVasundhara Volam 		return -EINVAL;
38940b0eacf3SVasundhara Volam 	}
38950b0eacf3SVasundhara Volam 
38960b0eacf3SVasundhara Volam 	if (!IS_ENABLED(CONFIG_TEE_BNXT_FW) && dump->flag == BNXT_DUMP_CRASH) {
38970b0eacf3SVasundhara Volam 		netdev_info(dev, "Cannot collect crash dump as TEE_BNXT_FW config option is not enabled.\n");
38980b0eacf3SVasundhara Volam 		return -EOPNOTSUPP;
38990b0eacf3SVasundhara Volam 	}
39000b0eacf3SVasundhara Volam 
39010b0eacf3SVasundhara Volam 	bp->dump_flag = dump->flag;
39020b0eacf3SVasundhara Volam 	return 0;
39030b0eacf3SVasundhara Volam }
39040b0eacf3SVasundhara Volam 
39056c5657d0SVasundhara Volam static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
39066c5657d0SVasundhara Volam {
39076c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
39086c5657d0SVasundhara Volam 
39096c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
39106c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
39116c5657d0SVasundhara Volam 
39126c5657d0SVasundhara Volam 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
39136c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_min_8b << 16 |
39146c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
39156c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_rsvd_8b;
39166c5657d0SVasundhara Volam 
39170b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
39180b0eacf3SVasundhara Volam 	if (bp->dump_flag == BNXT_DUMP_CRASH)
39190b0eacf3SVasundhara Volam 		dump->len = BNXT_CRASH_DUMP_LEN;
39200b0eacf3SVasundhara Volam 	else
39210b0eacf3SVasundhara Volam 		bnxt_get_coredump(bp, NULL, &dump->len);
39220b0eacf3SVasundhara Volam 	return 0;
39236c5657d0SVasundhara Volam }
39246c5657d0SVasundhara Volam 
39256c5657d0SVasundhara Volam static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
39266c5657d0SVasundhara Volam 			      void *buf)
39276c5657d0SVasundhara Volam {
39286c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
39296c5657d0SVasundhara Volam 
39306c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
39316c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
39326c5657d0SVasundhara Volam 
39336c5657d0SVasundhara Volam 	memset(buf, 0, dump->len);
39346c5657d0SVasundhara Volam 
39350b0eacf3SVasundhara Volam 	dump->flag = bp->dump_flag;
39360b0eacf3SVasundhara Volam 	if (dump->flag == BNXT_DUMP_CRASH) {
39370b0eacf3SVasundhara Volam #ifdef CONFIG_TEE_BNXT_FW
39380b0eacf3SVasundhara Volam 		return tee_bnxt_copy_coredump(buf, 0, dump->len);
39390b0eacf3SVasundhara Volam #endif
39400b0eacf3SVasundhara Volam 	} else {
39416c5657d0SVasundhara Volam 		return bnxt_get_coredump(bp, buf, &dump->len);
39426c5657d0SVasundhara Volam 	}
39436c5657d0SVasundhara Volam 
39440b0eacf3SVasundhara Volam 	return 0;
39450b0eacf3SVasundhara Volam }
39460b0eacf3SVasundhara Volam 
3947118612d5SMichael Chan static int bnxt_get_ts_info(struct net_device *dev,
3948118612d5SMichael Chan 			    struct ethtool_ts_info *info)
3949118612d5SMichael Chan {
3950118612d5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
3951118612d5SMichael Chan 	struct bnxt_ptp_cfg *ptp;
3952118612d5SMichael Chan 
3953118612d5SMichael Chan 	ptp = bp->ptp_cfg;
3954118612d5SMichael Chan 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
3955118612d5SMichael Chan 				SOF_TIMESTAMPING_RX_SOFTWARE |
3956118612d5SMichael Chan 				SOF_TIMESTAMPING_SOFTWARE;
3957118612d5SMichael Chan 
3958118612d5SMichael Chan 	info->phc_index = -1;
3959118612d5SMichael Chan 	if (!ptp)
3960118612d5SMichael Chan 		return 0;
3961118612d5SMichael Chan 
3962118612d5SMichael Chan 	info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
3963118612d5SMichael Chan 				 SOF_TIMESTAMPING_RX_HARDWARE |
3964118612d5SMichael Chan 				 SOF_TIMESTAMPING_RAW_HARDWARE;
3965118612d5SMichael Chan 	if (ptp->ptp_clock)
3966118612d5SMichael Chan 		info->phc_index = ptp_clock_index(ptp->ptp_clock);
3967118612d5SMichael Chan 
3968118612d5SMichael Chan 	info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
3969118612d5SMichael Chan 
3970118612d5SMichael Chan 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3971118612d5SMichael Chan 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3972118612d5SMichael Chan 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
3973118612d5SMichael Chan 	return 0;
3974118612d5SMichael Chan }
3975118612d5SMichael Chan 
3976eb513658SMichael Chan void bnxt_ethtool_init(struct bnxt *bp)
3977eb513658SMichael Chan {
3978eb513658SMichael Chan 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3979eb513658SMichael Chan 	struct hwrm_selftest_qlist_input req = {0};
3980eb513658SMichael Chan 	struct bnxt_test_info *test_info;
3981431aa1ebSMichael Chan 	struct net_device *dev = bp->dev;
3982eb513658SMichael Chan 	int i, rc;
3983eb513658SMichael Chan 
3984691aa620SVasundhara Volam 	if (!(bp->fw_cap & BNXT_FW_CAP_PKG_VER))
3985a60faa60SVasundhara Volam 		bnxt_get_pkgver(dev);
3986431aa1ebSMichael Chan 
3987ba642ab7SMichael Chan 	bp->num_tests = 0;
39886896cb35SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_PF(bp))
3989eb513658SMichael Chan 		return;
3990eb513658SMichael Chan 
3991eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3992eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3993eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3994eb513658SMichael Chan 	if (rc)
3995eb513658SMichael Chan 		goto ethtool_init_exit;
3996eb513658SMichael Chan 
3997ba642ab7SMichael Chan 	test_info = bp->test_info;
3998ba642ab7SMichael Chan 	if (!test_info)
3999eb513658SMichael Chan 		test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
4000eb513658SMichael Chan 	if (!test_info)
4001eb513658SMichael Chan 		goto ethtool_init_exit;
4002eb513658SMichael Chan 
4003eb513658SMichael Chan 	bp->test_info = test_info;
4004eb513658SMichael Chan 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
4005eb513658SMichael Chan 	if (bp->num_tests > BNXT_MAX_TEST)
4006eb513658SMichael Chan 		bp->num_tests = BNXT_MAX_TEST;
4007eb513658SMichael Chan 
4008eb513658SMichael Chan 	test_info->offline_mask = resp->offline_tests;
4009eb513658SMichael Chan 	test_info->timeout = le16_to_cpu(resp->test_timeout);
4010eb513658SMichael Chan 	if (!test_info->timeout)
4011eb513658SMichael Chan 		test_info->timeout = HWRM_CMD_TIMEOUT;
4012eb513658SMichael Chan 	for (i = 0; i < bp->num_tests; i++) {
4013eb513658SMichael Chan 		char *str = test_info->string[i];
4014eb513658SMichael Chan 		char *fw_str = resp->test0_name + i * 32;
4015eb513658SMichael Chan 
4016f7dc1ea6SMichael Chan 		if (i == BNXT_MACLPBK_TEST_IDX) {
4017f7dc1ea6SMichael Chan 			strcpy(str, "Mac loopback test (offline)");
401891725d89SMichael Chan 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
401991725d89SMichael Chan 			strcpy(str, "Phy loopback test (offline)");
402055fd0cf3SMichael Chan 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
402155fd0cf3SMichael Chan 			strcpy(str, "Ext loopback test (offline)");
402267fea463SMichael Chan 		} else if (i == BNXT_IRQ_TEST_IDX) {
402367fea463SMichael Chan 			strcpy(str, "Interrupt_test (offline)");
4024f7dc1ea6SMichael Chan 		} else {
4025eb513658SMichael Chan 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
4026eb513658SMichael Chan 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
4027eb513658SMichael Chan 			if (test_info->offline_mask & (1 << i))
4028eb513658SMichael Chan 				strncat(str, " (offline)",
4029eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
4030eb513658SMichael Chan 			else
4031eb513658SMichael Chan 				strncat(str, " (online)",
4032eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
4033eb513658SMichael Chan 		}
4034f7dc1ea6SMichael Chan 	}
4035eb513658SMichael Chan 
4036eb513658SMichael Chan ethtool_init_exit:
4037eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
4038eb513658SMichael Chan }
4039eb513658SMichael Chan 
4040782bc00aSJakub Kicinski static void bnxt_get_eth_phy_stats(struct net_device *dev,
4041782bc00aSJakub Kicinski 				   struct ethtool_eth_phy_stats *phy_stats)
4042782bc00aSJakub Kicinski {
4043782bc00aSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
4044782bc00aSJakub Kicinski 	u64 *rx;
4045782bc00aSJakub Kicinski 
4046782bc00aSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
4047782bc00aSJakub Kicinski 		return;
4048782bc00aSJakub Kicinski 
4049782bc00aSJakub Kicinski 	rx = bp->rx_port_stats_ext.sw_stats;
4050782bc00aSJakub Kicinski 	phy_stats->SymbolErrorDuringCarrier =
4051782bc00aSJakub Kicinski 		*(rx + BNXT_RX_STATS_EXT_OFFSET(rx_pcs_symbol_err));
4052782bc00aSJakub Kicinski }
4053782bc00aSJakub Kicinski 
4054782bc00aSJakub Kicinski static void bnxt_get_eth_mac_stats(struct net_device *dev,
4055782bc00aSJakub Kicinski 				   struct ethtool_eth_mac_stats *mac_stats)
4056782bc00aSJakub Kicinski {
4057782bc00aSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
4058782bc00aSJakub Kicinski 	u64 *rx, *tx;
4059782bc00aSJakub Kicinski 
4060782bc00aSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS))
4061782bc00aSJakub Kicinski 		return;
4062782bc00aSJakub Kicinski 
4063782bc00aSJakub Kicinski 	rx = bp->port_stats.sw_stats;
4064782bc00aSJakub Kicinski 	tx = bp->port_stats.sw_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4065782bc00aSJakub Kicinski 
4066782bc00aSJakub Kicinski 	mac_stats->FramesReceivedOK =
4067782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_good_frames);
4068782bc00aSJakub Kicinski 	mac_stats->FramesTransmittedOK =
4069782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_good_frames);
407037434782SJakub Kicinski 	mac_stats->FrameCheckSequenceErrors =
407137434782SJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
407237434782SJakub Kicinski 	mac_stats->AlignmentErrors =
407337434782SJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
407437434782SJakub Kicinski 	mac_stats->OutOfRangeLengthField =
407537434782SJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_oor_len_frames);
4076782bc00aSJakub Kicinski }
4077782bc00aSJakub Kicinski 
4078782bc00aSJakub Kicinski static void bnxt_get_eth_ctrl_stats(struct net_device *dev,
4079782bc00aSJakub Kicinski 				    struct ethtool_eth_ctrl_stats *ctrl_stats)
4080782bc00aSJakub Kicinski {
4081782bc00aSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
4082782bc00aSJakub Kicinski 	u64 *rx;
4083782bc00aSJakub Kicinski 
4084782bc00aSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS))
4085782bc00aSJakub Kicinski 		return;
4086782bc00aSJakub Kicinski 
4087782bc00aSJakub Kicinski 	rx = bp->port_stats.sw_stats;
4088782bc00aSJakub Kicinski 	ctrl_stats->MACControlFramesReceived =
4089782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_ctrl_frames);
4090782bc00aSJakub Kicinski }
4091782bc00aSJakub Kicinski 
4092782bc00aSJakub Kicinski static const struct ethtool_rmon_hist_range bnxt_rmon_ranges[] = {
4093782bc00aSJakub Kicinski 	{    0,    64 },
4094782bc00aSJakub Kicinski 	{   65,   127 },
4095782bc00aSJakub Kicinski 	{  128,   255 },
4096782bc00aSJakub Kicinski 	{  256,   511 },
4097782bc00aSJakub Kicinski 	{  512,  1023 },
4098782bc00aSJakub Kicinski 	{ 1024,  1518 },
4099782bc00aSJakub Kicinski 	{ 1519,  2047 },
4100782bc00aSJakub Kicinski 	{ 2048,  4095 },
4101782bc00aSJakub Kicinski 	{ 4096,  9216 },
4102782bc00aSJakub Kicinski 	{ 9217, 16383 },
4103782bc00aSJakub Kicinski 	{}
4104782bc00aSJakub Kicinski };
4105782bc00aSJakub Kicinski 
4106782bc00aSJakub Kicinski static void bnxt_get_rmon_stats(struct net_device *dev,
4107782bc00aSJakub Kicinski 				struct ethtool_rmon_stats *rmon_stats,
4108782bc00aSJakub Kicinski 				const struct ethtool_rmon_hist_range **ranges)
4109782bc00aSJakub Kicinski {
4110782bc00aSJakub Kicinski 	struct bnxt *bp = netdev_priv(dev);
4111782bc00aSJakub Kicinski 	u64 *rx, *tx;
4112782bc00aSJakub Kicinski 
4113782bc00aSJakub Kicinski 	if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS))
4114782bc00aSJakub Kicinski 		return;
4115782bc00aSJakub Kicinski 
4116782bc00aSJakub Kicinski 	rx = bp->port_stats.sw_stats;
4117782bc00aSJakub Kicinski 	tx = bp->port_stats.sw_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4118782bc00aSJakub Kicinski 
4119782bc00aSJakub Kicinski 	rmon_stats->jabbers =
4120782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
4121782bc00aSJakub Kicinski 	rmon_stats->oversize_pkts =
4122782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames);
4123782bc00aSJakub Kicinski 	rmon_stats->undersize_pkts =
4124782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames);
4125782bc00aSJakub Kicinski 
4126782bc00aSJakub Kicinski 	rmon_stats->hist[0] = BNXT_GET_RX_PORT_STATS64(rx, rx_64b_frames);
4127782bc00aSJakub Kicinski 	rmon_stats->hist[1] = BNXT_GET_RX_PORT_STATS64(rx, rx_65b_127b_frames);
4128782bc00aSJakub Kicinski 	rmon_stats->hist[2] = BNXT_GET_RX_PORT_STATS64(rx, rx_128b_255b_frames);
4129782bc00aSJakub Kicinski 	rmon_stats->hist[3] = BNXT_GET_RX_PORT_STATS64(rx, rx_256b_511b_frames);
4130782bc00aSJakub Kicinski 	rmon_stats->hist[4] =
4131782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_512b_1023b_frames);
4132782bc00aSJakub Kicinski 	rmon_stats->hist[5] =
4133782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_1024b_1518b_frames);
4134782bc00aSJakub Kicinski 	rmon_stats->hist[6] =
4135782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_1519b_2047b_frames);
4136782bc00aSJakub Kicinski 	rmon_stats->hist[7] =
4137782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_2048b_4095b_frames);
4138782bc00aSJakub Kicinski 	rmon_stats->hist[8] =
4139782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_4096b_9216b_frames);
4140782bc00aSJakub Kicinski 	rmon_stats->hist[9] =
4141782bc00aSJakub Kicinski 		BNXT_GET_RX_PORT_STATS64(rx, rx_9217b_16383b_frames);
4142782bc00aSJakub Kicinski 
4143782bc00aSJakub Kicinski 	rmon_stats->hist_tx[0] =
4144782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_64b_frames);
4145782bc00aSJakub Kicinski 	rmon_stats->hist_tx[1] =
4146782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_65b_127b_frames);
4147782bc00aSJakub Kicinski 	rmon_stats->hist_tx[2] =
4148782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_128b_255b_frames);
4149782bc00aSJakub Kicinski 	rmon_stats->hist_tx[3] =
4150782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_256b_511b_frames);
4151782bc00aSJakub Kicinski 	rmon_stats->hist_tx[4] =
4152782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_512b_1023b_frames);
4153782bc00aSJakub Kicinski 	rmon_stats->hist_tx[5] =
4154782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_1024b_1518b_frames);
4155782bc00aSJakub Kicinski 	rmon_stats->hist_tx[6] =
4156782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_1519b_2047b_frames);
4157782bc00aSJakub Kicinski 	rmon_stats->hist_tx[7] =
4158782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_2048b_4095b_frames);
4159782bc00aSJakub Kicinski 	rmon_stats->hist_tx[8] =
4160782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_4096b_9216b_frames);
4161782bc00aSJakub Kicinski 	rmon_stats->hist_tx[9] =
4162782bc00aSJakub Kicinski 		BNXT_GET_TX_PORT_STATS64(tx, tx_9217b_16383b_frames);
4163782bc00aSJakub Kicinski 
4164782bc00aSJakub Kicinski 	*ranges = bnxt_rmon_ranges;
4165782bc00aSJakub Kicinski }
4166782bc00aSJakub Kicinski 
4167eb513658SMichael Chan void bnxt_ethtool_free(struct bnxt *bp)
4168eb513658SMichael Chan {
4169eb513658SMichael Chan 	kfree(bp->test_info);
4170eb513658SMichael Chan 	bp->test_info = NULL;
4171eb513658SMichael Chan }
4172eb513658SMichael Chan 
4173c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
4174f704d243SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
4175f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES |
4176f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USECS_IRQ |
4177f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
4178f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_STATS_BLOCK_USECS |
4179f704d243SJakub Kicinski 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
418000c04a92SMichael Chan 	.get_link_ksettings	= bnxt_get_link_ksettings,
418100c04a92SMichael Chan 	.set_link_ksettings	= bnxt_set_link_ksettings,
4182c9ca5c3aSJakub Kicinski 	.get_fec_stats		= bnxt_get_fec_stats,
41838b277589SMichael Chan 	.get_fecparam		= bnxt_get_fecparam,
4184ccd6a9dcSMichael Chan 	.set_fecparam		= bnxt_set_fecparam,
4185423cffcfSJakub Kicinski 	.get_pause_stats	= bnxt_get_pause_stats,
4186c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
4187c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
4188c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
4189b5d600b0SVasundhara Volam 	.get_regs_len		= bnxt_get_regs_len,
4190b5d600b0SVasundhara Volam 	.get_regs		= bnxt_get_regs,
41918e202366SMichael Chan 	.get_wol		= bnxt_get_wol,
41925282db6cSMichael Chan 	.set_wol		= bnxt_set_wol,
4193c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
4194c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
4195c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
4196c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
4197c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
4198c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
4199c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
4200c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
4201c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
4202c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
4203c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
4204c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
4205a011952aSMichael Chan 	.set_rxnfc		= bnxt_set_rxnfc,
4206c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
4207c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
4208c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
4209bd3191b5SMichael Chan 	.set_rxfh		= bnxt_set_rxfh,
4210c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
4211c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
4212c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
4213c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
4214c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
421572b34f04SMichael Chan 	.get_eee		= bnxt_get_eee,
421672b34f04SMichael Chan 	.set_eee		= bnxt_set_eee,
421742ee18feSAjit Khaparde 	.get_module_info	= bnxt_get_module_info,
421842ee18feSAjit Khaparde 	.get_module_eeprom	= bnxt_get_module_eeprom,
42195ad2cbeeSMichael Chan 	.nway_reset		= bnxt_nway_reset,
42205ad2cbeeSMichael Chan 	.set_phys_id		= bnxt_set_phys_id,
4221eb513658SMichael Chan 	.self_test		= bnxt_self_test,
4222118612d5SMichael Chan 	.get_ts_info		= bnxt_get_ts_info,
422349f7972fSVasundhara Volam 	.reset			= bnxt_reset,
42240b0eacf3SVasundhara Volam 	.set_dump		= bnxt_set_dump,
42256c5657d0SVasundhara Volam 	.get_dump_flag		= bnxt_get_dump_flag,
42266c5657d0SVasundhara Volam 	.get_dump_data		= bnxt_get_dump_data,
4227782bc00aSJakub Kicinski 	.get_eth_phy_stats	= bnxt_get_eth_phy_stats,
4228782bc00aSJakub Kicinski 	.get_eth_mac_stats	= bnxt_get_eth_mac_stats,
4229782bc00aSJakub Kicinski 	.get_eth_ctrl_stats	= bnxt_get_eth_ctrl_stats,
4230782bc00aSJakub Kicinski 	.get_rmon_stats		= bnxt_get_rmon_stats,
4231c0c050c5SMichael Chan };
4232