1c0c050c5SMichael Chan /* Broadcom NetXtreme-C/E network driver.
2c0c050c5SMichael Chan  *
311f15ed3SMichael Chan  * Copyright (c) 2014-2016 Broadcom Corporation
4c0c050c5SMichael Chan  *
5c0c050c5SMichael Chan  * This program is free software; you can redistribute it and/or modify
6c0c050c5SMichael Chan  * it under the terms of the GNU General Public License as published by
7c0c050c5SMichael Chan  * the Free Software Foundation.
8c0c050c5SMichael Chan  */
9c0c050c5SMichael Chan 
103ebf6f0aSRob Swindell #include <linux/ctype.h>
118ddc9aaaSMichael Chan #include <linux/stringify.h>
12c0c050c5SMichael Chan #include <linux/ethtool.h>
13c0c050c5SMichael Chan #include <linux/interrupt.h>
14c0c050c5SMichael Chan #include <linux/pci.h>
15c0c050c5SMichael Chan #include <linux/etherdevice.h>
16c0c050c5SMichael Chan #include <linux/crc32.h>
17c0c050c5SMichael Chan #include <linux/firmware.h>
18c0c050c5SMichael Chan #include "bnxt_hsi.h"
19c0c050c5SMichael Chan #include "bnxt.h"
20c0c050c5SMichael Chan #include "bnxt_ethtool.h"
21c0c050c5SMichael Chan #include "bnxt_nvm_defs.h"	/* NVRAM content constant and structure defs */
22c0c050c5SMichael Chan #include "bnxt_fw_hdr.h"	/* Firmware hdr constant and structure defs */
23c0c050c5SMichael Chan #define FLASH_NVRAM_TIMEOUT	((HWRM_CMD_TIMEOUT) * 100)
24c0c050c5SMichael Chan 
253ebf6f0aSRob Swindell static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen);
263ebf6f0aSRob Swindell 
27c0c050c5SMichael Chan static u32 bnxt_get_msglevel(struct net_device *dev)
28c0c050c5SMichael Chan {
29c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
30c0c050c5SMichael Chan 
31c0c050c5SMichael Chan 	return bp->msg_enable;
32c0c050c5SMichael Chan }
33c0c050c5SMichael Chan 
34c0c050c5SMichael Chan static void bnxt_set_msglevel(struct net_device *dev, u32 value)
35c0c050c5SMichael Chan {
36c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
37c0c050c5SMichael Chan 
38c0c050c5SMichael Chan 	bp->msg_enable = value;
39c0c050c5SMichael Chan }
40c0c050c5SMichael Chan 
41c0c050c5SMichael Chan static int bnxt_get_coalesce(struct net_device *dev,
42c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
43c0c050c5SMichael Chan {
44c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
45c0c050c5SMichael Chan 
46c0c050c5SMichael Chan 	memset(coal, 0, sizeof(*coal));
47c0c050c5SMichael Chan 
48dfb5b894SMichael Chan 	coal->rx_coalesce_usecs = bp->rx_coal_ticks;
49dfb5b894SMichael Chan 	/* 2 completion records per rx packet */
50dfb5b894SMichael Chan 	coal->rx_max_coalesced_frames = bp->rx_coal_bufs / 2;
51dfb5b894SMichael Chan 	coal->rx_coalesce_usecs_irq = bp->rx_coal_ticks_irq;
52dfb5b894SMichael Chan 	coal->rx_max_coalesced_frames_irq = bp->rx_coal_bufs_irq / 2;
53c0c050c5SMichael Chan 
54dfc9c94aSMichael Chan 	coal->tx_coalesce_usecs = bp->tx_coal_ticks;
55dfc9c94aSMichael Chan 	coal->tx_max_coalesced_frames = bp->tx_coal_bufs;
56dfc9c94aSMichael Chan 	coal->tx_coalesce_usecs_irq = bp->tx_coal_ticks_irq;
57dfc9c94aSMichael Chan 	coal->tx_max_coalesced_frames_irq = bp->tx_coal_bufs_irq;
58dfc9c94aSMichael Chan 
59c0c050c5SMichael Chan 	return 0;
60c0c050c5SMichael Chan }
61c0c050c5SMichael Chan 
62c0c050c5SMichael Chan static int bnxt_set_coalesce(struct net_device *dev,
63c0c050c5SMichael Chan 			     struct ethtool_coalesce *coal)
64c0c050c5SMichael Chan {
65c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
66c0c050c5SMichael Chan 	int rc = 0;
67c0c050c5SMichael Chan 
68dfb5b894SMichael Chan 	bp->rx_coal_ticks = coal->rx_coalesce_usecs;
69dfb5b894SMichael Chan 	/* 2 completion records per rx packet */
70dfb5b894SMichael Chan 	bp->rx_coal_bufs = coal->rx_max_coalesced_frames * 2;
71dfb5b894SMichael Chan 	bp->rx_coal_ticks_irq = coal->rx_coalesce_usecs_irq;
72dfb5b894SMichael Chan 	bp->rx_coal_bufs_irq = coal->rx_max_coalesced_frames_irq * 2;
73c0c050c5SMichael Chan 
74dfc9c94aSMichael Chan 	bp->tx_coal_ticks = coal->tx_coalesce_usecs;
75dfc9c94aSMichael Chan 	bp->tx_coal_bufs = coal->tx_max_coalesced_frames;
76dfc9c94aSMichael Chan 	bp->tx_coal_ticks_irq = coal->tx_coalesce_usecs_irq;
77dfc9c94aSMichael Chan 	bp->tx_coal_bufs_irq = coal->tx_max_coalesced_frames_irq;
78dfc9c94aSMichael Chan 
79c0c050c5SMichael Chan 	if (netif_running(dev))
80c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_coal(bp);
81c0c050c5SMichael Chan 
82c0c050c5SMichael Chan 	return rc;
83c0c050c5SMichael Chan }
84c0c050c5SMichael Chan 
85c0c050c5SMichael Chan #define BNXT_NUM_STATS	21
86c0c050c5SMichael Chan 
878ddc9aaaSMichael Chan #define BNXT_RX_STATS_OFFSET(counter)	\
888ddc9aaaSMichael Chan 	(offsetof(struct rx_port_stats, counter) / 8)
898ddc9aaaSMichael Chan 
908ddc9aaaSMichael Chan #define BNXT_RX_STATS_ENTRY(counter)	\
918ddc9aaaSMichael Chan 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
928ddc9aaaSMichael Chan 
938ddc9aaaSMichael Chan #define BNXT_TX_STATS_OFFSET(counter)			\
948ddc9aaaSMichael Chan 	((offsetof(struct tx_port_stats, counter) +	\
958ddc9aaaSMichael Chan 	  sizeof(struct rx_port_stats) + 512) / 8)
968ddc9aaaSMichael Chan 
978ddc9aaaSMichael Chan #define BNXT_TX_STATS_ENTRY(counter)	\
988ddc9aaaSMichael Chan 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
998ddc9aaaSMichael Chan 
1008ddc9aaaSMichael Chan static const struct {
1018ddc9aaaSMichael Chan 	long offset;
1028ddc9aaaSMichael Chan 	char string[ETH_GSTRING_LEN];
1038ddc9aaaSMichael Chan } bnxt_port_stats_arr[] = {
1048ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
1058ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
1068ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
1078ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
1088ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
1098ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1024b_1518_frames),
1108ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
1118ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
1128ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
1138ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
1148ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
1158ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_total_frames),
1168ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
1178ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
1188ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
1198ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
1208ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
1218ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
1228ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
1238ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
1248ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
1258ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
1268ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
1278ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
1288ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
1298ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_good_frames),
1308ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
1318ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
1328ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
1338ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_bytes),
1348ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
1358ddc9aaaSMichael Chan 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
1368ddc9aaaSMichael Chan 
1378ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
1388ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
1398ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
1408ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
1418ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
1428ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1024b_1518_frames),
1438ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
1448ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_1519b_2047_frames),
1458ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
1468ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
1478ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
1488ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_good_frames),
1498ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_frames),
1508ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
1518ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
1528ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
1538ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
1548ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
1558ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
1568ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
1578ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_err),
1588ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
1598ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
1608ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
1618ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
1628ddc9aaaSMichael Chan 	BNXT_TX_STATS_ENTRY(tx_bytes),
1638ddc9aaaSMichael Chan };
1648ddc9aaaSMichael Chan 
1658ddc9aaaSMichael Chan #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
1668ddc9aaaSMichael Chan 
167c0c050c5SMichael Chan static int bnxt_get_sset_count(struct net_device *dev, int sset)
168c0c050c5SMichael Chan {
169c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
170c0c050c5SMichael Chan 
171c0c050c5SMichael Chan 	switch (sset) {
1728ddc9aaaSMichael Chan 	case ETH_SS_STATS: {
1738ddc9aaaSMichael Chan 		int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
1748ddc9aaaSMichael Chan 
1758ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS)
1768ddc9aaaSMichael Chan 			num_stats += BNXT_NUM_PORT_STATS;
1778ddc9aaaSMichael Chan 
1788ddc9aaaSMichael Chan 		return num_stats;
1798ddc9aaaSMichael Chan 	}
180c0c050c5SMichael Chan 	default:
181c0c050c5SMichael Chan 		return -EOPNOTSUPP;
182c0c050c5SMichael Chan 	}
183c0c050c5SMichael Chan }
184c0c050c5SMichael Chan 
185c0c050c5SMichael Chan static void bnxt_get_ethtool_stats(struct net_device *dev,
186c0c050c5SMichael Chan 				   struct ethtool_stats *stats, u64 *buf)
187c0c050c5SMichael Chan {
188c0c050c5SMichael Chan 	u32 i, j = 0;
189c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
190c0c050c5SMichael Chan 	u32 buf_size = sizeof(struct ctx_hw_stats) * bp->cp_nr_rings;
191c0c050c5SMichael Chan 	u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
192c0c050c5SMichael Chan 
193c0c050c5SMichael Chan 	memset(buf, 0, buf_size);
194c0c050c5SMichael Chan 
195c0c050c5SMichael Chan 	if (!bp->bnapi)
196c0c050c5SMichael Chan 		return;
197c0c050c5SMichael Chan 
198c0c050c5SMichael Chan 	for (i = 0; i < bp->cp_nr_rings; i++) {
199c0c050c5SMichael Chan 		struct bnxt_napi *bnapi = bp->bnapi[i];
200c0c050c5SMichael Chan 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
201c0c050c5SMichael Chan 		__le64 *hw_stats = (__le64 *)cpr->hw_stats;
202c0c050c5SMichael Chan 		int k;
203c0c050c5SMichael Chan 
204c0c050c5SMichael Chan 		for (k = 0; k < stat_fields; j++, k++)
205c0c050c5SMichael Chan 			buf[j] = le64_to_cpu(hw_stats[k]);
206c0c050c5SMichael Chan 		buf[j++] = cpr->rx_l4_csum_errors;
207c0c050c5SMichael Chan 	}
2088ddc9aaaSMichael Chan 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
2098ddc9aaaSMichael Chan 		__le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
2108ddc9aaaSMichael Chan 
2118ddc9aaaSMichael Chan 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
2128ddc9aaaSMichael Chan 			buf[j] = le64_to_cpu(*(port_stats +
2138ddc9aaaSMichael Chan 					       bnxt_port_stats_arr[i].offset));
2148ddc9aaaSMichael Chan 		}
2158ddc9aaaSMichael Chan 	}
216c0c050c5SMichael Chan }
217c0c050c5SMichael Chan 
218c0c050c5SMichael Chan static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
219c0c050c5SMichael Chan {
220c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
221c0c050c5SMichael Chan 	u32 i;
222c0c050c5SMichael Chan 
223c0c050c5SMichael Chan 	switch (stringset) {
224c0c050c5SMichael Chan 	/* The number of strings must match BNXT_NUM_STATS defined above. */
225c0c050c5SMichael Chan 	case ETH_SS_STATS:
226c0c050c5SMichael Chan 		for (i = 0; i < bp->cp_nr_rings; i++) {
227c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_ucast_packets", i);
228c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
229c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_mcast_packets", i);
230c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
231c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_bcast_packets", i);
232c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
233c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_discards", i);
234c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
235c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_drops", i);
236c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
237c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_ucast_bytes", i);
238c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
239c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_mcast_bytes", i);
240c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
241c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_bcast_bytes", i);
242c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
243c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_ucast_packets", i);
244c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
245c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_mcast_packets", i);
246c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
247c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_bcast_packets", i);
248c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
249c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_discards", i);
250c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
251c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_drops", i);
252c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
253c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_ucast_bytes", i);
254c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
255c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_mcast_bytes", i);
256c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
257c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tx_bcast_bytes", i);
258c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
259c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_packets", i);
260c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
261c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_bytes", i);
262c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
263c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_events", i);
264c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
265c0c050c5SMichael Chan 			sprintf(buf, "[%d]: tpa_aborts", i);
266c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
267c0c050c5SMichael Chan 			sprintf(buf, "[%d]: rx_l4_csum_errors", i);
268c0c050c5SMichael Chan 			buf += ETH_GSTRING_LEN;
269c0c050c5SMichael Chan 		}
2708ddc9aaaSMichael Chan 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
2718ddc9aaaSMichael Chan 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
2728ddc9aaaSMichael Chan 				strcpy(buf, bnxt_port_stats_arr[i].string);
2738ddc9aaaSMichael Chan 				buf += ETH_GSTRING_LEN;
2748ddc9aaaSMichael Chan 			}
2758ddc9aaaSMichael Chan 		}
276c0c050c5SMichael Chan 		break;
277c0c050c5SMichael Chan 	default:
278c0c050c5SMichael Chan 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
279c0c050c5SMichael Chan 			   stringset);
280c0c050c5SMichael Chan 		break;
281c0c050c5SMichael Chan 	}
282c0c050c5SMichael Chan }
283c0c050c5SMichael Chan 
284c0c050c5SMichael Chan static void bnxt_get_ringparam(struct net_device *dev,
285c0c050c5SMichael Chan 			       struct ethtool_ringparam *ering)
286c0c050c5SMichael Chan {
287c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
288c0c050c5SMichael Chan 
289c0c050c5SMichael Chan 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
290c0c050c5SMichael Chan 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
291c0c050c5SMichael Chan 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
292c0c050c5SMichael Chan 
293c0c050c5SMichael Chan 	ering->rx_pending = bp->rx_ring_size;
294c0c050c5SMichael Chan 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
295c0c050c5SMichael Chan 	ering->tx_pending = bp->tx_ring_size;
296c0c050c5SMichael Chan }
297c0c050c5SMichael Chan 
298c0c050c5SMichael Chan static int bnxt_set_ringparam(struct net_device *dev,
299c0c050c5SMichael Chan 			      struct ethtool_ringparam *ering)
300c0c050c5SMichael Chan {
301c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
302c0c050c5SMichael Chan 
303c0c050c5SMichael Chan 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
304c0c050c5SMichael Chan 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
305c0c050c5SMichael Chan 	    (ering->tx_pending <= MAX_SKB_FRAGS))
306c0c050c5SMichael Chan 		return -EINVAL;
307c0c050c5SMichael Chan 
308c0c050c5SMichael Chan 	if (netif_running(dev))
309c0c050c5SMichael Chan 		bnxt_close_nic(bp, false, false);
310c0c050c5SMichael Chan 
311c0c050c5SMichael Chan 	bp->rx_ring_size = ering->rx_pending;
312c0c050c5SMichael Chan 	bp->tx_ring_size = ering->tx_pending;
313c0c050c5SMichael Chan 	bnxt_set_ring_params(bp);
314c0c050c5SMichael Chan 
315c0c050c5SMichael Chan 	if (netif_running(dev))
316c0c050c5SMichael Chan 		return bnxt_open_nic(bp, false, false);
317c0c050c5SMichael Chan 
318c0c050c5SMichael Chan 	return 0;
319c0c050c5SMichael Chan }
320c0c050c5SMichael Chan 
321c0c050c5SMichael Chan static void bnxt_get_channels(struct net_device *dev,
322c0c050c5SMichael Chan 			      struct ethtool_channels *channel)
323c0c050c5SMichael Chan {
324c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
325c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
326c0c050c5SMichael Chan 
3276e6c5a57SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
328068c9ec6SMichael Chan 	channel->max_combined = max_rx_rings;
329068c9ec6SMichael Chan 
330068c9ec6SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false);
331c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
332c0c050c5SMichael Chan 	if (tcs > 1)
333c0c050c5SMichael Chan 		max_tx_rings /= tcs;
334c0c050c5SMichael Chan 
335c0c050c5SMichael Chan 	channel->max_rx = max_rx_rings;
336c0c050c5SMichael Chan 	channel->max_tx = max_tx_rings;
337c0c050c5SMichael Chan 	channel->max_other = 0;
338068c9ec6SMichael Chan 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
339068c9ec6SMichael Chan 		channel->combined_count = bp->rx_nr_rings;
340068c9ec6SMichael Chan 	} else {
341c0c050c5SMichael Chan 		channel->rx_count = bp->rx_nr_rings;
342c0c050c5SMichael Chan 		channel->tx_count = bp->tx_nr_rings_per_tc;
343c0c050c5SMichael Chan 	}
344068c9ec6SMichael Chan }
345c0c050c5SMichael Chan 
346c0c050c5SMichael Chan static int bnxt_set_channels(struct net_device *dev,
347c0c050c5SMichael Chan 			     struct ethtool_channels *channel)
348c0c050c5SMichael Chan {
349c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
350c0c050c5SMichael Chan 	int max_rx_rings, max_tx_rings, tcs;
351c0c050c5SMichael Chan 	u32 rc = 0;
352068c9ec6SMichael Chan 	bool sh = false;
353c0c050c5SMichael Chan 
354068c9ec6SMichael Chan 	if (channel->other_count)
355c0c050c5SMichael Chan 		return -EINVAL;
356c0c050c5SMichael Chan 
357068c9ec6SMichael Chan 	if (!channel->combined_count &&
358068c9ec6SMichael Chan 	    (!channel->rx_count || !channel->tx_count))
359068c9ec6SMichael Chan 		return -EINVAL;
360068c9ec6SMichael Chan 
361068c9ec6SMichael Chan 	if (channel->combined_count &&
362068c9ec6SMichael Chan 	    (channel->rx_count || channel->tx_count))
363068c9ec6SMichael Chan 		return -EINVAL;
364068c9ec6SMichael Chan 
365068c9ec6SMichael Chan 	if (channel->combined_count)
366068c9ec6SMichael Chan 		sh = true;
367068c9ec6SMichael Chan 
368068c9ec6SMichael Chan 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
369068c9ec6SMichael Chan 
370c0c050c5SMichael Chan 	tcs = netdev_get_num_tc(dev);
371c0c050c5SMichael Chan 	if (tcs > 1)
372c0c050c5SMichael Chan 		max_tx_rings /= tcs;
373c0c050c5SMichael Chan 
374068c9ec6SMichael Chan 	if (sh && (channel->combined_count > max_rx_rings ||
375068c9ec6SMichael Chan 		   channel->combined_count > max_tx_rings))
376068c9ec6SMichael Chan 		return -ENOMEM;
377068c9ec6SMichael Chan 
378068c9ec6SMichael Chan 	if (!sh && (channel->rx_count > max_rx_rings ||
379068c9ec6SMichael Chan 		    channel->tx_count > max_tx_rings))
380068c9ec6SMichael Chan 		return -ENOMEM;
381c0c050c5SMichael Chan 
382c0c050c5SMichael Chan 	if (netif_running(dev)) {
383c0c050c5SMichael Chan 		if (BNXT_PF(bp)) {
384c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
385c0c050c5SMichael Chan 			 * before PF unload
386c0c050c5SMichael Chan 			 */
387c0c050c5SMichael Chan 		}
388c0c050c5SMichael Chan 		rc = bnxt_close_nic(bp, true, false);
389c0c050c5SMichael Chan 		if (rc) {
390c0c050c5SMichael Chan 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
391c0c050c5SMichael Chan 				   rc);
392c0c050c5SMichael Chan 			return rc;
393c0c050c5SMichael Chan 		}
394c0c050c5SMichael Chan 	}
395c0c050c5SMichael Chan 
396068c9ec6SMichael Chan 	if (sh) {
397068c9ec6SMichael Chan 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
398068c9ec6SMichael Chan 		bp->rx_nr_rings = channel->combined_count;
399068c9ec6SMichael Chan 		bp->tx_nr_rings_per_tc = channel->combined_count;
400068c9ec6SMichael Chan 	} else {
401068c9ec6SMichael Chan 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
402c0c050c5SMichael Chan 		bp->rx_nr_rings = channel->rx_count;
403c0c050c5SMichael Chan 		bp->tx_nr_rings_per_tc = channel->tx_count;
404068c9ec6SMichael Chan 	}
405068c9ec6SMichael Chan 
406c0c050c5SMichael Chan 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
407c0c050c5SMichael Chan 	if (tcs > 1)
408c0c050c5SMichael Chan 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
409068c9ec6SMichael Chan 
410068c9ec6SMichael Chan 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
411068c9ec6SMichael Chan 			       bp->tx_nr_rings + bp->rx_nr_rings;
412068c9ec6SMichael Chan 
413c0c050c5SMichael Chan 	bp->num_stat_ctxs = bp->cp_nr_rings;
414c0c050c5SMichael Chan 
4152bcfa6f6SMichael Chan 	/* After changing number of rx channels, update NTUPLE feature. */
4162bcfa6f6SMichael Chan 	netdev_update_features(dev);
417c0c050c5SMichael Chan 	if (netif_running(dev)) {
418c0c050c5SMichael Chan 		rc = bnxt_open_nic(bp, true, false);
419c0c050c5SMichael Chan 		if ((!rc) && BNXT_PF(bp)) {
420c0c050c5SMichael Chan 			/* TODO CHIMP_FW: Send message to all VF's
421c0c050c5SMichael Chan 			 * to renable
422c0c050c5SMichael Chan 			 */
423c0c050c5SMichael Chan 		}
424c0c050c5SMichael Chan 	}
425c0c050c5SMichael Chan 
426c0c050c5SMichael Chan 	return rc;
427c0c050c5SMichael Chan }
428c0c050c5SMichael Chan 
429c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
430c0c050c5SMichael Chan static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
431c0c050c5SMichael Chan 			    u32 *rule_locs)
432c0c050c5SMichael Chan {
433c0c050c5SMichael Chan 	int i, j = 0;
434c0c050c5SMichael Chan 
435c0c050c5SMichael Chan 	cmd->data = bp->ntp_fltr_count;
436c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
437c0c050c5SMichael Chan 		struct hlist_head *head;
438c0c050c5SMichael Chan 		struct bnxt_ntuple_filter *fltr;
439c0c050c5SMichael Chan 
440c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
441c0c050c5SMichael Chan 		rcu_read_lock();
442c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
443c0c050c5SMichael Chan 			if (j == cmd->rule_cnt)
444c0c050c5SMichael Chan 				break;
445c0c050c5SMichael Chan 			rule_locs[j++] = fltr->sw_id;
446c0c050c5SMichael Chan 		}
447c0c050c5SMichael Chan 		rcu_read_unlock();
448c0c050c5SMichael Chan 		if (j == cmd->rule_cnt)
449c0c050c5SMichael Chan 			break;
450c0c050c5SMichael Chan 	}
451c0c050c5SMichael Chan 	cmd->rule_cnt = j;
452c0c050c5SMichael Chan 	return 0;
453c0c050c5SMichael Chan }
454c0c050c5SMichael Chan 
455c0c050c5SMichael Chan static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
456c0c050c5SMichael Chan {
457c0c050c5SMichael Chan 	struct ethtool_rx_flow_spec *fs =
458c0c050c5SMichael Chan 		(struct ethtool_rx_flow_spec *)&cmd->fs;
459c0c050c5SMichael Chan 	struct bnxt_ntuple_filter *fltr;
460c0c050c5SMichael Chan 	struct flow_keys *fkeys;
461c0c050c5SMichael Chan 	int i, rc = -EINVAL;
462c0c050c5SMichael Chan 
463c0c050c5SMichael Chan 	if (fs->location < 0 || fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
464c0c050c5SMichael Chan 		return rc;
465c0c050c5SMichael Chan 
466c0c050c5SMichael Chan 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
467c0c050c5SMichael Chan 		struct hlist_head *head;
468c0c050c5SMichael Chan 
469c0c050c5SMichael Chan 		head = &bp->ntp_fltr_hash_tbl[i];
470c0c050c5SMichael Chan 		rcu_read_lock();
471c0c050c5SMichael Chan 		hlist_for_each_entry_rcu(fltr, head, hash) {
472c0c050c5SMichael Chan 			if (fltr->sw_id == fs->location)
473c0c050c5SMichael Chan 				goto fltr_found;
474c0c050c5SMichael Chan 		}
475c0c050c5SMichael Chan 		rcu_read_unlock();
476c0c050c5SMichael Chan 	}
477c0c050c5SMichael Chan 	return rc;
478c0c050c5SMichael Chan 
479c0c050c5SMichael Chan fltr_found:
480c0c050c5SMichael Chan 	fkeys = &fltr->fkeys;
481c0c050c5SMichael Chan 	if (fkeys->basic.ip_proto == IPPROTO_TCP)
482c0c050c5SMichael Chan 		fs->flow_type = TCP_V4_FLOW;
483c0c050c5SMichael Chan 	else if (fkeys->basic.ip_proto == IPPROTO_UDP)
484c0c050c5SMichael Chan 		fs->flow_type = UDP_V4_FLOW;
485c0c050c5SMichael Chan 	else
486c0c050c5SMichael Chan 		goto fltr_err;
487c0c050c5SMichael Chan 
488c0c050c5SMichael Chan 	fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
489c0c050c5SMichael Chan 	fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
490c0c050c5SMichael Chan 
491c0c050c5SMichael Chan 	fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
492c0c050c5SMichael Chan 	fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
493c0c050c5SMichael Chan 
494c0c050c5SMichael Chan 	fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
495c0c050c5SMichael Chan 	fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
496c0c050c5SMichael Chan 
497c0c050c5SMichael Chan 	fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
498c0c050c5SMichael Chan 	fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
499c0c050c5SMichael Chan 
500c0c050c5SMichael Chan 	fs->ring_cookie = fltr->rxq;
501c0c050c5SMichael Chan 	rc = 0;
502c0c050c5SMichael Chan 
503c0c050c5SMichael Chan fltr_err:
504c0c050c5SMichael Chan 	rcu_read_unlock();
505c0c050c5SMichael Chan 
506c0c050c5SMichael Chan 	return rc;
507c0c050c5SMichael Chan }
508c0c050c5SMichael Chan 
509c0c050c5SMichael Chan static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
510c0c050c5SMichael Chan 			  u32 *rule_locs)
511c0c050c5SMichael Chan {
512c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
513c0c050c5SMichael Chan 	int rc = 0;
514c0c050c5SMichael Chan 
515c0c050c5SMichael Chan 	switch (cmd->cmd) {
516c0c050c5SMichael Chan 	case ETHTOOL_GRXRINGS:
517c0c050c5SMichael Chan 		cmd->data = bp->rx_nr_rings;
518c0c050c5SMichael Chan 		break;
519c0c050c5SMichael Chan 
520c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLCNT:
521c0c050c5SMichael Chan 		cmd->rule_cnt = bp->ntp_fltr_count;
522c0c050c5SMichael Chan 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
523c0c050c5SMichael Chan 		break;
524c0c050c5SMichael Chan 
525c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRLALL:
526c0c050c5SMichael Chan 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
527c0c050c5SMichael Chan 		break;
528c0c050c5SMichael Chan 
529c0c050c5SMichael Chan 	case ETHTOOL_GRXCLSRULE:
530c0c050c5SMichael Chan 		rc = bnxt_grxclsrule(bp, cmd);
531c0c050c5SMichael Chan 		break;
532c0c050c5SMichael Chan 
533c0c050c5SMichael Chan 	default:
534c0c050c5SMichael Chan 		rc = -EOPNOTSUPP;
535c0c050c5SMichael Chan 		break;
536c0c050c5SMichael Chan 	}
537c0c050c5SMichael Chan 
538c0c050c5SMichael Chan 	return rc;
539c0c050c5SMichael Chan }
540c0c050c5SMichael Chan #endif
541c0c050c5SMichael Chan 
542c0c050c5SMichael Chan static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
543c0c050c5SMichael Chan {
544c0c050c5SMichael Chan 	return HW_HASH_INDEX_SIZE;
545c0c050c5SMichael Chan }
546c0c050c5SMichael Chan 
547c0c050c5SMichael Chan static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
548c0c050c5SMichael Chan {
549c0c050c5SMichael Chan 	return HW_HASH_KEY_SIZE;
550c0c050c5SMichael Chan }
551c0c050c5SMichael Chan 
552c0c050c5SMichael Chan static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
553c0c050c5SMichael Chan 			 u8 *hfunc)
554c0c050c5SMichael Chan {
555c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
556c0c050c5SMichael Chan 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
557c0c050c5SMichael Chan 	int i = 0;
558c0c050c5SMichael Chan 
559c0c050c5SMichael Chan 	if (hfunc)
560c0c050c5SMichael Chan 		*hfunc = ETH_RSS_HASH_TOP;
561c0c050c5SMichael Chan 
562c0c050c5SMichael Chan 	if (indir)
563c0c050c5SMichael Chan 		for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
564c0c050c5SMichael Chan 			indir[i] = le16_to_cpu(vnic->rss_table[i]);
565c0c050c5SMichael Chan 
566c0c050c5SMichael Chan 	if (key)
567c0c050c5SMichael Chan 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
568c0c050c5SMichael Chan 
569c0c050c5SMichael Chan 	return 0;
570c0c050c5SMichael Chan }
571c0c050c5SMichael Chan 
572c0c050c5SMichael Chan static void bnxt_get_drvinfo(struct net_device *dev,
573c0c050c5SMichael Chan 			     struct ethtool_drvinfo *info)
574c0c050c5SMichael Chan {
575c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
5763ebf6f0aSRob Swindell 	char *pkglog;
5773ebf6f0aSRob Swindell 	char *pkgver = NULL;
578c0c050c5SMichael Chan 
5793ebf6f0aSRob Swindell 	pkglog = kmalloc(BNX_PKG_LOG_MAX_LENGTH, GFP_KERNEL);
5803ebf6f0aSRob Swindell 	if (pkglog)
5813ebf6f0aSRob Swindell 		pkgver = bnxt_get_pkgver(dev, pkglog, BNX_PKG_LOG_MAX_LENGTH);
582c0c050c5SMichael Chan 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
583c0c050c5SMichael Chan 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
5843ebf6f0aSRob Swindell 	if (pkgver && *pkgver != 0 && isdigit(*pkgver))
5853ebf6f0aSRob Swindell 		snprintf(info->fw_version, sizeof(info->fw_version) - 1,
5863ebf6f0aSRob Swindell 			 "%s pkg %s", bp->fw_ver_str, pkgver);
5873ebf6f0aSRob Swindell 	else
5883ebf6f0aSRob Swindell 		strlcpy(info->fw_version, bp->fw_ver_str,
5893ebf6f0aSRob Swindell 			sizeof(info->fw_version));
590c0c050c5SMichael Chan 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
591c0c050c5SMichael Chan 	info->n_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
592c0c050c5SMichael Chan 	info->testinfo_len = BNXT_NUM_TESTS(bp);
593c0c050c5SMichael Chan 	/* TODO CHIMP_FW: eeprom dump details */
594c0c050c5SMichael Chan 	info->eedump_len = 0;
595c0c050c5SMichael Chan 	/* TODO CHIMP FW: reg dump details */
596c0c050c5SMichael Chan 	info->regdump_len = 0;
5973ebf6f0aSRob Swindell 	kfree(pkglog);
598c0c050c5SMichael Chan }
599c0c050c5SMichael Chan 
60027c4d578SMichael Chan static u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
601c0c050c5SMichael Chan {
602c0c050c5SMichael Chan 	u32 speed_mask = 0;
603c0c050c5SMichael Chan 
604c0c050c5SMichael Chan 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
605c0c050c5SMichael Chan 	/* set the advertised speeds */
606c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
607c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_100baseT_Full;
608c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
609c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_1000baseT_Full;
610c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
611c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_2500baseX_Full;
612c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
613c0c050c5SMichael Chan 		speed_mask |= ADVERTISED_10000baseT_Full;
614c0c050c5SMichael Chan 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
6151c49c421SMichael Chan 		speed_mask |= ADVERTISED_40000baseCR4_Full;
61627c4d578SMichael Chan 
61727c4d578SMichael Chan 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
61827c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause;
61927c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
62027c4d578SMichael Chan 		speed_mask |= ADVERTISED_Asym_Pause;
62127c4d578SMichael Chan 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
62227c4d578SMichael Chan 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
62327c4d578SMichael Chan 
624c0c050c5SMichael Chan 	return speed_mask;
625c0c050c5SMichael Chan }
626c0c050c5SMichael Chan 
62727c4d578SMichael Chan static u32 bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info)
62827c4d578SMichael Chan {
62927c4d578SMichael Chan 	u16 fw_speeds = link_info->auto_link_speeds;
63027c4d578SMichael Chan 	u8 fw_pause = 0;
63127c4d578SMichael Chan 
63227c4d578SMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
63327c4d578SMichael Chan 		fw_pause = link_info->auto_pause_setting;
63427c4d578SMichael Chan 
63527c4d578SMichael Chan 	return _bnxt_fw_to_ethtool_adv_spds(fw_speeds, fw_pause);
63627c4d578SMichael Chan }
63727c4d578SMichael Chan 
6383277360eSMichael Chan static u32 bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info)
6393277360eSMichael Chan {
6403277360eSMichael Chan 	u16 fw_speeds = link_info->lp_auto_link_speeds;
6413277360eSMichael Chan 	u8 fw_pause = 0;
6423277360eSMichael Chan 
6433277360eSMichael Chan 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6443277360eSMichael Chan 		fw_pause = link_info->lp_pause;
6453277360eSMichael Chan 
6463277360eSMichael Chan 	return _bnxt_fw_to_ethtool_adv_spds(fw_speeds, fw_pause);
6473277360eSMichael Chan }
6483277360eSMichael Chan 
6494b32caccSMichael Chan static u32 bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info)
6504b32caccSMichael Chan {
6514b32caccSMichael Chan 	u16 fw_speeds = link_info->support_speeds;
6524b32caccSMichael Chan 	u32 supported;
6534b32caccSMichael Chan 
6544b32caccSMichael Chan 	supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6554b32caccSMichael Chan 	return supported | SUPPORTED_Pause | SUPPORTED_Asym_Pause;
6564b32caccSMichael Chan }
6574b32caccSMichael Chan 
658c0c050c5SMichael Chan u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
659c0c050c5SMichael Chan {
660c0c050c5SMichael Chan 	switch (fw_link_speed) {
661c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_100MB:
662c0c050c5SMichael Chan 		return SPEED_100;
663c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_1GB:
664c0c050c5SMichael Chan 		return SPEED_1000;
665c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_2_5GB:
666c0c050c5SMichael Chan 		return SPEED_2500;
667c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_10GB:
668c0c050c5SMichael Chan 		return SPEED_10000;
669c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_20GB:
670c0c050c5SMichael Chan 		return SPEED_20000;
671c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_25GB:
672c0c050c5SMichael Chan 		return SPEED_25000;
673c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_40GB:
674c0c050c5SMichael Chan 		return SPEED_40000;
675c0c050c5SMichael Chan 	case BNXT_LINK_SPEED_50GB:
676c0c050c5SMichael Chan 		return SPEED_50000;
677c0c050c5SMichael Chan 	default:
678c0c050c5SMichael Chan 		return SPEED_UNKNOWN;
679c0c050c5SMichael Chan 	}
680c0c050c5SMichael Chan }
681c0c050c5SMichael Chan 
682c0c050c5SMichael Chan static int bnxt_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
683c0c050c5SMichael Chan {
684c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
685c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
686c0c050c5SMichael Chan 	u16 ethtool_speed;
687c0c050c5SMichael Chan 
688c0c050c5SMichael Chan 	cmd->supported = bnxt_fw_to_ethtool_support_spds(link_info);
689c0c050c5SMichael Chan 
690c0c050c5SMichael Chan 	if (link_info->auto_link_speeds)
691c0c050c5SMichael Chan 		cmd->supported |= SUPPORTED_Autoneg;
692c0c050c5SMichael Chan 
693b763499eSMichael Chan 	if (link_info->autoneg) {
694c0c050c5SMichael Chan 		cmd->advertising =
695c0c050c5SMichael Chan 			bnxt_fw_to_ethtool_advertised_spds(link_info);
696c0c050c5SMichael Chan 		cmd->advertising |= ADVERTISED_Autoneg;
697c0c050c5SMichael Chan 		cmd->autoneg = AUTONEG_ENABLE;
6983277360eSMichael Chan 		if (link_info->phy_link_status == BNXT_LINK_LINK)
6993277360eSMichael Chan 			cmd->lp_advertising =
7003277360eSMichael Chan 				bnxt_fw_to_ethtool_lp_adv(link_info);
701c0c050c5SMichael Chan 	} else {
702c0c050c5SMichael Chan 		cmd->autoneg = AUTONEG_DISABLE;
703c0c050c5SMichael Chan 		cmd->advertising = 0;
704c0c050c5SMichael Chan 	}
705c0c050c5SMichael Chan 
706c0c050c5SMichael Chan 	cmd->port = PORT_NONE;
707c0c050c5SMichael Chan 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
708c0c050c5SMichael Chan 		cmd->port = PORT_TP;
709c0c050c5SMichael Chan 		cmd->supported |= SUPPORTED_TP;
710c0c050c5SMichael Chan 		cmd->advertising |= ADVERTISED_TP;
711c0c050c5SMichael Chan 	} else {
712c0c050c5SMichael Chan 		cmd->supported |= SUPPORTED_FIBRE;
713c0c050c5SMichael Chan 		cmd->advertising |= ADVERTISED_FIBRE;
714c0c050c5SMichael Chan 
715c0c050c5SMichael Chan 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
716c0c050c5SMichael Chan 			cmd->port = PORT_DA;
717c0c050c5SMichael Chan 		else if (link_info->media_type ==
718c0c050c5SMichael Chan 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
719c0c050c5SMichael Chan 			cmd->port = PORT_FIBRE;
720c0c050c5SMichael Chan 	}
721c0c050c5SMichael Chan 
722c0c050c5SMichael Chan 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
723c0c050c5SMichael Chan 		if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
724c0c050c5SMichael Chan 			cmd->duplex = DUPLEX_FULL;
725c0c050c5SMichael Chan 	} else {
726c0c050c5SMichael Chan 		cmd->duplex = DUPLEX_UNKNOWN;
727c0c050c5SMichael Chan 	}
728c0c050c5SMichael Chan 	ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
729c0c050c5SMichael Chan 	ethtool_cmd_speed_set(cmd, ethtool_speed);
730c0c050c5SMichael Chan 	if (link_info->transceiver ==
73111f15ed3SMichael Chan 	    PORT_PHY_QCFG_RESP_XCVR_PKG_TYPE_XCVR_INTERNAL)
732c0c050c5SMichael Chan 		cmd->transceiver = XCVR_INTERNAL;
733c0c050c5SMichael Chan 	else
734c0c050c5SMichael Chan 		cmd->transceiver = XCVR_EXTERNAL;
735c0c050c5SMichael Chan 	cmd->phy_address = link_info->phy_addr;
736c0c050c5SMichael Chan 
737c0c050c5SMichael Chan 	return 0;
738c0c050c5SMichael Chan }
739c0c050c5SMichael Chan 
740c0c050c5SMichael Chan static u32 bnxt_get_fw_speed(struct net_device *dev, u16 ethtool_speed)
741c0c050c5SMichael Chan {
742c0c050c5SMichael Chan 	switch (ethtool_speed) {
743c0c050c5SMichael Chan 	case SPEED_100:
744c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
745c0c050c5SMichael Chan 	case SPEED_1000:
746c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
747c0c050c5SMichael Chan 	case SPEED_2500:
748c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
749c0c050c5SMichael Chan 	case SPEED_10000:
750c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
751c0c050c5SMichael Chan 	case SPEED_20000:
752c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
753c0c050c5SMichael Chan 	case SPEED_25000:
754c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
755c0c050c5SMichael Chan 	case SPEED_40000:
756c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
757c0c050c5SMichael Chan 	case SPEED_50000:
758c0c050c5SMichael Chan 		return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
759c0c050c5SMichael Chan 	default:
760c0c050c5SMichael Chan 		netdev_err(dev, "unsupported speed!\n");
761c0c050c5SMichael Chan 		break;
762c0c050c5SMichael Chan 	}
763c0c050c5SMichael Chan 	return 0;
764c0c050c5SMichael Chan }
765c0c050c5SMichael Chan 
766c0c050c5SMichael Chan static u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
767c0c050c5SMichael Chan {
768c0c050c5SMichael Chan 	u16 fw_speed_mask = 0;
769c0c050c5SMichael Chan 
770c0c050c5SMichael Chan 	/* only support autoneg at speed 100, 1000, and 10000 */
771c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_100baseT_Full |
772c0c050c5SMichael Chan 			   ADVERTISED_100baseT_Half)) {
773c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
774c0c050c5SMichael Chan 	}
775c0c050c5SMichael Chan 	if (advertising & (ADVERTISED_1000baseT_Full |
776c0c050c5SMichael Chan 			   ADVERTISED_1000baseT_Half)) {
777c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
778c0c050c5SMichael Chan 	}
779c0c050c5SMichael Chan 	if (advertising & ADVERTISED_10000baseT_Full)
780c0c050c5SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
781c0c050c5SMichael Chan 
7821c49c421SMichael Chan 	if (advertising & ADVERTISED_40000baseCR4_Full)
7831c49c421SMichael Chan 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
7841c49c421SMichael Chan 
785c0c050c5SMichael Chan 	return fw_speed_mask;
786c0c050c5SMichael Chan }
787c0c050c5SMichael Chan 
788c0c050c5SMichael Chan static int bnxt_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
789c0c050c5SMichael Chan {
790c0c050c5SMichael Chan 	int rc = 0;
791c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
792c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
793c0c050c5SMichael Chan 	u32 speed, fw_advertising = 0;
794c0c050c5SMichael Chan 	bool set_pause = false;
795c0c050c5SMichael Chan 
796c0c050c5SMichael Chan 	if (BNXT_VF(bp))
797c0c050c5SMichael Chan 		return rc;
798c0c050c5SMichael Chan 
799c0c050c5SMichael Chan 	if (cmd->autoneg == AUTONEG_ENABLE) {
800f1a082a6SMichael Chan 		u32 supported_spds = bnxt_fw_to_ethtool_support_spds(link_info);
801f1a082a6SMichael Chan 
802f1a082a6SMichael Chan 		if (cmd->advertising & ~(supported_spds | ADVERTISED_Autoneg |
803f1a082a6SMichael Chan 					 ADVERTISED_TP | ADVERTISED_FIBRE)) {
804c0c050c5SMichael Chan 			netdev_err(dev, "Unsupported advertising mask (adv: 0x%x)\n",
805c0c050c5SMichael Chan 				   cmd->advertising);
806c0c050c5SMichael Chan 			rc = -EINVAL;
807c0c050c5SMichael Chan 			goto set_setting_exit;
808c0c050c5SMichael Chan 		}
809c0c050c5SMichael Chan 		fw_advertising = bnxt_get_fw_auto_link_speeds(cmd->advertising);
810c0c050c5SMichael Chan 		if (fw_advertising & ~link_info->support_speeds) {
811c0c050c5SMichael Chan 			netdev_err(dev, "Advertising parameters are not supported! (adv: 0x%x)\n",
812c0c050c5SMichael Chan 				   cmd->advertising);
813c0c050c5SMichael Chan 			rc = -EINVAL;
814c0c050c5SMichael Chan 			goto set_setting_exit;
815c0c050c5SMichael Chan 		}
816c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
817c0c050c5SMichael Chan 		if (!fw_advertising)
818c0c050c5SMichael Chan 			link_info->advertising = link_info->support_speeds;
819c0c050c5SMichael Chan 		else
820c0c050c5SMichael Chan 			link_info->advertising = fw_advertising;
821c0c050c5SMichael Chan 		/* any change to autoneg will cause link change, therefore the
822c0c050c5SMichael Chan 		 * driver should put back the original pause setting in autoneg
823c0c050c5SMichael Chan 		 */
824c0c050c5SMichael Chan 		set_pause = true;
825c0c050c5SMichael Chan 	} else {
826c0c050c5SMichael Chan 		/* TODO: currently don't support half duplex */
827c0c050c5SMichael Chan 		if (cmd->duplex == DUPLEX_HALF) {
828c0c050c5SMichael Chan 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
829c0c050c5SMichael Chan 			rc = -EINVAL;
830c0c050c5SMichael Chan 			goto set_setting_exit;
831c0c050c5SMichael Chan 		}
832c0c050c5SMichael Chan 		/* If received a request for an unknown duplex, assume full*/
833c0c050c5SMichael Chan 		if (cmd->duplex == DUPLEX_UNKNOWN)
834c0c050c5SMichael Chan 			cmd->duplex = DUPLEX_FULL;
835c0c050c5SMichael Chan 		speed = ethtool_cmd_speed(cmd);
836c0c050c5SMichael Chan 		link_info->req_link_speed = bnxt_get_fw_speed(dev, speed);
837c0c050c5SMichael Chan 		link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
838b763499eSMichael Chan 		link_info->autoneg = 0;
839c0c050c5SMichael Chan 		link_info->advertising = 0;
840c0c050c5SMichael Chan 	}
841c0c050c5SMichael Chan 
842c0c050c5SMichael Chan 	if (netif_running(dev))
843c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_link_setting(bp, set_pause);
844c0c050c5SMichael Chan 
845c0c050c5SMichael Chan set_setting_exit:
846c0c050c5SMichael Chan 	return rc;
847c0c050c5SMichael Chan }
848c0c050c5SMichael Chan 
849c0c050c5SMichael Chan static void bnxt_get_pauseparam(struct net_device *dev,
850c0c050c5SMichael Chan 				struct ethtool_pauseparam *epause)
851c0c050c5SMichael Chan {
852c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
853c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
854c0c050c5SMichael Chan 
855c0c050c5SMichael Chan 	if (BNXT_VF(bp))
856c0c050c5SMichael Chan 		return;
857b763499eSMichael Chan 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
8583c02d1bbSMichael Chan 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
8593c02d1bbSMichael Chan 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
860c0c050c5SMichael Chan }
861c0c050c5SMichael Chan 
862c0c050c5SMichael Chan static int bnxt_set_pauseparam(struct net_device *dev,
863c0c050c5SMichael Chan 			       struct ethtool_pauseparam *epause)
864c0c050c5SMichael Chan {
865c0c050c5SMichael Chan 	int rc = 0;
866c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
867c0c050c5SMichael Chan 	struct bnxt_link_info *link_info = &bp->link_info;
868c0c050c5SMichael Chan 
869c0c050c5SMichael Chan 	if (BNXT_VF(bp))
870c0c050c5SMichael Chan 		return rc;
871c0c050c5SMichael Chan 
872c0c050c5SMichael Chan 	if (epause->autoneg) {
873b763499eSMichael Chan 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
874b763499eSMichael Chan 			return -EINVAL;
875b763499eSMichael Chan 
876c0c050c5SMichael Chan 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
877c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_BOTH;
878c0c050c5SMichael Chan 	} else {
879c0c050c5SMichael Chan 		/* when transition from auto pause to force pause,
880c0c050c5SMichael Chan 		 * force a link change
881c0c050c5SMichael Chan 		 */
882c0c050c5SMichael Chan 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
883c0c050c5SMichael Chan 			link_info->force_link_chng = true;
884c0c050c5SMichael Chan 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
885c0c050c5SMichael Chan 		link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_BOTH;
886c0c050c5SMichael Chan 	}
887c0c050c5SMichael Chan 	if (epause->rx_pause)
888c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
889c0c050c5SMichael Chan 	else
890c0c050c5SMichael Chan 		link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_RX;
891c0c050c5SMichael Chan 
892c0c050c5SMichael Chan 	if (epause->tx_pause)
893c0c050c5SMichael Chan 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
894c0c050c5SMichael Chan 	else
895c0c050c5SMichael Chan 		link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_TX;
896c0c050c5SMichael Chan 
897c0c050c5SMichael Chan 	if (netif_running(dev))
898c0c050c5SMichael Chan 		rc = bnxt_hwrm_set_pause(bp);
899c0c050c5SMichael Chan 	return rc;
900c0c050c5SMichael Chan }
901c0c050c5SMichael Chan 
902c0c050c5SMichael Chan static u32 bnxt_get_link(struct net_device *dev)
903c0c050c5SMichael Chan {
904c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
905c0c050c5SMichael Chan 
906c0c050c5SMichael Chan 	/* TODO: handle MF, VF, driver close case */
907c0c050c5SMichael Chan 	return bp->link_info.link_up;
908c0c050c5SMichael Chan }
909c0c050c5SMichael Chan 
910c0c050c5SMichael Chan static int bnxt_flash_nvram(struct net_device *dev,
911c0c050c5SMichael Chan 			    u16 dir_type,
912c0c050c5SMichael Chan 			    u16 dir_ordinal,
913c0c050c5SMichael Chan 			    u16 dir_ext,
914c0c050c5SMichael Chan 			    u16 dir_attr,
915c0c050c5SMichael Chan 			    const u8 *data,
916c0c050c5SMichael Chan 			    size_t data_len)
917c0c050c5SMichael Chan {
918c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
919c0c050c5SMichael Chan 	int rc;
920c0c050c5SMichael Chan 	struct hwrm_nvm_write_input req = {0};
921c0c050c5SMichael Chan 	dma_addr_t dma_handle;
922c0c050c5SMichael Chan 	u8 *kmem;
923c0c050c5SMichael Chan 
924c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
925c0c050c5SMichael Chan 
926c0c050c5SMichael Chan 	req.dir_type = cpu_to_le16(dir_type);
927c0c050c5SMichael Chan 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
928c0c050c5SMichael Chan 	req.dir_ext = cpu_to_le16(dir_ext);
929c0c050c5SMichael Chan 	req.dir_attr = cpu_to_le16(dir_attr);
930c0c050c5SMichael Chan 	req.dir_data_length = cpu_to_le32(data_len);
931c0c050c5SMichael Chan 
932c0c050c5SMichael Chan 	kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
933c0c050c5SMichael Chan 				  GFP_KERNEL);
934c0c050c5SMichael Chan 	if (!kmem) {
935c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
936c0c050c5SMichael Chan 			   (unsigned)data_len);
937c0c050c5SMichael Chan 		return -ENOMEM;
938c0c050c5SMichael Chan 	}
939c0c050c5SMichael Chan 	memcpy(kmem, data, data_len);
940c0c050c5SMichael Chan 	req.host_src_addr = cpu_to_le64(dma_handle);
941c0c050c5SMichael Chan 
942c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
943c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
944c0c050c5SMichael Chan 
945c0c050c5SMichael Chan 	return rc;
946c0c050c5SMichael Chan }
947c0c050c5SMichael Chan 
948d2d6318cSRob Swindell static int bnxt_firmware_reset(struct net_device *dev,
949d2d6318cSRob Swindell 			       u16 dir_type)
950d2d6318cSRob Swindell {
951d2d6318cSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
952d2d6318cSRob Swindell 	struct hwrm_fw_reset_input req = {0};
953d2d6318cSRob Swindell 
954d2d6318cSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
955d2d6318cSRob Swindell 
956d2d6318cSRob Swindell 	/* TODO: Support ASAP ChiMP self-reset (e.g. upon PF driver unload) */
957d2d6318cSRob Swindell 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
958d2d6318cSRob Swindell 	/*       (e.g. when firmware isn't already running) */
959d2d6318cSRob Swindell 	switch (dir_type) {
960d2d6318cSRob Swindell 	case BNX_DIR_TYPE_CHIMP_PATCH:
961d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE:
962d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BOOTCODE_2:
963d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
964d2d6318cSRob Swindell 		/* Self-reset ChiMP upon next PCIe reset: */
965d2d6318cSRob Swindell 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
966d2d6318cSRob Swindell 		break;
967d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
968d2d6318cSRob Swindell 	case BNX_DIR_TYPE_APE_PATCH:
969d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
970d2d6318cSRob Swindell 		break;
971d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_FW:
972d2d6318cSRob Swindell 	case BNX_DIR_TYPE_KONG_PATCH:
973d2d6318cSRob Swindell 		req.embedded_proc_type =
974d2d6318cSRob Swindell 			FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
975d2d6318cSRob Swindell 		break;
976d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_FW:
977d2d6318cSRob Swindell 	case BNX_DIR_TYPE_BONO_PATCH:
978d2d6318cSRob Swindell 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
979d2d6318cSRob Swindell 		break;
980d2d6318cSRob Swindell 	default:
981d2d6318cSRob Swindell 		return -EINVAL;
982d2d6318cSRob Swindell 	}
983d2d6318cSRob Swindell 
984d2d6318cSRob Swindell 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
985d2d6318cSRob Swindell }
986d2d6318cSRob Swindell 
987c0c050c5SMichael Chan static int bnxt_flash_firmware(struct net_device *dev,
988c0c050c5SMichael Chan 			       u16 dir_type,
989c0c050c5SMichael Chan 			       const u8 *fw_data,
990c0c050c5SMichael Chan 			       size_t fw_size)
991c0c050c5SMichael Chan {
992c0c050c5SMichael Chan 	int	rc = 0;
993c0c050c5SMichael Chan 	u16	code_type;
994c0c050c5SMichael Chan 	u32	stored_crc;
995c0c050c5SMichael Chan 	u32	calculated_crc;
996c0c050c5SMichael Chan 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
997c0c050c5SMichael Chan 
998c0c050c5SMichael Chan 	switch (dir_type) {
999c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1000c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1001c0c050c5SMichael Chan 		code_type = CODE_BOOT;
1002c0c050c5SMichael Chan 		break;
10032731d70fSRob Swindell 	case BNX_DIR_TYPE_APE_FW:
10042731d70fSRob Swindell 		code_type = CODE_MCTP_PASSTHRU;
10052731d70fSRob Swindell 		break;
1006c0c050c5SMichael Chan 	default:
1007c0c050c5SMichael Chan 		netdev_err(dev, "Unsupported directory entry type: %u\n",
1008c0c050c5SMichael Chan 			   dir_type);
1009c0c050c5SMichael Chan 		return -EINVAL;
1010c0c050c5SMichael Chan 	}
1011c0c050c5SMichael Chan 	if (fw_size < sizeof(struct bnxt_fw_header)) {
1012c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware file size: %u\n",
1013c0c050c5SMichael Chan 			   (unsigned int)fw_size);
1014c0c050c5SMichael Chan 		return -EINVAL;
1015c0c050c5SMichael Chan 	}
1016c0c050c5SMichael Chan 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1017c0c050c5SMichael Chan 		netdev_err(dev, "Invalid firmware signature: %08X\n",
1018c0c050c5SMichael Chan 			   le32_to_cpu(header->signature));
1019c0c050c5SMichael Chan 		return -EINVAL;
1020c0c050c5SMichael Chan 	}
1021c0c050c5SMichael Chan 	if (header->code_type != code_type) {
1022c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1023c0c050c5SMichael Chan 			   code_type, header->code_type);
1024c0c050c5SMichael Chan 		return -EINVAL;
1025c0c050c5SMichael Chan 	}
1026c0c050c5SMichael Chan 	if (header->device != DEVICE_CUMULUS_FAMILY) {
1027c0c050c5SMichael Chan 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1028c0c050c5SMichael Chan 			   DEVICE_CUMULUS_FAMILY, header->device);
1029c0c050c5SMichael Chan 		return -EINVAL;
1030c0c050c5SMichael Chan 	}
1031c0c050c5SMichael Chan 	/* Confirm the CRC32 checksum of the file: */
1032c0c050c5SMichael Chan 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1033c0c050c5SMichael Chan 					     sizeof(stored_crc)));
1034c0c050c5SMichael Chan 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1035c0c050c5SMichael Chan 	if (calculated_crc != stored_crc) {
1036c0c050c5SMichael Chan 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1037c0c050c5SMichael Chan 			   (unsigned long)stored_crc,
1038c0c050c5SMichael Chan 			   (unsigned long)calculated_crc);
1039c0c050c5SMichael Chan 		return -EINVAL;
1040c0c050c5SMichael Chan 	}
1041c0c050c5SMichael Chan 	/* TODO: Validate digital signature (RSA-encrypted SHA-256 hash) here */
1042c0c050c5SMichael Chan 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1043c0c050c5SMichael Chan 			      0, 0, fw_data, fw_size);
1044d2d6318cSRob Swindell 	if (rc == 0)	/* Firmware update successful */
1045d2d6318cSRob Swindell 		rc = bnxt_firmware_reset(dev, dir_type);
1046d2d6318cSRob Swindell 
1047c0c050c5SMichael Chan 	return rc;
1048c0c050c5SMichael Chan }
1049c0c050c5SMichael Chan 
1050c0c050c5SMichael Chan static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1051c0c050c5SMichael Chan {
1052c0c050c5SMichael Chan 	switch (dir_type) {
1053c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CHIMP_PATCH:
1054c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE:
1055c0c050c5SMichael Chan 	case BNX_DIR_TYPE_BOOTCODE_2:
1056c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_FW:
1057c0c050c5SMichael Chan 	case BNX_DIR_TYPE_APE_PATCH:
1058c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_FW:
1059c0c050c5SMichael Chan 	case BNX_DIR_TYPE_KONG_PATCH:
1060c0c050c5SMichael Chan 		return true;
1061c0c050c5SMichael Chan 	}
1062c0c050c5SMichael Chan 
1063c0c050c5SMichael Chan 	return false;
1064c0c050c5SMichael Chan }
1065c0c050c5SMichael Chan 
1066c0c050c5SMichael Chan static bool bnxt_dir_type_is_unprotected_exec_format(u16 dir_type)
1067c0c050c5SMichael Chan {
1068c0c050c5SMichael Chan 	switch (dir_type) {
1069c0c050c5SMichael Chan 	case BNX_DIR_TYPE_AVS:
1070c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXP_ROM_MBA:
1071c0c050c5SMichael Chan 	case BNX_DIR_TYPE_PCIE:
1072c0c050c5SMichael Chan 	case BNX_DIR_TYPE_TSCF_UCODE:
1073c0c050c5SMichael Chan 	case BNX_DIR_TYPE_EXT_PHY:
1074c0c050c5SMichael Chan 	case BNX_DIR_TYPE_CCM:
1075c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT:
1076c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1077c0c050c5SMichael Chan 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1078c0c050c5SMichael Chan 		return true;
1079c0c050c5SMichael Chan 	}
1080c0c050c5SMichael Chan 
1081c0c050c5SMichael Chan 	return false;
1082c0c050c5SMichael Chan }
1083c0c050c5SMichael Chan 
1084c0c050c5SMichael Chan static bool bnxt_dir_type_is_executable(u16 dir_type)
1085c0c050c5SMichael Chan {
1086c0c050c5SMichael Chan 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
1087c0c050c5SMichael Chan 		bnxt_dir_type_is_unprotected_exec_format(dir_type);
1088c0c050c5SMichael Chan }
1089c0c050c5SMichael Chan 
1090c0c050c5SMichael Chan static int bnxt_flash_firmware_from_file(struct net_device *dev,
1091c0c050c5SMichael Chan 					 u16 dir_type,
1092c0c050c5SMichael Chan 					 const char *filename)
1093c0c050c5SMichael Chan {
1094c0c050c5SMichael Chan 	const struct firmware  *fw;
1095c0c050c5SMichael Chan 	int			rc;
1096c0c050c5SMichael Chan 
1097c0c050c5SMichael Chan 	if (bnxt_dir_type_is_executable(dir_type) == false)
1098c0c050c5SMichael Chan 		return -EINVAL;
1099c0c050c5SMichael Chan 
1100c0c050c5SMichael Chan 	rc = request_firmware(&fw, filename, &dev->dev);
1101c0c050c5SMichael Chan 	if (rc != 0) {
1102c0c050c5SMichael Chan 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
1103c0c050c5SMichael Chan 			   rc, filename);
1104c0c050c5SMichael Chan 		return rc;
1105c0c050c5SMichael Chan 	}
1106c0c050c5SMichael Chan 	if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1107c0c050c5SMichael Chan 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1108c0c050c5SMichael Chan 	else
1109c0c050c5SMichael Chan 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1110c0c050c5SMichael Chan 				      0, 0, fw->data, fw->size);
1111c0c050c5SMichael Chan 	release_firmware(fw);
1112c0c050c5SMichael Chan 	return rc;
1113c0c050c5SMichael Chan }
1114c0c050c5SMichael Chan 
1115c0c050c5SMichael Chan static int bnxt_flash_package_from_file(struct net_device *dev,
1116c0c050c5SMichael Chan 					char *filename)
1117c0c050c5SMichael Chan {
1118c0c050c5SMichael Chan 	netdev_err(dev, "packages are not yet supported\n");
1119c0c050c5SMichael Chan 	return -EINVAL;
1120c0c050c5SMichael Chan }
1121c0c050c5SMichael Chan 
1122c0c050c5SMichael Chan static int bnxt_flash_device(struct net_device *dev,
1123c0c050c5SMichael Chan 			     struct ethtool_flash *flash)
1124c0c050c5SMichael Chan {
1125c0c050c5SMichael Chan 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1126c0c050c5SMichael Chan 		netdev_err(dev, "flashdev not supported from a virtual function\n");
1127c0c050c5SMichael Chan 		return -EINVAL;
1128c0c050c5SMichael Chan 	}
1129c0c050c5SMichael Chan 
1130c0c050c5SMichael Chan 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS)
1131c0c050c5SMichael Chan 		return bnxt_flash_package_from_file(dev, flash->data);
1132c0c050c5SMichael Chan 
1133c0c050c5SMichael Chan 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1134c0c050c5SMichael Chan }
1135c0c050c5SMichael Chan 
1136c0c050c5SMichael Chan static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1137c0c050c5SMichael Chan {
1138c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1139c0c050c5SMichael Chan 	int rc;
1140c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_input req = {0};
1141c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1142c0c050c5SMichael Chan 
1143c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1144c0c050c5SMichael Chan 
1145c0c050c5SMichael Chan 	mutex_lock(&bp->hwrm_cmd_lock);
1146c0c050c5SMichael Chan 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1147c0c050c5SMichael Chan 	if (!rc) {
1148c0c050c5SMichael Chan 		*entries = le32_to_cpu(output->entries);
1149c0c050c5SMichael Chan 		*length = le32_to_cpu(output->entry_length);
1150c0c050c5SMichael Chan 	}
1151c0c050c5SMichael Chan 	mutex_unlock(&bp->hwrm_cmd_lock);
1152c0c050c5SMichael Chan 	return rc;
1153c0c050c5SMichael Chan }
1154c0c050c5SMichael Chan 
1155c0c050c5SMichael Chan static int bnxt_get_eeprom_len(struct net_device *dev)
1156c0c050c5SMichael Chan {
1157c0c050c5SMichael Chan 	/* The -1 return value allows the entire 32-bit range of offsets to be
1158c0c050c5SMichael Chan 	 * passed via the ethtool command-line utility.
1159c0c050c5SMichael Chan 	 */
1160c0c050c5SMichael Chan 	return -1;
1161c0c050c5SMichael Chan }
1162c0c050c5SMichael Chan 
1163c0c050c5SMichael Chan static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1164c0c050c5SMichael Chan {
1165c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1166c0c050c5SMichael Chan 	int rc;
1167c0c050c5SMichael Chan 	u32 dir_entries;
1168c0c050c5SMichael Chan 	u32 entry_length;
1169c0c050c5SMichael Chan 	u8 *buf;
1170c0c050c5SMichael Chan 	size_t buflen;
1171c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1172c0c050c5SMichael Chan 	struct hwrm_nvm_get_dir_entries_input req = {0};
1173c0c050c5SMichael Chan 
1174c0c050c5SMichael Chan 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1175c0c050c5SMichael Chan 	if (rc != 0)
1176c0c050c5SMichael Chan 		return rc;
1177c0c050c5SMichael Chan 
1178c0c050c5SMichael Chan 	/* Insert 2 bytes of directory info (count and size of entries) */
1179c0c050c5SMichael Chan 	if (len < 2)
1180c0c050c5SMichael Chan 		return -EINVAL;
1181c0c050c5SMichael Chan 
1182c0c050c5SMichael Chan 	*data++ = dir_entries;
1183c0c050c5SMichael Chan 	*data++ = entry_length;
1184c0c050c5SMichael Chan 	len -= 2;
1185c0c050c5SMichael Chan 	memset(data, 0xff, len);
1186c0c050c5SMichael Chan 
1187c0c050c5SMichael Chan 	buflen = dir_entries * entry_length;
1188c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1189c0c050c5SMichael Chan 				 GFP_KERNEL);
1190c0c050c5SMichael Chan 	if (!buf) {
1191c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1192c0c050c5SMichael Chan 			   (unsigned)buflen);
1193c0c050c5SMichael Chan 		return -ENOMEM;
1194c0c050c5SMichael Chan 	}
1195c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1196c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
1197c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1198c0c050c5SMichael Chan 	if (rc == 0)
1199c0c050c5SMichael Chan 		memcpy(data, buf, len > buflen ? buflen : len);
1200c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1201c0c050c5SMichael Chan 	return rc;
1202c0c050c5SMichael Chan }
1203c0c050c5SMichael Chan 
1204c0c050c5SMichael Chan static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1205c0c050c5SMichael Chan 			       u32 length, u8 *data)
1206c0c050c5SMichael Chan {
1207c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1208c0c050c5SMichael Chan 	int rc;
1209c0c050c5SMichael Chan 	u8 *buf;
1210c0c050c5SMichael Chan 	dma_addr_t dma_handle;
1211c0c050c5SMichael Chan 	struct hwrm_nvm_read_input req = {0};
1212c0c050c5SMichael Chan 
1213c0c050c5SMichael Chan 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1214c0c050c5SMichael Chan 				 GFP_KERNEL);
1215c0c050c5SMichael Chan 	if (!buf) {
1216c0c050c5SMichael Chan 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1217c0c050c5SMichael Chan 			   (unsigned)length);
1218c0c050c5SMichael Chan 		return -ENOMEM;
1219c0c050c5SMichael Chan 	}
1220c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1221c0c050c5SMichael Chan 	req.host_dest_addr = cpu_to_le64(dma_handle);
1222c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
1223c0c050c5SMichael Chan 	req.offset = cpu_to_le32(offset);
1224c0c050c5SMichael Chan 	req.len = cpu_to_le32(length);
1225c0c050c5SMichael Chan 
1226c0c050c5SMichael Chan 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1227c0c050c5SMichael Chan 	if (rc == 0)
1228c0c050c5SMichael Chan 		memcpy(data, buf, length);
1229c0c050c5SMichael Chan 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1230c0c050c5SMichael Chan 	return rc;
1231c0c050c5SMichael Chan }
1232c0c050c5SMichael Chan 
12333ebf6f0aSRob Swindell static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
12343ebf6f0aSRob Swindell 				u16 ext, u16 *index, u32 *item_length,
12353ebf6f0aSRob Swindell 				u32 *data_length)
12363ebf6f0aSRob Swindell {
12373ebf6f0aSRob Swindell 	struct bnxt *bp = netdev_priv(dev);
12383ebf6f0aSRob Swindell 	int rc;
12393ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_input req = {0};
12403ebf6f0aSRob Swindell 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
12413ebf6f0aSRob Swindell 
12423ebf6f0aSRob Swindell 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
12433ebf6f0aSRob Swindell 	req.enables = 0;
12443ebf6f0aSRob Swindell 	req.dir_idx = 0;
12453ebf6f0aSRob Swindell 	req.dir_type = cpu_to_le16(type);
12463ebf6f0aSRob Swindell 	req.dir_ordinal = cpu_to_le16(ordinal);
12473ebf6f0aSRob Swindell 	req.dir_ext = cpu_to_le16(ext);
12483ebf6f0aSRob Swindell 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
124990e20921SMichael Chan 	rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
12503ebf6f0aSRob Swindell 	if (rc == 0) {
12513ebf6f0aSRob Swindell 		if (index)
12523ebf6f0aSRob Swindell 			*index = le16_to_cpu(output->dir_idx);
12533ebf6f0aSRob Swindell 		if (item_length)
12543ebf6f0aSRob Swindell 			*item_length = le32_to_cpu(output->dir_item_length);
12553ebf6f0aSRob Swindell 		if (data_length)
12563ebf6f0aSRob Swindell 			*data_length = le32_to_cpu(output->dir_data_length);
12573ebf6f0aSRob Swindell 	}
12583ebf6f0aSRob Swindell 	return rc;
12593ebf6f0aSRob Swindell }
12603ebf6f0aSRob Swindell 
12613ebf6f0aSRob Swindell static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
12623ebf6f0aSRob Swindell {
12633ebf6f0aSRob Swindell 	char	*retval = NULL;
12643ebf6f0aSRob Swindell 	char	*p;
12653ebf6f0aSRob Swindell 	char	*value;
12663ebf6f0aSRob Swindell 	int	field = 0;
12673ebf6f0aSRob Swindell 
12683ebf6f0aSRob Swindell 	if (datalen < 1)
12693ebf6f0aSRob Swindell 		return NULL;
12703ebf6f0aSRob Swindell 	/* null-terminate the log data (removing last '\n'): */
12713ebf6f0aSRob Swindell 	data[datalen - 1] = 0;
12723ebf6f0aSRob Swindell 	for (p = data; *p != 0; p++) {
12733ebf6f0aSRob Swindell 		field = 0;
12743ebf6f0aSRob Swindell 		retval = NULL;
12753ebf6f0aSRob Swindell 		while (*p != 0 && *p != '\n') {
12763ebf6f0aSRob Swindell 			value = p;
12773ebf6f0aSRob Swindell 			while (*p != 0 && *p != '\t' && *p != '\n')
12783ebf6f0aSRob Swindell 				p++;
12793ebf6f0aSRob Swindell 			if (field == desired_field)
12803ebf6f0aSRob Swindell 				retval = value;
12813ebf6f0aSRob Swindell 			if (*p != '\t')
12823ebf6f0aSRob Swindell 				break;
12833ebf6f0aSRob Swindell 			*p = 0;
12843ebf6f0aSRob Swindell 			field++;
12853ebf6f0aSRob Swindell 			p++;
12863ebf6f0aSRob Swindell 		}
12873ebf6f0aSRob Swindell 		if (*p == 0)
12883ebf6f0aSRob Swindell 			break;
12893ebf6f0aSRob Swindell 		*p = 0;
12903ebf6f0aSRob Swindell 	}
12913ebf6f0aSRob Swindell 	return retval;
12923ebf6f0aSRob Swindell }
12933ebf6f0aSRob Swindell 
12943ebf6f0aSRob Swindell static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen)
12953ebf6f0aSRob Swindell {
12963ebf6f0aSRob Swindell 	u16 index = 0;
12973ebf6f0aSRob Swindell 	u32 datalen;
12983ebf6f0aSRob Swindell 
12993ebf6f0aSRob Swindell 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
13003ebf6f0aSRob Swindell 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
13013ebf6f0aSRob Swindell 				 &index, NULL, &datalen) != 0)
13023ebf6f0aSRob Swindell 		return NULL;
13033ebf6f0aSRob Swindell 
13043ebf6f0aSRob Swindell 	memset(buf, 0, buflen);
13053ebf6f0aSRob Swindell 	if (bnxt_get_nvram_item(dev, index, 0, datalen, buf) != 0)
13063ebf6f0aSRob Swindell 		return NULL;
13073ebf6f0aSRob Swindell 
13083ebf6f0aSRob Swindell 	return bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, buf,
13093ebf6f0aSRob Swindell 		datalen);
13103ebf6f0aSRob Swindell }
13113ebf6f0aSRob Swindell 
1312c0c050c5SMichael Chan static int bnxt_get_eeprom(struct net_device *dev,
1313c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
1314c0c050c5SMichael Chan 			   u8 *data)
1315c0c050c5SMichael Chan {
1316c0c050c5SMichael Chan 	u32 index;
1317c0c050c5SMichael Chan 	u32 offset;
1318c0c050c5SMichael Chan 
1319c0c050c5SMichael Chan 	if (eeprom->offset == 0) /* special offset value to get directory */
1320c0c050c5SMichael Chan 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
1321c0c050c5SMichael Chan 
1322c0c050c5SMichael Chan 	index = eeprom->offset >> 24;
1323c0c050c5SMichael Chan 	offset = eeprom->offset & 0xffffff;
1324c0c050c5SMichael Chan 
1325c0c050c5SMichael Chan 	if (index == 0) {
1326c0c050c5SMichael Chan 		netdev_err(dev, "unsupported index value: %d\n", index);
1327c0c050c5SMichael Chan 		return -EINVAL;
1328c0c050c5SMichael Chan 	}
1329c0c050c5SMichael Chan 
1330c0c050c5SMichael Chan 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
1331c0c050c5SMichael Chan }
1332c0c050c5SMichael Chan 
1333c0c050c5SMichael Chan static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
1334c0c050c5SMichael Chan {
1335c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1336c0c050c5SMichael Chan 	struct hwrm_nvm_erase_dir_entry_input req = {0};
1337c0c050c5SMichael Chan 
1338c0c050c5SMichael Chan 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
1339c0c050c5SMichael Chan 	req.dir_idx = cpu_to_le16(index);
1340c0c050c5SMichael Chan 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1341c0c050c5SMichael Chan }
1342c0c050c5SMichael Chan 
1343c0c050c5SMichael Chan static int bnxt_set_eeprom(struct net_device *dev,
1344c0c050c5SMichael Chan 			   struct ethtool_eeprom *eeprom,
1345c0c050c5SMichael Chan 			   u8 *data)
1346c0c050c5SMichael Chan {
1347c0c050c5SMichael Chan 	struct bnxt *bp = netdev_priv(dev);
1348c0c050c5SMichael Chan 	u8 index, dir_op;
1349c0c050c5SMichael Chan 	u16 type, ext, ordinal, attr;
1350c0c050c5SMichael Chan 
1351c0c050c5SMichael Chan 	if (!BNXT_PF(bp)) {
1352c0c050c5SMichael Chan 		netdev_err(dev, "NVM write not supported from a virtual function\n");
1353c0c050c5SMichael Chan 		return -EINVAL;
1354c0c050c5SMichael Chan 	}
1355c0c050c5SMichael Chan 
1356c0c050c5SMichael Chan 	type = eeprom->magic >> 16;
1357c0c050c5SMichael Chan 
1358c0c050c5SMichael Chan 	if (type == 0xffff) { /* special value for directory operations */
1359c0c050c5SMichael Chan 		index = eeprom->magic & 0xff;
1360c0c050c5SMichael Chan 		dir_op = eeprom->magic >> 8;
1361c0c050c5SMichael Chan 		if (index == 0)
1362c0c050c5SMichael Chan 			return -EINVAL;
1363c0c050c5SMichael Chan 		switch (dir_op) {
1364c0c050c5SMichael Chan 		case 0x0e: /* erase */
1365c0c050c5SMichael Chan 			if (eeprom->offset != ~eeprom->magic)
1366c0c050c5SMichael Chan 				return -EINVAL;
1367c0c050c5SMichael Chan 			return bnxt_erase_nvram_directory(dev, index - 1);
1368c0c050c5SMichael Chan 		default:
1369c0c050c5SMichael Chan 			return -EINVAL;
1370c0c050c5SMichael Chan 		}
1371c0c050c5SMichael Chan 	}
1372c0c050c5SMichael Chan 
1373c0c050c5SMichael Chan 	/* Create or re-write an NVM item: */
1374c0c050c5SMichael Chan 	if (bnxt_dir_type_is_executable(type) == true)
1375c0c050c5SMichael Chan 		return -EINVAL;
1376c0c050c5SMichael Chan 	ext = eeprom->magic & 0xffff;
1377c0c050c5SMichael Chan 	ordinal = eeprom->offset >> 16;
1378c0c050c5SMichael Chan 	attr = eeprom->offset & 0xffff;
1379c0c050c5SMichael Chan 
1380c0c050c5SMichael Chan 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
1381c0c050c5SMichael Chan 				eeprom->len);
1382c0c050c5SMichael Chan }
1383c0c050c5SMichael Chan 
1384c0c050c5SMichael Chan const struct ethtool_ops bnxt_ethtool_ops = {
1385c0c050c5SMichael Chan 	.get_settings		= bnxt_get_settings,
1386c0c050c5SMichael Chan 	.set_settings		= bnxt_set_settings,
1387c0c050c5SMichael Chan 	.get_pauseparam		= bnxt_get_pauseparam,
1388c0c050c5SMichael Chan 	.set_pauseparam		= bnxt_set_pauseparam,
1389c0c050c5SMichael Chan 	.get_drvinfo		= bnxt_get_drvinfo,
1390c0c050c5SMichael Chan 	.get_coalesce		= bnxt_get_coalesce,
1391c0c050c5SMichael Chan 	.set_coalesce		= bnxt_set_coalesce,
1392c0c050c5SMichael Chan 	.get_msglevel		= bnxt_get_msglevel,
1393c0c050c5SMichael Chan 	.set_msglevel		= bnxt_set_msglevel,
1394c0c050c5SMichael Chan 	.get_sset_count		= bnxt_get_sset_count,
1395c0c050c5SMichael Chan 	.get_strings		= bnxt_get_strings,
1396c0c050c5SMichael Chan 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
1397c0c050c5SMichael Chan 	.set_ringparam		= bnxt_set_ringparam,
1398c0c050c5SMichael Chan 	.get_ringparam		= bnxt_get_ringparam,
1399c0c050c5SMichael Chan 	.get_channels		= bnxt_get_channels,
1400c0c050c5SMichael Chan 	.set_channels		= bnxt_set_channels,
1401c0c050c5SMichael Chan #ifdef CONFIG_RFS_ACCEL
1402c0c050c5SMichael Chan 	.get_rxnfc		= bnxt_get_rxnfc,
1403c0c050c5SMichael Chan #endif
1404c0c050c5SMichael Chan 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
1405c0c050c5SMichael Chan 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
1406c0c050c5SMichael Chan 	.get_rxfh               = bnxt_get_rxfh,
1407c0c050c5SMichael Chan 	.flash_device		= bnxt_flash_device,
1408c0c050c5SMichael Chan 	.get_eeprom_len         = bnxt_get_eeprom_len,
1409c0c050c5SMichael Chan 	.get_eeprom             = bnxt_get_eeprom,
1410c0c050c5SMichael Chan 	.set_eeprom		= bnxt_set_eeprom,
1411c0c050c5SMichael Chan 	.get_link		= bnxt_get_link,
1412c0c050c5SMichael Chan };
1413