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