GNU Linux-libre 4.9.333-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                 link_info->req_flow_ctrl = 0;
1012         } else {
1013                 /* when transition from auto pause to force pause,
1014                  * force a link change
1015                  */
1016                 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1017                         link_info->force_link_chng = true;
1018                 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1019                 link_info->req_flow_ctrl = 0;
1020         }
1021         if (epause->rx_pause)
1022                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1023
1024         if (epause->tx_pause)
1025                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1026
1027         if (netif_running(dev))
1028                 rc = bnxt_hwrm_set_pause(bp);
1029
1030 pause_exit:
1031         mutex_unlock(&bp->link_lock);
1032         return rc;
1033 }
1034
1035 static u32 bnxt_get_link(struct net_device *dev)
1036 {
1037         struct bnxt *bp = netdev_priv(dev);
1038
1039         /* TODO: handle MF, VF, driver close case */
1040         return bp->link_info.link_up;
1041 }
1042
1043 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1044                                 u16 ext, u16 *index, u32 *item_length,
1045                                 u32 *data_length);
1046
1047 static int bnxt_flash_nvram(struct net_device *dev,
1048                             u16 dir_type,
1049                             u16 dir_ordinal,
1050                             u16 dir_ext,
1051                             u16 dir_attr,
1052                             const u8 *data,
1053                             size_t data_len)
1054 {
1055         struct bnxt *bp = netdev_priv(dev);
1056         int rc;
1057         struct hwrm_nvm_write_input req = {0};
1058         dma_addr_t dma_handle;
1059         u8 *kmem;
1060
1061         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1062
1063         req.dir_type = cpu_to_le16(dir_type);
1064         req.dir_ordinal = cpu_to_le16(dir_ordinal);
1065         req.dir_ext = cpu_to_le16(dir_ext);
1066         req.dir_attr = cpu_to_le16(dir_attr);
1067         req.dir_data_length = cpu_to_le32(data_len);
1068
1069         kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1070                                   GFP_KERNEL);
1071         if (!kmem) {
1072                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1073                            (unsigned)data_len);
1074                 return -ENOMEM;
1075         }
1076         memcpy(kmem, data, data_len);
1077         req.host_src_addr = cpu_to_le64(dma_handle);
1078
1079         rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1080         dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1081
1082         return rc;
1083 }
1084
1085 static int bnxt_firmware_reset(struct net_device *dev,
1086                                u16 dir_type)
1087 {
1088         struct bnxt *bp = netdev_priv(dev);
1089         struct hwrm_fw_reset_input req = {0};
1090
1091         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1092
1093         /* TODO: Support ASAP ChiMP self-reset (e.g. upon PF driver unload) */
1094         /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1095         /*       (e.g. when firmware isn't already running) */
1096         switch (dir_type) {
1097         case BNX_DIR_TYPE_CHIMP_PATCH:
1098         case BNX_DIR_TYPE_BOOTCODE:
1099         case BNX_DIR_TYPE_BOOTCODE_2:
1100                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1101                 /* Self-reset ChiMP upon next PCIe reset: */
1102                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1103                 break;
1104         case BNX_DIR_TYPE_APE_FW:
1105         case BNX_DIR_TYPE_APE_PATCH:
1106                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
1107                 /* Self-reset APE upon next PCIe reset: */
1108                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1109                 break;
1110         case BNX_DIR_TYPE_KONG_FW:
1111         case BNX_DIR_TYPE_KONG_PATCH:
1112                 req.embedded_proc_type =
1113                         FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1114                 break;
1115         case BNX_DIR_TYPE_BONO_FW:
1116         case BNX_DIR_TYPE_BONO_PATCH:
1117                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1118                 break;
1119         default:
1120                 return -EINVAL;
1121         }
1122
1123         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1124 }
1125
1126 static int bnxt_flash_firmware(struct net_device *dev,
1127                                u16 dir_type,
1128                                const u8 *fw_data,
1129                                size_t fw_size)
1130 {
1131         int     rc = 0;
1132         u16     code_type;
1133         u32     stored_crc;
1134         u32     calculated_crc;
1135         struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1136
1137         switch (dir_type) {
1138         case BNX_DIR_TYPE_BOOTCODE:
1139         case BNX_DIR_TYPE_BOOTCODE_2:
1140                 code_type = CODE_BOOT;
1141                 break;
1142         case BNX_DIR_TYPE_CHIMP_PATCH:
1143                 code_type = CODE_CHIMP_PATCH;
1144                 break;
1145         case BNX_DIR_TYPE_APE_FW:
1146                 code_type = CODE_MCTP_PASSTHRU;
1147                 break;
1148         case BNX_DIR_TYPE_APE_PATCH:
1149                 code_type = CODE_APE_PATCH;
1150                 break;
1151         case BNX_DIR_TYPE_KONG_FW:
1152                 code_type = CODE_KONG_FW;
1153                 break;
1154         case BNX_DIR_TYPE_KONG_PATCH:
1155                 code_type = CODE_KONG_PATCH;
1156                 break;
1157         case BNX_DIR_TYPE_BONO_FW:
1158                 code_type = CODE_BONO_FW;
1159                 break;
1160         case BNX_DIR_TYPE_BONO_PATCH:
1161                 code_type = CODE_BONO_PATCH;
1162                 break;
1163         default:
1164                 netdev_err(dev, "Unsupported directory entry type: %u\n",
1165                            dir_type);
1166                 return -EINVAL;
1167         }
1168         if (fw_size < sizeof(struct bnxt_fw_header)) {
1169                 netdev_err(dev, "Invalid firmware file size: %u\n",
1170                            (unsigned int)fw_size);
1171                 return -EINVAL;
1172         }
1173         if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1174                 netdev_err(dev, "Invalid firmware signature: %08X\n",
1175                            le32_to_cpu(header->signature));
1176                 return -EINVAL;
1177         }
1178         if (header->code_type != code_type) {
1179                 netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1180                            code_type, header->code_type);
1181                 return -EINVAL;
1182         }
1183         if (header->device != DEVICE_CUMULUS_FAMILY) {
1184                 netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1185                            DEVICE_CUMULUS_FAMILY, header->device);
1186                 return -EINVAL;
1187         }
1188         /* Confirm the CRC32 checksum of the file: */
1189         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1190                                              sizeof(stored_crc)));
1191         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1192         if (calculated_crc != stored_crc) {
1193                 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1194                            (unsigned long)stored_crc,
1195                            (unsigned long)calculated_crc);
1196                 return -EINVAL;
1197         }
1198         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1199                               0, 0, fw_data, fw_size);
1200         if (rc == 0)    /* Firmware update successful */
1201                 rc = bnxt_firmware_reset(dev, dir_type);
1202
1203         return rc;
1204 }
1205
1206 static int bnxt_flash_microcode(struct net_device *dev,
1207                                 u16 dir_type,
1208                                 const u8 *fw_data,
1209                                 size_t fw_size)
1210 {
1211         struct bnxt_ucode_trailer *trailer;
1212         u32 calculated_crc;
1213         u32 stored_crc;
1214         int rc = 0;
1215
1216         if (fw_size < sizeof(struct bnxt_ucode_trailer)) {
1217                 netdev_err(dev, "Invalid microcode file size: %u\n",
1218                            (unsigned int)fw_size);
1219                 return -EINVAL;
1220         }
1221         trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size -
1222                                                 sizeof(*trailer)));
1223         if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) {
1224                 netdev_err(dev, "Invalid microcode trailer signature: %08X\n",
1225                            le32_to_cpu(trailer->sig));
1226                 return -EINVAL;
1227         }
1228         if (le16_to_cpu(trailer->dir_type) != dir_type) {
1229                 netdev_err(dev, "Expected microcode type: %d, read: %d\n",
1230                            dir_type, le16_to_cpu(trailer->dir_type));
1231                 return -EINVAL;
1232         }
1233         if (le16_to_cpu(trailer->trailer_length) <
1234                 sizeof(struct bnxt_ucode_trailer)) {
1235                 netdev_err(dev, "Invalid microcode trailer length: %d\n",
1236                            le16_to_cpu(trailer->trailer_length));
1237                 return -EINVAL;
1238         }
1239
1240         /* Confirm the CRC32 checksum of the file: */
1241         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1242                                              sizeof(stored_crc)));
1243         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1244         if (calculated_crc != stored_crc) {
1245                 netdev_err(dev,
1246                            "CRC32 (%08lX) does not match calculated: %08lX\n",
1247                            (unsigned long)stored_crc,
1248                            (unsigned long)calculated_crc);
1249                 return -EINVAL;
1250         }
1251         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1252                               0, 0, fw_data, fw_size);
1253
1254         return rc;
1255 }
1256
1257 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1258 {
1259         switch (dir_type) {
1260         case BNX_DIR_TYPE_CHIMP_PATCH:
1261         case BNX_DIR_TYPE_BOOTCODE:
1262         case BNX_DIR_TYPE_BOOTCODE_2:
1263         case BNX_DIR_TYPE_APE_FW:
1264         case BNX_DIR_TYPE_APE_PATCH:
1265         case BNX_DIR_TYPE_KONG_FW:
1266         case BNX_DIR_TYPE_KONG_PATCH:
1267         case BNX_DIR_TYPE_BONO_FW:
1268         case BNX_DIR_TYPE_BONO_PATCH:
1269                 return true;
1270         }
1271
1272         return false;
1273 }
1274
1275 static bool bnxt_dir_type_is_other_exec_format(u16 dir_type)
1276 {
1277         switch (dir_type) {
1278         case BNX_DIR_TYPE_AVS:
1279         case BNX_DIR_TYPE_EXP_ROM_MBA:
1280         case BNX_DIR_TYPE_PCIE:
1281         case BNX_DIR_TYPE_TSCF_UCODE:
1282         case BNX_DIR_TYPE_EXT_PHY:
1283         case BNX_DIR_TYPE_CCM:
1284         case BNX_DIR_TYPE_ISCSI_BOOT:
1285         case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1286         case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1287                 return true;
1288         }
1289
1290         return false;
1291 }
1292
1293 static bool bnxt_dir_type_is_executable(u16 dir_type)
1294 {
1295         return bnxt_dir_type_is_ape_bin_format(dir_type) ||
1296                 bnxt_dir_type_is_other_exec_format(dir_type);
1297 }
1298
1299 static int bnxt_flash_firmware_from_file(struct net_device *dev,
1300                                          u16 dir_type,
1301                                          const char *filename)
1302 {
1303         const struct firmware  *fw;
1304         int                     rc;
1305
1306         rc = request_firmware(&fw, filename, &dev->dev);
1307         if (rc != 0) {
1308                 netdev_err(dev, "Error %d requesting firmware file: %s\n",
1309                            rc, filename);
1310                 return rc;
1311         }
1312         if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1313                 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1314         else if (bnxt_dir_type_is_other_exec_format(dir_type) == true)
1315                 rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size);
1316         else
1317                 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1318                                       0, 0, fw->data, fw->size);
1319         release_firmware(fw);
1320         return rc;
1321 }
1322
1323 static int bnxt_flash_package_from_file(struct net_device *dev,
1324                                         char *filename, u32 install_type)
1325 {
1326         struct bnxt *bp = netdev_priv(dev);
1327         struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr;
1328         struct hwrm_nvm_install_update_input install = {0};
1329         const struct firmware *fw;
1330         u32 item_len;
1331         u16 index;
1332         int rc;
1333
1334         bnxt_hwrm_fw_set_time(bp);
1335
1336         if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE,
1337                                  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1338                                  &index, &item_len, NULL) != 0) {
1339                 netdev_err(dev, "PKG update area not created in nvram\n");
1340                 return -ENOBUFS;
1341         }
1342
1343         rc = request_firmware(&fw, filename, &dev->dev);
1344         if (rc != 0) {
1345                 netdev_err(dev, "PKG error %d requesting file: %s\n",
1346                            rc, filename);
1347                 return rc;
1348         }
1349
1350         if (fw->size > item_len) {
1351                 netdev_err(dev, "PKG insufficient update area in nvram: %lu",
1352                            (unsigned long)fw->size);
1353                 rc = -EFBIG;
1354         } else {
1355                 dma_addr_t dma_handle;
1356                 u8 *kmem;
1357                 struct hwrm_nvm_modify_input modify = {0};
1358
1359                 bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1);
1360
1361                 modify.dir_idx = cpu_to_le16(index);
1362                 modify.len = cpu_to_le32(fw->size);
1363
1364                 kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size,
1365                                           &dma_handle, GFP_KERNEL);
1366                 if (!kmem) {
1367                         netdev_err(dev,
1368                                    "dma_alloc_coherent failure, length = %u\n",
1369                                    (unsigned int)fw->size);
1370                         rc = -ENOMEM;
1371                 } else {
1372                         memcpy(kmem, fw->data, fw->size);
1373                         modify.host_src_addr = cpu_to_le64(dma_handle);
1374
1375                         rc = hwrm_send_message(bp, &modify, sizeof(modify),
1376                                                FLASH_PACKAGE_TIMEOUT);
1377                         dma_free_coherent(&bp->pdev->dev, fw->size, kmem,
1378                                           dma_handle);
1379                 }
1380         }
1381         release_firmware(fw);
1382         if (rc)
1383                 return rc;
1384
1385         if ((install_type & 0xffff) == 0)
1386                 install_type >>= 16;
1387         bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1);
1388         install.install_type = cpu_to_le32(install_type);
1389
1390         rc = hwrm_send_message(bp, &install, sizeof(install),
1391                                INSTALL_PACKAGE_TIMEOUT);
1392         if (rc)
1393                 return -EOPNOTSUPP;
1394
1395         if (resp->result) {
1396                 netdev_err(dev, "PKG install error = %d, problem_item = %d\n",
1397                            (s8)resp->result, (int)resp->problem_item);
1398                 return -ENOPKG;
1399         }
1400         return 0;
1401 }
1402
1403 static int bnxt_flash_device(struct net_device *dev,
1404                              struct ethtool_flash *flash)
1405 {
1406         if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1407                 netdev_err(dev, "flashdev not supported from a virtual function\n");
1408                 return -EINVAL;
1409         }
1410
1411         if (flash->region == ETHTOOL_FLASH_ALL_REGIONS ||
1412             flash->region > 0xffff)
1413                 return bnxt_flash_package_from_file(dev, flash->data,
1414                                                     flash->region);
1415
1416         return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1417 }
1418
1419 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1420 {
1421         struct bnxt *bp = netdev_priv(dev);
1422         int rc;
1423         struct hwrm_nvm_get_dir_info_input req = {0};
1424         struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1425
1426         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1427
1428         mutex_lock(&bp->hwrm_cmd_lock);
1429         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1430         if (!rc) {
1431                 *entries = le32_to_cpu(output->entries);
1432                 *length = le32_to_cpu(output->entry_length);
1433         }
1434         mutex_unlock(&bp->hwrm_cmd_lock);
1435         return rc;
1436 }
1437
1438 static int bnxt_get_eeprom_len(struct net_device *dev)
1439 {
1440         /* The -1 return value allows the entire 32-bit range of offsets to be
1441          * passed via the ethtool command-line utility.
1442          */
1443         return -1;
1444 }
1445
1446 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1447 {
1448         struct bnxt *bp = netdev_priv(dev);
1449         int rc;
1450         u32 dir_entries;
1451         u32 entry_length;
1452         u8 *buf;
1453         size_t buflen;
1454         dma_addr_t dma_handle;
1455         struct hwrm_nvm_get_dir_entries_input req = {0};
1456
1457         rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1458         if (rc != 0)
1459                 return rc;
1460
1461         if (!dir_entries || !entry_length)
1462                 return -EIO;
1463
1464         /* Insert 2 bytes of directory info (count and size of entries) */
1465         if (len < 2)
1466                 return -EINVAL;
1467
1468         *data++ = dir_entries;
1469         *data++ = entry_length;
1470         len -= 2;
1471         memset(data, 0xff, len);
1472
1473         buflen = dir_entries * entry_length;
1474         buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1475                                  GFP_KERNEL);
1476         if (!buf) {
1477                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1478                            (unsigned)buflen);
1479                 return -ENOMEM;
1480         }
1481         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1482         req.host_dest_addr = cpu_to_le64(dma_handle);
1483         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1484         if (rc == 0)
1485                 memcpy(data, buf, len > buflen ? buflen : len);
1486         dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1487         return rc;
1488 }
1489
1490 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1491                                u32 length, u8 *data)
1492 {
1493         struct bnxt *bp = netdev_priv(dev);
1494         int rc;
1495         u8 *buf;
1496         dma_addr_t dma_handle;
1497         struct hwrm_nvm_read_input req = {0};
1498
1499         buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1500                                  GFP_KERNEL);
1501         if (!buf) {
1502                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1503                            (unsigned)length);
1504                 return -ENOMEM;
1505         }
1506         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1507         req.host_dest_addr = cpu_to_le64(dma_handle);
1508         req.dir_idx = cpu_to_le16(index);
1509         req.offset = cpu_to_le32(offset);
1510         req.len = cpu_to_le32(length);
1511
1512         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1513         if (rc == 0)
1514                 memcpy(data, buf, length);
1515         dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1516         return rc;
1517 }
1518
1519 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1520                                 u16 ext, u16 *index, u32 *item_length,
1521                                 u32 *data_length)
1522 {
1523         struct bnxt *bp = netdev_priv(dev);
1524         int rc;
1525         struct hwrm_nvm_find_dir_entry_input req = {0};
1526         struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
1527
1528         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
1529         req.enables = 0;
1530         req.dir_idx = 0;
1531         req.dir_type = cpu_to_le16(type);
1532         req.dir_ordinal = cpu_to_le16(ordinal);
1533         req.dir_ext = cpu_to_le16(ext);
1534         req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
1535         rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1536         if (rc == 0) {
1537                 if (index)
1538                         *index = le16_to_cpu(output->dir_idx);
1539                 if (item_length)
1540                         *item_length = le32_to_cpu(output->dir_item_length);
1541                 if (data_length)
1542                         *data_length = le32_to_cpu(output->dir_data_length);
1543         }
1544         return rc;
1545 }
1546
1547 static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
1548 {
1549         char    *retval = NULL;
1550         char    *p;
1551         char    *value;
1552         int     field = 0;
1553
1554         if (datalen < 1)
1555                 return NULL;
1556         /* null-terminate the log data (removing last '\n'): */
1557         data[datalen - 1] = 0;
1558         for (p = data; *p != 0; p++) {
1559                 field = 0;
1560                 retval = NULL;
1561                 while (*p != 0 && *p != '\n') {
1562                         value = p;
1563                         while (*p != 0 && *p != '\t' && *p != '\n')
1564                                 p++;
1565                         if (field == desired_field)
1566                                 retval = value;
1567                         if (*p != '\t')
1568                                 break;
1569                         *p = 0;
1570                         field++;
1571                         p++;
1572                 }
1573                 if (*p == 0)
1574                         break;
1575                 *p = 0;
1576         }
1577         return retval;
1578 }
1579
1580 static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen)
1581 {
1582         u16 index = 0;
1583         u32 datalen;
1584
1585         if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
1586                                  BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1587                                  &index, NULL, &datalen) != 0)
1588                 return NULL;
1589
1590         memset(buf, 0, buflen);
1591         if (bnxt_get_nvram_item(dev, index, 0, datalen, buf) != 0)
1592                 return NULL;
1593
1594         return bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, buf,
1595                 datalen);
1596 }
1597
1598 static int bnxt_get_eeprom(struct net_device *dev,
1599                            struct ethtool_eeprom *eeprom,
1600                            u8 *data)
1601 {
1602         u32 index;
1603         u32 offset;
1604
1605         if (eeprom->offset == 0) /* special offset value to get directory */
1606                 return bnxt_get_nvram_directory(dev, eeprom->len, data);
1607
1608         index = eeprom->offset >> 24;
1609         offset = eeprom->offset & 0xffffff;
1610
1611         if (index == 0) {
1612                 netdev_err(dev, "unsupported index value: %d\n", index);
1613                 return -EINVAL;
1614         }
1615
1616         return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
1617 }
1618
1619 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
1620 {
1621         struct bnxt *bp = netdev_priv(dev);
1622         struct hwrm_nvm_erase_dir_entry_input req = {0};
1623
1624         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
1625         req.dir_idx = cpu_to_le16(index);
1626         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1627 }
1628
1629 static int bnxt_set_eeprom(struct net_device *dev,
1630                            struct ethtool_eeprom *eeprom,
1631                            u8 *data)
1632 {
1633         struct bnxt *bp = netdev_priv(dev);
1634         u8 index, dir_op;
1635         u16 type, ext, ordinal, attr;
1636
1637         if (!BNXT_PF(bp)) {
1638                 netdev_err(dev, "NVM write not supported from a virtual function\n");
1639                 return -EINVAL;
1640         }
1641
1642         type = eeprom->magic >> 16;
1643
1644         if (type == 0xffff) { /* special value for directory operations */
1645                 index = eeprom->magic & 0xff;
1646                 dir_op = eeprom->magic >> 8;
1647                 if (index == 0)
1648                         return -EINVAL;
1649                 switch (dir_op) {
1650                 case 0x0e: /* erase */
1651                         if (eeprom->offset != ~eeprom->magic)
1652                                 return -EINVAL;
1653                         return bnxt_erase_nvram_directory(dev, index - 1);
1654                 default:
1655                         return -EINVAL;
1656                 }
1657         }
1658
1659         /* Create or re-write an NVM item: */
1660         if (bnxt_dir_type_is_executable(type) == true)
1661                 return -EOPNOTSUPP;
1662         ext = eeprom->magic & 0xffff;
1663         ordinal = eeprom->offset >> 16;
1664         attr = eeprom->offset & 0xffff;
1665
1666         return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
1667                                 eeprom->len);
1668 }
1669
1670 static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
1671 {
1672         struct bnxt *bp = netdev_priv(dev);
1673         struct ethtool_eee *eee = &bp->eee;
1674         struct bnxt_link_info *link_info = &bp->link_info;
1675         u32 advertising;
1676         int rc = 0;
1677
1678         if (!BNXT_SINGLE_PF(bp))
1679                 return -EOPNOTSUPP;
1680
1681         if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1682                 return -EOPNOTSUPP;
1683
1684         mutex_lock(&bp->link_lock);
1685         advertising = _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
1686         if (!edata->eee_enabled)
1687                 goto eee_ok;
1688
1689         if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
1690                 netdev_warn(dev, "EEE requires autoneg\n");
1691                 rc = -EINVAL;
1692                 goto eee_exit;
1693         }
1694         if (edata->tx_lpi_enabled) {
1695                 if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
1696                                        edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
1697                         netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
1698                                     bp->lpi_tmr_lo, bp->lpi_tmr_hi);
1699                         rc = -EINVAL;
1700                         goto eee_exit;
1701                 } else if (!bp->lpi_tmr_hi) {
1702                         edata->tx_lpi_timer = eee->tx_lpi_timer;
1703                 }
1704         }
1705         if (!edata->advertised) {
1706                 edata->advertised = advertising & eee->supported;
1707         } else if (edata->advertised & ~advertising) {
1708                 netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
1709                             edata->advertised, advertising);
1710                 rc = -EINVAL;
1711                 goto eee_exit;
1712         }
1713
1714         eee->advertised = edata->advertised;
1715         eee->tx_lpi_enabled = edata->tx_lpi_enabled;
1716         eee->tx_lpi_timer = edata->tx_lpi_timer;
1717 eee_ok:
1718         eee->eee_enabled = edata->eee_enabled;
1719
1720         if (netif_running(dev))
1721                 rc = bnxt_hwrm_set_link_setting(bp, false, true);
1722
1723 eee_exit:
1724         mutex_unlock(&bp->link_lock);
1725         return rc;
1726 }
1727
1728 static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
1729 {
1730         struct bnxt *bp = netdev_priv(dev);
1731
1732         if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1733                 return -EOPNOTSUPP;
1734
1735         *edata = bp->eee;
1736         if (!bp->eee.eee_enabled) {
1737                 /* Preserve tx_lpi_timer so that the last value will be used
1738                  * by default when it is re-enabled.
1739                  */
1740                 edata->advertised = 0;
1741                 edata->tx_lpi_enabled = 0;
1742         }
1743
1744         if (!bp->eee.eee_active)
1745                 edata->lp_advertised = 0;
1746
1747         return 0;
1748 }
1749
1750 static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
1751                                             u16 page_number, u16 start_addr,
1752                                             u16 data_length, u8 *buf)
1753 {
1754         struct hwrm_port_phy_i2c_read_input req = {0};
1755         struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
1756         int rc, byte_offset = 0;
1757
1758         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
1759         req.i2c_slave_addr = i2c_addr;
1760         req.page_number = cpu_to_le16(page_number);
1761         req.port_id = cpu_to_le16(bp->pf.port_id);
1762         do {
1763                 u16 xfer_size;
1764
1765                 xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
1766                 data_length -= xfer_size;
1767                 req.page_offset = cpu_to_le16(start_addr + byte_offset);
1768                 req.data_length = xfer_size;
1769                 req.enables = cpu_to_le32(start_addr + byte_offset ?
1770                                  PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
1771                 mutex_lock(&bp->hwrm_cmd_lock);
1772                 rc = _hwrm_send_message(bp, &req, sizeof(req),
1773                                         HWRM_CMD_TIMEOUT);
1774                 if (!rc)
1775                         memcpy(buf + byte_offset, output->data, xfer_size);
1776                 mutex_unlock(&bp->hwrm_cmd_lock);
1777                 byte_offset += xfer_size;
1778         } while (!rc && data_length > 0);
1779
1780         return rc;
1781 }
1782
1783 static int bnxt_get_module_info(struct net_device *dev,
1784                                 struct ethtool_modinfo *modinfo)
1785 {
1786         struct bnxt *bp = netdev_priv(dev);
1787         struct hwrm_port_phy_i2c_read_input req = {0};
1788         struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
1789         int rc;
1790
1791         /* No point in going further if phy status indicates
1792          * module is not inserted or if it is powered down or
1793          * if it is of type 10GBase-T
1794          */
1795         if (bp->link_info.module_status >
1796                 PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
1797                 return -EOPNOTSUPP;
1798
1799         /* This feature is not supported in older firmware versions */
1800         if (bp->hwrm_spec_code < 0x10202)
1801                 return -EOPNOTSUPP;
1802
1803         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
1804         req.i2c_slave_addr = I2C_DEV_ADDR_A0;
1805         req.page_number = 0;
1806         req.page_offset = cpu_to_le16(SFP_EEPROM_SFF_8472_COMP_ADDR);
1807         req.data_length = SFP_EEPROM_SFF_8472_COMP_SIZE;
1808         req.port_id = cpu_to_le16(bp->pf.port_id);
1809         mutex_lock(&bp->hwrm_cmd_lock);
1810         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1811         if (!rc) {
1812                 u32 module_id = le32_to_cpu(output->data[0]);
1813
1814                 switch (module_id) {
1815                 case SFF_MODULE_ID_SFP:
1816                         modinfo->type = ETH_MODULE_SFF_8472;
1817                         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1818                         break;
1819                 case SFF_MODULE_ID_QSFP:
1820                 case SFF_MODULE_ID_QSFP_PLUS:
1821                         modinfo->type = ETH_MODULE_SFF_8436;
1822                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1823                         break;
1824                 case SFF_MODULE_ID_QSFP28:
1825                         modinfo->type = ETH_MODULE_SFF_8636;
1826                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1827                         break;
1828                 default:
1829                         rc = -EOPNOTSUPP;
1830                         break;
1831                 }
1832         }
1833         mutex_unlock(&bp->hwrm_cmd_lock);
1834         return rc;
1835 }
1836
1837 static int bnxt_get_module_eeprom(struct net_device *dev,
1838                                   struct ethtool_eeprom *eeprom,
1839                                   u8 *data)
1840 {
1841         struct bnxt *bp = netdev_priv(dev);
1842         u16  start = eeprom->offset, length = eeprom->len;
1843         int rc = 0;
1844
1845         memset(data, 0, eeprom->len);
1846
1847         /* Read A0 portion of the EEPROM */
1848         if (start < ETH_MODULE_SFF_8436_LEN) {
1849                 if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
1850                         length = ETH_MODULE_SFF_8436_LEN - start;
1851                 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
1852                                                       start, length, data);
1853                 if (rc)
1854                         return rc;
1855                 start += length;
1856                 data += length;
1857                 length = eeprom->len - length;
1858         }
1859
1860         /* Read A2 portion of the EEPROM */
1861         if (length) {
1862                 start -= ETH_MODULE_SFF_8436_LEN;
1863                 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 0,
1864                                                       start, length, data);
1865         }
1866         return rc;
1867 }
1868
1869 static int bnxt_nway_reset(struct net_device *dev)
1870 {
1871         int rc = 0;
1872
1873         struct bnxt *bp = netdev_priv(dev);
1874         struct bnxt_link_info *link_info = &bp->link_info;
1875
1876         if (!BNXT_SINGLE_PF(bp))
1877                 return -EOPNOTSUPP;
1878
1879         if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
1880                 return -EINVAL;
1881
1882         if (netif_running(dev))
1883                 rc = bnxt_hwrm_set_link_setting(bp, true, false);
1884
1885         return rc;
1886 }
1887
1888 const struct ethtool_ops bnxt_ethtool_ops = {
1889         .get_link_ksettings     = bnxt_get_link_ksettings,
1890         .set_link_ksettings     = bnxt_set_link_ksettings,
1891         .get_pauseparam         = bnxt_get_pauseparam,
1892         .set_pauseparam         = bnxt_set_pauseparam,
1893         .get_drvinfo            = bnxt_get_drvinfo,
1894         .get_coalesce           = bnxt_get_coalesce,
1895         .set_coalesce           = bnxt_set_coalesce,
1896         .get_msglevel           = bnxt_get_msglevel,
1897         .set_msglevel           = bnxt_set_msglevel,
1898         .get_sset_count         = bnxt_get_sset_count,
1899         .get_strings            = bnxt_get_strings,
1900         .get_ethtool_stats      = bnxt_get_ethtool_stats,
1901         .set_ringparam          = bnxt_set_ringparam,
1902         .get_ringparam          = bnxt_get_ringparam,
1903         .get_channels           = bnxt_get_channels,
1904         .set_channels           = bnxt_set_channels,
1905 #ifdef CONFIG_RFS_ACCEL
1906         .get_rxnfc              = bnxt_get_rxnfc,
1907 #endif
1908         .get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
1909         .get_rxfh_key_size      = bnxt_get_rxfh_key_size,
1910         .get_rxfh               = bnxt_get_rxfh,
1911         .flash_device           = bnxt_flash_device,
1912         .get_eeprom_len         = bnxt_get_eeprom_len,
1913         .get_eeprom             = bnxt_get_eeprom,
1914         .set_eeprom             = bnxt_set_eeprom,
1915         .get_link               = bnxt_get_link,
1916         .get_eee                = bnxt_get_eee,
1917         .set_eee                = bnxt_set_eee,
1918         .get_module_info        = bnxt_get_module_info,
1919         .get_module_eeprom      = bnxt_get_module_eeprom,
1920         .nway_reset             = bnxt_nway_reset
1921 };