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