]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/broadcom/bnxt/bnxt_ethtool.c
e6b11969c9a0fe64a08b3c2ceb16f85299d80070
[karo-tx-linux.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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9
10 #include <linux/ctype.h>
11 #include <linux/stringify.h>
12 #include <linux/ethtool.h>
13 #include <linux/interrupt.h>
14 #include <linux/pci.h>
15 #include <linux/etherdevice.h>
16 #include <linux/crc32.h>
17 #include <linux/firmware.h>
18 #include "bnxt_hsi.h"
19 #include "bnxt.h"
20 #include "bnxt_ethtool.h"
21 #include "bnxt_nvm_defs.h"      /* NVRAM content constant and structure defs */
22 #include "bnxt_fw_hdr.h"        /* Firmware hdr constant and structure defs */
23 #define FLASH_NVRAM_TIMEOUT     ((HWRM_CMD_TIMEOUT) * 100)
24 #define FLASH_PACKAGE_TIMEOUT   ((HWRM_CMD_TIMEOUT) * 200)
25 #define INSTALL_PACKAGE_TIMEOUT ((HWRM_CMD_TIMEOUT) * 200)
26
27 static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen);
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
48         memset(coal, 0, sizeof(*coal));
49
50         coal->rx_coalesce_usecs = bp->rx_coal_ticks;
51         /* 2 completion records per rx packet */
52         coal->rx_max_coalesced_frames = bp->rx_coal_bufs / 2;
53         coal->rx_coalesce_usecs_irq = bp->rx_coal_ticks_irq;
54         coal->rx_max_coalesced_frames_irq = bp->rx_coal_bufs_irq / 2;
55
56         coal->tx_coalesce_usecs = bp->tx_coal_ticks;
57         coal->tx_max_coalesced_frames = bp->tx_coal_bufs;
58         coal->tx_coalesce_usecs_irq = bp->tx_coal_ticks_irq;
59         coal->tx_max_coalesced_frames_irq = bp->tx_coal_bufs_irq;
60
61         coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
62
63         return 0;
64 }
65
66 static int bnxt_set_coalesce(struct net_device *dev,
67                              struct ethtool_coalesce *coal)
68 {
69         struct bnxt *bp = netdev_priv(dev);
70         bool update_stats = false;
71         int rc = 0;
72
73         bp->rx_coal_ticks = coal->rx_coalesce_usecs;
74         /* 2 completion records per rx packet */
75         bp->rx_coal_bufs = coal->rx_max_coalesced_frames * 2;
76         bp->rx_coal_ticks_irq = coal->rx_coalesce_usecs_irq;
77         bp->rx_coal_bufs_irq = coal->rx_max_coalesced_frames_irq * 2;
78
79         bp->tx_coal_ticks = coal->tx_coalesce_usecs;
80         bp->tx_coal_bufs = coal->tx_max_coalesced_frames;
81         bp->tx_coal_ticks_irq = coal->tx_coalesce_usecs_irq;
82         bp->tx_coal_bufs_irq = coal->tx_max_coalesced_frames_irq;
83
84         if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
85                 u32 stats_ticks = coal->stats_block_coalesce_usecs;
86
87                 stats_ticks = clamp_t(u32, stats_ticks,
88                                       BNXT_MIN_STATS_COAL_TICKS,
89                                       BNXT_MAX_STATS_COAL_TICKS);
90                 stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
91                 bp->stats_coal_ticks = stats_ticks;
92                 update_stats = true;
93         }
94
95         if (netif_running(dev)) {
96                 if (update_stats) {
97                         rc = bnxt_close_nic(bp, true, false);
98                         if (!rc)
99                                 rc = bnxt_open_nic(bp, true, false);
100                 } else {
101                         rc = bnxt_hwrm_set_coal(bp);
102                 }
103         }
104
105         return rc;
106 }
107
108 #define BNXT_NUM_STATS  21
109
110 #define BNXT_RX_STATS_ENTRY(counter)    \
111         { BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
112
113 #define BNXT_TX_STATS_ENTRY(counter)    \
114         { BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
115
116 static const struct {
117         long offset;
118         char string[ETH_GSTRING_LEN];
119 } bnxt_port_stats_arr[] = {
120         BNXT_RX_STATS_ENTRY(rx_64b_frames),
121         BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
122         BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
123         BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
124         BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
125         BNXT_RX_STATS_ENTRY(rx_1024b_1518_frames),
126         BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
127         BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
128         BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
129         BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
130         BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
131         BNXT_RX_STATS_ENTRY(rx_total_frames),
132         BNXT_RX_STATS_ENTRY(rx_ucast_frames),
133         BNXT_RX_STATS_ENTRY(rx_mcast_frames),
134         BNXT_RX_STATS_ENTRY(rx_bcast_frames),
135         BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
136         BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
137         BNXT_RX_STATS_ENTRY(rx_pause_frames),
138         BNXT_RX_STATS_ENTRY(rx_pfc_frames),
139         BNXT_RX_STATS_ENTRY(rx_align_err_frames),
140         BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
141         BNXT_RX_STATS_ENTRY(rx_jbr_frames),
142         BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
143         BNXT_RX_STATS_ENTRY(rx_tagged_frames),
144         BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
145         BNXT_RX_STATS_ENTRY(rx_good_frames),
146         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0),
147         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1),
148         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2),
149         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3),
150         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4),
151         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5),
152         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6),
153         BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7),
154         BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
155         BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
156         BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
157         BNXT_RX_STATS_ENTRY(rx_bytes),
158         BNXT_RX_STATS_ENTRY(rx_runt_bytes),
159         BNXT_RX_STATS_ENTRY(rx_runt_frames),
160
161         BNXT_TX_STATS_ENTRY(tx_64b_frames),
162         BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
163         BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
164         BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
165         BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
166         BNXT_TX_STATS_ENTRY(tx_1024b_1518_frames),
167         BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
168         BNXT_TX_STATS_ENTRY(tx_1519b_2047_frames),
169         BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
170         BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
171         BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
172         BNXT_TX_STATS_ENTRY(tx_good_frames),
173         BNXT_TX_STATS_ENTRY(tx_total_frames),
174         BNXT_TX_STATS_ENTRY(tx_ucast_frames),
175         BNXT_TX_STATS_ENTRY(tx_mcast_frames),
176         BNXT_TX_STATS_ENTRY(tx_bcast_frames),
177         BNXT_TX_STATS_ENTRY(tx_pause_frames),
178         BNXT_TX_STATS_ENTRY(tx_pfc_frames),
179         BNXT_TX_STATS_ENTRY(tx_jabber_frames),
180         BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
181         BNXT_TX_STATS_ENTRY(tx_err),
182         BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
183         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0),
184         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1),
185         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2),
186         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3),
187         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4),
188         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5),
189         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6),
190         BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7),
191         BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
192         BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
193         BNXT_TX_STATS_ENTRY(tx_total_collisions),
194         BNXT_TX_STATS_ENTRY(tx_bytes),
195 };
196
197 #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
198
199 static int bnxt_get_sset_count(struct net_device *dev, int sset)
200 {
201         struct bnxt *bp = netdev_priv(dev);
202
203         switch (sset) {
204         case ETH_SS_STATS: {
205                 int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
206
207                 if (bp->flags & BNXT_FLAG_PORT_STATS)
208                         num_stats += BNXT_NUM_PORT_STATS;
209
210                 return num_stats;
211         }
212         default:
213                 return -EOPNOTSUPP;
214         }
215 }
216
217 static void bnxt_get_ethtool_stats(struct net_device *dev,
218                                    struct ethtool_stats *stats, u64 *buf)
219 {
220         u32 i, j = 0;
221         struct bnxt *bp = netdev_priv(dev);
222         u32 buf_size = sizeof(struct ctx_hw_stats) * bp->cp_nr_rings;
223         u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
224
225         memset(buf, 0, buf_size);
226
227         if (!bp->bnapi)
228                 return;
229
230         for (i = 0; i < bp->cp_nr_rings; i++) {
231                 struct bnxt_napi *bnapi = bp->bnapi[i];
232                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
233                 __le64 *hw_stats = (__le64 *)cpr->hw_stats;
234                 int k;
235
236                 for (k = 0; k < stat_fields; j++, k++)
237                         buf[j] = le64_to_cpu(hw_stats[k]);
238                 buf[j++] = cpr->rx_l4_csum_errors;
239         }
240         if (bp->flags & BNXT_FLAG_PORT_STATS) {
241                 __le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
242
243                 for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
244                         buf[j] = le64_to_cpu(*(port_stats +
245                                                bnxt_port_stats_arr[i].offset));
246                 }
247         }
248 }
249
250 static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
251 {
252         struct bnxt *bp = netdev_priv(dev);
253         u32 i;
254
255         switch (stringset) {
256         /* The number of strings must match BNXT_NUM_STATS defined above. */
257         case ETH_SS_STATS:
258                 for (i = 0; i < bp->cp_nr_rings; i++) {
259                         sprintf(buf, "[%d]: rx_ucast_packets", i);
260                         buf += ETH_GSTRING_LEN;
261                         sprintf(buf, "[%d]: rx_mcast_packets", i);
262                         buf += ETH_GSTRING_LEN;
263                         sprintf(buf, "[%d]: rx_bcast_packets", i);
264                         buf += ETH_GSTRING_LEN;
265                         sprintf(buf, "[%d]: rx_discards", i);
266                         buf += ETH_GSTRING_LEN;
267                         sprintf(buf, "[%d]: rx_drops", i);
268                         buf += ETH_GSTRING_LEN;
269                         sprintf(buf, "[%d]: rx_ucast_bytes", i);
270                         buf += ETH_GSTRING_LEN;
271                         sprintf(buf, "[%d]: rx_mcast_bytes", i);
272                         buf += ETH_GSTRING_LEN;
273                         sprintf(buf, "[%d]: rx_bcast_bytes", i);
274                         buf += ETH_GSTRING_LEN;
275                         sprintf(buf, "[%d]: tx_ucast_packets", i);
276                         buf += ETH_GSTRING_LEN;
277                         sprintf(buf, "[%d]: tx_mcast_packets", i);
278                         buf += ETH_GSTRING_LEN;
279                         sprintf(buf, "[%d]: tx_bcast_packets", i);
280                         buf += ETH_GSTRING_LEN;
281                         sprintf(buf, "[%d]: tx_discards", i);
282                         buf += ETH_GSTRING_LEN;
283                         sprintf(buf, "[%d]: tx_drops", i);
284                         buf += ETH_GSTRING_LEN;
285                         sprintf(buf, "[%d]: tx_ucast_bytes", i);
286                         buf += ETH_GSTRING_LEN;
287                         sprintf(buf, "[%d]: tx_mcast_bytes", i);
288                         buf += ETH_GSTRING_LEN;
289                         sprintf(buf, "[%d]: tx_bcast_bytes", i);
290                         buf += ETH_GSTRING_LEN;
291                         sprintf(buf, "[%d]: tpa_packets", i);
292                         buf += ETH_GSTRING_LEN;
293                         sprintf(buf, "[%d]: tpa_bytes", i);
294                         buf += ETH_GSTRING_LEN;
295                         sprintf(buf, "[%d]: tpa_events", i);
296                         buf += ETH_GSTRING_LEN;
297                         sprintf(buf, "[%d]: tpa_aborts", i);
298                         buf += ETH_GSTRING_LEN;
299                         sprintf(buf, "[%d]: rx_l4_csum_errors", i);
300                         buf += ETH_GSTRING_LEN;
301                 }
302                 if (bp->flags & BNXT_FLAG_PORT_STATS) {
303                         for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
304                                 strcpy(buf, bnxt_port_stats_arr[i].string);
305                                 buf += ETH_GSTRING_LEN;
306                         }
307                 }
308                 break;
309         default:
310                 netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
311                            stringset);
312                 break;
313         }
314 }
315
316 static void bnxt_get_ringparam(struct net_device *dev,
317                                struct ethtool_ringparam *ering)
318 {
319         struct bnxt *bp = netdev_priv(dev);
320
321         ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
322         ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
323         ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
324
325         ering->rx_pending = bp->rx_ring_size;
326         ering->rx_jumbo_pending = bp->rx_agg_ring_size;
327         ering->tx_pending = bp->tx_ring_size;
328 }
329
330 static int bnxt_set_ringparam(struct net_device *dev,
331                               struct ethtool_ringparam *ering)
332 {
333         struct bnxt *bp = netdev_priv(dev);
334
335         if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
336             (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
337             (ering->tx_pending <= MAX_SKB_FRAGS))
338                 return -EINVAL;
339
340         if (netif_running(dev))
341                 bnxt_close_nic(bp, false, false);
342
343         bp->rx_ring_size = ering->rx_pending;
344         bp->tx_ring_size = ering->tx_pending;
345         bnxt_set_ring_params(bp);
346
347         if (netif_running(dev))
348                 return bnxt_open_nic(bp, false, false);
349
350         return 0;
351 }
352
353 static void bnxt_get_channels(struct net_device *dev,
354                               struct ethtool_channels *channel)
355 {
356         struct bnxt *bp = netdev_priv(dev);
357         int max_rx_rings, max_tx_rings, tcs;
358
359         bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
360         channel->max_combined = max_t(int, max_rx_rings, max_tx_rings);
361
362         if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
363                 max_rx_rings = 0;
364                 max_tx_rings = 0;
365         }
366
367         tcs = netdev_get_num_tc(dev);
368         if (tcs > 1)
369                 max_tx_rings /= tcs;
370
371         channel->max_rx = max_rx_rings;
372         channel->max_tx = max_tx_rings;
373         channel->max_other = 0;
374         if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
375                 channel->combined_count = bp->rx_nr_rings;
376                 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
377                         channel->combined_count--;
378         } else {
379                 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
380                         channel->rx_count = bp->rx_nr_rings;
381                         channel->tx_count = bp->tx_nr_rings_per_tc;
382                 }
383         }
384 }
385
386 static int bnxt_set_channels(struct net_device *dev,
387                              struct ethtool_channels *channel)
388 {
389         struct bnxt *bp = netdev_priv(dev);
390         int max_rx_rings, max_tx_rings, tcs;
391         u32 rc = 0;
392         bool sh = false;
393
394         if (channel->other_count)
395                 return -EINVAL;
396
397         if (!channel->combined_count &&
398             (!channel->rx_count || !channel->tx_count))
399                 return -EINVAL;
400
401         if (channel->combined_count &&
402             (channel->rx_count || channel->tx_count))
403                 return -EINVAL;
404
405         if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
406                                             channel->tx_count))
407                 return -EINVAL;
408
409         if (channel->combined_count)
410                 sh = true;
411
412         bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
413
414         tcs = netdev_get_num_tc(dev);
415         if (tcs > 1)
416                 max_tx_rings /= tcs;
417
418         if (sh &&
419             channel->combined_count > max_t(int, max_rx_rings, max_tx_rings))
420                 return -ENOMEM;
421
422         if (!sh && (channel->rx_count > max_rx_rings ||
423                     channel->tx_count > max_tx_rings))
424                 return -ENOMEM;
425
426         if (netif_running(dev)) {
427                 if (BNXT_PF(bp)) {
428                         /* TODO CHIMP_FW: Send message to all VF's
429                          * before PF unload
430                          */
431                 }
432                 rc = bnxt_close_nic(bp, true, false);
433                 if (rc) {
434                         netdev_err(bp->dev, "Set channel failure rc :%x\n",
435                                    rc);
436                         return rc;
437                 }
438         }
439
440         if (sh) {
441                 bp->flags |= BNXT_FLAG_SHARED_RINGS;
442                 bp->rx_nr_rings = min_t(int, channel->combined_count,
443                                         max_rx_rings);
444                 bp->tx_nr_rings_per_tc = min_t(int, channel->combined_count,
445                                                max_tx_rings);
446         } else {
447                 bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
448                 bp->rx_nr_rings = channel->rx_count;
449                 bp->tx_nr_rings_per_tc = channel->tx_count;
450         }
451
452         bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
453         if (tcs > 1)
454                 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
455
456         bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
457                                bp->tx_nr_rings + bp->rx_nr_rings;
458
459         bp->num_stat_ctxs = bp->cp_nr_rings;
460
461         /* After changing number of rx channels, update NTUPLE feature. */
462         netdev_update_features(dev);
463         if (netif_running(dev)) {
464                 rc = bnxt_open_nic(bp, true, false);
465                 if ((!rc) && BNXT_PF(bp)) {
466                         /* TODO CHIMP_FW: Send message to all VF's
467                          * to renable
468                          */
469                 }
470         }
471
472         return rc;
473 }
474
475 #ifdef CONFIG_RFS_ACCEL
476 static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
477                             u32 *rule_locs)
478 {
479         int i, j = 0;
480
481         cmd->data = bp->ntp_fltr_count;
482         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
483                 struct hlist_head *head;
484                 struct bnxt_ntuple_filter *fltr;
485
486                 head = &bp->ntp_fltr_hash_tbl[i];
487                 rcu_read_lock();
488                 hlist_for_each_entry_rcu(fltr, head, hash) {
489                         if (j == cmd->rule_cnt)
490                                 break;
491                         rule_locs[j++] = fltr->sw_id;
492                 }
493                 rcu_read_unlock();
494                 if (j == cmd->rule_cnt)
495                         break;
496         }
497         cmd->rule_cnt = j;
498         return 0;
499 }
500
501 static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
502 {
503         struct ethtool_rx_flow_spec *fs =
504                 (struct ethtool_rx_flow_spec *)&cmd->fs;
505         struct bnxt_ntuple_filter *fltr;
506         struct flow_keys *fkeys;
507         int i, rc = -EINVAL;
508
509         if (fs->location < 0 || fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
510                 return rc;
511
512         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
513                 struct hlist_head *head;
514
515                 head = &bp->ntp_fltr_hash_tbl[i];
516                 rcu_read_lock();
517                 hlist_for_each_entry_rcu(fltr, head, hash) {
518                         if (fltr->sw_id == fs->location)
519                                 goto fltr_found;
520                 }
521                 rcu_read_unlock();
522         }
523         return rc;
524
525 fltr_found:
526         fkeys = &fltr->fkeys;
527         if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
528                 if (fkeys->basic.ip_proto == IPPROTO_TCP)
529                         fs->flow_type = TCP_V4_FLOW;
530                 else if (fkeys->basic.ip_proto == IPPROTO_UDP)
531                         fs->flow_type = UDP_V4_FLOW;
532                 else
533                         goto fltr_err;
534
535                 fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
536                 fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
537
538                 fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
539                 fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
540
541                 fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
542                 fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
543
544                 fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
545                 fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
546         } else {
547                 int i;
548
549                 if (fkeys->basic.ip_proto == IPPROTO_TCP)
550                         fs->flow_type = TCP_V6_FLOW;
551                 else if (fkeys->basic.ip_proto == IPPROTO_UDP)
552                         fs->flow_type = UDP_V6_FLOW;
553                 else
554                         goto fltr_err;
555
556                 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] =
557                         fkeys->addrs.v6addrs.src;
558                 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] =
559                         fkeys->addrs.v6addrs.dst;
560                 for (i = 0; i < 4; i++) {
561                         fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0);
562                         fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0);
563                 }
564                 fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src;
565                 fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0);
566
567                 fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst;
568                 fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0);
569         }
570
571         fs->ring_cookie = fltr->rxq;
572         rc = 0;
573
574 fltr_err:
575         rcu_read_unlock();
576
577         return rc;
578 }
579 #endif
580
581 static u64 get_ethtool_ipv4_rss(struct bnxt *bp)
582 {
583         if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
584                 return RXH_IP_SRC | RXH_IP_DST;
585         return 0;
586 }
587
588 static u64 get_ethtool_ipv6_rss(struct bnxt *bp)
589 {
590         if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
591                 return RXH_IP_SRC | RXH_IP_DST;
592         return 0;
593 }
594
595 static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
596 {
597         cmd->data = 0;
598         switch (cmd->flow_type) {
599         case TCP_V4_FLOW:
600                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4)
601                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
602                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
603                 cmd->data |= get_ethtool_ipv4_rss(bp);
604                 break;
605         case UDP_V4_FLOW:
606                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)
607                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
608                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
609                 /* fall through */
610         case SCTP_V4_FLOW:
611         case AH_ESP_V4_FLOW:
612         case AH_V4_FLOW:
613         case ESP_V4_FLOW:
614         case IPV4_FLOW:
615                 cmd->data |= get_ethtool_ipv4_rss(bp);
616                 break;
617
618         case TCP_V6_FLOW:
619                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6)
620                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
621                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
622                 cmd->data |= get_ethtool_ipv6_rss(bp);
623                 break;
624         case UDP_V6_FLOW:
625                 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)
626                         cmd->data |= RXH_IP_SRC | RXH_IP_DST |
627                                      RXH_L4_B_0_1 | RXH_L4_B_2_3;
628                 /* fall through */
629         case SCTP_V6_FLOW:
630         case AH_ESP_V6_FLOW:
631         case AH_V6_FLOW:
632         case ESP_V6_FLOW:
633         case IPV6_FLOW:
634                 cmd->data |= get_ethtool_ipv6_rss(bp);
635                 break;
636         }
637         return 0;
638 }
639
640 #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
641 #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST)
642
643 static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd)
644 {
645         u32 rss_hash_cfg = bp->rss_hash_cfg;
646         int tuple, rc = 0;
647
648         if (cmd->data == RXH_4TUPLE)
649                 tuple = 4;
650         else if (cmd->data == RXH_2TUPLE)
651                 tuple = 2;
652         else if (!cmd->data)
653                 tuple = 0;
654         else
655                 return -EINVAL;
656
657         if (cmd->flow_type == TCP_V4_FLOW) {
658                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
659                 if (tuple == 4)
660                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4;
661         } else if (cmd->flow_type == UDP_V4_FLOW) {
662                 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
663                         return -EINVAL;
664                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
665                 if (tuple == 4)
666                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4;
667         } else if (cmd->flow_type == TCP_V6_FLOW) {
668                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
669                 if (tuple == 4)
670                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
671         } else if (cmd->flow_type == UDP_V6_FLOW) {
672                 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP))
673                         return -EINVAL;
674                 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
675                 if (tuple == 4)
676                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
677         } else if (tuple == 4) {
678                 return -EINVAL;
679         }
680
681         switch (cmd->flow_type) {
682         case TCP_V4_FLOW:
683         case UDP_V4_FLOW:
684         case SCTP_V4_FLOW:
685         case AH_ESP_V4_FLOW:
686         case AH_V4_FLOW:
687         case ESP_V4_FLOW:
688         case IPV4_FLOW:
689                 if (tuple == 2)
690                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
691                 else if (!tuple)
692                         rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4;
693                 break;
694
695         case TCP_V6_FLOW:
696         case UDP_V6_FLOW:
697         case SCTP_V6_FLOW:
698         case AH_ESP_V6_FLOW:
699         case AH_V6_FLOW:
700         case ESP_V6_FLOW:
701         case IPV6_FLOW:
702                 if (tuple == 2)
703                         rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
704                 else if (!tuple)
705                         rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6;
706                 break;
707         }
708
709         if (bp->rss_hash_cfg == rss_hash_cfg)
710                 return 0;
711
712         bp->rss_hash_cfg = rss_hash_cfg;
713         if (netif_running(bp->dev)) {
714                 bnxt_close_nic(bp, false, false);
715                 rc = bnxt_open_nic(bp, false, false);
716         }
717         return rc;
718 }
719
720 static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
721                           u32 *rule_locs)
722 {
723         struct bnxt *bp = netdev_priv(dev);
724         int rc = 0;
725
726         switch (cmd->cmd) {
727 #ifdef CONFIG_RFS_ACCEL
728         case ETHTOOL_GRXRINGS:
729                 cmd->data = bp->rx_nr_rings;
730                 break;
731
732         case ETHTOOL_GRXCLSRLCNT:
733                 cmd->rule_cnt = bp->ntp_fltr_count;
734                 cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
735                 break;
736
737         case ETHTOOL_GRXCLSRLALL:
738                 rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
739                 break;
740
741         case ETHTOOL_GRXCLSRULE:
742                 rc = bnxt_grxclsrule(bp, cmd);
743                 break;
744 #endif
745
746         case ETHTOOL_GRXFH:
747                 rc = bnxt_grxfh(bp, cmd);
748                 break;
749
750         default:
751                 rc = -EOPNOTSUPP;
752                 break;
753         }
754
755         return rc;
756 }
757
758 static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
759 {
760         struct bnxt *bp = netdev_priv(dev);
761         int rc;
762
763         switch (cmd->cmd) {
764         case ETHTOOL_SRXFH:
765                 rc = bnxt_srxfh(bp, cmd);
766                 break;
767
768         default:
769                 rc = -EOPNOTSUPP;
770                 break;
771         }
772         return rc;
773 }
774
775 static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
776 {
777         return HW_HASH_INDEX_SIZE;
778 }
779
780 static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
781 {
782         return HW_HASH_KEY_SIZE;
783 }
784
785 static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
786                          u8 *hfunc)
787 {
788         struct bnxt *bp = netdev_priv(dev);
789         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
790         int i = 0;
791
792         if (hfunc)
793                 *hfunc = ETH_RSS_HASH_TOP;
794
795         if (indir)
796                 for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
797                         indir[i] = le16_to_cpu(vnic->rss_table[i]);
798
799         if (key)
800                 memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
801
802         return 0;
803 }
804
805 static void bnxt_get_drvinfo(struct net_device *dev,
806                              struct ethtool_drvinfo *info)
807 {
808         struct bnxt *bp = netdev_priv(dev);
809         char *pkglog;
810         char *pkgver = NULL;
811
812         pkglog = kmalloc(BNX_PKG_LOG_MAX_LENGTH, GFP_KERNEL);
813         if (pkglog)
814                 pkgver = bnxt_get_pkgver(dev, pkglog, BNX_PKG_LOG_MAX_LENGTH);
815         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
816         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
817         if (pkgver && *pkgver != 0 && isdigit(*pkgver))
818                 snprintf(info->fw_version, sizeof(info->fw_version) - 1,
819                          "%s pkg %s", bp->fw_ver_str, pkgver);
820         else
821                 strlcpy(info->fw_version, bp->fw_ver_str,
822                         sizeof(info->fw_version));
823         strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
824         info->n_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
825         info->testinfo_len = BNXT_NUM_TESTS(bp);
826         /* TODO CHIMP_FW: eeprom dump details */
827         info->eedump_len = 0;
828         /* TODO CHIMP FW: reg dump details */
829         info->regdump_len = 0;
830         kfree(pkglog);
831 }
832
833 u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
834 {
835         u32 speed_mask = 0;
836
837         /* TODO: support 25GB, 40GB, 50GB with different cable type */
838         /* set the advertised speeds */
839         if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
840                 speed_mask |= ADVERTISED_100baseT_Full;
841         if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
842                 speed_mask |= ADVERTISED_1000baseT_Full;
843         if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
844                 speed_mask |= ADVERTISED_2500baseX_Full;
845         if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
846                 speed_mask |= ADVERTISED_10000baseT_Full;
847         if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
848                 speed_mask |= ADVERTISED_40000baseCR4_Full;
849
850         if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
851                 speed_mask |= ADVERTISED_Pause;
852         else if (fw_pause & BNXT_LINK_PAUSE_TX)
853                 speed_mask |= ADVERTISED_Asym_Pause;
854         else if (fw_pause & BNXT_LINK_PAUSE_RX)
855                 speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
856
857         return speed_mask;
858 }
859
860 #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
861 {                                                                       \
862         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)                    \
863                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
864                                                      100baseT_Full);    \
865         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)                      \
866                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
867                                                      1000baseT_Full);   \
868         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)                     \
869                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
870                                                      10000baseT_Full);  \
871         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)                     \
872                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
873                                                      25000baseCR_Full); \
874         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)                     \
875                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
876                                                      40000baseCR4_Full);\
877         if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)                     \
878                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
879                                                      50000baseCR2_Full);\
880         if ((fw_pause) & BNXT_LINK_PAUSE_RX) {                          \
881                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
882                                                      Pause);            \
883                 if (!((fw_pause) & BNXT_LINK_PAUSE_TX))                 \
884                         ethtool_link_ksettings_add_link_mode(           \
885                                         lk_ksettings, name, Asym_Pause);\
886         } else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {                   \
887                 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
888                                                      Asym_Pause);       \
889         }                                                               \
890 }
891
892 #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)          \
893 {                                                                       \
894         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
895                                                   100baseT_Full) ||     \
896             ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
897                                                   100baseT_Half))       \
898                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;               \
899         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
900                                                   1000baseT_Full) ||    \
901             ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
902                                                   1000baseT_Half))      \
903                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;                 \
904         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
905                                                   10000baseT_Full))     \
906                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;                \
907         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
908                                                   25000baseCR_Full))    \
909                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;                \
910         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
911                                                   40000baseCR4_Full))   \
912                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;                \
913         if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
914                                                   50000baseCR2_Full))   \
915                 (fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;                \
916 }
917
918 static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
919                                 struct ethtool_link_ksettings *lk_ksettings)
920 {
921         u16 fw_speeds = link_info->advertising;
922         u8 fw_pause = 0;
923
924         if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
925                 fw_pause = link_info->auto_pause_setting;
926
927         BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
928 }
929
930 static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
931                                 struct ethtool_link_ksettings *lk_ksettings)
932 {
933         u16 fw_speeds = link_info->lp_auto_link_speeds;
934         u8 fw_pause = 0;
935
936         if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
937                 fw_pause = link_info->lp_pause;
938
939         BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
940                                 lp_advertising);
941 }
942
943 static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
944                                 struct ethtool_link_ksettings *lk_ksettings)
945 {
946         u16 fw_speeds = link_info->support_speeds;
947
948         BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
949
950         ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
951         ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
952                                              Asym_Pause);
953
954         if (link_info->support_auto_speeds)
955                 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
956                                                      Autoneg);
957 }
958
959 u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
960 {
961         switch (fw_link_speed) {
962         case BNXT_LINK_SPEED_100MB:
963                 return SPEED_100;
964         case BNXT_LINK_SPEED_1GB:
965                 return SPEED_1000;
966         case BNXT_LINK_SPEED_2_5GB:
967                 return SPEED_2500;
968         case BNXT_LINK_SPEED_10GB:
969                 return SPEED_10000;
970         case BNXT_LINK_SPEED_20GB:
971                 return SPEED_20000;
972         case BNXT_LINK_SPEED_25GB:
973                 return SPEED_25000;
974         case BNXT_LINK_SPEED_40GB:
975                 return SPEED_40000;
976         case BNXT_LINK_SPEED_50GB:
977                 return SPEED_50000;
978         default:
979                 return SPEED_UNKNOWN;
980         }
981 }
982
983 static int bnxt_get_link_ksettings(struct net_device *dev,
984                                    struct ethtool_link_ksettings *lk_ksettings)
985 {
986         struct bnxt *bp = netdev_priv(dev);
987         struct bnxt_link_info *link_info = &bp->link_info;
988         struct ethtool_link_settings *base = &lk_ksettings->base;
989         u32 ethtool_speed;
990
991         ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
992         bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
993
994         ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
995         if (link_info->autoneg) {
996                 bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
997                 ethtool_link_ksettings_add_link_mode(lk_ksettings,
998                                                      advertising, Autoneg);
999                 base->autoneg = AUTONEG_ENABLE;
1000                 if (link_info->phy_link_status == BNXT_LINK_LINK)
1001                         bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
1002                 ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
1003                 if (!netif_carrier_ok(dev))
1004                         base->duplex = DUPLEX_UNKNOWN;
1005                 else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
1006                         base->duplex = DUPLEX_FULL;
1007                 else
1008                         base->duplex = DUPLEX_HALF;
1009         } else {
1010                 base->autoneg = AUTONEG_DISABLE;
1011                 ethtool_speed =
1012                         bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
1013                 base->duplex = DUPLEX_HALF;
1014                 if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
1015                         base->duplex = DUPLEX_FULL;
1016         }
1017         base->speed = ethtool_speed;
1018
1019         base->port = PORT_NONE;
1020         if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
1021                 base->port = PORT_TP;
1022                 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1023                                                      TP);
1024                 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
1025                                                      TP);
1026         } else {
1027                 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
1028                                                      FIBRE);
1029                 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
1030                                                      FIBRE);
1031
1032                 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
1033                         base->port = PORT_DA;
1034                 else if (link_info->media_type ==
1035                          PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
1036                         base->port = PORT_FIBRE;
1037         }
1038         base->phy_address = link_info->phy_addr;
1039
1040         return 0;
1041 }
1042
1043 static u32 bnxt_get_fw_speed(struct net_device *dev, u16 ethtool_speed)
1044 {
1045         struct bnxt *bp = netdev_priv(dev);
1046         struct bnxt_link_info *link_info = &bp->link_info;
1047         u16 support_spds = link_info->support_speeds;
1048         u32 fw_speed = 0;
1049
1050         switch (ethtool_speed) {
1051         case SPEED_100:
1052                 if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
1053                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
1054                 break;
1055         case SPEED_1000:
1056                 if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
1057                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
1058                 break;
1059         case SPEED_2500:
1060                 if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
1061                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
1062                 break;
1063         case SPEED_10000:
1064                 if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
1065                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
1066                 break;
1067         case SPEED_20000:
1068                 if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
1069                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
1070                 break;
1071         case SPEED_25000:
1072                 if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
1073                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
1074                 break;
1075         case SPEED_40000:
1076                 if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
1077                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
1078                 break;
1079         case SPEED_50000:
1080                 if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
1081                         fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
1082                 break;
1083         default:
1084                 netdev_err(dev, "unsupported speed!\n");
1085                 break;
1086         }
1087         return fw_speed;
1088 }
1089
1090 u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
1091 {
1092         u16 fw_speed_mask = 0;
1093
1094         /* only support autoneg at speed 100, 1000, and 10000 */
1095         if (advertising & (ADVERTISED_100baseT_Full |
1096                            ADVERTISED_100baseT_Half)) {
1097                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
1098         }
1099         if (advertising & (ADVERTISED_1000baseT_Full |
1100                            ADVERTISED_1000baseT_Half)) {
1101                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
1102         }
1103         if (advertising & ADVERTISED_10000baseT_Full)
1104                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
1105
1106         if (advertising & ADVERTISED_40000baseCR4_Full)
1107                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
1108
1109         return fw_speed_mask;
1110 }
1111
1112 static int bnxt_set_link_ksettings(struct net_device *dev,
1113                            const struct ethtool_link_ksettings *lk_ksettings)
1114 {
1115         struct bnxt *bp = netdev_priv(dev);
1116         struct bnxt_link_info *link_info = &bp->link_info;
1117         const struct ethtool_link_settings *base = &lk_ksettings->base;
1118         bool set_pause = false;
1119         u16 fw_advertising = 0;
1120         u32 speed;
1121         int rc = 0;
1122
1123         if (!BNXT_SINGLE_PF(bp))
1124                 return -EOPNOTSUPP;
1125
1126         if (base->autoneg == AUTONEG_ENABLE) {
1127                 BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
1128                                         advertising);
1129                 link_info->autoneg |= BNXT_AUTONEG_SPEED;
1130                 if (!fw_advertising)
1131                         link_info->advertising = link_info->support_auto_speeds;
1132                 else
1133                         link_info->advertising = fw_advertising;
1134                 /* any change to autoneg will cause link change, therefore the
1135                  * driver should put back the original pause setting in autoneg
1136                  */
1137                 set_pause = true;
1138         } else {
1139                 u16 fw_speed;
1140                 u8 phy_type = link_info->phy_type;
1141
1142                 if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
1143                     phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
1144                     link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
1145                         netdev_err(dev, "10GBase-T devices must autoneg\n");
1146                         rc = -EINVAL;
1147                         goto set_setting_exit;
1148                 }
1149                 if (base->duplex == DUPLEX_HALF) {
1150                         netdev_err(dev, "HALF DUPLEX is not supported!\n");
1151                         rc = -EINVAL;
1152                         goto set_setting_exit;
1153                 }
1154                 speed = base->speed;
1155                 fw_speed = bnxt_get_fw_speed(dev, speed);
1156                 if (!fw_speed) {
1157                         rc = -EINVAL;
1158                         goto set_setting_exit;
1159                 }
1160                 link_info->req_link_speed = fw_speed;
1161                 link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
1162                 link_info->autoneg = 0;
1163                 link_info->advertising = 0;
1164         }
1165
1166         if (netif_running(dev))
1167                 rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
1168
1169 set_setting_exit:
1170         return rc;
1171 }
1172
1173 static void bnxt_get_pauseparam(struct net_device *dev,
1174                                 struct ethtool_pauseparam *epause)
1175 {
1176         struct bnxt *bp = netdev_priv(dev);
1177         struct bnxt_link_info *link_info = &bp->link_info;
1178
1179         if (BNXT_VF(bp))
1180                 return;
1181         epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
1182         epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
1183         epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
1184 }
1185
1186 static int bnxt_set_pauseparam(struct net_device *dev,
1187                                struct ethtool_pauseparam *epause)
1188 {
1189         int rc = 0;
1190         struct bnxt *bp = netdev_priv(dev);
1191         struct bnxt_link_info *link_info = &bp->link_info;
1192
1193         if (!BNXT_SINGLE_PF(bp))
1194                 return -EOPNOTSUPP;
1195
1196         if (epause->autoneg) {
1197                 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
1198                         return -EINVAL;
1199
1200                 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1201                 if (bp->hwrm_spec_code >= 0x10201)
1202                         link_info->req_flow_ctrl =
1203                                 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1204         } else {
1205                 /* when transition from auto pause to force pause,
1206                  * force a link change
1207                  */
1208                 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1209                         link_info->force_link_chng = true;
1210                 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1211                 link_info->req_flow_ctrl = 0;
1212         }
1213         if (epause->rx_pause)
1214                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1215
1216         if (epause->tx_pause)
1217                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1218
1219         if (netif_running(dev))
1220                 rc = bnxt_hwrm_set_pause(bp);
1221         return rc;
1222 }
1223
1224 static u32 bnxt_get_link(struct net_device *dev)
1225 {
1226         struct bnxt *bp = netdev_priv(dev);
1227
1228         /* TODO: handle MF, VF, driver close case */
1229         return bp->link_info.link_up;
1230 }
1231
1232 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1233                                 u16 ext, u16 *index, u32 *item_length,
1234                                 u32 *data_length);
1235
1236 static int bnxt_flash_nvram(struct net_device *dev,
1237                             u16 dir_type,
1238                             u16 dir_ordinal,
1239                             u16 dir_ext,
1240                             u16 dir_attr,
1241                             const u8 *data,
1242                             size_t data_len)
1243 {
1244         struct bnxt *bp = netdev_priv(dev);
1245         int rc;
1246         struct hwrm_nvm_write_input req = {0};
1247         dma_addr_t dma_handle;
1248         u8 *kmem;
1249
1250         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1251
1252         req.dir_type = cpu_to_le16(dir_type);
1253         req.dir_ordinal = cpu_to_le16(dir_ordinal);
1254         req.dir_ext = cpu_to_le16(dir_ext);
1255         req.dir_attr = cpu_to_le16(dir_attr);
1256         req.dir_data_length = cpu_to_le32(data_len);
1257
1258         kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1259                                   GFP_KERNEL);
1260         if (!kmem) {
1261                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1262                            (unsigned)data_len);
1263                 return -ENOMEM;
1264         }
1265         memcpy(kmem, data, data_len);
1266         req.host_src_addr = cpu_to_le64(dma_handle);
1267
1268         rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1269         dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1270
1271         return rc;
1272 }
1273
1274 static int bnxt_firmware_reset(struct net_device *dev,
1275                                u16 dir_type)
1276 {
1277         struct bnxt *bp = netdev_priv(dev);
1278         struct hwrm_fw_reset_input req = {0};
1279
1280         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1281
1282         /* TODO: Support ASAP ChiMP self-reset (e.g. upon PF driver unload) */
1283         /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1284         /*       (e.g. when firmware isn't already running) */
1285         switch (dir_type) {
1286         case BNX_DIR_TYPE_CHIMP_PATCH:
1287         case BNX_DIR_TYPE_BOOTCODE:
1288         case BNX_DIR_TYPE_BOOTCODE_2:
1289                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1290                 /* Self-reset ChiMP upon next PCIe reset: */
1291                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1292                 break;
1293         case BNX_DIR_TYPE_APE_FW:
1294         case BNX_DIR_TYPE_APE_PATCH:
1295                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
1296                 /* Self-reset APE upon next PCIe reset: */
1297                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1298                 break;
1299         case BNX_DIR_TYPE_KONG_FW:
1300         case BNX_DIR_TYPE_KONG_PATCH:
1301                 req.embedded_proc_type =
1302                         FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1303                 break;
1304         case BNX_DIR_TYPE_BONO_FW:
1305         case BNX_DIR_TYPE_BONO_PATCH:
1306                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1307                 break;
1308         default:
1309                 return -EINVAL;
1310         }
1311
1312         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1313 }
1314
1315 static int bnxt_flash_firmware(struct net_device *dev,
1316                                u16 dir_type,
1317                                const u8 *fw_data,
1318                                size_t fw_size)
1319 {
1320         int     rc = 0;
1321         u16     code_type;
1322         u32     stored_crc;
1323         u32     calculated_crc;
1324         struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1325
1326         switch (dir_type) {
1327         case BNX_DIR_TYPE_BOOTCODE:
1328         case BNX_DIR_TYPE_BOOTCODE_2:
1329                 code_type = CODE_BOOT;
1330                 break;
1331         case BNX_DIR_TYPE_CHIMP_PATCH:
1332                 code_type = CODE_CHIMP_PATCH;
1333                 break;
1334         case BNX_DIR_TYPE_APE_FW:
1335                 code_type = CODE_MCTP_PASSTHRU;
1336                 break;
1337         case BNX_DIR_TYPE_APE_PATCH:
1338                 code_type = CODE_APE_PATCH;
1339                 break;
1340         case BNX_DIR_TYPE_KONG_FW:
1341                 code_type = CODE_KONG_FW;
1342                 break;
1343         case BNX_DIR_TYPE_KONG_PATCH:
1344                 code_type = CODE_KONG_PATCH;
1345                 break;
1346         case BNX_DIR_TYPE_BONO_FW:
1347                 code_type = CODE_BONO_FW;
1348                 break;
1349         case BNX_DIR_TYPE_BONO_PATCH:
1350                 code_type = CODE_BONO_PATCH;
1351                 break;
1352         default:
1353                 netdev_err(dev, "Unsupported directory entry type: %u\n",
1354                            dir_type);
1355                 return -EINVAL;
1356         }
1357         if (fw_size < sizeof(struct bnxt_fw_header)) {
1358                 netdev_err(dev, "Invalid firmware file size: %u\n",
1359                            (unsigned int)fw_size);
1360                 return -EINVAL;
1361         }
1362         if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1363                 netdev_err(dev, "Invalid firmware signature: %08X\n",
1364                            le32_to_cpu(header->signature));
1365                 return -EINVAL;
1366         }
1367         if (header->code_type != code_type) {
1368                 netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1369                            code_type, header->code_type);
1370                 return -EINVAL;
1371         }
1372         if (header->device != DEVICE_CUMULUS_FAMILY) {
1373                 netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1374                            DEVICE_CUMULUS_FAMILY, header->device);
1375                 return -EINVAL;
1376         }
1377         /* Confirm the CRC32 checksum of the file: */
1378         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1379                                              sizeof(stored_crc)));
1380         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1381         if (calculated_crc != stored_crc) {
1382                 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1383                            (unsigned long)stored_crc,
1384                            (unsigned long)calculated_crc);
1385                 return -EINVAL;
1386         }
1387         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1388                               0, 0, fw_data, fw_size);
1389         if (rc == 0)    /* Firmware update successful */
1390                 rc = bnxt_firmware_reset(dev, dir_type);
1391
1392         return rc;
1393 }
1394
1395 static int bnxt_flash_microcode(struct net_device *dev,
1396                                 u16 dir_type,
1397                                 const u8 *fw_data,
1398                                 size_t fw_size)
1399 {
1400         struct bnxt_ucode_trailer *trailer;
1401         u32 calculated_crc;
1402         u32 stored_crc;
1403         int rc = 0;
1404
1405         if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
1406                 netdev_err(dev, "Invalid microcode file size: %u\n",
1407                            (unsigned int)fw_size);
1408                 return -EINVAL;
1409         }
1410         trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
1411                                                 sizeof(*trailer)));
1412         if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
1413                 netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
1414                            le32_to_cpu(trailer->sig));
1415                 return -EINVAL;
1416         }
1417         if (le16_to_cpu(trailer->dir_type) != dir_type) {
1418                 netdev_err(dev, "Expected microcode type: %d, read: %d\n",
1419                            dir_type, le16_to_cpu(trailer->dir_type));
1420                 return -EINVAL;
1421         }
1422         if (le16_to_cpu(trailer->trailer_length) <
1423                 sizeof(struct bnxt_ucode_trailer)) {
1424                 netdev_err(dev, "Invalid microcode trailer length: %d\n",
1425                            le16_to_cpu(trailer->trailer_length));
1426                 return -EINVAL;
1427         }
1428
1429         /* Confirm the CRC32 checksum of the file: */
1430         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1431                                              sizeof(stored_crc)));
1432         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1433         if (calculated_crc != stored_crc) {
1434                 netdev_err(dev,
1435                            "CRC32 (%08lX) does not match calculated: %08lX\n",
1436                            (unsigned long)stored_crc,
1437                            (unsigned long)calculated_crc);
1438                 return -EINVAL;
1439         }
1440         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1441                               0, 0, fw_data, fw_size);
1442
1443         return rc;
1444 }
1445
1446 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1447 {
1448         switch (dir_type) {
1449         case BNX_DIR_TYPE_CHIMP_PATCH:
1450         case BNX_DIR_TYPE_BOOTCODE:
1451         case BNX_DIR_TYPE_BOOTCODE_2:
1452         case BNX_DIR_TYPE_APE_FW:
1453         case BNX_DIR_TYPE_APE_PATCH:
1454         case BNX_DIR_TYPE_KONG_FW:
1455         case BNX_DIR_TYPE_KONG_PATCH:
1456         case BNX_DIR_TYPE_BONO_FW:
1457         case BNX_DIR_TYPE_BONO_PATCH:
1458                 return true;
1459         }
1460
1461         return false;
1462 }
1463
1464 static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
1465 {
1466         switch (dir_type) {
1467         case BNX_DIR_TYPE_AVS:
1468         case BNX_DIR_TYPE_EXP_ROM_MBA:
1469         case BNX_DIR_TYPE_PCIE:
1470         case BNX_DIR_TYPE_TSCF_UCODE:
1471         case BNX_DIR_TYPE_EXT_PHY:
1472         case BNX_DIR_TYPE_CCM:
1473         case BNX_DIR_TYPE_ISCSI_BOOT:
1474         case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1475         case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1476                 return true;
1477         }
1478
1479         return false;
1480 }
1481
1482 static bool bnxt_dir_type_is_executable(u16 dir_type)
1483 {
1484         return bnxt_dir_type_is_ape_bin_format(dir_type) ||
1485                 bnxt_dir_type_is_other_exec_format(dir_type);
1486 }
1487
1488 static int bnxt_flash_firmware_from_file(struct net_device *dev,
1489                                          u16 dir_type,
1490                                          const char *filename)
1491 {
1492         const struct firmware  *fw;
1493         int                     rc;
1494
1495         rc = request_firmware(&fw, filename, &dev->dev);
1496         if (rc != 0) {
1497                 netdev_err(dev, "Error %d requesting firmware file: %s\n",
1498                            rc, filename);
1499                 return rc;
1500         }
1501         if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1502                 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1503         else if (bnxt_dir_type_is_other_exec_format(dir_type) == true)
1504                 rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
1505         else
1506                 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1507                                       0, 0, fw->data, fw->size);
1508         release_firmware(fw);
1509         return rc;
1510 }
1511
1512 static int bnxt_flash_package_from_file(struct net_device *dev,
1513                                         char *filename, u32 install_type)
1514 {
1515         struct bnxt *bp = netdev_priv(dev);
1516         struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
1517         struct hwrm_nvm_install_update_input install = {0};
1518         const struct firmware *fw;
1519         u32 item_len;
1520         u16 index;
1521         int rc;
1522
1523         bnxt_hwrm_fw_set_time(bp);
1524
1525         if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
1526                                  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1527                                  &index, &item_len, NULL) != 0) {
1528                 netdev_err(dev, "PKG update area not created in nvram\n");
1529                 return -ENOBUFS;
1530         }
1531
1532         rc = request_firmware(&fw, filename, &dev->dev);
1533         if (rc != 0) {
1534                 netdev_err(dev, "PKG error %d requesting file: %s\n",
1535                            rc, filename);
1536                 return rc;
1537         }
1538
1539         if (fw->size > item_len) {
1540                 netdev_err(dev, "PKG insufficient update area in nvram: %lu",
1541                            (unsigned long)fw->size);
1542                 rc = -EFBIG;
1543         } else {
1544                 dma_addr_t dma_handle;
1545                 u8 *kmem;
1546                 struct hwrm_nvm_modify_input modify = {0};
1547
1548                 bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
1549
1550                 modify.dir_idx = cpu_to_le16(index);
1551                 modify.len = cpu_to_le32(fw->size);
1552
1553                 kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
1554                                           &dma_handle, GFP_KERNEL);
1555                 if (!kmem) {
1556                         netdev_err(dev,
1557                                    "dma_alloc_coherent failure, length = %u\n",
1558                                    (unsigned int)fw->size);
1559                         rc = -ENOMEM;
1560                 } else {
1561                         memcpy(kmem, fw->data, fw->size);
1562                         modify.host_src_addr = cpu_to_le64(dma_handle);
1563
1564                         rc = hwrm_send_message(bp, &modify, sizeof(modify),
1565                                                FLASH_PACKAGE_TIMEOUT);
1566                         dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
1567                                           dma_handle);
1568                 }
1569         }
1570         release_firmware(fw);
1571         if (rc)
1572                 return rc;
1573
1574         if ((install_type & 0xffff) == 0)
1575                 install_type >>= 16;
1576         bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
1577         install.install_type = cpu_to_le32(install_type);
1578
1579         rc = hwrm_send_message(bp, &install, sizeof(install),
1580                                INSTALL_PACKAGE_TIMEOUT);
1581         if (rc)
1582                 return -EOPNOTSUPP;
1583
1584         if (resp->result) {
1585                 netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
1586                            (s8)resp->result, (int)resp->problem_item);
1587                 return -ENOPKG;
1588         }
1589         return 0;
1590 }
1591
1592 static int bnxt_flash_device(struct net_device *dev,
1593                              struct ethtool_flash *flash)
1594 {
1595         if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1596                 netdev_err(dev, "flashdev not supported from a virtual function\n");
1597                 return -EINVAL;
1598         }
1599
1600         if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
1601             flash->region > 0xffff)
1602                 return bnxt_flash_package_from_file(dev, flash->data,
1603                                                     flash->region);
1604
1605         return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1606 }
1607
1608 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1609 {
1610         struct bnxt *bp = netdev_priv(dev);
1611         int rc;
1612         struct hwrm_nvm_get_dir_info_input req = {0};
1613         struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1614
1615         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1616
1617         mutex_lock(&bp->hwrm_cmd_lock);
1618         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1619         if (!rc) {
1620                 *entries = le32_to_cpu(output->entries);
1621                 *length = le32_to_cpu(output->entry_length);
1622         }
1623         mutex_unlock(&bp->hwrm_cmd_lock);
1624         return rc;
1625 }
1626
1627 static int bnxt_get_eeprom_len(struct net_device *dev)
1628 {
1629         /* The -1 return value allows the entire 32-bit range of offsets to be
1630          * passed via the ethtool command-line utility.
1631          */
1632         return -1;
1633 }
1634
1635 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1636 {
1637         struct bnxt *bp = netdev_priv(dev);
1638         int rc;
1639         u32 dir_entries;
1640         u32 entry_length;
1641         u8 *buf;
1642         size_t buflen;
1643         dma_addr_t dma_handle;
1644         struct hwrm_nvm_get_dir_entries_input req = {0};
1645
1646         rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1647         if (rc != 0)
1648                 return rc;
1649
1650         /* Insert 2 bytes of directory info (count and size of entries) */
1651         if (len < 2)
1652                 return -EINVAL;
1653
1654         *data++ = dir_entries;
1655         *data++ = entry_length;
1656         len -= 2;
1657         memset(data, 0xff, len);
1658
1659         buflen = dir_entries * entry_length;
1660         buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1661                                  GFP_KERNEL);
1662         if (!buf) {
1663                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1664                            (unsigned)buflen);
1665                 return -ENOMEM;
1666         }
1667         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1668         req.host_dest_addr = cpu_to_le64(dma_handle);
1669         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1670         if (rc == 0)
1671                 memcpy(data, buf, len > buflen ? buflen : len);
1672         dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1673         return rc;
1674 }
1675
1676 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1677                                u32 length, u8 *data)
1678 {
1679         struct bnxt *bp = netdev_priv(dev);
1680         int rc;
1681         u8 *buf;
1682         dma_addr_t dma_handle;
1683         struct hwrm_nvm_read_input req = {0};
1684
1685         buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1686                                  GFP_KERNEL);
1687         if (!buf) {
1688                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1689                            (unsigned)length);
1690                 return -ENOMEM;
1691         }
1692         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1693         req.host_dest_addr = cpu_to_le64(dma_handle);
1694         req.dir_idx = cpu_to_le16(index);
1695         req.offset = cpu_to_le32(offset);
1696         req.len = cpu_to_le32(length);
1697
1698         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1699         if (rc == 0)
1700                 memcpy(data, buf, length);
1701         dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1702         return rc;
1703 }
1704
1705 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1706                                 u16 ext, u16 *index, u32 *item_length,
1707                                 u32 *data_length)
1708 {
1709         struct bnxt *bp = netdev_priv(dev);
1710         int rc;
1711         struct hwrm_nvm_find_dir_entry_input req = {0};
1712         struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
1713
1714         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
1715         req.enables = 0;
1716         req.dir_idx = 0;
1717         req.dir_type = cpu_to_le16(type);
1718         req.dir_ordinal = cpu_to_le16(ordinal);
1719         req.dir_ext = cpu_to_le16(ext);
1720         req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
1721         rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1722         if (rc == 0) {
1723                 if (index)
1724                         *index = le16_to_cpu(output->dir_idx);
1725                 if (item_length)
1726                         *item_length = le32_to_cpu(output->dir_item_length);
1727                 if (data_length)
1728                         *data_length = le32_to_cpu(output->dir_data_length);
1729         }
1730         return rc;
1731 }
1732
1733 static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
1734 {
1735         char    *retval = NULL;
1736         char    *p;
1737         char    *value;
1738         int     field = 0;
1739
1740         if (datalen < 1)
1741                 return NULL;
1742         /* null-terminate the log data (removing last '\n'): */
1743         data[datalen - 1] = 0;
1744         for (p = data; *p != 0; p++) {
1745                 field = 0;
1746                 retval = NULL;
1747                 while (*p != 0 && *p != '\n') {
1748                         value = p;
1749                         while (*p != 0 && *p != '\t' && *p != '\n')
1750                                 p++;
1751                         if (field == desired_field)
1752                                 retval = value;
1753                         if (*p != '\t')
1754                                 break;
1755                         *p = 0;
1756                         field++;
1757                         p++;
1758                 }
1759                 if (*p == 0)
1760                         break;
1761                 *p = 0;
1762         }
1763         return retval;
1764 }
1765
1766 static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen)
1767 {
1768         u16 index = 0;
1769         u32 datalen;
1770
1771         if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
1772                                  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1773                                  &index, NULL, &datalen) != 0)
1774                 return NULL;
1775
1776         memset(buf, 0, buflen);
1777         if (bnxt_get_nvram_item(dev, index, 0, datalen, buf) != 0)
1778                 return NULL;
1779
1780         return bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, buf,
1781                 datalen);
1782 }
1783
1784 static int bnxt_get_eeprom(struct net_device *dev,
1785                            struct ethtool_eeprom *eeprom,
1786                            u8 *data)
1787 {
1788         u32 index;
1789         u32 offset;
1790
1791         if (eeprom->offset == 0) /* special offset value to get directory */
1792                 return bnxt_get_nvram_directory(dev, eeprom->len, data);
1793
1794         index = eeprom->offset >> 24;
1795         offset = eeprom->offset & 0xffffff;
1796
1797         if (index == 0) {
1798                 netdev_err(dev, "unsupported index value: %d\n", index);
1799                 return -EINVAL;
1800         }
1801
1802         return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
1803 }
1804
1805 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
1806 {
1807         struct bnxt *bp = netdev_priv(dev);
1808         struct hwrm_nvm_erase_dir_entry_input req = {0};
1809
1810         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
1811         req.dir_idx = cpu_to_le16(index);
1812         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1813 }
1814
1815 static int bnxt_set_eeprom(struct net_device *dev,
1816                            struct ethtool_eeprom *eeprom,
1817                            u8 *data)
1818 {
1819         struct bnxt *bp = netdev_priv(dev);
1820         u8 index, dir_op;
1821         u16 type, ext, ordinal, attr;
1822
1823         if (!BNXT_PF(bp)) {
1824                 netdev_err(dev, "NVM write not supported from a virtual function\n");
1825                 return -EINVAL;
1826         }
1827
1828         type = eeprom->magic >> 16;
1829
1830         if (type == 0xffff) { /* special value for directory operations */
1831                 index = eeprom->magic & 0xff;
1832                 dir_op = eeprom->magic >> 8;
1833                 if (index == 0)
1834                         return -EINVAL;
1835                 switch (dir_op) {
1836                 case 0x0e: /* erase */
1837                         if (eeprom->offset != ~eeprom->magic)
1838                                 return -EINVAL;
1839                         return bnxt_erase_nvram_directory(dev, index - 1);
1840                 default:
1841                         return -EINVAL;
1842                 }
1843         }
1844
1845         /* Create or re-write an NVM item: */
1846         if (bnxt_dir_type_is_executable(type) == true)
1847                 return -EOPNOTSUPP;
1848         ext = eeprom->magic & 0xffff;
1849         ordinal = eeprom->offset >> 16;
1850         attr = eeprom->offset & 0xffff;
1851
1852         return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
1853                                 eeprom->len);
1854 }
1855
1856 static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
1857 {
1858         struct bnxt *bp = netdev_priv(dev);
1859         struct ethtool_eee *eee = &bp->eee;
1860         struct bnxt_link_info *link_info = &bp->link_info;
1861         u32 advertising =
1862                  _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
1863         int rc = 0;
1864
1865         if (!BNXT_SINGLE_PF(bp))
1866                 return -EOPNOTSUPP;
1867
1868         if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1869                 return -EOPNOTSUPP;
1870
1871         if (!edata->eee_enabled)
1872                 goto eee_ok;
1873
1874         if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
1875                 netdev_warn(dev, "EEE requires autoneg\n");
1876                 return -EINVAL;
1877         }
1878         if (edata->tx_lpi_enabled) {
1879                 if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
1880                                        edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
1881                         netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
1882                                     bp->lpi_tmr_lo, bp->lpi_tmr_hi);
1883                         return -EINVAL;
1884                 } else if (!bp->lpi_tmr_hi) {
1885                         edata->tx_lpi_timer = eee->tx_lpi_timer;
1886                 }
1887         }
1888         if (!edata->advertised) {
1889                 edata->advertised = advertising & eee->supported;
1890         } else if (edata->advertised & ~advertising) {
1891                 netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
1892                             edata->advertised, advertising);
1893                 return -EINVAL;
1894         }
1895
1896         eee->advertised = edata->advertised;
1897         eee->tx_lpi_enabled = edata->tx_lpi_enabled;
1898         eee->tx_lpi_timer = edata->tx_lpi_timer;
1899 eee_ok:
1900         eee->eee_enabled = edata->eee_enabled;
1901
1902         if (netif_running(dev))
1903                 rc = bnxt_hwrm_set_link_setting(bp, false, true);
1904
1905         return rc;
1906 }
1907
1908 static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
1909 {
1910         struct bnxt *bp = netdev_priv(dev);
1911
1912         if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1913                 return -EOPNOTSUPP;
1914
1915         *edata = bp->eee;
1916         if (!bp->eee.eee_enabled) {
1917                 /* Preserve tx_lpi_timer so that the last value will be used
1918                  * by default when it is re-enabled.
1919                  */
1920                 edata->advertised = 0;
1921                 edata->tx_lpi_enabled = 0;
1922         }
1923
1924         if (!bp->eee.eee_active)
1925                 edata->lp_advertised = 0;
1926
1927         return 0;
1928 }
1929
1930 static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
1931                                             u16 page_number, u16 start_addr,
1932                                             u16 data_length, u8 *buf)
1933 {
1934         struct hwrm_port_phy_i2c_read_input req = {0};
1935         struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
1936         int rc, byte_offset = 0;
1937
1938         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
1939         req.i2c_slave_addr = i2c_addr;
1940         req.page_number = cpu_to_le16(page_number);
1941         req.port_id = cpu_to_le16(bp->pf.port_id);
1942         do {
1943                 u16 xfer_size;
1944
1945                 xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
1946                 data_length -= xfer_size;
1947                 req.page_offset = cpu_to_le16(start_addr + byte_offset);
1948                 req.data_length = xfer_size;
1949                 req.enables = cpu_to_le32(start_addr + byte_offset ?
1950                                  PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
1951                 mutex_lock(&bp->hwrm_cmd_lock);
1952                 rc = _hwrm_send_message(bp, &req, sizeof(req),
1953                                         HWRM_CMD_TIMEOUT);
1954                 if (!rc)
1955                         memcpy(buf + byte_offset, output->data, xfer_size);
1956                 mutex_unlock(&bp->hwrm_cmd_lock);
1957                 byte_offset += xfer_size;
1958         } while (!rc && data_length > 0);
1959
1960         return rc;
1961 }
1962
1963 static int bnxt_get_module_info(struct net_device *dev,
1964                                 struct ethtool_modinfo *modinfo)
1965 {
1966         struct bnxt *bp = netdev_priv(dev);
1967         struct hwrm_port_phy_i2c_read_input req = {0};
1968         struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
1969         int rc;
1970
1971         /* No point in going further if phy status indicates
1972          * module is not inserted or if it is powered down or
1973          * if it is of type 10GBase-T
1974          */
1975         if (bp->link_info.module_status >
1976                 PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
1977                 return -EOPNOTSUPP;
1978
1979         /* This feature is not supported in older firmware versions */
1980         if (bp->hwrm_spec_code < 0x10202)
1981                 return -EOPNOTSUPP;
1982
1983         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
1984         req.i2c_slave_addr = I2C_DEV_ADDR_A0;
1985         req.page_number = 0;
1986         req.page_offset = cpu_to_le16(SFP_EEPROM_SFF_8472_COMP_ADDR);
1987         req.data_length = SFP_EEPROM_SFF_8472_COMP_SIZE;
1988         req.port_id = cpu_to_le16(bp->pf.port_id);
1989         mutex_lock(&bp->hwrm_cmd_lock);
1990         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1991         if (!rc) {
1992                 u32 module_id = le32_to_cpu(output->data[0]);
1993
1994                 switch (module_id) {
1995                 case SFF_MODULE_ID_SFP:
1996                         modinfo->type = ETH_MODULE_SFF_8472;
1997                         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1998                         break;
1999                 case SFF_MODULE_ID_QSFP:
2000                 case SFF_MODULE_ID_QSFP_PLUS:
2001                         modinfo->type = ETH_MODULE_SFF_8436;
2002                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2003                         break;
2004                 case SFF_MODULE_ID_QSFP28:
2005                         modinfo->type = ETH_MODULE_SFF_8636;
2006                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
2007                         break;
2008                 default:
2009                         rc = -EOPNOTSUPP;
2010                         break;
2011                 }
2012         }
2013         mutex_unlock(&bp->hwrm_cmd_lock);
2014         return rc;
2015 }
2016
2017 static int bnxt_get_module_eeprom(struct net_device *dev,
2018                                   struct ethtool_eeprom *eeprom,
2019                                   u8 *data)
2020 {
2021         struct bnxt *bp = netdev_priv(dev);
2022         u16  start = eeprom->offset, length = eeprom->len;
2023         int rc = 0;
2024
2025         memset(data, 0, eeprom->len);
2026
2027         /* Read A0 portion of the EEPROM */
2028         if (start < ETH_MODULE_SFF_8436_LEN) {
2029                 if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
2030                         length = ETH_MODULE_SFF_8436_LEN - start;
2031                 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
2032                                                       start, length, data);
2033                 if (rc)
2034                         return rc;
2035                 start += length;
2036                 data += length;
2037                 length = eeprom->len - length;
2038         }
2039
2040         /* Read A2 portion of the EEPROM */
2041         if (length) {
2042                 start -= ETH_MODULE_SFF_8436_LEN;
2043                 bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1, start,
2044                                                  length, data);
2045         }
2046         return rc;
2047 }
2048
2049 static int bnxt_nway_reset(struct net_device *dev)
2050 {
2051         int rc = 0;
2052
2053         struct bnxt *bp = netdev_priv(dev);
2054         struct bnxt_link_info *link_info = &bp->link_info;
2055
2056         if (!BNXT_SINGLE_PF(bp))
2057                 return -EOPNOTSUPP;
2058
2059         if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
2060                 return -EINVAL;
2061
2062         if (netif_running(dev))
2063                 rc = bnxt_hwrm_set_link_setting(bp, true, false);
2064
2065         return rc;
2066 }
2067
2068 const struct ethtool_ops bnxt_ethtool_ops = {
2069         .get_link_ksettings     = bnxt_get_link_ksettings,
2070         .set_link_ksettings     = bnxt_set_link_ksettings,
2071         .get_pauseparam         = bnxt_get_pauseparam,
2072         .set_pauseparam         = bnxt_set_pauseparam,
2073         .get_drvinfo            = bnxt_get_drvinfo,
2074         .get_coalesce           = bnxt_get_coalesce,
2075         .set_coalesce           = bnxt_set_coalesce,
2076         .get_msglevel           = bnxt_get_msglevel,
2077         .set_msglevel           = bnxt_set_msglevel,
2078         .get_sset_count         = bnxt_get_sset_count,
2079         .get_strings            = bnxt_get_strings,
2080         .get_ethtool_stats      = bnxt_get_ethtool_stats,
2081         .set_ringparam          = bnxt_set_ringparam,
2082         .get_ringparam          = bnxt_get_ringparam,
2083         .get_channels           = bnxt_get_channels,
2084         .set_channels           = bnxt_set_channels,
2085         .get_rxnfc              = bnxt_get_rxnfc,
2086         .set_rxnfc              = bnxt_set_rxnfc,
2087         .get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
2088         .get_rxfh_key_size      = bnxt_get_rxfh_key_size,
2089         .get_rxfh               = bnxt_get_rxfh,
2090         .flash_device           = bnxt_flash_device,
2091         .get_eeprom_len         = bnxt_get_eeprom_len,
2092         .get_eeprom             = bnxt_get_eeprom,
2093         .set_eeprom             = bnxt_set_eeprom,
2094         .get_link               = bnxt_get_link,
2095         .get_eee                = bnxt_get_eee,
2096         .set_eee                = bnxt_set_eee,
2097         .get_module_info        = bnxt_get_module_info,
2098         .get_module_eeprom      = bnxt_get_module_eeprom,
2099         .nway_reset             = bnxt_nway_reset
2100 };