1adfc5217SJeff Kirsher /* bnx2x_ethtool.c: Broadcom Everest network driver.
2adfc5217SJeff Kirsher  *
3adfc5217SJeff Kirsher  * Copyright (c) 2007-2011 Broadcom Corporation
4adfc5217SJeff Kirsher  *
5adfc5217SJeff Kirsher  * This program is free software; you can redistribute it and/or modify
6adfc5217SJeff Kirsher  * it under the terms of the GNU General Public License as published by
7adfc5217SJeff Kirsher  * the Free Software Foundation.
8adfc5217SJeff Kirsher  *
9adfc5217SJeff Kirsher  * Maintained by: Eilon Greenstein <eilong@broadcom.com>
10adfc5217SJeff Kirsher  * Written by: Eliezer Tamir
11adfc5217SJeff Kirsher  * Based on code from Michael Chan's bnx2 driver
12adfc5217SJeff Kirsher  * UDP CSUM errata workaround by Arik Gendelman
13adfc5217SJeff Kirsher  * Slowpath and fastpath rework by Vladislav Zolotarov
14adfc5217SJeff Kirsher  * Statistics and Link management by Yitchak Gertner
15adfc5217SJeff Kirsher  *
16adfc5217SJeff Kirsher  */
17f1deab50SJoe Perches 
18f1deab50SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19f1deab50SJoe Perches 
20adfc5217SJeff Kirsher #include <linux/ethtool.h>
21adfc5217SJeff Kirsher #include <linux/netdevice.h>
22adfc5217SJeff Kirsher #include <linux/types.h>
23adfc5217SJeff Kirsher #include <linux/sched.h>
24adfc5217SJeff Kirsher #include <linux/crc32.h>
25adfc5217SJeff Kirsher 
26adfc5217SJeff Kirsher 
27adfc5217SJeff Kirsher #include "bnx2x.h"
28adfc5217SJeff Kirsher #include "bnx2x_cmn.h"
29adfc5217SJeff Kirsher #include "bnx2x_dump.h"
30adfc5217SJeff Kirsher #include "bnx2x_init.h"
31adfc5217SJeff Kirsher #include "bnx2x_sp.h"
32adfc5217SJeff Kirsher 
33adfc5217SJeff Kirsher /* Note: in the format strings below %s is replaced by the queue-name which is
34adfc5217SJeff Kirsher  * either its index or 'fcoe' for the fcoe queue. Make sure the format string
35adfc5217SJeff Kirsher  * length does not exceed ETH_GSTRING_LEN - MAX_QUEUE_NAME_LEN + 2
36adfc5217SJeff Kirsher  */
37adfc5217SJeff Kirsher #define MAX_QUEUE_NAME_LEN	4
38adfc5217SJeff Kirsher static const struct {
39adfc5217SJeff Kirsher 	long offset;
40adfc5217SJeff Kirsher 	int size;
41adfc5217SJeff Kirsher 	char string[ETH_GSTRING_LEN];
42adfc5217SJeff Kirsher } bnx2x_q_stats_arr[] = {
43adfc5217SJeff Kirsher /* 1 */	{ Q_STATS_OFFSET32(total_bytes_received_hi), 8, "[%s]: rx_bytes" },
44adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_unicast_packets_received_hi),
45adfc5217SJeff Kirsher 						8, "[%s]: rx_ucast_packets" },
46adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_multicast_packets_received_hi),
47adfc5217SJeff Kirsher 						8, "[%s]: rx_mcast_packets" },
48adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_broadcast_packets_received_hi),
49adfc5217SJeff Kirsher 						8, "[%s]: rx_bcast_packets" },
50adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(no_buff_discard_hi),	8, "[%s]: rx_discards" },
51adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(rx_err_discard_pkt),
52adfc5217SJeff Kirsher 					 4, "[%s]: rx_phy_ip_err_discards"},
53adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(rx_skb_alloc_failed),
54adfc5217SJeff Kirsher 					 4, "[%s]: rx_skb_alloc_discard" },
55adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(hw_csum_err), 4, "[%s]: rx_csum_offload_errors" },
56adfc5217SJeff Kirsher 
57adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_bytes_transmitted_hi),	8, "[%s]: tx_bytes" },
58adfc5217SJeff Kirsher /* 10 */{ Q_STATS_OFFSET32(total_unicast_packets_transmitted_hi),
59adfc5217SJeff Kirsher 						8, "[%s]: tx_ucast_packets" },
60adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_multicast_packets_transmitted_hi),
61adfc5217SJeff Kirsher 						8, "[%s]: tx_mcast_packets" },
62adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
63adfc5217SJeff Kirsher 						8, "[%s]: tx_bcast_packets" },
64adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_tpa_aggregations_hi),
65adfc5217SJeff Kirsher 						8, "[%s]: tpa_aggregations" },
66adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_tpa_aggregated_frames_hi),
67adfc5217SJeff Kirsher 					8, "[%s]: tpa_aggregated_frames"},
68adfc5217SJeff Kirsher 	{ Q_STATS_OFFSET32(total_tpa_bytes_hi),	8, "[%s]: tpa_bytes"}
69adfc5217SJeff Kirsher };
70adfc5217SJeff Kirsher 
71adfc5217SJeff Kirsher #define BNX2X_NUM_Q_STATS ARRAY_SIZE(bnx2x_q_stats_arr)
72adfc5217SJeff Kirsher 
73adfc5217SJeff Kirsher static const struct {
74adfc5217SJeff Kirsher 	long offset;
75adfc5217SJeff Kirsher 	int size;
76adfc5217SJeff Kirsher 	u32 flags;
77adfc5217SJeff Kirsher #define STATS_FLAGS_PORT		1
78adfc5217SJeff Kirsher #define STATS_FLAGS_FUNC		2
79adfc5217SJeff Kirsher #define STATS_FLAGS_BOTH		(STATS_FLAGS_FUNC | STATS_FLAGS_PORT)
80adfc5217SJeff Kirsher 	char string[ETH_GSTRING_LEN];
81adfc5217SJeff Kirsher } bnx2x_stats_arr[] = {
82adfc5217SJeff Kirsher /* 1 */	{ STATS_OFFSET32(total_bytes_received_hi),
83adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "rx_bytes" },
84adfc5217SJeff Kirsher 	{ STATS_OFFSET32(error_bytes_received_hi),
85adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "rx_error_bytes" },
86adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_unicast_packets_received_hi),
87adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "rx_ucast_packets" },
88adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_multicast_packets_received_hi),
89adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "rx_mcast_packets" },
90adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_broadcast_packets_received_hi),
91adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "rx_bcast_packets" },
92adfc5217SJeff Kirsher 	{ STATS_OFFSET32(rx_stat_dot3statsfcserrors_hi),
93adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_crc_errors" },
94adfc5217SJeff Kirsher 	{ STATS_OFFSET32(rx_stat_dot3statsalignmenterrors_hi),
95adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_align_errors" },
96adfc5217SJeff Kirsher 	{ STATS_OFFSET32(rx_stat_etherstatsundersizepkts_hi),
97adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_undersize_packets" },
98adfc5217SJeff Kirsher 	{ STATS_OFFSET32(etherstatsoverrsizepkts_hi),
99adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_oversize_packets" },
100adfc5217SJeff Kirsher /* 10 */{ STATS_OFFSET32(rx_stat_etherstatsfragments_hi),
101adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_fragments" },
102adfc5217SJeff Kirsher 	{ STATS_OFFSET32(rx_stat_etherstatsjabbers_hi),
103adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_jabbers" },
104adfc5217SJeff Kirsher 	{ STATS_OFFSET32(no_buff_discard_hi),
105adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "rx_discards" },
106adfc5217SJeff Kirsher 	{ STATS_OFFSET32(mac_filter_discard),
107adfc5217SJeff Kirsher 				4, STATS_FLAGS_PORT, "rx_filtered_packets" },
108adfc5217SJeff Kirsher 	{ STATS_OFFSET32(mf_tag_discard),
109adfc5217SJeff Kirsher 				4, STATS_FLAGS_PORT, "rx_mf_tag_discard" },
1100e898dd7SBarak Witkowski 	{ STATS_OFFSET32(pfc_frames_received_hi),
1110e898dd7SBarak Witkowski 				8, STATS_FLAGS_PORT, "pfc_frames_received" },
1120e898dd7SBarak Witkowski 	{ STATS_OFFSET32(pfc_frames_sent_hi),
1130e898dd7SBarak Witkowski 				8, STATS_FLAGS_PORT, "pfc_frames_sent" },
114adfc5217SJeff Kirsher 	{ STATS_OFFSET32(brb_drop_hi),
115adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_brb_discard" },
116adfc5217SJeff Kirsher 	{ STATS_OFFSET32(brb_truncate_hi),
117adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_brb_truncate" },
118adfc5217SJeff Kirsher 	{ STATS_OFFSET32(pause_frames_received_hi),
119adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_pause_frames" },
120adfc5217SJeff Kirsher 	{ STATS_OFFSET32(rx_stat_maccontrolframesreceived_hi),
121adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "rx_mac_ctrl_frames" },
122adfc5217SJeff Kirsher 	{ STATS_OFFSET32(nig_timer_max),
123adfc5217SJeff Kirsher 			4, STATS_FLAGS_PORT, "rx_constant_pause_events" },
124adfc5217SJeff Kirsher /* 20 */{ STATS_OFFSET32(rx_err_discard_pkt),
125adfc5217SJeff Kirsher 				4, STATS_FLAGS_BOTH, "rx_phy_ip_err_discards"},
126adfc5217SJeff Kirsher 	{ STATS_OFFSET32(rx_skb_alloc_failed),
127adfc5217SJeff Kirsher 				4, STATS_FLAGS_BOTH, "rx_skb_alloc_discard" },
128adfc5217SJeff Kirsher 	{ STATS_OFFSET32(hw_csum_err),
129adfc5217SJeff Kirsher 				4, STATS_FLAGS_BOTH, "rx_csum_offload_errors" },
130adfc5217SJeff Kirsher 
131adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_bytes_transmitted_hi),
132adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "tx_bytes" },
133adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_ifhcoutbadoctets_hi),
134adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_error_bytes" },
135adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_unicast_packets_transmitted_hi),
136adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "tx_ucast_packets" },
137adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_multicast_packets_transmitted_hi),
138adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "tx_mcast_packets" },
139adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_broadcast_packets_transmitted_hi),
140adfc5217SJeff Kirsher 				8, STATS_FLAGS_BOTH, "tx_bcast_packets" },
141adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_dot3statsinternalmactransmiterrors_hi),
142adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_mac_errors" },
143adfc5217SJeff Kirsher 	{ STATS_OFFSET32(rx_stat_dot3statscarriersenseerrors_hi),
144adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_carrier_errors" },
145adfc5217SJeff Kirsher /* 30 */{ STATS_OFFSET32(tx_stat_dot3statssinglecollisionframes_hi),
146adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_single_collisions" },
147adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_dot3statsmultiplecollisionframes_hi),
148adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_multi_collisions" },
149adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_dot3statsdeferredtransmissions_hi),
150adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_deferred" },
151adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_dot3statsexcessivecollisions_hi),
152adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_excess_collisions" },
153adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_dot3statslatecollisions_hi),
154adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_late_collisions" },
155adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_etherstatscollisions_hi),
156adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_total_collisions" },
157adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_etherstatspkts64octets_hi),
158adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_64_byte_packets" },
159adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_etherstatspkts65octetsto127octets_hi),
160adfc5217SJeff Kirsher 			8, STATS_FLAGS_PORT, "tx_65_to_127_byte_packets" },
161adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_etherstatspkts128octetsto255octets_hi),
162adfc5217SJeff Kirsher 			8, STATS_FLAGS_PORT, "tx_128_to_255_byte_packets" },
163adfc5217SJeff Kirsher 	{ STATS_OFFSET32(tx_stat_etherstatspkts256octetsto511octets_hi),
164adfc5217SJeff Kirsher 			8, STATS_FLAGS_PORT, "tx_256_to_511_byte_packets" },
165adfc5217SJeff Kirsher /* 40 */{ STATS_OFFSET32(tx_stat_etherstatspkts512octetsto1023octets_hi),
166adfc5217SJeff Kirsher 			8, STATS_FLAGS_PORT, "tx_512_to_1023_byte_packets" },
167adfc5217SJeff Kirsher 	{ STATS_OFFSET32(etherstatspkts1024octetsto1522octets_hi),
168adfc5217SJeff Kirsher 			8, STATS_FLAGS_PORT, "tx_1024_to_1522_byte_packets" },
169adfc5217SJeff Kirsher 	{ STATS_OFFSET32(etherstatspktsover1522octets_hi),
170adfc5217SJeff Kirsher 			8, STATS_FLAGS_PORT, "tx_1523_to_9022_byte_packets" },
171adfc5217SJeff Kirsher 	{ STATS_OFFSET32(pause_frames_sent_hi),
172adfc5217SJeff Kirsher 				8, STATS_FLAGS_PORT, "tx_pause_frames" },
173adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_tpa_aggregations_hi),
174adfc5217SJeff Kirsher 			8, STATS_FLAGS_FUNC, "tpa_aggregations" },
175adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_tpa_aggregated_frames_hi),
176adfc5217SJeff Kirsher 			8, STATS_FLAGS_FUNC, "tpa_aggregated_frames"},
177adfc5217SJeff Kirsher 	{ STATS_OFFSET32(total_tpa_bytes_hi),
178adfc5217SJeff Kirsher 			8, STATS_FLAGS_FUNC, "tpa_bytes"}
179adfc5217SJeff Kirsher };
180adfc5217SJeff Kirsher 
181adfc5217SJeff Kirsher #define BNX2X_NUM_STATS		ARRAY_SIZE(bnx2x_stats_arr)
182adfc5217SJeff Kirsher static int bnx2x_get_port_type(struct bnx2x *bp)
183adfc5217SJeff Kirsher {
184adfc5217SJeff Kirsher 	int port_type;
185adfc5217SJeff Kirsher 	u32 phy_idx = bnx2x_get_cur_phy_idx(bp);
186adfc5217SJeff Kirsher 	switch (bp->link_params.phy[phy_idx].media_type) {
187adfc5217SJeff Kirsher 	case ETH_PHY_SFP_FIBER:
188adfc5217SJeff Kirsher 	case ETH_PHY_XFP_FIBER:
189adfc5217SJeff Kirsher 	case ETH_PHY_KR:
190adfc5217SJeff Kirsher 	case ETH_PHY_CX4:
191adfc5217SJeff Kirsher 		port_type = PORT_FIBRE;
192adfc5217SJeff Kirsher 		break;
193adfc5217SJeff Kirsher 	case ETH_PHY_DA_TWINAX:
194adfc5217SJeff Kirsher 		port_type = PORT_DA;
195adfc5217SJeff Kirsher 		break;
196adfc5217SJeff Kirsher 	case ETH_PHY_BASE_T:
197adfc5217SJeff Kirsher 		port_type = PORT_TP;
198adfc5217SJeff Kirsher 		break;
199adfc5217SJeff Kirsher 	case ETH_PHY_NOT_PRESENT:
200adfc5217SJeff Kirsher 		port_type = PORT_NONE;
201adfc5217SJeff Kirsher 		break;
202adfc5217SJeff Kirsher 	case ETH_PHY_UNSPECIFIED:
203adfc5217SJeff Kirsher 	default:
204adfc5217SJeff Kirsher 		port_type = PORT_OTHER;
205adfc5217SJeff Kirsher 		break;
206adfc5217SJeff Kirsher 	}
207adfc5217SJeff Kirsher 	return port_type;
208adfc5217SJeff Kirsher }
209adfc5217SJeff Kirsher 
210adfc5217SJeff Kirsher static int bnx2x_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
211adfc5217SJeff Kirsher {
212adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
213adfc5217SJeff Kirsher 	int cfg_idx = bnx2x_get_link_cfg_idx(bp);
214adfc5217SJeff Kirsher 
215adfc5217SJeff Kirsher 	/* Dual Media boards present all available port types */
216adfc5217SJeff Kirsher 	cmd->supported = bp->port.supported[cfg_idx] |
217adfc5217SJeff Kirsher 		(bp->port.supported[cfg_idx ^ 1] &
218adfc5217SJeff Kirsher 		 (SUPPORTED_TP | SUPPORTED_FIBRE));
219adfc5217SJeff Kirsher 	cmd->advertising = bp->port.advertising[cfg_idx];
220adfc5217SJeff Kirsher 
221adfc5217SJeff Kirsher 	if ((bp->state == BNX2X_STATE_OPEN) &&
222adfc5217SJeff Kirsher 	    !(bp->flags & MF_FUNC_DIS) &&
223adfc5217SJeff Kirsher 	    (bp->link_vars.link_up)) {
224adfc5217SJeff Kirsher 		ethtool_cmd_speed_set(cmd, bp->link_vars.line_speed);
225adfc5217SJeff Kirsher 		cmd->duplex = bp->link_vars.duplex;
226adfc5217SJeff Kirsher 	} else {
227adfc5217SJeff Kirsher 		ethtool_cmd_speed_set(
228adfc5217SJeff Kirsher 			cmd, bp->link_params.req_line_speed[cfg_idx]);
229adfc5217SJeff Kirsher 		cmd->duplex = bp->link_params.req_duplex[cfg_idx];
230adfc5217SJeff Kirsher 	}
231adfc5217SJeff Kirsher 
232adfc5217SJeff Kirsher 	if (IS_MF(bp))
233adfc5217SJeff Kirsher 		ethtool_cmd_speed_set(cmd, bnx2x_get_mf_speed(bp));
234adfc5217SJeff Kirsher 
235adfc5217SJeff Kirsher 	cmd->port = bnx2x_get_port_type(bp);
236adfc5217SJeff Kirsher 
237adfc5217SJeff Kirsher 	cmd->phy_address = bp->mdio.prtad;
238adfc5217SJeff Kirsher 	cmd->transceiver = XCVR_INTERNAL;
239adfc5217SJeff Kirsher 
240adfc5217SJeff Kirsher 	if (bp->link_params.req_line_speed[cfg_idx] == SPEED_AUTO_NEG)
241adfc5217SJeff Kirsher 		cmd->autoneg = AUTONEG_ENABLE;
242adfc5217SJeff Kirsher 	else
243adfc5217SJeff Kirsher 		cmd->autoneg = AUTONEG_DISABLE;
244adfc5217SJeff Kirsher 
245adfc5217SJeff Kirsher 	cmd->maxtxpkt = 0;
246adfc5217SJeff Kirsher 	cmd->maxrxpkt = 0;
247adfc5217SJeff Kirsher 
248adfc5217SJeff Kirsher 	DP(NETIF_MSG_LINK, "ethtool_cmd: cmd %d\n"
249f1deab50SJoe Perches 	   "  supported 0x%x  advertising 0x%x  speed %u\n"
250f1deab50SJoe Perches 	   "  duplex %d  port %d  phy_address %d  transceiver %d\n"
251f1deab50SJoe Perches 	   "  autoneg %d  maxtxpkt %d  maxrxpkt %d\n",
252adfc5217SJeff Kirsher 	   cmd->cmd, cmd->supported, cmd->advertising,
253adfc5217SJeff Kirsher 	   ethtool_cmd_speed(cmd),
254adfc5217SJeff Kirsher 	   cmd->duplex, cmd->port, cmd->phy_address, cmd->transceiver,
255adfc5217SJeff Kirsher 	   cmd->autoneg, cmd->maxtxpkt, cmd->maxrxpkt);
256adfc5217SJeff Kirsher 
257adfc5217SJeff Kirsher 	return 0;
258adfc5217SJeff Kirsher }
259adfc5217SJeff Kirsher 
260adfc5217SJeff Kirsher static int bnx2x_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
261adfc5217SJeff Kirsher {
262adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
263adfc5217SJeff Kirsher 	u32 advertising, cfg_idx, old_multi_phy_config, new_multi_phy_config;
264adfc5217SJeff Kirsher 	u32 speed;
265adfc5217SJeff Kirsher 
266adfc5217SJeff Kirsher 	if (IS_MF_SD(bp))
267adfc5217SJeff Kirsher 		return 0;
268adfc5217SJeff Kirsher 
269adfc5217SJeff Kirsher 	DP(NETIF_MSG_LINK, "ethtool_cmd: cmd %d\n"
270adfc5217SJeff Kirsher 	   "  supported 0x%x  advertising 0x%x  speed %u\n"
271adfc5217SJeff Kirsher 	   "  duplex %d  port %d  phy_address %d  transceiver %d\n"
272adfc5217SJeff Kirsher 	   "  autoneg %d  maxtxpkt %d  maxrxpkt %d\n",
273adfc5217SJeff Kirsher 	   cmd->cmd, cmd->supported, cmd->advertising,
274adfc5217SJeff Kirsher 	   ethtool_cmd_speed(cmd),
275adfc5217SJeff Kirsher 	   cmd->duplex, cmd->port, cmd->phy_address, cmd->transceiver,
276adfc5217SJeff Kirsher 	   cmd->autoneg, cmd->maxtxpkt, cmd->maxrxpkt);
277adfc5217SJeff Kirsher 
278adfc5217SJeff Kirsher 	speed = ethtool_cmd_speed(cmd);
279adfc5217SJeff Kirsher 
280adfc5217SJeff Kirsher 	if (IS_MF_SI(bp)) {
281adfc5217SJeff Kirsher 		u32 part;
282adfc5217SJeff Kirsher 		u32 line_speed = bp->link_vars.line_speed;
283adfc5217SJeff Kirsher 
284adfc5217SJeff Kirsher 		/* use 10G if no link detected */
285adfc5217SJeff Kirsher 		if (!line_speed)
286adfc5217SJeff Kirsher 			line_speed = 10000;
287adfc5217SJeff Kirsher 
288adfc5217SJeff Kirsher 		if (bp->common.bc_ver < REQ_BC_VER_4_SET_MF_BW) {
289adfc5217SJeff Kirsher 			BNX2X_DEV_INFO("To set speed BC %X or higher "
290adfc5217SJeff Kirsher 				       "is required, please upgrade BC\n",
291adfc5217SJeff Kirsher 				       REQ_BC_VER_4_SET_MF_BW);
292adfc5217SJeff Kirsher 			return -EINVAL;
293adfc5217SJeff Kirsher 		}
294adfc5217SJeff Kirsher 
295adfc5217SJeff Kirsher 		part = (speed * 100) / line_speed;
296adfc5217SJeff Kirsher 
297adfc5217SJeff Kirsher 		if (line_speed < speed || !part) {
298adfc5217SJeff Kirsher 			BNX2X_DEV_INFO("Speed setting should be in a range "
299adfc5217SJeff Kirsher 				       "from 1%% to 100%% "
300adfc5217SJeff Kirsher 				       "of actual line speed\n");
301adfc5217SJeff Kirsher 			return -EINVAL;
302adfc5217SJeff Kirsher 		}
303adfc5217SJeff Kirsher 
304adfc5217SJeff Kirsher 		if (bp->state != BNX2X_STATE_OPEN)
305adfc5217SJeff Kirsher 			/* store value for following "load" */
306adfc5217SJeff Kirsher 			bp->pending_max = part;
307adfc5217SJeff Kirsher 		else
308adfc5217SJeff Kirsher 			bnx2x_update_max_mf_config(bp, part);
309adfc5217SJeff Kirsher 
310adfc5217SJeff Kirsher 		return 0;
311adfc5217SJeff Kirsher 	}
312adfc5217SJeff Kirsher 
313adfc5217SJeff Kirsher 	cfg_idx = bnx2x_get_link_cfg_idx(bp);
314adfc5217SJeff Kirsher 	old_multi_phy_config = bp->link_params.multi_phy_config;
315adfc5217SJeff Kirsher 	switch (cmd->port) {
316adfc5217SJeff Kirsher 	case PORT_TP:
317adfc5217SJeff Kirsher 		if (bp->port.supported[cfg_idx] & SUPPORTED_TP)
318adfc5217SJeff Kirsher 			break; /* no port change */
319adfc5217SJeff Kirsher 
320adfc5217SJeff Kirsher 		if (!(bp->port.supported[0] & SUPPORTED_TP ||
321adfc5217SJeff Kirsher 		      bp->port.supported[1] & SUPPORTED_TP)) {
322adfc5217SJeff Kirsher 			DP(NETIF_MSG_LINK, "Unsupported port type\n");
323adfc5217SJeff Kirsher 			return -EINVAL;
324adfc5217SJeff Kirsher 		}
325adfc5217SJeff Kirsher 		bp->link_params.multi_phy_config &=
326adfc5217SJeff Kirsher 			~PORT_HW_CFG_PHY_SELECTION_MASK;
327adfc5217SJeff Kirsher 		if (bp->link_params.multi_phy_config &
328adfc5217SJeff Kirsher 		    PORT_HW_CFG_PHY_SWAPPED_ENABLED)
329adfc5217SJeff Kirsher 			bp->link_params.multi_phy_config |=
330adfc5217SJeff Kirsher 			PORT_HW_CFG_PHY_SELECTION_SECOND_PHY;
331adfc5217SJeff Kirsher 		else
332adfc5217SJeff Kirsher 			bp->link_params.multi_phy_config |=
333adfc5217SJeff Kirsher 			PORT_HW_CFG_PHY_SELECTION_FIRST_PHY;
334adfc5217SJeff Kirsher 		break;
335adfc5217SJeff Kirsher 	case PORT_FIBRE:
336bfdb5823SYaniv Rosner 	case PORT_DA:
337adfc5217SJeff Kirsher 		if (bp->port.supported[cfg_idx] & SUPPORTED_FIBRE)
338adfc5217SJeff Kirsher 			break; /* no port change */
339adfc5217SJeff Kirsher 
340adfc5217SJeff Kirsher 		if (!(bp->port.supported[0] & SUPPORTED_FIBRE ||
341adfc5217SJeff Kirsher 		      bp->port.supported[1] & SUPPORTED_FIBRE)) {
342adfc5217SJeff Kirsher 			DP(NETIF_MSG_LINK, "Unsupported port type\n");
343adfc5217SJeff Kirsher 			return -EINVAL;
344adfc5217SJeff Kirsher 		}
345adfc5217SJeff Kirsher 		bp->link_params.multi_phy_config &=
346adfc5217SJeff Kirsher 			~PORT_HW_CFG_PHY_SELECTION_MASK;
347adfc5217SJeff Kirsher 		if (bp->link_params.multi_phy_config &
348adfc5217SJeff Kirsher 		    PORT_HW_CFG_PHY_SWAPPED_ENABLED)
349adfc5217SJeff Kirsher 			bp->link_params.multi_phy_config |=
350adfc5217SJeff Kirsher 			PORT_HW_CFG_PHY_SELECTION_FIRST_PHY;
351adfc5217SJeff Kirsher 		else
352adfc5217SJeff Kirsher 			bp->link_params.multi_phy_config |=
353adfc5217SJeff Kirsher 			PORT_HW_CFG_PHY_SELECTION_SECOND_PHY;
354adfc5217SJeff Kirsher 		break;
355adfc5217SJeff Kirsher 	default:
356adfc5217SJeff Kirsher 		DP(NETIF_MSG_LINK, "Unsupported port type\n");
357adfc5217SJeff Kirsher 		return -EINVAL;
358adfc5217SJeff Kirsher 	}
3592f751a80SYaniv Rosner 	/* Save new config in case command complete successully */
360adfc5217SJeff Kirsher 	new_multi_phy_config = bp->link_params.multi_phy_config;
361adfc5217SJeff Kirsher 	/* Get the new cfg_idx */
362adfc5217SJeff Kirsher 	cfg_idx = bnx2x_get_link_cfg_idx(bp);
363adfc5217SJeff Kirsher 	/* Restore old config in case command failed */
364adfc5217SJeff Kirsher 	bp->link_params.multi_phy_config = old_multi_phy_config;
365adfc5217SJeff Kirsher 	DP(NETIF_MSG_LINK, "cfg_idx = %x\n", cfg_idx);
366adfc5217SJeff Kirsher 
367adfc5217SJeff Kirsher 	if (cmd->autoneg == AUTONEG_ENABLE) {
368adfc5217SJeff Kirsher 		if (!(bp->port.supported[cfg_idx] & SUPPORTED_Autoneg)) {
369adfc5217SJeff Kirsher 			DP(NETIF_MSG_LINK, "Autoneg not supported\n");
370adfc5217SJeff Kirsher 			return -EINVAL;
371adfc5217SJeff Kirsher 		}
372adfc5217SJeff Kirsher 
373adfc5217SJeff Kirsher 		/* advertise the requested speed and duplex if supported */
3748decf868SDavid S. Miller 		if (cmd->advertising & ~(bp->port.supported[cfg_idx])) {
3758decf868SDavid S. Miller 			DP(NETIF_MSG_LINK, "Advertisement parameters "
3768decf868SDavid S. Miller 					   "are not supported\n");
3778decf868SDavid S. Miller 			return -EINVAL;
3788decf868SDavid S. Miller 		}
379adfc5217SJeff Kirsher 
380adfc5217SJeff Kirsher 		bp->link_params.req_line_speed[cfg_idx] = SPEED_AUTO_NEG;
3818decf868SDavid S. Miller 		bp->link_params.req_duplex[cfg_idx] = cmd->duplex;
3828decf868SDavid S. Miller 		bp->port.advertising[cfg_idx] = (ADVERTISED_Autoneg |
383adfc5217SJeff Kirsher 					 cmd->advertising);
3848decf868SDavid S. Miller 		if (cmd->advertising) {
385adfc5217SJeff Kirsher 
3868decf868SDavid S. Miller 			bp->link_params.speed_cap_mask[cfg_idx] = 0;
3878decf868SDavid S. Miller 			if (cmd->advertising & ADVERTISED_10baseT_Half) {
3888decf868SDavid S. Miller 				bp->link_params.speed_cap_mask[cfg_idx] |=
3898decf868SDavid S. Miller 				PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF;
3908decf868SDavid S. Miller 			}
3918decf868SDavid S. Miller 			if (cmd->advertising & ADVERTISED_10baseT_Full)
3928decf868SDavid S. Miller 				bp->link_params.speed_cap_mask[cfg_idx] |=
3938decf868SDavid S. Miller 				PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL;
3948decf868SDavid S. Miller 
3958decf868SDavid S. Miller 			if (cmd->advertising & ADVERTISED_100baseT_Full)
3968decf868SDavid S. Miller 				bp->link_params.speed_cap_mask[cfg_idx] |=
3978decf868SDavid S. Miller 				PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL;
3988decf868SDavid S. Miller 
3998decf868SDavid S. Miller 			if (cmd->advertising & ADVERTISED_100baseT_Half) {
4008decf868SDavid S. Miller 				bp->link_params.speed_cap_mask[cfg_idx] |=
4018decf868SDavid S. Miller 				     PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF;
4028decf868SDavid S. Miller 			}
4038decf868SDavid S. Miller 			if (cmd->advertising & ADVERTISED_1000baseT_Half) {
4048decf868SDavid S. Miller 				bp->link_params.speed_cap_mask[cfg_idx] |=
4058decf868SDavid S. Miller 					PORT_HW_CFG_SPEED_CAPABILITY_D0_1G;
4068decf868SDavid S. Miller 			}
4078decf868SDavid S. Miller 			if (cmd->advertising & (ADVERTISED_1000baseT_Full |
4088decf868SDavid S. Miller 						ADVERTISED_1000baseKX_Full))
4098decf868SDavid S. Miller 				bp->link_params.speed_cap_mask[cfg_idx] |=
4108decf868SDavid S. Miller 					PORT_HW_CFG_SPEED_CAPABILITY_D0_1G;
4118decf868SDavid S. Miller 
4128decf868SDavid S. Miller 			if (cmd->advertising & (ADVERTISED_10000baseT_Full |
4138decf868SDavid S. Miller 						ADVERTISED_10000baseKX4_Full |
4148decf868SDavid S. Miller 						ADVERTISED_10000baseKR_Full))
4158decf868SDavid S. Miller 				bp->link_params.speed_cap_mask[cfg_idx] |=
4168decf868SDavid S. Miller 					PORT_HW_CFG_SPEED_CAPABILITY_D0_10G;
4178decf868SDavid S. Miller 		}
418adfc5217SJeff Kirsher 	} else { /* forced speed */
419adfc5217SJeff Kirsher 		/* advertise the requested speed and duplex if supported */
420adfc5217SJeff Kirsher 		switch (speed) {
421adfc5217SJeff Kirsher 		case SPEED_10:
422adfc5217SJeff Kirsher 			if (cmd->duplex == DUPLEX_FULL) {
423adfc5217SJeff Kirsher 				if (!(bp->port.supported[cfg_idx] &
424adfc5217SJeff Kirsher 				      SUPPORTED_10baseT_Full)) {
425adfc5217SJeff Kirsher 					DP(NETIF_MSG_LINK,
426adfc5217SJeff Kirsher 					   "10M full not supported\n");
427adfc5217SJeff Kirsher 					return -EINVAL;
428adfc5217SJeff Kirsher 				}
429adfc5217SJeff Kirsher 
430adfc5217SJeff Kirsher 				advertising = (ADVERTISED_10baseT_Full |
431adfc5217SJeff Kirsher 					       ADVERTISED_TP);
432adfc5217SJeff Kirsher 			} else {
433adfc5217SJeff Kirsher 				if (!(bp->port.supported[cfg_idx] &
434adfc5217SJeff Kirsher 				      SUPPORTED_10baseT_Half)) {
435adfc5217SJeff Kirsher 					DP(NETIF_MSG_LINK,
436adfc5217SJeff Kirsher 					   "10M half not supported\n");
437adfc5217SJeff Kirsher 					return -EINVAL;
438adfc5217SJeff Kirsher 				}
439adfc5217SJeff Kirsher 
440adfc5217SJeff Kirsher 				advertising = (ADVERTISED_10baseT_Half |
441adfc5217SJeff Kirsher 					       ADVERTISED_TP);
442adfc5217SJeff Kirsher 			}
443adfc5217SJeff Kirsher 			break;
444adfc5217SJeff Kirsher 
445adfc5217SJeff Kirsher 		case SPEED_100:
446adfc5217SJeff Kirsher 			if (cmd->duplex == DUPLEX_FULL) {
447adfc5217SJeff Kirsher 				if (!(bp->port.supported[cfg_idx] &
448adfc5217SJeff Kirsher 						SUPPORTED_100baseT_Full)) {
449adfc5217SJeff Kirsher 					DP(NETIF_MSG_LINK,
450adfc5217SJeff Kirsher 					   "100M full not supported\n");
451adfc5217SJeff Kirsher 					return -EINVAL;
452adfc5217SJeff Kirsher 				}
453adfc5217SJeff Kirsher 
454adfc5217SJeff Kirsher 				advertising = (ADVERTISED_100baseT_Full |
455adfc5217SJeff Kirsher 					       ADVERTISED_TP);
456adfc5217SJeff Kirsher 			} else {
457adfc5217SJeff Kirsher 				if (!(bp->port.supported[cfg_idx] &
458adfc5217SJeff Kirsher 						SUPPORTED_100baseT_Half)) {
459adfc5217SJeff Kirsher 					DP(NETIF_MSG_LINK,
460adfc5217SJeff Kirsher 					   "100M half not supported\n");
461adfc5217SJeff Kirsher 					return -EINVAL;
462adfc5217SJeff Kirsher 				}
463adfc5217SJeff Kirsher 
464adfc5217SJeff Kirsher 				advertising = (ADVERTISED_100baseT_Half |
465adfc5217SJeff Kirsher 					       ADVERTISED_TP);
466adfc5217SJeff Kirsher 			}
467adfc5217SJeff Kirsher 			break;
468adfc5217SJeff Kirsher 
469adfc5217SJeff Kirsher 		case SPEED_1000:
470adfc5217SJeff Kirsher 			if (cmd->duplex != DUPLEX_FULL) {
471adfc5217SJeff Kirsher 				DP(NETIF_MSG_LINK, "1G half not supported\n");
472adfc5217SJeff Kirsher 				return -EINVAL;
473adfc5217SJeff Kirsher 			}
474adfc5217SJeff Kirsher 
475adfc5217SJeff Kirsher 			if (!(bp->port.supported[cfg_idx] &
476adfc5217SJeff Kirsher 			      SUPPORTED_1000baseT_Full)) {
477adfc5217SJeff Kirsher 				DP(NETIF_MSG_LINK, "1G full not supported\n");
478adfc5217SJeff Kirsher 				return -EINVAL;
479adfc5217SJeff Kirsher 			}
480adfc5217SJeff Kirsher 
481adfc5217SJeff Kirsher 			advertising = (ADVERTISED_1000baseT_Full |
482adfc5217SJeff Kirsher 				       ADVERTISED_TP);
483adfc5217SJeff Kirsher 			break;
484adfc5217SJeff Kirsher 
485adfc5217SJeff Kirsher 		case SPEED_2500:
486adfc5217SJeff Kirsher 			if (cmd->duplex != DUPLEX_FULL) {
487adfc5217SJeff Kirsher 				DP(NETIF_MSG_LINK,
488adfc5217SJeff Kirsher 				   "2.5G half not supported\n");
489adfc5217SJeff Kirsher 				return -EINVAL;
490adfc5217SJeff Kirsher 			}
491adfc5217SJeff Kirsher 
492adfc5217SJeff Kirsher 			if (!(bp->port.supported[cfg_idx]
493adfc5217SJeff Kirsher 			      & SUPPORTED_2500baseX_Full)) {
494adfc5217SJeff Kirsher 				DP(NETIF_MSG_LINK,
495adfc5217SJeff Kirsher 				   "2.5G full not supported\n");
496adfc5217SJeff Kirsher 				return -EINVAL;
497adfc5217SJeff Kirsher 			}
498adfc5217SJeff Kirsher 
499adfc5217SJeff Kirsher 			advertising = (ADVERTISED_2500baseX_Full |
500adfc5217SJeff Kirsher 				       ADVERTISED_TP);
501adfc5217SJeff Kirsher 			break;
502adfc5217SJeff Kirsher 
503adfc5217SJeff Kirsher 		case SPEED_10000:
504adfc5217SJeff Kirsher 			if (cmd->duplex != DUPLEX_FULL) {
505adfc5217SJeff Kirsher 				DP(NETIF_MSG_LINK, "10G half not supported\n");
506adfc5217SJeff Kirsher 				return -EINVAL;
507adfc5217SJeff Kirsher 			}
508adfc5217SJeff Kirsher 
509adfc5217SJeff Kirsher 			if (!(bp->port.supported[cfg_idx]
510adfc5217SJeff Kirsher 			      & SUPPORTED_10000baseT_Full)) {
511adfc5217SJeff Kirsher 				DP(NETIF_MSG_LINK, "10G full not supported\n");
512adfc5217SJeff Kirsher 				return -EINVAL;
513adfc5217SJeff Kirsher 			}
514adfc5217SJeff Kirsher 
515adfc5217SJeff Kirsher 			advertising = (ADVERTISED_10000baseT_Full |
516adfc5217SJeff Kirsher 				       ADVERTISED_FIBRE);
517adfc5217SJeff Kirsher 			break;
518adfc5217SJeff Kirsher 
519adfc5217SJeff Kirsher 		default:
520adfc5217SJeff Kirsher 			DP(NETIF_MSG_LINK, "Unsupported speed %u\n", speed);
521adfc5217SJeff Kirsher 			return -EINVAL;
522adfc5217SJeff Kirsher 		}
523adfc5217SJeff Kirsher 
524adfc5217SJeff Kirsher 		bp->link_params.req_line_speed[cfg_idx] = speed;
525adfc5217SJeff Kirsher 		bp->link_params.req_duplex[cfg_idx] = cmd->duplex;
526adfc5217SJeff Kirsher 		bp->port.advertising[cfg_idx] = advertising;
527adfc5217SJeff Kirsher 	}
528adfc5217SJeff Kirsher 
529adfc5217SJeff Kirsher 	DP(NETIF_MSG_LINK, "req_line_speed %d\n"
530f1deab50SJoe Perches 	   "  req_duplex %d  advertising 0x%x\n",
531adfc5217SJeff Kirsher 	   bp->link_params.req_line_speed[cfg_idx],
532adfc5217SJeff Kirsher 	   bp->link_params.req_duplex[cfg_idx],
533adfc5217SJeff Kirsher 	   bp->port.advertising[cfg_idx]);
534adfc5217SJeff Kirsher 
535adfc5217SJeff Kirsher 	/* Set new config */
536adfc5217SJeff Kirsher 	bp->link_params.multi_phy_config = new_multi_phy_config;
537adfc5217SJeff Kirsher 	if (netif_running(dev)) {
538adfc5217SJeff Kirsher 		bnx2x_stats_handle(bp, STATS_EVENT_STOP);
539adfc5217SJeff Kirsher 		bnx2x_link_set(bp);
540adfc5217SJeff Kirsher 	}
541adfc5217SJeff Kirsher 
542adfc5217SJeff Kirsher 	return 0;
543adfc5217SJeff Kirsher }
544adfc5217SJeff Kirsher 
545adfc5217SJeff Kirsher #define IS_E1_ONLINE(info)	(((info) & RI_E1_ONLINE) == RI_E1_ONLINE)
546adfc5217SJeff Kirsher #define IS_E1H_ONLINE(info)	(((info) & RI_E1H_ONLINE) == RI_E1H_ONLINE)
547adfc5217SJeff Kirsher #define IS_E2_ONLINE(info)	(((info) & RI_E2_ONLINE) == RI_E2_ONLINE)
548adfc5217SJeff Kirsher #define IS_E3_ONLINE(info)	(((info) & RI_E3_ONLINE) == RI_E3_ONLINE)
549adfc5217SJeff Kirsher #define IS_E3B0_ONLINE(info)	(((info) & RI_E3B0_ONLINE) == RI_E3B0_ONLINE)
550adfc5217SJeff Kirsher 
551adfc5217SJeff Kirsher static inline bool bnx2x_is_reg_online(struct bnx2x *bp,
552adfc5217SJeff Kirsher 				       const struct reg_addr *reg_info)
553adfc5217SJeff Kirsher {
554adfc5217SJeff Kirsher 	if (CHIP_IS_E1(bp))
555adfc5217SJeff Kirsher 		return IS_E1_ONLINE(reg_info->info);
556adfc5217SJeff Kirsher 	else if (CHIP_IS_E1H(bp))
557adfc5217SJeff Kirsher 		return IS_E1H_ONLINE(reg_info->info);
558adfc5217SJeff Kirsher 	else if (CHIP_IS_E2(bp))
559adfc5217SJeff Kirsher 		return IS_E2_ONLINE(reg_info->info);
560adfc5217SJeff Kirsher 	else if (CHIP_IS_E3A0(bp))
561adfc5217SJeff Kirsher 		return IS_E3_ONLINE(reg_info->info);
562adfc5217SJeff Kirsher 	else if (CHIP_IS_E3B0(bp))
563adfc5217SJeff Kirsher 		return IS_E3B0_ONLINE(reg_info->info);
564adfc5217SJeff Kirsher 	else
565adfc5217SJeff Kirsher 		return false;
566adfc5217SJeff Kirsher }
567adfc5217SJeff Kirsher 
568adfc5217SJeff Kirsher /******* Paged registers info selectors ********/
569adfc5217SJeff Kirsher static inline const u32 *__bnx2x_get_page_addr_ar(struct bnx2x *bp)
570adfc5217SJeff Kirsher {
571adfc5217SJeff Kirsher 	if (CHIP_IS_E2(bp))
572adfc5217SJeff Kirsher 		return page_vals_e2;
573adfc5217SJeff Kirsher 	else if (CHIP_IS_E3(bp))
574adfc5217SJeff Kirsher 		return page_vals_e3;
575adfc5217SJeff Kirsher 	else
576adfc5217SJeff Kirsher 		return NULL;
577adfc5217SJeff Kirsher }
578adfc5217SJeff Kirsher 
579adfc5217SJeff Kirsher static inline u32 __bnx2x_get_page_reg_num(struct bnx2x *bp)
580adfc5217SJeff Kirsher {
581adfc5217SJeff Kirsher 	if (CHIP_IS_E2(bp))
582adfc5217SJeff Kirsher 		return PAGE_MODE_VALUES_E2;
583adfc5217SJeff Kirsher 	else if (CHIP_IS_E3(bp))
584adfc5217SJeff Kirsher 		return PAGE_MODE_VALUES_E3;
585adfc5217SJeff Kirsher 	else
586adfc5217SJeff Kirsher 		return 0;
587adfc5217SJeff Kirsher }
588adfc5217SJeff Kirsher 
589adfc5217SJeff Kirsher static inline const u32 *__bnx2x_get_page_write_ar(struct bnx2x *bp)
590adfc5217SJeff Kirsher {
591adfc5217SJeff Kirsher 	if (CHIP_IS_E2(bp))
592adfc5217SJeff Kirsher 		return page_write_regs_e2;
593adfc5217SJeff Kirsher 	else if (CHIP_IS_E3(bp))
594adfc5217SJeff Kirsher 		return page_write_regs_e3;
595adfc5217SJeff Kirsher 	else
596adfc5217SJeff Kirsher 		return NULL;
597adfc5217SJeff Kirsher }
598adfc5217SJeff Kirsher 
599adfc5217SJeff Kirsher static inline u32 __bnx2x_get_page_write_num(struct bnx2x *bp)
600adfc5217SJeff Kirsher {
601adfc5217SJeff Kirsher 	if (CHIP_IS_E2(bp))
602adfc5217SJeff Kirsher 		return PAGE_WRITE_REGS_E2;
603adfc5217SJeff Kirsher 	else if (CHIP_IS_E3(bp))
604adfc5217SJeff Kirsher 		return PAGE_WRITE_REGS_E3;
605adfc5217SJeff Kirsher 	else
606adfc5217SJeff Kirsher 		return 0;
607adfc5217SJeff Kirsher }
608adfc5217SJeff Kirsher 
609adfc5217SJeff Kirsher static inline const struct reg_addr *__bnx2x_get_page_read_ar(struct bnx2x *bp)
610adfc5217SJeff Kirsher {
611adfc5217SJeff Kirsher 	if (CHIP_IS_E2(bp))
612adfc5217SJeff Kirsher 		return page_read_regs_e2;
613adfc5217SJeff Kirsher 	else if (CHIP_IS_E3(bp))
614adfc5217SJeff Kirsher 		return page_read_regs_e3;
615adfc5217SJeff Kirsher 	else
616adfc5217SJeff Kirsher 		return NULL;
617adfc5217SJeff Kirsher }
618adfc5217SJeff Kirsher 
619adfc5217SJeff Kirsher static inline u32 __bnx2x_get_page_read_num(struct bnx2x *bp)
620adfc5217SJeff Kirsher {
621adfc5217SJeff Kirsher 	if (CHIP_IS_E2(bp))
622adfc5217SJeff Kirsher 		return PAGE_READ_REGS_E2;
623adfc5217SJeff Kirsher 	else if (CHIP_IS_E3(bp))
624adfc5217SJeff Kirsher 		return PAGE_READ_REGS_E3;
625adfc5217SJeff Kirsher 	else
626adfc5217SJeff Kirsher 		return 0;
627adfc5217SJeff Kirsher }
628adfc5217SJeff Kirsher 
629adfc5217SJeff Kirsher static inline int __bnx2x_get_regs_len(struct bnx2x *bp)
630adfc5217SJeff Kirsher {
631adfc5217SJeff Kirsher 	int num_pages = __bnx2x_get_page_reg_num(bp);
632adfc5217SJeff Kirsher 	int page_write_num = __bnx2x_get_page_write_num(bp);
633adfc5217SJeff Kirsher 	const struct reg_addr *page_read_addr = __bnx2x_get_page_read_ar(bp);
634adfc5217SJeff Kirsher 	int page_read_num = __bnx2x_get_page_read_num(bp);
635adfc5217SJeff Kirsher 	int regdump_len = 0;
636adfc5217SJeff Kirsher 	int i, j, k;
637adfc5217SJeff Kirsher 
638adfc5217SJeff Kirsher 	for (i = 0; i < REGS_COUNT; i++)
639adfc5217SJeff Kirsher 		if (bnx2x_is_reg_online(bp, &reg_addrs[i]))
640adfc5217SJeff Kirsher 			regdump_len += reg_addrs[i].size;
641adfc5217SJeff Kirsher 
642adfc5217SJeff Kirsher 	for (i = 0; i < num_pages; i++)
643adfc5217SJeff Kirsher 		for (j = 0; j < page_write_num; j++)
644adfc5217SJeff Kirsher 			for (k = 0; k < page_read_num; k++)
645adfc5217SJeff Kirsher 				if (bnx2x_is_reg_online(bp, &page_read_addr[k]))
646adfc5217SJeff Kirsher 					regdump_len += page_read_addr[k].size;
647adfc5217SJeff Kirsher 
648adfc5217SJeff Kirsher 	return regdump_len;
649adfc5217SJeff Kirsher }
650adfc5217SJeff Kirsher 
651adfc5217SJeff Kirsher static int bnx2x_get_regs_len(struct net_device *dev)
652adfc5217SJeff Kirsher {
653adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
654adfc5217SJeff Kirsher 	int regdump_len = 0;
655adfc5217SJeff Kirsher 
656adfc5217SJeff Kirsher 	regdump_len = __bnx2x_get_regs_len(bp);
657adfc5217SJeff Kirsher 	regdump_len *= 4;
658adfc5217SJeff Kirsher 	regdump_len += sizeof(struct dump_hdr);
659adfc5217SJeff Kirsher 
660adfc5217SJeff Kirsher 	return regdump_len;
661adfc5217SJeff Kirsher }
662adfc5217SJeff Kirsher 
663adfc5217SJeff Kirsher /**
664adfc5217SJeff Kirsher  * bnx2x_read_pages_regs - read "paged" registers
665adfc5217SJeff Kirsher  *
666adfc5217SJeff Kirsher  * @bp		device handle
667adfc5217SJeff Kirsher  * @p		output buffer
668adfc5217SJeff Kirsher  *
669adfc5217SJeff Kirsher  * Reads "paged" memories: memories that may only be read by first writing to a
670adfc5217SJeff Kirsher  * specific address ("write address") and then reading from a specific address
671adfc5217SJeff Kirsher  * ("read address"). There may be more than one write address per "page" and
672adfc5217SJeff Kirsher  * more than one read address per write address.
673adfc5217SJeff Kirsher  */
674adfc5217SJeff Kirsher static inline void bnx2x_read_pages_regs(struct bnx2x *bp, u32 *p)
675adfc5217SJeff Kirsher {
676adfc5217SJeff Kirsher 	u32 i, j, k, n;
677adfc5217SJeff Kirsher 	/* addresses of the paged registers */
678adfc5217SJeff Kirsher 	const u32 *page_addr = __bnx2x_get_page_addr_ar(bp);
679adfc5217SJeff Kirsher 	/* number of paged registers */
680adfc5217SJeff Kirsher 	int num_pages = __bnx2x_get_page_reg_num(bp);
681adfc5217SJeff Kirsher 	/* write addresses */
682adfc5217SJeff Kirsher 	const u32 *write_addr = __bnx2x_get_page_write_ar(bp);
683adfc5217SJeff Kirsher 	/* number of write addresses */
684adfc5217SJeff Kirsher 	int write_num = __bnx2x_get_page_write_num(bp);
685adfc5217SJeff Kirsher 	/* read addresses info */
686adfc5217SJeff Kirsher 	const struct reg_addr *read_addr = __bnx2x_get_page_read_ar(bp);
687adfc5217SJeff Kirsher 	/* number of read addresses */
688adfc5217SJeff Kirsher 	int read_num = __bnx2x_get_page_read_num(bp);
689adfc5217SJeff Kirsher 
690adfc5217SJeff Kirsher 	for (i = 0; i < num_pages; i++) {
691adfc5217SJeff Kirsher 		for (j = 0; j < write_num; j++) {
692adfc5217SJeff Kirsher 			REG_WR(bp, write_addr[j], page_addr[i]);
693adfc5217SJeff Kirsher 			for (k = 0; k < read_num; k++)
694adfc5217SJeff Kirsher 				if (bnx2x_is_reg_online(bp, &read_addr[k]))
695adfc5217SJeff Kirsher 					for (n = 0; n <
696adfc5217SJeff Kirsher 					      read_addr[k].size; n++)
697adfc5217SJeff Kirsher 						*p++ = REG_RD(bp,
698adfc5217SJeff Kirsher 						       read_addr[k].addr + n*4);
699adfc5217SJeff Kirsher 		}
700adfc5217SJeff Kirsher 	}
701adfc5217SJeff Kirsher }
702adfc5217SJeff Kirsher 
703adfc5217SJeff Kirsher static inline void __bnx2x_get_regs(struct bnx2x *bp, u32 *p)
704adfc5217SJeff Kirsher {
705adfc5217SJeff Kirsher 	u32 i, j;
706adfc5217SJeff Kirsher 
707adfc5217SJeff Kirsher 	/* Read the regular registers */
708adfc5217SJeff Kirsher 	for (i = 0; i < REGS_COUNT; i++)
709adfc5217SJeff Kirsher 		if (bnx2x_is_reg_online(bp, &reg_addrs[i]))
710adfc5217SJeff Kirsher 			for (j = 0; j < reg_addrs[i].size; j++)
711adfc5217SJeff Kirsher 				*p++ = REG_RD(bp, reg_addrs[i].addr + j*4);
712adfc5217SJeff Kirsher 
713adfc5217SJeff Kirsher 	/* Read "paged" registes */
714adfc5217SJeff Kirsher 	bnx2x_read_pages_regs(bp, p);
715adfc5217SJeff Kirsher }
716adfc5217SJeff Kirsher 
717adfc5217SJeff Kirsher static void bnx2x_get_regs(struct net_device *dev,
718adfc5217SJeff Kirsher 			   struct ethtool_regs *regs, void *_p)
719adfc5217SJeff Kirsher {
720adfc5217SJeff Kirsher 	u32 *p = _p;
721adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
722adfc5217SJeff Kirsher 	struct dump_hdr dump_hdr = {0};
723adfc5217SJeff Kirsher 
724adfc5217SJeff Kirsher 	regs->version = 0;
725adfc5217SJeff Kirsher 	memset(p, 0, regs->len);
726adfc5217SJeff Kirsher 
727adfc5217SJeff Kirsher 	if (!netif_running(bp->dev))
728adfc5217SJeff Kirsher 		return;
729adfc5217SJeff Kirsher 
730adfc5217SJeff Kirsher 	/* Disable parity attentions as long as following dump may
731adfc5217SJeff Kirsher 	 * cause false alarms by reading never written registers. We
732adfc5217SJeff Kirsher 	 * will re-enable parity attentions right after the dump.
733adfc5217SJeff Kirsher 	 */
734adfc5217SJeff Kirsher 	bnx2x_disable_blocks_parity(bp);
735adfc5217SJeff Kirsher 
736adfc5217SJeff Kirsher 	dump_hdr.hdr_size = (sizeof(struct dump_hdr) / 4) - 1;
737adfc5217SJeff Kirsher 	dump_hdr.dump_sign = dump_sign_all;
738adfc5217SJeff Kirsher 	dump_hdr.xstorm_waitp = REG_RD(bp, XSTORM_WAITP_ADDR);
739adfc5217SJeff Kirsher 	dump_hdr.tstorm_waitp = REG_RD(bp, TSTORM_WAITP_ADDR);
740adfc5217SJeff Kirsher 	dump_hdr.ustorm_waitp = REG_RD(bp, USTORM_WAITP_ADDR);
741adfc5217SJeff Kirsher 	dump_hdr.cstorm_waitp = REG_RD(bp, CSTORM_WAITP_ADDR);
742adfc5217SJeff Kirsher 
743adfc5217SJeff Kirsher 	if (CHIP_IS_E1(bp))
744adfc5217SJeff Kirsher 		dump_hdr.info = RI_E1_ONLINE;
745adfc5217SJeff Kirsher 	else if (CHIP_IS_E1H(bp))
746adfc5217SJeff Kirsher 		dump_hdr.info = RI_E1H_ONLINE;
747adfc5217SJeff Kirsher 	else if (!CHIP_IS_E1x(bp))
748adfc5217SJeff Kirsher 		dump_hdr.info = RI_E2_ONLINE |
749adfc5217SJeff Kirsher 		(BP_PATH(bp) ? RI_PATH1_DUMP : RI_PATH0_DUMP);
750adfc5217SJeff Kirsher 
751adfc5217SJeff Kirsher 	memcpy(p, &dump_hdr, sizeof(struct dump_hdr));
752adfc5217SJeff Kirsher 	p += dump_hdr.hdr_size + 1;
753adfc5217SJeff Kirsher 
754adfc5217SJeff Kirsher 	/* Actually read the registers */
755adfc5217SJeff Kirsher 	__bnx2x_get_regs(bp, p);
756adfc5217SJeff Kirsher 
757adfc5217SJeff Kirsher 	/* Re-enable parity attentions */
758adfc5217SJeff Kirsher 	bnx2x_clear_blocks_parity(bp);
759adfc5217SJeff Kirsher 	bnx2x_enable_blocks_parity(bp);
760adfc5217SJeff Kirsher }
761adfc5217SJeff Kirsher 
762adfc5217SJeff Kirsher static void bnx2x_get_drvinfo(struct net_device *dev,
763adfc5217SJeff Kirsher 			      struct ethtool_drvinfo *info)
764adfc5217SJeff Kirsher {
765adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
766adfc5217SJeff Kirsher 	u8 phy_fw_ver[PHY_FW_VER_LEN];
767adfc5217SJeff Kirsher 
76868aad78cSRick Jones 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
76968aad78cSRick Jones 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
770adfc5217SJeff Kirsher 
771adfc5217SJeff Kirsher 	phy_fw_ver[0] = '\0';
772adfc5217SJeff Kirsher 	if (bp->port.pmf) {
773adfc5217SJeff Kirsher 		bnx2x_acquire_phy_lock(bp);
774adfc5217SJeff Kirsher 		bnx2x_get_ext_phy_fw_version(&bp->link_params,
775adfc5217SJeff Kirsher 					     (bp->state != BNX2X_STATE_CLOSED),
776adfc5217SJeff Kirsher 					     phy_fw_ver, PHY_FW_VER_LEN);
777adfc5217SJeff Kirsher 		bnx2x_release_phy_lock(bp);
778adfc5217SJeff Kirsher 	}
779adfc5217SJeff Kirsher 
78068aad78cSRick Jones 	strlcpy(info->fw_version, bp->fw_ver, sizeof(info->fw_version));
781adfc5217SJeff Kirsher 	snprintf(info->fw_version + strlen(bp->fw_ver), 32 - strlen(bp->fw_ver),
782adfc5217SJeff Kirsher 		 "bc %d.%d.%d%s%s",
783adfc5217SJeff Kirsher 		 (bp->common.bc_ver & 0xff0000) >> 16,
784adfc5217SJeff Kirsher 		 (bp->common.bc_ver & 0xff00) >> 8,
785adfc5217SJeff Kirsher 		 (bp->common.bc_ver & 0xff),
786adfc5217SJeff Kirsher 		 ((phy_fw_ver[0] != '\0') ? " phy " : ""), phy_fw_ver);
78768aad78cSRick Jones 	strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
788adfc5217SJeff Kirsher 	info->n_stats = BNX2X_NUM_STATS;
789adfc5217SJeff Kirsher 	info->testinfo_len = BNX2X_NUM_TESTS;
790adfc5217SJeff Kirsher 	info->eedump_len = bp->common.flash_size;
791adfc5217SJeff Kirsher 	info->regdump_len = bnx2x_get_regs_len(dev);
792adfc5217SJeff Kirsher }
793adfc5217SJeff Kirsher 
794adfc5217SJeff Kirsher static void bnx2x_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
795adfc5217SJeff Kirsher {
796adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
797adfc5217SJeff Kirsher 
798adfc5217SJeff Kirsher 	if (bp->flags & NO_WOL_FLAG) {
799adfc5217SJeff Kirsher 		wol->supported = 0;
800adfc5217SJeff Kirsher 		wol->wolopts = 0;
801adfc5217SJeff Kirsher 	} else {
802adfc5217SJeff Kirsher 		wol->supported = WAKE_MAGIC;
803adfc5217SJeff Kirsher 		if (bp->wol)
804adfc5217SJeff Kirsher 			wol->wolopts = WAKE_MAGIC;
805adfc5217SJeff Kirsher 		else
806adfc5217SJeff Kirsher 			wol->wolopts = 0;
807adfc5217SJeff Kirsher 	}
808adfc5217SJeff Kirsher 	memset(&wol->sopass, 0, sizeof(wol->sopass));
809adfc5217SJeff Kirsher }
810adfc5217SJeff Kirsher 
811adfc5217SJeff Kirsher static int bnx2x_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
812adfc5217SJeff Kirsher {
813adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
814adfc5217SJeff Kirsher 
815adfc5217SJeff Kirsher 	if (wol->wolopts & ~WAKE_MAGIC)
816adfc5217SJeff Kirsher 		return -EINVAL;
817adfc5217SJeff Kirsher 
818adfc5217SJeff Kirsher 	if (wol->wolopts & WAKE_MAGIC) {
819adfc5217SJeff Kirsher 		if (bp->flags & NO_WOL_FLAG)
820adfc5217SJeff Kirsher 			return -EINVAL;
821adfc5217SJeff Kirsher 
822adfc5217SJeff Kirsher 		bp->wol = 1;
823adfc5217SJeff Kirsher 	} else
824adfc5217SJeff Kirsher 		bp->wol = 0;
825adfc5217SJeff Kirsher 
826adfc5217SJeff Kirsher 	return 0;
827adfc5217SJeff Kirsher }
828adfc5217SJeff Kirsher 
829adfc5217SJeff Kirsher static u32 bnx2x_get_msglevel(struct net_device *dev)
830adfc5217SJeff Kirsher {
831adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
832adfc5217SJeff Kirsher 
833adfc5217SJeff Kirsher 	return bp->msg_enable;
834adfc5217SJeff Kirsher }
835adfc5217SJeff Kirsher 
836adfc5217SJeff Kirsher static void bnx2x_set_msglevel(struct net_device *dev, u32 level)
837adfc5217SJeff Kirsher {
838adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
839adfc5217SJeff Kirsher 
840adfc5217SJeff Kirsher 	if (capable(CAP_NET_ADMIN)) {
841adfc5217SJeff Kirsher 		/* dump MCP trace */
842adfc5217SJeff Kirsher 		if (level & BNX2X_MSG_MCP)
843adfc5217SJeff Kirsher 			bnx2x_fw_dump_lvl(bp, KERN_INFO);
844adfc5217SJeff Kirsher 		bp->msg_enable = level;
845adfc5217SJeff Kirsher 	}
846adfc5217SJeff Kirsher }
847adfc5217SJeff Kirsher 
848adfc5217SJeff Kirsher static int bnx2x_nway_reset(struct net_device *dev)
849adfc5217SJeff Kirsher {
850adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
851adfc5217SJeff Kirsher 
852adfc5217SJeff Kirsher 	if (!bp->port.pmf)
853adfc5217SJeff Kirsher 		return 0;
854adfc5217SJeff Kirsher 
855adfc5217SJeff Kirsher 	if (netif_running(dev)) {
856adfc5217SJeff Kirsher 		bnx2x_stats_handle(bp, STATS_EVENT_STOP);
857adfc5217SJeff Kirsher 		bnx2x_link_set(bp);
858adfc5217SJeff Kirsher 	}
859adfc5217SJeff Kirsher 
860adfc5217SJeff Kirsher 	return 0;
861adfc5217SJeff Kirsher }
862adfc5217SJeff Kirsher 
863adfc5217SJeff Kirsher static u32 bnx2x_get_link(struct net_device *dev)
864adfc5217SJeff Kirsher {
865adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
866adfc5217SJeff Kirsher 
867adfc5217SJeff Kirsher 	if (bp->flags & MF_FUNC_DIS || (bp->state != BNX2X_STATE_OPEN))
868adfc5217SJeff Kirsher 		return 0;
869adfc5217SJeff Kirsher 
870adfc5217SJeff Kirsher 	return bp->link_vars.link_up;
871adfc5217SJeff Kirsher }
872adfc5217SJeff Kirsher 
873adfc5217SJeff Kirsher static int bnx2x_get_eeprom_len(struct net_device *dev)
874adfc5217SJeff Kirsher {
875adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
876adfc5217SJeff Kirsher 
877adfc5217SJeff Kirsher 	return bp->common.flash_size;
878adfc5217SJeff Kirsher }
879adfc5217SJeff Kirsher 
880adfc5217SJeff Kirsher static int bnx2x_acquire_nvram_lock(struct bnx2x *bp)
881adfc5217SJeff Kirsher {
882adfc5217SJeff Kirsher 	int port = BP_PORT(bp);
883adfc5217SJeff Kirsher 	int count, i;
884adfc5217SJeff Kirsher 	u32 val = 0;
885adfc5217SJeff Kirsher 
886adfc5217SJeff Kirsher 	/* adjust timeout for emulation/FPGA */
887adfc5217SJeff Kirsher 	count = BNX2X_NVRAM_TIMEOUT_COUNT;
888adfc5217SJeff Kirsher 	if (CHIP_REV_IS_SLOW(bp))
889adfc5217SJeff Kirsher 		count *= 100;
890adfc5217SJeff Kirsher 
891adfc5217SJeff Kirsher 	/* request access to nvram interface */
892adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_SW_ARB,
893adfc5217SJeff Kirsher 	       (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
894adfc5217SJeff Kirsher 
895adfc5217SJeff Kirsher 	for (i = 0; i < count*10; i++) {
896adfc5217SJeff Kirsher 		val = REG_RD(bp, MCP_REG_MCPR_NVM_SW_ARB);
897adfc5217SJeff Kirsher 		if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))
898adfc5217SJeff Kirsher 			break;
899adfc5217SJeff Kirsher 
900adfc5217SJeff Kirsher 		udelay(5);
901adfc5217SJeff Kirsher 	}
902adfc5217SJeff Kirsher 
903adfc5217SJeff Kirsher 	if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
904adfc5217SJeff Kirsher 		DP(BNX2X_MSG_NVM, "cannot get access to nvram interface\n");
905adfc5217SJeff Kirsher 		return -EBUSY;
906adfc5217SJeff Kirsher 	}
907adfc5217SJeff Kirsher 
908adfc5217SJeff Kirsher 	return 0;
909adfc5217SJeff Kirsher }
910adfc5217SJeff Kirsher 
911adfc5217SJeff Kirsher static int bnx2x_release_nvram_lock(struct bnx2x *bp)
912adfc5217SJeff Kirsher {
913adfc5217SJeff Kirsher 	int port = BP_PORT(bp);
914adfc5217SJeff Kirsher 	int count, i;
915adfc5217SJeff Kirsher 	u32 val = 0;
916adfc5217SJeff Kirsher 
917adfc5217SJeff Kirsher 	/* adjust timeout for emulation/FPGA */
918adfc5217SJeff Kirsher 	count = BNX2X_NVRAM_TIMEOUT_COUNT;
919adfc5217SJeff Kirsher 	if (CHIP_REV_IS_SLOW(bp))
920adfc5217SJeff Kirsher 		count *= 100;
921adfc5217SJeff Kirsher 
922adfc5217SJeff Kirsher 	/* relinquish nvram interface */
923adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_SW_ARB,
924adfc5217SJeff Kirsher 	       (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
925adfc5217SJeff Kirsher 
926adfc5217SJeff Kirsher 	for (i = 0; i < count*10; i++) {
927adfc5217SJeff Kirsher 		val = REG_RD(bp, MCP_REG_MCPR_NVM_SW_ARB);
928adfc5217SJeff Kirsher 		if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)))
929adfc5217SJeff Kirsher 			break;
930adfc5217SJeff Kirsher 
931adfc5217SJeff Kirsher 		udelay(5);
932adfc5217SJeff Kirsher 	}
933adfc5217SJeff Kirsher 
934adfc5217SJeff Kirsher 	if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
935adfc5217SJeff Kirsher 		DP(BNX2X_MSG_NVM, "cannot free access to nvram interface\n");
936adfc5217SJeff Kirsher 		return -EBUSY;
937adfc5217SJeff Kirsher 	}
938adfc5217SJeff Kirsher 
939adfc5217SJeff Kirsher 	return 0;
940adfc5217SJeff Kirsher }
941adfc5217SJeff Kirsher 
942adfc5217SJeff Kirsher static void bnx2x_enable_nvram_access(struct bnx2x *bp)
943adfc5217SJeff Kirsher {
944adfc5217SJeff Kirsher 	u32 val;
945adfc5217SJeff Kirsher 
946adfc5217SJeff Kirsher 	val = REG_RD(bp, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
947adfc5217SJeff Kirsher 
948adfc5217SJeff Kirsher 	/* enable both bits, even on read */
949adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
950adfc5217SJeff Kirsher 	       (val | MCPR_NVM_ACCESS_ENABLE_EN |
951adfc5217SJeff Kirsher 		      MCPR_NVM_ACCESS_ENABLE_WR_EN));
952adfc5217SJeff Kirsher }
953adfc5217SJeff Kirsher 
954adfc5217SJeff Kirsher static void bnx2x_disable_nvram_access(struct bnx2x *bp)
955adfc5217SJeff Kirsher {
956adfc5217SJeff Kirsher 	u32 val;
957adfc5217SJeff Kirsher 
958adfc5217SJeff Kirsher 	val = REG_RD(bp, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
959adfc5217SJeff Kirsher 
960adfc5217SJeff Kirsher 	/* disable both bits, even after read */
961adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
962adfc5217SJeff Kirsher 	       (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
963adfc5217SJeff Kirsher 			MCPR_NVM_ACCESS_ENABLE_WR_EN)));
964adfc5217SJeff Kirsher }
965adfc5217SJeff Kirsher 
966adfc5217SJeff Kirsher static int bnx2x_nvram_read_dword(struct bnx2x *bp, u32 offset, __be32 *ret_val,
967adfc5217SJeff Kirsher 				  u32 cmd_flags)
968adfc5217SJeff Kirsher {
969adfc5217SJeff Kirsher 	int count, i, rc;
970adfc5217SJeff Kirsher 	u32 val;
971adfc5217SJeff Kirsher 
972adfc5217SJeff Kirsher 	/* build the command word */
973adfc5217SJeff Kirsher 	cmd_flags |= MCPR_NVM_COMMAND_DOIT;
974adfc5217SJeff Kirsher 
975adfc5217SJeff Kirsher 	/* need to clear DONE bit separately */
976adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
977adfc5217SJeff Kirsher 
978adfc5217SJeff Kirsher 	/* address of the NVRAM to read from */
979adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_ADDR,
980adfc5217SJeff Kirsher 	       (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
981adfc5217SJeff Kirsher 
982adfc5217SJeff Kirsher 	/* issue a read command */
983adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
984adfc5217SJeff Kirsher 
985adfc5217SJeff Kirsher 	/* adjust timeout for emulation/FPGA */
986adfc5217SJeff Kirsher 	count = BNX2X_NVRAM_TIMEOUT_COUNT;
987adfc5217SJeff Kirsher 	if (CHIP_REV_IS_SLOW(bp))
988adfc5217SJeff Kirsher 		count *= 100;
989adfc5217SJeff Kirsher 
990adfc5217SJeff Kirsher 	/* wait for completion */
991adfc5217SJeff Kirsher 	*ret_val = 0;
992adfc5217SJeff Kirsher 	rc = -EBUSY;
993adfc5217SJeff Kirsher 	for (i = 0; i < count; i++) {
994adfc5217SJeff Kirsher 		udelay(5);
995adfc5217SJeff Kirsher 		val = REG_RD(bp, MCP_REG_MCPR_NVM_COMMAND);
996adfc5217SJeff Kirsher 
997adfc5217SJeff Kirsher 		if (val & MCPR_NVM_COMMAND_DONE) {
998adfc5217SJeff Kirsher 			val = REG_RD(bp, MCP_REG_MCPR_NVM_READ);
999adfc5217SJeff Kirsher 			/* we read nvram data in cpu order
1000adfc5217SJeff Kirsher 			 * but ethtool sees it as an array of bytes
1001adfc5217SJeff Kirsher 			 * converting to big-endian will do the work */
1002adfc5217SJeff Kirsher 			*ret_val = cpu_to_be32(val);
1003adfc5217SJeff Kirsher 			rc = 0;
1004adfc5217SJeff Kirsher 			break;
1005adfc5217SJeff Kirsher 		}
1006adfc5217SJeff Kirsher 	}
1007adfc5217SJeff Kirsher 
1008adfc5217SJeff Kirsher 	return rc;
1009adfc5217SJeff Kirsher }
1010adfc5217SJeff Kirsher 
1011adfc5217SJeff Kirsher static int bnx2x_nvram_read(struct bnx2x *bp, u32 offset, u8 *ret_buf,
1012adfc5217SJeff Kirsher 			    int buf_size)
1013adfc5217SJeff Kirsher {
1014adfc5217SJeff Kirsher 	int rc;
1015adfc5217SJeff Kirsher 	u32 cmd_flags;
1016adfc5217SJeff Kirsher 	__be32 val;
1017adfc5217SJeff Kirsher 
1018adfc5217SJeff Kirsher 	if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1019adfc5217SJeff Kirsher 		DP(BNX2X_MSG_NVM,
1020adfc5217SJeff Kirsher 		   "Invalid parameter: offset 0x%x  buf_size 0x%x\n",
1021adfc5217SJeff Kirsher 		   offset, buf_size);
1022adfc5217SJeff Kirsher 		return -EINVAL;
1023adfc5217SJeff Kirsher 	}
1024adfc5217SJeff Kirsher 
1025adfc5217SJeff Kirsher 	if (offset + buf_size > bp->common.flash_size) {
1026adfc5217SJeff Kirsher 		DP(BNX2X_MSG_NVM, "Invalid parameter: offset (0x%x) +"
1027adfc5217SJeff Kirsher 				  " buf_size (0x%x) > flash_size (0x%x)\n",
1028adfc5217SJeff Kirsher 		   offset, buf_size, bp->common.flash_size);
1029adfc5217SJeff Kirsher 		return -EINVAL;
1030adfc5217SJeff Kirsher 	}
1031adfc5217SJeff Kirsher 
1032adfc5217SJeff Kirsher 	/* request access to nvram interface */
1033adfc5217SJeff Kirsher 	rc = bnx2x_acquire_nvram_lock(bp);
1034adfc5217SJeff Kirsher 	if (rc)
1035adfc5217SJeff Kirsher 		return rc;
1036adfc5217SJeff Kirsher 
1037adfc5217SJeff Kirsher 	/* enable access to nvram interface */
1038adfc5217SJeff Kirsher 	bnx2x_enable_nvram_access(bp);
1039adfc5217SJeff Kirsher 
1040adfc5217SJeff Kirsher 	/* read the first word(s) */
1041adfc5217SJeff Kirsher 	cmd_flags = MCPR_NVM_COMMAND_FIRST;
1042adfc5217SJeff Kirsher 	while ((buf_size > sizeof(u32)) && (rc == 0)) {
1043adfc5217SJeff Kirsher 		rc = bnx2x_nvram_read_dword(bp, offset, &val, cmd_flags);
1044adfc5217SJeff Kirsher 		memcpy(ret_buf, &val, 4);
1045adfc5217SJeff Kirsher 
1046adfc5217SJeff Kirsher 		/* advance to the next dword */
1047adfc5217SJeff Kirsher 		offset += sizeof(u32);
1048adfc5217SJeff Kirsher 		ret_buf += sizeof(u32);
1049adfc5217SJeff Kirsher 		buf_size -= sizeof(u32);
1050adfc5217SJeff Kirsher 		cmd_flags = 0;
1051adfc5217SJeff Kirsher 	}
1052adfc5217SJeff Kirsher 
1053adfc5217SJeff Kirsher 	if (rc == 0) {
1054adfc5217SJeff Kirsher 		cmd_flags |= MCPR_NVM_COMMAND_LAST;
1055adfc5217SJeff Kirsher 		rc = bnx2x_nvram_read_dword(bp, offset, &val, cmd_flags);
1056adfc5217SJeff Kirsher 		memcpy(ret_buf, &val, 4);
1057adfc5217SJeff Kirsher 	}
1058adfc5217SJeff Kirsher 
1059adfc5217SJeff Kirsher 	/* disable access to nvram interface */
1060adfc5217SJeff Kirsher 	bnx2x_disable_nvram_access(bp);
1061adfc5217SJeff Kirsher 	bnx2x_release_nvram_lock(bp);
1062adfc5217SJeff Kirsher 
1063adfc5217SJeff Kirsher 	return rc;
1064adfc5217SJeff Kirsher }
1065adfc5217SJeff Kirsher 
1066adfc5217SJeff Kirsher static int bnx2x_get_eeprom(struct net_device *dev,
1067adfc5217SJeff Kirsher 			    struct ethtool_eeprom *eeprom, u8 *eebuf)
1068adfc5217SJeff Kirsher {
1069adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1070adfc5217SJeff Kirsher 	int rc;
1071adfc5217SJeff Kirsher 
1072adfc5217SJeff Kirsher 	if (!netif_running(dev))
1073adfc5217SJeff Kirsher 		return -EAGAIN;
1074adfc5217SJeff Kirsher 
1075adfc5217SJeff Kirsher 	DP(BNX2X_MSG_NVM, "ethtool_eeprom: cmd %d\n"
1076f1deab50SJoe Perches 	   "  magic 0x%x  offset 0x%x (%d)  len 0x%x (%d)\n",
1077adfc5217SJeff Kirsher 	   eeprom->cmd, eeprom->magic, eeprom->offset, eeprom->offset,
1078adfc5217SJeff Kirsher 	   eeprom->len, eeprom->len);
1079adfc5217SJeff Kirsher 
1080adfc5217SJeff Kirsher 	/* parameters already validated in ethtool_get_eeprom */
1081adfc5217SJeff Kirsher 
1082adfc5217SJeff Kirsher 	rc = bnx2x_nvram_read(bp, eeprom->offset, eebuf, eeprom->len);
1083adfc5217SJeff Kirsher 
1084adfc5217SJeff Kirsher 	return rc;
1085adfc5217SJeff Kirsher }
1086adfc5217SJeff Kirsher 
1087adfc5217SJeff Kirsher static int bnx2x_nvram_write_dword(struct bnx2x *bp, u32 offset, u32 val,
1088adfc5217SJeff Kirsher 				   u32 cmd_flags)
1089adfc5217SJeff Kirsher {
1090adfc5217SJeff Kirsher 	int count, i, rc;
1091adfc5217SJeff Kirsher 
1092adfc5217SJeff Kirsher 	/* build the command word */
1093adfc5217SJeff Kirsher 	cmd_flags |= MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR;
1094adfc5217SJeff Kirsher 
1095adfc5217SJeff Kirsher 	/* need to clear DONE bit separately */
1096adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
1097adfc5217SJeff Kirsher 
1098adfc5217SJeff Kirsher 	/* write the data */
1099adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_WRITE, val);
1100adfc5217SJeff Kirsher 
1101adfc5217SJeff Kirsher 	/* address of the NVRAM to write to */
1102adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_ADDR,
1103adfc5217SJeff Kirsher 	       (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
1104adfc5217SJeff Kirsher 
1105adfc5217SJeff Kirsher 	/* issue the write command */
1106adfc5217SJeff Kirsher 	REG_WR(bp, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
1107adfc5217SJeff Kirsher 
1108adfc5217SJeff Kirsher 	/* adjust timeout for emulation/FPGA */
1109adfc5217SJeff Kirsher 	count = BNX2X_NVRAM_TIMEOUT_COUNT;
1110adfc5217SJeff Kirsher 	if (CHIP_REV_IS_SLOW(bp))
1111adfc5217SJeff Kirsher 		count *= 100;
1112adfc5217SJeff Kirsher 
1113adfc5217SJeff Kirsher 	/* wait for completion */
1114adfc5217SJeff Kirsher 	rc = -EBUSY;
1115adfc5217SJeff Kirsher 	for (i = 0; i < count; i++) {
1116adfc5217SJeff Kirsher 		udelay(5);
1117adfc5217SJeff Kirsher 		val = REG_RD(bp, MCP_REG_MCPR_NVM_COMMAND);
1118adfc5217SJeff Kirsher 		if (val & MCPR_NVM_COMMAND_DONE) {
1119adfc5217SJeff Kirsher 			rc = 0;
1120adfc5217SJeff Kirsher 			break;
1121adfc5217SJeff Kirsher 		}
1122adfc5217SJeff Kirsher 	}
1123adfc5217SJeff Kirsher 
1124adfc5217SJeff Kirsher 	return rc;
1125adfc5217SJeff Kirsher }
1126adfc5217SJeff Kirsher 
1127adfc5217SJeff Kirsher #define BYTE_OFFSET(offset)		(8 * (offset & 0x03))
1128adfc5217SJeff Kirsher 
1129adfc5217SJeff Kirsher static int bnx2x_nvram_write1(struct bnx2x *bp, u32 offset, u8 *data_buf,
1130adfc5217SJeff Kirsher 			      int buf_size)
1131adfc5217SJeff Kirsher {
1132adfc5217SJeff Kirsher 	int rc;
1133adfc5217SJeff Kirsher 	u32 cmd_flags;
1134adfc5217SJeff Kirsher 	u32 align_offset;
1135adfc5217SJeff Kirsher 	__be32 val;
1136adfc5217SJeff Kirsher 
1137adfc5217SJeff Kirsher 	if (offset + buf_size > bp->common.flash_size) {
1138adfc5217SJeff Kirsher 		DP(BNX2X_MSG_NVM, "Invalid parameter: offset (0x%x) +"
1139adfc5217SJeff Kirsher 				  " buf_size (0x%x) > flash_size (0x%x)\n",
1140adfc5217SJeff Kirsher 		   offset, buf_size, bp->common.flash_size);
1141adfc5217SJeff Kirsher 		return -EINVAL;
1142adfc5217SJeff Kirsher 	}
1143adfc5217SJeff Kirsher 
1144adfc5217SJeff Kirsher 	/* request access to nvram interface */
1145adfc5217SJeff Kirsher 	rc = bnx2x_acquire_nvram_lock(bp);
1146adfc5217SJeff Kirsher 	if (rc)
1147adfc5217SJeff Kirsher 		return rc;
1148adfc5217SJeff Kirsher 
1149adfc5217SJeff Kirsher 	/* enable access to nvram interface */
1150adfc5217SJeff Kirsher 	bnx2x_enable_nvram_access(bp);
1151adfc5217SJeff Kirsher 
1152adfc5217SJeff Kirsher 	cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
1153adfc5217SJeff Kirsher 	align_offset = (offset & ~0x03);
1154adfc5217SJeff Kirsher 	rc = bnx2x_nvram_read_dword(bp, align_offset, &val, cmd_flags);
1155adfc5217SJeff Kirsher 
1156adfc5217SJeff Kirsher 	if (rc == 0) {
1157adfc5217SJeff Kirsher 		val &= ~(0xff << BYTE_OFFSET(offset));
1158adfc5217SJeff Kirsher 		val |= (*data_buf << BYTE_OFFSET(offset));
1159adfc5217SJeff Kirsher 
1160adfc5217SJeff Kirsher 		/* nvram data is returned as an array of bytes
1161adfc5217SJeff Kirsher 		 * convert it back to cpu order */
1162adfc5217SJeff Kirsher 		val = be32_to_cpu(val);
1163adfc5217SJeff Kirsher 
1164adfc5217SJeff Kirsher 		rc = bnx2x_nvram_write_dword(bp, align_offset, val,
1165adfc5217SJeff Kirsher 					     cmd_flags);
1166adfc5217SJeff Kirsher 	}
1167adfc5217SJeff Kirsher 
1168adfc5217SJeff Kirsher 	/* disable access to nvram interface */
1169adfc5217SJeff Kirsher 	bnx2x_disable_nvram_access(bp);
1170adfc5217SJeff Kirsher 	bnx2x_release_nvram_lock(bp);
1171adfc5217SJeff Kirsher 
1172adfc5217SJeff Kirsher 	return rc;
1173adfc5217SJeff Kirsher }
1174adfc5217SJeff Kirsher 
1175adfc5217SJeff Kirsher static int bnx2x_nvram_write(struct bnx2x *bp, u32 offset, u8 *data_buf,
1176adfc5217SJeff Kirsher 			     int buf_size)
1177adfc5217SJeff Kirsher {
1178adfc5217SJeff Kirsher 	int rc;
1179adfc5217SJeff Kirsher 	u32 cmd_flags;
1180adfc5217SJeff Kirsher 	u32 val;
1181adfc5217SJeff Kirsher 	u32 written_so_far;
1182adfc5217SJeff Kirsher 
1183adfc5217SJeff Kirsher 	if (buf_size == 1)	/* ethtool */
1184adfc5217SJeff Kirsher 		return bnx2x_nvram_write1(bp, offset, data_buf, buf_size);
1185adfc5217SJeff Kirsher 
1186adfc5217SJeff Kirsher 	if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
1187adfc5217SJeff Kirsher 		DP(BNX2X_MSG_NVM,
1188adfc5217SJeff Kirsher 		   "Invalid parameter: offset 0x%x  buf_size 0x%x\n",
1189adfc5217SJeff Kirsher 		   offset, buf_size);
1190adfc5217SJeff Kirsher 		return -EINVAL;
1191adfc5217SJeff Kirsher 	}
1192adfc5217SJeff Kirsher 
1193adfc5217SJeff Kirsher 	if (offset + buf_size > bp->common.flash_size) {
1194adfc5217SJeff Kirsher 		DP(BNX2X_MSG_NVM, "Invalid parameter: offset (0x%x) +"
1195adfc5217SJeff Kirsher 				  " buf_size (0x%x) > flash_size (0x%x)\n",
1196adfc5217SJeff Kirsher 		   offset, buf_size, bp->common.flash_size);
1197adfc5217SJeff Kirsher 		return -EINVAL;
1198adfc5217SJeff Kirsher 	}
1199adfc5217SJeff Kirsher 
1200adfc5217SJeff Kirsher 	/* request access to nvram interface */
1201adfc5217SJeff Kirsher 	rc = bnx2x_acquire_nvram_lock(bp);
1202adfc5217SJeff Kirsher 	if (rc)
1203adfc5217SJeff Kirsher 		return rc;
1204adfc5217SJeff Kirsher 
1205adfc5217SJeff Kirsher 	/* enable access to nvram interface */
1206adfc5217SJeff Kirsher 	bnx2x_enable_nvram_access(bp);
1207adfc5217SJeff Kirsher 
1208adfc5217SJeff Kirsher 	written_so_far = 0;
1209adfc5217SJeff Kirsher 	cmd_flags = MCPR_NVM_COMMAND_FIRST;
1210adfc5217SJeff Kirsher 	while ((written_so_far < buf_size) && (rc == 0)) {
1211adfc5217SJeff Kirsher 		if (written_so_far == (buf_size - sizeof(u32)))
1212adfc5217SJeff Kirsher 			cmd_flags |= MCPR_NVM_COMMAND_LAST;
1213adfc5217SJeff Kirsher 		else if (((offset + 4) % BNX2X_NVRAM_PAGE_SIZE) == 0)
1214adfc5217SJeff Kirsher 			cmd_flags |= MCPR_NVM_COMMAND_LAST;
1215adfc5217SJeff Kirsher 		else if ((offset % BNX2X_NVRAM_PAGE_SIZE) == 0)
1216adfc5217SJeff Kirsher 			cmd_flags |= MCPR_NVM_COMMAND_FIRST;
1217adfc5217SJeff Kirsher 
1218adfc5217SJeff Kirsher 		memcpy(&val, data_buf, 4);
1219adfc5217SJeff Kirsher 
1220adfc5217SJeff Kirsher 		rc = bnx2x_nvram_write_dword(bp, offset, val, cmd_flags);
1221adfc5217SJeff Kirsher 
1222adfc5217SJeff Kirsher 		/* advance to the next dword */
1223adfc5217SJeff Kirsher 		offset += sizeof(u32);
1224adfc5217SJeff Kirsher 		data_buf += sizeof(u32);
1225adfc5217SJeff Kirsher 		written_so_far += sizeof(u32);
1226adfc5217SJeff Kirsher 		cmd_flags = 0;
1227adfc5217SJeff Kirsher 	}
1228adfc5217SJeff Kirsher 
1229adfc5217SJeff Kirsher 	/* disable access to nvram interface */
1230adfc5217SJeff Kirsher 	bnx2x_disable_nvram_access(bp);
1231adfc5217SJeff Kirsher 	bnx2x_release_nvram_lock(bp);
1232adfc5217SJeff Kirsher 
1233adfc5217SJeff Kirsher 	return rc;
1234adfc5217SJeff Kirsher }
1235adfc5217SJeff Kirsher 
1236adfc5217SJeff Kirsher static int bnx2x_set_eeprom(struct net_device *dev,
1237adfc5217SJeff Kirsher 			    struct ethtool_eeprom *eeprom, u8 *eebuf)
1238adfc5217SJeff Kirsher {
1239adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1240adfc5217SJeff Kirsher 	int port = BP_PORT(bp);
1241adfc5217SJeff Kirsher 	int rc = 0;
1242adfc5217SJeff Kirsher 	u32 ext_phy_config;
1243adfc5217SJeff Kirsher 	if (!netif_running(dev))
1244adfc5217SJeff Kirsher 		return -EAGAIN;
1245adfc5217SJeff Kirsher 
1246adfc5217SJeff Kirsher 	DP(BNX2X_MSG_NVM, "ethtool_eeprom: cmd %d\n"
1247f1deab50SJoe Perches 	   "  magic 0x%x  offset 0x%x (%d)  len 0x%x (%d)\n",
1248adfc5217SJeff Kirsher 	   eeprom->cmd, eeprom->magic, eeprom->offset, eeprom->offset,
1249adfc5217SJeff Kirsher 	   eeprom->len, eeprom->len);
1250adfc5217SJeff Kirsher 
1251adfc5217SJeff Kirsher 	/* parameters already validated in ethtool_set_eeprom */
1252adfc5217SJeff Kirsher 
1253adfc5217SJeff Kirsher 	/* PHY eeprom can be accessed only by the PMF */
1254adfc5217SJeff Kirsher 	if ((eeprom->magic >= 0x50485900) && (eeprom->magic <= 0x504859FF) &&
1255adfc5217SJeff Kirsher 	    !bp->port.pmf)
1256adfc5217SJeff Kirsher 		return -EINVAL;
1257adfc5217SJeff Kirsher 
1258adfc5217SJeff Kirsher 	ext_phy_config =
1259adfc5217SJeff Kirsher 		SHMEM_RD(bp,
1260adfc5217SJeff Kirsher 			 dev_info.port_hw_config[port].external_phy_config);
1261adfc5217SJeff Kirsher 
1262adfc5217SJeff Kirsher 	if (eeprom->magic == 0x50485950) {
1263adfc5217SJeff Kirsher 		/* 'PHYP' (0x50485950): prepare phy for FW upgrade */
1264adfc5217SJeff Kirsher 		bnx2x_stats_handle(bp, STATS_EVENT_STOP);
1265adfc5217SJeff Kirsher 
1266adfc5217SJeff Kirsher 		bnx2x_acquire_phy_lock(bp);
1267adfc5217SJeff Kirsher 		rc |= bnx2x_link_reset(&bp->link_params,
1268adfc5217SJeff Kirsher 				       &bp->link_vars, 0);
1269adfc5217SJeff Kirsher 		if (XGXS_EXT_PHY_TYPE(ext_phy_config) ==
1270adfc5217SJeff Kirsher 					PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101)
1271adfc5217SJeff Kirsher 			bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_0,
1272adfc5217SJeff Kirsher 				       MISC_REGISTERS_GPIO_HIGH, port);
1273adfc5217SJeff Kirsher 		bnx2x_release_phy_lock(bp);
1274adfc5217SJeff Kirsher 		bnx2x_link_report(bp);
1275adfc5217SJeff Kirsher 
1276adfc5217SJeff Kirsher 	} else if (eeprom->magic == 0x50485952) {
1277adfc5217SJeff Kirsher 		/* 'PHYR' (0x50485952): re-init link after FW upgrade */
1278adfc5217SJeff Kirsher 		if (bp->state == BNX2X_STATE_OPEN) {
1279adfc5217SJeff Kirsher 			bnx2x_acquire_phy_lock(bp);
1280adfc5217SJeff Kirsher 			rc |= bnx2x_link_reset(&bp->link_params,
1281adfc5217SJeff Kirsher 					       &bp->link_vars, 1);
1282adfc5217SJeff Kirsher 
1283adfc5217SJeff Kirsher 			rc |= bnx2x_phy_init(&bp->link_params,
1284adfc5217SJeff Kirsher 					     &bp->link_vars);
1285adfc5217SJeff Kirsher 			bnx2x_release_phy_lock(bp);
1286adfc5217SJeff Kirsher 			bnx2x_calc_fc_adv(bp);
1287adfc5217SJeff Kirsher 		}
1288adfc5217SJeff Kirsher 	} else if (eeprom->magic == 0x53985943) {
1289adfc5217SJeff Kirsher 		/* 'PHYC' (0x53985943): PHY FW upgrade completed */
1290adfc5217SJeff Kirsher 		if (XGXS_EXT_PHY_TYPE(ext_phy_config) ==
1291adfc5217SJeff Kirsher 				       PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101) {
1292adfc5217SJeff Kirsher 
1293adfc5217SJeff Kirsher 			/* DSP Remove Download Mode */
1294adfc5217SJeff Kirsher 			bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_0,
1295adfc5217SJeff Kirsher 				       MISC_REGISTERS_GPIO_LOW, port);
1296adfc5217SJeff Kirsher 
1297adfc5217SJeff Kirsher 			bnx2x_acquire_phy_lock(bp);
1298adfc5217SJeff Kirsher 
1299adfc5217SJeff Kirsher 			bnx2x_sfx7101_sp_sw_reset(bp,
1300adfc5217SJeff Kirsher 						&bp->link_params.phy[EXT_PHY1]);
1301adfc5217SJeff Kirsher 
1302adfc5217SJeff Kirsher 			/* wait 0.5 sec to allow it to run */
1303adfc5217SJeff Kirsher 			msleep(500);
1304adfc5217SJeff Kirsher 			bnx2x_ext_phy_hw_reset(bp, port);
1305adfc5217SJeff Kirsher 			msleep(500);
1306adfc5217SJeff Kirsher 			bnx2x_release_phy_lock(bp);
1307adfc5217SJeff Kirsher 		}
1308adfc5217SJeff Kirsher 	} else
1309adfc5217SJeff Kirsher 		rc = bnx2x_nvram_write(bp, eeprom->offset, eebuf, eeprom->len);
1310adfc5217SJeff Kirsher 
1311adfc5217SJeff Kirsher 	return rc;
1312adfc5217SJeff Kirsher }
1313adfc5217SJeff Kirsher 
1314adfc5217SJeff Kirsher static int bnx2x_get_coalesce(struct net_device *dev,
1315adfc5217SJeff Kirsher 			      struct ethtool_coalesce *coal)
1316adfc5217SJeff Kirsher {
1317adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1318adfc5217SJeff Kirsher 
1319adfc5217SJeff Kirsher 	memset(coal, 0, sizeof(struct ethtool_coalesce));
1320adfc5217SJeff Kirsher 
1321adfc5217SJeff Kirsher 	coal->rx_coalesce_usecs = bp->rx_ticks;
1322adfc5217SJeff Kirsher 	coal->tx_coalesce_usecs = bp->tx_ticks;
1323adfc5217SJeff Kirsher 
1324adfc5217SJeff Kirsher 	return 0;
1325adfc5217SJeff Kirsher }
1326adfc5217SJeff Kirsher 
1327adfc5217SJeff Kirsher static int bnx2x_set_coalesce(struct net_device *dev,
1328adfc5217SJeff Kirsher 			      struct ethtool_coalesce *coal)
1329adfc5217SJeff Kirsher {
1330adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1331adfc5217SJeff Kirsher 
1332adfc5217SJeff Kirsher 	bp->rx_ticks = (u16)coal->rx_coalesce_usecs;
1333adfc5217SJeff Kirsher 	if (bp->rx_ticks > BNX2X_MAX_COALESCE_TOUT)
1334adfc5217SJeff Kirsher 		bp->rx_ticks = BNX2X_MAX_COALESCE_TOUT;
1335adfc5217SJeff Kirsher 
1336adfc5217SJeff Kirsher 	bp->tx_ticks = (u16)coal->tx_coalesce_usecs;
1337adfc5217SJeff Kirsher 	if (bp->tx_ticks > BNX2X_MAX_COALESCE_TOUT)
1338adfc5217SJeff Kirsher 		bp->tx_ticks = BNX2X_MAX_COALESCE_TOUT;
1339adfc5217SJeff Kirsher 
1340adfc5217SJeff Kirsher 	if (netif_running(dev))
1341adfc5217SJeff Kirsher 		bnx2x_update_coalesce(bp);
1342adfc5217SJeff Kirsher 
1343adfc5217SJeff Kirsher 	return 0;
1344adfc5217SJeff Kirsher }
1345adfc5217SJeff Kirsher 
1346adfc5217SJeff Kirsher static void bnx2x_get_ringparam(struct net_device *dev,
1347adfc5217SJeff Kirsher 				struct ethtool_ringparam *ering)
1348adfc5217SJeff Kirsher {
1349adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1350adfc5217SJeff Kirsher 
1351adfc5217SJeff Kirsher 	ering->rx_max_pending = MAX_RX_AVAIL;
1352adfc5217SJeff Kirsher 
1353adfc5217SJeff Kirsher 	if (bp->rx_ring_size)
1354adfc5217SJeff Kirsher 		ering->rx_pending = bp->rx_ring_size;
1355adfc5217SJeff Kirsher 	else
1356adfc5217SJeff Kirsher 		ering->rx_pending = MAX_RX_AVAIL;
1357adfc5217SJeff Kirsher 
1358adfc5217SJeff Kirsher 	ering->tx_max_pending = MAX_TX_AVAIL;
1359adfc5217SJeff Kirsher 	ering->tx_pending = bp->tx_ring_size;
1360adfc5217SJeff Kirsher }
1361adfc5217SJeff Kirsher 
1362adfc5217SJeff Kirsher static int bnx2x_set_ringparam(struct net_device *dev,
1363adfc5217SJeff Kirsher 			       struct ethtool_ringparam *ering)
1364adfc5217SJeff Kirsher {
1365adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1366adfc5217SJeff Kirsher 
1367adfc5217SJeff Kirsher 	if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
1368f1deab50SJoe Perches 		pr_err("Handling parity error recovery. Try again later\n");
1369adfc5217SJeff Kirsher 		return -EAGAIN;
1370adfc5217SJeff Kirsher 	}
1371adfc5217SJeff Kirsher 
1372adfc5217SJeff Kirsher 	if ((ering->rx_pending > MAX_RX_AVAIL) ||
1373adfc5217SJeff Kirsher 	    (ering->rx_pending < (bp->disable_tpa ? MIN_RX_SIZE_NONTPA :
1374adfc5217SJeff Kirsher 						    MIN_RX_SIZE_TPA)) ||
1375adfc5217SJeff Kirsher 	    (ering->tx_pending > MAX_TX_AVAIL) ||
1376adfc5217SJeff Kirsher 	    (ering->tx_pending <= MAX_SKB_FRAGS + 4))
1377adfc5217SJeff Kirsher 		return -EINVAL;
1378adfc5217SJeff Kirsher 
1379adfc5217SJeff Kirsher 	bp->rx_ring_size = ering->rx_pending;
1380adfc5217SJeff Kirsher 	bp->tx_ring_size = ering->tx_pending;
1381adfc5217SJeff Kirsher 
1382adfc5217SJeff Kirsher 	return bnx2x_reload_if_running(dev);
1383adfc5217SJeff Kirsher }
1384adfc5217SJeff Kirsher 
1385adfc5217SJeff Kirsher static void bnx2x_get_pauseparam(struct net_device *dev,
1386adfc5217SJeff Kirsher 				 struct ethtool_pauseparam *epause)
1387adfc5217SJeff Kirsher {
1388adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1389adfc5217SJeff Kirsher 	int cfg_idx = bnx2x_get_link_cfg_idx(bp);
1390adfc5217SJeff Kirsher 	epause->autoneg = (bp->link_params.req_flow_ctrl[cfg_idx] ==
1391adfc5217SJeff Kirsher 			   BNX2X_FLOW_CTRL_AUTO);
1392adfc5217SJeff Kirsher 
1393adfc5217SJeff Kirsher 	epause->rx_pause = ((bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_RX) ==
1394adfc5217SJeff Kirsher 			    BNX2X_FLOW_CTRL_RX);
1395adfc5217SJeff Kirsher 	epause->tx_pause = ((bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_TX) ==
1396adfc5217SJeff Kirsher 			    BNX2X_FLOW_CTRL_TX);
1397adfc5217SJeff Kirsher 
1398adfc5217SJeff Kirsher 	DP(NETIF_MSG_LINK, "ethtool_pauseparam: cmd %d\n"
1399f1deab50SJoe Perches 	   "  autoneg %d  rx_pause %d  tx_pause %d\n",
1400adfc5217SJeff Kirsher 	   epause->cmd, epause->autoneg, epause->rx_pause, epause->tx_pause);
1401adfc5217SJeff Kirsher }
1402adfc5217SJeff Kirsher 
1403adfc5217SJeff Kirsher static int bnx2x_set_pauseparam(struct net_device *dev,
1404adfc5217SJeff Kirsher 				struct ethtool_pauseparam *epause)
1405adfc5217SJeff Kirsher {
1406adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
1407adfc5217SJeff Kirsher 	u32 cfg_idx = bnx2x_get_link_cfg_idx(bp);
1408adfc5217SJeff Kirsher 	if (IS_MF(bp))
1409adfc5217SJeff Kirsher 		return 0;
1410adfc5217SJeff Kirsher 
1411adfc5217SJeff Kirsher 	DP(NETIF_MSG_LINK, "ethtool_pauseparam: cmd %d\n"
1412f1deab50SJoe Perches 	   "  autoneg %d  rx_pause %d  tx_pause %d\n",
1413adfc5217SJeff Kirsher 	   epause->cmd, epause->autoneg, epause->rx_pause, epause->tx_pause);
1414adfc5217SJeff Kirsher 
1415adfc5217SJeff Kirsher 	bp->link_params.req_flow_ctrl[cfg_idx] = BNX2X_FLOW_CTRL_AUTO;
1416adfc5217SJeff Kirsher 
1417adfc5217SJeff Kirsher 	if (epause->rx_pause)
1418adfc5217SJeff Kirsher 		bp->link_params.req_flow_ctrl[cfg_idx] |= BNX2X_FLOW_CTRL_RX;
1419adfc5217SJeff Kirsher 
1420adfc5217SJeff Kirsher 	if (epause->tx_pause)
1421adfc5217SJeff Kirsher 		bp->link_params.req_flow_ctrl[cfg_idx] |= BNX2X_FLOW_CTRL_TX;
1422adfc5217SJeff Kirsher 
1423adfc5217SJeff Kirsher 	if (bp->link_params.req_flow_ctrl[cfg_idx] == BNX2X_FLOW_CTRL_AUTO)
1424adfc5217SJeff Kirsher 		bp->link_params.req_flow_ctrl[cfg_idx] = BNX2X_FLOW_CTRL_NONE;
1425adfc5217SJeff Kirsher 
1426adfc5217SJeff Kirsher 	if (epause->autoneg) {
1427adfc5217SJeff Kirsher 		if (!(bp->port.supported[cfg_idx] & SUPPORTED_Autoneg)) {
1428adfc5217SJeff Kirsher 			DP(NETIF_MSG_LINK, "autoneg not supported\n");
1429adfc5217SJeff Kirsher 			return -EINVAL;
1430adfc5217SJeff Kirsher 		}
1431adfc5217SJeff Kirsher 
1432adfc5217SJeff Kirsher 		if (bp->link_params.req_line_speed[cfg_idx] == SPEED_AUTO_NEG) {
1433adfc5217SJeff Kirsher 			bp->link_params.req_flow_ctrl[cfg_idx] =
1434adfc5217SJeff Kirsher 				BNX2X_FLOW_CTRL_AUTO;
1435adfc5217SJeff Kirsher 		}
1436adfc5217SJeff Kirsher 	}
1437adfc5217SJeff Kirsher 
1438adfc5217SJeff Kirsher 	DP(NETIF_MSG_LINK,
1439adfc5217SJeff Kirsher 	   "req_flow_ctrl 0x%x\n", bp->link_params.req_flow_ctrl[cfg_idx]);
1440adfc5217SJeff Kirsher 
1441adfc5217SJeff Kirsher 	if (netif_running(dev)) {
1442adfc5217SJeff Kirsher 		bnx2x_stats_handle(bp, STATS_EVENT_STOP);
1443adfc5217SJeff Kirsher 		bnx2x_link_set(bp);
1444adfc5217SJeff Kirsher 	}
1445adfc5217SJeff Kirsher 
1446adfc5217SJeff Kirsher 	return 0;
1447adfc5217SJeff Kirsher }
1448adfc5217SJeff Kirsher 
1449adfc5217SJeff Kirsher static const struct {
1450adfc5217SJeff Kirsher 	char string[ETH_GSTRING_LEN];
1451adfc5217SJeff Kirsher } bnx2x_tests_str_arr[BNX2X_NUM_TESTS] = {
1452adfc5217SJeff Kirsher 	{ "register_test (offline)" },
1453adfc5217SJeff Kirsher 	{ "memory_test (offline)" },
1454adfc5217SJeff Kirsher 	{ "loopback_test (offline)" },
1455adfc5217SJeff Kirsher 	{ "nvram_test (online)" },
1456adfc5217SJeff Kirsher 	{ "interrupt_test (online)" },
1457adfc5217SJeff Kirsher 	{ "link_test (online)" },
1458adfc5217SJeff Kirsher 	{ "idle check (online)" }
1459adfc5217SJeff Kirsher };
1460adfc5217SJeff Kirsher 
1461adfc5217SJeff Kirsher enum {
1462adfc5217SJeff Kirsher 	BNX2X_CHIP_E1_OFST = 0,
1463adfc5217SJeff Kirsher 	BNX2X_CHIP_E1H_OFST,
1464adfc5217SJeff Kirsher 	BNX2X_CHIP_E2_OFST,
1465adfc5217SJeff Kirsher 	BNX2X_CHIP_E3_OFST,
1466adfc5217SJeff Kirsher 	BNX2X_CHIP_E3B0_OFST,
1467adfc5217SJeff Kirsher 	BNX2X_CHIP_MAX_OFST
1468adfc5217SJeff Kirsher };
1469adfc5217SJeff Kirsher 
1470adfc5217SJeff Kirsher #define BNX2X_CHIP_MASK_E1	(1 << BNX2X_CHIP_E1_OFST)
1471adfc5217SJeff Kirsher #define BNX2X_CHIP_MASK_E1H	(1 << BNX2X_CHIP_E1H_OFST)
1472adfc5217SJeff Kirsher #define BNX2X_CHIP_MASK_E2	(1 << BNX2X_CHIP_E2_OFST)
1473adfc5217SJeff Kirsher #define BNX2X_CHIP_MASK_E3	(1 << BNX2X_CHIP_E3_OFST)
1474adfc5217SJeff Kirsher #define BNX2X_CHIP_MASK_E3B0	(1 << BNX2X_CHIP_E3B0_OFST)
1475adfc5217SJeff Kirsher 
1476adfc5217SJeff Kirsher #define BNX2X_CHIP_MASK_ALL	((1 << BNX2X_CHIP_MAX_OFST) - 1)
1477adfc5217SJeff Kirsher #define BNX2X_CHIP_MASK_E1X	(BNX2X_CHIP_MASK_E1 | BNX2X_CHIP_MASK_E1H)
1478adfc5217SJeff Kirsher 
1479adfc5217SJeff Kirsher static int bnx2x_test_registers(struct bnx2x *bp)
1480adfc5217SJeff Kirsher {
1481adfc5217SJeff Kirsher 	int idx, i, rc = -ENODEV;
1482adfc5217SJeff Kirsher 	u32 wr_val = 0, hw;
1483adfc5217SJeff Kirsher 	int port = BP_PORT(bp);
1484adfc5217SJeff Kirsher 	static const struct {
1485adfc5217SJeff Kirsher 		u32 hw;
1486adfc5217SJeff Kirsher 		u32 offset0;
1487adfc5217SJeff Kirsher 		u32 offset1;
1488adfc5217SJeff Kirsher 		u32 mask;
1489adfc5217SJeff Kirsher 	} reg_tbl[] = {
1490adfc5217SJeff Kirsher /* 0 */		{ BNX2X_CHIP_MASK_ALL,
1491adfc5217SJeff Kirsher 			BRB1_REG_PAUSE_LOW_THRESHOLD_0,	4, 0x000003ff },
1492adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1493adfc5217SJeff Kirsher 			DORQ_REG_DB_ADDR0,		4, 0xffffffff },
1494adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_E1X,
1495adfc5217SJeff Kirsher 			HC_REG_AGG_INT_0,		4, 0x000003ff },
1496adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1497adfc5217SJeff Kirsher 			PBF_REG_MAC_IF0_ENABLE,		4, 0x00000001 },
1498adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_E1X | BNX2X_CHIP_MASK_E2 | BNX2X_CHIP_MASK_E3,
1499adfc5217SJeff Kirsher 			PBF_REG_P0_INIT_CRD,		4, 0x000007ff },
1500adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_E3B0,
1501adfc5217SJeff Kirsher 			PBF_REG_INIT_CRD_Q0,		4, 0x000007ff },
1502adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1503adfc5217SJeff Kirsher 			PRS_REG_CID_PORT_0,		4, 0x00ffffff },
1504adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1505adfc5217SJeff Kirsher 			PXP2_REG_PSWRQ_CDU0_L2P,	4, 0x000fffff },
1506adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1507adfc5217SJeff Kirsher 			PXP2_REG_RQ_CDU0_EFIRST_MEM_ADDR, 8, 0x0003ffff },
1508adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1509adfc5217SJeff Kirsher 			PXP2_REG_PSWRQ_TM0_L2P,		4, 0x000fffff },
1510adfc5217SJeff Kirsher /* 10 */	{ BNX2X_CHIP_MASK_ALL,
1511adfc5217SJeff Kirsher 			PXP2_REG_RQ_USDM0_EFIRST_MEM_ADDR, 8, 0x0003ffff },
1512adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1513adfc5217SJeff Kirsher 			PXP2_REG_PSWRQ_TSDM0_L2P,	4, 0x000fffff },
1514adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1515adfc5217SJeff Kirsher 			QM_REG_CONNNUM_0,		4, 0x000fffff },
1516adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1517adfc5217SJeff Kirsher 			TM_REG_LIN0_MAX_ACTIVE_CID,	4, 0x0003ffff },
1518adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1519adfc5217SJeff Kirsher 			SRC_REG_KEYRSS0_0,		40, 0xffffffff },
1520adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1521adfc5217SJeff Kirsher 			SRC_REG_KEYRSS0_7,		40, 0xffffffff },
1522adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1523adfc5217SJeff Kirsher 			XCM_REG_WU_DA_SET_TMR_CNT_FLG_CMD00, 4, 0x00000001 },
1524adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1525adfc5217SJeff Kirsher 			XCM_REG_WU_DA_CNT_CMD00,	4, 0x00000003 },
1526adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1527adfc5217SJeff Kirsher 			XCM_REG_GLB_DEL_ACK_MAX_CNT_0,	4, 0x000000ff },
1528adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1529adfc5217SJeff Kirsher 			NIG_REG_LLH0_T_BIT,		4, 0x00000001 },
1530adfc5217SJeff Kirsher /* 20 */	{ BNX2X_CHIP_MASK_E1X | BNX2X_CHIP_MASK_E2,
1531adfc5217SJeff Kirsher 			NIG_REG_EMAC0_IN_EN,		4, 0x00000001 },
1532adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_E1X | BNX2X_CHIP_MASK_E2,
1533adfc5217SJeff Kirsher 			NIG_REG_BMAC0_IN_EN,		4, 0x00000001 },
1534adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1535adfc5217SJeff Kirsher 			NIG_REG_XCM0_OUT_EN,		4, 0x00000001 },
1536adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1537adfc5217SJeff Kirsher 			NIG_REG_BRB0_OUT_EN,		4, 0x00000001 },
1538adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1539adfc5217SJeff Kirsher 			NIG_REG_LLH0_XCM_MASK,		4, 0x00000007 },
1540adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1541adfc5217SJeff Kirsher 			NIG_REG_LLH0_ACPI_PAT_6_LEN,	68, 0x000000ff },
1542adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1543adfc5217SJeff Kirsher 			NIG_REG_LLH0_ACPI_PAT_0_CRC,	68, 0xffffffff },
1544adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1545adfc5217SJeff Kirsher 			NIG_REG_LLH0_DEST_MAC_0_0,	160, 0xffffffff },
1546adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1547adfc5217SJeff Kirsher 			NIG_REG_LLH0_DEST_IP_0_1,	160, 0xffffffff },
1548adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1549adfc5217SJeff Kirsher 			NIG_REG_LLH0_IPV4_IPV6_0,	160, 0x00000001 },
1550adfc5217SJeff Kirsher /* 30 */	{ BNX2X_CHIP_MASK_ALL,
1551adfc5217SJeff Kirsher 			NIG_REG_LLH0_DEST_UDP_0,	160, 0x0000ffff },
1552adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1553adfc5217SJeff Kirsher 			NIG_REG_LLH0_DEST_TCP_0,	160, 0x0000ffff },
1554adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1555adfc5217SJeff Kirsher 			NIG_REG_LLH0_VLAN_ID_0,	160, 0x00000fff },
1556adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_E1X | BNX2X_CHIP_MASK_E2,
1557adfc5217SJeff Kirsher 			NIG_REG_XGXS_SERDES0_MODE_SEL,	4, 0x00000001 },
1558adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1559adfc5217SJeff Kirsher 			NIG_REG_LED_CONTROL_OVERRIDE_TRAFFIC_P0, 4, 0x00000001},
1560adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL,
1561adfc5217SJeff Kirsher 			NIG_REG_STATUS_INTERRUPT_PORT0,	4, 0x07ffffff },
1562adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_E1X | BNX2X_CHIP_MASK_E2,
1563adfc5217SJeff Kirsher 			NIG_REG_XGXS0_CTRL_EXTREMOTEMDIOST, 24, 0x00000001 },
1564adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_E1X | BNX2X_CHIP_MASK_E2,
1565adfc5217SJeff Kirsher 			NIG_REG_SERDES0_CTRL_PHY_ADDR,	16, 0x0000001f },
1566adfc5217SJeff Kirsher 
1567adfc5217SJeff Kirsher 		{ BNX2X_CHIP_MASK_ALL, 0xffffffff, 0, 0x00000000 }
1568adfc5217SJeff Kirsher 	};
1569adfc5217SJeff Kirsher 
1570adfc5217SJeff Kirsher 	if (!netif_running(bp->dev))
1571adfc5217SJeff Kirsher 		return rc;
1572adfc5217SJeff Kirsher 
1573adfc5217SJeff Kirsher 	if (CHIP_IS_E1(bp))
1574adfc5217SJeff Kirsher 		hw = BNX2X_CHIP_MASK_E1;
1575adfc5217SJeff Kirsher 	else if (CHIP_IS_E1H(bp))
1576adfc5217SJeff Kirsher 		hw = BNX2X_CHIP_MASK_E1H;
1577adfc5217SJeff Kirsher 	else if (CHIP_IS_E2(bp))
1578adfc5217SJeff Kirsher 		hw = BNX2X_CHIP_MASK_E2;
1579adfc5217SJeff Kirsher 	else if (CHIP_IS_E3B0(bp))
1580adfc5217SJeff Kirsher 		hw = BNX2X_CHIP_MASK_E3B0;
1581adfc5217SJeff Kirsher 	else /* e3 A0 */
1582adfc5217SJeff Kirsher 		hw = BNX2X_CHIP_MASK_E3;
1583adfc5217SJeff Kirsher 
1584adfc5217SJeff Kirsher 	/* Repeat the test twice:
1585adfc5217SJeff Kirsher 	   First by writing 0x00000000, second by writing 0xffffffff */
1586adfc5217SJeff Kirsher 	for (idx = 0; idx < 2; idx++) {
1587adfc5217SJeff Kirsher 
1588adfc5217SJeff Kirsher 		switch (idx) {
1589adfc5217SJeff Kirsher 		case 0:
1590adfc5217SJeff Kirsher 			wr_val = 0;
1591adfc5217SJeff Kirsher 			break;
1592adfc5217SJeff Kirsher 		case 1:
1593adfc5217SJeff Kirsher 			wr_val = 0xffffffff;
1594adfc5217SJeff Kirsher 			break;
1595adfc5217SJeff Kirsher 		}
1596adfc5217SJeff Kirsher 
1597adfc5217SJeff Kirsher 		for (i = 0; reg_tbl[i].offset0 != 0xffffffff; i++) {
1598adfc5217SJeff Kirsher 			u32 offset, mask, save_val, val;
1599adfc5217SJeff Kirsher 			if (!(hw & reg_tbl[i].hw))
1600adfc5217SJeff Kirsher 				continue;
1601adfc5217SJeff Kirsher 
1602adfc5217SJeff Kirsher 			offset = reg_tbl[i].offset0 + port*reg_tbl[i].offset1;
1603adfc5217SJeff Kirsher 			mask = reg_tbl[i].mask;
1604adfc5217SJeff Kirsher 
1605adfc5217SJeff Kirsher 			save_val = REG_RD(bp, offset);
1606adfc5217SJeff Kirsher 
1607adfc5217SJeff Kirsher 			REG_WR(bp, offset, wr_val & mask);
1608adfc5217SJeff Kirsher 
1609adfc5217SJeff Kirsher 			val = REG_RD(bp, offset);
1610adfc5217SJeff Kirsher 
1611adfc5217SJeff Kirsher 			/* Restore the original register's value */
1612adfc5217SJeff Kirsher 			REG_WR(bp, offset, save_val);
1613adfc5217SJeff Kirsher 
1614adfc5217SJeff Kirsher 			/* verify value is as expected */
1615adfc5217SJeff Kirsher 			if ((val & mask) != (wr_val & mask)) {
1616adfc5217SJeff Kirsher 				DP(NETIF_MSG_HW,
1617adfc5217SJeff Kirsher 				   "offset 0x%x: val 0x%x != 0x%x mask 0x%x\n",
1618adfc5217SJeff Kirsher 				   offset, val, wr_val, mask);
1619adfc5217SJeff Kirsher 				goto test_reg_exit;
1620adfc5217SJeff Kirsher 			}
1621adfc5217SJeff Kirsher 		}
1622adfc5217SJeff Kirsher 	}
1623adfc5217SJeff Kirsher 
1624adfc5217SJeff Kirsher 	rc = 0;
1625adfc5217SJeff Kirsher 
1626adfc5217SJeff Kirsher test_reg_exit:
1627adfc5217SJeff Kirsher 	return rc;
1628adfc5217SJeff Kirsher }
1629adfc5217SJeff Kirsher 
1630adfc5217SJeff Kirsher static int bnx2x_test_memory(struct bnx2x *bp)
1631adfc5217SJeff Kirsher {
1632adfc5217SJeff Kirsher 	int i, j, rc = -ENODEV;
1633adfc5217SJeff Kirsher 	u32 val, index;
1634adfc5217SJeff Kirsher 	static const struct {
1635adfc5217SJeff Kirsher 		u32 offset;
1636adfc5217SJeff Kirsher 		int size;
1637adfc5217SJeff Kirsher 	} mem_tbl[] = {
1638adfc5217SJeff Kirsher 		{ CCM_REG_XX_DESCR_TABLE,   CCM_REG_XX_DESCR_TABLE_SIZE },
1639adfc5217SJeff Kirsher 		{ CFC_REG_ACTIVITY_COUNTER, CFC_REG_ACTIVITY_COUNTER_SIZE },
1640adfc5217SJeff Kirsher 		{ CFC_REG_LINK_LIST,        CFC_REG_LINK_LIST_SIZE },
1641adfc5217SJeff Kirsher 		{ DMAE_REG_CMD_MEM,         DMAE_REG_CMD_MEM_SIZE },
1642adfc5217SJeff Kirsher 		{ TCM_REG_XX_DESCR_TABLE,   TCM_REG_XX_DESCR_TABLE_SIZE },
1643adfc5217SJeff Kirsher 		{ UCM_REG_XX_DESCR_TABLE,   UCM_REG_XX_DESCR_TABLE_SIZE },
1644adfc5217SJeff Kirsher 		{ XCM_REG_XX_DESCR_TABLE,   XCM_REG_XX_DESCR_TABLE_SIZE },
1645adfc5217SJeff Kirsher 
1646adfc5217SJeff Kirsher 		{ 0xffffffff, 0 }
1647adfc5217SJeff Kirsher 	};
1648adfc5217SJeff Kirsher 
1649adfc5217SJeff Kirsher 	static const struct {
1650adfc5217SJeff Kirsher 		char *name;
1651adfc5217SJeff Kirsher 		u32 offset;
1652adfc5217SJeff Kirsher 		u32 hw_mask[BNX2X_CHIP_MAX_OFST];
1653adfc5217SJeff Kirsher 	} prty_tbl[] = {
1654adfc5217SJeff Kirsher 		{ "CCM_PRTY_STS",  CCM_REG_CCM_PRTY_STS,
1655adfc5217SJeff Kirsher 			{0x3ffc0, 0,   0, 0} },
1656adfc5217SJeff Kirsher 		{ "CFC_PRTY_STS",  CFC_REG_CFC_PRTY_STS,
1657adfc5217SJeff Kirsher 			{0x2,     0x2, 0, 0} },
1658adfc5217SJeff Kirsher 		{ "DMAE_PRTY_STS", DMAE_REG_DMAE_PRTY_STS,
1659adfc5217SJeff Kirsher 			{0,       0,   0, 0} },
1660adfc5217SJeff Kirsher 		{ "TCM_PRTY_STS",  TCM_REG_TCM_PRTY_STS,
1661adfc5217SJeff Kirsher 			{0x3ffc0, 0,   0, 0} },
1662adfc5217SJeff Kirsher 		{ "UCM_PRTY_STS",  UCM_REG_UCM_PRTY_STS,
1663adfc5217SJeff Kirsher 			{0x3ffc0, 0,   0, 0} },
1664adfc5217SJeff Kirsher 		{ "XCM_PRTY_STS",  XCM_REG_XCM_PRTY_STS,
1665adfc5217SJeff Kirsher 			{0x3ffc1, 0,   0, 0} },
1666adfc5217SJeff Kirsher 
1667adfc5217SJeff Kirsher 		{ NULL, 0xffffffff, {0, 0, 0, 0} }
1668adfc5217SJeff Kirsher 	};
1669adfc5217SJeff Kirsher 
1670adfc5217SJeff Kirsher 	if (!netif_running(bp->dev))
1671adfc5217SJeff Kirsher 		return rc;
1672adfc5217SJeff Kirsher 
1673adfc5217SJeff Kirsher 	if (CHIP_IS_E1(bp))
1674adfc5217SJeff Kirsher 		index = BNX2X_CHIP_E1_OFST;
1675adfc5217SJeff Kirsher 	else if (CHIP_IS_E1H(bp))
1676adfc5217SJeff Kirsher 		index = BNX2X_CHIP_E1H_OFST;
1677adfc5217SJeff Kirsher 	else if (CHIP_IS_E2(bp))
1678adfc5217SJeff Kirsher 		index = BNX2X_CHIP_E2_OFST;
1679adfc5217SJeff Kirsher 	else /* e3 */
1680adfc5217SJeff Kirsher 		index = BNX2X_CHIP_E3_OFST;
1681adfc5217SJeff Kirsher 
1682adfc5217SJeff Kirsher 	/* pre-Check the parity status */
1683adfc5217SJeff Kirsher 	for (i = 0; prty_tbl[i].offset != 0xffffffff; i++) {
1684adfc5217SJeff Kirsher 		val = REG_RD(bp, prty_tbl[i].offset);
1685adfc5217SJeff Kirsher 		if (val & ~(prty_tbl[i].hw_mask[index])) {
1686adfc5217SJeff Kirsher 			DP(NETIF_MSG_HW,
1687adfc5217SJeff Kirsher 			   "%s is 0x%x\n", prty_tbl[i].name, val);
1688adfc5217SJeff Kirsher 			goto test_mem_exit;
1689adfc5217SJeff Kirsher 		}
1690adfc5217SJeff Kirsher 	}
1691adfc5217SJeff Kirsher 
1692adfc5217SJeff Kirsher 	/* Go through all the memories */
1693adfc5217SJeff Kirsher 	for (i = 0; mem_tbl[i].offset != 0xffffffff; i++)
1694adfc5217SJeff Kirsher 		for (j = 0; j < mem_tbl[i].size; j++)
1695adfc5217SJeff Kirsher 			REG_RD(bp, mem_tbl[i].offset + j*4);
1696adfc5217SJeff Kirsher 
1697adfc5217SJeff Kirsher 	/* Check the parity status */
1698adfc5217SJeff Kirsher 	for (i = 0; prty_tbl[i].offset != 0xffffffff; i++) {
1699adfc5217SJeff Kirsher 		val = REG_RD(bp, prty_tbl[i].offset);
1700adfc5217SJeff Kirsher 		if (val & ~(prty_tbl[i].hw_mask[index])) {
1701adfc5217SJeff Kirsher 			DP(NETIF_MSG_HW,
1702adfc5217SJeff Kirsher 			   "%s is 0x%x\n", prty_tbl[i].name, val);
1703adfc5217SJeff Kirsher 			goto test_mem_exit;
1704adfc5217SJeff Kirsher 		}
1705adfc5217SJeff Kirsher 	}
1706adfc5217SJeff Kirsher 
1707adfc5217SJeff Kirsher 	rc = 0;
1708adfc5217SJeff Kirsher 
1709adfc5217SJeff Kirsher test_mem_exit:
1710adfc5217SJeff Kirsher 	return rc;
1711adfc5217SJeff Kirsher }
1712adfc5217SJeff Kirsher 
1713adfc5217SJeff Kirsher static void bnx2x_wait_for_link(struct bnx2x *bp, u8 link_up, u8 is_serdes)
1714adfc5217SJeff Kirsher {
1715adfc5217SJeff Kirsher 	int cnt = 1400;
1716adfc5217SJeff Kirsher 
1717adfc5217SJeff Kirsher 	if (link_up) {
1718adfc5217SJeff Kirsher 		while (bnx2x_link_test(bp, is_serdes) && cnt--)
1719adfc5217SJeff Kirsher 			msleep(20);
1720adfc5217SJeff Kirsher 
1721adfc5217SJeff Kirsher 		if (cnt <= 0 && bnx2x_link_test(bp, is_serdes))
1722adfc5217SJeff Kirsher 			DP(NETIF_MSG_LINK, "Timeout waiting for link up\n");
1723adfc5217SJeff Kirsher 	}
1724adfc5217SJeff Kirsher }
1725adfc5217SJeff Kirsher 
1726adfc5217SJeff Kirsher static int bnx2x_run_loopback(struct bnx2x *bp, int loopback_mode)
1727adfc5217SJeff Kirsher {
1728adfc5217SJeff Kirsher 	unsigned int pkt_size, num_pkts, i;
1729adfc5217SJeff Kirsher 	struct sk_buff *skb;
1730adfc5217SJeff Kirsher 	unsigned char *packet;
1731adfc5217SJeff Kirsher 	struct bnx2x_fastpath *fp_rx = &bp->fp[0];
1732adfc5217SJeff Kirsher 	struct bnx2x_fastpath *fp_tx = &bp->fp[0];
1733adfc5217SJeff Kirsher 	struct bnx2x_fp_txdata *txdata = &fp_tx->txdata[0];
1734adfc5217SJeff Kirsher 	u16 tx_start_idx, tx_idx;
1735adfc5217SJeff Kirsher 	u16 rx_start_idx, rx_idx;
1736adfc5217SJeff Kirsher 	u16 pkt_prod, bd_prod, rx_comp_cons;
1737adfc5217SJeff Kirsher 	struct sw_tx_bd *tx_buf;
1738adfc5217SJeff Kirsher 	struct eth_tx_start_bd *tx_start_bd;
1739adfc5217SJeff Kirsher 	struct eth_tx_parse_bd_e1x  *pbd_e1x = NULL;
1740adfc5217SJeff Kirsher 	struct eth_tx_parse_bd_e2  *pbd_e2 = NULL;
1741adfc5217SJeff Kirsher 	dma_addr_t mapping;
1742adfc5217SJeff Kirsher 	union eth_rx_cqe *cqe;
1743adfc5217SJeff Kirsher 	u8 cqe_fp_flags, cqe_fp_type;
1744adfc5217SJeff Kirsher 	struct sw_rx_bd *rx_buf;
1745adfc5217SJeff Kirsher 	u16 len;
1746adfc5217SJeff Kirsher 	int rc = -ENODEV;
1747e52fcb24SEric Dumazet 	u8 *data;
1748adfc5217SJeff Kirsher 
1749adfc5217SJeff Kirsher 	/* check the loopback mode */
1750adfc5217SJeff Kirsher 	switch (loopback_mode) {
1751adfc5217SJeff Kirsher 	case BNX2X_PHY_LOOPBACK:
1752adfc5217SJeff Kirsher 		if (bp->link_params.loopback_mode != LOOPBACK_XGXS)
1753adfc5217SJeff Kirsher 			return -EINVAL;
1754adfc5217SJeff Kirsher 		break;
1755adfc5217SJeff Kirsher 	case BNX2X_MAC_LOOPBACK:
175632911333SYaniv Rosner 		if (CHIP_IS_E3(bp)) {
175732911333SYaniv Rosner 			int cfg_idx = bnx2x_get_link_cfg_idx(bp);
175832911333SYaniv Rosner 			if (bp->port.supported[cfg_idx] &
175932911333SYaniv Rosner 			    (SUPPORTED_10000baseT_Full |
176032911333SYaniv Rosner 			     SUPPORTED_20000baseMLD2_Full |
176132911333SYaniv Rosner 			     SUPPORTED_20000baseKR2_Full))
176232911333SYaniv Rosner 				bp->link_params.loopback_mode = LOOPBACK_XMAC;
176332911333SYaniv Rosner 			else
176432911333SYaniv Rosner 				bp->link_params.loopback_mode = LOOPBACK_UMAC;
176532911333SYaniv Rosner 		} else
176632911333SYaniv Rosner 			bp->link_params.loopback_mode = LOOPBACK_BMAC;
176732911333SYaniv Rosner 
1768adfc5217SJeff Kirsher 		bnx2x_phy_init(&bp->link_params, &bp->link_vars);
1769adfc5217SJeff Kirsher 		break;
1770adfc5217SJeff Kirsher 	default:
1771adfc5217SJeff Kirsher 		return -EINVAL;
1772adfc5217SJeff Kirsher 	}
1773adfc5217SJeff Kirsher 
1774adfc5217SJeff Kirsher 	/* prepare the loopback packet */
1775adfc5217SJeff Kirsher 	pkt_size = (((bp->dev->mtu < ETH_MAX_PACKET_SIZE) ?
1776adfc5217SJeff Kirsher 		     bp->dev->mtu : ETH_MAX_PACKET_SIZE) + ETH_HLEN);
1777adfc5217SJeff Kirsher 	skb = netdev_alloc_skb(bp->dev, fp_rx->rx_buf_size);
1778adfc5217SJeff Kirsher 	if (!skb) {
1779adfc5217SJeff Kirsher 		rc = -ENOMEM;
1780adfc5217SJeff Kirsher 		goto test_loopback_exit;
1781adfc5217SJeff Kirsher 	}
1782adfc5217SJeff Kirsher 	packet = skb_put(skb, pkt_size);
1783adfc5217SJeff Kirsher 	memcpy(packet, bp->dev->dev_addr, ETH_ALEN);
1784adfc5217SJeff Kirsher 	memset(packet + ETH_ALEN, 0, ETH_ALEN);
1785adfc5217SJeff Kirsher 	memset(packet + 2*ETH_ALEN, 0x77, (ETH_HLEN - 2*ETH_ALEN));
1786adfc5217SJeff Kirsher 	for (i = ETH_HLEN; i < pkt_size; i++)
1787adfc5217SJeff Kirsher 		packet[i] = (unsigned char) (i & 0xff);
1788adfc5217SJeff Kirsher 	mapping = dma_map_single(&bp->pdev->dev, skb->data,
1789adfc5217SJeff Kirsher 				 skb_headlen(skb), DMA_TO_DEVICE);
1790adfc5217SJeff Kirsher 	if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
1791adfc5217SJeff Kirsher 		rc = -ENOMEM;
1792adfc5217SJeff Kirsher 		dev_kfree_skb(skb);
1793adfc5217SJeff Kirsher 		BNX2X_ERR("Unable to map SKB\n");
1794adfc5217SJeff Kirsher 		goto test_loopback_exit;
1795adfc5217SJeff Kirsher 	}
1796adfc5217SJeff Kirsher 
1797adfc5217SJeff Kirsher 	/* send the loopback packet */
1798adfc5217SJeff Kirsher 	num_pkts = 0;
1799adfc5217SJeff Kirsher 	tx_start_idx = le16_to_cpu(*txdata->tx_cons_sb);
1800adfc5217SJeff Kirsher 	rx_start_idx = le16_to_cpu(*fp_rx->rx_cons_sb);
1801adfc5217SJeff Kirsher 
1802adfc5217SJeff Kirsher 	pkt_prod = txdata->tx_pkt_prod++;
1803adfc5217SJeff Kirsher 	tx_buf = &txdata->tx_buf_ring[TX_BD(pkt_prod)];
1804adfc5217SJeff Kirsher 	tx_buf->first_bd = txdata->tx_bd_prod;
1805adfc5217SJeff Kirsher 	tx_buf->skb = skb;
1806adfc5217SJeff Kirsher 	tx_buf->flags = 0;
1807adfc5217SJeff Kirsher 
1808adfc5217SJeff Kirsher 	bd_prod = TX_BD(txdata->tx_bd_prod);
1809adfc5217SJeff Kirsher 	tx_start_bd = &txdata->tx_desc_ring[bd_prod].start_bd;
1810adfc5217SJeff Kirsher 	tx_start_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
1811adfc5217SJeff Kirsher 	tx_start_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
1812adfc5217SJeff Kirsher 	tx_start_bd->nbd = cpu_to_le16(2); /* start + pbd */
1813adfc5217SJeff Kirsher 	tx_start_bd->nbytes = cpu_to_le16(skb_headlen(skb));
1814adfc5217SJeff Kirsher 	tx_start_bd->vlan_or_ethertype = cpu_to_le16(pkt_prod);
1815adfc5217SJeff Kirsher 	tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
1816adfc5217SJeff Kirsher 	SET_FLAG(tx_start_bd->general_data,
1817adfc5217SJeff Kirsher 		 ETH_TX_START_BD_ETH_ADDR_TYPE,
1818adfc5217SJeff Kirsher 		 UNICAST_ADDRESS);
1819adfc5217SJeff Kirsher 	SET_FLAG(tx_start_bd->general_data,
1820adfc5217SJeff Kirsher 		 ETH_TX_START_BD_HDR_NBDS,
1821adfc5217SJeff Kirsher 		 1);
1822adfc5217SJeff Kirsher 
1823adfc5217SJeff Kirsher 	/* turn on parsing and get a BD */
1824adfc5217SJeff Kirsher 	bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
1825adfc5217SJeff Kirsher 
1826adfc5217SJeff Kirsher 	pbd_e1x = &txdata->tx_desc_ring[bd_prod].parse_bd_e1x;
1827adfc5217SJeff Kirsher 	pbd_e2 = &txdata->tx_desc_ring[bd_prod].parse_bd_e2;
1828adfc5217SJeff Kirsher 
1829adfc5217SJeff Kirsher 	memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
1830adfc5217SJeff Kirsher 	memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
1831adfc5217SJeff Kirsher 
1832adfc5217SJeff Kirsher 	wmb();
1833adfc5217SJeff Kirsher 
1834adfc5217SJeff Kirsher 	txdata->tx_db.data.prod += 2;
1835adfc5217SJeff Kirsher 	barrier();
1836adfc5217SJeff Kirsher 	DOORBELL(bp, txdata->cid, txdata->tx_db.raw);
1837adfc5217SJeff Kirsher 
1838adfc5217SJeff Kirsher 	mmiowb();
1839adfc5217SJeff Kirsher 	barrier();
1840adfc5217SJeff Kirsher 
1841adfc5217SJeff Kirsher 	num_pkts++;
1842adfc5217SJeff Kirsher 	txdata->tx_bd_prod += 2; /* start + pbd */
1843adfc5217SJeff Kirsher 
1844adfc5217SJeff Kirsher 	udelay(100);
1845adfc5217SJeff Kirsher 
1846adfc5217SJeff Kirsher 	tx_idx = le16_to_cpu(*txdata->tx_cons_sb);
1847adfc5217SJeff Kirsher 	if (tx_idx != tx_start_idx + num_pkts)
1848adfc5217SJeff Kirsher 		goto test_loopback_exit;
1849adfc5217SJeff Kirsher 
1850adfc5217SJeff Kirsher 	/* Unlike HC IGU won't generate an interrupt for status block
1851adfc5217SJeff Kirsher 	 * updates that have been performed while interrupts were
1852adfc5217SJeff Kirsher 	 * disabled.
1853adfc5217SJeff Kirsher 	 */
1854adfc5217SJeff Kirsher 	if (bp->common.int_block == INT_BLOCK_IGU) {
1855adfc5217SJeff Kirsher 		/* Disable local BHes to prevent a dead-lock situation between
1856adfc5217SJeff Kirsher 		 * sch_direct_xmit() and bnx2x_run_loopback() (calling
1857adfc5217SJeff Kirsher 		 * bnx2x_tx_int()), as both are taking netif_tx_lock().
1858adfc5217SJeff Kirsher 		 */
1859adfc5217SJeff Kirsher 		local_bh_disable();
1860adfc5217SJeff Kirsher 		bnx2x_tx_int(bp, txdata);
1861adfc5217SJeff Kirsher 		local_bh_enable();
1862adfc5217SJeff Kirsher 	}
1863adfc5217SJeff Kirsher 
1864adfc5217SJeff Kirsher 	rx_idx = le16_to_cpu(*fp_rx->rx_cons_sb);
1865adfc5217SJeff Kirsher 	if (rx_idx != rx_start_idx + num_pkts)
1866adfc5217SJeff Kirsher 		goto test_loopback_exit;
1867adfc5217SJeff Kirsher 
1868adfc5217SJeff Kirsher 	rx_comp_cons = le16_to_cpu(fp_rx->rx_comp_cons);
1869adfc5217SJeff Kirsher 	cqe = &fp_rx->rx_comp_ring[RCQ_BD(rx_comp_cons)];
1870adfc5217SJeff Kirsher 	cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
1871adfc5217SJeff Kirsher 	cqe_fp_type = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
1872adfc5217SJeff Kirsher 	if (!CQE_TYPE_FAST(cqe_fp_type) || (cqe_fp_flags & ETH_RX_ERROR_FALGS))
1873adfc5217SJeff Kirsher 		goto test_loopback_rx_exit;
1874adfc5217SJeff Kirsher 
1875adfc5217SJeff Kirsher 	len = le16_to_cpu(cqe->fast_path_cqe.pkt_len);
1876adfc5217SJeff Kirsher 	if (len != pkt_size)
1877adfc5217SJeff Kirsher 		goto test_loopback_rx_exit;
1878adfc5217SJeff Kirsher 
1879adfc5217SJeff Kirsher 	rx_buf = &fp_rx->rx_buf_ring[RX_BD(fp_rx->rx_bd_cons)];
1880adfc5217SJeff Kirsher 	dma_sync_single_for_cpu(&bp->pdev->dev,
1881adfc5217SJeff Kirsher 				   dma_unmap_addr(rx_buf, mapping),
1882adfc5217SJeff Kirsher 				   fp_rx->rx_buf_size, DMA_FROM_DEVICE);
1883e52fcb24SEric Dumazet 	data = rx_buf->data + NET_SKB_PAD + cqe->fast_path_cqe.placement_offset;
1884adfc5217SJeff Kirsher 	for (i = ETH_HLEN; i < pkt_size; i++)
1885e52fcb24SEric Dumazet 		if (*(data + i) != (unsigned char) (i & 0xff))
1886adfc5217SJeff Kirsher 			goto test_loopback_rx_exit;
1887adfc5217SJeff Kirsher 
1888adfc5217SJeff Kirsher 	rc = 0;
1889adfc5217SJeff Kirsher 
1890adfc5217SJeff Kirsher test_loopback_rx_exit:
1891adfc5217SJeff Kirsher 
1892adfc5217SJeff Kirsher 	fp_rx->rx_bd_cons = NEXT_RX_IDX(fp_rx->rx_bd_cons);
1893adfc5217SJeff Kirsher 	fp_rx->rx_bd_prod = NEXT_RX_IDX(fp_rx->rx_bd_prod);
1894adfc5217SJeff Kirsher 	fp_rx->rx_comp_cons = NEXT_RCQ_IDX(fp_rx->rx_comp_cons);
1895adfc5217SJeff Kirsher 	fp_rx->rx_comp_prod = NEXT_RCQ_IDX(fp_rx->rx_comp_prod);
1896adfc5217SJeff Kirsher 
1897adfc5217SJeff Kirsher 	/* Update producers */
1898adfc5217SJeff Kirsher 	bnx2x_update_rx_prod(bp, fp_rx, fp_rx->rx_bd_prod, fp_rx->rx_comp_prod,
1899adfc5217SJeff Kirsher 			     fp_rx->rx_sge_prod);
1900adfc5217SJeff Kirsher 
1901adfc5217SJeff Kirsher test_loopback_exit:
1902adfc5217SJeff Kirsher 	bp->link_params.loopback_mode = LOOPBACK_NONE;
1903adfc5217SJeff Kirsher 
1904adfc5217SJeff Kirsher 	return rc;
1905adfc5217SJeff Kirsher }
1906adfc5217SJeff Kirsher 
1907adfc5217SJeff Kirsher static int bnx2x_test_loopback(struct bnx2x *bp)
1908adfc5217SJeff Kirsher {
1909adfc5217SJeff Kirsher 	int rc = 0, res;
1910adfc5217SJeff Kirsher 
1911adfc5217SJeff Kirsher 	if (BP_NOMCP(bp))
1912adfc5217SJeff Kirsher 		return rc;
1913adfc5217SJeff Kirsher 
1914adfc5217SJeff Kirsher 	if (!netif_running(bp->dev))
1915adfc5217SJeff Kirsher 		return BNX2X_LOOPBACK_FAILED;
1916adfc5217SJeff Kirsher 
1917adfc5217SJeff Kirsher 	bnx2x_netif_stop(bp, 1);
1918adfc5217SJeff Kirsher 	bnx2x_acquire_phy_lock(bp);
1919adfc5217SJeff Kirsher 
1920adfc5217SJeff Kirsher 	res = bnx2x_run_loopback(bp, BNX2X_PHY_LOOPBACK);
1921adfc5217SJeff Kirsher 	if (res) {
1922adfc5217SJeff Kirsher 		DP(NETIF_MSG_PROBE, "  PHY loopback failed  (res %d)\n", res);
1923adfc5217SJeff Kirsher 		rc |= BNX2X_PHY_LOOPBACK_FAILED;
1924adfc5217SJeff Kirsher 	}
1925adfc5217SJeff Kirsher 
1926adfc5217SJeff Kirsher 	res = bnx2x_run_loopback(bp, BNX2X_MAC_LOOPBACK);
1927adfc5217SJeff Kirsher 	if (res) {
1928adfc5217SJeff Kirsher 		DP(NETIF_MSG_PROBE, "  MAC loopback failed  (res %d)\n", res);
1929adfc5217SJeff Kirsher 		rc |= BNX2X_MAC_LOOPBACK_FAILED;
1930adfc5217SJeff Kirsher 	}
1931adfc5217SJeff Kirsher 
1932adfc5217SJeff Kirsher 	bnx2x_release_phy_lock(bp);
1933adfc5217SJeff Kirsher 	bnx2x_netif_start(bp);
1934adfc5217SJeff Kirsher 
1935adfc5217SJeff Kirsher 	return rc;
1936adfc5217SJeff Kirsher }
1937adfc5217SJeff Kirsher 
1938adfc5217SJeff Kirsher #define CRC32_RESIDUAL			0xdebb20e3
1939adfc5217SJeff Kirsher 
1940adfc5217SJeff Kirsher static int bnx2x_test_nvram(struct bnx2x *bp)
1941adfc5217SJeff Kirsher {
1942adfc5217SJeff Kirsher 	static const struct {
1943adfc5217SJeff Kirsher 		int offset;
1944adfc5217SJeff Kirsher 		int size;
1945adfc5217SJeff Kirsher 	} nvram_tbl[] = {
1946adfc5217SJeff Kirsher 		{     0,  0x14 }, /* bootstrap */
1947adfc5217SJeff Kirsher 		{  0x14,  0xec }, /* dir */
1948adfc5217SJeff Kirsher 		{ 0x100, 0x350 }, /* manuf_info */
1949adfc5217SJeff Kirsher 		{ 0x450,  0xf0 }, /* feature_info */
1950adfc5217SJeff Kirsher 		{ 0x640,  0x64 }, /* upgrade_key_info */
1951adfc5217SJeff Kirsher 		{ 0x708,  0x70 }, /* manuf_key_info */
1952adfc5217SJeff Kirsher 		{     0,     0 }
1953adfc5217SJeff Kirsher 	};
1954adfc5217SJeff Kirsher 	__be32 buf[0x350 / 4];
1955adfc5217SJeff Kirsher 	u8 *data = (u8 *)buf;
1956adfc5217SJeff Kirsher 	int i, rc;
1957adfc5217SJeff Kirsher 	u32 magic, crc;
1958adfc5217SJeff Kirsher 
1959adfc5217SJeff Kirsher 	if (BP_NOMCP(bp))
1960adfc5217SJeff Kirsher 		return 0;
1961adfc5217SJeff Kirsher 
1962adfc5217SJeff Kirsher 	rc = bnx2x_nvram_read(bp, 0, data, 4);
1963adfc5217SJeff Kirsher 	if (rc) {
1964adfc5217SJeff Kirsher 		DP(NETIF_MSG_PROBE, "magic value read (rc %d)\n", rc);
1965adfc5217SJeff Kirsher 		goto test_nvram_exit;
1966adfc5217SJeff Kirsher 	}
1967adfc5217SJeff Kirsher 
1968adfc5217SJeff Kirsher 	magic = be32_to_cpu(buf[0]);
1969adfc5217SJeff Kirsher 	if (magic != 0x669955aa) {
1970adfc5217SJeff Kirsher 		DP(NETIF_MSG_PROBE, "magic value (0x%08x)\n", magic);
1971adfc5217SJeff Kirsher 		rc = -ENODEV;
1972adfc5217SJeff Kirsher 		goto test_nvram_exit;
1973adfc5217SJeff Kirsher 	}
1974adfc5217SJeff Kirsher 
1975adfc5217SJeff Kirsher 	for (i = 0; nvram_tbl[i].size; i++) {
1976adfc5217SJeff Kirsher 
1977adfc5217SJeff Kirsher 		rc = bnx2x_nvram_read(bp, nvram_tbl[i].offset, data,
1978adfc5217SJeff Kirsher 				      nvram_tbl[i].size);
1979adfc5217SJeff Kirsher 		if (rc) {
1980adfc5217SJeff Kirsher 			DP(NETIF_MSG_PROBE,
1981adfc5217SJeff Kirsher 			   "nvram_tbl[%d] read data (rc %d)\n", i, rc);
1982adfc5217SJeff Kirsher 			goto test_nvram_exit;
1983adfc5217SJeff Kirsher 		}
1984adfc5217SJeff Kirsher 
1985adfc5217SJeff Kirsher 		crc = ether_crc_le(nvram_tbl[i].size, data);
1986adfc5217SJeff Kirsher 		if (crc != CRC32_RESIDUAL) {
1987adfc5217SJeff Kirsher 			DP(NETIF_MSG_PROBE,
1988adfc5217SJeff Kirsher 			   "nvram_tbl[%d] crc value (0x%08x)\n", i, crc);
1989adfc5217SJeff Kirsher 			rc = -ENODEV;
1990adfc5217SJeff Kirsher 			goto test_nvram_exit;
1991adfc5217SJeff Kirsher 		}
1992adfc5217SJeff Kirsher 	}
1993adfc5217SJeff Kirsher 
1994adfc5217SJeff Kirsher test_nvram_exit:
1995adfc5217SJeff Kirsher 	return rc;
1996adfc5217SJeff Kirsher }
1997adfc5217SJeff Kirsher 
1998adfc5217SJeff Kirsher /* Send an EMPTY ramrod on the first queue */
1999adfc5217SJeff Kirsher static int bnx2x_test_intr(struct bnx2x *bp)
2000adfc5217SJeff Kirsher {
2001adfc5217SJeff Kirsher 	struct bnx2x_queue_state_params params = {0};
2002adfc5217SJeff Kirsher 
2003adfc5217SJeff Kirsher 	if (!netif_running(bp->dev))
2004adfc5217SJeff Kirsher 		return -ENODEV;
2005adfc5217SJeff Kirsher 
2006adfc5217SJeff Kirsher 	params.q_obj = &bp->fp->q_obj;
2007adfc5217SJeff Kirsher 	params.cmd = BNX2X_Q_CMD_EMPTY;
2008adfc5217SJeff Kirsher 
2009adfc5217SJeff Kirsher 	__set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
2010adfc5217SJeff Kirsher 
2011adfc5217SJeff Kirsher 	return bnx2x_queue_state_change(bp, &params);
2012adfc5217SJeff Kirsher }
2013adfc5217SJeff Kirsher 
2014adfc5217SJeff Kirsher static void bnx2x_self_test(struct net_device *dev,
2015adfc5217SJeff Kirsher 			    struct ethtool_test *etest, u64 *buf)
2016adfc5217SJeff Kirsher {
2017adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2018adfc5217SJeff Kirsher 	u8 is_serdes;
2019adfc5217SJeff Kirsher 	if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
2020f1deab50SJoe Perches 		pr_err("Handling parity error recovery. Try again later\n");
2021adfc5217SJeff Kirsher 		etest->flags |= ETH_TEST_FL_FAILED;
2022adfc5217SJeff Kirsher 		return;
2023adfc5217SJeff Kirsher 	}
2024adfc5217SJeff Kirsher 
2025adfc5217SJeff Kirsher 	memset(buf, 0, sizeof(u64) * BNX2X_NUM_TESTS);
2026adfc5217SJeff Kirsher 
2027adfc5217SJeff Kirsher 	if (!netif_running(dev))
2028adfc5217SJeff Kirsher 		return;
2029adfc5217SJeff Kirsher 
2030adfc5217SJeff Kirsher 	/* offline tests are not supported in MF mode */
2031adfc5217SJeff Kirsher 	if (IS_MF(bp))
2032adfc5217SJeff Kirsher 		etest->flags &= ~ETH_TEST_FL_OFFLINE;
2033adfc5217SJeff Kirsher 	is_serdes = (bp->link_vars.link_status & LINK_STATUS_SERDES_LINK) > 0;
2034adfc5217SJeff Kirsher 
2035adfc5217SJeff Kirsher 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
2036adfc5217SJeff Kirsher 		int port = BP_PORT(bp);
2037adfc5217SJeff Kirsher 		u32 val;
2038adfc5217SJeff Kirsher 		u8 link_up;
2039adfc5217SJeff Kirsher 
2040adfc5217SJeff Kirsher 		/* save current value of input enable for TX port IF */
2041adfc5217SJeff Kirsher 		val = REG_RD(bp, NIG_REG_EGRESS_UMP0_IN_EN + port*4);
2042adfc5217SJeff Kirsher 		/* disable input for TX port IF */
2043adfc5217SJeff Kirsher 		REG_WR(bp, NIG_REG_EGRESS_UMP0_IN_EN + port*4, 0);
2044adfc5217SJeff Kirsher 
2045adfc5217SJeff Kirsher 		link_up = bp->link_vars.link_up;
2046adfc5217SJeff Kirsher 
2047adfc5217SJeff Kirsher 		bnx2x_nic_unload(bp, UNLOAD_NORMAL);
2048adfc5217SJeff Kirsher 		bnx2x_nic_load(bp, LOAD_DIAG);
2049adfc5217SJeff Kirsher 		/* wait until link state is restored */
2050adfc5217SJeff Kirsher 		bnx2x_wait_for_link(bp, 1, is_serdes);
2051adfc5217SJeff Kirsher 
2052adfc5217SJeff Kirsher 		if (bnx2x_test_registers(bp) != 0) {
2053adfc5217SJeff Kirsher 			buf[0] = 1;
2054adfc5217SJeff Kirsher 			etest->flags |= ETH_TEST_FL_FAILED;
2055adfc5217SJeff Kirsher 		}
2056adfc5217SJeff Kirsher 		if (bnx2x_test_memory(bp) != 0) {
2057adfc5217SJeff Kirsher 			buf[1] = 1;
2058adfc5217SJeff Kirsher 			etest->flags |= ETH_TEST_FL_FAILED;
2059adfc5217SJeff Kirsher 		}
2060adfc5217SJeff Kirsher 
2061adfc5217SJeff Kirsher 		buf[2] = bnx2x_test_loopback(bp);
2062adfc5217SJeff Kirsher 		if (buf[2] != 0)
2063adfc5217SJeff Kirsher 			etest->flags |= ETH_TEST_FL_FAILED;
2064adfc5217SJeff Kirsher 
2065adfc5217SJeff Kirsher 		bnx2x_nic_unload(bp, UNLOAD_NORMAL);
2066adfc5217SJeff Kirsher 
2067adfc5217SJeff Kirsher 		/* restore input for TX port IF */
2068adfc5217SJeff Kirsher 		REG_WR(bp, NIG_REG_EGRESS_UMP0_IN_EN + port*4, val);
2069adfc5217SJeff Kirsher 
2070adfc5217SJeff Kirsher 		bnx2x_nic_load(bp, LOAD_NORMAL);
2071adfc5217SJeff Kirsher 		/* wait until link state is restored */
2072adfc5217SJeff Kirsher 		bnx2x_wait_for_link(bp, link_up, is_serdes);
2073adfc5217SJeff Kirsher 	}
2074adfc5217SJeff Kirsher 	if (bnx2x_test_nvram(bp) != 0) {
2075adfc5217SJeff Kirsher 		buf[3] = 1;
2076adfc5217SJeff Kirsher 		etest->flags |= ETH_TEST_FL_FAILED;
2077adfc5217SJeff Kirsher 	}
2078adfc5217SJeff Kirsher 	if (bnx2x_test_intr(bp) != 0) {
2079adfc5217SJeff Kirsher 		buf[4] = 1;
2080adfc5217SJeff Kirsher 		etest->flags |= ETH_TEST_FL_FAILED;
2081adfc5217SJeff Kirsher 	}
2082adfc5217SJeff Kirsher 
2083adfc5217SJeff Kirsher 	if (bnx2x_link_test(bp, is_serdes) != 0) {
2084adfc5217SJeff Kirsher 		buf[5] = 1;
2085adfc5217SJeff Kirsher 		etest->flags |= ETH_TEST_FL_FAILED;
2086adfc5217SJeff Kirsher 	}
2087adfc5217SJeff Kirsher 
2088adfc5217SJeff Kirsher #ifdef BNX2X_EXTRA_DEBUG
2089adfc5217SJeff Kirsher 	bnx2x_panic_dump(bp);
2090adfc5217SJeff Kirsher #endif
2091adfc5217SJeff Kirsher }
2092adfc5217SJeff Kirsher 
2093adfc5217SJeff Kirsher #define IS_PORT_STAT(i) \
2094adfc5217SJeff Kirsher 	((bnx2x_stats_arr[i].flags & STATS_FLAGS_BOTH) == STATS_FLAGS_PORT)
2095adfc5217SJeff Kirsher #define IS_FUNC_STAT(i)		(bnx2x_stats_arr[i].flags & STATS_FLAGS_FUNC)
2096adfc5217SJeff Kirsher #define IS_MF_MODE_STAT(bp) \
2097adfc5217SJeff Kirsher 			(IS_MF(bp) && !(bp->msg_enable & BNX2X_MSG_STATS))
2098adfc5217SJeff Kirsher 
2099adfc5217SJeff Kirsher /* ethtool statistics are displayed for all regular ethernet queues and the
2100adfc5217SJeff Kirsher  * fcoe L2 queue if not disabled
2101adfc5217SJeff Kirsher  */
2102adfc5217SJeff Kirsher static inline int bnx2x_num_stat_queues(struct bnx2x *bp)
2103adfc5217SJeff Kirsher {
2104adfc5217SJeff Kirsher 	return BNX2X_NUM_ETH_QUEUES(bp);
2105adfc5217SJeff Kirsher }
2106adfc5217SJeff Kirsher 
2107adfc5217SJeff Kirsher static int bnx2x_get_sset_count(struct net_device *dev, int stringset)
2108adfc5217SJeff Kirsher {
2109adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2110adfc5217SJeff Kirsher 	int i, num_stats;
2111adfc5217SJeff Kirsher 
2112adfc5217SJeff Kirsher 	switch (stringset) {
2113adfc5217SJeff Kirsher 	case ETH_SS_STATS:
2114adfc5217SJeff Kirsher 		if (is_multi(bp)) {
2115adfc5217SJeff Kirsher 			num_stats = bnx2x_num_stat_queues(bp) *
2116adfc5217SJeff Kirsher 				BNX2X_NUM_Q_STATS;
2117adfc5217SJeff Kirsher 			if (!IS_MF_MODE_STAT(bp))
2118adfc5217SJeff Kirsher 				num_stats += BNX2X_NUM_STATS;
2119adfc5217SJeff Kirsher 		} else {
2120adfc5217SJeff Kirsher 			if (IS_MF_MODE_STAT(bp)) {
2121adfc5217SJeff Kirsher 				num_stats = 0;
2122adfc5217SJeff Kirsher 				for (i = 0; i < BNX2X_NUM_STATS; i++)
2123adfc5217SJeff Kirsher 					if (IS_FUNC_STAT(i))
2124adfc5217SJeff Kirsher 						num_stats++;
2125adfc5217SJeff Kirsher 			} else
2126adfc5217SJeff Kirsher 				num_stats = BNX2X_NUM_STATS;
2127adfc5217SJeff Kirsher 		}
2128adfc5217SJeff Kirsher 		return num_stats;
2129adfc5217SJeff Kirsher 
2130adfc5217SJeff Kirsher 	case ETH_SS_TEST:
2131adfc5217SJeff Kirsher 		return BNX2X_NUM_TESTS;
2132adfc5217SJeff Kirsher 
2133adfc5217SJeff Kirsher 	default:
2134adfc5217SJeff Kirsher 		return -EINVAL;
2135adfc5217SJeff Kirsher 	}
2136adfc5217SJeff Kirsher }
2137adfc5217SJeff Kirsher 
2138adfc5217SJeff Kirsher static void bnx2x_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
2139adfc5217SJeff Kirsher {
2140adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2141adfc5217SJeff Kirsher 	int i, j, k;
2142adfc5217SJeff Kirsher 	char queue_name[MAX_QUEUE_NAME_LEN+1];
2143adfc5217SJeff Kirsher 
2144adfc5217SJeff Kirsher 	switch (stringset) {
2145adfc5217SJeff Kirsher 	case ETH_SS_STATS:
2146adfc5217SJeff Kirsher 		if (is_multi(bp)) {
2147adfc5217SJeff Kirsher 			k = 0;
2148adfc5217SJeff Kirsher 			for_each_eth_queue(bp, i) {
2149adfc5217SJeff Kirsher 				memset(queue_name, 0, sizeof(queue_name));
2150adfc5217SJeff Kirsher 				sprintf(queue_name, "%d", i);
2151adfc5217SJeff Kirsher 				for (j = 0; j < BNX2X_NUM_Q_STATS; j++)
2152adfc5217SJeff Kirsher 					snprintf(buf + (k + j)*ETH_GSTRING_LEN,
2153adfc5217SJeff Kirsher 						ETH_GSTRING_LEN,
2154adfc5217SJeff Kirsher 						bnx2x_q_stats_arr[j].string,
2155adfc5217SJeff Kirsher 						queue_name);
2156adfc5217SJeff Kirsher 				k += BNX2X_NUM_Q_STATS;
2157adfc5217SJeff Kirsher 			}
2158adfc5217SJeff Kirsher 			if (IS_MF_MODE_STAT(bp))
2159adfc5217SJeff Kirsher 				break;
2160adfc5217SJeff Kirsher 			for (j = 0; j < BNX2X_NUM_STATS; j++)
2161adfc5217SJeff Kirsher 				strcpy(buf + (k + j)*ETH_GSTRING_LEN,
2162adfc5217SJeff Kirsher 				       bnx2x_stats_arr[j].string);
2163adfc5217SJeff Kirsher 		} else {
2164adfc5217SJeff Kirsher 			for (i = 0, j = 0; i < BNX2X_NUM_STATS; i++) {
2165adfc5217SJeff Kirsher 				if (IS_MF_MODE_STAT(bp) && IS_PORT_STAT(i))
2166adfc5217SJeff Kirsher 					continue;
2167adfc5217SJeff Kirsher 				strcpy(buf + j*ETH_GSTRING_LEN,
2168adfc5217SJeff Kirsher 				       bnx2x_stats_arr[i].string);
2169adfc5217SJeff Kirsher 				j++;
2170adfc5217SJeff Kirsher 			}
2171adfc5217SJeff Kirsher 		}
2172adfc5217SJeff Kirsher 		break;
2173adfc5217SJeff Kirsher 
2174adfc5217SJeff Kirsher 	case ETH_SS_TEST:
2175adfc5217SJeff Kirsher 		memcpy(buf, bnx2x_tests_str_arr, sizeof(bnx2x_tests_str_arr));
2176adfc5217SJeff Kirsher 		break;
2177adfc5217SJeff Kirsher 	}
2178adfc5217SJeff Kirsher }
2179adfc5217SJeff Kirsher 
2180adfc5217SJeff Kirsher static void bnx2x_get_ethtool_stats(struct net_device *dev,
2181adfc5217SJeff Kirsher 				    struct ethtool_stats *stats, u64 *buf)
2182adfc5217SJeff Kirsher {
2183adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2184adfc5217SJeff Kirsher 	u32 *hw_stats, *offset;
2185adfc5217SJeff Kirsher 	int i, j, k;
2186adfc5217SJeff Kirsher 
2187adfc5217SJeff Kirsher 	if (is_multi(bp)) {
2188adfc5217SJeff Kirsher 		k = 0;
2189adfc5217SJeff Kirsher 		for_each_eth_queue(bp, i) {
2190adfc5217SJeff Kirsher 			hw_stats = (u32 *)&bp->fp[i].eth_q_stats;
2191adfc5217SJeff Kirsher 			for (j = 0; j < BNX2X_NUM_Q_STATS; j++) {
2192adfc5217SJeff Kirsher 				if (bnx2x_q_stats_arr[j].size == 0) {
2193adfc5217SJeff Kirsher 					/* skip this counter */
2194adfc5217SJeff Kirsher 					buf[k + j] = 0;
2195adfc5217SJeff Kirsher 					continue;
2196adfc5217SJeff Kirsher 				}
2197adfc5217SJeff Kirsher 				offset = (hw_stats +
2198adfc5217SJeff Kirsher 					  bnx2x_q_stats_arr[j].offset);
2199adfc5217SJeff Kirsher 				if (bnx2x_q_stats_arr[j].size == 4) {
2200adfc5217SJeff Kirsher 					/* 4-byte counter */
2201adfc5217SJeff Kirsher 					buf[k + j] = (u64) *offset;
2202adfc5217SJeff Kirsher 					continue;
2203adfc5217SJeff Kirsher 				}
2204adfc5217SJeff Kirsher 				/* 8-byte counter */
2205adfc5217SJeff Kirsher 				buf[k + j] = HILO_U64(*offset, *(offset + 1));
2206adfc5217SJeff Kirsher 			}
2207adfc5217SJeff Kirsher 			k += BNX2X_NUM_Q_STATS;
2208adfc5217SJeff Kirsher 		}
2209adfc5217SJeff Kirsher 		if (IS_MF_MODE_STAT(bp))
2210adfc5217SJeff Kirsher 			return;
2211adfc5217SJeff Kirsher 		hw_stats = (u32 *)&bp->eth_stats;
2212adfc5217SJeff Kirsher 		for (j = 0; j < BNX2X_NUM_STATS; j++) {
2213adfc5217SJeff Kirsher 			if (bnx2x_stats_arr[j].size == 0) {
2214adfc5217SJeff Kirsher 				/* skip this counter */
2215adfc5217SJeff Kirsher 				buf[k + j] = 0;
2216adfc5217SJeff Kirsher 				continue;
2217adfc5217SJeff Kirsher 			}
2218adfc5217SJeff Kirsher 			offset = (hw_stats + bnx2x_stats_arr[j].offset);
2219adfc5217SJeff Kirsher 			if (bnx2x_stats_arr[j].size == 4) {
2220adfc5217SJeff Kirsher 				/* 4-byte counter */
2221adfc5217SJeff Kirsher 				buf[k + j] = (u64) *offset;
2222adfc5217SJeff Kirsher 				continue;
2223adfc5217SJeff Kirsher 			}
2224adfc5217SJeff Kirsher 			/* 8-byte counter */
2225adfc5217SJeff Kirsher 			buf[k + j] = HILO_U64(*offset, *(offset + 1));
2226adfc5217SJeff Kirsher 		}
2227adfc5217SJeff Kirsher 	} else {
2228adfc5217SJeff Kirsher 		hw_stats = (u32 *)&bp->eth_stats;
2229adfc5217SJeff Kirsher 		for (i = 0, j = 0; i < BNX2X_NUM_STATS; i++) {
2230adfc5217SJeff Kirsher 			if (IS_MF_MODE_STAT(bp) && IS_PORT_STAT(i))
2231adfc5217SJeff Kirsher 				continue;
2232adfc5217SJeff Kirsher 			if (bnx2x_stats_arr[i].size == 0) {
2233adfc5217SJeff Kirsher 				/* skip this counter */
2234adfc5217SJeff Kirsher 				buf[j] = 0;
2235adfc5217SJeff Kirsher 				j++;
2236adfc5217SJeff Kirsher 				continue;
2237adfc5217SJeff Kirsher 			}
2238adfc5217SJeff Kirsher 			offset = (hw_stats + bnx2x_stats_arr[i].offset);
2239adfc5217SJeff Kirsher 			if (bnx2x_stats_arr[i].size == 4) {
2240adfc5217SJeff Kirsher 				/* 4-byte counter */
2241adfc5217SJeff Kirsher 				buf[j] = (u64) *offset;
2242adfc5217SJeff Kirsher 				j++;
2243adfc5217SJeff Kirsher 				continue;
2244adfc5217SJeff Kirsher 			}
2245adfc5217SJeff Kirsher 			/* 8-byte counter */
2246adfc5217SJeff Kirsher 			buf[j] = HILO_U64(*offset, *(offset + 1));
2247adfc5217SJeff Kirsher 			j++;
2248adfc5217SJeff Kirsher 		}
2249adfc5217SJeff Kirsher 	}
2250adfc5217SJeff Kirsher }
2251adfc5217SJeff Kirsher 
2252adfc5217SJeff Kirsher static int bnx2x_set_phys_id(struct net_device *dev,
2253adfc5217SJeff Kirsher 			     enum ethtool_phys_id_state state)
2254adfc5217SJeff Kirsher {
2255adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2256adfc5217SJeff Kirsher 
2257adfc5217SJeff Kirsher 	if (!netif_running(dev))
2258adfc5217SJeff Kirsher 		return -EAGAIN;
2259adfc5217SJeff Kirsher 
2260adfc5217SJeff Kirsher 	if (!bp->port.pmf)
2261adfc5217SJeff Kirsher 		return -EOPNOTSUPP;
2262adfc5217SJeff Kirsher 
2263adfc5217SJeff Kirsher 	switch (state) {
2264adfc5217SJeff Kirsher 	case ETHTOOL_ID_ACTIVE:
2265adfc5217SJeff Kirsher 		return 1;	/* cycle on/off once per second */
2266adfc5217SJeff Kirsher 
2267adfc5217SJeff Kirsher 	case ETHTOOL_ID_ON:
2268adfc5217SJeff Kirsher 		bnx2x_set_led(&bp->link_params, &bp->link_vars,
2269adfc5217SJeff Kirsher 			      LED_MODE_ON, SPEED_1000);
2270adfc5217SJeff Kirsher 		break;
2271adfc5217SJeff Kirsher 
2272adfc5217SJeff Kirsher 	case ETHTOOL_ID_OFF:
2273adfc5217SJeff Kirsher 		bnx2x_set_led(&bp->link_params, &bp->link_vars,
2274adfc5217SJeff Kirsher 			      LED_MODE_FRONT_PANEL_OFF, 0);
2275adfc5217SJeff Kirsher 
2276adfc5217SJeff Kirsher 		break;
2277adfc5217SJeff Kirsher 
2278adfc5217SJeff Kirsher 	case ETHTOOL_ID_INACTIVE:
2279adfc5217SJeff Kirsher 		bnx2x_set_led(&bp->link_params, &bp->link_vars,
2280adfc5217SJeff Kirsher 			      LED_MODE_OPER,
2281adfc5217SJeff Kirsher 			      bp->link_vars.line_speed);
2282adfc5217SJeff Kirsher 	}
2283adfc5217SJeff Kirsher 
2284adfc5217SJeff Kirsher 	return 0;
2285adfc5217SJeff Kirsher }
2286adfc5217SJeff Kirsher 
2287adfc5217SJeff Kirsher static int bnx2x_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
2288815c7db5SBen Hutchings 			   u32 *rules __always_unused)
2289adfc5217SJeff Kirsher {
2290adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2291adfc5217SJeff Kirsher 
2292adfc5217SJeff Kirsher 	switch (info->cmd) {
2293adfc5217SJeff Kirsher 	case ETHTOOL_GRXRINGS:
2294adfc5217SJeff Kirsher 		info->data = BNX2X_NUM_ETH_QUEUES(bp);
2295adfc5217SJeff Kirsher 		return 0;
2296adfc5217SJeff Kirsher 
2297adfc5217SJeff Kirsher 	default:
2298adfc5217SJeff Kirsher 		return -EOPNOTSUPP;
2299adfc5217SJeff Kirsher 	}
2300adfc5217SJeff Kirsher }
2301adfc5217SJeff Kirsher 
2302adfc5217SJeff Kirsher static int bnx2x_get_rxfh_indir(struct net_device *dev,
2303adfc5217SJeff Kirsher 				struct ethtool_rxfh_indir *indir)
2304adfc5217SJeff Kirsher {
2305adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2306adfc5217SJeff Kirsher 	size_t copy_size =
2307adfc5217SJeff Kirsher 		min_t(size_t, indir->size, T_ETH_INDIRECTION_TABLE_SIZE);
2308adfc5217SJeff Kirsher 	u8 ind_table[T_ETH_INDIRECTION_TABLE_SIZE] = {0};
2309adfc5217SJeff Kirsher 	size_t i;
2310adfc5217SJeff Kirsher 
2311adfc5217SJeff Kirsher 	if (bp->multi_mode == ETH_RSS_MODE_DISABLED)
2312adfc5217SJeff Kirsher 		return -EOPNOTSUPP;
2313adfc5217SJeff Kirsher 
2314adfc5217SJeff Kirsher 	/* Get the current configuration of the RSS indirection table */
2315adfc5217SJeff Kirsher 	bnx2x_get_rss_ind_table(&bp->rss_conf_obj, ind_table);
2316adfc5217SJeff Kirsher 
2317adfc5217SJeff Kirsher 	/*
2318adfc5217SJeff Kirsher 	 * We can't use a memcpy() as an internal storage of an
2319adfc5217SJeff Kirsher 	 * indirection table is a u8 array while indir->ring_index
2320adfc5217SJeff Kirsher 	 * points to an array of u32.
2321adfc5217SJeff Kirsher 	 *
2322adfc5217SJeff Kirsher 	 * Indirection table contains the FW Client IDs, so we need to
2323adfc5217SJeff Kirsher 	 * align the returned table to the Client ID of the leading RSS
2324adfc5217SJeff Kirsher 	 * queue.
2325adfc5217SJeff Kirsher 	 */
2326adfc5217SJeff Kirsher 	for (i = 0; i < copy_size; i++)
2327adfc5217SJeff Kirsher 		indir->ring_index[i] = ind_table[i] - bp->fp->cl_id;
2328adfc5217SJeff Kirsher 
2329adfc5217SJeff Kirsher 	indir->size = T_ETH_INDIRECTION_TABLE_SIZE;
2330adfc5217SJeff Kirsher 
2331adfc5217SJeff Kirsher 	return 0;
2332adfc5217SJeff Kirsher }
2333adfc5217SJeff Kirsher 
2334adfc5217SJeff Kirsher static int bnx2x_set_rxfh_indir(struct net_device *dev,
2335adfc5217SJeff Kirsher 				const struct ethtool_rxfh_indir *indir)
2336adfc5217SJeff Kirsher {
2337adfc5217SJeff Kirsher 	struct bnx2x *bp = netdev_priv(dev);
2338adfc5217SJeff Kirsher 	size_t i;
2339adfc5217SJeff Kirsher 	u8 ind_table[T_ETH_INDIRECTION_TABLE_SIZE] = {0};
2340adfc5217SJeff Kirsher 	u32 num_eth_queues = BNX2X_NUM_ETH_QUEUES(bp);
2341adfc5217SJeff Kirsher 
2342adfc5217SJeff Kirsher 	if (bp->multi_mode == ETH_RSS_MODE_DISABLED)
2343adfc5217SJeff Kirsher 		return -EOPNOTSUPP;
2344adfc5217SJeff Kirsher 
2345adfc5217SJeff Kirsher 	/* validate the size */
2346adfc5217SJeff Kirsher 	if (indir->size != T_ETH_INDIRECTION_TABLE_SIZE)
2347adfc5217SJeff Kirsher 		return -EINVAL;
2348adfc5217SJeff Kirsher 
2349adfc5217SJeff Kirsher 	for (i = 0; i < T_ETH_INDIRECTION_TABLE_SIZE; i++) {
2350adfc5217SJeff Kirsher 		/* validate the indices */
2351adfc5217SJeff Kirsher 		if (indir->ring_index[i] >= num_eth_queues)
2352adfc5217SJeff Kirsher 			return -EINVAL;
2353adfc5217SJeff Kirsher 		/*
2354adfc5217SJeff Kirsher 		 * The same as in bnx2x_get_rxfh_indir: we can't use a memcpy()
2355adfc5217SJeff Kirsher 		 * as an internal storage of an indirection table is a u8 array
2356adfc5217SJeff Kirsher 		 * while indir->ring_index points to an array of u32.
2357adfc5217SJeff Kirsher 		 *
2358adfc5217SJeff Kirsher 		 * Indirection table contains the FW Client IDs, so we need to
2359adfc5217SJeff Kirsher 		 * align the received table to the Client ID of the leading RSS
2360adfc5217SJeff Kirsher 		 * queue
2361adfc5217SJeff Kirsher 		 */
2362adfc5217SJeff Kirsher 		ind_table[i] = indir->ring_index[i] + bp->fp->cl_id;
2363adfc5217SJeff Kirsher 	}
2364adfc5217SJeff Kirsher 
2365adfc5217SJeff Kirsher 	return bnx2x_config_rss_pf(bp, ind_table, false);
2366adfc5217SJeff Kirsher }
2367adfc5217SJeff Kirsher 
2368adfc5217SJeff Kirsher static const struct ethtool_ops bnx2x_ethtool_ops = {
2369adfc5217SJeff Kirsher 	.get_settings		= bnx2x_get_settings,
2370adfc5217SJeff Kirsher 	.set_settings		= bnx2x_set_settings,
2371adfc5217SJeff Kirsher 	.get_drvinfo		= bnx2x_get_drvinfo,
2372adfc5217SJeff Kirsher 	.get_regs_len		= bnx2x_get_regs_len,
2373adfc5217SJeff Kirsher 	.get_regs		= bnx2x_get_regs,
2374adfc5217SJeff Kirsher 	.get_wol		= bnx2x_get_wol,
2375adfc5217SJeff Kirsher 	.set_wol		= bnx2x_set_wol,
2376adfc5217SJeff Kirsher 	.get_msglevel		= bnx2x_get_msglevel,
2377adfc5217SJeff Kirsher 	.set_msglevel		= bnx2x_set_msglevel,
2378adfc5217SJeff Kirsher 	.nway_reset		= bnx2x_nway_reset,
2379adfc5217SJeff Kirsher 	.get_link		= bnx2x_get_link,
2380adfc5217SJeff Kirsher 	.get_eeprom_len		= bnx2x_get_eeprom_len,
2381adfc5217SJeff Kirsher 	.get_eeprom		= bnx2x_get_eeprom,
2382adfc5217SJeff Kirsher 	.set_eeprom		= bnx2x_set_eeprom,
2383adfc5217SJeff Kirsher 	.get_coalesce		= bnx2x_get_coalesce,
2384adfc5217SJeff Kirsher 	.set_coalesce		= bnx2x_set_coalesce,
2385adfc5217SJeff Kirsher 	.get_ringparam		= bnx2x_get_ringparam,
2386adfc5217SJeff Kirsher 	.set_ringparam		= bnx2x_set_ringparam,
2387adfc5217SJeff Kirsher 	.get_pauseparam		= bnx2x_get_pauseparam,
2388adfc5217SJeff Kirsher 	.set_pauseparam		= bnx2x_set_pauseparam,
2389adfc5217SJeff Kirsher 	.self_test		= bnx2x_self_test,
2390adfc5217SJeff Kirsher 	.get_sset_count		= bnx2x_get_sset_count,
2391adfc5217SJeff Kirsher 	.get_strings		= bnx2x_get_strings,
2392adfc5217SJeff Kirsher 	.set_phys_id		= bnx2x_set_phys_id,
2393adfc5217SJeff Kirsher 	.get_ethtool_stats	= bnx2x_get_ethtool_stats,
2394adfc5217SJeff Kirsher 	.get_rxnfc		= bnx2x_get_rxnfc,
2395adfc5217SJeff Kirsher 	.get_rxfh_indir		= bnx2x_get_rxfh_indir,
2396adfc5217SJeff Kirsher 	.set_rxfh_indir		= bnx2x_set_rxfh_indir,
2397adfc5217SJeff Kirsher };
2398adfc5217SJeff Kirsher 
2399adfc5217SJeff Kirsher void bnx2x_set_ethtool_ops(struct net_device *netdev)
2400adfc5217SJeff Kirsher {
2401adfc5217SJeff Kirsher 	SET_ETHTOOL_OPS(netdev, &bnx2x_ethtool_ops);
2402adfc5217SJeff Kirsher }
2403