xref: /openbmc/linux/drivers/net/ethernet/sfc/ethtool.c (revision e0d07278)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2005-2006 Fen Systems Ltd.
5  * Copyright 2006-2013 Solarflare Communications Inc.
6  */
7 
8 #include <linux/netdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/in.h>
12 #include "net_driver.h"
13 #include "workarounds.h"
14 #include "selftest.h"
15 #include "efx.h"
16 #include "efx_channels.h"
17 #include "rx_common.h"
18 #include "tx_common.h"
19 #include "ethtool_common.h"
20 #include "filter.h"
21 #include "nic.h"
22 
23 #define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB
24 
25 /**************************************************************************
26  *
27  * Ethtool operations
28  *
29  **************************************************************************
30  */
31 
32 /* Identify device by flashing LEDs */
33 static int efx_ethtool_phys_id(struct net_device *net_dev,
34 			       enum ethtool_phys_id_state state)
35 {
36 	struct efx_nic *efx = netdev_priv(net_dev);
37 	enum efx_led_mode mode = EFX_LED_DEFAULT;
38 
39 	switch (state) {
40 	case ETHTOOL_ID_ON:
41 		mode = EFX_LED_ON;
42 		break;
43 	case ETHTOOL_ID_OFF:
44 		mode = EFX_LED_OFF;
45 		break;
46 	case ETHTOOL_ID_INACTIVE:
47 		mode = EFX_LED_DEFAULT;
48 		break;
49 	case ETHTOOL_ID_ACTIVE:
50 		return 1;	/* cycle on/off once per second */
51 	}
52 
53 	efx->type->set_id_led(efx, mode);
54 	return 0;
55 }
56 
57 static int efx_ethtool_get_regs_len(struct net_device *net_dev)
58 {
59 	return efx_nic_get_regs_len(netdev_priv(net_dev));
60 }
61 
62 static void efx_ethtool_get_regs(struct net_device *net_dev,
63 				 struct ethtool_regs *regs, void *buf)
64 {
65 	struct efx_nic *efx = netdev_priv(net_dev);
66 
67 	regs->version = efx->type->revision;
68 	efx_nic_get_regs(efx, buf);
69 }
70 
71 /*
72  * Each channel has a single IRQ and moderation timer, started by any
73  * completion (or other event).  Unless the module parameter
74  * separate_tx_channels is set, IRQs and moderation are therefore
75  * shared between RX and TX completions.  In this case, when RX IRQ
76  * moderation is explicitly changed then TX IRQ moderation is
77  * automatically changed too, but otherwise we fail if the two values
78  * are requested to be different.
79  *
80  * The hardware does not support a limit on the number of completions
81  * before an IRQ, so we do not use the max_frames fields.  We should
82  * report and require that max_frames == (usecs != 0), but this would
83  * invalidate existing user documentation.
84  *
85  * The hardware does not have distinct settings for interrupt
86  * moderation while the previous IRQ is being handled, so we should
87  * not use the 'irq' fields.  However, an earlier developer
88  * misunderstood the meaning of the 'irq' fields and the driver did
89  * not support the standard fields.  To avoid invalidating existing
90  * user documentation, we report and accept changes through either the
91  * standard or 'irq' fields.  If both are changed at the same time, we
92  * prefer the standard field.
93  *
94  * We implement adaptive IRQ moderation, but use a different algorithm
95  * from that assumed in the definition of struct ethtool_coalesce.
96  * Therefore we do not use any of the adaptive moderation parameters
97  * in it.
98  */
99 
100 static int efx_ethtool_get_coalesce(struct net_device *net_dev,
101 				    struct ethtool_coalesce *coalesce)
102 {
103 	struct efx_nic *efx = netdev_priv(net_dev);
104 	unsigned int tx_usecs, rx_usecs;
105 	bool rx_adaptive;
106 
107 	efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &rx_adaptive);
108 
109 	coalesce->tx_coalesce_usecs = tx_usecs;
110 	coalesce->tx_coalesce_usecs_irq = tx_usecs;
111 	coalesce->rx_coalesce_usecs = rx_usecs;
112 	coalesce->rx_coalesce_usecs_irq = rx_usecs;
113 	coalesce->use_adaptive_rx_coalesce = rx_adaptive;
114 
115 	return 0;
116 }
117 
118 static int efx_ethtool_set_coalesce(struct net_device *net_dev,
119 				    struct ethtool_coalesce *coalesce)
120 {
121 	struct efx_nic *efx = netdev_priv(net_dev);
122 	struct efx_channel *channel;
123 	unsigned int tx_usecs, rx_usecs;
124 	bool adaptive, rx_may_override_tx;
125 	int rc;
126 
127 	efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &adaptive);
128 
129 	if (coalesce->rx_coalesce_usecs != rx_usecs)
130 		rx_usecs = coalesce->rx_coalesce_usecs;
131 	else
132 		rx_usecs = coalesce->rx_coalesce_usecs_irq;
133 
134 	adaptive = coalesce->use_adaptive_rx_coalesce;
135 
136 	/* If channels are shared, TX IRQ moderation can be quietly
137 	 * overridden unless it is changed from its old value.
138 	 */
139 	rx_may_override_tx = (coalesce->tx_coalesce_usecs == tx_usecs &&
140 			      coalesce->tx_coalesce_usecs_irq == tx_usecs);
141 	if (coalesce->tx_coalesce_usecs != tx_usecs)
142 		tx_usecs = coalesce->tx_coalesce_usecs;
143 	else
144 		tx_usecs = coalesce->tx_coalesce_usecs_irq;
145 
146 	rc = efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive,
147 				     rx_may_override_tx);
148 	if (rc != 0)
149 		return rc;
150 
151 	efx_for_each_channel(channel, efx)
152 		efx->type->push_irq_moderation(channel);
153 
154 	return 0;
155 }
156 
157 static void efx_ethtool_get_ringparam(struct net_device *net_dev,
158 				      struct ethtool_ringparam *ring)
159 {
160 	struct efx_nic *efx = netdev_priv(net_dev);
161 
162 	ring->rx_max_pending = EFX_MAX_DMAQ_SIZE;
163 	ring->tx_max_pending = EFX_TXQ_MAX_ENT(efx);
164 	ring->rx_pending = efx->rxq_entries;
165 	ring->tx_pending = efx->txq_entries;
166 }
167 
168 static int efx_ethtool_set_ringparam(struct net_device *net_dev,
169 				     struct ethtool_ringparam *ring)
170 {
171 	struct efx_nic *efx = netdev_priv(net_dev);
172 	u32 txq_entries;
173 
174 	if (ring->rx_mini_pending || ring->rx_jumbo_pending ||
175 	    ring->rx_pending > EFX_MAX_DMAQ_SIZE ||
176 	    ring->tx_pending > EFX_TXQ_MAX_ENT(efx))
177 		return -EINVAL;
178 
179 	if (ring->rx_pending < EFX_RXQ_MIN_ENT) {
180 		netif_err(efx, drv, efx->net_dev,
181 			  "RX queues cannot be smaller than %u\n",
182 			  EFX_RXQ_MIN_ENT);
183 		return -EINVAL;
184 	}
185 
186 	txq_entries = max(ring->tx_pending, EFX_TXQ_MIN_ENT(efx));
187 	if (txq_entries != ring->tx_pending)
188 		netif_warn(efx, drv, efx->net_dev,
189 			   "increasing TX queue size to minimum of %u\n",
190 			   txq_entries);
191 
192 	return efx_realloc_channels(efx, ring->rx_pending, txq_entries);
193 }
194 
195 static void efx_ethtool_get_wol(struct net_device *net_dev,
196 				struct ethtool_wolinfo *wol)
197 {
198 	struct efx_nic *efx = netdev_priv(net_dev);
199 	return efx->type->get_wol(efx, wol);
200 }
201 
202 
203 static int efx_ethtool_set_wol(struct net_device *net_dev,
204 			       struct ethtool_wolinfo *wol)
205 {
206 	struct efx_nic *efx = netdev_priv(net_dev);
207 	return efx->type->set_wol(efx, wol->wolopts);
208 }
209 
210 static int efx_ethtool_get_ts_info(struct net_device *net_dev,
211 				   struct ethtool_ts_info *ts_info)
212 {
213 	struct efx_nic *efx = netdev_priv(net_dev);
214 
215 	/* Software capabilities */
216 	ts_info->so_timestamping = (SOF_TIMESTAMPING_RX_SOFTWARE |
217 				    SOF_TIMESTAMPING_SOFTWARE);
218 	ts_info->phc_index = -1;
219 
220 	efx_ptp_get_ts_info(efx, ts_info);
221 	return 0;
222 }
223 
224 const struct ethtool_ops efx_ethtool_ops = {
225 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
226 				     ETHTOOL_COALESCE_USECS_IRQ |
227 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
228 	.get_drvinfo		= efx_ethtool_get_drvinfo,
229 	.get_regs_len		= efx_ethtool_get_regs_len,
230 	.get_regs		= efx_ethtool_get_regs,
231 	.get_msglevel		= efx_ethtool_get_msglevel,
232 	.set_msglevel		= efx_ethtool_set_msglevel,
233 	.get_link		= ethtool_op_get_link,
234 	.get_coalesce		= efx_ethtool_get_coalesce,
235 	.set_coalesce		= efx_ethtool_set_coalesce,
236 	.get_ringparam		= efx_ethtool_get_ringparam,
237 	.set_ringparam		= efx_ethtool_set_ringparam,
238 	.get_pauseparam         = efx_ethtool_get_pauseparam,
239 	.set_pauseparam         = efx_ethtool_set_pauseparam,
240 	.get_sset_count		= efx_ethtool_get_sset_count,
241 	.self_test		= efx_ethtool_self_test,
242 	.get_strings		= efx_ethtool_get_strings,
243 	.set_phys_id		= efx_ethtool_phys_id,
244 	.get_ethtool_stats	= efx_ethtool_get_stats,
245 	.get_wol                = efx_ethtool_get_wol,
246 	.set_wol                = efx_ethtool_set_wol,
247 	.reset			= efx_ethtool_reset,
248 	.get_rxnfc		= efx_ethtool_get_rxnfc,
249 	.set_rxnfc		= efx_ethtool_set_rxnfc,
250 	.get_rxfh_indir_size	= efx_ethtool_get_rxfh_indir_size,
251 	.get_rxfh_key_size	= efx_ethtool_get_rxfh_key_size,
252 	.get_rxfh		= efx_ethtool_get_rxfh,
253 	.set_rxfh		= efx_ethtool_set_rxfh,
254 	.get_rxfh_context	= efx_ethtool_get_rxfh_context,
255 	.set_rxfh_context	= efx_ethtool_set_rxfh_context,
256 	.get_ts_info		= efx_ethtool_get_ts_info,
257 	.get_module_info	= efx_ethtool_get_module_info,
258 	.get_module_eeprom	= efx_ethtool_get_module_eeprom,
259 	.get_link_ksettings	= efx_ethtool_get_link_ksettings,
260 	.set_link_ksettings	= efx_ethtool_set_link_ksettings,
261 	.get_fecparam		= efx_ethtool_get_fecparam,
262 	.set_fecparam		= efx_ethtool_set_fecparam,
263 };
264