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