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