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