1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2017 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/ctype.h>
12 #include <linux/stringify.h>
13 #include <linux/ethtool.h>
14 #include <linux/interrupt.h>
15 #include <linux/pci.h>
16 #include <linux/etherdevice.h>
17 #include <linux/crc32.h>
18 #include <linux/firmware.h>
19 #include <linux/utsname.h>
20 #include <linux/time.h>
21 #include "bnxt_hsi.h"
22 #include "bnxt.h"
23 #include "bnxt_xdp.h"
24 #include "bnxt_ethtool.h"
25 #include "bnxt_nvm_defs.h"	/* NVRAM content constant and structure defs */
26 #include "bnxt_fw_hdr.h"	/* Firmware hdr constant and structure defs */
27 #include "bnxt_coredump.h"
28 #define FLASH_NVRAM_TIMEOUT	((HWRM_CMD_TIMEOUT) * 100)
29 #define FLASH_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
30 #define INSTALL_PACKAGE_TIMEOUT	((HWRM_CMD_TIMEOUT) * 200)
31 
32 static u32 bnxt_get_msglevel(struct net_device *dev)
33 {
34 	struct bnxt *bp = netdev_priv(dev);
35 
36 	return bp->msg_enable;
37 }
38 
39 static void bnxt_set_msglevel(struct net_device *dev, u32 value)
40 {
41 	struct bnxt *bp = netdev_priv(dev);
42 
43 	bp->msg_enable = value;
44 }
45 
46 static int bnxt_get_coalesce(struct net_device *dev,
47 			     struct ethtool_coalesce *coal)
48 {
49 	struct bnxt *bp = netdev_priv(dev);
50 	struct bnxt_coal *hw_coal;
51 	u16 mult;
52 
53 	memset(coal, 0, sizeof(*coal));
54 
55 	coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM;
56 
57 	hw_coal = &bp->rx_coal;
58 	mult = hw_coal->bufs_per_record;
59 	coal->rx_coalesce_usecs = hw_coal->coal_ticks;
60 	coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult;
61 	coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
62 	coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
63 
64 	hw_coal = &bp->tx_coal;
65 	mult = hw_coal->bufs_per_record;
66 	coal->tx_coalesce_usecs = hw_coal->coal_ticks;
67 	coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult;
68 	coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
69 	coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
70 
71 	coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
72 
73 	return 0;
74 }
75 
76 static int bnxt_set_coalesce(struct net_device *dev,
77 			     struct ethtool_coalesce *coal)
78 {
79 	struct bnxt *bp = netdev_priv(dev);
80 	bool update_stats = false;
81 	struct bnxt_coal *hw_coal;
82 	int rc = 0;
83 	u16 mult;
84 
85 	if (coal->use_adaptive_rx_coalesce) {
86 		bp->flags |= BNXT_FLAG_DIM;
87 	} else {
88 		if (bp->flags & BNXT_FLAG_DIM) {
89 			bp->flags &= ~(BNXT_FLAG_DIM);
90 			goto reset_coalesce;
91 		}
92 	}
93 
94 	hw_coal = &bp->rx_coal;
95 	mult = hw_coal->bufs_per_record;
96 	hw_coal->coal_ticks = coal->rx_coalesce_usecs;
97 	hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult;
98 	hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq;
99 	hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult;
100 
101 	hw_coal = &bp->tx_coal;
102 	mult = hw_coal->bufs_per_record;
103 	hw_coal->coal_ticks = coal->tx_coalesce_usecs;
104 	hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult;
105 	hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq;
106 	hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult;
107 
108 	if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
109 		u32 stats_ticks = coal->stats_block_coalesce_usecs;
110 
111 		/* Allow 0, which means disable. */
112 		if (stats_ticks)
113 			stats_ticks = clamp_t(u32, stats_ticks,
114 					      BNXT_MIN_STATS_COAL_TICKS,
115 					      BNXT_MAX_STATS_COAL_TICKS);
116 		stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
117 		bp->stats_coal_ticks = stats_ticks;
118 		if (bp->stats_coal_ticks)
119 			bp->current_interval =
120 				bp->stats_coal_ticks * HZ / 1000000;
121 		else
122 			bp->current_interval = BNXT_TIMER_INTERVAL;
123 		update_stats = true;
124 	}
125 
126 reset_coalesce:
127 	if (netif_running(dev)) {
128 		if (update_stats) {
129 			rc = bnxt_close_nic(bp, true, false);
130 			if (!rc)
131 				rc = bnxt_open_nic(bp, true, false);
132 		} else {
133 			rc = bnxt_hwrm_set_coal(bp);
134 		}
135 	}
136 
137 	return rc;
138 }
139 
140 static const char * const bnxt_ring_stats_str[] = {
141 	"rx_ucast_packets",
142 	"rx_mcast_packets",
143 	"rx_bcast_packets",
144 	"rx_discards",
145 	"rx_drops",
146 	"rx_ucast_bytes",
147 	"rx_mcast_bytes",
148 	"rx_bcast_bytes",
149 	"tx_ucast_packets",
150 	"tx_mcast_packets",
151 	"tx_bcast_packets",
152 	"tx_discards",
153 	"tx_drops",
154 	"tx_ucast_bytes",
155 	"tx_mcast_bytes",
156 	"tx_bcast_bytes",
157 };
158 
159 static const char * const bnxt_ring_tpa_stats_str[] = {
160 	"tpa_packets",
161 	"tpa_bytes",
162 	"tpa_events",
163 	"tpa_aborts",
164 };
165 
166 static const char * const bnxt_ring_tpa2_stats_str[] = {
167 	"rx_tpa_eligible_pkt",
168 	"rx_tpa_eligible_bytes",
169 	"rx_tpa_pkt",
170 	"rx_tpa_bytes",
171 	"rx_tpa_errors",
172 };
173 
174 static const char * const bnxt_ring_sw_stats_str[] = {
175 	"rx_l4_csum_errors",
176 	"rx_buf_errors",
177 	"missed_irqs",
178 };
179 
180 #define BNXT_RX_STATS_ENTRY(counter)	\
181 	{ BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
182 
183 #define BNXT_TX_STATS_ENTRY(counter)	\
184 	{ BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
185 
186 #define BNXT_RX_STATS_EXT_ENTRY(counter)	\
187 	{ BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
188 
189 #define BNXT_TX_STATS_EXT_ENTRY(counter)	\
190 	{ BNXT_TX_STATS_EXT_OFFSET(counter), __stringify(counter) }
191 
192 #define BNXT_RX_STATS_EXT_PFC_ENTRY(n)				\
193 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_duration_us),	\
194 	BNXT_RX_STATS_EXT_ENTRY(pfc_pri##n##_rx_transitions)
195 
196 #define BNXT_TX_STATS_EXT_PFC_ENTRY(n)				\
197 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_duration_us),	\
198 	BNXT_TX_STATS_EXT_ENTRY(pfc_pri##n##_tx_transitions)
199 
200 #define BNXT_RX_STATS_EXT_PFC_ENTRIES				\
201 	BNXT_RX_STATS_EXT_PFC_ENTRY(0),				\
202 	BNXT_RX_STATS_EXT_PFC_ENTRY(1),				\
203 	BNXT_RX_STATS_EXT_PFC_ENTRY(2),				\
204 	BNXT_RX_STATS_EXT_PFC_ENTRY(3),				\
205 	BNXT_RX_STATS_EXT_PFC_ENTRY(4),				\
206 	BNXT_RX_STATS_EXT_PFC_ENTRY(5),				\
207 	BNXT_RX_STATS_EXT_PFC_ENTRY(6),				\
208 	BNXT_RX_STATS_EXT_PFC_ENTRY(7)
209 
210 #define BNXT_TX_STATS_EXT_PFC_ENTRIES				\
211 	BNXT_TX_STATS_EXT_PFC_ENTRY(0),				\
212 	BNXT_TX_STATS_EXT_PFC_ENTRY(1),				\
213 	BNXT_TX_STATS_EXT_PFC_ENTRY(2),				\
214 	BNXT_TX_STATS_EXT_PFC_ENTRY(3),				\
215 	BNXT_TX_STATS_EXT_PFC_ENTRY(4),				\
216 	BNXT_TX_STATS_EXT_PFC_ENTRY(5),				\
217 	BNXT_TX_STATS_EXT_PFC_ENTRY(6),				\
218 	BNXT_TX_STATS_EXT_PFC_ENTRY(7)
219 
220 #define BNXT_RX_STATS_EXT_COS_ENTRY(n)				\
221 	BNXT_RX_STATS_EXT_ENTRY(rx_bytes_cos##n),		\
222 	BNXT_RX_STATS_EXT_ENTRY(rx_packets_cos##n)
223 
224 #define BNXT_TX_STATS_EXT_COS_ENTRY(n)				\
225 	BNXT_TX_STATS_EXT_ENTRY(tx_bytes_cos##n),		\
226 	BNXT_TX_STATS_EXT_ENTRY(tx_packets_cos##n)
227 
228 #define BNXT_RX_STATS_EXT_COS_ENTRIES				\
229 	BNXT_RX_STATS_EXT_COS_ENTRY(0),				\
230 	BNXT_RX_STATS_EXT_COS_ENTRY(1),				\
231 	BNXT_RX_STATS_EXT_COS_ENTRY(2),				\
232 	BNXT_RX_STATS_EXT_COS_ENTRY(3),				\
233 	BNXT_RX_STATS_EXT_COS_ENTRY(4),				\
234 	BNXT_RX_STATS_EXT_COS_ENTRY(5),				\
235 	BNXT_RX_STATS_EXT_COS_ENTRY(6),				\
236 	BNXT_RX_STATS_EXT_COS_ENTRY(7)				\
237 
238 #define BNXT_TX_STATS_EXT_COS_ENTRIES				\
239 	BNXT_TX_STATS_EXT_COS_ENTRY(0),				\
240 	BNXT_TX_STATS_EXT_COS_ENTRY(1),				\
241 	BNXT_TX_STATS_EXT_COS_ENTRY(2),				\
242 	BNXT_TX_STATS_EXT_COS_ENTRY(3),				\
243 	BNXT_TX_STATS_EXT_COS_ENTRY(4),				\
244 	BNXT_TX_STATS_EXT_COS_ENTRY(5),				\
245 	BNXT_TX_STATS_EXT_COS_ENTRY(6),				\
246 	BNXT_TX_STATS_EXT_COS_ENTRY(7)				\
247 
248 #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(n)			\
249 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_bytes_cos##n),	\
250 	BNXT_RX_STATS_EXT_ENTRY(rx_discard_packets_cos##n)
251 
252 #define BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES				\
253 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(0),				\
254 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(1),				\
255 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(2),				\
256 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(3),				\
257 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(4),				\
258 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(5),				\
259 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(6),				\
260 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRY(7)
261 
262 #define BNXT_RX_STATS_PRI_ENTRY(counter, n)		\
263 	{ BNXT_RX_STATS_EXT_OFFSET(counter##_cos0),	\
264 	  __stringify(counter##_pri##n) }
265 
266 #define BNXT_TX_STATS_PRI_ENTRY(counter, n)		\
267 	{ BNXT_TX_STATS_EXT_OFFSET(counter##_cos0),	\
268 	  __stringify(counter##_pri##n) }
269 
270 #define BNXT_RX_STATS_PRI_ENTRIES(counter)		\
271 	BNXT_RX_STATS_PRI_ENTRY(counter, 0),		\
272 	BNXT_RX_STATS_PRI_ENTRY(counter, 1),		\
273 	BNXT_RX_STATS_PRI_ENTRY(counter, 2),		\
274 	BNXT_RX_STATS_PRI_ENTRY(counter, 3),		\
275 	BNXT_RX_STATS_PRI_ENTRY(counter, 4),		\
276 	BNXT_RX_STATS_PRI_ENTRY(counter, 5),		\
277 	BNXT_RX_STATS_PRI_ENTRY(counter, 6),		\
278 	BNXT_RX_STATS_PRI_ENTRY(counter, 7)
279 
280 #define BNXT_TX_STATS_PRI_ENTRIES(counter)		\
281 	BNXT_TX_STATS_PRI_ENTRY(counter, 0),		\
282 	BNXT_TX_STATS_PRI_ENTRY(counter, 1),		\
283 	BNXT_TX_STATS_PRI_ENTRY(counter, 2),		\
284 	BNXT_TX_STATS_PRI_ENTRY(counter, 3),		\
285 	BNXT_TX_STATS_PRI_ENTRY(counter, 4),		\
286 	BNXT_TX_STATS_PRI_ENTRY(counter, 5),		\
287 	BNXT_TX_STATS_PRI_ENTRY(counter, 6),		\
288 	BNXT_TX_STATS_PRI_ENTRY(counter, 7)
289 
290 #define BNXT_PCIE_STATS_ENTRY(counter)	\
291 	{ BNXT_PCIE_STATS_OFFSET(counter), __stringify(counter) }
292 
293 enum {
294 	RX_TOTAL_DISCARDS,
295 	TX_TOTAL_DISCARDS,
296 };
297 
298 static struct {
299 	u64			counter;
300 	char			string[ETH_GSTRING_LEN];
301 } bnxt_sw_func_stats[] = {
302 	{0, "rx_total_discard_pkts"},
303 	{0, "tx_total_discard_pkts"},
304 };
305 
306 static const struct {
307 	long offset;
308 	char string[ETH_GSTRING_LEN];
309 } bnxt_port_stats_arr[] = {
310 	BNXT_RX_STATS_ENTRY(rx_64b_frames),
311 	BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
312 	BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
313 	BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
314 	BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
315 	BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
316 	BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
317 	BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
318 	BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
319 	BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
320 	BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
321 	BNXT_RX_STATS_ENTRY(rx_total_frames),
322 	BNXT_RX_STATS_ENTRY(rx_ucast_frames),
323 	BNXT_RX_STATS_ENTRY(rx_mcast_frames),
324 	BNXT_RX_STATS_ENTRY(rx_bcast_frames),
325 	BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
326 	BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
327 	BNXT_RX_STATS_ENTRY(rx_pause_frames),
328 	BNXT_RX_STATS_ENTRY(rx_pfc_frames),
329 	BNXT_RX_STATS_ENTRY(rx_align_err_frames),
330 	BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
331 	BNXT_RX_STATS_ENTRY(rx_jbr_frames),
332 	BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
333 	BNXT_RX_STATS_ENTRY(rx_tagged_frames),
334 	BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
335 	BNXT_RX_STATS_ENTRY(rx_good_frames),
336 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
337 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
338 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
339 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
340 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
341 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
342 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
343 	BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
344 	BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
345 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
346 	BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
347 	BNXT_RX_STATS_ENTRY(rx_bytes),
348 	BNXT_RX_STATS_ENTRY(rx_runt_bytes),
349 	BNXT_RX_STATS_ENTRY(rx_runt_frames),
350 	BNXT_RX_STATS_ENTRY(rx_stat_discard),
351 	BNXT_RX_STATS_ENTRY(rx_stat_err),
352 
353 	BNXT_TX_STATS_ENTRY(tx_64b_frames),
354 	BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
355 	BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
356 	BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
357 	BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
358 	BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
359 	BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
360 	BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
361 	BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
362 	BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
363 	BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
364 	BNXT_TX_STATS_ENTRY(tx_good_frames),
365 	BNXT_TX_STATS_ENTRY(tx_total_frames),
366 	BNXT_TX_STATS_ENTRY(tx_ucast_frames),
367 	BNXT_TX_STATS_ENTRY(tx_mcast_frames),
368 	BNXT_TX_STATS_ENTRY(tx_bcast_frames),
369 	BNXT_TX_STATS_ENTRY(tx_pause_frames),
370 	BNXT_TX_STATS_ENTRY(tx_pfc_frames),
371 	BNXT_TX_STATS_ENTRY(tx_jabber_frames),
372 	BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
373 	BNXT_TX_STATS_ENTRY(tx_err),
374 	BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
375 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
376 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
377 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
378 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
379 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
380 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
381 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
382 	BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
383 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
384 	BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
385 	BNXT_TX_STATS_ENTRY(tx_total_collisions),
386 	BNXT_TX_STATS_ENTRY(tx_bytes),
387 	BNXT_TX_STATS_ENTRY(tx_xthol_frames),
388 	BNXT_TX_STATS_ENTRY(tx_stat_discard),
389 	BNXT_TX_STATS_ENTRY(tx_stat_error),
390 };
391 
392 static const struct {
393 	long offset;
394 	char string[ETH_GSTRING_LEN];
395 } bnxt_port_stats_ext_arr[] = {
396 	BNXT_RX_STATS_EXT_ENTRY(link_down_events),
397 	BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
398 	BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
399 	BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
400 	BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
401 	BNXT_RX_STATS_EXT_COS_ENTRIES,
402 	BNXT_RX_STATS_EXT_PFC_ENTRIES,
403 	BNXT_RX_STATS_EXT_ENTRY(rx_bits),
404 	BNXT_RX_STATS_EXT_ENTRY(rx_buffer_passed_threshold),
405 	BNXT_RX_STATS_EXT_ENTRY(rx_pcs_symbol_err),
406 	BNXT_RX_STATS_EXT_ENTRY(rx_corrected_bits),
407 	BNXT_RX_STATS_EXT_DISCARD_COS_ENTRIES,
408 };
409 
410 static const struct {
411 	long offset;
412 	char string[ETH_GSTRING_LEN];
413 } bnxt_tx_port_stats_ext_arr[] = {
414 	BNXT_TX_STATS_EXT_COS_ENTRIES,
415 	BNXT_TX_STATS_EXT_PFC_ENTRIES,
416 };
417 
418 static const struct {
419 	long base_off;
420 	char string[ETH_GSTRING_LEN];
421 } bnxt_rx_bytes_pri_arr[] = {
422 	BNXT_RX_STATS_PRI_ENTRIES(rx_bytes),
423 };
424 
425 static const struct {
426 	long base_off;
427 	char string[ETH_GSTRING_LEN];
428 } bnxt_rx_pkts_pri_arr[] = {
429 	BNXT_RX_STATS_PRI_ENTRIES(rx_packets),
430 };
431 
432 static const struct {
433 	long base_off;
434 	char string[ETH_GSTRING_LEN];
435 } bnxt_tx_bytes_pri_arr[] = {
436 	BNXT_TX_STATS_PRI_ENTRIES(tx_bytes),
437 };
438 
439 static const struct {
440 	long base_off;
441 	char string[ETH_GSTRING_LEN];
442 } bnxt_tx_pkts_pri_arr[] = {
443 	BNXT_TX_STATS_PRI_ENTRIES(tx_packets),
444 };
445 
446 static const struct {
447 	long offset;
448 	char string[ETH_GSTRING_LEN];
449 } bnxt_pcie_stats_arr[] = {
450 	BNXT_PCIE_STATS_ENTRY(pcie_pl_signal_integrity),
451 	BNXT_PCIE_STATS_ENTRY(pcie_dl_signal_integrity),
452 	BNXT_PCIE_STATS_ENTRY(pcie_tl_signal_integrity),
453 	BNXT_PCIE_STATS_ENTRY(pcie_link_integrity),
454 	BNXT_PCIE_STATS_ENTRY(pcie_tx_traffic_rate),
455 	BNXT_PCIE_STATS_ENTRY(pcie_rx_traffic_rate),
456 	BNXT_PCIE_STATS_ENTRY(pcie_tx_dllp_statistics),
457 	BNXT_PCIE_STATS_ENTRY(pcie_rx_dllp_statistics),
458 	BNXT_PCIE_STATS_ENTRY(pcie_equalization_time),
459 	BNXT_PCIE_STATS_ENTRY(pcie_ltssm_histogram[0]),
460 	BNXT_PCIE_STATS_ENTRY(pcie_ltssm_histogram[2]),
461 	BNXT_PCIE_STATS_ENTRY(pcie_recovery_histogram),
462 };
463 
464 #define BNXT_NUM_SW_FUNC_STATS	ARRAY_SIZE(bnxt_sw_func_stats)
465 #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
466 #define BNXT_NUM_STATS_PRI			\
467 	(ARRAY_SIZE(bnxt_rx_bytes_pri_arr) +	\
468 	 ARRAY_SIZE(bnxt_rx_pkts_pri_arr) +	\
469 	 ARRAY_SIZE(bnxt_tx_bytes_pri_arr) +	\
470 	 ARRAY_SIZE(bnxt_tx_pkts_pri_arr))
471 #define BNXT_NUM_PCIE_STATS ARRAY_SIZE(bnxt_pcie_stats_arr)
472 
473 static int bnxt_get_num_tpa_ring_stats(struct bnxt *bp)
474 {
475 	if (BNXT_SUPPORTS_TPA(bp)) {
476 		if (bp->max_tpa_v2)
477 			return ARRAY_SIZE(bnxt_ring_tpa2_stats_str);
478 		return ARRAY_SIZE(bnxt_ring_tpa_stats_str);
479 	}
480 	return 0;
481 }
482 
483 static int bnxt_get_num_ring_stats(struct bnxt *bp)
484 {
485 	int num_stats;
486 
487 	num_stats = ARRAY_SIZE(bnxt_ring_stats_str) +
488 		    ARRAY_SIZE(bnxt_ring_sw_stats_str) +
489 		    bnxt_get_num_tpa_ring_stats(bp);
490 	return num_stats * bp->cp_nr_rings;
491 }
492 
493 static int bnxt_get_num_stats(struct bnxt *bp)
494 {
495 	int num_stats = bnxt_get_num_ring_stats(bp);
496 
497 	num_stats += BNXT_NUM_SW_FUNC_STATS;
498 
499 	if (bp->flags & BNXT_FLAG_PORT_STATS)
500 		num_stats += BNXT_NUM_PORT_STATS;
501 
502 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
503 		num_stats += bp->fw_rx_stats_ext_size +
504 			     bp->fw_tx_stats_ext_size;
505 		if (bp->pri2cos_valid)
506 			num_stats += BNXT_NUM_STATS_PRI;
507 	}
508 
509 	if (bp->flags & BNXT_FLAG_PCIE_STATS)
510 		num_stats += BNXT_NUM_PCIE_STATS;
511 
512 	return num_stats;
513 }
514 
515 static int bnxt_get_sset_count(struct net_device *dev, int sset)
516 {
517 	struct bnxt *bp = netdev_priv(dev);
518 
519 	switch (sset) {
520 	case ETH_SS_STATS:
521 		return bnxt_get_num_stats(bp);
522 	case ETH_SS_TEST:
523 		if (!bp->num_tests)
524 			return -EOPNOTSUPP;
525 		return bp->num_tests;
526 	default:
527 		return -EOPNOTSUPP;
528 	}
529 }
530 
531 static void bnxt_get_ethtool_stats(struct net_device *dev,
532 				   struct ethtool_stats *stats, u64 *buf)
533 {
534 	u32 i, j = 0;
535 	struct bnxt *bp = netdev_priv(dev);
536 	u32 stat_fields = ARRAY_SIZE(bnxt_ring_stats_str) +
537 			  bnxt_get_num_tpa_ring_stats(bp);
538 
539 	if (!bp->bnapi) {
540 		j += bnxt_get_num_ring_stats(bp) + BNXT_NUM_SW_FUNC_STATS;
541 		goto skip_ring_stats;
542 	}
543 
544 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
545 		bnxt_sw_func_stats[i].counter = 0;
546 
547 	for (i = 0; i < bp->cp_nr_rings; i++) {
548 		struct bnxt_napi *bnapi = bp->bnapi[i];
549 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
550 		__le64 *hw_stats = (__le64 *)cpr->hw_stats;
551 		int k;
552 
553 		for (k = 0; k < stat_fields; j++, k++)
554 			buf[j] = le64_to_cpu(hw_stats[k]);
555 		buf[j++] = cpr->rx_l4_csum_errors;
556 		buf[j++] = cpr->rx_buf_errors;
557 		buf[j++] = cpr->missed_irqs;
558 
559 		bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
560 			le64_to_cpu(cpr->hw_stats->rx_discard_pkts);
561 		bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
562 			le64_to_cpu(cpr->hw_stats->tx_discard_pkts);
563 	}
564 
565 	for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
566 		buf[j] = bnxt_sw_func_stats[i].counter;
567 
568 skip_ring_stats:
569 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
570 		__le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
571 
572 		for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
573 			buf[j] = le64_to_cpu(*(port_stats +
574 					       bnxt_port_stats_arr[i].offset));
575 		}
576 	}
577 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
578 		__le64 *rx_port_stats_ext = (__le64 *)bp->hw_rx_port_stats_ext;
579 		__le64 *tx_port_stats_ext = (__le64 *)bp->hw_tx_port_stats_ext;
580 
581 		for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
582 			buf[j] = le64_to_cpu(*(rx_port_stats_ext +
583 					    bnxt_port_stats_ext_arr[i].offset));
584 		}
585 		for (i = 0; i < bp->fw_tx_stats_ext_size; i++, j++) {
586 			buf[j] = le64_to_cpu(*(tx_port_stats_ext +
587 					bnxt_tx_port_stats_ext_arr[i].offset));
588 		}
589 		if (bp->pri2cos_valid) {
590 			for (i = 0; i < 8; i++, j++) {
591 				long n = bnxt_rx_bytes_pri_arr[i].base_off +
592 					 bp->pri2cos_idx[i];
593 
594 				buf[j] = le64_to_cpu(*(rx_port_stats_ext + n));
595 			}
596 			for (i = 0; i < 8; i++, j++) {
597 				long n = bnxt_rx_pkts_pri_arr[i].base_off +
598 					 bp->pri2cos_idx[i];
599 
600 				buf[j] = le64_to_cpu(*(rx_port_stats_ext + n));
601 			}
602 			for (i = 0; i < 8; i++, j++) {
603 				long n = bnxt_tx_bytes_pri_arr[i].base_off +
604 					 bp->pri2cos_idx[i];
605 
606 				buf[j] = le64_to_cpu(*(tx_port_stats_ext + n));
607 			}
608 			for (i = 0; i < 8; i++, j++) {
609 				long n = bnxt_tx_pkts_pri_arr[i].base_off +
610 					 bp->pri2cos_idx[i];
611 
612 				buf[j] = le64_to_cpu(*(tx_port_stats_ext + n));
613 			}
614 		}
615 	}
616 	if (bp->flags & BNXT_FLAG_PCIE_STATS) {
617 		__le64 *pcie_stats = (__le64 *)bp->hw_pcie_stats;
618 
619 		for (i = 0; i < BNXT_NUM_PCIE_STATS; i++, j++) {
620 			buf[j] = le64_to_cpu(*(pcie_stats +
621 					       bnxt_pcie_stats_arr[i].offset));
622 		}
623 	}
624 }
625 
626 static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
627 {
628 	struct bnxt *bp = netdev_priv(dev);
629 	static const char * const *str;
630 	u32 i, j, num_str;
631 
632 	switch (stringset) {
633 	case ETH_SS_STATS:
634 		for (i = 0; i < bp->cp_nr_rings; i++) {
635 			num_str = ARRAY_SIZE(bnxt_ring_stats_str);
636 			for (j = 0; j < num_str; j++) {
637 				sprintf(buf, "[%d]: %s", i,
638 					bnxt_ring_stats_str[j]);
639 				buf += ETH_GSTRING_LEN;
640 			}
641 			if (!BNXT_SUPPORTS_TPA(bp))
642 				goto skip_tpa_stats;
643 
644 			if (bp->max_tpa_v2) {
645 				num_str = ARRAY_SIZE(bnxt_ring_tpa2_stats_str);
646 				str = bnxt_ring_tpa2_stats_str;
647 			} else {
648 				num_str = ARRAY_SIZE(bnxt_ring_tpa_stats_str);
649 				str = bnxt_ring_tpa_stats_str;
650 			}
651 			for (j = 0; j < num_str; j++) {
652 				sprintf(buf, "[%d]: %s", i, str[j]);
653 				buf += ETH_GSTRING_LEN;
654 			}
655 skip_tpa_stats:
656 			num_str = ARRAY_SIZE(bnxt_ring_sw_stats_str);
657 			for (j = 0; j < num_str; j++) {
658 				sprintf(buf, "[%d]: %s", i,
659 					bnxt_ring_sw_stats_str[j]);
660 				buf += ETH_GSTRING_LEN;
661 			}
662 		}
663 		for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
664 			strcpy(buf, bnxt_sw_func_stats[i].string);
665 			buf += ETH_GSTRING_LEN;
666 		}
667 
668 		if (bp->flags & BNXT_FLAG_PORT_STATS) {
669 			for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
670 				strcpy(buf, bnxt_port_stats_arr[i].string);
671 				buf += ETH_GSTRING_LEN;
672 			}
673 		}
674 		if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
675 			for (i = 0; i < bp->fw_rx_stats_ext_size; i++) {
676 				strcpy(buf, bnxt_port_stats_ext_arr[i].string);
677 				buf += ETH_GSTRING_LEN;
678 			}
679 			for (i = 0; i < bp->fw_tx_stats_ext_size; i++) {
680 				strcpy(buf,
681 				       bnxt_tx_port_stats_ext_arr[i].string);
682 				buf += ETH_GSTRING_LEN;
683 			}
684 			if (bp->pri2cos_valid) {
685 				for (i = 0; i < 8; i++) {
686 					strcpy(buf,
687 					       bnxt_rx_bytes_pri_arr[i].string);
688 					buf += ETH_GSTRING_LEN;
689 				}
690 				for (i = 0; i < 8; i++) {
691 					strcpy(buf,
692 					       bnxt_rx_pkts_pri_arr[i].string);
693 					buf += ETH_GSTRING_LEN;
694 				}
695 				for (i = 0; i < 8; i++) {
696 					strcpy(buf,
697 					       bnxt_tx_bytes_pri_arr[i].string);
698 					buf += ETH_GSTRING_LEN;
699 				}
700 				for (i = 0; i < 8; i++) {
701 					strcpy(buf,
702 					       bnxt_tx_pkts_pri_arr[i].string);
703 					buf += ETH_GSTRING_LEN;
704 				}
705 			}
706 		}
707 		if (bp->flags & BNXT_FLAG_PCIE_STATS) {
708 			for (i = 0; i < BNXT_NUM_PCIE_STATS; i++) {
709 				strcpy(buf, bnxt_pcie_stats_arr[i].string);
710 				buf += ETH_GSTRING_LEN;
711 			}
712 		}
713 		break;
714 	case ETH_SS_TEST:
715 		if (bp->num_tests)
716 			memcpy(buf, bp->test_info->string,
717 			       bp->num_tests * ETH_GSTRING_LEN);
718 		break;
719 	default:
720 		netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
721 			   stringset);
722 		break;
723 	}
724 }
725 
726 static void bnxt_get_ringparam(struct net_device *dev,
727 			       struct ethtool_ringparam *ering)
728 {
729 	struct bnxt *bp = netdev_priv(dev);
730 
731 	ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
732 	ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
733 	ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
734 
735 	ering->rx_pending = bp->rx_ring_size;
736 	ering->rx_jumbo_pending = bp->rx_agg_ring_size;
737 	ering->tx_pending = bp->tx_ring_size;
738 }
739 
740 static int bnxt_set_ringparam(struct net_device *dev,
741 			      struct ethtool_ringparam *ering)
742 {
743 	struct bnxt *bp = netdev_priv(dev);
744 
745 	if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
746 	    (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
747 	    (ering->tx_pending <= MAX_SKB_FRAGS))
748 		return -EINVAL;
749 
750 	if (netif_running(dev))
751 		bnxt_close_nic(bp, false, false);
752 
753 	bp->rx_ring_size = ering->rx_pending;
754 	bp->tx_ring_size = ering->tx_pending;
755 	bnxt_set_ring_params(bp);
756 
757 	if (netif_running(dev))
758 		return bnxt_open_nic(bp, false, false);
759 
760 	return 0;
761 }
762 
763 static void bnxt_get_channels(struct net_device *dev,
764 			      struct ethtool_channels *channel)
765 {
766 	struct bnxt *bp = netdev_priv(dev);
767 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
768 	int max_rx_rings, max_tx_rings, tcs;
769 	int max_tx_sch_inputs;
770 
771 	/* Get the most up-to-date max_tx_sch_inputs. */
772 	if (BNXT_NEW_RM(bp))
773 		bnxt_hwrm_func_resc_qcaps(bp, false);
774 	max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
775 
776 	bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
777 	if (max_tx_sch_inputs)
778 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
779 	channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
780 
781 	if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
782 		max_rx_rings = 0;
783 		max_tx_rings = 0;
784 	}
785 	if (max_tx_sch_inputs)
786 		max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
787 
788 	tcs = netdev_get_num_tc(dev);
789 	if (tcs > 1)
790 		max_tx_rings /= tcs;
791 
792 	channel->max_rx = max_rx_rings;
793 	channel->max_tx = max_tx_rings;
794 	channel->max_other = 0;
795 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
796 		channel->combined_count = bp->rx_nr_rings;
797 		if (BNXT_CHIP_TYPE_NITRO_A0(bp))
798 			channel->combined_count--;
799 	} else {
800 		if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
801 			channel->rx_count = bp->rx_nr_rings;
802 			channel->tx_count = bp->tx_nr_rings_per_tc;
803 		}
804 	}
805 }
806 
807 static int bnxt_set_channels(struct net_device *dev,
808 			     struct ethtool_channels *channel)
809 {
810 	struct bnxt *bp = netdev_priv(dev);
811 	int req_tx_rings, req_rx_rings, tcs;
812 	bool sh = false;
813 	int tx_xdp = 0;
814 	int rc = 0;
815 
816 	if (channel->other_count)
817 		return -EINVAL;
818 
819 	if (!channel->combined_count &&
820 	    (!channel->rx_count || !channel->tx_count))
821 		return -EINVAL;
822 
823 	if (channel->combined_count &&
824 	    (channel->rx_count || channel->tx_count))
825 		return -EINVAL;
826 
827 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
828 					    channel->tx_count))
829 		return -EINVAL;
830 
831 	if (channel->combined_count)
832 		sh = true;
833 
834 	tcs = netdev_get_num_tc(dev);
835 
836 	req_tx_rings = sh ? channel->combined_count : channel->tx_count;
837 	req_rx_rings = sh ? channel->combined_count : channel->rx_count;
838 	if (bp->tx_nr_rings_xdp) {
839 		if (!sh) {
840 			netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
841 			return -EINVAL;
842 		}
843 		tx_xdp = req_rx_rings;
844 	}
845 	rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
846 	if (rc) {
847 		netdev_warn(dev, "Unable to allocate the requested rings\n");
848 		return rc;
849 	}
850 
851 	if (netif_running(dev)) {
852 		if (BNXT_PF(bp)) {
853 			/* TODO CHIMP_FW: Send message to all VF's
854 			 * before PF unload
855 			 */
856 		}
857 		rc = bnxt_close_nic(bp, true, false);
858 		if (rc) {
859 			netdev_err(bp->dev, "Set channel failure rc :%x\n",
860 				   rc);
861 			return rc;
862 		}
863 	}
864 
865 	if (sh) {
866 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
867 		bp->rx_nr_rings = channel->combined_count;
868 		bp->tx_nr_rings_per_tc = channel->combined_count;
869 	} else {
870 		bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
871 		bp->rx_nr_rings = channel->rx_count;
872 		bp->tx_nr_rings_per_tc = channel->tx_count;
873 	}
874 	bp->tx_nr_rings_xdp = tx_xdp;
875 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
876 	if (tcs > 1)
877 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
878 
879 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
880 			       bp->tx_nr_rings + bp->rx_nr_rings;
881 
882 	/* After changing number of rx channels, update NTUPLE feature. */
883 	netdev_update_features(dev);
884 	if (netif_running(dev)) {
885 		rc = bnxt_open_nic(bp, true, false);
886 		if ((!rc) && BNXT_PF(bp)) {
887 			/* TODO CHIMP_FW: Send message to all VF's
888 			 * to renable
889 			 */
890 		}
891 	} else {
892 		rc = bnxt_reserve_rings(bp, true);
893 	}
894 
895 	return rc;
896 }
897 
898 #ifdef CONFIG_RFS_ACCEL
899 static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
900 			    u32 *rule_locs)
901 {
902 	int i, j = 0;
903 
904 	cmd->data = bp->ntp_fltr_count;
905 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
906 		struct hlist_head *head;
907 		struct bnxt_ntuple_filter *fltr;
908 
909 		head = &bp->ntp_fltr_hash_tbl[i];
910 		rcu_read_lock();
911 		hlist_for_each_entry_rcu(fltr, head, hash) {
912 			if (j == cmd->rule_cnt)
913 				break;
914 			rule_locs[j++] = fltr->sw_id;
915 		}
916 		rcu_read_unlock();
917 		if (j == cmd->rule_cnt)
918 			break;
919 	}
920 	cmd->rule_cnt = j;
921 	return 0;
922 }
923 
924 static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
925 {
926 	struct ethtool_rx_flow_spec *fs =
927 		(struct ethtool_rx_flow_spec *)&cmd->fs;
928 	struct bnxt_ntuple_filter *fltr;
929 	struct flow_keys *fkeys;
930 	int i, rc = -EINVAL;
931 
932 	if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
933 		return rc;
934 
935 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
936 		struct hlist_head *head;
937 
938 		head = &bp->ntp_fltr_hash_tbl[i];
939 		rcu_read_lock();
940 		hlist_for_each_entry_rcu(fltr, head, hash) {
941 			if (fltr->sw_id == fs->location)
942 				goto fltr_found;
943 		}
944 		rcu_read_unlock();
945 	}
946 	return rc;
947 
948 fltr_found:
949 	fkeys = &fltr->fkeys;
950 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
951 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
952 			fs->flow_type = TCP_V4_FLOW;
953 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
954 			fs->flow_type = UDP_V4_FLOW;
955 		else
956 			goto fltr_err;
957 
958 		fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
959 		fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
960 
961 		fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
962 		fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
963 
964 		fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
965 		fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
966 
967 		fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
968 		fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
969 	} else {
970 		int i;
971 
972 		if (fkeys->basic.ip_proto == IPPROTO_TCP)
973 			fs->flow_type = TCP_V6_FLOW;
974 		else if (fkeys->basic.ip_proto == IPPROTO_UDP)
975 			fs->flow_type = UDP_V6_FLOW;
976 		else
977 			goto fltr_err;
978 
979 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
980 			fkeys->addrs.v6addrs.src;
981 		*(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
982 			fkeys->addrs.v6addrs.dst;
983 		for (i = 0; i < 4; i++) {
984 			fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
985 			fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
986 		}
987 		fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
988 		fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
989 
990 		fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
991 		fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
992 	}
993 
994 	fs->ring_cookie = fltr->rxq;
995 	rc = 0;
996 
997 fltr_err:
998 	rcu_read_unlock();
999 
1000 	return rc;
1001 }
1002 #endif
1003 
1004 static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
1005 {
1006 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
1007 		return RXH_IP_SRC | RXH_IP_DST;
1008 	return 0;
1009 }
1010 
1011 static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
1012 {
1013 	if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
1014 		return RXH_IP_SRC | RXH_IP_DST;
1015 	return 0;
1016 }
1017 
1018 static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1019 {
1020 	cmd->data = 0;
1021 	switch (cmd->flow_type) {
1022 	case TCP_V4_FLOW:
1023 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
1024 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1025 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1026 		cmd->data |= get_ethtool_ipv4_rss(bp);
1027 		break;
1028 	case UDP_V4_FLOW:
1029 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
1030 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1031 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1032 		/* fall through */
1033 	case SCTP_V4_FLOW:
1034 	case AH_ESP_V4_FLOW:
1035 	case AH_V4_FLOW:
1036 	case ESP_V4_FLOW:
1037 	case IPV4_FLOW:
1038 		cmd->data |= get_ethtool_ipv4_rss(bp);
1039 		break;
1040 
1041 	case TCP_V6_FLOW:
1042 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
1043 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1044 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1045 		cmd->data |= get_ethtool_ipv6_rss(bp);
1046 		break;
1047 	case UDP_V6_FLOW:
1048 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
1049 			cmd->data |= RXH_IP_SRC | RXH_IP_DST |
1050 				     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1051 		/* fall through */
1052 	case SCTP_V6_FLOW:
1053 	case AH_ESP_V6_FLOW:
1054 	case AH_V6_FLOW:
1055 	case ESP_V6_FLOW:
1056 	case IPV6_FLOW:
1057 		cmd->data |= get_ethtool_ipv6_rss(bp);
1058 		break;
1059 	}
1060 	return 0;
1061 }
1062 
1063 #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1064 #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
1065 
1066 static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
1067 {
1068 	u32 rss_hash_cfg = bp->rss_hash_cfg;
1069 	int tuple, rc = 0;
1070 
1071 	if (cmd->data == RXH_4TUPLE)
1072 		tuple = 4;
1073 	else if (cmd->data == RXH_2TUPLE)
1074 		tuple = 2;
1075 	else if (!cmd->data)
1076 		tuple = 0;
1077 	else
1078 		return -EINVAL;
1079 
1080 	if (cmd->flow_type == TCP_V4_FLOW) {
1081 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1082 		if (tuple == 4)
1083 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
1084 	} else if (cmd->flow_type == UDP_V4_FLOW) {
1085 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1086 			return -EINVAL;
1087 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1088 		if (tuple == 4)
1089 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
1090 	} else if (cmd->flow_type == TCP_V6_FLOW) {
1091 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1092 		if (tuple == 4)
1093 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
1094 	} else if (cmd->flow_type == UDP_V6_FLOW) {
1095 		if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
1096 			return -EINVAL;
1097 		rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1098 		if (tuple == 4)
1099 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
1100 	} else if (tuple == 4) {
1101 		return -EINVAL;
1102 	}
1103 
1104 	switch (cmd->flow_type) {
1105 	case TCP_V4_FLOW:
1106 	case UDP_V4_FLOW:
1107 	case SCTP_V4_FLOW:
1108 	case AH_ESP_V4_FLOW:
1109 	case AH_V4_FLOW:
1110 	case ESP_V4_FLOW:
1111 	case IPV4_FLOW:
1112 		if (tuple == 2)
1113 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1114 		else if (!tuple)
1115 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
1116 		break;
1117 
1118 	case TCP_V6_FLOW:
1119 	case UDP_V6_FLOW:
1120 	case SCTP_V6_FLOW:
1121 	case AH_ESP_V6_FLOW:
1122 	case AH_V6_FLOW:
1123 	case ESP_V6_FLOW:
1124 	case IPV6_FLOW:
1125 		if (tuple == 2)
1126 			rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1127 		else if (!tuple)
1128 			rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
1129 		break;
1130 	}
1131 
1132 	if (bp->rss_hash_cfg == rss_hash_cfg)
1133 		return 0;
1134 
1135 	bp->rss_hash_cfg = rss_hash_cfg;
1136 	if (netif_running(bp->dev)) {
1137 		bnxt_close_nic(bp, false, false);
1138 		rc = bnxt_open_nic(bp, false, false);
1139 	}
1140 	return rc;
1141 }
1142 
1143 static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1144 			  u32 *rule_locs)
1145 {
1146 	struct bnxt *bp = netdev_priv(dev);
1147 	int rc = 0;
1148 
1149 	switch (cmd->cmd) {
1150 #ifdef CONFIG_RFS_ACCEL
1151 	case ETHTOOL_GRXRINGS:
1152 		cmd->data = bp->rx_nr_rings;
1153 		break;
1154 
1155 	case ETHTOOL_GRXCLSRLCNT:
1156 		cmd->rule_cnt = bp->ntp_fltr_count;
1157 		cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
1158 		break;
1159 
1160 	case ETHTOOL_GRXCLSRLALL:
1161 		rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
1162 		break;
1163 
1164 	case ETHTOOL_GRXCLSRULE:
1165 		rc = bnxt_grxclsrule(bp, cmd);
1166 		break;
1167 #endif
1168 
1169 	case ETHTOOL_GRXFH:
1170 		rc = bnxt_grxfh(bp, cmd);
1171 		break;
1172 
1173 	default:
1174 		rc = -EOPNOTSUPP;
1175 		break;
1176 	}
1177 
1178 	return rc;
1179 }
1180 
1181 static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1182 {
1183 	struct bnxt *bp = netdev_priv(dev);
1184 	int rc;
1185 
1186 	switch (cmd->cmd) {
1187 	case ETHTOOL_SRXFH:
1188 		rc = bnxt_srxfh(bp, cmd);
1189 		break;
1190 
1191 	default:
1192 		rc = -EOPNOTSUPP;
1193 		break;
1194 	}
1195 	return rc;
1196 }
1197 
1198 static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
1199 {
1200 	return HW_HASH_INDEX_SIZE;
1201 }
1202 
1203 static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
1204 {
1205 	return HW_HASH_KEY_SIZE;
1206 }
1207 
1208 static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1209 			 u8 *hfunc)
1210 {
1211 	struct bnxt *bp = netdev_priv(dev);
1212 	struct bnxt_vnic_info *vnic;
1213 	int i = 0;
1214 
1215 	if (hfunc)
1216 		*hfunc = ETH_RSS_HASH_TOP;
1217 
1218 	if (!bp->vnic_info)
1219 		return 0;
1220 
1221 	vnic = &bp->vnic_info[0];
1222 	if (indir && vnic->rss_table) {
1223 		for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
1224 			indir[i] = le16_to_cpu(vnic->rss_table[i]);
1225 	}
1226 
1227 	if (key && vnic->rss_hash_key)
1228 		memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
1229 
1230 	return 0;
1231 }
1232 
1233 static void bnxt_get_drvinfo(struct net_device *dev,
1234 			     struct ethtool_drvinfo *info)
1235 {
1236 	struct bnxt *bp = netdev_priv(dev);
1237 
1238 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1239 	strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
1240 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
1241 	info->n_stats = bnxt_get_num_stats(bp);
1242 	info->testinfo_len = bp->num_tests;
1243 	/* TODO CHIMP_FW: eeprom dump details */
1244 	info->eedump_len = 0;
1245 	/* TODO CHIMP FW: reg dump details */
1246 	info->regdump_len = 0;
1247 }
1248 
1249 static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1250 {
1251 	struct bnxt *bp = netdev_priv(dev);
1252 
1253 	wol->supported = 0;
1254 	wol->wolopts = 0;
1255 	memset(&wol->sopass, 0, sizeof(wol->sopass));
1256 	if (bp->flags & BNXT_FLAG_WOL_CAP) {
1257 		wol->supported = WAKE_MAGIC;
1258 		if (bp->wol)
1259 			wol->wolopts = WAKE_MAGIC;
1260 	}
1261 }
1262 
1263 static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1264 {
1265 	struct bnxt *bp = netdev_priv(dev);
1266 
1267 	if (wol->wolopts & ~WAKE_MAGIC)
1268 		return -EINVAL;
1269 
1270 	if (wol->wolopts & WAKE_MAGIC) {
1271 		if (!(bp->flags & BNXT_FLAG_WOL_CAP))
1272 			return -EINVAL;
1273 		if (!bp->wol) {
1274 			if (bnxt_hwrm_alloc_wol_fltr(bp))
1275 				return -EBUSY;
1276 			bp->wol = 1;
1277 		}
1278 	} else {
1279 		if (bp->wol) {
1280 			if (bnxt_hwrm_free_wol_fltr(bp))
1281 				return -EBUSY;
1282 			bp->wol = 0;
1283 		}
1284 	}
1285 	return 0;
1286 }
1287 
1288 u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
1289 {
1290 	u32 speed_mask = 0;
1291 
1292 	/* TODO: support 25GB, 40GB, 50GB with different cable type */
1293 	/* set the advertised speeds */
1294 	if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
1295 		speed_mask |= ADVERTISED_100baseT_Full;
1296 	if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
1297 		speed_mask |= ADVERTISED_1000baseT_Full;
1298 	if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1299 		speed_mask |= ADVERTISED_2500baseX_Full;
1300 	if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1301 		speed_mask |= ADVERTISED_10000baseT_Full;
1302 	if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
1303 		speed_mask |= ADVERTISED_40000baseCR4_Full;
1304 
1305 	if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
1306 		speed_mask |= ADVERTISED_Pause;
1307 	else if (fw_pause & BNXT_LINK_PAUSE_TX)
1308 		speed_mask |= ADVERTISED_Asym_Pause;
1309 	else if (fw_pause & BNXT_LINK_PAUSE_RX)
1310 		speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
1311 
1312 	return speed_mask;
1313 }
1314 
1315 #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
1316 {									\
1317 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)			\
1318 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1319 						     100baseT_Full);	\
1320 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)			\
1321 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1322 						     1000baseT_Full);	\
1323 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)			\
1324 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1325 						     10000baseT_Full);	\
1326 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)			\
1327 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1328 						     25000baseCR_Full);	\
1329 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)			\
1330 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1331 						     40000baseCR4_Full);\
1332 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)			\
1333 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1334 						     50000baseCR2_Full);\
1335 	if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)			\
1336 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1337 						     100000baseCR4_Full);\
1338 	if ((fw_pause) & BNXT_LINK_PAUSE_RX) {				\
1339 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1340 						     Pause);		\
1341 		if (!((fw_pause) & BNXT_LINK_PAUSE_TX))			\
1342 			ethtool_link_ksettings_add_link_mode(		\
1343 					lk_ksettings, name, Asym_Pause);\
1344 	} else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {			\
1345 		ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1346 						     Asym_Pause);	\
1347 	}								\
1348 }
1349 
1350 #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)		\
1351 {									\
1352 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1353 						  100baseT_Full) ||	\
1354 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1355 						  100baseT_Half))	\
1356 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;		\
1357 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1358 						  1000baseT_Full) ||	\
1359 	    ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1360 						  1000baseT_Half))	\
1361 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;			\
1362 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1363 						  10000baseT_Full))	\
1364 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;		\
1365 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1366 						  25000baseCR_Full))	\
1367 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;		\
1368 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1369 						  40000baseCR4_Full))	\
1370 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;		\
1371 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1372 						  50000baseCR2_Full))	\
1373 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;		\
1374 	if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,	\
1375 						  100000baseCR4_Full))	\
1376 		(fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;		\
1377 }
1378 
1379 static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
1380 				struct ethtool_link_ksettings *lk_ksettings)
1381 {
1382 	u16 fw_speeds = link_info->advertising;
1383 	u8 fw_pause = 0;
1384 
1385 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1386 		fw_pause = link_info->auto_pause_setting;
1387 
1388 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
1389 }
1390 
1391 static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
1392 				struct ethtool_link_ksettings *lk_ksettings)
1393 {
1394 	u16 fw_speeds = link_info->lp_auto_link_speeds;
1395 	u8 fw_pause = 0;
1396 
1397 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1398 		fw_pause = link_info->lp_pause;
1399 
1400 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
1401 				lp_advertising);
1402 }
1403 
1404 static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
1405 				struct ethtool_link_ksettings *lk_ksettings)
1406 {
1407 	u16 fw_speeds = link_info->support_speeds;
1408 
1409 	BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
1410 
1411 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
1412 	ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1413 					     Asym_Pause);
1414 
1415 	if (link_info->support_auto_speeds)
1416 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1417 						     Autoneg);
1418 }
1419 
1420 u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1421 {
1422 	switch (fw_link_speed) {
1423 	case BNXT_LINK_SPEED_100MB:
1424 		return SPEED_100;
1425 	case BNXT_LINK_SPEED_1GB:
1426 		return SPEED_1000;
1427 	case BNXT_LINK_SPEED_2_5GB:
1428 		return SPEED_2500;
1429 	case BNXT_LINK_SPEED_10GB:
1430 		return SPEED_10000;
1431 	case BNXT_LINK_SPEED_20GB:
1432 		return SPEED_20000;
1433 	case BNXT_LINK_SPEED_25GB:
1434 		return SPEED_25000;
1435 	case BNXT_LINK_SPEED_40GB:
1436 		return SPEED_40000;
1437 	case BNXT_LINK_SPEED_50GB:
1438 		return SPEED_50000;
1439 	case BNXT_LINK_SPEED_100GB:
1440 		return SPEED_100000;
1441 	default:
1442 		return SPEED_UNKNOWN;
1443 	}
1444 }
1445 
1446 static int bnxt_get_link_ksettings(struct net_device *dev,
1447 				   struct ethtool_link_ksettings *lk_ksettings)
1448 {
1449 	struct bnxt *bp = netdev_priv(dev);
1450 	struct bnxt_link_info *link_info = &bp->link_info;
1451 	struct ethtool_link_settings *base = &lk_ksettings->base;
1452 	u32 ethtool_speed;
1453 
1454 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1455 	mutex_lock(&bp->link_lock);
1456 	bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1457 
1458 	ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1459 	if (link_info->autoneg) {
1460 		bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
1461 		ethtool_link_ksettings_add_link_mode(lk_ksettings,
1462 						     advertising, Autoneg);
1463 		base->autoneg = AUTONEG_ENABLE;
1464 		base->duplex = DUPLEX_UNKNOWN;
1465 		if (link_info->phy_link_status == BNXT_LINK_LINK) {
1466 			bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
1467 			if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
1468 				base->duplex = DUPLEX_FULL;
1469 			else
1470 				base->duplex = DUPLEX_HALF;
1471 		}
1472 		ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1473 	} else {
1474 		base->autoneg = AUTONEG_DISABLE;
1475 		ethtool_speed =
1476 			bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
1477 		base->duplex = DUPLEX_HALF;
1478 		if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
1479 			base->duplex = DUPLEX_FULL;
1480 	}
1481 	base->speed = ethtool_speed;
1482 
1483 	base->port = PORT_NONE;
1484 	if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
1485 		base->port = PORT_TP;
1486 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1487 						     TP);
1488 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
1489 						     TP);
1490 	} else {
1491 		ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1492 						     FIBRE);
1493 		ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
1494 						     FIBRE);
1495 
1496 		if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
1497 			base->port = PORT_DA;
1498 		else if (link_info->media_type ==
1499 			 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
1500 			base->port = PORT_FIBRE;
1501 	}
1502 	base->phy_address = link_info->phy_addr;
1503 	mutex_unlock(&bp->link_lock);
1504 
1505 	return 0;
1506 }
1507 
1508 static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed)
1509 {
1510 	struct bnxt *bp = netdev_priv(dev);
1511 	struct bnxt_link_info *link_info = &bp->link_info;
1512 	u16 support_spds = link_info->support_speeds;
1513 	u32 fw_speed = 0;
1514 
1515 	switch (ethtool_speed) {
1516 	case SPEED_100:
1517 		if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
1518 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
1519 		break;
1520 	case SPEED_1000:
1521 		if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
1522 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
1523 		break;
1524 	case SPEED_2500:
1525 		if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
1526 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
1527 		break;
1528 	case SPEED_10000:
1529 		if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
1530 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
1531 		break;
1532 	case SPEED_20000:
1533 		if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
1534 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
1535 		break;
1536 	case SPEED_25000:
1537 		if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
1538 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
1539 		break;
1540 	case SPEED_40000:
1541 		if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
1542 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
1543 		break;
1544 	case SPEED_50000:
1545 		if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
1546 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
1547 		break;
1548 	case SPEED_100000:
1549 		if (support_spds & BNXT_LINK_SPEED_MSK_100GB)
1550 			fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB;
1551 		break;
1552 	default:
1553 		netdev_err(dev, "unsupported speed!\n");
1554 		break;
1555 	}
1556 	return fw_speed;
1557 }
1558 
1559 u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1560 {
1561 	u16 fw_speed_mask = 0;
1562 
1563 	/* only support autoneg at speed 100, 1000, and 10000 */
1564 	if (advertising & (ADVERTISED_100baseT_Full |
1565 			   ADVERTISED_100baseT_Half)) {
1566 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1567 	}
1568 	if (advertising & (ADVERTISED_1000baseT_Full |
1569 			   ADVERTISED_1000baseT_Half)) {
1570 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1571 	}
1572 	if (advertising & ADVERTISED_10000baseT_Full)
1573 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1574 
1575 	if (advertising & ADVERTISED_40000baseCR4_Full)
1576 		fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
1577 
1578 	return fw_speed_mask;
1579 }
1580 
1581 static int bnxt_set_link_ksettings(struct net_device *dev,
1582 			   const struct ethtool_link_ksettings *lk_ksettings)
1583 {
1584 	struct bnxt *bp = netdev_priv(dev);
1585 	struct bnxt_link_info *link_info = &bp->link_info;
1586 	const struct ethtool_link_settings *base = &lk_ksettings->base;
1587 	bool set_pause = false;
1588 	u16 fw_advertising = 0;
1589 	u32 speed;
1590 	int rc = 0;
1591 
1592 	if (!BNXT_PHY_CFG_ABLE(bp))
1593 		return -EOPNOTSUPP;
1594 
1595 	mutex_lock(&bp->link_lock);
1596 	if (base->autoneg == AUTONEG_ENABLE) {
1597 		BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
1598 					advertising);
1599 		link_info->autoneg |= BNXT_AUTONEG_SPEED;
1600 		if (!fw_advertising)
1601 			link_info->advertising = link_info->support_auto_speeds;
1602 		else
1603 			link_info->advertising = fw_advertising;
1604 		/* any change to autoneg will cause link change, therefore the
1605 		 * driver should put back the original pause setting in autoneg
1606 		 */
1607 		set_pause = true;
1608 	} else {
1609 		u16 fw_speed;
1610 		u8 phy_type = link_info->phy_type;
1611 
1612 		if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
1613 		    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
1614 		    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
1615 			netdev_err(dev, "10GBase-T devices must autoneg\n");
1616 			rc = -EINVAL;
1617 			goto set_setting_exit;
1618 		}
1619 		if (base->duplex == DUPLEX_HALF) {
1620 			netdev_err(dev, "HALF DUPLEX is not supported!\n");
1621 			rc = -EINVAL;
1622 			goto set_setting_exit;
1623 		}
1624 		speed = base->speed;
1625 		fw_speed = bnxt_get_fw_speed(dev, speed);
1626 		if (!fw_speed) {
1627 			rc = -EINVAL;
1628 			goto set_setting_exit;
1629 		}
1630 		link_info->req_link_speed = fw_speed;
1631 		link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1632 		link_info->autoneg = 0;
1633 		link_info->advertising = 0;
1634 	}
1635 
1636 	if (netif_running(dev))
1637 		rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1638 
1639 set_setting_exit:
1640 	mutex_unlock(&bp->link_lock);
1641 	return rc;
1642 }
1643 
1644 static void bnxt_get_pauseparam(struct net_device *dev,
1645 				struct ethtool_pauseparam *epause)
1646 {
1647 	struct bnxt *bp = netdev_priv(dev);
1648 	struct bnxt_link_info *link_info = &bp->link_info;
1649 
1650 	if (BNXT_VF(bp))
1651 		return;
1652 	epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
1653 	epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
1654 	epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1655 }
1656 
1657 static int bnxt_set_pauseparam(struct net_device *dev,
1658 			       struct ethtool_pauseparam *epause)
1659 {
1660 	int rc = 0;
1661 	struct bnxt *bp = netdev_priv(dev);
1662 	struct bnxt_link_info *link_info = &bp->link_info;
1663 
1664 	if (!BNXT_PHY_CFG_ABLE(bp))
1665 		return -EOPNOTSUPP;
1666 
1667 	if (epause->autoneg) {
1668 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
1669 			return -EINVAL;
1670 
1671 		link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1672 		if (bp->hwrm_spec_code >= 0x10201)
1673 			link_info->req_flow_ctrl =
1674 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1675 	} else {
1676 		/* when transition from auto pause to force pause,
1677 		 * force a link change
1678 		 */
1679 		if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1680 			link_info->force_link_chng = true;
1681 		link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1682 		link_info->req_flow_ctrl = 0;
1683 	}
1684 	if (epause->rx_pause)
1685 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1686 
1687 	if (epause->tx_pause)
1688 		link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1689 
1690 	if (netif_running(dev))
1691 		rc = bnxt_hwrm_set_pause(bp);
1692 	return rc;
1693 }
1694 
1695 static u32 bnxt_get_link(struct net_device *dev)
1696 {
1697 	struct bnxt *bp = netdev_priv(dev);
1698 
1699 	/* TODO: handle MF, VF, driver close case */
1700 	return bp->link_info.link_up;
1701 }
1702 
1703 static void bnxt_print_admin_err(struct bnxt *bp)
1704 {
1705 	netdev_info(bp->dev, "PF does not have admin privileges to flash or reset the device\n");
1706 }
1707 
1708 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1709 				u16 ext, u16 *index, u32 *item_length,
1710 				u32 *data_length);
1711 
1712 static int bnxt_flash_nvram(struct net_device *dev,
1713 			    u16 dir_type,
1714 			    u16 dir_ordinal,
1715 			    u16 dir_ext,
1716 			    u16 dir_attr,
1717 			    const u8 *data,
1718 			    size_t data_len)
1719 {
1720 	struct bnxt *bp = netdev_priv(dev);
1721 	int rc;
1722 	struct hwrm_nvm_write_input req = {0};
1723 	dma_addr_t dma_handle;
1724 	u8 *kmem;
1725 
1726 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1727 
1728 	req.dir_type = cpu_to_le16(dir_type);
1729 	req.dir_ordinal = cpu_to_le16(dir_ordinal);
1730 	req.dir_ext = cpu_to_le16(dir_ext);
1731 	req.dir_attr = cpu_to_le16(dir_attr);
1732 	req.dir_data_length = cpu_to_le32(data_len);
1733 
1734 	kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1735 				  GFP_KERNEL);
1736 	if (!kmem) {
1737 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1738 			   (unsigned)data_len);
1739 		return -ENOMEM;
1740 	}
1741 	memcpy(kmem, data, data_len);
1742 	req.host_src_addr = cpu_to_le64(dma_handle);
1743 
1744 	rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1745 	dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1746 
1747 	if (rc == -EACCES)
1748 		bnxt_print_admin_err(bp);
1749 	return rc;
1750 }
1751 
1752 static int bnxt_firmware_reset(struct net_device *dev,
1753 			       u16 dir_type)
1754 {
1755 	struct hwrm_fw_reset_input req = {0};
1756 	struct bnxt *bp = netdev_priv(dev);
1757 	int rc;
1758 
1759 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1760 
1761 	/* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1762 	/*       (e.g. when firmware isn't already running) */
1763 	switch (dir_type) {
1764 	case BNX_DIR_TYPE_CHIMP_PATCH:
1765 	case BNX_DIR_TYPE_BOOTCODE:
1766 	case BNX_DIR_TYPE_BOOTCODE_2:
1767 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1768 		/* Self-reset ChiMP upon next PCIe reset: */
1769 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1770 		break;
1771 	case BNX_DIR_TYPE_APE_FW:
1772 	case BNX_DIR_TYPE_APE_PATCH:
1773 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
1774 		/* Self-reset APE upon next PCIe reset: */
1775 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1776 		break;
1777 	case BNX_DIR_TYPE_KONG_FW:
1778 	case BNX_DIR_TYPE_KONG_PATCH:
1779 		req.embedded_proc_type =
1780 			FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1781 		break;
1782 	case BNX_DIR_TYPE_BONO_FW:
1783 	case BNX_DIR_TYPE_BONO_PATCH:
1784 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1785 		break;
1786 	case BNXT_FW_RESET_CHIP:
1787 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
1788 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
1789 		if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
1790 			req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
1791 		break;
1792 	case BNXT_FW_RESET_AP:
1793 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP;
1794 		break;
1795 	default:
1796 		return -EINVAL;
1797 	}
1798 
1799 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1800 	if (rc == -EACCES)
1801 		bnxt_print_admin_err(bp);
1802 	return rc;
1803 }
1804 
1805 static int bnxt_flash_firmware(struct net_device *dev,
1806 			       u16 dir_type,
1807 			       const u8 *fw_data,
1808 			       size_t fw_size)
1809 {
1810 	int	rc = 0;
1811 	u16	code_type;
1812 	u32	stored_crc;
1813 	u32	calculated_crc;
1814 	struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1815 
1816 	switch (dir_type) {
1817 	case BNX_DIR_TYPE_BOOTCODE:
1818 	case BNX_DIR_TYPE_BOOTCODE_2:
1819 		code_type = CODE_BOOT;
1820 		break;
1821 	case BNX_DIR_TYPE_CHIMP_PATCH:
1822 		code_type = CODE_CHIMP_PATCH;
1823 		break;
1824 	case BNX_DIR_TYPE_APE_FW:
1825 		code_type = CODE_MCTP_PASSTHRU;
1826 		break;
1827 	case BNX_DIR_TYPE_APE_PATCH:
1828 		code_type = CODE_APE_PATCH;
1829 		break;
1830 	case BNX_DIR_TYPE_KONG_FW:
1831 		code_type = CODE_KONG_FW;
1832 		break;
1833 	case BNX_DIR_TYPE_KONG_PATCH:
1834 		code_type = CODE_KONG_PATCH;
1835 		break;
1836 	case BNX_DIR_TYPE_BONO_FW:
1837 		code_type = CODE_BONO_FW;
1838 		break;
1839 	case BNX_DIR_TYPE_BONO_PATCH:
1840 		code_type = CODE_BONO_PATCH;
1841 		break;
1842 	default:
1843 		netdev_err(dev, "Unsupported directory entry type: %u\n",
1844 			   dir_type);
1845 		return -EINVAL;
1846 	}
1847 	if (fw_size < sizeof(struct bnxt_fw_header)) {
1848 		netdev_err(dev, "Invalid firmware file size: %u\n",
1849 			   (unsigned int)fw_size);
1850 		return -EINVAL;
1851 	}
1852 	if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1853 		netdev_err(dev, "Invalid firmware signature: %08X\n",
1854 			   le32_to_cpu(header->signature));
1855 		return -EINVAL;
1856 	}
1857 	if (header->code_type != code_type) {
1858 		netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1859 			   code_type, header->code_type);
1860 		return -EINVAL;
1861 	}
1862 	if (header->device != DEVICE_CUMULUS_FAMILY) {
1863 		netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1864 			   DEVICE_CUMULUS_FAMILY, header->device);
1865 		return -EINVAL;
1866 	}
1867 	/* Confirm the CRC32 checksum of the file: */
1868 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1869 					     sizeof(stored_crc)));
1870 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1871 	if (calculated_crc != stored_crc) {
1872 		netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1873 			   (unsigned long)stored_crc,
1874 			   (unsigned long)calculated_crc);
1875 		return -EINVAL;
1876 	}
1877 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1878 			      0, 0, fw_data, fw_size);
1879 	if (rc == 0)	/* Firmware update successful */
1880 		rc = bnxt_firmware_reset(dev, dir_type);
1881 
1882 	return rc;
1883 }
1884 
1885 static int bnxt_flash_microcode(struct net_device *dev,
1886 				u16 dir_type,
1887 				const u8 *fw_data,
1888 				size_t fw_size)
1889 {
1890 	struct bnxt_ucode_trailer *trailer;
1891 	u32 calculated_crc;
1892 	u32 stored_crc;
1893 	int rc = 0;
1894 
1895 	if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
1896 		netdev_err(dev, "Invalid microcode file size: %u\n",
1897 			   (unsigned int)fw_size);
1898 		return -EINVAL;
1899 	}
1900 	trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
1901 						sizeof(*trailer)));
1902 	if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
1903 		netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
1904 			   le32_to_cpu(trailer->sig));
1905 		return -EINVAL;
1906 	}
1907 	if (le16_to_cpu(trailer->dir_type) != dir_type) {
1908 		netdev_err(dev, "Expected microcode type: %d, read: %d\n",
1909 			   dir_type, le16_to_cpu(trailer->dir_type));
1910 		return -EINVAL;
1911 	}
1912 	if (le16_to_cpu(trailer->trailer_length) <
1913 		sizeof(struct bnxt_ucode_trailer)) {
1914 		netdev_err(dev, "Invalid microcode trailer length: %d\n",
1915 			   le16_to_cpu(trailer->trailer_length));
1916 		return -EINVAL;
1917 	}
1918 
1919 	/* Confirm the CRC32 checksum of the file: */
1920 	stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1921 					     sizeof(stored_crc)));
1922 	calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1923 	if (calculated_crc != stored_crc) {
1924 		netdev_err(dev,
1925 			   "CRC32 (%08lX) does not match calculated: %08lX\n",
1926 			   (unsigned long)stored_crc,
1927 			   (unsigned long)calculated_crc);
1928 		return -EINVAL;
1929 	}
1930 	rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1931 			      0, 0, fw_data, fw_size);
1932 
1933 	return rc;
1934 }
1935 
1936 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1937 {
1938 	switch (dir_type) {
1939 	case BNX_DIR_TYPE_CHIMP_PATCH:
1940 	case BNX_DIR_TYPE_BOOTCODE:
1941 	case BNX_DIR_TYPE_BOOTCODE_2:
1942 	case BNX_DIR_TYPE_APE_FW:
1943 	case BNX_DIR_TYPE_APE_PATCH:
1944 	case BNX_DIR_TYPE_KONG_FW:
1945 	case BNX_DIR_TYPE_KONG_PATCH:
1946 	case BNX_DIR_TYPE_BONO_FW:
1947 	case BNX_DIR_TYPE_BONO_PATCH:
1948 		return true;
1949 	}
1950 
1951 	return false;
1952 }
1953 
1954 static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
1955 {
1956 	switch (dir_type) {
1957 	case BNX_DIR_TYPE_AVS:
1958 	case BNX_DIR_TYPE_EXP_ROM_MBA:
1959 	case BNX_DIR_TYPE_PCIE:
1960 	case BNX_DIR_TYPE_TSCF_UCODE:
1961 	case BNX_DIR_TYPE_EXT_PHY:
1962 	case BNX_DIR_TYPE_CCM:
1963 	case BNX_DIR_TYPE_ISCSI_BOOT:
1964 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1965 	case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1966 		return true;
1967 	}
1968 
1969 	return false;
1970 }
1971 
1972 static bool bnxt_dir_type_is_executable(u16 dir_type)
1973 {
1974 	return bnxt_dir_type_is_ape_bin_format(dir_type) ||
1975 		bnxt_dir_type_is_other_exec_format(dir_type);
1976 }
1977 
1978 static int bnxt_flash_firmware_from_file(struct net_device *dev,
1979 					 u16 dir_type,
1980 					 const char *filename)
1981 {
1982 	const struct firmware  *fw;
1983 	int			rc;
1984 
1985 	rc = request_firmware(&fw, filename, &dev->dev);
1986 	if (rc != 0) {
1987 		netdev_err(dev, "Error %d requesting firmware file: %s\n",
1988 			   rc, filename);
1989 		return rc;
1990 	}
1991 	if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1992 		rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1993 	else if (bnxt_dir_type_is_other_exec_format(dir_type) == true)
1994 		rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
1995 	else
1996 		rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1997 				      0, 0, fw->data, fw->size);
1998 	release_firmware(fw);
1999 	return rc;
2000 }
2001 
2002 int bnxt_flash_package_from_file(struct net_device *dev, const char *filename,
2003 				 u32 install_type)
2004 {
2005 	struct bnxt *bp = netdev_priv(dev);
2006 	struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
2007 	struct hwrm_nvm_install_update_input install = {0};
2008 	const struct firmware *fw;
2009 	u32 item_len;
2010 	int rc = 0;
2011 	u16 index;
2012 
2013 	bnxt_hwrm_fw_set_time(bp);
2014 
2015 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
2016 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2017 				 &index, &item_len, NULL) != 0) {
2018 		netdev_err(dev, "PKG update area not created in nvram\n");
2019 		return -ENOBUFS;
2020 	}
2021 
2022 	rc = request_firmware(&fw, filename, &dev->dev);
2023 	if (rc != 0) {
2024 		netdev_err(dev, "PKG error %d requesting file: %s\n",
2025 			   rc, filename);
2026 		return rc;
2027 	}
2028 
2029 	if (fw->size > item_len) {
2030 		netdev_err(dev, "PKG insufficient update area in nvram: %lu\n",
2031 			   (unsigned long)fw->size);
2032 		rc = -EFBIG;
2033 	} else {
2034 		dma_addr_t dma_handle;
2035 		u8 *kmem;
2036 		struct hwrm_nvm_modify_input modify = {0};
2037 
2038 		bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
2039 
2040 		modify.dir_idx = cpu_to_le16(index);
2041 		modify.len = cpu_to_le32(fw->size);
2042 
2043 		kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
2044 					  &dma_handle, GFP_KERNEL);
2045 		if (!kmem) {
2046 			netdev_err(dev,
2047 				   "dma_alloc_coherent failure, length = %u\n",
2048 				   (unsigned int)fw->size);
2049 			rc = -ENOMEM;
2050 		} else {
2051 			memcpy(kmem, fw->data, fw->size);
2052 			modify.host_src_addr = cpu_to_le64(dma_handle);
2053 
2054 			rc = hwrm_send_message(bp, &modify, sizeof(modify),
2055 					       FLASH_PACKAGE_TIMEOUT);
2056 			dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
2057 					  dma_handle);
2058 		}
2059 	}
2060 	release_firmware(fw);
2061 	if (rc)
2062 		goto err_exit;
2063 
2064 	if ((install_type & 0xffff) == 0)
2065 		install_type >>= 16;
2066 	bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
2067 	install.install_type = cpu_to_le32(install_type);
2068 
2069 	mutex_lock(&bp->hwrm_cmd_lock);
2070 	rc = _hwrm_send_message(bp, &install, sizeof(install),
2071 				INSTALL_PACKAGE_TIMEOUT);
2072 	if (rc) {
2073 		u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err;
2074 
2075 		if (resp->error_code && error_code ==
2076 		    NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
2077 			install.flags |= cpu_to_le16(
2078 			       NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
2079 			rc = _hwrm_send_message(bp, &install, sizeof(install),
2080 						INSTALL_PACKAGE_TIMEOUT);
2081 		}
2082 		if (rc)
2083 			goto flash_pkg_exit;
2084 	}
2085 
2086 	if (resp->result) {
2087 		netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
2088 			   (s8)resp->result, (int)resp->problem_item);
2089 		rc = -ENOPKG;
2090 	}
2091 flash_pkg_exit:
2092 	mutex_unlock(&bp->hwrm_cmd_lock);
2093 err_exit:
2094 	if (rc == -EACCES)
2095 		bnxt_print_admin_err(bp);
2096 	return rc;
2097 }
2098 
2099 static int bnxt_flash_device(struct net_device *dev,
2100 			     struct ethtool_flash *flash)
2101 {
2102 	if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
2103 		netdev_err(dev, "flashdev not supported from a virtual function\n");
2104 		return -EINVAL;
2105 	}
2106 
2107 	if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
2108 	    flash->region > 0xffff)
2109 		return bnxt_flash_package_from_file(dev, flash->data,
2110 						    flash->region);
2111 
2112 	return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
2113 }
2114 
2115 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
2116 {
2117 	struct bnxt *bp = netdev_priv(dev);
2118 	int rc;
2119 	struct hwrm_nvm_get_dir_info_input req = {0};
2120 	struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
2121 
2122 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
2123 
2124 	mutex_lock(&bp->hwrm_cmd_lock);
2125 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2126 	if (!rc) {
2127 		*entries = le32_to_cpu(output->entries);
2128 		*length = le32_to_cpu(output->entry_length);
2129 	}
2130 	mutex_unlock(&bp->hwrm_cmd_lock);
2131 	return rc;
2132 }
2133 
2134 static int bnxt_get_eeprom_len(struct net_device *dev)
2135 {
2136 	struct bnxt *bp = netdev_priv(dev);
2137 
2138 	if (BNXT_VF(bp))
2139 		return 0;
2140 
2141 	/* The -1 return value allows the entire 32-bit range of offsets to be
2142 	 * passed via the ethtool command-line utility.
2143 	 */
2144 	return -1;
2145 }
2146 
2147 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
2148 {
2149 	struct bnxt *bp = netdev_priv(dev);
2150 	int rc;
2151 	u32 dir_entries;
2152 	u32 entry_length;
2153 	u8 *buf;
2154 	size_t buflen;
2155 	dma_addr_t dma_handle;
2156 	struct hwrm_nvm_get_dir_entries_input req = {0};
2157 
2158 	rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
2159 	if (rc != 0)
2160 		return rc;
2161 
2162 	/* Insert 2 bytes of directory info (count and size of entries) */
2163 	if (len < 2)
2164 		return -EINVAL;
2165 
2166 	*data++ = dir_entries;
2167 	*data++ = entry_length;
2168 	len -= 2;
2169 	memset(data, 0xff, len);
2170 
2171 	buflen = dir_entries * entry_length;
2172 	buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
2173 				 GFP_KERNEL);
2174 	if (!buf) {
2175 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2176 			   (unsigned)buflen);
2177 		return -ENOMEM;
2178 	}
2179 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
2180 	req.host_dest_addr = cpu_to_le64(dma_handle);
2181 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2182 	if (rc == 0)
2183 		memcpy(data, buf, len > buflen ? buflen : len);
2184 	dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
2185 	return rc;
2186 }
2187 
2188 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
2189 			       u32 length, u8 *data)
2190 {
2191 	struct bnxt *bp = netdev_priv(dev);
2192 	int rc;
2193 	u8 *buf;
2194 	dma_addr_t dma_handle;
2195 	struct hwrm_nvm_read_input req = {0};
2196 
2197 	if (!length)
2198 		return -EINVAL;
2199 
2200 	buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
2201 				 GFP_KERNEL);
2202 	if (!buf) {
2203 		netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
2204 			   (unsigned)length);
2205 		return -ENOMEM;
2206 	}
2207 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
2208 	req.host_dest_addr = cpu_to_le64(dma_handle);
2209 	req.dir_idx = cpu_to_le16(index);
2210 	req.offset = cpu_to_le32(offset);
2211 	req.len = cpu_to_le32(length);
2212 
2213 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2214 	if (rc == 0)
2215 		memcpy(data, buf, length);
2216 	dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
2217 	return rc;
2218 }
2219 
2220 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
2221 				u16 ext, u16 *index, u32 *item_length,
2222 				u32 *data_length)
2223 {
2224 	struct bnxt *bp = netdev_priv(dev);
2225 	int rc;
2226 	struct hwrm_nvm_find_dir_entry_input req = {0};
2227 	struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
2228 
2229 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
2230 	req.enables = 0;
2231 	req.dir_idx = 0;
2232 	req.dir_type = cpu_to_le16(type);
2233 	req.dir_ordinal = cpu_to_le16(ordinal);
2234 	req.dir_ext = cpu_to_le16(ext);
2235 	req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
2236 	mutex_lock(&bp->hwrm_cmd_lock);
2237 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2238 	if (rc == 0) {
2239 		if (index)
2240 			*index = le16_to_cpu(output->dir_idx);
2241 		if (item_length)
2242 			*item_length = le32_to_cpu(output->dir_item_length);
2243 		if (data_length)
2244 			*data_length = le32_to_cpu(output->dir_data_length);
2245 	}
2246 	mutex_unlock(&bp->hwrm_cmd_lock);
2247 	return rc;
2248 }
2249 
2250 static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
2251 {
2252 	char	*retval = NULL;
2253 	char	*p;
2254 	char	*value;
2255 	int	field = 0;
2256 
2257 	if (datalen < 1)
2258 		return NULL;
2259 	/* null-terminate the log data (removing last '\n'): */
2260 	data[datalen - 1] = 0;
2261 	for (p = data; *p != 0; p++) {
2262 		field = 0;
2263 		retval = NULL;
2264 		while (*p != 0 && *p != '\n') {
2265 			value = p;
2266 			while (*p != 0 && *p != '\t' && *p != '\n')
2267 				p++;
2268 			if (field == desired_field)
2269 				retval = value;
2270 			if (*p != '\t')
2271 				break;
2272 			*p = 0;
2273 			field++;
2274 			p++;
2275 		}
2276 		if (*p == 0)
2277 			break;
2278 		*p = 0;
2279 	}
2280 	return retval;
2281 }
2282 
2283 static void bnxt_get_pkgver(struct net_device *dev)
2284 {
2285 	struct bnxt *bp = netdev_priv(dev);
2286 	u16 index = 0;
2287 	char *pkgver;
2288 	u32 pkglen;
2289 	u8 *pkgbuf;
2290 	int len;
2291 
2292 	if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
2293 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
2294 				 &index, NULL, &pkglen) != 0)
2295 		return;
2296 
2297 	pkgbuf = kzalloc(pkglen, GFP_KERNEL);
2298 	if (!pkgbuf) {
2299 		dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
2300 			pkglen);
2301 		return;
2302 	}
2303 
2304 	if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
2305 		goto err;
2306 
2307 	pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2308 				   pkglen);
2309 	if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2310 		len = strlen(bp->fw_ver_str);
2311 		snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2312 			 "/pkg %s", pkgver);
2313 	}
2314 err:
2315 	kfree(pkgbuf);
2316 }
2317 
2318 static int bnxt_get_eeprom(struct net_device *dev,
2319 			   struct ethtool_eeprom *eeprom,
2320 			   u8 *data)
2321 {
2322 	u32 index;
2323 	u32 offset;
2324 
2325 	if (eeprom->offset == 0) /* special offset value to get directory */
2326 		return bnxt_get_nvram_directory(dev, eeprom->len, data);
2327 
2328 	index = eeprom->offset >> 24;
2329 	offset = eeprom->offset & 0xffffff;
2330 
2331 	if (index == 0) {
2332 		netdev_err(dev, "unsupported index value: %d\n", index);
2333 		return -EINVAL;
2334 	}
2335 
2336 	return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2337 }
2338 
2339 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2340 {
2341 	struct bnxt *bp = netdev_priv(dev);
2342 	struct hwrm_nvm_erase_dir_entry_input req = {0};
2343 
2344 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2345 	req.dir_idx = cpu_to_le16(index);
2346 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2347 }
2348 
2349 static int bnxt_set_eeprom(struct net_device *dev,
2350 			   struct ethtool_eeprom *eeprom,
2351 			   u8 *data)
2352 {
2353 	struct bnxt *bp = netdev_priv(dev);
2354 	u8 index, dir_op;
2355 	u16 type, ext, ordinal, attr;
2356 
2357 	if (!BNXT_PF(bp)) {
2358 		netdev_err(dev, "NVM write not supported from a virtual function\n");
2359 		return -EINVAL;
2360 	}
2361 
2362 	type = eeprom->magic >> 16;
2363 
2364 	if (type == 0xffff) { /* special value for directory operations */
2365 		index = eeprom->magic & 0xff;
2366 		dir_op = eeprom->magic >> 8;
2367 		if (index == 0)
2368 			return -EINVAL;
2369 		switch (dir_op) {
2370 		case 0x0e: /* erase */
2371 			if (eeprom->offset != ~eeprom->magic)
2372 				return -EINVAL;
2373 			return bnxt_erase_nvram_directory(dev, index - 1);
2374 		default:
2375 			return -EINVAL;
2376 		}
2377 	}
2378 
2379 	/* Create or re-write an NVM item: */
2380 	if (bnxt_dir_type_is_executable(type) == true)
2381 		return -EOPNOTSUPP;
2382 	ext = eeprom->magic & 0xffff;
2383 	ordinal = eeprom->offset >> 16;
2384 	attr = eeprom->offset & 0xffff;
2385 
2386 	return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2387 				eeprom->len);
2388 }
2389 
2390 static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
2391 {
2392 	struct bnxt *bp = netdev_priv(dev);
2393 	struct ethtool_eee *eee = &bp->eee;
2394 	struct bnxt_link_info *link_info = &bp->link_info;
2395 	u32 advertising =
2396 		 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
2397 	int rc = 0;
2398 
2399 	if (!BNXT_PHY_CFG_ABLE(bp))
2400 		return -EOPNOTSUPP;
2401 
2402 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
2403 		return -EOPNOTSUPP;
2404 
2405 	if (!edata->eee_enabled)
2406 		goto eee_ok;
2407 
2408 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
2409 		netdev_warn(dev, "EEE requires autoneg\n");
2410 		return -EINVAL;
2411 	}
2412 	if (edata->tx_lpi_enabled) {
2413 		if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
2414 				       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
2415 			netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
2416 				    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
2417 			return -EINVAL;
2418 		} else if (!bp->lpi_tmr_hi) {
2419 			edata->tx_lpi_timer = eee->tx_lpi_timer;
2420 		}
2421 	}
2422 	if (!edata->advertised) {
2423 		edata->advertised = advertising & eee->supported;
2424 	} else if (edata->advertised & ~advertising) {
2425 		netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
2426 			    edata->advertised, advertising);
2427 		return -EINVAL;
2428 	}
2429 
2430 	eee->advertised = edata->advertised;
2431 	eee->tx_lpi_enabled = edata->tx_lpi_enabled;
2432 	eee->tx_lpi_timer = edata->tx_lpi_timer;
2433 eee_ok:
2434 	eee->eee_enabled = edata->eee_enabled;
2435 
2436 	if (netif_running(dev))
2437 		rc = bnxt_hwrm_set_link_setting(bp, false, true);
2438 
2439 	return rc;
2440 }
2441 
2442 static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
2443 {
2444 	struct bnxt *bp = netdev_priv(dev);
2445 
2446 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
2447 		return -EOPNOTSUPP;
2448 
2449 	*edata = bp->eee;
2450 	if (!bp->eee.eee_enabled) {
2451 		/* Preserve tx_lpi_timer so that the last value will be used
2452 		 * by default when it is re-enabled.
2453 		 */
2454 		edata->advertised = 0;
2455 		edata->tx_lpi_enabled = 0;
2456 	}
2457 
2458 	if (!bp->eee.eee_active)
2459 		edata->lp_advertised = 0;
2460 
2461 	return 0;
2462 }
2463 
2464 static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
2465 					    u16 page_number, u16 start_addr,
2466 					    u16 data_length, u8 *buf)
2467 {
2468 	struct hwrm_port_phy_i2c_read_input req = {0};
2469 	struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
2470 	int rc, byte_offset = 0;
2471 
2472 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
2473 	req.i2c_slave_addr = i2c_addr;
2474 	req.page_number = cpu_to_le16(page_number);
2475 	req.port_id = cpu_to_le16(bp->pf.port_id);
2476 	do {
2477 		u16 xfer_size;
2478 
2479 		xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
2480 		data_length -= xfer_size;
2481 		req.page_offset = cpu_to_le16(start_addr + byte_offset);
2482 		req.data_length = xfer_size;
2483 		req.enables = cpu_to_le32(start_addr + byte_offset ?
2484 				 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
2485 		mutex_lock(&bp->hwrm_cmd_lock);
2486 		rc = _hwrm_send_message(bp, &req, sizeof(req),
2487 					HWRM_CMD_TIMEOUT);
2488 		if (!rc)
2489 			memcpy(buf + byte_offset, output->data, xfer_size);
2490 		mutex_unlock(&bp->hwrm_cmd_lock);
2491 		byte_offset += xfer_size;
2492 	} while (!rc && data_length > 0);
2493 
2494 	return rc;
2495 }
2496 
2497 static int bnxt_get_module_info(struct net_device *dev,
2498 				struct ethtool_modinfo *modinfo)
2499 {
2500 	u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
2501 	struct bnxt *bp = netdev_priv(dev);
2502 	int rc;
2503 
2504 	/* No point in going further if phy status indicates
2505 	 * module is not inserted or if it is powered down or
2506 	 * if it is of type 10GBase-T
2507 	 */
2508 	if (bp->link_info.module_status >
2509 		PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
2510 		return -EOPNOTSUPP;
2511 
2512 	/* This feature is not supported in older firmware versions */
2513 	if (bp->hwrm_spec_code < 0x10202)
2514 		return -EOPNOTSUPP;
2515 
2516 	rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
2517 					      SFF_DIAG_SUPPORT_OFFSET + 1,
2518 					      data);
2519 	if (!rc) {
2520 		u8 module_id = data[0];
2521 		u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
2522 
2523 		switch (module_id) {
2524 		case SFF_MODULE_ID_SFP:
2525 			modinfo->type = ETH_MODULE_SFF_8472;
2526 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2527 			if (!diag_supported)
2528 				modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2529 			break;
2530 		case SFF_MODULE_ID_QSFP:
2531 		case SFF_MODULE_ID_QSFP_PLUS:
2532 			modinfo->type = ETH_MODULE_SFF_8436;
2533 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2534 			break;
2535 		case SFF_MODULE_ID_QSFP28:
2536 			modinfo->type = ETH_MODULE_SFF_8636;
2537 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
2538 			break;
2539 		default:
2540 			rc = -EOPNOTSUPP;
2541 			break;
2542 		}
2543 	}
2544 	return rc;
2545 }
2546 
2547 static int bnxt_get_module_eeprom(struct net_device *dev,
2548 				  struct ethtool_eeprom *eeprom,
2549 				  u8 *data)
2550 {
2551 	struct bnxt *bp = netdev_priv(dev);
2552 	u16  start = eeprom->offset, length = eeprom->len;
2553 	int rc = 0;
2554 
2555 	memset(data, 0, eeprom->len);
2556 
2557 	/* Read A0 portion of the EEPROM */
2558 	if (start < ETH_MODULE_SFF_8436_LEN) {
2559 		if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
2560 			length = ETH_MODULE_SFF_8436_LEN - start;
2561 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
2562 						      start, length, data);
2563 		if (rc)
2564 			return rc;
2565 		start += length;
2566 		data += length;
2567 		length = eeprom->len - length;
2568 	}
2569 
2570 	/* Read A2 portion of the EEPROM */
2571 	if (length) {
2572 		start -= ETH_MODULE_SFF_8436_LEN;
2573 		rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1,
2574 						      start, length, data);
2575 	}
2576 	return rc;
2577 }
2578 
2579 static int bnxt_nway_reset(struct net_device *dev)
2580 {
2581 	int rc = 0;
2582 
2583 	struct bnxt *bp = netdev_priv(dev);
2584 	struct bnxt_link_info *link_info = &bp->link_info;
2585 
2586 	if (!BNXT_PHY_CFG_ABLE(bp))
2587 		return -EOPNOTSUPP;
2588 
2589 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2590 		return -EINVAL;
2591 
2592 	if (netif_running(dev))
2593 		rc = bnxt_hwrm_set_link_setting(bp, true, false);
2594 
2595 	return rc;
2596 }
2597 
2598 static int bnxt_set_phys_id(struct net_device *dev,
2599 			    enum ethtool_phys_id_state state)
2600 {
2601 	struct hwrm_port_led_cfg_input req = {0};
2602 	struct bnxt *bp = netdev_priv(dev);
2603 	struct bnxt_pf_info *pf = &bp->pf;
2604 	struct bnxt_led_cfg *led_cfg;
2605 	u8 led_state;
2606 	__le16 duration;
2607 	int i;
2608 
2609 	if (!bp->num_leds || BNXT_VF(bp))
2610 		return -EOPNOTSUPP;
2611 
2612 	if (state == ETHTOOL_ID_ACTIVE) {
2613 		led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
2614 		duration = cpu_to_le16(500);
2615 	} else if (state == ETHTOOL_ID_INACTIVE) {
2616 		led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
2617 		duration = cpu_to_le16(0);
2618 	} else {
2619 		return -EINVAL;
2620 	}
2621 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
2622 	req.port_id = cpu_to_le16(pf->port_id);
2623 	req.num_leds = bp->num_leds;
2624 	led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
2625 	for (i = 0; i < bp->num_leds; i++, led_cfg++) {
2626 		req.enables |= BNXT_LED_DFLT_ENABLES(i);
2627 		led_cfg->led_id = bp->leds[i].led_id;
2628 		led_cfg->led_state = led_state;
2629 		led_cfg->led_blink_on = duration;
2630 		led_cfg->led_blink_off = duration;
2631 		led_cfg->led_group_id = bp->leds[i].led_group_id;
2632 	}
2633 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2634 }
2635 
2636 static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
2637 {
2638 	struct hwrm_selftest_irq_input req = {0};
2639 
2640 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
2641 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2642 }
2643 
2644 static int bnxt_test_irq(struct bnxt *bp)
2645 {
2646 	int i;
2647 
2648 	for (i = 0; i < bp->cp_nr_rings; i++) {
2649 		u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
2650 		int rc;
2651 
2652 		rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
2653 		if (rc)
2654 			return rc;
2655 	}
2656 	return 0;
2657 }
2658 
2659 static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
2660 {
2661 	struct hwrm_port_mac_cfg_input req = {0};
2662 
2663 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
2664 
2665 	req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
2666 	if (enable)
2667 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
2668 	else
2669 		req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
2670 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2671 }
2672 
2673 static int bnxt_query_force_speeds(struct bnxt *bp, u16 *force_speeds)
2674 {
2675 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
2676 	struct hwrm_port_phy_qcaps_input req = {0};
2677 	int rc;
2678 
2679 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
2680 	mutex_lock(&bp->hwrm_cmd_lock);
2681 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2682 	if (!rc)
2683 		*force_speeds = le16_to_cpu(resp->supported_speeds_force_mode);
2684 
2685 	mutex_unlock(&bp->hwrm_cmd_lock);
2686 	return rc;
2687 }
2688 
2689 static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
2690 				    struct hwrm_port_phy_cfg_input *req)
2691 {
2692 	struct bnxt_link_info *link_info = &bp->link_info;
2693 	u16 fw_advertising;
2694 	u16 fw_speed;
2695 	int rc;
2696 
2697 	if (!link_info->autoneg ||
2698 	    (bp->test_info->flags & BNXT_TEST_FL_AN_PHY_LPBK))
2699 		return 0;
2700 
2701 	rc = bnxt_query_force_speeds(bp, &fw_advertising);
2702 	if (rc)
2703 		return rc;
2704 
2705 	fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
2706 	if (bp->link_info.link_up)
2707 		fw_speed = bp->link_info.link_speed;
2708 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
2709 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
2710 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
2711 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
2712 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
2713 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
2714 	else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
2715 		fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
2716 
2717 	req->force_link_speed = cpu_to_le16(fw_speed);
2718 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
2719 				  PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
2720 	rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
2721 	req->flags = 0;
2722 	req->force_link_speed = cpu_to_le16(0);
2723 	return rc;
2724 }
2725 
2726 static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable, bool ext)
2727 {
2728 	struct hwrm_port_phy_cfg_input req = {0};
2729 
2730 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
2731 
2732 	if (enable) {
2733 		bnxt_disable_an_for_lpbk(bp, &req);
2734 		if (ext)
2735 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_EXTERNAL;
2736 		else
2737 			req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
2738 	} else {
2739 		req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
2740 	}
2741 	req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
2742 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2743 }
2744 
2745 static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2746 			    u32 raw_cons, int pkt_size)
2747 {
2748 	struct bnxt_napi *bnapi = cpr->bnapi;
2749 	struct bnxt_rx_ring_info *rxr;
2750 	struct bnxt_sw_rx_bd *rx_buf;
2751 	struct rx_cmp *rxcmp;
2752 	u16 cp_cons, cons;
2753 	u8 *data;
2754 	u32 len;
2755 	int i;
2756 
2757 	rxr = bnapi->rx_ring;
2758 	cp_cons = RING_CMP(raw_cons);
2759 	rxcmp = (struct rx_cmp *)
2760 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2761 	cons = rxcmp->rx_cmp_opaque;
2762 	rx_buf = &rxr->rx_buf_ring[cons];
2763 	data = rx_buf->data_ptr;
2764 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
2765 	if (len != pkt_size)
2766 		return -EIO;
2767 	i = ETH_ALEN;
2768 	if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
2769 		return -EIO;
2770 	i += ETH_ALEN;
2771 	for (  ; i < pkt_size; i++) {
2772 		if (data[i] != (u8)(i & 0xff))
2773 			return -EIO;
2774 	}
2775 	return 0;
2776 }
2777 
2778 static int bnxt_poll_loopback(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2779 			      int pkt_size)
2780 {
2781 	struct tx_cmp *txcmp;
2782 	int rc = -EIO;
2783 	u32 raw_cons;
2784 	u32 cons;
2785 	int i;
2786 
2787 	raw_cons = cpr->cp_raw_cons;
2788 	for (i = 0; i < 200; i++) {
2789 		cons = RING_CMP(raw_cons);
2790 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2791 
2792 		if (!TX_CMP_VALID(txcmp, raw_cons)) {
2793 			udelay(5);
2794 			continue;
2795 		}
2796 
2797 		/* The valid test of the entry must be done first before
2798 		 * reading any further.
2799 		 */
2800 		dma_rmb();
2801 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
2802 			rc = bnxt_rx_loopback(bp, cpr, raw_cons, pkt_size);
2803 			raw_cons = NEXT_RAW_CMP(raw_cons);
2804 			raw_cons = NEXT_RAW_CMP(raw_cons);
2805 			break;
2806 		}
2807 		raw_cons = NEXT_RAW_CMP(raw_cons);
2808 	}
2809 	cpr->cp_raw_cons = raw_cons;
2810 	return rc;
2811 }
2812 
2813 static int bnxt_run_loopback(struct bnxt *bp)
2814 {
2815 	struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
2816 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
2817 	struct bnxt_cp_ring_info *cpr;
2818 	int pkt_size, i = 0;
2819 	struct sk_buff *skb;
2820 	dma_addr_t map;
2821 	u8 *data;
2822 	int rc;
2823 
2824 	cpr = &rxr->bnapi->cp_ring;
2825 	if (bp->flags & BNXT_FLAG_CHIP_P5)
2826 		cpr = cpr->cp_ring_arr[BNXT_RX_HDL];
2827 	pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
2828 	skb = netdev_alloc_skb(bp->dev, pkt_size);
2829 	if (!skb)
2830 		return -ENOMEM;
2831 	data = skb_put(skb, pkt_size);
2832 	eth_broadcast_addr(data);
2833 	i += ETH_ALEN;
2834 	ether_addr_copy(&data[i], bp->dev->dev_addr);
2835 	i += ETH_ALEN;
2836 	for ( ; i < pkt_size; i++)
2837 		data[i] = (u8)(i & 0xff);
2838 
2839 	map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
2840 			     PCI_DMA_TODEVICE);
2841 	if (dma_mapping_error(&bp->pdev->dev, map)) {
2842 		dev_kfree_skb(skb);
2843 		return -EIO;
2844 	}
2845 	bnxt_xmit_bd(bp, txr, map, pkt_size);
2846 
2847 	/* Sync BD data before updating doorbell */
2848 	wmb();
2849 
2850 	bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
2851 	rc = bnxt_poll_loopback(bp, cpr, pkt_size);
2852 
2853 	dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
2854 	dev_kfree_skb(skb);
2855 	return rc;
2856 }
2857 
2858 static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
2859 {
2860 	struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
2861 	struct hwrm_selftest_exec_input req = {0};
2862 	int rc;
2863 
2864 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
2865 	mutex_lock(&bp->hwrm_cmd_lock);
2866 	resp->test_success = 0;
2867 	req.flags = test_mask;
2868 	rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
2869 	*test_results = resp->test_success;
2870 	mutex_unlock(&bp->hwrm_cmd_lock);
2871 	return rc;
2872 }
2873 
2874 #define BNXT_DRV_TESTS			4
2875 #define BNXT_MACLPBK_TEST_IDX		(bp->num_tests - BNXT_DRV_TESTS)
2876 #define BNXT_PHYLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 1)
2877 #define BNXT_EXTLPBK_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 2)
2878 #define BNXT_IRQ_TEST_IDX		(BNXT_MACLPBK_TEST_IDX + 3)
2879 
2880 static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
2881 			   u64 *buf)
2882 {
2883 	struct bnxt *bp = netdev_priv(dev);
2884 	bool do_ext_lpbk = false;
2885 	bool offline = false;
2886 	u8 test_results = 0;
2887 	u8 test_mask = 0;
2888 	int rc = 0, i;
2889 
2890 	if (!bp->num_tests || !BNXT_SINGLE_PF(bp))
2891 		return;
2892 	memset(buf, 0, sizeof(u64) * bp->num_tests);
2893 	if (!netif_running(dev)) {
2894 		etest->flags |= ETH_TEST_FL_FAILED;
2895 		return;
2896 	}
2897 
2898 	if ((etest->flags & ETH_TEST_FL_EXTERNAL_LB) &&
2899 	    (bp->test_info->flags & BNXT_TEST_FL_EXT_LPBK))
2900 		do_ext_lpbk = true;
2901 
2902 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
2903 		if (bp->pf.active_vfs) {
2904 			etest->flags |= ETH_TEST_FL_FAILED;
2905 			netdev_warn(dev, "Offline tests cannot be run with active VFs\n");
2906 			return;
2907 		}
2908 		offline = true;
2909 	}
2910 
2911 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2912 		u8 bit_val = 1 << i;
2913 
2914 		if (!(bp->test_info->offline_mask & bit_val))
2915 			test_mask |= bit_val;
2916 		else if (offline)
2917 			test_mask |= bit_val;
2918 	}
2919 	if (!offline) {
2920 		bnxt_run_fw_tests(bp, test_mask, &test_results);
2921 	} else {
2922 		rc = bnxt_close_nic(bp, false, false);
2923 		if (rc)
2924 			return;
2925 		bnxt_run_fw_tests(bp, test_mask, &test_results);
2926 
2927 		buf[BNXT_MACLPBK_TEST_IDX] = 1;
2928 		bnxt_hwrm_mac_loopback(bp, true);
2929 		msleep(250);
2930 		rc = bnxt_half_open_nic(bp);
2931 		if (rc) {
2932 			bnxt_hwrm_mac_loopback(bp, false);
2933 			etest->flags |= ETH_TEST_FL_FAILED;
2934 			return;
2935 		}
2936 		if (bnxt_run_loopback(bp))
2937 			etest->flags |= ETH_TEST_FL_FAILED;
2938 		else
2939 			buf[BNXT_MACLPBK_TEST_IDX] = 0;
2940 
2941 		bnxt_hwrm_mac_loopback(bp, false);
2942 		bnxt_hwrm_phy_loopback(bp, true, false);
2943 		msleep(1000);
2944 		if (bnxt_run_loopback(bp)) {
2945 			buf[BNXT_PHYLPBK_TEST_IDX] = 1;
2946 			etest->flags |= ETH_TEST_FL_FAILED;
2947 		}
2948 		if (do_ext_lpbk) {
2949 			etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
2950 			bnxt_hwrm_phy_loopback(bp, true, true);
2951 			msleep(1000);
2952 			if (bnxt_run_loopback(bp)) {
2953 				buf[BNXT_EXTLPBK_TEST_IDX] = 1;
2954 				etest->flags |= ETH_TEST_FL_FAILED;
2955 			}
2956 		}
2957 		bnxt_hwrm_phy_loopback(bp, false, false);
2958 		bnxt_half_close_nic(bp);
2959 		rc = bnxt_open_nic(bp, false, true);
2960 	}
2961 	if (rc || bnxt_test_irq(bp)) {
2962 		buf[BNXT_IRQ_TEST_IDX] = 1;
2963 		etest->flags |= ETH_TEST_FL_FAILED;
2964 	}
2965 	for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2966 		u8 bit_val = 1 << i;
2967 
2968 		if ((test_mask & bit_val) && !(test_results & bit_val)) {
2969 			buf[i] = 1;
2970 			etest->flags |= ETH_TEST_FL_FAILED;
2971 		}
2972 	}
2973 }
2974 
2975 static int bnxt_reset(struct net_device *dev, u32 *flags)
2976 {
2977 	struct bnxt *bp = netdev_priv(dev);
2978 	int rc = 0;
2979 
2980 	if (!BNXT_PF(bp)) {
2981 		netdev_err(dev, "Reset is not supported from a VF\n");
2982 		return -EOPNOTSUPP;
2983 	}
2984 
2985 	if (pci_vfs_assigned(bp->pdev) &&
2986 	    !(bp->fw_cap & BNXT_FW_CAP_HOT_RESET)) {
2987 		netdev_err(dev,
2988 			   "Reset not allowed when VFs are assigned to VMs\n");
2989 		return -EBUSY;
2990 	}
2991 
2992 	if (*flags == ETH_RESET_ALL) {
2993 		/* This feature is not supported in older firmware versions */
2994 		if (bp->hwrm_spec_code < 0x10803)
2995 			return -EOPNOTSUPP;
2996 
2997 		rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_CHIP);
2998 		if (!rc) {
2999 			netdev_info(dev, "Reset request successful.\n");
3000 			if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET))
3001 				netdev_info(dev, "Reload driver to complete reset\n");
3002 			*flags = 0;
3003 		}
3004 	} else if (*flags == ETH_RESET_AP) {
3005 		/* This feature is not supported in older firmware versions */
3006 		if (bp->hwrm_spec_code < 0x10803)
3007 			return -EOPNOTSUPP;
3008 
3009 		rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_AP);
3010 		if (!rc) {
3011 			netdev_info(dev, "Reset Application Processor request successful.\n");
3012 			*flags = 0;
3013 		}
3014 	} else {
3015 		rc = -EINVAL;
3016 	}
3017 
3018 	return rc;
3019 }
3020 
3021 static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg, int msg_len,
3022 				  struct bnxt_hwrm_dbg_dma_info *info)
3023 {
3024 	struct hwrm_dbg_cmn_output *cmn_resp = bp->hwrm_cmd_resp_addr;
3025 	struct hwrm_dbg_cmn_input *cmn_req = msg;
3026 	__le16 *seq_ptr = msg + info->seq_off;
3027 	u16 seq = 0, len, segs_off;
3028 	void *resp = cmn_resp;
3029 	dma_addr_t dma_handle;
3030 	int rc, off = 0;
3031 	void *dma_buf;
3032 
3033 	dma_buf = dma_alloc_coherent(&bp->pdev->dev, info->dma_len, &dma_handle,
3034 				     GFP_KERNEL);
3035 	if (!dma_buf)
3036 		return -ENOMEM;
3037 
3038 	segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
3039 			    total_segments);
3040 	cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
3041 	cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
3042 	mutex_lock(&bp->hwrm_cmd_lock);
3043 	while (1) {
3044 		*seq_ptr = cpu_to_le16(seq);
3045 		rc = _hwrm_send_message(bp, msg, msg_len,
3046 					HWRM_COREDUMP_TIMEOUT);
3047 		if (rc)
3048 			break;
3049 
3050 		len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
3051 		if (!seq &&
3052 		    cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
3053 			info->segs = le16_to_cpu(*((__le16 *)(resp +
3054 							      segs_off)));
3055 			if (!info->segs) {
3056 				rc = -EIO;
3057 				break;
3058 			}
3059 
3060 			info->dest_buf_size = info->segs *
3061 					sizeof(struct coredump_segment_record);
3062 			info->dest_buf = kmalloc(info->dest_buf_size,
3063 						 GFP_KERNEL);
3064 			if (!info->dest_buf) {
3065 				rc = -ENOMEM;
3066 				break;
3067 			}
3068 		}
3069 
3070 		if (info->dest_buf) {
3071 			if ((info->seg_start + off + len) <=
3072 			    BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
3073 				memcpy(info->dest_buf + off, dma_buf, len);
3074 			} else {
3075 				rc = -ENOBUFS;
3076 				break;
3077 			}
3078 		}
3079 
3080 		if (cmn_req->req_type ==
3081 				cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
3082 			info->dest_buf_size += len;
3083 
3084 		if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
3085 			break;
3086 
3087 		seq++;
3088 		off += len;
3089 	}
3090 	mutex_unlock(&bp->hwrm_cmd_lock);
3091 	dma_free_coherent(&bp->pdev->dev, info->dma_len, dma_buf, dma_handle);
3092 	return rc;
3093 }
3094 
3095 static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
3096 				       struct bnxt_coredump *coredump)
3097 {
3098 	struct hwrm_dbg_coredump_list_input req = {0};
3099 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
3100 	int rc;
3101 
3102 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_LIST, -1, -1);
3103 
3104 	info.dma_len = COREDUMP_LIST_BUF_LEN;
3105 	info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
3106 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
3107 				     data_len);
3108 
3109 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
3110 	if (!rc) {
3111 		coredump->data = info.dest_buf;
3112 		coredump->data_size = info.dest_buf_size;
3113 		coredump->total_segs = info.segs;
3114 	}
3115 	return rc;
3116 }
3117 
3118 static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
3119 					   u16 segment_id)
3120 {
3121 	struct hwrm_dbg_coredump_initiate_input req = {0};
3122 
3123 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_INITIATE, -1, -1);
3124 	req.component_id = cpu_to_le16(component_id);
3125 	req.segment_id = cpu_to_le16(segment_id);
3126 
3127 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_COREDUMP_TIMEOUT);
3128 }
3129 
3130 static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
3131 					   u16 segment_id, u32 *seg_len,
3132 					   void *buf, u32 buf_len, u32 offset)
3133 {
3134 	struct hwrm_dbg_coredump_retrieve_input req = {0};
3135 	struct bnxt_hwrm_dbg_dma_info info = {NULL};
3136 	int rc;
3137 
3138 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_COREDUMP_RETRIEVE, -1, -1);
3139 	req.component_id = cpu_to_le16(component_id);
3140 	req.segment_id = cpu_to_le16(segment_id);
3141 
3142 	info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
3143 	info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
3144 				seq_no);
3145 	info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
3146 				     data_len);
3147 	if (buf) {
3148 		info.dest_buf = buf + offset;
3149 		info.buf_len = buf_len;
3150 		info.seg_start = offset;
3151 	}
3152 
3153 	rc = bnxt_hwrm_dbg_dma_data(bp, &req, sizeof(req), &info);
3154 	if (!rc)
3155 		*seg_len = info.dest_buf_size;
3156 
3157 	return rc;
3158 }
3159 
3160 static void
3161 bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
3162 			   struct bnxt_coredump_segment_hdr *seg_hdr,
3163 			   struct coredump_segment_record *seg_rec, u32 seg_len,
3164 			   int status, u32 duration, u32 instance)
3165 {
3166 	memset(seg_hdr, 0, sizeof(*seg_hdr));
3167 	memcpy(seg_hdr->signature, "sEgM", 4);
3168 	if (seg_rec) {
3169 		seg_hdr->component_id = (__force __le32)seg_rec->component_id;
3170 		seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
3171 		seg_hdr->low_version = seg_rec->version_low;
3172 		seg_hdr->high_version = seg_rec->version_hi;
3173 	} else {
3174 		/* For hwrm_ver_get response Component id = 2
3175 		 * and Segment id = 0
3176 		 */
3177 		seg_hdr->component_id = cpu_to_le32(2);
3178 		seg_hdr->segment_id = 0;
3179 	}
3180 	seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
3181 	seg_hdr->length = cpu_to_le32(seg_len);
3182 	seg_hdr->status = cpu_to_le32(status);
3183 	seg_hdr->duration = cpu_to_le32(duration);
3184 	seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
3185 	seg_hdr->instance = cpu_to_le32(instance);
3186 }
3187 
3188 static void
3189 bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
3190 			  time64_t start, s16 start_utc, u16 total_segs,
3191 			  int status)
3192 {
3193 	time64_t end = ktime_get_real_seconds();
3194 	u32 os_ver_major = 0, os_ver_minor = 0;
3195 	struct tm tm;
3196 
3197 	time64_to_tm(start, 0, &tm);
3198 	memset(record, 0, sizeof(*record));
3199 	memcpy(record->signature, "cOrE", 4);
3200 	record->flags = 0;
3201 	record->low_version = 0;
3202 	record->high_version = 1;
3203 	record->asic_state = 0;
3204 	strlcpy(record->system_name, utsname()->nodename,
3205 		sizeof(record->system_name));
3206 	record->year = cpu_to_le16(tm.tm_year + 1900);
3207 	record->month = cpu_to_le16(tm.tm_mon + 1);
3208 	record->day = cpu_to_le16(tm.tm_mday);
3209 	record->hour = cpu_to_le16(tm.tm_hour);
3210 	record->minute = cpu_to_le16(tm.tm_min);
3211 	record->second = cpu_to_le16(tm.tm_sec);
3212 	record->utc_bias = cpu_to_le16(start_utc);
3213 	strcpy(record->commandline, "ethtool -w");
3214 	record->total_segments = cpu_to_le32(total_segs);
3215 
3216 	sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor);
3217 	record->os_ver_major = cpu_to_le32(os_ver_major);
3218 	record->os_ver_minor = cpu_to_le32(os_ver_minor);
3219 
3220 	strlcpy(record->os_name, utsname()->sysname, 32);
3221 	time64_to_tm(end, 0, &tm);
3222 	record->end_year = cpu_to_le16(tm.tm_year + 1900);
3223 	record->end_month = cpu_to_le16(tm.tm_mon + 1);
3224 	record->end_day = cpu_to_le16(tm.tm_mday);
3225 	record->end_hour = cpu_to_le16(tm.tm_hour);
3226 	record->end_minute = cpu_to_le16(tm.tm_min);
3227 	record->end_second = cpu_to_le16(tm.tm_sec);
3228 	record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
3229 	record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
3230 				       bp->ver_resp.chip_rev << 8 |
3231 				       bp->ver_resp.chip_metal);
3232 	record->asic_id2 = 0;
3233 	record->coredump_status = cpu_to_le32(status);
3234 	record->ioctl_low_version = 0;
3235 	record->ioctl_high_version = 0;
3236 }
3237 
3238 static int bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
3239 {
3240 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
3241 	u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
3242 	struct coredump_segment_record *seg_record = NULL;
3243 	struct bnxt_coredump_segment_hdr seg_hdr;
3244 	struct bnxt_coredump coredump = {NULL};
3245 	time64_t start_time;
3246 	u16 start_utc;
3247 	int rc = 0, i;
3248 
3249 	if (buf)
3250 		buf_len = *dump_len;
3251 
3252 	start_time = ktime_get_real_seconds();
3253 	start_utc = sys_tz.tz_minuteswest * 60;
3254 	seg_hdr_len = sizeof(seg_hdr);
3255 
3256 	/* First segment should be hwrm_ver_get response */
3257 	*dump_len = seg_hdr_len + ver_get_resp_len;
3258 	if (buf) {
3259 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
3260 					   0, 0, 0);
3261 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
3262 		offset += seg_hdr_len;
3263 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
3264 		offset += ver_get_resp_len;
3265 	}
3266 
3267 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
3268 	if (rc) {
3269 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
3270 		goto err;
3271 	}
3272 
3273 	*dump_len += seg_hdr_len * coredump.total_segs;
3274 
3275 	seg_record = (struct coredump_segment_record *)coredump.data;
3276 	seg_record_len = sizeof(*seg_record);
3277 
3278 	for (i = 0; i < coredump.total_segs; i++) {
3279 		u16 comp_id = le16_to_cpu(seg_record->component_id);
3280 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
3281 		u32 duration = 0, seg_len = 0;
3282 		unsigned long start, end;
3283 
3284 		if (buf && ((offset + seg_hdr_len) >
3285 			    BNXT_COREDUMP_BUF_LEN(buf_len))) {
3286 			rc = -ENOBUFS;
3287 			goto err;
3288 		}
3289 
3290 		start = jiffies;
3291 
3292 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
3293 		if (rc) {
3294 			netdev_err(bp->dev,
3295 				   "Failed to initiate coredump for seg = %d\n",
3296 				   seg_record->segment_id);
3297 			goto next_seg;
3298 		}
3299 
3300 		/* Write segment data into the buffer */
3301 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
3302 						     &seg_len, buf, buf_len,
3303 						     offset + seg_hdr_len);
3304 		if (rc && rc == -ENOBUFS)
3305 			goto err;
3306 		else if (rc)
3307 			netdev_err(bp->dev,
3308 				   "Failed to retrieve coredump for seg = %d\n",
3309 				   seg_record->segment_id);
3310 
3311 next_seg:
3312 		end = jiffies;
3313 		duration = jiffies_to_msecs(end - start);
3314 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
3315 					   rc, duration, 0);
3316 
3317 		if (buf) {
3318 			/* Write segment header into the buffer */
3319 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
3320 			offset += seg_hdr_len + seg_len;
3321 		}
3322 
3323 		*dump_len += seg_len;
3324 		seg_record =
3325 			(struct coredump_segment_record *)((u8 *)seg_record +
3326 							   seg_record_len);
3327 	}
3328 
3329 err:
3330 	if (buf)
3331 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
3332 					  start_utc, coredump.total_segs + 1,
3333 					  rc);
3334 	kfree(coredump.data);
3335 	*dump_len += sizeof(struct bnxt_coredump_record);
3336 	if (rc == -ENOBUFS)
3337 		netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
3338 	return rc;
3339 }
3340 
3341 static int bnxt_set_dump(struct net_device *dev, struct ethtool_dump *dump)
3342 {
3343 	struct bnxt *bp = netdev_priv(dev);
3344 
3345 	if (dump->flag > BNXT_DUMP_CRASH) {
3346 		netdev_info(dev, "Supports only Live(0) and Crash(1) dumps.\n");
3347 		return -EINVAL;
3348 	}
3349 
3350 	if (!IS_ENABLED(CONFIG_TEE_BNXT_FW) && dump->flag == BNXT_DUMP_CRASH) {
3351 		netdev_info(dev, "Cannot collect crash dump as TEE_BNXT_FW config option is not enabled.\n");
3352 		return -EOPNOTSUPP;
3353 	}
3354 
3355 	bp->dump_flag = dump->flag;
3356 	return 0;
3357 }
3358 
3359 static int bnxt_get_dump_flag(struct net_device *dev, struct ethtool_dump *dump)
3360 {
3361 	struct bnxt *bp = netdev_priv(dev);
3362 
3363 	if (bp->hwrm_spec_code < 0x10801)
3364 		return -EOPNOTSUPP;
3365 
3366 	dump->version = bp->ver_resp.hwrm_fw_maj_8b << 24 |
3367 			bp->ver_resp.hwrm_fw_min_8b << 16 |
3368 			bp->ver_resp.hwrm_fw_bld_8b << 8 |
3369 			bp->ver_resp.hwrm_fw_rsvd_8b;
3370 
3371 	dump->flag = bp->dump_flag;
3372 	if (bp->dump_flag == BNXT_DUMP_CRASH)
3373 		dump->len = BNXT_CRASH_DUMP_LEN;
3374 	else
3375 		bnxt_get_coredump(bp, NULL, &dump->len);
3376 	return 0;
3377 }
3378 
3379 static int bnxt_get_dump_data(struct net_device *dev, struct ethtool_dump *dump,
3380 			      void *buf)
3381 {
3382 	struct bnxt *bp = netdev_priv(dev);
3383 
3384 	if (bp->hwrm_spec_code < 0x10801)
3385 		return -EOPNOTSUPP;
3386 
3387 	memset(buf, 0, dump->len);
3388 
3389 	dump->flag = bp->dump_flag;
3390 	if (dump->flag == BNXT_DUMP_CRASH) {
3391 #ifdef CONFIG_TEE_BNXT_FW
3392 		return tee_bnxt_copy_coredump(buf, 0, dump->len);
3393 #endif
3394 	} else {
3395 		return bnxt_get_coredump(bp, buf, &dump->len);
3396 	}
3397 
3398 	return 0;
3399 }
3400 
3401 void bnxt_ethtool_init(struct bnxt *bp)
3402 {
3403 	struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
3404 	struct hwrm_selftest_qlist_input req = {0};
3405 	struct bnxt_test_info *test_info;
3406 	struct net_device *dev = bp->dev;
3407 	int i, rc;
3408 
3409 	if (!(bp->fw_cap & BNXT_FW_CAP_PKG_VER))
3410 		bnxt_get_pkgver(dev);
3411 
3412 	bp->num_tests = 0;
3413 	if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp))
3414 		return;
3415 
3416 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
3417 	mutex_lock(&bp->hwrm_cmd_lock);
3418 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3419 	if (rc)
3420 		goto ethtool_init_exit;
3421 
3422 	test_info = bp->test_info;
3423 	if (!test_info)
3424 		test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
3425 	if (!test_info)
3426 		goto ethtool_init_exit;
3427 
3428 	bp->test_info = test_info;
3429 	bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
3430 	if (bp->num_tests > BNXT_MAX_TEST)
3431 		bp->num_tests = BNXT_MAX_TEST;
3432 
3433 	test_info->offline_mask = resp->offline_tests;
3434 	test_info->timeout = le16_to_cpu(resp->test_timeout);
3435 	if (!test_info->timeout)
3436 		test_info->timeout = HWRM_CMD_TIMEOUT;
3437 	for (i = 0; i < bp->num_tests; i++) {
3438 		char *str = test_info->string[i];
3439 		char *fw_str = resp->test0_name + i * 32;
3440 
3441 		if (i == BNXT_MACLPBK_TEST_IDX) {
3442 			strcpy(str, "Mac loopback test (offline)");
3443 		} else if (i == BNXT_PHYLPBK_TEST_IDX) {
3444 			strcpy(str, "Phy loopback test (offline)");
3445 		} else if (i == BNXT_EXTLPBK_TEST_IDX) {
3446 			strcpy(str, "Ext loopback test (offline)");
3447 		} else if (i == BNXT_IRQ_TEST_IDX) {
3448 			strcpy(str, "Interrupt_test (offline)");
3449 		} else {
3450 			strlcpy(str, fw_str, ETH_GSTRING_LEN);
3451 			strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
3452 			if (test_info->offline_mask & (1 << i))
3453 				strncat(str, " (offline)",
3454 					ETH_GSTRING_LEN - strlen(str));
3455 			else
3456 				strncat(str, " (online)",
3457 					ETH_GSTRING_LEN - strlen(str));
3458 		}
3459 	}
3460 
3461 ethtool_init_exit:
3462 	mutex_unlock(&bp->hwrm_cmd_lock);
3463 }
3464 
3465 void bnxt_ethtool_free(struct bnxt *bp)
3466 {
3467 	kfree(bp->test_info);
3468 	bp->test_info = NULL;
3469 }
3470 
3471 const struct ethtool_ops bnxt_ethtool_ops = {
3472 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3473 				     ETHTOOL_COALESCE_MAX_FRAMES |
3474 				     ETHTOOL_COALESCE_USECS_IRQ |
3475 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
3476 				     ETHTOOL_COALESCE_STATS_BLOCK_USECS |
3477 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
3478 	.get_link_ksettings	= bnxt_get_link_ksettings,
3479 	.set_link_ksettings	= bnxt_set_link_ksettings,
3480 	.get_pauseparam		= bnxt_get_pauseparam,
3481 	.set_pauseparam		= bnxt_set_pauseparam,
3482 	.get_drvinfo		= bnxt_get_drvinfo,
3483 	.get_wol		= bnxt_get_wol,
3484 	.set_wol		= bnxt_set_wol,
3485 	.get_coalesce		= bnxt_get_coalesce,
3486 	.set_coalesce		= bnxt_set_coalesce,
3487 	.get_msglevel		= bnxt_get_msglevel,
3488 	.set_msglevel		= bnxt_set_msglevel,
3489 	.get_sset_count		= bnxt_get_sset_count,
3490 	.get_strings		= bnxt_get_strings,
3491 	.get_ethtool_stats	= bnxt_get_ethtool_stats,
3492 	.set_ringparam		= bnxt_set_ringparam,
3493 	.get_ringparam		= bnxt_get_ringparam,
3494 	.get_channels		= bnxt_get_channels,
3495 	.set_channels		= bnxt_set_channels,
3496 	.get_rxnfc		= bnxt_get_rxnfc,
3497 	.set_rxnfc		= bnxt_set_rxnfc,
3498 	.get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
3499 	.get_rxfh_key_size      = bnxt_get_rxfh_key_size,
3500 	.get_rxfh               = bnxt_get_rxfh,
3501 	.flash_device		= bnxt_flash_device,
3502 	.get_eeprom_len         = bnxt_get_eeprom_len,
3503 	.get_eeprom             = bnxt_get_eeprom,
3504 	.set_eeprom		= bnxt_set_eeprom,
3505 	.get_link		= bnxt_get_link,
3506 	.get_eee		= bnxt_get_eee,
3507 	.set_eee		= bnxt_set_eee,
3508 	.get_module_info	= bnxt_get_module_info,
3509 	.get_module_eeprom	= bnxt_get_module_eeprom,
3510 	.nway_reset		= bnxt_nway_reset,
3511 	.set_phys_id		= bnxt_set_phys_id,
3512 	.self_test		= bnxt_self_test,
3513 	.reset			= bnxt_reset,
3514 	.set_dump		= bnxt_set_dump,
3515 	.get_dump_flag		= bnxt_get_dump_flag,
3516 	.get_dump_data		= bnxt_get_dump_data,
3517 };
3518