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