]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/broadcom/bnxt/bnxt_ethtool.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/ide
[karo-tx-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);
215         tcs = netdev_get_num_tc(dev);
216         if (tcs > 1)
217                 max_tx_rings /= tcs;
218
219         channel->max_rx = max_rx_rings;
220         channel->max_tx = max_tx_rings;
221         channel->max_other = 0;
222         channel->max_combined = 0;
223         channel->rx_count = bp->rx_nr_rings;
224         channel->tx_count = bp->tx_nr_rings_per_tc;
225 }
226
227 static int bnxt_set_channels(struct net_device *dev,
228                              struct ethtool_channels *channel)
229 {
230         struct bnxt *bp = netdev_priv(dev);
231         int max_rx_rings, max_tx_rings, tcs;
232         u32 rc = 0;
233
234         if (channel->other_count || channel->combined_count ||
235             !channel->rx_count || !channel->tx_count)
236                 return -EINVAL;
237
238         bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings);
239         tcs = netdev_get_num_tc(dev);
240         if (tcs > 1)
241                 max_tx_rings /= tcs;
242
243         if (channel->rx_count > max_rx_rings ||
244             channel->tx_count > max_tx_rings)
245                 return -EINVAL;
246
247         if (netif_running(dev)) {
248                 if (BNXT_PF(bp)) {
249                         /* TODO CHIMP_FW: Send message to all VF's
250                          * before PF unload
251                          */
252                 }
253                 rc = bnxt_close_nic(bp, true, false);
254                 if (rc) {
255                         netdev_err(bp->dev, "Set channel failure rc :%x\n",
256                                    rc);
257                         return rc;
258                 }
259         }
260
261         bp->rx_nr_rings = channel->rx_count;
262         bp->tx_nr_rings_per_tc = channel->tx_count;
263         bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
264         if (tcs > 1)
265                 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
266         bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
267         bp->num_stat_ctxs = bp->cp_nr_rings;
268
269         if (netif_running(dev)) {
270                 rc = bnxt_open_nic(bp, true, false);
271                 if ((!rc) && BNXT_PF(bp)) {
272                         /* TODO CHIMP_FW: Send message to all VF's
273                          * to renable
274                          */
275                 }
276         }
277
278         return rc;
279 }
280
281 #ifdef CONFIG_RFS_ACCEL
282 static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
283                             u32 *rule_locs)
284 {
285         int i, j = 0;
286
287         cmd->data = bp->ntp_fltr_count;
288         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
289                 struct hlist_head *head;
290                 struct bnxt_ntuple_filter *fltr;
291
292                 head = &bp->ntp_fltr_hash_tbl[i];
293                 rcu_read_lock();
294                 hlist_for_each_entry_rcu(fltr, head, hash) {
295                         if (j == cmd->rule_cnt)
296                                 break;
297                         rule_locs[j++] = fltr->sw_id;
298                 }
299                 rcu_read_unlock();
300                 if (j == cmd->rule_cnt)
301                         break;
302         }
303         cmd->rule_cnt = j;
304         return 0;
305 }
306
307 static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
308 {
309         struct ethtool_rx_flow_spec *fs =
310                 (struct ethtool_rx_flow_spec *)&cmd->fs;
311         struct bnxt_ntuple_filter *fltr;
312         struct flow_keys *fkeys;
313         int i, rc = -EINVAL;
314
315         if (fs->location < 0 || fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
316                 return rc;
317
318         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
319                 struct hlist_head *head;
320
321                 head = &bp->ntp_fltr_hash_tbl[i];
322                 rcu_read_lock();
323                 hlist_for_each_entry_rcu(fltr, head, hash) {
324                         if (fltr->sw_id == fs->location)
325                                 goto fltr_found;
326                 }
327                 rcu_read_unlock();
328         }
329         return rc;
330
331 fltr_found:
332         fkeys = &fltr->fkeys;
333         if (fkeys->basic.ip_proto == IPPROTO_TCP)
334                 fs->flow_type = TCP_V4_FLOW;
335         else if (fkeys->basic.ip_proto == IPPROTO_UDP)
336                 fs->flow_type = UDP_V4_FLOW;
337         else
338                 goto fltr_err;
339
340         fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
341         fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
342
343         fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
344         fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
345
346         fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
347         fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
348
349         fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
350         fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
351
352         fs->ring_cookie = fltr->rxq;
353         rc = 0;
354
355 fltr_err:
356         rcu_read_unlock();
357
358         return rc;
359 }
360
361 static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
362                           u32 *rule_locs)
363 {
364         struct bnxt *bp = netdev_priv(dev);
365         int rc = 0;
366
367         switch (cmd->cmd) {
368         case ETHTOOL_GRXRINGS:
369                 cmd->data = bp->rx_nr_rings;
370                 break;
371
372         case ETHTOOL_GRXCLSRLCNT:
373                 cmd->rule_cnt = bp->ntp_fltr_count;
374                 cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
375                 break;
376
377         case ETHTOOL_GRXCLSRLALL:
378                 rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
379                 break;
380
381         case ETHTOOL_GRXCLSRULE:
382                 rc = bnxt_grxclsrule(bp, cmd);
383                 break;
384
385         default:
386                 rc = -EOPNOTSUPP;
387                 break;
388         }
389
390         return rc;
391 }
392 #endif
393
394 static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
395 {
396         return HW_HASH_INDEX_SIZE;
397 }
398
399 static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
400 {
401         return HW_HASH_KEY_SIZE;
402 }
403
404 static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
405                          u8 *hfunc)
406 {
407         struct bnxt *bp = netdev_priv(dev);
408         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
409         int i = 0;
410
411         if (hfunc)
412                 *hfunc = ETH_RSS_HASH_TOP;
413
414         if (indir)
415                 for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
416                         indir[i] = le16_to_cpu(vnic->rss_table[i]);
417
418         if (key)
419                 memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
420
421         return 0;
422 }
423
424 static void bnxt_get_drvinfo(struct net_device *dev,
425                              struct ethtool_drvinfo *info)
426 {
427         struct bnxt *bp = netdev_priv(dev);
428
429         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
430         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
431         strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version));
432         strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
433         info->n_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
434         info->testinfo_len = BNXT_NUM_TESTS(bp);
435         /* TODO CHIMP_FW: eeprom dump details */
436         info->eedump_len = 0;
437         /* TODO CHIMP FW: reg dump details */
438         info->regdump_len = 0;
439 }
440
441 static u32 bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info)
442 {
443         u16 fw_speeds = link_info->support_speeds;
444         u32 speed_mask = 0;
445
446         if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
447                 speed_mask |= SUPPORTED_100baseT_Full;
448         if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
449                 speed_mask |= SUPPORTED_1000baseT_Full;
450         if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
451                 speed_mask |= SUPPORTED_2500baseX_Full;
452         if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
453                 speed_mask |= SUPPORTED_10000baseT_Full;
454         /* TODO: support 25GB, 50GB with different cable type */
455         if (fw_speeds & BNXT_LINK_SPEED_MSK_20GB)
456                 speed_mask |= SUPPORTED_20000baseMLD2_Full |
457                         SUPPORTED_20000baseKR2_Full;
458         if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
459                 speed_mask |= SUPPORTED_40000baseKR4_Full |
460                         SUPPORTED_40000baseCR4_Full |
461                         SUPPORTED_40000baseSR4_Full |
462                         SUPPORTED_40000baseLR4_Full;
463
464         return speed_mask;
465 }
466
467 static u32 bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info)
468 {
469         u16 fw_speeds = link_info->auto_link_speeds;
470         u32 speed_mask = 0;
471
472         /* TODO: support 25GB, 40GB, 50GB with different cable type */
473         /* set the advertised speeds */
474         if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
475                 speed_mask |= ADVERTISED_100baseT_Full;
476         if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
477                 speed_mask |= ADVERTISED_1000baseT_Full;
478         if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
479                 speed_mask |= ADVERTISED_2500baseX_Full;
480         if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
481                 speed_mask |= ADVERTISED_10000baseT_Full;
482         /* TODO: how to advertise 20, 25, 40, 50GB with different cable type ?*/
483         if (fw_speeds & BNXT_LINK_SPEED_MSK_20GB)
484                 speed_mask |= ADVERTISED_20000baseMLD2_Full |
485                               ADVERTISED_20000baseKR2_Full;
486         if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
487                 speed_mask |= ADVERTISED_40000baseKR4_Full |
488                               ADVERTISED_40000baseCR4_Full |
489                               ADVERTISED_40000baseSR4_Full |
490                               ADVERTISED_40000baseLR4_Full;
491         return speed_mask;
492 }
493
494 u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
495 {
496         switch (fw_link_speed) {
497         case BNXT_LINK_SPEED_100MB:
498                 return SPEED_100;
499         case BNXT_LINK_SPEED_1GB:
500                 return SPEED_1000;
501         case BNXT_LINK_SPEED_2_5GB:
502                 return SPEED_2500;
503         case BNXT_LINK_SPEED_10GB:
504                 return SPEED_10000;
505         case BNXT_LINK_SPEED_20GB:
506                 return SPEED_20000;
507         case BNXT_LINK_SPEED_25GB:
508                 return SPEED_25000;
509         case BNXT_LINK_SPEED_40GB:
510                 return SPEED_40000;
511         case BNXT_LINK_SPEED_50GB:
512                 return SPEED_50000;
513         default:
514                 return SPEED_UNKNOWN;
515         }
516 }
517
518 static int bnxt_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
519 {
520         struct bnxt *bp = netdev_priv(dev);
521         struct bnxt_link_info *link_info = &bp->link_info;
522         u16 ethtool_speed;
523
524         cmd->supported = bnxt_fw_to_ethtool_support_spds(link_info);
525
526         if (link_info->auto_link_speeds)
527                 cmd->supported |= SUPPORTED_Autoneg;
528
529         if (BNXT_AUTO_MODE(link_info->auto_mode)) {
530                 cmd->advertising =
531                         bnxt_fw_to_ethtool_advertised_spds(link_info);
532                 cmd->advertising |= ADVERTISED_Autoneg;
533                 cmd->autoneg = AUTONEG_ENABLE;
534         } else {
535                 cmd->autoneg = AUTONEG_DISABLE;
536                 cmd->advertising = 0;
537         }
538         if (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) {
539                 if ((link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) ==
540                     BNXT_LINK_PAUSE_BOTH) {
541                         cmd->advertising |= ADVERTISED_Pause;
542                         cmd->supported |= SUPPORTED_Pause;
543                 } else {
544                         cmd->advertising |= ADVERTISED_Asym_Pause;
545                         cmd->supported |= SUPPORTED_Asym_Pause;
546                         if (link_info->auto_pause_setting &
547                             BNXT_LINK_PAUSE_RX)
548                                 cmd->advertising |= ADVERTISED_Pause;
549                 }
550         } else if (link_info->force_pause_setting & BNXT_LINK_PAUSE_BOTH) {
551                 if ((link_info->force_pause_setting & BNXT_LINK_PAUSE_BOTH) ==
552                     BNXT_LINK_PAUSE_BOTH) {
553                         cmd->supported |= SUPPORTED_Pause;
554                 } else {
555                         cmd->supported |= SUPPORTED_Asym_Pause;
556                         if (link_info->force_pause_setting &
557                             BNXT_LINK_PAUSE_RX)
558                                 cmd->supported |= SUPPORTED_Pause;
559                 }
560         }
561
562         cmd->port = PORT_NONE;
563         if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
564                 cmd->port = PORT_TP;
565                 cmd->supported |= SUPPORTED_TP;
566                 cmd->advertising |= ADVERTISED_TP;
567         } else {
568                 cmd->supported |= SUPPORTED_FIBRE;
569                 cmd->advertising |= ADVERTISED_FIBRE;
570
571                 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
572                         cmd->port = PORT_DA;
573                 else if (link_info->media_type ==
574                          PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
575                         cmd->port = PORT_FIBRE;
576         }
577
578         if (link_info->phy_link_status == BNXT_LINK_LINK) {
579                 if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
580                         cmd->duplex = DUPLEX_FULL;
581         } else {
582                 cmd->duplex = DUPLEX_UNKNOWN;
583         }
584         ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
585         ethtool_cmd_speed_set(cmd, ethtool_speed);
586         if (link_info->transceiver ==
587                 PORT_PHY_QCFG_RESP_TRANSCEIVER_TYPE_XCVR_INTERNAL)
588                 cmd->transceiver = XCVR_INTERNAL;
589         else
590                 cmd->transceiver = XCVR_EXTERNAL;
591         cmd->phy_address = link_info->phy_addr;
592
593         return 0;
594 }
595
596 static u32 bnxt_get_fw_speed(struct net_device *dev, u16 ethtool_speed)
597 {
598         switch (ethtool_speed) {
599         case SPEED_100:
600                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
601         case SPEED_1000:
602                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
603         case SPEED_2500:
604                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
605         case SPEED_10000:
606                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
607         case SPEED_20000:
608                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
609         case SPEED_25000:
610                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
611         case SPEED_40000:
612                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
613         case SPEED_50000:
614                 return PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
615         default:
616                 netdev_err(dev, "unsupported speed!\n");
617                 break;
618         }
619         return 0;
620 }
621
622 static u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
623 {
624         u16 fw_speed_mask = 0;
625
626         /* only support autoneg at speed 100, 1000, and 10000 */
627         if (advertising & (ADVERTISED_100baseT_Full |
628                            ADVERTISED_100baseT_Half)) {
629                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
630         }
631         if (advertising & (ADVERTISED_1000baseT_Full |
632                            ADVERTISED_1000baseT_Half)) {
633                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
634         }
635         if (advertising & ADVERTISED_10000baseT_Full)
636                 fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
637
638         return fw_speed_mask;
639 }
640
641 static int bnxt_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
642 {
643         int rc = 0;
644         struct bnxt *bp = netdev_priv(dev);
645         struct bnxt_link_info *link_info = &bp->link_info;
646         u32 speed, fw_advertising = 0;
647         bool set_pause = false;
648
649         if (BNXT_VF(bp))
650                 return rc;
651
652         if (cmd->autoneg == AUTONEG_ENABLE) {
653                 if (link_info->media_type != PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
654                         netdev_err(dev, "Media type doesn't support autoneg\n");
655                         rc = -EINVAL;
656                         goto set_setting_exit;
657                 }
658                 if (cmd->advertising & ~(BNXT_ALL_COPPER_ETHTOOL_SPEED |
659                                          ADVERTISED_Autoneg |
660                                          ADVERTISED_TP |
661                                          ADVERTISED_Pause |
662                                          ADVERTISED_Asym_Pause)) {
663                         netdev_err(dev, "Unsupported advertising mask (adv: 0x%x)\n",
664                                    cmd->advertising);
665                         rc = -EINVAL;
666                         goto set_setting_exit;
667                 }
668                 fw_advertising = bnxt_get_fw_auto_link_speeds(cmd->advertising);
669                 if (fw_advertising & ~link_info->support_speeds) {
670                         netdev_err(dev, "Advertising parameters are not supported! (adv: 0x%x)\n",
671                                    cmd->advertising);
672                         rc = -EINVAL;
673                         goto set_setting_exit;
674                 }
675                 link_info->autoneg |= BNXT_AUTONEG_SPEED;
676                 if (!fw_advertising)
677                         link_info->advertising = link_info->support_speeds;
678                 else
679                         link_info->advertising = fw_advertising;
680                 /* any change to autoneg will cause link change, therefore the
681                  * driver should put back the original pause setting in autoneg
682                  */
683                 set_pause = true;
684         } else {
685                 /* TODO: currently don't support half duplex */
686                 if (cmd->duplex == DUPLEX_HALF) {
687                         netdev_err(dev, "HALF DUPLEX is not supported!\n");
688                         rc = -EINVAL;
689                         goto set_setting_exit;
690                 }
691                 /* If received a request for an unknown duplex, assume full*/
692                 if (cmd->duplex == DUPLEX_UNKNOWN)
693                         cmd->duplex = DUPLEX_FULL;
694                 speed = ethtool_cmd_speed(cmd);
695                 link_info->req_link_speed = bnxt_get_fw_speed(dev, speed);
696                 link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
697                 link_info->autoneg &= ~BNXT_AUTONEG_SPEED;
698                 link_info->advertising = 0;
699         }
700
701         if (netif_running(dev))
702                 rc = bnxt_hwrm_set_link_setting(bp, set_pause);
703
704 set_setting_exit:
705         return rc;
706 }
707
708 static void bnxt_get_pauseparam(struct net_device *dev,
709                                 struct ethtool_pauseparam *epause)
710 {
711         struct bnxt *bp = netdev_priv(dev);
712         struct bnxt_link_info *link_info = &bp->link_info;
713
714         if (BNXT_VF(bp))
715                 return;
716         epause->autoneg = !!(link_info->auto_pause_setting &
717                              BNXT_LINK_PAUSE_BOTH);
718         epause->rx_pause = ((link_info->pause & BNXT_LINK_PAUSE_RX) != 0);
719         epause->tx_pause = ((link_info->pause & BNXT_LINK_PAUSE_TX) != 0);
720 }
721
722 static int bnxt_set_pauseparam(struct net_device *dev,
723                                struct ethtool_pauseparam *epause)
724 {
725         int rc = 0;
726         struct bnxt *bp = netdev_priv(dev);
727         struct bnxt_link_info *link_info = &bp->link_info;
728
729         if (BNXT_VF(bp))
730                 return rc;
731
732         if (epause->autoneg) {
733                 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
734                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_BOTH;
735         } else {
736                 /* when transition from auto pause to force pause,
737                  * force a link change
738                  */
739                 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
740                         link_info->force_link_chng = true;
741                 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
742                 link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_BOTH;
743         }
744         if (epause->rx_pause)
745                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
746         else
747                 link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_RX;
748
749         if (epause->tx_pause)
750                 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
751         else
752                 link_info->req_flow_ctrl &= ~BNXT_LINK_PAUSE_TX;
753
754         if (netif_running(dev))
755                 rc = bnxt_hwrm_set_pause(bp);
756         return rc;
757 }
758
759 static u32 bnxt_get_link(struct net_device *dev)
760 {
761         struct bnxt *bp = netdev_priv(dev);
762
763         /* TODO: handle MF, VF, driver close case */
764         return bp->link_info.link_up;
765 }
766
767 static int bnxt_flash_nvram(struct net_device *dev,
768                             u16 dir_type,
769                             u16 dir_ordinal,
770                             u16 dir_ext,
771                             u16 dir_attr,
772                             const u8 *data,
773                             size_t data_len)
774 {
775         struct bnxt *bp = netdev_priv(dev);
776         int rc;
777         struct hwrm_nvm_write_input req = {0};
778         dma_addr_t dma_handle;
779         u8 *kmem;
780
781         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
782
783         req.dir_type = cpu_to_le16(dir_type);
784         req.dir_ordinal = cpu_to_le16(dir_ordinal);
785         req.dir_ext = cpu_to_le16(dir_ext);
786         req.dir_attr = cpu_to_le16(dir_attr);
787         req.dir_data_length = cpu_to_le32(data_len);
788
789         kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
790                                   GFP_KERNEL);
791         if (!kmem) {
792                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
793                            (unsigned)data_len);
794                 return -ENOMEM;
795         }
796         memcpy(kmem, data, data_len);
797         req.host_src_addr = cpu_to_le64(dma_handle);
798
799         rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
800         dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
801
802         return rc;
803 }
804
805 static int bnxt_flash_firmware(struct net_device *dev,
806                                u16 dir_type,
807                                const u8 *fw_data,
808                                size_t fw_size)
809 {
810         int     rc = 0;
811         u16     code_type;
812         u32     stored_crc;
813         u32     calculated_crc;
814         struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
815
816         switch (dir_type) {
817         case BNX_DIR_TYPE_BOOTCODE:
818         case BNX_DIR_TYPE_BOOTCODE_2:
819                 code_type = CODE_BOOT;
820                 break;
821         default:
822                 netdev_err(dev, "Unsupported directory entry type: %u\n",
823                            dir_type);
824                 return -EINVAL;
825         }
826         if (fw_size < sizeof(struct bnxt_fw_header)) {
827                 netdev_err(dev, "Invalid firmware file size: %u\n",
828                            (unsigned int)fw_size);
829                 return -EINVAL;
830         }
831         if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
832                 netdev_err(dev, "Invalid firmware signature: %08X\n",
833                            le32_to_cpu(header->signature));
834                 return -EINVAL;
835         }
836         if (header->code_type != code_type) {
837                 netdev_err(dev, "Expected firmware type: %d, read: %d\n",
838                            code_type, header->code_type);
839                 return -EINVAL;
840         }
841         if (header->device != DEVICE_CUMULUS_FAMILY) {
842                 netdev_err(dev, "Expected firmware device family %d, read: %d\n",
843                            DEVICE_CUMULUS_FAMILY, header->device);
844                 return -EINVAL;
845         }
846         /* Confirm the CRC32 checksum of the file: */
847         stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
848                                              sizeof(stored_crc)));
849         calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
850         if (calculated_crc != stored_crc) {
851                 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
852                            (unsigned long)stored_crc,
853                            (unsigned long)calculated_crc);
854                 return -EINVAL;
855         }
856         /* TODO: Validate digital signature (RSA-encrypted SHA-256 hash) here */
857         rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
858                               0, 0, fw_data, fw_size);
859         if (rc == 0) {  /* Firmware update successful */
860                 /* TODO: Notify processor it needs to reset itself
861                  */
862         }
863         return rc;
864 }
865
866 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
867 {
868         switch (dir_type) {
869         case BNX_DIR_TYPE_CHIMP_PATCH:
870         case BNX_DIR_TYPE_BOOTCODE:
871         case BNX_DIR_TYPE_BOOTCODE_2:
872         case BNX_DIR_TYPE_APE_FW:
873         case BNX_DIR_TYPE_APE_PATCH:
874         case BNX_DIR_TYPE_KONG_FW:
875         case BNX_DIR_TYPE_KONG_PATCH:
876                 return true;
877         }
878
879         return false;
880 }
881
882 static bool bnxt_dir_type_is_unprotected_exec_format(u16 dir_type)
883 {
884         switch (dir_type) {
885         case BNX_DIR_TYPE_AVS:
886         case BNX_DIR_TYPE_EXP_ROM_MBA:
887         case BNX_DIR_TYPE_PCIE:
888         case BNX_DIR_TYPE_TSCF_UCODE:
889         case BNX_DIR_TYPE_EXT_PHY:
890         case BNX_DIR_TYPE_CCM:
891         case BNX_DIR_TYPE_ISCSI_BOOT:
892         case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
893         case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
894                 return true;
895         }
896
897         return false;
898 }
899
900 static bool bnxt_dir_type_is_executable(u16 dir_type)
901 {
902         return bnxt_dir_type_is_ape_bin_format(dir_type) ||
903                 bnxt_dir_type_is_unprotected_exec_format(dir_type);
904 }
905
906 static int bnxt_flash_firmware_from_file(struct net_device *dev,
907                                          u16 dir_type,
908                                          const char *filename)
909 {
910         const struct firmware  *fw;
911         int                     rc;
912
913         if (bnxt_dir_type_is_executable(dir_type) == false)
914                 return -EINVAL;
915
916         rc = request_firmware(&fw, filename, &dev->dev);
917         if (rc != 0) {
918                 netdev_err(dev, "Error %d requesting firmware file: %s\n",
919                            rc, filename);
920                 return rc;
921         }
922         if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
923                 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
924         else
925                 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
926                                       0, 0, fw->data, fw->size);
927         release_firmware(fw);
928         return rc;
929 }
930
931 static int bnxt_flash_package_from_file(struct net_device *dev,
932                                         char *filename)
933 {
934         netdev_err(dev, "packages are not yet supported\n");
935         return -EINVAL;
936 }
937
938 static int bnxt_flash_device(struct net_device *dev,
939                              struct ethtool_flash *flash)
940 {
941         if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
942                 netdev_err(dev, "flashdev not supported from a virtual function\n");
943                 return -EINVAL;
944         }
945
946         if (flash->region == ETHTOOL_FLASH_ALL_REGIONS)
947                 return bnxt_flash_package_from_file(dev, flash->data);
948
949         return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
950 }
951
952 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
953 {
954         struct bnxt *bp = netdev_priv(dev);
955         int rc;
956         struct hwrm_nvm_get_dir_info_input req = {0};
957         struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
958
959         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
960
961         mutex_lock(&bp->hwrm_cmd_lock);
962         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
963         if (!rc) {
964                 *entries = le32_to_cpu(output->entries);
965                 *length = le32_to_cpu(output->entry_length);
966         }
967         mutex_unlock(&bp->hwrm_cmd_lock);
968         return rc;
969 }
970
971 static int bnxt_get_eeprom_len(struct net_device *dev)
972 {
973         /* The -1 return value allows the entire 32-bit range of offsets to be
974          * passed via the ethtool command-line utility.
975          */
976         return -1;
977 }
978
979 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
980 {
981         struct bnxt *bp = netdev_priv(dev);
982         int rc;
983         u32 dir_entries;
984         u32 entry_length;
985         u8 *buf;
986         size_t buflen;
987         dma_addr_t dma_handle;
988         struct hwrm_nvm_get_dir_entries_input req = {0};
989
990         rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
991         if (rc != 0)
992                 return rc;
993
994         /* Insert 2 bytes of directory info (count and size of entries) */
995         if (len < 2)
996                 return -EINVAL;
997
998         *data++ = dir_entries;
999         *data++ = entry_length;
1000         len -= 2;
1001         memset(data, 0xff, len);
1002
1003         buflen = dir_entries * entry_length;
1004         buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1005                                  GFP_KERNEL);
1006         if (!buf) {
1007                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1008                            (unsigned)buflen);
1009                 return -ENOMEM;
1010         }
1011         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1012         req.host_dest_addr = cpu_to_le64(dma_handle);
1013         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1014         if (rc == 0)
1015                 memcpy(data, buf, len > buflen ? buflen : len);
1016         dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1017         return rc;
1018 }
1019
1020 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1021                                u32 length, u8 *data)
1022 {
1023         struct bnxt *bp = netdev_priv(dev);
1024         int rc;
1025         u8 *buf;
1026         dma_addr_t dma_handle;
1027         struct hwrm_nvm_read_input req = {0};
1028
1029         buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1030                                  GFP_KERNEL);
1031         if (!buf) {
1032                 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1033                            (unsigned)length);
1034                 return -ENOMEM;
1035         }
1036         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1037         req.host_dest_addr = cpu_to_le64(dma_handle);
1038         req.dir_idx = cpu_to_le16(index);
1039         req.offset = cpu_to_le32(offset);
1040         req.len = cpu_to_le32(length);
1041
1042         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1043         if (rc == 0)
1044                 memcpy(data, buf, length);
1045         dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1046         return rc;
1047 }
1048
1049 static int bnxt_get_eeprom(struct net_device *dev,
1050                            struct ethtool_eeprom *eeprom,
1051                            u8 *data)
1052 {
1053         u32 index;
1054         u32 offset;
1055
1056         if (eeprom->offset == 0) /* special offset value to get directory */
1057                 return bnxt_get_nvram_directory(dev, eeprom->len, data);
1058
1059         index = eeprom->offset >> 24;
1060         offset = eeprom->offset & 0xffffff;
1061
1062         if (index == 0) {
1063                 netdev_err(dev, "unsupported index value: %d\n", index);
1064                 return -EINVAL;
1065         }
1066
1067         return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
1068 }
1069
1070 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
1071 {
1072         struct bnxt *bp = netdev_priv(dev);
1073         struct hwrm_nvm_erase_dir_entry_input req = {0};
1074
1075         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
1076         req.dir_idx = cpu_to_le16(index);
1077         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1078 }
1079
1080 static int bnxt_set_eeprom(struct net_device *dev,
1081                            struct ethtool_eeprom *eeprom,
1082                            u8 *data)
1083 {
1084         struct bnxt *bp = netdev_priv(dev);
1085         u8 index, dir_op;
1086         u16 type, ext, ordinal, attr;
1087
1088         if (!BNXT_PF(bp)) {
1089                 netdev_err(dev, "NVM write not supported from a virtual function\n");
1090                 return -EINVAL;
1091         }
1092
1093         type = eeprom->magic >> 16;
1094
1095         if (type == 0xffff) { /* special value for directory operations */
1096                 index = eeprom->magic & 0xff;
1097                 dir_op = eeprom->magic >> 8;
1098                 if (index == 0)
1099                         return -EINVAL;
1100                 switch (dir_op) {
1101                 case 0x0e: /* erase */
1102                         if (eeprom->offset != ~eeprom->magic)
1103                                 return -EINVAL;
1104                         return bnxt_erase_nvram_directory(dev, index - 1);
1105                 default:
1106                         return -EINVAL;
1107                 }
1108         }
1109
1110         /* Create or re-write an NVM item: */
1111         if (bnxt_dir_type_is_executable(type) == true)
1112                 return -EINVAL;
1113         ext = eeprom->magic & 0xffff;
1114         ordinal = eeprom->offset >> 16;
1115         attr = eeprom->offset & 0xffff;
1116
1117         return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
1118                                 eeprom->len);
1119 }
1120
1121 const struct ethtool_ops bnxt_ethtool_ops = {
1122         .get_settings           = bnxt_get_settings,
1123         .set_settings           = bnxt_set_settings,
1124         .get_pauseparam         = bnxt_get_pauseparam,
1125         .set_pauseparam         = bnxt_set_pauseparam,
1126         .get_drvinfo            = bnxt_get_drvinfo,
1127         .get_coalesce           = bnxt_get_coalesce,
1128         .set_coalesce           = bnxt_set_coalesce,
1129         .get_msglevel           = bnxt_get_msglevel,
1130         .set_msglevel           = bnxt_set_msglevel,
1131         .get_sset_count         = bnxt_get_sset_count,
1132         .get_strings            = bnxt_get_strings,
1133         .get_ethtool_stats      = bnxt_get_ethtool_stats,
1134         .set_ringparam          = bnxt_set_ringparam,
1135         .get_ringparam          = bnxt_get_ringparam,
1136         .get_channels           = bnxt_get_channels,
1137         .set_channels           = bnxt_set_channels,
1138 #ifdef CONFIG_RFS_ACCEL
1139         .get_rxnfc              = bnxt_get_rxnfc,
1140 #endif
1141         .get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
1142         .get_rxfh_key_size      = bnxt_get_rxfh_key_size,
1143         .get_rxfh               = bnxt_get_rxfh,
1144         .flash_device           = bnxt_flash_device,
1145         .get_eeprom_len         = bnxt_get_eeprom_len,
1146         .get_eeprom             = bnxt_get_eeprom,
1147         .set_eeprom             = bnxt_set_eeprom,
1148         .get_link               = bnxt_get_link,
1149 };