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