xref: /linux/drivers/net/ethernet/intel/igbvf/ethtool.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009 - 2018 Intel Corporation. */
3 
4 /* ethtool support for igbvf */
5 
6 #include <linux/netdevice.h>
7 #include <linux/ethtool.h>
8 #include <linux/pci.h>
9 #include <linux/vmalloc.h>
10 #include <linux/delay.h>
11 
12 #include "igbvf.h"
13 #include <linux/if_vlan.h>
14 
15 struct igbvf_stats {
16 	char stat_string[ETH_GSTRING_LEN];
17 	int sizeof_stat;
18 	int stat_offset;
19 	int base_stat_offset;
20 };
21 
22 #define IGBVF_STAT(current, base) \
23 		sizeof(((struct igbvf_adapter *)0)->current), \
24 		offsetof(struct igbvf_adapter, current), \
25 		offsetof(struct igbvf_adapter, base)
26 
27 static const struct igbvf_stats igbvf_gstrings_stats[] = {
28 	{ "rx_packets", IGBVF_STAT(stats.gprc, stats.base_gprc) },
29 	{ "tx_packets", IGBVF_STAT(stats.gptc, stats.base_gptc) },
30 	{ "rx_bytes", IGBVF_STAT(stats.gorc, stats.base_gorc) },
31 	{ "tx_bytes", IGBVF_STAT(stats.gotc, stats.base_gotc) },
32 	{ "multicast", IGBVF_STAT(stats.mprc, stats.base_mprc) },
33 	{ "lbrx_bytes", IGBVF_STAT(stats.gorlbc, stats.base_gorlbc) },
34 	{ "lbrx_packets", IGBVF_STAT(stats.gprlbc, stats.base_gprlbc) },
35 	{ "tx_restart_queue", IGBVF_STAT(restart_queue, zero_base) },
36 	{ "tx_timeout_count", IGBVF_STAT(tx_timeout_count, zero_base) },
37 	{ "rx_long_byte_count", IGBVF_STAT(stats.gorc, stats.base_gorc) },
38 	{ "rx_csum_offload_good", IGBVF_STAT(hw_csum_good, zero_base) },
39 	{ "rx_csum_offload_errors", IGBVF_STAT(hw_csum_err, zero_base) },
40 	{ "rx_header_split", IGBVF_STAT(rx_hdr_split, zero_base) },
41 	{ "alloc_rx_buff_failed", IGBVF_STAT(alloc_rx_buff_failed, zero_base) },
42 };
43 
44 #define IGBVF_GLOBAL_STATS_LEN ARRAY_SIZE(igbvf_gstrings_stats)
45 
46 static const char igbvf_gstrings_test[][ETH_GSTRING_LEN] = {
47 	"Link test   (on/offline)"
48 };
49 
50 #define IGBVF_TEST_LEN ARRAY_SIZE(igbvf_gstrings_test)
51 
igbvf_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)52 static int igbvf_get_link_ksettings(struct net_device *netdev,
53 				    struct ethtool_link_ksettings *cmd)
54 {
55 	struct igbvf_adapter *adapter = netdev_priv(netdev);
56 	struct e1000_hw *hw = &adapter->hw;
57 	u32 status;
58 
59 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
60 	ethtool_link_ksettings_add_link_mode(cmd, supported, 1000baseT_Full);
61 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
62 	ethtool_link_ksettings_add_link_mode(cmd, advertising, 1000baseT_Full);
63 
64 	cmd->base.port = -1;
65 
66 	status = er32(STATUS);
67 	if (status & E1000_STATUS_LU) {
68 		if (status & E1000_STATUS_SPEED_1000)
69 			cmd->base.speed = SPEED_1000;
70 		else if (status & E1000_STATUS_SPEED_100)
71 			cmd->base.speed = SPEED_100;
72 		else
73 			cmd->base.speed = SPEED_10;
74 
75 		if (status & E1000_STATUS_FD)
76 			cmd->base.duplex = DUPLEX_FULL;
77 		else
78 			cmd->base.duplex = DUPLEX_HALF;
79 	} else {
80 		cmd->base.speed = SPEED_UNKNOWN;
81 		cmd->base.duplex = DUPLEX_UNKNOWN;
82 	}
83 
84 	cmd->base.autoneg = AUTONEG_DISABLE;
85 
86 	return 0;
87 }
88 
igbvf_set_link_ksettings(struct net_device * netdev,const struct ethtool_link_ksettings * cmd)89 static int igbvf_set_link_ksettings(struct net_device *netdev,
90 				    const struct ethtool_link_ksettings *cmd)
91 {
92 	return -EOPNOTSUPP;
93 }
94 
igbvf_get_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)95 static void igbvf_get_pauseparam(struct net_device *netdev,
96 				 struct ethtool_pauseparam *pause)
97 {
98 }
99 
igbvf_set_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)100 static int igbvf_set_pauseparam(struct net_device *netdev,
101 				struct ethtool_pauseparam *pause)
102 {
103 	return -EOPNOTSUPP;
104 }
105 
igbvf_get_msglevel(struct net_device * netdev)106 static u32 igbvf_get_msglevel(struct net_device *netdev)
107 {
108 	struct igbvf_adapter *adapter = netdev_priv(netdev);
109 
110 	return adapter->msg_enable;
111 }
112 
igbvf_set_msglevel(struct net_device * netdev,u32 data)113 static void igbvf_set_msglevel(struct net_device *netdev, u32 data)
114 {
115 	struct igbvf_adapter *adapter = netdev_priv(netdev);
116 
117 	adapter->msg_enable = data;
118 }
119 
igbvf_get_regs_len(struct net_device * netdev)120 static int igbvf_get_regs_len(struct net_device *netdev)
121 {
122 #define IGBVF_REGS_LEN 8
123 	return IGBVF_REGS_LEN * sizeof(u32);
124 }
125 
igbvf_get_regs(struct net_device * netdev,struct ethtool_regs * regs,void * p)126 static void igbvf_get_regs(struct net_device *netdev,
127 			   struct ethtool_regs *regs, void *p)
128 {
129 	struct igbvf_adapter *adapter = netdev_priv(netdev);
130 	struct e1000_hw *hw = &adapter->hw;
131 	u32 *regs_buff = p;
132 
133 	memset(p, 0, IGBVF_REGS_LEN * sizeof(u32));
134 
135 	regs->version = (1u << 24) |
136 			(adapter->pdev->revision << 16) |
137 			adapter->pdev->device;
138 
139 	regs_buff[0] = er32(CTRL);
140 	regs_buff[1] = er32(STATUS);
141 
142 	regs_buff[2] = er32(RDLEN(0));
143 	regs_buff[3] = er32(RDH(0));
144 	regs_buff[4] = er32(RDT(0));
145 
146 	regs_buff[5] = er32(TDLEN(0));
147 	regs_buff[6] = er32(TDH(0));
148 	regs_buff[7] = er32(TDT(0));
149 }
150 
igbvf_get_eeprom_len(struct net_device * netdev)151 static int igbvf_get_eeprom_len(struct net_device *netdev)
152 {
153 	return 0;
154 }
155 
igbvf_get_eeprom(struct net_device * netdev,struct ethtool_eeprom * eeprom,u8 * bytes)156 static int igbvf_get_eeprom(struct net_device *netdev,
157 			    struct ethtool_eeprom *eeprom, u8 *bytes)
158 {
159 	return -EOPNOTSUPP;
160 }
161 
igbvf_set_eeprom(struct net_device * netdev,struct ethtool_eeprom * eeprom,u8 * bytes)162 static int igbvf_set_eeprom(struct net_device *netdev,
163 			    struct ethtool_eeprom *eeprom, u8 *bytes)
164 {
165 	return -EOPNOTSUPP;
166 }
167 
igbvf_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)168 static void igbvf_get_drvinfo(struct net_device *netdev,
169 			      struct ethtool_drvinfo *drvinfo)
170 {
171 	struct igbvf_adapter *adapter = netdev_priv(netdev);
172 
173 	strscpy(drvinfo->driver,  igbvf_driver_name, sizeof(drvinfo->driver));
174 	strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
175 		sizeof(drvinfo->bus_info));
176 }
177 
igbvf_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)178 static void igbvf_get_ringparam(struct net_device *netdev,
179 				struct ethtool_ringparam *ring,
180 				struct kernel_ethtool_ringparam *kernel_ring,
181 				struct netlink_ext_ack *extack)
182 {
183 	struct igbvf_adapter *adapter = netdev_priv(netdev);
184 	struct igbvf_ring *tx_ring = adapter->tx_ring;
185 	struct igbvf_ring *rx_ring = adapter->rx_ring;
186 
187 	ring->rx_max_pending = IGBVF_MAX_RXD;
188 	ring->tx_max_pending = IGBVF_MAX_TXD;
189 	ring->rx_pending = rx_ring->count;
190 	ring->tx_pending = tx_ring->count;
191 }
192 
igbvf_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)193 static int igbvf_set_ringparam(struct net_device *netdev,
194 			       struct ethtool_ringparam *ring,
195 			       struct kernel_ethtool_ringparam *kernel_ring,
196 			       struct netlink_ext_ack *extack)
197 {
198 	struct igbvf_adapter *adapter = netdev_priv(netdev);
199 	struct igbvf_ring *temp_ring;
200 	int err = 0;
201 	u32 new_rx_count, new_tx_count;
202 
203 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
204 		return -EINVAL;
205 
206 	new_rx_count = max_t(u32, ring->rx_pending, IGBVF_MIN_RXD);
207 	new_rx_count = min_t(u32, new_rx_count, IGBVF_MAX_RXD);
208 	new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE);
209 
210 	new_tx_count = max_t(u32, ring->tx_pending, IGBVF_MIN_TXD);
211 	new_tx_count = min_t(u32, new_tx_count, IGBVF_MAX_TXD);
212 	new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE);
213 
214 	if ((new_tx_count == adapter->tx_ring->count) &&
215 	    (new_rx_count == adapter->rx_ring->count)) {
216 		/* nothing to do */
217 		return 0;
218 	}
219 
220 	while (test_and_set_bit(__IGBVF_RESETTING, &adapter->state))
221 		usleep_range(1000, 2000);
222 
223 	if (!netif_running(adapter->netdev)) {
224 		adapter->tx_ring->count = new_tx_count;
225 		adapter->rx_ring->count = new_rx_count;
226 		goto clear_reset;
227 	}
228 
229 	temp_ring = vmalloc(sizeof(struct igbvf_ring));
230 	if (!temp_ring) {
231 		err = -ENOMEM;
232 		goto clear_reset;
233 	}
234 
235 	igbvf_down(adapter);
236 
237 	/* We can't just free everything and then setup again,
238 	 * because the ISRs in MSI-X mode get passed pointers
239 	 * to the Tx and Rx ring structs.
240 	 */
241 	if (new_tx_count != adapter->tx_ring->count) {
242 		memcpy(temp_ring, adapter->tx_ring, sizeof(struct igbvf_ring));
243 
244 		temp_ring->count = new_tx_count;
245 		err = igbvf_setup_tx_resources(adapter, temp_ring);
246 		if (err)
247 			goto err_setup;
248 
249 		igbvf_free_tx_resources(adapter->tx_ring);
250 
251 		memcpy(adapter->tx_ring, temp_ring, sizeof(struct igbvf_ring));
252 	}
253 
254 	if (new_rx_count != adapter->rx_ring->count) {
255 		memcpy(temp_ring, adapter->rx_ring, sizeof(struct igbvf_ring));
256 
257 		temp_ring->count = new_rx_count;
258 		err = igbvf_setup_rx_resources(adapter, temp_ring);
259 		if (err)
260 			goto err_setup;
261 
262 		igbvf_free_rx_resources(adapter->rx_ring);
263 
264 		memcpy(adapter->rx_ring, temp_ring, sizeof(struct igbvf_ring));
265 	}
266 err_setup:
267 	igbvf_up(adapter);
268 	vfree(temp_ring);
269 clear_reset:
270 	clear_bit(__IGBVF_RESETTING, &adapter->state);
271 	return err;
272 }
273 
igbvf_link_test(struct igbvf_adapter * adapter,u64 * data)274 static int igbvf_link_test(struct igbvf_adapter *adapter, u64 *data)
275 {
276 	struct e1000_hw *hw = &adapter->hw;
277 	*data = 0;
278 
279 	spin_lock_bh(&hw->mbx_lock);
280 
281 	hw->mac.ops.check_for_link(hw);
282 
283 	spin_unlock_bh(&hw->mbx_lock);
284 
285 	if (!(er32(STATUS) & E1000_STATUS_LU))
286 		*data = 1;
287 
288 	return *data;
289 }
290 
igbvf_diag_test(struct net_device * netdev,struct ethtool_test * eth_test,u64 * data)291 static void igbvf_diag_test(struct net_device *netdev,
292 			    struct ethtool_test *eth_test, u64 *data)
293 {
294 	struct igbvf_adapter *adapter = netdev_priv(netdev);
295 
296 	set_bit(__IGBVF_TESTING, &adapter->state);
297 
298 	/* Link test performed before hardware reset so autoneg doesn't
299 	 * interfere with test result
300 	 */
301 	if (igbvf_link_test(adapter, &data[0]))
302 		eth_test->flags |= ETH_TEST_FL_FAILED;
303 
304 	clear_bit(__IGBVF_TESTING, &adapter->state);
305 	msleep_interruptible(4 * 1000);
306 }
307 
igbvf_get_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)308 static void igbvf_get_wol(struct net_device *netdev,
309 			  struct ethtool_wolinfo *wol)
310 {
311 	wol->supported = 0;
312 	wol->wolopts = 0;
313 }
314 
igbvf_set_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)315 static int igbvf_set_wol(struct net_device *netdev,
316 			 struct ethtool_wolinfo *wol)
317 {
318 	return -EOPNOTSUPP;
319 }
320 
igbvf_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)321 static int igbvf_get_coalesce(struct net_device *netdev,
322 			      struct ethtool_coalesce *ec,
323 			      struct kernel_ethtool_coalesce *kernel_coal,
324 			      struct netlink_ext_ack *extack)
325 {
326 	struct igbvf_adapter *adapter = netdev_priv(netdev);
327 
328 	if (adapter->requested_itr <= 3)
329 		ec->rx_coalesce_usecs = adapter->requested_itr;
330 	else
331 		ec->rx_coalesce_usecs = adapter->current_itr >> 2;
332 
333 	return 0;
334 }
335 
igbvf_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)336 static int igbvf_set_coalesce(struct net_device *netdev,
337 			      struct ethtool_coalesce *ec,
338 			      struct kernel_ethtool_coalesce *kernel_coal,
339 			      struct netlink_ext_ack *extack)
340 {
341 	struct igbvf_adapter *adapter = netdev_priv(netdev);
342 	struct e1000_hw *hw = &adapter->hw;
343 
344 	if ((ec->rx_coalesce_usecs >= IGBVF_MIN_ITR_USECS) &&
345 	    (ec->rx_coalesce_usecs <= IGBVF_MAX_ITR_USECS)) {
346 		adapter->current_itr = ec->rx_coalesce_usecs << 2;
347 		adapter->requested_itr = 1000000000 /
348 					(adapter->current_itr * 256);
349 	} else if ((ec->rx_coalesce_usecs == 3) ||
350 		   (ec->rx_coalesce_usecs == 2)) {
351 		adapter->current_itr = IGBVF_START_ITR;
352 		adapter->requested_itr = ec->rx_coalesce_usecs;
353 	} else if (ec->rx_coalesce_usecs == 0) {
354 		/* The user's desire is to turn off interrupt throttling
355 		 * altogether, but due to HW limitations, we can't do that.
356 		 * Instead we set a very small value in EITR, which would
357 		 * allow ~967k interrupts per second, but allow the adapter's
358 		 * internal clocking to still function properly.
359 		 */
360 		adapter->current_itr = 4;
361 		adapter->requested_itr = 1000000000 /
362 					(adapter->current_itr * 256);
363 	} else {
364 		return -EINVAL;
365 	}
366 
367 	writel(adapter->current_itr,
368 	       hw->hw_addr + adapter->rx_ring->itr_register);
369 
370 	return 0;
371 }
372 
igbvf_nway_reset(struct net_device * netdev)373 static int igbvf_nway_reset(struct net_device *netdev)
374 {
375 	struct igbvf_adapter *adapter = netdev_priv(netdev);
376 
377 	if (netif_running(netdev))
378 		igbvf_reinit_locked(adapter);
379 	return 0;
380 }
381 
igbvf_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)382 static void igbvf_get_ethtool_stats(struct net_device *netdev,
383 				    struct ethtool_stats *stats,
384 				    u64 *data)
385 {
386 	struct igbvf_adapter *adapter = netdev_priv(netdev);
387 	int i;
388 
389 	igbvf_update_stats(adapter);
390 	for (i = 0; i < IGBVF_GLOBAL_STATS_LEN; i++) {
391 		char *p = (char *)adapter +
392 			  igbvf_gstrings_stats[i].stat_offset;
393 		char *b = (char *)adapter +
394 			  igbvf_gstrings_stats[i].base_stat_offset;
395 		data[i] = ((igbvf_gstrings_stats[i].sizeof_stat ==
396 			    sizeof(u64)) ? (*(u64 *)p - *(u64 *)b) :
397 			    (*(u32 *)p - *(u32 *)b));
398 	}
399 }
400 
igbvf_get_sset_count(struct net_device * dev,int stringset)401 static int igbvf_get_sset_count(struct net_device *dev, int stringset)
402 {
403 	switch (stringset) {
404 	case ETH_SS_TEST:
405 		return IGBVF_TEST_LEN;
406 	case ETH_SS_STATS:
407 		return IGBVF_GLOBAL_STATS_LEN;
408 	default:
409 		return -EINVAL;
410 	}
411 }
412 
igbvf_get_strings(struct net_device * netdev,u32 stringset,u8 * data)413 static void igbvf_get_strings(struct net_device *netdev, u32 stringset,
414 			      u8 *data)
415 {
416 	u8 *p = data;
417 	int i;
418 
419 	switch (stringset) {
420 	case ETH_SS_TEST:
421 		memcpy(data, *igbvf_gstrings_test, sizeof(igbvf_gstrings_test));
422 		break;
423 	case ETH_SS_STATS:
424 		for (i = 0; i < IGBVF_GLOBAL_STATS_LEN; i++) {
425 			memcpy(p, igbvf_gstrings_stats[i].stat_string,
426 			       ETH_GSTRING_LEN);
427 			p += ETH_GSTRING_LEN;
428 		}
429 		break;
430 	}
431 }
432 
433 static const struct ethtool_ops igbvf_ethtool_ops = {
434 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
435 	.get_drvinfo		= igbvf_get_drvinfo,
436 	.get_regs_len		= igbvf_get_regs_len,
437 	.get_regs		= igbvf_get_regs,
438 	.get_wol		= igbvf_get_wol,
439 	.set_wol		= igbvf_set_wol,
440 	.get_msglevel		= igbvf_get_msglevel,
441 	.set_msglevel		= igbvf_set_msglevel,
442 	.nway_reset		= igbvf_nway_reset,
443 	.get_link		= ethtool_op_get_link,
444 	.get_eeprom_len		= igbvf_get_eeprom_len,
445 	.get_eeprom		= igbvf_get_eeprom,
446 	.set_eeprom		= igbvf_set_eeprom,
447 	.get_ringparam		= igbvf_get_ringparam,
448 	.set_ringparam		= igbvf_set_ringparam,
449 	.get_pauseparam		= igbvf_get_pauseparam,
450 	.set_pauseparam		= igbvf_set_pauseparam,
451 	.self_test		= igbvf_diag_test,
452 	.get_sset_count		= igbvf_get_sset_count,
453 	.get_strings		= igbvf_get_strings,
454 	.get_ethtool_stats	= igbvf_get_ethtool_stats,
455 	.get_coalesce		= igbvf_get_coalesce,
456 	.set_coalesce		= igbvf_set_coalesce,
457 	.get_link_ksettings	= igbvf_get_link_ksettings,
458 	.set_link_ksettings	= igbvf_set_link_ksettings,
459 };
460 
igbvf_set_ethtool_ops(struct net_device * netdev)461 void igbvf_set_ethtool_ops(struct net_device *netdev)
462 {
463 	netdev->ethtool_ops = &igbvf_ethtool_ops;
464 }
465