18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/****************************************************************************
38c2ecf20Sopenharmony_ci * Driver for Solarflare network controllers and boards
48c2ecf20Sopenharmony_ci * Copyright 2005-2006 Fen Systems Ltd.
58c2ecf20Sopenharmony_ci * Copyright 2006-2013 Solarflare Communications Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
98c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
108c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
118c2ecf20Sopenharmony_ci#include <linux/in.h>
128c2ecf20Sopenharmony_ci#include "net_driver.h"
138c2ecf20Sopenharmony_ci#include "workarounds.h"
148c2ecf20Sopenharmony_ci#include "selftest.h"
158c2ecf20Sopenharmony_ci#include "efx.h"
168c2ecf20Sopenharmony_ci#include "efx_channels.h"
178c2ecf20Sopenharmony_ci#include "rx_common.h"
188c2ecf20Sopenharmony_ci#include "tx_common.h"
198c2ecf20Sopenharmony_ci#include "ethtool_common.h"
208c2ecf20Sopenharmony_ci#include "filter.h"
218c2ecf20Sopenharmony_ci#include "nic.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/**************************************************************************
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * Ethtool operations
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci **************************************************************************
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Identify device by flashing LEDs */
338c2ecf20Sopenharmony_cistatic int efx_ethtool_phys_id(struct net_device *net_dev,
348c2ecf20Sopenharmony_ci			       enum ethtool_phys_id_state state)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
378c2ecf20Sopenharmony_ci	enum efx_led_mode mode = EFX_LED_DEFAULT;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	switch (state) {
408c2ecf20Sopenharmony_ci	case ETHTOOL_ID_ON:
418c2ecf20Sopenharmony_ci		mode = EFX_LED_ON;
428c2ecf20Sopenharmony_ci		break;
438c2ecf20Sopenharmony_ci	case ETHTOOL_ID_OFF:
448c2ecf20Sopenharmony_ci		mode = EFX_LED_OFF;
458c2ecf20Sopenharmony_ci		break;
468c2ecf20Sopenharmony_ci	case ETHTOOL_ID_INACTIVE:
478c2ecf20Sopenharmony_ci		mode = EFX_LED_DEFAULT;
488c2ecf20Sopenharmony_ci		break;
498c2ecf20Sopenharmony_ci	case ETHTOOL_ID_ACTIVE:
508c2ecf20Sopenharmony_ci		return 1;	/* cycle on/off once per second */
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return efx_mcdi_set_id_led(efx, mode);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int efx_ethtool_get_regs_len(struct net_device *net_dev)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	return efx_nic_get_regs_len(netdev_priv(net_dev));
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic void efx_ethtool_get_regs(struct net_device *net_dev,
628c2ecf20Sopenharmony_ci				 struct ethtool_regs *regs, void *buf)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	regs->version = efx->type->revision;
678c2ecf20Sopenharmony_ci	efx_nic_get_regs(efx, buf);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * Each channel has a single IRQ and moderation timer, started by any
728c2ecf20Sopenharmony_ci * completion (or other event).  Unless the module parameter
738c2ecf20Sopenharmony_ci * separate_tx_channels is set, IRQs and moderation are therefore
748c2ecf20Sopenharmony_ci * shared between RX and TX completions.  In this case, when RX IRQ
758c2ecf20Sopenharmony_ci * moderation is explicitly changed then TX IRQ moderation is
768c2ecf20Sopenharmony_ci * automatically changed too, but otherwise we fail if the two values
778c2ecf20Sopenharmony_ci * are requested to be different.
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * The hardware does not support a limit on the number of completions
808c2ecf20Sopenharmony_ci * before an IRQ, so we do not use the max_frames fields.  We should
818c2ecf20Sopenharmony_ci * report and require that max_frames == (usecs != 0), but this would
828c2ecf20Sopenharmony_ci * invalidate existing user documentation.
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * The hardware does not have distinct settings for interrupt
858c2ecf20Sopenharmony_ci * moderation while the previous IRQ is being handled, so we should
868c2ecf20Sopenharmony_ci * not use the 'irq' fields.  However, an earlier developer
878c2ecf20Sopenharmony_ci * misunderstood the meaning of the 'irq' fields and the driver did
888c2ecf20Sopenharmony_ci * not support the standard fields.  To avoid invalidating existing
898c2ecf20Sopenharmony_ci * user documentation, we report and accept changes through either the
908c2ecf20Sopenharmony_ci * standard or 'irq' fields.  If both are changed at the same time, we
918c2ecf20Sopenharmony_ci * prefer the standard field.
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * We implement adaptive IRQ moderation, but use a different algorithm
948c2ecf20Sopenharmony_ci * from that assumed in the definition of struct ethtool_coalesce.
958c2ecf20Sopenharmony_ci * Therefore we do not use any of the adaptive moderation parameters
968c2ecf20Sopenharmony_ci * in it.
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic int efx_ethtool_get_coalesce(struct net_device *net_dev,
1008c2ecf20Sopenharmony_ci				    struct ethtool_coalesce *coalesce)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
1038c2ecf20Sopenharmony_ci	unsigned int tx_usecs, rx_usecs;
1048c2ecf20Sopenharmony_ci	bool rx_adaptive;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &rx_adaptive);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	coalesce->tx_coalesce_usecs = tx_usecs;
1098c2ecf20Sopenharmony_ci	coalesce->tx_coalesce_usecs_irq = tx_usecs;
1108c2ecf20Sopenharmony_ci	coalesce->rx_coalesce_usecs = rx_usecs;
1118c2ecf20Sopenharmony_ci	coalesce->rx_coalesce_usecs_irq = rx_usecs;
1128c2ecf20Sopenharmony_ci	coalesce->use_adaptive_rx_coalesce = rx_adaptive;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int efx_ethtool_set_coalesce(struct net_device *net_dev,
1188c2ecf20Sopenharmony_ci				    struct ethtool_coalesce *coalesce)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
1218c2ecf20Sopenharmony_ci	struct efx_channel *channel;
1228c2ecf20Sopenharmony_ci	unsigned int tx_usecs, rx_usecs;
1238c2ecf20Sopenharmony_ci	bool adaptive, rx_may_override_tx;
1248c2ecf20Sopenharmony_ci	int rc;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &adaptive);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (coalesce->rx_coalesce_usecs != rx_usecs)
1298c2ecf20Sopenharmony_ci		rx_usecs = coalesce->rx_coalesce_usecs;
1308c2ecf20Sopenharmony_ci	else
1318c2ecf20Sopenharmony_ci		rx_usecs = coalesce->rx_coalesce_usecs_irq;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	adaptive = coalesce->use_adaptive_rx_coalesce;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/* If channels are shared, TX IRQ moderation can be quietly
1368c2ecf20Sopenharmony_ci	 * overridden unless it is changed from its old value.
1378c2ecf20Sopenharmony_ci	 */
1388c2ecf20Sopenharmony_ci	rx_may_override_tx = (coalesce->tx_coalesce_usecs == tx_usecs &&
1398c2ecf20Sopenharmony_ci			      coalesce->tx_coalesce_usecs_irq == tx_usecs);
1408c2ecf20Sopenharmony_ci	if (coalesce->tx_coalesce_usecs != tx_usecs)
1418c2ecf20Sopenharmony_ci		tx_usecs = coalesce->tx_coalesce_usecs;
1428c2ecf20Sopenharmony_ci	else
1438c2ecf20Sopenharmony_ci		tx_usecs = coalesce->tx_coalesce_usecs_irq;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	rc = efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive,
1468c2ecf20Sopenharmony_ci				     rx_may_override_tx);
1478c2ecf20Sopenharmony_ci	if (rc != 0)
1488c2ecf20Sopenharmony_ci		return rc;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	efx_for_each_channel(channel, efx)
1518c2ecf20Sopenharmony_ci		efx->type->push_irq_moderation(channel);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return 0;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic void efx_ethtool_get_ringparam(struct net_device *net_dev,
1578c2ecf20Sopenharmony_ci				      struct ethtool_ringparam *ring)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ring->rx_max_pending = EFX_MAX_DMAQ_SIZE;
1628c2ecf20Sopenharmony_ci	ring->tx_max_pending = EFX_TXQ_MAX_ENT(efx);
1638c2ecf20Sopenharmony_ci	ring->rx_pending = efx->rxq_entries;
1648c2ecf20Sopenharmony_ci	ring->tx_pending = efx->txq_entries;
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int efx_ethtool_set_ringparam(struct net_device *net_dev,
1688c2ecf20Sopenharmony_ci				     struct ethtool_ringparam *ring)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
1718c2ecf20Sopenharmony_ci	u32 txq_entries;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (ring->rx_mini_pending || ring->rx_jumbo_pending ||
1748c2ecf20Sopenharmony_ci	    ring->rx_pending > EFX_MAX_DMAQ_SIZE ||
1758c2ecf20Sopenharmony_ci	    ring->tx_pending > EFX_TXQ_MAX_ENT(efx))
1768c2ecf20Sopenharmony_ci		return -EINVAL;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (ring->rx_pending < EFX_RXQ_MIN_ENT) {
1798c2ecf20Sopenharmony_ci		netif_err(efx, drv, efx->net_dev,
1808c2ecf20Sopenharmony_ci			  "RX queues cannot be smaller than %u\n",
1818c2ecf20Sopenharmony_ci			  EFX_RXQ_MIN_ENT);
1828c2ecf20Sopenharmony_ci		return -EINVAL;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	txq_entries = max(ring->tx_pending, EFX_TXQ_MIN_ENT(efx));
1868c2ecf20Sopenharmony_ci	if (txq_entries != ring->tx_pending)
1878c2ecf20Sopenharmony_ci		netif_warn(efx, drv, efx->net_dev,
1888c2ecf20Sopenharmony_ci			   "increasing TX queue size to minimum of %u\n",
1898c2ecf20Sopenharmony_ci			   txq_entries);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return efx_realloc_channels(efx, ring->rx_pending, txq_entries);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic void efx_ethtool_get_wol(struct net_device *net_dev,
1958c2ecf20Sopenharmony_ci				struct ethtool_wolinfo *wol)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
1988c2ecf20Sopenharmony_ci	return efx->type->get_wol(efx, wol);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int efx_ethtool_set_wol(struct net_device *net_dev,
2038c2ecf20Sopenharmony_ci			       struct ethtool_wolinfo *wol)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
2068c2ecf20Sopenharmony_ci	return efx->type->set_wol(efx, wol->wolopts);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic int efx_ethtool_get_ts_info(struct net_device *net_dev,
2108c2ecf20Sopenharmony_ci				   struct ethtool_ts_info *ts_info)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/* Software capabilities */
2158c2ecf20Sopenharmony_ci	ts_info->so_timestamping = (SOF_TIMESTAMPING_RX_SOFTWARE |
2168c2ecf20Sopenharmony_ci				    SOF_TIMESTAMPING_SOFTWARE);
2178c2ecf20Sopenharmony_ci	ts_info->phc_index = -1;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	efx_ptp_get_ts_info(efx, ts_info);
2208c2ecf20Sopenharmony_ci	return 0;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ciconst struct ethtool_ops efx_ethtool_ops = {
2248c2ecf20Sopenharmony_ci	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2258c2ecf20Sopenharmony_ci				     ETHTOOL_COALESCE_USECS_IRQ |
2268c2ecf20Sopenharmony_ci				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
2278c2ecf20Sopenharmony_ci	.get_drvinfo		= efx_ethtool_get_drvinfo,
2288c2ecf20Sopenharmony_ci	.get_regs_len		= efx_ethtool_get_regs_len,
2298c2ecf20Sopenharmony_ci	.get_regs		= efx_ethtool_get_regs,
2308c2ecf20Sopenharmony_ci	.get_msglevel		= efx_ethtool_get_msglevel,
2318c2ecf20Sopenharmony_ci	.set_msglevel		= efx_ethtool_set_msglevel,
2328c2ecf20Sopenharmony_ci	.get_link		= ethtool_op_get_link,
2338c2ecf20Sopenharmony_ci	.get_coalesce		= efx_ethtool_get_coalesce,
2348c2ecf20Sopenharmony_ci	.set_coalesce		= efx_ethtool_set_coalesce,
2358c2ecf20Sopenharmony_ci	.get_ringparam		= efx_ethtool_get_ringparam,
2368c2ecf20Sopenharmony_ci	.set_ringparam		= efx_ethtool_set_ringparam,
2378c2ecf20Sopenharmony_ci	.get_pauseparam         = efx_ethtool_get_pauseparam,
2388c2ecf20Sopenharmony_ci	.set_pauseparam         = efx_ethtool_set_pauseparam,
2398c2ecf20Sopenharmony_ci	.get_sset_count		= efx_ethtool_get_sset_count,
2408c2ecf20Sopenharmony_ci	.self_test		= efx_ethtool_self_test,
2418c2ecf20Sopenharmony_ci	.get_strings		= efx_ethtool_get_strings,
2428c2ecf20Sopenharmony_ci	.set_phys_id		= efx_ethtool_phys_id,
2438c2ecf20Sopenharmony_ci	.get_ethtool_stats	= efx_ethtool_get_stats,
2448c2ecf20Sopenharmony_ci	.get_wol                = efx_ethtool_get_wol,
2458c2ecf20Sopenharmony_ci	.set_wol                = efx_ethtool_set_wol,
2468c2ecf20Sopenharmony_ci	.reset			= efx_ethtool_reset,
2478c2ecf20Sopenharmony_ci	.get_rxnfc		= efx_ethtool_get_rxnfc,
2488c2ecf20Sopenharmony_ci	.set_rxnfc		= efx_ethtool_set_rxnfc,
2498c2ecf20Sopenharmony_ci	.get_rxfh_indir_size	= efx_ethtool_get_rxfh_indir_size,
2508c2ecf20Sopenharmony_ci	.get_rxfh_key_size	= efx_ethtool_get_rxfh_key_size,
2518c2ecf20Sopenharmony_ci	.get_rxfh		= efx_ethtool_get_rxfh,
2528c2ecf20Sopenharmony_ci	.set_rxfh		= efx_ethtool_set_rxfh,
2538c2ecf20Sopenharmony_ci	.get_rxfh_context	= efx_ethtool_get_rxfh_context,
2548c2ecf20Sopenharmony_ci	.set_rxfh_context	= efx_ethtool_set_rxfh_context,
2558c2ecf20Sopenharmony_ci	.get_ts_info		= efx_ethtool_get_ts_info,
2568c2ecf20Sopenharmony_ci	.get_module_info	= efx_ethtool_get_module_info,
2578c2ecf20Sopenharmony_ci	.get_module_eeprom	= efx_ethtool_get_module_eeprom,
2588c2ecf20Sopenharmony_ci	.get_link_ksettings	= efx_ethtool_get_link_ksettings,
2598c2ecf20Sopenharmony_ci	.set_link_ksettings	= efx_ethtool_set_link_ksettings,
2608c2ecf20Sopenharmony_ci	.get_fecparam		= efx_ethtool_get_fecparam,
2618c2ecf20Sopenharmony_ci	.set_fecparam		= efx_ethtool_set_fecparam,
2628c2ecf20Sopenharmony_ci};
263