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