1 /*
2  * Copyright (C) 2005 - 2016 Broadcom
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(rx_crc_errors)},
41 	{DRVSTAT_INFO(rx_alignment_symbol_errors)},
42 	{DRVSTAT_INFO(rx_pause_frames)},
43 	{DRVSTAT_INFO(rx_control_frames)},
44 	/* Received packets dropped when the Ethernet length field
45 	 * is not equal to the actual Ethernet data length.
46 	 */
47 	{DRVSTAT_INFO(rx_in_range_errors)},
48 	/* Received packets dropped when their length field is >= 1501 bytes
49 	 * and <= 1535 bytes.
50 	 */
51 	{DRVSTAT_INFO(rx_out_range_errors)},
52 	/* Received packets dropped when they are longer than 9216 bytes */
53 	{DRVSTAT_INFO(rx_frame_too_long)},
54 	/* Received packets dropped when they don't pass the unicast or
55 	 * multicast address filtering.
56 	 */
57 	{DRVSTAT_INFO(rx_address_filtered)},
58 	/* Received packets dropped when IP packet length field is less than
59 	 * the IP header length field.
60 	 */
61 	{DRVSTAT_INFO(rx_dropped_too_small)},
62 	/* Received packets dropped when IP length field is greater than
63 	 * the actual packet length.
64 	 */
65 	{DRVSTAT_INFO(rx_dropped_too_short)},
66 	/* Received packets dropped when the IP header length field is less
67 	 * than 5.
68 	 */
69 	{DRVSTAT_INFO(rx_dropped_header_too_small)},
70 	/* Received packets dropped when the TCP header length field is less
71 	 * than 5 or the TCP header length + IP header length is more
72 	 * than IP packet length.
73 	 */
74 	{DRVSTAT_INFO(rx_dropped_tcp_length)},
75 	{DRVSTAT_INFO(rx_dropped_runt)},
76 	/* Number of received packets dropped when a fifo for descriptors going
77 	 * into the packet demux block overflows. In normal operation, this
78 	 * fifo must never overflow.
79 	 */
80 	{DRVSTAT_INFO(rxpp_fifo_overflow_drop)},
81 	/* Received packets dropped when the RX block runs out of space in
82 	 * one of its input FIFOs. This could happen due a long burst of
83 	 * minimum-sized (64b) frames in the receive path.
84 	 * This counter may also be erroneously incremented rarely.
85 	 */
86 	{DRVSTAT_INFO(rx_input_fifo_overflow_drop)},
87 	{DRVSTAT_INFO(rx_ip_checksum_errs)},
88 	{DRVSTAT_INFO(rx_tcp_checksum_errs)},
89 	{DRVSTAT_INFO(rx_udp_checksum_errs)},
90 	{DRVSTAT_INFO(tx_pauseframes)},
91 	{DRVSTAT_INFO(tx_controlframes)},
92 	{DRVSTAT_INFO(rx_priority_pause_frames)},
93 	{DRVSTAT_INFO(tx_priority_pauseframes)},
94 	/* Received packets dropped when an internal fifo going into
95 	 * main packet buffer tank (PMEM) overflows.
96 	 */
97 	{DRVSTAT_INFO(pmem_fifo_overflow_drop)},
98 	{DRVSTAT_INFO(jabber_events)},
99 	/* Received packets dropped due to lack of available HW packet buffers
100 	 * used to temporarily hold the received packets.
101 	 */
102 	{DRVSTAT_INFO(rx_drops_no_pbuf)},
103 	/* Received packets dropped due to input receive buffer
104 	 * descriptor fifo overflowing.
105 	 */
106 	{DRVSTAT_INFO(rx_drops_no_erx_descr)},
107 	/* Packets dropped because the internal FIFO to the offloaded TCP
108 	 * receive processing block is full. This could happen only for
109 	 * offloaded iSCSI or FCoE trarffic.
110 	 */
111 	{DRVSTAT_INFO(rx_drops_no_tpre_descr)},
112 	/* Received packets dropped when they need more than 8
113 	 * receive buffers. This cannot happen as the driver configures
114 	 * 2048 byte receive buffers.
115 	 */
116 	{DRVSTAT_INFO(rx_drops_too_many_frags)},
117 	{DRVSTAT_INFO(forwarded_packets)},
118 	/* Received packets dropped when the frame length
119 	 * is more than 9018 bytes
120 	 */
121 	{DRVSTAT_INFO(rx_drops_mtu)},
122 	/* Number of dma mapping errors */
123 	{DRVSTAT_INFO(dma_map_errors)},
124 	/* Number of packets dropped due to random early drop function */
125 	{DRVSTAT_INFO(eth_red_drops)},
126 	{DRVSTAT_INFO(rx_roce_bytes_lsd)},
127 	{DRVSTAT_INFO(rx_roce_bytes_msd)},
128 	{DRVSTAT_INFO(rx_roce_frames)},
129 	{DRVSTAT_INFO(roce_drops_payload_len)},
130 	{DRVSTAT_INFO(roce_drops_crc)}
131 };
132 
133 #define ETHTOOL_STATS_NUM ARRAY_SIZE(et_stats)
134 
135 /* Stats related to multi RX queues: get_stats routine assumes bytes, pkts
136  * are first and second members respectively.
137  */
138 static const struct be_ethtool_stat et_rx_stats[] = {
139 	{DRVSTAT_RX_INFO(rx_bytes)},/* If moving this member see above note */
140 	{DRVSTAT_RX_INFO(rx_pkts)}, /* If moving this member see above note */
141 	{DRVSTAT_RX_INFO(rx_vxlan_offload_pkts)},
142 	{DRVSTAT_RX_INFO(rx_compl)},
143 	{DRVSTAT_RX_INFO(rx_compl_err)},
144 	{DRVSTAT_RX_INFO(rx_mcast_pkts)},
145 	/* Number of page allocation failures while posting receive buffers
146 	 * to HW.
147 	 */
148 	{DRVSTAT_RX_INFO(rx_post_fail)},
149 	/* Recevied packets dropped due to skb allocation failure */
150 	{DRVSTAT_RX_INFO(rx_drops_no_skbs)},
151 	/* Received packets dropped due to lack of available fetched buffers
152 	 * posted by the driver.
153 	 */
154 	{DRVSTAT_RX_INFO(rx_drops_no_frags)}
155 };
156 
157 #define ETHTOOL_RXSTATS_NUM (ARRAY_SIZE(et_rx_stats))
158 
159 /* Stats related to multi TX queues: get_stats routine assumes compl is the
160  * first member
161  */
162 static const struct be_ethtool_stat et_tx_stats[] = {
163 	{DRVSTAT_TX_INFO(tx_compl)}, /* If moving this member see above note */
164 	/* This counter is incremented when the HW encounters an error while
165 	 * parsing the packet header of an outgoing TX request. This counter is
166 	 * applicable only for BE2, BE3 and Skyhawk based adapters.
167 	 */
168 	{DRVSTAT_TX_INFO(tx_hdr_parse_err)},
169 	/* This counter is incremented when an error occurs in the DMA
170 	 * operation associated with the TX request from the host to the device.
171 	 */
172 	{DRVSTAT_TX_INFO(tx_dma_err)},
173 	/* This counter is incremented when MAC or VLAN spoof checking is
174 	 * enabled on the interface and the TX request fails the spoof check
175 	 * in HW.
176 	 */
177 	{DRVSTAT_TX_INFO(tx_spoof_check_err)},
178 	/* This counter is incremented when the HW encounters an error while
179 	 * performing TSO offload. This counter is applicable only for Lancer
180 	 * adapters.
181 	 */
182 	{DRVSTAT_TX_INFO(tx_tso_err)},
183 	/* This counter is incremented when the HW detects Q-in-Q style VLAN
184 	 * tagging in a packet and such tagging is not expected on the outgoing
185 	 * interface. This counter is applicable only for Lancer adapters.
186 	 */
187 	{DRVSTAT_TX_INFO(tx_qinq_err)},
188 	/* This counter is incremented when the HW detects parity errors in the
189 	 * packet data. This counter is applicable only for Lancer adapters.
190 	 */
191 	{DRVSTAT_TX_INFO(tx_internal_parity_err)},
192 	{DRVSTAT_TX_INFO(tx_sge_err)},
193 	{DRVSTAT_TX_INFO(tx_bytes)},
194 	{DRVSTAT_TX_INFO(tx_pkts)},
195 	{DRVSTAT_TX_INFO(tx_vxlan_offload_pkts)},
196 	/* Number of skbs queued for trasmission by the driver */
197 	{DRVSTAT_TX_INFO(tx_reqs)},
198 	/* Number of times the TX queue was stopped due to lack
199 	 * of spaces in the TXQ.
200 	 */
201 	{DRVSTAT_TX_INFO(tx_stops)},
202 	/* Pkts dropped in the driver's transmit path */
203 	{DRVSTAT_TX_INFO(tx_drv_drops)}
204 };
205 
206 #define ETHTOOL_TXSTATS_NUM (ARRAY_SIZE(et_tx_stats))
207 
208 static const char et_self_tests[][ETH_GSTRING_LEN] = {
209 	"MAC Loopback test",
210 	"PHY Loopback test",
211 	"External Loopback test",
212 	"DDR DMA test",
213 	"Link test"
214 };
215 
216 #define ETHTOOL_TESTS_NUM ARRAY_SIZE(et_self_tests)
217 #define BE_MAC_LOOPBACK 0x0
218 #define BE_PHY_LOOPBACK 0x1
219 #define BE_ONE_PORT_EXT_LOOPBACK 0x2
220 #define BE_NO_LOOPBACK 0xff
221 
222 static void be_get_drvinfo(struct net_device *netdev,
223 			   struct ethtool_drvinfo *drvinfo)
224 {
225 	struct be_adapter *adapter = netdev_priv(netdev);
226 
227 	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
228 	strlcpy(drvinfo->version, DRV_VER, sizeof(drvinfo->version));
229 	if (!memcmp(adapter->fw_ver, adapter->fw_on_flash, FW_VER_LEN))
230 		strlcpy(drvinfo->fw_version, adapter->fw_ver,
231 			sizeof(drvinfo->fw_version));
232 	else
233 		snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
234 			 "%s [%s]", adapter->fw_ver, adapter->fw_on_flash);
235 
236 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
237 		sizeof(drvinfo->bus_info));
238 }
239 
240 static u32 lancer_cmd_get_file_len(struct be_adapter *adapter, u8 *file_name)
241 {
242 	u32 data_read = 0, eof;
243 	u8 addn_status;
244 	struct be_dma_mem data_len_cmd;
245 
246 	memset(&data_len_cmd, 0, sizeof(data_len_cmd));
247 	/* data_offset and data_size should be 0 to get reg len */
248 	lancer_cmd_read_object(adapter, &data_len_cmd, 0, 0, file_name,
249 			       &data_read, &eof, &addn_status);
250 
251 	return data_read;
252 }
253 
254 static int be_get_dump_len(struct be_adapter *adapter)
255 {
256 	u32 dump_size = 0;
257 
258 	if (lancer_chip(adapter))
259 		dump_size = lancer_cmd_get_file_len(adapter,
260 						    LANCER_FW_DUMP_FILE);
261 	else
262 		dump_size = adapter->fat_dump_len;
263 
264 	return dump_size;
265 }
266 
267 static int lancer_cmd_read_file(struct be_adapter *adapter, u8 *file_name,
268 				u32 buf_len, void *buf)
269 {
270 	struct be_dma_mem read_cmd;
271 	u32 read_len = 0, total_read_len = 0, chunk_size;
272 	u32 eof = 0;
273 	u8 addn_status;
274 	int status = 0;
275 
276 	read_cmd.size = LANCER_READ_FILE_CHUNK;
277 	read_cmd.va = dma_zalloc_coherent(&adapter->pdev->dev, read_cmd.size,
278 					  &read_cmd.dma, GFP_ATOMIC);
279 
280 	if (!read_cmd.va) {
281 		dev_err(&adapter->pdev->dev,
282 			"Memory allocation failure while reading dump\n");
283 		return -ENOMEM;
284 	}
285 
286 	while ((total_read_len < buf_len) && !eof) {
287 		chunk_size = min_t(u32, (buf_len - total_read_len),
288 				   LANCER_READ_FILE_CHUNK);
289 		chunk_size = ALIGN(chunk_size, 4);
290 		status = lancer_cmd_read_object(adapter, &read_cmd, chunk_size,
291 						total_read_len, file_name,
292 						&read_len, &eof, &addn_status);
293 		if (!status) {
294 			memcpy(buf + total_read_len, read_cmd.va, read_len);
295 			total_read_len += read_len;
296 			eof &= LANCER_READ_FILE_EOF_MASK;
297 		} else {
298 			status = -EIO;
299 			break;
300 		}
301 	}
302 	dma_free_coherent(&adapter->pdev->dev, read_cmd.size, read_cmd.va,
303 			  read_cmd.dma);
304 
305 	return status;
306 }
307 
308 static int be_read_dump_data(struct be_adapter *adapter, u32 dump_len,
309 			     void *buf)
310 {
311 	int status = 0;
312 
313 	if (lancer_chip(adapter))
314 		status = lancer_cmd_read_file(adapter, LANCER_FW_DUMP_FILE,
315 					      dump_len, buf);
316 	else
317 		status = be_cmd_get_fat_dump(adapter, dump_len, buf);
318 
319 	return status;
320 }
321 
322 static int be_get_coalesce(struct net_device *netdev,
323 			   struct ethtool_coalesce *et)
324 {
325 	struct be_adapter *adapter = netdev_priv(netdev);
326 	struct be_aic_obj *aic = &adapter->aic_obj[0];
327 
328 	et->rx_coalesce_usecs = aic->prev_eqd;
329 	et->rx_coalesce_usecs_high = aic->max_eqd;
330 	et->rx_coalesce_usecs_low = aic->min_eqd;
331 
332 	et->tx_coalesce_usecs = aic->prev_eqd;
333 	et->tx_coalesce_usecs_high = aic->max_eqd;
334 	et->tx_coalesce_usecs_low = aic->min_eqd;
335 
336 	et->use_adaptive_rx_coalesce = aic->enable;
337 	et->use_adaptive_tx_coalesce = aic->enable;
338 
339 	return 0;
340 }
341 
342 /* TX attributes are ignored. Only RX attributes are considered
343  * eqd cmd is issued in the worker thread.
344  */
345 static int be_set_coalesce(struct net_device *netdev,
346 			   struct ethtool_coalesce *et)
347 {
348 	struct be_adapter *adapter = netdev_priv(netdev);
349 	struct be_aic_obj *aic = &adapter->aic_obj[0];
350 	struct be_eq_obj *eqo;
351 	int i;
352 
353 	for_all_evt_queues(adapter, eqo, i) {
354 		aic->enable = et->use_adaptive_rx_coalesce;
355 		aic->max_eqd = min(et->rx_coalesce_usecs_high, BE_MAX_EQD);
356 		aic->min_eqd = min(et->rx_coalesce_usecs_low, aic->max_eqd);
357 		aic->et_eqd = min(et->rx_coalesce_usecs, aic->max_eqd);
358 		aic->et_eqd = max(aic->et_eqd, aic->min_eqd);
359 		aic++;
360 	}
361 
362 	/* For Skyhawk, the EQD setting happens via EQ_DB when AIC is enabled.
363 	 * When AIC is disabled, persistently force set EQD value via the
364 	 * FW cmd, so that we don't have to calculate the delay multiplier
365 	 * encode value each time EQ_DB is rung
366 	 */
367 	if (!et->use_adaptive_rx_coalesce && skyhawk_chip(adapter))
368 		be_eqd_update(adapter, true);
369 
370 	return 0;
371 }
372 
373 static void be_get_ethtool_stats(struct net_device *netdev,
374 				 struct ethtool_stats *stats, uint64_t *data)
375 {
376 	struct be_adapter *adapter = netdev_priv(netdev);
377 	struct be_rx_obj *rxo;
378 	struct be_tx_obj *txo;
379 	void *p;
380 	unsigned int i, j, base = 0, start;
381 
382 	for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
383 		p = (u8 *)&adapter->drv_stats + et_stats[i].offset;
384 		data[i] = *(u32 *)p;
385 	}
386 	base += ETHTOOL_STATS_NUM;
387 
388 	for_all_rx_queues(adapter, rxo, j) {
389 		struct be_rx_stats *stats = rx_stats(rxo);
390 
391 		do {
392 			start = u64_stats_fetch_begin_irq(&stats->sync);
393 			data[base] = stats->rx_bytes;
394 			data[base + 1] = stats->rx_pkts;
395 		} while (u64_stats_fetch_retry_irq(&stats->sync, start));
396 
397 		for (i = 2; i < ETHTOOL_RXSTATS_NUM; i++) {
398 			p = (u8 *)stats + et_rx_stats[i].offset;
399 			data[base + i] = *(u32 *)p;
400 		}
401 		base += ETHTOOL_RXSTATS_NUM;
402 	}
403 
404 	for_all_tx_queues(adapter, txo, j) {
405 		struct be_tx_stats *stats = tx_stats(txo);
406 
407 		do {
408 			start = u64_stats_fetch_begin_irq(&stats->sync_compl);
409 			data[base] = stats->tx_compl;
410 		} while (u64_stats_fetch_retry_irq(&stats->sync_compl, start));
411 
412 		do {
413 			start = u64_stats_fetch_begin_irq(&stats->sync);
414 			for (i = 1; i < ETHTOOL_TXSTATS_NUM; i++) {
415 				p = (u8 *)stats + et_tx_stats[i].offset;
416 				data[base + i] =
417 					(et_tx_stats[i].size == sizeof(u64)) ?
418 						*(u64 *)p : *(u32 *)p;
419 			}
420 		} while (u64_stats_fetch_retry_irq(&stats->sync, start));
421 		base += ETHTOOL_TXSTATS_NUM;
422 	}
423 }
424 
425 static const char be_priv_flags[][ETH_GSTRING_LEN] = {
426 	"disable-tpe-recovery"
427 };
428 
429 static void be_get_stat_strings(struct net_device *netdev, uint32_t stringset,
430 				uint8_t *data)
431 {
432 	struct be_adapter *adapter = netdev_priv(netdev);
433 	int i, j;
434 
435 	switch (stringset) {
436 	case ETH_SS_STATS:
437 		for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
438 			memcpy(data, et_stats[i].desc, ETH_GSTRING_LEN);
439 			data += ETH_GSTRING_LEN;
440 		}
441 		for (i = 0; i < adapter->num_rx_qs; i++) {
442 			for (j = 0; j < ETHTOOL_RXSTATS_NUM; j++) {
443 				sprintf(data, "rxq%d: %s", i,
444 					et_rx_stats[j].desc);
445 				data += ETH_GSTRING_LEN;
446 			}
447 		}
448 		for (i = 0; i < adapter->num_tx_qs; i++) {
449 			for (j = 0; j < ETHTOOL_TXSTATS_NUM; j++) {
450 				sprintf(data, "txq%d: %s", i,
451 					et_tx_stats[j].desc);
452 				data += ETH_GSTRING_LEN;
453 			}
454 		}
455 		break;
456 	case ETH_SS_TEST:
457 		for (i = 0; i < ETHTOOL_TESTS_NUM; i++) {
458 			memcpy(data, et_self_tests[i], ETH_GSTRING_LEN);
459 			data += ETH_GSTRING_LEN;
460 		}
461 		break;
462 	case ETH_SS_PRIV_FLAGS:
463 		for (i = 0; i < ARRAY_SIZE(be_priv_flags); i++)
464 			strcpy(data + i * ETH_GSTRING_LEN, be_priv_flags[i]);
465 		break;
466 	}
467 }
468 
469 static int be_get_sset_count(struct net_device *netdev, int stringset)
470 {
471 	struct be_adapter *adapter = netdev_priv(netdev);
472 
473 	switch (stringset) {
474 	case ETH_SS_TEST:
475 		return ETHTOOL_TESTS_NUM;
476 	case ETH_SS_STATS:
477 		return ETHTOOL_STATS_NUM +
478 			adapter->num_rx_qs * ETHTOOL_RXSTATS_NUM +
479 			adapter->num_tx_qs * ETHTOOL_TXSTATS_NUM;
480 	case ETH_SS_PRIV_FLAGS:
481 		return ARRAY_SIZE(be_priv_flags);
482 	default:
483 		return -EINVAL;
484 	}
485 }
486 
487 static u32 be_get_port_type(struct be_adapter *adapter)
488 {
489 	u32 port;
490 
491 	switch (adapter->phy.interface_type) {
492 	case PHY_TYPE_BASET_1GB:
493 	case PHY_TYPE_BASEX_1GB:
494 	case PHY_TYPE_SGMII:
495 		port = PORT_TP;
496 		break;
497 	case PHY_TYPE_SFP_PLUS_10GB:
498 		if (adapter->phy.cable_type & SFP_PLUS_COPPER_CABLE)
499 			port = PORT_DA;
500 		else
501 			port = PORT_FIBRE;
502 		break;
503 	case PHY_TYPE_QSFP:
504 		if (adapter->phy.cable_type & QSFP_PLUS_CR4_CABLE)
505 			port = PORT_DA;
506 		else
507 			port = PORT_FIBRE;
508 		break;
509 	case PHY_TYPE_XFP_10GB:
510 	case PHY_TYPE_SFP_1GB:
511 		port = PORT_FIBRE;
512 		break;
513 	case PHY_TYPE_BASET_10GB:
514 		port = PORT_TP;
515 		break;
516 	default:
517 		port = PORT_OTHER;
518 	}
519 
520 	return port;
521 }
522 
523 static u32 convert_to_et_setting(struct be_adapter *adapter, u32 if_speeds)
524 {
525 	u32 val = 0;
526 
527 	switch (adapter->phy.interface_type) {
528 	case PHY_TYPE_BASET_1GB:
529 	case PHY_TYPE_BASEX_1GB:
530 	case PHY_TYPE_SGMII:
531 		val |= SUPPORTED_TP;
532 		if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
533 			val |= SUPPORTED_1000baseT_Full;
534 		if (if_speeds & BE_SUPPORTED_SPEED_100MBPS)
535 			val |= SUPPORTED_100baseT_Full;
536 		if (if_speeds & BE_SUPPORTED_SPEED_10MBPS)
537 			val |= SUPPORTED_10baseT_Full;
538 		break;
539 	case PHY_TYPE_KX4_10GB:
540 		val |= SUPPORTED_Backplane;
541 		if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
542 			val |= SUPPORTED_1000baseKX_Full;
543 		if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
544 			val |= SUPPORTED_10000baseKX4_Full;
545 		break;
546 	case PHY_TYPE_KR2_20GB:
547 		val |= SUPPORTED_Backplane;
548 		if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
549 			val |= SUPPORTED_10000baseKR_Full;
550 		if (if_speeds & BE_SUPPORTED_SPEED_20GBPS)
551 			val |= SUPPORTED_20000baseKR2_Full;
552 		break;
553 	case PHY_TYPE_KR_10GB:
554 		val |= SUPPORTED_Backplane |
555 				SUPPORTED_10000baseKR_Full;
556 		break;
557 	case PHY_TYPE_KR4_40GB:
558 		val |= SUPPORTED_Backplane;
559 		if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
560 			val |= SUPPORTED_10000baseKR_Full;
561 		if (if_speeds & BE_SUPPORTED_SPEED_40GBPS)
562 			val |= SUPPORTED_40000baseKR4_Full;
563 		break;
564 	case PHY_TYPE_QSFP:
565 		if (if_speeds & BE_SUPPORTED_SPEED_40GBPS) {
566 			switch (adapter->phy.cable_type) {
567 			case QSFP_PLUS_CR4_CABLE:
568 				val |= SUPPORTED_40000baseCR4_Full;
569 				break;
570 			case QSFP_PLUS_LR4_CABLE:
571 				val |= SUPPORTED_40000baseLR4_Full;
572 				break;
573 			default:
574 				val |= SUPPORTED_40000baseSR4_Full;
575 				break;
576 			}
577 		}
578 		/* fall through */
579 	case PHY_TYPE_SFP_PLUS_10GB:
580 	case PHY_TYPE_XFP_10GB:
581 	case PHY_TYPE_SFP_1GB:
582 		val |= SUPPORTED_FIBRE;
583 		if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
584 			val |= SUPPORTED_10000baseT_Full;
585 		if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
586 			val |= SUPPORTED_1000baseT_Full;
587 		break;
588 	case PHY_TYPE_BASET_10GB:
589 		val |= SUPPORTED_TP;
590 		if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
591 			val |= SUPPORTED_10000baseT_Full;
592 		if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
593 			val |= SUPPORTED_1000baseT_Full;
594 		if (if_speeds & BE_SUPPORTED_SPEED_100MBPS)
595 			val |= SUPPORTED_100baseT_Full;
596 		break;
597 	default:
598 		val |= SUPPORTED_TP;
599 	}
600 
601 	return val;
602 }
603 
604 bool be_pause_supported(struct be_adapter *adapter)
605 {
606 	return (adapter->phy.interface_type == PHY_TYPE_SFP_PLUS_10GB ||
607 		adapter->phy.interface_type == PHY_TYPE_XFP_10GB) ?
608 		false : true;
609 }
610 
611 static int be_get_link_ksettings(struct net_device *netdev,
612 				 struct ethtool_link_ksettings *cmd)
613 {
614 	struct be_adapter *adapter = netdev_priv(netdev);
615 	u8 link_status;
616 	u16 link_speed = 0;
617 	int status;
618 	u32 auto_speeds;
619 	u32 fixed_speeds;
620 	u32 supported = 0, advertising = 0;
621 
622 	if (adapter->phy.link_speed < 0) {
623 		status = be_cmd_link_status_query(adapter, &link_speed,
624 						  &link_status, 0);
625 		if (!status)
626 			be_link_status_update(adapter, link_status);
627 		cmd->base.speed = link_speed;
628 
629 		status = be_cmd_get_phy_info(adapter);
630 		if (!status) {
631 			auto_speeds = adapter->phy.auto_speeds_supported;
632 			fixed_speeds = adapter->phy.fixed_speeds_supported;
633 
634 			be_cmd_query_cable_type(adapter);
635 
636 			supported =
637 				convert_to_et_setting(adapter,
638 						      auto_speeds |
639 						      fixed_speeds);
640 			advertising =
641 				convert_to_et_setting(adapter, auto_speeds);
642 
643 			cmd->base.port = be_get_port_type(adapter);
644 
645 			if (adapter->phy.auto_speeds_supported) {
646 				supported |= SUPPORTED_Autoneg;
647 				cmd->base.autoneg = AUTONEG_ENABLE;
648 				advertising |= ADVERTISED_Autoneg;
649 			}
650 
651 			supported |= SUPPORTED_Pause;
652 			if (be_pause_supported(adapter))
653 				advertising |= ADVERTISED_Pause;
654 		} else {
655 			cmd->base.port = PORT_OTHER;
656 			cmd->base.autoneg = AUTONEG_DISABLE;
657 		}
658 
659 		/* Save for future use */
660 		adapter->phy.link_speed = cmd->base.speed;
661 		adapter->phy.port_type = cmd->base.port;
662 		adapter->phy.autoneg = cmd->base.autoneg;
663 		adapter->phy.advertising = advertising;
664 		adapter->phy.supported = supported;
665 	} else {
666 		cmd->base.speed = adapter->phy.link_speed;
667 		cmd->base.port = adapter->phy.port_type;
668 		cmd->base.autoneg = adapter->phy.autoneg;
669 		advertising = adapter->phy.advertising;
670 		supported = adapter->phy.supported;
671 	}
672 
673 	cmd->base.duplex = netif_carrier_ok(netdev) ?
674 		DUPLEX_FULL : DUPLEX_UNKNOWN;
675 	cmd->base.phy_address = adapter->port_num;
676 
677 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
678 						supported);
679 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
680 						advertising);
681 
682 	return 0;
683 }
684 
685 static void be_get_ringparam(struct net_device *netdev,
686 			     struct ethtool_ringparam *ring)
687 {
688 	struct be_adapter *adapter = netdev_priv(netdev);
689 
690 	ring->rx_max_pending = adapter->rx_obj[0].q.len;
691 	ring->rx_pending = adapter->rx_obj[0].q.len;
692 	ring->tx_max_pending = adapter->tx_obj[0].q.len;
693 	ring->tx_pending = adapter->tx_obj[0].q.len;
694 }
695 
696 static void
697 be_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
698 {
699 	struct be_adapter *adapter = netdev_priv(netdev);
700 
701 	be_cmd_get_flow_control(adapter, &ecmd->tx_pause, &ecmd->rx_pause);
702 	ecmd->autoneg = adapter->phy.fc_autoneg;
703 }
704 
705 static int
706 be_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
707 {
708 	struct be_adapter *adapter = netdev_priv(netdev);
709 	int status;
710 
711 	if (ecmd->autoneg != adapter->phy.fc_autoneg)
712 		return -EINVAL;
713 
714 	status = be_cmd_set_flow_control(adapter, ecmd->tx_pause,
715 					 ecmd->rx_pause);
716 	if (status) {
717 		dev_warn(&adapter->pdev->dev, "Pause param set failed\n");
718 		return be_cmd_status(status);
719 	}
720 
721 	adapter->tx_fc = ecmd->tx_pause;
722 	adapter->rx_fc = ecmd->rx_pause;
723 	return 0;
724 }
725 
726 static int be_set_phys_id(struct net_device *netdev,
727 			  enum ethtool_phys_id_state state)
728 {
729 	struct be_adapter *adapter = netdev_priv(netdev);
730 	int status = 0;
731 
732 	switch (state) {
733 	case ETHTOOL_ID_ACTIVE:
734 		status = be_cmd_get_beacon_state(adapter, adapter->hba_port_num,
735 						 &adapter->beacon_state);
736 		if (status)
737 			return be_cmd_status(status);
738 		return 1;       /* cycle on/off once per second */
739 
740 	case ETHTOOL_ID_ON:
741 		status = be_cmd_set_beacon_state(adapter, adapter->hba_port_num,
742 						 0, 0, BEACON_STATE_ENABLED);
743 		break;
744 
745 	case ETHTOOL_ID_OFF:
746 		status = be_cmd_set_beacon_state(adapter, adapter->hba_port_num,
747 						 0, 0, BEACON_STATE_DISABLED);
748 		break;
749 
750 	case ETHTOOL_ID_INACTIVE:
751 		status = be_cmd_set_beacon_state(adapter, adapter->hba_port_num,
752 						 0, 0, adapter->beacon_state);
753 	}
754 
755 	return be_cmd_status(status);
756 }
757 
758 static int be_set_dump(struct net_device *netdev, struct ethtool_dump *dump)
759 {
760 	struct be_adapter *adapter = netdev_priv(netdev);
761 	struct device *dev = &adapter->pdev->dev;
762 	int status;
763 
764 	if (!lancer_chip(adapter) ||
765 	    !check_privilege(adapter, MAX_PRIVILEGES))
766 		return -EOPNOTSUPP;
767 
768 	switch (dump->flag) {
769 	case LANCER_INITIATE_FW_DUMP:
770 		status = lancer_initiate_dump(adapter);
771 		if (!status)
772 			dev_info(dev, "FW dump initiated successfully\n");
773 		break;
774 	case LANCER_DELETE_FW_DUMP:
775 		status = lancer_delete_dump(adapter);
776 		if (!status)
777 			dev_info(dev, "FW dump deleted successfully\n");
778 	break;
779 	default:
780 		dev_err(dev, "Invalid dump level: 0x%x\n", dump->flag);
781 		return -EINVAL;
782 	}
783 	return status;
784 }
785 
786 static void be_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
787 {
788 	struct be_adapter *adapter = netdev_priv(netdev);
789 
790 	if (adapter->wol_cap & BE_WOL_CAP) {
791 		wol->supported |= WAKE_MAGIC;
792 		if (adapter->wol_en)
793 			wol->wolopts |= WAKE_MAGIC;
794 	} else {
795 		wol->wolopts = 0;
796 	}
797 	memset(&wol->sopass, 0, sizeof(wol->sopass));
798 }
799 
800 static int be_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
801 {
802 	struct be_adapter *adapter = netdev_priv(netdev);
803 	struct device *dev = &adapter->pdev->dev;
804 	struct be_dma_mem cmd;
805 	u8 mac[ETH_ALEN];
806 	bool enable;
807 	int status;
808 
809 	if (wol->wolopts & ~WAKE_MAGIC)
810 		return -EOPNOTSUPP;
811 
812 	if (!(adapter->wol_cap & BE_WOL_CAP)) {
813 		dev_warn(&adapter->pdev->dev, "WOL not supported\n");
814 		return -EOPNOTSUPP;
815 	}
816 
817 	cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
818 	cmd.va = dma_zalloc_coherent(dev, cmd.size, &cmd.dma, GFP_KERNEL);
819 	if (!cmd.va)
820 		return -ENOMEM;
821 
822 	eth_zero_addr(mac);
823 
824 	enable = wol->wolopts & WAKE_MAGIC;
825 	if (enable)
826 		ether_addr_copy(mac, adapter->netdev->dev_addr);
827 
828 	status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
829 	if (status) {
830 		dev_err(dev, "Could not set Wake-on-lan mac address\n");
831 		status = be_cmd_status(status);
832 		goto err;
833 	}
834 
835 	pci_enable_wake(adapter->pdev, PCI_D3hot, enable);
836 	pci_enable_wake(adapter->pdev, PCI_D3cold, enable);
837 
838 	adapter->wol_en = enable ? true : false;
839 
840 err:
841 	dma_free_coherent(dev, cmd.size, cmd.va, cmd.dma);
842 	return status;
843 }
844 
845 static int be_test_ddr_dma(struct be_adapter *adapter)
846 {
847 	int ret, i;
848 	struct be_dma_mem ddrdma_cmd;
849 	static const u64 pattern[2] = {
850 		0x5a5a5a5a5a5a5a5aULL, 0xa5a5a5a5a5a5a5a5ULL
851 	};
852 
853 	ddrdma_cmd.size = sizeof(struct be_cmd_req_ddrdma_test);
854 	ddrdma_cmd.va = dma_zalloc_coherent(&adapter->pdev->dev,
855 					    ddrdma_cmd.size, &ddrdma_cmd.dma,
856 					    GFP_KERNEL);
857 	if (!ddrdma_cmd.va)
858 		return -ENOMEM;
859 
860 	for (i = 0; i < 2; i++) {
861 		ret = be_cmd_ddr_dma_test(adapter, pattern[i],
862 					  4096, &ddrdma_cmd);
863 		if (ret != 0)
864 			goto err;
865 	}
866 
867 err:
868 	dma_free_coherent(&adapter->pdev->dev, ddrdma_cmd.size, ddrdma_cmd.va,
869 			  ddrdma_cmd.dma);
870 	return be_cmd_status(ret);
871 }
872 
873 static u64 be_loopback_test(struct be_adapter *adapter, u8 loopback_type,
874 			    u64 *status)
875 {
876 	int ret;
877 
878 	ret = be_cmd_set_loopback(adapter, adapter->hba_port_num,
879 				  loopback_type, 1);
880 	if (ret)
881 		return ret;
882 
883 	*status = be_cmd_loopback_test(adapter, adapter->hba_port_num,
884 				       loopback_type, 1500, 2, 0xabc);
885 
886 	ret = be_cmd_set_loopback(adapter, adapter->hba_port_num,
887 				  BE_NO_LOOPBACK, 1);
888 	if (ret)
889 		return ret;
890 
891 	return *status;
892 }
893 
894 static void be_self_test(struct net_device *netdev, struct ethtool_test *test,
895 			 u64 *data)
896 {
897 	struct be_adapter *adapter = netdev_priv(netdev);
898 	int status;
899 	u8 link_status = 0;
900 
901 	if (adapter->function_caps & BE_FUNCTION_CAPS_SUPER_NIC) {
902 		dev_err(&adapter->pdev->dev, "Self test not supported\n");
903 		test->flags |= ETH_TEST_FL_FAILED;
904 		return;
905 	}
906 
907 	memset(data, 0, sizeof(u64) * ETHTOOL_TESTS_NUM);
908 
909 	if (test->flags & ETH_TEST_FL_OFFLINE) {
910 		if (be_loopback_test(adapter, BE_MAC_LOOPBACK, &data[0]) != 0)
911 			test->flags |= ETH_TEST_FL_FAILED;
912 
913 		if (be_loopback_test(adapter, BE_PHY_LOOPBACK, &data[1]) != 0)
914 			test->flags |= ETH_TEST_FL_FAILED;
915 
916 		if (test->flags & ETH_TEST_FL_EXTERNAL_LB) {
917 			if (be_loopback_test(adapter, BE_ONE_PORT_EXT_LOOPBACK,
918 					     &data[2]) != 0)
919 				test->flags |= ETH_TEST_FL_FAILED;
920 			test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
921 		}
922 	}
923 
924 	if (!lancer_chip(adapter) && be_test_ddr_dma(adapter) != 0) {
925 		data[3] = 1;
926 		test->flags |= ETH_TEST_FL_FAILED;
927 	}
928 
929 	status = be_cmd_link_status_query(adapter, NULL, &link_status, 0);
930 	if (status) {
931 		test->flags |= ETH_TEST_FL_FAILED;
932 		data[4] = -1;
933 	} else if (!link_status) {
934 		test->flags |= ETH_TEST_FL_FAILED;
935 		data[4] = 1;
936 	}
937 }
938 
939 static int be_do_flash(struct net_device *netdev, struct ethtool_flash *efl)
940 {
941 	struct be_adapter *adapter = netdev_priv(netdev);
942 
943 	return be_load_fw(adapter, efl->data);
944 }
945 
946 static int
947 be_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
948 {
949 	struct be_adapter *adapter = netdev_priv(netdev);
950 
951 	if (!check_privilege(adapter, MAX_PRIVILEGES))
952 		return -EOPNOTSUPP;
953 
954 	dump->len = be_get_dump_len(adapter);
955 	dump->version = 1;
956 	dump->flag = 0x1;	/* FW dump is enabled */
957 	return 0;
958 }
959 
960 static int
961 be_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
962 		 void *buf)
963 {
964 	struct be_adapter *adapter = netdev_priv(netdev);
965 	int status;
966 
967 	if (!check_privilege(adapter, MAX_PRIVILEGES))
968 		return -EOPNOTSUPP;
969 
970 	status = be_read_dump_data(adapter, dump->len, buf);
971 	return be_cmd_status(status);
972 }
973 
974 static int be_get_eeprom_len(struct net_device *netdev)
975 {
976 	struct be_adapter *adapter = netdev_priv(netdev);
977 
978 	if (!check_privilege(adapter, MAX_PRIVILEGES))
979 		return 0;
980 
981 	if (lancer_chip(adapter)) {
982 		if (be_physfn(adapter))
983 			return lancer_cmd_get_file_len(adapter,
984 						       LANCER_VPD_PF_FILE);
985 		else
986 			return lancer_cmd_get_file_len(adapter,
987 						       LANCER_VPD_VF_FILE);
988 	} else {
989 		return BE_READ_SEEPROM_LEN;
990 	}
991 }
992 
993 static int be_read_eeprom(struct net_device *netdev,
994 			  struct ethtool_eeprom *eeprom, uint8_t *data)
995 {
996 	struct be_adapter *adapter = netdev_priv(netdev);
997 	struct be_dma_mem eeprom_cmd;
998 	struct be_cmd_resp_seeprom_read *resp;
999 	int status;
1000 
1001 	if (!eeprom->len)
1002 		return -EINVAL;
1003 
1004 	if (lancer_chip(adapter)) {
1005 		if (be_physfn(adapter))
1006 			return lancer_cmd_read_file(adapter, LANCER_VPD_PF_FILE,
1007 						    eeprom->len, data);
1008 		else
1009 			return lancer_cmd_read_file(adapter, LANCER_VPD_VF_FILE,
1010 						    eeprom->len, data);
1011 	}
1012 
1013 	eeprom->magic = BE_VENDOR_ID | (adapter->pdev->device<<16);
1014 
1015 	memset(&eeprom_cmd, 0, sizeof(struct be_dma_mem));
1016 	eeprom_cmd.size = sizeof(struct be_cmd_req_seeprom_read);
1017 	eeprom_cmd.va = dma_zalloc_coherent(&adapter->pdev->dev,
1018 					    eeprom_cmd.size, &eeprom_cmd.dma,
1019 					    GFP_KERNEL);
1020 
1021 	if (!eeprom_cmd.va)
1022 		return -ENOMEM;
1023 
1024 	status = be_cmd_get_seeprom_data(adapter, &eeprom_cmd);
1025 
1026 	if (!status) {
1027 		resp = eeprom_cmd.va;
1028 		memcpy(data, resp->seeprom_data + eeprom->offset, eeprom->len);
1029 	}
1030 	dma_free_coherent(&adapter->pdev->dev, eeprom_cmd.size, eeprom_cmd.va,
1031 			  eeprom_cmd.dma);
1032 
1033 	return be_cmd_status(status);
1034 }
1035 
1036 static u32 be_get_msg_level(struct net_device *netdev)
1037 {
1038 	struct be_adapter *adapter = netdev_priv(netdev);
1039 
1040 	return adapter->msg_enable;
1041 }
1042 
1043 static void be_set_msg_level(struct net_device *netdev, u32 level)
1044 {
1045 	struct be_adapter *adapter = netdev_priv(netdev);
1046 
1047 	if (adapter->msg_enable == level)
1048 		return;
1049 
1050 	if ((level & NETIF_MSG_HW) != (adapter->msg_enable & NETIF_MSG_HW))
1051 		if (BEx_chip(adapter))
1052 			be_cmd_set_fw_log_level(adapter, level & NETIF_MSG_HW ?
1053 						FW_LOG_LEVEL_DEFAULT :
1054 						FW_LOG_LEVEL_FATAL);
1055 	adapter->msg_enable = level;
1056 }
1057 
1058 static u64 be_get_rss_hash_opts(struct be_adapter *adapter, u64 flow_type)
1059 {
1060 	u64 data = 0;
1061 
1062 	switch (flow_type) {
1063 	case TCP_V4_FLOW:
1064 		if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV4)
1065 			data |= RXH_IP_DST | RXH_IP_SRC;
1066 		if (adapter->rss_info.rss_flags & RSS_ENABLE_TCP_IPV4)
1067 			data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1068 		break;
1069 	case UDP_V4_FLOW:
1070 		if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV4)
1071 			data |= RXH_IP_DST | RXH_IP_SRC;
1072 		if (adapter->rss_info.rss_flags & RSS_ENABLE_UDP_IPV4)
1073 			data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1074 		break;
1075 	case TCP_V6_FLOW:
1076 		if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV6)
1077 			data |= RXH_IP_DST | RXH_IP_SRC;
1078 		if (adapter->rss_info.rss_flags & RSS_ENABLE_TCP_IPV6)
1079 			data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1080 		break;
1081 	case UDP_V6_FLOW:
1082 		if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV6)
1083 			data |= RXH_IP_DST | RXH_IP_SRC;
1084 		if (adapter->rss_info.rss_flags & RSS_ENABLE_UDP_IPV6)
1085 			data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1086 		break;
1087 	}
1088 
1089 	return data;
1090 }
1091 
1092 static int be_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1093 			u32 *rule_locs)
1094 {
1095 	struct be_adapter *adapter = netdev_priv(netdev);
1096 
1097 	if (!be_multi_rxq(adapter)) {
1098 		dev_info(&adapter->pdev->dev,
1099 			 "ethtool::get_rxnfc: RX flow hashing is disabled\n");
1100 		return -EINVAL;
1101 	}
1102 
1103 	switch (cmd->cmd) {
1104 	case ETHTOOL_GRXFH:
1105 		cmd->data = be_get_rss_hash_opts(adapter, cmd->flow_type);
1106 		break;
1107 	case ETHTOOL_GRXRINGS:
1108 		cmd->data = adapter->num_rx_qs - 1;
1109 		break;
1110 	default:
1111 		return -EINVAL;
1112 	}
1113 
1114 	return 0;
1115 }
1116 
1117 static int be_set_rss_hash_opts(struct be_adapter *adapter,
1118 				struct ethtool_rxnfc *cmd)
1119 {
1120 	int status;
1121 	u32 rss_flags = adapter->rss_info.rss_flags;
1122 
1123 	if (cmd->data != L3_RSS_FLAGS &&
1124 	    cmd->data != (L3_RSS_FLAGS | L4_RSS_FLAGS))
1125 		return -EINVAL;
1126 
1127 	switch (cmd->flow_type) {
1128 	case TCP_V4_FLOW:
1129 		if (cmd->data == L3_RSS_FLAGS)
1130 			rss_flags &= ~RSS_ENABLE_TCP_IPV4;
1131 		else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1132 			rss_flags |= RSS_ENABLE_IPV4 |
1133 					RSS_ENABLE_TCP_IPV4;
1134 		break;
1135 	case TCP_V6_FLOW:
1136 		if (cmd->data == L3_RSS_FLAGS)
1137 			rss_flags &= ~RSS_ENABLE_TCP_IPV6;
1138 		else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1139 			rss_flags |= RSS_ENABLE_IPV6 |
1140 					RSS_ENABLE_TCP_IPV6;
1141 		break;
1142 	case UDP_V4_FLOW:
1143 		if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) &&
1144 		    BEx_chip(adapter))
1145 			return -EINVAL;
1146 
1147 		if (cmd->data == L3_RSS_FLAGS)
1148 			rss_flags &= ~RSS_ENABLE_UDP_IPV4;
1149 		else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1150 			rss_flags |= RSS_ENABLE_IPV4 |
1151 					RSS_ENABLE_UDP_IPV4;
1152 		break;
1153 	case UDP_V6_FLOW:
1154 		if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) &&
1155 		    BEx_chip(adapter))
1156 			return -EINVAL;
1157 
1158 		if (cmd->data == L3_RSS_FLAGS)
1159 			rss_flags &= ~RSS_ENABLE_UDP_IPV6;
1160 		else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1161 			rss_flags |= RSS_ENABLE_IPV6 |
1162 					RSS_ENABLE_UDP_IPV6;
1163 		break;
1164 	default:
1165 		return -EINVAL;
1166 	}
1167 
1168 	if (rss_flags == adapter->rss_info.rss_flags)
1169 		return 0;
1170 
1171 	status = be_cmd_rss_config(adapter, adapter->rss_info.rsstable,
1172 				   rss_flags, RSS_INDIR_TABLE_LEN,
1173 				   adapter->rss_info.rss_hkey);
1174 	if (!status)
1175 		adapter->rss_info.rss_flags = rss_flags;
1176 
1177 	return be_cmd_status(status);
1178 }
1179 
1180 static int be_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1181 {
1182 	struct be_adapter *adapter = netdev_priv(netdev);
1183 	int status = 0;
1184 
1185 	if (!be_multi_rxq(adapter)) {
1186 		dev_err(&adapter->pdev->dev,
1187 			"ethtool::set_rxnfc: RX flow hashing is disabled\n");
1188 		return -EINVAL;
1189 	}
1190 
1191 	switch (cmd->cmd) {
1192 	case ETHTOOL_SRXFH:
1193 		status = be_set_rss_hash_opts(adapter, cmd);
1194 		break;
1195 	default:
1196 		return -EINVAL;
1197 	}
1198 
1199 	return status;
1200 }
1201 
1202 static void be_get_channels(struct net_device *netdev,
1203 			    struct ethtool_channels *ch)
1204 {
1205 	struct be_adapter *adapter = netdev_priv(netdev);
1206 	u16 num_rx_irqs = max_t(u16, adapter->num_rss_qs, 1);
1207 
1208 	/* num_tx_qs is always same as the number of irqs used for TX */
1209 	ch->combined_count = min(adapter->num_tx_qs, num_rx_irqs);
1210 	ch->rx_count = num_rx_irqs - ch->combined_count;
1211 	ch->tx_count = adapter->num_tx_qs - ch->combined_count;
1212 
1213 	ch->max_combined = be_max_qp_irqs(adapter);
1214 	/* The user must create atleast one combined channel */
1215 	ch->max_rx = be_max_rx_irqs(adapter) - 1;
1216 	ch->max_tx = be_max_tx_irqs(adapter) - 1;
1217 }
1218 
1219 static int be_set_channels(struct net_device  *netdev,
1220 			   struct ethtool_channels *ch)
1221 {
1222 	struct be_adapter *adapter = netdev_priv(netdev);
1223 	int status;
1224 
1225 	/* we support either only combined channels or a combination of
1226 	 * combined and either RX-only or TX-only channels.
1227 	 */
1228 	if (ch->other_count || !ch->combined_count ||
1229 	    (ch->rx_count && ch->tx_count))
1230 		return -EINVAL;
1231 
1232 	if (ch->combined_count > be_max_qp_irqs(adapter) ||
1233 	    (ch->rx_count &&
1234 	     (ch->rx_count + ch->combined_count) > be_max_rx_irqs(adapter)) ||
1235 	    (ch->tx_count &&
1236 	     (ch->tx_count + ch->combined_count) > be_max_tx_irqs(adapter)))
1237 		return -EINVAL;
1238 
1239 	adapter->cfg_num_rx_irqs = ch->combined_count + ch->rx_count;
1240 	adapter->cfg_num_tx_irqs = ch->combined_count + ch->tx_count;
1241 
1242 	status = be_update_queues(adapter);
1243 	return be_cmd_status(status);
1244 }
1245 
1246 static u32 be_get_rxfh_indir_size(struct net_device *netdev)
1247 {
1248 	return RSS_INDIR_TABLE_LEN;
1249 }
1250 
1251 static u32 be_get_rxfh_key_size(struct net_device *netdev)
1252 {
1253 	return RSS_HASH_KEY_LEN;
1254 }
1255 
1256 static int be_get_rxfh(struct net_device *netdev, u32 *indir, u8 *hkey,
1257 		       u8 *hfunc)
1258 {
1259 	struct be_adapter *adapter = netdev_priv(netdev);
1260 	int i;
1261 	struct rss_info *rss = &adapter->rss_info;
1262 
1263 	if (indir) {
1264 		for (i = 0; i < RSS_INDIR_TABLE_LEN; i++)
1265 			indir[i] = rss->rss_queue[i];
1266 	}
1267 
1268 	if (hkey)
1269 		memcpy(hkey, rss->rss_hkey, RSS_HASH_KEY_LEN);
1270 
1271 	if (hfunc)
1272 		*hfunc = ETH_RSS_HASH_TOP;
1273 
1274 	return 0;
1275 }
1276 
1277 static int be_set_rxfh(struct net_device *netdev, const u32 *indir,
1278 		       const u8 *hkey, const u8 hfunc)
1279 {
1280 	int rc = 0, i, j;
1281 	struct be_adapter *adapter = netdev_priv(netdev);
1282 	u8 rsstable[RSS_INDIR_TABLE_LEN];
1283 
1284 	/* We do not allow change in unsupported parameters */
1285 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1286 		return -EOPNOTSUPP;
1287 
1288 	if (indir) {
1289 		struct be_rx_obj *rxo;
1290 
1291 		for (i = 0; i < RSS_INDIR_TABLE_LEN; i++) {
1292 			j = indir[i];
1293 			rxo = &adapter->rx_obj[j];
1294 			rsstable[i] = rxo->rss_id;
1295 			adapter->rss_info.rss_queue[i] = j;
1296 		}
1297 	} else {
1298 		memcpy(rsstable, adapter->rss_info.rsstable,
1299 		       RSS_INDIR_TABLE_LEN);
1300 	}
1301 
1302 	if (!hkey)
1303 		hkey =  adapter->rss_info.rss_hkey;
1304 
1305 	rc = be_cmd_rss_config(adapter, rsstable,
1306 			       adapter->rss_info.rss_flags,
1307 			       RSS_INDIR_TABLE_LEN, hkey);
1308 	if (rc) {
1309 		adapter->rss_info.rss_flags = RSS_ENABLE_NONE;
1310 		return -EIO;
1311 	}
1312 	memcpy(adapter->rss_info.rss_hkey, hkey, RSS_HASH_KEY_LEN);
1313 	memcpy(adapter->rss_info.rsstable, rsstable,
1314 	       RSS_INDIR_TABLE_LEN);
1315 	return 0;
1316 }
1317 
1318 static int be_get_module_info(struct net_device *netdev,
1319 			      struct ethtool_modinfo *modinfo)
1320 {
1321 	struct be_adapter *adapter = netdev_priv(netdev);
1322 	u8 page_data[PAGE_DATA_LEN];
1323 	int status;
1324 
1325 	if (!check_privilege(adapter, MAX_PRIVILEGES))
1326 		return -EOPNOTSUPP;
1327 
1328 	status = be_cmd_read_port_transceiver_data(adapter, TR_PAGE_A0,
1329 						   page_data);
1330 	if (!status) {
1331 		if (!page_data[SFP_PLUS_SFF_8472_COMP]) {
1332 			modinfo->type = ETH_MODULE_SFF_8079;
1333 			modinfo->eeprom_len = PAGE_DATA_LEN;
1334 		} else {
1335 			modinfo->type = ETH_MODULE_SFF_8472;
1336 			modinfo->eeprom_len = 2 * PAGE_DATA_LEN;
1337 		}
1338 	}
1339 	return be_cmd_status(status);
1340 }
1341 
1342 static int be_get_module_eeprom(struct net_device *netdev,
1343 				struct ethtool_eeprom *eeprom, u8 *data)
1344 {
1345 	struct be_adapter *adapter = netdev_priv(netdev);
1346 	int status;
1347 
1348 	if (!check_privilege(adapter, MAX_PRIVILEGES))
1349 		return -EOPNOTSUPP;
1350 
1351 	status = be_cmd_read_port_transceiver_data(adapter, TR_PAGE_A0,
1352 						   data);
1353 	if (status)
1354 		goto err;
1355 
1356 	if (eeprom->offset + eeprom->len > PAGE_DATA_LEN) {
1357 		status = be_cmd_read_port_transceiver_data(adapter,
1358 							   TR_PAGE_A2,
1359 							   data +
1360 							   PAGE_DATA_LEN);
1361 		if (status)
1362 			goto err;
1363 	}
1364 	if (eeprom->offset)
1365 		memcpy(data, data + eeprom->offset, eeprom->len);
1366 err:
1367 	return be_cmd_status(status);
1368 }
1369 
1370 static u32 be_get_priv_flags(struct net_device *netdev)
1371 {
1372 	struct be_adapter *adapter = netdev_priv(netdev);
1373 
1374 	return adapter->priv_flags;
1375 }
1376 
1377 static int be_set_priv_flags(struct net_device *netdev, u32 flags)
1378 {
1379 	struct be_adapter *adapter = netdev_priv(netdev);
1380 	bool tpe_old = !!(adapter->priv_flags & BE_DISABLE_TPE_RECOVERY);
1381 	bool tpe_new = !!(flags & BE_DISABLE_TPE_RECOVERY);
1382 
1383 	if (tpe_old != tpe_new) {
1384 		if (tpe_new) {
1385 			adapter->priv_flags |= BE_DISABLE_TPE_RECOVERY;
1386 			dev_info(&adapter->pdev->dev,
1387 				 "HW error recovery is disabled\n");
1388 		} else {
1389 			adapter->priv_flags &= ~BE_DISABLE_TPE_RECOVERY;
1390 			dev_info(&adapter->pdev->dev,
1391 				 "HW error recovery is enabled\n");
1392 		}
1393 	}
1394 
1395 	return 0;
1396 }
1397 
1398 const struct ethtool_ops be_ethtool_ops = {
1399 	.get_drvinfo = be_get_drvinfo,
1400 	.get_wol = be_get_wol,
1401 	.set_wol = be_set_wol,
1402 	.get_link = ethtool_op_get_link,
1403 	.get_eeprom_len = be_get_eeprom_len,
1404 	.get_eeprom = be_read_eeprom,
1405 	.get_coalesce = be_get_coalesce,
1406 	.set_coalesce = be_set_coalesce,
1407 	.get_ringparam = be_get_ringparam,
1408 	.get_pauseparam = be_get_pauseparam,
1409 	.set_pauseparam = be_set_pauseparam,
1410 	.set_priv_flags = be_set_priv_flags,
1411 	.get_priv_flags = be_get_priv_flags,
1412 	.get_strings = be_get_stat_strings,
1413 	.set_phys_id = be_set_phys_id,
1414 	.set_dump = be_set_dump,
1415 	.get_msglevel = be_get_msg_level,
1416 	.set_msglevel = be_set_msg_level,
1417 	.get_sset_count = be_get_sset_count,
1418 	.get_ethtool_stats = be_get_ethtool_stats,
1419 	.flash_device = be_do_flash,
1420 	.self_test = be_self_test,
1421 	.get_rxnfc = be_get_rxnfc,
1422 	.set_rxnfc = be_set_rxnfc,
1423 	.get_rxfh_indir_size = be_get_rxfh_indir_size,
1424 	.get_rxfh_key_size = be_get_rxfh_key_size,
1425 	.get_rxfh = be_get_rxfh,
1426 	.set_rxfh = be_set_rxfh,
1427 	.get_dump_flag = be_get_dump_flag,
1428 	.get_dump_data = be_get_dump_data,
1429 	.get_channels = be_get_channels,
1430 	.set_channels = be_set_channels,
1431 	.get_module_info = be_get_module_info,
1432 	.get_module_eeprom = be_get_module_eeprom,
1433 	.get_link_ksettings = be_get_link_ksettings,
1434 };
1435