1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
3 
4 /* ethtool support for ixgbevf */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/pci.h>
12 #include <linux/netdevice.h>
13 #include <linux/ethtool.h>
14 #include <linux/vmalloc.h>
15 #include <linux/if_vlan.h>
16 #include <linux/uaccess.h>
17 
18 #include "ixgbevf.h"
19 
20 #define IXGBE_ALL_RAR_ENTRIES 16
21 
22 enum {NETDEV_STATS, IXGBEVF_STATS};
23 
24 struct ixgbe_stats {
25 	char stat_string[ETH_GSTRING_LEN];
26 	int type;
27 	int sizeof_stat;
28 	int stat_offset;
29 };
30 
31 #define IXGBEVF_STAT(_name, _stat) { \
32 	.stat_string = _name, \
33 	.type = IXGBEVF_STATS, \
34 	.sizeof_stat = FIELD_SIZEOF(struct ixgbevf_adapter, _stat), \
35 	.stat_offset = offsetof(struct ixgbevf_adapter, _stat) \
36 }
37 
38 #define IXGBEVF_NETDEV_STAT(_net_stat) { \
39 	.stat_string = #_net_stat, \
40 	.type = NETDEV_STATS, \
41 	.sizeof_stat = FIELD_SIZEOF(struct net_device_stats, _net_stat), \
42 	.stat_offset = offsetof(struct net_device_stats, _net_stat) \
43 }
44 
45 static struct ixgbe_stats ixgbevf_gstrings_stats[] = {
46 	IXGBEVF_NETDEV_STAT(rx_packets),
47 	IXGBEVF_NETDEV_STAT(tx_packets),
48 	IXGBEVF_NETDEV_STAT(rx_bytes),
49 	IXGBEVF_NETDEV_STAT(tx_bytes),
50 	IXGBEVF_STAT("tx_busy", tx_busy),
51 	IXGBEVF_STAT("tx_restart_queue", restart_queue),
52 	IXGBEVF_STAT("tx_timeout_count", tx_timeout_count),
53 	IXGBEVF_NETDEV_STAT(multicast),
54 	IXGBEVF_STAT("rx_csum_offload_errors", hw_csum_rx_error),
55 	IXGBEVF_STAT("alloc_rx_page", alloc_rx_page),
56 	IXGBEVF_STAT("alloc_rx_page_failed", alloc_rx_page_failed),
57 	IXGBEVF_STAT("alloc_rx_buff_failed", alloc_rx_buff_failed),
58 	IXGBEVF_STAT("tx_ipsec", tx_ipsec),
59 	IXGBEVF_STAT("rx_ipsec", rx_ipsec),
60 };
61 
62 #define IXGBEVF_QUEUE_STATS_LEN ( \
63 	(((struct ixgbevf_adapter *)netdev_priv(netdev))->num_tx_queues + \
64 	 ((struct ixgbevf_adapter *)netdev_priv(netdev))->num_xdp_queues + \
65 	 ((struct ixgbevf_adapter *)netdev_priv(netdev))->num_rx_queues) * \
66 	 (sizeof(struct ixgbevf_stats) / sizeof(u64)))
67 #define IXGBEVF_GLOBAL_STATS_LEN ARRAY_SIZE(ixgbevf_gstrings_stats)
68 
69 #define IXGBEVF_STATS_LEN (IXGBEVF_GLOBAL_STATS_LEN + IXGBEVF_QUEUE_STATS_LEN)
70 static const char ixgbe_gstrings_test[][ETH_GSTRING_LEN] = {
71 	"Register test  (offline)",
72 	"Link test   (on/offline)"
73 };
74 
75 #define IXGBEVF_TEST_LEN (sizeof(ixgbe_gstrings_test) / ETH_GSTRING_LEN)
76 
77 static const char ixgbevf_priv_flags_strings[][ETH_GSTRING_LEN] = {
78 #define IXGBEVF_PRIV_FLAGS_LEGACY_RX	BIT(0)
79 	"legacy-rx",
80 };
81 
82 #define IXGBEVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(ixgbevf_priv_flags_strings)
83 
84 static int ixgbevf_get_link_ksettings(struct net_device *netdev,
85 				      struct ethtool_link_ksettings *cmd)
86 {
87 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
88 	struct ixgbe_hw *hw = &adapter->hw;
89 	u32 link_speed = 0;
90 	bool link_up;
91 
92 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
93 	ethtool_link_ksettings_add_link_mode(cmd, supported, 10000baseT_Full);
94 	cmd->base.autoneg = AUTONEG_DISABLE;
95 	cmd->base.port = -1;
96 
97 	hw->mac.get_link_status = 1;
98 	hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
99 
100 	if (link_up) {
101 		__u32 speed = SPEED_10000;
102 
103 		switch (link_speed) {
104 		case IXGBE_LINK_SPEED_10GB_FULL:
105 			speed = SPEED_10000;
106 			break;
107 		case IXGBE_LINK_SPEED_1GB_FULL:
108 			speed = SPEED_1000;
109 			break;
110 		case IXGBE_LINK_SPEED_100_FULL:
111 			speed = SPEED_100;
112 			break;
113 		}
114 
115 		cmd->base.speed = speed;
116 		cmd->base.duplex = DUPLEX_FULL;
117 	} else {
118 		cmd->base.speed = SPEED_UNKNOWN;
119 		cmd->base.duplex = DUPLEX_UNKNOWN;
120 	}
121 
122 	return 0;
123 }
124 
125 static u32 ixgbevf_get_msglevel(struct net_device *netdev)
126 {
127 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
128 
129 	return adapter->msg_enable;
130 }
131 
132 static void ixgbevf_set_msglevel(struct net_device *netdev, u32 data)
133 {
134 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
135 
136 	adapter->msg_enable = data;
137 }
138 
139 #define IXGBE_GET_STAT(_A_, _R_) (_A_->stats._R_)
140 
141 static int ixgbevf_get_regs_len(struct net_device *netdev)
142 {
143 #define IXGBE_REGS_LEN 45
144 	return IXGBE_REGS_LEN * sizeof(u32);
145 }
146 
147 static void ixgbevf_get_regs(struct net_device *netdev,
148 			     struct ethtool_regs *regs,
149 			     void *p)
150 {
151 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
152 	struct ixgbe_hw *hw = &adapter->hw;
153 	u32 *regs_buff = p;
154 	u32 regs_len = ixgbevf_get_regs_len(netdev);
155 	u8 i;
156 
157 	memset(p, 0, regs_len);
158 
159 	/* generate a number suitable for ethtool's register version */
160 	regs->version = (1u << 24) | (hw->revision_id << 16) | hw->device_id;
161 
162 	/* General Registers */
163 	regs_buff[0] = IXGBE_READ_REG(hw, IXGBE_VFCTRL);
164 	regs_buff[1] = IXGBE_READ_REG(hw, IXGBE_VFSTATUS);
165 	regs_buff[2] = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
166 	regs_buff[3] = IXGBE_READ_REG(hw, IXGBE_VFRXMEMWRAP);
167 	regs_buff[4] = IXGBE_READ_REG(hw, IXGBE_VFFRTIMER);
168 
169 	/* Interrupt */
170 	/* don't read EICR because it can clear interrupt causes, instead
171 	 * read EICS which is a shadow but doesn't clear EICR
172 	 */
173 	regs_buff[5] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
174 	regs_buff[6] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
175 	regs_buff[7] = IXGBE_READ_REG(hw, IXGBE_VTEIMS);
176 	regs_buff[8] = IXGBE_READ_REG(hw, IXGBE_VTEIMC);
177 	regs_buff[9] = IXGBE_READ_REG(hw, IXGBE_VTEIAC);
178 	regs_buff[10] = IXGBE_READ_REG(hw, IXGBE_VTEIAM);
179 	regs_buff[11] = IXGBE_READ_REG(hw, IXGBE_VTEITR(0));
180 	regs_buff[12] = IXGBE_READ_REG(hw, IXGBE_VTIVAR(0));
181 	regs_buff[13] = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
182 
183 	/* Receive DMA */
184 	for (i = 0; i < 2; i++)
185 		regs_buff[14 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAL(i));
186 	for (i = 0; i < 2; i++)
187 		regs_buff[16 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAH(i));
188 	for (i = 0; i < 2; i++)
189 		regs_buff[18 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDLEN(i));
190 	for (i = 0; i < 2; i++)
191 		regs_buff[20 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDH(i));
192 	for (i = 0; i < 2; i++)
193 		regs_buff[22 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDT(i));
194 	for (i = 0; i < 2; i++)
195 		regs_buff[24 + i] = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
196 	for (i = 0; i < 2; i++)
197 		regs_buff[26 + i] = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(i));
198 
199 	/* Receive */
200 	regs_buff[28] = IXGBE_READ_REG(hw, IXGBE_VFPSRTYPE);
201 
202 	/* Transmit */
203 	for (i = 0; i < 2; i++)
204 		regs_buff[29 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAL(i));
205 	for (i = 0; i < 2; i++)
206 		regs_buff[31 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAH(i));
207 	for (i = 0; i < 2; i++)
208 		regs_buff[33 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDLEN(i));
209 	for (i = 0; i < 2; i++)
210 		regs_buff[35 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDH(i));
211 	for (i = 0; i < 2; i++)
212 		regs_buff[37 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDT(i));
213 	for (i = 0; i < 2; i++)
214 		regs_buff[39 + i] = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
215 	for (i = 0; i < 2; i++)
216 		regs_buff[41 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAL(i));
217 	for (i = 0; i < 2; i++)
218 		regs_buff[43 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAH(i));
219 }
220 
221 static void ixgbevf_get_drvinfo(struct net_device *netdev,
222 				struct ethtool_drvinfo *drvinfo)
223 {
224 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
225 
226 	strlcpy(drvinfo->driver, ixgbevf_driver_name, sizeof(drvinfo->driver));
227 	strlcpy(drvinfo->version, ixgbevf_driver_version,
228 		sizeof(drvinfo->version));
229 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
230 		sizeof(drvinfo->bus_info));
231 
232 	drvinfo->n_priv_flags = IXGBEVF_PRIV_FLAGS_STR_LEN;
233 }
234 
235 static void ixgbevf_get_ringparam(struct net_device *netdev,
236 				  struct ethtool_ringparam *ring)
237 {
238 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
239 
240 	ring->rx_max_pending = IXGBEVF_MAX_RXD;
241 	ring->tx_max_pending = IXGBEVF_MAX_TXD;
242 	ring->rx_pending = adapter->rx_ring_count;
243 	ring->tx_pending = adapter->tx_ring_count;
244 }
245 
246 static int ixgbevf_set_ringparam(struct net_device *netdev,
247 				 struct ethtool_ringparam *ring)
248 {
249 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
250 	struct ixgbevf_ring *tx_ring = NULL, *rx_ring = NULL;
251 	u32 new_rx_count, new_tx_count;
252 	int i, j, err = 0;
253 
254 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
255 		return -EINVAL;
256 
257 	new_tx_count = max_t(u32, ring->tx_pending, IXGBEVF_MIN_TXD);
258 	new_tx_count = min_t(u32, new_tx_count, IXGBEVF_MAX_TXD);
259 	new_tx_count = ALIGN(new_tx_count, IXGBE_REQ_TX_DESCRIPTOR_MULTIPLE);
260 
261 	new_rx_count = max_t(u32, ring->rx_pending, IXGBEVF_MIN_RXD);
262 	new_rx_count = min_t(u32, new_rx_count, IXGBEVF_MAX_RXD);
263 	new_rx_count = ALIGN(new_rx_count, IXGBE_REQ_RX_DESCRIPTOR_MULTIPLE);
264 
265 	/* if nothing to do return success */
266 	if ((new_tx_count == adapter->tx_ring_count) &&
267 	    (new_rx_count == adapter->rx_ring_count))
268 		return 0;
269 
270 	while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
271 		usleep_range(1000, 2000);
272 
273 	if (!netif_running(adapter->netdev)) {
274 		for (i = 0; i < adapter->num_tx_queues; i++)
275 			adapter->tx_ring[i]->count = new_tx_count;
276 		for (i = 0; i < adapter->num_xdp_queues; i++)
277 			adapter->xdp_ring[i]->count = new_tx_count;
278 		for (i = 0; i < adapter->num_rx_queues; i++)
279 			adapter->rx_ring[i]->count = new_rx_count;
280 		adapter->tx_ring_count = new_tx_count;
281 		adapter->xdp_ring_count = new_tx_count;
282 		adapter->rx_ring_count = new_rx_count;
283 		goto clear_reset;
284 	}
285 
286 	if (new_tx_count != adapter->tx_ring_count) {
287 		tx_ring = vmalloc(array_size(sizeof(*tx_ring),
288 					     adapter->num_tx_queues +
289 						adapter->num_xdp_queues));
290 		if (!tx_ring) {
291 			err = -ENOMEM;
292 			goto clear_reset;
293 		}
294 
295 		for (i = 0; i < adapter->num_tx_queues; i++) {
296 			/* clone ring and setup updated count */
297 			tx_ring[i] = *adapter->tx_ring[i];
298 			tx_ring[i].count = new_tx_count;
299 			err = ixgbevf_setup_tx_resources(&tx_ring[i]);
300 			if (err) {
301 				while (i) {
302 					i--;
303 					ixgbevf_free_tx_resources(&tx_ring[i]);
304 				}
305 
306 				vfree(tx_ring);
307 				tx_ring = NULL;
308 
309 				goto clear_reset;
310 			}
311 		}
312 
313 		for (j = 0; j < adapter->num_xdp_queues; i++, j++) {
314 			/* clone ring and setup updated count */
315 			tx_ring[i] = *adapter->xdp_ring[j];
316 			tx_ring[i].count = new_tx_count;
317 			err = ixgbevf_setup_tx_resources(&tx_ring[i]);
318 			if (err) {
319 				while (i) {
320 					i--;
321 					ixgbevf_free_tx_resources(&tx_ring[i]);
322 				}
323 
324 				vfree(tx_ring);
325 				tx_ring = NULL;
326 
327 				goto clear_reset;
328 			}
329 		}
330 	}
331 
332 	if (new_rx_count != adapter->rx_ring_count) {
333 		rx_ring = vmalloc(array_size(sizeof(*rx_ring),
334 					     adapter->num_rx_queues));
335 		if (!rx_ring) {
336 			err = -ENOMEM;
337 			goto clear_reset;
338 		}
339 
340 		for (i = 0; i < adapter->num_rx_queues; i++) {
341 			/* clone ring and setup updated count */
342 			rx_ring[i] = *adapter->rx_ring[i];
343 
344 			/* Clear copied XDP RX-queue info */
345 			memset(&rx_ring[i].xdp_rxq, 0,
346 			       sizeof(rx_ring[i].xdp_rxq));
347 
348 			rx_ring[i].count = new_rx_count;
349 			err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
350 			if (err) {
351 				while (i) {
352 					i--;
353 					ixgbevf_free_rx_resources(&rx_ring[i]);
354 				}
355 
356 				vfree(rx_ring);
357 				rx_ring = NULL;
358 
359 				goto clear_reset;
360 			}
361 		}
362 	}
363 
364 	/* bring interface down to prepare for update */
365 	ixgbevf_down(adapter);
366 
367 	/* Tx */
368 	if (tx_ring) {
369 		for (i = 0; i < adapter->num_tx_queues; i++) {
370 			ixgbevf_free_tx_resources(adapter->tx_ring[i]);
371 			*adapter->tx_ring[i] = tx_ring[i];
372 		}
373 		adapter->tx_ring_count = new_tx_count;
374 
375 		for (j = 0; j < adapter->num_xdp_queues; i++, j++) {
376 			ixgbevf_free_tx_resources(adapter->xdp_ring[j]);
377 			*adapter->xdp_ring[j] = tx_ring[i];
378 		}
379 		adapter->xdp_ring_count = new_tx_count;
380 
381 		vfree(tx_ring);
382 		tx_ring = NULL;
383 	}
384 
385 	/* Rx */
386 	if (rx_ring) {
387 		for (i = 0; i < adapter->num_rx_queues; i++) {
388 			ixgbevf_free_rx_resources(adapter->rx_ring[i]);
389 			*adapter->rx_ring[i] = rx_ring[i];
390 		}
391 		adapter->rx_ring_count = new_rx_count;
392 
393 		vfree(rx_ring);
394 		rx_ring = NULL;
395 	}
396 
397 	/* restore interface using new values */
398 	ixgbevf_up(adapter);
399 
400 clear_reset:
401 	/* free Tx resources if Rx error is encountered */
402 	if (tx_ring) {
403 		for (i = 0;
404 		     i < adapter->num_tx_queues + adapter->num_xdp_queues; i++)
405 			ixgbevf_free_tx_resources(&tx_ring[i]);
406 		vfree(tx_ring);
407 	}
408 
409 	clear_bit(__IXGBEVF_RESETTING, &adapter->state);
410 	return err;
411 }
412 
413 static int ixgbevf_get_sset_count(struct net_device *netdev, int stringset)
414 {
415 	switch (stringset) {
416 	case ETH_SS_TEST:
417 		return IXGBEVF_TEST_LEN;
418 	case ETH_SS_STATS:
419 		return IXGBEVF_STATS_LEN;
420 	case ETH_SS_PRIV_FLAGS:
421 		return IXGBEVF_PRIV_FLAGS_STR_LEN;
422 	default:
423 		return -EINVAL;
424 	}
425 }
426 
427 static void ixgbevf_get_ethtool_stats(struct net_device *netdev,
428 				      struct ethtool_stats *stats, u64 *data)
429 {
430 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
431 	struct rtnl_link_stats64 temp;
432 	const struct rtnl_link_stats64 *net_stats;
433 	unsigned int start;
434 	struct ixgbevf_ring *ring;
435 	int i, j;
436 	char *p;
437 
438 	ixgbevf_update_stats(adapter);
439 	net_stats = dev_get_stats(netdev, &temp);
440 	for (i = 0; i < IXGBEVF_GLOBAL_STATS_LEN; i++) {
441 		switch (ixgbevf_gstrings_stats[i].type) {
442 		case NETDEV_STATS:
443 			p = (char *)net_stats +
444 					ixgbevf_gstrings_stats[i].stat_offset;
445 			break;
446 		case IXGBEVF_STATS:
447 			p = (char *)adapter +
448 					ixgbevf_gstrings_stats[i].stat_offset;
449 			break;
450 		default:
451 			data[i] = 0;
452 			continue;
453 		}
454 
455 		data[i] = (ixgbevf_gstrings_stats[i].sizeof_stat ==
456 			   sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
457 	}
458 
459 	/* populate Tx queue data */
460 	for (j = 0; j < adapter->num_tx_queues; j++) {
461 		ring = adapter->tx_ring[j];
462 		if (!ring) {
463 			data[i++] = 0;
464 			data[i++] = 0;
465 			continue;
466 		}
467 
468 		do {
469 			start = u64_stats_fetch_begin_irq(&ring->syncp);
470 			data[i]   = ring->stats.packets;
471 			data[i + 1] = ring->stats.bytes;
472 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
473 		i += 2;
474 	}
475 
476 	/* populate XDP queue data */
477 	for (j = 0; j < adapter->num_xdp_queues; j++) {
478 		ring = adapter->xdp_ring[j];
479 		if (!ring) {
480 			data[i++] = 0;
481 			data[i++] = 0;
482 			continue;
483 		}
484 
485 		do {
486 			start = u64_stats_fetch_begin_irq(&ring->syncp);
487 			data[i] = ring->stats.packets;
488 			data[i + 1] = ring->stats.bytes;
489 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
490 		i += 2;
491 	}
492 
493 	/* populate Rx queue data */
494 	for (j = 0; j < adapter->num_rx_queues; j++) {
495 		ring = adapter->rx_ring[j];
496 		if (!ring) {
497 			data[i++] = 0;
498 			data[i++] = 0;
499 			continue;
500 		}
501 
502 		do {
503 			start = u64_stats_fetch_begin_irq(&ring->syncp);
504 			data[i]   = ring->stats.packets;
505 			data[i + 1] = ring->stats.bytes;
506 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
507 		i += 2;
508 	}
509 }
510 
511 static void ixgbevf_get_strings(struct net_device *netdev, u32 stringset,
512 				u8 *data)
513 {
514 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
515 	char *p = (char *)data;
516 	int i;
517 
518 	switch (stringset) {
519 	case ETH_SS_TEST:
520 		memcpy(data, *ixgbe_gstrings_test,
521 		       IXGBEVF_TEST_LEN * ETH_GSTRING_LEN);
522 		break;
523 	case ETH_SS_STATS:
524 		for (i = 0; i < IXGBEVF_GLOBAL_STATS_LEN; i++) {
525 			memcpy(p, ixgbevf_gstrings_stats[i].stat_string,
526 			       ETH_GSTRING_LEN);
527 			p += ETH_GSTRING_LEN;
528 		}
529 
530 		for (i = 0; i < adapter->num_tx_queues; i++) {
531 			sprintf(p, "tx_queue_%u_packets", i);
532 			p += ETH_GSTRING_LEN;
533 			sprintf(p, "tx_queue_%u_bytes", i);
534 			p += ETH_GSTRING_LEN;
535 		}
536 		for (i = 0; i < adapter->num_xdp_queues; i++) {
537 			sprintf(p, "xdp_queue_%u_packets", i);
538 			p += ETH_GSTRING_LEN;
539 			sprintf(p, "xdp_queue_%u_bytes", i);
540 			p += ETH_GSTRING_LEN;
541 		}
542 		for (i = 0; i < adapter->num_rx_queues; i++) {
543 			sprintf(p, "rx_queue_%u_packets", i);
544 			p += ETH_GSTRING_LEN;
545 			sprintf(p, "rx_queue_%u_bytes", i);
546 			p += ETH_GSTRING_LEN;
547 		}
548 		break;
549 	case ETH_SS_PRIV_FLAGS:
550 		memcpy(data, ixgbevf_priv_flags_strings,
551 		       IXGBEVF_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN);
552 		break;
553 	}
554 }
555 
556 static int ixgbevf_link_test(struct ixgbevf_adapter *adapter, u64 *data)
557 {
558 	struct ixgbe_hw *hw = &adapter->hw;
559 	bool link_up;
560 	u32 link_speed = 0;
561 	*data = 0;
562 
563 	hw->mac.ops.check_link(hw, &link_speed, &link_up, true);
564 	if (!link_up)
565 		*data = 1;
566 
567 	return *data;
568 }
569 
570 /* ethtool register test data */
571 struct ixgbevf_reg_test {
572 	u16 reg;
573 	u8  array_len;
574 	u8  test_type;
575 	u32 mask;
576 	u32 write;
577 };
578 
579 /* In the hardware, registers are laid out either singly, in arrays
580  * spaced 0x40 bytes apart, or in contiguous tables.  We assume
581  * most tests take place on arrays or single registers (handled
582  * as a single-element array) and special-case the tables.
583  * Table tests are always pattern tests.
584  *
585  * We also make provision for some required setup steps by specifying
586  * registers to be written without any read-back testing.
587  */
588 
589 #define PATTERN_TEST	1
590 #define SET_READ_TEST	2
591 #define WRITE_NO_TEST	3
592 #define TABLE32_TEST	4
593 #define TABLE64_TEST_LO	5
594 #define TABLE64_TEST_HI	6
595 
596 /* default VF register test */
597 static const struct ixgbevf_reg_test reg_test_vf[] = {
598 	{ IXGBE_VFRDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFF80 },
599 	{ IXGBE_VFRDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
600 	{ IXGBE_VFRDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFFFF },
601 	{ IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, IXGBE_RXDCTL_ENABLE },
602 	{ IXGBE_VFRDT(0), 2, PATTERN_TEST, 0x0000FFFF, 0x0000FFFF },
603 	{ IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, 0 },
604 	{ IXGBE_VFTDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFFFF },
605 	{ IXGBE_VFTDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
606 	{ IXGBE_VFTDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFF80 },
607 	{ .reg = 0 }
608 };
609 
610 static const u32 register_test_patterns[] = {
611 	0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
612 };
613 
614 static bool reg_pattern_test(struct ixgbevf_adapter *adapter, u64 *data,
615 			     int reg, u32 mask, u32 write)
616 {
617 	u32 pat, val, before;
618 
619 	if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
620 		*data = 1;
621 		return true;
622 	}
623 	for (pat = 0; pat < ARRAY_SIZE(register_test_patterns); pat++) {
624 		before = ixgbevf_read_reg(&adapter->hw, reg);
625 		ixgbe_write_reg(&adapter->hw, reg,
626 				register_test_patterns[pat] & write);
627 		val = ixgbevf_read_reg(&adapter->hw, reg);
628 		if (val != (register_test_patterns[pat] & write & mask)) {
629 			hw_dbg(&adapter->hw,
630 			       "pattern test reg %04X failed: got 0x%08X expected 0x%08X\n",
631 			       reg, val,
632 			       register_test_patterns[pat] & write & mask);
633 			*data = reg;
634 			ixgbe_write_reg(&adapter->hw, reg, before);
635 			return true;
636 		}
637 		ixgbe_write_reg(&adapter->hw, reg, before);
638 	}
639 	return false;
640 }
641 
642 static bool reg_set_and_check(struct ixgbevf_adapter *adapter, u64 *data,
643 			      int reg, u32 mask, u32 write)
644 {
645 	u32 val, before;
646 
647 	if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
648 		*data = 1;
649 		return true;
650 	}
651 	before = ixgbevf_read_reg(&adapter->hw, reg);
652 	ixgbe_write_reg(&adapter->hw, reg, write & mask);
653 	val = ixgbevf_read_reg(&adapter->hw, reg);
654 	if ((write & mask) != (val & mask)) {
655 		pr_err("set/check reg %04X test failed: got 0x%08X expected 0x%08X\n",
656 		       reg, (val & mask), write & mask);
657 		*data = reg;
658 		ixgbe_write_reg(&adapter->hw, reg, before);
659 		return true;
660 	}
661 	ixgbe_write_reg(&adapter->hw, reg, before);
662 	return false;
663 }
664 
665 static int ixgbevf_reg_test(struct ixgbevf_adapter *adapter, u64 *data)
666 {
667 	const struct ixgbevf_reg_test *test;
668 	u32 i;
669 
670 	if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
671 		dev_err(&adapter->pdev->dev,
672 			"Adapter removed - register test blocked\n");
673 		*data = 1;
674 		return 1;
675 	}
676 	test = reg_test_vf;
677 
678 	/* Perform the register test, looping through the test table
679 	 * until we either fail or reach the null entry.
680 	 */
681 	while (test->reg) {
682 		for (i = 0; i < test->array_len; i++) {
683 			bool b = false;
684 
685 			switch (test->test_type) {
686 			case PATTERN_TEST:
687 				b = reg_pattern_test(adapter, data,
688 						     test->reg + (i * 0x40),
689 						     test->mask,
690 						     test->write);
691 				break;
692 			case SET_READ_TEST:
693 				b = reg_set_and_check(adapter, data,
694 						      test->reg + (i * 0x40),
695 						      test->mask,
696 						      test->write);
697 				break;
698 			case WRITE_NO_TEST:
699 				ixgbe_write_reg(&adapter->hw,
700 						test->reg + (i * 0x40),
701 						test->write);
702 				break;
703 			case TABLE32_TEST:
704 				b = reg_pattern_test(adapter, data,
705 						     test->reg + (i * 4),
706 						     test->mask,
707 						     test->write);
708 				break;
709 			case TABLE64_TEST_LO:
710 				b = reg_pattern_test(adapter, data,
711 						     test->reg + (i * 8),
712 						     test->mask,
713 						     test->write);
714 				break;
715 			case TABLE64_TEST_HI:
716 				b = reg_pattern_test(adapter, data,
717 						     test->reg + 4 + (i * 8),
718 						     test->mask,
719 						     test->write);
720 				break;
721 			}
722 			if (b)
723 				return 1;
724 		}
725 		test++;
726 	}
727 
728 	*data = 0;
729 	return *data;
730 }
731 
732 static void ixgbevf_diag_test(struct net_device *netdev,
733 			      struct ethtool_test *eth_test, u64 *data)
734 {
735 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
736 	bool if_running = netif_running(netdev);
737 
738 	if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
739 		dev_err(&adapter->pdev->dev,
740 			"Adapter removed - test blocked\n");
741 		data[0] = 1;
742 		data[1] = 1;
743 		eth_test->flags |= ETH_TEST_FL_FAILED;
744 		return;
745 	}
746 	set_bit(__IXGBEVF_TESTING, &adapter->state);
747 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
748 		/* Offline tests */
749 
750 		hw_dbg(&adapter->hw, "offline testing starting\n");
751 
752 		/* Link test performed before hardware reset so autoneg doesn't
753 		 * interfere with test result
754 		 */
755 		if (ixgbevf_link_test(adapter, &data[1]))
756 			eth_test->flags |= ETH_TEST_FL_FAILED;
757 
758 		if (if_running)
759 			/* indicate we're in test mode */
760 			ixgbevf_close(netdev);
761 		else
762 			ixgbevf_reset(adapter);
763 
764 		hw_dbg(&adapter->hw, "register testing starting\n");
765 		if (ixgbevf_reg_test(adapter, &data[0]))
766 			eth_test->flags |= ETH_TEST_FL_FAILED;
767 
768 		ixgbevf_reset(adapter);
769 
770 		clear_bit(__IXGBEVF_TESTING, &adapter->state);
771 		if (if_running)
772 			ixgbevf_open(netdev);
773 	} else {
774 		hw_dbg(&adapter->hw, "online testing starting\n");
775 		/* Online tests */
776 		if (ixgbevf_link_test(adapter, &data[1]))
777 			eth_test->flags |= ETH_TEST_FL_FAILED;
778 
779 		/* Online tests aren't run; pass by default */
780 		data[0] = 0;
781 
782 		clear_bit(__IXGBEVF_TESTING, &adapter->state);
783 	}
784 	msleep_interruptible(4 * 1000);
785 }
786 
787 static int ixgbevf_nway_reset(struct net_device *netdev)
788 {
789 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
790 
791 	if (netif_running(netdev))
792 		ixgbevf_reinit_locked(adapter);
793 
794 	return 0;
795 }
796 
797 static int ixgbevf_get_coalesce(struct net_device *netdev,
798 				struct ethtool_coalesce *ec)
799 {
800 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
801 
802 	/* only valid if in constant ITR mode */
803 	if (adapter->rx_itr_setting <= 1)
804 		ec->rx_coalesce_usecs = adapter->rx_itr_setting;
805 	else
806 		ec->rx_coalesce_usecs = adapter->rx_itr_setting >> 2;
807 
808 	/* if in mixed Tx/Rx queues per vector mode, report only Rx settings */
809 	if (adapter->q_vector[0]->tx.count && adapter->q_vector[0]->rx.count)
810 		return 0;
811 
812 	/* only valid if in constant ITR mode */
813 	if (adapter->tx_itr_setting <= 1)
814 		ec->tx_coalesce_usecs = adapter->tx_itr_setting;
815 	else
816 		ec->tx_coalesce_usecs = adapter->tx_itr_setting >> 2;
817 
818 	return 0;
819 }
820 
821 static int ixgbevf_set_coalesce(struct net_device *netdev,
822 				struct ethtool_coalesce *ec)
823 {
824 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
825 	struct ixgbevf_q_vector *q_vector;
826 	int num_vectors, i;
827 	u16 tx_itr_param, rx_itr_param;
828 
829 	/* don't accept Tx specific changes if we've got mixed RxTx vectors */
830 	if (adapter->q_vector[0]->tx.count &&
831 	    adapter->q_vector[0]->rx.count && ec->tx_coalesce_usecs)
832 		return -EINVAL;
833 
834 	if ((ec->rx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)) ||
835 	    (ec->tx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)))
836 		return -EINVAL;
837 
838 	if (ec->rx_coalesce_usecs > 1)
839 		adapter->rx_itr_setting = ec->rx_coalesce_usecs << 2;
840 	else
841 		adapter->rx_itr_setting = ec->rx_coalesce_usecs;
842 
843 	if (adapter->rx_itr_setting == 1)
844 		rx_itr_param = IXGBE_20K_ITR;
845 	else
846 		rx_itr_param = adapter->rx_itr_setting;
847 
848 	if (ec->tx_coalesce_usecs > 1)
849 		adapter->tx_itr_setting = ec->tx_coalesce_usecs << 2;
850 	else
851 		adapter->tx_itr_setting = ec->tx_coalesce_usecs;
852 
853 	if (adapter->tx_itr_setting == 1)
854 		tx_itr_param = IXGBE_12K_ITR;
855 	else
856 		tx_itr_param = adapter->tx_itr_setting;
857 
858 	num_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
859 
860 	for (i = 0; i < num_vectors; i++) {
861 		q_vector = adapter->q_vector[i];
862 		if (q_vector->tx.count && !q_vector->rx.count)
863 			/* Tx only */
864 			q_vector->itr = tx_itr_param;
865 		else
866 			/* Rx only or mixed */
867 			q_vector->itr = rx_itr_param;
868 		ixgbevf_write_eitr(q_vector);
869 	}
870 
871 	return 0;
872 }
873 
874 static int ixgbevf_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
875 			     u32 *rules __always_unused)
876 {
877 	struct ixgbevf_adapter *adapter = netdev_priv(dev);
878 
879 	switch (info->cmd) {
880 	case ETHTOOL_GRXRINGS:
881 		info->data = adapter->num_rx_queues;
882 		return 0;
883 	default:
884 		hw_dbg(&adapter->hw, "Command parameters not supported\n");
885 		return -EOPNOTSUPP;
886 	}
887 }
888 
889 static u32 ixgbevf_get_rxfh_indir_size(struct net_device *netdev)
890 {
891 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
892 
893 	if (adapter->hw.mac.type >= ixgbe_mac_X550_vf)
894 		return IXGBEVF_X550_VFRETA_SIZE;
895 
896 	return IXGBEVF_82599_RETA_SIZE;
897 }
898 
899 static u32 ixgbevf_get_rxfh_key_size(struct net_device *netdev)
900 {
901 	return IXGBEVF_RSS_HASH_KEY_SIZE;
902 }
903 
904 static int ixgbevf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
905 			    u8 *hfunc)
906 {
907 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
908 	int err = 0;
909 
910 	if (hfunc)
911 		*hfunc = ETH_RSS_HASH_TOP;
912 
913 	if (adapter->hw.mac.type >= ixgbe_mac_X550_vf) {
914 		if (key)
915 			memcpy(key, adapter->rss_key,
916 			       ixgbevf_get_rxfh_key_size(netdev));
917 
918 		if (indir) {
919 			int i;
920 
921 			for (i = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++)
922 				indir[i] = adapter->rss_indir_tbl[i];
923 		}
924 	} else {
925 		/* If neither indirection table nor hash key was requested
926 		 *  - just return a success avoiding taking any locks.
927 		 */
928 		if (!indir && !key)
929 			return 0;
930 
931 		spin_lock_bh(&adapter->mbx_lock);
932 		if (indir)
933 			err = ixgbevf_get_reta_locked(&adapter->hw, indir,
934 						      adapter->num_rx_queues);
935 
936 		if (!err && key)
937 			err = ixgbevf_get_rss_key_locked(&adapter->hw, key);
938 
939 		spin_unlock_bh(&adapter->mbx_lock);
940 	}
941 
942 	return err;
943 }
944 
945 static u32 ixgbevf_get_priv_flags(struct net_device *netdev)
946 {
947 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
948 	u32 priv_flags = 0;
949 
950 	if (adapter->flags & IXGBEVF_FLAGS_LEGACY_RX)
951 		priv_flags |= IXGBEVF_PRIV_FLAGS_LEGACY_RX;
952 
953 	return priv_flags;
954 }
955 
956 static int ixgbevf_set_priv_flags(struct net_device *netdev, u32 priv_flags)
957 {
958 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
959 	unsigned int flags = adapter->flags;
960 
961 	flags &= ~IXGBEVF_FLAGS_LEGACY_RX;
962 	if (priv_flags & IXGBEVF_PRIV_FLAGS_LEGACY_RX)
963 		flags |= IXGBEVF_FLAGS_LEGACY_RX;
964 
965 	if (flags != adapter->flags) {
966 		adapter->flags = flags;
967 
968 		/* reset interface to repopulate queues */
969 		if (netif_running(netdev))
970 			ixgbevf_reinit_locked(adapter);
971 	}
972 
973 	return 0;
974 }
975 
976 static const struct ethtool_ops ixgbevf_ethtool_ops = {
977 	.get_drvinfo		= ixgbevf_get_drvinfo,
978 	.get_regs_len		= ixgbevf_get_regs_len,
979 	.get_regs		= ixgbevf_get_regs,
980 	.nway_reset		= ixgbevf_nway_reset,
981 	.get_link		= ethtool_op_get_link,
982 	.get_ringparam		= ixgbevf_get_ringparam,
983 	.set_ringparam		= ixgbevf_set_ringparam,
984 	.get_msglevel		= ixgbevf_get_msglevel,
985 	.set_msglevel		= ixgbevf_set_msglevel,
986 	.self_test		= ixgbevf_diag_test,
987 	.get_sset_count		= ixgbevf_get_sset_count,
988 	.get_strings		= ixgbevf_get_strings,
989 	.get_ethtool_stats	= ixgbevf_get_ethtool_stats,
990 	.get_coalesce		= ixgbevf_get_coalesce,
991 	.set_coalesce		= ixgbevf_set_coalesce,
992 	.get_rxnfc		= ixgbevf_get_rxnfc,
993 	.get_rxfh_indir_size	= ixgbevf_get_rxfh_indir_size,
994 	.get_rxfh_key_size	= ixgbevf_get_rxfh_key_size,
995 	.get_rxfh		= ixgbevf_get_rxfh,
996 	.get_link_ksettings	= ixgbevf_get_link_ksettings,
997 	.get_priv_flags		= ixgbevf_get_priv_flags,
998 	.set_priv_flags		= ixgbevf_set_priv_flags,
999 };
1000 
1001 void ixgbevf_set_ethtool_ops(struct net_device *netdev)
1002 {
1003 	netdev->ethtool_ops = &ixgbevf_ethtool_ops;
1004 }
1005