1c0c050c5SMichael Chan /* Broadcom NetXtreme-C/E network driver.
2c0c050c5SMichael Chan  *
311f15ed3SMichael Chan  * Copyright (c) 2014-2016 Broadcom Corporation
48e202366SMichael Chan  * Copyright (c) 2016-2017 Broadcom Limited
5c0c050c5SMichael Chan  *
6c0c050c5SMichael Chan  * This program is free software; you can redistribute it and/or modify
7c0c050c5SMichael Chan  * it under the terms of the GNU General Public License as published by
8c0c050c5SMichael Chan  * the Free Software Foundation.
9c0c050c5SMichael Chan  */
10c0c050c5SMichael Chan 
113ebf6f0aSRob Swindell #include <linux/ctype.h>
128ddc9aaaSMichael Chan #include <linux/stringify.h>
13c0c050c5SMichael Chan #include <linux/ethtool.h>
14c0c050c5SMichael Chan #include <linux/interrupt.h>
15c0c050c5SMichael Chan #include <linux/pci.h>
16c0c050c5SMichael Chan #include <linux/etherdevice.h>
17c0c050c5SMichael Chan #include <linux/crc32.h>
18c0c050c5SMichael Chan #include <linux/firmware.h>
196c5657d0SVasundhara Volam #include <linux/utsname.h>
206c5657d0SVasundhara Volam #include <linux/time.h>
21c0c050c5SMichael Chan #include "bnxt_hsi.h"
22c0c050c5SMichael Chan #include "bnxt.h"
23f7dc1ea6SMichael Chan #include "bnxt_xdp.h"
24c0c050c5SMichael Chan #include "bnxt_ethtool.h"
25c0c050c5SMichael Chan #include "bnxt_nvm_defs.h"	/* NVRAM content constant and structure defs */
26c0c050c5SMichael Chan #include "bnxt_fw_hdr.h"	/* Firmware hdr constant and structure defs */
276c5657d0SVasundhara Volam #include "bnxt_coredump.h"
28c0c050c5SMichael Chan #define FLASH_NVRAM_TIMEOUT	((HWRM_CMD_TIMEOUT) * 100)
295ac67d8bSRob Swindell #define FLASH_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
305ac67d8bSRob Swindell #define INSTALL_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
31c0c050c5SMichael Chan 
32c0c050c5SMichael Chan static u32 bnxt_get_msglevel(struct net_device *dev)
33c0c050c5SMichael Chan {
34c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
35c0c050c5SMichael Chan 
36c0c050c5SMichael Chan 	return bp->msg_enable;
37c0c050c5SMichael Chan }
38c0c050c5SMichael Chan 
39c0c050c5SMichael Chan static void bnxt_set_msglevel(struct net_device *dev, u32 value)
40c0c050c5SMichael Chan {
41c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
42c0c050c5SMichael Chan 
43c0c050c5SMichael Chan 	bp->msg_enable = value;
44c0c050c5SMichael Chan }
45c0c050c5SMichael Chan 
46c0c050c5SMichael Chan static int bnxt_get_coalesce(struct net_device *dev,
47c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
48c0c050c5SMichael Chan {
49c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5018775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
5118775aa8SMichael Chan 	u16 mult;
52c0c050c5SMichael Chan 
53c0c050c5SMichael Chan 	memset(coal, 0, sizeof(*coal));
54c0c050c5SMichael Chan 
556a8788f2SAndy Gospodarek 	coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM;
566a8788f2SAndy Gospodarek 
5718775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
5818775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
5918775aa8SMichael Chan 	coal->rx_coalesce_usecs = hw_coal->coal_ticks;
6018775aa8SMichael Chan 	coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6118775aa8SMichael Chan 	coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
6218775aa8SMichael Chan 	coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
63c0c050c5SMichael Chan 
6418775aa8SMichael Chan 	hw_coal = &bp->tx_coal;
6518775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
6618775aa8SMichael Chan 	coal->tx_coalesce_usecs = hw_coal->coal_ticks;
6718775aa8SMichael Chan 	coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult;
6818775aa8SMichael Chan 	coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
6918775aa8SMichael Chan 	coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
70dfc9c94aSMichael Chan 
7151f30785SMichael Chan 	coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
7251f30785SMichael Chan 
73c0c050c5SMichael Chan 	return 0;
74c0c050c5SMichael Chan }
75c0c050c5SMichael Chan 
76c0c050c5SMichael Chan static int bnxt_set_coalesce(struct net_device *dev,
77c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
78c0c050c5SMichael Chan {
79c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
8051f30785SMichael Chan 	bool update_stats = false;
8118775aa8SMichael Chan 	struct bnxt_coal *hw_coal;
82c0c050c5SMichael Chan 	int rc = 0;
8318775aa8SMichael Chan 	u16 mult;
84c0c050c5SMichael Chan 
856a8788f2SAndy Gospodarek 	if (coal->use_adaptive_rx_coalesce) {
866a8788f2SAndy Gospodarek 		bp->flags |= BNXT_FLAG_DIM;
876a8788f2SAndy Gospodarek 	} else {
886a8788f2SAndy Gospodarek 		if (bp->flags & BNXT_FLAG_DIM) {
896a8788f2SAndy Gospodarek 			bp->flags &= ~(BNXT_FLAG_DIM);
906a8788f2SAndy Gospodarek 			goto reset_coalesce;
916a8788f2SAndy Gospodarek 		}
926a8788f2SAndy Gospodarek 	}
936a8788f2SAndy Gospodarek 
9418775aa8SMichael Chan 	hw_coal = &bp->rx_coal;
9518775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
9618775aa8SMichael Chan 	hw_coal->coal_ticks = coal->rx_coalesce_usecs;
9718775aa8SMichael Chan 	hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult;
9818775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq;
9918775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult;
100c0c050c5SMichael Chan 
101de4a10efSAndy Gospodarek 	hw_coal = &bp->tx_coal;
10218775aa8SMichael Chan 	mult = hw_coal->bufs_per_record;
10318775aa8SMichael Chan 	hw_coal->coal_ticks = coal->tx_coalesce_usecs;
10418775aa8SMichael Chan 	hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult;
10518775aa8SMichael Chan 	hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq;
10618775aa8SMichael Chan 	hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult;
107dfc9c94aSMichael Chan 
10851f30785SMichael Chan 	if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
10951f30785SMichael Chan 		u32 stats_ticks = coal->stats_block_coalesce_usecs;
11051f30785SMichael Chan 
111adcc331eSMichael Chan 		/* Allow 0, which means disable. */
112adcc331eSMichael Chan 		if (stats_ticks)
11351f30785SMichael Chan 			stats_ticks = clamp_t(u32, stats_ticks,
11451f30785SMichael Chan 					      BNXT_MIN_STATS_COAL_TICKS,
11551f30785SMichael Chan 					      BNXT_MAX_STATS_COAL_TICKS);
11651f30785SMichael Chan 		stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
11751f30785SMichael Chan 		bp->stats_coal_ticks = stats_ticks;
118e795892eSMichael Chan 		if (bp->stats_coal_ticks)
119e795892eSMichael Chan 			bp->current_interval =
120e795892eSMichael Chan 				bp->stats_coal_ticks * HZ / 1000000;
121e795892eSMichael Chan 		else
122e795892eSMichael Chan 			bp->current_interval = BNXT_TIMER_INTERVAL;
12351f30785SMichael Chan 		update_stats = true;
12451f30785SMichael Chan 	}
12551f30785SMichael Chan 
1266a8788f2SAndy Gospodarek reset_coalesce:
12751f30785SMichael Chan 	if (netif_running(dev)) {
12851f30785SMichael Chan 		if (update_stats) {
12951f30785SMichael Chan 			rc = bnxt_close_nic(bp, true, false);
13051f30785SMichael Chan 			if (!rc)
13151f30785SMichael Chan 				rc = bnxt_open_nic(bp, true, false);
13251f30785SMichael Chan 		} else {
133c0c050c5SMichael Chan 			rc = bnxt_hwrm_set_coal(bp);
13451f30785SMichael Chan 		}
13551f30785SMichael Chan 	}
136c0c050c5SMichael Chan 
137c0c050c5SMichael Chan 	return rc;
138c0c050c5SMichael Chan }
139c0c050c5SMichael Chan 
140c0c050c5SMichael Chan #define BNXT_NUM_STATS	21
141c0c050c5SMichael Chan 
1428ddc9aaaSMichael Chan #define BNXT_RX_STATS_ENTRY(counter)	\
1438ddc9aaaSMichael Chan 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
1448ddc9aaaSMichael Chan 
1458ddc9aaaSMichael Chan #define BNXT_TX_STATS_ENTRY(counter)	\
1468ddc9aaaSMichael Chan 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
1478ddc9aaaSMichael Chan 
14800db3cbaSVasundhara Volam #define BNXT_RX_STATS_EXT_ENTRY(counter)	\
14900db3cbaSVasundhara Volam 	{ BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
15000db3cbaSVasundhara Volam 
15120c1d28eSVasundhara Volam enum {
15220c1d28eSVasundhara Volam 	RX_TOTAL_DISCARDS,
15320c1d28eSVasundhara Volam 	TX_TOTAL_DISCARDS,
15420c1d28eSVasundhara Volam };
15520c1d28eSVasundhara Volam 
15620c1d28eSVasundhara Volam static struct {
15720c1d28eSVasundhara Volam 	u64			counter;
15820c1d28eSVasundhara Volam 	char			string[ETH_GSTRING_LEN];
15920c1d28eSVasundhara Volam } bnxt_sw_func_stats[] = {
16020c1d28eSVasundhara Volam 	{0, "rx_total_discard_pkts"},
16120c1d28eSVasundhara Volam 	{0, "tx_total_discard_pkts"},
16220c1d28eSVasundhara Volam };
16320c1d28eSVasundhara Volam 
1648ddc9aaaSMichael Chan static const struct {
1658ddc9aaaSMichael Chan 	long offset;
1668ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
1678ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
1688ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
1698ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
1708ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
1718ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
1728ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
1736fc92c33SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
1748ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
1758ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
1768ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
1778ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
1788ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
1798ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
1808ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
1818ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
1828ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
1838ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
1848ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
1858ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
1868ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
1878ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
1888ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
1898ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
1908ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
1918ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
1928ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
1938ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
194c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
195c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
196c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
197c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
198c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
199c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
200c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
201c77192f2SMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
2028ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
2038ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
2048ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
2058ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
2068ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
2078ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
208699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
209699efed0SVasundhara Volam 	BNXT_RX_STATS_ENTRY(rx_stat_err),
2108ddc9aaaSMichael Chan 
2118ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
2128ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
2138ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
2148ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
2158ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
2166fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
2178ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
2186fc92c33SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
2198ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
2208ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
2218ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
2228ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
2238ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
2248ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
2258ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
2268ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
2278ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
2288ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
2298ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
2308ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
2318ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
2328ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
233c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
234c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
235c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
236c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
237c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
238c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
239c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
240c77192f2SMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
2418ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
2428ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
2438ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
2448ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
245699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
246699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
247699efed0SVasundhara Volam 	BNXT_TX_STATS_ENTRY(tx_stat_error),
2488ddc9aaaSMichael Chan };
2498ddc9aaaSMichael Chan 
25000db3cbaSVasundhara Volam static const struct {
25100db3cbaSVasundhara Volam 	long offset;
25200db3cbaSVasundhara Volam 	char string[ETH_GSTRING_LEN];
25300db3cbaSVasundhara Volam } bnxt_port_stats_ext_arr[] = {
25400db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
25500db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
25600db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
25700db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
25800db3cbaSVasundhara Volam 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
25900db3cbaSVasundhara Volam };
26000db3cbaSVasundhara Volam 
26120c1d28eSVasundhara Volam #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
2628ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
26300db3cbaSVasundhara Volam #define BNXT_NUM_PORT_STATS_EXT ARRAY_SIZE(bnxt_port_stats_ext_arr)
2648ddc9aaaSMichael Chan 
2655c8227d0SMichael Chan static int bnxt_get_num_stats(struct bnxt *bp)
266c0c050c5SMichael Chan {
2678ddc9aaaSMichael Chan 	int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
2688ddc9aaaSMichael Chan 
26920c1d28eSVasundhara Volam 	num_stats += BNXT_NUM_SW_FUNC_STATS;
27020c1d28eSVasundhara Volam 
2718ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS)
2728ddc9aaaSMichael Chan 		num_stats += BNXT_NUM_PORT_STATS;
2738ddc9aaaSMichael Chan 
27400db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT)
27500db3cbaSVasundhara Volam 		num_stats += BNXT_NUM_PORT_STATS_EXT;
27600db3cbaSVasundhara Volam 
2778ddc9aaaSMichael Chan 	return num_stats;
2788ddc9aaaSMichael Chan }
2795c8227d0SMichael Chan 
2805c8227d0SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
2815c8227d0SMichael Chan {
2825c8227d0SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2835c8227d0SMichael Chan 
2845c8227d0SMichael Chan 	switch (sset) {
2855c8227d0SMichael Chan 	case ETH_SS_STATS:
2865c8227d0SMichael Chan 		return bnxt_get_num_stats(bp);
287eb513658SMichael Chan 	case ETH_SS_TEST:
288eb513658SMichael Chan 		if (!bp->num_tests)
289eb513658SMichael Chan 			return -EOPNOTSUPP;
290eb513658SMichael Chan 		return bp->num_tests;
291c0c050c5SMichael Chan 	default:
292c0c050c5SMichael Chan 		return -EOPNOTSUPP;
293c0c050c5SMichael Chan 	}
294c0c050c5SMichael Chan }
295c0c050c5SMichael Chan 
296c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
297c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
298c0c050c5SMichael Chan {
299c0c050c5SMichael Chan 	u32 i, j = 0;
300c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
301c0c050c5SMichael Chan 	u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
302c0c050c5SMichael Chan 
303c0c050c5SMichael Chan 	if (!bp->bnapi)
304c0c050c5SMichael Chan 		return;
305c0c050c5SMichael Chan 
30620c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
30720c1d28eSVasundhara Volam 		bnxt_sw_func_stats[i].counter = 0;
30820c1d28eSVasundhara Volam 
309c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
310c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
311c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
312c0c050c5SMichael Chan 		__le64 *hw_stats = (__le64 *)cpr->hw_stats;
313c0c050c5SMichael Chan 		int k;
314c0c050c5SMichael Chan 
315c0c050c5SMichael Chan 		for (k = 0; k < stat_fields; j++, k++)
316c0c050c5SMichael Chan 			buf[j] = le64_to_cpu(hw_stats[k]);
317c0c050c5SMichael Chan 		buf[j++] = cpr->rx_l4_csum_errors;
31820c1d28eSVasundhara Volam 
31920c1d28eSVasundhara Volam 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
32020c1d28eSVasundhara Volam 			le64_to_cpu(cpr->hw_stats->rx_discard_pkts);
32120c1d28eSVasundhara Volam 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
32220c1d28eSVasundhara Volam 			le64_to_cpu(cpr->hw_stats->tx_discard_pkts);
323c0c050c5SMichael Chan 	}
32420c1d28eSVasundhara Volam 
32520c1d28eSVasundhara Volam 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
32620c1d28eSVasundhara Volam 		buf[j] = bnxt_sw_func_stats[i].counter;
32720c1d28eSVasundhara Volam 
3288ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
3298ddc9aaaSMichael Chan 		__le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
3308ddc9aaaSMichael Chan 
3318ddc9aaaSMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
3328ddc9aaaSMichael Chan 			buf[j] = le64_to_cpu(*(port_stats +
3338ddc9aaaSMichael Chan 					       bnxt_port_stats_arr[i].offset));
3348ddc9aaaSMichael Chan 		}
3358ddc9aaaSMichael Chan 	}
33600db3cbaSVasundhara Volam 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
33700db3cbaSVasundhara Volam 		__le64 *port_stats_ext = (__le64 *)bp->hw_rx_port_stats_ext;
33800db3cbaSVasundhara Volam 
33900db3cbaSVasundhara Volam 		for (i = 0; i < BNXT_NUM_PORT_STATS_EXT; i++, j++) {
34000db3cbaSVasundhara Volam 			buf[j] = le64_to_cpu(*(port_stats_ext +
34100db3cbaSVasundhara Volam 					    bnxt_port_stats_ext_arr[i].offset));
34200db3cbaSVasundhara Volam 		}
34300db3cbaSVasundhara Volam 	}
344c0c050c5SMichael Chan }
345c0c050c5SMichael Chan 
346c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
347c0c050c5SMichael Chan {
348c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
349c0c050c5SMichael Chan 	u32 i;
350c0c050c5SMichael Chan 
351c0c050c5SMichael Chan 	switch (stringset) {
352c0c050c5SMichael Chan 	/* The number of strings must match BNXT_NUM_STATS defined above. */
353c0c050c5SMichael Chan 	case ETH_SS_STATS:
354c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
355c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_ucast_packets", i);
356c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
357c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_mcast_packets", i);
358c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
359c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_bcast_packets", i);
360c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
361c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_discards", i);
362c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
363c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_drops", i);
364c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
365c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_ucast_bytes", i);
366c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
367c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_mcast_bytes", i);
368c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
369c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_bcast_bytes", i);
370c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
371c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_ucast_packets", i);
372c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
373c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_mcast_packets", i);
374c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
375c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_bcast_packets", i);
376c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
377c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_discards", i);
378c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
379c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_drops", i);
380c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
381c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_ucast_bytes", i);
382c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
383c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_mcast_bytes", i);
384c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
385c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_bcast_bytes", i);
386c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
387c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_packets", i);
388c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
389c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_bytes", i);
390c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
391c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_events", i);
392c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
393c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_aborts", i);
394c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
395c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_l4_csum_errors", i);
396c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
397c0c050c5SMichael Chan 		}
39820c1d28eSVasundhara Volam 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
39920c1d28eSVasundhara Volam 			strcpy(buf, bnxt_sw_func_stats[i].string);
40020c1d28eSVasundhara Volam 			buf += ETH_GSTRING_LEN;
40120c1d28eSVasundhara Volam 		}
40220c1d28eSVasundhara Volam 
4038ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
4048ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
4058ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
4068ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
4078ddc9aaaSMichael Chan 			}
4088ddc9aaaSMichael Chan 		}
40900db3cbaSVasundhara Volam 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
41000db3cbaSVasundhara Volam 			for (i = 0; i < BNXT_NUM_PORT_STATS_EXT; i++) {
41100db3cbaSVasundhara Volam 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
41200db3cbaSVasundhara Volam 				buf += ETH_GSTRING_LEN;
41300db3cbaSVasundhara Volam 			}
41400db3cbaSVasundhara Volam 		}
415c0c050c5SMichael Chan 		break;
416eb513658SMichael Chan 	case ETH_SS_TEST:
417eb513658SMichael Chan 		if (bp->num_tests)
418eb513658SMichael Chan 			memcpy(buf, bp->test_info->string,
419eb513658SMichael Chan 			       bp->num_tests * ETH_GSTRING_LEN);
420eb513658SMichael Chan 		break;
421c0c050c5SMichael Chan 	default:
422c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
423c0c050c5SMichael Chan 			   stringset);
424c0c050c5SMichael Chan 		break;
425c0c050c5SMichael Chan 	}
426c0c050c5SMichael Chan }
427c0c050c5SMichael Chan 
428c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
429c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
430c0c050c5SMichael Chan {
431c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
432c0c050c5SMichael Chan 
433c0c050c5SMichael Chan 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
434c0c050c5SMichael Chan 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
435c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
436c0c050c5SMichael Chan 
437c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
438c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
439c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
440c0c050c5SMichael Chan }
441c0c050c5SMichael Chan 
442c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
443c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
444c0c050c5SMichael Chan {
445c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
446c0c050c5SMichael Chan 
447c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
448c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
449c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
450c0c050c5SMichael Chan 		return -EINVAL;
451c0c050c5SMichael Chan 
452c0c050c5SMichael Chan 	if (netif_running(dev))
453c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
454c0c050c5SMichael Chan 
455c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
456c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
457c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
458c0c050c5SMichael Chan 
459c0c050c5SMichael Chan 	if (netif_running(dev))
460c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
461c0c050c5SMichael Chan 
462c0c050c5SMichael Chan 	return 0;
463c0c050c5SMichael Chan }
464c0c050c5SMichael Chan 
465c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
466c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
467c0c050c5SMichael Chan {
468c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
469db4723b3SMichael Chan 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
470c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
471db4723b3SMichael Chan 	int max_tx_sch_inputs;
472db4723b3SMichael Chan 
473db4723b3SMichael Chan 	/* Get the most up-to-date max_tx_sch_inputs. */
474f1ca94deSMichael Chan 	if (BNXT_NEW_RM(bp))
475db4723b3SMichael Chan 		bnxt_hwrm_func_resc_qcaps(bp, false);
476db4723b3SMichael Chan 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
477c0c050c5SMichael Chan 
4786e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
479db4723b3SMichael Chan 	if (max_tx_sch_inputs)
480db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
481a79a5276SMichael Chan 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
482068c9ec6SMichael Chan 
48318d6e4e2SSatish Baddipadige 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
48418d6e4e2SSatish Baddipadige 		max_rx_rings = 0;
48518d6e4e2SSatish Baddipadige 		max_tx_rings = 0;
48618d6e4e2SSatish Baddipadige 	}
487db4723b3SMichael Chan 	if (max_tx_sch_inputs)
488db4723b3SMichael Chan 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
48918d6e4e2SSatish Baddipadige 
490c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
491c0c050c5SMichael Chan 	if (tcs > 1)
492c0c050c5SMichael Chan 		max_tx_rings /= tcs;
493c0c050c5SMichael Chan 
494c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
495c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
496c0c050c5SMichael Chan 	channel->max_other = 0;
497068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
498068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
49976595193SPrashant Sreedharan 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
50076595193SPrashant Sreedharan 			channel->combined_count--;
501068c9ec6SMichael Chan 	} else {
50276595193SPrashant Sreedharan 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
503c0c050c5SMichael Chan 			channel->rx_count = bp->rx_nr_rings;
504c0c050c5SMichael Chan 			channel->tx_count = bp->tx_nr_rings_per_tc;
505c0c050c5SMichael Chan 		}
506068c9ec6SMichael Chan 	}
50776595193SPrashant Sreedharan }
508c0c050c5SMichael Chan 
509c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
510c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
511c0c050c5SMichael Chan {
512c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
513d1e7925eSMichael Chan 	int req_tx_rings, req_rx_rings, tcs;
514068c9ec6SMichael Chan 	bool sh = false;
5155f449249SMichael Chan 	int tx_xdp = 0;
516d1e7925eSMichael Chan 	int rc = 0;
517c0c050c5SMichael Chan 
518068c9ec6SMichael Chan 	if (channel->other_count)
519c0c050c5SMichael Chan 		return -EINVAL;
520c0c050c5SMichael Chan 
521068c9ec6SMichael Chan 	if (!channel->combined_count &&
522068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
523068c9ec6SMichael Chan 		return -EINVAL;
524068c9ec6SMichael Chan 
525068c9ec6SMichael Chan 	if (channel->combined_count &&
526068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
527068c9ec6SMichael Chan 		return -EINVAL;
528068c9ec6SMichael Chan 
52976595193SPrashant Sreedharan 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
53076595193SPrashant Sreedharan 					    channel->tx_count))
53176595193SPrashant Sreedharan 		return -EINVAL;
53276595193SPrashant Sreedharan 
533068c9ec6SMichael Chan 	if (channel->combined_count)
534068c9ec6SMichael Chan 		sh = true;
535068c9ec6SMichael Chan 
536c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
537c0c050c5SMichael Chan 
538391be5c2SMichael Chan 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
539d1e7925eSMichael Chan 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
5405f449249SMichael Chan 	if (bp->tx_nr_rings_xdp) {
5415f449249SMichael Chan 		if (!sh) {
5425f449249SMichael Chan 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
5435f449249SMichael Chan 			return -EINVAL;
5445f449249SMichael Chan 		}
5455f449249SMichael Chan 		tx_xdp = req_rx_rings;
5465f449249SMichael Chan 	}
54798fdbe73SMichael Chan 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
548d1e7925eSMichael Chan 	if (rc) {
549d1e7925eSMichael Chan 		netdev_warn(dev, "Unable to allocate the requested rings\n");
550d1e7925eSMichael Chan 		return rc;
551391be5c2SMichael Chan 	}
552391be5c2SMichael Chan 
553c0c050c5SMichael Chan 	if (netif_running(dev)) {
554c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
555c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
556c0c050c5SMichael Chan 			 * before PF unload
557c0c050c5SMichael Chan 			 */
558c0c050c5SMichael Chan 		}
559c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
560c0c050c5SMichael Chan 		if (rc) {
561c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
562c0c050c5SMichael Chan 				   rc);
563c0c050c5SMichael Chan 			return rc;
564c0c050c5SMichael Chan 		}
565c0c050c5SMichael Chan 	}
566c0c050c5SMichael Chan 
567068c9ec6SMichael Chan 	if (sh) {
568068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
569d1e7925eSMichael Chan 		bp->rx_nr_rings = channel->combined_count;
570d1e7925eSMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
571068c9ec6SMichael Chan 	} else {
572068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
573c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
574c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
575068c9ec6SMichael Chan 	}
5765f449249SMichael Chan 	bp->tx_nr_rings_xdp = tx_xdp;
5775f449249SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
578c0c050c5SMichael Chan 	if (tcs > 1)
5795f449249SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
580068c9ec6SMichael Chan 
581068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
582068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
583068c9ec6SMichael Chan 
584c0c050c5SMichael Chan 	bp->num_stat_ctxs = bp->cp_nr_rings;
585c0c050c5SMichael Chan 
5862bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
5872bcfa6f6SMichael Chan 	netdev_update_features(dev);
588c0c050c5SMichael Chan 	if (netif_running(dev)) {
589c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
590c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
591c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
592c0c050c5SMichael Chan 			 * to renable
593c0c050c5SMichael Chan 			 */
594c0c050c5SMichael Chan 		}
595d8c09f19SMichael Chan 	} else {
596d8c09f19SMichael Chan 		rc = bnxt_reserve_rings(bp);
597c0c050c5SMichael Chan 	}
598c0c050c5SMichael Chan 
599c0c050c5SMichael Chan 	return rc;
600c0c050c5SMichael Chan }
601c0c050c5SMichael Chan 
602c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
603c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
604c0c050c5SMichael Chan 			    u32 *rule_locs)
605c0c050c5SMichael Chan {
606c0c050c5SMichael Chan 	int i, j = 0;
607c0c050c5SMichael Chan 
608c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
609c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
610c0c050c5SMichael Chan 		struct hlist_head *head;
611c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
612c0c050c5SMichael Chan 
613c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
614c0c050c5SMichael Chan 		rcu_read_lock();
615c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
616c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
617c0c050c5SMichael Chan 				break;
618c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
619c0c050c5SMichael Chan 		}
620c0c050c5SMichael Chan 		rcu_read_unlock();
621c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
622c0c050c5SMichael Chan 			break;
623c0c050c5SMichael Chan 	}
624c0c050c5SMichael Chan 	cmd->rule_cnt = j;
625c0c050c5SMichael Chan 	return 0;
626c0c050c5SMichael Chan }
627c0c050c5SMichael Chan 
628c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
629c0c050c5SMichael Chan {
630c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
631c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
632c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
633c0c050c5SMichael Chan 	struct flow_keys *fkeys;
634c0c050c5SMichael Chan 	int i, rc = -EINVAL;
635c0c050c5SMichael Chan 
636b721cfafSstephen hemminger 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
637c0c050c5SMichael Chan 		return rc;
638c0c050c5SMichael Chan 
639c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
640c0c050c5SMichael Chan 		struct hlist_head *head;
641c0c050c5SMichael Chan 
642c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
643c0c050c5SMichael Chan 		rcu_read_lock();
644c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
645c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
646c0c050c5SMichael Chan 				goto fltr_found;
647c0c050c5SMichael Chan 		}
648c0c050c5SMichael Chan 		rcu_read_unlock();
649c0c050c5SMichael Chan 	}
650c0c050c5SMichael Chan 	return rc;
651c0c050c5SMichael Chan 
652c0c050c5SMichael Chan fltr_found:
653c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
654dda0e746SMichael Chan 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
655c0c050c5SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
656c0c050c5SMichael Chan 			fs->flow_type = TCP_V4_FLOW;
657c0c050c5SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
658c0c050c5SMichael Chan 			fs->flow_type = UDP_V4_FLOW;
659c0c050c5SMichael Chan 		else
660c0c050c5SMichael Chan 			goto fltr_err;
661c0c050c5SMichael Chan 
662c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
663c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
664c0c050c5SMichael Chan 
665c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
666c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
667c0c050c5SMichael Chan 
668c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
669c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
670c0c050c5SMichael Chan 
671c0c050c5SMichael Chan 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
672c0c050c5SMichael Chan 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
673dda0e746SMichael Chan 	} else {
674dda0e746SMichael Chan 		int i;
675dda0e746SMichael Chan 
676dda0e746SMichael Chan 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
677dda0e746SMichael Chan 			fs->flow_type = TCP_V6_FLOW;
678dda0e746SMichael Chan 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
679dda0e746SMichael Chan 			fs->flow_type = UDP_V6_FLOW;
680dda0e746SMichael Chan 		else
681dda0e746SMichael Chan 			goto fltr_err;
682dda0e746SMichael Chan 
683dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
684dda0e746SMichael Chan 			fkeys->addrs.v6addrs.src;
685dda0e746SMichael Chan 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
686dda0e746SMichael Chan 			fkeys->addrs.v6addrs.dst;
687dda0e746SMichael Chan 		for (i = 0; i < 4; i++) {
688dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
689dda0e746SMichael Chan 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
690dda0e746SMichael Chan 		}
691dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
692dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
693dda0e746SMichael Chan 
694dda0e746SMichael Chan 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
695dda0e746SMichael Chan 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
696dda0e746SMichael Chan 	}
697c0c050c5SMichael Chan 
698c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
699c0c050c5SMichael Chan 	rc = 0;
700c0c050c5SMichael Chan 
701c0c050c5SMichael Chan fltr_err:
702c0c050c5SMichael Chan 	rcu_read_unlock();
703c0c050c5SMichael Chan 
704c0c050c5SMichael Chan 	return rc;
705c0c050c5SMichael Chan }
706a011952aSMichael Chan #endif
707a011952aSMichael Chan 
708a011952aSMichael Chan static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
709a011952aSMichael Chan {
710a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
711a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
712a011952aSMichael Chan 	return 0;
713a011952aSMichael Chan }
714a011952aSMichael Chan 
715a011952aSMichael Chan static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
716a011952aSMichael Chan {
717a011952aSMichael Chan 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
718a011952aSMichael Chan 		return RXH_IP_SRC | RXH_IP_DST;
719a011952aSMichael Chan 	return 0;
720a011952aSMichael Chan }
721a011952aSMichael Chan 
722a011952aSMichael Chan static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
723a011952aSMichael Chan {
724a011952aSMichael Chan 	cmd->data = 0;
725a011952aSMichael Chan 	switch (cmd->flow_type) {
726a011952aSMichael Chan 	case TCP_V4_FLOW:
727a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
728a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
729a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
730a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
731a011952aSMichael Chan 		break;
732a011952aSMichael Chan 	case UDP_V4_FLOW:
733a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
734a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
735a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
736a011952aSMichael Chan 		/* fall through */
737a011952aSMichael Chan 	case SCTP_V4_FLOW:
738a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
739a011952aSMichael Chan 	case AH_V4_FLOW:
740a011952aSMichael Chan 	case ESP_V4_FLOW:
741a011952aSMichael Chan 	case IPV4_FLOW:
742a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv4_rss(bp);
743a011952aSMichael Chan 		break;
744a011952aSMichael Chan 
745a011952aSMichael Chan 	case TCP_V6_FLOW:
746a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
747a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
748a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
749a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
750a011952aSMichael Chan 		break;
751a011952aSMichael Chan 	case UDP_V6_FLOW:
752a011952aSMichael Chan 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
753a011952aSMichael Chan 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
754a011952aSMichael Chan 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
755a011952aSMichael Chan 		/* fall through */
756a011952aSMichael Chan 	case SCTP_V6_FLOW:
757a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
758a011952aSMichael Chan 	case AH_V6_FLOW:
759a011952aSMichael Chan 	case ESP_V6_FLOW:
760a011952aSMichael Chan 	case IPV6_FLOW:
761a011952aSMichael Chan 		cmd->data |= get_ethtool_ipv6_rss(bp);
762a011952aSMichael Chan 		break;
763a011952aSMichael Chan 	}
764a011952aSMichael Chan 	return 0;
765a011952aSMichael Chan }
766a011952aSMichael Chan 
767a011952aSMichael Chan #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
768a011952aSMichael Chan #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
769a011952aSMichael Chan 
770a011952aSMichael Chan static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
771a011952aSMichael Chan {
772a011952aSMichael Chan 	u32 rss_hash_cfg = bp->rss_hash_cfg;
773a011952aSMichael Chan 	int tuple, rc = 0;
774a011952aSMichael Chan 
775a011952aSMichael Chan 	if (cmd->data == RXH_4TUPLE)
776a011952aSMichael Chan 		tuple = 4;
777a011952aSMichael Chan 	else if (cmd->data == RXH_2TUPLE)
778a011952aSMichael Chan 		tuple = 2;
779a011952aSMichael Chan 	else if (!cmd->data)
780a011952aSMichael Chan 		tuple = 0;
781a011952aSMichael Chan 	else
782a011952aSMichael Chan 		return -EINVAL;
783a011952aSMichael Chan 
784a011952aSMichael Chan 	if (cmd->flow_type == TCP_V4_FLOW) {
785a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
786a011952aSMichael Chan 		if (tuple == 4)
787a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
788a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V4_FLOW) {
789a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
790a011952aSMichael Chan 			return -EINVAL;
791a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
792a011952aSMichael Chan 		if (tuple == 4)
793a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
794a011952aSMichael Chan 	} else if (cmd->flow_type == TCP_V6_FLOW) {
795a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
796a011952aSMichael Chan 		if (tuple == 4)
797a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
798a011952aSMichael Chan 	} else if (cmd->flow_type == UDP_V6_FLOW) {
799a011952aSMichael Chan 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
800a011952aSMichael Chan 			return -EINVAL;
801a011952aSMichael Chan 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
802a011952aSMichael Chan 		if (tuple == 4)
803a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
804a011952aSMichael Chan 	} else if (tuple == 4) {
805a011952aSMichael Chan 		return -EINVAL;
806a011952aSMichael Chan 	}
807a011952aSMichael Chan 
808a011952aSMichael Chan 	switch (cmd->flow_type) {
809a011952aSMichael Chan 	case TCP_V4_FLOW:
810a011952aSMichael Chan 	case UDP_V4_FLOW:
811a011952aSMichael Chan 	case SCTP_V4_FLOW:
812a011952aSMichael Chan 	case AH_ESP_V4_FLOW:
813a011952aSMichael Chan 	case AH_V4_FLOW:
814a011952aSMichael Chan 	case ESP_V4_FLOW:
815a011952aSMichael Chan 	case IPV4_FLOW:
816a011952aSMichael Chan 		if (tuple == 2)
817a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
818a011952aSMichael Chan 		else if (!tuple)
819a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
820a011952aSMichael Chan 		break;
821a011952aSMichael Chan 
822a011952aSMichael Chan 	case TCP_V6_FLOW:
823a011952aSMichael Chan 	case UDP_V6_FLOW:
824a011952aSMichael Chan 	case SCTP_V6_FLOW:
825a011952aSMichael Chan 	case AH_ESP_V6_FLOW:
826a011952aSMichael Chan 	case AH_V6_FLOW:
827a011952aSMichael Chan 	case ESP_V6_FLOW:
828a011952aSMichael Chan 	case IPV6_FLOW:
829a011952aSMichael Chan 		if (tuple == 2)
830a011952aSMichael Chan 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
831a011952aSMichael Chan 		else if (!tuple)
832a011952aSMichael Chan 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
833a011952aSMichael Chan 		break;
834a011952aSMichael Chan 	}
835a011952aSMichael Chan 
836a011952aSMichael Chan 	if (bp->rss_hash_cfg == rss_hash_cfg)
837a011952aSMichael Chan 		return 0;
838a011952aSMichael Chan 
839a011952aSMichael Chan 	bp->rss_hash_cfg = rss_hash_cfg;
840a011952aSMichael Chan 	if (netif_running(bp->dev)) {
841a011952aSMichael Chan 		bnxt_close_nic(bp, false, false);
842a011952aSMichael Chan 		rc = bnxt_open_nic(bp, false, false);
843a011952aSMichael Chan 	}
844a011952aSMichael Chan 	return rc;
845a011952aSMichael Chan }
846c0c050c5SMichael Chan 
847c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
848c0c050c5SMichael Chan 			  u32 *rule_locs)
849c0c050c5SMichael Chan {
850c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
851c0c050c5SMichael Chan 	int rc = 0;
852c0c050c5SMichael Chan 
853c0c050c5SMichael Chan 	switch (cmd->cmd) {
854a011952aSMichael Chan #ifdef CONFIG_RFS_ACCEL
855c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
856c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
857c0c050c5SMichael Chan 		break;
858c0c050c5SMichael Chan 
859c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
860c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
861c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
862c0c050c5SMichael Chan 		break;
863c0c050c5SMichael Chan 
864c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
865c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
866c0c050c5SMichael Chan 		break;
867c0c050c5SMichael Chan 
868c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
869c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
870c0c050c5SMichael Chan 		break;
871a011952aSMichael Chan #endif
872a011952aSMichael Chan 
873a011952aSMichael Chan 	case ETHTOOL_GRXFH:
874a011952aSMichael Chan 		rc = bnxt_grxfh(bp, cmd);
875a011952aSMichael Chan 		break;
876c0c050c5SMichael Chan 
877c0c050c5SMichael Chan 	default:
878c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
879c0c050c5SMichael Chan 		break;
880c0c050c5SMichael Chan 	}
881c0c050c5SMichael Chan 
882c0c050c5SMichael Chan 	return rc;
883c0c050c5SMichael Chan }
884a011952aSMichael Chan 
885a011952aSMichael Chan static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
886a011952aSMichael Chan {
887a011952aSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
888a011952aSMichael Chan 	int rc;
889a011952aSMichael Chan 
890a011952aSMichael Chan 	switch (cmd->cmd) {
891a011952aSMichael Chan 	case ETHTOOL_SRXFH:
892a011952aSMichael Chan 		rc = bnxt_srxfh(bp, cmd);
893a011952aSMichael Chan 		break;
894a011952aSMichael Chan 
895a011952aSMichael Chan 	default:
896a011952aSMichael Chan 		rc = -EOPNOTSUPP;
897a011952aSMichael Chan 		break;
898a011952aSMichael Chan 	}
899a011952aSMichael Chan 	return rc;
900a011952aSMichael Chan }
901c0c050c5SMichael Chan 
902c0c050c5SMichael Chan static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
903c0c050c5SMichael Chan {
904c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
905c0c050c5SMichael Chan }
906c0c050c5SMichael Chan 
907c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
908c0c050c5SMichael Chan {
909c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
910c0c050c5SMichael Chan }
911c0c050c5SMichael Chan 
912c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
913c0c050c5SMichael Chan 			 u8 *hfunc)
914c0c050c5SMichael Chan {
915c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
9167991cb9cSMichael Chan 	struct bnxt_vnic_info *vnic;
917c0c050c5SMichael Chan 	int i = 0;
918c0c050c5SMichael Chan 
919c0c050c5SMichael Chan 	if (hfunc)
920c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
921c0c050c5SMichael Chan 
9227991cb9cSMichael Chan 	if (!bp->vnic_info)
9237991cb9cSMichael Chan 		return 0;
9247991cb9cSMichael Chan 
9257991cb9cSMichael Chan 	vnic = &bp->vnic_info[0];
9267991cb9cSMichael Chan 	if (indir && vnic->rss_table) {
927c0c050c5SMichael Chan 		for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
928c0c050c5SMichael Chan 			indir[i] = le16_to_cpu(vnic->rss_table[i]);
9297991cb9cSMichael Chan 	}
930c0c050c5SMichael Chan 
9317991cb9cSMichael Chan 	if (key && vnic->rss_hash_key)
932c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
933c0c050c5SMichael Chan 
934c0c050c5SMichael Chan 	return 0;
935c0c050c5SMichael Chan }
936c0c050c5SMichael Chan 
937c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
938c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
939c0c050c5SMichael Chan {
940c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
941c0c050c5SMichael Chan 
942c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
943c0c050c5SMichael Chan 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
944431aa1ebSMichael Chan 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
945c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
9465c8227d0SMichael Chan 	info->n_stats = bnxt_get_num_stats(bp);
947eb513658SMichael Chan 	info->testinfo_len = bp->num_tests;
948c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
949c0c050c5SMichael Chan 	info->eedump_len = 0;
950c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
951c0c050c5SMichael Chan 	info->regdump_len = 0;
952c0c050c5SMichael Chan }
953c0c050c5SMichael Chan 
9548e202366SMichael Chan static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9558e202366SMichael Chan {
9568e202366SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
9578e202366SMichael Chan 
9588e202366SMichael Chan 	wol->supported = 0;
9598e202366SMichael Chan 	wol->wolopts = 0;
9608e202366SMichael Chan 	memset(&wol->sopass, 0, sizeof(wol->sopass));
9618e202366SMichael Chan 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
9628e202366SMichael Chan 		wol->supported = WAKE_MAGIC;
9638e202366SMichael Chan 		if (bp->wol)
9648e202366SMichael Chan 			wol->wolopts = WAKE_MAGIC;
9658e202366SMichael Chan 	}
9668e202366SMichael Chan }
9678e202366SMichael Chan 
9685282db6cSMichael Chan static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9695282db6cSMichael Chan {
9705282db6cSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
9715282db6cSMichael Chan 
9725282db6cSMichael Chan 	if (wol->wolopts & ~WAKE_MAGIC)
9735282db6cSMichael Chan 		return -EINVAL;
9745282db6cSMichael Chan 
9755282db6cSMichael Chan 	if (wol->wolopts & WAKE_MAGIC) {
9765282db6cSMichael Chan 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
9775282db6cSMichael Chan 			return -EINVAL;
9785282db6cSMichael Chan 		if (!bp->wol) {
9795282db6cSMichael Chan 			if (bnxt_hwrm_alloc_wol_fltr(bp))
9805282db6cSMichael Chan 				return -EBUSY;
9815282db6cSMichael Chan 			bp->wol = 1;
9825282db6cSMichael Chan 		}
9835282db6cSMichael Chan 	} else {
9845282db6cSMichael Chan 		if (bp->wol) {
9855282db6cSMichael Chan 			if (bnxt_hwrm_free_wol_fltr(bp))
9865282db6cSMichael Chan 				return -EBUSY;
9875282db6cSMichael Chan 			bp->wol = 0;
9885282db6cSMichael Chan 		}
9895282db6cSMichael Chan 	}
9905282db6cSMichael Chan 	return 0;
9915282db6cSMichael Chan }
9925282db6cSMichael Chan 
993170ce013SMichael Chan u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
994c0c050c5SMichael Chan {
995c0c050c5SMichael Chan 	u32 speed_mask = 0;
996c0c050c5SMichael Chan 
997c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
998c0c050c5SMichael Chan 	/* set the advertised speeds */
999c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1000c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
1001c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1002c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
1003c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1004c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
1005c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1006c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
1007c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
10081c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
100927c4d578SMichael Chan 
101027c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
101127c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
101227c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
101327c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
101427c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
101527c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
101627c4d578SMichael Chan 
1017c0c050c5SMichael Chan 	return speed_mask;
1018c0c050c5SMichael Chan }
1019c0c050c5SMichael Chan 
102000c04a92SMichael Chan #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
102100c04a92SMichael Chan {									\
102200c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
102300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
102400c04a92SMichael Chan 						     100baseT_Full);	\
102500c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
102600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
102700c04a92SMichael Chan 						     1000baseT_Full);	\
102800c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
102900c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
103000c04a92SMichael Chan 						     10000baseT_Full);	\
103100c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
103200c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
103300c04a92SMichael Chan 						     25000baseCR_Full);	\
103400c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
103500c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
103600c04a92SMichael Chan 						     40000baseCR4_Full);\
103700c04a92SMichael Chan 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
103800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
103900c04a92SMichael Chan 						     50000baseCR2_Full);\
104038a21b34SDeepak Khungar 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
104138a21b34SDeepak Khungar 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
104238a21b34SDeepak Khungar 						     100000baseCR4_Full);\
104300c04a92SMichael Chan 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
104400c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
104500c04a92SMichael Chan 						     Pause);		\
104600c04a92SMichael Chan 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
104700c04a92SMichael Chan 			ethtool_link_ksettings_add_link_mode(		\
104800c04a92SMichael Chan 					lk_ksettings, name, Asym_Pause);\
104900c04a92SMichael Chan 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
105000c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
105100c04a92SMichael Chan 						     Asym_Pause);	\
105200c04a92SMichael Chan 	}								\
105300c04a92SMichael Chan }
105400c04a92SMichael Chan 
105500c04a92SMichael Chan #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
105600c04a92SMichael Chan {									\
105700c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
105800c04a92SMichael Chan 						  100baseT_Full) ||	\
105900c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
106000c04a92SMichael Chan 						  100baseT_Half))	\
106100c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
106200c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
106300c04a92SMichael Chan 						  1000baseT_Full) ||	\
106400c04a92SMichael Chan 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
106500c04a92SMichael Chan 						  1000baseT_Half))	\
106600c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
106700c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
106800c04a92SMichael Chan 						  10000baseT_Full))	\
106900c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
107000c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
107100c04a92SMichael Chan 						  25000baseCR_Full))	\
107200c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
107300c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
107400c04a92SMichael Chan 						  40000baseCR4_Full))	\
107500c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
107600c04a92SMichael Chan 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
107700c04a92SMichael Chan 						  50000baseCR2_Full))	\
107800c04a92SMichael Chan 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
107938a21b34SDeepak Khungar 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
108038a21b34SDeepak Khungar 						  100000baseCR4_Full))	\
108138a21b34SDeepak Khungar 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
108200c04a92SMichael Chan }
108300c04a92SMichael Chan 
108400c04a92SMichael Chan static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
108500c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
108627c4d578SMichael Chan {
108768515a18SMichael Chan 	u16 fw_speeds = link_info->advertising;
108827c4d578SMichael Chan 	u8 fw_pause = 0;
108927c4d578SMichael Chan 
109027c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
109127c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
109227c4d578SMichael Chan 
109300c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
109427c4d578SMichael Chan }
109527c4d578SMichael Chan 
109600c04a92SMichael Chan static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
109700c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
10983277360eSMichael Chan {
10993277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
11003277360eSMichael Chan 	u8 fw_pause = 0;
11013277360eSMichael Chan 
11023277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11033277360eSMichael Chan 		fw_pause = link_info->lp_pause;
11043277360eSMichael Chan 
110500c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
110600c04a92SMichael Chan 				lp_advertising);
11073277360eSMichael Chan }
11083277360eSMichael Chan 
110900c04a92SMichael Chan static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
111000c04a92SMichael Chan 				struct ethtool_link_ksettings *lk_ksettings)
11114b32caccSMichael Chan {
11124b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
11134b32caccSMichael Chan 
111400c04a92SMichael Chan 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
11154b32caccSMichael Chan 
111600c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
111700c04a92SMichael Chan 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
111800c04a92SMichael Chan 					     Asym_Pause);
111993ed8117SMichael Chan 
112000c04a92SMichael Chan 	if (link_info->support_auto_speeds)
112100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
112200c04a92SMichael Chan 						     Autoneg);
112393ed8117SMichael Chan }
112493ed8117SMichael Chan 
1125c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1126c0c050c5SMichael Chan {
1127c0c050c5SMichael Chan 	switch (fw_link_speed) {
1128c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
1129c0c050c5SMichael Chan 		return SPEED_100;
1130c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
1131c0c050c5SMichael Chan 		return SPEED_1000;
1132c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
1133c0c050c5SMichael Chan 		return SPEED_2500;
1134c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
1135c0c050c5SMichael Chan 		return SPEED_10000;
1136c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
1137c0c050c5SMichael Chan 		return SPEED_20000;
1138c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
1139c0c050c5SMichael Chan 		return SPEED_25000;
1140c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
1141c0c050c5SMichael Chan 		return SPEED_40000;
1142c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
1143c0c050c5SMichael Chan 		return SPEED_50000;
114438a21b34SDeepak Khungar 	case BNXT_LINK_SPEED_100GB:
114538a21b34SDeepak Khungar 		return SPEED_100000;
1146c0c050c5SMichael Chan 	default:
1147c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
1148c0c050c5SMichael Chan 	}
1149c0c050c5SMichael Chan }
1150c0c050c5SMichael Chan 
115100c04a92SMichael Chan static int bnxt_get_link_ksettings(struct net_device *dev,
115200c04a92SMichael Chan 				   struct ethtool_link_ksettings *lk_ksettings)
1153c0c050c5SMichael Chan {
1154c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1155c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
115600c04a92SMichael Chan 	struct ethtool_link_settings *base = &lk_ksettings->base;
115700c04a92SMichael Chan 	u32 ethtool_speed;
1158c0c050c5SMichael Chan 
115900c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1160e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
116100c04a92SMichael Chan 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1162c0c050c5SMichael Chan 
116300c04a92SMichael Chan 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1164b763499eSMichael Chan 	if (link_info->autoneg) {
116500c04a92SMichael Chan 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
116600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
116700c04a92SMichael Chan 						     advertising, Autoneg);
116800c04a92SMichael Chan 		base->autoneg = AUTONEG_ENABLE;
11693277360eSMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK)
117000c04a92SMichael Chan 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
117129c262feSMichael Chan 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
117229c262feSMichael Chan 		if (!netif_carrier_ok(dev))
117300c04a92SMichael Chan 			base->duplex = DUPLEX_UNKNOWN;
117429c262feSMichael Chan 		else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
117500c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
117629c262feSMichael Chan 		else
117700c04a92SMichael Chan 			base->duplex = DUPLEX_HALF;
1178c0c050c5SMichael Chan 	} else {
117900c04a92SMichael Chan 		base->autoneg = AUTONEG_DISABLE;
118029c262feSMichael Chan 		ethtool_speed =
118129c262feSMichael Chan 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
118200c04a92SMichael Chan 		base->duplex = DUPLEX_HALF;
118329c262feSMichael Chan 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
118400c04a92SMichael Chan 			base->duplex = DUPLEX_FULL;
1185c0c050c5SMichael Chan 	}
118600c04a92SMichael Chan 	base->speed = ethtool_speed;
1187c0c050c5SMichael Chan 
118800c04a92SMichael Chan 	base->port = PORT_NONE;
1189c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
119000c04a92SMichael Chan 		base->port = PORT_TP;
119100c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
119200c04a92SMichael Chan 						     TP);
119300c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
119400c04a92SMichael Chan 						     TP);
1195c0c050c5SMichael Chan 	} else {
119600c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
119700c04a92SMichael Chan 						     FIBRE);
119800c04a92SMichael Chan 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
119900c04a92SMichael Chan 						     FIBRE);
1200c0c050c5SMichael Chan 
1201c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
120200c04a92SMichael Chan 			base->port = PORT_DA;
1203c0c050c5SMichael Chan 		else if (link_info->media_type ==
1204c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
120500c04a92SMichael Chan 			base->port = PORT_FIBRE;
1206c0c050c5SMichael Chan 	}
120700c04a92SMichael Chan 	base->phy_address = link_info->phy_addr;
1208e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1209c0c050c5SMichael Chan 
1210c0c050c5SMichael Chan 	return 0;
1211c0c050c5SMichael Chan }
1212c0c050c5SMichael Chan 
121338a21b34SDeepak Khungar static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed)
1214c0c050c5SMichael Chan {
12159d9cee08SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
12169d9cee08SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
12179d9cee08SMichael Chan 	u16 support_spds = link_info->support_speeds;
12189d9cee08SMichael Chan 	u32 fw_speed = 0;
12199d9cee08SMichael Chan 
1220c0c050c5SMichael Chan 	switch (ethtool_speed) {
1221c0c050c5SMichael Chan 	case SPEED_100:
12229d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
12239d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
12249d9cee08SMichael Chan 		break;
1225c0c050c5SMichael Chan 	case SPEED_1000:
12269d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
12279d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
12289d9cee08SMichael Chan 		break;
1229c0c050c5SMichael Chan 	case SPEED_2500:
12309d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
12319d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
12329d9cee08SMichael Chan 		break;
1233c0c050c5SMichael Chan 	case SPEED_10000:
12349d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
12359d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
12369d9cee08SMichael Chan 		break;
1237c0c050c5SMichael Chan 	case SPEED_20000:
12389d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
12399d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
12409d9cee08SMichael Chan 		break;
1241c0c050c5SMichael Chan 	case SPEED_25000:
12429d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
12439d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
12449d9cee08SMichael Chan 		break;
1245c0c050c5SMichael Chan 	case SPEED_40000:
12469d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
12479d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
12489d9cee08SMichael Chan 		break;
1249c0c050c5SMichael Chan 	case SPEED_50000:
12509d9cee08SMichael Chan 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
12519d9cee08SMichael Chan 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
12529d9cee08SMichael Chan 		break;
125338a21b34SDeepak Khungar 	case SPEED_100000:
125438a21b34SDeepak Khungar 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB)
125538a21b34SDeepak Khungar 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB;
125638a21b34SDeepak Khungar 		break;
1257c0c050c5SMichael Chan 	default:
1258c0c050c5SMichael Chan 		netdev_err(dev, "unsupported speed!\n");
1259c0c050c5SMichael Chan 		break;
1260c0c050c5SMichael Chan 	}
12619d9cee08SMichael Chan 	return fw_speed;
1262c0c050c5SMichael Chan }
1263c0c050c5SMichael Chan 
1264939f7f0cSMichael Chan u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1265c0c050c5SMichael Chan {
1266c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
1267c0c050c5SMichael Chan 
1268c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
1269c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
1270c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
1271c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1272c0c050c5SMichael Chan 	}
1273c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
1274c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
1275c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1276c0c050c5SMichael Chan 	}
1277c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
1278c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1279c0c050c5SMichael Chan 
12801c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
12811c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
12821c49c421SMichael Chan 
1283c0c050c5SMichael Chan 	return fw_speed_mask;
1284c0c050c5SMichael Chan }
1285c0c050c5SMichael Chan 
128600c04a92SMichael Chan static int bnxt_set_link_ksettings(struct net_device *dev,
128700c04a92SMichael Chan 			   const struct ethtool_link_ksettings *lk_ksettings)
1288c0c050c5SMichael Chan {
1289c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1290c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
129100c04a92SMichael Chan 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1292c0c050c5SMichael Chan 	bool set_pause = false;
129368515a18SMichael Chan 	u16 fw_advertising = 0;
129468515a18SMichael Chan 	u32 speed;
129500c04a92SMichael Chan 	int rc = 0;
1296c0c050c5SMichael Chan 
1297567b2abeSSatish Baddipadige 	if (!BNXT_SINGLE_PF(bp))
129800c04a92SMichael Chan 		return -EOPNOTSUPP;
1299c0c050c5SMichael Chan 
1300e2dc9b6eSMichael Chan 	mutex_lock(&bp->link_lock);
130100c04a92SMichael Chan 	if (base->autoneg == AUTONEG_ENABLE) {
130200c04a92SMichael Chan 		BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
130300c04a92SMichael Chan 					advertising);
1304c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1305c0c050c5SMichael Chan 		if (!fw_advertising)
130693ed8117SMichael Chan 			link_info->advertising = link_info->support_auto_speeds;
1307c0c050c5SMichael Chan 		else
1308c0c050c5SMichael Chan 			link_info->advertising = fw_advertising;
1309c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
1310c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
1311c0c050c5SMichael Chan 		 */
1312c0c050c5SMichael Chan 		set_pause = true;
1313c0c050c5SMichael Chan 	} else {
13149d9cee08SMichael Chan 		u16 fw_speed;
131503efbec0SMichael Chan 		u8 phy_type = link_info->phy_type;
13169d9cee08SMichael Chan 
131703efbec0SMichael Chan 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
131803efbec0SMichael Chan 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
131903efbec0SMichael Chan 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
132003efbec0SMichael Chan 			netdev_err(dev, "10GBase-T devices must autoneg\n");
132103efbec0SMichael Chan 			rc = -EINVAL;
132203efbec0SMichael Chan 			goto set_setting_exit;
132303efbec0SMichael Chan 		}
132400c04a92SMichael Chan 		if (base->duplex == DUPLEX_HALF) {
1325c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1326c0c050c5SMichael Chan 			rc = -EINVAL;
1327c0c050c5SMichael Chan 			goto set_setting_exit;
1328c0c050c5SMichael Chan 		}
132900c04a92SMichael Chan 		speed = base->speed;
13309d9cee08SMichael Chan 		fw_speed = bnxt_get_fw_speed(dev, speed);
13319d9cee08SMichael Chan 		if (!fw_speed) {
13329d9cee08SMichael Chan 			rc = -EINVAL;
13339d9cee08SMichael Chan 			goto set_setting_exit;
13349d9cee08SMichael Chan 		}
13359d9cee08SMichael Chan 		link_info->req_link_speed = fw_speed;
1336c0c050c5SMichael Chan 		link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1337b763499eSMichael Chan 		link_info->autoneg = 0;
1338c0c050c5SMichael Chan 		link_info->advertising = 0;
1339c0c050c5SMichael Chan 	}
1340c0c050c5SMichael Chan 
1341c0c050c5SMichael Chan 	if (netif_running(dev))
1342939f7f0cSMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1343c0c050c5SMichael Chan 
1344c0c050c5SMichael Chan set_setting_exit:
1345e2dc9b6eSMichael Chan 	mutex_unlock(&bp->link_lock);
1346c0c050c5SMichael Chan 	return rc;
1347c0c050c5SMichael Chan }
1348c0c050c5SMichael Chan 
1349c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
1350c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
1351c0c050c5SMichael Chan {
1352c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1353c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1354c0c050c5SMichael Chan 
1355c0c050c5SMichael Chan 	if (BNXT_VF(bp))
1356c0c050c5SMichael Chan 		return;
1357b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
13583c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
13593c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1360c0c050c5SMichael Chan }
1361c0c050c5SMichael Chan 
1362c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
1363c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
1364c0c050c5SMichael Chan {
1365c0c050c5SMichael Chan 	int rc = 0;
1366c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1367c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
1368c0c050c5SMichael Chan 
1369567b2abeSSatish Baddipadige 	if (!BNXT_SINGLE_PF(bp))
137075362a3fSMichael Chan 		return -EOPNOTSUPP;
1371c0c050c5SMichael Chan 
1372c0c050c5SMichael Chan 	if (epause->autoneg) {
1373b763499eSMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
1374b763499eSMichael Chan 			return -EINVAL;
1375b763499eSMichael Chan 
1376c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1377c9ee9516SMichael Chan 		if (bp->hwrm_spec_code >= 0x10201)
1378c9ee9516SMichael Chan 			link_info->req_flow_ctrl =
1379c9ee9516SMichael Chan 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1380c0c050c5SMichael Chan 	} else {
1381c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
1382c0c050c5SMichael Chan 		 * force a link change
1383c0c050c5SMichael Chan 		 */
1384c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1385c0c050c5SMichael Chan 			link_info->force_link_chng = true;
1386c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1387c9ee9516SMichael Chan 		link_info->req_flow_ctrl = 0;
1388c0c050c5SMichael Chan 	}
1389c0c050c5SMichael Chan 	if (epause->rx_pause)
1390c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1391c0c050c5SMichael Chan 
1392c0c050c5SMichael Chan 	if (epause->tx_pause)
1393c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1394c0c050c5SMichael Chan 
1395c0c050c5SMichael Chan 	if (netif_running(dev))
1396c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
1397c0c050c5SMichael Chan 	return rc;
1398c0c050c5SMichael Chan }
1399c0c050c5SMichael Chan 
1400c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
1401c0c050c5SMichael Chan {
1402c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1403c0c050c5SMichael Chan 
1404c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
1405c0c050c5SMichael Chan 	return bp->link_info.link_up;
1406c0c050c5SMichael Chan }
1407c0c050c5SMichael Chan 
14085ac67d8bSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
14095ac67d8bSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
14105ac67d8bSRob Swindell 				u32 *data_length);
14115ac67d8bSRob Swindell 
1412c0c050c5SMichael Chan static int bnxt_flash_nvram(struct net_device *dev,
1413c0c050c5SMichael Chan 			    u16 dir_type,
1414c0c050c5SMichael Chan 			    u16 dir_ordinal,
1415c0c050c5SMichael Chan 			    u16 dir_ext,
1416c0c050c5SMichael Chan 			    u16 dir_attr,
1417c0c050c5SMichael Chan 			    const u8 *data,
1418c0c050c5SMichael Chan 			    size_t data_len)
1419c0c050c5SMichael Chan {
1420c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1421c0c050c5SMichael Chan 	int rc;
1422c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
1423c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1424c0c050c5SMichael Chan 	u8 *kmem;
1425c0c050c5SMichael Chan 
1426c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1427c0c050c5SMichael Chan 
1428c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
1429c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
1430c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
1431c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
1432c0c050c5SMichael Chan 	req.dir_data_length = cpu_to_le32(data_len);
1433c0c050c5SMichael Chan 
1434c0c050c5SMichael Chan 	kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1435c0c050c5SMichael Chan 				  GFP_KERNEL);
1436c0c050c5SMichael Chan 	if (!kmem) {
1437c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1438c0c050c5SMichael Chan 			   (unsigned)data_len);
1439c0c050c5SMichael Chan 		return -ENOMEM;
1440c0c050c5SMichael Chan 	}
1441c0c050c5SMichael Chan 	memcpy(kmem, data, data_len);
1442c0c050c5SMichael Chan 	req.host_src_addr = cpu_to_le64(dma_handle);
1443c0c050c5SMichael Chan 
1444c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1445c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1446c0c050c5SMichael Chan 
1447c0c050c5SMichael Chan 	return rc;
1448c0c050c5SMichael Chan }
1449c0c050c5SMichael Chan 
1450d2d6318cSRob Swindell static int bnxt_firmware_reset(struct net_device *dev,
1451d2d6318cSRob Swindell 			       u16 dir_type)
1452d2d6318cSRob Swindell {
1453d2d6318cSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
1454d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
1455d2d6318cSRob Swindell 
1456d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1457d2d6318cSRob Swindell 
1458d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1459d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
1460d2d6318cSRob Swindell 	switch (dir_type) {
1461d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
1462d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
1463d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
1464d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1465d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
1466d2d6318cSRob Swindell 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1467d2d6318cSRob Swindell 		break;
1468d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
1469d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
1470d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
147108141e0bSRob Swindell 		/* Self-reset APE upon next PCIe reset: */
147208141e0bSRob Swindell 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1473d2d6318cSRob Swindell 		break;
1474d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
1475d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
1476d2d6318cSRob Swindell 		req.embedded_proc_type =
1477d2d6318cSRob Swindell 			FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1478d2d6318cSRob Swindell 		break;
1479d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
1480d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
1481d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1482d2d6318cSRob Swindell 		break;
148349f7972fSVasundhara Volam 	case BNXT_FW_RESET_CHIP:
148449f7972fSVasundhara Volam 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
148549f7972fSVasundhara Volam 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
148649f7972fSVasundhara Volam 		break;
14876502ad59SScott Branden 	case BNXT_FW_RESET_AP:
14886502ad59SScott Branden 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP;
14896502ad59SScott Branden 		break;
1490d2d6318cSRob Swindell 	default:
1491d2d6318cSRob Swindell 		return -EINVAL;
1492d2d6318cSRob Swindell 	}
1493d2d6318cSRob Swindell 
1494d2d6318cSRob Swindell 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1495d2d6318cSRob Swindell }
1496d2d6318cSRob Swindell 
1497c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
1498c0c050c5SMichael Chan 			       u16 dir_type,
1499c0c050c5SMichael Chan 			       const u8 *fw_data,
1500c0c050c5SMichael Chan 			       size_t fw_size)
1501c0c050c5SMichael Chan {
1502c0c050c5SMichael Chan 	int	rc = 0;
1503c0c050c5SMichael Chan 	u16	code_type;
1504c0c050c5SMichael Chan 	u32	stored_crc;
1505c0c050c5SMichael Chan 	u32	calculated_crc;
1506c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1507c0c050c5SMichael Chan 
1508c0c050c5SMichael Chan 	switch (dir_type) {
1509c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1510c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1511c0c050c5SMichael Chan 		code_type = CODE_BOOT;
1512c0c050c5SMichael Chan 		break;
151393e0b4feSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
151493e0b4feSRob Swindell 		code_type = CODE_CHIMP_PATCH;
151593e0b4feSRob Swindell 		break;
15162731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
15172731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
15182731d70fSRob Swindell 		break;
151993e0b4feSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
152093e0b4feSRob Swindell 		code_type = CODE_APE_PATCH;
152193e0b4feSRob Swindell 		break;
152293e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
152393e0b4feSRob Swindell 		code_type = CODE_KONG_FW;
152493e0b4feSRob Swindell 		break;
152593e0b4feSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
152693e0b4feSRob Swindell 		code_type = CODE_KONG_PATCH;
152793e0b4feSRob Swindell 		break;
152893e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
152993e0b4feSRob Swindell 		code_type = CODE_BONO_FW;
153093e0b4feSRob Swindell 		break;
153193e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
153293e0b4feSRob Swindell 		code_type = CODE_BONO_PATCH;
153393e0b4feSRob Swindell 		break;
1534c0c050c5SMichael Chan 	default:
1535c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
1536c0c050c5SMichael Chan 			   dir_type);
1537c0c050c5SMichael Chan 		return -EINVAL;
1538c0c050c5SMichael Chan 	}
1539c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
1540c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
1541c0c050c5SMichael Chan 			   (unsigned int)fw_size);
1542c0c050c5SMichael Chan 		return -EINVAL;
1543c0c050c5SMichael Chan 	}
1544c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1545c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
1546c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
1547c0c050c5SMichael Chan 		return -EINVAL;
1548c0c050c5SMichael Chan 	}
1549c0c050c5SMichael Chan 	if (header->code_type != code_type) {
1550c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1551c0c050c5SMichael Chan 			   code_type, header->code_type);
1552c0c050c5SMichael Chan 		return -EINVAL;
1553c0c050c5SMichael Chan 	}
1554c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
1555c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1556c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
1557c0c050c5SMichael Chan 		return -EINVAL;
1558c0c050c5SMichael Chan 	}
1559c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
1560c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1561c0c050c5SMichael Chan 					     sizeof(stored_crc)));
1562c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1563c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
1564c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1565c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
1566c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
1567c0c050c5SMichael Chan 		return -EINVAL;
1568c0c050c5SMichael Chan 	}
1569c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1570c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
1571d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
1572d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
1573d2d6318cSRob Swindell 
1574c0c050c5SMichael Chan 	return rc;
1575c0c050c5SMichael Chan }
1576c0c050c5SMichael Chan 
15775ac67d8bSRob Swindell static int bnxt_flash_microcode(struct net_device *dev,
15785ac67d8bSRob Swindell 				u16 dir_type,
15795ac67d8bSRob Swindell 				const u8 *fw_data,
15805ac67d8bSRob Swindell 				size_t fw_size)
15815ac67d8bSRob Swindell {
15825ac67d8bSRob Swindell 	struct bnxt_ucode_trailer *trailer;
15835ac67d8bSRob Swindell 	u32 calculated_crc;
15845ac67d8bSRob Swindell 	u32 stored_crc;
15855ac67d8bSRob Swindell 	int rc = 0;
15865ac67d8bSRob Swindell 
15875ac67d8bSRob Swindell 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
15885ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode file size: %u\n",
15895ac67d8bSRob Swindell 			   (unsigned int)fw_size);
15905ac67d8bSRob Swindell 		return -EINVAL;
15915ac67d8bSRob Swindell 	}
15925ac67d8bSRob Swindell 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
15935ac67d8bSRob Swindell 						sizeof(*trailer)));
15945ac67d8bSRob Swindell 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
15955ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
15965ac67d8bSRob Swindell 			   le32_to_cpu(trailer->sig));
15975ac67d8bSRob Swindell 		return -EINVAL;
15985ac67d8bSRob Swindell 	}
15995ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
16005ac67d8bSRob Swindell 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
16015ac67d8bSRob Swindell 			   dir_type, le16_to_cpu(trailer->dir_type));
16025ac67d8bSRob Swindell 		return -EINVAL;
16035ac67d8bSRob Swindell 	}
16045ac67d8bSRob Swindell 	if (le16_to_cpu(trailer->trailer_length) <
16055ac67d8bSRob Swindell 		sizeof(struct bnxt_ucode_trailer)) {
16065ac67d8bSRob Swindell 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
16075ac67d8bSRob Swindell 			   le16_to_cpu(trailer->trailer_length));
16085ac67d8bSRob Swindell 		return -EINVAL;
16095ac67d8bSRob Swindell 	}
16105ac67d8bSRob Swindell 
16115ac67d8bSRob Swindell 	/* Confirm the CRC32 checksum of the file: */
16125ac67d8bSRob Swindell 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
16135ac67d8bSRob Swindell 					     sizeof(stored_crc)));
16145ac67d8bSRob Swindell 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
16155ac67d8bSRob Swindell 	if (calculated_crc != stored_crc) {
16165ac67d8bSRob Swindell 		netdev_err(dev,
16175ac67d8bSRob Swindell 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
16185ac67d8bSRob Swindell 			   (unsigned long)stored_crc,
16195ac67d8bSRob Swindell 			   (unsigned long)calculated_crc);
16205ac67d8bSRob Swindell 		return -EINVAL;
16215ac67d8bSRob Swindell 	}
16225ac67d8bSRob Swindell 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
16235ac67d8bSRob Swindell 			      0, 0, fw_data, fw_size);
16245ac67d8bSRob Swindell 
16255ac67d8bSRob Swindell 	return rc;
16265ac67d8bSRob Swindell }
16275ac67d8bSRob Swindell 
1628c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1629c0c050c5SMichael Chan {
1630c0c050c5SMichael Chan 	switch (dir_type) {
1631c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
1632c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1633c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1634c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
1635c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
1636c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
1637c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
163893e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
163993e0b4feSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
1640c0c050c5SMichael Chan 		return true;
1641c0c050c5SMichael Chan 	}
1642c0c050c5SMichael Chan 
1643c0c050c5SMichael Chan 	return false;
1644c0c050c5SMichael Chan }
1645c0c050c5SMichael Chan 
16465ac67d8bSRob Swindell static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
1647c0c050c5SMichael Chan {
1648c0c050c5SMichael Chan 	switch (dir_type) {
1649c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
1650c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
1651c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
1652c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
1653c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
1654c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
1655c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
1656c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1657c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1658c0c050c5SMichael Chan 		return true;
1659c0c050c5SMichael Chan 	}
1660c0c050c5SMichael Chan 
1661c0c050c5SMichael Chan 	return false;
1662c0c050c5SMichael Chan }
1663c0c050c5SMichael Chan 
1664c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
1665c0c050c5SMichael Chan {
1666c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
16675ac67d8bSRob Swindell 		bnxt_dir_type_is_other_exec_format(dir_type);
1668c0c050c5SMichael Chan }
1669c0c050c5SMichael Chan 
1670c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
1671c0c050c5SMichael Chan 					 u16 dir_type,
1672c0c050c5SMichael Chan 					 const char *filename)
1673c0c050c5SMichael Chan {
1674c0c050c5SMichael Chan 	const struct firmware  *fw;
1675c0c050c5SMichael Chan 	int			rc;
1676c0c050c5SMichael Chan 
1677c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
1678c0c050c5SMichael Chan 	if (rc != 0) {
1679c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
1680c0c050c5SMichael Chan 			   rc, filename);
1681c0c050c5SMichael Chan 		return rc;
1682c0c050c5SMichael Chan 	}
1683c0c050c5SMichael Chan 	if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1684c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
16855ac67d8bSRob Swindell 	else if (bnxt_dir_type_is_other_exec_format(dir_type) == true)
16865ac67d8bSRob Swindell 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
1687c0c050c5SMichael Chan 	else
1688c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1689c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
1690c0c050c5SMichael Chan 	release_firmware(fw);
1691c0c050c5SMichael Chan 	return rc;
1692c0c050c5SMichael Chan }
1693c0c050c5SMichael Chan 
1694c0c050c5SMichael Chan static int bnxt_flash_package_from_file(struct net_device *dev,
16955ac67d8bSRob Swindell 					char *filename, u32 install_type)
1696c0c050c5SMichael Chan {
16975ac67d8bSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
16985ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
16995ac67d8bSRob Swindell 	struct hwrm_nvm_install_update_input install = {0};
17005ac67d8bSRob Swindell 	const struct firmware *fw;
17015ac67d8bSRob Swindell 	u32 item_len;
17025ac67d8bSRob Swindell 	u16 index;
17035ac67d8bSRob Swindell 	int rc;
17045ac67d8bSRob Swindell 
17055ac67d8bSRob Swindell 	bnxt_hwrm_fw_set_time(bp);
17065ac67d8bSRob Swindell 
17075ac67d8bSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
17085ac67d8bSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
17095ac67d8bSRob Swindell 				 &index, &item_len, NULL) != 0) {
17105ac67d8bSRob Swindell 		netdev_err(dev, "PKG update area not created in nvram\n");
17115ac67d8bSRob Swindell 		return -ENOBUFS;
17125ac67d8bSRob Swindell 	}
17135ac67d8bSRob Swindell 
17145ac67d8bSRob Swindell 	rc = request_firmware(&fw, filename, &dev->dev);
17155ac67d8bSRob Swindell 	if (rc != 0) {
17165ac67d8bSRob Swindell 		netdev_err(dev, "PKG error %d requesting file: %s\n",
17175ac67d8bSRob Swindell 			   rc, filename);
17185ac67d8bSRob Swindell 		return rc;
17195ac67d8bSRob Swindell 	}
17205ac67d8bSRob Swindell 
17215ac67d8bSRob Swindell 	if (fw->size > item_len) {
17225ac67d8bSRob Swindell 		netdev_err(dev, "PKG insufficient update area in nvram: %lu",
17235ac67d8bSRob Swindell 			   (unsigned long)fw->size);
17245ac67d8bSRob Swindell 		rc = -EFBIG;
17255ac67d8bSRob Swindell 	} else {
17265ac67d8bSRob Swindell 		dma_addr_t dma_handle;
17275ac67d8bSRob Swindell 		u8 *kmem;
17285ac67d8bSRob Swindell 		struct hwrm_nvm_modify_input modify = {0};
17295ac67d8bSRob Swindell 
17305ac67d8bSRob Swindell 		bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
17315ac67d8bSRob Swindell 
17325ac67d8bSRob Swindell 		modify.dir_idx = cpu_to_le16(index);
17335ac67d8bSRob Swindell 		modify.len = cpu_to_le32(fw->size);
17345ac67d8bSRob Swindell 
17355ac67d8bSRob Swindell 		kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
17365ac67d8bSRob Swindell 					  &dma_handle, GFP_KERNEL);
17375ac67d8bSRob Swindell 		if (!kmem) {
17385ac67d8bSRob Swindell 			netdev_err(dev,
17395ac67d8bSRob Swindell 				   "dma_alloc_coherent failure, length = %u\n",
17405ac67d8bSRob Swindell 				   (unsigned int)fw->size);
17415ac67d8bSRob Swindell 			rc = -ENOMEM;
17425ac67d8bSRob Swindell 		} else {
17435ac67d8bSRob Swindell 			memcpy(kmem, fw->data, fw->size);
17445ac67d8bSRob Swindell 			modify.host_src_addr = cpu_to_le64(dma_handle);
17455ac67d8bSRob Swindell 
17465ac67d8bSRob Swindell 			rc = hwrm_send_message(bp, &modify, sizeof(modify),
17475ac67d8bSRob Swindell 					       FLASH_PACKAGE_TIMEOUT);
17485ac67d8bSRob Swindell 			dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
17495ac67d8bSRob Swindell 					  dma_handle);
17505ac67d8bSRob Swindell 		}
17515ac67d8bSRob Swindell 	}
17525ac67d8bSRob Swindell 	release_firmware(fw);
17535ac67d8bSRob Swindell 	if (rc)
17545ac67d8bSRob Swindell 		return rc;
17555ac67d8bSRob Swindell 
17565ac67d8bSRob Swindell 	if ((install_type & 0xffff) == 0)
17575ac67d8bSRob Swindell 		install_type >>= 16;
17585ac67d8bSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
17595ac67d8bSRob Swindell 	install.install_type = cpu_to_le32(install_type);
17605ac67d8bSRob Swindell 
1761cb4d1d62SKshitij Soni 	mutex_lock(&bp->hwrm_cmd_lock);
1762cb4d1d62SKshitij Soni 	rc = _hwrm_send_message(bp, &install, sizeof(install),
17635ac67d8bSRob Swindell 				INSTALL_PACKAGE_TIMEOUT);
1764cb4d1d62SKshitij Soni 	if (rc) {
1765cb4d1d62SKshitij Soni 		rc = -EOPNOTSUPP;
1766cb4d1d62SKshitij Soni 		goto flash_pkg_exit;
1767cb4d1d62SKshitij Soni 	}
1768cb4d1d62SKshitij Soni 
1769cb4d1d62SKshitij Soni 	if (resp->error_code) {
1770cb4d1d62SKshitij Soni 		u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err;
1771cb4d1d62SKshitij Soni 
1772cb4d1d62SKshitij Soni 		if (error_code == NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
1773cb4d1d62SKshitij Soni 			install.flags |= cpu_to_le16(
1774cb4d1d62SKshitij Soni 			       NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
1775cb4d1d62SKshitij Soni 			rc = _hwrm_send_message(bp, &install, sizeof(install),
1776cb4d1d62SKshitij Soni 						INSTALL_PACKAGE_TIMEOUT);
1777cb4d1d62SKshitij Soni 			if (rc) {
1778cb4d1d62SKshitij Soni 				rc = -EOPNOTSUPP;
1779cb4d1d62SKshitij Soni 				goto flash_pkg_exit;
1780cb4d1d62SKshitij Soni 			}
1781cb4d1d62SKshitij Soni 		}
1782cb4d1d62SKshitij Soni 	}
17835ac67d8bSRob Swindell 
17845ac67d8bSRob Swindell 	if (resp->result) {
17855ac67d8bSRob Swindell 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
17865ac67d8bSRob Swindell 			   (s8)resp->result, (int)resp->problem_item);
1787cb4d1d62SKshitij Soni 		rc = -ENOPKG;
17885ac67d8bSRob Swindell 	}
1789cb4d1d62SKshitij Soni flash_pkg_exit:
1790cb4d1d62SKshitij Soni 	mutex_unlock(&bp->hwrm_cmd_lock);
1791cb4d1d62SKshitij Soni 	return rc;
1792c0c050c5SMichael Chan }
1793c0c050c5SMichael Chan 
1794c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
1795c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
1796c0c050c5SMichael Chan {
1797c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1798c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
1799c0c050c5SMichael Chan 		return -EINVAL;
1800c0c050c5SMichael Chan 	}
1801c0c050c5SMichael Chan 
18025ac67d8bSRob Swindell 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
18035ac67d8bSRob Swindell 	    flash->region > 0xffff)
18045ac67d8bSRob Swindell 		return bnxt_flash_package_from_file(dev, flash->data,
18055ac67d8bSRob Swindell 						    flash->region);
1806c0c050c5SMichael Chan 
1807c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1808c0c050c5SMichael Chan }
1809c0c050c5SMichael Chan 
1810c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1811c0c050c5SMichael Chan {
1812c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1813c0c050c5SMichael Chan 	int rc;
1814c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
1815c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1816c0c050c5SMichael Chan 
1817c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1818c0c050c5SMichael Chan 
1819c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
1820c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1821c0c050c5SMichael Chan 	if (!rc) {
1822c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
1823c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
1824c0c050c5SMichael Chan 	}
1825c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
1826c0c050c5SMichael Chan 	return rc;
1827c0c050c5SMichael Chan }
1828c0c050c5SMichael Chan 
1829c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
1830c0c050c5SMichael Chan {
18314cebbacaSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
18324cebbacaSMichael Chan 
18334cebbacaSMichael Chan 	if (BNXT_VF(bp))
18344cebbacaSMichael Chan 		return 0;
18354cebbacaSMichael Chan 
1836c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
1837c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
1838c0c050c5SMichael Chan 	 */
1839c0c050c5SMichael Chan 	return -1;
1840c0c050c5SMichael Chan }
1841c0c050c5SMichael Chan 
1842c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1843c0c050c5SMichael Chan {
1844c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1845c0c050c5SMichael Chan 	int rc;
1846c0c050c5SMichael Chan 	u32 dir_entries;
1847c0c050c5SMichael Chan 	u32 entry_length;
1848c0c050c5SMichael Chan 	u8 *buf;
1849c0c050c5SMichael Chan 	size_t buflen;
1850c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1851c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
1852c0c050c5SMichael Chan 
1853c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1854c0c050c5SMichael Chan 	if (rc != 0)
1855c0c050c5SMichael Chan 		return rc;
1856c0c050c5SMichael Chan 
1857c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
1858c0c050c5SMichael Chan 	if (len < 2)
1859c0c050c5SMichael Chan 		return -EINVAL;
1860c0c050c5SMichael Chan 
1861c0c050c5SMichael Chan 	*data++ = dir_entries;
1862c0c050c5SMichael Chan 	*data++ = entry_length;
1863c0c050c5SMichael Chan 	len -= 2;
1864c0c050c5SMichael Chan 	memset(data, 0xff, len);
1865c0c050c5SMichael Chan 
1866c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
1867c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1868c0c050c5SMichael Chan 				 GFP_KERNEL);
1869c0c050c5SMichael Chan 	if (!buf) {
1870c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1871c0c050c5SMichael Chan 			   (unsigned)buflen);
1872c0c050c5SMichael Chan 		return -ENOMEM;
1873c0c050c5SMichael Chan 	}
1874c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1875c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
1876c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1877c0c050c5SMichael Chan 	if (rc == 0)
1878c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
1879c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1880c0c050c5SMichael Chan 	return rc;
1881c0c050c5SMichael Chan }
1882c0c050c5SMichael Chan 
1883c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1884c0c050c5SMichael Chan 			       u32 length, u8 *data)
1885c0c050c5SMichael Chan {
1886c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1887c0c050c5SMichael Chan 	int rc;
1888c0c050c5SMichael Chan 	u8 *buf;
1889c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1890c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
1891c0c050c5SMichael Chan 
1892e0ad8fc5SMichael Chan 	if (!length)
1893e0ad8fc5SMichael Chan 		return -EINVAL;
1894e0ad8fc5SMichael Chan 
1895c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1896c0c050c5SMichael Chan 				 GFP_KERNEL);
1897c0c050c5SMichael Chan 	if (!buf) {
1898c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1899c0c050c5SMichael Chan 			   (unsigned)length);
1900c0c050c5SMichael Chan 		return -ENOMEM;
1901c0c050c5SMichael Chan 	}
1902c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1903c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
1904c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
1905c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
1906c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
1907c0c050c5SMichael Chan 
1908c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1909c0c050c5SMichael Chan 	if (rc == 0)
1910c0c050c5SMichael Chan 		memcpy(data, buf, length);
1911c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1912c0c050c5SMichael Chan 	return rc;
1913c0c050c5SMichael Chan }
1914c0c050c5SMichael Chan 
19153ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
19163ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
19173ebf6f0aSRob Swindell 				u32 *data_length)
19183ebf6f0aSRob Swindell {
19193ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
19203ebf6f0aSRob Swindell 	int rc;
19213ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
19223ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
19233ebf6f0aSRob Swindell 
19243ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
19253ebf6f0aSRob Swindell 	req.enables = 0;
19263ebf6f0aSRob Swindell 	req.dir_idx = 0;
19273ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
19283ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
19293ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
19303ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
1931cc72f3b1SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
1932cc72f3b1SMichael Chan 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
19333ebf6f0aSRob Swindell 	if (rc == 0) {
19343ebf6f0aSRob Swindell 		if (index)
19353ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
19363ebf6f0aSRob Swindell 		if (item_length)
19373ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
19383ebf6f0aSRob Swindell 		if (data_length)
19393ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
19403ebf6f0aSRob Swindell 	}
1941cc72f3b1SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
19423ebf6f0aSRob Swindell 	return rc;
19433ebf6f0aSRob Swindell }
19443ebf6f0aSRob Swindell 
19453ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
19463ebf6f0aSRob Swindell {
19473ebf6f0aSRob Swindell 	char	*retval = NULL;
19483ebf6f0aSRob Swindell 	char	*p;
19493ebf6f0aSRob Swindell 	char	*value;
19503ebf6f0aSRob Swindell 	int	field = 0;
19513ebf6f0aSRob Swindell 
19523ebf6f0aSRob Swindell 	if (datalen < 1)
19533ebf6f0aSRob Swindell 		return NULL;
19543ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
19553ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
19563ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
19573ebf6f0aSRob Swindell 		field = 0;
19583ebf6f0aSRob Swindell 		retval = NULL;
19593ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
19603ebf6f0aSRob Swindell 			value = p;
19613ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
19623ebf6f0aSRob Swindell 				p++;
19633ebf6f0aSRob Swindell 			if (field == desired_field)
19643ebf6f0aSRob Swindell 				retval = value;
19653ebf6f0aSRob Swindell 			if (*p != '\t')
19663ebf6f0aSRob Swindell 				break;
19673ebf6f0aSRob Swindell 			*p = 0;
19683ebf6f0aSRob Swindell 			field++;
19693ebf6f0aSRob Swindell 			p++;
19703ebf6f0aSRob Swindell 		}
19713ebf6f0aSRob Swindell 		if (*p == 0)
19723ebf6f0aSRob Swindell 			break;
19733ebf6f0aSRob Swindell 		*p = 0;
19743ebf6f0aSRob Swindell 	}
19753ebf6f0aSRob Swindell 	return retval;
19763ebf6f0aSRob Swindell }
19773ebf6f0aSRob Swindell 
1978a60faa60SVasundhara Volam static void bnxt_get_pkgver(struct net_device *dev)
19793ebf6f0aSRob Swindell {
1980a60faa60SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
19813ebf6f0aSRob Swindell 	u16 index = 0;
1982a60faa60SVasundhara Volam 	char *pkgver;
1983a60faa60SVasundhara Volam 	u32 pkglen;
1984a60faa60SVasundhara Volam 	u8 *pkgbuf;
1985a60faa60SVasundhara Volam 	int len;
19863ebf6f0aSRob Swindell 
19873ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
19883ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1989a60faa60SVasundhara Volam 				 &index, NULL, &pkglen) != 0)
1990a60faa60SVasundhara Volam 		return;
19913ebf6f0aSRob Swindell 
1992a60faa60SVasundhara Volam 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
1993a60faa60SVasundhara Volam 	if (!pkgbuf) {
1994a60faa60SVasundhara Volam 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
1995a60faa60SVasundhara Volam 			pkglen);
1996a60faa60SVasundhara Volam 		return;
1997a60faa60SVasundhara Volam 	}
19983ebf6f0aSRob Swindell 
1999a60faa60SVasundhara Volam 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2000a60faa60SVasundhara Volam 		goto err;
2001a60faa60SVasundhara Volam 
2002a60faa60SVasundhara Volam 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2003a60faa60SVasundhara Volam 				   pkglen);
2004a60faa60SVasundhara Volam 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2005a60faa60SVasundhara Volam 		len = strlen(bp->fw_ver_str);
2006a60faa60SVasundhara Volam 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2007a60faa60SVasundhara Volam 			 "/pkg %s", pkgver);
2008a60faa60SVasundhara Volam 	}
2009a60faa60SVasundhara Volam err:
2010a60faa60SVasundhara Volam 	kfree(pkgbuf);
20113ebf6f0aSRob Swindell }
20123ebf6f0aSRob Swindell 
2013c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
2014c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2015c0c050c5SMichael Chan 			   u8 *data)
2016c0c050c5SMichael Chan {
2017c0c050c5SMichael Chan 	u32 index;
2018c0c050c5SMichael Chan 	u32 offset;
2019c0c050c5SMichael Chan 
2020c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
2021c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2022c0c050c5SMichael Chan 
2023c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
2024c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
2025c0c050c5SMichael Chan 
2026c0c050c5SMichael Chan 	if (index == 0) {
2027c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
2028c0c050c5SMichael Chan 		return -EINVAL;
2029c0c050c5SMichael Chan 	}
2030c0c050c5SMichael Chan 
2031c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2032c0c050c5SMichael Chan }
2033c0c050c5SMichael Chan 
2034c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2035c0c050c5SMichael Chan {
2036c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2037c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2038c0c050c5SMichael Chan 
2039c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2040c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
2041c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2042c0c050c5SMichael Chan }
2043c0c050c5SMichael Chan 
2044c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
2045c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
2046c0c050c5SMichael Chan 			   u8 *data)
2047c0c050c5SMichael Chan {
2048c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
2049c0c050c5SMichael Chan 	u8 index, dir_op;
2050c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
2051c0c050c5SMichael Chan 
2052c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
2053c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2054c0c050c5SMichael Chan 		return -EINVAL;
2055c0c050c5SMichael Chan 	}
2056c0c050c5SMichael Chan 
2057c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
2058c0c050c5SMichael Chan 
2059c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
2060c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
2061c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
2062c0c050c5SMichael Chan 		if (index == 0)
2063c0c050c5SMichael Chan 			return -EINVAL;
2064c0c050c5SMichael Chan 		switch (dir_op) {
2065c0c050c5SMichael Chan 		case 0x0e: /* erase */
2066c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
2067c0c050c5SMichael Chan 				return -EINVAL;
2068c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
2069c0c050c5SMichael Chan 		default:
2070c0c050c5SMichael Chan 			return -EINVAL;
2071c0c050c5SMichael Chan 		}
2072c0c050c5SMichael Chan 	}
2073c0c050c5SMichael Chan 
2074c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
2075c0c050c5SMichael Chan 	if (bnxt_dir_type_is_executable(type) == true)
20765ac67d8bSRob Swindell 		return -EOPNOTSUPP;
2077c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
2078c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
2079c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
2080c0c050c5SMichael Chan 
2081c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2082c0c050c5SMichael Chan 				eeprom->len);
2083c0c050c5SMichael Chan }
2084c0c050c5SMichael Chan 
208572b34f04SMichael Chan static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
208672b34f04SMichael Chan {
208772b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
208872b34f04SMichael Chan 	struct ethtool_eee *eee = &bp->eee;
208972b34f04SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
209072b34f04SMichael Chan 	u32 advertising =
209172b34f04SMichael Chan 		 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
209272b34f04SMichael Chan 	int rc = 0;
209372b34f04SMichael Chan 
2094567b2abeSSatish Baddipadige 	if (!BNXT_SINGLE_PF(bp))
209575362a3fSMichael Chan 		return -EOPNOTSUPP;
209672b34f04SMichael Chan 
209772b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
209872b34f04SMichael Chan 		return -EOPNOTSUPP;
209972b34f04SMichael Chan 
210072b34f04SMichael Chan 	if (!edata->eee_enabled)
210172b34f04SMichael Chan 		goto eee_ok;
210272b34f04SMichael Chan 
210372b34f04SMichael Chan 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
210472b34f04SMichael Chan 		netdev_warn(dev, "EEE requires autoneg\n");
210572b34f04SMichael Chan 		return -EINVAL;
210672b34f04SMichael Chan 	}
210772b34f04SMichael Chan 	if (edata->tx_lpi_enabled) {
210872b34f04SMichael Chan 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
210972b34f04SMichael Chan 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
211072b34f04SMichael Chan 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
211172b34f04SMichael Chan 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
211272b34f04SMichael Chan 			return -EINVAL;
211372b34f04SMichael Chan 		} else if (!bp->lpi_tmr_hi) {
211472b34f04SMichael Chan 			edata->tx_lpi_timer = eee->tx_lpi_timer;
211572b34f04SMichael Chan 		}
211672b34f04SMichael Chan 	}
211772b34f04SMichael Chan 	if (!edata->advertised) {
211872b34f04SMichael Chan 		edata->advertised = advertising & eee->supported;
211972b34f04SMichael Chan 	} else if (edata->advertised & ~advertising) {
212072b34f04SMichael Chan 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
212172b34f04SMichael Chan 			    edata->advertised, advertising);
212272b34f04SMichael Chan 		return -EINVAL;
212372b34f04SMichael Chan 	}
212472b34f04SMichael Chan 
212572b34f04SMichael Chan 	eee->advertised = edata->advertised;
212672b34f04SMichael Chan 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
212772b34f04SMichael Chan 	eee->tx_lpi_timer = edata->tx_lpi_timer;
212872b34f04SMichael Chan eee_ok:
212972b34f04SMichael Chan 	eee->eee_enabled = edata->eee_enabled;
213072b34f04SMichael Chan 
213172b34f04SMichael Chan 	if (netif_running(dev))
213272b34f04SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
213372b34f04SMichael Chan 
213472b34f04SMichael Chan 	return rc;
213572b34f04SMichael Chan }
213672b34f04SMichael Chan 
213772b34f04SMichael Chan static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
213872b34f04SMichael Chan {
213972b34f04SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
214072b34f04SMichael Chan 
214172b34f04SMichael Chan 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
214272b34f04SMichael Chan 		return -EOPNOTSUPP;
214372b34f04SMichael Chan 
214472b34f04SMichael Chan 	*edata = bp->eee;
214572b34f04SMichael Chan 	if (!bp->eee.eee_enabled) {
214672b34f04SMichael Chan 		/* Preserve tx_lpi_timer so that the last value will be used
214772b34f04SMichael Chan 		 * by default when it is re-enabled.
214872b34f04SMichael Chan 		 */
214972b34f04SMichael Chan 		edata->advertised = 0;
215072b34f04SMichael Chan 		edata->tx_lpi_enabled = 0;
215172b34f04SMichael Chan 	}
215272b34f04SMichael Chan 
215372b34f04SMichael Chan 	if (!bp->eee.eee_active)
215472b34f04SMichael Chan 		edata->lp_advertised = 0;
215572b34f04SMichael Chan 
215672b34f04SMichael Chan 	return 0;
215772b34f04SMichael Chan }
215872b34f04SMichael Chan 
215942ee18feSAjit Khaparde static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
216042ee18feSAjit Khaparde 					    u16 page_number, u16 start_addr,
216142ee18feSAjit Khaparde 					    u16 data_length, u8 *buf)
216242ee18feSAjit Khaparde {
216342ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_input req = {0};
216442ee18feSAjit Khaparde 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
216542ee18feSAjit Khaparde 	int rc, byte_offset = 0;
216642ee18feSAjit Khaparde 
216742ee18feSAjit Khaparde 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
216842ee18feSAjit Khaparde 	req.i2c_slave_addr = i2c_addr;
216942ee18feSAjit Khaparde 	req.page_number = cpu_to_le16(page_number);
217042ee18feSAjit Khaparde 	req.port_id = cpu_to_le16(bp->pf.port_id);
217142ee18feSAjit Khaparde 	do {
217242ee18feSAjit Khaparde 		u16 xfer_size;
217342ee18feSAjit Khaparde 
217442ee18feSAjit Khaparde 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
217542ee18feSAjit Khaparde 		data_length -= xfer_size;
217642ee18feSAjit Khaparde 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
217742ee18feSAjit Khaparde 		req.data_length = xfer_size;
217842ee18feSAjit Khaparde 		req.enables = cpu_to_le32(start_addr + byte_offset ?
217942ee18feSAjit Khaparde 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
218042ee18feSAjit Khaparde 		mutex_lock(&bp->hwrm_cmd_lock);
218142ee18feSAjit Khaparde 		rc = _hwrm_send_message(bp, &req, sizeof(req),
218242ee18feSAjit Khaparde 					HWRM_CMD_TIMEOUT);
218342ee18feSAjit Khaparde 		if (!rc)
218442ee18feSAjit Khaparde 			memcpy(buf + byte_offset, output->data, xfer_size);
218542ee18feSAjit Khaparde 		mutex_unlock(&bp->hwrm_cmd_lock);
218642ee18feSAjit Khaparde 		byte_offset += xfer_size;
218742ee18feSAjit Khaparde 	} while (!rc && data_length > 0);
218842ee18feSAjit Khaparde 
218942ee18feSAjit Khaparde 	return rc;
219042ee18feSAjit Khaparde }
219142ee18feSAjit Khaparde 
219242ee18feSAjit Khaparde static int bnxt_get_module_info(struct net_device *dev,
219342ee18feSAjit Khaparde 				struct ethtool_modinfo *modinfo)
219442ee18feSAjit Khaparde {
21957328a23cSVasundhara Volam 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
219642ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
219742ee18feSAjit Khaparde 	int rc;
219842ee18feSAjit Khaparde 
219942ee18feSAjit Khaparde 	/* No point in going further if phy status indicates
220042ee18feSAjit Khaparde 	 * module is not inserted or if it is powered down or
220142ee18feSAjit Khaparde 	 * if it is of type 10GBase-T
220242ee18feSAjit Khaparde 	 */
220342ee18feSAjit Khaparde 	if (bp->link_info.module_status >
220442ee18feSAjit Khaparde 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
220542ee18feSAjit Khaparde 		return -EOPNOTSUPP;
220642ee18feSAjit Khaparde 
220742ee18feSAjit Khaparde 	/* This feature is not supported in older firmware versions */
220842ee18feSAjit Khaparde 	if (bp->hwrm_spec_code < 0x10202)
220942ee18feSAjit Khaparde 		return -EOPNOTSUPP;
221042ee18feSAjit Khaparde 
22117328a23cSVasundhara Volam 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
22127328a23cSVasundhara Volam 					      SFF_DIAG_SUPPORT_OFFSET + 1,
22137328a23cSVasundhara Volam 					      data);
221442ee18feSAjit Khaparde 	if (!rc) {
22157328a23cSVasundhara Volam 		u8 module_id = data[0];
22167328a23cSVasundhara Volam 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
221742ee18feSAjit Khaparde 
221842ee18feSAjit Khaparde 		switch (module_id) {
221942ee18feSAjit Khaparde 		case SFF_MODULE_ID_SFP:
222042ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8472;
222142ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
22227328a23cSVasundhara Volam 			if (!diag_supported)
22237328a23cSVasundhara Volam 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
222442ee18feSAjit Khaparde 			break;
222542ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP:
222642ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP_PLUS:
222742ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8436;
222842ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
222942ee18feSAjit Khaparde 			break;
223042ee18feSAjit Khaparde 		case SFF_MODULE_ID_QSFP28:
223142ee18feSAjit Khaparde 			modinfo->type = ETH_MODULE_SFF_8636;
223242ee18feSAjit Khaparde 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
223342ee18feSAjit Khaparde 			break;
223442ee18feSAjit Khaparde 		default:
223542ee18feSAjit Khaparde 			rc = -EOPNOTSUPP;
223642ee18feSAjit Khaparde 			break;
223742ee18feSAjit Khaparde 		}
223842ee18feSAjit Khaparde 	}
223942ee18feSAjit Khaparde 	return rc;
224042ee18feSAjit Khaparde }
224142ee18feSAjit Khaparde 
224242ee18feSAjit Khaparde static int bnxt_get_module_eeprom(struct net_device *dev,
224342ee18feSAjit Khaparde 				  struct ethtool_eeprom *eeprom,
224442ee18feSAjit Khaparde 				  u8 *data)
224542ee18feSAjit Khaparde {
224642ee18feSAjit Khaparde 	struct bnxt *bp = netdev_priv(dev);
224742ee18feSAjit Khaparde 	u16  start = eeprom->offset, length = eeprom->len;
2248f3ea3119SColin Ian King 	int rc = 0;
224942ee18feSAjit Khaparde 
225042ee18feSAjit Khaparde 	memset(data, 0, eeprom->len);
225142ee18feSAjit Khaparde 
225242ee18feSAjit Khaparde 	/* Read A0 portion of the EEPROM */
225342ee18feSAjit Khaparde 	if (start < ETH_MODULE_SFF_8436_LEN) {
225442ee18feSAjit Khaparde 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
225542ee18feSAjit Khaparde 			length = ETH_MODULE_SFF_8436_LEN - start;
225642ee18feSAjit Khaparde 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
225742ee18feSAjit Khaparde 						      start, length, data);
225842ee18feSAjit Khaparde 		if (rc)
225942ee18feSAjit Khaparde 			return rc;
226042ee18feSAjit Khaparde 		start += length;
226142ee18feSAjit Khaparde 		data += length;
226242ee18feSAjit Khaparde 		length = eeprom->len - length;
226342ee18feSAjit Khaparde 	}
226442ee18feSAjit Khaparde 
226542ee18feSAjit Khaparde 	/* Read A2 portion of the EEPROM */
226642ee18feSAjit Khaparde 	if (length) {
226742ee18feSAjit Khaparde 		start -= ETH_MODULE_SFF_8436_LEN;
2268dea521a2SChristophe JAILLET 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1,
2269dea521a2SChristophe JAILLET 						      start, length, data);
227042ee18feSAjit Khaparde 	}
227142ee18feSAjit Khaparde 	return rc;
227242ee18feSAjit Khaparde }
227342ee18feSAjit Khaparde 
2274ae8e98a6SDeepak Khungar static int bnxt_nway_reset(struct net_device *dev)
2275ae8e98a6SDeepak Khungar {
2276ae8e98a6SDeepak Khungar 	int rc = 0;
2277ae8e98a6SDeepak Khungar 
2278ae8e98a6SDeepak Khungar 	struct bnxt *bp = netdev_priv(dev);
2279ae8e98a6SDeepak Khungar 	struct bnxt_link_info *link_info = &bp->link_info;
2280ae8e98a6SDeepak Khungar 
2281ae8e98a6SDeepak Khungar 	if (!BNXT_SINGLE_PF(bp))
2282ae8e98a6SDeepak Khungar 		return -EOPNOTSUPP;
2283ae8e98a6SDeepak Khungar 
2284ae8e98a6SDeepak Khungar 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2285ae8e98a6SDeepak Khungar 		return -EINVAL;
2286ae8e98a6SDeepak Khungar 
2287ae8e98a6SDeepak Khungar 	if (netif_running(dev))
2288ae8e98a6SDeepak Khungar 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
2289ae8e98a6SDeepak Khungar 
2290ae8e98a6SDeepak Khungar 	return rc;
2291ae8e98a6SDeepak Khungar }
2292ae8e98a6SDeepak Khungar 
22935ad2cbeeSMichael Chan static int bnxt_set_phys_id(struct net_device *dev,
22945ad2cbeeSMichael Chan 			    enum ethtool_phys_id_state state)
22955ad2cbeeSMichael Chan {
22965ad2cbeeSMichael Chan 	struct hwrm_port_led_cfg_input req = {0};
22975ad2cbeeSMichael Chan 	struct bnxt *bp = netdev_priv(dev);
22985ad2cbeeSMichael Chan 	struct bnxt_pf_info *pf = &bp->pf;
22995ad2cbeeSMichael Chan 	struct bnxt_led_cfg *led_cfg;
23005ad2cbeeSMichael Chan 	u8 led_state;
23015ad2cbeeSMichael Chan 	__le16 duration;
23025ad2cbeeSMichael Chan 	int i, rc;
23035ad2cbeeSMichael Chan 
23045ad2cbeeSMichael Chan 	if (!bp->num_leds || BNXT_VF(bp))
23055ad2cbeeSMichael Chan 		return -EOPNOTSUPP;
23065ad2cbeeSMichael Chan 
23075ad2cbeeSMichael Chan 	if (state == ETHTOOL_ID_ACTIVE) {
23085ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
23095ad2cbeeSMichael Chan 		duration = cpu_to_le16(500);
23105ad2cbeeSMichael Chan 	} else if (state == ETHTOOL_ID_INACTIVE) {
23115ad2cbeeSMichael Chan 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
23125ad2cbeeSMichael Chan 		duration = cpu_to_le16(0);
23135ad2cbeeSMichael Chan 	} else {
23145ad2cbeeSMichael Chan 		return -EINVAL;
23155ad2cbeeSMichael Chan 	}
23165ad2cbeeSMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
23175ad2cbeeSMichael Chan 	req.port_id = cpu_to_le16(pf->port_id);
23185ad2cbeeSMichael Chan 	req.num_leds = bp->num_leds;
23195ad2cbeeSMichael Chan 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
23205ad2cbeeSMichael Chan 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
23215ad2cbeeSMichael Chan 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
23225ad2cbeeSMichael Chan 		led_cfg->led_id = bp->leds[i].led_id;
23235ad2cbeeSMichael Chan 		led_cfg->led_state = led_state;
23245ad2cbeeSMichael Chan 		led_cfg->led_blink_on = duration;
23255ad2cbeeSMichael Chan 		led_cfg->led_blink_off = duration;
23265ad2cbeeSMichael Chan 		led_cfg->led_group_id = bp->leds[i].led_group_id;
23275ad2cbeeSMichael Chan 	}
23285ad2cbeeSMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
23295ad2cbeeSMichael Chan 	if (rc)
23305ad2cbeeSMichael Chan 		rc = -EIO;
23315ad2cbeeSMichael Chan 	return rc;
23325ad2cbeeSMichael Chan }
23335ad2cbeeSMichael Chan 
233467fea463SMichael Chan static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
233567fea463SMichael Chan {
233667fea463SMichael Chan 	struct hwrm_selftest_irq_input req = {0};
233767fea463SMichael Chan 
233867fea463SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
233967fea463SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
234067fea463SMichael Chan }
234167fea463SMichael Chan 
234267fea463SMichael Chan static int bnxt_test_irq(struct bnxt *bp)
234367fea463SMichael Chan {
234467fea463SMichael Chan 	int i;
234567fea463SMichael Chan 
234667fea463SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
234767fea463SMichael Chan 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
234867fea463SMichael Chan 		int rc;
234967fea463SMichael Chan 
235067fea463SMichael Chan 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
235167fea463SMichael Chan 		if (rc)
235267fea463SMichael Chan 			return rc;
235367fea463SMichael Chan 	}
235467fea463SMichael Chan 	return 0;
235567fea463SMichael Chan }
235667fea463SMichael Chan 
2357f7dc1ea6SMichael Chan static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
2358f7dc1ea6SMichael Chan {
2359f7dc1ea6SMichael Chan 	struct hwrm_port_mac_cfg_input req = {0};
2360f7dc1ea6SMichael Chan 
2361f7dc1ea6SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
2362f7dc1ea6SMichael Chan 
2363f7dc1ea6SMichael Chan 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
2364f7dc1ea6SMichael Chan 	if (enable)
2365f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
2366f7dc1ea6SMichael Chan 	else
2367f7dc1ea6SMichael Chan 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
2368f7dc1ea6SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2369f7dc1ea6SMichael Chan }
2370f7dc1ea6SMichael Chan 
237191725d89SMichael Chan static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
237291725d89SMichael Chan 				    struct hwrm_port_phy_cfg_input *req)
237391725d89SMichael Chan {
237491725d89SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
237591725d89SMichael Chan 	u16 fw_advertising = link_info->advertising;
237691725d89SMichael Chan 	u16 fw_speed;
237791725d89SMichael Chan 	int rc;
237891725d89SMichael Chan 
237991725d89SMichael Chan 	if (!link_info->autoneg)
238091725d89SMichael Chan 		return 0;
238191725d89SMichael Chan 
238291725d89SMichael Chan 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
238391725d89SMichael Chan 	if (netif_carrier_ok(bp->dev))
238491725d89SMichael Chan 		fw_speed = bp->link_info.link_speed;
238591725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
238691725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
238791725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
238891725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
238991725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
239091725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
239191725d89SMichael Chan 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
239291725d89SMichael Chan 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
239391725d89SMichael Chan 
239491725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(fw_speed);
239591725d89SMichael Chan 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
239691725d89SMichael Chan 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
239791725d89SMichael Chan 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
239891725d89SMichael Chan 	req->flags = 0;
239991725d89SMichael Chan 	req->force_link_speed = cpu_to_le16(0);
240091725d89SMichael Chan 	return rc;
240191725d89SMichael Chan }
240291725d89SMichael Chan 
240355fd0cf3SMichael Chan static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
240491725d89SMichael Chan {
240591725d89SMichael Chan 	struct hwrm_port_phy_cfg_input req = {0};
240691725d89SMichael Chan 
240791725d89SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
240891725d89SMichael Chan 
240991725d89SMichael Chan 	if (enable) {
241091725d89SMichael Chan 		bnxt_disable_an_for_lpbk(bp, &req);
241155fd0cf3SMichael Chan 		if (ext)
241255fd0cf3SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
241355fd0cf3SMichael Chan 		else
241491725d89SMichael Chan 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
241591725d89SMichael Chan 	} else {
241691725d89SMichael Chan 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
241791725d89SMichael Chan 	}
241891725d89SMichael Chan 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
241991725d89SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
242091725d89SMichael Chan }
242191725d89SMichael Chan 
2422f7dc1ea6SMichael Chan static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_napi *bnapi,
2423f7dc1ea6SMichael Chan 			    u32 raw_cons, int pkt_size)
2424f7dc1ea6SMichael Chan {
2425f7dc1ea6SMichael Chan 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2426f7dc1ea6SMichael Chan 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2427f7dc1ea6SMichael Chan 	struct bnxt_sw_rx_bd *rx_buf;
2428f7dc1ea6SMichael Chan 	struct rx_cmp *rxcmp;
2429f7dc1ea6SMichael Chan 	u16 cp_cons, cons;
2430f7dc1ea6SMichael Chan 	u8 *data;
2431f7dc1ea6SMichael Chan 	u32 len;
2432f7dc1ea6SMichael Chan 	int i;
2433f7dc1ea6SMichael Chan 
2434f7dc1ea6SMichael Chan 	cp_cons = RING_CMP(raw_cons);
2435f7dc1ea6SMichael Chan 	rxcmp = (struct rx_cmp *)
2436f7dc1ea6SMichael Chan 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2437f7dc1ea6SMichael Chan 	cons = rxcmp->rx_cmp_opaque;
2438f7dc1ea6SMichael Chan 	rx_buf = &rxr->rx_buf_ring[cons];
2439f7dc1ea6SMichael Chan 	data = rx_buf->data_ptr;
2440f7dc1ea6SMichael Chan 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
2441f7dc1ea6SMichael Chan 	if (len != pkt_size)
2442f7dc1ea6SMichael Chan 		return -EIO;
2443f7dc1ea6SMichael Chan 	i = ETH_ALEN;
2444f7dc1ea6SMichael Chan 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
2445f7dc1ea6SMichael Chan 		return -EIO;
2446f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2447f7dc1ea6SMichael Chan 	for (  ; i < pkt_size; i++) {
2448f7dc1ea6SMichael Chan 		if (data[i] != (u8)(i & 0xff))
2449f7dc1ea6SMichael Chan 			return -EIO;
2450f7dc1ea6SMichael Chan 	}
2451f7dc1ea6SMichael Chan 	return 0;
2452f7dc1ea6SMichael Chan }
2453f7dc1ea6SMichael Chan 
2454f7dc1ea6SMichael Chan static int bnxt_poll_loopback(struct bnxt *bp, int pkt_size)
2455f7dc1ea6SMichael Chan {
2456f7dc1ea6SMichael Chan 	struct bnxt_napi *bnapi = bp->bnapi[0];
2457f7dc1ea6SMichael Chan 	struct bnxt_cp_ring_info *cpr;
2458f7dc1ea6SMichael Chan 	struct tx_cmp *txcmp;
2459f7dc1ea6SMichael Chan 	int rc = -EIO;
2460f7dc1ea6SMichael Chan 	u32 raw_cons;
2461f7dc1ea6SMichael Chan 	u32 cons;
2462f7dc1ea6SMichael Chan 	int i;
2463f7dc1ea6SMichael Chan 
2464f7dc1ea6SMichael Chan 	cpr = &bnapi->cp_ring;
2465f7dc1ea6SMichael Chan 	raw_cons = cpr->cp_raw_cons;
2466f7dc1ea6SMichael Chan 	for (i = 0; i < 200; i++) {
2467f7dc1ea6SMichael Chan 		cons = RING_CMP(raw_cons);
2468f7dc1ea6SMichael Chan 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2469f7dc1ea6SMichael Chan 
2470f7dc1ea6SMichael Chan 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
2471f7dc1ea6SMichael Chan 			udelay(5);
2472f7dc1ea6SMichael Chan 			continue;
2473f7dc1ea6SMichael Chan 		}
2474f7dc1ea6SMichael Chan 
2475f7dc1ea6SMichael Chan 		/* The valid test of the entry must be done first before
2476f7dc1ea6SMichael Chan 		 * reading any further.
2477f7dc1ea6SMichael Chan 		 */
2478f7dc1ea6SMichael Chan 		dma_rmb();
2479f7dc1ea6SMichael Chan 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
2480f7dc1ea6SMichael Chan 			rc = bnxt_rx_loopback(bp, bnapi, raw_cons, pkt_size);
2481f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2482f7dc1ea6SMichael Chan 			raw_cons = NEXT_RAW_CMP(raw_cons);
2483f7dc1ea6SMichael Chan 			break;
2484f7dc1ea6SMichael Chan 		}
2485f7dc1ea6SMichael Chan 		raw_cons = NEXT_RAW_CMP(raw_cons);
2486f7dc1ea6SMichael Chan 	}
2487f7dc1ea6SMichael Chan 	cpr->cp_raw_cons = raw_cons;
2488f7dc1ea6SMichael Chan 	return rc;
2489f7dc1ea6SMichael Chan }
2490f7dc1ea6SMichael Chan 
2491f7dc1ea6SMichael Chan static int bnxt_run_loopback(struct bnxt *bp)
2492f7dc1ea6SMichael Chan {
2493f7dc1ea6SMichael Chan 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
2494f7dc1ea6SMichael Chan 	int pkt_size, i = 0;
2495f7dc1ea6SMichael Chan 	struct sk_buff *skb;
2496f7dc1ea6SMichael Chan 	dma_addr_t map;
2497f7dc1ea6SMichael Chan 	u8 *data;
2498f7dc1ea6SMichael Chan 	int rc;
2499f7dc1ea6SMichael Chan 
2500f7dc1ea6SMichael Chan 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
2501f7dc1ea6SMichael Chan 	skb = netdev_alloc_skb(bp->dev, pkt_size);
2502f7dc1ea6SMichael Chan 	if (!skb)
2503f7dc1ea6SMichael Chan 		return -ENOMEM;
2504f7dc1ea6SMichael Chan 	data = skb_put(skb, pkt_size);
2505f7dc1ea6SMichael Chan 	eth_broadcast_addr(data);
2506f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2507f7dc1ea6SMichael Chan 	ether_addr_copy(&data[i], bp->dev->dev_addr);
2508f7dc1ea6SMichael Chan 	i += ETH_ALEN;
2509f7dc1ea6SMichael Chan 	for ( ; i < pkt_size; i++)
2510f7dc1ea6SMichael Chan 		data[i] = (u8)(i & 0xff);
2511f7dc1ea6SMichael Chan 
2512f7dc1ea6SMichael Chan 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
2513f7dc1ea6SMichael Chan 			     PCI_DMA_TODEVICE);
2514f7dc1ea6SMichael Chan 	if (dma_mapping_error(&bp->pdev->dev, map)) {
2515f7dc1ea6SMichael Chan 		dev_kfree_skb(skb);
2516f7dc1ea6SMichael Chan 		return -EIO;
2517f7dc1ea6SMichael Chan 	}
2518f7dc1ea6SMichael Chan 	bnxt_xmit_xdp(bp, txr, map, pkt_size, 0);
2519f7dc1ea6SMichael Chan 
2520f7dc1ea6SMichael Chan 	/* Sync BD data before updating doorbell */
2521f7dc1ea6SMichael Chan 	wmb();
2522f7dc1ea6SMichael Chan 
2523434c975aSMichael Chan 	bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | txr->tx_prod);
2524f7dc1ea6SMichael Chan 	rc = bnxt_poll_loopback(bp, pkt_size);
2525f7dc1ea6SMichael Chan 
2526f7dc1ea6SMichael Chan 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
2527f7dc1ea6SMichael Chan 	dev_kfree_skb(skb);
2528f7dc1ea6SMichael Chan 	return rc;
2529f7dc1ea6SMichael Chan }
2530f7dc1ea6SMichael Chan 
2531eb513658SMichael Chan static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
2532eb513658SMichael Chan {
2533eb513658SMichael Chan 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
2534eb513658SMichael Chan 	struct hwrm_selftest_exec_input req = {0};
2535eb513658SMichael Chan 	int rc;
2536eb513658SMichael Chan 
2537eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
2538eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
2539eb513658SMichael Chan 	resp->test_success = 0;
2540eb513658SMichael Chan 	req.flags = test_mask;
2541eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
2542eb513658SMichael Chan 	*test_results = resp->test_success;
2543eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
2544eb513658SMichael Chan 	return rc;
2545eb513658SMichael Chan }
2546eb513658SMichael Chan 
254755fd0cf3SMichael Chan #define BNXT_DRV_TESTS			4
2548f7dc1ea6SMichael Chan #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
254991725d89SMichael Chan #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
255055fd0cf3SMichael Chan #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
255155fd0cf3SMichael Chan #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
2552eb513658SMichael Chan 
2553eb513658SMichael Chan static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
2554eb513658SMichael Chan 			   u64 *buf)
2555eb513658SMichael Chan {
2556eb513658SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
255755fd0cf3SMichael Chan 	bool do_ext_lpbk = false;
2558eb513658SMichael Chan 	bool offline = false;
2559eb513658SMichael Chan 	u8 test_results = 0;
2560eb513658SMichael Chan 	u8 test_mask = 0;
2561eb513658SMichael Chan 	int rc, i;
2562eb513658SMichael Chan 
2563eb513658SMichael Chan 	if (!bp->num_tests || !BNXT_SINGLE_PF(bp))
2564eb513658SMichael Chan 		return;
2565eb513658SMichael Chan 	memset(buf, 0, sizeof(u64) * bp->num_tests);
2566eb513658SMichael Chan 	if (!netif_running(dev)) {
2567eb513658SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
2568eb513658SMichael Chan 		return;
2569eb513658SMichael Chan 	}
2570eb513658SMichael Chan 
257155fd0cf3SMichael Chan 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
257255fd0cf3SMichael Chan 	    (bp->test_info->flags & BNXT_TEST_FL_EXT_LPBK))
257355fd0cf3SMichael Chan 		do_ext_lpbk = true;
257455fd0cf3SMichael Chan 
2575eb513658SMichael Chan 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
2576eb513658SMichael Chan 		if (bp->pf.active_vfs) {
2577eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2578eb513658SMichael Chan 			netdev_warn(dev, "Offline tests cannot be run with active VFs\n");
2579eb513658SMichael Chan 			return;
2580eb513658SMichael Chan 		}
2581eb513658SMichael Chan 		offline = true;
2582eb513658SMichael Chan 	}
2583eb513658SMichael Chan 
2584eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2585eb513658SMichael Chan 		u8 bit_val = 1 << i;
2586eb513658SMichael Chan 
2587eb513658SMichael Chan 		if (!(bp->test_info->offline_mask & bit_val))
2588eb513658SMichael Chan 			test_mask |= bit_val;
2589eb513658SMichael Chan 		else if (offline)
2590eb513658SMichael Chan 			test_mask |= bit_val;
2591eb513658SMichael Chan 	}
2592eb513658SMichael Chan 	if (!offline) {
2593eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
2594eb513658SMichael Chan 	} else {
2595eb513658SMichael Chan 		rc = bnxt_close_nic(bp, false, false);
2596eb513658SMichael Chan 		if (rc)
2597eb513658SMichael Chan 			return;
2598eb513658SMichael Chan 		bnxt_run_fw_tests(bp, test_mask, &test_results);
2599f7dc1ea6SMichael Chan 
2600f7dc1ea6SMichael Chan 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
2601f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, true);
2602f7dc1ea6SMichael Chan 		msleep(250);
2603f7dc1ea6SMichael Chan 		rc = bnxt_half_open_nic(bp);
2604f7dc1ea6SMichael Chan 		if (rc) {
2605f7dc1ea6SMichael Chan 			bnxt_hwrm_mac_loopback(bp, false);
2606f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2607f7dc1ea6SMichael Chan 			return;
2608f7dc1ea6SMichael Chan 		}
2609f7dc1ea6SMichael Chan 		if (bnxt_run_loopback(bp))
2610f7dc1ea6SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2611f7dc1ea6SMichael Chan 		else
2612f7dc1ea6SMichael Chan 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
2613f7dc1ea6SMichael Chan 
2614f7dc1ea6SMichael Chan 		bnxt_hwrm_mac_loopback(bp, false);
261555fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, true, false);
261691725d89SMichael Chan 		msleep(1000);
261791725d89SMichael Chan 		if (bnxt_run_loopback(bp)) {
261891725d89SMichael Chan 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
261991725d89SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
262091725d89SMichael Chan 		}
262155fd0cf3SMichael Chan 		if (do_ext_lpbk) {
262255fd0cf3SMichael Chan 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
262355fd0cf3SMichael Chan 			bnxt_hwrm_phy_loopback(bp, true, true);
262455fd0cf3SMichael Chan 			msleep(1000);
262555fd0cf3SMichael Chan 			if (bnxt_run_loopback(bp)) {
262655fd0cf3SMichael Chan 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
262755fd0cf3SMichael Chan 				etest->flags |= ETH_TEST_FL_FAILED;
262855fd0cf3SMichael Chan 			}
262955fd0cf3SMichael Chan 		}
263055fd0cf3SMichael Chan 		bnxt_hwrm_phy_loopback(bp, false, false);
263191725d89SMichael Chan 		bnxt_half_close_nic(bp);
2632eb513658SMichael Chan 		bnxt_open_nic(bp, false, true);
2633eb513658SMichael Chan 	}
263467fea463SMichael Chan 	if (bnxt_test_irq(bp)) {
263567fea463SMichael Chan 		buf[BNXT_IRQ_TEST_IDX] = 1;
263667fea463SMichael Chan 		etest->flags |= ETH_TEST_FL_FAILED;
263767fea463SMichael Chan 	}
2638eb513658SMichael Chan 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2639eb513658SMichael Chan 		u8 bit_val = 1 << i;
2640eb513658SMichael Chan 
2641eb513658SMichael Chan 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
2642eb513658SMichael Chan 			buf[i] = 1;
2643eb513658SMichael Chan 			etest->flags |= ETH_TEST_FL_FAILED;
2644eb513658SMichael Chan 		}
2645eb513658SMichael Chan 	}
2646eb513658SMichael Chan }
2647eb513658SMichael Chan 
264849f7972fSVasundhara Volam static int bnxt_reset(struct net_device *dev, u32 *flags)
264949f7972fSVasundhara Volam {
265049f7972fSVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
265149f7972fSVasundhara Volam 	int rc = 0;
265249f7972fSVasundhara Volam 
265349f7972fSVasundhara Volam 	if (!BNXT_PF(bp)) {
265449f7972fSVasundhara Volam 		netdev_err(dev, "Reset is not supported from a VF\n");
265549f7972fSVasundhara Volam 		return -EOPNOTSUPP;
265649f7972fSVasundhara Volam 	}
265749f7972fSVasundhara Volam 
265849f7972fSVasundhara Volam 	if (pci_vfs_assigned(bp->pdev)) {
265949f7972fSVasundhara Volam 		netdev_err(dev,
266049f7972fSVasundhara Volam 			   "Reset not allowed when VFs are assigned to VMs\n");
266149f7972fSVasundhara Volam 		return -EBUSY;
266249f7972fSVasundhara Volam 	}
266349f7972fSVasundhara Volam 
266449f7972fSVasundhara Volam 	if (*flags == ETH_RESET_ALL) {
266549f7972fSVasundhara Volam 		/* This feature is not supported in older firmware versions */
266649f7972fSVasundhara Volam 		if (bp->hwrm_spec_code < 0x10803)
266749f7972fSVasundhara Volam 			return -EOPNOTSUPP;
266849f7972fSVasundhara Volam 
266949f7972fSVasundhara Volam 		rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_CHIP);
26702373d8d6SScott Branden 		if (!rc) {
267149f7972fSVasundhara Volam 			netdev_info(dev, "Reset request successful. Reload driver to complete reset\n");
26722373d8d6SScott Branden 			*flags = 0;
26732373d8d6SScott Branden 		}
26746502ad59SScott Branden 	} else if (*flags == ETH_RESET_AP) {
26756502ad59SScott Branden 		/* This feature is not supported in older firmware versions */
26766502ad59SScott Branden 		if (bp->hwrm_spec_code < 0x10803)
26776502ad59SScott Branden 			return -EOPNOTSUPP;
26786502ad59SScott Branden 
26796502ad59SScott Branden 		rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_AP);
26802373d8d6SScott Branden 		if (!rc) {
26816502ad59SScott Branden 			netdev_info(dev, "Reset Application Processor request successful.\n");
26822373d8d6SScott Branden 			*flags = 0;
26832373d8d6SScott Branden 		}
268449f7972fSVasundhara Volam 	} else {
268549f7972fSVasundhara Volam 		rc = -EINVAL;
268649f7972fSVasundhara Volam 	}
268749f7972fSVasundhara Volam 
268849f7972fSVasundhara Volam 	return rc;
268949f7972fSVasundhara Volam }
269049f7972fSVasundhara Volam 
26916c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
26926c5657d0SVasundhara Volam 				  struct bnxt_hwrm_dbg_dma_info *info)
26936c5657d0SVasundhara Volam {
26946c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
26956c5657d0SVasundhara Volam 	struct hwrm_dbg_cmn_input *cmn_req = msg;
26966c5657d0SVasundhara Volam 	__le16 *seq_ptr = msg + info->seq_off;
26976c5657d0SVasundhara Volam 	u16 seq = 0, len, segs_off;
26986c5657d0SVasundhara Volam 	void *resp = cmn_resp;
26996c5657d0SVasundhara Volam 	dma_addr_t dma_handle;
27006c5657d0SVasundhara Volam 	int rc, off = 0;
27016c5657d0SVasundhara Volam 	void *dma_buf;
27026c5657d0SVasundhara Volam 
27036c5657d0SVasundhara Volam 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
27046c5657d0SVasundhara Volam 				     GFP_KERNEL);
27056c5657d0SVasundhara Volam 	if (!dma_buf)
27066c5657d0SVasundhara Volam 		return -ENOMEM;
27076c5657d0SVasundhara Volam 
27086c5657d0SVasundhara Volam 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
27096c5657d0SVasundhara Volam 			    total_segments);
27106c5657d0SVasundhara Volam 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
27116c5657d0SVasundhara Volam 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
27126c5657d0SVasundhara Volam 	mutex_lock(&bp->hwrm_cmd_lock);
27136c5657d0SVasundhara Volam 	while (1) {
27146c5657d0SVasundhara Volam 		*seq_ptr = cpu_to_le16(seq);
27156c5657d0SVasundhara Volam 		rc = _hwrm_send_message(bp, msg, msg_len, HWRM_CMD_TIMEOUT);
27166c5657d0SVasundhara Volam 		if (rc)
27176c5657d0SVasundhara Volam 			break;
27186c5657d0SVasundhara Volam 
27196c5657d0SVasundhara Volam 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
27206c5657d0SVasundhara Volam 		if (!seq &&
27216c5657d0SVasundhara Volam 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
27226c5657d0SVasundhara Volam 			info->segs = le16_to_cpu(*((__le16 *)(resp +
27236c5657d0SVasundhara Volam 							      segs_off)));
27246c5657d0SVasundhara Volam 			if (!info->segs) {
27256c5657d0SVasundhara Volam 				rc = -EIO;
27266c5657d0SVasundhara Volam 				break;
27276c5657d0SVasundhara Volam 			}
27286c5657d0SVasundhara Volam 
27296c5657d0SVasundhara Volam 			info->dest_buf_size = info->segs *
27306c5657d0SVasundhara Volam 					sizeof(struct coredump_segment_record);
27316c5657d0SVasundhara Volam 			info->dest_buf = kmalloc(info->dest_buf_size,
27326c5657d0SVasundhara Volam 						 GFP_KERNEL);
27336c5657d0SVasundhara Volam 			if (!info->dest_buf) {
27346c5657d0SVasundhara Volam 				rc = -ENOMEM;
27356c5657d0SVasundhara Volam 				break;
27366c5657d0SVasundhara Volam 			}
27376c5657d0SVasundhara Volam 		}
27386c5657d0SVasundhara Volam 
27396c5657d0SVasundhara Volam 		if (info->dest_buf)
27406c5657d0SVasundhara Volam 			memcpy(info->dest_buf + off, dma_buf, len);
27416c5657d0SVasundhara Volam 
27426c5657d0SVasundhara Volam 		if (cmn_req->req_type ==
27436c5657d0SVasundhara Volam 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
27446c5657d0SVasundhara Volam 			info->dest_buf_size += len;
27456c5657d0SVasundhara Volam 
27466c5657d0SVasundhara Volam 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
27476c5657d0SVasundhara Volam 			break;
27486c5657d0SVasundhara Volam 
27496c5657d0SVasundhara Volam 		seq++;
27506c5657d0SVasundhara Volam 		off += len;
27516c5657d0SVasundhara Volam 	}
27526c5657d0SVasundhara Volam 	mutex_unlock(&bp->hwrm_cmd_lock);
27536c5657d0SVasundhara Volam 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
27546c5657d0SVasundhara Volam 	return rc;
27556c5657d0SVasundhara Volam }
27566c5657d0SVasundhara Volam 
27576c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
27586c5657d0SVasundhara Volam 				       struct bnxt_coredump *coredump)
27596c5657d0SVasundhara Volam {
27606c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_list_input req = {0};
27616c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
27626c5657d0SVasundhara Volam 	int rc;
27636c5657d0SVasundhara Volam 
27646c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
27656c5657d0SVasundhara Volam 
27666c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_LIST_BUF_LEN;
27676c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
27686c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
27696c5657d0SVasundhara Volam 				     data_len);
27706c5657d0SVasundhara Volam 
27716c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
27726c5657d0SVasundhara Volam 	if (!rc) {
27736c5657d0SVasundhara Volam 		coredump->data = info.dest_buf;
27746c5657d0SVasundhara Volam 		coredump->data_size = info.dest_buf_size;
27756c5657d0SVasundhara Volam 		coredump->total_segs = info.segs;
27766c5657d0SVasundhara Volam 	}
27776c5657d0SVasundhara Volam 	return rc;
27786c5657d0SVasundhara Volam }
27796c5657d0SVasundhara Volam 
27806c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
27816c5657d0SVasundhara Volam 					   u16 segment_id)
27826c5657d0SVasundhara Volam {
27836c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_initiate_input req = {0};
27846c5657d0SVasundhara Volam 
27856c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
27866c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
27876c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
27886c5657d0SVasundhara Volam 
27896c5657d0SVasundhara Volam 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
27906c5657d0SVasundhara Volam }
27916c5657d0SVasundhara Volam 
27926c5657d0SVasundhara Volam static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
27936c5657d0SVasundhara Volam 					   u16 segment_id, u32 *seg_len,
27946c5657d0SVasundhara Volam 					   void *buf, u32 offset)
27956c5657d0SVasundhara Volam {
27966c5657d0SVasundhara Volam 	struct hwrm_dbg_coredump_retrieve_input req = {0};
27976c5657d0SVasundhara Volam 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
27986c5657d0SVasundhara Volam 	int rc;
27996c5657d0SVasundhara Volam 
28006c5657d0SVasundhara Volam 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
28016c5657d0SVasundhara Volam 	req.component_id = cpu_to_le16(component_id);
28026c5657d0SVasundhara Volam 	req.segment_id = cpu_to_le16(segment_id);
28036c5657d0SVasundhara Volam 
28046c5657d0SVasundhara Volam 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
28056c5657d0SVasundhara Volam 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
28066c5657d0SVasundhara Volam 				seq_no);
28076c5657d0SVasundhara Volam 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
28086c5657d0SVasundhara Volam 				     data_len);
28096c5657d0SVasundhara Volam 	if (buf)
28106c5657d0SVasundhara Volam 		info.dest_buf = buf + offset;
28116c5657d0SVasundhara Volam 
28126c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
28136c5657d0SVasundhara Volam 	if (!rc)
28146c5657d0SVasundhara Volam 		*seg_len = info.dest_buf_size;
28156c5657d0SVasundhara Volam 
28166c5657d0SVasundhara Volam 	return rc;
28176c5657d0SVasundhara Volam }
28186c5657d0SVasundhara Volam 
28196c5657d0SVasundhara Volam static void
28206c5657d0SVasundhara Volam bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
28216c5657d0SVasundhara Volam 			   struct bnxt_coredump_segment_hdr *seg_hdr,
28226c5657d0SVasundhara Volam 			   struct coredump_segment_record *seg_rec, u32 seg_len,
28236c5657d0SVasundhara Volam 			   int status, u32 duration, u32 instance)
28246c5657d0SVasundhara Volam {
28256c5657d0SVasundhara Volam 	memset(seg_hdr, 0, sizeof(*seg_hdr));
28266c5657d0SVasundhara Volam 	strcpy(seg_hdr->signature, "sEgM");
28276c5657d0SVasundhara Volam 	if (seg_rec) {
28286c5657d0SVasundhara Volam 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
28296c5657d0SVasundhara Volam 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
28306c5657d0SVasundhara Volam 		seg_hdr->low_version = seg_rec->version_low;
28316c5657d0SVasundhara Volam 		seg_hdr->high_version = seg_rec->version_hi;
28326c5657d0SVasundhara Volam 	} else {
28336c5657d0SVasundhara Volam 		/* For hwrm_ver_get response Component id = 2
28346c5657d0SVasundhara Volam 		 * and Segment id = 0
28356c5657d0SVasundhara Volam 		 */
28366c5657d0SVasundhara Volam 		seg_hdr->component_id = cpu_to_le32(2);
28376c5657d0SVasundhara Volam 		seg_hdr->segment_id = 0;
28386c5657d0SVasundhara Volam 	}
28396c5657d0SVasundhara Volam 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
28406c5657d0SVasundhara Volam 	seg_hdr->length = cpu_to_le32(seg_len);
28416c5657d0SVasundhara Volam 	seg_hdr->status = cpu_to_le32(status);
28426c5657d0SVasundhara Volam 	seg_hdr->duration = cpu_to_le32(duration);
28436c5657d0SVasundhara Volam 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
28446c5657d0SVasundhara Volam 	seg_hdr->instance = cpu_to_le32(instance);
28456c5657d0SVasundhara Volam }
28466c5657d0SVasundhara Volam 
28476c5657d0SVasundhara Volam static void
28486c5657d0SVasundhara Volam bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
28496c5657d0SVasundhara Volam 			  time64_t start, s16 start_utc, u16 total_segs,
28506c5657d0SVasundhara Volam 			  int status)
28516c5657d0SVasundhara Volam {
28526c5657d0SVasundhara Volam 	time64_t end = ktime_get_real_seconds();
28536c5657d0SVasundhara Volam 	u32 os_ver_major = 0, os_ver_minor = 0;
28546c5657d0SVasundhara Volam 	struct tm tm;
28556c5657d0SVasundhara Volam 
28566c5657d0SVasundhara Volam 	time64_to_tm(start, 0, &tm);
28576c5657d0SVasundhara Volam 	memset(record, 0, sizeof(*record));
28586c5657d0SVasundhara Volam 	strcpy(record->signature, "cOrE");
28596c5657d0SVasundhara Volam 	record->flags = 0;
28606c5657d0SVasundhara Volam 	record->low_version = 0;
28616c5657d0SVasundhara Volam 	record->high_version = 1;
28626c5657d0SVasundhara Volam 	record->asic_state = 0;
28636c5657d0SVasundhara Volam 	strncpy(record->system_name, utsname()->nodename,
28646c5657d0SVasundhara Volam 		strlen(utsname()->nodename));
28656c5657d0SVasundhara Volam 	record->year = cpu_to_le16(tm.tm_year);
28666c5657d0SVasundhara Volam 	record->month = cpu_to_le16(tm.tm_mon);
28676c5657d0SVasundhara Volam 	record->day = cpu_to_le16(tm.tm_mday);
28686c5657d0SVasundhara Volam 	record->hour = cpu_to_le16(tm.tm_hour);
28696c5657d0SVasundhara Volam 	record->minute = cpu_to_le16(tm.tm_min);
28706c5657d0SVasundhara Volam 	record->second = cpu_to_le16(tm.tm_sec);
28716c5657d0SVasundhara Volam 	record->utc_bias = cpu_to_le16(start_utc);
28726c5657d0SVasundhara Volam 	strcpy(record->commandline, "ethtool -w");
28736c5657d0SVasundhara Volam 	record->total_segments = cpu_to_le32(total_segs);
28746c5657d0SVasundhara Volam 
28756c5657d0SVasundhara Volam 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
28766c5657d0SVasundhara Volam 	record->os_ver_major = cpu_to_le32(os_ver_major);
28776c5657d0SVasundhara Volam 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
28786c5657d0SVasundhara Volam 
28796c5657d0SVasundhara Volam 	strcpy(record->os_name, utsname()->sysname);
28806c5657d0SVasundhara Volam 	time64_to_tm(end, 0, &tm);
28816c5657d0SVasundhara Volam 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
28826c5657d0SVasundhara Volam 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
28836c5657d0SVasundhara Volam 	record->end_day = cpu_to_le16(tm.tm_mday);
28846c5657d0SVasundhara Volam 	record->end_hour = cpu_to_le16(tm.tm_hour);
28856c5657d0SVasundhara Volam 	record->end_minute = cpu_to_le16(tm.tm_min);
28866c5657d0SVasundhara Volam 	record->end_second = cpu_to_le16(tm.tm_sec);
28876c5657d0SVasundhara Volam 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
28886c5657d0SVasundhara Volam 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
28896c5657d0SVasundhara Volam 				       bp->ver_resp.chip_rev << 8 |
28906c5657d0SVasundhara Volam 				       bp->ver_resp.chip_metal);
28916c5657d0SVasundhara Volam 	record->asic_id2 = 0;
28926c5657d0SVasundhara Volam 	record->coredump_status = cpu_to_le32(status);
28936c5657d0SVasundhara Volam 	record->ioctl_low_version = 0;
28946c5657d0SVasundhara Volam 	record->ioctl_high_version = 0;
28956c5657d0SVasundhara Volam }
28966c5657d0SVasundhara Volam 
28976c5657d0SVasundhara Volam static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
28986c5657d0SVasundhara Volam {
28996c5657d0SVasundhara Volam 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
29006c5657d0SVasundhara Volam 	struct coredump_segment_record *seg_record = NULL;
29016c5657d0SVasundhara Volam 	u32 offset = 0, seg_hdr_len, seg_record_len;
29026c5657d0SVasundhara Volam 	struct bnxt_coredump_segment_hdr seg_hdr;
29036c5657d0SVasundhara Volam 	struct bnxt_coredump_record coredump_rec;
29046c5657d0SVasundhara Volam 	struct bnxt_coredump coredump = {NULL};
29056c5657d0SVasundhara Volam 	time64_t start_time;
29066c5657d0SVasundhara Volam 	u16 start_utc;
29076c5657d0SVasundhara Volam 	int rc = 0, i;
29086c5657d0SVasundhara Volam 
29096c5657d0SVasundhara Volam 	start_time = ktime_get_real_seconds();
29106c5657d0SVasundhara Volam 	start_utc = sys_tz.tz_minuteswest * 60;
29116c5657d0SVasundhara Volam 	seg_hdr_len = sizeof(seg_hdr);
29126c5657d0SVasundhara Volam 
29136c5657d0SVasundhara Volam 	/* First segment should be hwrm_ver_get response */
29146c5657d0SVasundhara Volam 	*dump_len = seg_hdr_len + ver_get_resp_len;
29156c5657d0SVasundhara Volam 	if (buf) {
29166c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
29176c5657d0SVasundhara Volam 					   0, 0, 0);
29186c5657d0SVasundhara Volam 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
29196c5657d0SVasundhara Volam 		offset += seg_hdr_len;
29206c5657d0SVasundhara Volam 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
29216c5657d0SVasundhara Volam 		offset += ver_get_resp_len;
29226c5657d0SVasundhara Volam 	}
29236c5657d0SVasundhara Volam 
29246c5657d0SVasundhara Volam 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
29256c5657d0SVasundhara Volam 	if (rc) {
29266c5657d0SVasundhara Volam 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
29276c5657d0SVasundhara Volam 		goto err;
29286c5657d0SVasundhara Volam 	}
29296c5657d0SVasundhara Volam 
29306c5657d0SVasundhara Volam 	*dump_len += seg_hdr_len * coredump.total_segs;
29316c5657d0SVasundhara Volam 
29326c5657d0SVasundhara Volam 	seg_record = (struct coredump_segment_record *)coredump.data;
29336c5657d0SVasundhara Volam 	seg_record_len = sizeof(*seg_record);
29346c5657d0SVasundhara Volam 
29356c5657d0SVasundhara Volam 	for (i = 0; i < coredump.total_segs; i++) {
29366c5657d0SVasundhara Volam 		u16 comp_id = le16_to_cpu(seg_record->component_id);
29376c5657d0SVasundhara Volam 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
29386c5657d0SVasundhara Volam 		u32 duration = 0, seg_len = 0;
29396c5657d0SVasundhara Volam 		unsigned long start, end;
29406c5657d0SVasundhara Volam 
29416c5657d0SVasundhara Volam 		start = jiffies;
29426c5657d0SVasundhara Volam 
29436c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
29446c5657d0SVasundhara Volam 		if (rc) {
29456c5657d0SVasundhara Volam 			netdev_err(bp->dev,
29466c5657d0SVasundhara Volam 				   "Failed to initiate coredump for seg = %d\n",
29476c5657d0SVasundhara Volam 				   seg_record->segment_id);
29486c5657d0SVasundhara Volam 			goto next_seg;
29496c5657d0SVasundhara Volam 		}
29506c5657d0SVasundhara Volam 
29516c5657d0SVasundhara Volam 		/* Write segment data into the buffer */
29526c5657d0SVasundhara Volam 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
29536c5657d0SVasundhara Volam 						     &seg_len, buf,
29546c5657d0SVasundhara Volam 						     offset + seg_hdr_len);
29556c5657d0SVasundhara Volam 		if (rc)
29566c5657d0SVasundhara Volam 			netdev_err(bp->dev,
29576c5657d0SVasundhara Volam 				   "Failed to retrieve coredump for seg = %d\n",
29586c5657d0SVasundhara Volam 				   seg_record->segment_id);
29596c5657d0SVasundhara Volam 
29606c5657d0SVasundhara Volam next_seg:
29616c5657d0SVasundhara Volam 		end = jiffies;
29626c5657d0SVasundhara Volam 		duration = jiffies_to_msecs(end - start);
29636c5657d0SVasundhara Volam 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
29646c5657d0SVasundhara Volam 					   rc, duration, 0);
29656c5657d0SVasundhara Volam 
29666c5657d0SVasundhara Volam 		if (buf) {
29676c5657d0SVasundhara Volam 			/* Write segment header into the buffer */
29686c5657d0SVasundhara Volam 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
29696c5657d0SVasundhara Volam 			offset += seg_hdr_len + seg_len;
29706c5657d0SVasundhara Volam 		}
29716c5657d0SVasundhara Volam 
29726c5657d0SVasundhara Volam 		*dump_len += seg_len;
29736c5657d0SVasundhara Volam 		seg_record =
29746c5657d0SVasundhara Volam 			(struct coredump_segment_record *)((u8 *)seg_record +
29756c5657d0SVasundhara Volam 							   seg_record_len);
29766c5657d0SVasundhara Volam 	}
29776c5657d0SVasundhara Volam 
29786c5657d0SVasundhara Volam err:
29796c5657d0SVasundhara Volam 	if (buf) {
29806c5657d0SVasundhara Volam 		bnxt_fill_coredump_record(bp, &coredump_rec, start_time,
29816c5657d0SVasundhara Volam 					  start_utc, coredump.total_segs + 1,
29826c5657d0SVasundhara Volam 					  rc);
29836c5657d0SVasundhara Volam 		memcpy(buf + offset, &coredump_rec, sizeof(coredump_rec));
29846c5657d0SVasundhara Volam 	}
29856c5657d0SVasundhara Volam 	kfree(coredump.data);
29866c5657d0SVasundhara Volam 	*dump_len += sizeof(coredump_rec);
29876c5657d0SVasundhara Volam 
29886c5657d0SVasundhara Volam 	return rc;
29896c5657d0SVasundhara Volam }
29906c5657d0SVasundhara Volam 
29916c5657d0SVasundhara Volam static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
29926c5657d0SVasundhara Volam {
29936c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
29946c5657d0SVasundhara Volam 
29956c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
29966c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
29976c5657d0SVasundhara Volam 
29986c5657d0SVasundhara Volam 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
29996c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_min_8b << 16 |
30006c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
30016c5657d0SVasundhara Volam 			bp->ver_resp.hwrm_fw_rsvd_8b;
30026c5657d0SVasundhara Volam 
30036c5657d0SVasundhara Volam 	return bnxt_get_coredump(bp, NULL, &dump->len);
30046c5657d0SVasundhara Volam }
30056c5657d0SVasundhara Volam 
30066c5657d0SVasundhara Volam static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
30076c5657d0SVasundhara Volam 			      void *buf)
30086c5657d0SVasundhara Volam {
30096c5657d0SVasundhara Volam 	struct bnxt *bp = netdev_priv(dev);
30106c5657d0SVasundhara Volam 
30116c5657d0SVasundhara Volam 	if (bp->hwrm_spec_code < 0x10801)
30126c5657d0SVasundhara Volam 		return -EOPNOTSUPP;
30136c5657d0SVasundhara Volam 
30146c5657d0SVasundhara Volam 	memset(buf, 0, dump->len);
30156c5657d0SVasundhara Volam 
30166c5657d0SVasundhara Volam 	return bnxt_get_coredump(bp, buf, &dump->len);
30176c5657d0SVasundhara Volam }
30186c5657d0SVasundhara Volam 
3019eb513658SMichael Chan void bnxt_ethtool_init(struct bnxt *bp)
3020eb513658SMichael Chan {
3021eb513658SMichael Chan 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3022eb513658SMichael Chan 	struct hwrm_selftest_qlist_input req = {0};
3023eb513658SMichael Chan 	struct bnxt_test_info *test_info;
3024431aa1ebSMichael Chan 	struct net_device *dev = bp->dev;
3025eb513658SMichael Chan 	int i, rc;
3026eb513658SMichael Chan 
3027a60faa60SVasundhara Volam 	bnxt_get_pkgver(dev);
3028431aa1ebSMichael Chan 
3029eb513658SMichael Chan 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp))
3030eb513658SMichael Chan 		return;
3031eb513658SMichael Chan 
3032eb513658SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3033eb513658SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
3034eb513658SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3035eb513658SMichael Chan 	if (rc)
3036eb513658SMichael Chan 		goto ethtool_init_exit;
3037eb513658SMichael Chan 
3038eb513658SMichael Chan 	test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
3039eb513658SMichael Chan 	if (!test_info)
3040eb513658SMichael Chan 		goto ethtool_init_exit;
3041eb513658SMichael Chan 
3042eb513658SMichael Chan 	bp->test_info = test_info;
3043eb513658SMichael Chan 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
3044eb513658SMichael Chan 	if (bp->num_tests > BNXT_MAX_TEST)
3045eb513658SMichael Chan 		bp->num_tests = BNXT_MAX_TEST;
3046eb513658SMichael Chan 
3047eb513658SMichael Chan 	test_info->offline_mask = resp->offline_tests;
3048eb513658SMichael Chan 	test_info->timeout = le16_to_cpu(resp->test_timeout);
3049eb513658SMichael Chan 	if (!test_info->timeout)
3050eb513658SMichael Chan 		test_info->timeout = HWRM_CMD_TIMEOUT;
3051eb513658SMichael Chan 	for (i = 0; i < bp->num_tests; i++) {
3052eb513658SMichael Chan 		char *str = test_info->string[i];
3053eb513658SMichael Chan 		char *fw_str = resp->test0_name + i * 32;
3054eb513658SMichael Chan 
3055f7dc1ea6SMichael Chan 		if (i == BNXT_MACLPBK_TEST_IDX) {
3056f7dc1ea6SMichael Chan 			strcpy(str, "Mac loopback test (offline)");
305791725d89SMichael Chan 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
305891725d89SMichael Chan 			strcpy(str, "Phy loopback test (offline)");
305955fd0cf3SMichael Chan 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
306055fd0cf3SMichael Chan 			strcpy(str, "Ext loopback test (offline)");
306167fea463SMichael Chan 		} else if (i == BNXT_IRQ_TEST_IDX) {
306267fea463SMichael Chan 			strcpy(str, "Interrupt_test (offline)");
3063f7dc1ea6SMichael Chan 		} else {
3064eb513658SMichael Chan 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
3065eb513658SMichael Chan 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
3066eb513658SMichael Chan 			if (test_info->offline_mask & (1 << i))
3067eb513658SMichael Chan 				strncat(str, " (offline)",
3068eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3069eb513658SMichael Chan 			else
3070eb513658SMichael Chan 				strncat(str, " (online)",
3071eb513658SMichael Chan 					ETH_GSTRING_LEN - strlen(str));
3072eb513658SMichael Chan 		}
3073f7dc1ea6SMichael Chan 	}
3074eb513658SMichael Chan 
3075eb513658SMichael Chan ethtool_init_exit:
3076eb513658SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
3077eb513658SMichael Chan }
3078eb513658SMichael Chan 
3079eb513658SMichael Chan void bnxt_ethtool_free(struct bnxt *bp)
3080eb513658SMichael Chan {
3081eb513658SMichael Chan 	kfree(bp->test_info);
3082eb513658SMichael Chan 	bp->test_info = NULL;
3083eb513658SMichael Chan }
3084eb513658SMichael Chan 
3085c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
308600c04a92SMichael Chan 	.get_link_ksettings	= bnxt_get_link_ksettings,
308700c04a92SMichael Chan 	.set_link_ksettings	= bnxt_set_link_ksettings,
3088c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
3089c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
3090c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
30918e202366SMichael Chan 	.get_wol		= bnxt_get_wol,
30925282db6cSMichael Chan 	.set_wol		= bnxt_set_wol,
3093c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
3094c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
3095c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
3096c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
3097c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
3098c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
3099c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
3100c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
3101c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
3102c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
3103c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
3104c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
3105a011952aSMichael Chan 	.set_rxnfc		= bnxt_set_rxnfc,
3106c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
3107c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
3108c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
3109c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
3110c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
3111c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
3112c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
3113c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
311472b34f04SMichael Chan 	.get_eee		= bnxt_get_eee,
311572b34f04SMichael Chan 	.set_eee		= bnxt_set_eee,
311642ee18feSAjit Khaparde 	.get_module_info	= bnxt_get_module_info,
311742ee18feSAjit Khaparde 	.get_module_eeprom	= bnxt_get_module_eeprom,
31185ad2cbeeSMichael Chan 	.nway_reset		= bnxt_nway_reset,
31195ad2cbeeSMichael Chan 	.set_phys_id		= bnxt_set_phys_id,
3120eb513658SMichael Chan 	.self_test		= bnxt_self_test,
312149f7972fSVasundhara Volam 	.reset			= bnxt_reset,
31226c5657d0SVasundhara Volam 	.get_dump_flag		= bnxt_get_dump_flag,
31236c5657d0SVasundhara Volam 	.get_dump_data		= bnxt_get_dump_data,
3124c0c050c5SMichael Chan };
3125