2 * Copyright (C) 2013-2015 Chelsio Communications. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
18 #include <linux/firmware.h>
19 #include <linux/mdio.h>
24 #include "cxgb4_cudbg.h"
26 #define EEPROM_MAGIC 0x38E2F10C
28 static u32 get_msglevel(struct net_device *dev)
30 return netdev2adap(dev)->msg_enable;
33 static void set_msglevel(struct net_device *dev, u32 val)
35 netdev2adap(dev)->msg_enable = val;
38 static const char stats_strings[][ETH_GSTRING_LEN] = {
41 "tx_broadcast_frames ",
42 "tx_multicast_frames ",
47 "tx_frames_65_to_127 ",
48 "tx_frames_128_to_255 ",
49 "tx_frames_256_to_511 ",
50 "tx_frames_512_to_1023 ",
51 "tx_frames_1024_to_1518 ",
52 "tx_frames_1519_to_max ",
67 "rx_broadcast_frames ",
68 "rx_multicast_frames ",
71 "rx_frames_too_long ",
79 "rx_frames_65_to_127 ",
80 "rx_frames_128_to_255 ",
81 "rx_frames_256_to_511 ",
82 "rx_frames_512_to_1023 ",
83 "rx_frames_1024_to_1518 ",
84 "rx_frames_1519_to_max ",
96 "rx_bg0_frames_dropped ",
97 "rx_bg1_frames_dropped ",
98 "rx_bg2_frames_dropped ",
99 "rx_bg3_frames_dropped ",
100 "rx_bg0_frames_trunc ",
101 "rx_bg1_frames_trunc ",
102 "rx_bg2_frames_trunc ",
103 "rx_bg3_frames_trunc ",
114 static char adapter_stats_strings[][ETH_GSTRING_LEN] = {
118 "tcp_ipv4_out_rsts ",
120 "tcp_ipv4_out_segs ",
121 "tcp_ipv4_retrans_segs ",
122 "tcp_ipv6_out_rsts ",
124 "tcp_ipv6_out_segs ",
125 "tcp_ipv6_retrans_segs ",
129 "rdma_no_rqe_mod_defer ",
130 "rdma_no_rqe_pkt_defer ",
131 "tp_err_ofld_no_neigh ",
132 "tp_err_ofld_cong_defer ",
133 "write_coal_success ",
137 static char channel_stats_strings[][ETH_GSTRING_LEN] = {
138 "--------Channel--------- ",
145 "tp_tnl_cong_drops ",
147 "tp_ofld_vlan_drops ",
148 "tp_ofld_chan_drops ",
154 static char loopback_stats_strings[][ETH_GSTRING_LEN] = {
155 "-------Loopback----------- ",
164 "frames_128_to_255 ",
165 "frames_256_to_511 ",
166 "frames_512_to_1023 ",
167 "frames_1024_to_1518 ",
168 "frames_1519_to_max ",
170 "bg0_frames_dropped ",
171 "bg1_frames_dropped ",
172 "bg2_frames_dropped ",
173 "bg3_frames_dropped ",
180 static int get_sset_count(struct net_device *dev, int sset)
184 return ARRAY_SIZE(stats_strings) +
185 ARRAY_SIZE(adapter_stats_strings) +
186 ARRAY_SIZE(channel_stats_strings) +
187 ARRAY_SIZE(loopback_stats_strings);
193 static int get_regs_len(struct net_device *dev)
195 struct adapter *adap = netdev2adap(dev);
197 return t4_get_regs_len(adap);
200 static int get_eeprom_len(struct net_device *dev)
205 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
207 struct adapter *adapter = netdev2adap(dev);
210 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
211 strlcpy(info->version, cxgb4_driver_version,
212 sizeof(info->version));
213 strlcpy(info->bus_info, pci_name(adapter->pdev),
214 sizeof(info->bus_info));
215 info->regdump_len = get_regs_len(dev);
217 if (!adapter->params.fw_vers)
218 strcpy(info->fw_version, "N/A");
220 snprintf(info->fw_version, sizeof(info->fw_version),
221 "%u.%u.%u.%u, TP %u.%u.%u.%u",
222 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
223 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
224 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
225 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers),
226 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
227 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
228 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
229 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
231 if (!t4_get_exprom_version(adapter, &exprom_vers))
232 snprintf(info->erom_version, sizeof(info->erom_version),
234 FW_HDR_FW_VER_MAJOR_G(exprom_vers),
235 FW_HDR_FW_VER_MINOR_G(exprom_vers),
236 FW_HDR_FW_VER_MICRO_G(exprom_vers),
237 FW_HDR_FW_VER_BUILD_G(exprom_vers));
240 static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
242 if (stringset == ETH_SS_STATS) {
243 memcpy(data, stats_strings, sizeof(stats_strings));
244 data += sizeof(stats_strings);
245 memcpy(data, adapter_stats_strings,
246 sizeof(adapter_stats_strings));
247 data += sizeof(adapter_stats_strings);
248 memcpy(data, channel_stats_strings,
249 sizeof(channel_stats_strings));
250 data += sizeof(channel_stats_strings);
251 memcpy(data, loopback_stats_strings,
252 sizeof(loopback_stats_strings));
256 /* port stats maintained per queue of the port. They should be in the same
257 * order as in stats_strings above.
259 struct queue_port_stats {
269 struct adapter_stats {
276 u64 tcp_v4_retrans_segs;
280 u64 tcp_v6_retrans_segs;
292 struct channel_stats {
308 static void collect_sge_port_stats(const struct adapter *adap,
309 const struct port_info *p,
310 struct queue_port_stats *s)
313 const struct sge_eth_txq *tx = &adap->sge.ethtxq[p->first_qset];
314 const struct sge_eth_rxq *rx = &adap->sge.ethrxq[p->first_qset];
316 memset(s, 0, sizeof(*s));
317 for (i = 0; i < p->nqsets; i++, rx++, tx++) {
319 s->tx_csum += tx->tx_cso;
320 s->rx_csum += rx->stats.rx_cso;
321 s->vlan_ex += rx->stats.vlan_ex;
322 s->vlan_ins += tx->vlan_ins;
323 s->gro_pkts += rx->stats.lro_pkts;
324 s->gro_merged += rx->stats.lro_merged;
328 static void collect_adapter_stats(struct adapter *adap, struct adapter_stats *s)
330 struct tp_tcp_stats v4, v6;
331 struct tp_rdma_stats rdma_stats;
332 struct tp_err_stats err_stats;
333 struct tp_usm_stats usm_stats;
336 memset(s, 0, sizeof(*s));
338 spin_lock(&adap->stats_lock);
339 t4_tp_get_tcp_stats(adap, &v4, &v6, false);
340 t4_tp_get_rdma_stats(adap, &rdma_stats, false);
341 t4_get_usm_stats(adap, &usm_stats, false);
342 t4_tp_get_err_stats(adap, &err_stats, false);
343 spin_unlock(&adap->stats_lock);
345 s->db_drop = adap->db_stats.db_drop;
346 s->db_full = adap->db_stats.db_full;
347 s->db_empty = adap->db_stats.db_empty;
349 s->tcp_v4_out_rsts = v4.tcp_out_rsts;
350 s->tcp_v4_in_segs = v4.tcp_in_segs;
351 s->tcp_v4_out_segs = v4.tcp_out_segs;
352 s->tcp_v4_retrans_segs = v4.tcp_retrans_segs;
353 s->tcp_v6_out_rsts = v6.tcp_out_rsts;
354 s->tcp_v6_in_segs = v6.tcp_in_segs;
355 s->tcp_v6_out_segs = v6.tcp_out_segs;
356 s->tcp_v6_retrans_segs = v6.tcp_retrans_segs;
358 if (is_offload(adap)) {
359 s->frames = usm_stats.frames;
360 s->octets = usm_stats.octets;
361 s->drops = usm_stats.drops;
362 s->rqe_dfr_mod = rdma_stats.rqe_dfr_mod;
363 s->rqe_dfr_pkt = rdma_stats.rqe_dfr_pkt;
366 s->ofld_no_neigh = err_stats.ofld_no_neigh;
367 s->ofld_cong_defer = err_stats.ofld_cong_defer;
369 if (!is_t4(adap->params.chip)) {
372 v = t4_read_reg(adap, SGE_STAT_CFG_A);
373 if (STATSOURCE_T5_G(v) == 7) {
374 val2 = t4_read_reg(adap, SGE_STAT_MATCH_A);
375 val1 = t4_read_reg(adap, SGE_STAT_TOTAL_A);
376 s->wc_success = val1 - val2;
382 static void collect_channel_stats(struct adapter *adap, struct channel_stats *s,
385 struct tp_cpl_stats cpl_stats;
386 struct tp_err_stats err_stats;
387 struct tp_fcoe_stats fcoe_stats;
389 memset(s, 0, sizeof(*s));
391 spin_lock(&adap->stats_lock);
392 t4_tp_get_cpl_stats(adap, &cpl_stats, false);
393 t4_tp_get_err_stats(adap, &err_stats, false);
394 t4_get_fcoe_stats(adap, i, &fcoe_stats, false);
395 spin_unlock(&adap->stats_lock);
397 s->cpl_req = cpl_stats.req[i];
398 s->cpl_rsp = cpl_stats.rsp[i];
399 s->mac_in_errs = err_stats.mac_in_errs[i];
400 s->hdr_in_errs = err_stats.hdr_in_errs[i];
401 s->tcp_in_errs = err_stats.tcp_in_errs[i];
402 s->tcp6_in_errs = err_stats.tcp6_in_errs[i];
403 s->tnl_cong_drops = err_stats.tnl_cong_drops[i];
404 s->tnl_tx_drops = err_stats.tnl_tx_drops[i];
405 s->ofld_vlan_drops = err_stats.ofld_vlan_drops[i];
406 s->ofld_chan_drops = err_stats.ofld_chan_drops[i];
407 s->octets_ddp = fcoe_stats.octets_ddp;
408 s->frames_ddp = fcoe_stats.frames_ddp;
409 s->frames_drop = fcoe_stats.frames_drop;
412 static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
415 struct port_info *pi = netdev_priv(dev);
416 struct adapter *adapter = pi->adapter;
417 struct lb_port_stats s;
421 t4_get_port_stats_offset(adapter, pi->tx_chan,
422 (struct port_stats *)data,
425 data += sizeof(struct port_stats) / sizeof(u64);
426 collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
427 data += sizeof(struct queue_port_stats) / sizeof(u64);
428 collect_adapter_stats(adapter, (struct adapter_stats *)data);
429 data += sizeof(struct adapter_stats) / sizeof(u64);
431 *data++ = (u64)pi->port_id;
432 collect_channel_stats(adapter, (struct channel_stats *)data,
434 data += sizeof(struct channel_stats) / sizeof(u64);
436 *data++ = (u64)pi->port_id;
437 memset(&s, 0, sizeof(s));
438 t4_get_lb_stats(adapter, pi->port_id, &s);
441 for (i = 0; i < ARRAY_SIZE(loopback_stats_strings) - 1; i++)
442 *data++ = (unsigned long long)*p0++;
445 static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
448 struct adapter *adap = netdev2adap(dev);
451 buf_size = t4_get_regs_len(adap);
452 regs->version = mk_adap_vers(adap);
453 t4_get_regs(adap, buf, buf_size);
456 static int restart_autoneg(struct net_device *dev)
458 struct port_info *p = netdev_priv(dev);
460 if (!netif_running(dev))
462 if (p->link_cfg.autoneg != AUTONEG_ENABLE)
464 t4_restart_aneg(p->adapter, p->adapter->pf, p->tx_chan);
468 static int identify_port(struct net_device *dev,
469 enum ethtool_phys_id_state state)
472 struct adapter *adap = netdev2adap(dev);
474 if (state == ETHTOOL_ID_ACTIVE)
476 else if (state == ETHTOOL_ID_INACTIVE)
481 return t4_identify_port(adap, adap->pf, netdev2pinfo(dev)->viid, val);
485 * from_fw_port_mod_type - translate Firmware Port/Module type to Ethtool
486 * @port_type: Firmware Port Type
487 * @mod_type: Firmware Module Type
489 * Translate Firmware Port/Module type to Ethtool Port Type.
491 static int from_fw_port_mod_type(enum fw_port_type port_type,
492 enum fw_port_module_type mod_type)
494 if (port_type == FW_PORT_TYPE_BT_SGMII ||
495 port_type == FW_PORT_TYPE_BT_XFI ||
496 port_type == FW_PORT_TYPE_BT_XAUI) {
498 } else if (port_type == FW_PORT_TYPE_FIBER_XFI ||
499 port_type == FW_PORT_TYPE_FIBER_XAUI) {
501 } else if (port_type == FW_PORT_TYPE_SFP ||
502 port_type == FW_PORT_TYPE_QSFP_10G ||
503 port_type == FW_PORT_TYPE_QSA ||
504 port_type == FW_PORT_TYPE_QSFP ||
505 port_type == FW_PORT_TYPE_CR4_QSFP ||
506 port_type == FW_PORT_TYPE_CR_QSFP ||
507 port_type == FW_PORT_TYPE_CR2_QSFP ||
508 port_type == FW_PORT_TYPE_SFP28) {
509 if (mod_type == FW_PORT_MOD_TYPE_LR ||
510 mod_type == FW_PORT_MOD_TYPE_SR ||
511 mod_type == FW_PORT_MOD_TYPE_ER ||
512 mod_type == FW_PORT_MOD_TYPE_LRM)
514 else if (mod_type == FW_PORT_MOD_TYPE_TWINAX_PASSIVE ||
515 mod_type == FW_PORT_MOD_TYPE_TWINAX_ACTIVE)
519 } else if (port_type == FW_PORT_TYPE_KR4_100G ||
520 port_type == FW_PORT_TYPE_KR_SFP28) {
528 * speed_to_fw_caps - translate Port Speed to Firmware Port Capabilities
529 * @speed: speed in Kb/s
531 * Translates a specific Port Speed into a Firmware Port Capabilities
534 static unsigned int speed_to_fw_caps(int speed)
537 return FW_PORT_CAP32_SPEED_100M;
539 return FW_PORT_CAP32_SPEED_1G;
541 return FW_PORT_CAP32_SPEED_10G;
543 return FW_PORT_CAP32_SPEED_25G;
545 return FW_PORT_CAP32_SPEED_40G;
547 return FW_PORT_CAP32_SPEED_50G;
549 return FW_PORT_CAP32_SPEED_100G;
551 return FW_PORT_CAP32_SPEED_200G;
553 return FW_PORT_CAP32_SPEED_400G;
558 * fw_caps_to_lmm - translate Firmware to ethtool Link Mode Mask
559 * @port_type: Firmware Port Type
560 * @fw_caps: Firmware Port Capabilities
561 * @link_mode_mask: ethtool Link Mode Mask
563 * Translate a Firmware Port Capabilities specification to an ethtool
566 static void fw_caps_to_lmm(enum fw_port_type port_type,
567 unsigned int fw_caps,
568 unsigned long *link_mode_mask)
570 #define SET_LMM(__lmm_name) \
571 __set_bit(ETHTOOL_LINK_MODE_ ## __lmm_name ## _BIT, \
574 #define FW_CAPS_TO_LMM(__fw_name, __lmm_name) \
576 if (fw_caps & FW_PORT_CAP32_ ## __fw_name) \
577 SET_LMM(__lmm_name); \
581 case FW_PORT_TYPE_BT_SGMII:
582 case FW_PORT_TYPE_BT_XFI:
583 case FW_PORT_TYPE_BT_XAUI:
585 FW_CAPS_TO_LMM(SPEED_100M, 100baseT_Full);
586 FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
587 FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
590 case FW_PORT_TYPE_KX4:
591 case FW_PORT_TYPE_KX:
593 FW_CAPS_TO_LMM(SPEED_1G, 1000baseKX_Full);
594 FW_CAPS_TO_LMM(SPEED_10G, 10000baseKX4_Full);
597 case FW_PORT_TYPE_KR:
599 SET_LMM(10000baseKR_Full);
602 case FW_PORT_TYPE_BP_AP:
604 SET_LMM(10000baseR_FEC);
605 SET_LMM(10000baseKR_Full);
606 SET_LMM(1000baseKX_Full);
609 case FW_PORT_TYPE_BP4_AP:
611 SET_LMM(10000baseR_FEC);
612 SET_LMM(10000baseKR_Full);
613 SET_LMM(1000baseKX_Full);
614 SET_LMM(10000baseKX4_Full);
617 case FW_PORT_TYPE_FIBER_XFI:
618 case FW_PORT_TYPE_FIBER_XAUI:
619 case FW_PORT_TYPE_SFP:
620 case FW_PORT_TYPE_QSFP_10G:
621 case FW_PORT_TYPE_QSA:
623 FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
624 FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
627 case FW_PORT_TYPE_BP40_BA:
628 case FW_PORT_TYPE_QSFP:
630 SET_LMM(40000baseSR4_Full);
633 case FW_PORT_TYPE_CR_QSFP:
634 case FW_PORT_TYPE_SFP28:
636 FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
637 FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
638 FW_CAPS_TO_LMM(SPEED_25G, 25000baseCR_Full);
641 case FW_PORT_TYPE_KR_SFP28:
643 FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
644 FW_CAPS_TO_LMM(SPEED_10G, 10000baseKR_Full);
645 FW_CAPS_TO_LMM(SPEED_25G, 25000baseKR_Full);
648 case FW_PORT_TYPE_CR2_QSFP:
650 SET_LMM(50000baseSR2_Full);
653 case FW_PORT_TYPE_KR4_100G:
654 case FW_PORT_TYPE_CR4_QSFP:
656 FW_CAPS_TO_LMM(SPEED_40G, 40000baseSR4_Full);
657 FW_CAPS_TO_LMM(SPEED_25G, 25000baseCR_Full);
658 FW_CAPS_TO_LMM(SPEED_50G, 50000baseCR2_Full);
659 FW_CAPS_TO_LMM(SPEED_100G, 100000baseCR4_Full);
666 FW_CAPS_TO_LMM(ANEG, Autoneg);
667 FW_CAPS_TO_LMM(802_3_PAUSE, Pause);
668 FW_CAPS_TO_LMM(802_3_ASM_DIR, Asym_Pause);
670 #undef FW_CAPS_TO_LMM
675 * lmm_to_fw_caps - translate ethtool Link Mode Mask to Firmware
677 * @et_lmm: ethtool Link Mode Mask
679 * Translate ethtool Link Mode Mask into a Firmware Port capabilities
682 static unsigned int lmm_to_fw_caps(const unsigned long *link_mode_mask)
684 unsigned int fw_caps = 0;
686 #define LMM_TO_FW_CAPS(__lmm_name, __fw_name) \
688 if (test_bit(ETHTOOL_LINK_MODE_ ## __lmm_name ## _BIT, \
690 fw_caps |= FW_PORT_CAP32_ ## __fw_name; \
693 LMM_TO_FW_CAPS(100baseT_Full, SPEED_100M);
694 LMM_TO_FW_CAPS(1000baseT_Full, SPEED_1G);
695 LMM_TO_FW_CAPS(10000baseT_Full, SPEED_10G);
696 LMM_TO_FW_CAPS(40000baseSR4_Full, SPEED_40G);
697 LMM_TO_FW_CAPS(25000baseCR_Full, SPEED_25G);
698 LMM_TO_FW_CAPS(50000baseCR2_Full, SPEED_50G);
699 LMM_TO_FW_CAPS(100000baseCR4_Full, SPEED_100G);
701 #undef LMM_TO_FW_CAPS
706 static int get_link_ksettings(struct net_device *dev,
707 struct ethtool_link_ksettings *link_ksettings)
709 struct port_info *pi = netdev_priv(dev);
710 struct ethtool_link_settings *base = &link_ksettings->base;
712 /* For the nonce, the Firmware doesn't send up Port State changes
713 * when the Virtual Interface attached to the Port is down. So
714 * if it's down, let's grab any changes.
716 if (!netif_running(dev))
717 (void)t4_update_port_info(pi);
719 ethtool_link_ksettings_zero_link_mode(link_ksettings, supported);
720 ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
721 ethtool_link_ksettings_zero_link_mode(link_ksettings, lp_advertising);
723 base->port = from_fw_port_mod_type(pi->port_type, pi->mod_type);
725 if (pi->mdio_addr >= 0) {
726 base->phy_address = pi->mdio_addr;
727 base->mdio_support = (pi->port_type == FW_PORT_TYPE_BT_SGMII
728 ? ETH_MDIO_SUPPORTS_C22
729 : ETH_MDIO_SUPPORTS_C45);
731 base->phy_address = 255;
732 base->mdio_support = 0;
735 fw_caps_to_lmm(pi->port_type, pi->link_cfg.pcaps,
736 link_ksettings->link_modes.supported);
737 fw_caps_to_lmm(pi->port_type, pi->link_cfg.acaps,
738 link_ksettings->link_modes.advertising);
739 fw_caps_to_lmm(pi->port_type, pi->link_cfg.lpacaps,
740 link_ksettings->link_modes.lp_advertising);
742 if (netif_carrier_ok(dev)) {
743 base->speed = pi->link_cfg.speed;
744 base->duplex = DUPLEX_FULL;
746 base->speed = SPEED_UNKNOWN;
747 base->duplex = DUPLEX_UNKNOWN;
750 if (pi->link_cfg.fc & PAUSE_RX) {
751 if (pi->link_cfg.fc & PAUSE_TX) {
752 ethtool_link_ksettings_add_link_mode(link_ksettings,
756 ethtool_link_ksettings_add_link_mode(link_ksettings,
760 } else if (pi->link_cfg.fc & PAUSE_TX) {
761 ethtool_link_ksettings_add_link_mode(link_ksettings,
766 base->autoneg = pi->link_cfg.autoneg;
767 if (pi->link_cfg.pcaps & FW_PORT_CAP32_ANEG)
768 ethtool_link_ksettings_add_link_mode(link_ksettings,
770 if (pi->link_cfg.autoneg)
771 ethtool_link_ksettings_add_link_mode(link_ksettings,
772 advertising, Autoneg);
777 static int set_link_ksettings(struct net_device *dev,
778 const struct ethtool_link_ksettings *link_ksettings)
780 struct port_info *pi = netdev_priv(dev);
781 struct link_config *lc = &pi->link_cfg;
782 const struct ethtool_link_settings *base = &link_ksettings->base;
783 struct link_config old_lc;
784 unsigned int fw_caps;
787 /* only full-duplex supported */
788 if (base->duplex != DUPLEX_FULL)
791 if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
792 /* PHY offers a single speed. See if that's what's
795 if (base->autoneg == AUTONEG_DISABLE &&
796 (lc->pcaps & speed_to_fw_caps(base->speed)))
802 if (base->autoneg == AUTONEG_DISABLE) {
803 fw_caps = speed_to_fw_caps(base->speed);
805 if (!(lc->pcaps & fw_caps))
807 lc->speed_caps = fw_caps;
811 lmm_to_fw_caps(link_ksettings->link_modes.advertising);
812 if (!(lc->pcaps & fw_caps))
815 lc->acaps = fw_caps | FW_PORT_CAP32_ANEG;
817 lc->autoneg = base->autoneg;
819 /* If the firmware rejects the Link Configuration request, back out
820 * the changes and report the error.
822 ret = t4_link_l1cfg(pi->adapter, pi->adapter->mbox, pi->tx_chan, lc);
829 /* Translate the Firmware FEC value into the ethtool value. */
830 static inline unsigned int fwcap_to_eth_fec(unsigned int fw_fec)
832 unsigned int eth_fec = 0;
834 if (fw_fec & FW_PORT_CAP32_FEC_RS)
835 eth_fec |= ETHTOOL_FEC_RS;
836 if (fw_fec & FW_PORT_CAP32_FEC_BASER_RS)
837 eth_fec |= ETHTOOL_FEC_BASER;
839 /* if nothing is set, then FEC is off */
841 eth_fec = ETHTOOL_FEC_OFF;
846 /* Translate Common Code FEC value into ethtool value. */
847 static inline unsigned int cc_to_eth_fec(unsigned int cc_fec)
849 unsigned int eth_fec = 0;
851 if (cc_fec & FEC_AUTO)
852 eth_fec |= ETHTOOL_FEC_AUTO;
854 eth_fec |= ETHTOOL_FEC_RS;
855 if (cc_fec & FEC_BASER_RS)
856 eth_fec |= ETHTOOL_FEC_BASER;
858 /* if nothing is set, then FEC is off */
860 eth_fec = ETHTOOL_FEC_OFF;
865 /* Translate ethtool FEC value into Common Code value. */
866 static inline unsigned int eth_to_cc_fec(unsigned int eth_fec)
868 unsigned int cc_fec = 0;
870 if (eth_fec & ETHTOOL_FEC_OFF)
873 if (eth_fec & ETHTOOL_FEC_AUTO)
875 if (eth_fec & ETHTOOL_FEC_RS)
877 if (eth_fec & ETHTOOL_FEC_BASER)
878 cc_fec |= FEC_BASER_RS;
883 static int get_fecparam(struct net_device *dev, struct ethtool_fecparam *fec)
885 const struct port_info *pi = netdev_priv(dev);
886 const struct link_config *lc = &pi->link_cfg;
888 /* Translate the Firmware FEC Support into the ethtool value. We
889 * always support IEEE 802.3 "automatic" selection of Link FEC type if
890 * any FEC is supported.
892 fec->fec = fwcap_to_eth_fec(lc->pcaps);
893 if (fec->fec != ETHTOOL_FEC_OFF)
894 fec->fec |= ETHTOOL_FEC_AUTO;
896 /* Translate the current internal FEC parameters into the
899 fec->active_fec = cc_to_eth_fec(lc->fec);
904 static int set_fecparam(struct net_device *dev, struct ethtool_fecparam *fec)
906 struct port_info *pi = netdev_priv(dev);
907 struct link_config *lc = &pi->link_cfg;
908 struct link_config old_lc;
911 /* Save old Link Configuration in case the L1 Configure below
916 /* Try to perform the L1 Configure and return the result of that
917 * effort. If it fails, revert the attempted change.
919 lc->requested_fec = eth_to_cc_fec(fec->fec);
920 ret = t4_link_l1cfg(pi->adapter, pi->adapter->mbox,
927 static void get_pauseparam(struct net_device *dev,
928 struct ethtool_pauseparam *epause)
930 struct port_info *p = netdev_priv(dev);
932 epause->autoneg = (p->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
933 epause->rx_pause = (p->link_cfg.fc & PAUSE_RX) != 0;
934 epause->tx_pause = (p->link_cfg.fc & PAUSE_TX) != 0;
937 static int set_pauseparam(struct net_device *dev,
938 struct ethtool_pauseparam *epause)
940 struct port_info *p = netdev_priv(dev);
941 struct link_config *lc = &p->link_cfg;
943 if (epause->autoneg == AUTONEG_DISABLE)
944 lc->requested_fc = 0;
945 else if (lc->pcaps & FW_PORT_CAP32_ANEG)
946 lc->requested_fc = PAUSE_AUTONEG;
950 if (epause->rx_pause)
951 lc->requested_fc |= PAUSE_RX;
952 if (epause->tx_pause)
953 lc->requested_fc |= PAUSE_TX;
954 if (netif_running(dev))
955 return t4_link_l1cfg(p->adapter, p->adapter->mbox, p->tx_chan,
960 static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
962 const struct port_info *pi = netdev_priv(dev);
963 const struct sge *s = &pi->adapter->sge;
965 e->rx_max_pending = MAX_RX_BUFFERS;
966 e->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
967 e->rx_jumbo_max_pending = 0;
968 e->tx_max_pending = MAX_TXQ_ENTRIES;
970 e->rx_pending = s->ethrxq[pi->first_qset].fl.size - 8;
971 e->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
972 e->rx_jumbo_pending = 0;
973 e->tx_pending = s->ethtxq[pi->first_qset].q.size;
976 static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
979 const struct port_info *pi = netdev_priv(dev);
980 struct adapter *adapter = pi->adapter;
981 struct sge *s = &adapter->sge;
983 if (e->rx_pending > MAX_RX_BUFFERS || e->rx_jumbo_pending ||
984 e->tx_pending > MAX_TXQ_ENTRIES ||
985 e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
986 e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
987 e->rx_pending < MIN_FL_ENTRIES || e->tx_pending < MIN_TXQ_ENTRIES)
990 if (adapter->flags & FULL_INIT_DONE)
993 for (i = 0; i < pi->nqsets; ++i) {
994 s->ethtxq[pi->first_qset + i].q.size = e->tx_pending;
995 s->ethrxq[pi->first_qset + i].fl.size = e->rx_pending + 8;
996 s->ethrxq[pi->first_qset + i].rspq.size = e->rx_mini_pending;
1002 * set_rx_intr_params - set a net devices's RX interrupt holdoff paramete!
1003 * @dev: the network device
1004 * @us: the hold-off time in us, or 0 to disable timer
1005 * @cnt: the hold-off packet count, or 0 to disable counter
1007 * Set the RX interrupt hold-off parameters for a network device.
1009 static int set_rx_intr_params(struct net_device *dev,
1010 unsigned int us, unsigned int cnt)
1013 struct port_info *pi = netdev_priv(dev);
1014 struct adapter *adap = pi->adapter;
1015 struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
1017 for (i = 0; i < pi->nqsets; i++, q++) {
1018 err = cxgb4_set_rspq_intr_params(&q->rspq, us, cnt);
1025 static int set_adaptive_rx_setting(struct net_device *dev, int adaptive_rx)
1028 struct port_info *pi = netdev_priv(dev);
1029 struct adapter *adap = pi->adapter;
1030 struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
1032 for (i = 0; i < pi->nqsets; i++, q++)
1033 q->rspq.adaptive_rx = adaptive_rx;
1038 static int get_adaptive_rx_setting(struct net_device *dev)
1040 struct port_info *pi = netdev_priv(dev);
1041 struct adapter *adap = pi->adapter;
1042 struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
1044 return q->rspq.adaptive_rx;
1047 static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
1049 set_adaptive_rx_setting(dev, c->use_adaptive_rx_coalesce);
1050 return set_rx_intr_params(dev, c->rx_coalesce_usecs,
1051 c->rx_max_coalesced_frames);
1054 static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
1056 const struct port_info *pi = netdev_priv(dev);
1057 const struct adapter *adap = pi->adapter;
1058 const struct sge_rspq *rq = &adap->sge.ethrxq[pi->first_qset].rspq;
1060 c->rx_coalesce_usecs = qtimer_val(adap, rq);
1061 c->rx_max_coalesced_frames = (rq->intr_params & QINTR_CNT_EN_F) ?
1062 adap->sge.counter_val[rq->pktcnt_idx] : 0;
1063 c->use_adaptive_rx_coalesce = get_adaptive_rx_setting(dev);
1068 * eeprom_ptov - translate a physical EEPROM address to virtual
1069 * @phys_addr: the physical EEPROM address
1070 * @fn: the PCI function number
1071 * @sz: size of function-specific area
1073 * Translate a physical EEPROM address to virtual. The first 1K is
1074 * accessed through virtual addresses starting at 31K, the rest is
1075 * accessed through virtual addresses starting at 0.
1077 * The mapping is as follows:
1078 * [0..1K) -> [31K..32K)
1079 * [1K..1K+A) -> [31K-A..31K)
1080 * [1K+A..ES) -> [0..ES-A-1K)
1082 * where A = @fn * @sz, and ES = EEPROM size.
1084 static int eeprom_ptov(unsigned int phys_addr, unsigned int fn, unsigned int sz)
1087 if (phys_addr < 1024)
1088 return phys_addr + (31 << 10);
1089 if (phys_addr < 1024 + fn)
1090 return 31744 - fn + phys_addr - 1024;
1091 if (phys_addr < EEPROMSIZE)
1092 return phys_addr - 1024 - fn;
1096 /* The next two routines implement eeprom read/write from physical addresses.
1098 static int eeprom_rd_phys(struct adapter *adap, unsigned int phys_addr, u32 *v)
1100 int vaddr = eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
1103 vaddr = pci_read_vpd(adap->pdev, vaddr, sizeof(u32), v);
1104 return vaddr < 0 ? vaddr : 0;
1107 static int eeprom_wr_phys(struct adapter *adap, unsigned int phys_addr, u32 v)
1109 int vaddr = eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
1112 vaddr = pci_write_vpd(adap->pdev, vaddr, sizeof(u32), &v);
1113 return vaddr < 0 ? vaddr : 0;
1116 #define EEPROM_MAGIC 0x38E2F10C
1118 static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
1122 struct adapter *adapter = netdev2adap(dev);
1123 u8 *buf = kvzalloc(EEPROMSIZE, GFP_KERNEL);
1128 e->magic = EEPROM_MAGIC;
1129 for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
1130 err = eeprom_rd_phys(adapter, i, (u32 *)&buf[i]);
1133 memcpy(data, buf + e->offset, e->len);
1138 static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
1143 u32 aligned_offset, aligned_len, *p;
1144 struct adapter *adapter = netdev2adap(dev);
1146 if (eeprom->magic != EEPROM_MAGIC)
1149 aligned_offset = eeprom->offset & ~3;
1150 aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
1152 if (adapter->pf > 0) {
1153 u32 start = 1024 + adapter->pf * EEPROMPFSIZE;
1155 if (aligned_offset < start ||
1156 aligned_offset + aligned_len > start + EEPROMPFSIZE)
1160 if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
1161 /* RMW possibly needed for first or last words.
1163 buf = kvzalloc(aligned_len, GFP_KERNEL);
1166 err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
1167 if (!err && aligned_len > 4)
1168 err = eeprom_rd_phys(adapter,
1169 aligned_offset + aligned_len - 4,
1170 (u32 *)&buf[aligned_len - 4]);
1173 memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
1178 err = t4_seeprom_wp(adapter, false);
1182 for (p = (u32 *)buf; !err && aligned_len; aligned_len -= 4, p++) {
1183 err = eeprom_wr_phys(adapter, aligned_offset, *p);
1184 aligned_offset += 4;
1188 err = t4_seeprom_wp(adapter, true);
1195 static int set_flash(struct net_device *netdev, struct ethtool_flash *ef)
1198 const struct firmware *fw;
1199 struct adapter *adap = netdev2adap(netdev);
1200 unsigned int mbox = PCIE_FW_MASTER_M + 1;
1202 unsigned int master;
1205 pcie_fw = t4_read_reg(adap, PCIE_FW_A);
1206 master = PCIE_FW_MASTER_G(pcie_fw);
1207 if (pcie_fw & PCIE_FW_MASTER_VLD_F)
1209 /* if csiostor is the master return */
1210 if (master_vld && (master != adap->pf)) {
1211 dev_warn(adap->pdev_dev,
1212 "cxgb4 driver needs to be loaded as MASTER to support FW flash\n");
1216 ef->data[sizeof(ef->data) - 1] = '\0';
1217 ret = request_firmware(&fw, ef->data, adap->pdev_dev);
1221 /* If the adapter has been fully initialized then we'll go ahead and
1222 * try to get the firmware's cooperation in upgrading to the new
1223 * firmware image otherwise we'll try to do the entire job from the
1224 * host ... and we always "force" the operation in this path.
1226 if (adap->flags & FULL_INIT_DONE)
1229 ret = t4_fw_upgrade(adap, mbox, fw->data, fw->size, 1);
1230 release_firmware(fw);
1232 dev_info(adap->pdev_dev,
1233 "loaded firmware %s, reload cxgb4 driver\n", ef->data);
1237 static int get_ts_info(struct net_device *dev, struct ethtool_ts_info *ts_info)
1239 struct port_info *pi = netdev_priv(dev);
1240 struct adapter *adapter = pi->adapter;
1242 ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1243 SOF_TIMESTAMPING_RX_SOFTWARE |
1244 SOF_TIMESTAMPING_SOFTWARE;
1246 ts_info->so_timestamping |= SOF_TIMESTAMPING_RX_HARDWARE |
1247 SOF_TIMESTAMPING_TX_HARDWARE |
1248 SOF_TIMESTAMPING_RAW_HARDWARE;
1250 ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1251 (1 << HWTSTAMP_TX_ON);
1253 ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1254 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1255 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1256 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1257 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1258 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
1260 if (adapter->ptp_clock)
1261 ts_info->phc_index = ptp_clock_index(adapter->ptp_clock);
1263 ts_info->phc_index = -1;
1268 static u32 get_rss_table_size(struct net_device *dev)
1270 const struct port_info *pi = netdev_priv(dev);
1272 return pi->rss_size;
1275 static int get_rss_table(struct net_device *dev, u32 *p, u8 *key, u8 *hfunc)
1277 const struct port_info *pi = netdev_priv(dev);
1278 unsigned int n = pi->rss_size;
1281 *hfunc = ETH_RSS_HASH_TOP;
1289 static int set_rss_table(struct net_device *dev, const u32 *p, const u8 *key,
1293 struct port_info *pi = netdev_priv(dev);
1295 /* We require at least one supported parameter to be changed and no
1296 * change in any of the unsupported parameters
1299 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1304 /* Interface must be brought up atleast once */
1305 if (pi->adapter->flags & FULL_INIT_DONE) {
1306 for (i = 0; i < pi->rss_size; i++)
1309 return cxgb4_write_rss(pi, pi->rss);
1315 static int get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1318 const struct port_info *pi = netdev_priv(dev);
1320 switch (info->cmd) {
1321 case ETHTOOL_GRXFH: {
1322 unsigned int v = pi->rss_mode;
1325 switch (info->flow_type) {
1327 if (v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F)
1328 info->data = RXH_IP_SRC | RXH_IP_DST |
1329 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1330 else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1331 info->data = RXH_IP_SRC | RXH_IP_DST;
1334 if ((v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F) &&
1335 (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
1336 info->data = RXH_IP_SRC | RXH_IP_DST |
1337 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1338 else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1339 info->data = RXH_IP_SRC | RXH_IP_DST;
1342 case AH_ESP_V4_FLOW:
1344 if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1345 info->data = RXH_IP_SRC | RXH_IP_DST;
1348 if (v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F)
1349 info->data = RXH_IP_SRC | RXH_IP_DST |
1350 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1351 else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1352 info->data = RXH_IP_SRC | RXH_IP_DST;
1355 if ((v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F) &&
1356 (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
1357 info->data = RXH_IP_SRC | RXH_IP_DST |
1358 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1359 else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1360 info->data = RXH_IP_SRC | RXH_IP_DST;
1363 case AH_ESP_V6_FLOW:
1365 if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1366 info->data = RXH_IP_SRC | RXH_IP_DST;
1371 case ETHTOOL_GRXRINGS:
1372 info->data = pi->nqsets;
1378 static int set_dump(struct net_device *dev, struct ethtool_dump *eth_dump)
1380 struct adapter *adapter = netdev2adap(dev);
1383 len = sizeof(struct cudbg_hdr) +
1384 sizeof(struct cudbg_entity_hdr) * CUDBG_MAX_ENTITY;
1385 len += cxgb4_get_dump_length(adapter, eth_dump->flag);
1387 adapter->eth_dump.flag = eth_dump->flag;
1388 adapter->eth_dump.len = len;
1392 static int get_dump_flag(struct net_device *dev, struct ethtool_dump *eth_dump)
1394 struct adapter *adapter = netdev2adap(dev);
1396 eth_dump->flag = adapter->eth_dump.flag;
1397 eth_dump->len = adapter->eth_dump.len;
1398 eth_dump->version = adapter->eth_dump.version;
1402 static int get_dump_data(struct net_device *dev, struct ethtool_dump *eth_dump,
1405 struct adapter *adapter = netdev2adap(dev);
1409 if (adapter->eth_dump.flag == CXGB4_ETH_DUMP_NONE)
1412 len = sizeof(struct cudbg_hdr) +
1413 sizeof(struct cudbg_entity_hdr) * CUDBG_MAX_ENTITY;
1414 len += cxgb4_get_dump_length(adapter, adapter->eth_dump.flag);
1415 if (eth_dump->len < len)
1418 ret = cxgb4_cudbg_collect(adapter, buf, &len, adapter->eth_dump.flag);
1422 eth_dump->flag = adapter->eth_dump.flag;
1423 eth_dump->len = len;
1424 eth_dump->version = adapter->eth_dump.version;
1428 static const struct ethtool_ops cxgb_ethtool_ops = {
1429 .get_link_ksettings = get_link_ksettings,
1430 .set_link_ksettings = set_link_ksettings,
1431 .get_fecparam = get_fecparam,
1432 .set_fecparam = set_fecparam,
1433 .get_drvinfo = get_drvinfo,
1434 .get_msglevel = get_msglevel,
1435 .set_msglevel = set_msglevel,
1436 .get_ringparam = get_sge_param,
1437 .set_ringparam = set_sge_param,
1438 .get_coalesce = get_coalesce,
1439 .set_coalesce = set_coalesce,
1440 .get_eeprom_len = get_eeprom_len,
1441 .get_eeprom = get_eeprom,
1442 .set_eeprom = set_eeprom,
1443 .get_pauseparam = get_pauseparam,
1444 .set_pauseparam = set_pauseparam,
1445 .get_link = ethtool_op_get_link,
1446 .get_strings = get_strings,
1447 .set_phys_id = identify_port,
1448 .nway_reset = restart_autoneg,
1449 .get_sset_count = get_sset_count,
1450 .get_ethtool_stats = get_stats,
1451 .get_regs_len = get_regs_len,
1452 .get_regs = get_regs,
1453 .get_rxnfc = get_rxnfc,
1454 .get_rxfh_indir_size = get_rss_table_size,
1455 .get_rxfh = get_rss_table,
1456 .set_rxfh = set_rss_table,
1457 .flash_device = set_flash,
1458 .get_ts_info = get_ts_info,
1459 .set_dump = set_dump,
1460 .get_dump_flag = get_dump_flag,
1461 .get_dump_data = get_dump_data,
1464 void cxgb4_set_ethtool_ops(struct net_device *netdev)
1466 netdev->ethtool_ops = &cxgb_ethtool_ops;