ASoC: nau8825: fix issue that pop noise when start playback
[cascardo/linux.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_ethtool.c
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2015 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/ethtool.h>
11 #include <linux/interrupt.h>
12 #include <linux/pci.h>
13 #include <linux/etherdevice.h>
14 #include <linux/crc32.h>
15 #include <linux/firmware.h>
16 #include "bnxt_hsi.h"
17 #include "bnxt.h"
18 #include "bnxt_ethtool.h"
19 #include "bnxt_nvm_defs.h"      /* NVRAM content constant and structure defs */
20 #include "bnxt_fw_hdr.h"        /* Firmware hdr constant and structure defs */
21 #define FLASH_NVRAM_TIMEOUT     ((HWRM_CMD_TIMEOUT) * 100)
22
23 static u32 bnxt_get_msglevel(struct net_device *dev)
24 {
25         struct bnxt *bp = netdev_priv(dev);
26
27         return bp->msg_enable;
28 }
29
30 static void bnxt_set_msglevel(struct net_device *dev, u32 value)
31 {
32         struct bnxt *bp = netdev_priv(dev);
33
34         bp->msg_enable = value;
35 }
36
37 static int bnxt_get_coalesce(struct net_device *dev,
38                              struct ethtool_coalesce *coal)
39 {
40         struct bnxt *bp = netdev_priv(dev);
41
42         memset(coal, 0, sizeof(*coal));
43
44         coal->rx_coalesce_usecs =
45                 max_t(u16, BNXT_COAL_TIMER_TO_USEC(bp->coal_ticks), 1);
46         coal->rx_max_coalesced_frames = bp->coal_bufs / 2;
47         coal->rx_coalesce_usecs_irq =
48                 max_t(u16, BNXT_COAL_TIMER_TO_USEC(bp->coal_ticks_irq), 1);
49         coal->rx_max_coalesced_frames_irq = bp->coal_bufs_irq / 2;
50
51         return 0;
52 }
53
54 static int bnxt_set_coalesce(struct net_device *dev,
55                              struct ethtool_coalesce *coal)
56 {
57         struct bnxt *bp = netdev_priv(dev);
58         int rc = 0;
59
60         bp->coal_ticks = BNXT_USEC_TO_COAL_TIMER(coal->rx_coalesce_usecs);
61         bp->coal_bufs = coal->rx_max_coalesced_frames * 2;
62         bp->coal_ticks_irq =
63                 BNXT_USEC_TO_COAL_TIMER(coal->rx_coalesce_usecs_irq);
64         bp->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * 2;
65
66         if (netif_running(dev))
67                 rc = bnxt_hwrm_set_coal(bp);
68
69         return rc;
70 }
71
72 #define BNXT_NUM_STATS  21
73
74 static int bnxt_get_sset_count(struct net_device *dev, int sset)
75 {
76         struct bnxt *bp = netdev_priv(dev);
77
78         switch (sset) {
79         case ETH_SS_STATS:
80                 return BNXT_NUM_STATS * bp->cp_nr_rings;
81         default:
82                 return -EOPNOTSUPP;
83         }
84 }
85
86 static void bnxt_get_ethtool_stats(struct net_device *dev,
87                                    struct ethtool_stats *stats, u64 *buf)
88 {
89         u32 i, j = 0;
90         struct bnxt *bp = netdev_priv(dev);
91         u32 buf_size = sizeof(struct ctx_hw_stats) * bp->cp_nr_rings;
92         u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
93
94         memset(buf, 0, buf_size);
95
96         if (!bp->bnapi)
97                 return;
98
99         for (i = 0; i < bp->cp_nr_rings; i++) {
100                 struct bnxt_napi *bnapi = bp->bnapi[i];
101                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
102                 __le64 *hw_stats = (__le64 *)cpr->hw_stats;
103                 int k;
104
105                 for (k = 0; k < stat_fields; j++, k++)
106                         buf[j] = le64_to_cpu(hw_stats[k]);
107                 buf[j++] = cpr->rx_l4_csum_errors;
108         }
109 }
110
111 static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
112 {
113         struct bnxt *bp = netdev_priv(dev);
114         u32 i;
115
116         switch (stringset) {
117         /* The number of strings must match BNXT_NUM_STATS defined above. */
118         case ETH_SS_STATS:
119                 for (i = 0; i < bp->cp_nr_rings; i++) {
120                         sprintf(buf, "[%d]: rx_ucast_packets", i);
121                         buf += ETH_GSTRING_LEN;
122                         sprintf(buf, "[%d]: rx_mcast_packets", i);
123                         buf += ETH_GSTRING_LEN;
124                         sprintf(buf, "[%d]: rx_bcast_packets", i);
125                         buf += ETH_GSTRING_LEN;
126                         sprintf(buf, "[%d]: rx_discards", i);
127                         buf += ETH_GSTRING_LEN;
128                         sprintf(buf, "[%d]: rx_drops", i);
129                         buf += ETH_GSTRING_LEN;
130                         sprintf(buf, "[%d]: rx_ucast_bytes", i);
131                         buf += ETH_GSTRING_LEN;
132                         sprintf(buf, "[%d]: rx_mcast_bytes", i);
133                         buf += ETH_GSTRING_LEN;
134                         sprintf(buf, "[%d]: rx_bcast_bytes", i);
135                         buf += ETH_GSTRING_LEN;
136                         sprintf(buf, "[%d]: tx_ucast_packets", i);
137                         buf += ETH_GSTRING_LEN;
138                         sprintf(buf, "[%d]: tx_mcast_packets", i);
139                         buf += ETH_GSTRING_LEN;
140                         sprintf(buf, "[%d]: tx_bcast_packets", i);
141                         buf += ETH_GSTRING_LEN;
142                         sprintf(buf, "[%d]: tx_discards", i);
143                         buf += ETH_GSTRING_LEN;
144                         sprintf(buf, "[%d]: tx_drops", i);
145                         buf += ETH_GSTRING_LEN;
146                         sprintf(buf, "[%d]: tx_ucast_bytes", i);
147                         buf += ETH_GSTRING_LEN;
148                         sprintf(buf, "[%d]: tx_mcast_bytes", i);
149                         buf += ETH_GSTRING_LEN;
150                         sprintf(buf, "[%d]: tx_bcast_bytes", i);
151                         buf += ETH_GSTRING_LEN;
152                         sprintf(buf, "[%d]: tpa_packets", i);
153                         buf += ETH_GSTRING_LEN;
154                         sprintf(buf, "[%d]: tpa_bytes", i);
155                         buf += ETH_GSTRING_LEN;
156                         sprintf(buf, "[%d]: tpa_events", i);
157                         buf += ETH_GSTRING_LEN;
158                         sprintf(buf, "[%d]: tpa_aborts", i);
159                         buf += ETH_GSTRING_LEN;
160                         sprintf(buf, "[%d]: rx_l4_csum_errors", i);
161                         buf += ETH_GSTRING_LEN;
162                 }
163                 break;
164         default:
165                 netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
166                            stringset);
167                 break;
168         }
169 }
170
171 static void bnxt_get_ringparam(struct net_device *dev,
172                                struct ethtool_ringparam *ering)
173 {
174         struct bnxt *bp = netdev_priv(dev);
175
176         ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
177         ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
178         ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
179
180         ering->rx_pending = bp->rx_ring_size;
181         ering->rx_jumbo_pending = bp->rx_agg_ring_size;
182         ering->tx_pending = bp->tx_ring_size;
183 }
184
185 static int bnxt_set_ringparam(struct net_device *dev,
186                               struct ethtool_ringparam *ering)
187 {
188         struct bnxt *bp = netdev_priv(dev);
189
190         if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
191             (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
192             (ering->tx_pending <= MAX_SKB_FRAGS))
193                 return -EINVAL;
194
195         if (netif_running(dev))
196                 bnxt_close_nic(bp, false, false);
197
198         bp->rx_ring_size = ering->rx_pending;
199         bp->tx_ring_size = ering->tx_pending;
200         bnxt_set_ring_params(bp);
201
202         if (netif_running(dev))
203                 return bnxt_open_nic(bp, false, false);
204
205         return 0;
206 }
207
208 static void bnxt_get_channels(struct net_device *dev,
209                               struct ethtool_channels *channel)
210 {
211         struct bnxt *bp = netdev_priv(dev);
212         int max_rx_rings, max_tx_rings, tcs;
213
214         bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
215         channel->max_combined = max_rx_rings;
216
217         bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false);
218         tcs = netdev_get_num_tc(dev);
219         if (tcs > 1)
220                 max_tx_rings /= tcs;
221
222         channel->max_rx = max_rx_rings;
223         channel->max_tx = max_tx_rings;
224         channel->max_other = 0;
225         if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
226                 channel->combined_count = bp->rx_nr_rings;
227         } else {
228                 channel->rx_count = bp->rx_nr_rings;
229                 channel->tx_count = bp->tx_nr_rings_per_tc;
230         }
231 }
232
233 static int bnxt_set_channels(struct net_device *dev,
234                              struct ethtool_channels *channel)
235 {
236         struct bnxt *bp = netdev_priv(dev);
237         int max_rx_rings, max_tx_rings, tcs;
238         u32 rc = 0;
239         bool sh = false;
240
241         if (channel->other_count)
242                 return -EINVAL;
243
244         if (!channel->combined_count &&
245             (!channel->rx_count || !channel->tx_count))
246                 return -EINVAL;
247
248         if (channel->combined_count &&
249             (channel->rx_count || channel->tx_count))
250                 return -EINVAL;
251
252         if (channel->combined_count)
253                 sh = true;
254
255         bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
256
257         tcs = netdev_get_num_tc(dev);
258         if (tcs > 1)
259                 max_tx_rings /= tcs;
260
261         if (sh && (channel->combined_count > max_rx_rings ||
262                    channel->combined_count > max_tx_rings))
263                 return -ENOMEM;
264
265         if (!sh && (channel->rx_count > max_rx_rings ||
266                     channel->tx_count > max_tx_rings))
267                 return -ENOMEM;
268
269         if (netif_running(dev)) {
270                 if (BNXT_PF(bp)) {
271                         /* TODO CHIMP_FW: Send message to all VF's
272                          * before PF unload
273                          */
274                 }
275                 rc = bnxt_close_nic(bp, true, false);
276                 if (rc) {
277                         netdev_err(bp->dev, "Set channel failure rc :%x\n",
278                                    rc);
279                         return rc;
280                 }
281         }
282
283         if (sh) {
284                 bp->flags |= BNXT_FLAG_SHARED_RINGS;
285                 bp->rx_nr_rings = channel->combined_count;
286                 bp->tx_nr_rings_per_tc = channel->combined_count;
287         } else {
288                 bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
289                 bp->rx_nr_rings = channel->rx_count;
290                 bp->tx_nr_rings_per_tc = channel->tx_count;
291         }
292
293         bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
294         if (tcs > 1)
295                 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
296
297         bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
298                                bp->tx_nr_rings + bp->rx_nr_rings;
299
300         bp->num_stat_ctxs = bp->cp_nr_rings;
301
302         /* After changing number of rx channels, update NTUPLE feature. */
303         netdev_update_features(dev);
304         if (netif_running(dev)) {
305                 rc = bnxt_open_nic(bp, true, false);
306                 if ((!rc) && BNXT_PF(bp)) {
307                         /* TODO CHIMP_FW: Send message to all VF's
308                          * to renable
309                          */
310                 }
311         }
312
313         return rc;
314 }
315
316 #ifdef CONFIG_RFS_ACCEL
317 static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
318                             u32 *rule_locs)
319 {
320         int i, j = 0;
321
322         cmd->data = bp->ntp_fltr_count;
323         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
324                 struct hlist_head *head;
325                 struct bnxt_ntuple_filter *fltr;
326
327                 head = &bp->ntp_fltr_hash_tbl[i];
328                 rcu_read_lock();
329                 hlist_for_each_entry_rcu(fltr, head, hash) {
330                         if (j == cmd->rule_cnt)
331                                 break;
332                         rule_locs[j++] = fltr->sw_id;
333                 }
334                 rcu_read_unlock();
335                 if (j == cmd->rule_cnt)
336                         break;
337         }
338         cmd->rule_cnt = j;
339         return 0;
340 }
341
342 static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
343 {
344         struct ethtool_rx_flow_spec *fs =
345                 (struct ethtool_rx_flow_spec *)&cmd->fs;
346         struct bnxt_ntuple_filter *fltr;
347         struct flow_keys *fkeys;
348         int i, rc = -EINVAL;
349
350         if (fs->location < 0 || fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
351                 return rc;
352
353         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
354                 struct hlist_head *head;
355
356                 head = &bp->ntp_fltr_hash_tbl[i];
357                 rcu_read_lock();
358                 hlist_for_each_entry_rcu(fltr, head, hash) {
359                         if (fltr->sw_id == fs->location)
360                                 goto fltr_found;
361                 }
362                 rcu_read_unlock();
363         }
364         return rc;
365
366 fltr_found:
367         fkeys = &fltr->fkeys;
368         if (fkeys->basic.ip_proto == IPPROTO_TCP)
369                 fs->flow_type = TCP_V4_FLOW;
370         else if (fkeys->basic.ip_proto == IPPROTO_UDP)
371                 fs->flow_type = UDP_V4_FLOW;
372         else
373                 goto fltr_err;
374
375         fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
376         fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
377
378         fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
379         fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
380
381         fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
382         fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
383
384         fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
385         fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
386
387         fs->ring_cookie = fltr->rxq;
388         rc = 0;
389
390 fltr_err:
391         rcu_read_unlock();
392
393         return rc;
394 }
395
396 static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
397                           u32 *rule_locs)
398 {
399         struct bnxt *bp = netdev_priv(dev);
400         int rc = 0;
401
402         switch (cmd->cmd) {
403         case ETHTOOL_GRXRINGS:
404                 cmd->data = bp->rx_nr_rings;
405                 break;
406
407         case ETHTOOL_GRXCLSRLCNT:
408                 cmd->rule_cnt = bp->ntp_fltr_count;
409                 cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
410                 break;
411
412         case ETHTOOL_GRXCLSRLALL:
413                 rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
414                 break;
415
416         case ETHTOOL_GRXCLSRULE:
417                 rc = bnxt_grxclsrule(bp, cmd);
418                 break;
419
420         default:
421                 rc = -EOPNOTSUPP;
422                 break;
423         }
424
425         return rc;
426 }
427 #endif
428
429 static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
430 {
431         return HW_HASH_INDEX_SIZE;
432 }
433
434 static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
435 {
436         return HW_HASH_KEY_SIZE;
437 }
438
439 static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
440                          u8 *hfunc)
441 {
442         struct bnxt *bp = netdev_priv(dev);
443         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
444         int i = 0;
445
446         if (hfunc)
447                 *hfunc = ETH_RSS_HASH_TOP;
448
449         if (indir)
450                 for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
451                         indir[i] = le16_to_cpu(vnic->rss_table[i]);
452
453         if (key)
454                 memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
455
456         return 0;
457 }
458
459 static void bnxt_get_drvinfo(struct net_device *dev,
460                              struct ethtool_drvinfo *info)
461 {
462         struct bnxt *bp = netdev_priv(dev);
463
464         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
465         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
466         strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
467         strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
468         info->n_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
469         info->testinfo_len = BNXT_NUM_TESTS(bp);
470         /* TODO CHIMP_FW: eeprom dump details */
471         info->eedump_len = 0;
472         /* TODO CHIMP FW: reg dump details */
473         info->regdump_len = 0;
474 }
475
476 static u32 bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info)
477 {
478         u16 fw_speeds = link_info->support_speeds;
479         u32 speed_mask = 0;
480
481         if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
482                 speed_mask |= SUPPORTED_100baseT_Full;
483         if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
484                 speed_mask |= SUPPORTED_1000baseT_Full;
485         if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
486                 speed_mask |= SUPPORTED_2500baseX_Full;
487         if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
488                 speed_mask |= SUPPORTED_10000baseT_Full;
489         /* TODO: support 25GB, 50GB with different cable type */
490         if (fw_speeds & BNXT_LINK_SPEED_MSK_20GB)
491                 speed_mask |= SUPPORTED_20000baseMLD2_Full |
492                         SUPPORTED_20000baseKR2_Full;
493         if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
494                 speed_mask |= SUPPORTED_40000baseKR4_Full |
495                         SUPPORTED_40000baseCR4_Full |
496                         SUPPORTED_40000baseSR4_Full |
497                         SUPPORTED_40000baseLR4_Full;
498
499         return speed_mask;
500 }
501
502 static u32 bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info)
503 {
504         u16 fw_speeds = link_info->auto_link_speeds;
505         u32 speed_mask = 0;
506
507         /* TODO: support 25GB, 40GB, 50GB with different cable type */
508         /* set the advertised speeds */
509         if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
510                 speed_mask |= ADVERTISED_100baseT_Full;
511         if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
512                 speed_mask |= ADVERTISED_1000baseT_Full;
513         if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
514                 speed_mask |= ADVERTISED_2500baseX_Full;
515         if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
516                 speed_mask |= ADVERTISED_10000baseT_Full;
517         /* TODO: how to advertise 20, 25, 40, 50GB with different cable type ?*/
518         if (fw_speeds & BNXT_LINK_SPEED_MSK_20GB)
519                 speed_mask |= ADVERTISED_20000baseMLD2_Full |
520                               ADVERTISED_20000baseKR2_Full;
521         if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
522                 speed_mask |= ADVERTISED_40000baseKR4_Full |
523                               ADVERTISED_40000baseCR4_Full |
524                               ADVERTISED_40000baseSR4_Full |
525                               ADVERTISED_40000baseLR4_Full;
526         return speed_mask;
527 }
528
529 u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
530 {
531         switch (fw_link_speed) {
532         case BNXT_LINK_SPEED_100MB:
533                 return SPEED_100;
534         case BNXT_LINK_SPEED_1GB:
535                 return SPEED_1000;
536         case BNXT_LINK_SPEED_2_5GB:
537                 return SPEED_2500;
538         case BNXT_LINK_SPEED_10GB:
539                 return SPEED_10000;
540         case BNXT_LINK_SPEED_20GB:
541                 return SPEED_20000;
542         case BNXT_LINK_SPEED_25GB:
543                 return SPEED_25000;
544         case BNXT_LINK_SPEED_40GB:
545                 return SPEED_40000;
546         case BNXT_LINK_SPEED_50GB:
547                 return SPEED_50000;
548         default:
549                 return SPEED_UNKNOWN;
550         }
551 }
552
553 static int bnxt_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
554 {
555         struct bnxt *bp = netdev_priv(dev);
556         struct bnxt_link_info *link_info = &bp->link_info;
557         u16 ethtool_speed;
558
559         cmd->supported = bnxt_fw_to_ethtool_support_spds(link_info);
560
561         if (link_info->auto_link_speeds)
562                 cmd->supported |= SUPPORTED_Autoneg;
563
564         if (BNXT_AUTO_MODE(link_info->auto_mode)) {
565                 cmd->advertising =
566                         bnxt_fw_to_ethtool_advertised_spds(link_info);
567                 cmd->advertising |= ADVERTISED_Autoneg;
568                 cmd->autoneg = AUTONEG_ENABLE;
569         } else {
570                 cmd->autoneg = AUTONEG_DISABLE;
571                 cmd->advertising = 0;
572         }
573         if (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) {
574                 if ((link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) ==
575                     BNXT_LINK_PAUSE_BOTH) {
576                         cmd->advertising |= ADVERTISED_Pause;
577                         cmd->supported |= SUPPORTED_Pause;
578                 } else {
579                         cmd->advertising |= ADVERTISED_Asym_Pause;
580                         cmd->supported |= SUPPORTED_Asym_Pause;
581                         if (link_info->auto_pause_setting &
582                             BNXT_LINK_PAUSE_RX)
583                                 cmd->advertising |= ADVERTISED_Pause;
584                 }
585         } else if (link_info->force_pause_setting & BNXT_LINK_PAUSE_BOTH) {
586                 if ((link_info->force_pause_setting & BNXT_LINK_PAUSE_BOTH) ==
587                     BNXT_LINK_PAUSE_BOTH) {
588                         cmd->supported |= SUPPORTED_Pause;
589                 } else {
590                         cmd->supported |= SUPPORTED_Asym_Pause;
591                         if (link_info->force_pause_setting &
592                             BNXT_LINK_PAUSE_RX)
593                                 cmd->supported |= SUPPORTED_Pause;
594                 }
595         }
596
597         cmd->port = PORT_NONE;
598         if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
599                 cmd->port = PORT_TP;
600                 cmd->supported |= SUPPORTED_TP;
601                 cmd->advertising |= ADVERTISED_TP;
602         } else {
603                 cmd->supported |= SUPPORTED_FIBRE;
604                 cmd->advertising |= ADVERTISED_FIBRE;
605
606                 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
607                         cmd->port = PORT_DA;
608                 else if (link_info->media_type ==
609                          PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
610                         cmd->port = PORT_FIBRE;
611         }
612
613         if (link_info->phy_link_status == BNXT_LINK_LINK) {
614                 if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
615                         cmd->duplex = DUPLEX_FULL;
616         } else {
617                 cmd->duplex = DUPLEX_UNKNOWN;
618         }
619         ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
620         ethtool_cmd_speed_set(cmd, ethtool_speed);
621         if (link_info->transceiver ==
622                 PORT_PHY_QCFG_RESP_TRANSCEIVER_TYPE_XCVR_INTERNAL)
623                 cmd->transceiver = XCVR_INTERNAL;
624         else
625                 cmd->transceiver = XCVR_EXTERNAL;
626         cmd->phy_address = link_info->phy_addr;
627
628         return 0;
629 }
630
631 static u32 bnxt_get_fw_speed(struct net_device *dev, u16 ethtool_speed)
632 {
633         switch (ethtool_speed) {
634         case SPEED_100:
635                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
636         case SPEED_1000:
637                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
638         case SPEED_2500:
639                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
640         case SPEED_10000:
641                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
642         case SPEED_20000:
643                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
644         case SPEED_25000:
645                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
646         case SPEED_40000:
647                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
648         case SPEED_50000:
649                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
650         default:
651                 netdev_err(dev, "unsupported speed!\n");
652                 break;
653         }
654         return 0;
655 }
656
657 static u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
658 {
659         u16 fw_speed_mask = 0;
660
661         /* only support autoneg at speed 100, 1000, and 10000 */
662         if (advertising & (ADVERTISED_100baseT_Full |
663                            ADVERTISED_100baseT_Half)) {
664                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
665         }
666         if (advertising & (ADVERTISED_1000baseT_Full |
667                            ADVERTISED_1000baseT_Half)) {
668                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
669         }
670         if (advertising & ADVERTISED_10000baseT_Full)
671                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
672
673         return fw_speed_mask;
674 }
675
676 static int bnxt_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
677 {
678         int rc = 0;
679         struct bnxt *bp = netdev_priv(dev);
680         struct bnxt_link_info *link_info = &bp->link_info;
681         u32 speed, fw_advertising = 0;
682         bool set_pause = false;
683
684         if (BNXT_VF(bp))
685                 return rc;
686
687         if (cmd->autoneg == AUTONEG_ENABLE) {
688                 if (link_info->media_type != PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
689                         netdev_err(dev, "Media type doesn't support autoneg\n");
690                         rc = -EINVAL;
691                         goto set_setting_exit;
692                 }
693                 if (cmd->advertising & ~(BNXT_ALL_COPPER_ETHTOOL_SPEED |
694                                          ADVERTISED_Autoneg |
695                                          ADVERTISED_TP |
696                                          ADVERTISED_Pause |
697                                          ADVERTISED_Asym_Pause)) {
698                         netdev_err(dev, "Unsupported advertising mask (adv: 0x%x)\n",
699                                    cmd->advertising);
700                         rc = -EINVAL;
701                         goto set_setting_exit;
702                 }
703                 fw_advertising = bnxt_get_fw_auto_link_speeds(cmd->advertising);
704                 if (fw_advertising & ~link_info->support_speeds) {
705                         netdev_err(dev, "Advertising parameters are not supported! (adv: 0x%x)\n",
706                                    cmd->advertising);
707                         rc = -EINVAL;
708                         goto set_setting_exit;
709                 }
710                 link_info->autoneg |= BNXT_AUTONEG_SPEED;
711                 if (!fw_advertising)
712                         link_info->advertising = link_info->support_speeds;
713                 else
714                         link_info->advertising = fw_advertising;
715                 /* any change to autoneg will cause link change, therefore the
716                  * driver should put back the original pause setting in autoneg
717                  */
718                 set_pause = true;
719         } else {
720                 /* TODO: currently don't support half duplex */
721                 if (cmd->duplex == DUPLEX_HALF) {
722                         netdev_err(dev, "HALF DUPLEX is not supported!\n");
723                         rc = -EINVAL;
724                         goto set_setting_exit;
725                 }
726                 /* If received a request for an unknown duplex, assume full*/
727                 if (cmd->duplex == DUPLEX_UNKNOWN)
728                         cmd->duplex = DUPLEX_FULL;
729                 speed = ethtool_cmd_speed(cmd);
730                 link_info->req_link_speed = bnxt_get_fw_speed(dev, speed);
731                 link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
732                 link_info->autoneg &= ~BNXT_AUTONEG_SPEED;
733                 link_info->advertising = 0;
734         }
735
736         if (netif_running(dev))
737                 rc = bnxt_hwrm_set_link_setting(bp, set_pause);
738
739 set_setting_exit:
740         return rc;
741 }
742
743 static void bnxt_get_pauseparam(struct net_device *dev,
744                                 struct ethtool_pauseparam *epause)
745 {
746         struct bnxt *bp = netdev_priv(dev);
747         struct bnxt_link_info *link_info = &bp->link_info;
748
749         if (BNXT_VF(bp))
750                 return;
751         epause->autoneg = !!(link_info->auto_pause_setting &
752                              BNXT_LINK_PAUSE_BOTH);
753         epause->rx_pause = ((link_info->pause & BNXT_LINK_PAUSE_RX) != 0);
754         epause->tx_pause = ((link_info->pause & BNXT_LINK_PAUSE_TX) != 0);
755 }
756
757 static int bnxt_set_pauseparam(struct net_device *dev,
758                                struct ethtool_pauseparam *epause)
759 {
760         int rc = 0;
761         struct bnxt *bp = netdev_priv(dev);
762         struct bnxt_link_info *link_info = &bp->link_info;
763
764         if (BNXT_VF(bp))
765                 return rc;
766
767         if (epause->autoneg) {
768                 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
769                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_BOTH;
770         } else {
771                 /* when transition from auto pause to force pause,
772                  * force a link change
773                  */
774                 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
775                         link_info->force_link_chng = true;
776                 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
777                 link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_BOTH;
778         }
779         if (epause->rx_pause)
780                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
781         else
782                 link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_RX;
783
784         if (epause->tx_pause)
785                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
786         else
787                 link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_TX;
788
789         if (netif_running(dev))
790                 rc = bnxt_hwrm_set_pause(bp);
791         return rc;
792 }
793
794 static u32 bnxt_get_link(struct net_device *dev)
795 {
796         struct bnxt *bp = netdev_priv(dev);
797
798         /* TODO: handle MF, VF, driver close case */
799         return bp->link_info.link_up;
800 }
801
802 static int bnxt_flash_nvram(struct net_device *dev,
803                             u16 dir_type,
804                             u16 dir_ordinal,
805                             u16 dir_ext,
806                             u16 dir_attr,
807                             const u8 *data,
808                             size_t data_len)
809 {
810         struct bnxt *bp = netdev_priv(dev);
811         int rc;
812         struct hwrm_nvm_write_input req = {0};
813         dma_addr_t dma_handle;
814         u8 *kmem;
815
816         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
817
818         req.dir_type = cpu_to_le16(dir_type);
819         req.dir_ordinal = cpu_to_le16(dir_ordinal);
820         req.dir_ext = cpu_to_le16(dir_ext);
821         req.dir_attr = cpu_to_le16(dir_attr);
822         req.dir_data_length = cpu_to_le32(data_len);
823
824         kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
825                                   GFP_KERNEL);
826         if (!kmem) {
827                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
828                            (unsigned)data_len);
829                 return -ENOMEM;
830         }
831         memcpy(kmem, data, data_len);
832         req.host_src_addr = cpu_to_le64(dma_handle);
833
834         rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
835         dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
836
837         return rc;
838 }
839
840 static int bnxt_firmware_reset(struct net_device *dev,
841                                u16 dir_type)
842 {
843         struct bnxt *bp = netdev_priv(dev);
844         struct hwrm_fw_reset_input req = {0};
845
846         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
847
848         /* TODO: Support ASAP ChiMP self-reset (e.g. upon PF driver unload) */
849         /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
850         /*       (e.g. when firmware isn't already running) */
851         switch (dir_type) {
852         case BNX_DIR_TYPE_CHIMP_PATCH:
853         case BNX_DIR_TYPE_BOOTCODE:
854         case BNX_DIR_TYPE_BOOTCODE_2:
855                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
856                 /* Self-reset ChiMP upon next PCIe reset: */
857                 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
858                 break;
859         case BNX_DIR_TYPE_APE_FW:
860         case BNX_DIR_TYPE_APE_PATCH:
861                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
862                 break;
863         case BNX_DIR_TYPE_KONG_FW:
864         case BNX_DIR_TYPE_KONG_PATCH:
865                 req.embedded_proc_type =
866                         FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
867                 break;
868         case BNX_DIR_TYPE_BONO_FW:
869         case BNX_DIR_TYPE_BONO_PATCH:
870                 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
871                 break;
872         default:
873                 return -EINVAL;
874         }
875
876         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
877 }
878
879 static int bnxt_flash_firmware(struct net_device *dev,
880                                u16 dir_type,
881                                const u8 *fw_data,
882                                size_t fw_size)
883 {
884         int     rc = 0;
885         u16     code_type;
886         u32     stored_crc;
887         u32     calculated_crc;
888         struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
889
890         switch (dir_type) {
891         case BNX_DIR_TYPE_BOOTCODE:
892         case BNX_DIR_TYPE_BOOTCODE_2:
893                 code_type = CODE_BOOT;
894                 break;
895         case BNX_DIR_TYPE_APE_FW:
896                 code_type = CODE_MCTP_PASSTHRU;
897                 break;
898         default:
899                 netdev_err(dev, "Unsupported directory entry type: %u\n",
900                            dir_type);
901                 return -EINVAL;
902         }
903         if (fw_size < sizeof(struct bnxt_fw_header)) {
904                 netdev_err(dev, "Invalid firmware file size: %u\n",
905                            (unsigned int)fw_size);
906                 return -EINVAL;
907         }
908         if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
909                 netdev_err(dev, "Invalid firmware signature: %08X\n",
910                            le32_to_cpu(header->signature));
911                 return -EINVAL;
912         }
913         if (header->code_type != code_type) {
914                 netdev_err(dev, "Expected firmware type: %d, read: %d\n",
915                            code_type, header->code_type);
916                 return -EINVAL;
917         }
918         if (header->device != DEVICE_CUMULUS_FAMILY) {
919                 netdev_err(dev, "Expected firmware device family %d, read: %d\n",
920                            DEVICE_CUMULUS_FAMILY, header->device);
921                 return -EINVAL;
922         }
923         /* Confirm the CRC32 checksum of the file: */
924         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
925                                              sizeof(stored_crc)));
926         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
927         if (calculated_crc != stored_crc) {
928                 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
929                            (unsigned long)stored_crc,
930                            (unsigned long)calculated_crc);
931                 return -EINVAL;
932         }
933         /* TODO: Validate digital signature (RSA-encrypted SHA-256 hash) here */
934         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
935                               0, 0, fw_data, fw_size);
936         if (rc == 0)    /* Firmware update successful */
937                 rc = bnxt_firmware_reset(dev, dir_type);
938
939         return rc;
940 }
941
942 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
943 {
944         switch (dir_type) {
945         case BNX_DIR_TYPE_CHIMP_PATCH:
946         case BNX_DIR_TYPE_BOOTCODE:
947         case BNX_DIR_TYPE_BOOTCODE_2:
948         case BNX_DIR_TYPE_APE_FW:
949         case BNX_DIR_TYPE_APE_PATCH:
950         case BNX_DIR_TYPE_KONG_FW:
951         case BNX_DIR_TYPE_KONG_PATCH:
952                 return true;
953         }
954
955         return false;
956 }
957
958 static bool bnxt_dir_type_is_unprotected_exec_format(u16 dir_type)
959 {
960         switch (dir_type) {
961         case BNX_DIR_TYPE_AVS:
962         case BNX_DIR_TYPE_EXP_ROM_MBA:
963         case BNX_DIR_TYPE_PCIE:
964         case BNX_DIR_TYPE_TSCF_UCODE:
965         case BNX_DIR_TYPE_EXT_PHY:
966         case BNX_DIR_TYPE_CCM:
967         case BNX_DIR_TYPE_ISCSI_BOOT:
968         case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
969         case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
970                 return true;
971         }
972
973         return false;
974 }
975
976 static bool bnxt_dir_type_is_executable(u16 dir_type)
977 {
978         return bnxt_dir_type_is_ape_bin_format(dir_type) ||
979                 bnxt_dir_type_is_unprotected_exec_format(dir_type);
980 }
981
982 static int bnxt_flash_firmware_from_file(struct net_device *dev,
983                                          u16 dir_type,
984                                          const char *filename)
985 {
986         const struct firmware  *fw;
987         int                     rc;
988
989         if (bnxt_dir_type_is_executable(dir_type) == false)
990                 return -EINVAL;
991
992         rc = request_firmware(&fw, filename, &dev->dev);
993         if (rc != 0) {
994                 netdev_err(dev, "Error %d requesting firmware file: %s\n",
995                            rc, filename);
996                 return rc;
997         }
998         if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
999                 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1000         else
1001                 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1002                                       0, 0, fw->data, fw->size);
1003         release_firmware(fw);
1004         return rc;
1005 }
1006
1007 static int bnxt_flash_package_from_file(struct net_device *dev,
1008                                         char *filename)
1009 {
1010         netdev_err(dev, "packages are not yet supported\n");
1011         return -EINVAL;
1012 }
1013
1014 static int bnxt_flash_device(struct net_device *dev,
1015                              struct ethtool_flash *flash)
1016 {
1017         if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1018                 netdev_err(dev, "flashdev not supported from a virtual function\n");
1019                 return -EINVAL;
1020         }
1021
1022         if (flash->region == ETHTOOL_FLASH_ALL_REGIONS)
1023                 return bnxt_flash_package_from_file(dev, flash->data);
1024
1025         return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1026 }
1027
1028 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1029 {
1030         struct bnxt *bp = netdev_priv(dev);
1031         int rc;
1032         struct hwrm_nvm_get_dir_info_input req = {0};
1033         struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1034
1035         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1036
1037         mutex_lock(&bp->hwrm_cmd_lock);
1038         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1039         if (!rc) {
1040                 *entries = le32_to_cpu(output->entries);
1041                 *length = le32_to_cpu(output->entry_length);
1042         }
1043         mutex_unlock(&bp->hwrm_cmd_lock);
1044         return rc;
1045 }
1046
1047 static int bnxt_get_eeprom_len(struct net_device *dev)
1048 {
1049         /* The -1 return value allows the entire 32-bit range of offsets to be
1050          * passed via the ethtool command-line utility.
1051          */
1052         return -1;
1053 }
1054
1055 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1056 {
1057         struct bnxt *bp = netdev_priv(dev);
1058         int rc;
1059         u32 dir_entries;
1060         u32 entry_length;
1061         u8 *buf;
1062         size_t buflen;
1063         dma_addr_t dma_handle;
1064         struct hwrm_nvm_get_dir_entries_input req = {0};
1065
1066         rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1067         if (rc != 0)
1068                 return rc;
1069
1070         /* Insert 2 bytes of directory info (count and size of entries) */
1071         if (len < 2)
1072                 return -EINVAL;
1073
1074         *data++ = dir_entries;
1075         *data++ = entry_length;
1076         len -= 2;
1077         memset(data, 0xff, len);
1078
1079         buflen = dir_entries * entry_length;
1080         buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1081                                  GFP_KERNEL);
1082         if (!buf) {
1083                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1084                            (unsigned)buflen);
1085                 return -ENOMEM;
1086         }
1087         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1088         req.host_dest_addr = cpu_to_le64(dma_handle);
1089         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1090         if (rc == 0)
1091                 memcpy(data, buf, len > buflen ? buflen : len);
1092         dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1093         return rc;
1094 }
1095
1096 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1097                                u32 length, u8 *data)
1098 {
1099         struct bnxt *bp = netdev_priv(dev);
1100         int rc;
1101         u8 *buf;
1102         dma_addr_t dma_handle;
1103         struct hwrm_nvm_read_input req = {0};
1104
1105         buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1106                                  GFP_KERNEL);
1107         if (!buf) {
1108                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1109                            (unsigned)length);
1110                 return -ENOMEM;
1111         }
1112         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1113         req.host_dest_addr = cpu_to_le64(dma_handle);
1114         req.dir_idx = cpu_to_le16(index);
1115         req.offset = cpu_to_le32(offset);
1116         req.len = cpu_to_le32(length);
1117
1118         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1119         if (rc == 0)
1120                 memcpy(data, buf, length);
1121         dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1122         return rc;
1123 }
1124
1125 static int bnxt_get_eeprom(struct net_device *dev,
1126                            struct ethtool_eeprom *eeprom,
1127                            u8 *data)
1128 {
1129         u32 index;
1130         u32 offset;
1131
1132         if (eeprom->offset == 0) /* special offset value to get directory */
1133                 return bnxt_get_nvram_directory(dev, eeprom->len, data);
1134
1135         index = eeprom->offset >> 24;
1136         offset = eeprom->offset & 0xffffff;
1137
1138         if (index == 0) {
1139                 netdev_err(dev, "unsupported index value: %d\n", index);
1140                 return -EINVAL;
1141         }
1142
1143         return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
1144 }
1145
1146 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
1147 {
1148         struct bnxt *bp = netdev_priv(dev);
1149         struct hwrm_nvm_erase_dir_entry_input req = {0};
1150
1151         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
1152         req.dir_idx = cpu_to_le16(index);
1153         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1154 }
1155
1156 static int bnxt_set_eeprom(struct net_device *dev,
1157                            struct ethtool_eeprom *eeprom,
1158                            u8 *data)
1159 {
1160         struct bnxt *bp = netdev_priv(dev);
1161         u8 index, dir_op;
1162         u16 type, ext, ordinal, attr;
1163
1164         if (!BNXT_PF(bp)) {
1165                 netdev_err(dev, "NVM write not supported from a virtual function\n");
1166                 return -EINVAL;
1167         }
1168
1169         type = eeprom->magic >> 16;
1170
1171         if (type == 0xffff) { /* special value for directory operations */
1172                 index = eeprom->magic & 0xff;
1173                 dir_op = eeprom->magic >> 8;
1174                 if (index == 0)
1175                         return -EINVAL;
1176                 switch (dir_op) {
1177                 case 0x0e: /* erase */
1178                         if (eeprom->offset != ~eeprom->magic)
1179                                 return -EINVAL;
1180                         return bnxt_erase_nvram_directory(dev, index - 1);
1181                 default:
1182                         return -EINVAL;
1183                 }
1184         }
1185
1186         /* Create or re-write an NVM item: */
1187         if (bnxt_dir_type_is_executable(type) == true)
1188                 return -EINVAL;
1189         ext = eeprom->magic & 0xffff;
1190         ordinal = eeprom->offset >> 16;
1191         attr = eeprom->offset & 0xffff;
1192
1193         return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
1194                                 eeprom->len);
1195 }
1196
1197 const struct ethtool_ops bnxt_ethtool_ops = {
1198         .get_settings           = bnxt_get_settings,
1199         .set_settings           = bnxt_set_settings,
1200         .get_pauseparam         = bnxt_get_pauseparam,
1201         .set_pauseparam         = bnxt_set_pauseparam,
1202         .get_drvinfo            = bnxt_get_drvinfo,
1203         .get_coalesce           = bnxt_get_coalesce,
1204         .set_coalesce           = bnxt_set_coalesce,
1205         .get_msglevel           = bnxt_get_msglevel,
1206         .set_msglevel           = bnxt_set_msglevel,
1207         .get_sset_count         = bnxt_get_sset_count,
1208         .get_strings            = bnxt_get_strings,
1209         .get_ethtool_stats      = bnxt_get_ethtool_stats,
1210         .set_ringparam          = bnxt_set_ringparam,
1211         .get_ringparam          = bnxt_get_ringparam,
1212         .get_channels           = bnxt_get_channels,
1213         .set_channels           = bnxt_set_channels,
1214 #ifdef CONFIG_RFS_ACCEL
1215         .get_rxnfc              = bnxt_get_rxnfc,
1216 #endif
1217         .get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
1218         .get_rxfh_key_size      = bnxt_get_rxfh_key_size,
1219         .get_rxfh               = bnxt_get_rxfh,
1220         .flash_device           = bnxt_flash_device,
1221         .get_eeprom_len         = bnxt_get_eeprom_len,
1222         .get_eeprom             = bnxt_get_eeprom,
1223         .set_eeprom             = bnxt_set_eeprom,
1224         .get_link               = bnxt_get_link,
1225 };