xref: /linux/drivers/net/ethernet/emulex/benet/be_ethtool.c (revision 800c5eb7b5eba6cb2a32738d763fd59f0fbcdde4)
1 /*
2  * Copyright (C) 2005 - 2011 Emulex
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.  The full GNU General
8  * Public License is included in this distribution in the file called COPYING.
9  *
10  * Contact Information:
11  * linux-drivers@emulex.com
12  *
13  * Emulex
14  * 3333 Susan Street
15  * Costa Mesa, CA 92626
16  */
17 
18 #include "be.h"
19 #include "be_cmds.h"
20 #include <linux/ethtool.h>
21 
22 struct be_ethtool_stat {
23 	char desc[ETH_GSTRING_LEN];
24 	int type;
25 	int size;
26 	int offset;
27 };
28 
29 enum {DRVSTAT_TX, DRVSTAT_RX, DRVSTAT};
30 #define FIELDINFO(_struct, field) FIELD_SIZEOF(_struct, field), \
31 					offsetof(_struct, field)
32 #define DRVSTAT_TX_INFO(field)	#field, DRVSTAT_TX,\
33 					FIELDINFO(struct be_tx_stats, field)
34 #define DRVSTAT_RX_INFO(field)	#field, DRVSTAT_RX,\
35 					FIELDINFO(struct be_rx_stats, field)
36 #define	DRVSTAT_INFO(field)	#field, DRVSTAT,\
37 					FIELDINFO(struct be_drv_stats, field)
38 
39 static const struct be_ethtool_stat et_stats[] = {
40 	{DRVSTAT_INFO(tx_events)},
41 	{DRVSTAT_INFO(rx_crc_errors)},
42 	{DRVSTAT_INFO(rx_alignment_symbol_errors)},
43 	{DRVSTAT_INFO(rx_pause_frames)},
44 	{DRVSTAT_INFO(rx_control_frames)},
45 	/* Received packets dropped when the Ethernet length field
46 	 * is not equal to the actual Ethernet data length.
47 	 */
48 	{DRVSTAT_INFO(rx_in_range_errors)},
49 	/* Received packets dropped when their length field is >= 1501 bytes
50 	 * and <= 1535 bytes.
51 	 */
52 	{DRVSTAT_INFO(rx_out_range_errors)},
53 	/* Received packets dropped when they are longer than 9216 bytes */
54 	{DRVSTAT_INFO(rx_frame_too_long)},
55 	/* Received packets dropped when they don't pass the unicast or
56 	 * multicast address filtering.
57 	 */
58 	{DRVSTAT_INFO(rx_address_mismatch_drops)},
59 	/* Received packets dropped when IP packet length field is less than
60 	 * the IP header length field.
61 	 */
62 	{DRVSTAT_INFO(rx_dropped_too_small)},
63 	/* Received packets dropped when IP length field is greater than
64 	 * the actual packet length.
65 	 */
66 	{DRVSTAT_INFO(rx_dropped_too_short)},
67 	/* Received packets dropped when the IP header length field is less
68 	 * than 5.
69 	 */
70 	{DRVSTAT_INFO(rx_dropped_header_too_small)},
71 	/* Received packets dropped when the TCP header length field is less
72 	 * than 5 or the TCP header length + IP header length is more
73 	 * than IP packet length.
74 	 */
75 	{DRVSTAT_INFO(rx_dropped_tcp_length)},
76 	{DRVSTAT_INFO(rx_dropped_runt)},
77 	/* Number of received packets dropped when a fifo for descriptors going
78 	 * into the packet demux block overflows. In normal operation, this
79 	 * fifo must never overflow.
80 	 */
81 	{DRVSTAT_INFO(rxpp_fifo_overflow_drop)},
82 	{DRVSTAT_INFO(rx_input_fifo_overflow_drop)},
83 	{DRVSTAT_INFO(rx_ip_checksum_errs)},
84 	{DRVSTAT_INFO(rx_tcp_checksum_errs)},
85 	{DRVSTAT_INFO(rx_udp_checksum_errs)},
86 	{DRVSTAT_INFO(tx_pauseframes)},
87 	{DRVSTAT_INFO(tx_controlframes)},
88 	{DRVSTAT_INFO(rx_priority_pause_frames)},
89 	/* Received packets dropped when an internal fifo going into
90 	 * main packet buffer tank (PMEM) overflows.
91 	 */
92 	{DRVSTAT_INFO(pmem_fifo_overflow_drop)},
93 	{DRVSTAT_INFO(jabber_events)},
94 	/* Received packets dropped due to lack of available HW packet buffers
95 	 * used to temporarily hold the received packets.
96 	 */
97 	{DRVSTAT_INFO(rx_drops_no_pbuf)},
98 	/* Received packets dropped due to input receive buffer
99 	 * descriptor fifo overflowing.
100 	 */
101 	{DRVSTAT_INFO(rx_drops_no_erx_descr)},
102 	/* Packets dropped because the internal FIFO to the offloaded TCP
103 	 * receive processing block is full. This could happen only for
104 	 * offloaded iSCSI or FCoE trarffic.
105 	 */
106 	{DRVSTAT_INFO(rx_drops_no_tpre_descr)},
107 	/* Received packets dropped when they need more than 8
108 	 * receive buffers. This cannot happen as the driver configures
109 	 * 2048 byte receive buffers.
110 	 */
111 	{DRVSTAT_INFO(rx_drops_too_many_frags)},
112 	{DRVSTAT_INFO(forwarded_packets)},
113 	/* Received packets dropped when the frame length
114 	 * is more than 9018 bytes
115 	 */
116 	{DRVSTAT_INFO(rx_drops_mtu)},
117 	/* Number of packets dropped due to random early drop function */
118 	{DRVSTAT_INFO(eth_red_drops)},
119 	{DRVSTAT_INFO(be_on_die_temperature)}
120 };
121 #define ETHTOOL_STATS_NUM ARRAY_SIZE(et_stats)
122 
123 /* Stats related to multi RX queues: get_stats routine assumes bytes, pkts
124  * are first and second members respectively.
125  */
126 static const struct be_ethtool_stat et_rx_stats[] = {
127 	{DRVSTAT_RX_INFO(rx_bytes)},/* If moving this member see above note */
128 	{DRVSTAT_RX_INFO(rx_pkts)}, /* If moving this member see above note */
129 	{DRVSTAT_RX_INFO(rx_polls)},
130 	{DRVSTAT_RX_INFO(rx_events)},
131 	{DRVSTAT_RX_INFO(rx_compl)},
132 	{DRVSTAT_RX_INFO(rx_mcast_pkts)},
133 	/* Number of page allocation failures while posting receive buffers
134 	 * to HW.
135 	 */
136 	{DRVSTAT_RX_INFO(rx_post_fail)},
137 	/* Recevied packets dropped due to skb allocation failure */
138 	{DRVSTAT_RX_INFO(rx_drops_no_skbs)},
139 	/* Received packets dropped due to lack of available fetched buffers
140 	 * posted by the driver.
141 	 */
142 	{DRVSTAT_RX_INFO(rx_drops_no_frags)}
143 };
144 #define ETHTOOL_RXSTATS_NUM (ARRAY_SIZE(et_rx_stats))
145 
146 /* Stats related to multi TX queues: get_stats routine assumes compl is the
147  * first member
148  */
149 static const struct be_ethtool_stat et_tx_stats[] = {
150 	{DRVSTAT_TX_INFO(tx_compl)}, /* If moving this member see above note */
151 	{DRVSTAT_TX_INFO(tx_bytes)},
152 	{DRVSTAT_TX_INFO(tx_pkts)},
153 	/* Number of skbs queued for trasmission by the driver */
154 	{DRVSTAT_TX_INFO(tx_reqs)},
155 	/* Number of TX work request blocks DMAed to HW */
156 	{DRVSTAT_TX_INFO(tx_wrbs)},
157 	{DRVSTAT_TX_INFO(tx_compl)},
158 	/* Number of times the TX queue was stopped due to lack
159 	 * of spaces in the TXQ.
160 	 */
161 	{DRVSTAT_TX_INFO(tx_stops)}
162 };
163 #define ETHTOOL_TXSTATS_NUM (ARRAY_SIZE(et_tx_stats))
164 
165 static const char et_self_tests[][ETH_GSTRING_LEN] = {
166 	"MAC Loopback test",
167 	"PHY Loopback test",
168 	"External Loopback test",
169 	"DDR DMA test",
170 	"Link test"
171 };
172 
173 #define ETHTOOL_TESTS_NUM ARRAY_SIZE(et_self_tests)
174 #define BE_MAC_LOOPBACK 0x0
175 #define BE_PHY_LOOPBACK 0x1
176 #define BE_ONE_PORT_EXT_LOOPBACK 0x2
177 #define BE_NO_LOOPBACK 0xff
178 
179 static void be_get_drvinfo(struct net_device *netdev,
180 				struct ethtool_drvinfo *drvinfo)
181 {
182 	struct be_adapter *adapter = netdev_priv(netdev);
183 	char fw_on_flash[FW_VER_LEN];
184 
185 	memset(fw_on_flash, 0 , sizeof(fw_on_flash));
186 	be_cmd_get_fw_ver(adapter, adapter->fw_ver, fw_on_flash);
187 
188 	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
189 	strlcpy(drvinfo->version, DRV_VER, sizeof(drvinfo->version));
190 	strncpy(drvinfo->fw_version, adapter->fw_ver, FW_VER_LEN);
191 	if (memcmp(adapter->fw_ver, fw_on_flash, FW_VER_LEN) != 0) {
192 		strcat(drvinfo->fw_version, " [");
193 		strcat(drvinfo->fw_version, fw_on_flash);
194 		strcat(drvinfo->fw_version, "]");
195 	}
196 
197 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
198 		sizeof(drvinfo->bus_info));
199 	drvinfo->testinfo_len = 0;
200 	drvinfo->regdump_len = 0;
201 	drvinfo->eedump_len = 0;
202 }
203 
204 static u32
205 lancer_cmd_get_file_len(struct be_adapter *adapter, u8 *file_name)
206 {
207 	u32 data_read = 0, eof;
208 	u8 addn_status;
209 	struct be_dma_mem data_len_cmd;
210 	int status;
211 
212 	memset(&data_len_cmd, 0, sizeof(data_len_cmd));
213 	/* data_offset and data_size should be 0 to get reg len */
214 	status = lancer_cmd_read_object(adapter, &data_len_cmd, 0, 0,
215 				file_name, &data_read, &eof, &addn_status);
216 
217 	return data_read;
218 }
219 
220 static int
221 lancer_cmd_read_file(struct be_adapter *adapter, u8 *file_name,
222 		u32 buf_len, void *buf)
223 {
224 	struct be_dma_mem read_cmd;
225 	u32 read_len = 0, total_read_len = 0, chunk_size;
226 	u32 eof = 0;
227 	u8 addn_status;
228 	int status = 0;
229 
230 	read_cmd.size = LANCER_READ_FILE_CHUNK;
231 	read_cmd.va = pci_alloc_consistent(adapter->pdev, read_cmd.size,
232 			&read_cmd.dma);
233 
234 	if (!read_cmd.va) {
235 		dev_err(&adapter->pdev->dev,
236 				"Memory allocation failure while reading dump\n");
237 		return -ENOMEM;
238 	}
239 
240 	while ((total_read_len < buf_len) && !eof) {
241 		chunk_size = min_t(u32, (buf_len - total_read_len),
242 				LANCER_READ_FILE_CHUNK);
243 		chunk_size = ALIGN(chunk_size, 4);
244 		status = lancer_cmd_read_object(adapter, &read_cmd, chunk_size,
245 				total_read_len, file_name, &read_len,
246 				&eof, &addn_status);
247 		if (!status) {
248 			memcpy(buf + total_read_len, read_cmd.va, read_len);
249 			total_read_len += read_len;
250 			eof &= LANCER_READ_FILE_EOF_MASK;
251 		} else {
252 			status = -EIO;
253 			break;
254 		}
255 	}
256 	pci_free_consistent(adapter->pdev, read_cmd.size, read_cmd.va,
257 			read_cmd.dma);
258 
259 	return status;
260 }
261 
262 static int
263 be_get_reg_len(struct net_device *netdev)
264 {
265 	struct be_adapter *adapter = netdev_priv(netdev);
266 	u32 log_size = 0;
267 
268 	if (be_physfn(adapter)) {
269 		if (lancer_chip(adapter))
270 			log_size = lancer_cmd_get_file_len(adapter,
271 					LANCER_FW_DUMP_FILE);
272 		else
273 			be_cmd_get_reg_len(adapter, &log_size);
274 	}
275 	return log_size;
276 }
277 
278 static void
279 be_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *buf)
280 {
281 	struct be_adapter *adapter = netdev_priv(netdev);
282 
283 	if (be_physfn(adapter)) {
284 		memset(buf, 0, regs->len);
285 		if (lancer_chip(adapter))
286 			lancer_cmd_read_file(adapter, LANCER_FW_DUMP_FILE,
287 					regs->len, buf);
288 		else
289 			be_cmd_get_regs(adapter, regs->len, buf);
290 	}
291 }
292 
293 static int
294 be_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
295 {
296 	struct be_adapter *adapter = netdev_priv(netdev);
297 	struct be_eq_obj *rx_eq = &adapter->rx_obj[0].rx_eq;
298 	struct be_eq_obj *tx_eq = &adapter->tx_eq;
299 
300 	coalesce->rx_coalesce_usecs = rx_eq->cur_eqd;
301 	coalesce->rx_coalesce_usecs_high = rx_eq->max_eqd;
302 	coalesce->rx_coalesce_usecs_low = rx_eq->min_eqd;
303 
304 	coalesce->tx_coalesce_usecs = tx_eq->cur_eqd;
305 	coalesce->tx_coalesce_usecs_high = tx_eq->max_eqd;
306 	coalesce->tx_coalesce_usecs_low = tx_eq->min_eqd;
307 
308 	coalesce->use_adaptive_rx_coalesce = rx_eq->enable_aic;
309 	coalesce->use_adaptive_tx_coalesce = tx_eq->enable_aic;
310 
311 	return 0;
312 }
313 
314 /*
315  * This routine is used to set interrup coalescing delay
316  */
317 static int
318 be_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
319 {
320 	struct be_adapter *adapter = netdev_priv(netdev);
321 	struct be_rx_obj *rxo;
322 	struct be_eq_obj *rx_eq;
323 	struct be_eq_obj *tx_eq = &adapter->tx_eq;
324 	u32 rx_max, rx_min, rx_cur;
325 	int status = 0, i;
326 	u32 tx_cur;
327 
328 	if (coalesce->use_adaptive_tx_coalesce == 1)
329 		return -EINVAL;
330 
331 	for_all_rx_queues(adapter, rxo, i) {
332 		rx_eq = &rxo->rx_eq;
333 
334 		if (!rx_eq->enable_aic && coalesce->use_adaptive_rx_coalesce)
335 			rx_eq->cur_eqd = 0;
336 		rx_eq->enable_aic = coalesce->use_adaptive_rx_coalesce;
337 
338 		rx_max = coalesce->rx_coalesce_usecs_high;
339 		rx_min = coalesce->rx_coalesce_usecs_low;
340 		rx_cur = coalesce->rx_coalesce_usecs;
341 
342 		if (rx_eq->enable_aic) {
343 			if (rx_max > BE_MAX_EQD)
344 				rx_max = BE_MAX_EQD;
345 			if (rx_min > rx_max)
346 				rx_min = rx_max;
347 			rx_eq->max_eqd = rx_max;
348 			rx_eq->min_eqd = rx_min;
349 			if (rx_eq->cur_eqd > rx_max)
350 				rx_eq->cur_eqd = rx_max;
351 			if (rx_eq->cur_eqd < rx_min)
352 				rx_eq->cur_eqd = rx_min;
353 		} else {
354 			if (rx_cur > BE_MAX_EQD)
355 				rx_cur = BE_MAX_EQD;
356 			if (rx_eq->cur_eqd != rx_cur) {
357 				status = be_cmd_modify_eqd(adapter, rx_eq->q.id,
358 						rx_cur);
359 				if (!status)
360 					rx_eq->cur_eqd = rx_cur;
361 			}
362 		}
363 	}
364 
365 	tx_cur = coalesce->tx_coalesce_usecs;
366 
367 	if (tx_cur > BE_MAX_EQD)
368 		tx_cur = BE_MAX_EQD;
369 	if (tx_eq->cur_eqd != tx_cur) {
370 		status = be_cmd_modify_eqd(adapter, tx_eq->q.id, tx_cur);
371 		if (!status)
372 			tx_eq->cur_eqd = tx_cur;
373 	}
374 
375 	return 0;
376 }
377 
378 static void
379 be_get_ethtool_stats(struct net_device *netdev,
380 		struct ethtool_stats *stats, uint64_t *data)
381 {
382 	struct be_adapter *adapter = netdev_priv(netdev);
383 	struct be_rx_obj *rxo;
384 	struct be_tx_obj *txo;
385 	void *p;
386 	unsigned int i, j, base = 0, start;
387 
388 	for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
389 		p = (u8 *)&adapter->drv_stats + et_stats[i].offset;
390 		data[i] = *(u32 *)p;
391 	}
392 	base += ETHTOOL_STATS_NUM;
393 
394 	for_all_rx_queues(adapter, rxo, j) {
395 		struct be_rx_stats *stats = rx_stats(rxo);
396 
397 		do {
398 			start = u64_stats_fetch_begin_bh(&stats->sync);
399 			data[base] = stats->rx_bytes;
400 			data[base + 1] = stats->rx_pkts;
401 		} while (u64_stats_fetch_retry_bh(&stats->sync, start));
402 
403 		for (i = 2; i < ETHTOOL_RXSTATS_NUM; i++) {
404 			p = (u8 *)stats + et_rx_stats[i].offset;
405 			data[base + i] = *(u32 *)p;
406 		}
407 		base += ETHTOOL_RXSTATS_NUM;
408 	}
409 
410 	for_all_tx_queues(adapter, txo, j) {
411 		struct be_tx_stats *stats = tx_stats(txo);
412 
413 		do {
414 			start = u64_stats_fetch_begin_bh(&stats->sync_compl);
415 			data[base] = stats->tx_compl;
416 		} while (u64_stats_fetch_retry_bh(&stats->sync_compl, start));
417 
418 		do {
419 			start = u64_stats_fetch_begin_bh(&stats->sync);
420 			for (i = 1; i < ETHTOOL_TXSTATS_NUM; i++) {
421 				p = (u8 *)stats + et_tx_stats[i].offset;
422 				data[base + i] =
423 					(et_tx_stats[i].size == sizeof(u64)) ?
424 						*(u64 *)p : *(u32 *)p;
425 			}
426 		} while (u64_stats_fetch_retry_bh(&stats->sync, start));
427 		base += ETHTOOL_TXSTATS_NUM;
428 	}
429 }
430 
431 static void
432 be_get_stat_strings(struct net_device *netdev, uint32_t stringset,
433 		uint8_t *data)
434 {
435 	struct be_adapter *adapter = netdev_priv(netdev);
436 	int i, j;
437 
438 	switch (stringset) {
439 	case ETH_SS_STATS:
440 		for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
441 			memcpy(data, et_stats[i].desc, ETH_GSTRING_LEN);
442 			data += ETH_GSTRING_LEN;
443 		}
444 		for (i = 0; i < adapter->num_rx_qs; i++) {
445 			for (j = 0; j < ETHTOOL_RXSTATS_NUM; j++) {
446 				sprintf(data, "rxq%d: %s", i,
447 					et_rx_stats[j].desc);
448 				data += ETH_GSTRING_LEN;
449 			}
450 		}
451 		for (i = 0; i < adapter->num_tx_qs; i++) {
452 			for (j = 0; j < ETHTOOL_TXSTATS_NUM; j++) {
453 				sprintf(data, "txq%d: %s", i,
454 					et_tx_stats[j].desc);
455 				data += ETH_GSTRING_LEN;
456 			}
457 		}
458 		break;
459 	case ETH_SS_TEST:
460 		for (i = 0; i < ETHTOOL_TESTS_NUM; i++) {
461 			memcpy(data, et_self_tests[i], ETH_GSTRING_LEN);
462 			data += ETH_GSTRING_LEN;
463 		}
464 		break;
465 	}
466 }
467 
468 static int be_get_sset_count(struct net_device *netdev, int stringset)
469 {
470 	struct be_adapter *adapter = netdev_priv(netdev);
471 
472 	switch (stringset) {
473 	case ETH_SS_TEST:
474 		return ETHTOOL_TESTS_NUM;
475 	case ETH_SS_STATS:
476 		return ETHTOOL_STATS_NUM +
477 			adapter->num_rx_qs * ETHTOOL_RXSTATS_NUM +
478 			adapter->num_tx_qs * ETHTOOL_TXSTATS_NUM;
479 	default:
480 		return -EINVAL;
481 	}
482 }
483 
484 static int be_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
485 {
486 	struct be_adapter *adapter = netdev_priv(netdev);
487 	struct be_phy_info phy_info;
488 	u8 mac_speed = 0;
489 	u16 link_speed = 0;
490 	u8 link_status;
491 	int status;
492 
493 	if ((adapter->link_speed < 0) || (!(netdev->flags & IFF_UP))) {
494 		status = be_cmd_link_status_query(adapter, &mac_speed,
495 						  &link_speed, &link_status, 0);
496 		if (!status)
497 			be_link_status_update(adapter, link_status);
498 
499 		/* link_speed is in units of 10 Mbps */
500 		if (link_speed) {
501 			ethtool_cmd_speed_set(ecmd, link_speed*10);
502 		} else {
503 			switch (mac_speed) {
504 			case PHY_LINK_SPEED_10MBPS:
505 				ethtool_cmd_speed_set(ecmd, SPEED_10);
506 				break;
507 			case PHY_LINK_SPEED_100MBPS:
508 				ethtool_cmd_speed_set(ecmd, SPEED_100);
509 				break;
510 			case PHY_LINK_SPEED_1GBPS:
511 				ethtool_cmd_speed_set(ecmd, SPEED_1000);
512 				break;
513 			case PHY_LINK_SPEED_10GBPS:
514 				ethtool_cmd_speed_set(ecmd, SPEED_10000);
515 				break;
516 			case PHY_LINK_SPEED_ZERO:
517 				ethtool_cmd_speed_set(ecmd, 0);
518 				break;
519 			}
520 		}
521 
522 		status = be_cmd_get_phy_info(adapter, &phy_info);
523 		if (!status) {
524 			switch (phy_info.interface_type) {
525 			case PHY_TYPE_XFP_10GB:
526 			case PHY_TYPE_SFP_1GB:
527 			case PHY_TYPE_SFP_PLUS_10GB:
528 				ecmd->port = PORT_FIBRE;
529 				break;
530 			default:
531 				ecmd->port = PORT_TP;
532 				break;
533 			}
534 
535 			switch (phy_info.interface_type) {
536 			case PHY_TYPE_KR_10GB:
537 			case PHY_TYPE_KX4_10GB:
538 				ecmd->autoneg = AUTONEG_ENABLE;
539 			ecmd->transceiver = XCVR_INTERNAL;
540 				break;
541 			default:
542 				ecmd->autoneg = AUTONEG_DISABLE;
543 				ecmd->transceiver = XCVR_EXTERNAL;
544 				break;
545 			}
546 		}
547 
548 		/* Save for future use */
549 		adapter->link_speed = ethtool_cmd_speed(ecmd);
550 		adapter->port_type = ecmd->port;
551 		adapter->transceiver = ecmd->transceiver;
552 		adapter->autoneg = ecmd->autoneg;
553 	} else {
554 		ethtool_cmd_speed_set(ecmd, adapter->link_speed);
555 		ecmd->port = adapter->port_type;
556 		ecmd->transceiver = adapter->transceiver;
557 		ecmd->autoneg = adapter->autoneg;
558 	}
559 
560 	ecmd->duplex = DUPLEX_FULL;
561 	ecmd->phy_address = adapter->port_num;
562 	switch (ecmd->port) {
563 	case PORT_FIBRE:
564 		ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
565 		break;
566 	case PORT_TP:
567 		ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_TP);
568 		break;
569 	case PORT_AUI:
570 		ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_AUI);
571 		break;
572 	}
573 
574 	if (ecmd->autoneg) {
575 		ecmd->supported |= SUPPORTED_1000baseT_Full;
576 		ecmd->supported |= SUPPORTED_Autoneg;
577 		ecmd->advertising |= (ADVERTISED_10000baseT_Full |
578 				ADVERTISED_1000baseT_Full);
579 	}
580 
581 	return 0;
582 }
583 
584 static void be_get_ringparam(struct net_device *netdev,
585 			     struct ethtool_ringparam *ring)
586 {
587 	struct be_adapter *adapter = netdev_priv(netdev);
588 
589 	ring->rx_max_pending = ring->rx_pending = adapter->rx_obj[0].q.len;
590 	ring->tx_max_pending = ring->tx_pending = adapter->tx_obj[0].q.len;
591 }
592 
593 static void
594 be_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
595 {
596 	struct be_adapter *adapter = netdev_priv(netdev);
597 
598 	be_cmd_get_flow_control(adapter, &ecmd->tx_pause, &ecmd->rx_pause);
599 	ecmd->autoneg = 0;
600 }
601 
602 static int
603 be_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
604 {
605 	struct be_adapter *adapter = netdev_priv(netdev);
606 	int status;
607 
608 	if (ecmd->autoneg != 0)
609 		return -EINVAL;
610 	adapter->tx_fc = ecmd->tx_pause;
611 	adapter->rx_fc = ecmd->rx_pause;
612 
613 	status = be_cmd_set_flow_control(adapter,
614 					adapter->tx_fc, adapter->rx_fc);
615 	if (status)
616 		dev_warn(&adapter->pdev->dev, "Pause param set failed.\n");
617 
618 	return status;
619 }
620 
621 static int
622 be_set_phys_id(struct net_device *netdev,
623 	       enum ethtool_phys_id_state state)
624 {
625 	struct be_adapter *adapter = netdev_priv(netdev);
626 
627 	switch (state) {
628 	case ETHTOOL_ID_ACTIVE:
629 		be_cmd_get_beacon_state(adapter, adapter->hba_port_num,
630 					&adapter->beacon_state);
631 		return 1;	/* cycle on/off once per second */
632 
633 	case ETHTOOL_ID_ON:
634 		be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0,
635 					BEACON_STATE_ENABLED);
636 		break;
637 
638 	case ETHTOOL_ID_OFF:
639 		be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0,
640 					BEACON_STATE_DISABLED);
641 		break;
642 
643 	case ETHTOOL_ID_INACTIVE:
644 		be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0,
645 					adapter->beacon_state);
646 	}
647 
648 	return 0;
649 }
650 
651 static bool
652 be_is_wol_supported(struct be_adapter *adapter)
653 {
654 	if (!be_physfn(adapter))
655 		return false;
656 	else
657 		return true;
658 }
659 
660 static void
661 be_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
662 {
663 	struct be_adapter *adapter = netdev_priv(netdev);
664 
665 	if (be_is_wol_supported(adapter))
666 		wol->supported = WAKE_MAGIC;
667 
668 	if (adapter->wol)
669 		wol->wolopts = WAKE_MAGIC;
670 	else
671 		wol->wolopts = 0;
672 	memset(&wol->sopass, 0, sizeof(wol->sopass));
673 }
674 
675 static int
676 be_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
677 {
678 	struct be_adapter *adapter = netdev_priv(netdev);
679 
680 	if (wol->wolopts & ~WAKE_MAGIC)
681 		return -EINVAL;
682 
683 	if ((wol->wolopts & WAKE_MAGIC) && be_is_wol_supported(adapter))
684 		adapter->wol = true;
685 	else
686 		adapter->wol = false;
687 
688 	return 0;
689 }
690 
691 static int
692 be_test_ddr_dma(struct be_adapter *adapter)
693 {
694 	int ret, i;
695 	struct be_dma_mem ddrdma_cmd;
696 	static const u64 pattern[2] = {
697 		0x5a5a5a5a5a5a5a5aULL, 0xa5a5a5a5a5a5a5a5ULL
698 	};
699 
700 	ddrdma_cmd.size = sizeof(struct be_cmd_req_ddrdma_test);
701 	ddrdma_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, ddrdma_cmd.size,
702 					   &ddrdma_cmd.dma, GFP_KERNEL);
703 	if (!ddrdma_cmd.va) {
704 		dev_err(&adapter->pdev->dev, "Memory allocation failure\n");
705 		return -ENOMEM;
706 	}
707 
708 	for (i = 0; i < 2; i++) {
709 		ret = be_cmd_ddr_dma_test(adapter, pattern[i],
710 					4096, &ddrdma_cmd);
711 		if (ret != 0)
712 			goto err;
713 	}
714 
715 err:
716 	dma_free_coherent(&adapter->pdev->dev, ddrdma_cmd.size, ddrdma_cmd.va,
717 			  ddrdma_cmd.dma);
718 	return ret;
719 }
720 
721 static u64 be_loopback_test(struct be_adapter *adapter, u8 loopback_type,
722 				u64 *status)
723 {
724 	be_cmd_set_loopback(adapter, adapter->hba_port_num,
725 				loopback_type, 1);
726 	*status = be_cmd_loopback_test(adapter, adapter->hba_port_num,
727 				loopback_type, 1500,
728 				2, 0xabc);
729 	be_cmd_set_loopback(adapter, adapter->hba_port_num,
730 				BE_NO_LOOPBACK, 1);
731 	return *status;
732 }
733 
734 static void
735 be_self_test(struct net_device *netdev, struct ethtool_test *test, u64 *data)
736 {
737 	struct be_adapter *adapter = netdev_priv(netdev);
738 	u8 mac_speed = 0;
739 	u16 qos_link_speed = 0;
740 
741 	memset(data, 0, sizeof(u64) * ETHTOOL_TESTS_NUM);
742 
743 	if (test->flags & ETH_TEST_FL_OFFLINE) {
744 		if (be_loopback_test(adapter, BE_MAC_LOOPBACK,
745 						&data[0]) != 0) {
746 			test->flags |= ETH_TEST_FL_FAILED;
747 		}
748 		if (be_loopback_test(adapter, BE_PHY_LOOPBACK,
749 						&data[1]) != 0) {
750 			test->flags |= ETH_TEST_FL_FAILED;
751 		}
752 		if (be_loopback_test(adapter, BE_ONE_PORT_EXT_LOOPBACK,
753 						&data[2]) != 0) {
754 			test->flags |= ETH_TEST_FL_FAILED;
755 		}
756 	}
757 
758 	if (be_test_ddr_dma(adapter) != 0) {
759 		data[3] = 1;
760 		test->flags |= ETH_TEST_FL_FAILED;
761 	}
762 
763 	if (be_cmd_link_status_query(adapter, &mac_speed,
764 				     &qos_link_speed, NULL, 0) != 0) {
765 		test->flags |= ETH_TEST_FL_FAILED;
766 		data[4] = -1;
767 	} else if (!mac_speed) {
768 		test->flags |= ETH_TEST_FL_FAILED;
769 		data[4] = 1;
770 	}
771 }
772 
773 static int
774 be_do_flash(struct net_device *netdev, struct ethtool_flash *efl)
775 {
776 	struct be_adapter *adapter = netdev_priv(netdev);
777 
778 	return be_load_fw(adapter, efl->data);
779 }
780 
781 static int
782 be_get_eeprom_len(struct net_device *netdev)
783 {
784 	struct be_adapter *adapter = netdev_priv(netdev);
785 	if (lancer_chip(adapter)) {
786 		if (be_physfn(adapter))
787 			return lancer_cmd_get_file_len(adapter,
788 					LANCER_VPD_PF_FILE);
789 		else
790 			return lancer_cmd_get_file_len(adapter,
791 					LANCER_VPD_VF_FILE);
792 	} else {
793 		return BE_READ_SEEPROM_LEN;
794 	}
795 }
796 
797 static int
798 be_read_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
799 			uint8_t *data)
800 {
801 	struct be_adapter *adapter = netdev_priv(netdev);
802 	struct be_dma_mem eeprom_cmd;
803 	struct be_cmd_resp_seeprom_read *resp;
804 	int status;
805 
806 	if (!eeprom->len)
807 		return -EINVAL;
808 
809 	if (lancer_chip(adapter)) {
810 		if (be_physfn(adapter))
811 			return lancer_cmd_read_file(adapter, LANCER_VPD_PF_FILE,
812 					eeprom->len, data);
813 		else
814 			return lancer_cmd_read_file(adapter, LANCER_VPD_VF_FILE,
815 					eeprom->len, data);
816 	}
817 
818 	eeprom->magic = BE_VENDOR_ID | (adapter->pdev->device<<16);
819 
820 	memset(&eeprom_cmd, 0, sizeof(struct be_dma_mem));
821 	eeprom_cmd.size = sizeof(struct be_cmd_req_seeprom_read);
822 	eeprom_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, eeprom_cmd.size,
823 					   &eeprom_cmd.dma, GFP_KERNEL);
824 
825 	if (!eeprom_cmd.va) {
826 		dev_err(&adapter->pdev->dev,
827 			"Memory allocation failure. Could not read eeprom\n");
828 		return -ENOMEM;
829 	}
830 
831 	status = be_cmd_get_seeprom_data(adapter, &eeprom_cmd);
832 
833 	if (!status) {
834 		resp = eeprom_cmd.va;
835 		memcpy(data, resp->seeprom_data + eeprom->offset, eeprom->len);
836 	}
837 	dma_free_coherent(&adapter->pdev->dev, eeprom_cmd.size, eeprom_cmd.va,
838 			  eeprom_cmd.dma);
839 
840 	return status;
841 }
842 
843 const struct ethtool_ops be_ethtool_ops = {
844 	.get_settings = be_get_settings,
845 	.get_drvinfo = be_get_drvinfo,
846 	.get_wol = be_get_wol,
847 	.set_wol = be_set_wol,
848 	.get_link = ethtool_op_get_link,
849 	.get_eeprom_len = be_get_eeprom_len,
850 	.get_eeprom = be_read_eeprom,
851 	.get_coalesce = be_get_coalesce,
852 	.set_coalesce = be_set_coalesce,
853 	.get_ringparam = be_get_ringparam,
854 	.get_pauseparam = be_get_pauseparam,
855 	.set_pauseparam = be_set_pauseparam,
856 	.get_strings = be_get_stat_strings,
857 	.set_phys_id = be_set_phys_id,
858 	.get_sset_count = be_get_sset_count,
859 	.get_ethtool_stats = be_get_ethtool_stats,
860 	.get_regs_len = be_get_reg_len,
861 	.get_regs = be_get_regs,
862 	.flash_device = be_do_flash,
863 	.self_test = be_self_test,
864 };
865