1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
4 #include <linux/module.h>
5 #include <linux/netdevice.h>
10 #include "ionic_ethtool.h"
11 #include "ionic_stats.h"
13 static const char ionic_priv_flags_strings[][ETH_GSTRING_LEN] = {
14 #define PRIV_F_SW_DBG_STATS BIT(0)
17 #define PRIV_FLAGS_COUNT ARRAY_SIZE(ionic_priv_flags_strings)
19 static void ionic_get_stats_strings(struct ionic_lif *lif, u8 *buf)
23 for (i = 0; i < ionic_num_stats_grps; i++)
24 ionic_stats_groups[i].get_strings(lif, &buf);
27 static void ionic_get_stats(struct net_device *netdev,
28 struct ethtool_stats *stats, u64 *buf)
30 struct ionic_lif *lif;
33 lif = netdev_priv(netdev);
35 memset(buf, 0, stats->n_stats * sizeof(*buf));
36 for (i = 0; i < ionic_num_stats_grps; i++)
37 ionic_stats_groups[i].get_values(lif, &buf);
40 static int ionic_get_stats_count(struct ionic_lif *lif)
44 for (i = 0; i < ionic_num_stats_grps; i++)
45 num_stats += ionic_stats_groups[i].get_count(lif);
50 static int ionic_get_sset_count(struct net_device *netdev, int sset)
52 struct ionic_lif *lif = netdev_priv(netdev);
57 count = ionic_get_stats_count(lif);
59 case ETH_SS_PRIV_FLAGS:
60 count = PRIV_FLAGS_COUNT;
66 static void ionic_get_strings(struct net_device *netdev,
69 struct ionic_lif *lif = netdev_priv(netdev);
73 ionic_get_stats_strings(lif, buf);
75 case ETH_SS_PRIV_FLAGS:
76 memcpy(buf, ionic_priv_flags_strings,
77 PRIV_FLAGS_COUNT * ETH_GSTRING_LEN);
82 static void ionic_get_drvinfo(struct net_device *netdev,
83 struct ethtool_drvinfo *drvinfo)
85 struct ionic_lif *lif = netdev_priv(netdev);
86 struct ionic *ionic = lif->ionic;
88 strlcpy(drvinfo->driver, IONIC_DRV_NAME, sizeof(drvinfo->driver));
89 strlcpy(drvinfo->version, IONIC_DRV_VERSION, sizeof(drvinfo->version));
90 strlcpy(drvinfo->fw_version, ionic->idev.dev_info.fw_version,
91 sizeof(drvinfo->fw_version));
92 strlcpy(drvinfo->bus_info, ionic_bus_info(ionic),
93 sizeof(drvinfo->bus_info));
96 static int ionic_get_regs_len(struct net_device *netdev)
98 return (IONIC_DEV_INFO_REG_COUNT + IONIC_DEV_CMD_REG_COUNT) * sizeof(u32);
101 static void ionic_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
104 struct ionic_lif *lif = netdev_priv(netdev);
107 regs->version = IONIC_DEV_CMD_REG_VERSION;
109 size = IONIC_DEV_INFO_REG_COUNT * sizeof(u32);
110 memcpy_fromio(p, lif->ionic->idev.dev_info_regs->words, size);
112 size = IONIC_DEV_CMD_REG_COUNT * sizeof(u32);
113 memcpy_fromio(p, lif->ionic->idev.dev_cmd_regs->words, size);
116 static int ionic_get_link_ksettings(struct net_device *netdev,
117 struct ethtool_link_ksettings *ks)
119 struct ionic_lif *lif = netdev_priv(netdev);
120 struct ionic_dev *idev = &lif->ionic->idev;
123 ethtool_link_ksettings_zero_link_mode(ks, supported);
125 /* The port_info data is found in a DMA space that the NIC keeps
126 * up-to-date, so there's no need to request the data from the
127 * NIC, we already have it in our memory space.
130 switch (le16_to_cpu(idev->port_info->status.xcvr.pid)) {
132 case IONIC_XCVR_PID_QSFP_100G_CR4:
133 ethtool_link_ksettings_add_link_mode(ks, supported,
137 case IONIC_XCVR_PID_QSFP_40GBASE_CR4:
138 ethtool_link_ksettings_add_link_mode(ks, supported,
142 case IONIC_XCVR_PID_SFP_25GBASE_CR_S:
143 case IONIC_XCVR_PID_SFP_25GBASE_CR_L:
144 case IONIC_XCVR_PID_SFP_25GBASE_CR_N:
145 ethtool_link_ksettings_add_link_mode(ks, supported,
149 case IONIC_XCVR_PID_SFP_10GBASE_AOC:
150 case IONIC_XCVR_PID_SFP_10GBASE_CU:
151 ethtool_link_ksettings_add_link_mode(ks, supported,
157 case IONIC_XCVR_PID_QSFP_100G_SR4:
158 case IONIC_XCVR_PID_QSFP_100G_AOC:
159 ethtool_link_ksettings_add_link_mode(ks, supported,
162 case IONIC_XCVR_PID_QSFP_100G_LR4:
163 ethtool_link_ksettings_add_link_mode(ks, supported,
164 100000baseLR4_ER4_Full);
166 case IONIC_XCVR_PID_QSFP_100G_ER4:
167 ethtool_link_ksettings_add_link_mode(ks, supported,
168 100000baseLR4_ER4_Full);
170 case IONIC_XCVR_PID_QSFP_40GBASE_SR4:
171 case IONIC_XCVR_PID_QSFP_40GBASE_AOC:
172 ethtool_link_ksettings_add_link_mode(ks, supported,
175 case IONIC_XCVR_PID_QSFP_40GBASE_LR4:
176 ethtool_link_ksettings_add_link_mode(ks, supported,
179 case IONIC_XCVR_PID_SFP_25GBASE_SR:
180 case IONIC_XCVR_PID_SFP_25GBASE_AOC:
181 ethtool_link_ksettings_add_link_mode(ks, supported,
184 case IONIC_XCVR_PID_SFP_10GBASE_SR:
185 ethtool_link_ksettings_add_link_mode(ks, supported,
188 case IONIC_XCVR_PID_SFP_10GBASE_LR:
189 ethtool_link_ksettings_add_link_mode(ks, supported,
192 case IONIC_XCVR_PID_SFP_10GBASE_LRM:
193 ethtool_link_ksettings_add_link_mode(ks, supported,
196 case IONIC_XCVR_PID_SFP_10GBASE_ER:
197 ethtool_link_ksettings_add_link_mode(ks, supported,
200 case IONIC_XCVR_PID_UNKNOWN:
201 /* This means there's no module plugged in */
204 dev_info(lif->ionic->dev, "unknown xcvr type pid=%d / 0x%x\n",
205 idev->port_info->status.xcvr.pid,
206 idev->port_info->status.xcvr.pid);
210 bitmap_copy(ks->link_modes.advertising, ks->link_modes.supported,
211 __ETHTOOL_LINK_MODE_MASK_NBITS);
213 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
214 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
215 if (idev->port_info->config.fec_type == IONIC_PORT_FEC_TYPE_FC)
216 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_BASER);
217 else if (idev->port_info->config.fec_type == IONIC_PORT_FEC_TYPE_RS)
218 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
220 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
221 ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
223 if (idev->port_info->status.xcvr.phy == IONIC_PHY_TYPE_COPPER ||
225 ks->base.port = PORT_DA;
226 else if (idev->port_info->status.xcvr.phy == IONIC_PHY_TYPE_FIBER)
227 ks->base.port = PORT_FIBRE;
229 ks->base.port = PORT_NONE;
231 if (ks->base.port != PORT_NONE) {
232 ks->base.speed = le32_to_cpu(lif->info->status.link_speed);
234 if (le16_to_cpu(lif->info->status.link_status))
235 ks->base.duplex = DUPLEX_FULL;
237 ks->base.duplex = DUPLEX_UNKNOWN;
239 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
241 if (idev->port_info->config.an_enable) {
242 ethtool_link_ksettings_add_link_mode(ks, advertising,
244 ks->base.autoneg = AUTONEG_ENABLE;
251 static int ionic_set_link_ksettings(struct net_device *netdev,
252 const struct ethtool_link_ksettings *ks)
254 struct ionic_lif *lif = netdev_priv(netdev);
255 struct ionic *ionic = lif->ionic;
256 struct ionic_dev *idev;
261 idev = &lif->ionic->idev;
262 fec_type = IONIC_PORT_FEC_TYPE_NONE;
265 if (ks->base.autoneg != idev->port_info->config.an_enable) {
266 mutex_lock(&ionic->dev_cmd_lock);
267 ionic_dev_cmd_port_autoneg(idev, ks->base.autoneg);
268 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
269 mutex_unlock(&ionic->dev_cmd_lock);
275 if (ks->base.speed != le32_to_cpu(idev->port_info->config.speed)) {
276 mutex_lock(&ionic->dev_cmd_lock);
277 ionic_dev_cmd_port_speed(idev, ks->base.speed);
278 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
279 mutex_unlock(&ionic->dev_cmd_lock);
285 req_rs = ethtool_link_ksettings_test_link_mode(ks, advertising, FEC_RS);
286 req_fc = ethtool_link_ksettings_test_link_mode(ks, advertising, FEC_BASER);
287 if (req_rs && req_fc) {
288 netdev_info(netdev, "Only select one FEC mode at a time\n");
291 fec_type = IONIC_PORT_FEC_TYPE_FC;
293 fec_type = IONIC_PORT_FEC_TYPE_RS;
294 } else if (!(req_rs | req_fc)) {
295 fec_type = IONIC_PORT_FEC_TYPE_NONE;
298 if (fec_type != idev->port_info->config.fec_type) {
299 mutex_lock(&ionic->dev_cmd_lock);
300 ionic_dev_cmd_port_fec(idev, fec_type);
301 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
302 mutex_unlock(&ionic->dev_cmd_lock);
310 static void ionic_get_pauseparam(struct net_device *netdev,
311 struct ethtool_pauseparam *pause)
313 struct ionic_lif *lif = netdev_priv(netdev);
318 pause_type = lif->ionic->idev.port_info->config.pause_type;
320 pause->rx_pause = pause_type & IONIC_PAUSE_F_RX ? 1 : 0;
321 pause->tx_pause = pause_type & IONIC_PAUSE_F_TX ? 1 : 0;
325 static int ionic_set_pauseparam(struct net_device *netdev,
326 struct ethtool_pauseparam *pause)
328 struct ionic_lif *lif = netdev_priv(netdev);
329 struct ionic *ionic = lif->ionic;
336 /* change both at the same time */
337 requested_pause = IONIC_PORT_PAUSE_TYPE_LINK;
339 requested_pause |= IONIC_PAUSE_F_RX;
341 requested_pause |= IONIC_PAUSE_F_TX;
343 if (requested_pause == lif->ionic->idev.port_info->config.pause_type)
346 mutex_lock(&ionic->dev_cmd_lock);
347 ionic_dev_cmd_port_pause(&lif->ionic->idev, requested_pause);
348 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
349 mutex_unlock(&ionic->dev_cmd_lock);
356 static int ionic_get_coalesce(struct net_device *netdev,
357 struct ethtool_coalesce *coalesce)
359 struct ionic_lif *lif = netdev_priv(netdev);
361 /* Tx uses Rx interrupt */
362 coalesce->tx_coalesce_usecs = lif->rx_coalesce_usecs;
363 coalesce->rx_coalesce_usecs = lif->rx_coalesce_usecs;
368 static int ionic_set_coalesce(struct net_device *netdev,
369 struct ethtool_coalesce *coalesce)
371 struct ionic_lif *lif = netdev_priv(netdev);
372 struct ionic_identity *ident;
373 struct ionic_qcq *qcq;
378 if (coalesce->rx_max_coalesced_frames ||
379 coalesce->rx_coalesce_usecs_irq ||
380 coalesce->rx_max_coalesced_frames_irq ||
381 coalesce->tx_max_coalesced_frames ||
382 coalesce->tx_coalesce_usecs_irq ||
383 coalesce->tx_max_coalesced_frames_irq ||
384 coalesce->stats_block_coalesce_usecs ||
385 coalesce->use_adaptive_rx_coalesce ||
386 coalesce->use_adaptive_tx_coalesce ||
387 coalesce->pkt_rate_low ||
388 coalesce->rx_coalesce_usecs_low ||
389 coalesce->rx_max_coalesced_frames_low ||
390 coalesce->tx_coalesce_usecs_low ||
391 coalesce->tx_max_coalesced_frames_low ||
392 coalesce->pkt_rate_high ||
393 coalesce->rx_coalesce_usecs_high ||
394 coalesce->rx_max_coalesced_frames_high ||
395 coalesce->tx_coalesce_usecs_high ||
396 coalesce->tx_max_coalesced_frames_high ||
397 coalesce->rate_sample_interval)
400 ident = &lif->ionic->ident;
401 if (ident->dev.intr_coal_div == 0) {
402 netdev_warn(netdev, "bad HW value in dev.intr_coal_div = %d\n",
403 ident->dev.intr_coal_div);
407 /* Tx uses Rx interrupt, so only change Rx */
408 if (coalesce->tx_coalesce_usecs != lif->rx_coalesce_usecs) {
409 netdev_warn(netdev, "only the rx-usecs can be changed\n");
413 coal = ionic_coal_usec_to_hw(lif->ionic, coalesce->rx_coalesce_usecs);
415 if (coal > IONIC_INTR_CTRL_COAL_MAX)
418 /* If they asked for non-zero and it resolved to zero, bump it up */
419 if (!coal && coalesce->rx_coalesce_usecs)
422 /* Convert it back to get device resolution */
423 usecs = ionic_coal_hw_to_usec(lif->ionic, coal);
425 if (usecs != lif->rx_coalesce_usecs) {
426 lif->rx_coalesce_usecs = usecs;
428 if (test_bit(IONIC_LIF_UP, lif->state)) {
429 for (i = 0; i < lif->nxqs; i++) {
430 qcq = lif->rxqcqs[i].qcq;
431 ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
432 qcq->intr.index, coal);
440 static void ionic_get_ringparam(struct net_device *netdev,
441 struct ethtool_ringparam *ring)
443 struct ionic_lif *lif = netdev_priv(netdev);
445 ring->tx_max_pending = IONIC_MAX_TXRX_DESC;
446 ring->tx_pending = lif->ntxq_descs;
447 ring->rx_max_pending = IONIC_MAX_TXRX_DESC;
448 ring->rx_pending = lif->nrxq_descs;
451 static int ionic_set_ringparam(struct net_device *netdev,
452 struct ethtool_ringparam *ring)
454 struct ionic_lif *lif = netdev_priv(netdev);
457 if (ring->rx_mini_pending || ring->rx_jumbo_pending) {
458 netdev_info(netdev, "Changing jumbo or mini descriptors not supported\n");
462 if (!is_power_of_2(ring->tx_pending) ||
463 !is_power_of_2(ring->rx_pending)) {
464 netdev_info(netdev, "Descriptor count must be a power of 2\n");
468 /* if nothing to do return success */
469 if (ring->tx_pending == lif->ntxq_descs &&
470 ring->rx_pending == lif->nrxq_descs)
473 if (!ionic_wait_for_bit(lif, IONIC_LIF_QUEUE_RESET))
476 running = test_bit(IONIC_LIF_UP, lif->state);
480 lif->ntxq_descs = ring->tx_pending;
481 lif->nrxq_descs = ring->rx_pending;
485 clear_bit(IONIC_LIF_QUEUE_RESET, lif->state);
490 static void ionic_get_channels(struct net_device *netdev,
491 struct ethtool_channels *ch)
493 struct ionic_lif *lif = netdev_priv(netdev);
495 /* report maximum channels */
496 ch->max_combined = lif->ionic->ntxqs_per_lif;
498 /* report current channels */
499 ch->combined_count = lif->nxqs;
502 static int ionic_set_channels(struct net_device *netdev,
503 struct ethtool_channels *ch)
505 struct ionic_lif *lif = netdev_priv(netdev);
508 if (!ch->combined_count || ch->other_count ||
509 ch->rx_count || ch->tx_count)
512 if (ch->combined_count == lif->nxqs)
515 if (!ionic_wait_for_bit(lif, IONIC_LIF_QUEUE_RESET))
518 running = test_bit(IONIC_LIF_UP, lif->state);
522 lif->nxqs = ch->combined_count;
526 clear_bit(IONIC_LIF_QUEUE_RESET, lif->state);
531 static u32 ionic_get_priv_flags(struct net_device *netdev)
533 struct ionic_lif *lif = netdev_priv(netdev);
536 if (test_bit(IONIC_LIF_SW_DEBUG_STATS, lif->state))
537 priv_flags |= PRIV_F_SW_DBG_STATS;
542 static int ionic_set_priv_flags(struct net_device *netdev, u32 priv_flags)
544 struct ionic_lif *lif = netdev_priv(netdev);
545 u32 flags = lif->flags;
547 clear_bit(IONIC_LIF_SW_DEBUG_STATS, lif->state);
548 if (priv_flags & PRIV_F_SW_DBG_STATS)
549 set_bit(IONIC_LIF_SW_DEBUG_STATS, lif->state);
551 if (flags != lif->flags)
557 static int ionic_get_rxnfc(struct net_device *netdev,
558 struct ethtool_rxnfc *info, u32 *rules)
560 struct ionic_lif *lif = netdev_priv(netdev);
564 case ETHTOOL_GRXRINGS:
565 info->data = lif->nxqs;
568 netdev_err(netdev, "Command parameter %d is not supported\n",
576 static u32 ionic_get_rxfh_indir_size(struct net_device *netdev)
578 struct ionic_lif *lif = netdev_priv(netdev);
580 return le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
583 static u32 ionic_get_rxfh_key_size(struct net_device *netdev)
585 return IONIC_RSS_HASH_KEY_SIZE;
588 static int ionic_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
591 struct ionic_lif *lif = netdev_priv(netdev);
592 unsigned int i, tbl_sz;
595 tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
596 for (i = 0; i < tbl_sz; i++)
597 indir[i] = lif->rss_ind_tbl[i];
601 memcpy(key, lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
604 *hfunc = ETH_RSS_HASH_TOP;
609 static int ionic_set_rxfh(struct net_device *netdev, const u32 *indir,
610 const u8 *key, const u8 hfunc)
612 struct ionic_lif *lif = netdev_priv(netdev);
615 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
618 err = ionic_lif_rss_config(lif, lif->rss_types, key, indir);
625 static int ionic_set_tunable(struct net_device *dev,
626 const struct ethtool_tunable *tuna,
629 struct ionic_lif *lif = netdev_priv(dev);
632 case ETHTOOL_RX_COPYBREAK:
633 lif->rx_copybreak = *(u32 *)data;
642 static int ionic_get_tunable(struct net_device *netdev,
643 const struct ethtool_tunable *tuna, void *data)
645 struct ionic_lif *lif = netdev_priv(netdev);
648 case ETHTOOL_RX_COPYBREAK:
649 *(u32 *)data = lif->rx_copybreak;
658 static int ionic_get_module_info(struct net_device *netdev,
659 struct ethtool_modinfo *modinfo)
662 struct ionic_lif *lif = netdev_priv(netdev);
663 struct ionic_dev *idev = &lif->ionic->idev;
664 struct ionic_xcvr_status *xcvr;
666 xcvr = &idev->port_info->status.xcvr;
668 /* report the module data type and length */
669 switch (xcvr->sprom[0]) {
671 modinfo->type = ETH_MODULE_SFF_8079;
672 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
674 case 0x0D: /* QSFP */
675 case 0x11: /* QSFP28 */
676 modinfo->type = ETH_MODULE_SFF_8436;
677 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
680 netdev_info(netdev, "unknown xcvr type 0x%02x\n",
688 static int ionic_get_module_eeprom(struct net_device *netdev,
689 struct ethtool_eeprom *ee,
692 struct ionic_lif *lif = netdev_priv(netdev);
693 struct ionic_dev *idev = &lif->ionic->idev;
694 struct ionic_xcvr_status *xcvr;
695 char tbuf[sizeof(xcvr->sprom)];
699 /* The NIC keeps the module prom up-to-date in the DMA space
700 * so we can simply copy the module bytes into the data buffer.
702 xcvr = &idev->port_info->status.xcvr;
703 len = min_t(u32, sizeof(xcvr->sprom), ee->len);
706 memcpy(data, xcvr->sprom, len);
707 memcpy(tbuf, xcvr->sprom, len);
709 /* Let's make sure we got a consistent copy */
710 if (!memcmp(data, tbuf, len))
721 static int ionic_nway_reset(struct net_device *netdev)
723 struct ionic_lif *lif = netdev_priv(netdev);
724 struct ionic *ionic = lif->ionic;
727 /* flap the link to force auto-negotiation */
729 mutex_lock(&ionic->dev_cmd_lock);
731 ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_DOWN);
732 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
735 ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
736 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
739 mutex_unlock(&ionic->dev_cmd_lock);
744 static const struct ethtool_ops ionic_ethtool_ops = {
745 .get_drvinfo = ionic_get_drvinfo,
746 .get_regs_len = ionic_get_regs_len,
747 .get_regs = ionic_get_regs,
748 .get_link = ethtool_op_get_link,
749 .get_link_ksettings = ionic_get_link_ksettings,
750 .get_coalesce = ionic_get_coalesce,
751 .set_coalesce = ionic_set_coalesce,
752 .get_ringparam = ionic_get_ringparam,
753 .set_ringparam = ionic_set_ringparam,
754 .get_channels = ionic_get_channels,
755 .set_channels = ionic_set_channels,
756 .get_strings = ionic_get_strings,
757 .get_ethtool_stats = ionic_get_stats,
758 .get_sset_count = ionic_get_sset_count,
759 .get_priv_flags = ionic_get_priv_flags,
760 .set_priv_flags = ionic_set_priv_flags,
761 .get_rxnfc = ionic_get_rxnfc,
762 .get_rxfh_indir_size = ionic_get_rxfh_indir_size,
763 .get_rxfh_key_size = ionic_get_rxfh_key_size,
764 .get_rxfh = ionic_get_rxfh,
765 .set_rxfh = ionic_set_rxfh,
766 .get_tunable = ionic_get_tunable,
767 .set_tunable = ionic_set_tunable,
768 .get_module_info = ionic_get_module_info,
769 .get_module_eeprom = ionic_get_module_eeprom,
770 .get_pauseparam = ionic_get_pauseparam,
771 .set_pauseparam = ionic_set_pauseparam,
772 .set_link_ksettings = ionic_set_link_ksettings,
773 .nway_reset = ionic_nway_reset,
776 void ionic_ethtool_set_ops(struct net_device *netdev)
778 netdev->ethtool_ops = &ionic_ethtool_ops;