1 /*******************************************************************************
2 
3   Intel 82599 Virtual Function driver
4   Copyright(c) 1999 - 2012 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, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21 
22   Contact Information:
23   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 
26 *******************************************************************************/
27 
28 /* ethtool support for ixgbevf */
29 
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 
32 #include <linux/types.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/pci.h>
36 #include <linux/netdevice.h>
37 #include <linux/ethtool.h>
38 #include <linux/vmalloc.h>
39 #include <linux/if_vlan.h>
40 #include <linux/uaccess.h>
41 
42 #include "ixgbevf.h"
43 
44 #define IXGBE_ALL_RAR_ENTRIES 16
45 
46 struct ixgbe_stats {
47 	char stat_string[ETH_GSTRING_LEN];
48 	struct {
49 		int sizeof_stat;
50 		int stat_offset;
51 		int base_stat_offset;
52 		int saved_reset_offset;
53 	};
54 };
55 
56 #define IXGBEVF_STAT(m, b, r) { \
57 	.sizeof_stat = FIELD_SIZEOF(struct ixgbevf_adapter, m), \
58 	.stat_offset = offsetof(struct ixgbevf_adapter, m), \
59 	.base_stat_offset = offsetof(struct ixgbevf_adapter, b), \
60 	.saved_reset_offset = offsetof(struct ixgbevf_adapter, r) \
61 }
62 
63 #define IXGBEVF_ZSTAT(m) { \
64 	.sizeof_stat = FIELD_SIZEOF(struct ixgbevf_adapter, m), \
65 	.stat_offset = offsetof(struct ixgbevf_adapter, m), \
66 	.base_stat_offset = -1, \
67 	.saved_reset_offset = -1 \
68 }
69 
70 static const struct ixgbe_stats ixgbe_gstrings_stats[] = {
71 	{"rx_packets", IXGBEVF_STAT(stats.vfgprc, stats.base_vfgprc,
72 				    stats.saved_reset_vfgprc)},
73 	{"tx_packets", IXGBEVF_STAT(stats.vfgptc, stats.base_vfgptc,
74 				    stats.saved_reset_vfgptc)},
75 	{"rx_bytes", IXGBEVF_STAT(stats.vfgorc, stats.base_vfgorc,
76 				  stats.saved_reset_vfgorc)},
77 	{"tx_bytes", IXGBEVF_STAT(stats.vfgotc, stats.base_vfgotc,
78 				  stats.saved_reset_vfgotc)},
79 	{"tx_busy", IXGBEVF_ZSTAT(tx_busy)},
80 	{"multicast", IXGBEVF_STAT(stats.vfmprc, stats.base_vfmprc,
81 				   stats.saved_reset_vfmprc)},
82 	{"rx_csum_offload_good", IXGBEVF_ZSTAT(hw_csum_rx_good)},
83 	{"rx_csum_offload_errors", IXGBEVF_ZSTAT(hw_csum_rx_error)},
84 	{"tx_csum_offload_ctxt", IXGBEVF_ZSTAT(hw_csum_tx_good)},
85 #ifdef BP_EXTENDED_STATS
86 	{"rx_bp_poll_yield", IXGBEVF_ZSTAT(bp_rx_yields)},
87 	{"rx_bp_cleaned", IXGBEVF_ZSTAT(bp_rx_cleaned)},
88 	{"rx_bp_misses", IXGBEVF_ZSTAT(bp_rx_missed)},
89 	{"tx_bp_napi_yield", IXGBEVF_ZSTAT(bp_tx_yields)},
90 	{"tx_bp_cleaned", IXGBEVF_ZSTAT(bp_tx_cleaned)},
91 	{"tx_bp_misses", IXGBEVF_ZSTAT(bp_tx_missed)},
92 #endif
93 };
94 
95 #define IXGBE_QUEUE_STATS_LEN 0
96 #define IXGBE_GLOBAL_STATS_LEN	ARRAY_SIZE(ixgbe_gstrings_stats)
97 
98 #define IXGBEVF_STATS_LEN (IXGBE_GLOBAL_STATS_LEN + IXGBE_QUEUE_STATS_LEN)
99 static const char ixgbe_gstrings_test[][ETH_GSTRING_LEN] = {
100 	"Register test  (offline)",
101 	"Link test   (on/offline)"
102 };
103 #define IXGBE_TEST_LEN (sizeof(ixgbe_gstrings_test) / ETH_GSTRING_LEN)
104 
105 static int ixgbevf_get_settings(struct net_device *netdev,
106 				struct ethtool_cmd *ecmd)
107 {
108 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
109 	struct ixgbe_hw *hw = &adapter->hw;
110 	u32 link_speed = 0;
111 	bool link_up;
112 
113 	ecmd->supported = SUPPORTED_10000baseT_Full;
114 	ecmd->autoneg = AUTONEG_DISABLE;
115 	ecmd->transceiver = XCVR_DUMMY1;
116 	ecmd->port = -1;
117 
118 	hw->mac.get_link_status = 1;
119 	hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
120 
121 	if (link_up) {
122 		__u32 speed = SPEED_10000;
123 		switch (link_speed) {
124 		case IXGBE_LINK_SPEED_10GB_FULL:
125 			speed = SPEED_10000;
126 			break;
127 		case IXGBE_LINK_SPEED_1GB_FULL:
128 			speed = SPEED_1000;
129 			break;
130 		case IXGBE_LINK_SPEED_100_FULL:
131 			speed = SPEED_100;
132 			break;
133 		}
134 
135 		ethtool_cmd_speed_set(ecmd, speed);
136 		ecmd->duplex = DUPLEX_FULL;
137 	} else {
138 		ethtool_cmd_speed_set(ecmd, -1);
139 		ecmd->duplex = -1;
140 	}
141 
142 	return 0;
143 }
144 
145 static u32 ixgbevf_get_msglevel(struct net_device *netdev)
146 {
147 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
148 	return adapter->msg_enable;
149 }
150 
151 static void ixgbevf_set_msglevel(struct net_device *netdev, u32 data)
152 {
153 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
154 	adapter->msg_enable = data;
155 }
156 
157 #define IXGBE_GET_STAT(_A_, _R_) (_A_->stats._R_)
158 
159 static int ixgbevf_get_regs_len(struct net_device *netdev)
160 {
161 #define IXGBE_REGS_LEN 45
162 	return IXGBE_REGS_LEN * sizeof(u32);
163 }
164 
165 static void ixgbevf_get_regs(struct net_device *netdev,
166 			     struct ethtool_regs *regs,
167 			     void *p)
168 {
169 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
170 	struct ixgbe_hw *hw = &adapter->hw;
171 	u32 *regs_buff = p;
172 	u32 regs_len = ixgbevf_get_regs_len(netdev);
173 	u8 i;
174 
175 	memset(p, 0, regs_len);
176 
177 	regs->version = (1 << 24) | hw->revision_id << 16 | hw->device_id;
178 
179 	/* General Registers */
180 	regs_buff[0] = IXGBE_READ_REG(hw, IXGBE_VFCTRL);
181 	regs_buff[1] = IXGBE_READ_REG(hw, IXGBE_VFSTATUS);
182 	regs_buff[2] = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
183 	regs_buff[3] = IXGBE_READ_REG(hw, IXGBE_VFRXMEMWRAP);
184 	regs_buff[4] = IXGBE_READ_REG(hw, IXGBE_VFFRTIMER);
185 
186 	/* Interrupt */
187 	/* don't read EICR because it can clear interrupt causes, instead
188 	 * read EICS which is a shadow but doesn't clear EICR */
189 	regs_buff[5] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
190 	regs_buff[6] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
191 	regs_buff[7] = IXGBE_READ_REG(hw, IXGBE_VTEIMS);
192 	regs_buff[8] = IXGBE_READ_REG(hw, IXGBE_VTEIMC);
193 	regs_buff[9] = IXGBE_READ_REG(hw, IXGBE_VTEIAC);
194 	regs_buff[10] = IXGBE_READ_REG(hw, IXGBE_VTEIAM);
195 	regs_buff[11] = IXGBE_READ_REG(hw, IXGBE_VTEITR(0));
196 	regs_buff[12] = IXGBE_READ_REG(hw, IXGBE_VTIVAR(0));
197 	regs_buff[13] = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
198 
199 	/* Receive DMA */
200 	for (i = 0; i < 2; i++)
201 		regs_buff[14 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAL(i));
202 	for (i = 0; i < 2; i++)
203 		regs_buff[16 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAH(i));
204 	for (i = 0; i < 2; i++)
205 		regs_buff[18 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDLEN(i));
206 	for (i = 0; i < 2; i++)
207 		regs_buff[20 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDH(i));
208 	for (i = 0; i < 2; i++)
209 		regs_buff[22 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDT(i));
210 	for (i = 0; i < 2; i++)
211 		regs_buff[24 + i] = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
212 	for (i = 0; i < 2; i++)
213 		regs_buff[26 + i] = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(i));
214 
215 	/* Receive */
216 	regs_buff[28] = IXGBE_READ_REG(hw, IXGBE_VFPSRTYPE);
217 
218 	/* Transmit */
219 	for (i = 0; i < 2; i++)
220 		regs_buff[29 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAL(i));
221 	for (i = 0; i < 2; i++)
222 		regs_buff[31 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAH(i));
223 	for (i = 0; i < 2; i++)
224 		regs_buff[33 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDLEN(i));
225 	for (i = 0; i < 2; i++)
226 		regs_buff[35 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDH(i));
227 	for (i = 0; i < 2; i++)
228 		regs_buff[37 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDT(i));
229 	for (i = 0; i < 2; i++)
230 		regs_buff[39 + i] = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
231 	for (i = 0; i < 2; i++)
232 		regs_buff[41 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAL(i));
233 	for (i = 0; i < 2; i++)
234 		regs_buff[43 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAH(i));
235 }
236 
237 static void ixgbevf_get_drvinfo(struct net_device *netdev,
238 				struct ethtool_drvinfo *drvinfo)
239 {
240 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
241 
242 	strlcpy(drvinfo->driver, ixgbevf_driver_name, sizeof(drvinfo->driver));
243 	strlcpy(drvinfo->version, ixgbevf_driver_version,
244 		sizeof(drvinfo->version));
245 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
246 		sizeof(drvinfo->bus_info));
247 }
248 
249 static void ixgbevf_get_ringparam(struct net_device *netdev,
250 				  struct ethtool_ringparam *ring)
251 {
252 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
253 
254 	ring->rx_max_pending = IXGBEVF_MAX_RXD;
255 	ring->tx_max_pending = IXGBEVF_MAX_TXD;
256 	ring->rx_pending = adapter->rx_ring_count;
257 	ring->tx_pending = adapter->tx_ring_count;
258 }
259 
260 static int ixgbevf_set_ringparam(struct net_device *netdev,
261 				 struct ethtool_ringparam *ring)
262 {
263 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
264 	struct ixgbevf_ring *tx_ring = NULL, *rx_ring = NULL;
265 	u32 new_rx_count, new_tx_count;
266 	int i, err = 0;
267 
268 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
269 		return -EINVAL;
270 
271 	new_tx_count = max_t(u32, ring->tx_pending, IXGBEVF_MIN_TXD);
272 	new_tx_count = min_t(u32, new_tx_count, IXGBEVF_MAX_TXD);
273 	new_tx_count = ALIGN(new_tx_count, IXGBE_REQ_TX_DESCRIPTOR_MULTIPLE);
274 
275 	new_rx_count = max_t(u32, ring->rx_pending, IXGBEVF_MIN_RXD);
276 	new_rx_count = min_t(u32, new_rx_count, IXGBEVF_MAX_RXD);
277 	new_rx_count = ALIGN(new_rx_count, IXGBE_REQ_RX_DESCRIPTOR_MULTIPLE);
278 
279 	/* if nothing to do return success */
280 	if ((new_tx_count == adapter->tx_ring_count) &&
281 	    (new_rx_count == adapter->rx_ring_count))
282 		return 0;
283 
284 	while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
285 		usleep_range(1000, 2000);
286 
287 	if (!netif_running(adapter->netdev)) {
288 		for (i = 0; i < adapter->num_tx_queues; i++)
289 			adapter->tx_ring[i].count = new_tx_count;
290 		for (i = 0; i < adapter->num_rx_queues; i++)
291 			adapter->rx_ring[i].count = new_rx_count;
292 		adapter->tx_ring_count = new_tx_count;
293 		adapter->rx_ring_count = new_rx_count;
294 		goto clear_reset;
295 	}
296 
297 	if (new_tx_count != adapter->tx_ring_count) {
298 		tx_ring = vmalloc(adapter->num_tx_queues * sizeof(*tx_ring));
299 		if (!tx_ring) {
300 			err = -ENOMEM;
301 			goto clear_reset;
302 		}
303 
304 		for (i = 0; i < adapter->num_tx_queues; i++) {
305 			/* clone ring and setup updated count */
306 			tx_ring[i] = adapter->tx_ring[i];
307 			tx_ring[i].count = new_tx_count;
308 			err = ixgbevf_setup_tx_resources(adapter, &tx_ring[i]);
309 			if (!err)
310 				continue;
311 			while (i) {
312 				i--;
313 				ixgbevf_free_tx_resources(adapter, &tx_ring[i]);
314 			}
315 
316 			vfree(tx_ring);
317 			tx_ring = NULL;
318 
319 			goto clear_reset;
320 		}
321 	}
322 
323 	if (new_rx_count != adapter->rx_ring_count) {
324 		rx_ring = vmalloc(adapter->num_rx_queues * sizeof(*rx_ring));
325 		if (!rx_ring) {
326 			err = -ENOMEM;
327 			goto clear_reset;
328 		}
329 
330 		for (i = 0; i < adapter->num_rx_queues; i++) {
331 			/* clone ring and setup updated count */
332 			rx_ring[i] = adapter->rx_ring[i];
333 			rx_ring[i].count = new_rx_count;
334 			err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
335 			if (!err)
336 				continue;
337 			while (i) {
338 				i--;
339 				ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
340 			}
341 
342 			vfree(rx_ring);
343 			rx_ring = NULL;
344 
345 			goto clear_reset;
346 		}
347 	}
348 
349 	/* bring interface down to prepare for update */
350 	ixgbevf_down(adapter);
351 
352 	/* Tx */
353 	if (tx_ring) {
354 		for (i = 0; i < adapter->num_tx_queues; i++) {
355 			ixgbevf_free_tx_resources(adapter,
356 						  &adapter->tx_ring[i]);
357 			adapter->tx_ring[i] = tx_ring[i];
358 		}
359 		adapter->tx_ring_count = new_tx_count;
360 
361 		vfree(tx_ring);
362 		tx_ring = NULL;
363 	}
364 
365 	/* Rx */
366 	if (rx_ring) {
367 		for (i = 0; i < adapter->num_rx_queues; i++) {
368 			ixgbevf_free_rx_resources(adapter,
369 						  &adapter->rx_ring[i]);
370 			adapter->rx_ring[i] = rx_ring[i];
371 		}
372 		adapter->rx_ring_count = new_rx_count;
373 
374 		vfree(rx_ring);
375 		rx_ring = NULL;
376 	}
377 
378 	/* restore interface using new values */
379 	ixgbevf_up(adapter);
380 
381 clear_reset:
382 	/* free Tx resources if Rx error is encountered */
383 	if (tx_ring) {
384 		for (i = 0; i < adapter->num_tx_queues; i++)
385 			ixgbevf_free_tx_resources(adapter, &tx_ring[i]);
386 		vfree(tx_ring);
387 	}
388 
389 	clear_bit(__IXGBEVF_RESETTING, &adapter->state);
390 	return err;
391 }
392 
393 static int ixgbevf_get_sset_count(struct net_device *dev, int stringset)
394 {
395        switch (stringset) {
396        case ETH_SS_TEST:
397 	       return IXGBE_TEST_LEN;
398        case ETH_SS_STATS:
399 	       return IXGBE_GLOBAL_STATS_LEN;
400        default:
401 	       return -EINVAL;
402        }
403 }
404 
405 static void ixgbevf_get_ethtool_stats(struct net_device *netdev,
406 				      struct ethtool_stats *stats, u64 *data)
407 {
408 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
409 	char *base = (char *) adapter;
410 	int i;
411 #ifdef BP_EXTENDED_STATS
412 	u64 rx_yields = 0, rx_cleaned = 0, rx_missed = 0,
413 	    tx_yields = 0, tx_cleaned = 0, tx_missed = 0;
414 
415 	for (i = 0; i < adapter->num_rx_queues; i++) {
416 		rx_yields += adapter->rx_ring[i].bp_yields;
417 		rx_cleaned += adapter->rx_ring[i].bp_cleaned;
418 		rx_yields += adapter->rx_ring[i].bp_yields;
419 	}
420 
421 	for (i = 0; i < adapter->num_tx_queues; i++) {
422 		tx_yields += adapter->tx_ring[i].bp_yields;
423 		tx_cleaned += adapter->tx_ring[i].bp_cleaned;
424 		tx_yields += adapter->tx_ring[i].bp_yields;
425 	}
426 
427 	adapter->bp_rx_yields = rx_yields;
428 	adapter->bp_rx_cleaned = rx_cleaned;
429 	adapter->bp_rx_missed = rx_missed;
430 
431 	adapter->bp_tx_yields = tx_yields;
432 	adapter->bp_tx_cleaned = tx_cleaned;
433 	adapter->bp_tx_missed = tx_missed;
434 #endif
435 
436 	ixgbevf_update_stats(adapter);
437 	for (i = 0; i < IXGBE_GLOBAL_STATS_LEN; i++) {
438 		char *p = base + ixgbe_gstrings_stats[i].stat_offset;
439 		char *b = base + ixgbe_gstrings_stats[i].base_stat_offset;
440 		char *r = base + ixgbe_gstrings_stats[i].saved_reset_offset;
441 
442 		if (ixgbe_gstrings_stats[i].sizeof_stat == sizeof(u64)) {
443 			if (ixgbe_gstrings_stats[i].base_stat_offset >= 0)
444 				data[i] = *(u64 *)p - *(u64 *)b + *(u64 *)r;
445 			else
446 				data[i] = *(u64 *)p;
447 		} else {
448 			if (ixgbe_gstrings_stats[i].base_stat_offset >= 0)
449 				data[i] = *(u32 *)p - *(u32 *)b + *(u32 *)r;
450 			else
451 				data[i] = *(u32 *)p;
452 		}
453 	}
454 }
455 
456 static void ixgbevf_get_strings(struct net_device *netdev, u32 stringset,
457 				u8 *data)
458 {
459 	char *p = (char *)data;
460 	int i;
461 
462 	switch (stringset) {
463 	case ETH_SS_TEST:
464 		memcpy(data, *ixgbe_gstrings_test,
465 		       IXGBE_TEST_LEN * ETH_GSTRING_LEN);
466 		break;
467 	case ETH_SS_STATS:
468 		for (i = 0; i < IXGBE_GLOBAL_STATS_LEN; i++) {
469 			memcpy(p, ixgbe_gstrings_stats[i].stat_string,
470 			       ETH_GSTRING_LEN);
471 			p += ETH_GSTRING_LEN;
472 		}
473 		break;
474 	}
475 }
476 
477 static int ixgbevf_link_test(struct ixgbevf_adapter *adapter, u64 *data)
478 {
479 	struct ixgbe_hw *hw = &adapter->hw;
480 	bool link_up;
481 	u32 link_speed = 0;
482 	*data = 0;
483 
484 	hw->mac.ops.check_link(hw, &link_speed, &link_up, true);
485 	if (!link_up)
486 		*data = 1;
487 
488 	return *data;
489 }
490 
491 /* ethtool register test data */
492 struct ixgbevf_reg_test {
493 	u16 reg;
494 	u8  array_len;
495 	u8  test_type;
496 	u32 mask;
497 	u32 write;
498 };
499 
500 /* In the hardware, registers are laid out either singly, in arrays
501  * spaced 0x40 bytes apart, or in contiguous tables.  We assume
502  * most tests take place on arrays or single registers (handled
503  * as a single-element array) and special-case the tables.
504  * Table tests are always pattern tests.
505  *
506  * We also make provision for some required setup steps by specifying
507  * registers to be written without any read-back testing.
508  */
509 
510 #define PATTERN_TEST	1
511 #define SET_READ_TEST	2
512 #define WRITE_NO_TEST	3
513 #define TABLE32_TEST	4
514 #define TABLE64_TEST_LO	5
515 #define TABLE64_TEST_HI	6
516 
517 /* default VF register test */
518 static const struct ixgbevf_reg_test reg_test_vf[] = {
519 	{ IXGBE_VFRDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFF80 },
520 	{ IXGBE_VFRDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
521 	{ IXGBE_VFRDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFFFF },
522 	{ IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, IXGBE_RXDCTL_ENABLE },
523 	{ IXGBE_VFRDT(0), 2, PATTERN_TEST, 0x0000FFFF, 0x0000FFFF },
524 	{ IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, 0 },
525 	{ IXGBE_VFTDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFFFF },
526 	{ IXGBE_VFTDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
527 	{ IXGBE_VFTDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFF80 },
528 	{ 0, 0, 0, 0 }
529 };
530 
531 static const u32 register_test_patterns[] = {
532 	0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
533 };
534 
535 #define REG_PATTERN_TEST(R, M, W)                                             \
536 {                                                                             \
537 	u32 pat, val, before;                                                 \
538 	for (pat = 0; pat < ARRAY_SIZE(register_test_patterns); pat++) {      \
539 		before = readl(adapter->hw.hw_addr + R);                      \
540 		writel((register_test_patterns[pat] & W),                     \
541 		       (adapter->hw.hw_addr + R));                            \
542 		val = readl(adapter->hw.hw_addr + R);                         \
543 		if (val != (register_test_patterns[pat] & W & M)) {           \
544 			hw_dbg(&adapter->hw,                                  \
545 			"pattern test reg %04X failed: got "                  \
546 			"0x%08X expected 0x%08X\n",                           \
547 			R, val, (register_test_patterns[pat] & W & M));       \
548 			*data = R;                                            \
549 			writel(before, adapter->hw.hw_addr + R);              \
550 			return 1;                                             \
551 		}                                                             \
552 		writel(before, adapter->hw.hw_addr + R);                      \
553 	}                                                                     \
554 }
555 
556 #define REG_SET_AND_CHECK(R, M, W)                                            \
557 {                                                                             \
558 	u32 val, before;                                                      \
559 	before = readl(adapter->hw.hw_addr + R);                              \
560 	writel((W & M), (adapter->hw.hw_addr + R));                           \
561 	val = readl(adapter->hw.hw_addr + R);                                 \
562 	if ((W & M) != (val & M)) {                                           \
563 		pr_err("set/check reg %04X test failed: got 0x%08X expected " \
564 		       "0x%08X\n", R, (val & M), (W & M));                    \
565 		*data = R;                                                    \
566 		writel(before, (adapter->hw.hw_addr + R));                    \
567 		return 1;                                                     \
568 	}                                                                     \
569 	writel(before, (adapter->hw.hw_addr + R));                            \
570 }
571 
572 static int ixgbevf_reg_test(struct ixgbevf_adapter *adapter, u64 *data)
573 {
574 	const struct ixgbevf_reg_test *test;
575 	u32 i;
576 
577 	test = reg_test_vf;
578 
579 	/*
580 	 * Perform the register test, looping through the test table
581 	 * until we either fail or reach the null entry.
582 	 */
583 	while (test->reg) {
584 		for (i = 0; i < test->array_len; i++) {
585 			switch (test->test_type) {
586 			case PATTERN_TEST:
587 				REG_PATTERN_TEST(test->reg + (i * 0x40),
588 						test->mask,
589 						test->write);
590 				break;
591 			case SET_READ_TEST:
592 				REG_SET_AND_CHECK(test->reg + (i * 0x40),
593 						test->mask,
594 						test->write);
595 				break;
596 			case WRITE_NO_TEST:
597 				writel(test->write,
598 				       (adapter->hw.hw_addr + test->reg)
599 				       + (i * 0x40));
600 				break;
601 			case TABLE32_TEST:
602 				REG_PATTERN_TEST(test->reg + (i * 4),
603 						test->mask,
604 						test->write);
605 				break;
606 			case TABLE64_TEST_LO:
607 				REG_PATTERN_TEST(test->reg + (i * 8),
608 						test->mask,
609 						test->write);
610 				break;
611 			case TABLE64_TEST_HI:
612 				REG_PATTERN_TEST((test->reg + 4) + (i * 8),
613 						test->mask,
614 						test->write);
615 				break;
616 			}
617 		}
618 		test++;
619 	}
620 
621 	*data = 0;
622 	return *data;
623 }
624 
625 static void ixgbevf_diag_test(struct net_device *netdev,
626 			      struct ethtool_test *eth_test, u64 *data)
627 {
628 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
629 	bool if_running = netif_running(netdev);
630 
631 	set_bit(__IXGBEVF_TESTING, &adapter->state);
632 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
633 		/* Offline tests */
634 
635 		hw_dbg(&adapter->hw, "offline testing starting\n");
636 
637 		/* Link test performed before hardware reset so autoneg doesn't
638 		 * interfere with test result */
639 		if (ixgbevf_link_test(adapter, &data[1]))
640 			eth_test->flags |= ETH_TEST_FL_FAILED;
641 
642 		if (if_running)
643 			/* indicate we're in test mode */
644 			dev_close(netdev);
645 		else
646 			ixgbevf_reset(adapter);
647 
648 		hw_dbg(&adapter->hw, "register testing starting\n");
649 		if (ixgbevf_reg_test(adapter, &data[0]))
650 			eth_test->flags |= ETH_TEST_FL_FAILED;
651 
652 		ixgbevf_reset(adapter);
653 
654 		clear_bit(__IXGBEVF_TESTING, &adapter->state);
655 		if (if_running)
656 			dev_open(netdev);
657 	} else {
658 		hw_dbg(&adapter->hw, "online testing starting\n");
659 		/* Online tests */
660 		if (ixgbevf_link_test(adapter, &data[1]))
661 			eth_test->flags |= ETH_TEST_FL_FAILED;
662 
663 		/* Online tests aren't run; pass by default */
664 		data[0] = 0;
665 
666 		clear_bit(__IXGBEVF_TESTING, &adapter->state);
667 	}
668 	msleep_interruptible(4 * 1000);
669 }
670 
671 static int ixgbevf_nway_reset(struct net_device *netdev)
672 {
673 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
674 
675 	if (netif_running(netdev))
676 		ixgbevf_reinit_locked(adapter);
677 
678 	return 0;
679 }
680 
681 static int ixgbevf_get_coalesce(struct net_device *netdev,
682 				struct ethtool_coalesce *ec)
683 {
684 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
685 
686 	/* only valid if in constant ITR mode */
687 	if (adapter->rx_itr_setting <= 1)
688 		ec->rx_coalesce_usecs = adapter->rx_itr_setting;
689 	else
690 		ec->rx_coalesce_usecs = adapter->rx_itr_setting >> 2;
691 
692 	/* if in mixed tx/rx queues per vector mode, report only rx settings */
693 	if (adapter->q_vector[0]->tx.count && adapter->q_vector[0]->rx.count)
694 		return 0;
695 
696 	/* only valid if in constant ITR mode */
697 	if (adapter->tx_itr_setting <= 1)
698 		ec->tx_coalesce_usecs = adapter->tx_itr_setting;
699 	else
700 		ec->tx_coalesce_usecs = adapter->tx_itr_setting >> 2;
701 
702 	return 0;
703 }
704 
705 static int ixgbevf_set_coalesce(struct net_device *netdev,
706 				struct ethtool_coalesce *ec)
707 {
708 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
709 	struct ixgbevf_q_vector *q_vector;
710 	int num_vectors, i;
711 	u16 tx_itr_param, rx_itr_param;
712 
713 	/* don't accept tx specific changes if we've got mixed RxTx vectors */
714 	if (adapter->q_vector[0]->tx.count && adapter->q_vector[0]->rx.count
715 	    && ec->tx_coalesce_usecs)
716 		return -EINVAL;
717 
718 
719 	if ((ec->rx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)) ||
720 	    (ec->tx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)))
721 		return -EINVAL;
722 
723 	if (ec->rx_coalesce_usecs > 1)
724 		adapter->rx_itr_setting = ec->rx_coalesce_usecs << 2;
725 	else
726 		adapter->rx_itr_setting = ec->rx_coalesce_usecs;
727 
728 	if (adapter->rx_itr_setting == 1)
729 		rx_itr_param = IXGBE_20K_ITR;
730 	else
731 		rx_itr_param = adapter->rx_itr_setting;
732 
733 
734 	if (ec->tx_coalesce_usecs > 1)
735 		adapter->tx_itr_setting = ec->tx_coalesce_usecs << 2;
736 	else
737 		adapter->tx_itr_setting = ec->tx_coalesce_usecs;
738 
739 	if (adapter->tx_itr_setting == 1)
740 		tx_itr_param = IXGBE_10K_ITR;
741 	else
742 		tx_itr_param = adapter->tx_itr_setting;
743 
744 	num_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
745 
746 	for (i = 0; i < num_vectors; i++) {
747 		q_vector = adapter->q_vector[i];
748 		if (q_vector->tx.count && !q_vector->rx.count)
749 			/* tx only */
750 			q_vector->itr = tx_itr_param;
751 		else
752 			/* rx only or mixed */
753 			q_vector->itr = rx_itr_param;
754 		ixgbevf_write_eitr(q_vector);
755 	}
756 
757 	return 0;
758 }
759 
760 static const struct ethtool_ops ixgbevf_ethtool_ops = {
761 	.get_settings           = ixgbevf_get_settings,
762 	.get_drvinfo            = ixgbevf_get_drvinfo,
763 	.get_regs_len           = ixgbevf_get_regs_len,
764 	.get_regs               = ixgbevf_get_regs,
765 	.nway_reset             = ixgbevf_nway_reset,
766 	.get_link               = ethtool_op_get_link,
767 	.get_ringparam          = ixgbevf_get_ringparam,
768 	.set_ringparam          = ixgbevf_set_ringparam,
769 	.get_msglevel           = ixgbevf_get_msglevel,
770 	.set_msglevel           = ixgbevf_set_msglevel,
771 	.self_test              = ixgbevf_diag_test,
772 	.get_sset_count         = ixgbevf_get_sset_count,
773 	.get_strings            = ixgbevf_get_strings,
774 	.get_ethtool_stats      = ixgbevf_get_ethtool_stats,
775 	.get_coalesce           = ixgbevf_get_coalesce,
776 	.set_coalesce           = ixgbevf_set_coalesce,
777 };
778 
779 void ixgbevf_set_ethtool_ops(struct net_device *netdev)
780 {
781 	SET_ETHTOOL_OPS(netdev, &ixgbevf_ethtool_ops);
782 }
783