OSDN Git Service

bnxt_en: Adjust timer based on ethtool stats-block-usecs settings.
[android-x86/kernel.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_ethtool.c
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 "bnxt_hsi.h"
20 #include "bnxt.h"
21 #include "bnxt_xdp.h"
22 #include "bnxt_ethtool.h"
23 #include "bnxt_nvm_defs.h"      /* NVRAM content constant and structure defs */
24 #include "bnxt_fw_hdr.h"        /* Firmware hdr constant and structure defs */
25 #define FLASH_NVRAM_TIMEOUT     ((HWRM_CMD_TIMEOUT) * 100)
26 #define FLASH_PACKAGE_TIMEOUT   ((HWRM_CMD_TIMEOUT) * 200)
27 #define INSTALL_PACKAGE_TIMEOUT ((HWRM_CMD_TIMEOUT) * 200)
28
29 static u32 bnxt_get_msglevel(struct net_device *dev)
30 {
31         struct bnxt *bp = netdev_priv(dev);
32
33         return bp->msg_enable;
34 }
35
36 static void bnxt_set_msglevel(struct net_device *dev, u32 value)
37 {
38         struct bnxt *bp = netdev_priv(dev);
39
40         bp->msg_enable = value;
41 }
42
43 static int bnxt_get_coalesce(struct net_device *dev,
44                              struct ethtool_coalesce *coal)
45 {
46         struct bnxt *bp = netdev_priv(dev);
47         struct bnxt_coal *hw_coal;
48         u16 mult;
49
50         memset(coal, 0, sizeof(*coal));
51
52         coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM;
53
54         hw_coal = &bp->rx_coal;
55         mult = hw_coal->bufs_per_record;
56         coal->rx_coalesce_usecs = hw_coal->coal_ticks;
57         coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult;
58         coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
59         coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
60
61         hw_coal = &bp->tx_coal;
62         mult = hw_coal->bufs_per_record;
63         coal->tx_coalesce_usecs = hw_coal->coal_ticks;
64         coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult;
65         coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq;
66         coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult;
67
68         coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
69
70         return 0;
71 }
72
73 static int bnxt_set_coalesce(struct net_device *dev,
74                              struct ethtool_coalesce *coal)
75 {
76         struct bnxt *bp = netdev_priv(dev);
77         bool update_stats = false;
78         struct bnxt_coal *hw_coal;
79         int rc = 0;
80         u16 mult;
81
82         if (coal->use_adaptive_rx_coalesce) {
83                 bp->flags |= BNXT_FLAG_DIM;
84         } else {
85                 if (bp->flags & BNXT_FLAG_DIM) {
86                         bp->flags &= ~(BNXT_FLAG_DIM);
87                         goto reset_coalesce;
88                 }
89         }
90
91         hw_coal = &bp->rx_coal;
92         mult = hw_coal->bufs_per_record;
93         hw_coal->coal_ticks = coal->rx_coalesce_usecs;
94         hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult;
95         hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq;
96         hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult;
97
98         hw_coal = &bp->tx_coal;
99         mult = hw_coal->bufs_per_record;
100         hw_coal->coal_ticks = coal->tx_coalesce_usecs;
101         hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult;
102         hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq;
103         hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult;
104
105         if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
106                 u32 stats_ticks = coal->stats_block_coalesce_usecs;
107
108                 /* Allow 0, which means disable. */
109                 if (stats_ticks)
110                         stats_ticks = clamp_t(u32, stats_ticks,
111                                               BNXT_MIN_STATS_COAL_TICKS,
112                                               BNXT_MAX_STATS_COAL_TICKS);
113                 stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
114                 bp->stats_coal_ticks = stats_ticks;
115                 if (bp->stats_coal_ticks)
116                         bp->current_interval =
117                                 bp->stats_coal_ticks * HZ / 1000000;
118                 else
119                         bp->current_interval = BNXT_TIMER_INTERVAL;
120                 update_stats = true;
121         }
122
123 reset_coalesce:
124         if (netif_running(dev)) {
125                 if (update_stats) {
126                         rc = bnxt_close_nic(bp, true, false);
127                         if (!rc)
128                                 rc = bnxt_open_nic(bp, true, false);
129                 } else {
130                         rc = bnxt_hwrm_set_coal(bp);
131                 }
132         }
133
134         return rc;
135 }
136
137 #define BNXT_NUM_STATS  21
138
139 #define BNXT_RX_STATS_ENTRY(counter)    \
140         { BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
141
142 #define BNXT_TX_STATS_ENTRY(counter)    \
143         { BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
144
145 #define BNXT_RX_STATS_EXT_ENTRY(counter)        \
146         { BNXT_RX_STATS_EXT_OFFSET(counter), __stringify(counter) }
147
148 enum {
149         RX_TOTAL_DISCARDS,
150         TX_TOTAL_DISCARDS,
151 };
152
153 static struct {
154         u64                     counter;
155         char                    string[ETH_GSTRING_LEN];
156 } bnxt_sw_func_stats[] = {
157         {0, "rx_total_discard_pkts"},
158         {0, "tx_total_discard_pkts"},
159 };
160
161 static const struct {
162         long offset;
163         char string[ETH_GSTRING_LEN];
164 } bnxt_port_stats_arr[] = {
165         BNXT_RX_STATS_ENTRY(rx_64b_frames),
166         BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
167         BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
168         BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
169         BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
170         BNXT_RX_STATS_ENTRY(rx_1024b_1518b_frames),
171         BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
172         BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
173         BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
174         BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
175         BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
176         BNXT_RX_STATS_ENTRY(rx_total_frames),
177         BNXT_RX_STATS_ENTRY(rx_ucast_frames),
178         BNXT_RX_STATS_ENTRY(rx_mcast_frames),
179         BNXT_RX_STATS_ENTRY(rx_bcast_frames),
180         BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
181         BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
182         BNXT_RX_STATS_ENTRY(rx_pause_frames),
183         BNXT_RX_STATS_ENTRY(rx_pfc_frames),
184         BNXT_RX_STATS_ENTRY(rx_align_err_frames),
185         BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
186         BNXT_RX_STATS_ENTRY(rx_jbr_frames),
187         BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
188         BNXT_RX_STATS_ENTRY(rx_tagged_frames),
189         BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
190         BNXT_RX_STATS_ENTRY(rx_good_frames),
191         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
192         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
193         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
194         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
195         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
196         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
197         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
198         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
199         BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
200         BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
201         BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
202         BNXT_RX_STATS_ENTRY(rx_bytes),
203         BNXT_RX_STATS_ENTRY(rx_runt_bytes),
204         BNXT_RX_STATS_ENTRY(rx_runt_frames),
205         BNXT_RX_STATS_ENTRY(rx_stat_discard),
206         BNXT_RX_STATS_ENTRY(rx_stat_err),
207
208         BNXT_TX_STATS_ENTRY(tx_64b_frames),
209         BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
210         BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
211         BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
212         BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
213         BNXT_TX_STATS_ENTRY(tx_1024b_1518b_frames),
214         BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
215         BNXT_TX_STATS_ENTRY(tx_1519b_2047b_frames),
216         BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
217         BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
218         BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
219         BNXT_TX_STATS_ENTRY(tx_good_frames),
220         BNXT_TX_STATS_ENTRY(tx_total_frames),
221         BNXT_TX_STATS_ENTRY(tx_ucast_frames),
222         BNXT_TX_STATS_ENTRY(tx_mcast_frames),
223         BNXT_TX_STATS_ENTRY(tx_bcast_frames),
224         BNXT_TX_STATS_ENTRY(tx_pause_frames),
225         BNXT_TX_STATS_ENTRY(tx_pfc_frames),
226         BNXT_TX_STATS_ENTRY(tx_jabber_frames),
227         BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
228         BNXT_TX_STATS_ENTRY(tx_err),
229         BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
230         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
231         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
232         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
233         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
234         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
235         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
236         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
237         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
238         BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
239         BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
240         BNXT_TX_STATS_ENTRY(tx_total_collisions),
241         BNXT_TX_STATS_ENTRY(tx_bytes),
242         BNXT_TX_STATS_ENTRY(tx_xthol_frames),
243         BNXT_TX_STATS_ENTRY(tx_stat_discard),
244         BNXT_TX_STATS_ENTRY(tx_stat_error),
245 };
246
247 static const struct {
248         long offset;
249         char string[ETH_GSTRING_LEN];
250 } bnxt_port_stats_ext_arr[] = {
251         BNXT_RX_STATS_EXT_ENTRY(link_down_events),
252         BNXT_RX_STATS_EXT_ENTRY(continuous_pause_events),
253         BNXT_RX_STATS_EXT_ENTRY(resume_pause_events),
254         BNXT_RX_STATS_EXT_ENTRY(continuous_roce_pause_events),
255         BNXT_RX_STATS_EXT_ENTRY(resume_roce_pause_events),
256 };
257
258 #define BNXT_NUM_SW_FUNC_STATS  ARRAY_SIZE(bnxt_sw_func_stats)
259 #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
260 #define BNXT_NUM_PORT_STATS_EXT ARRAY_SIZE(bnxt_port_stats_ext_arr)
261
262 static int bnxt_get_num_stats(struct bnxt *bp)
263 {
264         int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
265
266         num_stats += BNXT_NUM_SW_FUNC_STATS;
267
268         if (bp->flags & BNXT_FLAG_PORT_STATS)
269                 num_stats += BNXT_NUM_PORT_STATS;
270
271         if (bp->flags & BNXT_FLAG_PORT_STATS_EXT)
272                 num_stats += BNXT_NUM_PORT_STATS_EXT;
273
274         return num_stats;
275 }
276
277 static int bnxt_get_sset_count(struct net_device *dev, int sset)
278 {
279         struct bnxt *bp = netdev_priv(dev);
280
281         switch (sset) {
282         case ETH_SS_STATS:
283                 return bnxt_get_num_stats(bp);
284         case ETH_SS_TEST:
285                 if (!bp->num_tests)
286                         return -EOPNOTSUPP;
287                 return bp->num_tests;
288         default:
289                 return -EOPNOTSUPP;
290         }
291 }
292
293 static void bnxt_get_ethtool_stats(struct net_device *dev,
294                                    struct ethtool_stats *stats, u64 *buf)
295 {
296         u32 i, j = 0;
297         struct bnxt *bp = netdev_priv(dev);
298         u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
299
300         if (!bp->bnapi)
301                 return;
302
303         for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++)
304                 bnxt_sw_func_stats[i].counter = 0;
305
306         for (i = 0; i < bp->cp_nr_rings; i++) {
307                 struct bnxt_napi *bnapi = bp->bnapi[i];
308                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
309                 __le64 *hw_stats = (__le64 *)cpr->hw_stats;
310                 int k;
311
312                 for (k = 0; k < stat_fields; j++, k++)
313                         buf[j] = le64_to_cpu(hw_stats[k]);
314                 buf[j++] = cpr->rx_l4_csum_errors;
315
316                 bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
317                         le64_to_cpu(cpr->hw_stats->rx_discard_pkts);
318                 bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
319                         le64_to_cpu(cpr->hw_stats->tx_discard_pkts);
320         }
321
322         for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
323                 buf[j] = bnxt_sw_func_stats[i].counter;
324
325         if (bp->flags & BNXT_FLAG_PORT_STATS) {
326                 __le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
327
328                 for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
329                         buf[j] = le64_to_cpu(*(port_stats +
330                                                bnxt_port_stats_arr[i].offset));
331                 }
332         }
333         if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
334                 __le64 *port_stats_ext = (__le64 *)bp->hw_rx_port_stats_ext;
335
336                 for (i = 0; i < BNXT_NUM_PORT_STATS_EXT; i++, j++) {
337                         buf[j] = le64_to_cpu(*(port_stats_ext +
338                                             bnxt_port_stats_ext_arr[i].offset));
339                 }
340         }
341 }
342
343 static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
344 {
345         struct bnxt *bp = netdev_priv(dev);
346         u32 i;
347
348         switch (stringset) {
349         /* The number of strings must match BNXT_NUM_STATS defined above. */
350         case ETH_SS_STATS:
351                 for (i = 0; i < bp->cp_nr_rings; i++) {
352                         sprintf(buf, "[%d]: rx_ucast_packets", i);
353                         buf += ETH_GSTRING_LEN;
354                         sprintf(buf, "[%d]: rx_mcast_packets", i);
355                         buf += ETH_GSTRING_LEN;
356                         sprintf(buf, "[%d]: rx_bcast_packets", i);
357                         buf += ETH_GSTRING_LEN;
358                         sprintf(buf, "[%d]: rx_discards", i);
359                         buf += ETH_GSTRING_LEN;
360                         sprintf(buf, "[%d]: rx_drops", i);
361                         buf += ETH_GSTRING_LEN;
362                         sprintf(buf, "[%d]: rx_ucast_bytes", i);
363                         buf += ETH_GSTRING_LEN;
364                         sprintf(buf, "[%d]: rx_mcast_bytes", i);
365                         buf += ETH_GSTRING_LEN;
366                         sprintf(buf, "[%d]: rx_bcast_bytes", i);
367                         buf += ETH_GSTRING_LEN;
368                         sprintf(buf, "[%d]: tx_ucast_packets", i);
369                         buf += ETH_GSTRING_LEN;
370                         sprintf(buf, "[%d]: tx_mcast_packets", i);
371                         buf += ETH_GSTRING_LEN;
372                         sprintf(buf, "[%d]: tx_bcast_packets", i);
373                         buf += ETH_GSTRING_LEN;
374                         sprintf(buf, "[%d]: tx_discards", i);
375                         buf += ETH_GSTRING_LEN;
376                         sprintf(buf, "[%d]: tx_drops", i);
377                         buf += ETH_GSTRING_LEN;
378                         sprintf(buf, "[%d]: tx_ucast_bytes", i);
379                         buf += ETH_GSTRING_LEN;
380                         sprintf(buf, "[%d]: tx_mcast_bytes", i);
381                         buf += ETH_GSTRING_LEN;
382                         sprintf(buf, "[%d]: tx_bcast_bytes", i);
383                         buf += ETH_GSTRING_LEN;
384                         sprintf(buf, "[%d]: tpa_packets", i);
385                         buf += ETH_GSTRING_LEN;
386                         sprintf(buf, "[%d]: tpa_bytes", i);
387                         buf += ETH_GSTRING_LEN;
388                         sprintf(buf, "[%d]: tpa_events", i);
389                         buf += ETH_GSTRING_LEN;
390                         sprintf(buf, "[%d]: tpa_aborts", i);
391                         buf += ETH_GSTRING_LEN;
392                         sprintf(buf, "[%d]: rx_l4_csum_errors", i);
393                         buf += ETH_GSTRING_LEN;
394                 }
395                 for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++) {
396                         strcpy(buf, bnxt_sw_func_stats[i].string);
397                         buf += ETH_GSTRING_LEN;
398                 }
399
400                 if (bp->flags & BNXT_FLAG_PORT_STATS) {
401                         for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
402                                 strcpy(buf, bnxt_port_stats_arr[i].string);
403                                 buf += ETH_GSTRING_LEN;
404                         }
405                 }
406                 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
407                         for (i = 0; i < BNXT_NUM_PORT_STATS_EXT; i++) {
408                                 strcpy(buf, bnxt_port_stats_ext_arr[i].string);
409                                 buf += ETH_GSTRING_LEN;
410                         }
411                 }
412                 break;
413         case ETH_SS_TEST:
414                 if (bp->num_tests)
415                         memcpy(buf, bp->test_info->string,
416                                bp->num_tests * ETH_GSTRING_LEN);
417                 break;
418         default:
419                 netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
420                            stringset);
421                 break;
422         }
423 }
424
425 static void bnxt_get_ringparam(struct net_device *dev,
426                                struct ethtool_ringparam *ering)
427 {
428         struct bnxt *bp = netdev_priv(dev);
429
430         ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
431         ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
432         ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
433
434         ering->rx_pending = bp->rx_ring_size;
435         ering->rx_jumbo_pending = bp->rx_agg_ring_size;
436         ering->tx_pending = bp->tx_ring_size;
437 }
438
439 static int bnxt_set_ringparam(struct net_device *dev,
440                               struct ethtool_ringparam *ering)
441 {
442         struct bnxt *bp = netdev_priv(dev);
443
444         if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
445             (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
446             (ering->tx_pending <= MAX_SKB_FRAGS))
447                 return -EINVAL;
448
449         if (netif_running(dev))
450                 bnxt_close_nic(bp, false, false);
451
452         bp->rx_ring_size = ering->rx_pending;
453         bp->tx_ring_size = ering->tx_pending;
454         bnxt_set_ring_params(bp);
455
456         if (netif_running(dev))
457                 return bnxt_open_nic(bp, false, false);
458
459         return 0;
460 }
461
462 static void bnxt_get_channels(struct net_device *dev,
463                               struct ethtool_channels *channel)
464 {
465         struct bnxt *bp = netdev_priv(dev);
466         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
467         int max_rx_rings, max_tx_rings, tcs;
468         int max_tx_sch_inputs;
469
470         /* Get the most up-to-date max_tx_sch_inputs. */
471         if (bp->flags & BNXT_FLAG_NEW_RM)
472                 bnxt_hwrm_func_resc_qcaps(bp, false);
473         max_tx_sch_inputs = hw_resc->max_tx_sch_inputs;
474
475         bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
476         if (max_tx_sch_inputs)
477                 max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
478         channel->max_combined = min_t(int, max_rx_rings, max_tx_rings);
479
480         if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
481                 max_rx_rings = 0;
482                 max_tx_rings = 0;
483         }
484         if (max_tx_sch_inputs)
485                 max_tx_rings = min_t(int, max_tx_rings, max_tx_sch_inputs);
486
487         tcs = netdev_get_num_tc(dev);
488         if (tcs > 1)
489                 max_tx_rings /= tcs;
490
491         channel->max_rx = max_rx_rings;
492         channel->max_tx = max_tx_rings;
493         channel->max_other = 0;
494         if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
495                 channel->combined_count = bp->rx_nr_rings;
496                 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
497                         channel->combined_count--;
498         } else {
499                 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
500                         channel->rx_count = bp->rx_nr_rings;
501                         channel->tx_count = bp->tx_nr_rings_per_tc;
502                 }
503         }
504 }
505
506 static int bnxt_set_channels(struct net_device *dev,
507                              struct ethtool_channels *channel)
508 {
509         struct bnxt *bp = netdev_priv(dev);
510         int req_tx_rings, req_rx_rings, tcs;
511         bool sh = false;
512         int tx_xdp = 0;
513         int rc = 0;
514
515         if (channel->other_count)
516                 return -EINVAL;
517
518         if (!channel->combined_count &&
519             (!channel->rx_count || !channel->tx_count))
520                 return -EINVAL;
521
522         if (channel->combined_count &&
523             (channel->rx_count || channel->tx_count))
524                 return -EINVAL;
525
526         if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
527                                             channel->tx_count))
528                 return -EINVAL;
529
530         if (channel->combined_count)
531                 sh = true;
532
533         tcs = netdev_get_num_tc(dev);
534
535         req_tx_rings = sh ? channel->combined_count : channel->tx_count;
536         req_rx_rings = sh ? channel->combined_count : channel->rx_count;
537         if (bp->tx_nr_rings_xdp) {
538                 if (!sh) {
539                         netdev_err(dev, "Only combined mode supported when XDP is enabled.\n");
540                         return -EINVAL;
541                 }
542                 tx_xdp = req_rx_rings;
543         }
544         rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp);
545         if (rc) {
546                 netdev_warn(dev, "Unable to allocate the requested rings\n");
547                 return rc;
548         }
549
550         if (netif_running(dev)) {
551                 if (BNXT_PF(bp)) {
552                         /* TODO CHIMP_FW: Send message to all VF's
553                          * before PF unload
554                          */
555                 }
556                 rc = bnxt_close_nic(bp, true, false);
557                 if (rc) {
558                         netdev_err(bp->dev, "Set channel failure rc :%x\n",
559                                    rc);
560                         return rc;
561                 }
562         }
563
564         if (sh) {
565                 bp->flags |= BNXT_FLAG_SHARED_RINGS;
566                 bp->rx_nr_rings = channel->combined_count;
567                 bp->tx_nr_rings_per_tc = channel->combined_count;
568         } else {
569                 bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
570                 bp->rx_nr_rings = channel->rx_count;
571                 bp->tx_nr_rings_per_tc = channel->tx_count;
572         }
573         bp->tx_nr_rings_xdp = tx_xdp;
574         bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp;
575         if (tcs > 1)
576                 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp;
577
578         bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
579                                bp->tx_nr_rings + bp->rx_nr_rings;
580
581         bp->num_stat_ctxs = bp->cp_nr_rings;
582
583         /* After changing number of rx channels, update NTUPLE feature. */
584         netdev_update_features(dev);
585         if (netif_running(dev)) {
586                 rc = bnxt_open_nic(bp, true, false);
587                 if ((!rc) && BNXT_PF(bp)) {
588                         /* TODO CHIMP_FW: Send message to all VF's
589                          * to renable
590                          */
591                 }
592         } else {
593                 rc = bnxt_reserve_rings(bp);
594         }
595
596         return rc;
597 }
598
599 #ifdef CONFIG_RFS_ACCEL
600 static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
601                             u32 *rule_locs)
602 {
603         int i, j = 0;
604
605         cmd->data = bp->ntp_fltr_count;
606         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
607                 struct hlist_head *head;
608                 struct bnxt_ntuple_filter *fltr;
609
610                 head = &bp->ntp_fltr_hash_tbl[i];
611                 rcu_read_lock();
612                 hlist_for_each_entry_rcu(fltr, head, hash) {
613                         if (j == cmd->rule_cnt)
614                                 break;
615                         rule_locs[j++] = fltr->sw_id;
616                 }
617                 rcu_read_unlock();
618                 if (j == cmd->rule_cnt)
619                         break;
620         }
621         cmd->rule_cnt = j;
622         return 0;
623 }
624
625 static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
626 {
627         struct ethtool_rx_flow_spec *fs =
628                 (struct ethtool_rx_flow_spec *)&cmd->fs;
629         struct bnxt_ntuple_filter *fltr;
630         struct flow_keys *fkeys;
631         int i, rc = -EINVAL;
632
633         if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
634                 return rc;
635
636         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
637                 struct hlist_head *head;
638
639                 head = &bp->ntp_fltr_hash_tbl[i];
640                 rcu_read_lock();
641                 hlist_for_each_entry_rcu(fltr, head, hash) {
642                         if (fltr->sw_id == fs->location)
643                                 goto fltr_found;
644                 }
645                 rcu_read_unlock();
646         }
647         return rc;
648
649 fltr_found:
650         fkeys = &fltr->fkeys;
651         if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
652                 if (fkeys->basic.ip_proto == IPPROTO_TCP)
653                         fs->flow_type = TCP_V4_FLOW;
654                 else if (fkeys->basic.ip_proto == IPPROTO_UDP)
655                         fs->flow_type = UDP_V4_FLOW;
656                 else
657                         goto fltr_err;
658
659                 fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
660                 fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
661
662                 fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
663                 fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
664
665                 fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
666                 fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
667
668                 fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
669                 fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
670         } else {
671                 int i;
672
673                 if (fkeys->basic.ip_proto == IPPROTO_TCP)
674                         fs->flow_type = TCP_V6_FLOW;
675                 else if (fkeys->basic.ip_proto == IPPROTO_UDP)
676                         fs->flow_type = UDP_V6_FLOW;
677                 else
678                         goto fltr_err;
679
680                 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
681                         fkeys->addrs.v6addrs.src;
682                 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
683                         fkeys->addrs.v6addrs.dst;
684                 for (i = 0; i < 4; i++) {
685                         fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
686                         fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
687                 }
688                 fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
689                 fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
690
691                 fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
692                 fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
693         }
694
695         fs->ring_cookie = fltr->rxq;
696         rc = 0;
697
698 fltr_err:
699         rcu_read_unlock();
700
701         return rc;
702 }
703 #endif
704
705 static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
706 {
707         if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
708                 return RXH_IP_SRC | RXH_IP_DST;
709         return 0;
710 }
711
712 static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
713 {
714         if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
715                 return RXH_IP_SRC | RXH_IP_DST;
716         return 0;
717 }
718
719 static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
720 {
721         cmd->data = 0;
722         switch (cmd->flow_type) {
723         case TCP_V4_FLOW:
724                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
725                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
726                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
727                 cmd->data |= get_ethtool_ipv4_rss(bp);
728                 break;
729         case UDP_V4_FLOW:
730                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
731                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
732                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
733                 /* fall through */
734         case SCTP_V4_FLOW:
735         case AH_ESP_V4_FLOW:
736         case AH_V4_FLOW:
737         case ESP_V4_FLOW:
738         case IPV4_FLOW:
739                 cmd->data |= get_ethtool_ipv4_rss(bp);
740                 break;
741
742         case TCP_V6_FLOW:
743                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
744                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
745                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
746                 cmd->data |= get_ethtool_ipv6_rss(bp);
747                 break;
748         case UDP_V6_FLOW:
749                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
750                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
751                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
752                 /* fall through */
753         case SCTP_V6_FLOW:
754         case AH_ESP_V6_FLOW:
755         case AH_V6_FLOW:
756         case ESP_V6_FLOW:
757         case IPV6_FLOW:
758                 cmd->data |= get_ethtool_ipv6_rss(bp);
759                 break;
760         }
761         return 0;
762 }
763
764 #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
765 #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
766
767 static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
768 {
769         u32 rss_hash_cfg = bp->rss_hash_cfg;
770         int tuple, rc = 0;
771
772         if (cmd->data == RXH_4TUPLE)
773                 tuple = 4;
774         else if (cmd->data == RXH_2TUPLE)
775                 tuple = 2;
776         else if (!cmd->data)
777                 tuple = 0;
778         else
779                 return -EINVAL;
780
781         if (cmd->flow_type == TCP_V4_FLOW) {
782                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
783                 if (tuple == 4)
784                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
785         } else if (cmd->flow_type == UDP_V4_FLOW) {
786                 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
787                         return -EINVAL;
788                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
789                 if (tuple == 4)
790                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
791         } else if (cmd->flow_type == TCP_V6_FLOW) {
792                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
793                 if (tuple == 4)
794                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
795         } else if (cmd->flow_type == UDP_V6_FLOW) {
796                 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
797                         return -EINVAL;
798                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
799                 if (tuple == 4)
800                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
801         } else if (tuple == 4) {
802                 return -EINVAL;
803         }
804
805         switch (cmd->flow_type) {
806         case TCP_V4_FLOW:
807         case UDP_V4_FLOW:
808         case SCTP_V4_FLOW:
809         case AH_ESP_V4_FLOW:
810         case AH_V4_FLOW:
811         case ESP_V4_FLOW:
812         case IPV4_FLOW:
813                 if (tuple == 2)
814                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
815                 else if (!tuple)
816                         rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
817                 break;
818
819         case TCP_V6_FLOW:
820         case UDP_V6_FLOW:
821         case SCTP_V6_FLOW:
822         case AH_ESP_V6_FLOW:
823         case AH_V6_FLOW:
824         case ESP_V6_FLOW:
825         case IPV6_FLOW:
826                 if (tuple == 2)
827                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
828                 else if (!tuple)
829                         rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
830                 break;
831         }
832
833         if (bp->rss_hash_cfg == rss_hash_cfg)
834                 return 0;
835
836         bp->rss_hash_cfg = rss_hash_cfg;
837         if (netif_running(bp->dev)) {
838                 bnxt_close_nic(bp, false, false);
839                 rc = bnxt_open_nic(bp, false, false);
840         }
841         return rc;
842 }
843
844 static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
845                           u32 *rule_locs)
846 {
847         struct bnxt *bp = netdev_priv(dev);
848         int rc = 0;
849
850         switch (cmd->cmd) {
851 #ifdef CONFIG_RFS_ACCEL
852         case ETHTOOL_GRXRINGS:
853                 cmd->data = bp->rx_nr_rings;
854                 break;
855
856         case ETHTOOL_GRXCLSRLCNT:
857                 cmd->rule_cnt = bp->ntp_fltr_count;
858                 cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
859                 break;
860
861         case ETHTOOL_GRXCLSRLALL:
862                 rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
863                 break;
864
865         case ETHTOOL_GRXCLSRULE:
866                 rc = bnxt_grxclsrule(bp, cmd);
867                 break;
868 #endif
869
870         case ETHTOOL_GRXFH:
871                 rc = bnxt_grxfh(bp, cmd);
872                 break;
873
874         default:
875                 rc = -EOPNOTSUPP;
876                 break;
877         }
878
879         return rc;
880 }
881
882 static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
883 {
884         struct bnxt *bp = netdev_priv(dev);
885         int rc;
886
887         switch (cmd->cmd) {
888         case ETHTOOL_SRXFH:
889                 rc = bnxt_srxfh(bp, cmd);
890                 break;
891
892         default:
893                 rc = -EOPNOTSUPP;
894                 break;
895         }
896         return rc;
897 }
898
899 static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
900 {
901         return HW_HASH_INDEX_SIZE;
902 }
903
904 static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
905 {
906         return HW_HASH_KEY_SIZE;
907 }
908
909 static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
910                          u8 *hfunc)
911 {
912         struct bnxt *bp = netdev_priv(dev);
913         struct bnxt_vnic_info *vnic;
914         int i = 0;
915
916         if (hfunc)
917                 *hfunc = ETH_RSS_HASH_TOP;
918
919         if (!bp->vnic_info)
920                 return 0;
921
922         vnic = &bp->vnic_info[0];
923         if (indir && vnic->rss_table) {
924                 for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
925                         indir[i] = le16_to_cpu(vnic->rss_table[i]);
926         }
927
928         if (key && vnic->rss_hash_key)
929                 memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
930
931         return 0;
932 }
933
934 static void bnxt_get_drvinfo(struct net_device *dev,
935                              struct ethtool_drvinfo *info)
936 {
937         struct bnxt *bp = netdev_priv(dev);
938
939         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
940         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
941         strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
942         strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
943         info->n_stats = bnxt_get_num_stats(bp);
944         info->testinfo_len = bp->num_tests;
945         /* TODO CHIMP_FW: eeprom dump details */
946         info->eedump_len = 0;
947         /* TODO CHIMP FW: reg dump details */
948         info->regdump_len = 0;
949 }
950
951 static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
952 {
953         struct bnxt *bp = netdev_priv(dev);
954
955         wol->supported = 0;
956         wol->wolopts = 0;
957         memset(&wol->sopass, 0, sizeof(wol->sopass));
958         if (bp->flags & BNXT_FLAG_WOL_CAP) {
959                 wol->supported = WAKE_MAGIC;
960                 if (bp->wol)
961                         wol->wolopts = WAKE_MAGIC;
962         }
963 }
964
965 static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
966 {
967         struct bnxt *bp = netdev_priv(dev);
968
969         if (wol->wolopts & ~WAKE_MAGIC)
970                 return -EINVAL;
971
972         if (wol->wolopts & WAKE_MAGIC) {
973                 if (!(bp->flags & BNXT_FLAG_WOL_CAP))
974                         return -EINVAL;
975                 if (!bp->wol) {
976                         if (bnxt_hwrm_alloc_wol_fltr(bp))
977                                 return -EBUSY;
978                         bp->wol = 1;
979                 }
980         } else {
981                 if (bp->wol) {
982                         if (bnxt_hwrm_free_wol_fltr(bp))
983                                 return -EBUSY;
984                         bp->wol = 0;
985                 }
986         }
987         return 0;
988 }
989
990 u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
991 {
992         u32 speed_mask = 0;
993
994         /* TODO: support 25GB, 40GB, 50GB with different cable type */
995         /* set the advertised speeds */
996         if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
997                 speed_mask |= ADVERTISED_100baseT_Full;
998         if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
999                 speed_mask |= ADVERTISED_1000baseT_Full;
1000         if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
1001                 speed_mask |= ADVERTISED_2500baseX_Full;
1002         if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
1003                 speed_mask |= ADVERTISED_10000baseT_Full;
1004         if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
1005                 speed_mask |= ADVERTISED_40000baseCR4_Full;
1006
1007         if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
1008                 speed_mask |= ADVERTISED_Pause;
1009         else if (fw_pause & BNXT_LINK_PAUSE_TX)
1010                 speed_mask |= ADVERTISED_Asym_Pause;
1011         else if (fw_pause & BNXT_LINK_PAUSE_RX)
1012                 speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
1013
1014         return speed_mask;
1015 }
1016
1017 #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
1018 {                                                                       \
1019         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)                    \
1020                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1021                                                      100baseT_Full);    \
1022         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)                      \
1023                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1024                                                      1000baseT_Full);   \
1025         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)                     \
1026                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1027                                                      10000baseT_Full);  \
1028         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)                     \
1029                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1030                                                      25000baseCR_Full); \
1031         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)                     \
1032                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1033                                                      40000baseCR4_Full);\
1034         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)                     \
1035                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1036                                                      50000baseCR2_Full);\
1037         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB)                    \
1038                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1039                                                      100000baseCR4_Full);\
1040         if ((fw_pause) & BNXT_LINK_PAUSE_RX) {                          \
1041                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1042                                                      Pause);            \
1043                 if (!((fw_pause) & BNXT_LINK_PAUSE_TX))                 \
1044                         ethtool_link_ksettings_add_link_mode(           \
1045                                         lk_ksettings, name, Asym_Pause);\
1046         } else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {                   \
1047                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
1048                                                      Asym_Pause);       \
1049         }                                                               \
1050 }
1051
1052 #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)          \
1053 {                                                                       \
1054         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1055                                                   100baseT_Full) ||     \
1056             ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1057                                                   100baseT_Half))       \
1058                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;               \
1059         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1060                                                   1000baseT_Full) ||    \
1061             ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1062                                                   1000baseT_Half))      \
1063                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;                 \
1064         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1065                                                   10000baseT_Full))     \
1066                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;                \
1067         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1068                                                   25000baseCR_Full))    \
1069                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;                \
1070         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1071                                                   40000baseCR4_Full))   \
1072                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;                \
1073         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1074                                                   50000baseCR2_Full))   \
1075                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;                \
1076         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
1077                                                   100000baseCR4_Full))  \
1078                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB;               \
1079 }
1080
1081 static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
1082                                 struct ethtool_link_ksettings *lk_ksettings)
1083 {
1084         u16 fw_speeds = link_info->advertising;
1085         u8 fw_pause = 0;
1086
1087         if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1088                 fw_pause = link_info->auto_pause_setting;
1089
1090         BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
1091 }
1092
1093 static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
1094                                 struct ethtool_link_ksettings *lk_ksettings)
1095 {
1096         u16 fw_speeds = link_info->lp_auto_link_speeds;
1097         u8 fw_pause = 0;
1098
1099         if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1100                 fw_pause = link_info->lp_pause;
1101
1102         BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
1103                                 lp_advertising);
1104 }
1105
1106 static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
1107                                 struct ethtool_link_ksettings *lk_ksettings)
1108 {
1109         u16 fw_speeds = link_info->support_speeds;
1110
1111         BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
1112
1113         ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
1114         ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1115                                              Asym_Pause);
1116
1117         if (link_info->support_auto_speeds)
1118                 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1119                                                      Autoneg);
1120 }
1121
1122 u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
1123 {
1124         switch (fw_link_speed) {
1125         case BNXT_LINK_SPEED_100MB:
1126                 return SPEED_100;
1127         case BNXT_LINK_SPEED_1GB:
1128                 return SPEED_1000;
1129         case BNXT_LINK_SPEED_2_5GB:
1130                 return SPEED_2500;
1131         case BNXT_LINK_SPEED_10GB:
1132                 return SPEED_10000;
1133         case BNXT_LINK_SPEED_20GB:
1134                 return SPEED_20000;
1135         case BNXT_LINK_SPEED_25GB:
1136                 return SPEED_25000;
1137         case BNXT_LINK_SPEED_40GB:
1138                 return SPEED_40000;
1139         case BNXT_LINK_SPEED_50GB:
1140                 return SPEED_50000;
1141         case BNXT_LINK_SPEED_100GB:
1142                 return SPEED_100000;
1143         default:
1144                 return SPEED_UNKNOWN;
1145         }
1146 }
1147
1148 static int bnxt_get_link_ksettings(struct net_device *dev,
1149                                    struct ethtool_link_ksettings *lk_ksettings)
1150 {
1151         struct bnxt *bp = netdev_priv(dev);
1152         struct bnxt_link_info *link_info = &bp->link_info;
1153         struct ethtool_link_settings *base = &lk_ksettings->base;
1154         u32 ethtool_speed;
1155
1156         ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
1157         mutex_lock(&bp->link_lock);
1158         bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
1159
1160         ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
1161         if (link_info->autoneg) {
1162                 bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
1163                 ethtool_link_ksettings_add_link_mode(lk_ksettings,
1164                                                      advertising, Autoneg);
1165                 base->autoneg = AUTONEG_ENABLE;
1166                 if (link_info->phy_link_status == BNXT_LINK_LINK)
1167                         bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
1168                 ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1169                 if (!netif_carrier_ok(dev))
1170                         base->duplex = DUPLEX_UNKNOWN;
1171                 else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
1172                         base->duplex = DUPLEX_FULL;
1173                 else
1174                         base->duplex = DUPLEX_HALF;
1175         } else {
1176                 base->autoneg = AUTONEG_DISABLE;
1177                 ethtool_speed =
1178                         bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
1179                 base->duplex = DUPLEX_HALF;
1180                 if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
1181                         base->duplex = DUPLEX_FULL;
1182         }
1183         base->speed = ethtool_speed;
1184
1185         base->port = PORT_NONE;
1186         if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
1187                 base->port = PORT_TP;
1188                 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1189                                                      TP);
1190                 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
1191                                                      TP);
1192         } else {
1193                 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1194                                                      FIBRE);
1195                 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
1196                                                      FIBRE);
1197
1198                 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
1199                         base->port = PORT_DA;
1200                 else if (link_info->media_type ==
1201                          PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
1202                         base->port = PORT_FIBRE;
1203         }
1204         base->phy_address = link_info->phy_addr;
1205         mutex_unlock(&bp->link_lock);
1206
1207         return 0;
1208 }
1209
1210 static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed)
1211 {
1212         struct bnxt *bp = netdev_priv(dev);
1213         struct bnxt_link_info *link_info = &bp->link_info;
1214         u16 support_spds = link_info->support_speeds;
1215         u32 fw_speed = 0;
1216
1217         switch (ethtool_speed) {
1218         case SPEED_100:
1219                 if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
1220                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
1221                 break;
1222         case SPEED_1000:
1223                 if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
1224                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
1225                 break;
1226         case SPEED_2500:
1227                 if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
1228                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
1229                 break;
1230         case SPEED_10000:
1231                 if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
1232                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
1233                 break;
1234         case SPEED_20000:
1235                 if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
1236                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
1237                 break;
1238         case SPEED_25000:
1239                 if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
1240                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
1241                 break;
1242         case SPEED_40000:
1243                 if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
1244                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
1245                 break;
1246         case SPEED_50000:
1247                 if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
1248                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
1249                 break;
1250         case SPEED_100000:
1251                 if (support_spds & BNXT_LINK_SPEED_MSK_100GB)
1252                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB;
1253                 break;
1254         default:
1255                 netdev_err(dev, "unsupported speed!\n");
1256                 break;
1257         }
1258         return fw_speed;
1259 }
1260
1261 u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1262 {
1263         u16 fw_speed_mask = 0;
1264
1265         /* only support autoneg at speed 100, 1000, and 10000 */
1266         if (advertising & (ADVERTISED_100baseT_Full |
1267                            ADVERTISED_100baseT_Half)) {
1268                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1269         }
1270         if (advertising & (ADVERTISED_1000baseT_Full |
1271                            ADVERTISED_1000baseT_Half)) {
1272                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1273         }
1274         if (advertising & ADVERTISED_10000baseT_Full)
1275                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1276
1277         if (advertising & ADVERTISED_40000baseCR4_Full)
1278                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
1279
1280         return fw_speed_mask;
1281 }
1282
1283 static int bnxt_set_link_ksettings(struct net_device *dev,
1284                            const struct ethtool_link_ksettings *lk_ksettings)
1285 {
1286         struct bnxt *bp = netdev_priv(dev);
1287         struct bnxt_link_info *link_info = &bp->link_info;
1288         const struct ethtool_link_settings *base = &lk_ksettings->base;
1289         bool set_pause = false;
1290         u16 fw_advertising = 0;
1291         u32 speed;
1292         int rc = 0;
1293
1294         if (!BNXT_SINGLE_PF(bp))
1295                 return -EOPNOTSUPP;
1296
1297         mutex_lock(&bp->link_lock);
1298         if (base->autoneg == AUTONEG_ENABLE) {
1299                 BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
1300                                         advertising);
1301                 link_info->autoneg |= BNXT_AUTONEG_SPEED;
1302                 if (!fw_advertising)
1303                         link_info->advertising = link_info->support_auto_speeds;
1304                 else
1305                         link_info->advertising = fw_advertising;
1306                 /* any change to autoneg will cause link change, therefore the
1307                  * driver should put back the original pause setting in autoneg
1308                  */
1309                 set_pause = true;
1310         } else {
1311                 u16 fw_speed;
1312                 u8 phy_type = link_info->phy_type;
1313
1314                 if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
1315                     phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
1316                     link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
1317                         netdev_err(dev, "10GBase-T devices must autoneg\n");
1318                         rc = -EINVAL;
1319                         goto set_setting_exit;
1320                 }
1321                 if (base->duplex == DUPLEX_HALF) {
1322                         netdev_err(dev, "HALF DUPLEX is not supported!\n");
1323                         rc = -EINVAL;
1324                         goto set_setting_exit;
1325                 }
1326                 speed = base->speed;
1327                 fw_speed = bnxt_get_fw_speed(dev, speed);
1328                 if (!fw_speed) {
1329                         rc = -EINVAL;
1330                         goto set_setting_exit;
1331                 }
1332                 link_info->req_link_speed = fw_speed;
1333                 link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1334                 link_info->autoneg = 0;
1335                 link_info->advertising = 0;
1336         }
1337
1338         if (netif_running(dev))
1339                 rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1340
1341 set_setting_exit:
1342         mutex_unlock(&bp->link_lock);
1343         return rc;
1344 }
1345
1346 static void bnxt_get_pauseparam(struct net_device *dev,
1347                                 struct ethtool_pauseparam *epause)
1348 {
1349         struct bnxt *bp = netdev_priv(dev);
1350         struct bnxt_link_info *link_info = &bp->link_info;
1351
1352         if (BNXT_VF(bp))
1353                 return;
1354         epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
1355         epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
1356         epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1357 }
1358
1359 static int bnxt_set_pauseparam(struct net_device *dev,
1360                                struct ethtool_pauseparam *epause)
1361 {
1362         int rc = 0;
1363         struct bnxt *bp = netdev_priv(dev);
1364         struct bnxt_link_info *link_info = &bp->link_info;
1365
1366         if (!BNXT_SINGLE_PF(bp))
1367                 return -EOPNOTSUPP;
1368
1369         if (epause->autoneg) {
1370                 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
1371                         return -EINVAL;
1372
1373                 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1374                 if (bp->hwrm_spec_code >= 0x10201)
1375                         link_info->req_flow_ctrl =
1376                                 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1377         } else {
1378                 /* when transition from auto pause to force pause,
1379                  * force a link change
1380                  */
1381                 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1382                         link_info->force_link_chng = true;
1383                 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1384                 link_info->req_flow_ctrl = 0;
1385         }
1386         if (epause->rx_pause)
1387                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1388
1389         if (epause->tx_pause)
1390                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1391
1392         if (netif_running(dev))
1393                 rc = bnxt_hwrm_set_pause(bp);
1394         return rc;
1395 }
1396
1397 static u32 bnxt_get_link(struct net_device *dev)
1398 {
1399         struct bnxt *bp = netdev_priv(dev);
1400
1401         /* TODO: handle MF, VF, driver close case */
1402         return bp->link_info.link_up;
1403 }
1404
1405 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1406                                 u16 ext, u16 *index, u32 *item_length,
1407                                 u32 *data_length);
1408
1409 static int bnxt_flash_nvram(struct net_device *dev,
1410                             u16 dir_type,
1411                             u16 dir_ordinal,
1412                             u16 dir_ext,
1413                             u16 dir_attr,
1414                             const u8 *data,
1415                             size_t data_len)
1416 {
1417         struct bnxt *bp = netdev_priv(dev);
1418         int rc;
1419         struct hwrm_nvm_write_input req = {0};
1420         dma_addr_t dma_handle;
1421         u8 *kmem;
1422
1423         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1424
1425         req.dir_type = cpu_to_le16(dir_type);
1426         req.dir_ordinal = cpu_to_le16(dir_ordinal);
1427         req.dir_ext = cpu_to_le16(dir_ext);
1428         req.dir_attr = cpu_to_le16(dir_attr);
1429         req.dir_data_length = cpu_to_le32(data_len);
1430
1431         kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1432                                   GFP_KERNEL);
1433         if (!kmem) {
1434                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1435                            (unsigned)data_len);
1436                 return -ENOMEM;
1437         }
1438         memcpy(kmem, data, data_len);
1439         req.host_src_addr = cpu_to_le64(dma_handle);
1440
1441         rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1442         dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1443
1444         return rc;
1445 }
1446
1447 static int bnxt_firmware_reset(struct net_device *dev,
1448                                u16 dir_type)
1449 {
1450         struct bnxt *bp = netdev_priv(dev);
1451         struct hwrm_fw_reset_input req = {0};
1452
1453         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1454
1455         /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1456         /*       (e.g. when firmware isn't already running) */
1457         switch (dir_type) {
1458         case BNX_DIR_TYPE_CHIMP_PATCH:
1459         case BNX_DIR_TYPE_BOOTCODE:
1460         case BNX_DIR_TYPE_BOOTCODE_2:
1461                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1462                 /* Self-reset ChiMP upon next PCIe reset: */
1463                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1464                 break;
1465         case BNX_DIR_TYPE_APE_FW:
1466         case BNX_DIR_TYPE_APE_PATCH:
1467                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
1468                 /* Self-reset APE upon next PCIe reset: */
1469                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1470                 break;
1471         case BNX_DIR_TYPE_KONG_FW:
1472         case BNX_DIR_TYPE_KONG_PATCH:
1473                 req.embedded_proc_type =
1474                         FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1475                 break;
1476         case BNX_DIR_TYPE_BONO_FW:
1477         case BNX_DIR_TYPE_BONO_PATCH:
1478                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1479                 break;
1480         case BNXT_FW_RESET_CHIP:
1481                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
1482                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
1483                 break;
1484         case BNXT_FW_RESET_AP:
1485                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP;
1486                 break;
1487         default:
1488                 return -EINVAL;
1489         }
1490
1491         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1492 }
1493
1494 static int bnxt_flash_firmware(struct net_device *dev,
1495                                u16 dir_type,
1496                                const u8 *fw_data,
1497                                size_t fw_size)
1498 {
1499         int     rc = 0;
1500         u16     code_type;
1501         u32     stored_crc;
1502         u32     calculated_crc;
1503         struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1504
1505         switch (dir_type) {
1506         case BNX_DIR_TYPE_BOOTCODE:
1507         case BNX_DIR_TYPE_BOOTCODE_2:
1508                 code_type = CODE_BOOT;
1509                 break;
1510         case BNX_DIR_TYPE_CHIMP_PATCH:
1511                 code_type = CODE_CHIMP_PATCH;
1512                 break;
1513         case BNX_DIR_TYPE_APE_FW:
1514                 code_type = CODE_MCTP_PASSTHRU;
1515                 break;
1516         case BNX_DIR_TYPE_APE_PATCH:
1517                 code_type = CODE_APE_PATCH;
1518                 break;
1519         case BNX_DIR_TYPE_KONG_FW:
1520                 code_type = CODE_KONG_FW;
1521                 break;
1522         case BNX_DIR_TYPE_KONG_PATCH:
1523                 code_type = CODE_KONG_PATCH;
1524                 break;
1525         case BNX_DIR_TYPE_BONO_FW:
1526                 code_type = CODE_BONO_FW;
1527                 break;
1528         case BNX_DIR_TYPE_BONO_PATCH:
1529                 code_type = CODE_BONO_PATCH;
1530                 break;
1531         default:
1532                 netdev_err(dev, "Unsupported directory entry type: %u\n",
1533                            dir_type);
1534                 return -EINVAL;
1535         }
1536         if (fw_size < sizeof(struct bnxt_fw_header)) {
1537                 netdev_err(dev, "Invalid firmware file size: %u\n",
1538                            (unsigned int)fw_size);
1539                 return -EINVAL;
1540         }
1541         if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1542                 netdev_err(dev, "Invalid firmware signature: %08X\n",
1543                            le32_to_cpu(header->signature));
1544                 return -EINVAL;
1545         }
1546         if (header->code_type != code_type) {
1547                 netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1548                            code_type, header->code_type);
1549                 return -EINVAL;
1550         }
1551         if (header->device != DEVICE_CUMULUS_FAMILY) {
1552                 netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1553                            DEVICE_CUMULUS_FAMILY, header->device);
1554                 return -EINVAL;
1555         }
1556         /* Confirm the CRC32 checksum of the file: */
1557         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1558                                              sizeof(stored_crc)));
1559         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1560         if (calculated_crc != stored_crc) {
1561                 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1562                            (unsigned long)stored_crc,
1563                            (unsigned long)calculated_crc);
1564                 return -EINVAL;
1565         }
1566         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1567                               0, 0, fw_data, fw_size);
1568         if (rc == 0)    /* Firmware update successful */
1569                 rc = bnxt_firmware_reset(dev, dir_type);
1570
1571         return rc;
1572 }
1573
1574 static int bnxt_flash_microcode(struct net_device *dev,
1575                                 u16 dir_type,
1576                                 const u8 *fw_data,
1577                                 size_t fw_size)
1578 {
1579         struct bnxt_ucode_trailer *trailer;
1580         u32 calculated_crc;
1581         u32 stored_crc;
1582         int rc = 0;
1583
1584         if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
1585                 netdev_err(dev, "Invalid microcode file size: %u\n",
1586                            (unsigned int)fw_size);
1587                 return -EINVAL;
1588         }
1589         trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
1590                                                 sizeof(*trailer)));
1591         if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
1592                 netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
1593                            le32_to_cpu(trailer->sig));
1594                 return -EINVAL;
1595         }
1596         if (le16_to_cpu(trailer->dir_type) != dir_type) {
1597                 netdev_err(dev, "Expected microcode type: %d, read: %d\n",
1598                            dir_type, le16_to_cpu(trailer->dir_type));
1599                 return -EINVAL;
1600         }
1601         if (le16_to_cpu(trailer->trailer_length) <
1602                 sizeof(struct bnxt_ucode_trailer)) {
1603                 netdev_err(dev, "Invalid microcode trailer length: %d\n",
1604                            le16_to_cpu(trailer->trailer_length));
1605                 return -EINVAL;
1606         }
1607
1608         /* Confirm the CRC32 checksum of the file: */
1609         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1610                                              sizeof(stored_crc)));
1611         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1612         if (calculated_crc != stored_crc) {
1613                 netdev_err(dev,
1614                            "CRC32 (%08lX) does not match calculated: %08lX\n",
1615                            (unsigned long)stored_crc,
1616                            (unsigned long)calculated_crc);
1617                 return -EINVAL;
1618         }
1619         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1620                               0, 0, fw_data, fw_size);
1621
1622         return rc;
1623 }
1624
1625 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1626 {
1627         switch (dir_type) {
1628         case BNX_DIR_TYPE_CHIMP_PATCH:
1629         case BNX_DIR_TYPE_BOOTCODE:
1630         case BNX_DIR_TYPE_BOOTCODE_2:
1631         case BNX_DIR_TYPE_APE_FW:
1632         case BNX_DIR_TYPE_APE_PATCH:
1633         case BNX_DIR_TYPE_KONG_FW:
1634         case BNX_DIR_TYPE_KONG_PATCH:
1635         case BNX_DIR_TYPE_BONO_FW:
1636         case BNX_DIR_TYPE_BONO_PATCH:
1637                 return true;
1638         }
1639
1640         return false;
1641 }
1642
1643 static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
1644 {
1645         switch (dir_type) {
1646         case BNX_DIR_TYPE_AVS:
1647         case BNX_DIR_TYPE_EXP_ROM_MBA:
1648         case BNX_DIR_TYPE_PCIE:
1649         case BNX_DIR_TYPE_TSCF_UCODE:
1650         case BNX_DIR_TYPE_EXT_PHY:
1651         case BNX_DIR_TYPE_CCM:
1652         case BNX_DIR_TYPE_ISCSI_BOOT:
1653         case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1654         case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1655                 return true;
1656         }
1657
1658         return false;
1659 }
1660
1661 static bool bnxt_dir_type_is_executable(u16 dir_type)
1662 {
1663         return bnxt_dir_type_is_ape_bin_format(dir_type) ||
1664                 bnxt_dir_type_is_other_exec_format(dir_type);
1665 }
1666
1667 static int bnxt_flash_firmware_from_file(struct net_device *dev,
1668                                          u16 dir_type,
1669                                          const char *filename)
1670 {
1671         const struct firmware  *fw;
1672         int                     rc;
1673
1674         rc = request_firmware(&fw, filename, &dev->dev);
1675         if (rc != 0) {
1676                 netdev_err(dev, "Error %d requesting firmware file: %s\n",
1677                            rc, filename);
1678                 return rc;
1679         }
1680         if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1681                 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1682         else if (bnxt_dir_type_is_other_exec_format(dir_type) == true)
1683                 rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
1684         else
1685                 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1686                                       0, 0, fw->data, fw->size);
1687         release_firmware(fw);
1688         return rc;
1689 }
1690
1691 static int bnxt_flash_package_from_file(struct net_device *dev,
1692                                         char *filename, u32 install_type)
1693 {
1694         struct bnxt *bp = netdev_priv(dev);
1695         struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
1696         struct hwrm_nvm_install_update_input install = {0};
1697         const struct firmware *fw;
1698         u32 item_len;
1699         u16 index;
1700         int rc;
1701
1702         bnxt_hwrm_fw_set_time(bp);
1703
1704         if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
1705                                  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1706                                  &index, &item_len, NULL) != 0) {
1707                 netdev_err(dev, "PKG update area not created in nvram\n");
1708                 return -ENOBUFS;
1709         }
1710
1711         rc = request_firmware(&fw, filename, &dev->dev);
1712         if (rc != 0) {
1713                 netdev_err(dev, "PKG error %d requesting file: %s\n",
1714                            rc, filename);
1715                 return rc;
1716         }
1717
1718         if (fw->size > item_len) {
1719                 netdev_err(dev, "PKG insufficient update area in nvram: %lu",
1720                            (unsigned long)fw->size);
1721                 rc = -EFBIG;
1722         } else {
1723                 dma_addr_t dma_handle;
1724                 u8 *kmem;
1725                 struct hwrm_nvm_modify_input modify = {0};
1726
1727                 bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
1728
1729                 modify.dir_idx = cpu_to_le16(index);
1730                 modify.len = cpu_to_le32(fw->size);
1731
1732                 kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
1733                                           &dma_handle, GFP_KERNEL);
1734                 if (!kmem) {
1735                         netdev_err(dev,
1736                                    "dma_alloc_coherent failure, length = %u\n",
1737                                    (unsigned int)fw->size);
1738                         rc = -ENOMEM;
1739                 } else {
1740                         memcpy(kmem, fw->data, fw->size);
1741                         modify.host_src_addr = cpu_to_le64(dma_handle);
1742
1743                         rc = hwrm_send_message(bp, &modify, sizeof(modify),
1744                                                FLASH_PACKAGE_TIMEOUT);
1745                         dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
1746                                           dma_handle);
1747                 }
1748         }
1749         release_firmware(fw);
1750         if (rc)
1751                 return rc;
1752
1753         if ((install_type & 0xffff) == 0)
1754                 install_type >>= 16;
1755         bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
1756         install.install_type = cpu_to_le32(install_type);
1757
1758         mutex_lock(&bp->hwrm_cmd_lock);
1759         rc = _hwrm_send_message(bp, &install, sizeof(install),
1760                                 INSTALL_PACKAGE_TIMEOUT);
1761         if (rc) {
1762                 rc = -EOPNOTSUPP;
1763                 goto flash_pkg_exit;
1764         }
1765
1766         if (resp->error_code) {
1767                 u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err;
1768
1769                 if (error_code == NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) {
1770                         install.flags |= cpu_to_le16(
1771                                NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG);
1772                         rc = _hwrm_send_message(bp, &install, sizeof(install),
1773                                                 INSTALL_PACKAGE_TIMEOUT);
1774                         if (rc) {
1775                                 rc = -EOPNOTSUPP;
1776                                 goto flash_pkg_exit;
1777                         }
1778                 }
1779         }
1780
1781         if (resp->result) {
1782                 netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
1783                            (s8)resp->result, (int)resp->problem_item);
1784                 rc = -ENOPKG;
1785         }
1786 flash_pkg_exit:
1787         mutex_unlock(&bp->hwrm_cmd_lock);
1788         return rc;
1789 }
1790
1791 static int bnxt_flash_device(struct net_device *dev,
1792                              struct ethtool_flash *flash)
1793 {
1794         if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1795                 netdev_err(dev, "flashdev not supported from a virtual function\n");
1796                 return -EINVAL;
1797         }
1798
1799         if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
1800             flash->region > 0xffff)
1801                 return bnxt_flash_package_from_file(dev, flash->data,
1802                                                     flash->region);
1803
1804         return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1805 }
1806
1807 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1808 {
1809         struct bnxt *bp = netdev_priv(dev);
1810         int rc;
1811         struct hwrm_nvm_get_dir_info_input req = {0};
1812         struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1813
1814         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1815
1816         mutex_lock(&bp->hwrm_cmd_lock);
1817         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1818         if (!rc) {
1819                 *entries = le32_to_cpu(output->entries);
1820                 *length = le32_to_cpu(output->entry_length);
1821         }
1822         mutex_unlock(&bp->hwrm_cmd_lock);
1823         return rc;
1824 }
1825
1826 static int bnxt_get_eeprom_len(struct net_device *dev)
1827 {
1828         struct bnxt *bp = netdev_priv(dev);
1829
1830         if (BNXT_VF(bp))
1831                 return 0;
1832
1833         /* The -1 return value allows the entire 32-bit range of offsets to be
1834          * passed via the ethtool command-line utility.
1835          */
1836         return -1;
1837 }
1838
1839 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1840 {
1841         struct bnxt *bp = netdev_priv(dev);
1842         int rc;
1843         u32 dir_entries;
1844         u32 entry_length;
1845         u8 *buf;
1846         size_t buflen;
1847         dma_addr_t dma_handle;
1848         struct hwrm_nvm_get_dir_entries_input req = {0};
1849
1850         rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1851         if (rc != 0)
1852                 return rc;
1853
1854         /* Insert 2 bytes of directory info (count and size of entries) */
1855         if (len < 2)
1856                 return -EINVAL;
1857
1858         *data++ = dir_entries;
1859         *data++ = entry_length;
1860         len -= 2;
1861         memset(data, 0xff, len);
1862
1863         buflen = dir_entries * entry_length;
1864         buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1865                                  GFP_KERNEL);
1866         if (!buf) {
1867                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1868                            (unsigned)buflen);
1869                 return -ENOMEM;
1870         }
1871         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1872         req.host_dest_addr = cpu_to_le64(dma_handle);
1873         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1874         if (rc == 0)
1875                 memcpy(data, buf, len > buflen ? buflen : len);
1876         dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1877         return rc;
1878 }
1879
1880 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1881                                u32 length, u8 *data)
1882 {
1883         struct bnxt *bp = netdev_priv(dev);
1884         int rc;
1885         u8 *buf;
1886         dma_addr_t dma_handle;
1887         struct hwrm_nvm_read_input req = {0};
1888
1889         if (!length)
1890                 return -EINVAL;
1891
1892         buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1893                                  GFP_KERNEL);
1894         if (!buf) {
1895                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1896                            (unsigned)length);
1897                 return -ENOMEM;
1898         }
1899         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1900         req.host_dest_addr = cpu_to_le64(dma_handle);
1901         req.dir_idx = cpu_to_le16(index);
1902         req.offset = cpu_to_le32(offset);
1903         req.len = cpu_to_le32(length);
1904
1905         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1906         if (rc == 0)
1907                 memcpy(data, buf, length);
1908         dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1909         return rc;
1910 }
1911
1912 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1913                                 u16 ext, u16 *index, u32 *item_length,
1914                                 u32 *data_length)
1915 {
1916         struct bnxt *bp = netdev_priv(dev);
1917         int rc;
1918         struct hwrm_nvm_find_dir_entry_input req = {0};
1919         struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
1920
1921         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
1922         req.enables = 0;
1923         req.dir_idx = 0;
1924         req.dir_type = cpu_to_le16(type);
1925         req.dir_ordinal = cpu_to_le16(ordinal);
1926         req.dir_ext = cpu_to_le16(ext);
1927         req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
1928         mutex_lock(&bp->hwrm_cmd_lock);
1929         rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1930         if (rc == 0) {
1931                 if (index)
1932                         *index = le16_to_cpu(output->dir_idx);
1933                 if (item_length)
1934                         *item_length = le32_to_cpu(output->dir_item_length);
1935                 if (data_length)
1936                         *data_length = le32_to_cpu(output->dir_data_length);
1937         }
1938         mutex_unlock(&bp->hwrm_cmd_lock);
1939         return rc;
1940 }
1941
1942 static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
1943 {
1944         char    *retval = NULL;
1945         char    *p;
1946         char    *value;
1947         int     field = 0;
1948
1949         if (datalen < 1)
1950                 return NULL;
1951         /* null-terminate the log data (removing last '\n'): */
1952         data[datalen - 1] = 0;
1953         for (p = data; *p != 0; p++) {
1954                 field = 0;
1955                 retval = NULL;
1956                 while (*p != 0 && *p != '\n') {
1957                         value = p;
1958                         while (*p != 0 && *p != '\t' && *p != '\n')
1959                                 p++;
1960                         if (field == desired_field)
1961                                 retval = value;
1962                         if (*p != '\t')
1963                                 break;
1964                         *p = 0;
1965                         field++;
1966                         p++;
1967                 }
1968                 if (*p == 0)
1969                         break;
1970                 *p = 0;
1971         }
1972         return retval;
1973 }
1974
1975 static void bnxt_get_pkgver(struct net_device *dev)
1976 {
1977         struct bnxt *bp = netdev_priv(dev);
1978         u16 index = 0;
1979         char *pkgver;
1980         u32 pkglen;
1981         u8 *pkgbuf;
1982         int len;
1983
1984         if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
1985                                  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1986                                  &index, NULL, &pkglen) != 0)
1987                 return;
1988
1989         pkgbuf = kzalloc(pkglen, GFP_KERNEL);
1990         if (!pkgbuf) {
1991                 dev_err(&bp->pdev->dev, "Unable to allocate memory for pkg version, length = %u\n",
1992                         pkglen);
1993                 return;
1994         }
1995
1996         if (bnxt_get_nvram_item(dev, index, 0, pkglen, pkgbuf))
1997                 goto err;
1998
1999         pkgver = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkgbuf,
2000                                    pkglen);
2001         if (pkgver && *pkgver != 0 && isdigit(*pkgver)) {
2002                 len = strlen(bp->fw_ver_str);
2003                 snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1,
2004                          "/pkg %s", pkgver);
2005         }
2006 err:
2007         kfree(pkgbuf);
2008 }
2009
2010 static int bnxt_get_eeprom(struct net_device *dev,
2011                            struct ethtool_eeprom *eeprom,
2012                            u8 *data)
2013 {
2014         u32 index;
2015         u32 offset;
2016
2017         if (eeprom->offset == 0) /* special offset value to get directory */
2018                 return bnxt_get_nvram_directory(dev, eeprom->len, data);
2019
2020         index = eeprom->offset >> 24;
2021         offset = eeprom->offset & 0xffffff;
2022
2023         if (index == 0) {
2024                 netdev_err(dev, "unsupported index value: %d\n", index);
2025                 return -EINVAL;
2026         }
2027
2028         return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
2029 }
2030
2031 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
2032 {
2033         struct bnxt *bp = netdev_priv(dev);
2034         struct hwrm_nvm_erase_dir_entry_input req = {0};
2035
2036         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
2037         req.dir_idx = cpu_to_le16(index);
2038         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2039 }
2040
2041 static int bnxt_set_eeprom(struct net_device *dev,
2042                            struct ethtool_eeprom *eeprom,
2043                            u8 *data)
2044 {
2045         struct bnxt *bp = netdev_priv(dev);
2046         u8 index, dir_op;
2047         u16 type, ext, ordinal, attr;
2048
2049         if (!BNXT_PF(bp)) {
2050                 netdev_err(dev, "NVM write not supported from a virtual function\n");
2051                 return -EINVAL;
2052         }
2053
2054         type = eeprom->magic >> 16;
2055
2056         if (type == 0xffff) { /* special value for directory operations */
2057                 index = eeprom->magic & 0xff;
2058                 dir_op = eeprom->magic >> 8;
2059                 if (index == 0)
2060                         return -EINVAL;
2061                 switch (dir_op) {
2062                 case 0x0e: /* erase */
2063                         if (eeprom->offset != ~eeprom->magic)
2064                                 return -EINVAL;
2065                         return bnxt_erase_nvram_directory(dev, index - 1);
2066                 default:
2067                         return -EINVAL;
2068                 }
2069         }
2070
2071         /* Create or re-write an NVM item: */
2072         if (bnxt_dir_type_is_executable(type) == true)
2073                 return -EOPNOTSUPP;
2074         ext = eeprom->magic & 0xffff;
2075         ordinal = eeprom->offset >> 16;
2076         attr = eeprom->offset & 0xffff;
2077
2078         return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
2079                                 eeprom->len);
2080 }
2081
2082 static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
2083 {
2084         struct bnxt *bp = netdev_priv(dev);
2085         struct ethtool_eee *eee = &bp->eee;
2086         struct bnxt_link_info *link_info = &bp->link_info;
2087         u32 advertising =
2088                  _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
2089         int rc = 0;
2090
2091         if (!BNXT_SINGLE_PF(bp))
2092                 return -EOPNOTSUPP;
2093
2094         if (!(bp->flags & BNXT_FLAG_EEE_CAP))
2095                 return -EOPNOTSUPP;
2096
2097         if (!edata->eee_enabled)
2098                 goto eee_ok;
2099
2100         if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
2101                 netdev_warn(dev, "EEE requires autoneg\n");
2102                 return -EINVAL;
2103         }
2104         if (edata->tx_lpi_enabled) {
2105                 if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
2106                                        edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
2107                         netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
2108                                     bp->lpi_tmr_lo, bp->lpi_tmr_hi);
2109                         return -EINVAL;
2110                 } else if (!bp->lpi_tmr_hi) {
2111                         edata->tx_lpi_timer = eee->tx_lpi_timer;
2112                 }
2113         }
2114         if (!edata->advertised) {
2115                 edata->advertised = advertising & eee->supported;
2116         } else if (edata->advertised & ~advertising) {
2117                 netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
2118                             edata->advertised, advertising);
2119                 return -EINVAL;
2120         }
2121
2122         eee->advertised = edata->advertised;
2123         eee->tx_lpi_enabled = edata->tx_lpi_enabled;
2124         eee->tx_lpi_timer = edata->tx_lpi_timer;
2125 eee_ok:
2126         eee->eee_enabled = edata->eee_enabled;
2127
2128         if (netif_running(dev))
2129                 rc = bnxt_hwrm_set_link_setting(bp, false, true);
2130
2131         return rc;
2132 }
2133
2134 static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
2135 {
2136         struct bnxt *bp = netdev_priv(dev);
2137
2138         if (!(bp->flags & BNXT_FLAG_EEE_CAP))
2139                 return -EOPNOTSUPP;
2140
2141         *edata = bp->eee;
2142         if (!bp->eee.eee_enabled) {
2143                 /* Preserve tx_lpi_timer so that the last value will be used
2144                  * by default when it is re-enabled.
2145                  */
2146                 edata->advertised = 0;
2147                 edata->tx_lpi_enabled = 0;
2148         }
2149
2150         if (!bp->eee.eee_active)
2151                 edata->lp_advertised = 0;
2152
2153         return 0;
2154 }
2155
2156 static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
2157                                             u16 page_number, u16 start_addr,
2158                                             u16 data_length, u8 *buf)
2159 {
2160         struct hwrm_port_phy_i2c_read_input req = {0};
2161         struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
2162         int rc, byte_offset = 0;
2163
2164         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
2165         req.i2c_slave_addr = i2c_addr;
2166         req.page_number = cpu_to_le16(page_number);
2167         req.port_id = cpu_to_le16(bp->pf.port_id);
2168         do {
2169                 u16 xfer_size;
2170
2171                 xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
2172                 data_length -= xfer_size;
2173                 req.page_offset = cpu_to_le16(start_addr + byte_offset);
2174                 req.data_length = xfer_size;
2175                 req.enables = cpu_to_le32(start_addr + byte_offset ?
2176                                  PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
2177                 mutex_lock(&bp->hwrm_cmd_lock);
2178                 rc = _hwrm_send_message(bp, &req, sizeof(req),
2179                                         HWRM_CMD_TIMEOUT);
2180                 if (!rc)
2181                         memcpy(buf + byte_offset, output->data, xfer_size);
2182                 mutex_unlock(&bp->hwrm_cmd_lock);
2183                 byte_offset += xfer_size;
2184         } while (!rc && data_length > 0);
2185
2186         return rc;
2187 }
2188
2189 static int bnxt_get_module_info(struct net_device *dev,
2190                                 struct ethtool_modinfo *modinfo)
2191 {
2192         u8 data[SFF_DIAG_SUPPORT_OFFSET + 1];
2193         struct bnxt *bp = netdev_priv(dev);
2194         int rc;
2195
2196         /* No point in going further if phy status indicates
2197          * module is not inserted or if it is powered down or
2198          * if it is of type 10GBase-T
2199          */
2200         if (bp->link_info.module_status >
2201                 PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
2202                 return -EOPNOTSUPP;
2203
2204         /* This feature is not supported in older firmware versions */
2205         if (bp->hwrm_spec_code < 0x10202)
2206                 return -EOPNOTSUPP;
2207
2208         rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 0,
2209                                               SFF_DIAG_SUPPORT_OFFSET + 1,
2210                                               data);
2211         if (!rc) {
2212                 u8 module_id = data[0];
2213                 u8 diag_supported = data[SFF_DIAG_SUPPORT_OFFSET];
2214
2215                 switch (module_id) {
2216                 case SFF_MODULE_ID_SFP:
2217                         modinfo->type = ETH_MODULE_SFF_8472;
2218                         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2219                         if (!diag_supported)
2220                                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2221                         break;
2222                 case SFF_MODULE_ID_QSFP:
2223                 case SFF_MODULE_ID_QSFP_PLUS:
2224                         modinfo->type = ETH_MODULE_SFF_8436;
2225                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2226                         break;
2227                 case SFF_MODULE_ID_QSFP28:
2228                         modinfo->type = ETH_MODULE_SFF_8636;
2229                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
2230                         break;
2231                 default:
2232                         rc = -EOPNOTSUPP;
2233                         break;
2234                 }
2235         }
2236         return rc;
2237 }
2238
2239 static int bnxt_get_module_eeprom(struct net_device *dev,
2240                                   struct ethtool_eeprom *eeprom,
2241                                   u8 *data)
2242 {
2243         struct bnxt *bp = netdev_priv(dev);
2244         u16  start = eeprom->offset, length = eeprom->len;
2245         int rc = 0;
2246
2247         memset(data, 0, eeprom->len);
2248
2249         /* Read A0 portion of the EEPROM */
2250         if (start < ETH_MODULE_SFF_8436_LEN) {
2251                 if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
2252                         length = ETH_MODULE_SFF_8436_LEN - start;
2253                 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
2254                                                       start, length, data);
2255                 if (rc)
2256                         return rc;
2257                 start += length;
2258                 data += length;
2259                 length = eeprom->len - length;
2260         }
2261
2262         /* Read A2 portion of the EEPROM */
2263         if (length) {
2264                 start -= ETH_MODULE_SFF_8436_LEN;
2265                 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1,
2266                                                       start, length, data);
2267         }
2268         return rc;
2269 }
2270
2271 static int bnxt_nway_reset(struct net_device *dev)
2272 {
2273         int rc = 0;
2274
2275         struct bnxt *bp = netdev_priv(dev);
2276         struct bnxt_link_info *link_info = &bp->link_info;
2277
2278         if (!BNXT_SINGLE_PF(bp))
2279                 return -EOPNOTSUPP;
2280
2281         if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2282                 return -EINVAL;
2283
2284         if (netif_running(dev))
2285                 rc = bnxt_hwrm_set_link_setting(bp, true, false);
2286
2287         return rc;
2288 }
2289
2290 static int bnxt_set_phys_id(struct net_device *dev,
2291                             enum ethtool_phys_id_state state)
2292 {
2293         struct hwrm_port_led_cfg_input req = {0};
2294         struct bnxt *bp = netdev_priv(dev);
2295         struct bnxt_pf_info *pf = &bp->pf;
2296         struct bnxt_led_cfg *led_cfg;
2297         u8 led_state;
2298         __le16 duration;
2299         int i, rc;
2300
2301         if (!bp->num_leds || BNXT_VF(bp))
2302                 return -EOPNOTSUPP;
2303
2304         if (state == ETHTOOL_ID_ACTIVE) {
2305                 led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT;
2306                 duration = cpu_to_le16(500);
2307         } else if (state == ETHTOOL_ID_INACTIVE) {
2308                 led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT;
2309                 duration = cpu_to_le16(0);
2310         } else {
2311                 return -EINVAL;
2312         }
2313         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1);
2314         req.port_id = cpu_to_le16(pf->port_id);
2315         req.num_leds = bp->num_leds;
2316         led_cfg = (struct bnxt_led_cfg *)&req.led0_id;
2317         for (i = 0; i < bp->num_leds; i++, led_cfg++) {
2318                 req.enables |= BNXT_LED_DFLT_ENABLES(i);
2319                 led_cfg->led_id = bp->leds[i].led_id;
2320                 led_cfg->led_state = led_state;
2321                 led_cfg->led_blink_on = duration;
2322                 led_cfg->led_blink_off = duration;
2323                 led_cfg->led_group_id = bp->leds[i].led_group_id;
2324         }
2325         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2326         if (rc)
2327                 rc = -EIO;
2328         return rc;
2329 }
2330
2331 static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring)
2332 {
2333         struct hwrm_selftest_irq_input req = {0};
2334
2335         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1);
2336         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2337 }
2338
2339 static int bnxt_test_irq(struct bnxt *bp)
2340 {
2341         int i;
2342
2343         for (i = 0; i < bp->cp_nr_rings; i++) {
2344                 u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id;
2345                 int rc;
2346
2347                 rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring);
2348                 if (rc)
2349                         return rc;
2350         }
2351         return 0;
2352 }
2353
2354 static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable)
2355 {
2356         struct hwrm_port_mac_cfg_input req = {0};
2357
2358         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
2359
2360         req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK);
2361         if (enable)
2362                 req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL;
2363         else
2364                 req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE;
2365         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2366 }
2367
2368 static int bnxt_disable_an_for_lpbk(struct bnxt *bp,
2369                                     struct hwrm_port_phy_cfg_input *req)
2370 {
2371         struct bnxt_link_info *link_info = &bp->link_info;
2372         u16 fw_advertising = link_info->advertising;
2373         u16 fw_speed;
2374         int rc;
2375
2376         if (!link_info->autoneg)
2377                 return 0;
2378
2379         fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB;
2380         if (netif_carrier_ok(bp->dev))
2381                 fw_speed = bp->link_info.link_speed;
2382         else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB)
2383                 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB;
2384         else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB)
2385                 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB;
2386         else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB)
2387                 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB;
2388         else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB)
2389                 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB;
2390
2391         req->force_link_speed = cpu_to_le16(fw_speed);
2392         req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE |
2393                                   PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
2394         rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT);
2395         req->flags = 0;
2396         req->force_link_speed = cpu_to_le16(0);
2397         return rc;
2398 }
2399
2400 static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable)
2401 {
2402         struct hwrm_port_phy_cfg_input req = {0};
2403
2404         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
2405
2406         if (enable) {
2407                 bnxt_disable_an_for_lpbk(bp, &req);
2408                 req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL;
2409         } else {
2410                 req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE;
2411         }
2412         req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK);
2413         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2414 }
2415
2416 static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_napi *bnapi,
2417                             u32 raw_cons, int pkt_size)
2418 {
2419         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2420         struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2421         struct bnxt_sw_rx_bd *rx_buf;
2422         struct rx_cmp *rxcmp;
2423         u16 cp_cons, cons;
2424         u8 *data;
2425         u32 len;
2426         int i;
2427
2428         cp_cons = RING_CMP(raw_cons);
2429         rxcmp = (struct rx_cmp *)
2430                 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2431         cons = rxcmp->rx_cmp_opaque;
2432         rx_buf = &rxr->rx_buf_ring[cons];
2433         data = rx_buf->data_ptr;
2434         len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
2435         if (len != pkt_size)
2436                 return -EIO;
2437         i = ETH_ALEN;
2438         if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr))
2439                 return -EIO;
2440         i += ETH_ALEN;
2441         for (  ; i < pkt_size; i++) {
2442                 if (data[i] != (u8)(i & 0xff))
2443                         return -EIO;
2444         }
2445         return 0;
2446 }
2447
2448 static int bnxt_poll_loopback(struct bnxt *bp, int pkt_size)
2449 {
2450         struct bnxt_napi *bnapi = bp->bnapi[0];
2451         struct bnxt_cp_ring_info *cpr;
2452         struct tx_cmp *txcmp;
2453         int rc = -EIO;
2454         u32 raw_cons;
2455         u32 cons;
2456         int i;
2457
2458         cpr = &bnapi->cp_ring;
2459         raw_cons = cpr->cp_raw_cons;
2460         for (i = 0; i < 200; i++) {
2461                 cons = RING_CMP(raw_cons);
2462                 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2463
2464                 if (!TX_CMP_VALID(txcmp, raw_cons)) {
2465                         udelay(5);
2466                         continue;
2467                 }
2468
2469                 /* The valid test of the entry must be done first before
2470                  * reading any further.
2471                  */
2472                 dma_rmb();
2473                 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) {
2474                         rc = bnxt_rx_loopback(bp, bnapi, raw_cons, pkt_size);
2475                         raw_cons = NEXT_RAW_CMP(raw_cons);
2476                         raw_cons = NEXT_RAW_CMP(raw_cons);
2477                         break;
2478                 }
2479                 raw_cons = NEXT_RAW_CMP(raw_cons);
2480         }
2481         cpr->cp_raw_cons = raw_cons;
2482         return rc;
2483 }
2484
2485 static int bnxt_run_loopback(struct bnxt *bp)
2486 {
2487         struct bnxt_tx_ring_info *txr = &bp->tx_ring[0];
2488         int pkt_size, i = 0;
2489         struct sk_buff *skb;
2490         dma_addr_t map;
2491         u8 *data;
2492         int rc;
2493
2494         pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh);
2495         skb = netdev_alloc_skb(bp->dev, pkt_size);
2496         if (!skb)
2497                 return -ENOMEM;
2498         data = skb_put(skb, pkt_size);
2499         eth_broadcast_addr(data);
2500         i += ETH_ALEN;
2501         ether_addr_copy(&data[i], bp->dev->dev_addr);
2502         i += ETH_ALEN;
2503         for ( ; i < pkt_size; i++)
2504                 data[i] = (u8)(i & 0xff);
2505
2506         map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size,
2507                              PCI_DMA_TODEVICE);
2508         if (dma_mapping_error(&bp->pdev->dev, map)) {
2509                 dev_kfree_skb(skb);
2510                 return -EIO;
2511         }
2512         bnxt_xmit_xdp(bp, txr, map, pkt_size, 0);
2513
2514         /* Sync BD data before updating doorbell */
2515         wmb();
2516
2517         bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | txr->tx_prod);
2518         rc = bnxt_poll_loopback(bp, pkt_size);
2519
2520         dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE);
2521         dev_kfree_skb(skb);
2522         return rc;
2523 }
2524
2525 static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results)
2526 {
2527         struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr;
2528         struct hwrm_selftest_exec_input req = {0};
2529         int rc;
2530
2531         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1);
2532         mutex_lock(&bp->hwrm_cmd_lock);
2533         resp->test_success = 0;
2534         req.flags = test_mask;
2535         rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout);
2536         *test_results = resp->test_success;
2537         mutex_unlock(&bp->hwrm_cmd_lock);
2538         return rc;
2539 }
2540
2541 #define BNXT_DRV_TESTS                  3
2542 #define BNXT_MACLPBK_TEST_IDX           (bp->num_tests - BNXT_DRV_TESTS)
2543 #define BNXT_PHYLPBK_TEST_IDX           (BNXT_MACLPBK_TEST_IDX + 1)
2544 #define BNXT_IRQ_TEST_IDX               (BNXT_MACLPBK_TEST_IDX + 2)
2545
2546 static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest,
2547                            u64 *buf)
2548 {
2549         struct bnxt *bp = netdev_priv(dev);
2550         bool offline = false;
2551         u8 test_results = 0;
2552         u8 test_mask = 0;
2553         int rc, i;
2554
2555         if (!bp->num_tests || !BNXT_SINGLE_PF(bp))
2556                 return;
2557         memset(buf, 0, sizeof(u64) * bp->num_tests);
2558         if (!netif_running(dev)) {
2559                 etest->flags |= ETH_TEST_FL_FAILED;
2560                 return;
2561         }
2562
2563         if (etest->flags & ETH_TEST_FL_OFFLINE) {
2564                 if (bp->pf.active_vfs) {
2565                         etest->flags |= ETH_TEST_FL_FAILED;
2566                         netdev_warn(dev, "Offline tests cannot be run with active VFs\n");
2567                         return;
2568                 }
2569                 offline = true;
2570         }
2571
2572         for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2573                 u8 bit_val = 1 << i;
2574
2575                 if (!(bp->test_info->offline_mask & bit_val))
2576                         test_mask |= bit_val;
2577                 else if (offline)
2578                         test_mask |= bit_val;
2579         }
2580         if (!offline) {
2581                 bnxt_run_fw_tests(bp, test_mask, &test_results);
2582         } else {
2583                 rc = bnxt_close_nic(bp, false, false);
2584                 if (rc)
2585                         return;
2586                 bnxt_run_fw_tests(bp, test_mask, &test_results);
2587
2588                 buf[BNXT_MACLPBK_TEST_IDX] = 1;
2589                 bnxt_hwrm_mac_loopback(bp, true);
2590                 msleep(250);
2591                 rc = bnxt_half_open_nic(bp);
2592                 if (rc) {
2593                         bnxt_hwrm_mac_loopback(bp, false);
2594                         etest->flags |= ETH_TEST_FL_FAILED;
2595                         return;
2596                 }
2597                 if (bnxt_run_loopback(bp))
2598                         etest->flags |= ETH_TEST_FL_FAILED;
2599                 else
2600                         buf[BNXT_MACLPBK_TEST_IDX] = 0;
2601
2602                 bnxt_hwrm_mac_loopback(bp, false);
2603                 bnxt_hwrm_phy_loopback(bp, true);
2604                 msleep(1000);
2605                 if (bnxt_run_loopback(bp)) {
2606                         buf[BNXT_PHYLPBK_TEST_IDX] = 1;
2607                         etest->flags |= ETH_TEST_FL_FAILED;
2608                 }
2609                 bnxt_hwrm_phy_loopback(bp, false);
2610                 bnxt_half_close_nic(bp);
2611                 bnxt_open_nic(bp, false, true);
2612         }
2613         if (bnxt_test_irq(bp)) {
2614                 buf[BNXT_IRQ_TEST_IDX] = 1;
2615                 etest->flags |= ETH_TEST_FL_FAILED;
2616         }
2617         for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) {
2618                 u8 bit_val = 1 << i;
2619
2620                 if ((test_mask & bit_val) && !(test_results & bit_val)) {
2621                         buf[i] = 1;
2622                         etest->flags |= ETH_TEST_FL_FAILED;
2623                 }
2624         }
2625 }
2626
2627 static int bnxt_reset(struct net_device *dev, u32 *flags)
2628 {
2629         struct bnxt *bp = netdev_priv(dev);
2630         int rc = 0;
2631
2632         if (!BNXT_PF(bp)) {
2633                 netdev_err(dev, "Reset is not supported from a VF\n");
2634                 return -EOPNOTSUPP;
2635         }
2636
2637         if (pci_vfs_assigned(bp->pdev)) {
2638                 netdev_err(dev,
2639                            "Reset not allowed when VFs are assigned to VMs\n");
2640                 return -EBUSY;
2641         }
2642
2643         if (*flags == ETH_RESET_ALL) {
2644                 /* This feature is not supported in older firmware versions */
2645                 if (bp->hwrm_spec_code < 0x10803)
2646                         return -EOPNOTSUPP;
2647
2648                 rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_CHIP);
2649                 if (!rc) {
2650                         netdev_info(dev, "Reset request successful. Reload driver to complete reset\n");
2651                         *flags = 0;
2652                 }
2653         } else if (*flags == ETH_RESET_AP) {
2654                 /* This feature is not supported in older firmware versions */
2655                 if (bp->hwrm_spec_code < 0x10803)
2656                         return -EOPNOTSUPP;
2657
2658                 rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_AP);
2659                 if (!rc) {
2660                         netdev_info(dev, "Reset Application Processor request successful.\n");
2661                         *flags = 0;
2662                 }
2663         } else {
2664                 rc = -EINVAL;
2665         }
2666
2667         return rc;
2668 }
2669
2670 void bnxt_ethtool_init(struct bnxt *bp)
2671 {
2672         struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr;
2673         struct hwrm_selftest_qlist_input req = {0};
2674         struct bnxt_test_info *test_info;
2675         struct net_device *dev = bp->dev;
2676         int i, rc;
2677
2678         bnxt_get_pkgver(dev);
2679
2680         if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp))
2681                 return;
2682
2683         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1);
2684         mutex_lock(&bp->hwrm_cmd_lock);
2685         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2686         if (rc)
2687                 goto ethtool_init_exit;
2688
2689         test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL);
2690         if (!test_info)
2691                 goto ethtool_init_exit;
2692
2693         bp->test_info = test_info;
2694         bp->num_tests = resp->num_tests + BNXT_DRV_TESTS;
2695         if (bp->num_tests > BNXT_MAX_TEST)
2696                 bp->num_tests = BNXT_MAX_TEST;
2697
2698         test_info->offline_mask = resp->offline_tests;
2699         test_info->timeout = le16_to_cpu(resp->test_timeout);
2700         if (!test_info->timeout)
2701                 test_info->timeout = HWRM_CMD_TIMEOUT;
2702         for (i = 0; i < bp->num_tests; i++) {
2703                 char *str = test_info->string[i];
2704                 char *fw_str = resp->test0_name + i * 32;
2705
2706                 if (i == BNXT_MACLPBK_TEST_IDX) {
2707                         strcpy(str, "Mac loopback test (offline)");
2708                 } else if (i == BNXT_PHYLPBK_TEST_IDX) {
2709                         strcpy(str, "Phy loopback test (offline)");
2710                 } else if (i == BNXT_IRQ_TEST_IDX) {
2711                         strcpy(str, "Interrupt_test (offline)");
2712                 } else {
2713                         strlcpy(str, fw_str, ETH_GSTRING_LEN);
2714                         strncat(str, " test", ETH_GSTRING_LEN - strlen(str));
2715                         if (test_info->offline_mask & (1 << i))
2716                                 strncat(str, " (offline)",
2717                                         ETH_GSTRING_LEN - strlen(str));
2718                         else
2719                                 strncat(str, " (online)",
2720                                         ETH_GSTRING_LEN - strlen(str));
2721                 }
2722         }
2723
2724 ethtool_init_exit:
2725         mutex_unlock(&bp->hwrm_cmd_lock);
2726 }
2727
2728 void bnxt_ethtool_free(struct bnxt *bp)
2729 {
2730         kfree(bp->test_info);
2731         bp->test_info = NULL;
2732 }
2733
2734 const struct ethtool_ops bnxt_ethtool_ops = {
2735         .get_link_ksettings     = bnxt_get_link_ksettings,
2736         .set_link_ksettings     = bnxt_set_link_ksettings,
2737         .get_pauseparam         = bnxt_get_pauseparam,
2738         .set_pauseparam         = bnxt_set_pauseparam,
2739         .get_drvinfo            = bnxt_get_drvinfo,
2740         .get_wol                = bnxt_get_wol,
2741         .set_wol                = bnxt_set_wol,
2742         .get_coalesce           = bnxt_get_coalesce,
2743         .set_coalesce           = bnxt_set_coalesce,
2744         .get_msglevel           = bnxt_get_msglevel,
2745         .set_msglevel           = bnxt_set_msglevel,
2746         .get_sset_count         = bnxt_get_sset_count,
2747         .get_strings            = bnxt_get_strings,
2748         .get_ethtool_stats      = bnxt_get_ethtool_stats,
2749         .set_ringparam          = bnxt_set_ringparam,
2750         .get_ringparam          = bnxt_get_ringparam,
2751         .get_channels           = bnxt_get_channels,
2752         .set_channels           = bnxt_set_channels,
2753         .get_rxnfc              = bnxt_get_rxnfc,
2754         .set_rxnfc              = bnxt_set_rxnfc,
2755         .get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
2756         .get_rxfh_key_size      = bnxt_get_rxfh_key_size,
2757         .get_rxfh               = bnxt_get_rxfh,
2758         .flash_device           = bnxt_flash_device,
2759         .get_eeprom_len         = bnxt_get_eeprom_len,
2760         .get_eeprom             = bnxt_get_eeprom,
2761         .set_eeprom             = bnxt_set_eeprom,
2762         .get_link               = bnxt_get_link,
2763         .get_eee                = bnxt_get_eee,
2764         .set_eee                = bnxt_set_eee,
2765         .get_module_info        = bnxt_get_module_info,
2766         .get_module_eeprom      = bnxt_get_module_eeprom,
2767         .nway_reset             = bnxt_nway_reset,
2768         .set_phys_id            = bnxt_set_phys_id,
2769         .self_test              = bnxt_self_test,
2770         .reset                  = bnxt_reset,
2771 };