xref: /linux/drivers/net/ethernet/broadcom/bnx2x/bnx2x_link.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /* Copyright 2008-2013 Broadcom Corporation
2  * Copyright (c) 2014 QLogic Corporation
3  * All rights reserved
4  *
5  * Unless you and QLogic execute a separate written software license
6  * agreement governing use of this software, this software is licensed to you
7  * under the terms of the GNU General Public License version 2, available
8  * at http://www.gnu.org/licenses/gpl-2.0.html (the "GPL").
9  *
10  * Notwithstanding the above, under no circumstances may you combine this
11  * software in any way with any other Qlogic software provided under a
12  * license other than the GPL, without Qlogic's express prior written
13  * consent.
14  *
15  * Written by Yaniv Rosner
16  *
17  */
18 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/pci.h>
24 #include <linux/netdevice.h>
25 #include <linux/delay.h>
26 #include <linux/ethtool.h>
27 #include <linux/mutex.h>
28 
29 #include "bnx2x.h"
30 #include "bnx2x_cmn.h"
31 
32 typedef int (*read_sfp_module_eeprom_func_p)(struct bnx2x_phy *phy,
33 					     struct link_params *params,
34 					     u8 dev_addr, u16 addr, u8 byte_cnt,
35 					     u8 *o_buf, u8);
36 /********************************************************/
37 #define MDIO_ACCESS_TIMEOUT		1000
38 #define WC_LANE_MAX			4
39 #define I2C_SWITCH_WIDTH		2
40 #define I2C_BSC0			0
41 #define I2C_BSC1			1
42 #define I2C_WA_RETRY_CNT		3
43 #define I2C_WA_PWR_ITER			(I2C_WA_RETRY_CNT - 1)
44 #define MCPR_IMC_COMMAND_READ_OP	1
45 #define MCPR_IMC_COMMAND_WRITE_OP	2
46 
47 /* LED Blink rate that will achieve ~15.9Hz */
48 #define LED_BLINK_RATE_VAL_E3		354
49 #define LED_BLINK_RATE_VAL_E1X_E2	480
50 /***********************************************************/
51 /*			Shortcut definitions		   */
52 /***********************************************************/
53 
54 #define NIG_LATCH_BC_ENABLE_MI_INT 0
55 
56 #define NIG_STATUS_EMAC0_MI_INT \
57 		NIG_STATUS_INTERRUPT_PORT0_REG_STATUS_EMAC0_MISC_MI_INT
58 #define NIG_STATUS_XGXS0_LINK10G \
59 		NIG_STATUS_INTERRUPT_PORT0_REG_STATUS_XGXS0_LINK10G
60 #define NIG_STATUS_XGXS0_LINK_STATUS \
61 		NIG_STATUS_INTERRUPT_PORT0_REG_STATUS_XGXS0_LINK_STATUS
62 #define NIG_STATUS_XGXS0_LINK_STATUS_SIZE \
63 		NIG_STATUS_INTERRUPT_PORT0_REG_STATUS_XGXS0_LINK_STATUS_SIZE
64 #define NIG_STATUS_SERDES0_LINK_STATUS \
65 		NIG_STATUS_INTERRUPT_PORT0_REG_STATUS_SERDES0_LINK_STATUS
66 #define NIG_MASK_MI_INT \
67 		NIG_MASK_INTERRUPT_PORT0_REG_MASK_EMAC0_MISC_MI_INT
68 #define NIG_MASK_XGXS0_LINK10G \
69 		NIG_MASK_INTERRUPT_PORT0_REG_MASK_XGXS0_LINK10G
70 #define NIG_MASK_XGXS0_LINK_STATUS \
71 		NIG_MASK_INTERRUPT_PORT0_REG_MASK_XGXS0_LINK_STATUS
72 #define NIG_MASK_SERDES0_LINK_STATUS \
73 		NIG_MASK_INTERRUPT_PORT0_REG_MASK_SERDES0_LINK_STATUS
74 
75 #define MDIO_AN_CL73_OR_37_COMPLETE \
76 		(MDIO_GP_STATUS_TOP_AN_STATUS1_CL73_AUTONEG_COMPLETE | \
77 		 MDIO_GP_STATUS_TOP_AN_STATUS1_CL37_AUTONEG_COMPLETE)
78 
79 #define XGXS_RESET_BITS \
80 	(MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_XGXS0_RSTB_HW |   \
81 	 MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_XGXS0_IDDQ |      \
82 	 MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_XGXS0_PWRDWN |    \
83 	 MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_XGXS0_PWRDWN_SD | \
84 	 MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_XGXS0_TXD_FIFO_RSTB)
85 
86 #define SERDES_RESET_BITS \
87 	(MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_SERDES0_RSTB_HW | \
88 	 MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_SERDES0_IDDQ |    \
89 	 MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_SERDES0_PWRDWN |  \
90 	 MISC_REGISTERS_RESET_REG_3_MISC_NIG_MUX_SERDES0_PWRDWN_SD)
91 
92 #define AUTONEG_CL37		SHARED_HW_CFG_AN_ENABLE_CL37
93 #define AUTONEG_CL73		SHARED_HW_CFG_AN_ENABLE_CL73
94 #define AUTONEG_BAM		SHARED_HW_CFG_AN_ENABLE_BAM
95 #define AUTONEG_PARALLEL \
96 				SHARED_HW_CFG_AN_ENABLE_PARALLEL_DETECTION
97 #define AUTONEG_SGMII_FIBER_AUTODET \
98 				SHARED_HW_CFG_AN_EN_SGMII_FIBER_AUTO_DETECT
99 #define AUTONEG_REMOTE_PHY	SHARED_HW_CFG_AN_ENABLE_REMOTE_PHY
100 
101 #define GP_STATUS_PAUSE_RSOLUTION_TXSIDE \
102 			MDIO_GP_STATUS_TOP_AN_STATUS1_PAUSE_RSOLUTION_TXSIDE
103 #define GP_STATUS_PAUSE_RSOLUTION_RXSIDE \
104 			MDIO_GP_STATUS_TOP_AN_STATUS1_PAUSE_RSOLUTION_RXSIDE
105 #define GP_STATUS_SPEED_MASK \
106 			MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_MASK
107 #define GP_STATUS_10M	MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_10M
108 #define GP_STATUS_100M	MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_100M
109 #define GP_STATUS_1G	MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_1G
110 #define GP_STATUS_2_5G	MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_2_5G
111 #define GP_STATUS_5G	MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_5G
112 #define GP_STATUS_6G	MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_6G
113 #define GP_STATUS_10G_HIG \
114 			MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_10G_HIG
115 #define GP_STATUS_10G_CX4 \
116 			MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_10G_CX4
117 #define GP_STATUS_1G_KX MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_1G_KX
118 #define GP_STATUS_10G_KX4 \
119 			MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_10G_KX4
120 #define	GP_STATUS_10G_KR MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_10G_KR
121 #define	GP_STATUS_10G_XFI   MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_10G_XFI
122 #define	GP_STATUS_20G_DXGXS MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_20G_DXGXS
123 #define	GP_STATUS_10G_SFI   MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_10G_SFI
124 #define	GP_STATUS_20G_KR2 MDIO_GP_STATUS_TOP_AN_STATUS1_ACTUAL_SPEED_20G_KR2
125 #define LINK_10THD		LINK_STATUS_SPEED_AND_DUPLEX_10THD
126 #define LINK_10TFD		LINK_STATUS_SPEED_AND_DUPLEX_10TFD
127 #define LINK_100TXHD		LINK_STATUS_SPEED_AND_DUPLEX_100TXHD
128 #define LINK_100T4		LINK_STATUS_SPEED_AND_DUPLEX_100T4
129 #define LINK_100TXFD		LINK_STATUS_SPEED_AND_DUPLEX_100TXFD
130 #define LINK_1000THD		LINK_STATUS_SPEED_AND_DUPLEX_1000THD
131 #define LINK_1000TFD		LINK_STATUS_SPEED_AND_DUPLEX_1000TFD
132 #define LINK_1000XFD		LINK_STATUS_SPEED_AND_DUPLEX_1000XFD
133 #define LINK_2500THD		LINK_STATUS_SPEED_AND_DUPLEX_2500THD
134 #define LINK_2500TFD		LINK_STATUS_SPEED_AND_DUPLEX_2500TFD
135 #define LINK_2500XFD		LINK_STATUS_SPEED_AND_DUPLEX_2500XFD
136 #define LINK_10GTFD		LINK_STATUS_SPEED_AND_DUPLEX_10GTFD
137 #define LINK_10GXFD		LINK_STATUS_SPEED_AND_DUPLEX_10GXFD
138 #define LINK_20GTFD		LINK_STATUS_SPEED_AND_DUPLEX_20GTFD
139 #define LINK_20GXFD		LINK_STATUS_SPEED_AND_DUPLEX_20GXFD
140 
141 #define LINK_UPDATE_MASK \
142 			(LINK_STATUS_SPEED_AND_DUPLEX_MASK | \
143 			 LINK_STATUS_LINK_UP | \
144 			 LINK_STATUS_PHYSICAL_LINK_FLAG | \
145 			 LINK_STATUS_AUTO_NEGOTIATE_COMPLETE | \
146 			 LINK_STATUS_RX_FLOW_CONTROL_FLAG_MASK | \
147 			 LINK_STATUS_TX_FLOW_CONTROL_FLAG_MASK | \
148 			 LINK_STATUS_PARALLEL_DETECTION_FLAG_MASK | \
149 			 LINK_STATUS_LINK_PARTNER_SYMMETRIC_PAUSE | \
150 			 LINK_STATUS_LINK_PARTNER_ASYMMETRIC_PAUSE)
151 
152 #define SFP_EEPROM_CON_TYPE_ADDR		0x2
153 	#define SFP_EEPROM_CON_TYPE_VAL_UNKNOWN	0x0
154 	#define SFP_EEPROM_CON_TYPE_VAL_LC	0x7
155 	#define SFP_EEPROM_CON_TYPE_VAL_COPPER	0x21
156 	#define SFP_EEPROM_CON_TYPE_VAL_RJ45	0x22
157 
158 
159 #define SFP_EEPROM_10G_COMP_CODE_ADDR		0x3
160 	#define SFP_EEPROM_10G_COMP_CODE_SR_MASK	(1<<4)
161 	#define SFP_EEPROM_10G_COMP_CODE_LR_MASK	(1<<5)
162 	#define SFP_EEPROM_10G_COMP_CODE_LRM_MASK	(1<<6)
163 
164 #define SFP_EEPROM_1G_COMP_CODE_ADDR		0x6
165 	#define SFP_EEPROM_1G_COMP_CODE_SX	(1<<0)
166 	#define SFP_EEPROM_1G_COMP_CODE_LX	(1<<1)
167 	#define SFP_EEPROM_1G_COMP_CODE_CX	(1<<2)
168 	#define SFP_EEPROM_1G_COMP_CODE_BASE_T	(1<<3)
169 
170 #define SFP_EEPROM_FC_TX_TECH_ADDR		0x8
171 	#define SFP_EEPROM_FC_TX_TECH_BITMASK_COPPER_PASSIVE 0x4
172 	#define SFP_EEPROM_FC_TX_TECH_BITMASK_COPPER_ACTIVE  0x8
173 
174 #define SFP_EEPROM_OPTIONS_ADDR			0x40
175 	#define SFP_EEPROM_OPTIONS_LINEAR_RX_OUT_MASK 0x1
176 #define SFP_EEPROM_OPTIONS_SIZE			2
177 
178 #define EDC_MODE_LINEAR				0x0022
179 #define EDC_MODE_LIMITING				0x0044
180 #define EDC_MODE_PASSIVE_DAC			0x0055
181 #define EDC_MODE_ACTIVE_DAC			0x0066
182 
183 /* ETS defines*/
184 #define DCBX_INVALID_COS					(0xFF)
185 
186 #define ETS_BW_LIMIT_CREDIT_UPPER_BOUND		(0x5000)
187 #define ETS_BW_LIMIT_CREDIT_WEIGHT		(0x5000)
188 #define ETS_E3B0_NIG_MIN_W_VAL_UP_TO_10GBPS		(1360)
189 #define ETS_E3B0_NIG_MIN_W_VAL_20GBPS			(2720)
190 #define ETS_E3B0_PBF_MIN_W_VAL				(10000)
191 
192 #define MAX_PACKET_SIZE					(9700)
193 #define MAX_KR_LINK_RETRY				4
194 #define DEFAULT_TX_DRV_BRDCT		2
195 #define DEFAULT_TX_DRV_IFIR		0
196 #define DEFAULT_TX_DRV_POST2		3
197 #define DEFAULT_TX_DRV_IPRE_DRIVER	6
198 
199 /**********************************************************/
200 /*                     INTERFACE                          */
201 /**********************************************************/
202 
203 #define CL22_WR_OVER_CL45(_bp, _phy, _bank, _addr, _val) \
204 	bnx2x_cl45_write(_bp, _phy, \
205 		(_phy)->def_md_devad, \
206 		(_bank + (_addr & 0xf)), \
207 		_val)
208 
209 #define CL22_RD_OVER_CL45(_bp, _phy, _bank, _addr, _val) \
210 	bnx2x_cl45_read(_bp, _phy, \
211 		(_phy)->def_md_devad, \
212 		(_bank + (_addr & 0xf)), \
213 		_val)
214 
215 static int bnx2x_check_half_open_conn(struct link_params *params,
216 				      struct link_vars *vars, u8 notify);
217 static int bnx2x_sfp_module_detection(struct bnx2x_phy *phy,
218 				      struct link_params *params);
219 
220 static u32 bnx2x_bits_en(struct bnx2x *bp, u32 reg, u32 bits)
221 {
222 	u32 val = REG_RD(bp, reg);
223 
224 	val |= bits;
225 	REG_WR(bp, reg, val);
226 	return val;
227 }
228 
229 static u32 bnx2x_bits_dis(struct bnx2x *bp, u32 reg, u32 bits)
230 {
231 	u32 val = REG_RD(bp, reg);
232 
233 	val &= ~bits;
234 	REG_WR(bp, reg, val);
235 	return val;
236 }
237 
238 /*
239  * bnx2x_check_lfa - This function checks if link reinitialization is required,
240  *                   or link flap can be avoided.
241  *
242  * @params:	link parameters
243  * Returns 0 if Link Flap Avoidance conditions are met otherwise, the failed
244  *         condition code.
245  */
246 static int bnx2x_check_lfa(struct link_params *params)
247 {
248 	u32 link_status, cfg_idx, lfa_mask, cfg_size;
249 	u32 cur_speed_cap_mask, cur_req_fc_auto_adv, additional_config;
250 	u32 saved_val, req_val, eee_status;
251 	struct bnx2x *bp = params->bp;
252 
253 	additional_config =
254 		REG_RD(bp, params->lfa_base +
255 			   offsetof(struct shmem_lfa, additional_config));
256 
257 	/* NOTE: must be first condition checked -
258 	* to verify DCC bit is cleared in any case!
259 	*/
260 	if (additional_config & NO_LFA_DUE_TO_DCC_MASK) {
261 		DP(NETIF_MSG_LINK, "No LFA due to DCC flap after clp exit\n");
262 		REG_WR(bp, params->lfa_base +
263 			   offsetof(struct shmem_lfa, additional_config),
264 		       additional_config & ~NO_LFA_DUE_TO_DCC_MASK);
265 		return LFA_DCC_LFA_DISABLED;
266 	}
267 
268 	/* Verify that link is up */
269 	link_status = REG_RD(bp, params->shmem_base +
270 			     offsetof(struct shmem_region,
271 				      port_mb[params->port].link_status));
272 	if (!(link_status & LINK_STATUS_LINK_UP))
273 		return LFA_LINK_DOWN;
274 
275 	/* if loaded after BOOT from SAN, don't flap the link in any case and
276 	 * rely on link set by preboot driver
277 	 */
278 	if (params->feature_config_flags & FEATURE_CONFIG_BOOT_FROM_SAN)
279 		return 0;
280 
281 	/* Verify that loopback mode is not set */
282 	if (params->loopback_mode)
283 		return LFA_LOOPBACK_ENABLED;
284 
285 	/* Verify that MFW supports LFA */
286 	if (!params->lfa_base)
287 		return LFA_MFW_IS_TOO_OLD;
288 
289 	if (params->num_phys == 3) {
290 		cfg_size = 2;
291 		lfa_mask = 0xffffffff;
292 	} else {
293 		cfg_size = 1;
294 		lfa_mask = 0xffff;
295 	}
296 
297 	/* Compare Duplex */
298 	saved_val = REG_RD(bp, params->lfa_base +
299 			   offsetof(struct shmem_lfa, req_duplex));
300 	req_val = params->req_duplex[0] | (params->req_duplex[1] << 16);
301 	if ((saved_val & lfa_mask) != (req_val & lfa_mask)) {
302 		DP(NETIF_MSG_LINK, "Duplex mismatch %x vs. %x\n",
303 			       (saved_val & lfa_mask), (req_val & lfa_mask));
304 		return LFA_DUPLEX_MISMATCH;
305 	}
306 	/* Compare Flow Control */
307 	saved_val = REG_RD(bp, params->lfa_base +
308 			   offsetof(struct shmem_lfa, req_flow_ctrl));
309 	req_val = params->req_flow_ctrl[0] | (params->req_flow_ctrl[1] << 16);
310 	if ((saved_val & lfa_mask) != (req_val & lfa_mask)) {
311 		DP(NETIF_MSG_LINK, "Flow control mismatch %x vs. %x\n",
312 			       (saved_val & lfa_mask), (req_val & lfa_mask));
313 		return LFA_FLOW_CTRL_MISMATCH;
314 	}
315 	/* Compare Link Speed */
316 	saved_val = REG_RD(bp, params->lfa_base +
317 			   offsetof(struct shmem_lfa, req_line_speed));
318 	req_val = params->req_line_speed[0] | (params->req_line_speed[1] << 16);
319 	if ((saved_val & lfa_mask) != (req_val & lfa_mask)) {
320 		DP(NETIF_MSG_LINK, "Link speed mismatch %x vs. %x\n",
321 			       (saved_val & lfa_mask), (req_val & lfa_mask));
322 		return LFA_LINK_SPEED_MISMATCH;
323 	}
324 
325 	for (cfg_idx = 0; cfg_idx < cfg_size; cfg_idx++) {
326 		cur_speed_cap_mask = REG_RD(bp, params->lfa_base +
327 					    offsetof(struct shmem_lfa,
328 						     speed_cap_mask[cfg_idx]));
329 
330 		if (cur_speed_cap_mask != params->speed_cap_mask[cfg_idx]) {
331 			DP(NETIF_MSG_LINK, "Speed Cap mismatch %x vs. %x\n",
332 				       cur_speed_cap_mask,
333 				       params->speed_cap_mask[cfg_idx]);
334 			return LFA_SPEED_CAP_MISMATCH;
335 		}
336 	}
337 
338 	cur_req_fc_auto_adv =
339 		REG_RD(bp, params->lfa_base +
340 		       offsetof(struct shmem_lfa, additional_config)) &
341 		REQ_FC_AUTO_ADV_MASK;
342 
343 	if ((u16)cur_req_fc_auto_adv != params->req_fc_auto_adv) {
344 		DP(NETIF_MSG_LINK, "Flow Ctrl AN mismatch %x vs. %x\n",
345 			       cur_req_fc_auto_adv, params->req_fc_auto_adv);
346 		return LFA_FLOW_CTRL_MISMATCH;
347 	}
348 
349 	eee_status = REG_RD(bp, params->shmem2_base +
350 			    offsetof(struct shmem2_region,
351 				     eee_status[params->port]));
352 
353 	if (((eee_status & SHMEM_EEE_LPI_REQUESTED_BIT) ^
354 	     (params->eee_mode & EEE_MODE_ENABLE_LPI)) ||
355 	    ((eee_status & SHMEM_EEE_REQUESTED_BIT) ^
356 	     (params->eee_mode & EEE_MODE_ADV_LPI))) {
357 		DP(NETIF_MSG_LINK, "EEE mismatch %x vs. %x\n", params->eee_mode,
358 			       eee_status);
359 		return LFA_EEE_MISMATCH;
360 	}
361 
362 	/* LFA conditions are met */
363 	return 0;
364 }
365 /******************************************************************/
366 /*			EPIO/GPIO section			  */
367 /******************************************************************/
368 static void bnx2x_get_epio(struct bnx2x *bp, u32 epio_pin, u32 *en)
369 {
370 	u32 epio_mask, gp_oenable;
371 	*en = 0;
372 	/* Sanity check */
373 	if (epio_pin > 31) {
374 		DP(NETIF_MSG_LINK, "Invalid EPIO pin %d to get\n", epio_pin);
375 		return;
376 	}
377 
378 	epio_mask = 1 << epio_pin;
379 	/* Set this EPIO to output */
380 	gp_oenable = REG_RD(bp, MCP_REG_MCPR_GP_OENABLE);
381 	REG_WR(bp, MCP_REG_MCPR_GP_OENABLE, gp_oenable & ~epio_mask);
382 
383 	*en = (REG_RD(bp, MCP_REG_MCPR_GP_INPUTS) & epio_mask) >> epio_pin;
384 }
385 static void bnx2x_set_epio(struct bnx2x *bp, u32 epio_pin, u32 en)
386 {
387 	u32 epio_mask, gp_output, gp_oenable;
388 
389 	/* Sanity check */
390 	if (epio_pin > 31) {
391 		DP(NETIF_MSG_LINK, "Invalid EPIO pin %d to set\n", epio_pin);
392 		return;
393 	}
394 	DP(NETIF_MSG_LINK, "Setting EPIO pin %d to %d\n", epio_pin, en);
395 	epio_mask = 1 << epio_pin;
396 	/* Set this EPIO to output */
397 	gp_output = REG_RD(bp, MCP_REG_MCPR_GP_OUTPUTS);
398 	if (en)
399 		gp_output |= epio_mask;
400 	else
401 		gp_output &= ~epio_mask;
402 
403 	REG_WR(bp, MCP_REG_MCPR_GP_OUTPUTS, gp_output);
404 
405 	/* Set the value for this EPIO */
406 	gp_oenable = REG_RD(bp, MCP_REG_MCPR_GP_OENABLE);
407 	REG_WR(bp, MCP_REG_MCPR_GP_OENABLE, gp_oenable | epio_mask);
408 }
409 
410 static void bnx2x_set_cfg_pin(struct bnx2x *bp, u32 pin_cfg, u32 val)
411 {
412 	if (pin_cfg == PIN_CFG_NA)
413 		return;
414 	if (pin_cfg >= PIN_CFG_EPIO0) {
415 		bnx2x_set_epio(bp, pin_cfg - PIN_CFG_EPIO0, val);
416 	} else {
417 		u8 gpio_num = (pin_cfg - PIN_CFG_GPIO0_P0) & 0x3;
418 		u8 gpio_port = (pin_cfg - PIN_CFG_GPIO0_P0) >> 2;
419 		bnx2x_set_gpio(bp, gpio_num, (u8)val, gpio_port);
420 	}
421 }
422 
423 static u32 bnx2x_get_cfg_pin(struct bnx2x *bp, u32 pin_cfg, u32 *val)
424 {
425 	if (pin_cfg == PIN_CFG_NA)
426 		return -EINVAL;
427 	if (pin_cfg >= PIN_CFG_EPIO0) {
428 		bnx2x_get_epio(bp, pin_cfg - PIN_CFG_EPIO0, val);
429 	} else {
430 		u8 gpio_num = (pin_cfg - PIN_CFG_GPIO0_P0) & 0x3;
431 		u8 gpio_port = (pin_cfg - PIN_CFG_GPIO0_P0) >> 2;
432 		*val = bnx2x_get_gpio(bp, gpio_num, gpio_port);
433 	}
434 	return 0;
435 
436 }
437 /******************************************************************/
438 /*				ETS section			  */
439 /******************************************************************/
440 static void bnx2x_ets_e2e3a0_disabled(struct link_params *params)
441 {
442 	/* ETS disabled configuration*/
443 	struct bnx2x *bp = params->bp;
444 
445 	DP(NETIF_MSG_LINK, "ETS E2E3 disabled configuration\n");
446 
447 	/* mapping between entry  priority to client number (0,1,2 -debug and
448 	 * management clients, 3 - COS0 client, 4 - COS client)(HIGHEST)
449 	 * 3bits client num.
450 	 *   PRI4    |    PRI3    |    PRI2    |    PRI1    |    PRI0
451 	 * cos1-100     cos0-011     dbg1-010     dbg0-001     MCP-000
452 	 */
453 
454 	REG_WR(bp, NIG_REG_P0_TX_ARB_PRIORITY_CLIENT, 0x4688);
455 	/* Bitmap of 5bits length. Each bit specifies whether the entry behaves
456 	 * as strict.  Bits 0,1,2 - debug and management entries, 3 -
457 	 * COS0 entry, 4 - COS1 entry.
458 	 * COS1 | COS0 | DEBUG1 | DEBUG0 | MGMT
459 	 * bit4   bit3	  bit2   bit1	  bit0
460 	 * MCP and debug are strict
461 	 */
462 
463 	REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_IS_STRICT, 0x7);
464 	/* defines which entries (clients) are subjected to WFQ arbitration */
465 	REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_IS_SUBJECT2WFQ, 0);
466 	/* For strict priority entries defines the number of consecutive
467 	 * slots for the highest priority.
468 	 */
469 	REG_WR(bp, NIG_REG_P0_TX_ARB_NUM_STRICT_ARB_SLOTS, 0x100);
470 	/* mapping between the CREDIT_WEIGHT registers and actual client
471 	 * numbers
472 	 */
473 	REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_CREDIT_MAP, 0);
474 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_0, 0);
475 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_1, 0);
476 
477 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_0, 0);
478 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_1, 0);
479 	REG_WR(bp, PBF_REG_HIGH_PRIORITY_COS_NUM, 0);
480 	/* ETS mode disable */
481 	REG_WR(bp, PBF_REG_ETS_ENABLED, 0);
482 	/* If ETS mode is enabled (there is no strict priority) defines a WFQ
483 	 * weight for COS0/COS1.
484 	 */
485 	REG_WR(bp, PBF_REG_COS0_WEIGHT, 0x2710);
486 	REG_WR(bp, PBF_REG_COS1_WEIGHT, 0x2710);
487 	/* Upper bound that COS0_WEIGHT can reach in the WFQ arbiter */
488 	REG_WR(bp, PBF_REG_COS0_UPPER_BOUND, 0x989680);
489 	REG_WR(bp, PBF_REG_COS1_UPPER_BOUND, 0x989680);
490 	/* Defines the number of consecutive slots for the strict priority */
491 	REG_WR(bp, PBF_REG_NUM_STRICT_ARB_SLOTS, 0);
492 }
493 /******************************************************************************
494 * Description:
495 *	Getting min_w_val will be set according to line speed .
496 *.
497 ******************************************************************************/
498 static u32 bnx2x_ets_get_min_w_val_nig(const struct link_vars *vars)
499 {
500 	u32 min_w_val = 0;
501 	/* Calculate min_w_val.*/
502 	if (vars->link_up) {
503 		if (vars->line_speed == SPEED_20000)
504 			min_w_val = ETS_E3B0_NIG_MIN_W_VAL_20GBPS;
505 		else
506 			min_w_val = ETS_E3B0_NIG_MIN_W_VAL_UP_TO_10GBPS;
507 	} else
508 		min_w_val = ETS_E3B0_NIG_MIN_W_VAL_20GBPS;
509 	/* If the link isn't up (static configuration for example ) The
510 	 * link will be according to 20GBPS.
511 	 */
512 	return min_w_val;
513 }
514 /******************************************************************************
515 * Description:
516 *	Getting credit upper bound form min_w_val.
517 *.
518 ******************************************************************************/
519 static u32 bnx2x_ets_get_credit_upper_bound(const u32 min_w_val)
520 {
521 	const u32 credit_upper_bound = (u32)MAXVAL((150 * min_w_val),
522 						MAX_PACKET_SIZE);
523 	return credit_upper_bound;
524 }
525 /******************************************************************************
526 * Description:
527 *	Set credit upper bound for NIG.
528 *.
529 ******************************************************************************/
530 static void bnx2x_ets_e3b0_set_credit_upper_bound_nig(
531 	const struct link_params *params,
532 	const u32 min_w_val)
533 {
534 	struct bnx2x *bp = params->bp;
535 	const u8 port = params->port;
536 	const u32 credit_upper_bound =
537 	    bnx2x_ets_get_credit_upper_bound(min_w_val);
538 
539 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_UPPER_BOUND_0 :
540 		NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_0, credit_upper_bound);
541 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_UPPER_BOUND_1 :
542 		   NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_1, credit_upper_bound);
543 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_UPPER_BOUND_2 :
544 		   NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_2, credit_upper_bound);
545 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_UPPER_BOUND_3 :
546 		   NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_3, credit_upper_bound);
547 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_UPPER_BOUND_4 :
548 		   NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_4, credit_upper_bound);
549 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_UPPER_BOUND_5 :
550 		   NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_5, credit_upper_bound);
551 
552 	if (!port) {
553 		REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_6,
554 			credit_upper_bound);
555 		REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_7,
556 			credit_upper_bound);
557 		REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_8,
558 			credit_upper_bound);
559 	}
560 }
561 /******************************************************************************
562 * Description:
563 *	Will return the NIG ETS registers to init values.Except
564 *	credit_upper_bound.
565 *	That isn't used in this configuration (No WFQ is enabled) and will be
566 *	configured according to spec
567 *.
568 ******************************************************************************/
569 static void bnx2x_ets_e3b0_nig_disabled(const struct link_params *params,
570 					const struct link_vars *vars)
571 {
572 	struct bnx2x *bp = params->bp;
573 	const u8 port = params->port;
574 	const u32 min_w_val = bnx2x_ets_get_min_w_val_nig(vars);
575 	/* Mapping between entry  priority to client number (0,1,2 -debug and
576 	 * management clients, 3 - COS0 client, 4 - COS1, ... 8 -
577 	 * COS5)(HIGHEST) 4bits client num.TODO_ETS - Should be done by
578 	 * reset value or init tool
579 	 */
580 	if (port) {
581 		REG_WR(bp, NIG_REG_P1_TX_ARB_PRIORITY_CLIENT2_LSB, 0x543210);
582 		REG_WR(bp, NIG_REG_P1_TX_ARB_PRIORITY_CLIENT2_MSB, 0x0);
583 	} else {
584 		REG_WR(bp, NIG_REG_P0_TX_ARB_PRIORITY_CLIENT2_LSB, 0x76543210);
585 		REG_WR(bp, NIG_REG_P0_TX_ARB_PRIORITY_CLIENT2_MSB, 0x8);
586 	}
587 	/* For strict priority entries defines the number of consecutive
588 	 * slots for the highest priority.
589 	 */
590 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_NUM_STRICT_ARB_SLOTS :
591 		   NIG_REG_P1_TX_ARB_NUM_STRICT_ARB_SLOTS, 0x100);
592 	/* Mapping between the CREDIT_WEIGHT registers and actual client
593 	 * numbers
594 	 */
595 	if (port) {
596 		/*Port 1 has 6 COS*/
597 		REG_WR(bp, NIG_REG_P1_TX_ARB_CLIENT_CREDIT_MAP2_LSB, 0x210543);
598 		REG_WR(bp, NIG_REG_P1_TX_ARB_CLIENT_CREDIT_MAP2_MSB, 0x0);
599 	} else {
600 		/*Port 0 has 9 COS*/
601 		REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_CREDIT_MAP2_LSB,
602 		       0x43210876);
603 		REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_CREDIT_MAP2_MSB, 0x5);
604 	}
605 
606 	/* Bitmap of 5bits length. Each bit specifies whether the entry behaves
607 	 * as strict.  Bits 0,1,2 - debug and management entries, 3 -
608 	 * COS0 entry, 4 - COS1 entry.
609 	 * COS1 | COS0 | DEBUG1 | DEBUG0 | MGMT
610 	 * bit4   bit3	  bit2   bit1	  bit0
611 	 * MCP and debug are strict
612 	 */
613 	if (port)
614 		REG_WR(bp, NIG_REG_P1_TX_ARB_CLIENT_IS_STRICT, 0x3f);
615 	else
616 		REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_IS_STRICT, 0x1ff);
617 	/* defines which entries (clients) are subjected to WFQ arbitration */
618 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CLIENT_IS_SUBJECT2WFQ :
619 		   NIG_REG_P0_TX_ARB_CLIENT_IS_SUBJECT2WFQ, 0);
620 
621 	/* Please notice the register address are note continuous and a
622 	 * for here is note appropriate.In 2 port mode port0 only COS0-5
623 	 * can be used. DEBUG1,DEBUG1,MGMT are never used for WFQ* In 4
624 	 * port mode port1 only COS0-2 can be used. DEBUG1,DEBUG1,MGMT
625 	 * are never used for WFQ
626 	 */
627 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_0 :
628 		   NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_0, 0x0);
629 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_1 :
630 		   NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_1, 0x0);
631 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_2 :
632 		   NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_2, 0x0);
633 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_3 :
634 		   NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_3, 0x0);
635 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_4 :
636 		   NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_4, 0x0);
637 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_5 :
638 		   NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_5, 0x0);
639 	if (!port) {
640 		REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_6, 0x0);
641 		REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_7, 0x0);
642 		REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_8, 0x0);
643 	}
644 
645 	bnx2x_ets_e3b0_set_credit_upper_bound_nig(params, min_w_val);
646 }
647 /******************************************************************************
648 * Description:
649 *	Set credit upper bound for PBF.
650 *.
651 ******************************************************************************/
652 static void bnx2x_ets_e3b0_set_credit_upper_bound_pbf(
653 	const struct link_params *params,
654 	const u32 min_w_val)
655 {
656 	struct bnx2x *bp = params->bp;
657 	const u32 credit_upper_bound =
658 	    bnx2x_ets_get_credit_upper_bound(min_w_val);
659 	const u8 port = params->port;
660 	u32 base_upper_bound = 0;
661 	u8 max_cos = 0;
662 	u8 i = 0;
663 	/* In 2 port mode port0 has COS0-5 that can be used for WFQ.In 4
664 	 * port mode port1 has COS0-2 that can be used for WFQ.
665 	 */
666 	if (!port) {
667 		base_upper_bound = PBF_REG_COS0_UPPER_BOUND_P0;
668 		max_cos = DCBX_E3B0_MAX_NUM_COS_PORT0;
669 	} else {
670 		base_upper_bound = PBF_REG_COS0_UPPER_BOUND_P1;
671 		max_cos = DCBX_E3B0_MAX_NUM_COS_PORT1;
672 	}
673 
674 	for (i = 0; i < max_cos; i++)
675 		REG_WR(bp, base_upper_bound + (i << 2), credit_upper_bound);
676 }
677 
678 /******************************************************************************
679 * Description:
680 *	Will return the PBF ETS registers to init values.Except
681 *	credit_upper_bound.
682 *	That isn't used in this configuration (No WFQ is enabled) and will be
683 *	configured according to spec
684 *.
685 ******************************************************************************/
686 static void bnx2x_ets_e3b0_pbf_disabled(const struct link_params *params)
687 {
688 	struct bnx2x *bp = params->bp;
689 	const u8 port = params->port;
690 	const u32 min_w_val_pbf = ETS_E3B0_PBF_MIN_W_VAL;
691 	u8 i = 0;
692 	u32 base_weight = 0;
693 	u8 max_cos = 0;
694 
695 	/* Mapping between entry  priority to client number 0 - COS0
696 	 * client, 2 - COS1, ... 5 - COS5)(HIGHEST) 4bits client num.
697 	 * TODO_ETS - Should be done by reset value or init tool
698 	 */
699 	if (port)
700 		/*  0x688 (|011|0 10|00 1|000) */
701 		REG_WR(bp, PBF_REG_ETS_ARB_PRIORITY_CLIENT_P1 , 0x688);
702 	else
703 		/*  (10 1|100 |011|0 10|00 1|000) */
704 		REG_WR(bp, PBF_REG_ETS_ARB_PRIORITY_CLIENT_P0 , 0x2C688);
705 
706 	/* TODO_ETS - Should be done by reset value or init tool */
707 	if (port)
708 		/* 0x688 (|011|0 10|00 1|000)*/
709 		REG_WR(bp, PBF_REG_ETS_ARB_CLIENT_CREDIT_MAP_P1, 0x688);
710 	else
711 	/* 0x2C688 (10 1|100 |011|0 10|00 1|000) */
712 	REG_WR(bp, PBF_REG_ETS_ARB_CLIENT_CREDIT_MAP_P0, 0x2C688);
713 
714 	REG_WR(bp, (port) ? PBF_REG_ETS_ARB_NUM_STRICT_ARB_SLOTS_P1 :
715 		   PBF_REG_ETS_ARB_NUM_STRICT_ARB_SLOTS_P0 , 0x100);
716 
717 
718 	REG_WR(bp, (port) ? PBF_REG_ETS_ARB_CLIENT_IS_STRICT_P1 :
719 		   PBF_REG_ETS_ARB_CLIENT_IS_STRICT_P0 , 0);
720 
721 	REG_WR(bp, (port) ? PBF_REG_ETS_ARB_CLIENT_IS_SUBJECT2WFQ_P1 :
722 		   PBF_REG_ETS_ARB_CLIENT_IS_SUBJECT2WFQ_P0 , 0);
723 	/* In 2 port mode port0 has COS0-5 that can be used for WFQ.
724 	 * In 4 port mode port1 has COS0-2 that can be used for WFQ.
725 	 */
726 	if (!port) {
727 		base_weight = PBF_REG_COS0_WEIGHT_P0;
728 		max_cos = DCBX_E3B0_MAX_NUM_COS_PORT0;
729 	} else {
730 		base_weight = PBF_REG_COS0_WEIGHT_P1;
731 		max_cos = DCBX_E3B0_MAX_NUM_COS_PORT1;
732 	}
733 
734 	for (i = 0; i < max_cos; i++)
735 		REG_WR(bp, base_weight + (0x4 * i), 0);
736 
737 	bnx2x_ets_e3b0_set_credit_upper_bound_pbf(params, min_w_val_pbf);
738 }
739 /******************************************************************************
740 * Description:
741 *	E3B0 disable will return basically the values to init values.
742 *.
743 ******************************************************************************/
744 static int bnx2x_ets_e3b0_disabled(const struct link_params *params,
745 				   const struct link_vars *vars)
746 {
747 	struct bnx2x *bp = params->bp;
748 
749 	if (!CHIP_IS_E3B0(bp)) {
750 		DP(NETIF_MSG_LINK,
751 		   "bnx2x_ets_e3b0_disabled the chip isn't E3B0\n");
752 		return -EINVAL;
753 	}
754 
755 	bnx2x_ets_e3b0_nig_disabled(params, vars);
756 
757 	bnx2x_ets_e3b0_pbf_disabled(params);
758 
759 	return 0;
760 }
761 
762 /******************************************************************************
763 * Description:
764 *	Disable will return basically the values to init values.
765 *
766 ******************************************************************************/
767 int bnx2x_ets_disabled(struct link_params *params,
768 		      struct link_vars *vars)
769 {
770 	struct bnx2x *bp = params->bp;
771 	int bnx2x_status = 0;
772 
773 	if ((CHIP_IS_E2(bp)) || (CHIP_IS_E3A0(bp)))
774 		bnx2x_ets_e2e3a0_disabled(params);
775 	else if (CHIP_IS_E3B0(bp))
776 		bnx2x_status = bnx2x_ets_e3b0_disabled(params, vars);
777 	else {
778 		DP(NETIF_MSG_LINK, "bnx2x_ets_disabled - chip not supported\n");
779 		return -EINVAL;
780 	}
781 
782 	return bnx2x_status;
783 }
784 
785 /******************************************************************************
786 * Description
787 *	Set the COS mappimg to SP and BW until this point all the COS are not
788 *	set as SP or BW.
789 ******************************************************************************/
790 static int bnx2x_ets_e3b0_cli_map(const struct link_params *params,
791 				  const struct bnx2x_ets_params *ets_params,
792 				  const u8 cos_sp_bitmap,
793 				  const u8 cos_bw_bitmap)
794 {
795 	struct bnx2x *bp = params->bp;
796 	const u8 port = params->port;
797 	const u8 nig_cli_sp_bitmap = 0x7 | (cos_sp_bitmap << 3);
798 	const u8 pbf_cli_sp_bitmap = cos_sp_bitmap;
799 	const u8 nig_cli_subject2wfq_bitmap = cos_bw_bitmap << 3;
800 	const u8 pbf_cli_subject2wfq_bitmap = cos_bw_bitmap;
801 
802 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CLIENT_IS_STRICT :
803 	       NIG_REG_P0_TX_ARB_CLIENT_IS_STRICT, nig_cli_sp_bitmap);
804 
805 	REG_WR(bp, (port) ? PBF_REG_ETS_ARB_CLIENT_IS_STRICT_P1 :
806 	       PBF_REG_ETS_ARB_CLIENT_IS_STRICT_P0 , pbf_cli_sp_bitmap);
807 
808 	REG_WR(bp, (port) ? NIG_REG_P1_TX_ARB_CLIENT_IS_SUBJECT2WFQ :
809 	       NIG_REG_P0_TX_ARB_CLIENT_IS_SUBJECT2WFQ,
810 	       nig_cli_subject2wfq_bitmap);
811 
812 	REG_WR(bp, (port) ? PBF_REG_ETS_ARB_CLIENT_IS_SUBJECT2WFQ_P1 :
813 	       PBF_REG_ETS_ARB_CLIENT_IS_SUBJECT2WFQ_P0,
814 	       pbf_cli_subject2wfq_bitmap);
815 
816 	return 0;
817 }
818 
819 /******************************************************************************
820 * Description:
821 *	This function is needed because NIG ARB_CREDIT_WEIGHT_X are
822 *	not continues and ARB_CREDIT_WEIGHT_0 + offset is suitable.
823 ******************************************************************************/
824 static int bnx2x_ets_e3b0_set_cos_bw(struct bnx2x *bp,
825 				     const u8 cos_entry,
826 				     const u32 min_w_val_nig,
827 				     const u32 min_w_val_pbf,
828 				     const u16 total_bw,
829 				     const u8 bw,
830 				     const u8 port)
831 {
832 	u32 nig_reg_adress_crd_weight = 0;
833 	u32 pbf_reg_adress_crd_weight = 0;
834 	/* Calculate and set BW for this COS - use 1 instead of 0 for BW */
835 	const u32 cos_bw_nig = ((bw ? bw : 1) * min_w_val_nig) / total_bw;
836 	const u32 cos_bw_pbf = ((bw ? bw : 1) * min_w_val_pbf) / total_bw;
837 
838 	switch (cos_entry) {
839 	case 0:
840 	    nig_reg_adress_crd_weight =
841 		 (port) ? NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_0 :
842 		     NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_0;
843 	     pbf_reg_adress_crd_weight = (port) ?
844 		 PBF_REG_COS0_WEIGHT_P1 : PBF_REG_COS0_WEIGHT_P0;
845 	     break;
846 	case 1:
847 	     nig_reg_adress_crd_weight = (port) ?
848 		 NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_1 :
849 		 NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_1;
850 	     pbf_reg_adress_crd_weight = (port) ?
851 		 PBF_REG_COS1_WEIGHT_P1 : PBF_REG_COS1_WEIGHT_P0;
852 	     break;
853 	case 2:
854 	     nig_reg_adress_crd_weight = (port) ?
855 		 NIG_REG_P1_TX_ARB_CREDIT_WEIGHT_2 :
856 		 NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_2;
857 
858 		 pbf_reg_adress_crd_weight = (port) ?
859 		     PBF_REG_COS2_WEIGHT_P1 : PBF_REG_COS2_WEIGHT_P0;
860 	     break;
861 	case 3:
862 	    if (port)
863 			return -EINVAL;
864 	     nig_reg_adress_crd_weight =
865 		 NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_3;
866 	     pbf_reg_adress_crd_weight =
867 		 PBF_REG_COS3_WEIGHT_P0;
868 	     break;
869 	case 4:
870 	    if (port)
871 		return -EINVAL;
872 	     nig_reg_adress_crd_weight =
873 		 NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_4;
874 	     pbf_reg_adress_crd_weight = PBF_REG_COS4_WEIGHT_P0;
875 	     break;
876 	case 5:
877 	    if (port)
878 		return -EINVAL;
879 	     nig_reg_adress_crd_weight =
880 		 NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_5;
881 	     pbf_reg_adress_crd_weight = PBF_REG_COS5_WEIGHT_P0;
882 	     break;
883 	}
884 
885 	REG_WR(bp, nig_reg_adress_crd_weight, cos_bw_nig);
886 
887 	REG_WR(bp, pbf_reg_adress_crd_weight, cos_bw_pbf);
888 
889 	return 0;
890 }
891 /******************************************************************************
892 * Description:
893 *	Calculate the total BW.A value of 0 isn't legal.
894 *
895 ******************************************************************************/
896 static int bnx2x_ets_e3b0_get_total_bw(
897 	const struct link_params *params,
898 	struct bnx2x_ets_params *ets_params,
899 	u16 *total_bw)
900 {
901 	struct bnx2x *bp = params->bp;
902 	u8 cos_idx = 0;
903 	u8 is_bw_cos_exist = 0;
904 
905 	*total_bw = 0 ;
906 	/* Calculate total BW requested */
907 	for (cos_idx = 0; cos_idx < ets_params->num_of_cos; cos_idx++) {
908 		if (ets_params->cos[cos_idx].state == bnx2x_cos_state_bw) {
909 			is_bw_cos_exist = 1;
910 			if (!ets_params->cos[cos_idx].params.bw_params.bw) {
911 				DP(NETIF_MSG_LINK, "bnx2x_ets_E3B0_config BW"
912 						   "was set to 0\n");
913 				/* This is to prevent a state when ramrods
914 				 * can't be sent
915 				 */
916 				ets_params->cos[cos_idx].params.bw_params.bw
917 					 = 1;
918 			}
919 			*total_bw +=
920 				ets_params->cos[cos_idx].params.bw_params.bw;
921 		}
922 	}
923 
924 	/* Check total BW is valid */
925 	if ((is_bw_cos_exist == 1) && (*total_bw != 100)) {
926 		if (*total_bw == 0) {
927 			DP(NETIF_MSG_LINK,
928 			   "bnx2x_ets_E3B0_config total BW shouldn't be 0\n");
929 			return -EINVAL;
930 		}
931 		DP(NETIF_MSG_LINK,
932 		   "bnx2x_ets_E3B0_config total BW should be 100\n");
933 		/* We can handle a case whre the BW isn't 100 this can happen
934 		 * if the TC are joined.
935 		 */
936 	}
937 	return 0;
938 }
939 
940 /******************************************************************************
941 * Description:
942 *	Invalidate all the sp_pri_to_cos.
943 *
944 ******************************************************************************/
945 static void bnx2x_ets_e3b0_sp_pri_to_cos_init(u8 *sp_pri_to_cos)
946 {
947 	u8 pri = 0;
948 	for (pri = 0; pri < DCBX_MAX_NUM_COS; pri++)
949 		sp_pri_to_cos[pri] = DCBX_INVALID_COS;
950 }
951 /******************************************************************************
952 * Description:
953 *	Calculate and set the SP (ARB_PRIORITY_CLIENT) NIG and PBF registers
954 *	according to sp_pri_to_cos.
955 *
956 ******************************************************************************/
957 static int bnx2x_ets_e3b0_sp_pri_to_cos_set(const struct link_params *params,
958 					    u8 *sp_pri_to_cos, const u8 pri,
959 					    const u8 cos_entry)
960 {
961 	struct bnx2x *bp = params->bp;
962 	const u8 port = params->port;
963 	const u8 max_num_of_cos = (port) ? DCBX_E3B0_MAX_NUM_COS_PORT1 :
964 		DCBX_E3B0_MAX_NUM_COS_PORT0;
965 
966 	if (pri >= max_num_of_cos) {
967 		DP(NETIF_MSG_LINK, "bnx2x_ets_e3b0_sp_pri_to_cos_set invalid "
968 		   "parameter Illegal strict priority\n");
969 	    return -EINVAL;
970 	}
971 
972 	if (sp_pri_to_cos[pri] != DCBX_INVALID_COS) {
973 		DP(NETIF_MSG_LINK, "bnx2x_ets_e3b0_sp_pri_to_cos_set invalid "
974 				   "parameter There can't be two COS's with "
975 				   "the same strict pri\n");
976 		return -EINVAL;
977 	}
978 
979 	sp_pri_to_cos[pri] = cos_entry;
980 	return 0;
981 
982 }
983 
984 /******************************************************************************
985 * Description:
986 *	Returns the correct value according to COS and priority in
987 *	the sp_pri_cli register.
988 *
989 ******************************************************************************/
990 static u64 bnx2x_e3b0_sp_get_pri_cli_reg(const u8 cos, const u8 cos_offset,
991 					 const u8 pri_set,
992 					 const u8 pri_offset,
993 					 const u8 entry_size)
994 {
995 	u64 pri_cli_nig = 0;
996 	pri_cli_nig = ((u64)(cos + cos_offset)) << (entry_size *
997 						    (pri_set + pri_offset));
998 
999 	return pri_cli_nig;
1000 }
1001 /******************************************************************************
1002 * Description:
1003 *	Returns the correct value according to COS and priority in the
1004 *	sp_pri_cli register for NIG.
1005 *
1006 ******************************************************************************/
1007 static u64 bnx2x_e3b0_sp_get_pri_cli_reg_nig(const u8 cos, const u8 pri_set)
1008 {
1009 	/* MCP Dbg0 and dbg1 are always with higher strict pri*/
1010 	const u8 nig_cos_offset = 3;
1011 	const u8 nig_pri_offset = 3;
1012 
1013 	return bnx2x_e3b0_sp_get_pri_cli_reg(cos, nig_cos_offset, pri_set,
1014 		nig_pri_offset, 4);
1015 
1016 }
1017 /******************************************************************************
1018 * Description:
1019 *	Returns the correct value according to COS and priority in the
1020 *	sp_pri_cli register for PBF.
1021 *
1022 ******************************************************************************/
1023 static u64 bnx2x_e3b0_sp_get_pri_cli_reg_pbf(const u8 cos, const u8 pri_set)
1024 {
1025 	const u8 pbf_cos_offset = 0;
1026 	const u8 pbf_pri_offset = 0;
1027 
1028 	return bnx2x_e3b0_sp_get_pri_cli_reg(cos, pbf_cos_offset, pri_set,
1029 		pbf_pri_offset, 3);
1030 
1031 }
1032 
1033 /******************************************************************************
1034 * Description:
1035 *	Calculate and set the SP (ARB_PRIORITY_CLIENT) NIG and PBF registers
1036 *	according to sp_pri_to_cos.(which COS has higher priority)
1037 *
1038 ******************************************************************************/
1039 static int bnx2x_ets_e3b0_sp_set_pri_cli_reg(const struct link_params *params,
1040 					     u8 *sp_pri_to_cos)
1041 {
1042 	struct bnx2x *bp = params->bp;
1043 	u8 i = 0;
1044 	const u8 port = params->port;
1045 	/* MCP Dbg0 and dbg1 are always with higher strict pri*/
1046 	u64 pri_cli_nig = 0x210;
1047 	u32 pri_cli_pbf = 0x0;
1048 	u8 pri_set = 0;
1049 	u8 pri_bitmask = 0;
1050 	const u8 max_num_of_cos = (port) ? DCBX_E3B0_MAX_NUM_COS_PORT1 :
1051 		DCBX_E3B0_MAX_NUM_COS_PORT0;
1052 
1053 	u8 cos_bit_to_set = (1 << max_num_of_cos) - 1;
1054 
1055 	/* Set all the strict priority first */
1056 	for (i = 0; i < max_num_of_cos; i++) {
1057 		if (sp_pri_to_cos[i] != DCBX_INVALID_COS) {
1058 			if (sp_pri_to_cos[i] >= DCBX_MAX_NUM_COS) {
1059 				DP(NETIF_MSG_LINK,
1060 					   "bnx2x_ets_e3b0_sp_set_pri_cli_reg "
1061 					   "invalid cos entry\n");
1062 				return -EINVAL;
1063 			}
1064 
1065 			pri_cli_nig |= bnx2x_e3b0_sp_get_pri_cli_reg_nig(
1066 			    sp_pri_to_cos[i], pri_set);
1067 
1068 			pri_cli_pbf |= bnx2x_e3b0_sp_get_pri_cli_reg_pbf(
1069 			    sp_pri_to_cos[i], pri_set);
1070 			pri_bitmask = 1 << sp_pri_to_cos[i];
1071 			/* COS is used remove it from bitmap.*/
1072 			if (!(pri_bitmask & cos_bit_to_set)) {
1073 				DP(NETIF_MSG_LINK,
1074 					"bnx2x_ets_e3b0_sp_set_pri_cli_reg "
1075 					"invalid There can't be two COS's with"
1076 					" the same strict pri\n");
1077 				return -EINVAL;
1078 			}
1079 			cos_bit_to_set &= ~pri_bitmask;
1080 			pri_set++;
1081 		}
1082 	}
1083 
1084 	/* Set all the Non strict priority i= COS*/
1085 	for (i = 0; i < max_num_of_cos; i++) {
1086 		pri_bitmask = 1 << i;
1087 		/* Check if COS was already used for SP */
1088 		if (pri_bitmask & cos_bit_to_set) {
1089 			/* COS wasn't used for SP */
1090 			pri_cli_nig |= bnx2x_e3b0_sp_get_pri_cli_reg_nig(
1091 			    i, pri_set);
1092 
1093 			pri_cli_pbf |= bnx2x_e3b0_sp_get_pri_cli_reg_pbf(
1094 			    i, pri_set);
1095 			/* COS is used remove it from bitmap.*/
1096 			cos_bit_to_set &= ~pri_bitmask;
1097 			pri_set++;
1098 		}
1099 	}
1100 
1101 	if (pri_set != max_num_of_cos) {
1102 		DP(NETIF_MSG_LINK, "bnx2x_ets_e3b0_sp_set_pri_cli_reg not all "
1103 				   "entries were set\n");
1104 		return -EINVAL;
1105 	}
1106 
1107 	if (port) {
1108 		/* Only 6 usable clients*/
1109 		REG_WR(bp, NIG_REG_P1_TX_ARB_PRIORITY_CLIENT2_LSB,
1110 		       (u32)pri_cli_nig);
1111 
1112 		REG_WR(bp, PBF_REG_ETS_ARB_PRIORITY_CLIENT_P1 , pri_cli_pbf);
1113 	} else {
1114 		/* Only 9 usable clients*/
1115 		const u32 pri_cli_nig_lsb = (u32) (pri_cli_nig);
1116 		const u32 pri_cli_nig_msb = (u32) ((pri_cli_nig >> 32) & 0xF);
1117 
1118 		REG_WR(bp, NIG_REG_P0_TX_ARB_PRIORITY_CLIENT2_LSB,
1119 		       pri_cli_nig_lsb);
1120 		REG_WR(bp, NIG_REG_P0_TX_ARB_PRIORITY_CLIENT2_MSB,
1121 		       pri_cli_nig_msb);
1122 
1123 		REG_WR(bp, PBF_REG_ETS_ARB_PRIORITY_CLIENT_P0 , pri_cli_pbf);
1124 	}
1125 	return 0;
1126 }
1127 
1128 /******************************************************************************
1129 * Description:
1130 *	Configure the COS to ETS according to BW and SP settings.
1131 ******************************************************************************/
1132 int bnx2x_ets_e3b0_config(const struct link_params *params,
1133 			 const struct link_vars *vars,
1134 			 struct bnx2x_ets_params *ets_params)
1135 {
1136 	struct bnx2x *bp = params->bp;
1137 	int bnx2x_status = 0;
1138 	const u8 port = params->port;
1139 	u16 total_bw = 0;
1140 	const u32 min_w_val_nig = bnx2x_ets_get_min_w_val_nig(vars);
1141 	const u32 min_w_val_pbf = ETS_E3B0_PBF_MIN_W_VAL;
1142 	u8 cos_bw_bitmap = 0;
1143 	u8 cos_sp_bitmap = 0;
1144 	u8 sp_pri_to_cos[DCBX_MAX_NUM_COS] = {0};
1145 	const u8 max_num_of_cos = (port) ? DCBX_E3B0_MAX_NUM_COS_PORT1 :
1146 		DCBX_E3B0_MAX_NUM_COS_PORT0;
1147 	u8 cos_entry = 0;
1148 
1149 	if (!CHIP_IS_E3B0(bp)) {
1150 		DP(NETIF_MSG_LINK,
1151 		   "bnx2x_ets_e3b0_disabled the chip isn't E3B0\n");
1152 		return -EINVAL;
1153 	}
1154 
1155 	if ((ets_params->num_of_cos > max_num_of_cos)) {
1156 		DP(NETIF_MSG_LINK, "bnx2x_ets_E3B0_config the number of COS "
1157 				   "isn't supported\n");
1158 		return -EINVAL;
1159 	}
1160 
1161 	/* Prepare sp strict priority parameters*/
1162 	bnx2x_ets_e3b0_sp_pri_to_cos_init(sp_pri_to_cos);
1163 
1164 	/* Prepare BW parameters*/
1165 	bnx2x_status = bnx2x_ets_e3b0_get_total_bw(params, ets_params,
1166 						   &total_bw);
1167 	if (bnx2x_status) {
1168 		DP(NETIF_MSG_LINK,
1169 		   "bnx2x_ets_E3B0_config get_total_bw failed\n");
1170 		return -EINVAL;
1171 	}
1172 
1173 	/* Upper bound is set according to current link speed (min_w_val
1174 	 * should be the same for upper bound and COS credit val).
1175 	 */
1176 	bnx2x_ets_e3b0_set_credit_upper_bound_nig(params, min_w_val_nig);
1177 	bnx2x_ets_e3b0_set_credit_upper_bound_pbf(params, min_w_val_pbf);
1178 
1179 
1180 	for (cos_entry = 0; cos_entry < ets_params->num_of_cos; cos_entry++) {
1181 		if (bnx2x_cos_state_bw == ets_params->cos[cos_entry].state) {
1182 			cos_bw_bitmap |= (1 << cos_entry);
1183 			/* The function also sets the BW in HW(not the mappin
1184 			 * yet)
1185 			 */
1186 			bnx2x_status = bnx2x_ets_e3b0_set_cos_bw(
1187 				bp, cos_entry, min_w_val_nig, min_w_val_pbf,
1188 				total_bw,
1189 				ets_params->cos[cos_entry].params.bw_params.bw,
1190 				 port);
1191 		} else if (bnx2x_cos_state_strict ==
1192 			ets_params->cos[cos_entry].state){
1193 			cos_sp_bitmap |= (1 << cos_entry);
1194 
1195 			bnx2x_status = bnx2x_ets_e3b0_sp_pri_to_cos_set(
1196 				params,
1197 				sp_pri_to_cos,
1198 				ets_params->cos[cos_entry].params.sp_params.pri,
1199 				cos_entry);
1200 
1201 		} else {
1202 			DP(NETIF_MSG_LINK,
1203 			   "bnx2x_ets_e3b0_config cos state not valid\n");
1204 			return -EINVAL;
1205 		}
1206 		if (bnx2x_status) {
1207 			DP(NETIF_MSG_LINK,
1208 			   "bnx2x_ets_e3b0_config set cos bw failed\n");
1209 			return bnx2x_status;
1210 		}
1211 	}
1212 
1213 	/* Set SP register (which COS has higher priority) */
1214 	bnx2x_status = bnx2x_ets_e3b0_sp_set_pri_cli_reg(params,
1215 							 sp_pri_to_cos);
1216 
1217 	if (bnx2x_status) {
1218 		DP(NETIF_MSG_LINK,
1219 		   "bnx2x_ets_E3B0_config set_pri_cli_reg failed\n");
1220 		return bnx2x_status;
1221 	}
1222 
1223 	/* Set client mapping of BW and strict */
1224 	bnx2x_status = bnx2x_ets_e3b0_cli_map(params, ets_params,
1225 					      cos_sp_bitmap,
1226 					      cos_bw_bitmap);
1227 
1228 	if (bnx2x_status) {
1229 		DP(NETIF_MSG_LINK, "bnx2x_ets_E3B0_config SP failed\n");
1230 		return bnx2x_status;
1231 	}
1232 	return 0;
1233 }
1234 static void bnx2x_ets_bw_limit_common(const struct link_params *params)
1235 {
1236 	/* ETS disabled configuration */
1237 	struct bnx2x *bp = params->bp;
1238 	DP(NETIF_MSG_LINK, "ETS enabled BW limit configuration\n");
1239 	/* Defines which entries (clients) are subjected to WFQ arbitration
1240 	 * COS0 0x8
1241 	 * COS1 0x10
1242 	 */
1243 	REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_IS_SUBJECT2WFQ, 0x18);
1244 	/* Mapping between the ARB_CREDIT_WEIGHT registers and actual
1245 	 * client numbers (WEIGHT_0 does not actually have to represent
1246 	 * client 0)
1247 	 *    PRI4    |    PRI3    |    PRI2    |    PRI1    |    PRI0
1248 	 *  cos1-001     cos0-000     dbg1-100     dbg0-011     MCP-010
1249 	 */
1250 	REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_CREDIT_MAP, 0x111A);
1251 
1252 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_0,
1253 	       ETS_BW_LIMIT_CREDIT_UPPER_BOUND);
1254 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_UPPER_BOUND_1,
1255 	       ETS_BW_LIMIT_CREDIT_UPPER_BOUND);
1256 
1257 	/* ETS mode enabled*/
1258 	REG_WR(bp, PBF_REG_ETS_ENABLED, 1);
1259 
1260 	/* Defines the number of consecutive slots for the strict priority */
1261 	REG_WR(bp, PBF_REG_NUM_STRICT_ARB_SLOTS, 0);
1262 	/* Bitmap of 5bits length. Each bit specifies whether the entry behaves
1263 	 * as strict.  Bits 0,1,2 - debug and management entries, 3 - COS0
1264 	 * entry, 4 - COS1 entry.
1265 	 * COS1 | COS0 | DEBUG21 | DEBUG0 | MGMT
1266 	 * bit4   bit3	  bit2     bit1	   bit0
1267 	 * MCP and debug are strict
1268 	 */
1269 	REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_IS_STRICT, 0x7);
1270 
1271 	/* Upper bound that COS0_WEIGHT can reach in the WFQ arbiter.*/
1272 	REG_WR(bp, PBF_REG_COS0_UPPER_BOUND,
1273 	       ETS_BW_LIMIT_CREDIT_UPPER_BOUND);
1274 	REG_WR(bp, PBF_REG_COS1_UPPER_BOUND,
1275 	       ETS_BW_LIMIT_CREDIT_UPPER_BOUND);
1276 }
1277 
1278 void bnx2x_ets_bw_limit(const struct link_params *params, const u32 cos0_bw,
1279 			const u32 cos1_bw)
1280 {
1281 	/* ETS disabled configuration*/
1282 	struct bnx2x *bp = params->bp;
1283 	const u32 total_bw = cos0_bw + cos1_bw;
1284 	u32 cos0_credit_weight = 0;
1285 	u32 cos1_credit_weight = 0;
1286 
1287 	DP(NETIF_MSG_LINK, "ETS enabled BW limit configuration\n");
1288 
1289 	if ((!total_bw) ||
1290 	    (!cos0_bw) ||
1291 	    (!cos1_bw)) {
1292 		DP(NETIF_MSG_LINK, "Total BW can't be zero\n");
1293 		return;
1294 	}
1295 
1296 	cos0_credit_weight = (cos0_bw * ETS_BW_LIMIT_CREDIT_WEIGHT)/
1297 		total_bw;
1298 	cos1_credit_weight = (cos1_bw * ETS_BW_LIMIT_CREDIT_WEIGHT)/
1299 		total_bw;
1300 
1301 	bnx2x_ets_bw_limit_common(params);
1302 
1303 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_0, cos0_credit_weight);
1304 	REG_WR(bp, NIG_REG_P0_TX_ARB_CREDIT_WEIGHT_1, cos1_credit_weight);
1305 
1306 	REG_WR(bp, PBF_REG_COS0_WEIGHT, cos0_credit_weight);
1307 	REG_WR(bp, PBF_REG_COS1_WEIGHT, cos1_credit_weight);
1308 }
1309 
1310 int bnx2x_ets_strict(const struct link_params *params, const u8 strict_cos)
1311 {
1312 	/* ETS disabled configuration*/
1313 	struct bnx2x *bp = params->bp;
1314 	u32 val	= 0;
1315 
1316 	DP(NETIF_MSG_LINK, "ETS enabled strict configuration\n");
1317 	/* Bitmap of 5bits length. Each bit specifies whether the entry behaves
1318 	 * as strict.  Bits 0,1,2 - debug and management entries,
1319 	 * 3 - COS0 entry, 4 - COS1 entry.
1320 	 *  COS1 | COS0 | DEBUG21 | DEBUG0 | MGMT
1321 	 *  bit4   bit3	  bit2      bit1     bit0
1322 	 * MCP and debug are strict
1323 	 */
1324 	REG_WR(bp, NIG_REG_P0_TX_ARB_CLIENT_IS_STRICT, 0x1F);
1325 	/* For strict priority entries defines the number of consecutive slots
1326 	 * for the highest priority.
1327 	 */
1328 	REG_WR(bp, NIG_REG_P0_TX_ARB_NUM_STRICT_ARB_SLOTS, 0x100);
1329 	/* ETS mode disable */
1330 	REG_WR(bp, PBF_REG_ETS_ENABLED, 0);
1331 	/* Defines the number of consecutive slots for the strict priority */
1332 	REG_WR(bp, PBF_REG_NUM_STRICT_ARB_SLOTS, 0x100);
1333 
1334 	/* Defines the number of consecutive slots for the strict priority */
1335 	REG_WR(bp, PBF_REG_HIGH_PRIORITY_COS_NUM, strict_cos);
1336 
1337 	/* Mapping between entry  priority to client number (0,1,2 -debug and
1338 	 * management clients, 3 - COS0 client, 4 - COS client)(HIGHEST)
1339 	 * 3bits client num.
1340 	 *   PRI4    |    PRI3    |    PRI2    |    PRI1    |    PRI0
1341 	 * dbg0-010     dbg1-001     cos1-100     cos0-011     MCP-000
1342 	 * dbg0-010     dbg1-001     cos0-011     cos1-100     MCP-000
1343 	 */
1344 	val = (!strict_cos) ? 0x2318 : 0x22E0;
1345 	REG_WR(bp, NIG_REG_P0_TX_ARB_PRIORITY_CLIENT, val);
1346 
1347 	return 0;
1348 }
1349 
1350 /******************************************************************/
1351 /*			PFC section				  */
1352 /******************************************************************/
1353 static void bnx2x_update_pfc_xmac(struct link_params *params,
1354 				  struct link_vars *vars,
1355 				  u8 is_lb)
1356 {
1357 	struct bnx2x *bp = params->bp;
1358 	u32 xmac_base;
1359 	u32 pause_val, pfc0_val, pfc1_val;
1360 
1361 	/* XMAC base adrr */
1362 	xmac_base = (params->port) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
1363 
1364 	/* Initialize pause and pfc registers */
1365 	pause_val = 0x18000;
1366 	pfc0_val = 0xFFFF8000;
1367 	pfc1_val = 0x2;
1368 
1369 	/* No PFC support */
1370 	if (!(params->feature_config_flags &
1371 	      FEATURE_CONFIG_PFC_ENABLED)) {
1372 
1373 		/* RX flow control - Process pause frame in receive direction
1374 		 */
1375 		if (vars->flow_ctrl & BNX2X_FLOW_CTRL_RX)
1376 			pause_val |= XMAC_PAUSE_CTRL_REG_RX_PAUSE_EN;
1377 
1378 		/* TX flow control - Send pause packet when buffer is full */
1379 		if (vars->flow_ctrl & BNX2X_FLOW_CTRL_TX)
1380 			pause_val |= XMAC_PAUSE_CTRL_REG_TX_PAUSE_EN;
1381 	} else {/* PFC support */
1382 		pfc1_val |= XMAC_PFC_CTRL_HI_REG_PFC_REFRESH_EN |
1383 			XMAC_PFC_CTRL_HI_REG_PFC_STATS_EN |
1384 			XMAC_PFC_CTRL_HI_REG_RX_PFC_EN |
1385 			XMAC_PFC_CTRL_HI_REG_TX_PFC_EN |
1386 			XMAC_PFC_CTRL_HI_REG_FORCE_PFC_XON;
1387 		/* Write pause and PFC registers */
1388 		REG_WR(bp, xmac_base + XMAC_REG_PAUSE_CTRL, pause_val);
1389 		REG_WR(bp, xmac_base + XMAC_REG_PFC_CTRL, pfc0_val);
1390 		REG_WR(bp, xmac_base + XMAC_REG_PFC_CTRL_HI, pfc1_val);
1391 		pfc1_val &= ~XMAC_PFC_CTRL_HI_REG_FORCE_PFC_XON;
1392 
1393 	}
1394 
1395 	/* Write pause and PFC registers */
1396 	REG_WR(bp, xmac_base + XMAC_REG_PAUSE_CTRL, pause_val);
1397 	REG_WR(bp, xmac_base + XMAC_REG_PFC_CTRL, pfc0_val);
1398 	REG_WR(bp, xmac_base + XMAC_REG_PFC_CTRL_HI, pfc1_val);
1399 
1400 
1401 	/* Set MAC address for source TX Pause/PFC frames */
1402 	REG_WR(bp, xmac_base + XMAC_REG_CTRL_SA_LO,
1403 	       ((params->mac_addr[2] << 24) |
1404 		(params->mac_addr[3] << 16) |
1405 		(params->mac_addr[4] << 8) |
1406 		(params->mac_addr[5])));
1407 	REG_WR(bp, xmac_base + XMAC_REG_CTRL_SA_HI,
1408 	       ((params->mac_addr[0] << 8) |
1409 		(params->mac_addr[1])));
1410 
1411 	udelay(30);
1412 }
1413 
1414 /******************************************************************/
1415 /*			MAC/PBF section				  */
1416 /******************************************************************/
1417 static void bnx2x_set_mdio_clk(struct bnx2x *bp, u32 chip_id,
1418 			       u32 emac_base)
1419 {
1420 	u32 new_mode, cur_mode;
1421 	u32 clc_cnt;
1422 	/* Set clause 45 mode, slow down the MDIO clock to 2.5MHz
1423 	 * (a value of 49==0x31) and make sure that the AUTO poll is off
1424 	 */
1425 	cur_mode = REG_RD(bp, emac_base + EMAC_REG_EMAC_MDIO_MODE);
1426 
1427 	if (USES_WARPCORE(bp))
1428 		clc_cnt = 74L << EMAC_MDIO_MODE_CLOCK_CNT_BITSHIFT;
1429 	else
1430 		clc_cnt = 49L << EMAC_MDIO_MODE_CLOCK_CNT_BITSHIFT;
1431 
1432 	if (((cur_mode & EMAC_MDIO_MODE_CLOCK_CNT) == clc_cnt) &&
1433 	    (cur_mode & (EMAC_MDIO_MODE_CLAUSE_45)))
1434 		return;
1435 
1436 	new_mode = cur_mode &
1437 		~(EMAC_MDIO_MODE_AUTO_POLL | EMAC_MDIO_MODE_CLOCK_CNT);
1438 	new_mode |= clc_cnt;
1439 	new_mode |= (EMAC_MDIO_MODE_CLAUSE_45);
1440 
1441 	DP(NETIF_MSG_LINK, "Changing emac_mode from 0x%x to 0x%x\n",
1442 	   cur_mode, new_mode);
1443 	REG_WR(bp, emac_base + EMAC_REG_EMAC_MDIO_MODE, new_mode);
1444 	udelay(40);
1445 }
1446 
1447 static void bnx2x_set_mdio_emac_per_phy(struct bnx2x *bp,
1448 					struct link_params *params)
1449 {
1450 	u8 phy_index;
1451 	/* Set mdio clock per phy */
1452 	for (phy_index = INT_PHY; phy_index < params->num_phys;
1453 	      phy_index++)
1454 		bnx2x_set_mdio_clk(bp, params->chip_id,
1455 				   params->phy[phy_index].mdio_ctrl);
1456 }
1457 
1458 static u8 bnx2x_is_4_port_mode(struct bnx2x *bp)
1459 {
1460 	u32 port4mode_ovwr_val;
1461 	/* Check 4-port override enabled */
1462 	port4mode_ovwr_val = REG_RD(bp, MISC_REG_PORT4MODE_EN_OVWR);
1463 	if (port4mode_ovwr_val & (1<<0)) {
1464 		/* Return 4-port mode override value */
1465 		return ((port4mode_ovwr_val & (1<<1)) == (1<<1));
1466 	}
1467 	/* Return 4-port mode from input pin */
1468 	return (u8)REG_RD(bp, MISC_REG_PORT4MODE_EN);
1469 }
1470 
1471 static void bnx2x_emac_init(struct link_params *params,
1472 			    struct link_vars *vars)
1473 {
1474 	/* reset and unreset the emac core */
1475 	struct bnx2x *bp = params->bp;
1476 	u8 port = params->port;
1477 	u32 emac_base = port ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
1478 	u32 val;
1479 	u16 timeout;
1480 
1481 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
1482 	       (MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE << port));
1483 	udelay(5);
1484 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
1485 	       (MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE << port));
1486 
1487 	/* init emac - use read-modify-write */
1488 	/* self clear reset */
1489 	val = REG_RD(bp, emac_base + EMAC_REG_EMAC_MODE);
1490 	EMAC_WR(bp, EMAC_REG_EMAC_MODE, (val | EMAC_MODE_RESET));
1491 
1492 	timeout = 200;
1493 	do {
1494 		val = REG_RD(bp, emac_base + EMAC_REG_EMAC_MODE);
1495 		DP(NETIF_MSG_LINK, "EMAC reset reg is %u\n", val);
1496 		if (!timeout) {
1497 			DP(NETIF_MSG_LINK, "EMAC timeout!\n");
1498 			return;
1499 		}
1500 		timeout--;
1501 	} while (val & EMAC_MODE_RESET);
1502 
1503 	bnx2x_set_mdio_emac_per_phy(bp, params);
1504 	/* Set mac address */
1505 	val = ((params->mac_addr[0] << 8) |
1506 		params->mac_addr[1]);
1507 	EMAC_WR(bp, EMAC_REG_EMAC_MAC_MATCH, val);
1508 
1509 	val = ((params->mac_addr[2] << 24) |
1510 	       (params->mac_addr[3] << 16) |
1511 	       (params->mac_addr[4] << 8) |
1512 		params->mac_addr[5]);
1513 	EMAC_WR(bp, EMAC_REG_EMAC_MAC_MATCH + 4, val);
1514 }
1515 
1516 static void bnx2x_set_xumac_nig(struct link_params *params,
1517 				u16 tx_pause_en,
1518 				u8 enable)
1519 {
1520 	struct bnx2x *bp = params->bp;
1521 
1522 	REG_WR(bp, params->port ? NIG_REG_P1_MAC_IN_EN : NIG_REG_P0_MAC_IN_EN,
1523 	       enable);
1524 	REG_WR(bp, params->port ? NIG_REG_P1_MAC_OUT_EN : NIG_REG_P0_MAC_OUT_EN,
1525 	       enable);
1526 	REG_WR(bp, params->port ? NIG_REG_P1_MAC_PAUSE_OUT_EN :
1527 	       NIG_REG_P0_MAC_PAUSE_OUT_EN, tx_pause_en);
1528 }
1529 
1530 static void bnx2x_set_umac_rxtx(struct link_params *params, u8 en)
1531 {
1532 	u32 umac_base = params->port ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
1533 	u32 val;
1534 	struct bnx2x *bp = params->bp;
1535 	if (!(REG_RD(bp, MISC_REG_RESET_REG_2) &
1536 		   (MISC_REGISTERS_RESET_REG_2_UMAC0 << params->port)))
1537 		return;
1538 	val = REG_RD(bp, umac_base + UMAC_REG_COMMAND_CONFIG);
1539 	if (en)
1540 		val |= (UMAC_COMMAND_CONFIG_REG_TX_ENA |
1541 			UMAC_COMMAND_CONFIG_REG_RX_ENA);
1542 	else
1543 		val &= ~(UMAC_COMMAND_CONFIG_REG_TX_ENA |
1544 			 UMAC_COMMAND_CONFIG_REG_RX_ENA);
1545 	/* Disable RX and TX */
1546 	REG_WR(bp, umac_base + UMAC_REG_COMMAND_CONFIG, val);
1547 }
1548 
1549 static void bnx2x_umac_enable(struct link_params *params,
1550 			    struct link_vars *vars, u8 lb)
1551 {
1552 	u32 val;
1553 	u32 umac_base = params->port ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
1554 	struct bnx2x *bp = params->bp;
1555 	/* Reset UMAC */
1556 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
1557 	       (MISC_REGISTERS_RESET_REG_2_UMAC0 << params->port));
1558 	usleep_range(1000, 2000);
1559 
1560 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
1561 	       (MISC_REGISTERS_RESET_REG_2_UMAC0 << params->port));
1562 
1563 	DP(NETIF_MSG_LINK, "enabling UMAC\n");
1564 
1565 	/* This register opens the gate for the UMAC despite its name */
1566 	REG_WR(bp, NIG_REG_EGRESS_EMAC0_PORT + params->port*4, 1);
1567 
1568 	val = UMAC_COMMAND_CONFIG_REG_PROMIS_EN |
1569 		UMAC_COMMAND_CONFIG_REG_PAD_EN |
1570 		UMAC_COMMAND_CONFIG_REG_SW_RESET |
1571 		UMAC_COMMAND_CONFIG_REG_NO_LGTH_CHECK;
1572 	switch (vars->line_speed) {
1573 	case SPEED_10:
1574 		val |= (0<<2);
1575 		break;
1576 	case SPEED_100:
1577 		val |= (1<<2);
1578 		break;
1579 	case SPEED_1000:
1580 		val |= (2<<2);
1581 		break;
1582 	case SPEED_2500:
1583 		val |= (3<<2);
1584 		break;
1585 	default:
1586 		DP(NETIF_MSG_LINK, "Invalid speed for UMAC %d\n",
1587 			       vars->line_speed);
1588 		break;
1589 	}
1590 	if (!(vars->flow_ctrl & BNX2X_FLOW_CTRL_TX))
1591 		val |= UMAC_COMMAND_CONFIG_REG_IGNORE_TX_PAUSE;
1592 
1593 	if (!(vars->flow_ctrl & BNX2X_FLOW_CTRL_RX))
1594 		val |= UMAC_COMMAND_CONFIG_REG_PAUSE_IGNORE;
1595 
1596 	if (vars->duplex == DUPLEX_HALF)
1597 		val |= UMAC_COMMAND_CONFIG_REG_HD_ENA;
1598 
1599 	REG_WR(bp, umac_base + UMAC_REG_COMMAND_CONFIG, val);
1600 	udelay(50);
1601 
1602 	/* Configure UMAC for EEE */
1603 	if (vars->eee_status & SHMEM_EEE_ADV_STATUS_MASK) {
1604 		DP(NETIF_MSG_LINK, "configured UMAC for EEE\n");
1605 		REG_WR(bp, umac_base + UMAC_REG_UMAC_EEE_CTRL,
1606 		       UMAC_UMAC_EEE_CTRL_REG_EEE_EN);
1607 		REG_WR(bp, umac_base + UMAC_REG_EEE_WAKE_TIMER, 0x11);
1608 	} else {
1609 		REG_WR(bp, umac_base + UMAC_REG_UMAC_EEE_CTRL, 0x0);
1610 	}
1611 
1612 	/* Set MAC address for source TX Pause/PFC frames (under SW reset) */
1613 	REG_WR(bp, umac_base + UMAC_REG_MAC_ADDR0,
1614 	       ((params->mac_addr[2] << 24) |
1615 		(params->mac_addr[3] << 16) |
1616 		(params->mac_addr[4] << 8) |
1617 		(params->mac_addr[5])));
1618 	REG_WR(bp, umac_base + UMAC_REG_MAC_ADDR1,
1619 	       ((params->mac_addr[0] << 8) |
1620 		(params->mac_addr[1])));
1621 
1622 	/* Enable RX and TX */
1623 	val &= ~UMAC_COMMAND_CONFIG_REG_PAD_EN;
1624 	val |= UMAC_COMMAND_CONFIG_REG_TX_ENA |
1625 		UMAC_COMMAND_CONFIG_REG_RX_ENA;
1626 	REG_WR(bp, umac_base + UMAC_REG_COMMAND_CONFIG, val);
1627 	udelay(50);
1628 
1629 	/* Remove SW Reset */
1630 	val &= ~UMAC_COMMAND_CONFIG_REG_SW_RESET;
1631 
1632 	/* Check loopback mode */
1633 	if (lb)
1634 		val |= UMAC_COMMAND_CONFIG_REG_LOOP_ENA;
1635 	REG_WR(bp, umac_base + UMAC_REG_COMMAND_CONFIG, val);
1636 
1637 	/* Maximum Frame Length (RW). Defines a 14-Bit maximum frame
1638 	 * length used by the MAC receive logic to check frames.
1639 	 */
1640 	REG_WR(bp, umac_base + UMAC_REG_MAXFR, 0x2710);
1641 	bnx2x_set_xumac_nig(params,
1642 			    ((vars->flow_ctrl & BNX2X_FLOW_CTRL_TX) != 0), 1);
1643 	vars->mac_type = MAC_TYPE_UMAC;
1644 
1645 }
1646 
1647 /* Define the XMAC mode */
1648 static void bnx2x_xmac_init(struct link_params *params, u32 max_speed)
1649 {
1650 	struct bnx2x *bp = params->bp;
1651 	u32 is_port4mode = bnx2x_is_4_port_mode(bp);
1652 
1653 	/* In 4-port mode, need to set the mode only once, so if XMAC is
1654 	 * already out of reset, it means the mode has already been set,
1655 	 * and it must not* reset the XMAC again, since it controls both
1656 	 * ports of the path
1657 	 */
1658 
1659 	if (((CHIP_NUM(bp) == CHIP_NUM_57840_4_10) ||
1660 	     (CHIP_NUM(bp) == CHIP_NUM_57840_2_20) ||
1661 	     (CHIP_NUM(bp) == CHIP_NUM_57840_OBSOLETE)) &&
1662 	    is_port4mode &&
1663 	    (REG_RD(bp, MISC_REG_RESET_REG_2) &
1664 	     MISC_REGISTERS_RESET_REG_2_XMAC)) {
1665 		DP(NETIF_MSG_LINK,
1666 		   "XMAC already out of reset in 4-port mode\n");
1667 		return;
1668 	}
1669 
1670 	/* Hard reset */
1671 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
1672 	       MISC_REGISTERS_RESET_REG_2_XMAC);
1673 	usleep_range(1000, 2000);
1674 
1675 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
1676 	       MISC_REGISTERS_RESET_REG_2_XMAC);
1677 	if (is_port4mode) {
1678 		DP(NETIF_MSG_LINK, "Init XMAC to 2 ports x 10G per path\n");
1679 
1680 		/* Set the number of ports on the system side to up to 2 */
1681 		REG_WR(bp, MISC_REG_XMAC_CORE_PORT_MODE, 1);
1682 
1683 		/* Set the number of ports on the Warp Core to 10G */
1684 		REG_WR(bp, MISC_REG_XMAC_PHY_PORT_MODE, 3);
1685 	} else {
1686 		/* Set the number of ports on the system side to 1 */
1687 		REG_WR(bp, MISC_REG_XMAC_CORE_PORT_MODE, 0);
1688 		if (max_speed == SPEED_10000) {
1689 			DP(NETIF_MSG_LINK,
1690 			   "Init XMAC to 10G x 1 port per path\n");
1691 			/* Set the number of ports on the Warp Core to 10G */
1692 			REG_WR(bp, MISC_REG_XMAC_PHY_PORT_MODE, 3);
1693 		} else {
1694 			DP(NETIF_MSG_LINK,
1695 			   "Init XMAC to 20G x 2 ports per path\n");
1696 			/* Set the number of ports on the Warp Core to 20G */
1697 			REG_WR(bp, MISC_REG_XMAC_PHY_PORT_MODE, 1);
1698 		}
1699 	}
1700 	/* Soft reset */
1701 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
1702 	       MISC_REGISTERS_RESET_REG_2_XMAC_SOFT);
1703 	usleep_range(1000, 2000);
1704 
1705 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
1706 	       MISC_REGISTERS_RESET_REG_2_XMAC_SOFT);
1707 
1708 }
1709 
1710 static void bnx2x_set_xmac_rxtx(struct link_params *params, u8 en)
1711 {
1712 	u8 port = params->port;
1713 	struct bnx2x *bp = params->bp;
1714 	u32 pfc_ctrl, xmac_base = (port) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
1715 	u32 val;
1716 
1717 	if (REG_RD(bp, MISC_REG_RESET_REG_2) &
1718 	    MISC_REGISTERS_RESET_REG_2_XMAC) {
1719 		/* Send an indication to change the state in the NIG back to XON
1720 		 * Clearing this bit enables the next set of this bit to get
1721 		 * rising edge
1722 		 */
1723 		pfc_ctrl = REG_RD(bp, xmac_base + XMAC_REG_PFC_CTRL_HI);
1724 		REG_WR(bp, xmac_base + XMAC_REG_PFC_CTRL_HI,
1725 		       (pfc_ctrl & ~(1<<1)));
1726 		REG_WR(bp, xmac_base + XMAC_REG_PFC_CTRL_HI,
1727 		       (pfc_ctrl | (1<<1)));
1728 		DP(NETIF_MSG_LINK, "Disable XMAC on port %x\n", port);
1729 		val = REG_RD(bp, xmac_base + XMAC_REG_CTRL);
1730 		if (en)
1731 			val |= (XMAC_CTRL_REG_TX_EN | XMAC_CTRL_REG_RX_EN);
1732 		else
1733 			val &= ~(XMAC_CTRL_REG_TX_EN | XMAC_CTRL_REG_RX_EN);
1734 		REG_WR(bp, xmac_base + XMAC_REG_CTRL, val);
1735 	}
1736 }
1737 
1738 static int bnx2x_xmac_enable(struct link_params *params,
1739 			     struct link_vars *vars, u8 lb)
1740 {
1741 	u32 val, xmac_base;
1742 	struct bnx2x *bp = params->bp;
1743 	DP(NETIF_MSG_LINK, "enabling XMAC\n");
1744 
1745 	xmac_base = (params->port) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
1746 
1747 	bnx2x_xmac_init(params, vars->line_speed);
1748 
1749 	/* This register determines on which events the MAC will assert
1750 	 * error on the i/f to the NIG along w/ EOP.
1751 	 */
1752 
1753 	/* This register tells the NIG whether to send traffic to UMAC
1754 	 * or XMAC
1755 	 */
1756 	REG_WR(bp, NIG_REG_EGRESS_EMAC0_PORT + params->port*4, 0);
1757 
1758 	/* When XMAC is in XLGMII mode, disable sending idles for fault
1759 	 * detection.
1760 	 */
1761 	if (!(params->phy[INT_PHY].flags & FLAGS_TX_ERROR_CHECK)) {
1762 		REG_WR(bp, xmac_base + XMAC_REG_RX_LSS_CTRL,
1763 		       (XMAC_RX_LSS_CTRL_REG_LOCAL_FAULT_DISABLE |
1764 			XMAC_RX_LSS_CTRL_REG_REMOTE_FAULT_DISABLE));
1765 		REG_WR(bp, xmac_base + XMAC_REG_CLEAR_RX_LSS_STATUS, 0);
1766 		REG_WR(bp, xmac_base + XMAC_REG_CLEAR_RX_LSS_STATUS,
1767 		       XMAC_CLEAR_RX_LSS_STATUS_REG_CLEAR_LOCAL_FAULT_STATUS |
1768 		       XMAC_CLEAR_RX_LSS_STATUS_REG_CLEAR_REMOTE_FAULT_STATUS);
1769 	}
1770 	/* Set Max packet size */
1771 	REG_WR(bp, xmac_base + XMAC_REG_RX_MAX_SIZE, 0x2710);
1772 
1773 	/* CRC append for Tx packets */
1774 	REG_WR(bp, xmac_base + XMAC_REG_TX_CTRL, 0xC800);
1775 
1776 	/* update PFC */
1777 	bnx2x_update_pfc_xmac(params, vars, 0);
1778 
1779 	if (vars->eee_status & SHMEM_EEE_ADV_STATUS_MASK) {
1780 		DP(NETIF_MSG_LINK, "Setting XMAC for EEE\n");
1781 		REG_WR(bp, xmac_base + XMAC_REG_EEE_TIMERS_HI, 0x1380008);
1782 		REG_WR(bp, xmac_base + XMAC_REG_EEE_CTRL, 0x1);
1783 	} else {
1784 		REG_WR(bp, xmac_base + XMAC_REG_EEE_CTRL, 0x0);
1785 	}
1786 
1787 	/* Enable TX and RX */
1788 	val = XMAC_CTRL_REG_TX_EN | XMAC_CTRL_REG_RX_EN;
1789 
1790 	/* Set MAC in XLGMII mode for dual-mode */
1791 	if ((vars->line_speed == SPEED_20000) &&
1792 	    (params->phy[INT_PHY].supported &
1793 	     SUPPORTED_20000baseKR2_Full))
1794 		val |= XMAC_CTRL_REG_XLGMII_ALIGN_ENB;
1795 
1796 	/* Check loopback mode */
1797 	if (lb)
1798 		val |= XMAC_CTRL_REG_LINE_LOCAL_LPBK;
1799 	REG_WR(bp, xmac_base + XMAC_REG_CTRL, val);
1800 	bnx2x_set_xumac_nig(params,
1801 			    ((vars->flow_ctrl & BNX2X_FLOW_CTRL_TX) != 0), 1);
1802 
1803 	vars->mac_type = MAC_TYPE_XMAC;
1804 
1805 	return 0;
1806 }
1807 
1808 static int bnx2x_emac_enable(struct link_params *params,
1809 			     struct link_vars *vars, u8 lb)
1810 {
1811 	struct bnx2x *bp = params->bp;
1812 	u8 port = params->port;
1813 	u32 emac_base = port ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
1814 	u32 val;
1815 
1816 	DP(NETIF_MSG_LINK, "enabling EMAC\n");
1817 
1818 	/* Disable BMAC */
1819 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
1820 	       (MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port));
1821 
1822 	/* enable emac and not bmac */
1823 	REG_WR(bp, NIG_REG_EGRESS_EMAC0_PORT + port*4, 1);
1824 
1825 	/* ASIC */
1826 	if (vars->phy_flags & PHY_XGXS_FLAG) {
1827 		u32 ser_lane = ((params->lane_config &
1828 				 PORT_HW_CFG_LANE_SWAP_CFG_MASTER_MASK) >>
1829 				PORT_HW_CFG_LANE_SWAP_CFG_MASTER_SHIFT);
1830 
1831 		DP(NETIF_MSG_LINK, "XGXS\n");
1832 		/* select the master lanes (out of 0-3) */
1833 		REG_WR(bp, NIG_REG_XGXS_LANE_SEL_P0 + port*4, ser_lane);
1834 		/* select XGXS */
1835 		REG_WR(bp, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
1836 
1837 	} else { /* SerDes */
1838 		DP(NETIF_MSG_LINK, "SerDes\n");
1839 		/* select SerDes */
1840 		REG_WR(bp, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 0);
1841 	}
1842 
1843 	bnx2x_bits_en(bp, emac_base + EMAC_REG_EMAC_RX_MODE,
1844 		      EMAC_RX_MODE_RESET);
1845 	bnx2x_bits_en(bp, emac_base + EMAC_REG_EMAC_TX_MODE,
1846 		      EMAC_TX_MODE_RESET);
1847 
1848 		/* pause enable/disable */
1849 		bnx2x_bits_dis(bp, emac_base + EMAC_REG_EMAC_RX_MODE,
1850 			       EMAC_RX_MODE_FLOW_EN);
1851 
1852 		bnx2x_bits_dis(bp,  emac_base + EMAC_REG_EMAC_TX_MODE,
1853 			       (EMAC_TX_MODE_EXT_PAUSE_EN |
1854 				EMAC_TX_MODE_FLOW_EN));
1855 		if (!(params->feature_config_flags &
1856 		      FEATURE_CONFIG_PFC_ENABLED)) {
1857 			if (vars->flow_ctrl & BNX2X_FLOW_CTRL_RX)
1858 				bnx2x_bits_en(bp, emac_base +
1859 					      EMAC_REG_EMAC_RX_MODE,
1860 					      EMAC_RX_MODE_FLOW_EN);
1861 
1862 			if (vars->flow_ctrl & BNX2X_FLOW_CTRL_TX)
1863 				bnx2x_bits_en(bp, emac_base +
1864 					      EMAC_REG_EMAC_TX_MODE,
1865 					      (EMAC_TX_MODE_EXT_PAUSE_EN |
1866 					       EMAC_TX_MODE_FLOW_EN));
1867 		} else
1868 			bnx2x_bits_en(bp, emac_base + EMAC_REG_EMAC_TX_MODE,
1869 				      EMAC_TX_MODE_FLOW_EN);
1870 
1871 	/* KEEP_VLAN_TAG, promiscuous */
1872 	val = REG_RD(bp, emac_base + EMAC_REG_EMAC_RX_MODE);
1873 	val |= EMAC_RX_MODE_KEEP_VLAN_TAG | EMAC_RX_MODE_PROMISCUOUS;
1874 
1875 	/* Setting this bit causes MAC control frames (except for pause
1876 	 * frames) to be passed on for processing. This setting has no
1877 	 * affect on the operation of the pause frames. This bit effects
1878 	 * all packets regardless of RX Parser packet sorting logic.
1879 	 * Turn the PFC off to make sure we are in Xon state before
1880 	 * enabling it.
1881 	 */
1882 	EMAC_WR(bp, EMAC_REG_RX_PFC_MODE, 0);
1883 	if (params->feature_config_flags & FEATURE_CONFIG_PFC_ENABLED) {
1884 		DP(NETIF_MSG_LINK, "PFC is enabled\n");
1885 		/* Enable PFC again */
1886 		EMAC_WR(bp, EMAC_REG_RX_PFC_MODE,
1887 			EMAC_REG_RX_PFC_MODE_RX_EN |
1888 			EMAC_REG_RX_PFC_MODE_TX_EN |
1889 			EMAC_REG_RX_PFC_MODE_PRIORITIES);
1890 
1891 		EMAC_WR(bp, EMAC_REG_RX_PFC_PARAM,
1892 			((0x0101 <<
1893 			  EMAC_REG_RX_PFC_PARAM_OPCODE_BITSHIFT) |
1894 			 (0x00ff <<
1895 			  EMAC_REG_RX_PFC_PARAM_PRIORITY_EN_BITSHIFT)));
1896 		val |= EMAC_RX_MODE_KEEP_MAC_CONTROL;
1897 	}
1898 	EMAC_WR(bp, EMAC_REG_EMAC_RX_MODE, val);
1899 
1900 	/* Set Loopback */
1901 	val = REG_RD(bp, emac_base + EMAC_REG_EMAC_MODE);
1902 	if (lb)
1903 		val |= 0x810;
1904 	else
1905 		val &= ~0x810;
1906 	EMAC_WR(bp, EMAC_REG_EMAC_MODE, val);
1907 
1908 	/* Enable emac */
1909 	REG_WR(bp, NIG_REG_NIG_EMAC0_EN + port*4, 1);
1910 
1911 	/* Enable emac for jumbo packets */
1912 	EMAC_WR(bp, EMAC_REG_EMAC_RX_MTU_SIZE,
1913 		(EMAC_RX_MTU_SIZE_JUMBO_ENA |
1914 		 (ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVERHEAD)));
1915 
1916 	/* Strip CRC */
1917 	REG_WR(bp, NIG_REG_NIG_INGRESS_EMAC0_NO_CRC + port*4, 0x1);
1918 
1919 	/* Disable the NIG in/out to the bmac */
1920 	REG_WR(bp, NIG_REG_BMAC0_IN_EN + port*4, 0x0);
1921 	REG_WR(bp, NIG_REG_BMAC0_PAUSE_OUT_EN + port*4, 0x0);
1922 	REG_WR(bp, NIG_REG_BMAC0_OUT_EN + port*4, 0x0);
1923 
1924 	/* Enable the NIG in/out to the emac */
1925 	REG_WR(bp, NIG_REG_EMAC0_IN_EN + port*4, 0x1);
1926 	val = 0;
1927 	if ((params->feature_config_flags &
1928 	      FEATURE_CONFIG_PFC_ENABLED) ||
1929 	    (vars->flow_ctrl & BNX2X_FLOW_CTRL_TX))
1930 		val = 1;
1931 
1932 	REG_WR(bp, NIG_REG_EMAC0_PAUSE_OUT_EN + port*4, val);
1933 	REG_WR(bp, NIG_REG_EGRESS_EMAC0_OUT_EN + port*4, 0x1);
1934 
1935 	REG_WR(bp, NIG_REG_BMAC0_REGS_OUT_EN + port*4, 0x0);
1936 
1937 	vars->mac_type = MAC_TYPE_EMAC;
1938 	return 0;
1939 }
1940 
1941 static void bnx2x_update_pfc_bmac1(struct link_params *params,
1942 				   struct link_vars *vars)
1943 {
1944 	u32 wb_data[2];
1945 	struct bnx2x *bp = params->bp;
1946 	u32 bmac_addr =  params->port ? NIG_REG_INGRESS_BMAC1_MEM :
1947 		NIG_REG_INGRESS_BMAC0_MEM;
1948 
1949 	u32 val = 0x14;
1950 	if ((!(params->feature_config_flags &
1951 	      FEATURE_CONFIG_PFC_ENABLED)) &&
1952 		(vars->flow_ctrl & BNX2X_FLOW_CTRL_RX))
1953 		/* Enable BigMAC to react on received Pause packets */
1954 		val |= (1<<5);
1955 	wb_data[0] = val;
1956 	wb_data[1] = 0;
1957 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_RX_CONTROL, wb_data, 2);
1958 
1959 	/* TX control */
1960 	val = 0xc0;
1961 	if (!(params->feature_config_flags &
1962 	      FEATURE_CONFIG_PFC_ENABLED) &&
1963 		(vars->flow_ctrl & BNX2X_FLOW_CTRL_TX))
1964 		val |= 0x800000;
1965 	wb_data[0] = val;
1966 	wb_data[1] = 0;
1967 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_TX_CONTROL, wb_data, 2);
1968 }
1969 
1970 static void bnx2x_update_pfc_bmac2(struct link_params *params,
1971 				   struct link_vars *vars,
1972 				   u8 is_lb)
1973 {
1974 	/* Set rx control: Strip CRC and enable BigMAC to relay
1975 	 * control packets to the system as well
1976 	 */
1977 	u32 wb_data[2];
1978 	struct bnx2x *bp = params->bp;
1979 	u32 bmac_addr = params->port ? NIG_REG_INGRESS_BMAC1_MEM :
1980 		NIG_REG_INGRESS_BMAC0_MEM;
1981 	u32 val = 0x14;
1982 
1983 	if ((!(params->feature_config_flags &
1984 	      FEATURE_CONFIG_PFC_ENABLED)) &&
1985 		(vars->flow_ctrl & BNX2X_FLOW_CTRL_RX))
1986 		/* Enable BigMAC to react on received Pause packets */
1987 		val |= (1<<5);
1988 	wb_data[0] = val;
1989 	wb_data[1] = 0;
1990 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_RX_CONTROL, wb_data, 2);
1991 	udelay(30);
1992 
1993 	/* Tx control */
1994 	val = 0xc0;
1995 	if (!(params->feature_config_flags &
1996 				FEATURE_CONFIG_PFC_ENABLED) &&
1997 	    (vars->flow_ctrl & BNX2X_FLOW_CTRL_TX))
1998 		val |= 0x800000;
1999 	wb_data[0] = val;
2000 	wb_data[1] = 0;
2001 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_TX_CONTROL, wb_data, 2);
2002 
2003 	if (params->feature_config_flags & FEATURE_CONFIG_PFC_ENABLED) {
2004 		DP(NETIF_MSG_LINK, "PFC is enabled\n");
2005 		/* Enable PFC RX & TX & STATS and set 8 COS  */
2006 		wb_data[0] = 0x0;
2007 		wb_data[0] |= (1<<0);  /* RX */
2008 		wb_data[0] |= (1<<1);  /* TX */
2009 		wb_data[0] |= (1<<2);  /* Force initial Xon */
2010 		wb_data[0] |= (1<<3);  /* 8 cos */
2011 		wb_data[0] |= (1<<5);  /* STATS */
2012 		wb_data[1] = 0;
2013 		REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_PFC_CONTROL,
2014 			    wb_data, 2);
2015 		/* Clear the force Xon */
2016 		wb_data[0] &= ~(1<<2);
2017 	} else {
2018 		DP(NETIF_MSG_LINK, "PFC is disabled\n");
2019 		/* Disable PFC RX & TX & STATS and set 8 COS */
2020 		wb_data[0] = 0x8;
2021 		wb_data[1] = 0;
2022 	}
2023 
2024 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_PFC_CONTROL, wb_data, 2);
2025 
2026 	/* Set Time (based unit is 512 bit time) between automatic
2027 	 * re-sending of PP packets amd enable automatic re-send of
2028 	 * Per-Priroity Packet as long as pp_gen is asserted and
2029 	 * pp_disable is low.
2030 	 */
2031 	val = 0x8000;
2032 	if (params->feature_config_flags & FEATURE_CONFIG_PFC_ENABLED)
2033 		val |= (1<<16); /* enable automatic re-send */
2034 
2035 	wb_data[0] = val;
2036 	wb_data[1] = 0;
2037 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_TX_PAUSE_CONTROL,
2038 		    wb_data, 2);
2039 
2040 	/* mac control */
2041 	val = 0x3; /* Enable RX and TX */
2042 	if (is_lb) {
2043 		val |= 0x4; /* Local loopback */
2044 		DP(NETIF_MSG_LINK, "enable bmac loopback\n");
2045 	}
2046 	/* When PFC enabled, Pass pause frames towards the NIG. */
2047 	if (params->feature_config_flags & FEATURE_CONFIG_PFC_ENABLED)
2048 		val |= ((1<<6)|(1<<5));
2049 
2050 	wb_data[0] = val;
2051 	wb_data[1] = 0;
2052 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_BMAC_CONTROL, wb_data, 2);
2053 }
2054 
2055 /******************************************************************************
2056 * Description:
2057 *  This function is needed because NIG ARB_CREDIT_WEIGHT_X are
2058 *  not continues and ARB_CREDIT_WEIGHT_0 + offset is suitable.
2059 ******************************************************************************/
2060 static int bnx2x_pfc_nig_rx_priority_mask(struct bnx2x *bp,
2061 					   u8 cos_entry,
2062 					   u32 priority_mask, u8 port)
2063 {
2064 	u32 nig_reg_rx_priority_mask_add = 0;
2065 
2066 	switch (cos_entry) {
2067 	case 0:
2068 	     nig_reg_rx_priority_mask_add = (port) ?
2069 		 NIG_REG_P1_RX_COS0_PRIORITY_MASK :
2070 		 NIG_REG_P0_RX_COS0_PRIORITY_MASK;
2071 	     break;
2072 	case 1:
2073 	    nig_reg_rx_priority_mask_add = (port) ?
2074 		NIG_REG_P1_RX_COS1_PRIORITY_MASK :
2075 		NIG_REG_P0_RX_COS1_PRIORITY_MASK;
2076 	    break;
2077 	case 2:
2078 	    nig_reg_rx_priority_mask_add = (port) ?
2079 		NIG_REG_P1_RX_COS2_PRIORITY_MASK :
2080 		NIG_REG_P0_RX_COS2_PRIORITY_MASK;
2081 	    break;
2082 	case 3:
2083 	    if (port)
2084 		return -EINVAL;
2085 	    nig_reg_rx_priority_mask_add = NIG_REG_P0_RX_COS3_PRIORITY_MASK;
2086 	    break;
2087 	case 4:
2088 	    if (port)
2089 		return -EINVAL;
2090 	    nig_reg_rx_priority_mask_add = NIG_REG_P0_RX_COS4_PRIORITY_MASK;
2091 	    break;
2092 	case 5:
2093 	    if (port)
2094 		return -EINVAL;
2095 	    nig_reg_rx_priority_mask_add = NIG_REG_P0_RX_COS5_PRIORITY_MASK;
2096 	    break;
2097 	}
2098 
2099 	REG_WR(bp, nig_reg_rx_priority_mask_add, priority_mask);
2100 
2101 	return 0;
2102 }
2103 static void bnx2x_update_mng(struct link_params *params, u32 link_status)
2104 {
2105 	struct bnx2x *bp = params->bp;
2106 
2107 	REG_WR(bp, params->shmem_base +
2108 	       offsetof(struct shmem_region,
2109 			port_mb[params->port].link_status), link_status);
2110 }
2111 
2112 static void bnx2x_update_link_attr(struct link_params *params, u32 link_attr)
2113 {
2114 	struct bnx2x *bp = params->bp;
2115 
2116 	if (SHMEM2_HAS(bp, link_attr_sync))
2117 		REG_WR(bp, params->shmem2_base +
2118 		       offsetof(struct shmem2_region,
2119 				link_attr_sync[params->port]), link_attr);
2120 }
2121 
2122 static void bnx2x_update_pfc_nig(struct link_params *params,
2123 		struct link_vars *vars,
2124 		struct bnx2x_nig_brb_pfc_port_params *nig_params)
2125 {
2126 	u32 xcm_mask = 0, ppp_enable = 0, pause_enable = 0, llfc_out_en = 0;
2127 	u32 llfc_enable = 0, xcm_out_en = 0, hwpfc_enable = 0;
2128 	u32 pkt_priority_to_cos = 0;
2129 	struct bnx2x *bp = params->bp;
2130 	u8 port = params->port;
2131 
2132 	int set_pfc = params->feature_config_flags &
2133 		FEATURE_CONFIG_PFC_ENABLED;
2134 	DP(NETIF_MSG_LINK, "updating pfc nig parameters\n");
2135 
2136 	/* When NIG_LLH0_XCM_MASK_REG_LLHX_XCM_MASK_BCN bit is set
2137 	 * MAC control frames (that are not pause packets)
2138 	 * will be forwarded to the XCM.
2139 	 */
2140 	xcm_mask = REG_RD(bp, port ? NIG_REG_LLH1_XCM_MASK :
2141 			  NIG_REG_LLH0_XCM_MASK);
2142 	/* NIG params will override non PFC params, since it's possible to
2143 	 * do transition from PFC to SAFC
2144 	 */
2145 	if (set_pfc) {
2146 		pause_enable = 0;
2147 		llfc_out_en = 0;
2148 		llfc_enable = 0;
2149 		if (CHIP_IS_E3(bp))
2150 			ppp_enable = 0;
2151 		else
2152 			ppp_enable = 1;
2153 		xcm_mask &= ~(port ? NIG_LLH1_XCM_MASK_REG_LLH1_XCM_MASK_BCN :
2154 				     NIG_LLH0_XCM_MASK_REG_LLH0_XCM_MASK_BCN);
2155 		xcm_out_en = 0;
2156 		hwpfc_enable = 1;
2157 	} else  {
2158 		if (nig_params) {
2159 			llfc_out_en = nig_params->llfc_out_en;
2160 			llfc_enable = nig_params->llfc_enable;
2161 			pause_enable = nig_params->pause_enable;
2162 		} else  /* Default non PFC mode - PAUSE */
2163 			pause_enable = 1;
2164 
2165 		xcm_mask |= (port ? NIG_LLH1_XCM_MASK_REG_LLH1_XCM_MASK_BCN :
2166 			NIG_LLH0_XCM_MASK_REG_LLH0_XCM_MASK_BCN);
2167 		xcm_out_en = 1;
2168 	}
2169 
2170 	if (CHIP_IS_E3(bp))
2171 		REG_WR(bp, port ? NIG_REG_BRB1_PAUSE_IN_EN :
2172 		       NIG_REG_BRB0_PAUSE_IN_EN, pause_enable);
2173 	REG_WR(bp, port ? NIG_REG_LLFC_OUT_EN_1 :
2174 	       NIG_REG_LLFC_OUT_EN_0, llfc_out_en);
2175 	REG_WR(bp, port ? NIG_REG_LLFC_ENABLE_1 :
2176 	       NIG_REG_LLFC_ENABLE_0, llfc_enable);
2177 	REG_WR(bp, port ? NIG_REG_PAUSE_ENABLE_1 :
2178 	       NIG_REG_PAUSE_ENABLE_0, pause_enable);
2179 
2180 	REG_WR(bp, port ? NIG_REG_PPP_ENABLE_1 :
2181 	       NIG_REG_PPP_ENABLE_0, ppp_enable);
2182 
2183 	REG_WR(bp, port ? NIG_REG_LLH1_XCM_MASK :
2184 	       NIG_REG_LLH0_XCM_MASK, xcm_mask);
2185 
2186 	REG_WR(bp, port ? NIG_REG_LLFC_EGRESS_SRC_ENABLE_1 :
2187 	       NIG_REG_LLFC_EGRESS_SRC_ENABLE_0, 0x7);
2188 
2189 	/* Output enable for RX_XCM # IF */
2190 	REG_WR(bp, port ? NIG_REG_XCM1_OUT_EN :
2191 	       NIG_REG_XCM0_OUT_EN, xcm_out_en);
2192 
2193 	/* HW PFC TX enable */
2194 	REG_WR(bp, port ? NIG_REG_P1_HWPFC_ENABLE :
2195 	       NIG_REG_P0_HWPFC_ENABLE, hwpfc_enable);
2196 
2197 	if (nig_params) {
2198 		u8 i = 0;
2199 		pkt_priority_to_cos = nig_params->pkt_priority_to_cos;
2200 
2201 		for (i = 0; i < nig_params->num_of_rx_cos_priority_mask; i++)
2202 			bnx2x_pfc_nig_rx_priority_mask(bp, i,
2203 		nig_params->rx_cos_priority_mask[i], port);
2204 
2205 		REG_WR(bp, port ? NIG_REG_LLFC_HIGH_PRIORITY_CLASSES_1 :
2206 		       NIG_REG_LLFC_HIGH_PRIORITY_CLASSES_0,
2207 		       nig_params->llfc_high_priority_classes);
2208 
2209 		REG_WR(bp, port ? NIG_REG_LLFC_LOW_PRIORITY_CLASSES_1 :
2210 		       NIG_REG_LLFC_LOW_PRIORITY_CLASSES_0,
2211 		       nig_params->llfc_low_priority_classes);
2212 	}
2213 	REG_WR(bp, port ? NIG_REG_P1_PKT_PRIORITY_TO_COS :
2214 	       NIG_REG_P0_PKT_PRIORITY_TO_COS,
2215 	       pkt_priority_to_cos);
2216 }
2217 
2218 int bnx2x_update_pfc(struct link_params *params,
2219 		      struct link_vars *vars,
2220 		      struct bnx2x_nig_brb_pfc_port_params *pfc_params)
2221 {
2222 	/* The PFC and pause are orthogonal to one another, meaning when
2223 	 * PFC is enabled, the pause are disabled, and when PFC is
2224 	 * disabled, pause are set according to the pause result.
2225 	 */
2226 	u32 val;
2227 	struct bnx2x *bp = params->bp;
2228 	u8 bmac_loopback = (params->loopback_mode == LOOPBACK_BMAC);
2229 
2230 	if (params->feature_config_flags & FEATURE_CONFIG_PFC_ENABLED)
2231 		vars->link_status |= LINK_STATUS_PFC_ENABLED;
2232 	else
2233 		vars->link_status &= ~LINK_STATUS_PFC_ENABLED;
2234 
2235 	bnx2x_update_mng(params, vars->link_status);
2236 
2237 	/* Update NIG params */
2238 	bnx2x_update_pfc_nig(params, vars, pfc_params);
2239 
2240 	if (!vars->link_up)
2241 		return 0;
2242 
2243 	DP(NETIF_MSG_LINK, "About to update PFC in BMAC\n");
2244 
2245 	if (CHIP_IS_E3(bp)) {
2246 		if (vars->mac_type == MAC_TYPE_XMAC)
2247 			bnx2x_update_pfc_xmac(params, vars, 0);
2248 	} else {
2249 		val = REG_RD(bp, MISC_REG_RESET_REG_2);
2250 		if ((val &
2251 		     (MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << params->port))
2252 		    == 0) {
2253 			DP(NETIF_MSG_LINK, "About to update PFC in EMAC\n");
2254 			bnx2x_emac_enable(params, vars, 0);
2255 			return 0;
2256 		}
2257 		if (CHIP_IS_E2(bp))
2258 			bnx2x_update_pfc_bmac2(params, vars, bmac_loopback);
2259 		else
2260 			bnx2x_update_pfc_bmac1(params, vars);
2261 
2262 		val = 0;
2263 		if ((params->feature_config_flags &
2264 		     FEATURE_CONFIG_PFC_ENABLED) ||
2265 		    (vars->flow_ctrl & BNX2X_FLOW_CTRL_TX))
2266 			val = 1;
2267 		REG_WR(bp, NIG_REG_BMAC0_PAUSE_OUT_EN + params->port*4, val);
2268 	}
2269 	return 0;
2270 }
2271 
2272 static int bnx2x_bmac1_enable(struct link_params *params,
2273 			      struct link_vars *vars,
2274 			      u8 is_lb)
2275 {
2276 	struct bnx2x *bp = params->bp;
2277 	u8 port = params->port;
2278 	u32 bmac_addr = port ? NIG_REG_INGRESS_BMAC1_MEM :
2279 			       NIG_REG_INGRESS_BMAC0_MEM;
2280 	u32 wb_data[2];
2281 	u32 val;
2282 
2283 	DP(NETIF_MSG_LINK, "Enabling BigMAC1\n");
2284 
2285 	/* XGXS control */
2286 	wb_data[0] = 0x3c;
2287 	wb_data[1] = 0;
2288 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_BMAC_XGXS_CONTROL,
2289 		    wb_data, 2);
2290 
2291 	/* TX MAC SA */
2292 	wb_data[0] = ((params->mac_addr[2] << 24) |
2293 		       (params->mac_addr[3] << 16) |
2294 		       (params->mac_addr[4] << 8) |
2295 			params->mac_addr[5]);
2296 	wb_data[1] = ((params->mac_addr[0] << 8) |
2297 			params->mac_addr[1]);
2298 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_TX_SOURCE_ADDR, wb_data, 2);
2299 
2300 	/* MAC control */
2301 	val = 0x3;
2302 	if (is_lb) {
2303 		val |= 0x4;
2304 		DP(NETIF_MSG_LINK, "enable bmac loopback\n");
2305 	}
2306 	wb_data[0] = val;
2307 	wb_data[1] = 0;
2308 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_BMAC_CONTROL, wb_data, 2);
2309 
2310 	/* Set rx mtu */
2311 	wb_data[0] = ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVERHEAD;
2312 	wb_data[1] = 0;
2313 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_RX_MAX_SIZE, wb_data, 2);
2314 
2315 	bnx2x_update_pfc_bmac1(params, vars);
2316 
2317 	/* Set tx mtu */
2318 	wb_data[0] = ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVERHEAD;
2319 	wb_data[1] = 0;
2320 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_TX_MAX_SIZE, wb_data, 2);
2321 
2322 	/* Set cnt max size */
2323 	wb_data[0] = ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVERHEAD;
2324 	wb_data[1] = 0;
2325 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_CNT_MAX_SIZE, wb_data, 2);
2326 
2327 	/* Configure SAFC */
2328 	wb_data[0] = 0x1000200;
2329 	wb_data[1] = 0;
2330 	REG_WR_DMAE(bp, bmac_addr + BIGMAC_REGISTER_RX_LLFC_MSG_FLDS,
2331 		    wb_data, 2);
2332 
2333 	return 0;
2334 }
2335 
2336 static int bnx2x_bmac2_enable(struct link_params *params,
2337 			      struct link_vars *vars,
2338 			      u8 is_lb)
2339 {
2340 	struct bnx2x *bp = params->bp;
2341 	u8 port = params->port;
2342 	u32 bmac_addr = port ? NIG_REG_INGRESS_BMAC1_MEM :
2343 			       NIG_REG_INGRESS_BMAC0_MEM;
2344 	u32 wb_data[2];
2345 
2346 	DP(NETIF_MSG_LINK, "Enabling BigMAC2\n");
2347 
2348 	wb_data[0] = 0;
2349 	wb_data[1] = 0;
2350 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_BMAC_CONTROL, wb_data, 2);
2351 	udelay(30);
2352 
2353 	/* XGXS control: Reset phy HW, MDIO registers, PHY PLL and BMAC */
2354 	wb_data[0] = 0x3c;
2355 	wb_data[1] = 0;
2356 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_BMAC_XGXS_CONTROL,
2357 		    wb_data, 2);
2358 
2359 	udelay(30);
2360 
2361 	/* TX MAC SA */
2362 	wb_data[0] = ((params->mac_addr[2] << 24) |
2363 		       (params->mac_addr[3] << 16) |
2364 		       (params->mac_addr[4] << 8) |
2365 			params->mac_addr[5]);
2366 	wb_data[1] = ((params->mac_addr[0] << 8) |
2367 			params->mac_addr[1]);
2368 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_TX_SOURCE_ADDR,
2369 		    wb_data, 2);
2370 
2371 	udelay(30);
2372 
2373 	/* Configure SAFC */
2374 	wb_data[0] = 0x1000200;
2375 	wb_data[1] = 0;
2376 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_RX_LLFC_MSG_FLDS,
2377 		    wb_data, 2);
2378 	udelay(30);
2379 
2380 	/* Set RX MTU */
2381 	wb_data[0] = ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVERHEAD;
2382 	wb_data[1] = 0;
2383 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_RX_MAX_SIZE, wb_data, 2);
2384 	udelay(30);
2385 
2386 	/* Set TX MTU */
2387 	wb_data[0] = ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVERHEAD;
2388 	wb_data[1] = 0;
2389 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_TX_MAX_SIZE, wb_data, 2);
2390 	udelay(30);
2391 	/* Set cnt max size */
2392 	wb_data[0] = ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVERHEAD - 2;
2393 	wb_data[1] = 0;
2394 	REG_WR_DMAE(bp, bmac_addr + BIGMAC2_REGISTER_CNT_MAX_SIZE, wb_data, 2);
2395 	udelay(30);
2396 	bnx2x_update_pfc_bmac2(params, vars, is_lb);
2397 
2398 	return 0;
2399 }
2400 
2401 static int bnx2x_bmac_enable(struct link_params *params,
2402 			     struct link_vars *vars,
2403 			     u8 is_lb, u8 reset_bmac)
2404 {
2405 	int rc = 0;
2406 	u8 port = params->port;
2407 	struct bnx2x *bp = params->bp;
2408 	u32 val;
2409 	/* Reset and unreset the BigMac */
2410 	if (reset_bmac) {
2411 		REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
2412 		       (MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port));
2413 		usleep_range(1000, 2000);
2414 	}
2415 
2416 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
2417 	       (MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port));
2418 
2419 	/* Enable access for bmac registers */
2420 	REG_WR(bp, NIG_REG_BMAC0_REGS_OUT_EN + port*4, 0x1);
2421 
2422 	/* Enable BMAC according to BMAC type*/
2423 	if (CHIP_IS_E2(bp))
2424 		rc = bnx2x_bmac2_enable(params, vars, is_lb);
2425 	else
2426 		rc = bnx2x_bmac1_enable(params, vars, is_lb);
2427 	REG_WR(bp, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 0x1);
2428 	REG_WR(bp, NIG_REG_XGXS_LANE_SEL_P0 + port*4, 0x0);
2429 	REG_WR(bp, NIG_REG_EGRESS_EMAC0_PORT + port*4, 0x0);
2430 	val = 0;
2431 	if ((params->feature_config_flags &
2432 	      FEATURE_CONFIG_PFC_ENABLED) ||
2433 	    (vars->flow_ctrl & BNX2X_FLOW_CTRL_TX))
2434 		val = 1;
2435 	REG_WR(bp, NIG_REG_BMAC0_PAUSE_OUT_EN + port*4, val);
2436 	REG_WR(bp, NIG_REG_EGRESS_EMAC0_OUT_EN + port*4, 0x0);
2437 	REG_WR(bp, NIG_REG_EMAC0_IN_EN + port*4, 0x0);
2438 	REG_WR(bp, NIG_REG_EMAC0_PAUSE_OUT_EN + port*4, 0x0);
2439 	REG_WR(bp, NIG_REG_BMAC0_IN_EN + port*4, 0x1);
2440 	REG_WR(bp, NIG_REG_BMAC0_OUT_EN + port*4, 0x1);
2441 
2442 	vars->mac_type = MAC_TYPE_BMAC;
2443 	return rc;
2444 }
2445 
2446 static void bnx2x_set_bmac_rx(struct bnx2x *bp, u32 chip_id, u8 port, u8 en)
2447 {
2448 	u32 bmac_addr = port ? NIG_REG_INGRESS_BMAC1_MEM :
2449 			NIG_REG_INGRESS_BMAC0_MEM;
2450 	u32 wb_data[2];
2451 	u32 nig_bmac_enable = REG_RD(bp, NIG_REG_BMAC0_REGS_OUT_EN + port*4);
2452 
2453 	if (CHIP_IS_E2(bp))
2454 		bmac_addr += BIGMAC2_REGISTER_BMAC_CONTROL;
2455 	else
2456 		bmac_addr += BIGMAC_REGISTER_BMAC_CONTROL;
2457 	/* Only if the bmac is out of reset */
2458 	if (REG_RD(bp, MISC_REG_RESET_REG_2) &
2459 			(MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port) &&
2460 	    nig_bmac_enable) {
2461 		/* Clear Rx Enable bit in BMAC_CONTROL register */
2462 		REG_RD_DMAE(bp, bmac_addr, wb_data, 2);
2463 		if (en)
2464 			wb_data[0] |= BMAC_CONTROL_RX_ENABLE;
2465 		else
2466 			wb_data[0] &= ~BMAC_CONTROL_RX_ENABLE;
2467 		REG_WR_DMAE(bp, bmac_addr, wb_data, 2);
2468 		usleep_range(1000, 2000);
2469 	}
2470 }
2471 
2472 static int bnx2x_pbf_update(struct link_params *params, u32 flow_ctrl,
2473 			    u32 line_speed)
2474 {
2475 	struct bnx2x *bp = params->bp;
2476 	u8 port = params->port;
2477 	u32 init_crd, crd;
2478 	u32 count = 1000;
2479 
2480 	/* Disable port */
2481 	REG_WR(bp, PBF_REG_DISABLE_NEW_TASK_PROC_P0 + port*4, 0x1);
2482 
2483 	/* Wait for init credit */
2484 	init_crd = REG_RD(bp, PBF_REG_P0_INIT_CRD + port*4);
2485 	crd = REG_RD(bp, PBF_REG_P0_CREDIT + port*8);
2486 	DP(NETIF_MSG_LINK, "init_crd 0x%x  crd 0x%x\n", init_crd, crd);
2487 
2488 	while ((init_crd != crd) && count) {
2489 		usleep_range(5000, 10000);
2490 		crd = REG_RD(bp, PBF_REG_P0_CREDIT + port*8);
2491 		count--;
2492 	}
2493 	crd = REG_RD(bp, PBF_REG_P0_CREDIT + port*8);
2494 	if (init_crd != crd) {
2495 		DP(NETIF_MSG_LINK, "BUG! init_crd 0x%x != crd 0x%x\n",
2496 			  init_crd, crd);
2497 		return -EINVAL;
2498 	}
2499 
2500 	if (flow_ctrl & BNX2X_FLOW_CTRL_RX ||
2501 	    line_speed == SPEED_10 ||
2502 	    line_speed == SPEED_100 ||
2503 	    line_speed == SPEED_1000 ||
2504 	    line_speed == SPEED_2500) {
2505 		REG_WR(bp, PBF_REG_P0_PAUSE_ENABLE + port*4, 1);
2506 		/* Update threshold */
2507 		REG_WR(bp, PBF_REG_P0_ARB_THRSH + port*4, 0);
2508 		/* Update init credit */
2509 		init_crd = 778;		/* (800-18-4) */
2510 
2511 	} else {
2512 		u32 thresh = (ETH_MAX_JUMBO_PACKET_SIZE +
2513 			      ETH_OVERHEAD)/16;
2514 		REG_WR(bp, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
2515 		/* Update threshold */
2516 		REG_WR(bp, PBF_REG_P0_ARB_THRSH + port*4, thresh);
2517 		/* Update init credit */
2518 		switch (line_speed) {
2519 		case SPEED_10000:
2520 			init_crd = thresh + 553 - 22;
2521 			break;
2522 		default:
2523 			DP(NETIF_MSG_LINK, "Invalid line_speed 0x%x\n",
2524 				  line_speed);
2525 			return -EINVAL;
2526 		}
2527 	}
2528 	REG_WR(bp, PBF_REG_P0_INIT_CRD + port*4, init_crd);
2529 	DP(NETIF_MSG_LINK, "PBF updated to speed %d credit %d\n",
2530 		 line_speed, init_crd);
2531 
2532 	/* Probe the credit changes */
2533 	REG_WR(bp, PBF_REG_INIT_P0 + port*4, 0x1);
2534 	usleep_range(5000, 10000);
2535 	REG_WR(bp, PBF_REG_INIT_P0 + port*4, 0x0);
2536 
2537 	/* Enable port */
2538 	REG_WR(bp, PBF_REG_DISABLE_NEW_TASK_PROC_P0 + port*4, 0x0);
2539 	return 0;
2540 }
2541 
2542 /**
2543  * bnx2x_get_emac_base - retrive emac base address
2544  *
2545  * @bp:			driver handle
2546  * @mdc_mdio_access:	access type
2547  * @port:		port id
2548  *
2549  * This function selects the MDC/MDIO access (through emac0 or
2550  * emac1) depend on the mdc_mdio_access, port, port swapped. Each
2551  * phy has a default access mode, which could also be overridden
2552  * by nvram configuration. This parameter, whether this is the
2553  * default phy configuration, or the nvram overrun
2554  * configuration, is passed here as mdc_mdio_access and selects
2555  * the emac_base for the CL45 read/writes operations
2556  */
2557 static u32 bnx2x_get_emac_base(struct bnx2x *bp,
2558 			       u32 mdc_mdio_access, u8 port)
2559 {
2560 	u32 emac_base = 0;
2561 	switch (mdc_mdio_access) {
2562 	case SHARED_HW_CFG_MDC_MDIO_ACCESS1_PHY_TYPE:
2563 		break;
2564 	case SHARED_HW_CFG_MDC_MDIO_ACCESS1_EMAC0:
2565 		if (REG_RD(bp, NIG_REG_PORT_SWAP))
2566 			emac_base = GRCBASE_EMAC1;
2567 		else
2568 			emac_base = GRCBASE_EMAC0;
2569 		break;
2570 	case SHARED_HW_CFG_MDC_MDIO_ACCESS1_EMAC1:
2571 		if (REG_RD(bp, NIG_REG_PORT_SWAP))
2572 			emac_base = GRCBASE_EMAC0;
2573 		else
2574 			emac_base = GRCBASE_EMAC1;
2575 		break;
2576 	case SHARED_HW_CFG_MDC_MDIO_ACCESS1_BOTH:
2577 		emac_base = (port) ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
2578 		break;
2579 	case SHARED_HW_CFG_MDC_MDIO_ACCESS1_SWAPPED:
2580 		emac_base = (port) ? GRCBASE_EMAC0 : GRCBASE_EMAC1;
2581 		break;
2582 	default:
2583 		break;
2584 	}
2585 	return emac_base;
2586 
2587 }
2588 
2589 /******************************************************************/
2590 /*			CL22 access functions			  */
2591 /******************************************************************/
2592 static int bnx2x_cl22_write(struct bnx2x *bp,
2593 				       struct bnx2x_phy *phy,
2594 				       u16 reg, u16 val)
2595 {
2596 	u32 tmp, mode;
2597 	u8 i;
2598 	int rc = 0;
2599 	/* Switch to CL22 */
2600 	mode = REG_RD(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_MODE);
2601 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_MODE,
2602 	       mode & ~EMAC_MDIO_MODE_CLAUSE_45);
2603 
2604 	/* Address */
2605 	tmp = ((phy->addr << 21) | (reg << 16) | val |
2606 	       EMAC_MDIO_COMM_COMMAND_WRITE_22 |
2607 	       EMAC_MDIO_COMM_START_BUSY);
2608 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM, tmp);
2609 
2610 	for (i = 0; i < 50; i++) {
2611 		udelay(10);
2612 
2613 		tmp = REG_RD(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM);
2614 		if (!(tmp & EMAC_MDIO_COMM_START_BUSY)) {
2615 			udelay(5);
2616 			break;
2617 		}
2618 	}
2619 	if (tmp & EMAC_MDIO_COMM_START_BUSY) {
2620 		DP(NETIF_MSG_LINK, "write phy register failed\n");
2621 		rc = -EFAULT;
2622 	}
2623 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_MODE, mode);
2624 	return rc;
2625 }
2626 
2627 static int bnx2x_cl22_read(struct bnx2x *bp,
2628 				      struct bnx2x_phy *phy,
2629 				      u16 reg, u16 *ret_val)
2630 {
2631 	u32 val, mode;
2632 	u16 i;
2633 	int rc = 0;
2634 
2635 	/* Switch to CL22 */
2636 	mode = REG_RD(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_MODE);
2637 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_MODE,
2638 	       mode & ~EMAC_MDIO_MODE_CLAUSE_45);
2639 
2640 	/* Address */
2641 	val = ((phy->addr << 21) | (reg << 16) |
2642 	       EMAC_MDIO_COMM_COMMAND_READ_22 |
2643 	       EMAC_MDIO_COMM_START_BUSY);
2644 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM, val);
2645 
2646 	for (i = 0; i < 50; i++) {
2647 		udelay(10);
2648 
2649 		val = REG_RD(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM);
2650 		if (!(val & EMAC_MDIO_COMM_START_BUSY)) {
2651 			*ret_val = (u16)(val & EMAC_MDIO_COMM_DATA);
2652 			udelay(5);
2653 			break;
2654 		}
2655 	}
2656 	if (val & EMAC_MDIO_COMM_START_BUSY) {
2657 		DP(NETIF_MSG_LINK, "read phy register failed\n");
2658 
2659 		*ret_val = 0;
2660 		rc = -EFAULT;
2661 	}
2662 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_MODE, mode);
2663 	return rc;
2664 }
2665 
2666 /******************************************************************/
2667 /*			CL45 access functions			  */
2668 /******************************************************************/
2669 static int bnx2x_cl45_read(struct bnx2x *bp, struct bnx2x_phy *phy,
2670 			   u8 devad, u16 reg, u16 *ret_val)
2671 {
2672 	u32 val;
2673 	u16 i;
2674 	int rc = 0;
2675 	u32 chip_id;
2676 	if (phy->flags & FLAGS_MDC_MDIO_WA_G) {
2677 		chip_id = (REG_RD(bp, MISC_REG_CHIP_NUM) << 16) |
2678 			  ((REG_RD(bp, MISC_REG_CHIP_REV) & 0xf) << 12);
2679 		bnx2x_set_mdio_clk(bp, chip_id, phy->mdio_ctrl);
2680 	}
2681 
2682 	if (phy->flags & FLAGS_MDC_MDIO_WA_B0)
2683 		bnx2x_bits_en(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_STATUS,
2684 			      EMAC_MDIO_STATUS_10MB);
2685 	/* Address */
2686 	val = ((phy->addr << 21) | (devad << 16) | reg |
2687 	       EMAC_MDIO_COMM_COMMAND_ADDRESS |
2688 	       EMAC_MDIO_COMM_START_BUSY);
2689 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM, val);
2690 
2691 	for (i = 0; i < 50; i++) {
2692 		udelay(10);
2693 
2694 		val = REG_RD(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM);
2695 		if (!(val & EMAC_MDIO_COMM_START_BUSY)) {
2696 			udelay(5);
2697 			break;
2698 		}
2699 	}
2700 	if (val & EMAC_MDIO_COMM_START_BUSY) {
2701 		DP(NETIF_MSG_LINK, "read phy register failed\n");
2702 		netdev_err(bp->dev,  "MDC/MDIO access timeout\n");
2703 		*ret_val = 0;
2704 		rc = -EFAULT;
2705 	} else {
2706 		/* Data */
2707 		val = ((phy->addr << 21) | (devad << 16) |
2708 		       EMAC_MDIO_COMM_COMMAND_READ_45 |
2709 		       EMAC_MDIO_COMM_START_BUSY);
2710 		REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM, val);
2711 
2712 		for (i = 0; i < 50; i++) {
2713 			udelay(10);
2714 
2715 			val = REG_RD(bp, phy->mdio_ctrl +
2716 				     EMAC_REG_EMAC_MDIO_COMM);
2717 			if (!(val & EMAC_MDIO_COMM_START_BUSY)) {
2718 				*ret_val = (u16)(val & EMAC_MDIO_COMM_DATA);
2719 				break;
2720 			}
2721 		}
2722 		if (val & EMAC_MDIO_COMM_START_BUSY) {
2723 			DP(NETIF_MSG_LINK, "read phy register failed\n");
2724 			netdev_err(bp->dev,  "MDC/MDIO access timeout\n");
2725 			*ret_val = 0;
2726 			rc = -EFAULT;
2727 		}
2728 	}
2729 	/* Work around for E3 A0 */
2730 	if (phy->flags & FLAGS_MDC_MDIO_WA) {
2731 		phy->flags ^= FLAGS_DUMMY_READ;
2732 		if (phy->flags & FLAGS_DUMMY_READ) {
2733 			u16 temp_val;
2734 			bnx2x_cl45_read(bp, phy, devad, 0xf, &temp_val);
2735 		}
2736 	}
2737 
2738 	if (phy->flags & FLAGS_MDC_MDIO_WA_B0)
2739 		bnx2x_bits_dis(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_STATUS,
2740 			       EMAC_MDIO_STATUS_10MB);
2741 	return rc;
2742 }
2743 
2744 static int bnx2x_cl45_write(struct bnx2x *bp, struct bnx2x_phy *phy,
2745 			    u8 devad, u16 reg, u16 val)
2746 {
2747 	u32 tmp;
2748 	u8 i;
2749 	int rc = 0;
2750 	u32 chip_id;
2751 	if (phy->flags & FLAGS_MDC_MDIO_WA_G) {
2752 		chip_id = (REG_RD(bp, MISC_REG_CHIP_NUM) << 16) |
2753 			  ((REG_RD(bp, MISC_REG_CHIP_REV) & 0xf) << 12);
2754 		bnx2x_set_mdio_clk(bp, chip_id, phy->mdio_ctrl);
2755 	}
2756 
2757 	if (phy->flags & FLAGS_MDC_MDIO_WA_B0)
2758 		bnx2x_bits_en(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_STATUS,
2759 			      EMAC_MDIO_STATUS_10MB);
2760 
2761 	/* Address */
2762 	tmp = ((phy->addr << 21) | (devad << 16) | reg |
2763 	       EMAC_MDIO_COMM_COMMAND_ADDRESS |
2764 	       EMAC_MDIO_COMM_START_BUSY);
2765 	REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM, tmp);
2766 
2767 	for (i = 0; i < 50; i++) {
2768 		udelay(10);
2769 
2770 		tmp = REG_RD(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM);
2771 		if (!(tmp & EMAC_MDIO_COMM_START_BUSY)) {
2772 			udelay(5);
2773 			break;
2774 		}
2775 	}
2776 	if (tmp & EMAC_MDIO_COMM_START_BUSY) {
2777 		DP(NETIF_MSG_LINK, "write phy register failed\n");
2778 		netdev_err(bp->dev,  "MDC/MDIO access timeout\n");
2779 		rc = -EFAULT;
2780 	} else {
2781 		/* Data */
2782 		tmp = ((phy->addr << 21) | (devad << 16) | val |
2783 		       EMAC_MDIO_COMM_COMMAND_WRITE_45 |
2784 		       EMAC_MDIO_COMM_START_BUSY);
2785 		REG_WR(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_COMM, tmp);
2786 
2787 		for (i = 0; i < 50; i++) {
2788 			udelay(10);
2789 
2790 			tmp = REG_RD(bp, phy->mdio_ctrl +
2791 				     EMAC_REG_EMAC_MDIO_COMM);
2792 			if (!(tmp & EMAC_MDIO_COMM_START_BUSY)) {
2793 				udelay(5);
2794 				break;
2795 			}
2796 		}
2797 		if (tmp & EMAC_MDIO_COMM_START_BUSY) {
2798 			DP(NETIF_MSG_LINK, "write phy register failed\n");
2799 			netdev_err(bp->dev,  "MDC/MDIO access timeout\n");
2800 			rc = -EFAULT;
2801 		}
2802 	}
2803 	/* Work around for E3 A0 */
2804 	if (phy->flags & FLAGS_MDC_MDIO_WA) {
2805 		phy->flags ^= FLAGS_DUMMY_READ;
2806 		if (phy->flags & FLAGS_DUMMY_READ) {
2807 			u16 temp_val;
2808 			bnx2x_cl45_read(bp, phy, devad, 0xf, &temp_val);
2809 		}
2810 	}
2811 	if (phy->flags & FLAGS_MDC_MDIO_WA_B0)
2812 		bnx2x_bits_dis(bp, phy->mdio_ctrl + EMAC_REG_EMAC_MDIO_STATUS,
2813 			       EMAC_MDIO_STATUS_10MB);
2814 	return rc;
2815 }
2816 
2817 /******************************************************************/
2818 /*			EEE section				   */
2819 /******************************************************************/
2820 static u8 bnx2x_eee_has_cap(struct link_params *params)
2821 {
2822 	struct bnx2x *bp = params->bp;
2823 
2824 	if (REG_RD(bp, params->shmem2_base) <=
2825 		   offsetof(struct shmem2_region, eee_status[params->port]))
2826 		return 0;
2827 
2828 	return 1;
2829 }
2830 
2831 static int bnx2x_eee_nvram_to_time(u32 nvram_mode, u32 *idle_timer)
2832 {
2833 	switch (nvram_mode) {
2834 	case PORT_FEAT_CFG_EEE_POWER_MODE_BALANCED:
2835 		*idle_timer = EEE_MODE_NVRAM_BALANCED_TIME;
2836 		break;
2837 	case PORT_FEAT_CFG_EEE_POWER_MODE_AGGRESSIVE:
2838 		*idle_timer = EEE_MODE_NVRAM_AGGRESSIVE_TIME;
2839 		break;
2840 	case PORT_FEAT_CFG_EEE_POWER_MODE_LOW_LATENCY:
2841 		*idle_timer = EEE_MODE_NVRAM_LATENCY_TIME;
2842 		break;
2843 	default:
2844 		*idle_timer = 0;
2845 		break;
2846 	}
2847 
2848 	return 0;
2849 }
2850 
2851 static int bnx2x_eee_time_to_nvram(u32 idle_timer, u32 *nvram_mode)
2852 {
2853 	switch (idle_timer) {
2854 	case EEE_MODE_NVRAM_BALANCED_TIME:
2855 		*nvram_mode = PORT_FEAT_CFG_EEE_POWER_MODE_BALANCED;
2856 		break;
2857 	case EEE_MODE_NVRAM_AGGRESSIVE_TIME:
2858 		*nvram_mode = PORT_FEAT_CFG_EEE_POWER_MODE_AGGRESSIVE;
2859 		break;
2860 	case EEE_MODE_NVRAM_LATENCY_TIME:
2861 		*nvram_mode = PORT_FEAT_CFG_EEE_POWER_MODE_LOW_LATENCY;
2862 		break;
2863 	default:
2864 		*nvram_mode = PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED;
2865 		break;
2866 	}
2867 
2868 	return 0;
2869 }
2870 
2871 static u32 bnx2x_eee_calc_timer(struct link_params *params)
2872 {
2873 	u32 eee_mode, eee_idle;
2874 	struct bnx2x *bp = params->bp;
2875 
2876 	if (params->eee_mode & EEE_MODE_OVERRIDE_NVRAM) {
2877 		if (params->eee_mode & EEE_MODE_OUTPUT_TIME) {
2878 			/* time value in eee_mode --> used directly*/
2879 			eee_idle = params->eee_mode & EEE_MODE_TIMER_MASK;
2880 		} else {
2881 			/* hsi value in eee_mode --> time */
2882 			if (bnx2x_eee_nvram_to_time(params->eee_mode &
2883 						    EEE_MODE_NVRAM_MASK,
2884 						    &eee_idle))
2885 				return 0;
2886 		}
2887 	} else {
2888 		/* hsi values in nvram --> time*/
2889 		eee_mode = ((REG_RD(bp, params->shmem_base +
2890 				    offsetof(struct shmem_region, dev_info.
2891 				    port_feature_config[params->port].
2892 				    eee_power_mode)) &
2893 			     PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
2894 			    PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
2895 
2896 		if (bnx2x_eee_nvram_to_time(eee_mode, &eee_idle))
2897 			return 0;
2898 	}
2899 
2900 	return eee_idle;
2901 }
2902 
2903 static int bnx2x_eee_set_timers(struct link_params *params,
2904 				   struct link_vars *vars)
2905 {
2906 	u32 eee_idle = 0, eee_mode;
2907 	struct bnx2x *bp = params->bp;
2908 
2909 	eee_idle = bnx2x_eee_calc_timer(params);
2910 
2911 	if (eee_idle) {
2912 		REG_WR(bp, MISC_REG_CPMU_LP_IDLE_THR_P0 + (params->port << 2),
2913 		       eee_idle);
2914 	} else if ((params->eee_mode & EEE_MODE_ENABLE_LPI) &&
2915 		   (params->eee_mode & EEE_MODE_OVERRIDE_NVRAM) &&
2916 		   (params->eee_mode & EEE_MODE_OUTPUT_TIME)) {
2917 		DP(NETIF_MSG_LINK, "Error: Tx LPI is enabled with timer 0\n");
2918 		return -EINVAL;
2919 	}
2920 
2921 	vars->eee_status &= ~(SHMEM_EEE_TIMER_MASK | SHMEM_EEE_TIME_OUTPUT_BIT);
2922 	if (params->eee_mode & EEE_MODE_OUTPUT_TIME) {
2923 		/* eee_idle in 1u --> eee_status in 16u */
2924 		eee_idle >>= 4;
2925 		vars->eee_status |= (eee_idle & SHMEM_EEE_TIMER_MASK) |
2926 				    SHMEM_EEE_TIME_OUTPUT_BIT;
2927 	} else {
2928 		if (bnx2x_eee_time_to_nvram(eee_idle, &eee_mode))
2929 			return -EINVAL;
2930 		vars->eee_status |= eee_mode;
2931 	}
2932 
2933 	return 0;
2934 }
2935 
2936 static int bnx2x_eee_initial_config(struct link_params *params,
2937 				     struct link_vars *vars, u8 mode)
2938 {
2939 	vars->eee_status |= ((u32) mode) << SHMEM_EEE_SUPPORTED_SHIFT;
2940 
2941 	/* Propagate params' bits --> vars (for migration exposure) */
2942 	if (params->eee_mode & EEE_MODE_ENABLE_LPI)
2943 		vars->eee_status |= SHMEM_EEE_LPI_REQUESTED_BIT;
2944 	else
2945 		vars->eee_status &= ~SHMEM_EEE_LPI_REQUESTED_BIT;
2946 
2947 	if (params->eee_mode & EEE_MODE_ADV_LPI)
2948 		vars->eee_status |= SHMEM_EEE_REQUESTED_BIT;
2949 	else
2950 		vars->eee_status &= ~SHMEM_EEE_REQUESTED_BIT;
2951 
2952 	return bnx2x_eee_set_timers(params, vars);
2953 }
2954 
2955 static int bnx2x_eee_disable(struct bnx2x_phy *phy,
2956 				struct link_params *params,
2957 				struct link_vars *vars)
2958 {
2959 	struct bnx2x *bp = params->bp;
2960 
2961 	/* Make Certain LPI is disabled */
2962 	REG_WR(bp, MISC_REG_CPMU_LP_FW_ENABLE_P0 + (params->port << 2), 0);
2963 
2964 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_EEE_ADV, 0x0);
2965 
2966 	vars->eee_status &= ~SHMEM_EEE_ADV_STATUS_MASK;
2967 
2968 	return 0;
2969 }
2970 
2971 static int bnx2x_eee_advertise(struct bnx2x_phy *phy,
2972 				  struct link_params *params,
2973 				  struct link_vars *vars, u8 modes)
2974 {
2975 	struct bnx2x *bp = params->bp;
2976 	u16 val = 0;
2977 
2978 	/* Mask events preventing LPI generation */
2979 	REG_WR(bp, MISC_REG_CPMU_LP_MASK_EXT_P0 + (params->port << 2), 0xfc20);
2980 
2981 	if (modes & SHMEM_EEE_10G_ADV) {
2982 		DP(NETIF_MSG_LINK, "Advertise 10GBase-T EEE\n");
2983 		val |= 0x8;
2984 	}
2985 	if (modes & SHMEM_EEE_1G_ADV) {
2986 		DP(NETIF_MSG_LINK, "Advertise 1GBase-T EEE\n");
2987 		val |= 0x4;
2988 	}
2989 
2990 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_EEE_ADV, val);
2991 
2992 	vars->eee_status &= ~SHMEM_EEE_ADV_STATUS_MASK;
2993 	vars->eee_status |= (modes << SHMEM_EEE_ADV_STATUS_SHIFT);
2994 
2995 	return 0;
2996 }
2997 
2998 static void bnx2x_update_mng_eee(struct link_params *params, u32 eee_status)
2999 {
3000 	struct bnx2x *bp = params->bp;
3001 
3002 	if (bnx2x_eee_has_cap(params))
3003 		REG_WR(bp, params->shmem2_base +
3004 		       offsetof(struct shmem2_region,
3005 				eee_status[params->port]), eee_status);
3006 }
3007 
3008 static void bnx2x_eee_an_resolve(struct bnx2x_phy *phy,
3009 				  struct link_params *params,
3010 				  struct link_vars *vars)
3011 {
3012 	struct bnx2x *bp = params->bp;
3013 	u16 adv = 0, lp = 0;
3014 	u32 lp_adv = 0;
3015 	u8 neg = 0;
3016 
3017 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_EEE_ADV, &adv);
3018 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_LP_EEE_ADV, &lp);
3019 
3020 	if (lp & 0x2) {
3021 		lp_adv |= SHMEM_EEE_100M_ADV;
3022 		if (adv & 0x2) {
3023 			if (vars->line_speed == SPEED_100)
3024 				neg = 1;
3025 			DP(NETIF_MSG_LINK, "EEE negotiated - 100M\n");
3026 		}
3027 	}
3028 	if (lp & 0x14) {
3029 		lp_adv |= SHMEM_EEE_1G_ADV;
3030 		if (adv & 0x14) {
3031 			if (vars->line_speed == SPEED_1000)
3032 				neg = 1;
3033 			DP(NETIF_MSG_LINK, "EEE negotiated - 1G\n");
3034 		}
3035 	}
3036 	if (lp & 0x68) {
3037 		lp_adv |= SHMEM_EEE_10G_ADV;
3038 		if (adv & 0x68) {
3039 			if (vars->line_speed == SPEED_10000)
3040 				neg = 1;
3041 			DP(NETIF_MSG_LINK, "EEE negotiated - 10G\n");
3042 		}
3043 	}
3044 
3045 	vars->eee_status &= ~SHMEM_EEE_LP_ADV_STATUS_MASK;
3046 	vars->eee_status |= (lp_adv << SHMEM_EEE_LP_ADV_STATUS_SHIFT);
3047 
3048 	if (neg) {
3049 		DP(NETIF_MSG_LINK, "EEE is active\n");
3050 		vars->eee_status |= SHMEM_EEE_ACTIVE_BIT;
3051 	}
3052 
3053 }
3054 
3055 /******************************************************************/
3056 /*			BSC access functions from E3	          */
3057 /******************************************************************/
3058 static void bnx2x_bsc_module_sel(struct link_params *params)
3059 {
3060 	int idx;
3061 	u32 board_cfg, sfp_ctrl;
3062 	u32 i2c_pins[I2C_SWITCH_WIDTH], i2c_val[I2C_SWITCH_WIDTH];
3063 	struct bnx2x *bp = params->bp;
3064 	u8 port = params->port;
3065 	/* Read I2C output PINs */
3066 	board_cfg = REG_RD(bp, params->shmem_base +
3067 			   offsetof(struct shmem_region,
3068 				    dev_info.shared_hw_config.board));
3069 	i2c_pins[I2C_BSC0] = board_cfg & SHARED_HW_CFG_E3_I2C_MUX0_MASK;
3070 	i2c_pins[I2C_BSC1] = (board_cfg & SHARED_HW_CFG_E3_I2C_MUX1_MASK) >>
3071 			SHARED_HW_CFG_E3_I2C_MUX1_SHIFT;
3072 
3073 	/* Read I2C output value */
3074 	sfp_ctrl = REG_RD(bp, params->shmem_base +
3075 			  offsetof(struct shmem_region,
3076 				 dev_info.port_hw_config[port].e3_cmn_pin_cfg));
3077 	i2c_val[I2C_BSC0] = (sfp_ctrl & PORT_HW_CFG_E3_I2C_MUX0_MASK) > 0;
3078 	i2c_val[I2C_BSC1] = (sfp_ctrl & PORT_HW_CFG_E3_I2C_MUX1_MASK) > 0;
3079 	DP(NETIF_MSG_LINK, "Setting BSC switch\n");
3080 	for (idx = 0; idx < I2C_SWITCH_WIDTH; idx++)
3081 		bnx2x_set_cfg_pin(bp, i2c_pins[idx], i2c_val[idx]);
3082 }
3083 
3084 static int bnx2x_bsc_read(struct link_params *params,
3085 			  struct bnx2x *bp,
3086 			  u8 sl_devid,
3087 			  u16 sl_addr,
3088 			  u8 lc_addr,
3089 			  u8 xfer_cnt,
3090 			  u32 *data_array)
3091 {
3092 	u32 val, i;
3093 	int rc = 0;
3094 
3095 	if (xfer_cnt > 16) {
3096 		DP(NETIF_MSG_LINK, "invalid xfer_cnt %d. Max is 16 bytes\n",
3097 					xfer_cnt);
3098 		return -EINVAL;
3099 	}
3100 	bnx2x_bsc_module_sel(params);
3101 
3102 	xfer_cnt = 16 - lc_addr;
3103 
3104 	/* Enable the engine */
3105 	val = REG_RD(bp, MCP_REG_MCPR_IMC_COMMAND);
3106 	val |= MCPR_IMC_COMMAND_ENABLE;
3107 	REG_WR(bp, MCP_REG_MCPR_IMC_COMMAND, val);
3108 
3109 	/* Program slave device ID */
3110 	val = (sl_devid << 16) | sl_addr;
3111 	REG_WR(bp, MCP_REG_MCPR_IMC_SLAVE_CONTROL, val);
3112 
3113 	/* Start xfer with 0 byte to update the address pointer ???*/
3114 	val = (MCPR_IMC_COMMAND_ENABLE) |
3115 	      (MCPR_IMC_COMMAND_WRITE_OP <<
3116 		MCPR_IMC_COMMAND_OPERATION_BITSHIFT) |
3117 		(lc_addr << MCPR_IMC_COMMAND_TRANSFER_ADDRESS_BITSHIFT) | (0);
3118 	REG_WR(bp, MCP_REG_MCPR_IMC_COMMAND, val);
3119 
3120 	/* Poll for completion */
3121 	i = 0;
3122 	val = REG_RD(bp, MCP_REG_MCPR_IMC_COMMAND);
3123 	while (((val >> MCPR_IMC_COMMAND_IMC_STATUS_BITSHIFT) & 0x3) != 1) {
3124 		udelay(10);
3125 		val = REG_RD(bp, MCP_REG_MCPR_IMC_COMMAND);
3126 		if (i++ > 1000) {
3127 			DP(NETIF_MSG_LINK, "wr 0 byte timed out after %d try\n",
3128 								i);
3129 			rc = -EFAULT;
3130 			break;
3131 		}
3132 	}
3133 	if (rc == -EFAULT)
3134 		return rc;
3135 
3136 	/* Start xfer with read op */
3137 	val = (MCPR_IMC_COMMAND_ENABLE) |
3138 		(MCPR_IMC_COMMAND_READ_OP <<
3139 		MCPR_IMC_COMMAND_OPERATION_BITSHIFT) |
3140 		(lc_addr << MCPR_IMC_COMMAND_TRANSFER_ADDRESS_BITSHIFT) |
3141 		  (xfer_cnt);
3142 	REG_WR(bp, MCP_REG_MCPR_IMC_COMMAND, val);
3143 
3144 	/* Poll for completion */
3145 	i = 0;
3146 	val = REG_RD(bp, MCP_REG_MCPR_IMC_COMMAND);
3147 	while (((val >> MCPR_IMC_COMMAND_IMC_STATUS_BITSHIFT) & 0x3) != 1) {
3148 		udelay(10);
3149 		val = REG_RD(bp, MCP_REG_MCPR_IMC_COMMAND);
3150 		if (i++ > 1000) {
3151 			DP(NETIF_MSG_LINK, "rd op timed out after %d try\n", i);
3152 			rc = -EFAULT;
3153 			break;
3154 		}
3155 	}
3156 	if (rc == -EFAULT)
3157 		return rc;
3158 
3159 	for (i = (lc_addr >> 2); i < 4; i++) {
3160 		data_array[i] = REG_RD(bp, (MCP_REG_MCPR_IMC_DATAREG0 + i*4));
3161 #ifdef __BIG_ENDIAN
3162 		data_array[i] = ((data_array[i] & 0x000000ff) << 24) |
3163 				((data_array[i] & 0x0000ff00) << 8) |
3164 				((data_array[i] & 0x00ff0000) >> 8) |
3165 				((data_array[i] & 0xff000000) >> 24);
3166 #endif
3167 	}
3168 	return rc;
3169 }
3170 
3171 static void bnx2x_cl45_read_or_write(struct bnx2x *bp, struct bnx2x_phy *phy,
3172 				     u8 devad, u16 reg, u16 or_val)
3173 {
3174 	u16 val;
3175 	bnx2x_cl45_read(bp, phy, devad, reg, &val);
3176 	bnx2x_cl45_write(bp, phy, devad, reg, val | or_val);
3177 }
3178 
3179 static void bnx2x_cl45_read_and_write(struct bnx2x *bp,
3180 				      struct bnx2x_phy *phy,
3181 				      u8 devad, u16 reg, u16 and_val)
3182 {
3183 	u16 val;
3184 	bnx2x_cl45_read(bp, phy, devad, reg, &val);
3185 	bnx2x_cl45_write(bp, phy, devad, reg, val & and_val);
3186 }
3187 
3188 int bnx2x_phy_read(struct link_params *params, u8 phy_addr,
3189 		   u8 devad, u16 reg, u16 *ret_val)
3190 {
3191 	u8 phy_index;
3192 	/* Probe for the phy according to the given phy_addr, and execute
3193 	 * the read request on it
3194 	 */
3195 	for (phy_index = 0; phy_index < params->num_phys; phy_index++) {
3196 		if (params->phy[phy_index].addr == phy_addr) {
3197 			return bnx2x_cl45_read(params->bp,
3198 					       &params->phy[phy_index], devad,
3199 					       reg, ret_val);
3200 		}
3201 	}
3202 	return -EINVAL;
3203 }
3204 
3205 int bnx2x_phy_write(struct link_params *params, u8 phy_addr,
3206 		    u8 devad, u16 reg, u16 val)
3207 {
3208 	u8 phy_index;
3209 	/* Probe for the phy according to the given phy_addr, and execute
3210 	 * the write request on it
3211 	 */
3212 	for (phy_index = 0; phy_index < params->num_phys; phy_index++) {
3213 		if (params->phy[phy_index].addr == phy_addr) {
3214 			return bnx2x_cl45_write(params->bp,
3215 						&params->phy[phy_index], devad,
3216 						reg, val);
3217 		}
3218 	}
3219 	return -EINVAL;
3220 }
3221 static u8 bnx2x_get_warpcore_lane(struct bnx2x_phy *phy,
3222 				  struct link_params *params)
3223 {
3224 	u8 lane = 0;
3225 	struct bnx2x *bp = params->bp;
3226 	u32 path_swap, path_swap_ovr;
3227 	u8 path, port;
3228 
3229 	path = BP_PATH(bp);
3230 	port = params->port;
3231 
3232 	if (bnx2x_is_4_port_mode(bp)) {
3233 		u32 port_swap, port_swap_ovr;
3234 
3235 		/* Figure out path swap value */
3236 		path_swap_ovr = REG_RD(bp, MISC_REG_FOUR_PORT_PATH_SWAP_OVWR);
3237 		if (path_swap_ovr & 0x1)
3238 			path_swap = (path_swap_ovr & 0x2);
3239 		else
3240 			path_swap = REG_RD(bp, MISC_REG_FOUR_PORT_PATH_SWAP);
3241 
3242 		if (path_swap)
3243 			path = path ^ 1;
3244 
3245 		/* Figure out port swap value */
3246 		port_swap_ovr = REG_RD(bp, MISC_REG_FOUR_PORT_PORT_SWAP_OVWR);
3247 		if (port_swap_ovr & 0x1)
3248 			port_swap = (port_swap_ovr & 0x2);
3249 		else
3250 			port_swap = REG_RD(bp, MISC_REG_FOUR_PORT_PORT_SWAP);
3251 
3252 		if (port_swap)
3253 			port = port ^ 1;
3254 
3255 		lane = (port<<1) + path;
3256 	} else { /* Two port mode - no port swap */
3257 
3258 		/* Figure out path swap value */
3259 		path_swap_ovr =
3260 			REG_RD(bp, MISC_REG_TWO_PORT_PATH_SWAP_OVWR);
3261 		if (path_swap_ovr & 0x1) {
3262 			path_swap = (path_swap_ovr & 0x2);
3263 		} else {
3264 			path_swap =
3265 				REG_RD(bp, MISC_REG_TWO_PORT_PATH_SWAP);
3266 		}
3267 		if (path_swap)
3268 			path = path ^ 1;
3269 
3270 		lane = path << 1 ;
3271 	}
3272 	return lane;
3273 }
3274 
3275 static void bnx2x_set_aer_mmd(struct link_params *params,
3276 			      struct bnx2x_phy *phy)
3277 {
3278 	u32 ser_lane;
3279 	u16 offset, aer_val;
3280 	struct bnx2x *bp = params->bp;
3281 	ser_lane = ((params->lane_config &
3282 		     PORT_HW_CFG_LANE_SWAP_CFG_MASTER_MASK) >>
3283 		     PORT_HW_CFG_LANE_SWAP_CFG_MASTER_SHIFT);
3284 
3285 	offset = (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT) ?
3286 		(phy->addr + ser_lane) : 0;
3287 
3288 	if (USES_WARPCORE(bp)) {
3289 		aer_val = bnx2x_get_warpcore_lane(phy, params);
3290 		/* In Dual-lane mode, two lanes are joined together,
3291 		 * so in order to configure them, the AER broadcast method is
3292 		 * used here.
3293 		 * 0x200 is the broadcast address for lanes 0,1
3294 		 * 0x201 is the broadcast address for lanes 2,3
3295 		 */
3296 		if (phy->flags & FLAGS_WC_DUAL_MODE)
3297 			aer_val = (aer_val >> 1) | 0x200;
3298 	} else if (CHIP_IS_E2(bp))
3299 		aer_val = 0x3800 + offset - 1;
3300 	else
3301 		aer_val = 0x3800 + offset;
3302 
3303 	CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
3304 			  MDIO_AER_BLOCK_AER_REG, aer_val);
3305 
3306 }
3307 
3308 /******************************************************************/
3309 /*			Internal phy section			  */
3310 /******************************************************************/
3311 
3312 static void bnx2x_set_serdes_access(struct bnx2x *bp, u8 port)
3313 {
3314 	u32 emac_base = (port) ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
3315 
3316 	/* Set Clause 22 */
3317 	REG_WR(bp, NIG_REG_SERDES0_CTRL_MD_ST + port*0x10, 1);
3318 	REG_WR(bp, emac_base + EMAC_REG_EMAC_MDIO_COMM, 0x245f8000);
3319 	udelay(500);
3320 	REG_WR(bp, emac_base + EMAC_REG_EMAC_MDIO_COMM, 0x245d000f);
3321 	udelay(500);
3322 	 /* Set Clause 45 */
3323 	REG_WR(bp, NIG_REG_SERDES0_CTRL_MD_ST + port*0x10, 0);
3324 }
3325 
3326 static void bnx2x_serdes_deassert(struct bnx2x *bp, u8 port)
3327 {
3328 	u32 val;
3329 
3330 	DP(NETIF_MSG_LINK, "bnx2x_serdes_deassert\n");
3331 
3332 	val = SERDES_RESET_BITS << (port*16);
3333 
3334 	/* Reset and unreset the SerDes/XGXS */
3335 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_3_CLEAR, val);
3336 	udelay(500);
3337 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_3_SET, val);
3338 
3339 	bnx2x_set_serdes_access(bp, port);
3340 
3341 	REG_WR(bp, NIG_REG_SERDES0_CTRL_MD_DEVAD + port*0x10,
3342 	       DEFAULT_PHY_DEV_ADDR);
3343 }
3344 
3345 static void bnx2x_xgxs_specific_func(struct bnx2x_phy *phy,
3346 				     struct link_params *params,
3347 				     u32 action)
3348 {
3349 	struct bnx2x *bp = params->bp;
3350 	switch (action) {
3351 	case PHY_INIT:
3352 		/* Set correct devad */
3353 		REG_WR(bp, NIG_REG_XGXS0_CTRL_MD_ST + params->port*0x18, 0);
3354 		REG_WR(bp, NIG_REG_XGXS0_CTRL_MD_DEVAD + params->port*0x18,
3355 		       phy->def_md_devad);
3356 		break;
3357 	}
3358 }
3359 
3360 static void bnx2x_xgxs_deassert(struct link_params *params)
3361 {
3362 	struct bnx2x *bp = params->bp;
3363 	u8 port;
3364 	u32 val;
3365 	DP(NETIF_MSG_LINK, "bnx2x_xgxs_deassert\n");
3366 	port = params->port;
3367 
3368 	val = XGXS_RESET_BITS << (port*16);
3369 
3370 	/* Reset and unreset the SerDes/XGXS */
3371 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_3_CLEAR, val);
3372 	udelay(500);
3373 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_3_SET, val);
3374 	bnx2x_xgxs_specific_func(&params->phy[INT_PHY], params,
3375 				 PHY_INIT);
3376 }
3377 
3378 static void bnx2x_calc_ieee_aneg_adv(struct bnx2x_phy *phy,
3379 				     struct link_params *params, u16 *ieee_fc)
3380 {
3381 	struct bnx2x *bp = params->bp;
3382 	*ieee_fc = MDIO_COMBO_IEEE0_AUTO_NEG_ADV_FULL_DUPLEX;
3383 	/* Resolve pause mode and advertisement Please refer to Table
3384 	 * 28B-3 of the 802.3ab-1999 spec
3385 	 */
3386 
3387 	switch (phy->req_flow_ctrl) {
3388 	case BNX2X_FLOW_CTRL_AUTO:
3389 		switch (params->req_fc_auto_adv) {
3390 		case BNX2X_FLOW_CTRL_BOTH:
3391 		case BNX2X_FLOW_CTRL_RX:
3392 			*ieee_fc |= MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH;
3393 			break;
3394 		case BNX2X_FLOW_CTRL_TX:
3395 			*ieee_fc |=
3396 				MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC;
3397 			break;
3398 		default:
3399 			break;
3400 		}
3401 		break;
3402 	case BNX2X_FLOW_CTRL_TX:
3403 		*ieee_fc |= MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC;
3404 		break;
3405 
3406 	case BNX2X_FLOW_CTRL_RX:
3407 	case BNX2X_FLOW_CTRL_BOTH:
3408 		*ieee_fc |= MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH;
3409 		break;
3410 
3411 	case BNX2X_FLOW_CTRL_NONE:
3412 	default:
3413 		*ieee_fc |= MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE;
3414 		break;
3415 	}
3416 	DP(NETIF_MSG_LINK, "ieee_fc = 0x%x\n", *ieee_fc);
3417 }
3418 
3419 static void set_phy_vars(struct link_params *params,
3420 			 struct link_vars *vars)
3421 {
3422 	struct bnx2x *bp = params->bp;
3423 	u8 actual_phy_idx, phy_index, link_cfg_idx;
3424 	u8 phy_config_swapped = params->multi_phy_config &
3425 			PORT_HW_CFG_PHY_SWAPPED_ENABLED;
3426 	for (phy_index = INT_PHY; phy_index < params->num_phys;
3427 	      phy_index++) {
3428 		link_cfg_idx = LINK_CONFIG_IDX(phy_index);
3429 		actual_phy_idx = phy_index;
3430 		if (phy_config_swapped) {
3431 			if (phy_index == EXT_PHY1)
3432 				actual_phy_idx = EXT_PHY2;
3433 			else if (phy_index == EXT_PHY2)
3434 				actual_phy_idx = EXT_PHY1;
3435 		}
3436 		params->phy[actual_phy_idx].req_flow_ctrl =
3437 			params->req_flow_ctrl[link_cfg_idx];
3438 
3439 		params->phy[actual_phy_idx].req_line_speed =
3440 			params->req_line_speed[link_cfg_idx];
3441 
3442 		params->phy[actual_phy_idx].speed_cap_mask =
3443 			params->speed_cap_mask[link_cfg_idx];
3444 
3445 		params->phy[actual_phy_idx].req_duplex =
3446 			params->req_duplex[link_cfg_idx];
3447 
3448 		if (params->req_line_speed[link_cfg_idx] ==
3449 		    SPEED_AUTO_NEG)
3450 			vars->link_status |= LINK_STATUS_AUTO_NEGOTIATE_ENABLED;
3451 
3452 		DP(NETIF_MSG_LINK, "req_flow_ctrl %x, req_line_speed %x,"
3453 			   " speed_cap_mask %x\n",
3454 			   params->phy[actual_phy_idx].req_flow_ctrl,
3455 			   params->phy[actual_phy_idx].req_line_speed,
3456 			   params->phy[actual_phy_idx].speed_cap_mask);
3457 	}
3458 }
3459 
3460 static void bnx2x_ext_phy_set_pause(struct link_params *params,
3461 				    struct bnx2x_phy *phy,
3462 				    struct link_vars *vars)
3463 {
3464 	u16 val;
3465 	struct bnx2x *bp = params->bp;
3466 	/* Read modify write pause advertizing */
3467 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_ADV_PAUSE, &val);
3468 
3469 	val &= ~MDIO_AN_REG_ADV_PAUSE_BOTH;
3470 
3471 	/* Please refer to Table 28B-3 of 802.3ab-1999 spec. */
3472 	bnx2x_calc_ieee_aneg_adv(phy, params, &vars->ieee_fc);
3473 	if ((vars->ieee_fc &
3474 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC) ==
3475 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC) {
3476 		val |= MDIO_AN_REG_ADV_PAUSE_ASYMMETRIC;
3477 	}
3478 	if ((vars->ieee_fc &
3479 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH) ==
3480 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH) {
3481 		val |= MDIO_AN_REG_ADV_PAUSE_PAUSE;
3482 	}
3483 	DP(NETIF_MSG_LINK, "Ext phy AN advertize 0x%x\n", val);
3484 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_ADV_PAUSE, val);
3485 }
3486 
3487 static void bnx2x_pause_resolve(struct bnx2x_phy *phy,
3488 				struct link_params *params,
3489 				struct link_vars *vars,
3490 				u32 pause_result)
3491 {
3492 	struct bnx2x *bp = params->bp;
3493 						/*  LD	    LP	 */
3494 	switch (pause_result) {			/* ASYM P ASYM P */
3495 	case 0xb:				/*   1  0   1  1 */
3496 		DP(NETIF_MSG_LINK, "Flow Control: TX only\n");
3497 		vars->flow_ctrl = BNX2X_FLOW_CTRL_TX;
3498 		break;
3499 
3500 	case 0xe:				/*   1  1   1  0 */
3501 		DP(NETIF_MSG_LINK, "Flow Control: RX only\n");
3502 		vars->flow_ctrl = BNX2X_FLOW_CTRL_RX;
3503 		break;
3504 
3505 	case 0x5:				/*   0  1   0  1 */
3506 	case 0x7:				/*   0  1   1  1 */
3507 	case 0xd:				/*   1  1   0  1 */
3508 	case 0xf:				/*   1  1   1  1 */
3509 		/* If the user selected to advertise RX ONLY,
3510 		 * although we advertised both, need to enable
3511 		 * RX only.
3512 		 */
3513 		if (params->req_fc_auto_adv == BNX2X_FLOW_CTRL_BOTH) {
3514 			DP(NETIF_MSG_LINK, "Flow Control: RX & TX\n");
3515 			vars->flow_ctrl = BNX2X_FLOW_CTRL_BOTH;
3516 		} else {
3517 			DP(NETIF_MSG_LINK, "Flow Control: RX only\n");
3518 			vars->flow_ctrl = BNX2X_FLOW_CTRL_RX;
3519 		}
3520 		break;
3521 
3522 	default:
3523 		DP(NETIF_MSG_LINK, "Flow Control: None\n");
3524 		vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
3525 		break;
3526 	}
3527 	if (pause_result & (1<<0))
3528 		vars->link_status |= LINK_STATUS_LINK_PARTNER_SYMMETRIC_PAUSE;
3529 	if (pause_result & (1<<1))
3530 		vars->link_status |= LINK_STATUS_LINK_PARTNER_ASYMMETRIC_PAUSE;
3531 
3532 }
3533 
3534 static void bnx2x_ext_phy_update_adv_fc(struct bnx2x_phy *phy,
3535 					struct link_params *params,
3536 					struct link_vars *vars)
3537 {
3538 	u16 ld_pause;		/* local */
3539 	u16 lp_pause;		/* link partner */
3540 	u16 pause_result;
3541 	struct bnx2x *bp = params->bp;
3542 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54618SE) {
3543 		bnx2x_cl22_read(bp, phy, 0x4, &ld_pause);
3544 		bnx2x_cl22_read(bp, phy, 0x5, &lp_pause);
3545 	} else if (CHIP_IS_E3(bp) &&
3546 		SINGLE_MEDIA_DIRECT(params)) {
3547 		u8 lane = bnx2x_get_warpcore_lane(phy, params);
3548 		u16 gp_status, gp_mask;
3549 		bnx2x_cl45_read(bp, phy,
3550 				MDIO_AN_DEVAD, MDIO_WC_REG_GP2_STATUS_GP_2_4,
3551 				&gp_status);
3552 		gp_mask = (MDIO_WC_REG_GP2_STATUS_GP_2_4_CL73_AN_CMPL |
3553 			   MDIO_WC_REG_GP2_STATUS_GP_2_4_CL37_LP_AN_CAP) <<
3554 			lane;
3555 		if ((gp_status & gp_mask) == gp_mask) {
3556 			bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
3557 					MDIO_AN_REG_ADV_PAUSE, &ld_pause);
3558 			bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
3559 					MDIO_AN_REG_LP_AUTO_NEG, &lp_pause);
3560 		} else {
3561 			bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
3562 					MDIO_AN_REG_CL37_FC_LD, &ld_pause);
3563 			bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
3564 					MDIO_AN_REG_CL37_FC_LP, &lp_pause);
3565 			ld_pause = ((ld_pause &
3566 				     MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH)
3567 				    << 3);
3568 			lp_pause = ((lp_pause &
3569 				     MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH)
3570 				    << 3);
3571 		}
3572 	} else {
3573 		bnx2x_cl45_read(bp, phy,
3574 				MDIO_AN_DEVAD,
3575 				MDIO_AN_REG_ADV_PAUSE, &ld_pause);
3576 		bnx2x_cl45_read(bp, phy,
3577 				MDIO_AN_DEVAD,
3578 				MDIO_AN_REG_LP_AUTO_NEG, &lp_pause);
3579 	}
3580 	pause_result = (ld_pause &
3581 			MDIO_AN_REG_ADV_PAUSE_MASK) >> 8;
3582 	pause_result |= (lp_pause &
3583 			 MDIO_AN_REG_ADV_PAUSE_MASK) >> 10;
3584 	DP(NETIF_MSG_LINK, "Ext PHY pause result 0x%x\n", pause_result);
3585 	bnx2x_pause_resolve(phy, params, vars, pause_result);
3586 
3587 }
3588 
3589 static u8 bnx2x_ext_phy_resolve_fc(struct bnx2x_phy *phy,
3590 				   struct link_params *params,
3591 				   struct link_vars *vars)
3592 {
3593 	u8 ret = 0;
3594 	vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
3595 	if (phy->req_flow_ctrl != BNX2X_FLOW_CTRL_AUTO) {
3596 		/* Update the advertised flow-controled of LD/LP in AN */
3597 		if (phy->req_line_speed == SPEED_AUTO_NEG)
3598 			bnx2x_ext_phy_update_adv_fc(phy, params, vars);
3599 		/* But set the flow-control result as the requested one */
3600 		vars->flow_ctrl = phy->req_flow_ctrl;
3601 	} else if (phy->req_line_speed != SPEED_AUTO_NEG)
3602 		vars->flow_ctrl = params->req_fc_auto_adv;
3603 	else if (vars->link_status & LINK_STATUS_AUTO_NEGOTIATE_COMPLETE) {
3604 		ret = 1;
3605 		bnx2x_ext_phy_update_adv_fc(phy, params, vars);
3606 	}
3607 	return ret;
3608 }
3609 /******************************************************************/
3610 /*			Warpcore section			  */
3611 /******************************************************************/
3612 /* The init_internal_warpcore should mirror the xgxs,
3613  * i.e. reset the lane (if needed), set aer for the
3614  * init configuration, and set/clear SGMII flag. Internal
3615  * phy init is done purely in phy_init stage.
3616  */
3617 #define WC_TX_DRIVER(post2, idriver, ipre, ifir) \
3618 	((post2 << MDIO_WC_REG_TX0_TX_DRIVER_POST2_COEFF_OFFSET) | \
3619 	 (idriver << MDIO_WC_REG_TX0_TX_DRIVER_IDRIVER_OFFSET) | \
3620 	 (ipre << MDIO_WC_REG_TX0_TX_DRIVER_IPRE_DRIVER_OFFSET) | \
3621 	 (ifir << MDIO_WC_REG_TX0_TX_DRIVER_IFIR_OFFSET))
3622 
3623 #define WC_TX_FIR(post, main, pre) \
3624 	((post << MDIO_WC_REG_TX_FIR_TAP_POST_TAP_OFFSET) | \
3625 	 (main << MDIO_WC_REG_TX_FIR_TAP_MAIN_TAP_OFFSET) | \
3626 	 (pre << MDIO_WC_REG_TX_FIR_TAP_PRE_TAP_OFFSET))
3627 
3628 static void bnx2x_warpcore_enable_AN_KR2(struct bnx2x_phy *phy,
3629 					 struct link_params *params,
3630 					 struct link_vars *vars)
3631 {
3632 	struct bnx2x *bp = params->bp;
3633 	u16 i;
3634 	static struct bnx2x_reg_set reg_set[] = {
3635 		/* Step 1 - Program the TX/RX alignment markers */
3636 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL5, 0xa157},
3637 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL7, 0xcbe2},
3638 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL6, 0x7537},
3639 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL9, 0xa157},
3640 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_RX_CTRL11, 0xcbe2},
3641 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_RX_CTRL10, 0x7537},
3642 		/* Step 2 - Configure the NP registers */
3643 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_USERB0_CTRL, 0x000a},
3644 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_BAM_CTRL1, 0x6400},
3645 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_BAM_CTRL3, 0x0620},
3646 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_BAM_CODE_FIELD, 0x0157},
3647 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_OUI1, 0x6464},
3648 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_OUI2, 0x3150},
3649 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_OUI3, 0x3150},
3650 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_LD_BAM_CODE, 0x0157},
3651 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_LD_UD_CODE, 0x0620}
3652 	};
3653 	DP(NETIF_MSG_LINK, "Enabling 20G-KR2\n");
3654 
3655 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3656 				 MDIO_WC_REG_CL49_USERB0_CTRL, (3<<6));
3657 
3658 	for (i = 0; i < ARRAY_SIZE(reg_set); i++)
3659 		bnx2x_cl45_write(bp, phy, reg_set[i].devad, reg_set[i].reg,
3660 				 reg_set[i].val);
3661 
3662 	/* Start KR2 work-around timer which handles BCM8073 link-parner */
3663 	params->link_attr_sync |= LINK_ATTR_SYNC_KR2_ENABLE;
3664 	bnx2x_update_link_attr(params, params->link_attr_sync);
3665 }
3666 
3667 static void bnx2x_disable_kr2(struct link_params *params,
3668 			      struct link_vars *vars,
3669 			      struct bnx2x_phy *phy)
3670 {
3671 	struct bnx2x *bp = params->bp;
3672 	int i;
3673 	static struct bnx2x_reg_set reg_set[] = {
3674 		/* Step 1 - Program the TX/RX alignment markers */
3675 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL5, 0x7690},
3676 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL7, 0xe647},
3677 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL6, 0xc4f0},
3678 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_TX_CTRL9, 0x7690},
3679 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_RX_CTRL11, 0xe647},
3680 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL82_USERB1_RX_CTRL10, 0xc4f0},
3681 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_USERB0_CTRL, 0x000c},
3682 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_BAM_CTRL1, 0x6000},
3683 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_BAM_CTRL3, 0x0000},
3684 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL73_BAM_CODE_FIELD, 0x0002},
3685 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_OUI1, 0x0000},
3686 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_OUI2, 0x0af7},
3687 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_OUI3, 0x0af7},
3688 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_LD_BAM_CODE, 0x0002},
3689 		{MDIO_WC_DEVAD, MDIO_WC_REG_ETA_CL73_LD_UD_CODE, 0x0000}
3690 	};
3691 	DP(NETIF_MSG_LINK, "Disabling 20G-KR2\n");
3692 
3693 	for (i = 0; i < ARRAY_SIZE(reg_set); i++)
3694 		bnx2x_cl45_write(bp, phy, reg_set[i].devad, reg_set[i].reg,
3695 				 reg_set[i].val);
3696 	params->link_attr_sync &= ~LINK_ATTR_SYNC_KR2_ENABLE;
3697 	bnx2x_update_link_attr(params, params->link_attr_sync);
3698 
3699 	vars->check_kr2_recovery_cnt = CHECK_KR2_RECOVERY_CNT;
3700 }
3701 
3702 static void bnx2x_warpcore_set_lpi_passthrough(struct bnx2x_phy *phy,
3703 					       struct link_params *params)
3704 {
3705 	struct bnx2x *bp = params->bp;
3706 
3707 	DP(NETIF_MSG_LINK, "Configure WC for LPI pass through\n");
3708 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3709 			 MDIO_WC_REG_EEE_COMBO_CONTROL0, 0x7c);
3710 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3711 				 MDIO_WC_REG_DIGITAL4_MISC5, 0xc000);
3712 }
3713 
3714 static void bnx2x_warpcore_restart_AN_KR(struct bnx2x_phy *phy,
3715 					 struct link_params *params)
3716 {
3717 	/* Restart autoneg on the leading lane only */
3718 	struct bnx2x *bp = params->bp;
3719 	u16 lane = bnx2x_get_warpcore_lane(phy, params);
3720 	CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
3721 			  MDIO_AER_BLOCK_AER_REG, lane);
3722 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
3723 			 MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x1200);
3724 
3725 	/* Restore AER */
3726 	bnx2x_set_aer_mmd(params, phy);
3727 }
3728 
3729 static void bnx2x_warpcore_enable_AN_KR(struct bnx2x_phy *phy,
3730 					struct link_params *params,
3731 					struct link_vars *vars) {
3732 	u16 lane, i, cl72_ctrl, an_adv = 0, val;
3733 	u32 wc_lane_config;
3734 	struct bnx2x *bp = params->bp;
3735 	static struct bnx2x_reg_set reg_set[] = {
3736 		{MDIO_WC_DEVAD, MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2, 0x7},
3737 		{MDIO_PMA_DEVAD, MDIO_WC_REG_IEEE0BLK_AUTONEGNP, 0x0},
3738 		{MDIO_WC_DEVAD, MDIO_WC_REG_RX66_CONTROL, 0x7415},
3739 		{MDIO_WC_DEVAD, MDIO_WC_REG_SERDESDIGITAL_MISC2, 0x6190},
3740 		/* Disable Autoneg: re-enable it after adv is done. */
3741 		{MDIO_AN_DEVAD, MDIO_WC_REG_IEEE0BLK_MIICNTL, 0},
3742 		{MDIO_PMA_DEVAD, MDIO_WC_REG_PMD_KR_CONTROL, 0x2},
3743 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL72_USERB0_CL72_TX_FIR_TAP, 0},
3744 	};
3745 	DP(NETIF_MSG_LINK, "Enable Auto Negotiation for KR\n");
3746 	/* Set to default registers that may be overriden by 10G force */
3747 	for (i = 0; i < ARRAY_SIZE(reg_set); i++)
3748 		bnx2x_cl45_write(bp, phy, reg_set[i].devad, reg_set[i].reg,
3749 				 reg_set[i].val);
3750 
3751 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
3752 			MDIO_WC_REG_CL72_USERB0_CL72_MISC1_CONTROL, &cl72_ctrl);
3753 	cl72_ctrl &= 0x08ff;
3754 	cl72_ctrl |= 0x3800;
3755 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3756 			 MDIO_WC_REG_CL72_USERB0_CL72_MISC1_CONTROL, cl72_ctrl);
3757 
3758 	/* Check adding advertisement for 1G KX */
3759 	if (((vars->line_speed == SPEED_AUTO_NEG) &&
3760 	     (phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) ||
3761 	    (vars->line_speed == SPEED_1000)) {
3762 		u16 addr = MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2;
3763 		an_adv |= (1<<5);
3764 
3765 		/* Enable CL37 1G Parallel Detect */
3766 		bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD, addr, 0x1);
3767 		DP(NETIF_MSG_LINK, "Advertize 1G\n");
3768 	}
3769 	if (((vars->line_speed == SPEED_AUTO_NEG) &&
3770 	     (phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) ||
3771 	    (vars->line_speed ==  SPEED_10000)) {
3772 		/* Check adding advertisement for 10G KR */
3773 		an_adv |= (1<<7);
3774 		/* Enable 10G Parallel Detect */
3775 		CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
3776 				  MDIO_AER_BLOCK_AER_REG, 0);
3777 
3778 		bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
3779 				 MDIO_WC_REG_PAR_DET_10G_CTRL, 1);
3780 		bnx2x_set_aer_mmd(params, phy);
3781 		DP(NETIF_MSG_LINK, "Advertize 10G\n");
3782 	}
3783 
3784 	/* Set Transmit PMD settings */
3785 	lane = bnx2x_get_warpcore_lane(phy, params);
3786 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3787 			 MDIO_WC_REG_TX0_TX_DRIVER + 0x10*lane,
3788 			 WC_TX_DRIVER(0x02, 0x06, 0x09, 0));
3789 	/* Configure the next lane if dual mode */
3790 	if (phy->flags & FLAGS_WC_DUAL_MODE)
3791 		bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3792 				 MDIO_WC_REG_TX0_TX_DRIVER + 0x10*(lane+1),
3793 				 WC_TX_DRIVER(0x02, 0x06, 0x09, 0));
3794 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3795 			 MDIO_WC_REG_CL72_USERB0_CL72_OS_DEF_CTRL,
3796 			 0x03f0);
3797 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3798 			 MDIO_WC_REG_CL72_USERB0_CL72_2P5_DEF_CTRL,
3799 			 0x03f0);
3800 
3801 	/* Advertised speeds */
3802 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
3803 			 MDIO_WC_REG_AN_IEEE1BLK_AN_ADVERTISEMENT1, an_adv);
3804 
3805 	/* Advertised and set FEC (Forward Error Correction) */
3806 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
3807 			 MDIO_WC_REG_AN_IEEE1BLK_AN_ADVERTISEMENT2,
3808 			 (MDIO_WC_REG_AN_IEEE1BLK_AN_ADV2_FEC_ABILITY |
3809 			  MDIO_WC_REG_AN_IEEE1BLK_AN_ADV2_FEC_REQ));
3810 
3811 	/* Enable CL37 BAM */
3812 	if (REG_RD(bp, params->shmem_base +
3813 		   offsetof(struct shmem_region, dev_info.
3814 			    port_hw_config[params->port].default_cfg)) &
3815 	    PORT_HW_CFG_ENABLE_BAM_ON_KR_ENABLED) {
3816 		bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3817 					 MDIO_WC_REG_DIGITAL6_MP5_NEXTPAGECTRL,
3818 					 1);
3819 		DP(NETIF_MSG_LINK, "Enable CL37 BAM on KR\n");
3820 	}
3821 
3822 	/* Advertise pause */
3823 	bnx2x_ext_phy_set_pause(params, phy, vars);
3824 	vars->rx_tx_asic_rst = MAX_KR_LINK_RETRY;
3825 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3826 				 MDIO_WC_REG_DIGITAL5_MISC7, 0x100);
3827 
3828 	/* Over 1G - AN local device user page 1 */
3829 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3830 			MDIO_WC_REG_DIGITAL3_UP1, 0x1f);
3831 
3832 	if (((phy->req_line_speed == SPEED_AUTO_NEG) &&
3833 	     (phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) ||
3834 	    (phy->req_line_speed == SPEED_20000)) {
3835 
3836 		CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
3837 				  MDIO_AER_BLOCK_AER_REG, lane);
3838 
3839 		bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3840 					 MDIO_WC_REG_RX1_PCI_CTRL + (0x10*lane),
3841 					 (1<<11));
3842 
3843 		bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3844 				 MDIO_WC_REG_XGXS_X2_CONTROL3, 0x7);
3845 		bnx2x_set_aer_mmd(params, phy);
3846 
3847 		bnx2x_warpcore_enable_AN_KR2(phy, params, vars);
3848 	} else {
3849 		/* Enable Auto-Detect to support 1G over CL37 as well */
3850 		bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3851 				 MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X1, 0x10);
3852 		wc_lane_config = REG_RD(bp, params->shmem_base +
3853 					offsetof(struct shmem_region, dev_info.
3854 					shared_hw_config.wc_lane_config));
3855 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
3856 				MDIO_WC_REG_RX0_PCI_CTRL + (lane << 4), &val);
3857 		/* Force cl48 sync_status LOW to avoid getting stuck in CL73
3858 		 * parallel-detect loop when CL73 and CL37 are enabled.
3859 		 */
3860 		val |= 1 << 11;
3861 
3862 		/* Restore Polarity settings in case it was run over by
3863 		 * previous link owner
3864 		 */
3865 		if (wc_lane_config &
3866 		    (SHARED_HW_CFG_RX_LANE0_POL_FLIP_ENABLED << lane))
3867 			val |= 3 << 2;
3868 		else
3869 			val &= ~(3 << 2);
3870 		bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3871 				 MDIO_WC_REG_RX0_PCI_CTRL + (lane << 4),
3872 				 val);
3873 
3874 		bnx2x_disable_kr2(params, vars, phy);
3875 	}
3876 
3877 	/* Enable Autoneg: only on the main lane */
3878 	bnx2x_warpcore_restart_AN_KR(phy, params);
3879 }
3880 
3881 static void bnx2x_warpcore_set_10G_KR(struct bnx2x_phy *phy,
3882 				      struct link_params *params,
3883 				      struct link_vars *vars)
3884 {
3885 	struct bnx2x *bp = params->bp;
3886 	u16 val16, i, lane;
3887 	static struct bnx2x_reg_set reg_set[] = {
3888 		/* Disable Autoneg */
3889 		{MDIO_WC_DEVAD, MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2, 0x7},
3890 		{MDIO_WC_DEVAD, MDIO_WC_REG_CL72_USERB0_CL72_MISC1_CONTROL,
3891 			0x3f00},
3892 		{MDIO_AN_DEVAD, MDIO_WC_REG_AN_IEEE1BLK_AN_ADVERTISEMENT1, 0},
3893 		{MDIO_AN_DEVAD, MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x0},
3894 		{MDIO_WC_DEVAD, MDIO_WC_REG_DIGITAL3_UP1, 0x1},
3895 		{MDIO_WC_DEVAD, MDIO_WC_REG_DIGITAL5_MISC7, 0xa},
3896 		/* Leave cl72 training enable, needed for KR */
3897 		{MDIO_PMA_DEVAD, MDIO_WC_REG_PMD_KR_CONTROL, 0x2}
3898 	};
3899 
3900 	for (i = 0; i < ARRAY_SIZE(reg_set); i++)
3901 		bnx2x_cl45_write(bp, phy, reg_set[i].devad, reg_set[i].reg,
3902 				 reg_set[i].val);
3903 
3904 	lane = bnx2x_get_warpcore_lane(phy, params);
3905 	/* Global registers */
3906 	CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
3907 			  MDIO_AER_BLOCK_AER_REG, 0);
3908 	/* Disable CL36 PCS Tx */
3909 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
3910 			MDIO_WC_REG_XGXSBLK1_LANECTRL0, &val16);
3911 	val16 &= ~(0x0011 << lane);
3912 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3913 			 MDIO_WC_REG_XGXSBLK1_LANECTRL0, val16);
3914 
3915 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
3916 			MDIO_WC_REG_XGXSBLK1_LANECTRL1, &val16);
3917 	val16 |= (0x0303 << (lane << 1));
3918 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3919 			 MDIO_WC_REG_XGXSBLK1_LANECTRL1, val16);
3920 	/* Restore AER */
3921 	bnx2x_set_aer_mmd(params, phy);
3922 	/* Set speed via PMA/PMD register */
3923 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD,
3924 			 MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x2040);
3925 
3926 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD,
3927 			 MDIO_WC_REG_IEEE0BLK_AUTONEGNP, 0xB);
3928 
3929 	/* Enable encoded forced speed */
3930 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3931 			 MDIO_WC_REG_SERDESDIGITAL_MISC2, 0x30);
3932 
3933 	/* Turn TX scramble payload only the 64/66 scrambler */
3934 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3935 			 MDIO_WC_REG_TX66_CONTROL, 0x9);
3936 
3937 	/* Turn RX scramble payload only the 64/66 scrambler */
3938 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3939 				 MDIO_WC_REG_RX66_CONTROL, 0xF9);
3940 
3941 	/* Set and clear loopback to cause a reset to 64/66 decoder */
3942 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3943 			 MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x4000);
3944 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3945 			 MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x0);
3946 
3947 }
3948 
3949 static void bnx2x_warpcore_set_10G_XFI(struct bnx2x_phy *phy,
3950 				       struct link_params *params,
3951 				       u8 is_xfi)
3952 {
3953 	struct bnx2x *bp = params->bp;
3954 	u16 misc1_val, tap_val, tx_driver_val, lane, val;
3955 	u32 cfg_tap_val, tx_drv_brdct, tx_equal;
3956 	u32 ifir_val, ipost2_val, ipre_driver_val;
3957 
3958 	/* Hold rxSeqStart */
3959 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3960 				 MDIO_WC_REG_DSC2B0_DSC_MISC_CTRL0, 0x8000);
3961 
3962 	/* Hold tx_fifo_reset */
3963 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3964 				 MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X3, 0x1);
3965 
3966 	/* Disable CL73 AN */
3967 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, 0);
3968 
3969 	/* Disable 100FX Enable and Auto-Detect */
3970 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
3971 				  MDIO_WC_REG_FX100_CTRL1, 0xFFFA);
3972 
3973 	/* Disable 100FX Idle detect */
3974 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
3975 				 MDIO_WC_REG_FX100_CTRL3, 0x0080);
3976 
3977 	/* Set Block address to Remote PHY & Clear forced_speed[5] */
3978 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
3979 				  MDIO_WC_REG_DIGITAL4_MISC3, 0xFF7F);
3980 
3981 	/* Turn off auto-detect & fiber mode */
3982 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
3983 				  MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X1,
3984 				  0xFFEE);
3985 
3986 	/* Set filter_force_link, disable_false_link and parallel_detect */
3987 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
3988 			MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2, &val);
3989 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
3990 			 MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2,
3991 			 ((val | 0x0006) & 0xFFFE));
3992 
3993 	/* Set XFI / SFI */
3994 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
3995 			MDIO_WC_REG_SERDESDIGITAL_MISC1, &misc1_val);
3996 
3997 	misc1_val &= ~(0x1f);
3998 
3999 	if (is_xfi) {
4000 		misc1_val |= 0x5;
4001 		tap_val = WC_TX_FIR(0x08, 0x37, 0x00);
4002 		tx_driver_val = WC_TX_DRIVER(0x00, 0x02, 0x03, 0);
4003 	} else {
4004 		cfg_tap_val = REG_RD(bp, params->shmem_base +
4005 				     offsetof(struct shmem_region, dev_info.
4006 					      port_hw_config[params->port].
4007 					      sfi_tap_values));
4008 
4009 		tx_equal = cfg_tap_val & PORT_HW_CFG_TX_EQUALIZATION_MASK;
4010 
4011 		misc1_val |= 0x9;
4012 
4013 		/* TAP values are controlled by nvram, if value there isn't 0 */
4014 		if (tx_equal)
4015 			tap_val = (u16)tx_equal;
4016 		else
4017 			tap_val = WC_TX_FIR(0x0f, 0x2b, 0x02);
4018 
4019 		ifir_val = DEFAULT_TX_DRV_IFIR;
4020 		ipost2_val = DEFAULT_TX_DRV_POST2;
4021 		ipre_driver_val = DEFAULT_TX_DRV_IPRE_DRIVER;
4022 		tx_drv_brdct = DEFAULT_TX_DRV_BRDCT;
4023 
4024 		/* If any of the IFIR/IPRE_DRIVER/POST@ is set, apply all
4025 		 * configuration.
4026 		 */
4027 		if (cfg_tap_val & (PORT_HW_CFG_TX_DRV_IFIR_MASK |
4028 				   PORT_HW_CFG_TX_DRV_IPREDRIVER_MASK |
4029 				   PORT_HW_CFG_TX_DRV_POST2_MASK)) {
4030 			ifir_val = (cfg_tap_val &
4031 				    PORT_HW_CFG_TX_DRV_IFIR_MASK) >>
4032 				PORT_HW_CFG_TX_DRV_IFIR_SHIFT;
4033 			ipre_driver_val = (cfg_tap_val &
4034 					   PORT_HW_CFG_TX_DRV_IPREDRIVER_MASK)
4035 			>> PORT_HW_CFG_TX_DRV_IPREDRIVER_SHIFT;
4036 			ipost2_val = (cfg_tap_val &
4037 				      PORT_HW_CFG_TX_DRV_POST2_MASK) >>
4038 				PORT_HW_CFG_TX_DRV_POST2_SHIFT;
4039 		}
4040 
4041 		if (cfg_tap_val & PORT_HW_CFG_TX_DRV_BROADCAST_MASK) {
4042 			tx_drv_brdct = (cfg_tap_val &
4043 					PORT_HW_CFG_TX_DRV_BROADCAST_MASK) >>
4044 				PORT_HW_CFG_TX_DRV_BROADCAST_SHIFT;
4045 		}
4046 
4047 		tx_driver_val = WC_TX_DRIVER(ipost2_val, tx_drv_brdct,
4048 					     ipre_driver_val, ifir_val);
4049 	}
4050 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4051 			 MDIO_WC_REG_SERDESDIGITAL_MISC1, misc1_val);
4052 
4053 	/* Set Transmit PMD settings */
4054 	lane = bnx2x_get_warpcore_lane(phy, params);
4055 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4056 			 MDIO_WC_REG_TX_FIR_TAP,
4057 			 tap_val | MDIO_WC_REG_TX_FIR_TAP_ENABLE);
4058 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4059 			 MDIO_WC_REG_TX0_TX_DRIVER + 0x10*lane,
4060 			 tx_driver_val);
4061 
4062 	/* Enable fiber mode, enable and invert sig_det */
4063 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4064 				 MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X1, 0xd);
4065 
4066 	/* Set Block address to Remote PHY & Set forced_speed[5], 40bit mode */
4067 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4068 				 MDIO_WC_REG_DIGITAL4_MISC3, 0x8080);
4069 
4070 	bnx2x_warpcore_set_lpi_passthrough(phy, params);
4071 
4072 	/* 10G XFI Full Duplex */
4073 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4074 			 MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x100);
4075 
4076 	/* Release tx_fifo_reset */
4077 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4078 				  MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X3,
4079 				  0xFFFE);
4080 	/* Release rxSeqStart */
4081 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4082 				  MDIO_WC_REG_DSC2B0_DSC_MISC_CTRL0, 0x7FFF);
4083 }
4084 
4085 static void bnx2x_warpcore_set_20G_force_KR2(struct bnx2x_phy *phy,
4086 					     struct link_params *params)
4087 {
4088 	u16 val;
4089 	struct bnx2x *bp = params->bp;
4090 	/* Set global registers, so set AER lane to 0 */
4091 	CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
4092 			  MDIO_AER_BLOCK_AER_REG, 0);
4093 
4094 	/* Disable sequencer */
4095 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4096 				  MDIO_WC_REG_XGXSBLK0_XGXSCONTROL, ~(1<<13));
4097 
4098 	bnx2x_set_aer_mmd(params, phy);
4099 
4100 	bnx2x_cl45_read_and_write(bp, phy, MDIO_PMA_DEVAD,
4101 				  MDIO_WC_REG_PMD_KR_CONTROL, ~(1<<1));
4102 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
4103 			 MDIO_AN_REG_CTRL, 0);
4104 	/* Turn off CL73 */
4105 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4106 			MDIO_WC_REG_CL73_USERB0_CTRL, &val);
4107 	val &= ~(1<<5);
4108 	val |= (1<<6);
4109 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4110 			 MDIO_WC_REG_CL73_USERB0_CTRL, val);
4111 
4112 	/* Set 20G KR2 force speed */
4113 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4114 				 MDIO_WC_REG_SERDESDIGITAL_MISC1, 0x1f);
4115 
4116 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4117 				 MDIO_WC_REG_DIGITAL4_MISC3, (1<<7));
4118 
4119 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4120 			MDIO_WC_REG_CL72_USERB0_CL72_MISC1_CONTROL, &val);
4121 	val &= ~(3<<14);
4122 	val |= (1<<15);
4123 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4124 			 MDIO_WC_REG_CL72_USERB0_CL72_MISC1_CONTROL, val);
4125 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4126 			 MDIO_WC_REG_CL72_USERB0_CL72_TX_FIR_TAP, 0x835A);
4127 
4128 	/* Enable sequencer (over lane 0) */
4129 	CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
4130 			  MDIO_AER_BLOCK_AER_REG, 0);
4131 
4132 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4133 				 MDIO_WC_REG_XGXSBLK0_XGXSCONTROL, (1<<13));
4134 
4135 	bnx2x_set_aer_mmd(params, phy);
4136 }
4137 
4138 static void bnx2x_warpcore_set_20G_DXGXS(struct bnx2x *bp,
4139 					 struct bnx2x_phy *phy,
4140 					 u16 lane)
4141 {
4142 	/* Rx0 anaRxControl1G */
4143 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4144 			 MDIO_WC_REG_RX0_ANARXCONTROL1G, 0x90);
4145 
4146 	/* Rx2 anaRxControl1G */
4147 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4148 			 MDIO_WC_REG_RX2_ANARXCONTROL1G, 0x90);
4149 
4150 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4151 			 MDIO_WC_REG_RX66_SCW0, 0xE070);
4152 
4153 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4154 			 MDIO_WC_REG_RX66_SCW1, 0xC0D0);
4155 
4156 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4157 			 MDIO_WC_REG_RX66_SCW2, 0xA0B0);
4158 
4159 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4160 			 MDIO_WC_REG_RX66_SCW3, 0x8090);
4161 
4162 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4163 			 MDIO_WC_REG_RX66_SCW0_MASK, 0xF0F0);
4164 
4165 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4166 			 MDIO_WC_REG_RX66_SCW1_MASK, 0xF0F0);
4167 
4168 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4169 			 MDIO_WC_REG_RX66_SCW2_MASK, 0xF0F0);
4170 
4171 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4172 			 MDIO_WC_REG_RX66_SCW3_MASK, 0xF0F0);
4173 
4174 	/* Serdes Digital Misc1 */
4175 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4176 			 MDIO_WC_REG_SERDESDIGITAL_MISC1, 0x6008);
4177 
4178 	/* Serdes Digital4 Misc3 */
4179 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4180 			 MDIO_WC_REG_DIGITAL4_MISC3, 0x8088);
4181 
4182 	/* Set Transmit PMD settings */
4183 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4184 			 MDIO_WC_REG_TX_FIR_TAP,
4185 			 (WC_TX_FIR(0x12, 0x2d, 0x00) |
4186 			  MDIO_WC_REG_TX_FIR_TAP_ENABLE));
4187 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4188 			 MDIO_WC_REG_TX0_TX_DRIVER + 0x10*lane,
4189 			 WC_TX_DRIVER(0x02, 0x02, 0x02, 0));
4190 }
4191 
4192 static void bnx2x_warpcore_set_sgmii_speed(struct bnx2x_phy *phy,
4193 					   struct link_params *params,
4194 					   u8 fiber_mode,
4195 					   u8 always_autoneg)
4196 {
4197 	struct bnx2x *bp = params->bp;
4198 	u16 val16, digctrl_kx1, digctrl_kx2;
4199 
4200 	/* Clear XFI clock comp in non-10G single lane mode. */
4201 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4202 				  MDIO_WC_REG_RX66_CONTROL, ~(3<<13));
4203 
4204 	bnx2x_warpcore_set_lpi_passthrough(phy, params);
4205 
4206 	if (always_autoneg || phy->req_line_speed == SPEED_AUTO_NEG) {
4207 		/* SGMII Autoneg */
4208 		bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4209 					 MDIO_WC_REG_COMBO_IEEE0_MIICTRL,
4210 					 0x1000);
4211 		DP(NETIF_MSG_LINK, "set SGMII AUTONEG\n");
4212 	} else {
4213 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4214 				MDIO_WC_REG_COMBO_IEEE0_MIICTRL, &val16);
4215 		val16 &= 0xcebf;
4216 		switch (phy->req_line_speed) {
4217 		case SPEED_10:
4218 			break;
4219 		case SPEED_100:
4220 			val16 |= 0x2000;
4221 			break;
4222 		case SPEED_1000:
4223 			val16 |= 0x0040;
4224 			break;
4225 		default:
4226 			DP(NETIF_MSG_LINK,
4227 			   "Speed not supported: 0x%x\n", phy->req_line_speed);
4228 			return;
4229 		}
4230 
4231 		if (phy->req_duplex == DUPLEX_FULL)
4232 			val16 |= 0x0100;
4233 
4234 		bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4235 				MDIO_WC_REG_COMBO_IEEE0_MIICTRL, val16);
4236 
4237 		DP(NETIF_MSG_LINK, "set SGMII force speed %d\n",
4238 			       phy->req_line_speed);
4239 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4240 				MDIO_WC_REG_COMBO_IEEE0_MIICTRL, &val16);
4241 		DP(NETIF_MSG_LINK, "  (readback) %x\n", val16);
4242 	}
4243 
4244 	/* SGMII Slave mode and disable signal detect */
4245 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4246 			MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X1, &digctrl_kx1);
4247 	if (fiber_mode)
4248 		digctrl_kx1 = 1;
4249 	else
4250 		digctrl_kx1 &= 0xff4a;
4251 
4252 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4253 			MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X1,
4254 			digctrl_kx1);
4255 
4256 	/* Turn off parallel detect */
4257 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4258 			MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2, &digctrl_kx2);
4259 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4260 			MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2,
4261 			(digctrl_kx2 & ~(1<<2)));
4262 
4263 	/* Re-enable parallel detect */
4264 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4265 			MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2,
4266 			(digctrl_kx2 | (1<<2)));
4267 
4268 	/* Enable autodet */
4269 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4270 			MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X1,
4271 			(digctrl_kx1 | 0x10));
4272 }
4273 
4274 static void bnx2x_warpcore_reset_lane(struct bnx2x *bp,
4275 				      struct bnx2x_phy *phy,
4276 				      u8 reset)
4277 {
4278 	u16 val;
4279 	/* Take lane out of reset after configuration is finished */
4280 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4281 			MDIO_WC_REG_DIGITAL5_MISC6, &val);
4282 	if (reset)
4283 		val |= 0xC000;
4284 	else
4285 		val &= 0x3FFF;
4286 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4287 			 MDIO_WC_REG_DIGITAL5_MISC6, val);
4288 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4289 			 MDIO_WC_REG_DIGITAL5_MISC6, &val);
4290 }
4291 /* Clear SFI/XFI link settings registers */
4292 static void bnx2x_warpcore_clear_regs(struct bnx2x_phy *phy,
4293 				      struct link_params *params,
4294 				      u16 lane)
4295 {
4296 	struct bnx2x *bp = params->bp;
4297 	u16 i;
4298 	static struct bnx2x_reg_set wc_regs[] = {
4299 		{MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, 0},
4300 		{MDIO_WC_DEVAD, MDIO_WC_REG_FX100_CTRL1, 0x014a},
4301 		{MDIO_WC_DEVAD, MDIO_WC_REG_FX100_CTRL3, 0x0800},
4302 		{MDIO_WC_DEVAD, MDIO_WC_REG_DIGITAL4_MISC3, 0x8008},
4303 		{MDIO_WC_DEVAD, MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X1,
4304 			0x0195},
4305 		{MDIO_WC_DEVAD, MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X2,
4306 			0x0007},
4307 		{MDIO_WC_DEVAD, MDIO_WC_REG_SERDESDIGITAL_CONTROL1000X3,
4308 			0x0002},
4309 		{MDIO_WC_DEVAD, MDIO_WC_REG_SERDESDIGITAL_MISC1, 0x6000},
4310 		{MDIO_WC_DEVAD, MDIO_WC_REG_TX_FIR_TAP, 0x0000},
4311 		{MDIO_WC_DEVAD, MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x2040},
4312 		{MDIO_WC_DEVAD, MDIO_WC_REG_COMBO_IEEE0_MIICTRL, 0x0140}
4313 	};
4314 	/* Set XFI clock comp as default. */
4315 	bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4316 				 MDIO_WC_REG_RX66_CONTROL, (3<<13));
4317 
4318 	for (i = 0; i < ARRAY_SIZE(wc_regs); i++)
4319 		bnx2x_cl45_write(bp, phy, wc_regs[i].devad, wc_regs[i].reg,
4320 				 wc_regs[i].val);
4321 
4322 	lane = bnx2x_get_warpcore_lane(phy, params);
4323 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4324 			 MDIO_WC_REG_TX0_TX_DRIVER + 0x10*lane, 0x0990);
4325 
4326 }
4327 
4328 static int bnx2x_get_mod_abs_int_cfg(struct bnx2x *bp,
4329 						u32 chip_id,
4330 						u32 shmem_base, u8 port,
4331 						u8 *gpio_num, u8 *gpio_port)
4332 {
4333 	u32 cfg_pin;
4334 	*gpio_num = 0;
4335 	*gpio_port = 0;
4336 	if (CHIP_IS_E3(bp)) {
4337 		cfg_pin = (REG_RD(bp, shmem_base +
4338 				offsetof(struct shmem_region,
4339 				dev_info.port_hw_config[port].e3_sfp_ctrl)) &
4340 				PORT_HW_CFG_E3_MOD_ABS_MASK) >>
4341 				PORT_HW_CFG_E3_MOD_ABS_SHIFT;
4342 
4343 		/* Should not happen. This function called upon interrupt
4344 		 * triggered by GPIO ( since EPIO can only generate interrupts
4345 		 * to MCP).
4346 		 * So if this function was called and none of the GPIOs was set,
4347 		 * it means the shit hit the fan.
4348 		 */
4349 		if ((cfg_pin < PIN_CFG_GPIO0_P0) ||
4350 		    (cfg_pin > PIN_CFG_GPIO3_P1)) {
4351 			DP(NETIF_MSG_LINK,
4352 			   "No cfg pin %x for module detect indication\n",
4353 			   cfg_pin);
4354 			return -EINVAL;
4355 		}
4356 
4357 		*gpio_num = (cfg_pin - PIN_CFG_GPIO0_P0) & 0x3;
4358 		*gpio_port = (cfg_pin - PIN_CFG_GPIO0_P0) >> 2;
4359 	} else {
4360 		*gpio_num = MISC_REGISTERS_GPIO_3;
4361 		*gpio_port = port;
4362 	}
4363 
4364 	return 0;
4365 }
4366 
4367 static int bnx2x_is_sfp_module_plugged(struct bnx2x_phy *phy,
4368 				       struct link_params *params)
4369 {
4370 	struct bnx2x *bp = params->bp;
4371 	u8 gpio_num, gpio_port;
4372 	u32 gpio_val;
4373 	if (bnx2x_get_mod_abs_int_cfg(bp, params->chip_id,
4374 				      params->shmem_base, params->port,
4375 				      &gpio_num, &gpio_port) != 0)
4376 		return 0;
4377 	gpio_val = bnx2x_get_gpio(bp, gpio_num, gpio_port);
4378 
4379 	/* Call the handling function in case module is detected */
4380 	if (gpio_val == 0)
4381 		return 1;
4382 	else
4383 		return 0;
4384 }
4385 static int bnx2x_warpcore_get_sigdet(struct bnx2x_phy *phy,
4386 				     struct link_params *params)
4387 {
4388 	u16 gp2_status_reg0, lane;
4389 	struct bnx2x *bp = params->bp;
4390 
4391 	lane = bnx2x_get_warpcore_lane(phy, params);
4392 
4393 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD, MDIO_WC_REG_GP2_STATUS_GP_2_0,
4394 				 &gp2_status_reg0);
4395 
4396 	return (gp2_status_reg0 >> (8+lane)) & 0x1;
4397 }
4398 
4399 static void bnx2x_warpcore_config_runtime(struct bnx2x_phy *phy,
4400 					  struct link_params *params,
4401 					  struct link_vars *vars)
4402 {
4403 	struct bnx2x *bp = params->bp;
4404 	u32 serdes_net_if;
4405 	u16 gp_status1 = 0, lnkup = 0, lnkup_kr = 0;
4406 
4407 	vars->turn_to_run_wc_rt = vars->turn_to_run_wc_rt ? 0 : 1;
4408 
4409 	if (!vars->turn_to_run_wc_rt)
4410 		return;
4411 
4412 	if (vars->rx_tx_asic_rst) {
4413 		u16 lane = bnx2x_get_warpcore_lane(phy, params);
4414 		serdes_net_if = (REG_RD(bp, params->shmem_base +
4415 				offsetof(struct shmem_region, dev_info.
4416 				port_hw_config[params->port].default_cfg)) &
4417 				PORT_HW_CFG_NET_SERDES_IF_MASK);
4418 
4419 		switch (serdes_net_if) {
4420 		case PORT_HW_CFG_NET_SERDES_IF_KR:
4421 			/* Do we get link yet? */
4422 			bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD, 0x81d1,
4423 					&gp_status1);
4424 			lnkup = (gp_status1 >> (8+lane)) & 0x1;/* 1G */
4425 				/*10G KR*/
4426 			lnkup_kr = (gp_status1 >> (12+lane)) & 0x1;
4427 
4428 			if (lnkup_kr || lnkup) {
4429 				vars->rx_tx_asic_rst = 0;
4430 			} else {
4431 				/* Reset the lane to see if link comes up.*/
4432 				bnx2x_warpcore_reset_lane(bp, phy, 1);
4433 				bnx2x_warpcore_reset_lane(bp, phy, 0);
4434 
4435 				/* Restart Autoneg */
4436 				bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
4437 					MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x1200);
4438 
4439 				vars->rx_tx_asic_rst--;
4440 				DP(NETIF_MSG_LINK, "0x%x retry left\n",
4441 				vars->rx_tx_asic_rst);
4442 			}
4443 			break;
4444 
4445 		default:
4446 			break;
4447 		}
4448 
4449 	} /*params->rx_tx_asic_rst*/
4450 
4451 }
4452 static void bnx2x_warpcore_config_sfi(struct bnx2x_phy *phy,
4453 				      struct link_params *params)
4454 {
4455 	u16 lane = bnx2x_get_warpcore_lane(phy, params);
4456 	struct bnx2x *bp = params->bp;
4457 	bnx2x_warpcore_clear_regs(phy, params, lane);
4458 	if ((params->req_line_speed[LINK_CONFIG_IDX(INT_PHY)] ==
4459 	     SPEED_10000) &&
4460 	    (phy->media_type != ETH_PHY_SFP_1G_FIBER)) {
4461 		DP(NETIF_MSG_LINK, "Setting 10G SFI\n");
4462 		bnx2x_warpcore_set_10G_XFI(phy, params, 0);
4463 	} else {
4464 		DP(NETIF_MSG_LINK, "Setting 1G Fiber\n");
4465 		bnx2x_warpcore_set_sgmii_speed(phy, params, 1, 0);
4466 	}
4467 }
4468 
4469 static void bnx2x_sfp_e3_set_transmitter(struct link_params *params,
4470 					 struct bnx2x_phy *phy,
4471 					 u8 tx_en)
4472 {
4473 	struct bnx2x *bp = params->bp;
4474 	u32 cfg_pin;
4475 	u8 port = params->port;
4476 
4477 	cfg_pin = REG_RD(bp, params->shmem_base +
4478 			 offsetof(struct shmem_region,
4479 				  dev_info.port_hw_config[port].e3_sfp_ctrl)) &
4480 		PORT_HW_CFG_E3_TX_LASER_MASK;
4481 	/* Set the !tx_en since this pin is DISABLE_TX_LASER */
4482 	DP(NETIF_MSG_LINK, "Setting WC TX to %d\n", tx_en);
4483 
4484 	/* For 20G, the expected pin to be used is 3 pins after the current */
4485 	bnx2x_set_cfg_pin(bp, cfg_pin, tx_en ^ 1);
4486 	if (phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)
4487 		bnx2x_set_cfg_pin(bp, cfg_pin + 3, tx_en ^ 1);
4488 }
4489 
4490 static void bnx2x_warpcore_config_init(struct bnx2x_phy *phy,
4491 				       struct link_params *params,
4492 				       struct link_vars *vars)
4493 {
4494 	struct bnx2x *bp = params->bp;
4495 	u32 serdes_net_if;
4496 	u8 fiber_mode;
4497 	u16 lane = bnx2x_get_warpcore_lane(phy, params);
4498 	serdes_net_if = (REG_RD(bp, params->shmem_base +
4499 			 offsetof(struct shmem_region, dev_info.
4500 				  port_hw_config[params->port].default_cfg)) &
4501 			 PORT_HW_CFG_NET_SERDES_IF_MASK);
4502 	DP(NETIF_MSG_LINK, "Begin Warpcore init, link_speed %d, "
4503 			   "serdes_net_if = 0x%x\n",
4504 		       vars->line_speed, serdes_net_if);
4505 	bnx2x_set_aer_mmd(params, phy);
4506 	bnx2x_warpcore_reset_lane(bp, phy, 1);
4507 	vars->phy_flags |= PHY_XGXS_FLAG;
4508 	if ((serdes_net_if == PORT_HW_CFG_NET_SERDES_IF_SGMII) ||
4509 	    (phy->req_line_speed &&
4510 	     ((phy->req_line_speed == SPEED_100) ||
4511 	      (phy->req_line_speed == SPEED_10)))) {
4512 		vars->phy_flags |= PHY_SGMII_FLAG;
4513 		DP(NETIF_MSG_LINK, "Setting SGMII mode\n");
4514 		bnx2x_warpcore_clear_regs(phy, params, lane);
4515 		bnx2x_warpcore_set_sgmii_speed(phy, params, 0, 1);
4516 	} else {
4517 		switch (serdes_net_if) {
4518 		case PORT_HW_CFG_NET_SERDES_IF_KR:
4519 			/* Enable KR Auto Neg */
4520 			if (params->loopback_mode != LOOPBACK_EXT)
4521 				bnx2x_warpcore_enable_AN_KR(phy, params, vars);
4522 			else {
4523 				DP(NETIF_MSG_LINK, "Setting KR 10G-Force\n");
4524 				bnx2x_warpcore_set_10G_KR(phy, params, vars);
4525 			}
4526 			break;
4527 
4528 		case PORT_HW_CFG_NET_SERDES_IF_XFI:
4529 			bnx2x_warpcore_clear_regs(phy, params, lane);
4530 			if (vars->line_speed == SPEED_10000) {
4531 				DP(NETIF_MSG_LINK, "Setting 10G XFI\n");
4532 				bnx2x_warpcore_set_10G_XFI(phy, params, 1);
4533 			} else {
4534 				if (SINGLE_MEDIA_DIRECT(params)) {
4535 					DP(NETIF_MSG_LINK, "1G Fiber\n");
4536 					fiber_mode = 1;
4537 				} else {
4538 					DP(NETIF_MSG_LINK, "10/100/1G SGMII\n");
4539 					fiber_mode = 0;
4540 				}
4541 				bnx2x_warpcore_set_sgmii_speed(phy,
4542 								params,
4543 								fiber_mode,
4544 								0);
4545 			}
4546 
4547 			break;
4548 
4549 		case PORT_HW_CFG_NET_SERDES_IF_SFI:
4550 			/* Issue Module detection if module is plugged, or
4551 			 * enabled transmitter to avoid current leakage in case
4552 			 * no module is connected
4553 			 */
4554 			if ((params->loopback_mode == LOOPBACK_NONE) ||
4555 			    (params->loopback_mode == LOOPBACK_EXT)) {
4556 				if (bnx2x_is_sfp_module_plugged(phy, params))
4557 					bnx2x_sfp_module_detection(phy, params);
4558 				else
4559 					bnx2x_sfp_e3_set_transmitter(params,
4560 								     phy, 1);
4561 			}
4562 
4563 			bnx2x_warpcore_config_sfi(phy, params);
4564 			break;
4565 
4566 		case PORT_HW_CFG_NET_SERDES_IF_DXGXS:
4567 			if (vars->line_speed != SPEED_20000) {
4568 				DP(NETIF_MSG_LINK, "Speed not supported yet\n");
4569 				return;
4570 			}
4571 			DP(NETIF_MSG_LINK, "Setting 20G DXGXS\n");
4572 			bnx2x_warpcore_set_20G_DXGXS(bp, phy, lane);
4573 			/* Issue Module detection */
4574 
4575 			bnx2x_sfp_module_detection(phy, params);
4576 			break;
4577 		case PORT_HW_CFG_NET_SERDES_IF_KR2:
4578 			if (!params->loopback_mode) {
4579 				bnx2x_warpcore_enable_AN_KR(phy, params, vars);
4580 			} else {
4581 				DP(NETIF_MSG_LINK, "Setting KR 20G-Force\n");
4582 				bnx2x_warpcore_set_20G_force_KR2(phy, params);
4583 			}
4584 			break;
4585 		default:
4586 			DP(NETIF_MSG_LINK,
4587 			   "Unsupported Serdes Net Interface 0x%x\n",
4588 			   serdes_net_if);
4589 			return;
4590 		}
4591 	}
4592 
4593 	/* Take lane out of reset after configuration is finished */
4594 	bnx2x_warpcore_reset_lane(bp, phy, 0);
4595 	DP(NETIF_MSG_LINK, "Exit config init\n");
4596 }
4597 
4598 static void bnx2x_warpcore_link_reset(struct bnx2x_phy *phy,
4599 				      struct link_params *params)
4600 {
4601 	struct bnx2x *bp = params->bp;
4602 	u16 val16, lane;
4603 	bnx2x_sfp_e3_set_transmitter(params, phy, 0);
4604 	bnx2x_set_mdio_emac_per_phy(bp, params);
4605 	bnx2x_set_aer_mmd(params, phy);
4606 	/* Global register */
4607 	bnx2x_warpcore_reset_lane(bp, phy, 1);
4608 
4609 	/* Clear loopback settings (if any) */
4610 	/* 10G & 20G */
4611 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4612 				  MDIO_WC_REG_COMBO_IEEE0_MIICTRL, 0xBFFF);
4613 
4614 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4615 				  MDIO_WC_REG_IEEE0BLK_MIICNTL, 0xfffe);
4616 
4617 	/* Update those 1-copy registers */
4618 	CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
4619 			  MDIO_AER_BLOCK_AER_REG, 0);
4620 	/* Enable 1G MDIO (1-copy) */
4621 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4622 				  MDIO_WC_REG_XGXSBLK0_XGXSCONTROL,
4623 				  ~0x10);
4624 
4625 	bnx2x_cl45_read_and_write(bp, phy, MDIO_WC_DEVAD,
4626 				  MDIO_WC_REG_XGXSBLK1_LANECTRL2, 0xff00);
4627 	lane = bnx2x_get_warpcore_lane(phy, params);
4628 	/* Disable CL36 PCS Tx */
4629 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4630 			MDIO_WC_REG_XGXSBLK1_LANECTRL0, &val16);
4631 	val16 |= (0x11 << lane);
4632 	if (phy->flags & FLAGS_WC_DUAL_MODE)
4633 		val16 |= (0x22 << lane);
4634 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4635 			 MDIO_WC_REG_XGXSBLK1_LANECTRL0, val16);
4636 
4637 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4638 			MDIO_WC_REG_XGXSBLK1_LANECTRL1, &val16);
4639 	val16 &= ~(0x0303 << (lane << 1));
4640 	val16 |= (0x0101 << (lane << 1));
4641 	if (phy->flags & FLAGS_WC_DUAL_MODE) {
4642 		val16 &= ~(0x0c0c << (lane << 1));
4643 		val16 |= (0x0404 << (lane << 1));
4644 	}
4645 
4646 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4647 			 MDIO_WC_REG_XGXSBLK1_LANECTRL1, val16);
4648 	/* Restore AER */
4649 	bnx2x_set_aer_mmd(params, phy);
4650 
4651 }
4652 
4653 static void bnx2x_set_warpcore_loopback(struct bnx2x_phy *phy,
4654 					struct link_params *params)
4655 {
4656 	struct bnx2x *bp = params->bp;
4657 	u16 val16;
4658 	u32 lane;
4659 	DP(NETIF_MSG_LINK, "Setting Warpcore loopback type %x, speed %d\n",
4660 		       params->loopback_mode, phy->req_line_speed);
4661 
4662 	if (phy->req_line_speed < SPEED_10000 ||
4663 	    phy->supported & SUPPORTED_20000baseKR2_Full) {
4664 		/* 10/100/1000/20G-KR2 */
4665 
4666 		/* Update those 1-copy registers */
4667 		CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
4668 				  MDIO_AER_BLOCK_AER_REG, 0);
4669 		/* Enable 1G MDIO (1-copy) */
4670 		bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4671 					 MDIO_WC_REG_XGXSBLK0_XGXSCONTROL,
4672 					 0x10);
4673 		/* Set 1G loopback based on lane (1-copy) */
4674 		lane = bnx2x_get_warpcore_lane(phy, params);
4675 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
4676 				MDIO_WC_REG_XGXSBLK1_LANECTRL2, &val16);
4677 		val16 |= (1<<lane);
4678 		if (phy->flags & FLAGS_WC_DUAL_MODE)
4679 			val16 |= (2<<lane);
4680 		bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
4681 				 MDIO_WC_REG_XGXSBLK1_LANECTRL2,
4682 				 val16);
4683 
4684 		/* Switch back to 4-copy registers */
4685 		bnx2x_set_aer_mmd(params, phy);
4686 	} else {
4687 		/* 10G / 20G-DXGXS */
4688 		bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4689 					 MDIO_WC_REG_COMBO_IEEE0_MIICTRL,
4690 					 0x4000);
4691 		bnx2x_cl45_read_or_write(bp, phy, MDIO_WC_DEVAD,
4692 					 MDIO_WC_REG_IEEE0BLK_MIICNTL, 0x1);
4693 	}
4694 }
4695 
4696 
4697 
4698 static void bnx2x_sync_link(struct link_params *params,
4699 			     struct link_vars *vars)
4700 {
4701 	struct bnx2x *bp = params->bp;
4702 	u8 link_10g_plus;
4703 	if (vars->link_status & LINK_STATUS_PHYSICAL_LINK_FLAG)
4704 		vars->phy_flags |= PHY_PHYSICAL_LINK_FLAG;
4705 	vars->link_up = (vars->link_status & LINK_STATUS_LINK_UP);
4706 	if (vars->link_up) {
4707 		DP(NETIF_MSG_LINK, "phy link up\n");
4708 
4709 		vars->phy_link_up = 1;
4710 		vars->duplex = DUPLEX_FULL;
4711 		switch (vars->link_status &
4712 			LINK_STATUS_SPEED_AND_DUPLEX_MASK) {
4713 		case LINK_10THD:
4714 			vars->duplex = DUPLEX_HALF;
4715 			/* Fall thru */
4716 		case LINK_10TFD:
4717 			vars->line_speed = SPEED_10;
4718 			break;
4719 
4720 		case LINK_100TXHD:
4721 			vars->duplex = DUPLEX_HALF;
4722 			/* Fall thru */
4723 		case LINK_100T4:
4724 		case LINK_100TXFD:
4725 			vars->line_speed = SPEED_100;
4726 			break;
4727 
4728 		case LINK_1000THD:
4729 			vars->duplex = DUPLEX_HALF;
4730 			/* Fall thru */
4731 		case LINK_1000TFD:
4732 			vars->line_speed = SPEED_1000;
4733 			break;
4734 
4735 		case LINK_2500THD:
4736 			vars->duplex = DUPLEX_HALF;
4737 			/* Fall thru */
4738 		case LINK_2500TFD:
4739 			vars->line_speed = SPEED_2500;
4740 			break;
4741 
4742 		case LINK_10GTFD:
4743 			vars->line_speed = SPEED_10000;
4744 			break;
4745 		case LINK_20GTFD:
4746 			vars->line_speed = SPEED_20000;
4747 			break;
4748 		default:
4749 			break;
4750 		}
4751 		vars->flow_ctrl = 0;
4752 		if (vars->link_status & LINK_STATUS_TX_FLOW_CONTROL_ENABLED)
4753 			vars->flow_ctrl |= BNX2X_FLOW_CTRL_TX;
4754 
4755 		if (vars->link_status & LINK_STATUS_RX_FLOW_CONTROL_ENABLED)
4756 			vars->flow_ctrl |= BNX2X_FLOW_CTRL_RX;
4757 
4758 		if (!vars->flow_ctrl)
4759 			vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
4760 
4761 		if (vars->line_speed &&
4762 		    ((vars->line_speed == SPEED_10) ||
4763 		     (vars->line_speed == SPEED_100))) {
4764 			vars->phy_flags |= PHY_SGMII_FLAG;
4765 		} else {
4766 			vars->phy_flags &= ~PHY_SGMII_FLAG;
4767 		}
4768 		if (vars->line_speed &&
4769 		    USES_WARPCORE(bp) &&
4770 		    (vars->line_speed == SPEED_1000))
4771 			vars->phy_flags |= PHY_SGMII_FLAG;
4772 		/* Anything 10 and over uses the bmac */
4773 		link_10g_plus = (vars->line_speed >= SPEED_10000);
4774 
4775 		if (link_10g_plus) {
4776 			if (USES_WARPCORE(bp))
4777 				vars->mac_type = MAC_TYPE_XMAC;
4778 			else
4779 				vars->mac_type = MAC_TYPE_BMAC;
4780 		} else {
4781 			if (USES_WARPCORE(bp))
4782 				vars->mac_type = MAC_TYPE_UMAC;
4783 			else
4784 				vars->mac_type = MAC_TYPE_EMAC;
4785 		}
4786 	} else { /* Link down */
4787 		DP(NETIF_MSG_LINK, "phy link down\n");
4788 
4789 		vars->phy_link_up = 0;
4790 
4791 		vars->line_speed = 0;
4792 		vars->duplex = DUPLEX_FULL;
4793 		vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
4794 
4795 		/* Indicate no mac active */
4796 		vars->mac_type = MAC_TYPE_NONE;
4797 		if (vars->link_status & LINK_STATUS_PHYSICAL_LINK_FLAG)
4798 			vars->phy_flags |= PHY_HALF_OPEN_CONN_FLAG;
4799 		if (vars->link_status & LINK_STATUS_SFP_TX_FAULT)
4800 			vars->phy_flags |= PHY_SFP_TX_FAULT_FLAG;
4801 	}
4802 }
4803 
4804 void bnx2x_link_status_update(struct link_params *params,
4805 			      struct link_vars *vars)
4806 {
4807 	struct bnx2x *bp = params->bp;
4808 	u8 port = params->port;
4809 	u32 sync_offset, media_types;
4810 	/* Update PHY configuration */
4811 	set_phy_vars(params, vars);
4812 
4813 	vars->link_status = REG_RD(bp, params->shmem_base +
4814 				   offsetof(struct shmem_region,
4815 					    port_mb[port].link_status));
4816 
4817 	/* Force link UP in non LOOPBACK_EXT loopback mode(s) */
4818 	if (params->loopback_mode != LOOPBACK_NONE &&
4819 	    params->loopback_mode != LOOPBACK_EXT)
4820 		vars->link_status |= LINK_STATUS_LINK_UP;
4821 
4822 	if (bnx2x_eee_has_cap(params))
4823 		vars->eee_status = REG_RD(bp, params->shmem2_base +
4824 					  offsetof(struct shmem2_region,
4825 						   eee_status[params->port]));
4826 
4827 	vars->phy_flags = PHY_XGXS_FLAG;
4828 	bnx2x_sync_link(params, vars);
4829 	/* Sync media type */
4830 	sync_offset = params->shmem_base +
4831 			offsetof(struct shmem_region,
4832 				 dev_info.port_hw_config[port].media_type);
4833 	media_types = REG_RD(bp, sync_offset);
4834 
4835 	params->phy[INT_PHY].media_type =
4836 		(media_types & PORT_HW_CFG_MEDIA_TYPE_PHY0_MASK) >>
4837 		PORT_HW_CFG_MEDIA_TYPE_PHY0_SHIFT;
4838 	params->phy[EXT_PHY1].media_type =
4839 		(media_types & PORT_HW_CFG_MEDIA_TYPE_PHY1_MASK) >>
4840 		PORT_HW_CFG_MEDIA_TYPE_PHY1_SHIFT;
4841 	params->phy[EXT_PHY2].media_type =
4842 		(media_types & PORT_HW_CFG_MEDIA_TYPE_PHY2_MASK) >>
4843 		PORT_HW_CFG_MEDIA_TYPE_PHY2_SHIFT;
4844 	DP(NETIF_MSG_LINK, "media_types = 0x%x\n", media_types);
4845 
4846 	/* Sync AEU offset */
4847 	sync_offset = params->shmem_base +
4848 			offsetof(struct shmem_region,
4849 				 dev_info.port_hw_config[port].aeu_int_mask);
4850 
4851 	vars->aeu_int_mask = REG_RD(bp, sync_offset);
4852 
4853 	/* Sync PFC status */
4854 	if (vars->link_status & LINK_STATUS_PFC_ENABLED)
4855 		params->feature_config_flags |=
4856 					FEATURE_CONFIG_PFC_ENABLED;
4857 	else
4858 		params->feature_config_flags &=
4859 					~FEATURE_CONFIG_PFC_ENABLED;
4860 
4861 	if (SHMEM2_HAS(bp, link_attr_sync))
4862 		params->link_attr_sync = SHMEM2_RD(bp,
4863 						 link_attr_sync[params->port]);
4864 
4865 	DP(NETIF_MSG_LINK, "link_status 0x%x  phy_link_up %x int_mask 0x%x\n",
4866 		 vars->link_status, vars->phy_link_up, vars->aeu_int_mask);
4867 	DP(NETIF_MSG_LINK, "line_speed %x  duplex %x  flow_ctrl 0x%x\n",
4868 		 vars->line_speed, vars->duplex, vars->flow_ctrl);
4869 }
4870 
4871 static void bnx2x_set_master_ln(struct link_params *params,
4872 				struct bnx2x_phy *phy)
4873 {
4874 	struct bnx2x *bp = params->bp;
4875 	u16 new_master_ln, ser_lane;
4876 	ser_lane = ((params->lane_config &
4877 		     PORT_HW_CFG_LANE_SWAP_CFG_MASTER_MASK) >>
4878 		    PORT_HW_CFG_LANE_SWAP_CFG_MASTER_SHIFT);
4879 
4880 	/* Set the master_ln for AN */
4881 	CL22_RD_OVER_CL45(bp, phy,
4882 			  MDIO_REG_BANK_XGXS_BLOCK2,
4883 			  MDIO_XGXS_BLOCK2_TEST_MODE_LANE,
4884 			  &new_master_ln);
4885 
4886 	CL22_WR_OVER_CL45(bp, phy,
4887 			  MDIO_REG_BANK_XGXS_BLOCK2 ,
4888 			  MDIO_XGXS_BLOCK2_TEST_MODE_LANE,
4889 			  (new_master_ln | ser_lane));
4890 }
4891 
4892 static int bnx2x_reset_unicore(struct link_params *params,
4893 			       struct bnx2x_phy *phy,
4894 			       u8 set_serdes)
4895 {
4896 	struct bnx2x *bp = params->bp;
4897 	u16 mii_control;
4898 	u16 i;
4899 	CL22_RD_OVER_CL45(bp, phy,
4900 			  MDIO_REG_BANK_COMBO_IEEE0,
4901 			  MDIO_COMBO_IEEE0_MII_CONTROL, &mii_control);
4902 
4903 	/* Reset the unicore */
4904 	CL22_WR_OVER_CL45(bp, phy,
4905 			  MDIO_REG_BANK_COMBO_IEEE0,
4906 			  MDIO_COMBO_IEEE0_MII_CONTROL,
4907 			  (mii_control |
4908 			   MDIO_COMBO_IEEO_MII_CONTROL_RESET));
4909 	if (set_serdes)
4910 		bnx2x_set_serdes_access(bp, params->port);
4911 
4912 	/* Wait for the reset to self clear */
4913 	for (i = 0; i < MDIO_ACCESS_TIMEOUT; i++) {
4914 		udelay(5);
4915 
4916 		/* The reset erased the previous bank value */
4917 		CL22_RD_OVER_CL45(bp, phy,
4918 				  MDIO_REG_BANK_COMBO_IEEE0,
4919 				  MDIO_COMBO_IEEE0_MII_CONTROL,
4920 				  &mii_control);
4921 
4922 		if (!(mii_control & MDIO_COMBO_IEEO_MII_CONTROL_RESET)) {
4923 			udelay(5);
4924 			return 0;
4925 		}
4926 	}
4927 
4928 	netdev_err(bp->dev,  "Warning: PHY was not initialized,"
4929 			      " Port %d\n",
4930 			 params->port);
4931 	DP(NETIF_MSG_LINK, "BUG! XGXS is still in reset!\n");
4932 	return -EINVAL;
4933 
4934 }
4935 
4936 static void bnx2x_set_swap_lanes(struct link_params *params,
4937 				 struct bnx2x_phy *phy)
4938 {
4939 	struct bnx2x *bp = params->bp;
4940 	/* Each two bits represents a lane number:
4941 	 * No swap is 0123 => 0x1b no need to enable the swap
4942 	 */
4943 	u16 rx_lane_swap, tx_lane_swap;
4944 
4945 	rx_lane_swap = ((params->lane_config &
4946 			 PORT_HW_CFG_LANE_SWAP_CFG_RX_MASK) >>
4947 			PORT_HW_CFG_LANE_SWAP_CFG_RX_SHIFT);
4948 	tx_lane_swap = ((params->lane_config &
4949 			 PORT_HW_CFG_LANE_SWAP_CFG_TX_MASK) >>
4950 			PORT_HW_CFG_LANE_SWAP_CFG_TX_SHIFT);
4951 
4952 	if (rx_lane_swap != 0x1b) {
4953 		CL22_WR_OVER_CL45(bp, phy,
4954 				  MDIO_REG_BANK_XGXS_BLOCK2,
4955 				  MDIO_XGXS_BLOCK2_RX_LN_SWAP,
4956 				  (rx_lane_swap |
4957 				   MDIO_XGXS_BLOCK2_RX_LN_SWAP_ENABLE |
4958 				   MDIO_XGXS_BLOCK2_RX_LN_SWAP_FORCE_ENABLE));
4959 	} else {
4960 		CL22_WR_OVER_CL45(bp, phy,
4961 				  MDIO_REG_BANK_XGXS_BLOCK2,
4962 				  MDIO_XGXS_BLOCK2_RX_LN_SWAP, 0);
4963 	}
4964 
4965 	if (tx_lane_swap != 0x1b) {
4966 		CL22_WR_OVER_CL45(bp, phy,
4967 				  MDIO_REG_BANK_XGXS_BLOCK2,
4968 				  MDIO_XGXS_BLOCK2_TX_LN_SWAP,
4969 				  (tx_lane_swap |
4970 				   MDIO_XGXS_BLOCK2_TX_LN_SWAP_ENABLE));
4971 	} else {
4972 		CL22_WR_OVER_CL45(bp, phy,
4973 				  MDIO_REG_BANK_XGXS_BLOCK2,
4974 				  MDIO_XGXS_BLOCK2_TX_LN_SWAP, 0);
4975 	}
4976 }
4977 
4978 static void bnx2x_set_parallel_detection(struct bnx2x_phy *phy,
4979 					 struct link_params *params)
4980 {
4981 	struct bnx2x *bp = params->bp;
4982 	u16 control2;
4983 	CL22_RD_OVER_CL45(bp, phy,
4984 			  MDIO_REG_BANK_SERDES_DIGITAL,
4985 			  MDIO_SERDES_DIGITAL_A_1000X_CONTROL2,
4986 			  &control2);
4987 	if (phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)
4988 		control2 |= MDIO_SERDES_DIGITAL_A_1000X_CONTROL2_PRL_DT_EN;
4989 	else
4990 		control2 &= ~MDIO_SERDES_DIGITAL_A_1000X_CONTROL2_PRL_DT_EN;
4991 	DP(NETIF_MSG_LINK, "phy->speed_cap_mask = 0x%x, control2 = 0x%x\n",
4992 		phy->speed_cap_mask, control2);
4993 	CL22_WR_OVER_CL45(bp, phy,
4994 			  MDIO_REG_BANK_SERDES_DIGITAL,
4995 			  MDIO_SERDES_DIGITAL_A_1000X_CONTROL2,
4996 			  control2);
4997 
4998 	if ((phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT) &&
4999 	     (phy->speed_cap_mask &
5000 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
5001 		DP(NETIF_MSG_LINK, "XGXS\n");
5002 
5003 		CL22_WR_OVER_CL45(bp, phy,
5004 				 MDIO_REG_BANK_10G_PARALLEL_DETECT,
5005 				 MDIO_10G_PARALLEL_DETECT_PAR_DET_10G_LINK,
5006 				 MDIO_10G_PARALLEL_DETECT_PAR_DET_10G_LINK_CNT);
5007 
5008 		CL22_RD_OVER_CL45(bp, phy,
5009 				  MDIO_REG_BANK_10G_PARALLEL_DETECT,
5010 				  MDIO_10G_PARALLEL_DETECT_PAR_DET_10G_CONTROL,
5011 				  &control2);
5012 
5013 
5014 		control2 |=
5015 		    MDIO_10G_PARALLEL_DETECT_PAR_DET_10G_CONTROL_PARDET10G_EN;
5016 
5017 		CL22_WR_OVER_CL45(bp, phy,
5018 				  MDIO_REG_BANK_10G_PARALLEL_DETECT,
5019 				  MDIO_10G_PARALLEL_DETECT_PAR_DET_10G_CONTROL,
5020 				  control2);
5021 
5022 		/* Disable parallel detection of HiG */
5023 		CL22_WR_OVER_CL45(bp, phy,
5024 				  MDIO_REG_BANK_XGXS_BLOCK2,
5025 				  MDIO_XGXS_BLOCK2_UNICORE_MODE_10G,
5026 				  MDIO_XGXS_BLOCK2_UNICORE_MODE_10G_CX4_XGXS |
5027 				  MDIO_XGXS_BLOCK2_UNICORE_MODE_10G_HIGIG_XGXS);
5028 	}
5029 }
5030 
5031 static void bnx2x_set_autoneg(struct bnx2x_phy *phy,
5032 			      struct link_params *params,
5033 			      struct link_vars *vars,
5034 			      u8 enable_cl73)
5035 {
5036 	struct bnx2x *bp = params->bp;
5037 	u16 reg_val;
5038 
5039 	/* CL37 Autoneg */
5040 	CL22_RD_OVER_CL45(bp, phy,
5041 			  MDIO_REG_BANK_COMBO_IEEE0,
5042 			  MDIO_COMBO_IEEE0_MII_CONTROL, &reg_val);
5043 
5044 	/* CL37 Autoneg Enabled */
5045 	if (vars->line_speed == SPEED_AUTO_NEG)
5046 		reg_val |= MDIO_COMBO_IEEO_MII_CONTROL_AN_EN;
5047 	else /* CL37 Autoneg Disabled */
5048 		reg_val &= ~(MDIO_COMBO_IEEO_MII_CONTROL_AN_EN |
5049 			     MDIO_COMBO_IEEO_MII_CONTROL_RESTART_AN);
5050 
5051 	CL22_WR_OVER_CL45(bp, phy,
5052 			  MDIO_REG_BANK_COMBO_IEEE0,
5053 			  MDIO_COMBO_IEEE0_MII_CONTROL, reg_val);
5054 
5055 	/* Enable/Disable Autodetection */
5056 
5057 	CL22_RD_OVER_CL45(bp, phy,
5058 			  MDIO_REG_BANK_SERDES_DIGITAL,
5059 			  MDIO_SERDES_DIGITAL_A_1000X_CONTROL1, &reg_val);
5060 	reg_val &= ~(MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_SIGNAL_DETECT_EN |
5061 		    MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_INVERT_SIGNAL_DETECT);
5062 	reg_val |= MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_FIBER_MODE;
5063 	if (vars->line_speed == SPEED_AUTO_NEG)
5064 		reg_val |= MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_AUTODET;
5065 	else
5066 		reg_val &= ~MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_AUTODET;
5067 
5068 	CL22_WR_OVER_CL45(bp, phy,
5069 			  MDIO_REG_BANK_SERDES_DIGITAL,
5070 			  MDIO_SERDES_DIGITAL_A_1000X_CONTROL1, reg_val);
5071 
5072 	/* Enable TetonII and BAM autoneg */
5073 	CL22_RD_OVER_CL45(bp, phy,
5074 			  MDIO_REG_BANK_BAM_NEXT_PAGE,
5075 			  MDIO_BAM_NEXT_PAGE_MP5_NEXT_PAGE_CTRL,
5076 			  &reg_val);
5077 	if (vars->line_speed == SPEED_AUTO_NEG) {
5078 		/* Enable BAM aneg Mode and TetonII aneg Mode */
5079 		reg_val |= (MDIO_BAM_NEXT_PAGE_MP5_NEXT_PAGE_CTRL_BAM_MODE |
5080 			    MDIO_BAM_NEXT_PAGE_MP5_NEXT_PAGE_CTRL_TETON_AN);
5081 	} else {
5082 		/* TetonII and BAM Autoneg Disabled */
5083 		reg_val &= ~(MDIO_BAM_NEXT_PAGE_MP5_NEXT_PAGE_CTRL_BAM_MODE |
5084 			     MDIO_BAM_NEXT_PAGE_MP5_NEXT_PAGE_CTRL_TETON_AN);
5085 	}
5086 	CL22_WR_OVER_CL45(bp, phy,
5087 			  MDIO_REG_BANK_BAM_NEXT_PAGE,
5088 			  MDIO_BAM_NEXT_PAGE_MP5_NEXT_PAGE_CTRL,
5089 			  reg_val);
5090 
5091 	if (enable_cl73) {
5092 		/* Enable Cl73 FSM status bits */
5093 		CL22_WR_OVER_CL45(bp, phy,
5094 				  MDIO_REG_BANK_CL73_USERB0,
5095 				  MDIO_CL73_USERB0_CL73_UCTRL,
5096 				  0xe);
5097 
5098 		/* Enable BAM Station Manager*/
5099 		CL22_WR_OVER_CL45(bp, phy,
5100 			MDIO_REG_BANK_CL73_USERB0,
5101 			MDIO_CL73_USERB0_CL73_BAM_CTRL1,
5102 			MDIO_CL73_USERB0_CL73_BAM_CTRL1_BAM_EN |
5103 			MDIO_CL73_USERB0_CL73_BAM_CTRL1_BAM_STATION_MNGR_EN |
5104 			MDIO_CL73_USERB0_CL73_BAM_CTRL1_BAM_NP_AFTER_BP_EN);
5105 
5106 		/* Advertise CL73 link speeds */
5107 		CL22_RD_OVER_CL45(bp, phy,
5108 				  MDIO_REG_BANK_CL73_IEEEB1,
5109 				  MDIO_CL73_IEEEB1_AN_ADV2,
5110 				  &reg_val);
5111 		if (phy->speed_cap_mask &
5112 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)
5113 			reg_val |= MDIO_CL73_IEEEB1_AN_ADV2_ADVR_10G_KX4;
5114 		if (phy->speed_cap_mask &
5115 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)
5116 			reg_val |= MDIO_CL73_IEEEB1_AN_ADV2_ADVR_1000M_KX;
5117 
5118 		CL22_WR_OVER_CL45(bp, phy,
5119 				  MDIO_REG_BANK_CL73_IEEEB1,
5120 				  MDIO_CL73_IEEEB1_AN_ADV2,
5121 				  reg_val);
5122 
5123 		/* CL73 Autoneg Enabled */
5124 		reg_val = MDIO_CL73_IEEEB0_CL73_AN_CONTROL_AN_EN;
5125 
5126 	} else /* CL73 Autoneg Disabled */
5127 		reg_val = 0;
5128 
5129 	CL22_WR_OVER_CL45(bp, phy,
5130 			  MDIO_REG_BANK_CL73_IEEEB0,
5131 			  MDIO_CL73_IEEEB0_CL73_AN_CONTROL, reg_val);
5132 }
5133 
5134 /* Program SerDes, forced speed */
5135 static void bnx2x_program_serdes(struct bnx2x_phy *phy,
5136 				 struct link_params *params,
5137 				 struct link_vars *vars)
5138 {
5139 	struct bnx2x *bp = params->bp;
5140 	u16 reg_val;
5141 
5142 	/* Program duplex, disable autoneg and sgmii*/
5143 	CL22_RD_OVER_CL45(bp, phy,
5144 			  MDIO_REG_BANK_COMBO_IEEE0,
5145 			  MDIO_COMBO_IEEE0_MII_CONTROL, &reg_val);
5146 	reg_val &= ~(MDIO_COMBO_IEEO_MII_CONTROL_FULL_DUPLEX |
5147 		     MDIO_COMBO_IEEO_MII_CONTROL_AN_EN |
5148 		     MDIO_COMBO_IEEO_MII_CONTROL_MAN_SGMII_SP_MASK);
5149 	if (phy->req_duplex == DUPLEX_FULL)
5150 		reg_val |= MDIO_COMBO_IEEO_MII_CONTROL_FULL_DUPLEX;
5151 	CL22_WR_OVER_CL45(bp, phy,
5152 			  MDIO_REG_BANK_COMBO_IEEE0,
5153 			  MDIO_COMBO_IEEE0_MII_CONTROL, reg_val);
5154 
5155 	/* Program speed
5156 	 *  - needed only if the speed is greater than 1G (2.5G or 10G)
5157 	 */
5158 	CL22_RD_OVER_CL45(bp, phy,
5159 			  MDIO_REG_BANK_SERDES_DIGITAL,
5160 			  MDIO_SERDES_DIGITAL_MISC1, &reg_val);
5161 	/* Clearing the speed value before setting the right speed */
5162 	DP(NETIF_MSG_LINK, "MDIO_REG_BANK_SERDES_DIGITAL = 0x%x\n", reg_val);
5163 
5164 	reg_val &= ~(MDIO_SERDES_DIGITAL_MISC1_FORCE_SPEED_MASK |
5165 		     MDIO_SERDES_DIGITAL_MISC1_FORCE_SPEED_SEL);
5166 
5167 	if (!((vars->line_speed == SPEED_1000) ||
5168 	      (vars->line_speed == SPEED_100) ||
5169 	      (vars->line_speed == SPEED_10))) {
5170 
5171 		reg_val |= (MDIO_SERDES_DIGITAL_MISC1_REFCLK_SEL_156_25M |
5172 			    MDIO_SERDES_DIGITAL_MISC1_FORCE_SPEED_SEL);
5173 		if (vars->line_speed == SPEED_10000)
5174 			reg_val |=
5175 				MDIO_SERDES_DIGITAL_MISC1_FORCE_SPEED_10G_CX4;
5176 	}
5177 
5178 	CL22_WR_OVER_CL45(bp, phy,
5179 			  MDIO_REG_BANK_SERDES_DIGITAL,
5180 			  MDIO_SERDES_DIGITAL_MISC1, reg_val);
5181 
5182 }
5183 
5184 static void bnx2x_set_brcm_cl37_advertisement(struct bnx2x_phy *phy,
5185 					      struct link_params *params)
5186 {
5187 	struct bnx2x *bp = params->bp;
5188 	u16 val = 0;
5189 
5190 	/* Set extended capabilities */
5191 	if (phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G)
5192 		val |= MDIO_OVER_1G_UP1_2_5G;
5193 	if (phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)
5194 		val |= MDIO_OVER_1G_UP1_10G;
5195 	CL22_WR_OVER_CL45(bp, phy,
5196 			  MDIO_REG_BANK_OVER_1G,
5197 			  MDIO_OVER_1G_UP1, val);
5198 
5199 	CL22_WR_OVER_CL45(bp, phy,
5200 			  MDIO_REG_BANK_OVER_1G,
5201 			  MDIO_OVER_1G_UP3, 0x400);
5202 }
5203 
5204 static void bnx2x_set_ieee_aneg_advertisement(struct bnx2x_phy *phy,
5205 					      struct link_params *params,
5206 					      u16 ieee_fc)
5207 {
5208 	struct bnx2x *bp = params->bp;
5209 	u16 val;
5210 	/* For AN, we are always publishing full duplex */
5211 
5212 	CL22_WR_OVER_CL45(bp, phy,
5213 			  MDIO_REG_BANK_COMBO_IEEE0,
5214 			  MDIO_COMBO_IEEE0_AUTO_NEG_ADV, ieee_fc);
5215 	CL22_RD_OVER_CL45(bp, phy,
5216 			  MDIO_REG_BANK_CL73_IEEEB1,
5217 			  MDIO_CL73_IEEEB1_AN_ADV1, &val);
5218 	val &= ~MDIO_CL73_IEEEB1_AN_ADV1_PAUSE_BOTH;
5219 	val |= ((ieee_fc<<3) & MDIO_CL73_IEEEB1_AN_ADV1_PAUSE_MASK);
5220 	CL22_WR_OVER_CL45(bp, phy,
5221 			  MDIO_REG_BANK_CL73_IEEEB1,
5222 			  MDIO_CL73_IEEEB1_AN_ADV1, val);
5223 }
5224 
5225 static void bnx2x_restart_autoneg(struct bnx2x_phy *phy,
5226 				  struct link_params *params,
5227 				  u8 enable_cl73)
5228 {
5229 	struct bnx2x *bp = params->bp;
5230 	u16 mii_control;
5231 
5232 	DP(NETIF_MSG_LINK, "bnx2x_restart_autoneg\n");
5233 	/* Enable and restart BAM/CL37 aneg */
5234 
5235 	if (enable_cl73) {
5236 		CL22_RD_OVER_CL45(bp, phy,
5237 				  MDIO_REG_BANK_CL73_IEEEB0,
5238 				  MDIO_CL73_IEEEB0_CL73_AN_CONTROL,
5239 				  &mii_control);
5240 
5241 		CL22_WR_OVER_CL45(bp, phy,
5242 				  MDIO_REG_BANK_CL73_IEEEB0,
5243 				  MDIO_CL73_IEEEB0_CL73_AN_CONTROL,
5244 				  (mii_control |
5245 				  MDIO_CL73_IEEEB0_CL73_AN_CONTROL_AN_EN |
5246 				  MDIO_CL73_IEEEB0_CL73_AN_CONTROL_RESTART_AN));
5247 	} else {
5248 
5249 		CL22_RD_OVER_CL45(bp, phy,
5250 				  MDIO_REG_BANK_COMBO_IEEE0,
5251 				  MDIO_COMBO_IEEE0_MII_CONTROL,
5252 				  &mii_control);
5253 		DP(NETIF_MSG_LINK,
5254 			 "bnx2x_restart_autoneg mii_control before = 0x%x\n",
5255 			 mii_control);
5256 		CL22_WR_OVER_CL45(bp, phy,
5257 				  MDIO_REG_BANK_COMBO_IEEE0,
5258 				  MDIO_COMBO_IEEE0_MII_CONTROL,
5259 				  (mii_control |
5260 				   MDIO_COMBO_IEEO_MII_CONTROL_AN_EN |
5261 				   MDIO_COMBO_IEEO_MII_CONTROL_RESTART_AN));
5262 	}
5263 }
5264 
5265 static void bnx2x_initialize_sgmii_process(struct bnx2x_phy *phy,
5266 					   struct link_params *params,
5267 					   struct link_vars *vars)
5268 {
5269 	struct bnx2x *bp = params->bp;
5270 	u16 control1;
5271 
5272 	/* In SGMII mode, the unicore is always slave */
5273 
5274 	CL22_RD_OVER_CL45(bp, phy,
5275 			  MDIO_REG_BANK_SERDES_DIGITAL,
5276 			  MDIO_SERDES_DIGITAL_A_1000X_CONTROL1,
5277 			  &control1);
5278 	control1 |= MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_INVERT_SIGNAL_DETECT;
5279 	/* Set sgmii mode (and not fiber) */
5280 	control1 &= ~(MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_FIBER_MODE |
5281 		      MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_AUTODET |
5282 		      MDIO_SERDES_DIGITAL_A_1000X_CONTROL1_MSTR_MODE);
5283 	CL22_WR_OVER_CL45(bp, phy,
5284 			  MDIO_REG_BANK_SERDES_DIGITAL,
5285 			  MDIO_SERDES_DIGITAL_A_1000X_CONTROL1,
5286 			  control1);
5287 
5288 	/* If forced speed */
5289 	if (!(vars->line_speed == SPEED_AUTO_NEG)) {
5290 		/* Set speed, disable autoneg */
5291 		u16 mii_control;
5292 
5293 		CL22_RD_OVER_CL45(bp, phy,
5294 				  MDIO_REG_BANK_COMBO_IEEE0,
5295 				  MDIO_COMBO_IEEE0_MII_CONTROL,
5296 				  &mii_control);
5297 		mii_control &= ~(MDIO_COMBO_IEEO_MII_CONTROL_AN_EN |
5298 				 MDIO_COMBO_IEEO_MII_CONTROL_MAN_SGMII_SP_MASK|
5299 				 MDIO_COMBO_IEEO_MII_CONTROL_FULL_DUPLEX);
5300 
5301 		switch (vars->line_speed) {
5302 		case SPEED_100:
5303 			mii_control |=
5304 				MDIO_COMBO_IEEO_MII_CONTROL_MAN_SGMII_SP_100;
5305 			break;
5306 		case SPEED_1000:
5307 			mii_control |=
5308 				MDIO_COMBO_IEEO_MII_CONTROL_MAN_SGMII_SP_1000;
5309 			break;
5310 		case SPEED_10:
5311 			/* There is nothing to set for 10M */
5312 			break;
5313 		default:
5314 			/* Invalid speed for SGMII */
5315 			DP(NETIF_MSG_LINK, "Invalid line_speed 0x%x\n",
5316 				  vars->line_speed);
5317 			break;
5318 		}
5319 
5320 		/* Setting the full duplex */
5321 		if (phy->req_duplex == DUPLEX_FULL)
5322 			mii_control |=
5323 				MDIO_COMBO_IEEO_MII_CONTROL_FULL_DUPLEX;
5324 		CL22_WR_OVER_CL45(bp, phy,
5325 				  MDIO_REG_BANK_COMBO_IEEE0,
5326 				  MDIO_COMBO_IEEE0_MII_CONTROL,
5327 				  mii_control);
5328 
5329 	} else { /* AN mode */
5330 		/* Enable and restart AN */
5331 		bnx2x_restart_autoneg(phy, params, 0);
5332 	}
5333 }
5334 
5335 /* Link management
5336  */
5337 static int bnx2x_direct_parallel_detect_used(struct bnx2x_phy *phy,
5338 					     struct link_params *params)
5339 {
5340 	struct bnx2x *bp = params->bp;
5341 	u16 pd_10g, status2_1000x;
5342 	if (phy->req_line_speed != SPEED_AUTO_NEG)
5343 		return 0;
5344 	CL22_RD_OVER_CL45(bp, phy,
5345 			  MDIO_REG_BANK_SERDES_DIGITAL,
5346 			  MDIO_SERDES_DIGITAL_A_1000X_STATUS2,
5347 			  &status2_1000x);
5348 	CL22_RD_OVER_CL45(bp, phy,
5349 			  MDIO_REG_BANK_SERDES_DIGITAL,
5350 			  MDIO_SERDES_DIGITAL_A_1000X_STATUS2,
5351 			  &status2_1000x);
5352 	if (status2_1000x & MDIO_SERDES_DIGITAL_A_1000X_STATUS2_AN_DISABLED) {
5353 		DP(NETIF_MSG_LINK, "1G parallel detect link on port %d\n",
5354 			 params->port);
5355 		return 1;
5356 	}
5357 
5358 	CL22_RD_OVER_CL45(bp, phy,
5359 			  MDIO_REG_BANK_10G_PARALLEL_DETECT,
5360 			  MDIO_10G_PARALLEL_DETECT_PAR_DET_10G_STATUS,
5361 			  &pd_10g);
5362 
5363 	if (pd_10g & MDIO_10G_PARALLEL_DETECT_PAR_DET_10G_STATUS_PD_LINK) {
5364 		DP(NETIF_MSG_LINK, "10G parallel detect link on port %d\n",
5365 			 params->port);
5366 		return 1;
5367 	}
5368 	return 0;
5369 }
5370 
5371 static void bnx2x_update_adv_fc(struct bnx2x_phy *phy,
5372 				struct link_params *params,
5373 				struct link_vars *vars,
5374 				u32 gp_status)
5375 {
5376 	u16 ld_pause;   /* local driver */
5377 	u16 lp_pause;   /* link partner */
5378 	u16 pause_result;
5379 	struct bnx2x *bp = params->bp;
5380 	if ((gp_status &
5381 	     (MDIO_GP_STATUS_TOP_AN_STATUS1_CL73_AUTONEG_COMPLETE |
5382 	      MDIO_GP_STATUS_TOP_AN_STATUS1_CL73_MR_LP_NP_AN_ABLE)) ==
5383 	    (MDIO_GP_STATUS_TOP_AN_STATUS1_CL73_AUTONEG_COMPLETE |
5384 	     MDIO_GP_STATUS_TOP_AN_STATUS1_CL73_MR_LP_NP_AN_ABLE)) {
5385 
5386 		CL22_RD_OVER_CL45(bp, phy,
5387 				  MDIO_REG_BANK_CL73_IEEEB1,
5388 				  MDIO_CL73_IEEEB1_AN_ADV1,
5389 				  &ld_pause);
5390 		CL22_RD_OVER_CL45(bp, phy,
5391 				  MDIO_REG_BANK_CL73_IEEEB1,
5392 				  MDIO_CL73_IEEEB1_AN_LP_ADV1,
5393 				  &lp_pause);
5394 		pause_result = (ld_pause &
5395 				MDIO_CL73_IEEEB1_AN_ADV1_PAUSE_MASK) >> 8;
5396 		pause_result |= (lp_pause &
5397 				 MDIO_CL73_IEEEB1_AN_LP_ADV1_PAUSE_MASK) >> 10;
5398 		DP(NETIF_MSG_LINK, "pause_result CL73 0x%x\n", pause_result);
5399 	} else {
5400 		CL22_RD_OVER_CL45(bp, phy,
5401 				  MDIO_REG_BANK_COMBO_IEEE0,
5402 				  MDIO_COMBO_IEEE0_AUTO_NEG_ADV,
5403 				  &ld_pause);
5404 		CL22_RD_OVER_CL45(bp, phy,
5405 			MDIO_REG_BANK_COMBO_IEEE0,
5406 			MDIO_COMBO_IEEE0_AUTO_NEG_LINK_PARTNER_ABILITY1,
5407 			&lp_pause);
5408 		pause_result = (ld_pause &
5409 				MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK)>>5;
5410 		pause_result |= (lp_pause &
5411 				 MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK)>>7;
5412 		DP(NETIF_MSG_LINK, "pause_result CL37 0x%x\n", pause_result);
5413 	}
5414 	bnx2x_pause_resolve(phy, params, vars, pause_result);
5415 
5416 }
5417 
5418 static void bnx2x_flow_ctrl_resolve(struct bnx2x_phy *phy,
5419 				    struct link_params *params,
5420 				    struct link_vars *vars,
5421 				    u32 gp_status)
5422 {
5423 	struct bnx2x *bp = params->bp;
5424 	vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
5425 
5426 	/* Resolve from gp_status in case of AN complete and not sgmii */
5427 	if (phy->req_flow_ctrl != BNX2X_FLOW_CTRL_AUTO) {
5428 		/* Update the advertised flow-controled of LD/LP in AN */
5429 		if (phy->req_line_speed == SPEED_AUTO_NEG)
5430 			bnx2x_update_adv_fc(phy, params, vars, gp_status);
5431 		/* But set the flow-control result as the requested one */
5432 		vars->flow_ctrl = phy->req_flow_ctrl;
5433 	} else if (phy->req_line_speed != SPEED_AUTO_NEG)
5434 		vars->flow_ctrl = params->req_fc_auto_adv;
5435 	else if ((gp_status & MDIO_AN_CL73_OR_37_COMPLETE) &&
5436 		 (!(vars->phy_flags & PHY_SGMII_FLAG))) {
5437 		if (bnx2x_direct_parallel_detect_used(phy, params)) {
5438 			vars->flow_ctrl = params->req_fc_auto_adv;
5439 			return;
5440 		}
5441 		bnx2x_update_adv_fc(phy, params, vars, gp_status);
5442 	}
5443 	DP(NETIF_MSG_LINK, "flow_ctrl 0x%x\n", vars->flow_ctrl);
5444 }
5445 
5446 static void bnx2x_check_fallback_to_cl37(struct bnx2x_phy *phy,
5447 					 struct link_params *params)
5448 {
5449 	struct bnx2x *bp = params->bp;
5450 	u16 rx_status, ustat_val, cl37_fsm_received;
5451 	DP(NETIF_MSG_LINK, "bnx2x_check_fallback_to_cl37\n");
5452 	/* Step 1: Make sure signal is detected */
5453 	CL22_RD_OVER_CL45(bp, phy,
5454 			  MDIO_REG_BANK_RX0,
5455 			  MDIO_RX0_RX_STATUS,
5456 			  &rx_status);
5457 	if ((rx_status & MDIO_RX0_RX_STATUS_SIGDET) !=
5458 	    (MDIO_RX0_RX_STATUS_SIGDET)) {
5459 		DP(NETIF_MSG_LINK, "Signal is not detected. Restoring CL73."
5460 			     "rx_status(0x80b0) = 0x%x\n", rx_status);
5461 		CL22_WR_OVER_CL45(bp, phy,
5462 				  MDIO_REG_BANK_CL73_IEEEB0,
5463 				  MDIO_CL73_IEEEB0_CL73_AN_CONTROL,
5464 				  MDIO_CL73_IEEEB0_CL73_AN_CONTROL_AN_EN);
5465 		return;
5466 	}
5467 	/* Step 2: Check CL73 state machine */
5468 	CL22_RD_OVER_CL45(bp, phy,
5469 			  MDIO_REG_BANK_CL73_USERB0,
5470 			  MDIO_CL73_USERB0_CL73_USTAT1,
5471 			  &ustat_val);
5472 	if ((ustat_val &
5473 	     (MDIO_CL73_USERB0_CL73_USTAT1_LINK_STATUS_CHECK |
5474 	      MDIO_CL73_USERB0_CL73_USTAT1_AN_GOOD_CHECK_BAM37)) !=
5475 	    (MDIO_CL73_USERB0_CL73_USTAT1_LINK_STATUS_CHECK |
5476 	      MDIO_CL73_USERB0_CL73_USTAT1_AN_GOOD_CHECK_BAM37)) {
5477 		DP(NETIF_MSG_LINK, "CL73 state-machine is not stable. "
5478 			     "ustat_val(0x8371) = 0x%x\n", ustat_val);
5479 		return;
5480 	}
5481 	/* Step 3: Check CL37 Message Pages received to indicate LP
5482 	 * supports only CL37
5483 	 */
5484 	CL22_RD_OVER_CL45(bp, phy,
5485 			  MDIO_REG_BANK_REMOTE_PHY,
5486 			  MDIO_REMOTE_PHY_MISC_RX_STATUS,
5487 			  &cl37_fsm_received);
5488 	if ((cl37_fsm_received &
5489 	     (MDIO_REMOTE_PHY_MISC_RX_STATUS_CL37_FSM_RECEIVED_OVER1G_MSG |
5490 	     MDIO_REMOTE_PHY_MISC_RX_STATUS_CL37_FSM_RECEIVED_BRCM_OUI_MSG)) !=
5491 	    (MDIO_REMOTE_PHY_MISC_RX_STATUS_CL37_FSM_RECEIVED_OVER1G_MSG |
5492 	      MDIO_REMOTE_PHY_MISC_RX_STATUS_CL37_FSM_RECEIVED_BRCM_OUI_MSG)) {
5493 		DP(NETIF_MSG_LINK, "No CL37 FSM were received. "
5494 			     "misc_rx_status(0x8330) = 0x%x\n",
5495 			 cl37_fsm_received);
5496 		return;
5497 	}
5498 	/* The combined cl37/cl73 fsm state information indicating that
5499 	 * we are connected to a device which does not support cl73, but
5500 	 * does support cl37 BAM. In this case we disable cl73 and
5501 	 * restart cl37 auto-neg
5502 	 */
5503 
5504 	/* Disable CL73 */
5505 	CL22_WR_OVER_CL45(bp, phy,
5506 			  MDIO_REG_BANK_CL73_IEEEB0,
5507 			  MDIO_CL73_IEEEB0_CL73_AN_CONTROL,
5508 			  0);
5509 	/* Restart CL37 autoneg */
5510 	bnx2x_restart_autoneg(phy, params, 0);
5511 	DP(NETIF_MSG_LINK, "Disabling CL73, and restarting CL37 autoneg\n");
5512 }
5513 
5514 static void bnx2x_xgxs_an_resolve(struct bnx2x_phy *phy,
5515 				  struct link_params *params,
5516 				  struct link_vars *vars,
5517 				  u32 gp_status)
5518 {
5519 	if (gp_status & MDIO_AN_CL73_OR_37_COMPLETE)
5520 		vars->link_status |=
5521 			LINK_STATUS_AUTO_NEGOTIATE_COMPLETE;
5522 
5523 	if (bnx2x_direct_parallel_detect_used(phy, params))
5524 		vars->link_status |=
5525 			LINK_STATUS_PARALLEL_DETECTION_USED;
5526 }
5527 static int bnx2x_get_link_speed_duplex(struct bnx2x_phy *phy,
5528 				     struct link_params *params,
5529 				      struct link_vars *vars,
5530 				      u16 is_link_up,
5531 				      u16 speed_mask,
5532 				      u16 is_duplex)
5533 {
5534 	struct bnx2x *bp = params->bp;
5535 	if (phy->req_line_speed == SPEED_AUTO_NEG)
5536 		vars->link_status |= LINK_STATUS_AUTO_NEGOTIATE_ENABLED;
5537 	if (is_link_up) {
5538 		DP(NETIF_MSG_LINK, "phy link up\n");
5539 
5540 		vars->phy_link_up = 1;
5541 		vars->link_status |= LINK_STATUS_LINK_UP;
5542 
5543 		switch (speed_mask) {
5544 		case GP_STATUS_10M:
5545 			vars->line_speed = SPEED_10;
5546 			if (is_duplex == DUPLEX_FULL)
5547 				vars->link_status |= LINK_10TFD;
5548 			else
5549 				vars->link_status |= LINK_10THD;
5550 			break;
5551 
5552 		case GP_STATUS_100M:
5553 			vars->line_speed = SPEED_100;
5554 			if (is_duplex == DUPLEX_FULL)
5555 				vars->link_status |= LINK_100TXFD;
5556 			else
5557 				vars->link_status |= LINK_100TXHD;
5558 			break;
5559 
5560 		case GP_STATUS_1G:
5561 		case GP_STATUS_1G_KX:
5562 			vars->line_speed = SPEED_1000;
5563 			if (is_duplex == DUPLEX_FULL)
5564 				vars->link_status |= LINK_1000TFD;
5565 			else
5566 				vars->link_status |= LINK_1000THD;
5567 			break;
5568 
5569 		case GP_STATUS_2_5G:
5570 			vars->line_speed = SPEED_2500;
5571 			if (is_duplex == DUPLEX_FULL)
5572 				vars->link_status |= LINK_2500TFD;
5573 			else
5574 				vars->link_status |= LINK_2500THD;
5575 			break;
5576 
5577 		case GP_STATUS_5G:
5578 		case GP_STATUS_6G:
5579 			DP(NETIF_MSG_LINK,
5580 				 "link speed unsupported  gp_status 0x%x\n",
5581 				  speed_mask);
5582 			return -EINVAL;
5583 
5584 		case GP_STATUS_10G_KX4:
5585 		case GP_STATUS_10G_HIG:
5586 		case GP_STATUS_10G_CX4:
5587 		case GP_STATUS_10G_KR:
5588 		case GP_STATUS_10G_SFI:
5589 		case GP_STATUS_10G_XFI:
5590 			vars->line_speed = SPEED_10000;
5591 			vars->link_status |= LINK_10GTFD;
5592 			break;
5593 		case GP_STATUS_20G_DXGXS:
5594 		case GP_STATUS_20G_KR2:
5595 			vars->line_speed = SPEED_20000;
5596 			vars->link_status |= LINK_20GTFD;
5597 			break;
5598 		default:
5599 			DP(NETIF_MSG_LINK,
5600 				  "link speed unsupported gp_status 0x%x\n",
5601 				  speed_mask);
5602 			return -EINVAL;
5603 		}
5604 	} else { /* link_down */
5605 		DP(NETIF_MSG_LINK, "phy link down\n");
5606 
5607 		vars->phy_link_up = 0;
5608 
5609 		vars->duplex = DUPLEX_FULL;
5610 		vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
5611 		vars->mac_type = MAC_TYPE_NONE;
5612 	}
5613 	DP(NETIF_MSG_LINK, " phy_link_up %x line_speed %d\n",
5614 		    vars->phy_link_up, vars->line_speed);
5615 	return 0;
5616 }
5617 
5618 static int bnx2x_link_settings_status(struct bnx2x_phy *phy,
5619 				      struct link_params *params,
5620 				      struct link_vars *vars)
5621 {
5622 	struct bnx2x *bp = params->bp;
5623 
5624 	u16 gp_status, duplex = DUPLEX_HALF, link_up = 0, speed_mask;
5625 	int rc = 0;
5626 
5627 	/* Read gp_status */
5628 	CL22_RD_OVER_CL45(bp, phy,
5629 			  MDIO_REG_BANK_GP_STATUS,
5630 			  MDIO_GP_STATUS_TOP_AN_STATUS1,
5631 			  &gp_status);
5632 	if (gp_status & MDIO_GP_STATUS_TOP_AN_STATUS1_DUPLEX_STATUS)
5633 		duplex = DUPLEX_FULL;
5634 	if (gp_status & MDIO_GP_STATUS_TOP_AN_STATUS1_LINK_STATUS)
5635 		link_up = 1;
5636 	speed_mask = gp_status & GP_STATUS_SPEED_MASK;
5637 	DP(NETIF_MSG_LINK, "gp_status 0x%x, is_link_up %d, speed_mask 0x%x\n",
5638 		       gp_status, link_up, speed_mask);
5639 	rc = bnx2x_get_link_speed_duplex(phy, params, vars, link_up, speed_mask,
5640 					 duplex);
5641 	if (rc == -EINVAL)
5642 		return rc;
5643 
5644 	if (gp_status & MDIO_GP_STATUS_TOP_AN_STATUS1_LINK_STATUS) {
5645 		if (SINGLE_MEDIA_DIRECT(params)) {
5646 			vars->duplex = duplex;
5647 			bnx2x_flow_ctrl_resolve(phy, params, vars, gp_status);
5648 			if (phy->req_line_speed == SPEED_AUTO_NEG)
5649 				bnx2x_xgxs_an_resolve(phy, params, vars,
5650 						      gp_status);
5651 		}
5652 	} else { /* Link_down */
5653 		if ((phy->req_line_speed == SPEED_AUTO_NEG) &&
5654 		    SINGLE_MEDIA_DIRECT(params)) {
5655 			/* Check signal is detected */
5656 			bnx2x_check_fallback_to_cl37(phy, params);
5657 		}
5658 	}
5659 
5660 	/* Read LP advertised speeds*/
5661 	if (SINGLE_MEDIA_DIRECT(params) &&
5662 	    (vars->link_status & LINK_STATUS_AUTO_NEGOTIATE_COMPLETE)) {
5663 		u16 val;
5664 
5665 		CL22_RD_OVER_CL45(bp, phy, MDIO_REG_BANK_CL73_IEEEB1,
5666 				  MDIO_CL73_IEEEB1_AN_LP_ADV2, &val);
5667 
5668 		if (val & MDIO_CL73_IEEEB1_AN_ADV2_ADVR_1000M_KX)
5669 			vars->link_status |=
5670 				LINK_STATUS_LINK_PARTNER_1000TFD_CAPABLE;
5671 		if (val & (MDIO_CL73_IEEEB1_AN_ADV2_ADVR_10G_KX4 |
5672 			   MDIO_CL73_IEEEB1_AN_ADV2_ADVR_10G_KR))
5673 			vars->link_status |=
5674 				LINK_STATUS_LINK_PARTNER_10GXFD_CAPABLE;
5675 
5676 		CL22_RD_OVER_CL45(bp, phy, MDIO_REG_BANK_OVER_1G,
5677 				  MDIO_OVER_1G_LP_UP1, &val);
5678 
5679 		if (val & MDIO_OVER_1G_UP1_2_5G)
5680 			vars->link_status |=
5681 				LINK_STATUS_LINK_PARTNER_2500XFD_CAPABLE;
5682 		if (val & (MDIO_OVER_1G_UP1_10G | MDIO_OVER_1G_UP1_10GH))
5683 			vars->link_status |=
5684 				LINK_STATUS_LINK_PARTNER_10GXFD_CAPABLE;
5685 	}
5686 
5687 	DP(NETIF_MSG_LINK, "duplex %x  flow_ctrl 0x%x link_status 0x%x\n",
5688 		   vars->duplex, vars->flow_ctrl, vars->link_status);
5689 	return rc;
5690 }
5691 
5692 static int bnx2x_warpcore_read_status(struct bnx2x_phy *phy,
5693 				     struct link_params *params,
5694 				     struct link_vars *vars)
5695 {
5696 	struct bnx2x *bp = params->bp;
5697 	u8 lane;
5698 	u16 gp_status1, gp_speed, link_up, duplex = DUPLEX_FULL;
5699 	int rc = 0;
5700 	lane = bnx2x_get_warpcore_lane(phy, params);
5701 	/* Read gp_status */
5702 	if ((params->loopback_mode) &&
5703 	    (phy->flags & FLAGS_WC_DUAL_MODE)) {
5704 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5705 				MDIO_WC_REG_DIGITAL5_LINK_STATUS, &link_up);
5706 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5707 				MDIO_WC_REG_DIGITAL5_LINK_STATUS, &link_up);
5708 		link_up &= 0x1;
5709 	} else if ((phy->req_line_speed > SPEED_10000) &&
5710 		(phy->supported & SUPPORTED_20000baseMLD2_Full)) {
5711 		u16 temp_link_up;
5712 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5713 				1, &temp_link_up);
5714 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5715 				1, &link_up);
5716 		DP(NETIF_MSG_LINK, "PCS RX link status = 0x%x-->0x%x\n",
5717 			       temp_link_up, link_up);
5718 		link_up &= (1<<2);
5719 		if (link_up)
5720 			bnx2x_ext_phy_resolve_fc(phy, params, vars);
5721 	} else {
5722 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5723 				MDIO_WC_REG_GP2_STATUS_GP_2_1,
5724 				&gp_status1);
5725 		DP(NETIF_MSG_LINK, "0x81d1 = 0x%x\n", gp_status1);
5726 		/* Check for either KR, 1G, or AN up. */
5727 		link_up = ((gp_status1 >> 8) |
5728 			   (gp_status1 >> 12) |
5729 			   (gp_status1)) &
5730 			(1 << lane);
5731 		if (phy->supported & SUPPORTED_20000baseKR2_Full) {
5732 			u16 an_link;
5733 			bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
5734 					MDIO_AN_REG_STATUS, &an_link);
5735 			bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
5736 					MDIO_AN_REG_STATUS, &an_link);
5737 			link_up |= (an_link & (1<<2));
5738 		}
5739 		if (link_up && SINGLE_MEDIA_DIRECT(params)) {
5740 			u16 pd, gp_status4;
5741 			if (phy->req_line_speed == SPEED_AUTO_NEG) {
5742 				/* Check Autoneg complete */
5743 				bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5744 						MDIO_WC_REG_GP2_STATUS_GP_2_4,
5745 						&gp_status4);
5746 				if (gp_status4 & ((1<<12)<<lane))
5747 					vars->link_status |=
5748 					LINK_STATUS_AUTO_NEGOTIATE_COMPLETE;
5749 
5750 				/* Check parallel detect used */
5751 				bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5752 						MDIO_WC_REG_PAR_DET_10G_STATUS,
5753 						&pd);
5754 				if (pd & (1<<15))
5755 					vars->link_status |=
5756 					LINK_STATUS_PARALLEL_DETECTION_USED;
5757 			}
5758 			bnx2x_ext_phy_resolve_fc(phy, params, vars);
5759 			vars->duplex = duplex;
5760 		}
5761 	}
5762 
5763 	if ((vars->link_status & LINK_STATUS_AUTO_NEGOTIATE_COMPLETE) &&
5764 	    SINGLE_MEDIA_DIRECT(params)) {
5765 		u16 val;
5766 
5767 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
5768 				MDIO_AN_REG_LP_AUTO_NEG2, &val);
5769 
5770 		if (val & MDIO_CL73_IEEEB1_AN_ADV2_ADVR_1000M_KX)
5771 			vars->link_status |=
5772 				LINK_STATUS_LINK_PARTNER_1000TFD_CAPABLE;
5773 		if (val & (MDIO_CL73_IEEEB1_AN_ADV2_ADVR_10G_KX4 |
5774 			   MDIO_CL73_IEEEB1_AN_ADV2_ADVR_10G_KR))
5775 			vars->link_status |=
5776 				LINK_STATUS_LINK_PARTNER_10GXFD_CAPABLE;
5777 
5778 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5779 				MDIO_WC_REG_DIGITAL3_LP_UP1, &val);
5780 
5781 		if (val & MDIO_OVER_1G_UP1_2_5G)
5782 			vars->link_status |=
5783 				LINK_STATUS_LINK_PARTNER_2500XFD_CAPABLE;
5784 		if (val & (MDIO_OVER_1G_UP1_10G | MDIO_OVER_1G_UP1_10GH))
5785 			vars->link_status |=
5786 				LINK_STATUS_LINK_PARTNER_10GXFD_CAPABLE;
5787 
5788 	}
5789 
5790 
5791 	if (lane < 2) {
5792 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5793 				MDIO_WC_REG_GP2_STATUS_GP_2_2, &gp_speed);
5794 	} else {
5795 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
5796 				MDIO_WC_REG_GP2_STATUS_GP_2_3, &gp_speed);
5797 	}
5798 	DP(NETIF_MSG_LINK, "lane %d gp_speed 0x%x\n", lane, gp_speed);
5799 
5800 	if ((lane & 1) == 0)
5801 		gp_speed <<= 8;
5802 	gp_speed &= 0x3f00;
5803 	link_up = !!link_up;
5804 
5805 	rc = bnx2x_get_link_speed_duplex(phy, params, vars, link_up, gp_speed,
5806 					 duplex);
5807 
5808 	/* In case of KR link down, start up the recovering procedure */
5809 	if ((!link_up) && (phy->media_type == ETH_PHY_KR) &&
5810 	    (!(phy->flags & FLAGS_WC_DUAL_MODE)))
5811 		vars->rx_tx_asic_rst = MAX_KR_LINK_RETRY;
5812 
5813 	DP(NETIF_MSG_LINK, "duplex %x  flow_ctrl 0x%x link_status 0x%x\n",
5814 		   vars->duplex, vars->flow_ctrl, vars->link_status);
5815 	return rc;
5816 }
5817 static void bnx2x_set_gmii_tx_driver(struct link_params *params)
5818 {
5819 	struct bnx2x *bp = params->bp;
5820 	struct bnx2x_phy *phy = &params->phy[INT_PHY];
5821 	u16 lp_up2;
5822 	u16 tx_driver;
5823 	u16 bank;
5824 
5825 	/* Read precomp */
5826 	CL22_RD_OVER_CL45(bp, phy,
5827 			  MDIO_REG_BANK_OVER_1G,
5828 			  MDIO_OVER_1G_LP_UP2, &lp_up2);
5829 
5830 	/* Bits [10:7] at lp_up2, positioned at [15:12] */
5831 	lp_up2 = (((lp_up2 & MDIO_OVER_1G_LP_UP2_PREEMPHASIS_MASK) >>
5832 		   MDIO_OVER_1G_LP_UP2_PREEMPHASIS_SHIFT) <<
5833 		  MDIO_TX0_TX_DRIVER_PREEMPHASIS_SHIFT);
5834 
5835 	if (lp_up2 == 0)
5836 		return;
5837 
5838 	for (bank = MDIO_REG_BANK_TX0; bank <= MDIO_REG_BANK_TX3;
5839 	      bank += (MDIO_REG_BANK_TX1 - MDIO_REG_BANK_TX0)) {
5840 		CL22_RD_OVER_CL45(bp, phy,
5841 				  bank,
5842 				  MDIO_TX0_TX_DRIVER, &tx_driver);
5843 
5844 		/* Replace tx_driver bits [15:12] */
5845 		if (lp_up2 !=
5846 		    (tx_driver & MDIO_TX0_TX_DRIVER_PREEMPHASIS_MASK)) {
5847 			tx_driver &= ~MDIO_TX0_TX_DRIVER_PREEMPHASIS_MASK;
5848 			tx_driver |= lp_up2;
5849 			CL22_WR_OVER_CL45(bp, phy,
5850 					  bank,
5851 					  MDIO_TX0_TX_DRIVER, tx_driver);
5852 		}
5853 	}
5854 }
5855 
5856 static int bnx2x_emac_program(struct link_params *params,
5857 			      struct link_vars *vars)
5858 {
5859 	struct bnx2x *bp = params->bp;
5860 	u8 port = params->port;
5861 	u16 mode = 0;
5862 
5863 	DP(NETIF_MSG_LINK, "setting link speed & duplex\n");
5864 	bnx2x_bits_dis(bp, GRCBASE_EMAC0 + port*0x400 +
5865 		       EMAC_REG_EMAC_MODE,
5866 		       (EMAC_MODE_25G_MODE |
5867 			EMAC_MODE_PORT_MII_10M |
5868 			EMAC_MODE_HALF_DUPLEX));
5869 	switch (vars->line_speed) {
5870 	case SPEED_10:
5871 		mode |= EMAC_MODE_PORT_MII_10M;
5872 		break;
5873 
5874 	case SPEED_100:
5875 		mode |= EMAC_MODE_PORT_MII;
5876 		break;
5877 
5878 	case SPEED_1000:
5879 		mode |= EMAC_MODE_PORT_GMII;
5880 		break;
5881 
5882 	case SPEED_2500:
5883 		mode |= (EMAC_MODE_25G_MODE | EMAC_MODE_PORT_GMII);
5884 		break;
5885 
5886 	default:
5887 		/* 10G not valid for EMAC */
5888 		DP(NETIF_MSG_LINK, "Invalid line_speed 0x%x\n",
5889 			   vars->line_speed);
5890 		return -EINVAL;
5891 	}
5892 
5893 	if (vars->duplex == DUPLEX_HALF)
5894 		mode |= EMAC_MODE_HALF_DUPLEX;
5895 	bnx2x_bits_en(bp,
5896 		      GRCBASE_EMAC0 + port*0x400 + EMAC_REG_EMAC_MODE,
5897 		      mode);
5898 
5899 	bnx2x_set_led(params, vars, LED_MODE_OPER, vars->line_speed);
5900 	return 0;
5901 }
5902 
5903 static void bnx2x_set_preemphasis(struct bnx2x_phy *phy,
5904 				  struct link_params *params)
5905 {
5906 
5907 	u16 bank, i = 0;
5908 	struct bnx2x *bp = params->bp;
5909 
5910 	for (bank = MDIO_REG_BANK_RX0, i = 0; bank <= MDIO_REG_BANK_RX3;
5911 	      bank += (MDIO_REG_BANK_RX1-MDIO_REG_BANK_RX0), i++) {
5912 			CL22_WR_OVER_CL45(bp, phy,
5913 					  bank,
5914 					  MDIO_RX0_RX_EQ_BOOST,
5915 					  phy->rx_preemphasis[i]);
5916 	}
5917 
5918 	for (bank = MDIO_REG_BANK_TX0, i = 0; bank <= MDIO_REG_BANK_TX3;
5919 		      bank += (MDIO_REG_BANK_TX1 - MDIO_REG_BANK_TX0), i++) {
5920 			CL22_WR_OVER_CL45(bp, phy,
5921 					  bank,
5922 					  MDIO_TX0_TX_DRIVER,
5923 					  phy->tx_preemphasis[i]);
5924 	}
5925 }
5926 
5927 static void bnx2x_xgxs_config_init(struct bnx2x_phy *phy,
5928 				   struct link_params *params,
5929 				   struct link_vars *vars)
5930 {
5931 	struct bnx2x *bp = params->bp;
5932 	u8 enable_cl73 = (SINGLE_MEDIA_DIRECT(params) ||
5933 			  (params->loopback_mode == LOOPBACK_XGXS));
5934 	if (!(vars->phy_flags & PHY_SGMII_FLAG)) {
5935 		if (SINGLE_MEDIA_DIRECT(params) &&
5936 		    (params->feature_config_flags &
5937 		     FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED))
5938 			bnx2x_set_preemphasis(phy, params);
5939 
5940 		/* Forced speed requested? */
5941 		if (vars->line_speed != SPEED_AUTO_NEG ||
5942 		    (SINGLE_MEDIA_DIRECT(params) &&
5943 		     params->loopback_mode == LOOPBACK_EXT)) {
5944 			DP(NETIF_MSG_LINK, "not SGMII, no AN\n");
5945 
5946 			/* Disable autoneg */
5947 			bnx2x_set_autoneg(phy, params, vars, 0);
5948 
5949 			/* Program speed and duplex */
5950 			bnx2x_program_serdes(phy, params, vars);
5951 
5952 		} else { /* AN_mode */
5953 			DP(NETIF_MSG_LINK, "not SGMII, AN\n");
5954 
5955 			/* AN enabled */
5956 			bnx2x_set_brcm_cl37_advertisement(phy, params);
5957 
5958 			/* Program duplex & pause advertisement (for aneg) */
5959 			bnx2x_set_ieee_aneg_advertisement(phy, params,
5960 							  vars->ieee_fc);
5961 
5962 			/* Enable autoneg */
5963 			bnx2x_set_autoneg(phy, params, vars, enable_cl73);
5964 
5965 			/* Enable and restart AN */
5966 			bnx2x_restart_autoneg(phy, params, enable_cl73);
5967 		}
5968 
5969 	} else { /* SGMII mode */
5970 		DP(NETIF_MSG_LINK, "SGMII\n");
5971 
5972 		bnx2x_initialize_sgmii_process(phy, params, vars);
5973 	}
5974 }
5975 
5976 static int bnx2x_prepare_xgxs(struct bnx2x_phy *phy,
5977 			  struct link_params *params,
5978 			  struct link_vars *vars)
5979 {
5980 	int rc;
5981 	vars->phy_flags |= PHY_XGXS_FLAG;
5982 	if ((phy->req_line_speed &&
5983 	     ((phy->req_line_speed == SPEED_100) ||
5984 	      (phy->req_line_speed == SPEED_10))) ||
5985 	    (!phy->req_line_speed &&
5986 	     (phy->speed_cap_mask >=
5987 	      PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL) &&
5988 	     (phy->speed_cap_mask <
5989 	      PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) ||
5990 	    (phy->type == PORT_HW_CFG_SERDES_EXT_PHY_TYPE_DIRECT_SD))
5991 		vars->phy_flags |= PHY_SGMII_FLAG;
5992 	else
5993 		vars->phy_flags &= ~PHY_SGMII_FLAG;
5994 
5995 	bnx2x_calc_ieee_aneg_adv(phy, params, &vars->ieee_fc);
5996 	bnx2x_set_aer_mmd(params, phy);
5997 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
5998 		bnx2x_set_master_ln(params, phy);
5999 
6000 	rc = bnx2x_reset_unicore(params, phy, 0);
6001 	/* Reset the SerDes and wait for reset bit return low */
6002 	if (rc)
6003 		return rc;
6004 
6005 	bnx2x_set_aer_mmd(params, phy);
6006 	/* Setting the masterLn_def again after the reset */
6007 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT) {
6008 		bnx2x_set_master_ln(params, phy);
6009 		bnx2x_set_swap_lanes(params, phy);
6010 	}
6011 
6012 	return rc;
6013 }
6014 
6015 static u16 bnx2x_wait_reset_complete(struct bnx2x *bp,
6016 				     struct bnx2x_phy *phy,
6017 				     struct link_params *params)
6018 {
6019 	u16 cnt, ctrl;
6020 	/* Wait for soft reset to get cleared up to 1 sec */
6021 	for (cnt = 0; cnt < 1000; cnt++) {
6022 		if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54618SE)
6023 			bnx2x_cl22_read(bp, phy,
6024 				MDIO_PMA_REG_CTRL, &ctrl);
6025 		else
6026 			bnx2x_cl45_read(bp, phy,
6027 				MDIO_PMA_DEVAD,
6028 				MDIO_PMA_REG_CTRL, &ctrl);
6029 		if (!(ctrl & (1<<15)))
6030 			break;
6031 		usleep_range(1000, 2000);
6032 	}
6033 
6034 	if (cnt == 1000)
6035 		netdev_err(bp->dev,  "Warning: PHY was not initialized,"
6036 				      " Port %d\n",
6037 			 params->port);
6038 	DP(NETIF_MSG_LINK, "control reg 0x%x (after %d ms)\n", ctrl, cnt);
6039 	return cnt;
6040 }
6041 
6042 static void bnx2x_link_int_enable(struct link_params *params)
6043 {
6044 	u8 port = params->port;
6045 	u32 mask;
6046 	struct bnx2x *bp = params->bp;
6047 
6048 	/* Setting the status to report on link up for either XGXS or SerDes */
6049 	if (CHIP_IS_E3(bp)) {
6050 		mask = NIG_MASK_XGXS0_LINK_STATUS;
6051 		if (!(SINGLE_MEDIA_DIRECT(params)))
6052 			mask |= NIG_MASK_MI_INT;
6053 	} else if (params->switch_cfg == SWITCH_CFG_10G) {
6054 		mask = (NIG_MASK_XGXS0_LINK10G |
6055 			NIG_MASK_XGXS0_LINK_STATUS);
6056 		DP(NETIF_MSG_LINK, "enabled XGXS interrupt\n");
6057 		if (!(SINGLE_MEDIA_DIRECT(params)) &&
6058 			params->phy[INT_PHY].type !=
6059 				PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) {
6060 			mask |= NIG_MASK_MI_INT;
6061 			DP(NETIF_MSG_LINK, "enabled external phy int\n");
6062 		}
6063 
6064 	} else { /* SerDes */
6065 		mask = NIG_MASK_SERDES0_LINK_STATUS;
6066 		DP(NETIF_MSG_LINK, "enabled SerDes interrupt\n");
6067 		if (!(SINGLE_MEDIA_DIRECT(params)) &&
6068 			params->phy[INT_PHY].type !=
6069 				PORT_HW_CFG_SERDES_EXT_PHY_TYPE_NOT_CONN) {
6070 			mask |= NIG_MASK_MI_INT;
6071 			DP(NETIF_MSG_LINK, "enabled external phy int\n");
6072 		}
6073 	}
6074 	bnx2x_bits_en(bp,
6075 		      NIG_REG_MASK_INTERRUPT_PORT0 + port*4,
6076 		      mask);
6077 
6078 	DP(NETIF_MSG_LINK, "port %x, is_xgxs %x, int_status 0x%x\n", port,
6079 		 (params->switch_cfg == SWITCH_CFG_10G),
6080 		 REG_RD(bp, NIG_REG_STATUS_INTERRUPT_PORT0 + port*4));
6081 	DP(NETIF_MSG_LINK, " int_mask 0x%x, MI_INT %x, SERDES_LINK %x\n",
6082 		 REG_RD(bp, NIG_REG_MASK_INTERRUPT_PORT0 + port*4),
6083 		 REG_RD(bp, NIG_REG_EMAC0_STATUS_MISC_MI_INT + port*0x18),
6084 		 REG_RD(bp, NIG_REG_SERDES0_STATUS_LINK_STATUS+port*0x3c));
6085 	DP(NETIF_MSG_LINK, " 10G %x, XGXS_LINK %x\n",
6086 	   REG_RD(bp, NIG_REG_XGXS0_STATUS_LINK10G + port*0x68),
6087 	   REG_RD(bp, NIG_REG_XGXS0_STATUS_LINK_STATUS + port*0x68));
6088 }
6089 
6090 static void bnx2x_rearm_latch_signal(struct bnx2x *bp, u8 port,
6091 				     u8 exp_mi_int)
6092 {
6093 	u32 latch_status = 0;
6094 
6095 	/* Disable the MI INT ( external phy int ) by writing 1 to the
6096 	 * status register. Link down indication is high-active-signal,
6097 	 * so in this case we need to write the status to clear the XOR
6098 	 */
6099 	/* Read Latched signals */
6100 	latch_status = REG_RD(bp,
6101 				    NIG_REG_LATCH_STATUS_0 + port*8);
6102 	DP(NETIF_MSG_LINK, "latch_status = 0x%x\n", latch_status);
6103 	/* Handle only those with latched-signal=up.*/
6104 	if (exp_mi_int)
6105 		bnx2x_bits_en(bp,
6106 			      NIG_REG_STATUS_INTERRUPT_PORT0
6107 			      + port*4,
6108 			      NIG_STATUS_EMAC0_MI_INT);
6109 	else
6110 		bnx2x_bits_dis(bp,
6111 			       NIG_REG_STATUS_INTERRUPT_PORT0
6112 			       + port*4,
6113 			       NIG_STATUS_EMAC0_MI_INT);
6114 
6115 	if (latch_status & 1) {
6116 
6117 		/* For all latched-signal=up : Re-Arm Latch signals */
6118 		REG_WR(bp, NIG_REG_LATCH_STATUS_0 + port*8,
6119 		       (latch_status & 0xfffe) | (latch_status & 1));
6120 	}
6121 	/* For all latched-signal=up,Write original_signal to status */
6122 }
6123 
6124 static void bnx2x_link_int_ack(struct link_params *params,
6125 			       struct link_vars *vars, u8 is_10g_plus)
6126 {
6127 	struct bnx2x *bp = params->bp;
6128 	u8 port = params->port;
6129 	u32 mask;
6130 	/* First reset all status we assume only one line will be
6131 	 * change at a time
6132 	 */
6133 	bnx2x_bits_dis(bp, NIG_REG_STATUS_INTERRUPT_PORT0 + port*4,
6134 		       (NIG_STATUS_XGXS0_LINK10G |
6135 			NIG_STATUS_XGXS0_LINK_STATUS |
6136 			NIG_STATUS_SERDES0_LINK_STATUS));
6137 	if (vars->phy_link_up) {
6138 		if (USES_WARPCORE(bp))
6139 			mask = NIG_STATUS_XGXS0_LINK_STATUS;
6140 		else {
6141 			if (is_10g_plus)
6142 				mask = NIG_STATUS_XGXS0_LINK10G;
6143 			else if (params->switch_cfg == SWITCH_CFG_10G) {
6144 				/* Disable the link interrupt by writing 1 to
6145 				 * the relevant lane in the status register
6146 				 */
6147 				u32 ser_lane =
6148 					((params->lane_config &
6149 				    PORT_HW_CFG_LANE_SWAP_CFG_MASTER_MASK) >>
6150 				    PORT_HW_CFG_LANE_SWAP_CFG_MASTER_SHIFT);
6151 				mask = ((1 << ser_lane) <<
6152 				       NIG_STATUS_XGXS0_LINK_STATUS_SIZE);
6153 			} else
6154 				mask = NIG_STATUS_SERDES0_LINK_STATUS;
6155 		}
6156 		DP(NETIF_MSG_LINK, "Ack link up interrupt with mask 0x%x\n",
6157 			       mask);
6158 		bnx2x_bits_en(bp,
6159 			      NIG_REG_STATUS_INTERRUPT_PORT0 + port*4,
6160 			      mask);
6161 	}
6162 }
6163 
6164 static int bnx2x_format_ver(u32 num, u8 *str, u16 *len)
6165 {
6166 	u8 *str_ptr = str;
6167 	u32 mask = 0xf0000000;
6168 	u8 shift = 8*4;
6169 	u8 digit;
6170 	u8 remove_leading_zeros = 1;
6171 	if (*len < 10) {
6172 		/* Need more than 10chars for this format */
6173 		*str_ptr = '\0';
6174 		(*len)--;
6175 		return -EINVAL;
6176 	}
6177 	while (shift > 0) {
6178 
6179 		shift -= 4;
6180 		digit = ((num & mask) >> shift);
6181 		if (digit == 0 && remove_leading_zeros) {
6182 			*str_ptr = '0';
6183 		} else {
6184 			if (digit < 0xa)
6185 				*str_ptr = digit + '0';
6186 			else
6187 				*str_ptr = digit - 0xa + 'a';
6188 
6189 			remove_leading_zeros = 0;
6190 			str_ptr++;
6191 			(*len)--;
6192 		}
6193 		mask = mask >> 4;
6194 		if (shift == 4*4) {
6195 			if (remove_leading_zeros) {
6196 				str_ptr++;
6197 				(*len)--;
6198 			}
6199 			*str_ptr = '.';
6200 			str_ptr++;
6201 			(*len)--;
6202 			remove_leading_zeros = 1;
6203 		}
6204 	}
6205 	if (remove_leading_zeros)
6206 		(*len)--;
6207 	return 0;
6208 }
6209 
6210 static int bnx2x_3_seq_format_ver(u32 num, u8 *str, u16 *len)
6211 {
6212 	u8 *str_ptr = str;
6213 	u32 mask = 0x00f00000;
6214 	u8 shift = 8*3;
6215 	u8 digit;
6216 	u8 remove_leading_zeros = 1;
6217 
6218 	if (*len < 10) {
6219 		/* Need more than 10chars for this format */
6220 		*str_ptr = '\0';
6221 		(*len)--;
6222 		return -EINVAL;
6223 	}
6224 
6225 	while (shift > 0) {
6226 		shift -= 4;
6227 		digit = ((num & mask) >> shift);
6228 		if (digit == 0 && remove_leading_zeros) {
6229 			*str_ptr = '0';
6230 		} else {
6231 			if (digit < 0xa)
6232 				*str_ptr = digit + '0';
6233 			else
6234 				*str_ptr = digit - 0xa + 'a';
6235 
6236 			remove_leading_zeros = 0;
6237 			str_ptr++;
6238 			(*len)--;
6239 		}
6240 		mask = mask >> 4;
6241 		if ((shift == 4*4) || (shift == 4*2)) {
6242 			if (remove_leading_zeros) {
6243 				str_ptr++;
6244 				(*len)--;
6245 			}
6246 			*str_ptr = '.';
6247 			str_ptr++;
6248 			(*len)--;
6249 			remove_leading_zeros = 1;
6250 		}
6251 	}
6252 	if (remove_leading_zeros)
6253 		(*len)--;
6254 	return 0;
6255 }
6256 
6257 static int bnx2x_null_format_ver(u32 spirom_ver, u8 *str, u16 *len)
6258 {
6259 	str[0] = '\0';
6260 	(*len)--;
6261 	return 0;
6262 }
6263 
6264 int bnx2x_get_ext_phy_fw_version(struct link_params *params, u8 *version,
6265 				 u16 len)
6266 {
6267 	struct bnx2x *bp;
6268 	u32 spirom_ver = 0;
6269 	int status = 0;
6270 	u8 *ver_p = version;
6271 	u16 remain_len = len;
6272 	if (version == NULL || params == NULL)
6273 		return -EINVAL;
6274 	bp = params->bp;
6275 
6276 	/* Extract first external phy*/
6277 	version[0] = '\0';
6278 	spirom_ver = REG_RD(bp, params->phy[EXT_PHY1].ver_addr);
6279 
6280 	if (params->phy[EXT_PHY1].format_fw_ver) {
6281 		status |= params->phy[EXT_PHY1].format_fw_ver(spirom_ver,
6282 							      ver_p,
6283 							      &remain_len);
6284 		ver_p += (len - remain_len);
6285 	}
6286 	if ((params->num_phys == MAX_PHYS) &&
6287 	    (params->phy[EXT_PHY2].ver_addr != 0)) {
6288 		spirom_ver = REG_RD(bp, params->phy[EXT_PHY2].ver_addr);
6289 		if (params->phy[EXT_PHY2].format_fw_ver) {
6290 			*ver_p = '/';
6291 			ver_p++;
6292 			remain_len--;
6293 			status |= params->phy[EXT_PHY2].format_fw_ver(
6294 				spirom_ver,
6295 				ver_p,
6296 				&remain_len);
6297 			ver_p = version + (len - remain_len);
6298 		}
6299 	}
6300 	*ver_p = '\0';
6301 	return status;
6302 }
6303 
6304 static void bnx2x_set_xgxs_loopback(struct bnx2x_phy *phy,
6305 				    struct link_params *params)
6306 {
6307 	u8 port = params->port;
6308 	struct bnx2x *bp = params->bp;
6309 
6310 	if (phy->req_line_speed != SPEED_1000) {
6311 		u32 md_devad = 0;
6312 
6313 		DP(NETIF_MSG_LINK, "XGXS 10G loopback enable\n");
6314 
6315 		if (!CHIP_IS_E3(bp)) {
6316 			/* Change the uni_phy_addr in the nig */
6317 			md_devad = REG_RD(bp, (NIG_REG_XGXS0_CTRL_MD_DEVAD +
6318 					       port*0x18));
6319 
6320 			REG_WR(bp, NIG_REG_XGXS0_CTRL_MD_DEVAD + port*0x18,
6321 			       0x5);
6322 		}
6323 
6324 		bnx2x_cl45_write(bp, phy,
6325 				 5,
6326 				 (MDIO_REG_BANK_AER_BLOCK +
6327 				  (MDIO_AER_BLOCK_AER_REG & 0xf)),
6328 				 0x2800);
6329 
6330 		bnx2x_cl45_write(bp, phy,
6331 				 5,
6332 				 (MDIO_REG_BANK_CL73_IEEEB0 +
6333 				  (MDIO_CL73_IEEEB0_CL73_AN_CONTROL & 0xf)),
6334 				 0x6041);
6335 		msleep(200);
6336 		/* Set aer mmd back */
6337 		bnx2x_set_aer_mmd(params, phy);
6338 
6339 		if (!CHIP_IS_E3(bp)) {
6340 			/* And md_devad */
6341 			REG_WR(bp, NIG_REG_XGXS0_CTRL_MD_DEVAD + port*0x18,
6342 			       md_devad);
6343 		}
6344 	} else {
6345 		u16 mii_ctrl;
6346 		DP(NETIF_MSG_LINK, "XGXS 1G loopback enable\n");
6347 		bnx2x_cl45_read(bp, phy, 5,
6348 				(MDIO_REG_BANK_COMBO_IEEE0 +
6349 				(MDIO_COMBO_IEEE0_MII_CONTROL & 0xf)),
6350 				&mii_ctrl);
6351 		bnx2x_cl45_write(bp, phy, 5,
6352 				 (MDIO_REG_BANK_COMBO_IEEE0 +
6353 				 (MDIO_COMBO_IEEE0_MII_CONTROL & 0xf)),
6354 				 mii_ctrl |
6355 				 MDIO_COMBO_IEEO_MII_CONTROL_LOOPBACK);
6356 	}
6357 }
6358 
6359 int bnx2x_set_led(struct link_params *params,
6360 		  struct link_vars *vars, u8 mode, u32 speed)
6361 {
6362 	u8 port = params->port;
6363 	u16 hw_led_mode = params->hw_led_mode;
6364 	int rc = 0;
6365 	u8 phy_idx;
6366 	u32 tmp;
6367 	u32 emac_base = port ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
6368 	struct bnx2x *bp = params->bp;
6369 	DP(NETIF_MSG_LINK, "bnx2x_set_led: port %x, mode %d\n", port, mode);
6370 	DP(NETIF_MSG_LINK, "speed 0x%x, hw_led_mode 0x%x\n",
6371 		 speed, hw_led_mode);
6372 	/* In case */
6373 	for (phy_idx = EXT_PHY1; phy_idx < MAX_PHYS; phy_idx++) {
6374 		if (params->phy[phy_idx].set_link_led) {
6375 			params->phy[phy_idx].set_link_led(
6376 				&params->phy[phy_idx], params, mode);
6377 		}
6378 	}
6379 
6380 	switch (mode) {
6381 	case LED_MODE_FRONT_PANEL_OFF:
6382 	case LED_MODE_OFF:
6383 		REG_WR(bp, NIG_REG_LED_10G_P0 + port*4, 0);
6384 		REG_WR(bp, NIG_REG_LED_MODE_P0 + port*4,
6385 		       SHARED_HW_CFG_LED_MAC1);
6386 
6387 		tmp = EMAC_RD(bp, EMAC_REG_EMAC_LED);
6388 		if (params->phy[EXT_PHY1].type ==
6389 			PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54618SE)
6390 			tmp &= ~(EMAC_LED_1000MB_OVERRIDE |
6391 				EMAC_LED_100MB_OVERRIDE |
6392 				EMAC_LED_10MB_OVERRIDE);
6393 		else
6394 			tmp |= EMAC_LED_OVERRIDE;
6395 
6396 		EMAC_WR(bp, EMAC_REG_EMAC_LED, tmp);
6397 		break;
6398 
6399 	case LED_MODE_OPER:
6400 		/* For all other phys, OPER mode is same as ON, so in case
6401 		 * link is down, do nothing
6402 		 */
6403 		if (!vars->link_up)
6404 			break;
6405 	case LED_MODE_ON:
6406 		if (((params->phy[EXT_PHY1].type ==
6407 			  PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727) ||
6408 			 (params->phy[EXT_PHY1].type ==
6409 			  PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8722)) &&
6410 		    CHIP_IS_E2(bp) && params->num_phys == 2) {
6411 			/* This is a work-around for E2+8727 Configurations */
6412 			if (mode == LED_MODE_ON ||
6413 				speed == SPEED_10000){
6414 				REG_WR(bp, NIG_REG_LED_MODE_P0 + port*4, 0);
6415 				REG_WR(bp, NIG_REG_LED_10G_P0 + port*4, 1);
6416 
6417 				tmp = EMAC_RD(bp, EMAC_REG_EMAC_LED);
6418 				EMAC_WR(bp, EMAC_REG_EMAC_LED,
6419 					(tmp | EMAC_LED_OVERRIDE));
6420 				/* Return here without enabling traffic
6421 				 * LED blink and setting rate in ON mode.
6422 				 * In oper mode, enabling LED blink
6423 				 * and setting rate is needed.
6424 				 */
6425 				if (mode == LED_MODE_ON)
6426 					return rc;
6427 			}
6428 		} else if (SINGLE_MEDIA_DIRECT(params)) {
6429 			/* This is a work-around for HW issue found when link
6430 			 * is up in CL73
6431 			 */
6432 			if ((!CHIP_IS_E3(bp)) ||
6433 			    (CHIP_IS_E3(bp) &&
6434 			     mode == LED_MODE_ON))
6435 				REG_WR(bp, NIG_REG_LED_10G_P0 + port*4, 1);
6436 
6437 			if (CHIP_IS_E1x(bp) ||
6438 			    CHIP_IS_E2(bp) ||
6439 			    (mode == LED_MODE_ON))
6440 				REG_WR(bp, NIG_REG_LED_MODE_P0 + port*4, 0);
6441 			else
6442 				REG_WR(bp, NIG_REG_LED_MODE_P0 + port*4,
6443 				       hw_led_mode);
6444 		} else if ((params->phy[EXT_PHY1].type ==
6445 			    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54618SE) &&
6446 			   (mode == LED_MODE_ON)) {
6447 			REG_WR(bp, NIG_REG_LED_MODE_P0 + port*4, 0);
6448 			tmp = EMAC_RD(bp, EMAC_REG_EMAC_LED);
6449 			EMAC_WR(bp, EMAC_REG_EMAC_LED, tmp |
6450 				EMAC_LED_OVERRIDE | EMAC_LED_1000MB_OVERRIDE);
6451 			/* Break here; otherwise, it'll disable the
6452 			 * intended override.
6453 			 */
6454 			break;
6455 		} else {
6456 			u32 nig_led_mode = ((params->hw_led_mode <<
6457 					     SHARED_HW_CFG_LED_MODE_SHIFT) ==
6458 					    SHARED_HW_CFG_LED_EXTPHY2) ?
6459 				(SHARED_HW_CFG_LED_PHY1 >>
6460 				 SHARED_HW_CFG_LED_MODE_SHIFT) : hw_led_mode;
6461 			REG_WR(bp, NIG_REG_LED_MODE_P0 + port*4,
6462 			       nig_led_mode);
6463 		}
6464 
6465 		REG_WR(bp, NIG_REG_LED_CONTROL_OVERRIDE_TRAFFIC_P0 + port*4, 0);
6466 		/* Set blinking rate to ~15.9Hz */
6467 		if (CHIP_IS_E3(bp))
6468 			REG_WR(bp, NIG_REG_LED_CONTROL_BLINK_RATE_P0 + port*4,
6469 			       LED_BLINK_RATE_VAL_E3);
6470 		else
6471 			REG_WR(bp, NIG_REG_LED_CONTROL_BLINK_RATE_P0 + port*4,
6472 			       LED_BLINK_RATE_VAL_E1X_E2);
6473 		REG_WR(bp, NIG_REG_LED_CONTROL_BLINK_RATE_ENA_P0 +
6474 		       port*4, 1);
6475 		tmp = EMAC_RD(bp, EMAC_REG_EMAC_LED);
6476 		EMAC_WR(bp, EMAC_REG_EMAC_LED,
6477 			(tmp & (~EMAC_LED_OVERRIDE)));
6478 
6479 		if (CHIP_IS_E1(bp) &&
6480 		    ((speed == SPEED_2500) ||
6481 		     (speed == SPEED_1000) ||
6482 		     (speed == SPEED_100) ||
6483 		     (speed == SPEED_10))) {
6484 			/* For speeds less than 10G LED scheme is different */
6485 			REG_WR(bp, NIG_REG_LED_CONTROL_OVERRIDE_TRAFFIC_P0
6486 			       + port*4, 1);
6487 			REG_WR(bp, NIG_REG_LED_CONTROL_TRAFFIC_P0 +
6488 			       port*4, 0);
6489 			REG_WR(bp, NIG_REG_LED_CONTROL_BLINK_TRAFFIC_P0 +
6490 			       port*4, 1);
6491 		}
6492 		break;
6493 
6494 	default:
6495 		rc = -EINVAL;
6496 		DP(NETIF_MSG_LINK, "bnx2x_set_led: Invalid led mode %d\n",
6497 			 mode);
6498 		break;
6499 	}
6500 	return rc;
6501 
6502 }
6503 
6504 /* This function comes to reflect the actual link state read DIRECTLY from the
6505  * HW
6506  */
6507 int bnx2x_test_link(struct link_params *params, struct link_vars *vars,
6508 		    u8 is_serdes)
6509 {
6510 	struct bnx2x *bp = params->bp;
6511 	u16 gp_status = 0, phy_index = 0;
6512 	u8 ext_phy_link_up = 0, serdes_phy_type;
6513 	struct link_vars temp_vars;
6514 	struct bnx2x_phy *int_phy = &params->phy[INT_PHY];
6515 
6516 	if (CHIP_IS_E3(bp)) {
6517 		u16 link_up;
6518 		if (params->req_line_speed[LINK_CONFIG_IDX(INT_PHY)]
6519 		    > SPEED_10000) {
6520 			/* Check 20G link */
6521 			bnx2x_cl45_read(bp, int_phy, MDIO_WC_DEVAD,
6522 					1, &link_up);
6523 			bnx2x_cl45_read(bp, int_phy, MDIO_WC_DEVAD,
6524 					1, &link_up);
6525 			link_up &= (1<<2);
6526 		} else {
6527 			/* Check 10G link and below*/
6528 			u8 lane = bnx2x_get_warpcore_lane(int_phy, params);
6529 			bnx2x_cl45_read(bp, int_phy, MDIO_WC_DEVAD,
6530 					MDIO_WC_REG_GP2_STATUS_GP_2_1,
6531 					&gp_status);
6532 			gp_status = ((gp_status >> 8) & 0xf) |
6533 				((gp_status >> 12) & 0xf);
6534 			link_up = gp_status & (1 << lane);
6535 		}
6536 		if (!link_up)
6537 			return -ESRCH;
6538 	} else {
6539 		CL22_RD_OVER_CL45(bp, int_phy,
6540 			  MDIO_REG_BANK_GP_STATUS,
6541 			  MDIO_GP_STATUS_TOP_AN_STATUS1,
6542 			  &gp_status);
6543 	/* Link is up only if both local phy and external phy are up */
6544 	if (!(gp_status & MDIO_GP_STATUS_TOP_AN_STATUS1_LINK_STATUS))
6545 		return -ESRCH;
6546 	}
6547 	/* In XGXS loopback mode, do not check external PHY */
6548 	if (params->loopback_mode == LOOPBACK_XGXS)
6549 		return 0;
6550 
6551 	switch (params->num_phys) {
6552 	case 1:
6553 		/* No external PHY */
6554 		return 0;
6555 	case 2:
6556 		ext_phy_link_up = params->phy[EXT_PHY1].read_status(
6557 			&params->phy[EXT_PHY1],
6558 			params, &temp_vars);
6559 		break;
6560 	case 3: /* Dual Media */
6561 		for (phy_index = EXT_PHY1; phy_index < params->num_phys;
6562 		      phy_index++) {
6563 			serdes_phy_type = ((params->phy[phy_index].media_type ==
6564 					    ETH_PHY_SFPP_10G_FIBER) ||
6565 					   (params->phy[phy_index].media_type ==
6566 					    ETH_PHY_SFP_1G_FIBER) ||
6567 					   (params->phy[phy_index].media_type ==
6568 					    ETH_PHY_XFP_FIBER) ||
6569 					   (params->phy[phy_index].media_type ==
6570 					    ETH_PHY_DA_TWINAX));
6571 
6572 			if (is_serdes != serdes_phy_type)
6573 				continue;
6574 			if (params->phy[phy_index].read_status) {
6575 				ext_phy_link_up |=
6576 					params->phy[phy_index].read_status(
6577 						&params->phy[phy_index],
6578 						params, &temp_vars);
6579 			}
6580 		}
6581 		break;
6582 	}
6583 	if (ext_phy_link_up)
6584 		return 0;
6585 	return -ESRCH;
6586 }
6587 
6588 static int bnx2x_link_initialize(struct link_params *params,
6589 				 struct link_vars *vars)
6590 {
6591 	u8 phy_index, non_ext_phy;
6592 	struct bnx2x *bp = params->bp;
6593 	/* In case of external phy existence, the line speed would be the
6594 	 * line speed linked up by the external phy. In case it is direct
6595 	 * only, then the line_speed during initialization will be
6596 	 * equal to the req_line_speed
6597 	 */
6598 	vars->line_speed = params->phy[INT_PHY].req_line_speed;
6599 
6600 	/* Initialize the internal phy in case this is a direct board
6601 	 * (no external phys), or this board has external phy which requires
6602 	 * to first.
6603 	 */
6604 	if (!USES_WARPCORE(bp))
6605 		bnx2x_prepare_xgxs(&params->phy[INT_PHY], params, vars);
6606 	/* init ext phy and enable link state int */
6607 	non_ext_phy = (SINGLE_MEDIA_DIRECT(params) ||
6608 		       (params->loopback_mode == LOOPBACK_XGXS));
6609 
6610 	if (non_ext_phy ||
6611 	    (params->phy[EXT_PHY1].flags & FLAGS_INIT_XGXS_FIRST) ||
6612 	    (params->loopback_mode == LOOPBACK_EXT_PHY)) {
6613 		struct bnx2x_phy *phy = &params->phy[INT_PHY];
6614 		if (vars->line_speed == SPEED_AUTO_NEG &&
6615 		    (CHIP_IS_E1x(bp) ||
6616 		     CHIP_IS_E2(bp)))
6617 			bnx2x_set_parallel_detection(phy, params);
6618 		if (params->phy[INT_PHY].config_init)
6619 			params->phy[INT_PHY].config_init(phy, params, vars);
6620 	}
6621 
6622 	/* Re-read this value in case it was changed inside config_init due to
6623 	 * limitations of optic module
6624 	 */
6625 	vars->line_speed = params->phy[INT_PHY].req_line_speed;
6626 
6627 	/* Init external phy*/
6628 	if (non_ext_phy) {
6629 		if (params->phy[INT_PHY].supported &
6630 		    SUPPORTED_FIBRE)
6631 			vars->link_status |= LINK_STATUS_SERDES_LINK;
6632 	} else {
6633 		for (phy_index = EXT_PHY1; phy_index < params->num_phys;
6634 		      phy_index++) {
6635 			/* No need to initialize second phy in case of first
6636 			 * phy only selection. In case of second phy, we do
6637 			 * need to initialize the first phy, since they are
6638 			 * connected.
6639 			 */
6640 			if (params->phy[phy_index].supported &
6641 			    SUPPORTED_FIBRE)
6642 				vars->link_status |= LINK_STATUS_SERDES_LINK;
6643 
6644 			if (phy_index == EXT_PHY2 &&
6645 			    (bnx2x_phy_selection(params) ==
6646 			     PORT_HW_CFG_PHY_SELECTION_FIRST_PHY)) {
6647 				DP(NETIF_MSG_LINK,
6648 				   "Not initializing second phy\n");
6649 				continue;
6650 			}
6651 			params->phy[phy_index].config_init(
6652 				&params->phy[phy_index],
6653 				params, vars);
6654 		}
6655 	}
6656 	/* Reset the interrupt indication after phy was initialized */
6657 	bnx2x_bits_dis(bp, NIG_REG_STATUS_INTERRUPT_PORT0 +
6658 		       params->port*4,
6659 		       (NIG_STATUS_XGXS0_LINK10G |
6660 			NIG_STATUS_XGXS0_LINK_STATUS |
6661 			NIG_STATUS_SERDES0_LINK_STATUS |
6662 			NIG_MASK_MI_INT));
6663 	return 0;
6664 }
6665 
6666 static void bnx2x_int_link_reset(struct bnx2x_phy *phy,
6667 				 struct link_params *params)
6668 {
6669 	/* Reset the SerDes/XGXS */
6670 	REG_WR(params->bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_3_CLEAR,
6671 	       (0x1ff << (params->port*16)));
6672 }
6673 
6674 static void bnx2x_common_ext_link_reset(struct bnx2x_phy *phy,
6675 					struct link_params *params)
6676 {
6677 	struct bnx2x *bp = params->bp;
6678 	u8 gpio_port;
6679 	/* HW reset */
6680 	if (CHIP_IS_E2(bp))
6681 		gpio_port = BP_PATH(bp);
6682 	else
6683 		gpio_port = params->port;
6684 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_1,
6685 		       MISC_REGISTERS_GPIO_OUTPUT_LOW,
6686 		       gpio_port);
6687 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
6688 		       MISC_REGISTERS_GPIO_OUTPUT_LOW,
6689 		       gpio_port);
6690 	DP(NETIF_MSG_LINK, "reset external PHY\n");
6691 }
6692 
6693 static int bnx2x_update_link_down(struct link_params *params,
6694 				  struct link_vars *vars)
6695 {
6696 	struct bnx2x *bp = params->bp;
6697 	u8 port = params->port;
6698 
6699 	DP(NETIF_MSG_LINK, "Port %x: Link is down\n", port);
6700 	bnx2x_set_led(params, vars, LED_MODE_OFF, 0);
6701 	vars->phy_flags &= ~PHY_PHYSICAL_LINK_FLAG;
6702 	/* Indicate no mac active */
6703 	vars->mac_type = MAC_TYPE_NONE;
6704 
6705 	/* Update shared memory */
6706 	vars->link_status &= ~LINK_UPDATE_MASK;
6707 	vars->line_speed = 0;
6708 	bnx2x_update_mng(params, vars->link_status);
6709 
6710 	/* Activate nig drain */
6711 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + port*4, 1);
6712 
6713 	/* Disable emac */
6714 	if (!CHIP_IS_E3(bp))
6715 		REG_WR(bp, NIG_REG_NIG_EMAC0_EN + port*4, 0);
6716 
6717 	usleep_range(10000, 20000);
6718 	/* Reset BigMac/Xmac */
6719 	if (CHIP_IS_E1x(bp) ||
6720 	    CHIP_IS_E2(bp))
6721 		bnx2x_set_bmac_rx(bp, params->chip_id, params->port, 0);
6722 
6723 	if (CHIP_IS_E3(bp)) {
6724 		/* Prevent LPI Generation by chip */
6725 		REG_WR(bp, MISC_REG_CPMU_LP_FW_ENABLE_P0 + (params->port << 2),
6726 		       0);
6727 		REG_WR(bp, MISC_REG_CPMU_LP_MASK_ENT_P0 + (params->port << 2),
6728 		       0);
6729 		vars->eee_status &= ~(SHMEM_EEE_LP_ADV_STATUS_MASK |
6730 				      SHMEM_EEE_ACTIVE_BIT);
6731 
6732 		bnx2x_update_mng_eee(params, vars->eee_status);
6733 		bnx2x_set_xmac_rxtx(params, 0);
6734 		bnx2x_set_umac_rxtx(params, 0);
6735 	}
6736 
6737 	return 0;
6738 }
6739 
6740 static int bnx2x_update_link_up(struct link_params *params,
6741 				struct link_vars *vars,
6742 				u8 link_10g)
6743 {
6744 	struct bnx2x *bp = params->bp;
6745 	u8 phy_idx, port = params->port;
6746 	int rc = 0;
6747 
6748 	vars->link_status |= (LINK_STATUS_LINK_UP |
6749 			      LINK_STATUS_PHYSICAL_LINK_FLAG);
6750 	vars->phy_flags |= PHY_PHYSICAL_LINK_FLAG;
6751 
6752 	if (vars->flow_ctrl & BNX2X_FLOW_CTRL_TX)
6753 		vars->link_status |=
6754 			LINK_STATUS_TX_FLOW_CONTROL_ENABLED;
6755 
6756 	if (vars->flow_ctrl & BNX2X_FLOW_CTRL_RX)
6757 		vars->link_status |=
6758 			LINK_STATUS_RX_FLOW_CONTROL_ENABLED;
6759 	if (USES_WARPCORE(bp)) {
6760 		if (link_10g) {
6761 			if (bnx2x_xmac_enable(params, vars, 0) ==
6762 			    -ESRCH) {
6763 				DP(NETIF_MSG_LINK, "Found errors on XMAC\n");
6764 				vars->link_up = 0;
6765 				vars->phy_flags |= PHY_HALF_OPEN_CONN_FLAG;
6766 				vars->link_status &= ~LINK_STATUS_LINK_UP;
6767 			}
6768 		} else
6769 			bnx2x_umac_enable(params, vars, 0);
6770 		bnx2x_set_led(params, vars,
6771 			      LED_MODE_OPER, vars->line_speed);
6772 
6773 		if ((vars->eee_status & SHMEM_EEE_ACTIVE_BIT) &&
6774 		    (vars->eee_status & SHMEM_EEE_LPI_REQUESTED_BIT)) {
6775 			DP(NETIF_MSG_LINK, "Enabling LPI assertion\n");
6776 			REG_WR(bp, MISC_REG_CPMU_LP_FW_ENABLE_P0 +
6777 			       (params->port << 2), 1);
6778 			REG_WR(bp, MISC_REG_CPMU_LP_DR_ENABLE, 1);
6779 			REG_WR(bp, MISC_REG_CPMU_LP_MASK_ENT_P0 +
6780 			       (params->port << 2), 0xfc20);
6781 		}
6782 	}
6783 	if ((CHIP_IS_E1x(bp) ||
6784 	     CHIP_IS_E2(bp))) {
6785 		if (link_10g) {
6786 			if (bnx2x_bmac_enable(params, vars, 0, 1) ==
6787 			    -ESRCH) {
6788 				DP(NETIF_MSG_LINK, "Found errors on BMAC\n");
6789 				vars->link_up = 0;
6790 				vars->phy_flags |= PHY_HALF_OPEN_CONN_FLAG;
6791 				vars->link_status &= ~LINK_STATUS_LINK_UP;
6792 			}
6793 
6794 			bnx2x_set_led(params, vars,
6795 				      LED_MODE_OPER, SPEED_10000);
6796 		} else {
6797 			rc = bnx2x_emac_program(params, vars);
6798 			bnx2x_emac_enable(params, vars, 0);
6799 
6800 			/* AN complete? */
6801 			if ((vars->link_status &
6802 			     LINK_STATUS_AUTO_NEGOTIATE_COMPLETE)
6803 			    && (!(vars->phy_flags & PHY_SGMII_FLAG)) &&
6804 			    SINGLE_MEDIA_DIRECT(params))
6805 				bnx2x_set_gmii_tx_driver(params);
6806 		}
6807 	}
6808 
6809 	/* PBF - link up */
6810 	if (CHIP_IS_E1x(bp))
6811 		rc |= bnx2x_pbf_update(params, vars->flow_ctrl,
6812 				       vars->line_speed);
6813 
6814 	/* Disable drain */
6815 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + port*4, 0);
6816 
6817 	/* Update shared memory */
6818 	bnx2x_update_mng(params, vars->link_status);
6819 	bnx2x_update_mng_eee(params, vars->eee_status);
6820 	/* Check remote fault */
6821 	for (phy_idx = INT_PHY; phy_idx < MAX_PHYS; phy_idx++) {
6822 		if (params->phy[phy_idx].flags & FLAGS_TX_ERROR_CHECK) {
6823 			bnx2x_check_half_open_conn(params, vars, 0);
6824 			break;
6825 		}
6826 	}
6827 	msleep(20);
6828 	return rc;
6829 }
6830 
6831 static void bnx2x_chng_link_count(struct link_params *params, bool clear)
6832 {
6833 	struct bnx2x *bp = params->bp;
6834 	u32 addr, val;
6835 
6836 	/* Verify the link_change_count is supported by the MFW */
6837 	if (!(SHMEM2_HAS(bp, link_change_count)))
6838 		return;
6839 
6840 	addr = params->shmem2_base +
6841 		offsetof(struct shmem2_region, link_change_count[params->port]);
6842 	if (clear)
6843 		val = 0;
6844 	else
6845 		val = REG_RD(bp, addr) + 1;
6846 	REG_WR(bp, addr, val);
6847 }
6848 
6849 /* The bnx2x_link_update function should be called upon link
6850  * interrupt.
6851  * Link is considered up as follows:
6852  * - DIRECT_SINGLE_MEDIA - Only XGXS link (internal link) needs
6853  *   to be up
6854  * - SINGLE_MEDIA - The link between the 577xx and the external
6855  *   phy (XGXS) need to up as well as the external link of the
6856  *   phy (PHY_EXT1)
6857  * - DUAL_MEDIA - The link between the 577xx and the first
6858  *   external phy needs to be up, and at least one of the 2
6859  *   external phy link must be up.
6860  */
6861 int bnx2x_link_update(struct link_params *params, struct link_vars *vars)
6862 {
6863 	struct bnx2x *bp = params->bp;
6864 	struct link_vars phy_vars[MAX_PHYS];
6865 	u8 port = params->port;
6866 	u8 link_10g_plus, phy_index;
6867 	u32 prev_link_status = vars->link_status;
6868 	u8 ext_phy_link_up = 0, cur_link_up;
6869 	int rc = 0;
6870 	u8 is_mi_int = 0;
6871 	u16 ext_phy_line_speed = 0, prev_line_speed = vars->line_speed;
6872 	u8 active_external_phy = INT_PHY;
6873 	vars->phy_flags &= ~PHY_HALF_OPEN_CONN_FLAG;
6874 	vars->link_status &= ~LINK_UPDATE_MASK;
6875 	for (phy_index = INT_PHY; phy_index < params->num_phys;
6876 	      phy_index++) {
6877 		phy_vars[phy_index].flow_ctrl = 0;
6878 		phy_vars[phy_index].link_status = 0;
6879 		phy_vars[phy_index].line_speed = 0;
6880 		phy_vars[phy_index].duplex = DUPLEX_FULL;
6881 		phy_vars[phy_index].phy_link_up = 0;
6882 		phy_vars[phy_index].link_up = 0;
6883 		phy_vars[phy_index].fault_detected = 0;
6884 		/* different consideration, since vars holds inner state */
6885 		phy_vars[phy_index].eee_status = vars->eee_status;
6886 	}
6887 
6888 	if (USES_WARPCORE(bp))
6889 		bnx2x_set_aer_mmd(params, &params->phy[INT_PHY]);
6890 
6891 	DP(NETIF_MSG_LINK, "port %x, XGXS?%x, int_status 0x%x\n",
6892 		 port, (vars->phy_flags & PHY_XGXS_FLAG),
6893 		 REG_RD(bp, NIG_REG_STATUS_INTERRUPT_PORT0 + port*4));
6894 
6895 	is_mi_int = (u8)(REG_RD(bp, NIG_REG_EMAC0_STATUS_MISC_MI_INT +
6896 				port*0x18) > 0);
6897 	DP(NETIF_MSG_LINK, "int_mask 0x%x MI_INT %x, SERDES_LINK %x\n",
6898 		 REG_RD(bp, NIG_REG_MASK_INTERRUPT_PORT0 + port*4),
6899 		 is_mi_int,
6900 		 REG_RD(bp, NIG_REG_SERDES0_STATUS_LINK_STATUS + port*0x3c));
6901 
6902 	DP(NETIF_MSG_LINK, " 10G %x, XGXS_LINK %x\n",
6903 	  REG_RD(bp, NIG_REG_XGXS0_STATUS_LINK10G + port*0x68),
6904 	  REG_RD(bp, NIG_REG_XGXS0_STATUS_LINK_STATUS + port*0x68));
6905 
6906 	/* Disable emac */
6907 	if (!CHIP_IS_E3(bp))
6908 		REG_WR(bp, NIG_REG_NIG_EMAC0_EN + port*4, 0);
6909 
6910 	/* Step 1:
6911 	 * Check external link change only for external phys, and apply
6912 	 * priority selection between them in case the link on both phys
6913 	 * is up. Note that instead of the common vars, a temporary
6914 	 * vars argument is used since each phy may have different link/
6915 	 * speed/duplex result
6916 	 */
6917 	for (phy_index = EXT_PHY1; phy_index < params->num_phys;
6918 	      phy_index++) {
6919 		struct bnx2x_phy *phy = &params->phy[phy_index];
6920 		if (!phy->read_status)
6921 			continue;
6922 		/* Read link status and params of this ext phy */
6923 		cur_link_up = phy->read_status(phy, params,
6924 					       &phy_vars[phy_index]);
6925 		if (cur_link_up) {
6926 			DP(NETIF_MSG_LINK, "phy in index %d link is up\n",
6927 				   phy_index);
6928 		} else {
6929 			DP(NETIF_MSG_LINK, "phy in index %d link is down\n",
6930 				   phy_index);
6931 			continue;
6932 		}
6933 
6934 		if (!ext_phy_link_up) {
6935 			ext_phy_link_up = 1;
6936 			active_external_phy = phy_index;
6937 		} else {
6938 			switch (bnx2x_phy_selection(params)) {
6939 			case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
6940 			case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
6941 			/* In this option, the first PHY makes sure to pass the
6942 			 * traffic through itself only.
6943 			 * Its not clear how to reset the link on the second phy
6944 			 */
6945 				active_external_phy = EXT_PHY1;
6946 				break;
6947 			case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
6948 			/* In this option, the first PHY makes sure to pass the
6949 			 * traffic through the second PHY.
6950 			 */
6951 				active_external_phy = EXT_PHY2;
6952 				break;
6953 			default:
6954 			/* Link indication on both PHYs with the following cases
6955 			 * is invalid:
6956 			 * - FIRST_PHY means that second phy wasn't initialized,
6957 			 * hence its link is expected to be down
6958 			 * - SECOND_PHY means that first phy should not be able
6959 			 * to link up by itself (using configuration)
6960 			 * - DEFAULT should be overridden during initialization
6961 			 */
6962 				DP(NETIF_MSG_LINK, "Invalid link indication"
6963 					   "mpc=0x%x. DISABLING LINK !!!\n",
6964 					   params->multi_phy_config);
6965 				ext_phy_link_up = 0;
6966 				break;
6967 			}
6968 		}
6969 	}
6970 	prev_line_speed = vars->line_speed;
6971 	/* Step 2:
6972 	 * Read the status of the internal phy. In case of
6973 	 * DIRECT_SINGLE_MEDIA board, this link is the external link,
6974 	 * otherwise this is the link between the 577xx and the first
6975 	 * external phy
6976 	 */
6977 	if (params->phy[INT_PHY].read_status)
6978 		params->phy[INT_PHY].read_status(
6979 			&params->phy[INT_PHY],
6980 			params, vars);
6981 	/* The INT_PHY flow control reside in the vars. This include the
6982 	 * case where the speed or flow control are not set to AUTO.
6983 	 * Otherwise, the active external phy flow control result is set
6984 	 * to the vars. The ext_phy_line_speed is needed to check if the
6985 	 * speed is different between the internal phy and external phy.
6986 	 * This case may be result of intermediate link speed change.
6987 	 */
6988 	if (active_external_phy > INT_PHY) {
6989 		vars->flow_ctrl = phy_vars[active_external_phy].flow_ctrl;
6990 		/* Link speed is taken from the XGXS. AN and FC result from
6991 		 * the external phy.
6992 		 */
6993 		vars->link_status |= phy_vars[active_external_phy].link_status;
6994 
6995 		/* if active_external_phy is first PHY and link is up - disable
6996 		 * disable TX on second external PHY
6997 		 */
6998 		if (active_external_phy == EXT_PHY1) {
6999 			if (params->phy[EXT_PHY2].phy_specific_func) {
7000 				DP(NETIF_MSG_LINK,
7001 				   "Disabling TX on EXT_PHY2\n");
7002 				params->phy[EXT_PHY2].phy_specific_func(
7003 					&params->phy[EXT_PHY2],
7004 					params, DISABLE_TX);
7005 			}
7006 		}
7007 
7008 		ext_phy_line_speed = phy_vars[active_external_phy].line_speed;
7009 		vars->duplex = phy_vars[active_external_phy].duplex;
7010 		if (params->phy[active_external_phy].supported &
7011 		    SUPPORTED_FIBRE)
7012 			vars->link_status |= LINK_STATUS_SERDES_LINK;
7013 		else
7014 			vars->link_status &= ~LINK_STATUS_SERDES_LINK;
7015 
7016 		vars->eee_status = phy_vars[active_external_phy].eee_status;
7017 
7018 		DP(NETIF_MSG_LINK, "Active external phy selected: %x\n",
7019 			   active_external_phy);
7020 	}
7021 
7022 	for (phy_index = EXT_PHY1; phy_index < params->num_phys;
7023 	      phy_index++) {
7024 		if (params->phy[phy_index].flags &
7025 		    FLAGS_REARM_LATCH_SIGNAL) {
7026 			bnx2x_rearm_latch_signal(bp, port,
7027 						 phy_index ==
7028 						 active_external_phy);
7029 			break;
7030 		}
7031 	}
7032 	DP(NETIF_MSG_LINK, "vars->flow_ctrl = 0x%x, vars->link_status = 0x%x,"
7033 		   " ext_phy_line_speed = %d\n", vars->flow_ctrl,
7034 		   vars->link_status, ext_phy_line_speed);
7035 	/* Upon link speed change set the NIG into drain mode. Comes to
7036 	 * deals with possible FIFO glitch due to clk change when speed
7037 	 * is decreased without link down indicator
7038 	 */
7039 
7040 	if (vars->phy_link_up) {
7041 		if (!(SINGLE_MEDIA_DIRECT(params)) && ext_phy_link_up &&
7042 		    (ext_phy_line_speed != vars->line_speed)) {
7043 			DP(NETIF_MSG_LINK, "Internal link speed %d is"
7044 				   " different than the external"
7045 				   " link speed %d\n", vars->line_speed,
7046 				   ext_phy_line_speed);
7047 			vars->phy_link_up = 0;
7048 		} else if (prev_line_speed != vars->line_speed) {
7049 			REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4,
7050 			       0);
7051 			usleep_range(1000, 2000);
7052 		}
7053 	}
7054 
7055 	/* Anything 10 and over uses the bmac */
7056 	link_10g_plus = (vars->line_speed >= SPEED_10000);
7057 
7058 	bnx2x_link_int_ack(params, vars, link_10g_plus);
7059 
7060 	/* In case external phy link is up, and internal link is down
7061 	 * (not initialized yet probably after link initialization, it
7062 	 * needs to be initialized.
7063 	 * Note that after link down-up as result of cable plug, the xgxs
7064 	 * link would probably become up again without the need
7065 	 * initialize it
7066 	 */
7067 	if (!(SINGLE_MEDIA_DIRECT(params))) {
7068 		DP(NETIF_MSG_LINK, "ext_phy_link_up = %d, int_link_up = %d,"
7069 			   " init_preceding = %d\n", ext_phy_link_up,
7070 			   vars->phy_link_up,
7071 			   params->phy[EXT_PHY1].flags &
7072 			   FLAGS_INIT_XGXS_FIRST);
7073 		if (!(params->phy[EXT_PHY1].flags &
7074 		      FLAGS_INIT_XGXS_FIRST)
7075 		    && ext_phy_link_up && !vars->phy_link_up) {
7076 			vars->line_speed = ext_phy_line_speed;
7077 			if (vars->line_speed < SPEED_1000)
7078 				vars->phy_flags |= PHY_SGMII_FLAG;
7079 			else
7080 				vars->phy_flags &= ~PHY_SGMII_FLAG;
7081 
7082 			if (params->phy[INT_PHY].config_init)
7083 				params->phy[INT_PHY].config_init(
7084 					&params->phy[INT_PHY], params,
7085 						vars);
7086 		}
7087 	}
7088 	/* Link is up only if both local phy and external phy (in case of
7089 	 * non-direct board) are up and no fault detected on active PHY.
7090 	 */
7091 	vars->link_up = (vars->phy_link_up &&
7092 			 (ext_phy_link_up ||
7093 			  SINGLE_MEDIA_DIRECT(params)) &&
7094 			 (phy_vars[active_external_phy].fault_detected == 0));
7095 
7096 	/* Update the PFC configuration in case it was changed */
7097 	if (params->feature_config_flags & FEATURE_CONFIG_PFC_ENABLED)
7098 		vars->link_status |= LINK_STATUS_PFC_ENABLED;
7099 	else
7100 		vars->link_status &= ~LINK_STATUS_PFC_ENABLED;
7101 
7102 	if (vars->link_up)
7103 		rc = bnx2x_update_link_up(params, vars, link_10g_plus);
7104 	else
7105 		rc = bnx2x_update_link_down(params, vars);
7106 
7107 	if ((prev_link_status ^ vars->link_status) & LINK_STATUS_LINK_UP)
7108 		bnx2x_chng_link_count(params, false);
7109 
7110 	/* Update MCP link status was changed */
7111 	if (params->feature_config_flags & FEATURE_CONFIG_BC_SUPPORTS_AFEX)
7112 		bnx2x_fw_command(bp, DRV_MSG_CODE_LINK_STATUS_CHANGED, 0);
7113 
7114 	return rc;
7115 }
7116 
7117 /*****************************************************************************/
7118 /*			    External Phy section			     */
7119 /*****************************************************************************/
7120 void bnx2x_ext_phy_hw_reset(struct bnx2x *bp, u8 port)
7121 {
7122 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_1,
7123 		       MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
7124 	usleep_range(1000, 2000);
7125 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_1,
7126 		       MISC_REGISTERS_GPIO_OUTPUT_HIGH, port);
7127 }
7128 
7129 static void bnx2x_save_spirom_version(struct bnx2x *bp, u8 port,
7130 				      u32 spirom_ver, u32 ver_addr)
7131 {
7132 	DP(NETIF_MSG_LINK, "FW version 0x%x:0x%x for port %d\n",
7133 		 (u16)(spirom_ver>>16), (u16)spirom_ver, port);
7134 
7135 	if (ver_addr)
7136 		REG_WR(bp, ver_addr, spirom_ver);
7137 }
7138 
7139 static void bnx2x_save_bcm_spirom_ver(struct bnx2x *bp,
7140 				      struct bnx2x_phy *phy,
7141 				      u8 port)
7142 {
7143 	u16 fw_ver1, fw_ver2;
7144 
7145 	bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD,
7146 			MDIO_PMA_REG_ROM_VER1, &fw_ver1);
7147 	bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD,
7148 			MDIO_PMA_REG_ROM_VER2, &fw_ver2);
7149 	bnx2x_save_spirom_version(bp, port, (u32)(fw_ver1<<16 | fw_ver2),
7150 				  phy->ver_addr);
7151 }
7152 
7153 static void bnx2x_ext_phy_10G_an_resolve(struct bnx2x *bp,
7154 				       struct bnx2x_phy *phy,
7155 				       struct link_vars *vars)
7156 {
7157 	u16 val;
7158 	bnx2x_cl45_read(bp, phy,
7159 			MDIO_AN_DEVAD,
7160 			MDIO_AN_REG_STATUS, &val);
7161 	bnx2x_cl45_read(bp, phy,
7162 			MDIO_AN_DEVAD,
7163 			MDIO_AN_REG_STATUS, &val);
7164 	if (val & (1<<5))
7165 		vars->link_status |= LINK_STATUS_AUTO_NEGOTIATE_COMPLETE;
7166 	if ((val & (1<<0)) == 0)
7167 		vars->link_status |= LINK_STATUS_PARALLEL_DETECTION_USED;
7168 }
7169 
7170 /******************************************************************/
7171 /*		common BCM8073/BCM8727 PHY SECTION		  */
7172 /******************************************************************/
7173 static void bnx2x_8073_resolve_fc(struct bnx2x_phy *phy,
7174 				  struct link_params *params,
7175 				  struct link_vars *vars)
7176 {
7177 	struct bnx2x *bp = params->bp;
7178 	if (phy->req_line_speed == SPEED_10 ||
7179 	    phy->req_line_speed == SPEED_100) {
7180 		vars->flow_ctrl = phy->req_flow_ctrl;
7181 		return;
7182 	}
7183 
7184 	if (bnx2x_ext_phy_resolve_fc(phy, params, vars) &&
7185 	    (vars->flow_ctrl == BNX2X_FLOW_CTRL_NONE)) {
7186 		u16 pause_result;
7187 		u16 ld_pause;		/* local */
7188 		u16 lp_pause;		/* link partner */
7189 		bnx2x_cl45_read(bp, phy,
7190 				MDIO_AN_DEVAD,
7191 				MDIO_AN_REG_CL37_FC_LD, &ld_pause);
7192 
7193 		bnx2x_cl45_read(bp, phy,
7194 				MDIO_AN_DEVAD,
7195 				MDIO_AN_REG_CL37_FC_LP, &lp_pause);
7196 		pause_result = (ld_pause &
7197 				MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH) >> 5;
7198 		pause_result |= (lp_pause &
7199 				 MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH) >> 7;
7200 
7201 		bnx2x_pause_resolve(phy, params, vars, pause_result);
7202 		DP(NETIF_MSG_LINK, "Ext PHY CL37 pause result 0x%x\n",
7203 			   pause_result);
7204 	}
7205 }
7206 static int bnx2x_8073_8727_external_rom_boot(struct bnx2x *bp,
7207 					     struct bnx2x_phy *phy,
7208 					     u8 port)
7209 {
7210 	u32 count = 0;
7211 	u16 fw_ver1, fw_msgout;
7212 	int rc = 0;
7213 
7214 	/* Boot port from external ROM  */
7215 	/* EDC grst */
7216 	bnx2x_cl45_write(bp, phy,
7217 			 MDIO_PMA_DEVAD,
7218 			 MDIO_PMA_REG_GEN_CTRL,
7219 			 0x0001);
7220 
7221 	/* Ucode reboot and rst */
7222 	bnx2x_cl45_write(bp, phy,
7223 			 MDIO_PMA_DEVAD,
7224 			 MDIO_PMA_REG_GEN_CTRL,
7225 			 0x008c);
7226 
7227 	bnx2x_cl45_write(bp, phy,
7228 			 MDIO_PMA_DEVAD,
7229 			 MDIO_PMA_REG_MISC_CTRL1, 0x0001);
7230 
7231 	/* Reset internal microprocessor */
7232 	bnx2x_cl45_write(bp, phy,
7233 			 MDIO_PMA_DEVAD,
7234 			 MDIO_PMA_REG_GEN_CTRL,
7235 			 MDIO_PMA_REG_GEN_CTRL_ROM_MICRO_RESET);
7236 
7237 	/* Release srst bit */
7238 	bnx2x_cl45_write(bp, phy,
7239 			 MDIO_PMA_DEVAD,
7240 			 MDIO_PMA_REG_GEN_CTRL,
7241 			 MDIO_PMA_REG_GEN_CTRL_ROM_RESET_INTERNAL_MP);
7242 
7243 	/* Delay 100ms per the PHY specifications */
7244 	msleep(100);
7245 
7246 	/* 8073 sometimes taking longer to download */
7247 	do {
7248 		count++;
7249 		if (count > 300) {
7250 			DP(NETIF_MSG_LINK,
7251 				 "bnx2x_8073_8727_external_rom_boot port %x:"
7252 				 "Download failed. fw version = 0x%x\n",
7253 				 port, fw_ver1);
7254 			rc = -EINVAL;
7255 			break;
7256 		}
7257 
7258 		bnx2x_cl45_read(bp, phy,
7259 				MDIO_PMA_DEVAD,
7260 				MDIO_PMA_REG_ROM_VER1, &fw_ver1);
7261 		bnx2x_cl45_read(bp, phy,
7262 				MDIO_PMA_DEVAD,
7263 				MDIO_PMA_REG_M8051_MSGOUT_REG, &fw_msgout);
7264 
7265 		usleep_range(1000, 2000);
7266 	} while (fw_ver1 == 0 || fw_ver1 == 0x4321 ||
7267 			((fw_msgout & 0xff) != 0x03 && (phy->type ==
7268 			PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073)));
7269 
7270 	/* Clear ser_boot_ctl bit */
7271 	bnx2x_cl45_write(bp, phy,
7272 			 MDIO_PMA_DEVAD,
7273 			 MDIO_PMA_REG_MISC_CTRL1, 0x0000);
7274 	bnx2x_save_bcm_spirom_ver(bp, phy, port);
7275 
7276 	DP(NETIF_MSG_LINK,
7277 		 "bnx2x_8073_8727_external_rom_boot port %x:"
7278 		 "Download complete. fw version = 0x%x\n",
7279 		 port, fw_ver1);
7280 
7281 	return rc;
7282 }
7283 
7284 /******************************************************************/
7285 /*			BCM8073 PHY SECTION			  */
7286 /******************************************************************/
7287 static int bnx2x_8073_is_snr_needed(struct bnx2x *bp, struct bnx2x_phy *phy)
7288 {
7289 	/* This is only required for 8073A1, version 102 only */
7290 	u16 val;
7291 
7292 	/* Read 8073 HW revision*/
7293 	bnx2x_cl45_read(bp, phy,
7294 			MDIO_PMA_DEVAD,
7295 			MDIO_PMA_REG_8073_CHIP_REV, &val);
7296 
7297 	if (val != 1) {
7298 		/* No need to workaround in 8073 A1 */
7299 		return 0;
7300 	}
7301 
7302 	bnx2x_cl45_read(bp, phy,
7303 			MDIO_PMA_DEVAD,
7304 			MDIO_PMA_REG_ROM_VER2, &val);
7305 
7306 	/* SNR should be applied only for version 0x102 */
7307 	if (val != 0x102)
7308 		return 0;
7309 
7310 	return 1;
7311 }
7312 
7313 static int bnx2x_8073_xaui_wa(struct bnx2x *bp, struct bnx2x_phy *phy)
7314 {
7315 	u16 val, cnt, cnt1 ;
7316 
7317 	bnx2x_cl45_read(bp, phy,
7318 			MDIO_PMA_DEVAD,
7319 			MDIO_PMA_REG_8073_CHIP_REV, &val);
7320 
7321 	if (val > 0) {
7322 		/* No need to workaround in 8073 A1 */
7323 		return 0;
7324 	}
7325 	/* XAUI workaround in 8073 A0: */
7326 
7327 	/* After loading the boot ROM and restarting Autoneg, poll
7328 	 * Dev1, Reg $C820:
7329 	 */
7330 
7331 	for (cnt = 0; cnt < 1000; cnt++) {
7332 		bnx2x_cl45_read(bp, phy,
7333 				MDIO_PMA_DEVAD,
7334 				MDIO_PMA_REG_8073_SPEED_LINK_STATUS,
7335 				&val);
7336 		  /* If bit [14] = 0 or bit [13] = 0, continue on with
7337 		   * system initialization (XAUI work-around not required, as
7338 		   * these bits indicate 2.5G or 1G link up).
7339 		   */
7340 		if (!(val & (1<<14)) || !(val & (1<<13))) {
7341 			DP(NETIF_MSG_LINK, "XAUI work-around not required\n");
7342 			return 0;
7343 		} else if (!(val & (1<<15))) {
7344 			DP(NETIF_MSG_LINK, "bit 15 went off\n");
7345 			/* If bit 15 is 0, then poll Dev1, Reg $C841 until it's
7346 			 * MSB (bit15) goes to 1 (indicating that the XAUI
7347 			 * workaround has completed), then continue on with
7348 			 * system initialization.
7349 			 */
7350 			for (cnt1 = 0; cnt1 < 1000; cnt1++) {
7351 				bnx2x_cl45_read(bp, phy,
7352 					MDIO_PMA_DEVAD,
7353 					MDIO_PMA_REG_8073_XAUI_WA, &val);
7354 				if (val & (1<<15)) {
7355 					DP(NETIF_MSG_LINK,
7356 					  "XAUI workaround has completed\n");
7357 					return 0;
7358 				 }
7359 				 usleep_range(3000, 6000);
7360 			}
7361 			break;
7362 		}
7363 		usleep_range(3000, 6000);
7364 	}
7365 	DP(NETIF_MSG_LINK, "Warning: XAUI work-around timeout !!!\n");
7366 	return -EINVAL;
7367 }
7368 
7369 static void bnx2x_807x_force_10G(struct bnx2x *bp, struct bnx2x_phy *phy)
7370 {
7371 	/* Force KR or KX */
7372 	bnx2x_cl45_write(bp, phy,
7373 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0x2040);
7374 	bnx2x_cl45_write(bp, phy,
7375 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_10G_CTRL2, 0x000b);
7376 	bnx2x_cl45_write(bp, phy,
7377 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_BCM_CTRL, 0x0000);
7378 	bnx2x_cl45_write(bp, phy,
7379 			 MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, 0x0000);
7380 }
7381 
7382 static void bnx2x_8073_set_pause_cl37(struct link_params *params,
7383 				      struct bnx2x_phy *phy,
7384 				      struct link_vars *vars)
7385 {
7386 	u16 cl37_val;
7387 	struct bnx2x *bp = params->bp;
7388 	bnx2x_cl45_read(bp, phy,
7389 			MDIO_AN_DEVAD, MDIO_AN_REG_CL37_FC_LD, &cl37_val);
7390 
7391 	cl37_val &= ~MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH;
7392 	/* Please refer to Table 28B-3 of 802.3ab-1999 spec. */
7393 	bnx2x_calc_ieee_aneg_adv(phy, params, &vars->ieee_fc);
7394 	if ((vars->ieee_fc &
7395 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_SYMMETRIC) ==
7396 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_SYMMETRIC) {
7397 		cl37_val |=  MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_SYMMETRIC;
7398 	}
7399 	if ((vars->ieee_fc &
7400 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC) ==
7401 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC) {
7402 		cl37_val |=  MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC;
7403 	}
7404 	if ((vars->ieee_fc &
7405 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH) ==
7406 	    MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH) {
7407 		cl37_val |= MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH;
7408 	}
7409 	DP(NETIF_MSG_LINK,
7410 		 "Ext phy AN advertize cl37 0x%x\n", cl37_val);
7411 
7412 	bnx2x_cl45_write(bp, phy,
7413 			 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_FC_LD, cl37_val);
7414 	msleep(500);
7415 }
7416 
7417 static void bnx2x_8073_specific_func(struct bnx2x_phy *phy,
7418 				     struct link_params *params,
7419 				     u32 action)
7420 {
7421 	struct bnx2x *bp = params->bp;
7422 	switch (action) {
7423 	case PHY_INIT:
7424 		/* Enable LASI */
7425 		bnx2x_cl45_write(bp, phy,
7426 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXCTRL, (1<<2));
7427 		bnx2x_cl45_write(bp, phy,
7428 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL,  0x0004);
7429 		break;
7430 	}
7431 }
7432 
7433 static int bnx2x_8073_config_init(struct bnx2x_phy *phy,
7434 				  struct link_params *params,
7435 				  struct link_vars *vars)
7436 {
7437 	struct bnx2x *bp = params->bp;
7438 	u16 val = 0, tmp1;
7439 	u8 gpio_port;
7440 	DP(NETIF_MSG_LINK, "Init 8073\n");
7441 
7442 	if (CHIP_IS_E2(bp))
7443 		gpio_port = BP_PATH(bp);
7444 	else
7445 		gpio_port = params->port;
7446 	/* Restore normal power mode*/
7447 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
7448 		       MISC_REGISTERS_GPIO_OUTPUT_HIGH, gpio_port);
7449 
7450 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_1,
7451 		       MISC_REGISTERS_GPIO_OUTPUT_HIGH, gpio_port);
7452 
7453 	bnx2x_8073_specific_func(phy, params, PHY_INIT);
7454 	bnx2x_8073_set_pause_cl37(params, phy, vars);
7455 
7456 	bnx2x_cl45_read(bp, phy,
7457 			MDIO_PMA_DEVAD, MDIO_PMA_REG_M8051_MSGOUT_REG, &tmp1);
7458 
7459 	bnx2x_cl45_read(bp, phy,
7460 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXSTAT, &tmp1);
7461 
7462 	DP(NETIF_MSG_LINK, "Before rom RX_ALARM(port1): 0x%x\n", tmp1);
7463 
7464 	/* Swap polarity if required - Must be done only in non-1G mode */
7465 	if (params->lane_config & PORT_HW_CFG_SWAP_PHY_POLARITY_ENABLED) {
7466 		/* Configure the 8073 to swap _P and _N of the KR lines */
7467 		DP(NETIF_MSG_LINK, "Swapping polarity for the 8073\n");
7468 		/* 10G Rx/Tx and 1G Tx signal polarity swap */
7469 		bnx2x_cl45_read(bp, phy,
7470 				MDIO_PMA_DEVAD,
7471 				MDIO_PMA_REG_8073_OPT_DIGITAL_CTRL, &val);
7472 		bnx2x_cl45_write(bp, phy,
7473 				 MDIO_PMA_DEVAD,
7474 				 MDIO_PMA_REG_8073_OPT_DIGITAL_CTRL,
7475 				 (val | (3<<9)));
7476 	}
7477 
7478 
7479 	/* Enable CL37 BAM */
7480 	if (REG_RD(bp, params->shmem_base +
7481 			 offsetof(struct shmem_region, dev_info.
7482 				  port_hw_config[params->port].default_cfg)) &
7483 	    PORT_HW_CFG_ENABLE_BAM_ON_KR_ENABLED) {
7484 
7485 		bnx2x_cl45_read(bp, phy,
7486 				MDIO_AN_DEVAD,
7487 				MDIO_AN_REG_8073_BAM, &val);
7488 		bnx2x_cl45_write(bp, phy,
7489 				 MDIO_AN_DEVAD,
7490 				 MDIO_AN_REG_8073_BAM, val | 1);
7491 		DP(NETIF_MSG_LINK, "Enable CL37 BAM on KR\n");
7492 	}
7493 	if (params->loopback_mode == LOOPBACK_EXT) {
7494 		bnx2x_807x_force_10G(bp, phy);
7495 		DP(NETIF_MSG_LINK, "Forced speed 10G on 807X\n");
7496 		return 0;
7497 	} else {
7498 		bnx2x_cl45_write(bp, phy,
7499 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_BCM_CTRL, 0x0002);
7500 	}
7501 	if (phy->req_line_speed != SPEED_AUTO_NEG) {
7502 		if (phy->req_line_speed == SPEED_10000) {
7503 			val = (1<<7);
7504 		} else if (phy->req_line_speed ==  SPEED_2500) {
7505 			val = (1<<5);
7506 			/* Note that 2.5G works only when used with 1G
7507 			 * advertisement
7508 			 */
7509 		} else
7510 			val = (1<<5);
7511 	} else {
7512 		val = 0;
7513 		if (phy->speed_cap_mask &
7514 			PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)
7515 			val |= (1<<7);
7516 
7517 		/* Note that 2.5G works only when used with 1G advertisement */
7518 		if (phy->speed_cap_mask &
7519 			(PORT_HW_CFG_SPEED_CAPABILITY_D0_1G |
7520 			 PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G))
7521 			val |= (1<<5);
7522 		DP(NETIF_MSG_LINK, "807x autoneg val = 0x%x\n", val);
7523 	}
7524 
7525 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_ADV, val);
7526 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_8073_2_5G, &tmp1);
7527 
7528 	if (((phy->speed_cap_mask & PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G) &&
7529 	     (phy->req_line_speed == SPEED_AUTO_NEG)) ||
7530 	    (phy->req_line_speed == SPEED_2500)) {
7531 		u16 phy_ver;
7532 		/* Allow 2.5G for A1 and above */
7533 		bnx2x_cl45_read(bp, phy,
7534 				MDIO_PMA_DEVAD, MDIO_PMA_REG_8073_CHIP_REV,
7535 				&phy_ver);
7536 		DP(NETIF_MSG_LINK, "Add 2.5G\n");
7537 		if (phy_ver > 0)
7538 			tmp1 |= 1;
7539 		else
7540 			tmp1 &= 0xfffe;
7541 	} else {
7542 		DP(NETIF_MSG_LINK, "Disable 2.5G\n");
7543 		tmp1 &= 0xfffe;
7544 	}
7545 
7546 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_8073_2_5G, tmp1);
7547 	/* Add support for CL37 (passive mode) II */
7548 
7549 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_CL37_FC_LD, &tmp1);
7550 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_CL37_FC_LD,
7551 			 (tmp1 | ((phy->req_duplex == DUPLEX_FULL) ?
7552 				  0x20 : 0x40)));
7553 
7554 	/* Add support for CL37 (passive mode) III */
7555 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_CL37_AN, 0x1000);
7556 
7557 	/* The SNR will improve about 2db by changing BW and FEE main
7558 	 * tap. Rest commands are executed after link is up
7559 	 * Change FFE main cursor to 5 in EDC register
7560 	 */
7561 	if (bnx2x_8073_is_snr_needed(bp, phy))
7562 		bnx2x_cl45_write(bp, phy,
7563 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_EDC_FFE_MAIN,
7564 				 0xFB0C);
7565 
7566 	/* Enable FEC (Forware Error Correction) Request in the AN */
7567 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_ADV2, &tmp1);
7568 	tmp1 |= (1<<15);
7569 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_ADV2, tmp1);
7570 
7571 	bnx2x_ext_phy_set_pause(params, phy, vars);
7572 
7573 	/* Restart autoneg */
7574 	msleep(500);
7575 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, 0x1200);
7576 	DP(NETIF_MSG_LINK, "807x Autoneg Restart: Advertise 1G=%x, 10G=%x\n",
7577 		   ((val & (1<<5)) > 0), ((val & (1<<7)) > 0));
7578 	return 0;
7579 }
7580 
7581 static u8 bnx2x_8073_read_status(struct bnx2x_phy *phy,
7582 				 struct link_params *params,
7583 				 struct link_vars *vars)
7584 {
7585 	struct bnx2x *bp = params->bp;
7586 	u8 link_up = 0;
7587 	u16 val1, val2;
7588 	u16 link_status = 0;
7589 	u16 an1000_status = 0;
7590 
7591 	bnx2x_cl45_read(bp, phy,
7592 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_STAT, &val1);
7593 
7594 	DP(NETIF_MSG_LINK, "8703 LASI status 0x%x\n", val1);
7595 
7596 	/* Clear the interrupt LASI status register */
7597 	bnx2x_cl45_read(bp, phy,
7598 			MDIO_PCS_DEVAD, MDIO_PCS_REG_STATUS, &val2);
7599 	bnx2x_cl45_read(bp, phy,
7600 			MDIO_PCS_DEVAD, MDIO_PCS_REG_STATUS, &val1);
7601 	DP(NETIF_MSG_LINK, "807x PCS status 0x%x->0x%x\n", val2, val1);
7602 	/* Clear MSG-OUT */
7603 	bnx2x_cl45_read(bp, phy,
7604 			MDIO_PMA_DEVAD, MDIO_PMA_REG_M8051_MSGOUT_REG, &val1);
7605 
7606 	/* Check the LASI */
7607 	bnx2x_cl45_read(bp, phy,
7608 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXSTAT, &val2);
7609 
7610 	DP(NETIF_MSG_LINK, "KR 0x9003 0x%x\n", val2);
7611 
7612 	/* Check the link status */
7613 	bnx2x_cl45_read(bp, phy,
7614 			MDIO_PCS_DEVAD, MDIO_PCS_REG_STATUS, &val2);
7615 	DP(NETIF_MSG_LINK, "KR PCS status 0x%x\n", val2);
7616 
7617 	bnx2x_cl45_read(bp, phy,
7618 			MDIO_PMA_DEVAD, MDIO_PMA_REG_STATUS, &val2);
7619 	bnx2x_cl45_read(bp, phy,
7620 			MDIO_PMA_DEVAD, MDIO_PMA_REG_STATUS, &val1);
7621 	link_up = ((val1 & 4) == 4);
7622 	DP(NETIF_MSG_LINK, "PMA_REG_STATUS=0x%x\n", val1);
7623 
7624 	if (link_up &&
7625 	     ((phy->req_line_speed != SPEED_10000))) {
7626 		if (bnx2x_8073_xaui_wa(bp, phy) != 0)
7627 			return 0;
7628 	}
7629 	bnx2x_cl45_read(bp, phy,
7630 			MDIO_AN_DEVAD, MDIO_AN_REG_LINK_STATUS, &an1000_status);
7631 	bnx2x_cl45_read(bp, phy,
7632 			MDIO_AN_DEVAD, MDIO_AN_REG_LINK_STATUS, &an1000_status);
7633 
7634 	/* Check the link status on 1.1.2 */
7635 	bnx2x_cl45_read(bp, phy,
7636 			MDIO_PMA_DEVAD, MDIO_PMA_REG_STATUS, &val2);
7637 	bnx2x_cl45_read(bp, phy,
7638 			MDIO_PMA_DEVAD, MDIO_PMA_REG_STATUS, &val1);
7639 	DP(NETIF_MSG_LINK, "KR PMA status 0x%x->0x%x,"
7640 		   "an_link_status=0x%x\n", val2, val1, an1000_status);
7641 
7642 	link_up = (((val1 & 4) == 4) || (an1000_status & (1<<1)));
7643 	if (link_up && bnx2x_8073_is_snr_needed(bp, phy)) {
7644 		/* The SNR will improve about 2dbby changing the BW and FEE main
7645 		 * tap. The 1st write to change FFE main tap is set before
7646 		 * restart AN. Change PLL Bandwidth in EDC register
7647 		 */
7648 		bnx2x_cl45_write(bp, phy,
7649 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_PLL_BANDWIDTH,
7650 				 0x26BC);
7651 
7652 		/* Change CDR Bandwidth in EDC register */
7653 		bnx2x_cl45_write(bp, phy,
7654 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_CDR_BANDWIDTH,
7655 				 0x0333);
7656 	}
7657 	bnx2x_cl45_read(bp, phy,
7658 			MDIO_PMA_DEVAD, MDIO_PMA_REG_8073_SPEED_LINK_STATUS,
7659 			&link_status);
7660 
7661 	/* Bits 0..2 --> speed detected, bits 13..15--> link is down */
7662 	if ((link_status & (1<<2)) && (!(link_status & (1<<15)))) {
7663 		link_up = 1;
7664 		vars->line_speed = SPEED_10000;
7665 		DP(NETIF_MSG_LINK, "port %x: External link up in 10G\n",
7666 			   params->port);
7667 	} else if ((link_status & (1<<1)) && (!(link_status & (1<<14)))) {
7668 		link_up = 1;
7669 		vars->line_speed = SPEED_2500;
7670 		DP(NETIF_MSG_LINK, "port %x: External link up in 2.5G\n",
7671 			   params->port);
7672 	} else if ((link_status & (1<<0)) && (!(link_status & (1<<13)))) {
7673 		link_up = 1;
7674 		vars->line_speed = SPEED_1000;
7675 		DP(NETIF_MSG_LINK, "port %x: External link up in 1G\n",
7676 			   params->port);
7677 	} else {
7678 		link_up = 0;
7679 		DP(NETIF_MSG_LINK, "port %x: External link is down\n",
7680 			   params->port);
7681 	}
7682 
7683 	if (link_up) {
7684 		/* Swap polarity if required */
7685 		if (params->lane_config &
7686 		    PORT_HW_CFG_SWAP_PHY_POLARITY_ENABLED) {
7687 			/* Configure the 8073 to swap P and N of the KR lines */
7688 			bnx2x_cl45_read(bp, phy,
7689 					MDIO_XS_DEVAD,
7690 					MDIO_XS_REG_8073_RX_CTRL_PCIE, &val1);
7691 			/* Set bit 3 to invert Rx in 1G mode and clear this bit
7692 			 * when it`s in 10G mode.
7693 			 */
7694 			if (vars->line_speed == SPEED_1000) {
7695 				DP(NETIF_MSG_LINK, "Swapping 1G polarity for"
7696 					      "the 8073\n");
7697 				val1 |= (1<<3);
7698 			} else
7699 				val1 &= ~(1<<3);
7700 
7701 			bnx2x_cl45_write(bp, phy,
7702 					 MDIO_XS_DEVAD,
7703 					 MDIO_XS_REG_8073_RX_CTRL_PCIE,
7704 					 val1);
7705 		}
7706 		bnx2x_ext_phy_10G_an_resolve(bp, phy, vars);
7707 		bnx2x_8073_resolve_fc(phy, params, vars);
7708 		vars->duplex = DUPLEX_FULL;
7709 	}
7710 
7711 	if (vars->link_status & LINK_STATUS_AUTO_NEGOTIATE_COMPLETE) {
7712 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
7713 				MDIO_AN_REG_LP_AUTO_NEG2, &val1);
7714 
7715 		if (val1 & (1<<5))
7716 			vars->link_status |=
7717 				LINK_STATUS_LINK_PARTNER_1000TFD_CAPABLE;
7718 		if (val1 & (1<<7))
7719 			vars->link_status |=
7720 				LINK_STATUS_LINK_PARTNER_10GXFD_CAPABLE;
7721 	}
7722 
7723 	return link_up;
7724 }
7725 
7726 static void bnx2x_8073_link_reset(struct bnx2x_phy *phy,
7727 				  struct link_params *params)
7728 {
7729 	struct bnx2x *bp = params->bp;
7730 	u8 gpio_port;
7731 	if (CHIP_IS_E2(bp))
7732 		gpio_port = BP_PATH(bp);
7733 	else
7734 		gpio_port = params->port;
7735 	DP(NETIF_MSG_LINK, "Setting 8073 port %d into low power mode\n",
7736 	   gpio_port);
7737 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
7738 		       MISC_REGISTERS_GPIO_OUTPUT_LOW,
7739 		       gpio_port);
7740 }
7741 
7742 /******************************************************************/
7743 /*			BCM8705 PHY SECTION			  */
7744 /******************************************************************/
7745 static int bnx2x_8705_config_init(struct bnx2x_phy *phy,
7746 				  struct link_params *params,
7747 				  struct link_vars *vars)
7748 {
7749 	struct bnx2x *bp = params->bp;
7750 	DP(NETIF_MSG_LINK, "init 8705\n");
7751 	/* Restore normal power mode*/
7752 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
7753 		       MISC_REGISTERS_GPIO_OUTPUT_HIGH, params->port);
7754 	/* HW reset */
7755 	bnx2x_ext_phy_hw_reset(bp, params->port);
7756 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0xa040);
7757 	bnx2x_wait_reset_complete(bp, phy, params);
7758 
7759 	bnx2x_cl45_write(bp, phy,
7760 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_MISC_CTRL, 0x8288);
7761 	bnx2x_cl45_write(bp, phy,
7762 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_PHY_IDENTIFIER, 0x7fbf);
7763 	bnx2x_cl45_write(bp, phy,
7764 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_CMU_PLL_BYPASS, 0x0100);
7765 	bnx2x_cl45_write(bp, phy,
7766 			 MDIO_WIS_DEVAD, MDIO_WIS_REG_LASI_CNTL, 0x1);
7767 	/* BCM8705 doesn't have microcode, hence the 0 */
7768 	bnx2x_save_spirom_version(bp, params->port, params->shmem_base, 0);
7769 	return 0;
7770 }
7771 
7772 static u8 bnx2x_8705_read_status(struct bnx2x_phy *phy,
7773 				 struct link_params *params,
7774 				 struct link_vars *vars)
7775 {
7776 	u8 link_up = 0;
7777 	u16 val1, rx_sd;
7778 	struct bnx2x *bp = params->bp;
7779 	DP(NETIF_MSG_LINK, "read status 8705\n");
7780 	bnx2x_cl45_read(bp, phy,
7781 		      MDIO_WIS_DEVAD, MDIO_WIS_REG_LASI_STATUS, &val1);
7782 	DP(NETIF_MSG_LINK, "8705 LASI status 0x%x\n", val1);
7783 
7784 	bnx2x_cl45_read(bp, phy,
7785 		      MDIO_WIS_DEVAD, MDIO_WIS_REG_LASI_STATUS, &val1);
7786 	DP(NETIF_MSG_LINK, "8705 LASI status 0x%x\n", val1);
7787 
7788 	bnx2x_cl45_read(bp, phy,
7789 		      MDIO_PMA_DEVAD, MDIO_PMA_REG_RX_SD, &rx_sd);
7790 
7791 	bnx2x_cl45_read(bp, phy,
7792 		      MDIO_PMA_DEVAD, 0xc809, &val1);
7793 	bnx2x_cl45_read(bp, phy,
7794 		      MDIO_PMA_DEVAD, 0xc809, &val1);
7795 
7796 	DP(NETIF_MSG_LINK, "8705 1.c809 val=0x%x\n", val1);
7797 	link_up = ((rx_sd & 0x1) && (val1 & (1<<9)) && ((val1 & (1<<8)) == 0));
7798 	if (link_up) {
7799 		vars->line_speed = SPEED_10000;
7800 		bnx2x_ext_phy_resolve_fc(phy, params, vars);
7801 	}
7802 	return link_up;
7803 }
7804 
7805 /******************************************************************/
7806 /*			SFP+ module Section			  */
7807 /******************************************************************/
7808 static void bnx2x_set_disable_pmd_transmit(struct link_params *params,
7809 					   struct bnx2x_phy *phy,
7810 					   u8 pmd_dis)
7811 {
7812 	struct bnx2x *bp = params->bp;
7813 	/* Disable transmitter only for bootcodes which can enable it afterwards
7814 	 * (for D3 link)
7815 	 */
7816 	if (pmd_dis) {
7817 		if (params->feature_config_flags &
7818 		     FEATURE_CONFIG_BC_SUPPORTS_SFP_TX_DISABLED)
7819 			DP(NETIF_MSG_LINK, "Disabling PMD transmitter\n");
7820 		else {
7821 			DP(NETIF_MSG_LINK, "NOT disabling PMD transmitter\n");
7822 			return;
7823 		}
7824 	} else
7825 		DP(NETIF_MSG_LINK, "Enabling PMD transmitter\n");
7826 	bnx2x_cl45_write(bp, phy,
7827 			 MDIO_PMA_DEVAD,
7828 			 MDIO_PMA_REG_TX_DISABLE, pmd_dis);
7829 }
7830 
7831 static u8 bnx2x_get_gpio_port(struct link_params *params)
7832 {
7833 	u8 gpio_port;
7834 	u32 swap_val, swap_override;
7835 	struct bnx2x *bp = params->bp;
7836 	if (CHIP_IS_E2(bp))
7837 		gpio_port = BP_PATH(bp);
7838 	else
7839 		gpio_port = params->port;
7840 	swap_val = REG_RD(bp, NIG_REG_PORT_SWAP);
7841 	swap_override = REG_RD(bp, NIG_REG_STRAP_OVERRIDE);
7842 	return gpio_port ^ (swap_val && swap_override);
7843 }
7844 
7845 static void bnx2x_sfp_e1e2_set_transmitter(struct link_params *params,
7846 					   struct bnx2x_phy *phy,
7847 					   u8 tx_en)
7848 {
7849 	u16 val;
7850 	u8 port = params->port;
7851 	struct bnx2x *bp = params->bp;
7852 	u32 tx_en_mode;
7853 
7854 	/* Disable/Enable transmitter ( TX laser of the SFP+ module.)*/
7855 	tx_en_mode = REG_RD(bp, params->shmem_base +
7856 			    offsetof(struct shmem_region,
7857 				     dev_info.port_hw_config[port].sfp_ctrl)) &
7858 		PORT_HW_CFG_TX_LASER_MASK;
7859 	DP(NETIF_MSG_LINK, "Setting transmitter tx_en=%x for port %x "
7860 			   "mode = %x\n", tx_en, port, tx_en_mode);
7861 	switch (tx_en_mode) {
7862 	case PORT_HW_CFG_TX_LASER_MDIO:
7863 
7864 		bnx2x_cl45_read(bp, phy,
7865 				MDIO_PMA_DEVAD,
7866 				MDIO_PMA_REG_PHY_IDENTIFIER,
7867 				&val);
7868 
7869 		if (tx_en)
7870 			val &= ~(1<<15);
7871 		else
7872 			val |= (1<<15);
7873 
7874 		bnx2x_cl45_write(bp, phy,
7875 				 MDIO_PMA_DEVAD,
7876 				 MDIO_PMA_REG_PHY_IDENTIFIER,
7877 				 val);
7878 	break;
7879 	case PORT_HW_CFG_TX_LASER_GPIO0:
7880 	case PORT_HW_CFG_TX_LASER_GPIO1:
7881 	case PORT_HW_CFG_TX_LASER_GPIO2:
7882 	case PORT_HW_CFG_TX_LASER_GPIO3:
7883 	{
7884 		u16 gpio_pin;
7885 		u8 gpio_port, gpio_mode;
7886 		if (tx_en)
7887 			gpio_mode = MISC_REGISTERS_GPIO_OUTPUT_HIGH;
7888 		else
7889 			gpio_mode = MISC_REGISTERS_GPIO_OUTPUT_LOW;
7890 
7891 		gpio_pin = tx_en_mode - PORT_HW_CFG_TX_LASER_GPIO0;
7892 		gpio_port = bnx2x_get_gpio_port(params);
7893 		bnx2x_set_gpio(bp, gpio_pin, gpio_mode, gpio_port);
7894 		break;
7895 	}
7896 	default:
7897 		DP(NETIF_MSG_LINK, "Invalid TX_LASER_MDIO 0x%x\n", tx_en_mode);
7898 		break;
7899 	}
7900 }
7901 
7902 static void bnx2x_sfp_set_transmitter(struct link_params *params,
7903 				      struct bnx2x_phy *phy,
7904 				      u8 tx_en)
7905 {
7906 	struct bnx2x *bp = params->bp;
7907 	DP(NETIF_MSG_LINK, "Setting SFP+ transmitter to %d\n", tx_en);
7908 	if (CHIP_IS_E3(bp))
7909 		bnx2x_sfp_e3_set_transmitter(params, phy, tx_en);
7910 	else
7911 		bnx2x_sfp_e1e2_set_transmitter(params, phy, tx_en);
7912 }
7913 
7914 static int bnx2x_8726_read_sfp_module_eeprom(struct bnx2x_phy *phy,
7915 					     struct link_params *params,
7916 					     u8 dev_addr, u16 addr, u8 byte_cnt,
7917 					     u8 *o_buf, u8 is_init)
7918 {
7919 	struct bnx2x *bp = params->bp;
7920 	u16 val = 0;
7921 	u16 i;
7922 	if (byte_cnt > SFP_EEPROM_PAGE_SIZE) {
7923 		DP(NETIF_MSG_LINK,
7924 		   "Reading from eeprom is limited to 0xf\n");
7925 		return -EINVAL;
7926 	}
7927 	/* Set the read command byte count */
7928 	bnx2x_cl45_write(bp, phy,
7929 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_SFP_TWO_WIRE_BYTE_CNT,
7930 			 (byte_cnt | (dev_addr << 8)));
7931 
7932 	/* Set the read command address */
7933 	bnx2x_cl45_write(bp, phy,
7934 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_SFP_TWO_WIRE_MEM_ADDR,
7935 			 addr);
7936 
7937 	/* Activate read command */
7938 	bnx2x_cl45_write(bp, phy,
7939 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_SFP_TWO_WIRE_CTRL,
7940 			 0x2c0f);
7941 
7942 	/* Wait up to 500us for command complete status */
7943 	for (i = 0; i < 100; i++) {
7944 		bnx2x_cl45_read(bp, phy,
7945 				MDIO_PMA_DEVAD,
7946 				MDIO_PMA_REG_SFP_TWO_WIRE_CTRL, &val);
7947 		if ((val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK) ==
7948 		    MDIO_PMA_REG_SFP_TWO_WIRE_STATUS_COMPLETE)
7949 			break;
7950 		udelay(5);
7951 	}
7952 
7953 	if ((val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK) !=
7954 		    MDIO_PMA_REG_SFP_TWO_WIRE_STATUS_COMPLETE) {
7955 		DP(NETIF_MSG_LINK,
7956 			 "Got bad status 0x%x when reading from SFP+ EEPROM\n",
7957 			 (val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK));
7958 		return -EINVAL;
7959 	}
7960 
7961 	/* Read the buffer */
7962 	for (i = 0; i < byte_cnt; i++) {
7963 		bnx2x_cl45_read(bp, phy,
7964 				MDIO_PMA_DEVAD,
7965 				MDIO_PMA_REG_8726_TWO_WIRE_DATA_BUF + i, &val);
7966 		o_buf[i] = (u8)(val & MDIO_PMA_REG_8726_TWO_WIRE_DATA_MASK);
7967 	}
7968 
7969 	for (i = 0; i < 100; i++) {
7970 		bnx2x_cl45_read(bp, phy,
7971 				MDIO_PMA_DEVAD,
7972 				MDIO_PMA_REG_SFP_TWO_WIRE_CTRL, &val);
7973 		if ((val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK) ==
7974 		    MDIO_PMA_REG_SFP_TWO_WIRE_STATUS_IDLE)
7975 			return 0;
7976 		usleep_range(1000, 2000);
7977 	}
7978 	return -EINVAL;
7979 }
7980 
7981 static void bnx2x_warpcore_power_module(struct link_params *params,
7982 					u8 power)
7983 {
7984 	u32 pin_cfg;
7985 	struct bnx2x *bp = params->bp;
7986 
7987 	pin_cfg = (REG_RD(bp, params->shmem_base +
7988 			  offsetof(struct shmem_region,
7989 			dev_info.port_hw_config[params->port].e3_sfp_ctrl)) &
7990 			PORT_HW_CFG_E3_PWR_DIS_MASK) >>
7991 			PORT_HW_CFG_E3_PWR_DIS_SHIFT;
7992 
7993 	if (pin_cfg == PIN_CFG_NA)
7994 		return;
7995 	DP(NETIF_MSG_LINK, "Setting SFP+ module power to %d using pin cfg %d\n",
7996 		       power, pin_cfg);
7997 	/* Low ==> corresponding SFP+ module is powered
7998 	 * high ==> the SFP+ module is powered down
7999 	 */
8000 	bnx2x_set_cfg_pin(bp, pin_cfg, power ^ 1);
8001 }
8002 static int bnx2x_warpcore_read_sfp_module_eeprom(struct bnx2x_phy *phy,
8003 						 struct link_params *params,
8004 						 u8 dev_addr,
8005 						 u16 addr, u8 byte_cnt,
8006 						 u8 *o_buf, u8 is_init)
8007 {
8008 	int rc = 0;
8009 	u8 i, j = 0, cnt = 0;
8010 	u32 data_array[4];
8011 	u16 addr32;
8012 	struct bnx2x *bp = params->bp;
8013 
8014 	if (byte_cnt > SFP_EEPROM_PAGE_SIZE) {
8015 		DP(NETIF_MSG_LINK,
8016 		   "Reading from eeprom is limited to 16 bytes\n");
8017 		return -EINVAL;
8018 	}
8019 
8020 	/* 4 byte aligned address */
8021 	addr32 = addr & (~0x3);
8022 	do {
8023 		if ((!is_init) && (cnt == I2C_WA_PWR_ITER)) {
8024 			bnx2x_warpcore_power_module(params, 0);
8025 			/* Note that 100us are not enough here */
8026 			usleep_range(1000, 2000);
8027 			bnx2x_warpcore_power_module(params, 1);
8028 		}
8029 		rc = bnx2x_bsc_read(params, bp, dev_addr, addr32, 0, byte_cnt,
8030 				    data_array);
8031 	} while ((rc != 0) && (++cnt < I2C_WA_RETRY_CNT));
8032 
8033 	if (rc == 0) {
8034 		for (i = (addr - addr32); i < byte_cnt + (addr - addr32); i++) {
8035 			o_buf[j] = *((u8 *)data_array + i);
8036 			j++;
8037 		}
8038 	}
8039 
8040 	return rc;
8041 }
8042 
8043 static int bnx2x_8727_read_sfp_module_eeprom(struct bnx2x_phy *phy,
8044 					     struct link_params *params,
8045 					     u8 dev_addr, u16 addr, u8 byte_cnt,
8046 					     u8 *o_buf, u8 is_init)
8047 {
8048 	struct bnx2x *bp = params->bp;
8049 	u16 val, i;
8050 
8051 	if (byte_cnt > SFP_EEPROM_PAGE_SIZE) {
8052 		DP(NETIF_MSG_LINK,
8053 		   "Reading from eeprom is limited to 0xf\n");
8054 		return -EINVAL;
8055 	}
8056 
8057 	/* Set 2-wire transfer rate of SFP+ module EEPROM
8058 	 * to 100Khz since some DACs(direct attached cables) do
8059 	 * not work at 400Khz.
8060 	 */
8061 	bnx2x_cl45_write(bp, phy,
8062 			 MDIO_PMA_DEVAD,
8063 			 MDIO_PMA_REG_8727_TWO_WIRE_SLAVE_ADDR,
8064 			 ((dev_addr << 8) | 1));
8065 
8066 	/* Need to read from 1.8000 to clear it */
8067 	bnx2x_cl45_read(bp, phy,
8068 			MDIO_PMA_DEVAD,
8069 			MDIO_PMA_REG_SFP_TWO_WIRE_CTRL,
8070 			&val);
8071 
8072 	/* Set the read command byte count */
8073 	bnx2x_cl45_write(bp, phy,
8074 			 MDIO_PMA_DEVAD,
8075 			 MDIO_PMA_REG_SFP_TWO_WIRE_BYTE_CNT,
8076 			 ((byte_cnt < 2) ? 2 : byte_cnt));
8077 
8078 	/* Set the read command address */
8079 	bnx2x_cl45_write(bp, phy,
8080 			 MDIO_PMA_DEVAD,
8081 			 MDIO_PMA_REG_SFP_TWO_WIRE_MEM_ADDR,
8082 			 addr);
8083 	/* Set the destination address */
8084 	bnx2x_cl45_write(bp, phy,
8085 			 MDIO_PMA_DEVAD,
8086 			 0x8004,
8087 			 MDIO_PMA_REG_8727_TWO_WIRE_DATA_BUF);
8088 
8089 	/* Activate read command */
8090 	bnx2x_cl45_write(bp, phy,
8091 			 MDIO_PMA_DEVAD,
8092 			 MDIO_PMA_REG_SFP_TWO_WIRE_CTRL,
8093 			 0x8002);
8094 	/* Wait appropriate time for two-wire command to finish before
8095 	 * polling the status register
8096 	 */
8097 	usleep_range(1000, 2000);
8098 
8099 	/* Wait up to 500us for command complete status */
8100 	for (i = 0; i < 100; i++) {
8101 		bnx2x_cl45_read(bp, phy,
8102 				MDIO_PMA_DEVAD,
8103 				MDIO_PMA_REG_SFP_TWO_WIRE_CTRL, &val);
8104 		if ((val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK) ==
8105 		    MDIO_PMA_REG_SFP_TWO_WIRE_STATUS_COMPLETE)
8106 			break;
8107 		udelay(5);
8108 	}
8109 
8110 	if ((val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK) !=
8111 		    MDIO_PMA_REG_SFP_TWO_WIRE_STATUS_COMPLETE) {
8112 		DP(NETIF_MSG_LINK,
8113 			 "Got bad status 0x%x when reading from SFP+ EEPROM\n",
8114 			 (val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK));
8115 		return -EFAULT;
8116 	}
8117 
8118 	/* Read the buffer */
8119 	for (i = 0; i < byte_cnt; i++) {
8120 		bnx2x_cl45_read(bp, phy,
8121 				MDIO_PMA_DEVAD,
8122 				MDIO_PMA_REG_8727_TWO_WIRE_DATA_BUF + i, &val);
8123 		o_buf[i] = (u8)(val & MDIO_PMA_REG_8727_TWO_WIRE_DATA_MASK);
8124 	}
8125 
8126 	for (i = 0; i < 100; i++) {
8127 		bnx2x_cl45_read(bp, phy,
8128 				MDIO_PMA_DEVAD,
8129 				MDIO_PMA_REG_SFP_TWO_WIRE_CTRL, &val);
8130 		if ((val & MDIO_PMA_REG_SFP_TWO_WIRE_CTRL_STATUS_MASK) ==
8131 		    MDIO_PMA_REG_SFP_TWO_WIRE_STATUS_IDLE)
8132 			return 0;
8133 		usleep_range(1000, 2000);
8134 	}
8135 
8136 	return -EINVAL;
8137 }
8138 int bnx2x_read_sfp_module_eeprom(struct bnx2x_phy *phy,
8139 				 struct link_params *params, u8 dev_addr,
8140 				 u16 addr, u16 byte_cnt, u8 *o_buf)
8141 {
8142 	int rc = 0;
8143 	struct bnx2x *bp = params->bp;
8144 	u8 xfer_size;
8145 	u8 *user_data = o_buf;
8146 	read_sfp_module_eeprom_func_p read_func;
8147 
8148 	if ((dev_addr != 0xa0) && (dev_addr != 0xa2)) {
8149 		DP(NETIF_MSG_LINK, "invalid dev_addr 0x%x\n", dev_addr);
8150 		return -EINVAL;
8151 	}
8152 
8153 	switch (phy->type) {
8154 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
8155 		read_func = bnx2x_8726_read_sfp_module_eeprom;
8156 		break;
8157 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
8158 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8722:
8159 		read_func = bnx2x_8727_read_sfp_module_eeprom;
8160 		break;
8161 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT:
8162 		read_func = bnx2x_warpcore_read_sfp_module_eeprom;
8163 		break;
8164 	default:
8165 		return -EOPNOTSUPP;
8166 	}
8167 
8168 	while (!rc && (byte_cnt > 0)) {
8169 		xfer_size = (byte_cnt > SFP_EEPROM_PAGE_SIZE) ?
8170 			SFP_EEPROM_PAGE_SIZE : byte_cnt;
8171 		rc = read_func(phy, params, dev_addr, addr, xfer_size,
8172 			       user_data, 0);
8173 		byte_cnt -= xfer_size;
8174 		user_data += xfer_size;
8175 		addr += xfer_size;
8176 	}
8177 	return rc;
8178 }
8179 
8180 static int bnx2x_get_edc_mode(struct bnx2x_phy *phy,
8181 			      struct link_params *params,
8182 			      u16 *edc_mode)
8183 {
8184 	struct bnx2x *bp = params->bp;
8185 	u32 sync_offset = 0, phy_idx, media_types;
8186 	u8 val[SFP_EEPROM_FC_TX_TECH_ADDR + 1], check_limiting_mode = 0;
8187 	*edc_mode = EDC_MODE_LIMITING;
8188 	phy->media_type = ETH_PHY_UNSPECIFIED;
8189 	/* First check for copper cable */
8190 	if (bnx2x_read_sfp_module_eeprom(phy,
8191 					 params,
8192 					 I2C_DEV_ADDR_A0,
8193 					 0,
8194 					 SFP_EEPROM_FC_TX_TECH_ADDR + 1,
8195 					 (u8 *)val) != 0) {
8196 		DP(NETIF_MSG_LINK, "Failed to read from SFP+ module EEPROM\n");
8197 		return -EINVAL;
8198 	}
8199 	params->link_attr_sync &= ~LINK_SFP_EEPROM_COMP_CODE_MASK;
8200 	params->link_attr_sync |= val[SFP_EEPROM_10G_COMP_CODE_ADDR] <<
8201 		LINK_SFP_EEPROM_COMP_CODE_SHIFT;
8202 	bnx2x_update_link_attr(params, params->link_attr_sync);
8203 	switch (val[SFP_EEPROM_CON_TYPE_ADDR]) {
8204 	case SFP_EEPROM_CON_TYPE_VAL_COPPER:
8205 	{
8206 		u8 copper_module_type;
8207 		phy->media_type = ETH_PHY_DA_TWINAX;
8208 		/* Check if its active cable (includes SFP+ module)
8209 		 * of passive cable
8210 		 */
8211 		copper_module_type = val[SFP_EEPROM_FC_TX_TECH_ADDR];
8212 
8213 		if (copper_module_type &
8214 		    SFP_EEPROM_FC_TX_TECH_BITMASK_COPPER_ACTIVE) {
8215 			DP(NETIF_MSG_LINK, "Active Copper cable detected\n");
8216 			if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
8217 				*edc_mode = EDC_MODE_ACTIVE_DAC;
8218 			else
8219 				check_limiting_mode = 1;
8220 		} else {
8221 			*edc_mode = EDC_MODE_PASSIVE_DAC;
8222 			/* Even in case PASSIVE_DAC indication is not set,
8223 			 * treat it as a passive DAC cable, since some cables
8224 			 * don't have this indication.
8225 			 */
8226 			if (copper_module_type &
8227 			    SFP_EEPROM_FC_TX_TECH_BITMASK_COPPER_PASSIVE) {
8228 				DP(NETIF_MSG_LINK,
8229 				   "Passive Copper cable detected\n");
8230 			} else {
8231 				DP(NETIF_MSG_LINK,
8232 				   "Unknown copper-cable-type\n");
8233 			}
8234 		}
8235 		break;
8236 	}
8237 	case SFP_EEPROM_CON_TYPE_VAL_UNKNOWN:
8238 	case SFP_EEPROM_CON_TYPE_VAL_LC:
8239 	case SFP_EEPROM_CON_TYPE_VAL_RJ45:
8240 		check_limiting_mode = 1;
8241 		if (((val[SFP_EEPROM_10G_COMP_CODE_ADDR] &
8242 		     (SFP_EEPROM_10G_COMP_CODE_SR_MASK |
8243 		      SFP_EEPROM_10G_COMP_CODE_LR_MASK |
8244 		       SFP_EEPROM_10G_COMP_CODE_LRM_MASK)) == 0) &&
8245 		    (val[SFP_EEPROM_1G_COMP_CODE_ADDR] != 0)) {
8246 			DP(NETIF_MSG_LINK, "1G SFP module detected\n");
8247 			phy->media_type = ETH_PHY_SFP_1G_FIBER;
8248 			if (phy->req_line_speed != SPEED_1000) {
8249 				u8 gport = params->port;
8250 				phy->req_line_speed = SPEED_1000;
8251 				if (!CHIP_IS_E1x(bp)) {
8252 					gport = BP_PATH(bp) +
8253 					(params->port << 1);
8254 				}
8255 				netdev_err(bp->dev,
8256 					   "Warning: Link speed was forced to 1000Mbps. Current SFP module in port %d is not compliant with 10G Ethernet\n",
8257 					   gport);
8258 			}
8259 			if (val[SFP_EEPROM_1G_COMP_CODE_ADDR] &
8260 			    SFP_EEPROM_1G_COMP_CODE_BASE_T) {
8261 				bnx2x_sfp_set_transmitter(params, phy, 0);
8262 				msleep(40);
8263 				bnx2x_sfp_set_transmitter(params, phy, 1);
8264 			}
8265 		} else {
8266 			int idx, cfg_idx = 0;
8267 			DP(NETIF_MSG_LINK, "10G Optic module detected\n");
8268 			for (idx = INT_PHY; idx < MAX_PHYS; idx++) {
8269 				if (params->phy[idx].type == phy->type) {
8270 					cfg_idx = LINK_CONFIG_IDX(idx);
8271 					break;
8272 				}
8273 			}
8274 			phy->media_type = ETH_PHY_SFPP_10G_FIBER;
8275 			phy->req_line_speed = params->req_line_speed[cfg_idx];
8276 		}
8277 		break;
8278 	default:
8279 		DP(NETIF_MSG_LINK, "Unable to determine module type 0x%x !!!\n",
8280 			 val[SFP_EEPROM_CON_TYPE_ADDR]);
8281 		return -EINVAL;
8282 	}
8283 	sync_offset = params->shmem_base +
8284 		offsetof(struct shmem_region,
8285 			 dev_info.port_hw_config[params->port].media_type);
8286 	media_types = REG_RD(bp, sync_offset);
8287 	/* Update media type for non-PMF sync */
8288 	for (phy_idx = INT_PHY; phy_idx < MAX_PHYS; phy_idx++) {
8289 		if (&(params->phy[phy_idx]) == phy) {
8290 			media_types &= ~(PORT_HW_CFG_MEDIA_TYPE_PHY0_MASK <<
8291 				(PORT_HW_CFG_MEDIA_TYPE_PHY1_SHIFT * phy_idx));
8292 			media_types |= ((phy->media_type &
8293 					PORT_HW_CFG_MEDIA_TYPE_PHY0_MASK) <<
8294 				(PORT_HW_CFG_MEDIA_TYPE_PHY1_SHIFT * phy_idx));
8295 			break;
8296 		}
8297 	}
8298 	REG_WR(bp, sync_offset, media_types);
8299 	if (check_limiting_mode) {
8300 		u8 options[SFP_EEPROM_OPTIONS_SIZE];
8301 		if (bnx2x_read_sfp_module_eeprom(phy,
8302 						 params,
8303 						 I2C_DEV_ADDR_A0,
8304 						 SFP_EEPROM_OPTIONS_ADDR,
8305 						 SFP_EEPROM_OPTIONS_SIZE,
8306 						 options) != 0) {
8307 			DP(NETIF_MSG_LINK,
8308 			   "Failed to read Option field from module EEPROM\n");
8309 			return -EINVAL;
8310 		}
8311 		if ((options[0] & SFP_EEPROM_OPTIONS_LINEAR_RX_OUT_MASK))
8312 			*edc_mode = EDC_MODE_LINEAR;
8313 		else
8314 			*edc_mode = EDC_MODE_LIMITING;
8315 	}
8316 	DP(NETIF_MSG_LINK, "EDC mode is set to 0x%x\n", *edc_mode);
8317 	return 0;
8318 }
8319 /* This function read the relevant field from the module (SFP+), and verify it
8320  * is compliant with this board
8321  */
8322 static int bnx2x_verify_sfp_module(struct bnx2x_phy *phy,
8323 				   struct link_params *params)
8324 {
8325 	struct bnx2x *bp = params->bp;
8326 	u32 val, cmd;
8327 	u32 fw_resp, fw_cmd_param;
8328 	char vendor_name[SFP_EEPROM_VENDOR_NAME_SIZE+1];
8329 	char vendor_pn[SFP_EEPROM_PART_NO_SIZE+1];
8330 	phy->flags &= ~FLAGS_SFP_NOT_APPROVED;
8331 	val = REG_RD(bp, params->shmem_base +
8332 			 offsetof(struct shmem_region, dev_info.
8333 				  port_feature_config[params->port].config));
8334 	if ((val & PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_MASK) ==
8335 	    PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_NO_ENFORCEMENT) {
8336 		DP(NETIF_MSG_LINK, "NOT enforcing module verification\n");
8337 		return 0;
8338 	}
8339 
8340 	if (params->feature_config_flags &
8341 	    FEATURE_CONFIG_BC_SUPPORTS_DUAL_PHY_OPT_MDL_VRFY) {
8342 		/* Use specific phy request */
8343 		cmd = DRV_MSG_CODE_VRFY_SPECIFIC_PHY_OPT_MDL;
8344 	} else if (params->feature_config_flags &
8345 		   FEATURE_CONFIG_BC_SUPPORTS_OPT_MDL_VRFY) {
8346 		/* Use first phy request only in case of non-dual media*/
8347 		if (DUAL_MEDIA(params)) {
8348 			DP(NETIF_MSG_LINK,
8349 			   "FW does not support OPT MDL verification\n");
8350 			return -EINVAL;
8351 		}
8352 		cmd = DRV_MSG_CODE_VRFY_FIRST_PHY_OPT_MDL;
8353 	} else {
8354 		/* No support in OPT MDL detection */
8355 		DP(NETIF_MSG_LINK,
8356 		   "FW does not support OPT MDL verification\n");
8357 		return -EINVAL;
8358 	}
8359 
8360 	fw_cmd_param = FW_PARAM_SET(phy->addr, phy->type, phy->mdio_ctrl);
8361 	fw_resp = bnx2x_fw_command(bp, cmd, fw_cmd_param);
8362 	if (fw_resp == FW_MSG_CODE_VRFY_OPT_MDL_SUCCESS) {
8363 		DP(NETIF_MSG_LINK, "Approved module\n");
8364 		return 0;
8365 	}
8366 
8367 	/* Format the warning message */
8368 	if (bnx2x_read_sfp_module_eeprom(phy,
8369 					 params,
8370 					 I2C_DEV_ADDR_A0,
8371 					 SFP_EEPROM_VENDOR_NAME_ADDR,
8372 					 SFP_EEPROM_VENDOR_NAME_SIZE,
8373 					 (u8 *)vendor_name))
8374 		vendor_name[0] = '\0';
8375 	else
8376 		vendor_name[SFP_EEPROM_VENDOR_NAME_SIZE] = '\0';
8377 	if (bnx2x_read_sfp_module_eeprom(phy,
8378 					 params,
8379 					 I2C_DEV_ADDR_A0,
8380 					 SFP_EEPROM_PART_NO_ADDR,
8381 					 SFP_EEPROM_PART_NO_SIZE,
8382 					 (u8 *)vendor_pn))
8383 		vendor_pn[0] = '\0';
8384 	else
8385 		vendor_pn[SFP_EEPROM_PART_NO_SIZE] = '\0';
8386 
8387 	netdev_err(bp->dev,  "Warning: Unqualified SFP+ module detected,"
8388 			      " Port %d from %s part number %s\n",
8389 			 params->port, vendor_name, vendor_pn);
8390 	if ((val & PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_MASK) !=
8391 	    PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_WARNING_MSG)
8392 		phy->flags |= FLAGS_SFP_NOT_APPROVED;
8393 	return -EINVAL;
8394 }
8395 
8396 static int bnx2x_wait_for_sfp_module_initialized(struct bnx2x_phy *phy,
8397 						 struct link_params *params)
8398 
8399 {
8400 	u8 val;
8401 	int rc;
8402 	struct bnx2x *bp = params->bp;
8403 	u16 timeout;
8404 	/* Initialization time after hot-plug may take up to 300ms for
8405 	 * some phys type ( e.g. JDSU )
8406 	 */
8407 
8408 	for (timeout = 0; timeout < 60; timeout++) {
8409 		if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
8410 			rc = bnx2x_warpcore_read_sfp_module_eeprom(
8411 				phy, params, I2C_DEV_ADDR_A0, 1, 1, &val,
8412 				1);
8413 		else
8414 			rc = bnx2x_read_sfp_module_eeprom(phy, params,
8415 							  I2C_DEV_ADDR_A0,
8416 							  1, 1, &val);
8417 		if (rc == 0) {
8418 			DP(NETIF_MSG_LINK,
8419 			   "SFP+ module initialization took %d ms\n",
8420 			   timeout * 5);
8421 			return 0;
8422 		}
8423 		usleep_range(5000, 10000);
8424 	}
8425 	rc = bnx2x_read_sfp_module_eeprom(phy, params, I2C_DEV_ADDR_A0,
8426 					  1, 1, &val);
8427 	return rc;
8428 }
8429 
8430 static void bnx2x_8727_power_module(struct bnx2x *bp,
8431 				    struct bnx2x_phy *phy,
8432 				    u8 is_power_up) {
8433 	/* Make sure GPIOs are not using for LED mode */
8434 	u16 val;
8435 	/* In the GPIO register, bit 4 is use to determine if the GPIOs are
8436 	 * operating as INPUT or as OUTPUT. Bit 1 is for input, and 0 for
8437 	 * output
8438 	 * Bits 0-1 determine the GPIOs value for OUTPUT in case bit 4 val is 0
8439 	 * Bits 8-9 determine the GPIOs value for INPUT in case bit 4 val is 1
8440 	 * where the 1st bit is the over-current(only input), and 2nd bit is
8441 	 * for power( only output )
8442 	 *
8443 	 * In case of NOC feature is disabled and power is up, set GPIO control
8444 	 *  as input to enable listening of over-current indication
8445 	 */
8446 	if (phy->flags & FLAGS_NOC)
8447 		return;
8448 	if (is_power_up)
8449 		val = (1<<4);
8450 	else
8451 		/* Set GPIO control to OUTPUT, and set the power bit
8452 		 * to according to the is_power_up
8453 		 */
8454 		val = (1<<1);
8455 
8456 	bnx2x_cl45_write(bp, phy,
8457 			 MDIO_PMA_DEVAD,
8458 			 MDIO_PMA_REG_8727_GPIO_CTRL,
8459 			 val);
8460 }
8461 
8462 static int bnx2x_8726_set_limiting_mode(struct bnx2x *bp,
8463 					struct bnx2x_phy *phy,
8464 					u16 edc_mode)
8465 {
8466 	u16 cur_limiting_mode;
8467 
8468 	bnx2x_cl45_read(bp, phy,
8469 			MDIO_PMA_DEVAD,
8470 			MDIO_PMA_REG_ROM_VER2,
8471 			&cur_limiting_mode);
8472 	DP(NETIF_MSG_LINK, "Current Limiting mode is 0x%x\n",
8473 		 cur_limiting_mode);
8474 
8475 	if (edc_mode == EDC_MODE_LIMITING) {
8476 		DP(NETIF_MSG_LINK, "Setting LIMITING MODE\n");
8477 		bnx2x_cl45_write(bp, phy,
8478 				 MDIO_PMA_DEVAD,
8479 				 MDIO_PMA_REG_ROM_VER2,
8480 				 EDC_MODE_LIMITING);
8481 	} else { /* LRM mode ( default )*/
8482 
8483 		DP(NETIF_MSG_LINK, "Setting LRM MODE\n");
8484 
8485 		/* Changing to LRM mode takes quite few seconds. So do it only
8486 		 * if current mode is limiting (default is LRM)
8487 		 */
8488 		if (cur_limiting_mode != EDC_MODE_LIMITING)
8489 			return 0;
8490 
8491 		bnx2x_cl45_write(bp, phy,
8492 				 MDIO_PMA_DEVAD,
8493 				 MDIO_PMA_REG_LRM_MODE,
8494 				 0);
8495 		bnx2x_cl45_write(bp, phy,
8496 				 MDIO_PMA_DEVAD,
8497 				 MDIO_PMA_REG_ROM_VER2,
8498 				 0x128);
8499 		bnx2x_cl45_write(bp, phy,
8500 				 MDIO_PMA_DEVAD,
8501 				 MDIO_PMA_REG_MISC_CTRL0,
8502 				 0x4008);
8503 		bnx2x_cl45_write(bp, phy,
8504 				 MDIO_PMA_DEVAD,
8505 				 MDIO_PMA_REG_LRM_MODE,
8506 				 0xaaaa);
8507 	}
8508 	return 0;
8509 }
8510 
8511 static int bnx2x_8727_set_limiting_mode(struct bnx2x *bp,
8512 					struct bnx2x_phy *phy,
8513 					u16 edc_mode)
8514 {
8515 	u16 phy_identifier;
8516 	u16 rom_ver2_val;
8517 	bnx2x_cl45_read(bp, phy,
8518 			MDIO_PMA_DEVAD,
8519 			MDIO_PMA_REG_PHY_IDENTIFIER,
8520 			&phy_identifier);
8521 
8522 	bnx2x_cl45_write(bp, phy,
8523 			 MDIO_PMA_DEVAD,
8524 			 MDIO_PMA_REG_PHY_IDENTIFIER,
8525 			 (phy_identifier & ~(1<<9)));
8526 
8527 	bnx2x_cl45_read(bp, phy,
8528 			MDIO_PMA_DEVAD,
8529 			MDIO_PMA_REG_ROM_VER2,
8530 			&rom_ver2_val);
8531 	/* Keep the MSB 8-bits, and set the LSB 8-bits with the edc_mode */
8532 	bnx2x_cl45_write(bp, phy,
8533 			 MDIO_PMA_DEVAD,
8534 			 MDIO_PMA_REG_ROM_VER2,
8535 			 (rom_ver2_val & 0xff00) | (edc_mode & 0x00ff));
8536 
8537 	bnx2x_cl45_write(bp, phy,
8538 			 MDIO_PMA_DEVAD,
8539 			 MDIO_PMA_REG_PHY_IDENTIFIER,
8540 			 (phy_identifier | (1<<9)));
8541 
8542 	return 0;
8543 }
8544 
8545 static void bnx2x_8727_specific_func(struct bnx2x_phy *phy,
8546 				     struct link_params *params,
8547 				     u32 action)
8548 {
8549 	struct bnx2x *bp = params->bp;
8550 	u16 val;
8551 	switch (action) {
8552 	case DISABLE_TX:
8553 		bnx2x_sfp_set_transmitter(params, phy, 0);
8554 		break;
8555 	case ENABLE_TX:
8556 		if (!(phy->flags & FLAGS_SFP_NOT_APPROVED))
8557 			bnx2x_sfp_set_transmitter(params, phy, 1);
8558 		break;
8559 	case PHY_INIT:
8560 		bnx2x_cl45_write(bp, phy,
8561 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXCTRL,
8562 				 (1<<2) | (1<<5));
8563 		bnx2x_cl45_write(bp, phy,
8564 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_TXCTRL,
8565 				 0);
8566 		bnx2x_cl45_write(bp, phy,
8567 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL, 0x0006);
8568 		/* Make MOD_ABS give interrupt on change */
8569 		bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD,
8570 				MDIO_PMA_REG_8727_PCS_OPT_CTRL,
8571 				&val);
8572 		val |= (1<<12);
8573 		if (phy->flags & FLAGS_NOC)
8574 			val |= (3<<5);
8575 		/* Set 8727 GPIOs to input to allow reading from the 8727 GPIO0
8576 		 * status which reflect SFP+ module over-current
8577 		 */
8578 		if (!(phy->flags & FLAGS_NOC))
8579 			val &= 0xff8f; /* Reset bits 4-6 */
8580 		bnx2x_cl45_write(bp, phy,
8581 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_8727_PCS_OPT_CTRL,
8582 				 val);
8583 		break;
8584 	default:
8585 		DP(NETIF_MSG_LINK, "Function 0x%x not supported by 8727\n",
8586 		   action);
8587 		return;
8588 	}
8589 }
8590 
8591 static void bnx2x_set_e1e2_module_fault_led(struct link_params *params,
8592 					   u8 gpio_mode)
8593 {
8594 	struct bnx2x *bp = params->bp;
8595 
8596 	u32 fault_led_gpio = REG_RD(bp, params->shmem_base +
8597 			    offsetof(struct shmem_region,
8598 			dev_info.port_hw_config[params->port].sfp_ctrl)) &
8599 		PORT_HW_CFG_FAULT_MODULE_LED_MASK;
8600 	switch (fault_led_gpio) {
8601 	case PORT_HW_CFG_FAULT_MODULE_LED_DISABLED:
8602 		return;
8603 	case PORT_HW_CFG_FAULT_MODULE_LED_GPIO0:
8604 	case PORT_HW_CFG_FAULT_MODULE_LED_GPIO1:
8605 	case PORT_HW_CFG_FAULT_MODULE_LED_GPIO2:
8606 	case PORT_HW_CFG_FAULT_MODULE_LED_GPIO3:
8607 	{
8608 		u8 gpio_port = bnx2x_get_gpio_port(params);
8609 		u16 gpio_pin = fault_led_gpio -
8610 			PORT_HW_CFG_FAULT_MODULE_LED_GPIO0;
8611 		DP(NETIF_MSG_LINK, "Set fault module-detected led "
8612 				   "pin %x port %x mode %x\n",
8613 			       gpio_pin, gpio_port, gpio_mode);
8614 		bnx2x_set_gpio(bp, gpio_pin, gpio_mode, gpio_port);
8615 	}
8616 	break;
8617 	default:
8618 		DP(NETIF_MSG_LINK, "Error: Invalid fault led mode 0x%x\n",
8619 			       fault_led_gpio);
8620 	}
8621 }
8622 
8623 static void bnx2x_set_e3_module_fault_led(struct link_params *params,
8624 					  u8 gpio_mode)
8625 {
8626 	u32 pin_cfg;
8627 	u8 port = params->port;
8628 	struct bnx2x *bp = params->bp;
8629 	pin_cfg = (REG_RD(bp, params->shmem_base +
8630 			 offsetof(struct shmem_region,
8631 				  dev_info.port_hw_config[port].e3_sfp_ctrl)) &
8632 		PORT_HW_CFG_E3_FAULT_MDL_LED_MASK) >>
8633 		PORT_HW_CFG_E3_FAULT_MDL_LED_SHIFT;
8634 	DP(NETIF_MSG_LINK, "Setting Fault LED to %d using pin cfg %d\n",
8635 		       gpio_mode, pin_cfg);
8636 	bnx2x_set_cfg_pin(bp, pin_cfg, gpio_mode);
8637 }
8638 
8639 static void bnx2x_set_sfp_module_fault_led(struct link_params *params,
8640 					   u8 gpio_mode)
8641 {
8642 	struct bnx2x *bp = params->bp;
8643 	DP(NETIF_MSG_LINK, "Setting SFP+ module fault LED to %d\n", gpio_mode);
8644 	if (CHIP_IS_E3(bp)) {
8645 		/* Low ==> if SFP+ module is supported otherwise
8646 		 * High ==> if SFP+ module is not on the approved vendor list
8647 		 */
8648 		bnx2x_set_e3_module_fault_led(params, gpio_mode);
8649 	} else
8650 		bnx2x_set_e1e2_module_fault_led(params, gpio_mode);
8651 }
8652 
8653 static void bnx2x_warpcore_hw_reset(struct bnx2x_phy *phy,
8654 				    struct link_params *params)
8655 {
8656 	struct bnx2x *bp = params->bp;
8657 	bnx2x_warpcore_power_module(params, 0);
8658 	/* Put Warpcore in low power mode */
8659 	REG_WR(bp, MISC_REG_WC0_RESET, 0x0c0e);
8660 
8661 	/* Put LCPLL in low power mode */
8662 	REG_WR(bp, MISC_REG_LCPLL_E40_PWRDWN, 1);
8663 	REG_WR(bp, MISC_REG_LCPLL_E40_RESETB_ANA, 0);
8664 	REG_WR(bp, MISC_REG_LCPLL_E40_RESETB_DIG, 0);
8665 }
8666 
8667 static void bnx2x_power_sfp_module(struct link_params *params,
8668 				   struct bnx2x_phy *phy,
8669 				   u8 power)
8670 {
8671 	struct bnx2x *bp = params->bp;
8672 	DP(NETIF_MSG_LINK, "Setting SFP+ power to %x\n", power);
8673 
8674 	switch (phy->type) {
8675 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
8676 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8722:
8677 		bnx2x_8727_power_module(params->bp, phy, power);
8678 		break;
8679 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT:
8680 		bnx2x_warpcore_power_module(params, power);
8681 		break;
8682 	default:
8683 		break;
8684 	}
8685 }
8686 static void bnx2x_warpcore_set_limiting_mode(struct link_params *params,
8687 					     struct bnx2x_phy *phy,
8688 					     u16 edc_mode)
8689 {
8690 	u16 val = 0;
8691 	u16 mode = MDIO_WC_REG_UC_INFO_B1_FIRMWARE_MODE_DEFAULT;
8692 	struct bnx2x *bp = params->bp;
8693 
8694 	u8 lane = bnx2x_get_warpcore_lane(phy, params);
8695 	/* This is a global register which controls all lanes */
8696 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
8697 			MDIO_WC_REG_UC_INFO_B1_FIRMWARE_MODE, &val);
8698 	val &= ~(0xf << (lane << 2));
8699 
8700 	switch (edc_mode) {
8701 	case EDC_MODE_LINEAR:
8702 	case EDC_MODE_LIMITING:
8703 		mode = MDIO_WC_REG_UC_INFO_B1_FIRMWARE_MODE_DEFAULT;
8704 		break;
8705 	case EDC_MODE_PASSIVE_DAC:
8706 	case EDC_MODE_ACTIVE_DAC:
8707 		mode = MDIO_WC_REG_UC_INFO_B1_FIRMWARE_MODE_SFP_DAC;
8708 		break;
8709 	default:
8710 		break;
8711 	}
8712 
8713 	val |= (mode << (lane << 2));
8714 	bnx2x_cl45_write(bp, phy, MDIO_WC_DEVAD,
8715 			 MDIO_WC_REG_UC_INFO_B1_FIRMWARE_MODE, val);
8716 	/* A must read */
8717 	bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
8718 			MDIO_WC_REG_UC_INFO_B1_FIRMWARE_MODE, &val);
8719 
8720 	/* Restart microcode to re-read the new mode */
8721 	bnx2x_warpcore_reset_lane(bp, phy, 1);
8722 	bnx2x_warpcore_reset_lane(bp, phy, 0);
8723 
8724 }
8725 
8726 static void bnx2x_set_limiting_mode(struct link_params *params,
8727 				    struct bnx2x_phy *phy,
8728 				    u16 edc_mode)
8729 {
8730 	switch (phy->type) {
8731 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
8732 		bnx2x_8726_set_limiting_mode(params->bp, phy, edc_mode);
8733 		break;
8734 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
8735 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8722:
8736 		bnx2x_8727_set_limiting_mode(params->bp, phy, edc_mode);
8737 		break;
8738 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT:
8739 		bnx2x_warpcore_set_limiting_mode(params, phy, edc_mode);
8740 		break;
8741 	}
8742 }
8743 
8744 static int bnx2x_sfp_module_detection(struct bnx2x_phy *phy,
8745 				      struct link_params *params)
8746 {
8747 	struct bnx2x *bp = params->bp;
8748 	u16 edc_mode;
8749 	int rc = 0;
8750 
8751 	u32 val = REG_RD(bp, params->shmem_base +
8752 			     offsetof(struct shmem_region, dev_info.
8753 				     port_feature_config[params->port].config));
8754 	/* Enabled transmitter by default */
8755 	bnx2x_sfp_set_transmitter(params, phy, 1);
8756 	DP(NETIF_MSG_LINK, "SFP+ module plugged in/out detected on port %d\n",
8757 		 params->port);
8758 	/* Power up module */
8759 	bnx2x_power_sfp_module(params, phy, 1);
8760 	if (bnx2x_get_edc_mode(phy, params, &edc_mode) != 0) {
8761 		DP(NETIF_MSG_LINK, "Failed to get valid module type\n");
8762 		return -EINVAL;
8763 	} else if (bnx2x_verify_sfp_module(phy, params) != 0) {
8764 		/* Check SFP+ module compatibility */
8765 		DP(NETIF_MSG_LINK, "Module verification failed!!\n");
8766 		rc = -EINVAL;
8767 		/* Turn on fault module-detected led */
8768 		bnx2x_set_sfp_module_fault_led(params,
8769 					       MISC_REGISTERS_GPIO_HIGH);
8770 
8771 		/* Check if need to power down the SFP+ module */
8772 		if ((val & PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_MASK) ==
8773 		     PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_POWER_DOWN) {
8774 			DP(NETIF_MSG_LINK, "Shutdown SFP+ module!!\n");
8775 			bnx2x_power_sfp_module(params, phy, 0);
8776 			return rc;
8777 		}
8778 	} else {
8779 		/* Turn off fault module-detected led */
8780 		bnx2x_set_sfp_module_fault_led(params, MISC_REGISTERS_GPIO_LOW);
8781 	}
8782 
8783 	/* Check and set limiting mode / LRM mode on 8726. On 8727 it
8784 	 * is done automatically
8785 	 */
8786 	bnx2x_set_limiting_mode(params, phy, edc_mode);
8787 
8788 	/* Disable transmit for this module if the module is not approved, and
8789 	 * laser needs to be disabled.
8790 	 */
8791 	if ((rc) &&
8792 	    ((val & PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_MASK) ==
8793 	     PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_DISABLE_TX_LASER))
8794 		bnx2x_sfp_set_transmitter(params, phy, 0);
8795 
8796 	return rc;
8797 }
8798 
8799 void bnx2x_handle_module_detect_int(struct link_params *params)
8800 {
8801 	struct bnx2x *bp = params->bp;
8802 	struct bnx2x_phy *phy;
8803 	u32 gpio_val;
8804 	u8 gpio_num, gpio_port;
8805 	if (CHIP_IS_E3(bp)) {
8806 		phy = &params->phy[INT_PHY];
8807 		/* Always enable TX laser,will be disabled in case of fault */
8808 		bnx2x_sfp_set_transmitter(params, phy, 1);
8809 	} else {
8810 		phy = &params->phy[EXT_PHY1];
8811 	}
8812 	if (bnx2x_get_mod_abs_int_cfg(bp, params->chip_id, params->shmem_base,
8813 				      params->port, &gpio_num, &gpio_port) ==
8814 	    -EINVAL) {
8815 		DP(NETIF_MSG_LINK, "Failed to get MOD_ABS interrupt config\n");
8816 		return;
8817 	}
8818 
8819 	/* Set valid module led off */
8820 	bnx2x_set_sfp_module_fault_led(params, MISC_REGISTERS_GPIO_HIGH);
8821 
8822 	/* Get current gpio val reflecting module plugged in / out*/
8823 	gpio_val = bnx2x_get_gpio(bp, gpio_num, gpio_port);
8824 
8825 	/* Call the handling function in case module is detected */
8826 	if (gpio_val == 0) {
8827 		bnx2x_set_mdio_emac_per_phy(bp, params);
8828 		bnx2x_set_aer_mmd(params, phy);
8829 
8830 		bnx2x_power_sfp_module(params, phy, 1);
8831 		bnx2x_set_gpio_int(bp, gpio_num,
8832 				   MISC_REGISTERS_GPIO_INT_OUTPUT_CLR,
8833 				   gpio_port);
8834 		if (bnx2x_wait_for_sfp_module_initialized(phy, params) == 0) {
8835 			bnx2x_sfp_module_detection(phy, params);
8836 			if (CHIP_IS_E3(bp)) {
8837 				u16 rx_tx_in_reset;
8838 				/* In case WC is out of reset, reconfigure the
8839 				 * link speed while taking into account 1G
8840 				 * module limitation.
8841 				 */
8842 				bnx2x_cl45_read(bp, phy,
8843 						MDIO_WC_DEVAD,
8844 						MDIO_WC_REG_DIGITAL5_MISC6,
8845 						&rx_tx_in_reset);
8846 				if ((!rx_tx_in_reset) &&
8847 				    (params->link_flags &
8848 				     PHY_INITIALIZED)) {
8849 					bnx2x_warpcore_reset_lane(bp, phy, 1);
8850 					bnx2x_warpcore_config_sfi(phy, params);
8851 					bnx2x_warpcore_reset_lane(bp, phy, 0);
8852 				}
8853 			}
8854 		} else {
8855 			DP(NETIF_MSG_LINK, "SFP+ module is not initialized\n");
8856 		}
8857 	} else {
8858 		bnx2x_set_gpio_int(bp, gpio_num,
8859 				   MISC_REGISTERS_GPIO_INT_OUTPUT_SET,
8860 				   gpio_port);
8861 		/* Module was plugged out.
8862 		 * Disable transmit for this module
8863 		 */
8864 		phy->media_type = ETH_PHY_NOT_PRESENT;
8865 	}
8866 }
8867 
8868 /******************************************************************/
8869 /*		Used by 8706 and 8727                             */
8870 /******************************************************************/
8871 static void bnx2x_sfp_mask_fault(struct bnx2x *bp,
8872 				 struct bnx2x_phy *phy,
8873 				 u16 alarm_status_offset,
8874 				 u16 alarm_ctrl_offset)
8875 {
8876 	u16 alarm_status, val;
8877 	bnx2x_cl45_read(bp, phy,
8878 			MDIO_PMA_DEVAD, alarm_status_offset,
8879 			&alarm_status);
8880 	bnx2x_cl45_read(bp, phy,
8881 			MDIO_PMA_DEVAD, alarm_status_offset,
8882 			&alarm_status);
8883 	/* Mask or enable the fault event. */
8884 	bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD, alarm_ctrl_offset, &val);
8885 	if (alarm_status & (1<<0))
8886 		val &= ~(1<<0);
8887 	else
8888 		val |= (1<<0);
8889 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, alarm_ctrl_offset, val);
8890 }
8891 /******************************************************************/
8892 /*		common BCM8706/BCM8726 PHY SECTION		  */
8893 /******************************************************************/
8894 static u8 bnx2x_8706_8726_read_status(struct bnx2x_phy *phy,
8895 				      struct link_params *params,
8896 				      struct link_vars *vars)
8897 {
8898 	u8 link_up = 0;
8899 	u16 val1, val2, rx_sd, pcs_status;
8900 	struct bnx2x *bp = params->bp;
8901 	DP(NETIF_MSG_LINK, "XGXS 8706/8726\n");
8902 	/* Clear RX Alarm*/
8903 	bnx2x_cl45_read(bp, phy,
8904 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXSTAT, &val2);
8905 
8906 	bnx2x_sfp_mask_fault(bp, phy, MDIO_PMA_LASI_TXSTAT,
8907 			     MDIO_PMA_LASI_TXCTRL);
8908 
8909 	/* Clear LASI indication*/
8910 	bnx2x_cl45_read(bp, phy,
8911 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_STAT, &val1);
8912 	bnx2x_cl45_read(bp, phy,
8913 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_STAT, &val2);
8914 	DP(NETIF_MSG_LINK, "8706/8726 LASI status 0x%x--> 0x%x\n", val1, val2);
8915 
8916 	bnx2x_cl45_read(bp, phy,
8917 			MDIO_PMA_DEVAD, MDIO_PMA_REG_RX_SD, &rx_sd);
8918 	bnx2x_cl45_read(bp, phy,
8919 			MDIO_PCS_DEVAD, MDIO_PCS_REG_STATUS, &pcs_status);
8920 	bnx2x_cl45_read(bp, phy,
8921 			MDIO_AN_DEVAD, MDIO_AN_REG_LINK_STATUS, &val2);
8922 	bnx2x_cl45_read(bp, phy,
8923 			MDIO_AN_DEVAD, MDIO_AN_REG_LINK_STATUS, &val2);
8924 
8925 	DP(NETIF_MSG_LINK, "8706/8726 rx_sd 0x%x pcs_status 0x%x 1Gbps"
8926 			" link_status 0x%x\n", rx_sd, pcs_status, val2);
8927 	/* Link is up if both bit 0 of pmd_rx_sd and bit 0 of pcs_status
8928 	 * are set, or if the autoneg bit 1 is set
8929 	 */
8930 	link_up = ((rx_sd & pcs_status & 0x1) || (val2 & (1<<1)));
8931 	if (link_up) {
8932 		if (val2 & (1<<1))
8933 			vars->line_speed = SPEED_1000;
8934 		else
8935 			vars->line_speed = SPEED_10000;
8936 		bnx2x_ext_phy_resolve_fc(phy, params, vars);
8937 		vars->duplex = DUPLEX_FULL;
8938 	}
8939 
8940 	/* Capture 10G link fault. Read twice to clear stale value. */
8941 	if (vars->line_speed == SPEED_10000) {
8942 		bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD,
8943 			    MDIO_PMA_LASI_TXSTAT, &val1);
8944 		bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD,
8945 			    MDIO_PMA_LASI_TXSTAT, &val1);
8946 		if (val1 & (1<<0))
8947 			vars->fault_detected = 1;
8948 	}
8949 
8950 	return link_up;
8951 }
8952 
8953 /******************************************************************/
8954 /*			BCM8706 PHY SECTION			  */
8955 /******************************************************************/
8956 static u8 bnx2x_8706_config_init(struct bnx2x_phy *phy,
8957 				 struct link_params *params,
8958 				 struct link_vars *vars)
8959 {
8960 	u32 tx_en_mode;
8961 	u16 cnt, val, tmp1;
8962 	struct bnx2x *bp = params->bp;
8963 
8964 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
8965 		       MISC_REGISTERS_GPIO_OUTPUT_HIGH, params->port);
8966 	/* HW reset */
8967 	bnx2x_ext_phy_hw_reset(bp, params->port);
8968 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0xa040);
8969 	bnx2x_wait_reset_complete(bp, phy, params);
8970 
8971 	/* Wait until fw is loaded */
8972 	for (cnt = 0; cnt < 100; cnt++) {
8973 		bnx2x_cl45_read(bp, phy,
8974 				MDIO_PMA_DEVAD, MDIO_PMA_REG_ROM_VER1, &val);
8975 		if (val)
8976 			break;
8977 		usleep_range(10000, 20000);
8978 	}
8979 	DP(NETIF_MSG_LINK, "XGXS 8706 is initialized after %d ms\n", cnt);
8980 	if ((params->feature_config_flags &
8981 	     FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED)) {
8982 		u8 i;
8983 		u16 reg;
8984 		for (i = 0; i < 4; i++) {
8985 			reg = MDIO_XS_8706_REG_BANK_RX0 +
8986 				i*(MDIO_XS_8706_REG_BANK_RX1 -
8987 				   MDIO_XS_8706_REG_BANK_RX0);
8988 			bnx2x_cl45_read(bp, phy, MDIO_XS_DEVAD, reg, &val);
8989 			/* Clear first 3 bits of the control */
8990 			val &= ~0x7;
8991 			/* Set control bits according to configuration */
8992 			val |= (phy->rx_preemphasis[i] & 0x7);
8993 			DP(NETIF_MSG_LINK, "Setting RX Equalizer to BCM8706"
8994 				   " reg 0x%x <-- val 0x%x\n", reg, val);
8995 			bnx2x_cl45_write(bp, phy, MDIO_XS_DEVAD, reg, val);
8996 		}
8997 	}
8998 	/* Force speed */
8999 	if (phy->req_line_speed == SPEED_10000) {
9000 		DP(NETIF_MSG_LINK, "XGXS 8706 force 10Gbps\n");
9001 
9002 		bnx2x_cl45_write(bp, phy,
9003 				 MDIO_PMA_DEVAD,
9004 				 MDIO_PMA_REG_DIGITAL_CTRL, 0x400);
9005 		bnx2x_cl45_write(bp, phy,
9006 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_TXCTRL,
9007 				 0);
9008 		/* Arm LASI for link and Tx fault. */
9009 		bnx2x_cl45_write(bp, phy,
9010 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL, 3);
9011 	} else {
9012 		/* Force 1Gbps using autoneg with 1G advertisement */
9013 
9014 		/* Allow CL37 through CL73 */
9015 		DP(NETIF_MSG_LINK, "XGXS 8706 AutoNeg\n");
9016 		bnx2x_cl45_write(bp, phy,
9017 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_CL73, 0x040c);
9018 
9019 		/* Enable Full-Duplex advertisement on CL37 */
9020 		bnx2x_cl45_write(bp, phy,
9021 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_FC_LP, 0x0020);
9022 		/* Enable CL37 AN */
9023 		bnx2x_cl45_write(bp, phy,
9024 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_AN, 0x1000);
9025 		/* 1G support */
9026 		bnx2x_cl45_write(bp, phy,
9027 				 MDIO_AN_DEVAD, MDIO_AN_REG_ADV, (1<<5));
9028 
9029 		/* Enable clause 73 AN */
9030 		bnx2x_cl45_write(bp, phy,
9031 				 MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, 0x1200);
9032 		bnx2x_cl45_write(bp, phy,
9033 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXCTRL,
9034 				 0x0400);
9035 		bnx2x_cl45_write(bp, phy,
9036 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL,
9037 				 0x0004);
9038 	}
9039 	bnx2x_save_bcm_spirom_ver(bp, phy, params->port);
9040 
9041 	/* If TX Laser is controlled by GPIO_0, do not let PHY go into low
9042 	 * power mode, if TX Laser is disabled
9043 	 */
9044 
9045 	tx_en_mode = REG_RD(bp, params->shmem_base +
9046 			    offsetof(struct shmem_region,
9047 				dev_info.port_hw_config[params->port].sfp_ctrl))
9048 			& PORT_HW_CFG_TX_LASER_MASK;
9049 
9050 	if (tx_en_mode == PORT_HW_CFG_TX_LASER_GPIO0) {
9051 		DP(NETIF_MSG_LINK, "Enabling TXONOFF_PWRDN_DIS\n");
9052 		bnx2x_cl45_read(bp, phy,
9053 			MDIO_PMA_DEVAD, MDIO_PMA_REG_DIGITAL_CTRL, &tmp1);
9054 		tmp1 |= 0x1;
9055 		bnx2x_cl45_write(bp, phy,
9056 			MDIO_PMA_DEVAD, MDIO_PMA_REG_DIGITAL_CTRL, tmp1);
9057 	}
9058 
9059 	return 0;
9060 }
9061 
9062 static int bnx2x_8706_read_status(struct bnx2x_phy *phy,
9063 				  struct link_params *params,
9064 				  struct link_vars *vars)
9065 {
9066 	return bnx2x_8706_8726_read_status(phy, params, vars);
9067 }
9068 
9069 /******************************************************************/
9070 /*			BCM8726 PHY SECTION			  */
9071 /******************************************************************/
9072 static void bnx2x_8726_config_loopback(struct bnx2x_phy *phy,
9073 				       struct link_params *params)
9074 {
9075 	struct bnx2x *bp = params->bp;
9076 	DP(NETIF_MSG_LINK, "PMA/PMD ext_phy_loopback: 8726\n");
9077 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0x0001);
9078 }
9079 
9080 static void bnx2x_8726_external_rom_boot(struct bnx2x_phy *phy,
9081 					 struct link_params *params)
9082 {
9083 	struct bnx2x *bp = params->bp;
9084 	/* Need to wait 100ms after reset */
9085 	msleep(100);
9086 
9087 	/* Micro controller re-boot */
9088 	bnx2x_cl45_write(bp, phy,
9089 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_GEN_CTRL, 0x018B);
9090 
9091 	/* Set soft reset */
9092 	bnx2x_cl45_write(bp, phy,
9093 			 MDIO_PMA_DEVAD,
9094 			 MDIO_PMA_REG_GEN_CTRL,
9095 			 MDIO_PMA_REG_GEN_CTRL_ROM_MICRO_RESET);
9096 
9097 	bnx2x_cl45_write(bp, phy,
9098 			 MDIO_PMA_DEVAD,
9099 			 MDIO_PMA_REG_MISC_CTRL1, 0x0001);
9100 
9101 	bnx2x_cl45_write(bp, phy,
9102 			 MDIO_PMA_DEVAD,
9103 			 MDIO_PMA_REG_GEN_CTRL,
9104 			 MDIO_PMA_REG_GEN_CTRL_ROM_RESET_INTERNAL_MP);
9105 
9106 	/* Wait for 150ms for microcode load */
9107 	msleep(150);
9108 
9109 	/* Disable serial boot control, tristates pins SS_N, SCK, MOSI, MISO */
9110 	bnx2x_cl45_write(bp, phy,
9111 			 MDIO_PMA_DEVAD,
9112 			 MDIO_PMA_REG_MISC_CTRL1, 0x0000);
9113 
9114 	msleep(200);
9115 	bnx2x_save_bcm_spirom_ver(bp, phy, params->port);
9116 }
9117 
9118 static u8 bnx2x_8726_read_status(struct bnx2x_phy *phy,
9119 				 struct link_params *params,
9120 				 struct link_vars *vars)
9121 {
9122 	struct bnx2x *bp = params->bp;
9123 	u16 val1;
9124 	u8 link_up = bnx2x_8706_8726_read_status(phy, params, vars);
9125 	if (link_up) {
9126 		bnx2x_cl45_read(bp, phy,
9127 				MDIO_PMA_DEVAD, MDIO_PMA_REG_PHY_IDENTIFIER,
9128 				&val1);
9129 		if (val1 & (1<<15)) {
9130 			DP(NETIF_MSG_LINK, "Tx is disabled\n");
9131 			link_up = 0;
9132 			vars->line_speed = 0;
9133 		}
9134 	}
9135 	return link_up;
9136 }
9137 
9138 
9139 static int bnx2x_8726_config_init(struct bnx2x_phy *phy,
9140 				  struct link_params *params,
9141 				  struct link_vars *vars)
9142 {
9143 	struct bnx2x *bp = params->bp;
9144 	DP(NETIF_MSG_LINK, "Initializing BCM8726\n");
9145 
9146 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 1<<15);
9147 	bnx2x_wait_reset_complete(bp, phy, params);
9148 
9149 	bnx2x_8726_external_rom_boot(phy, params);
9150 
9151 	/* Need to call module detected on initialization since the module
9152 	 * detection triggered by actual module insertion might occur before
9153 	 * driver is loaded, and when driver is loaded, it reset all
9154 	 * registers, including the transmitter
9155 	 */
9156 	bnx2x_sfp_module_detection(phy, params);
9157 
9158 	if (phy->req_line_speed == SPEED_1000) {
9159 		DP(NETIF_MSG_LINK, "Setting 1G force\n");
9160 		bnx2x_cl45_write(bp, phy,
9161 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0x40);
9162 		bnx2x_cl45_write(bp, phy,
9163 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_10G_CTRL2, 0xD);
9164 		bnx2x_cl45_write(bp, phy,
9165 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL, 0x5);
9166 		bnx2x_cl45_write(bp, phy,
9167 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXCTRL,
9168 				 0x400);
9169 	} else if ((phy->req_line_speed == SPEED_AUTO_NEG) &&
9170 		   (phy->speed_cap_mask &
9171 		      PORT_HW_CFG_SPEED_CAPABILITY_D0_1G) &&
9172 		   ((phy->speed_cap_mask &
9173 		      PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) !=
9174 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
9175 		DP(NETIF_MSG_LINK, "Setting 1G clause37\n");
9176 		/* Set Flow control */
9177 		bnx2x_ext_phy_set_pause(params, phy, vars);
9178 		bnx2x_cl45_write(bp, phy,
9179 				 MDIO_AN_DEVAD, MDIO_AN_REG_ADV, 0x20);
9180 		bnx2x_cl45_write(bp, phy,
9181 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_CL73, 0x040c);
9182 		bnx2x_cl45_write(bp, phy,
9183 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_FC_LD, 0x0020);
9184 		bnx2x_cl45_write(bp, phy,
9185 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_AN, 0x1000);
9186 		bnx2x_cl45_write(bp, phy,
9187 				MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, 0x1200);
9188 		/* Enable RX-ALARM control to receive interrupt for 1G speed
9189 		 * change
9190 		 */
9191 		bnx2x_cl45_write(bp, phy,
9192 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL, 0x4);
9193 		bnx2x_cl45_write(bp, phy,
9194 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXCTRL,
9195 				 0x400);
9196 
9197 	} else { /* Default 10G. Set only LASI control */
9198 		bnx2x_cl45_write(bp, phy,
9199 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL, 1);
9200 	}
9201 
9202 	/* Set TX PreEmphasis if needed */
9203 	if ((params->feature_config_flags &
9204 	     FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED)) {
9205 		DP(NETIF_MSG_LINK,
9206 		   "Setting TX_CTRL1 0x%x, TX_CTRL2 0x%x\n",
9207 			 phy->tx_preemphasis[0],
9208 			 phy->tx_preemphasis[1]);
9209 		bnx2x_cl45_write(bp, phy,
9210 				 MDIO_PMA_DEVAD,
9211 				 MDIO_PMA_REG_8726_TX_CTRL1,
9212 				 phy->tx_preemphasis[0]);
9213 
9214 		bnx2x_cl45_write(bp, phy,
9215 				 MDIO_PMA_DEVAD,
9216 				 MDIO_PMA_REG_8726_TX_CTRL2,
9217 				 phy->tx_preemphasis[1]);
9218 	}
9219 
9220 	return 0;
9221 
9222 }
9223 
9224 static void bnx2x_8726_link_reset(struct bnx2x_phy *phy,
9225 				  struct link_params *params)
9226 {
9227 	struct bnx2x *bp = params->bp;
9228 	DP(NETIF_MSG_LINK, "bnx2x_8726_link_reset port %d\n", params->port);
9229 	/* Set serial boot control for external load */
9230 	bnx2x_cl45_write(bp, phy,
9231 			 MDIO_PMA_DEVAD,
9232 			 MDIO_PMA_REG_GEN_CTRL, 0x0001);
9233 }
9234 
9235 /******************************************************************/
9236 /*			BCM8727 PHY SECTION			  */
9237 /******************************************************************/
9238 
9239 static void bnx2x_8727_set_link_led(struct bnx2x_phy *phy,
9240 				    struct link_params *params, u8 mode)
9241 {
9242 	struct bnx2x *bp = params->bp;
9243 	u16 led_mode_bitmask = 0;
9244 	u16 gpio_pins_bitmask = 0;
9245 	u16 val;
9246 	/* Only NOC flavor requires to set the LED specifically */
9247 	if (!(phy->flags & FLAGS_NOC))
9248 		return;
9249 	switch (mode) {
9250 	case LED_MODE_FRONT_PANEL_OFF:
9251 	case LED_MODE_OFF:
9252 		led_mode_bitmask = 0;
9253 		gpio_pins_bitmask = 0x03;
9254 		break;
9255 	case LED_MODE_ON:
9256 		led_mode_bitmask = 0;
9257 		gpio_pins_bitmask = 0x02;
9258 		break;
9259 	case LED_MODE_OPER:
9260 		led_mode_bitmask = 0x60;
9261 		gpio_pins_bitmask = 0x11;
9262 		break;
9263 	}
9264 	bnx2x_cl45_read(bp, phy,
9265 			MDIO_PMA_DEVAD,
9266 			MDIO_PMA_REG_8727_PCS_OPT_CTRL,
9267 			&val);
9268 	val &= 0xff8f;
9269 	val |= led_mode_bitmask;
9270 	bnx2x_cl45_write(bp, phy,
9271 			 MDIO_PMA_DEVAD,
9272 			 MDIO_PMA_REG_8727_PCS_OPT_CTRL,
9273 			 val);
9274 	bnx2x_cl45_read(bp, phy,
9275 			MDIO_PMA_DEVAD,
9276 			MDIO_PMA_REG_8727_GPIO_CTRL,
9277 			&val);
9278 	val &= 0xffe0;
9279 	val |= gpio_pins_bitmask;
9280 	bnx2x_cl45_write(bp, phy,
9281 			 MDIO_PMA_DEVAD,
9282 			 MDIO_PMA_REG_8727_GPIO_CTRL,
9283 			 val);
9284 }
9285 static void bnx2x_8727_hw_reset(struct bnx2x_phy *phy,
9286 				struct link_params *params) {
9287 	u32 swap_val, swap_override;
9288 	u8 port;
9289 	/* The PHY reset is controlled by GPIO 1. Fake the port number
9290 	 * to cancel the swap done in set_gpio()
9291 	 */
9292 	struct bnx2x *bp = params->bp;
9293 	swap_val = REG_RD(bp, NIG_REG_PORT_SWAP);
9294 	swap_override = REG_RD(bp, NIG_REG_STRAP_OVERRIDE);
9295 	port = (swap_val && swap_override) ^ 1;
9296 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_1,
9297 		       MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
9298 }
9299 
9300 static void bnx2x_8727_config_speed(struct bnx2x_phy *phy,
9301 				    struct link_params *params)
9302 {
9303 	struct bnx2x *bp = params->bp;
9304 	u16 tmp1, val;
9305 	/* Set option 1G speed */
9306 	if ((phy->req_line_speed == SPEED_1000) ||
9307 	    (phy->media_type == ETH_PHY_SFP_1G_FIBER)) {
9308 		DP(NETIF_MSG_LINK, "Setting 1G force\n");
9309 		bnx2x_cl45_write(bp, phy,
9310 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0x40);
9311 		bnx2x_cl45_write(bp, phy,
9312 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_10G_CTRL2, 0xD);
9313 		bnx2x_cl45_read(bp, phy,
9314 				MDIO_PMA_DEVAD, MDIO_PMA_REG_10G_CTRL2, &tmp1);
9315 		DP(NETIF_MSG_LINK, "1.7 = 0x%x\n", tmp1);
9316 		/* Power down the XAUI until link is up in case of dual-media
9317 		 * and 1G
9318 		 */
9319 		if (DUAL_MEDIA(params)) {
9320 			bnx2x_cl45_read(bp, phy,
9321 					MDIO_PMA_DEVAD,
9322 					MDIO_PMA_REG_8727_PCS_GP, &val);
9323 			val |= (3<<10);
9324 			bnx2x_cl45_write(bp, phy,
9325 					 MDIO_PMA_DEVAD,
9326 					 MDIO_PMA_REG_8727_PCS_GP, val);
9327 		}
9328 	} else if ((phy->req_line_speed == SPEED_AUTO_NEG) &&
9329 		   ((phy->speed_cap_mask &
9330 		     PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) &&
9331 		   ((phy->speed_cap_mask &
9332 		      PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) !=
9333 		   PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) {
9334 
9335 		DP(NETIF_MSG_LINK, "Setting 1G clause37\n");
9336 		bnx2x_cl45_write(bp, phy,
9337 				 MDIO_AN_DEVAD, MDIO_AN_REG_8727_MISC_CTRL, 0);
9338 		bnx2x_cl45_write(bp, phy,
9339 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_AN, 0x1300);
9340 	} else {
9341 		/* Since the 8727 has only single reset pin, need to set the 10G
9342 		 * registers although it is default
9343 		 */
9344 		bnx2x_cl45_write(bp, phy,
9345 				 MDIO_AN_DEVAD, MDIO_AN_REG_8727_MISC_CTRL,
9346 				 0x0020);
9347 		bnx2x_cl45_write(bp, phy,
9348 				 MDIO_AN_DEVAD, MDIO_AN_REG_CL37_AN, 0x0100);
9349 		bnx2x_cl45_write(bp, phy,
9350 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0x2040);
9351 		bnx2x_cl45_write(bp, phy,
9352 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_10G_CTRL2,
9353 				 0x0008);
9354 	}
9355 }
9356 
9357 static int bnx2x_8727_config_init(struct bnx2x_phy *phy,
9358 				  struct link_params *params,
9359 				  struct link_vars *vars)
9360 {
9361 	u32 tx_en_mode;
9362 	u16 tmp1, mod_abs, tmp2;
9363 	struct bnx2x *bp = params->bp;
9364 	/* Enable PMD link, MOD_ABS_FLT, and 1G link alarm */
9365 
9366 	bnx2x_wait_reset_complete(bp, phy, params);
9367 
9368 	DP(NETIF_MSG_LINK, "Initializing BCM8727\n");
9369 
9370 	bnx2x_8727_specific_func(phy, params, PHY_INIT);
9371 	/* Initially configure MOD_ABS to interrupt when module is
9372 	 * presence( bit 8)
9373 	 */
9374 	bnx2x_cl45_read(bp, phy,
9375 			MDIO_PMA_DEVAD, MDIO_PMA_REG_PHY_IDENTIFIER, &mod_abs);
9376 	/* Set EDC off by setting OPTXLOS signal input to low (bit 9).
9377 	 * When the EDC is off it locks onto a reference clock and avoids
9378 	 * becoming 'lost'
9379 	 */
9380 	mod_abs &= ~(1<<8);
9381 	if (!(phy->flags & FLAGS_NOC))
9382 		mod_abs &= ~(1<<9);
9383 	bnx2x_cl45_write(bp, phy,
9384 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_PHY_IDENTIFIER, mod_abs);
9385 
9386 	/* Enable/Disable PHY transmitter output */
9387 	bnx2x_set_disable_pmd_transmit(params, phy, 0);
9388 
9389 	bnx2x_8727_power_module(bp, phy, 1);
9390 
9391 	bnx2x_cl45_read(bp, phy,
9392 			MDIO_PMA_DEVAD, MDIO_PMA_REG_M8051_MSGOUT_REG, &tmp1);
9393 
9394 	bnx2x_cl45_read(bp, phy,
9395 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXSTAT, &tmp1);
9396 
9397 	bnx2x_8727_config_speed(phy, params);
9398 
9399 
9400 	/* Set TX PreEmphasis if needed */
9401 	if ((params->feature_config_flags &
9402 	     FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED)) {
9403 		DP(NETIF_MSG_LINK, "Setting TX_CTRL1 0x%x, TX_CTRL2 0x%x\n",
9404 			   phy->tx_preemphasis[0],
9405 			   phy->tx_preemphasis[1]);
9406 		bnx2x_cl45_write(bp, phy,
9407 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_8727_TX_CTRL1,
9408 				 phy->tx_preemphasis[0]);
9409 
9410 		bnx2x_cl45_write(bp, phy,
9411 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_8727_TX_CTRL2,
9412 				 phy->tx_preemphasis[1]);
9413 	}
9414 
9415 	/* If TX Laser is controlled by GPIO_0, do not let PHY go into low
9416 	 * power mode, if TX Laser is disabled
9417 	 */
9418 	tx_en_mode = REG_RD(bp, params->shmem_base +
9419 			    offsetof(struct shmem_region,
9420 				dev_info.port_hw_config[params->port].sfp_ctrl))
9421 			& PORT_HW_CFG_TX_LASER_MASK;
9422 
9423 	if (tx_en_mode == PORT_HW_CFG_TX_LASER_GPIO0) {
9424 
9425 		DP(NETIF_MSG_LINK, "Enabling TXONOFF_PWRDN_DIS\n");
9426 		bnx2x_cl45_read(bp, phy,
9427 			MDIO_PMA_DEVAD, MDIO_PMA_REG_8727_OPT_CFG_REG, &tmp2);
9428 		tmp2 |= 0x1000;
9429 		tmp2 &= 0xFFEF;
9430 		bnx2x_cl45_write(bp, phy,
9431 			MDIO_PMA_DEVAD, MDIO_PMA_REG_8727_OPT_CFG_REG, tmp2);
9432 		bnx2x_cl45_read(bp, phy,
9433 				MDIO_PMA_DEVAD, MDIO_PMA_REG_PHY_IDENTIFIER,
9434 				&tmp2);
9435 		bnx2x_cl45_write(bp, phy,
9436 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_PHY_IDENTIFIER,
9437 				 (tmp2 & 0x7fff));
9438 	}
9439 
9440 	return 0;
9441 }
9442 
9443 static void bnx2x_8727_handle_mod_abs(struct bnx2x_phy *phy,
9444 				      struct link_params *params)
9445 {
9446 	struct bnx2x *bp = params->bp;
9447 	u16 mod_abs, rx_alarm_status;
9448 	u32 val = REG_RD(bp, params->shmem_base +
9449 			     offsetof(struct shmem_region, dev_info.
9450 				      port_feature_config[params->port].
9451 				      config));
9452 	bnx2x_cl45_read(bp, phy,
9453 			MDIO_PMA_DEVAD,
9454 			MDIO_PMA_REG_PHY_IDENTIFIER, &mod_abs);
9455 	if (mod_abs & (1<<8)) {
9456 
9457 		/* Module is absent */
9458 		DP(NETIF_MSG_LINK,
9459 		   "MOD_ABS indication show module is absent\n");
9460 		phy->media_type = ETH_PHY_NOT_PRESENT;
9461 		/* 1. Set mod_abs to detect next module
9462 		 *    presence event
9463 		 * 2. Set EDC off by setting OPTXLOS signal input to low
9464 		 *    (bit 9).
9465 		 *    When the EDC is off it locks onto a reference clock and
9466 		 *    avoids becoming 'lost'.
9467 		 */
9468 		mod_abs &= ~(1<<8);
9469 		if (!(phy->flags & FLAGS_NOC))
9470 			mod_abs &= ~(1<<9);
9471 		bnx2x_cl45_write(bp, phy,
9472 				 MDIO_PMA_DEVAD,
9473 				 MDIO_PMA_REG_PHY_IDENTIFIER, mod_abs);
9474 
9475 		/* Clear RX alarm since it stays up as long as
9476 		 * the mod_abs wasn't changed
9477 		 */
9478 		bnx2x_cl45_read(bp, phy,
9479 				MDIO_PMA_DEVAD,
9480 				MDIO_PMA_LASI_RXSTAT, &rx_alarm_status);
9481 
9482 	} else {
9483 		/* Module is present */
9484 		DP(NETIF_MSG_LINK,
9485 		   "MOD_ABS indication show module is present\n");
9486 		/* First disable transmitter, and if the module is ok, the
9487 		 * module_detection will enable it
9488 		 * 1. Set mod_abs to detect next module absent event ( bit 8)
9489 		 * 2. Restore the default polarity of the OPRXLOS signal and
9490 		 * this signal will then correctly indicate the presence or
9491 		 * absence of the Rx signal. (bit 9)
9492 		 */
9493 		mod_abs |= (1<<8);
9494 		if (!(phy->flags & FLAGS_NOC))
9495 			mod_abs |= (1<<9);
9496 		bnx2x_cl45_write(bp, phy,
9497 				 MDIO_PMA_DEVAD,
9498 				 MDIO_PMA_REG_PHY_IDENTIFIER, mod_abs);
9499 
9500 		/* Clear RX alarm since it stays up as long as the mod_abs
9501 		 * wasn't changed. This is need to be done before calling the
9502 		 * module detection, otherwise it will clear* the link update
9503 		 * alarm
9504 		 */
9505 		bnx2x_cl45_read(bp, phy,
9506 				MDIO_PMA_DEVAD,
9507 				MDIO_PMA_LASI_RXSTAT, &rx_alarm_status);
9508 
9509 
9510 		if ((val & PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_MASK) ==
9511 		    PORT_FEAT_CFG_OPT_MDL_ENFRCMNT_DISABLE_TX_LASER)
9512 			bnx2x_sfp_set_transmitter(params, phy, 0);
9513 
9514 		if (bnx2x_wait_for_sfp_module_initialized(phy, params) == 0)
9515 			bnx2x_sfp_module_detection(phy, params);
9516 		else
9517 			DP(NETIF_MSG_LINK, "SFP+ module is not initialized\n");
9518 
9519 		/* Reconfigure link speed based on module type limitations */
9520 		bnx2x_8727_config_speed(phy, params);
9521 	}
9522 
9523 	DP(NETIF_MSG_LINK, "8727 RX_ALARM_STATUS 0x%x\n",
9524 		   rx_alarm_status);
9525 	/* No need to check link status in case of module plugged in/out */
9526 }
9527 
9528 static u8 bnx2x_8727_read_status(struct bnx2x_phy *phy,
9529 				 struct link_params *params,
9530 				 struct link_vars *vars)
9531 
9532 {
9533 	struct bnx2x *bp = params->bp;
9534 	u8 link_up = 0, oc_port = params->port;
9535 	u16 link_status = 0;
9536 	u16 rx_alarm_status, lasi_ctrl, val1;
9537 
9538 	/* If PHY is not initialized, do not check link status */
9539 	bnx2x_cl45_read(bp, phy,
9540 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL,
9541 			&lasi_ctrl);
9542 	if (!lasi_ctrl)
9543 		return 0;
9544 
9545 	/* Check the LASI on Rx */
9546 	bnx2x_cl45_read(bp, phy,
9547 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXSTAT,
9548 			&rx_alarm_status);
9549 	vars->line_speed = 0;
9550 	DP(NETIF_MSG_LINK, "8727 RX_ALARM_STATUS  0x%x\n", rx_alarm_status);
9551 
9552 	bnx2x_sfp_mask_fault(bp, phy, MDIO_PMA_LASI_TXSTAT,
9553 			     MDIO_PMA_LASI_TXCTRL);
9554 
9555 	bnx2x_cl45_read(bp, phy,
9556 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_STAT, &val1);
9557 
9558 	DP(NETIF_MSG_LINK, "8727 LASI status 0x%x\n", val1);
9559 
9560 	/* Clear MSG-OUT */
9561 	bnx2x_cl45_read(bp, phy,
9562 			MDIO_PMA_DEVAD, MDIO_PMA_REG_M8051_MSGOUT_REG, &val1);
9563 
9564 	/* If a module is present and there is need to check
9565 	 * for over current
9566 	 */
9567 	if (!(phy->flags & FLAGS_NOC) && !(rx_alarm_status & (1<<5))) {
9568 		/* Check over-current using 8727 GPIO0 input*/
9569 		bnx2x_cl45_read(bp, phy,
9570 				MDIO_PMA_DEVAD, MDIO_PMA_REG_8727_GPIO_CTRL,
9571 				&val1);
9572 
9573 		if ((val1 & (1<<8)) == 0) {
9574 			if (!CHIP_IS_E1x(bp))
9575 				oc_port = BP_PATH(bp) + (params->port << 1);
9576 			DP(NETIF_MSG_LINK,
9577 			   "8727 Power fault has been detected on port %d\n",
9578 			   oc_port);
9579 			netdev_err(bp->dev, "Error: Power fault on Port %d has "
9580 					    "been detected and the power to "
9581 					    "that SFP+ module has been removed "
9582 					    "to prevent failure of the card. "
9583 					    "Please remove the SFP+ module and "
9584 					    "restart the system to clear this "
9585 					    "error.\n",
9586 			 oc_port);
9587 			/* Disable all RX_ALARMs except for mod_abs */
9588 			bnx2x_cl45_write(bp, phy,
9589 					 MDIO_PMA_DEVAD,
9590 					 MDIO_PMA_LASI_RXCTRL, (1<<5));
9591 
9592 			bnx2x_cl45_read(bp, phy,
9593 					MDIO_PMA_DEVAD,
9594 					MDIO_PMA_REG_PHY_IDENTIFIER, &val1);
9595 			/* Wait for module_absent_event */
9596 			val1 |= (1<<8);
9597 			bnx2x_cl45_write(bp, phy,
9598 					 MDIO_PMA_DEVAD,
9599 					 MDIO_PMA_REG_PHY_IDENTIFIER, val1);
9600 			/* Clear RX alarm */
9601 			bnx2x_cl45_read(bp, phy,
9602 				MDIO_PMA_DEVAD,
9603 				MDIO_PMA_LASI_RXSTAT, &rx_alarm_status);
9604 			bnx2x_8727_power_module(params->bp, phy, 0);
9605 			return 0;
9606 		}
9607 	} /* Over current check */
9608 
9609 	/* When module absent bit is set, check module */
9610 	if (rx_alarm_status & (1<<5)) {
9611 		bnx2x_8727_handle_mod_abs(phy, params);
9612 		/* Enable all mod_abs and link detection bits */
9613 		bnx2x_cl45_write(bp, phy,
9614 				 MDIO_PMA_DEVAD, MDIO_PMA_LASI_RXCTRL,
9615 				 ((1<<5) | (1<<2)));
9616 	}
9617 
9618 	if (!(phy->flags & FLAGS_SFP_NOT_APPROVED)) {
9619 		DP(NETIF_MSG_LINK, "Enabling 8727 TX laser\n");
9620 		bnx2x_sfp_set_transmitter(params, phy, 1);
9621 	} else {
9622 		DP(NETIF_MSG_LINK, "Tx is disabled\n");
9623 		return 0;
9624 	}
9625 
9626 	bnx2x_cl45_read(bp, phy,
9627 			MDIO_PMA_DEVAD,
9628 			MDIO_PMA_REG_8073_SPEED_LINK_STATUS, &link_status);
9629 
9630 	/* Bits 0..2 --> speed detected,
9631 	 * Bits 13..15--> link is down
9632 	 */
9633 	if ((link_status & (1<<2)) && (!(link_status & (1<<15)))) {
9634 		link_up = 1;
9635 		vars->line_speed = SPEED_10000;
9636 		DP(NETIF_MSG_LINK, "port %x: External link up in 10G\n",
9637 			   params->port);
9638 	} else if ((link_status & (1<<0)) && (!(link_status & (1<<13)))) {
9639 		link_up = 1;
9640 		vars->line_speed = SPEED_1000;
9641 		DP(NETIF_MSG_LINK, "port %x: External link up in 1G\n",
9642 			   params->port);
9643 	} else {
9644 		link_up = 0;
9645 		DP(NETIF_MSG_LINK, "port %x: External link is down\n",
9646 			   params->port);
9647 	}
9648 
9649 	/* Capture 10G link fault. */
9650 	if (vars->line_speed == SPEED_10000) {
9651 		bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD,
9652 			    MDIO_PMA_LASI_TXSTAT, &val1);
9653 
9654 		bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD,
9655 			    MDIO_PMA_LASI_TXSTAT, &val1);
9656 
9657 		if (val1 & (1<<0)) {
9658 			vars->fault_detected = 1;
9659 		}
9660 	}
9661 
9662 	if (link_up) {
9663 		bnx2x_ext_phy_resolve_fc(phy, params, vars);
9664 		vars->duplex = DUPLEX_FULL;
9665 		DP(NETIF_MSG_LINK, "duplex = 0x%x\n", vars->duplex);
9666 	}
9667 
9668 	if ((DUAL_MEDIA(params)) &&
9669 	    (phy->req_line_speed == SPEED_1000)) {
9670 		bnx2x_cl45_read(bp, phy,
9671 				MDIO_PMA_DEVAD,
9672 				MDIO_PMA_REG_8727_PCS_GP, &val1);
9673 		/* In case of dual-media board and 1G, power up the XAUI side,
9674 		 * otherwise power it down. For 10G it is done automatically
9675 		 */
9676 		if (link_up)
9677 			val1 &= ~(3<<10);
9678 		else
9679 			val1 |= (3<<10);
9680 		bnx2x_cl45_write(bp, phy,
9681 				 MDIO_PMA_DEVAD,
9682 				 MDIO_PMA_REG_8727_PCS_GP, val1);
9683 	}
9684 	return link_up;
9685 }
9686 
9687 static void bnx2x_8727_link_reset(struct bnx2x_phy *phy,
9688 				  struct link_params *params)
9689 {
9690 	struct bnx2x *bp = params->bp;
9691 
9692 	/* Enable/Disable PHY transmitter output */
9693 	bnx2x_set_disable_pmd_transmit(params, phy, 1);
9694 
9695 	/* Disable Transmitter */
9696 	bnx2x_sfp_set_transmitter(params, phy, 0);
9697 	/* Clear LASI */
9698 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL, 0);
9699 
9700 }
9701 
9702 /******************************************************************/
9703 /*		BCM8481/BCM84823/BCM84833 PHY SECTION	          */
9704 /******************************************************************/
9705 static int bnx2x_is_8483x_8485x(struct bnx2x_phy *phy)
9706 {
9707 	return ((phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833) ||
9708 		(phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84834) ||
9709 		(phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858));
9710 }
9711 
9712 static void bnx2x_save_848xx_spirom_version(struct bnx2x_phy *phy,
9713 					    struct bnx2x *bp,
9714 					    u8 port)
9715 {
9716 	u16 val, fw_ver2, cnt, i;
9717 	static struct bnx2x_reg_set reg_set[] = {
9718 		{MDIO_PMA_DEVAD, 0xA819, 0x0014},
9719 		{MDIO_PMA_DEVAD, 0xA81A, 0xc200},
9720 		{MDIO_PMA_DEVAD, 0xA81B, 0x0000},
9721 		{MDIO_PMA_DEVAD, 0xA81C, 0x0300},
9722 		{MDIO_PMA_DEVAD, 0xA817, 0x0009}
9723 	};
9724 	u16 fw_ver1;
9725 
9726 	if (bnx2x_is_8483x_8485x(phy)) {
9727 		bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD, 0x400f, &fw_ver1);
9728 		if (phy->type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858)
9729 			fw_ver1 &= 0xfff;
9730 		bnx2x_save_spirom_version(bp, port, fw_ver1, phy->ver_addr);
9731 	} else {
9732 		/* For 32-bit registers in 848xx, access via MDIO2ARM i/f. */
9733 		/* (1) set reg 0xc200_0014(SPI_BRIDGE_CTRL_2) to 0x03000000 */
9734 		for (i = 0; i < ARRAY_SIZE(reg_set); i++)
9735 			bnx2x_cl45_write(bp, phy, reg_set[i].devad,
9736 					 reg_set[i].reg, reg_set[i].val);
9737 
9738 		for (cnt = 0; cnt < 100; cnt++) {
9739 			bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD, 0xA818, &val);
9740 			if (val & 1)
9741 				break;
9742 			udelay(5);
9743 		}
9744 		if (cnt == 100) {
9745 			DP(NETIF_MSG_LINK, "Unable to read 848xx "
9746 					"phy fw version(1)\n");
9747 			bnx2x_save_spirom_version(bp, port, 0,
9748 						  phy->ver_addr);
9749 			return;
9750 		}
9751 
9752 
9753 		/* 2) read register 0xc200_0000 (SPI_FW_STATUS) */
9754 		bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, 0xA819, 0x0000);
9755 		bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, 0xA81A, 0xc200);
9756 		bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, 0xA817, 0x000A);
9757 		for (cnt = 0; cnt < 100; cnt++) {
9758 			bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD, 0xA818, &val);
9759 			if (val & 1)
9760 				break;
9761 			udelay(5);
9762 		}
9763 		if (cnt == 100) {
9764 			DP(NETIF_MSG_LINK, "Unable to read 848xx phy fw "
9765 					"version(2)\n");
9766 			bnx2x_save_spirom_version(bp, port, 0,
9767 						  phy->ver_addr);
9768 			return;
9769 		}
9770 
9771 		/* lower 16 bits of the register SPI_FW_STATUS */
9772 		bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD, 0xA81B, &fw_ver1);
9773 		/* upper 16 bits of register SPI_FW_STATUS */
9774 		bnx2x_cl45_read(bp, phy, MDIO_PMA_DEVAD, 0xA81C, &fw_ver2);
9775 
9776 		bnx2x_save_spirom_version(bp, port, (fw_ver2<<16) | fw_ver1,
9777 					  phy->ver_addr);
9778 	}
9779 
9780 }
9781 static void bnx2x_848xx_set_led(struct bnx2x *bp,
9782 				struct bnx2x_phy *phy)
9783 {
9784 	u16 val, led3_blink_rate, offset, i;
9785 	static struct bnx2x_reg_set reg_set[] = {
9786 		{MDIO_PMA_DEVAD, MDIO_PMA_REG_8481_LED1_MASK, 0x0080},
9787 		{MDIO_PMA_DEVAD, MDIO_PMA_REG_8481_LED2_MASK, 0x0018},
9788 		{MDIO_PMA_DEVAD, MDIO_PMA_REG_8481_LED3_MASK, 0x0006},
9789 		{MDIO_PMA_DEVAD, MDIO_PMA_REG_84823_CTL_SLOW_CLK_CNT_HIGH,
9790 			MDIO_PMA_REG_84823_BLINK_RATE_VAL_15P9HZ},
9791 		{MDIO_AN_DEVAD, 0xFFFB, 0xFFFD}
9792 	};
9793 
9794 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858) {
9795 		/* Set LED5 source */
9796 		bnx2x_cl45_write(bp, phy,
9797 				 MDIO_PMA_DEVAD,
9798 				 MDIO_PMA_REG_8481_LED5_MASK,
9799 				 0x90);
9800 		led3_blink_rate = 0x000f;
9801 	} else {
9802 		led3_blink_rate = 0x0000;
9803 	}
9804 	/* Set LED3 BLINK */
9805 	bnx2x_cl45_write(bp, phy,
9806 			 MDIO_PMA_DEVAD,
9807 			 MDIO_PMA_REG_8481_LED3_BLINK,
9808 			 led3_blink_rate);
9809 
9810 	/* PHYC_CTL_LED_CTL */
9811 	bnx2x_cl45_read(bp, phy,
9812 			MDIO_PMA_DEVAD,
9813 			MDIO_PMA_REG_8481_LINK_SIGNAL, &val);
9814 	val &= 0xFE00;
9815 	val |= 0x0092;
9816 
9817 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858)
9818 		val |= 2 << 12; /* LED5 ON based on source */
9819 
9820 	bnx2x_cl45_write(bp, phy,
9821 			 MDIO_PMA_DEVAD,
9822 			 MDIO_PMA_REG_8481_LINK_SIGNAL, val);
9823 
9824 	for (i = 0; i < ARRAY_SIZE(reg_set); i++)
9825 		bnx2x_cl45_write(bp, phy, reg_set[i].devad, reg_set[i].reg,
9826 				 reg_set[i].val);
9827 
9828 	if (bnx2x_is_8483x_8485x(phy))
9829 		offset = MDIO_PMA_REG_84833_CTL_LED_CTL_1;
9830 	else
9831 		offset = MDIO_PMA_REG_84823_CTL_LED_CTL_1;
9832 
9833 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858)
9834 		val = MDIO_PMA_REG_84858_ALLOW_GPHY_ACT |
9835 		      MDIO_PMA_REG_84823_LED3_STRETCH_EN;
9836 	else
9837 		val = MDIO_PMA_REG_84823_LED3_STRETCH_EN;
9838 
9839 	/* stretch_en for LEDs */
9840 	bnx2x_cl45_read_or_write(bp, phy,
9841 				 MDIO_PMA_DEVAD,
9842 				 offset,
9843 				 val);
9844 }
9845 
9846 static void bnx2x_848xx_specific_func(struct bnx2x_phy *phy,
9847 				      struct link_params *params,
9848 				      u32 action)
9849 {
9850 	struct bnx2x *bp = params->bp;
9851 	switch (action) {
9852 	case PHY_INIT:
9853 		if (bnx2x_is_8483x_8485x(phy)) {
9854 			/* Save spirom version */
9855 			bnx2x_save_848xx_spirom_version(phy, bp, params->port);
9856 		}
9857 		/* This phy uses the NIG latch mechanism since link indication
9858 		 * arrives through its LED4 and not via its LASI signal, so we
9859 		 * get steady signal instead of clear on read
9860 		 */
9861 		bnx2x_bits_en(bp, NIG_REG_LATCH_BC_0 + params->port*4,
9862 			      1 << NIG_LATCH_BC_ENABLE_MI_INT);
9863 
9864 		bnx2x_848xx_set_led(bp, phy);
9865 		break;
9866 	}
9867 }
9868 
9869 static int bnx2x_848xx_cmn_config_init(struct bnx2x_phy *phy,
9870 				       struct link_params *params,
9871 				       struct link_vars *vars)
9872 {
9873 	struct bnx2x *bp = params->bp;
9874 	u16 autoneg_val, an_1000_val, an_10_100_val;
9875 
9876 	bnx2x_848xx_specific_func(phy, params, PHY_INIT);
9877 	bnx2x_cl45_write(bp, phy,
9878 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 0x0000);
9879 
9880 	/* set 1000 speed advertisement */
9881 	bnx2x_cl45_read(bp, phy,
9882 			MDIO_AN_DEVAD, MDIO_AN_REG_8481_1000T_CTRL,
9883 			&an_1000_val);
9884 
9885 	bnx2x_ext_phy_set_pause(params, phy, vars);
9886 	bnx2x_cl45_read(bp, phy,
9887 			MDIO_AN_DEVAD,
9888 			MDIO_AN_REG_8481_LEGACY_AN_ADV,
9889 			&an_10_100_val);
9890 	bnx2x_cl45_read(bp, phy,
9891 			MDIO_AN_DEVAD, MDIO_AN_REG_8481_LEGACY_MII_CTRL,
9892 			&autoneg_val);
9893 	/* Disable forced speed */
9894 	autoneg_val &= ~((1<<6) | (1<<8) | (1<<9) | (1<<12) | (1<<13));
9895 	an_10_100_val &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));
9896 
9897 	if (((phy->req_line_speed == SPEED_AUTO_NEG) &&
9898 	     (phy->speed_cap_mask &
9899 	     PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) ||
9900 	    (phy->req_line_speed == SPEED_1000)) {
9901 		an_1000_val |= (1<<8);
9902 		autoneg_val |= (1<<9 | 1<<12);
9903 		if (phy->req_duplex == DUPLEX_FULL)
9904 			an_1000_val |= (1<<9);
9905 		DP(NETIF_MSG_LINK, "Advertising 1G\n");
9906 	} else
9907 		an_1000_val &= ~((1<<8) | (1<<9));
9908 
9909 	bnx2x_cl45_write(bp, phy,
9910 			 MDIO_AN_DEVAD, MDIO_AN_REG_8481_1000T_CTRL,
9911 			 an_1000_val);
9912 
9913 	/* Set 10/100 speed advertisement */
9914 	if (phy->req_line_speed == SPEED_AUTO_NEG) {
9915 		if (phy->speed_cap_mask &
9916 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL) {
9917 			/* Enable autoneg and restart autoneg for legacy speeds
9918 			 */
9919 			autoneg_val |= (1<<9 | 1<<12);
9920 			an_10_100_val |= (1<<8);
9921 			DP(NETIF_MSG_LINK, "Advertising 100M-FD\n");
9922 		}
9923 
9924 		if (phy->speed_cap_mask &
9925 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF) {
9926 			/* Enable autoneg and restart autoneg for legacy speeds
9927 			 */
9928 			autoneg_val |= (1<<9 | 1<<12);
9929 			an_10_100_val |= (1<<7);
9930 			DP(NETIF_MSG_LINK, "Advertising 100M-HD\n");
9931 		}
9932 
9933 		if ((phy->speed_cap_mask &
9934 		     PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL) &&
9935 		    (phy->supported & SUPPORTED_10baseT_Full)) {
9936 			an_10_100_val |= (1<<6);
9937 			autoneg_val |= (1<<9 | 1<<12);
9938 			DP(NETIF_MSG_LINK, "Advertising 10M-FD\n");
9939 		}
9940 
9941 		if ((phy->speed_cap_mask &
9942 		     PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF) &&
9943 		    (phy->supported & SUPPORTED_10baseT_Half)) {
9944 			an_10_100_val |= (1<<5);
9945 			autoneg_val |= (1<<9 | 1<<12);
9946 			DP(NETIF_MSG_LINK, "Advertising 10M-HD\n");
9947 		}
9948 	}
9949 
9950 	/* Only 10/100 are allowed to work in FORCE mode */
9951 	if ((phy->req_line_speed == SPEED_100) &&
9952 	    (phy->supported &
9953 	     (SUPPORTED_100baseT_Half |
9954 	      SUPPORTED_100baseT_Full))) {
9955 		autoneg_val |= (1<<13);
9956 		/* Enabled AUTO-MDIX when autoneg is disabled */
9957 		bnx2x_cl45_write(bp, phy,
9958 				 MDIO_AN_DEVAD, MDIO_AN_REG_8481_AUX_CTRL,
9959 				 (1<<15 | 1<<9 | 7<<0));
9960 		/* The PHY needs this set even for forced link. */
9961 		an_10_100_val |= (1<<8) | (1<<7);
9962 		DP(NETIF_MSG_LINK, "Setting 100M force\n");
9963 	}
9964 	if ((phy->req_line_speed == SPEED_10) &&
9965 	    (phy->supported &
9966 	     (SUPPORTED_10baseT_Half |
9967 	      SUPPORTED_10baseT_Full))) {
9968 		/* Enabled AUTO-MDIX when autoneg is disabled */
9969 		bnx2x_cl45_write(bp, phy,
9970 				 MDIO_AN_DEVAD, MDIO_AN_REG_8481_AUX_CTRL,
9971 				 (1<<15 | 1<<9 | 7<<0));
9972 		DP(NETIF_MSG_LINK, "Setting 10M force\n");
9973 	}
9974 
9975 	bnx2x_cl45_write(bp, phy,
9976 			 MDIO_AN_DEVAD, MDIO_AN_REG_8481_LEGACY_AN_ADV,
9977 			 an_10_100_val);
9978 
9979 	if (phy->req_duplex == DUPLEX_FULL)
9980 		autoneg_val |= (1<<8);
9981 
9982 	/* Always write this if this is not 84833/4.
9983 	 * For 84833/4, write it only when it's a forced speed.
9984 	 */
9985 	if (!bnx2x_is_8483x_8485x(phy) ||
9986 	    ((autoneg_val & (1<<12)) == 0))
9987 		bnx2x_cl45_write(bp, phy,
9988 			 MDIO_AN_DEVAD,
9989 			 MDIO_AN_REG_8481_LEGACY_MII_CTRL, autoneg_val);
9990 
9991 	if (((phy->req_line_speed == SPEED_AUTO_NEG) &&
9992 	    (phy->speed_cap_mask &
9993 	     PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)) ||
9994 		(phy->req_line_speed == SPEED_10000)) {
9995 			DP(NETIF_MSG_LINK, "Advertising 10G\n");
9996 			/* Restart autoneg for 10G*/
9997 
9998 			bnx2x_cl45_read_or_write(
9999 				bp, phy,
10000 				MDIO_AN_DEVAD,
10001 				MDIO_AN_REG_8481_10GBASE_T_AN_CTRL,
10002 				0x1000);
10003 			bnx2x_cl45_write(bp, phy,
10004 					 MDIO_AN_DEVAD, MDIO_AN_REG_CTRL,
10005 					 0x3200);
10006 	} else
10007 		bnx2x_cl45_write(bp, phy,
10008 				 MDIO_AN_DEVAD,
10009 				 MDIO_AN_REG_8481_10GBASE_T_AN_CTRL,
10010 				 1);
10011 
10012 	return 0;
10013 }
10014 
10015 static int bnx2x_8481_config_init(struct bnx2x_phy *phy,
10016 				  struct link_params *params,
10017 				  struct link_vars *vars)
10018 {
10019 	struct bnx2x *bp = params->bp;
10020 	/* Restore normal power mode*/
10021 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
10022 		       MISC_REGISTERS_GPIO_OUTPUT_HIGH, params->port);
10023 
10024 	/* HW reset */
10025 	bnx2x_ext_phy_hw_reset(bp, params->port);
10026 	bnx2x_wait_reset_complete(bp, phy, params);
10027 
10028 	bnx2x_cl45_write(bp, phy, MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 1<<15);
10029 	return bnx2x_848xx_cmn_config_init(phy, params, vars);
10030 }
10031 
10032 #define PHY848xx_CMDHDLR_WAIT 300
10033 #define PHY848xx_CMDHDLR_MAX_ARGS 5
10034 
10035 static int bnx2x_84858_cmd_hdlr(struct bnx2x_phy *phy,
10036 				struct link_params *params,
10037 				u16 fw_cmd,
10038 				u16 cmd_args[], int argc)
10039 {
10040 	int idx;
10041 	u16 val;
10042 	struct bnx2x *bp = params->bp;
10043 
10044 	/* Step 1: Poll the STATUS register to see whether the previous command
10045 	 * is in progress or the system is busy (CMD_IN_PROGRESS or
10046 	 * SYSTEM_BUSY). If previous command is in progress or system is busy,
10047 	 * check again until the previous command finishes execution and the
10048 	 * system is available for taking command
10049 	 */
10050 
10051 	for (idx = 0; idx < PHY848xx_CMDHDLR_WAIT; idx++) {
10052 		bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10053 				MDIO_848xx_CMD_HDLR_STATUS, &val);
10054 		if ((val != PHY84858_STATUS_CMD_IN_PROGRESS) &&
10055 		    (val != PHY84858_STATUS_CMD_SYSTEM_BUSY))
10056 			break;
10057 		usleep_range(1000, 2000);
10058 	}
10059 	if (idx >= PHY848xx_CMDHDLR_WAIT) {
10060 		DP(NETIF_MSG_LINK, "FW cmd: FW not ready.\n");
10061 		return -EINVAL;
10062 	}
10063 
10064 	/* Step2: If any parameters are required for the function, write them
10065 	 * to the required DATA registers
10066 	 */
10067 
10068 	for (idx = 0; idx < argc; idx++) {
10069 		bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10070 				 MDIO_848xx_CMD_HDLR_DATA1 + idx,
10071 				 cmd_args[idx]);
10072 	}
10073 
10074 	/* Step3: When the firmware is ready for commands, write the 'Command
10075 	 * code' to the CMD register
10076 	 */
10077 	bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10078 			 MDIO_848xx_CMD_HDLR_COMMAND, fw_cmd);
10079 
10080 	/* Step4: Once the command has been written, poll the STATUS register
10081 	 * to check whether the command has completed (CMD_COMPLETED_PASS/
10082 	 * CMD_FOR_CMDS or CMD_COMPLETED_ERROR).
10083 	 */
10084 
10085 	for (idx = 0; idx < PHY848xx_CMDHDLR_WAIT; idx++) {
10086 		bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10087 				MDIO_848xx_CMD_HDLR_STATUS, &val);
10088 		if ((val == PHY84858_STATUS_CMD_COMPLETE_PASS) ||
10089 		    (val == PHY84858_STATUS_CMD_COMPLETE_ERROR))
10090 			break;
10091 		usleep_range(1000, 2000);
10092 	}
10093 	if ((idx >= PHY848xx_CMDHDLR_WAIT) ||
10094 	    (val == PHY84858_STATUS_CMD_COMPLETE_ERROR)) {
10095 		DP(NETIF_MSG_LINK, "FW cmd failed.\n");
10096 		return -EINVAL;
10097 	}
10098 	/* Step5: Once the command has completed, read the specficied DATA
10099 	 * registers for any saved results for the command, if applicable
10100 	 */
10101 
10102 	/* Gather returning data */
10103 	for (idx = 0; idx < argc; idx++) {
10104 		bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10105 				MDIO_848xx_CMD_HDLR_DATA1 + idx,
10106 				&cmd_args[idx]);
10107 	}
10108 
10109 	return 0;
10110 }
10111 
10112 static int bnx2x_84833_cmd_hdlr(struct bnx2x_phy *phy,
10113 				struct link_params *params, u16 fw_cmd,
10114 				u16 cmd_args[], int argc, int process)
10115 {
10116 	int idx;
10117 	u16 val;
10118 	struct bnx2x *bp = params->bp;
10119 	int rc = 0;
10120 
10121 	if (process == PHY84833_MB_PROCESS2) {
10122 		/* Write CMD_OPEN_OVERRIDE to STATUS reg */
10123 		bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10124 				 MDIO_848xx_CMD_HDLR_STATUS,
10125 				 PHY84833_STATUS_CMD_OPEN_OVERRIDE);
10126 	}
10127 
10128 	for (idx = 0; idx < PHY848xx_CMDHDLR_WAIT; idx++) {
10129 		bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10130 				MDIO_848xx_CMD_HDLR_STATUS, &val);
10131 		if (val == PHY84833_STATUS_CMD_OPEN_FOR_CMDS)
10132 			break;
10133 		usleep_range(1000, 2000);
10134 	}
10135 	if (idx >= PHY848xx_CMDHDLR_WAIT) {
10136 		DP(NETIF_MSG_LINK, "FW cmd: FW not ready.\n");
10137 		/* if the status is CMD_COMPLETE_PASS or CMD_COMPLETE_ERROR
10138 		 * clear the status to CMD_CLEAR_COMPLETE
10139 		 */
10140 		if (val == PHY84833_STATUS_CMD_COMPLETE_PASS ||
10141 		    val == PHY84833_STATUS_CMD_COMPLETE_ERROR) {
10142 			bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10143 					 MDIO_848xx_CMD_HDLR_STATUS,
10144 					 PHY84833_STATUS_CMD_CLEAR_COMPLETE);
10145 		}
10146 		return -EINVAL;
10147 	}
10148 	if (process == PHY84833_MB_PROCESS1 ||
10149 	    process == PHY84833_MB_PROCESS2) {
10150 		/* Prepare argument(s) */
10151 		for (idx = 0; idx < argc; idx++) {
10152 			bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10153 					 MDIO_848xx_CMD_HDLR_DATA1 + idx,
10154 					 cmd_args[idx]);
10155 		}
10156 	}
10157 
10158 	bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10159 			MDIO_848xx_CMD_HDLR_COMMAND, fw_cmd);
10160 	for (idx = 0; idx < PHY848xx_CMDHDLR_WAIT; idx++) {
10161 		bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10162 				MDIO_848xx_CMD_HDLR_STATUS, &val);
10163 		if ((val == PHY84833_STATUS_CMD_COMPLETE_PASS) ||
10164 		    (val == PHY84833_STATUS_CMD_COMPLETE_ERROR))
10165 			break;
10166 		usleep_range(1000, 2000);
10167 	}
10168 	if ((idx >= PHY848xx_CMDHDLR_WAIT) ||
10169 	    (val == PHY84833_STATUS_CMD_COMPLETE_ERROR)) {
10170 		DP(NETIF_MSG_LINK, "FW cmd failed.\n");
10171 		rc = -EINVAL;
10172 	}
10173 	if (process == PHY84833_MB_PROCESS3 && rc == 0) {
10174 		/* Gather returning data */
10175 		for (idx = 0; idx < argc; idx++) {
10176 			bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10177 					MDIO_848xx_CMD_HDLR_DATA1 + idx,
10178 					&cmd_args[idx]);
10179 		}
10180 	}
10181 	if (val == PHY84833_STATUS_CMD_COMPLETE_ERROR ||
10182 	    val == PHY84833_STATUS_CMD_COMPLETE_PASS) {
10183 		bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10184 				 MDIO_848xx_CMD_HDLR_STATUS,
10185 				 PHY84833_STATUS_CMD_CLEAR_COMPLETE);
10186 	}
10187 	return rc;
10188 }
10189 
10190 static int bnx2x_848xx_cmd_hdlr(struct bnx2x_phy *phy,
10191 				struct link_params *params,
10192 				u16 fw_cmd,
10193 					   u16 cmd_args[], int argc,
10194 					   int process)
10195 {
10196 	struct bnx2x *bp = params->bp;
10197 
10198 	if ((phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858) ||
10199 	    (REG_RD(bp, params->shmem2_base +
10200 		    offsetof(struct shmem2_region,
10201 			     link_attr_sync[params->port])) &
10202 	     LINK_ATTR_84858)) {
10203 		return bnx2x_84858_cmd_hdlr(phy, params, fw_cmd, cmd_args,
10204 					    argc);
10205 	} else {
10206 		return bnx2x_84833_cmd_hdlr(phy, params, fw_cmd, cmd_args,
10207 					    argc, process);
10208 	}
10209 }
10210 
10211 static int bnx2x_848xx_pair_swap_cfg(struct bnx2x_phy *phy,
10212 				     struct link_params *params,
10213 				     struct link_vars *vars)
10214 {
10215 	u32 pair_swap;
10216 	u16 data[PHY848xx_CMDHDLR_MAX_ARGS];
10217 	int status;
10218 	struct bnx2x *bp = params->bp;
10219 
10220 	/* Check for configuration. */
10221 	pair_swap = REG_RD(bp, params->shmem_base +
10222 			   offsetof(struct shmem_region,
10223 			dev_info.port_hw_config[params->port].xgbt_phy_cfg)) &
10224 		PORT_HW_CFG_RJ45_PAIR_SWAP_MASK;
10225 
10226 	if (pair_swap == 0)
10227 		return 0;
10228 
10229 	/* Only the second argument is used for this command */
10230 	data[1] = (u16)pair_swap;
10231 
10232 	status = bnx2x_848xx_cmd_hdlr(phy, params,
10233 				      PHY848xx_CMD_SET_PAIR_SWAP, data,
10234 				      2, PHY84833_MB_PROCESS2);
10235 	if (status == 0)
10236 		DP(NETIF_MSG_LINK, "Pairswap OK, val=0x%x\n", data[1]);
10237 
10238 	return status;
10239 }
10240 
10241 static u8 bnx2x_84833_get_reset_gpios(struct bnx2x *bp,
10242 				      u32 shmem_base_path[],
10243 				      u32 chip_id)
10244 {
10245 	u32 reset_pin[2];
10246 	u32 idx;
10247 	u8 reset_gpios;
10248 	if (CHIP_IS_E3(bp)) {
10249 		/* Assume that these will be GPIOs, not EPIOs. */
10250 		for (idx = 0; idx < 2; idx++) {
10251 			/* Map config param to register bit. */
10252 			reset_pin[idx] = REG_RD(bp, shmem_base_path[idx] +
10253 				offsetof(struct shmem_region,
10254 				dev_info.port_hw_config[0].e3_cmn_pin_cfg));
10255 			reset_pin[idx] = (reset_pin[idx] &
10256 				PORT_HW_CFG_E3_PHY_RESET_MASK) >>
10257 				PORT_HW_CFG_E3_PHY_RESET_SHIFT;
10258 			reset_pin[idx] -= PIN_CFG_GPIO0_P0;
10259 			reset_pin[idx] = (1 << reset_pin[idx]);
10260 		}
10261 		reset_gpios = (u8)(reset_pin[0] | reset_pin[1]);
10262 	} else {
10263 		/* E2, look from diff place of shmem. */
10264 		for (idx = 0; idx < 2; idx++) {
10265 			reset_pin[idx] = REG_RD(bp, shmem_base_path[idx] +
10266 				offsetof(struct shmem_region,
10267 				dev_info.port_hw_config[0].default_cfg));
10268 			reset_pin[idx] &= PORT_HW_CFG_EXT_PHY_GPIO_RST_MASK;
10269 			reset_pin[idx] -= PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO0_P0;
10270 			reset_pin[idx] >>= PORT_HW_CFG_EXT_PHY_GPIO_RST_SHIFT;
10271 			reset_pin[idx] = (1 << reset_pin[idx]);
10272 		}
10273 		reset_gpios = (u8)(reset_pin[0] | reset_pin[1]);
10274 	}
10275 
10276 	return reset_gpios;
10277 }
10278 
10279 static int bnx2x_84833_hw_reset_phy(struct bnx2x_phy *phy,
10280 				struct link_params *params)
10281 {
10282 	struct bnx2x *bp = params->bp;
10283 	u8 reset_gpios;
10284 	u32 other_shmem_base_addr = REG_RD(bp, params->shmem2_base +
10285 				offsetof(struct shmem2_region,
10286 				other_shmem_base_addr));
10287 
10288 	u32 shmem_base_path[2];
10289 
10290 	/* Work around for 84833 LED failure inside RESET status */
10291 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
10292 		MDIO_AN_REG_8481_LEGACY_MII_CTRL,
10293 		MDIO_AN_REG_8481_MII_CTRL_FORCE_1G);
10294 	bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
10295 		MDIO_AN_REG_8481_1G_100T_EXT_CTRL,
10296 		MIDO_AN_REG_8481_EXT_CTRL_FORCE_LEDS_OFF);
10297 
10298 	shmem_base_path[0] = params->shmem_base;
10299 	shmem_base_path[1] = other_shmem_base_addr;
10300 
10301 	reset_gpios = bnx2x_84833_get_reset_gpios(bp, shmem_base_path,
10302 						  params->chip_id);
10303 
10304 	bnx2x_set_mult_gpio(bp, reset_gpios, MISC_REGISTERS_GPIO_OUTPUT_LOW);
10305 	udelay(10);
10306 	DP(NETIF_MSG_LINK, "84833 hw reset on pin values 0x%x\n",
10307 		reset_gpios);
10308 
10309 	return 0;
10310 }
10311 
10312 static int bnx2x_8483x_disable_eee(struct bnx2x_phy *phy,
10313 				   struct link_params *params,
10314 				   struct link_vars *vars)
10315 {
10316 	int rc;
10317 	struct bnx2x *bp = params->bp;
10318 	u16 cmd_args = 0;
10319 
10320 	DP(NETIF_MSG_LINK, "Don't Advertise 10GBase-T EEE\n");
10321 
10322 	/* Prevent Phy from working in EEE and advertising it */
10323 	rc = bnx2x_848xx_cmd_hdlr(phy, params, PHY848xx_CMD_SET_EEE_MODE,
10324 				  &cmd_args, 1, PHY84833_MB_PROCESS1);
10325 	if (rc) {
10326 		DP(NETIF_MSG_LINK, "EEE disable failed.\n");
10327 		return rc;
10328 	}
10329 
10330 	return bnx2x_eee_disable(phy, params, vars);
10331 }
10332 
10333 static int bnx2x_8483x_enable_eee(struct bnx2x_phy *phy,
10334 				   struct link_params *params,
10335 				   struct link_vars *vars)
10336 {
10337 	int rc;
10338 	struct bnx2x *bp = params->bp;
10339 	u16 cmd_args = 1;
10340 
10341 	rc = bnx2x_848xx_cmd_hdlr(phy, params, PHY848xx_CMD_SET_EEE_MODE,
10342 				  &cmd_args, 1, PHY84833_MB_PROCESS1);
10343 	if (rc) {
10344 		DP(NETIF_MSG_LINK, "EEE enable failed.\n");
10345 		return rc;
10346 	}
10347 
10348 	return bnx2x_eee_advertise(phy, params, vars, SHMEM_EEE_10G_ADV);
10349 }
10350 
10351 #define PHY84833_CONSTANT_LATENCY 1193
10352 static int bnx2x_848x3_config_init(struct bnx2x_phy *phy,
10353 				   struct link_params *params,
10354 				   struct link_vars *vars)
10355 {
10356 	struct bnx2x *bp = params->bp;
10357 	u8 port, initialize = 1;
10358 	u16 val;
10359 	u32 actual_phy_selection;
10360 	u16 cmd_args[PHY848xx_CMDHDLR_MAX_ARGS];
10361 	int rc = 0;
10362 
10363 	usleep_range(1000, 2000);
10364 
10365 	if (!(CHIP_IS_E1x(bp)))
10366 		port = BP_PATH(bp);
10367 	else
10368 		port = params->port;
10369 
10370 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84823) {
10371 		bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_3,
10372 			       MISC_REGISTERS_GPIO_OUTPUT_HIGH,
10373 			       port);
10374 	} else {
10375 		/* MDIO reset */
10376 		bnx2x_cl45_write(bp, phy,
10377 				MDIO_PMA_DEVAD,
10378 				MDIO_PMA_REG_CTRL, 0x8000);
10379 	}
10380 
10381 	bnx2x_wait_reset_complete(bp, phy, params);
10382 
10383 	/* Wait for GPHY to come out of reset */
10384 	msleep(50);
10385 	if (!bnx2x_is_8483x_8485x(phy)) {
10386 		/* BCM84823 requires that XGXS links up first @ 10G for normal
10387 		 * behavior.
10388 		 */
10389 		u16 temp;
10390 		temp = vars->line_speed;
10391 		vars->line_speed = SPEED_10000;
10392 		bnx2x_set_autoneg(&params->phy[INT_PHY], params, vars, 0);
10393 		bnx2x_program_serdes(&params->phy[INT_PHY], params, vars);
10394 		vars->line_speed = temp;
10395 	}
10396 	/* Check if this is actually BCM84858 */
10397 	if (phy->type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858) {
10398 		u16 hw_rev;
10399 
10400 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
10401 				MDIO_AN_REG_848xx_ID_MSB, &hw_rev);
10402 		if (hw_rev == BCM84858_PHY_ID) {
10403 			params->link_attr_sync |= LINK_ATTR_84858;
10404 			bnx2x_update_link_attr(params, params->link_attr_sync);
10405 		}
10406 	}
10407 
10408 	/* Set dual-media configuration according to configuration */
10409 	bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10410 			MDIO_CTL_REG_84823_MEDIA, &val);
10411 	val &= ~(MDIO_CTL_REG_84823_MEDIA_MAC_MASK |
10412 		 MDIO_CTL_REG_84823_MEDIA_LINE_MASK |
10413 		 MDIO_CTL_REG_84823_MEDIA_COPPER_CORE_DOWN |
10414 		 MDIO_CTL_REG_84823_MEDIA_PRIORITY_MASK |
10415 		 MDIO_CTL_REG_84823_MEDIA_FIBER_1G);
10416 
10417 	if (CHIP_IS_E3(bp)) {
10418 		val &= ~(MDIO_CTL_REG_84823_MEDIA_MAC_MASK |
10419 			 MDIO_CTL_REG_84823_MEDIA_LINE_MASK);
10420 	} else {
10421 		val |= (MDIO_CTL_REG_84823_CTRL_MAC_XFI |
10422 			MDIO_CTL_REG_84823_MEDIA_LINE_XAUI_L);
10423 	}
10424 
10425 	actual_phy_selection = bnx2x_phy_selection(params);
10426 
10427 	switch (actual_phy_selection) {
10428 	case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
10429 		/* Do nothing. Essentially this is like the priority copper */
10430 		break;
10431 	case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
10432 		val |= MDIO_CTL_REG_84823_MEDIA_PRIORITY_COPPER;
10433 		break;
10434 	case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
10435 		val |= MDIO_CTL_REG_84823_MEDIA_PRIORITY_FIBER;
10436 		break;
10437 	case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
10438 		/* Do nothing here. The first PHY won't be initialized at all */
10439 		break;
10440 	case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
10441 		val |= MDIO_CTL_REG_84823_MEDIA_COPPER_CORE_DOWN;
10442 		initialize = 0;
10443 		break;
10444 	}
10445 	if (params->phy[EXT_PHY2].req_line_speed == SPEED_1000)
10446 		val |= MDIO_CTL_REG_84823_MEDIA_FIBER_1G;
10447 
10448 	bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10449 			 MDIO_CTL_REG_84823_MEDIA, val);
10450 	DP(NETIF_MSG_LINK, "Multi_phy config = 0x%x, Media control = 0x%x\n",
10451 		   params->multi_phy_config, val);
10452 
10453 	if (bnx2x_is_8483x_8485x(phy)) {
10454 		bnx2x_848xx_pair_swap_cfg(phy, params, vars);
10455 
10456 		/* Keep AutogrEEEn disabled. */
10457 		cmd_args[0] = 0x0;
10458 		cmd_args[1] = 0x0;
10459 		cmd_args[2] = PHY84833_CONSTANT_LATENCY + 1;
10460 		cmd_args[3] = PHY84833_CONSTANT_LATENCY;
10461 		rc = bnx2x_848xx_cmd_hdlr(phy, params,
10462 					  PHY848xx_CMD_SET_EEE_MODE, cmd_args,
10463 					  4, PHY84833_MB_PROCESS1);
10464 		if (rc)
10465 			DP(NETIF_MSG_LINK, "Cfg AutogrEEEn failed.\n");
10466 	}
10467 	if (initialize)
10468 		rc = bnx2x_848xx_cmn_config_init(phy, params, vars);
10469 	else
10470 		bnx2x_save_848xx_spirom_version(phy, bp, params->port);
10471 	/* 84833 PHY has a better feature and doesn't need to support this. */
10472 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84823) {
10473 		u32 cms_enable = REG_RD(bp, params->shmem_base +
10474 			offsetof(struct shmem_region,
10475 			dev_info.port_hw_config[params->port].default_cfg)) &
10476 			PORT_HW_CFG_ENABLE_CMS_MASK;
10477 
10478 		bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10479 				MDIO_CTL_REG_84823_USER_CTRL_REG, &val);
10480 		if (cms_enable)
10481 			val |= MDIO_CTL_REG_84823_USER_CTRL_CMS;
10482 		else
10483 			val &= ~MDIO_CTL_REG_84823_USER_CTRL_CMS;
10484 		bnx2x_cl45_write(bp, phy, MDIO_CTL_DEVAD,
10485 				 MDIO_CTL_REG_84823_USER_CTRL_REG, val);
10486 	}
10487 
10488 	bnx2x_cl45_read(bp, phy, MDIO_CTL_DEVAD,
10489 			MDIO_84833_TOP_CFG_FW_REV, &val);
10490 
10491 	/* Configure EEE support */
10492 	if ((val >= MDIO_84833_TOP_CFG_FW_EEE) &&
10493 	    (val != MDIO_84833_TOP_CFG_FW_NO_EEE) &&
10494 	    bnx2x_eee_has_cap(params)) {
10495 		rc = bnx2x_eee_initial_config(params, vars, SHMEM_EEE_10G_ADV);
10496 		if (rc) {
10497 			DP(NETIF_MSG_LINK, "Failed to configure EEE timers\n");
10498 			bnx2x_8483x_disable_eee(phy, params, vars);
10499 			return rc;
10500 		}
10501 
10502 		if ((phy->req_duplex == DUPLEX_FULL) &&
10503 		    (params->eee_mode & EEE_MODE_ADV_LPI) &&
10504 		    (bnx2x_eee_calc_timer(params) ||
10505 		     !(params->eee_mode & EEE_MODE_ENABLE_LPI)))
10506 			rc = bnx2x_8483x_enable_eee(phy, params, vars);
10507 		else
10508 			rc = bnx2x_8483x_disable_eee(phy, params, vars);
10509 		if (rc) {
10510 			DP(NETIF_MSG_LINK, "Failed to set EEE advertisement\n");
10511 			return rc;
10512 		}
10513 	} else {
10514 		vars->eee_status &= ~SHMEM_EEE_SUPPORTED_MASK;
10515 	}
10516 
10517 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833) {
10518 		/* Additional settings for jumbo packets in 1000BASE-T mode */
10519 		/* Allow rx extended length */
10520 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
10521 				MDIO_AN_REG_8481_AUX_CTRL, &val);
10522 		val |= 0x4000;
10523 		bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
10524 				 MDIO_AN_REG_8481_AUX_CTRL, val);
10525 		/* TX FIFO Elasticity LSB */
10526 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
10527 				MDIO_AN_REG_8481_1G_100T_EXT_CTRL, &val);
10528 		val |= 0x1;
10529 		bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
10530 				 MDIO_AN_REG_8481_1G_100T_EXT_CTRL, val);
10531 		/* TX FIFO Elasticity MSB */
10532 		/* Enable expansion register 0x46 (Pattern Generator status) */
10533 		bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
10534 				 MDIO_AN_REG_8481_EXPANSION_REG_ACCESS, 0xf46);
10535 
10536 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
10537 				MDIO_AN_REG_8481_EXPANSION_REG_RD_RW, &val);
10538 		val |= 0x4000;
10539 		bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
10540 				 MDIO_AN_REG_8481_EXPANSION_REG_RD_RW, val);
10541 	}
10542 
10543 	if (bnx2x_is_8483x_8485x(phy)) {
10544 		/* Bring PHY out of super isolate mode as the final step. */
10545 		bnx2x_cl45_read_and_write(bp, phy,
10546 					  MDIO_CTL_DEVAD,
10547 					  MDIO_84833_TOP_CFG_XGPHY_STRAP1,
10548 					  (u16)~MDIO_84833_SUPER_ISOLATE);
10549 	}
10550 	return rc;
10551 }
10552 
10553 static u8 bnx2x_848xx_read_status(struct bnx2x_phy *phy,
10554 				  struct link_params *params,
10555 				  struct link_vars *vars)
10556 {
10557 	struct bnx2x *bp = params->bp;
10558 	u16 val, val1, val2;
10559 	u8 link_up = 0;
10560 
10561 
10562 	/* Check 10G-BaseT link status */
10563 	/* Check PMD signal ok */
10564 	bnx2x_cl45_read(bp, phy,
10565 			MDIO_AN_DEVAD, 0xFFFA, &val1);
10566 	bnx2x_cl45_read(bp, phy,
10567 			MDIO_PMA_DEVAD, MDIO_PMA_REG_8481_PMD_SIGNAL,
10568 			&val2);
10569 	DP(NETIF_MSG_LINK, "BCM848xx: PMD_SIGNAL 1.a811 = 0x%x\n", val2);
10570 
10571 	/* Check link 10G */
10572 	if (val2 & (1<<11)) {
10573 		vars->line_speed = SPEED_10000;
10574 		vars->duplex = DUPLEX_FULL;
10575 		link_up = 1;
10576 		bnx2x_ext_phy_10G_an_resolve(bp, phy, vars);
10577 	} else { /* Check Legacy speed link */
10578 		u16 legacy_status, legacy_speed;
10579 
10580 		/* Enable expansion register 0x42 (Operation mode status) */
10581 		bnx2x_cl45_write(bp, phy,
10582 				 MDIO_AN_DEVAD,
10583 				 MDIO_AN_REG_8481_EXPANSION_REG_ACCESS, 0xf42);
10584 
10585 		/* Get legacy speed operation status */
10586 		bnx2x_cl45_read(bp, phy,
10587 				MDIO_AN_DEVAD,
10588 				MDIO_AN_REG_8481_EXPANSION_REG_RD_RW,
10589 				&legacy_status);
10590 
10591 		DP(NETIF_MSG_LINK, "Legacy speed status = 0x%x\n",
10592 		   legacy_status);
10593 		link_up = ((legacy_status & (1<<11)) == (1<<11));
10594 		legacy_speed = (legacy_status & (3<<9));
10595 		if (legacy_speed == (0<<9))
10596 			vars->line_speed = SPEED_10;
10597 		else if (legacy_speed == (1<<9))
10598 			vars->line_speed = SPEED_100;
10599 		else if (legacy_speed == (2<<9))
10600 			vars->line_speed = SPEED_1000;
10601 		else { /* Should not happen: Treat as link down */
10602 			vars->line_speed = 0;
10603 			link_up = 0;
10604 		}
10605 
10606 		if (link_up) {
10607 			if (legacy_status & (1<<8))
10608 				vars->duplex = DUPLEX_FULL;
10609 			else
10610 				vars->duplex = DUPLEX_HALF;
10611 
10612 			DP(NETIF_MSG_LINK,
10613 			   "Link is up in %dMbps, is_duplex_full= %d\n",
10614 			   vars->line_speed,
10615 			   (vars->duplex == DUPLEX_FULL));
10616 			/* Check legacy speed AN resolution */
10617 			bnx2x_cl45_read(bp, phy,
10618 					MDIO_AN_DEVAD,
10619 					MDIO_AN_REG_8481_LEGACY_MII_STATUS,
10620 					&val);
10621 			if (val & (1<<5))
10622 				vars->link_status |=
10623 					LINK_STATUS_AUTO_NEGOTIATE_COMPLETE;
10624 			bnx2x_cl45_read(bp, phy,
10625 					MDIO_AN_DEVAD,
10626 					MDIO_AN_REG_8481_LEGACY_AN_EXPANSION,
10627 					&val);
10628 			if ((val & (1<<0)) == 0)
10629 				vars->link_status |=
10630 					LINK_STATUS_PARALLEL_DETECTION_USED;
10631 		}
10632 	}
10633 	if (link_up) {
10634 		DP(NETIF_MSG_LINK, "BCM848x3: link speed is %d\n",
10635 			   vars->line_speed);
10636 		bnx2x_ext_phy_resolve_fc(phy, params, vars);
10637 
10638 		/* Read LP advertised speeds */
10639 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
10640 				MDIO_AN_REG_CL37_FC_LP, &val);
10641 		if (val & (1<<5))
10642 			vars->link_status |=
10643 				LINK_STATUS_LINK_PARTNER_10THD_CAPABLE;
10644 		if (val & (1<<6))
10645 			vars->link_status |=
10646 				LINK_STATUS_LINK_PARTNER_10TFD_CAPABLE;
10647 		if (val & (1<<7))
10648 			vars->link_status |=
10649 				LINK_STATUS_LINK_PARTNER_100TXHD_CAPABLE;
10650 		if (val & (1<<8))
10651 			vars->link_status |=
10652 				LINK_STATUS_LINK_PARTNER_100TXFD_CAPABLE;
10653 		if (val & (1<<9))
10654 			vars->link_status |=
10655 				LINK_STATUS_LINK_PARTNER_100T4_CAPABLE;
10656 
10657 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
10658 				MDIO_AN_REG_1000T_STATUS, &val);
10659 
10660 		if (val & (1<<10))
10661 			vars->link_status |=
10662 				LINK_STATUS_LINK_PARTNER_1000THD_CAPABLE;
10663 		if (val & (1<<11))
10664 			vars->link_status |=
10665 				LINK_STATUS_LINK_PARTNER_1000TFD_CAPABLE;
10666 
10667 		bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
10668 				MDIO_AN_REG_MASTER_STATUS, &val);
10669 
10670 		if (val & (1<<11))
10671 			vars->link_status |=
10672 				LINK_STATUS_LINK_PARTNER_10GXFD_CAPABLE;
10673 
10674 		/* Determine if EEE was negotiated */
10675 		if (bnx2x_is_8483x_8485x(phy))
10676 			bnx2x_eee_an_resolve(phy, params, vars);
10677 	}
10678 
10679 	return link_up;
10680 }
10681 
10682 static int bnx2x_8485x_format_ver(u32 raw_ver, u8 *str, u16 *len)
10683 {
10684 	int status = 0;
10685 	u32 num;
10686 
10687 	num = ((raw_ver & 0xF80) >> 7) << 16 | ((raw_ver & 0x7F) << 8) |
10688 	      ((raw_ver & 0xF000) >> 12);
10689 	status = bnx2x_3_seq_format_ver(num, str, len);
10690 	return status;
10691 }
10692 
10693 static int bnx2x_848xx_format_ver(u32 raw_ver, u8 *str, u16 *len)
10694 {
10695 	int status = 0;
10696 	u32 spirom_ver;
10697 	spirom_ver = ((raw_ver & 0xF80) >> 7) << 16 | (raw_ver & 0x7F);
10698 	status = bnx2x_format_ver(spirom_ver, str, len);
10699 	return status;
10700 }
10701 
10702 static void bnx2x_8481_hw_reset(struct bnx2x_phy *phy,
10703 				struct link_params *params)
10704 {
10705 	bnx2x_set_gpio(params->bp, MISC_REGISTERS_GPIO_1,
10706 		       MISC_REGISTERS_GPIO_OUTPUT_LOW, 0);
10707 	bnx2x_set_gpio(params->bp, MISC_REGISTERS_GPIO_1,
10708 		       MISC_REGISTERS_GPIO_OUTPUT_LOW, 1);
10709 }
10710 
10711 static void bnx2x_8481_link_reset(struct bnx2x_phy *phy,
10712 					struct link_params *params)
10713 {
10714 	bnx2x_cl45_write(params->bp, phy,
10715 			 MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, 0x0000);
10716 	bnx2x_cl45_write(params->bp, phy,
10717 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 1);
10718 }
10719 
10720 static void bnx2x_848x3_link_reset(struct bnx2x_phy *phy,
10721 				   struct link_params *params)
10722 {
10723 	struct bnx2x *bp = params->bp;
10724 	u8 port;
10725 	u16 val16;
10726 
10727 	if (!(CHIP_IS_E1x(bp)))
10728 		port = BP_PATH(bp);
10729 	else
10730 		port = params->port;
10731 
10732 	if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84823) {
10733 		bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_3,
10734 			       MISC_REGISTERS_GPIO_OUTPUT_LOW,
10735 			       port);
10736 	} else {
10737 		bnx2x_cl45_read(bp, phy,
10738 				MDIO_CTL_DEVAD,
10739 				MDIO_84833_TOP_CFG_XGPHY_STRAP1, &val16);
10740 		val16 |= MDIO_84833_SUPER_ISOLATE;
10741 		bnx2x_cl45_write(bp, phy,
10742 				 MDIO_CTL_DEVAD,
10743 				 MDIO_84833_TOP_CFG_XGPHY_STRAP1, val16);
10744 	}
10745 }
10746 
10747 static void bnx2x_848xx_set_link_led(struct bnx2x_phy *phy,
10748 				     struct link_params *params, u8 mode)
10749 {
10750 	struct bnx2x *bp = params->bp;
10751 	u16 val;
10752 	u8 port;
10753 
10754 	if (!(CHIP_IS_E1x(bp)))
10755 		port = BP_PATH(bp);
10756 	else
10757 		port = params->port;
10758 
10759 	switch (mode) {
10760 	case LED_MODE_OFF:
10761 
10762 		DP(NETIF_MSG_LINK, "Port 0x%x: LED MODE OFF\n", port);
10763 
10764 		if ((params->hw_led_mode << SHARED_HW_CFG_LED_MODE_SHIFT) ==
10765 		    SHARED_HW_CFG_LED_EXTPHY1) {
10766 
10767 			/* Set LED masks */
10768 			bnx2x_cl45_write(bp, phy,
10769 					MDIO_PMA_DEVAD,
10770 					MDIO_PMA_REG_8481_LED1_MASK,
10771 					0x0);
10772 
10773 			bnx2x_cl45_write(bp, phy,
10774 					MDIO_PMA_DEVAD,
10775 					MDIO_PMA_REG_8481_LED2_MASK,
10776 					0x0);
10777 
10778 			bnx2x_cl45_write(bp, phy,
10779 					MDIO_PMA_DEVAD,
10780 					MDIO_PMA_REG_8481_LED3_MASK,
10781 					0x0);
10782 
10783 			bnx2x_cl45_write(bp, phy,
10784 					MDIO_PMA_DEVAD,
10785 					MDIO_PMA_REG_8481_LED5_MASK,
10786 					0x0);
10787 
10788 		} else {
10789 			/* LED 1 OFF */
10790 			bnx2x_cl45_write(bp, phy,
10791 					 MDIO_PMA_DEVAD,
10792 					 MDIO_PMA_REG_8481_LED1_MASK,
10793 					 0x0);
10794 
10795 			if (phy->type ==
10796 				PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858) {
10797 				/* LED 2 OFF */
10798 				bnx2x_cl45_write(bp, phy,
10799 						 MDIO_PMA_DEVAD,
10800 						 MDIO_PMA_REG_8481_LED2_MASK,
10801 						 0x0);
10802 				/* LED 3 OFF */
10803 				bnx2x_cl45_write(bp, phy,
10804 						 MDIO_PMA_DEVAD,
10805 						 MDIO_PMA_REG_8481_LED3_MASK,
10806 						 0x0);
10807 			}
10808 		}
10809 		break;
10810 	case LED_MODE_FRONT_PANEL_OFF:
10811 
10812 		DP(NETIF_MSG_LINK, "Port 0x%x: LED MODE FRONT PANEL OFF\n",
10813 		   port);
10814 
10815 		if ((params->hw_led_mode << SHARED_HW_CFG_LED_MODE_SHIFT) ==
10816 		    SHARED_HW_CFG_LED_EXTPHY1) {
10817 
10818 			/* Set LED masks */
10819 			bnx2x_cl45_write(bp, phy,
10820 					 MDIO_PMA_DEVAD,
10821 					 MDIO_PMA_REG_8481_LED1_MASK,
10822 					 0x0);
10823 
10824 			bnx2x_cl45_write(bp, phy,
10825 					 MDIO_PMA_DEVAD,
10826 					 MDIO_PMA_REG_8481_LED2_MASK,
10827 					 0x0);
10828 
10829 			bnx2x_cl45_write(bp, phy,
10830 					 MDIO_PMA_DEVAD,
10831 					 MDIO_PMA_REG_8481_LED3_MASK,
10832 					 0x0);
10833 
10834 			bnx2x_cl45_write(bp, phy,
10835 					 MDIO_PMA_DEVAD,
10836 					 MDIO_PMA_REG_8481_LED5_MASK,
10837 					 0x20);
10838 
10839 		} else {
10840 			bnx2x_cl45_write(bp, phy,
10841 					 MDIO_PMA_DEVAD,
10842 					 MDIO_PMA_REG_8481_LED1_MASK,
10843 					 0x0);
10844 			if (phy->type ==
10845 			    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84834) {
10846 				/* Disable MI_INT interrupt before setting LED4
10847 				 * source to constant off.
10848 				 */
10849 				if (REG_RD(bp, NIG_REG_MASK_INTERRUPT_PORT0 +
10850 					   params->port*4) &
10851 				    NIG_MASK_MI_INT) {
10852 					params->link_flags |=
10853 					LINK_FLAGS_INT_DISABLED;
10854 
10855 					bnx2x_bits_dis(
10856 						bp,
10857 						NIG_REG_MASK_INTERRUPT_PORT0 +
10858 						params->port*4,
10859 						NIG_MASK_MI_INT);
10860 				}
10861 				bnx2x_cl45_write(bp, phy,
10862 						 MDIO_PMA_DEVAD,
10863 						 MDIO_PMA_REG_8481_SIGNAL_MASK,
10864 						 0x0);
10865 			}
10866 			if (phy->type ==
10867 				PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858) {
10868 				/* LED 2 OFF */
10869 				bnx2x_cl45_write(bp, phy,
10870 						 MDIO_PMA_DEVAD,
10871 						 MDIO_PMA_REG_8481_LED2_MASK,
10872 						 0x0);
10873 				/* LED 3 OFF */
10874 				bnx2x_cl45_write(bp, phy,
10875 						 MDIO_PMA_DEVAD,
10876 						 MDIO_PMA_REG_8481_LED3_MASK,
10877 						 0x0);
10878 			}
10879 		}
10880 		break;
10881 	case LED_MODE_ON:
10882 
10883 		DP(NETIF_MSG_LINK, "Port 0x%x: LED MODE ON\n", port);
10884 
10885 		if ((params->hw_led_mode << SHARED_HW_CFG_LED_MODE_SHIFT) ==
10886 		    SHARED_HW_CFG_LED_EXTPHY1) {
10887 			/* Set control reg */
10888 			bnx2x_cl45_read(bp, phy,
10889 					MDIO_PMA_DEVAD,
10890 					MDIO_PMA_REG_8481_LINK_SIGNAL,
10891 					&val);
10892 			val &= 0x8000;
10893 			val |= 0x2492;
10894 
10895 			bnx2x_cl45_write(bp, phy,
10896 					 MDIO_PMA_DEVAD,
10897 					 MDIO_PMA_REG_8481_LINK_SIGNAL,
10898 					 val);
10899 
10900 			/* Set LED masks */
10901 			bnx2x_cl45_write(bp, phy,
10902 					 MDIO_PMA_DEVAD,
10903 					 MDIO_PMA_REG_8481_LED1_MASK,
10904 					 0x0);
10905 
10906 			bnx2x_cl45_write(bp, phy,
10907 					 MDIO_PMA_DEVAD,
10908 					 MDIO_PMA_REG_8481_LED2_MASK,
10909 					 0x20);
10910 
10911 			bnx2x_cl45_write(bp, phy,
10912 					 MDIO_PMA_DEVAD,
10913 					 MDIO_PMA_REG_8481_LED3_MASK,
10914 					 0x20);
10915 
10916 			bnx2x_cl45_write(bp, phy,
10917 					 MDIO_PMA_DEVAD,
10918 					 MDIO_PMA_REG_8481_LED5_MASK,
10919 					 0x0);
10920 		} else {
10921 			bnx2x_cl45_write(bp, phy,
10922 					 MDIO_PMA_DEVAD,
10923 					 MDIO_PMA_REG_8481_LED1_MASK,
10924 					 0x20);
10925 			if (phy->type ==
10926 			    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84834) {
10927 				/* Disable MI_INT interrupt before setting LED4
10928 				 * source to constant on.
10929 				 */
10930 				if (REG_RD(bp, NIG_REG_MASK_INTERRUPT_PORT0 +
10931 					   params->port*4) &
10932 				    NIG_MASK_MI_INT) {
10933 					params->link_flags |=
10934 					LINK_FLAGS_INT_DISABLED;
10935 
10936 					bnx2x_bits_dis(
10937 						bp,
10938 						NIG_REG_MASK_INTERRUPT_PORT0 +
10939 						params->port*4,
10940 						NIG_MASK_MI_INT);
10941 				}
10942 			}
10943 			if (phy->type ==
10944 			    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858) {
10945 				/* Tell LED3 to constant on */
10946 				bnx2x_cl45_read(bp, phy,
10947 						MDIO_PMA_DEVAD,
10948 						MDIO_PMA_REG_8481_LINK_SIGNAL,
10949 						&val);
10950 				val &= ~(7<<6);
10951 				val |= (2<<6);  /* A83B[8:6]= 2 */
10952 				bnx2x_cl45_write(bp, phy,
10953 						 MDIO_PMA_DEVAD,
10954 						 MDIO_PMA_REG_8481_LINK_SIGNAL,
10955 						 val);
10956 				bnx2x_cl45_write(bp, phy,
10957 						 MDIO_PMA_DEVAD,
10958 						 MDIO_PMA_REG_8481_LED3_MASK,
10959 						 0x20);
10960 			} else {
10961 				bnx2x_cl45_write(bp, phy,
10962 						 MDIO_PMA_DEVAD,
10963 						 MDIO_PMA_REG_8481_SIGNAL_MASK,
10964 						 0x20);
10965 			}
10966 		}
10967 		break;
10968 
10969 	case LED_MODE_OPER:
10970 
10971 		DP(NETIF_MSG_LINK, "Port 0x%x: LED MODE OPER\n", port);
10972 
10973 		if ((params->hw_led_mode << SHARED_HW_CFG_LED_MODE_SHIFT) ==
10974 		    SHARED_HW_CFG_LED_EXTPHY1) {
10975 
10976 			/* Set control reg */
10977 			bnx2x_cl45_read(bp, phy,
10978 					MDIO_PMA_DEVAD,
10979 					MDIO_PMA_REG_8481_LINK_SIGNAL,
10980 					&val);
10981 
10982 			if (!((val &
10983 			       MDIO_PMA_REG_8481_LINK_SIGNAL_LED4_ENABLE_MASK)
10984 			  >> MDIO_PMA_REG_8481_LINK_SIGNAL_LED4_ENABLE_SHIFT)) {
10985 				DP(NETIF_MSG_LINK, "Setting LINK_SIGNAL\n");
10986 				bnx2x_cl45_write(bp, phy,
10987 						 MDIO_PMA_DEVAD,
10988 						 MDIO_PMA_REG_8481_LINK_SIGNAL,
10989 						 0xa492);
10990 			}
10991 
10992 			/* Set LED masks */
10993 			bnx2x_cl45_write(bp, phy,
10994 					 MDIO_PMA_DEVAD,
10995 					 MDIO_PMA_REG_8481_LED1_MASK,
10996 					 0x10);
10997 
10998 			bnx2x_cl45_write(bp, phy,
10999 					 MDIO_PMA_DEVAD,
11000 					 MDIO_PMA_REG_8481_LED2_MASK,
11001 					 0x80);
11002 
11003 			bnx2x_cl45_write(bp, phy,
11004 					 MDIO_PMA_DEVAD,
11005 					 MDIO_PMA_REG_8481_LED3_MASK,
11006 					 0x98);
11007 
11008 			bnx2x_cl45_write(bp, phy,
11009 					 MDIO_PMA_DEVAD,
11010 					 MDIO_PMA_REG_8481_LED5_MASK,
11011 					 0x40);
11012 
11013 		} else {
11014 			/* EXTPHY2 LED mode indicate that the 100M/1G/10G LED
11015 			 * sources are all wired through LED1, rather than only
11016 			 * 10G in other modes.
11017 			 */
11018 			val = ((params->hw_led_mode <<
11019 				SHARED_HW_CFG_LED_MODE_SHIFT) ==
11020 			       SHARED_HW_CFG_LED_EXTPHY2) ? 0x98 : 0x80;
11021 
11022 			bnx2x_cl45_write(bp, phy,
11023 					 MDIO_PMA_DEVAD,
11024 					 MDIO_PMA_REG_8481_LED1_MASK,
11025 					 val);
11026 
11027 			/* Tell LED3 to blink on source */
11028 			bnx2x_cl45_read(bp, phy,
11029 					MDIO_PMA_DEVAD,
11030 					MDIO_PMA_REG_8481_LINK_SIGNAL,
11031 					&val);
11032 			val &= ~(7<<6);
11033 			val |= (1<<6); /* A83B[8:6]= 1 */
11034 			bnx2x_cl45_write(bp, phy,
11035 					 MDIO_PMA_DEVAD,
11036 					 MDIO_PMA_REG_8481_LINK_SIGNAL,
11037 					 val);
11038 			if (phy->type ==
11039 			    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858) {
11040 				bnx2x_cl45_write(bp, phy,
11041 						 MDIO_PMA_DEVAD,
11042 						 MDIO_PMA_REG_8481_LED2_MASK,
11043 						 0x18);
11044 				bnx2x_cl45_write(bp, phy,
11045 						 MDIO_PMA_DEVAD,
11046 						 MDIO_PMA_REG_8481_LED3_MASK,
11047 						 0x06);
11048 			}
11049 			if (phy->type ==
11050 			    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84834) {
11051 				/* Restore LED4 source to external link,
11052 				 * and re-enable interrupts.
11053 				 */
11054 				bnx2x_cl45_write(bp, phy,
11055 						 MDIO_PMA_DEVAD,
11056 						 MDIO_PMA_REG_8481_SIGNAL_MASK,
11057 						 0x40);
11058 				if (params->link_flags &
11059 				    LINK_FLAGS_INT_DISABLED) {
11060 					bnx2x_link_int_enable(params);
11061 					params->link_flags &=
11062 						~LINK_FLAGS_INT_DISABLED;
11063 				}
11064 			}
11065 		}
11066 		break;
11067 	}
11068 
11069 	/* This is a workaround for E3+84833 until autoneg
11070 	 * restart is fixed in f/w
11071 	 */
11072 	if (CHIP_IS_E3(bp)) {
11073 		bnx2x_cl45_read(bp, phy, MDIO_WC_DEVAD,
11074 				MDIO_WC_REG_GP2_STATUS_GP_2_1, &val);
11075 	}
11076 }
11077 
11078 /******************************************************************/
11079 /*			54618SE PHY SECTION			  */
11080 /******************************************************************/
11081 static void bnx2x_54618se_specific_func(struct bnx2x_phy *phy,
11082 					struct link_params *params,
11083 					u32 action)
11084 {
11085 	struct bnx2x *bp = params->bp;
11086 	u16 temp;
11087 	switch (action) {
11088 	case PHY_INIT:
11089 		/* Configure LED4: set to INTR (0x6). */
11090 		/* Accessing shadow register 0xe. */
11091 		bnx2x_cl22_write(bp, phy,
11092 				 MDIO_REG_GPHY_SHADOW,
11093 				 MDIO_REG_GPHY_SHADOW_LED_SEL2);
11094 		bnx2x_cl22_read(bp, phy,
11095 				MDIO_REG_GPHY_SHADOW,
11096 				&temp);
11097 		temp &= ~(0xf << 4);
11098 		temp |= (0x6 << 4);
11099 		bnx2x_cl22_write(bp, phy,
11100 				 MDIO_REG_GPHY_SHADOW,
11101 				 MDIO_REG_GPHY_SHADOW_WR_ENA | temp);
11102 		/* Configure INTR based on link status change. */
11103 		bnx2x_cl22_write(bp, phy,
11104 				 MDIO_REG_INTR_MASK,
11105 				 ~MDIO_REG_INTR_MASK_LINK_STATUS);
11106 		break;
11107 	}
11108 }
11109 
11110 static int bnx2x_54618se_config_init(struct bnx2x_phy *phy,
11111 					       struct link_params *params,
11112 					       struct link_vars *vars)
11113 {
11114 	struct bnx2x *bp = params->bp;
11115 	u8 port;
11116 	u16 autoneg_val, an_1000_val, an_10_100_val, fc_val, temp;
11117 	u32 cfg_pin;
11118 
11119 	DP(NETIF_MSG_LINK, "54618SE cfg init\n");
11120 	usleep_range(1000, 2000);
11121 
11122 	/* This works with E3 only, no need to check the chip
11123 	 * before determining the port.
11124 	 */
11125 	port = params->port;
11126 
11127 	cfg_pin = (REG_RD(bp, params->shmem_base +
11128 			offsetof(struct shmem_region,
11129 			dev_info.port_hw_config[port].e3_cmn_pin_cfg)) &
11130 			PORT_HW_CFG_E3_PHY_RESET_MASK) >>
11131 			PORT_HW_CFG_E3_PHY_RESET_SHIFT;
11132 
11133 	/* Drive pin high to bring the GPHY out of reset. */
11134 	bnx2x_set_cfg_pin(bp, cfg_pin, 1);
11135 
11136 	/* wait for GPHY to reset */
11137 	msleep(50);
11138 
11139 	/* reset phy */
11140 	bnx2x_cl22_write(bp, phy,
11141 			 MDIO_PMA_REG_CTRL, 0x8000);
11142 	bnx2x_wait_reset_complete(bp, phy, params);
11143 
11144 	/* Wait for GPHY to reset */
11145 	msleep(50);
11146 
11147 
11148 	bnx2x_54618se_specific_func(phy, params, PHY_INIT);
11149 	/* Flip the signal detect polarity (set 0x1c.0x1e[8]). */
11150 	bnx2x_cl22_write(bp, phy,
11151 			MDIO_REG_GPHY_SHADOW,
11152 			MDIO_REG_GPHY_SHADOW_AUTO_DET_MED);
11153 	bnx2x_cl22_read(bp, phy,
11154 			MDIO_REG_GPHY_SHADOW,
11155 			&temp);
11156 	temp |= MDIO_REG_GPHY_SHADOW_INVERT_FIB_SD;
11157 	bnx2x_cl22_write(bp, phy,
11158 			MDIO_REG_GPHY_SHADOW,
11159 			MDIO_REG_GPHY_SHADOW_WR_ENA | temp);
11160 
11161 	/* Set up fc */
11162 	/* Please refer to Table 28B-3 of 802.3ab-1999 spec. */
11163 	bnx2x_calc_ieee_aneg_adv(phy, params, &vars->ieee_fc);
11164 	fc_val = 0;
11165 	if ((vars->ieee_fc & MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC) ==
11166 			MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC)
11167 		fc_val |= MDIO_AN_REG_ADV_PAUSE_ASYMMETRIC;
11168 
11169 	if ((vars->ieee_fc & MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH) ==
11170 			MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH)
11171 		fc_val |= MDIO_AN_REG_ADV_PAUSE_PAUSE;
11172 
11173 	/* Read all advertisement */
11174 	bnx2x_cl22_read(bp, phy,
11175 			0x09,
11176 			&an_1000_val);
11177 
11178 	bnx2x_cl22_read(bp, phy,
11179 			0x04,
11180 			&an_10_100_val);
11181 
11182 	bnx2x_cl22_read(bp, phy,
11183 			MDIO_PMA_REG_CTRL,
11184 			&autoneg_val);
11185 
11186 	/* Disable forced speed */
11187 	autoneg_val &= ~((1<<6) | (1<<8) | (1<<9) | (1<<12) | (1<<13));
11188 	an_10_100_val &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8) | (1<<10) |
11189 			   (1<<11));
11190 
11191 	if (((phy->req_line_speed == SPEED_AUTO_NEG) &&
11192 	     (phy->speed_cap_mask &
11193 	      PORT_HW_CFG_SPEED_CAPABILITY_D0_1G)) ||
11194 	    (phy->req_line_speed == SPEED_1000)) {
11195 		an_1000_val |= (1<<8);
11196 		autoneg_val |= (1<<9 | 1<<12);
11197 		if (phy->req_duplex == DUPLEX_FULL)
11198 			an_1000_val |= (1<<9);
11199 		DP(NETIF_MSG_LINK, "Advertising 1G\n");
11200 	} else
11201 		an_1000_val &= ~((1<<8) | (1<<9));
11202 
11203 	bnx2x_cl22_write(bp, phy,
11204 			0x09,
11205 			an_1000_val);
11206 	bnx2x_cl22_read(bp, phy,
11207 			0x09,
11208 			&an_1000_val);
11209 
11210 	/* Advertise 10/100 link speed */
11211 	if (phy->req_line_speed == SPEED_AUTO_NEG) {
11212 		if (phy->speed_cap_mask &
11213 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF) {
11214 			an_10_100_val |= (1<<5);
11215 			autoneg_val |= (1<<9 | 1<<12);
11216 			DP(NETIF_MSG_LINK, "Advertising 10M-HD\n");
11217 		}
11218 		if (phy->speed_cap_mask &
11219 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL) {
11220 			an_10_100_val |= (1<<6);
11221 			autoneg_val |= (1<<9 | 1<<12);
11222 			DP(NETIF_MSG_LINK, "Advertising 10M-FD\n");
11223 		}
11224 		if (phy->speed_cap_mask &
11225 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF) {
11226 			an_10_100_val |= (1<<7);
11227 			autoneg_val |= (1<<9 | 1<<12);
11228 			DP(NETIF_MSG_LINK, "Advertising 100M-HD\n");
11229 		}
11230 		if (phy->speed_cap_mask &
11231 		    PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL) {
11232 			an_10_100_val |= (1<<8);
11233 			autoneg_val |= (1<<9 | 1<<12);
11234 			DP(NETIF_MSG_LINK, "Advertising 100M-FD\n");
11235 		}
11236 	}
11237 
11238 	/* Only 10/100 are allowed to work in FORCE mode */
11239 	if (phy->req_line_speed == SPEED_100) {
11240 		autoneg_val |= (1<<13);
11241 		/* Enabled AUTO-MDIX when autoneg is disabled */
11242 		bnx2x_cl22_write(bp, phy,
11243 				0x18,
11244 				(1<<15 | 1<<9 | 7<<0));
11245 		DP(NETIF_MSG_LINK, "Setting 100M force\n");
11246 	}
11247 	if (phy->req_line_speed == SPEED_10) {
11248 		/* Enabled AUTO-MDIX when autoneg is disabled */
11249 		bnx2x_cl22_write(bp, phy,
11250 				0x18,
11251 				(1<<15 | 1<<9 | 7<<0));
11252 		DP(NETIF_MSG_LINK, "Setting 10M force\n");
11253 	}
11254 
11255 	if ((phy->flags & FLAGS_EEE) && bnx2x_eee_has_cap(params)) {
11256 		int rc;
11257 
11258 		bnx2x_cl22_write(bp, phy, MDIO_REG_GPHY_EXP_ACCESS,
11259 				 MDIO_REG_GPHY_EXP_ACCESS_TOP |
11260 				 MDIO_REG_GPHY_EXP_TOP_2K_BUF);
11261 		bnx2x_cl22_read(bp, phy, MDIO_REG_GPHY_EXP_ACCESS_GATE, &temp);
11262 		temp &= 0xfffe;
11263 		bnx2x_cl22_write(bp, phy, MDIO_REG_GPHY_EXP_ACCESS_GATE, temp);
11264 
11265 		rc = bnx2x_eee_initial_config(params, vars, SHMEM_EEE_1G_ADV);
11266 		if (rc) {
11267 			DP(NETIF_MSG_LINK, "Failed to configure EEE timers\n");
11268 			bnx2x_eee_disable(phy, params, vars);
11269 		} else if ((params->eee_mode & EEE_MODE_ADV_LPI) &&
11270 			   (phy->req_duplex == DUPLEX_FULL) &&
11271 			   (bnx2x_eee_calc_timer(params) ||
11272 			    !(params->eee_mode & EEE_MODE_ENABLE_LPI))) {
11273 			/* Need to advertise EEE only when requested,
11274 			 * and either no LPI assertion was requested,
11275 			 * or it was requested and a valid timer was set.
11276 			 * Also notice full duplex is required for EEE.
11277 			 */
11278 			bnx2x_eee_advertise(phy, params, vars,
11279 					    SHMEM_EEE_1G_ADV);
11280 		} else {
11281 			DP(NETIF_MSG_LINK, "Don't Advertise 1GBase-T EEE\n");
11282 			bnx2x_eee_disable(phy, params, vars);
11283 		}
11284 	} else {
11285 		vars->eee_status &= ~SHMEM_EEE_1G_ADV <<
11286 				    SHMEM_EEE_SUPPORTED_SHIFT;
11287 
11288 		if (phy->flags & FLAGS_EEE) {
11289 			/* Handle legacy auto-grEEEn */
11290 			if (params->feature_config_flags &
11291 			    FEATURE_CONFIG_AUTOGREEEN_ENABLED) {
11292 				temp = 6;
11293 				DP(NETIF_MSG_LINK, "Enabling Auto-GrEEEn\n");
11294 			} else {
11295 				temp = 0;
11296 				DP(NETIF_MSG_LINK, "Don't Adv. EEE\n");
11297 			}
11298 			bnx2x_cl45_write(bp, phy, MDIO_AN_DEVAD,
11299 					 MDIO_AN_REG_EEE_ADV, temp);
11300 		}
11301 	}
11302 
11303 	bnx2x_cl22_write(bp, phy,
11304 			0x04,
11305 			an_10_100_val | fc_val);
11306 
11307 	if (phy->req_duplex == DUPLEX_FULL)
11308 		autoneg_val |= (1<<8);
11309 
11310 	bnx2x_cl22_write(bp, phy,
11311 			MDIO_PMA_REG_CTRL, autoneg_val);
11312 
11313 	return 0;
11314 }
11315 
11316 
11317 static void bnx2x_5461x_set_link_led(struct bnx2x_phy *phy,
11318 				       struct link_params *params, u8 mode)
11319 {
11320 	struct bnx2x *bp = params->bp;
11321 	u16 temp;
11322 
11323 	bnx2x_cl22_write(bp, phy,
11324 		MDIO_REG_GPHY_SHADOW,
11325 		MDIO_REG_GPHY_SHADOW_LED_SEL1);
11326 	bnx2x_cl22_read(bp, phy,
11327 		MDIO_REG_GPHY_SHADOW,
11328 		&temp);
11329 	temp &= 0xff00;
11330 
11331 	DP(NETIF_MSG_LINK, "54618x set link led (mode=%x)\n", mode);
11332 	switch (mode) {
11333 	case LED_MODE_FRONT_PANEL_OFF:
11334 	case LED_MODE_OFF:
11335 		temp |= 0x00ee;
11336 		break;
11337 	case LED_MODE_OPER:
11338 		temp |= 0x0001;
11339 		break;
11340 	case LED_MODE_ON:
11341 		temp |= 0x00ff;
11342 		break;
11343 	default:
11344 		break;
11345 	}
11346 	bnx2x_cl22_write(bp, phy,
11347 		MDIO_REG_GPHY_SHADOW,
11348 		MDIO_REG_GPHY_SHADOW_WR_ENA | temp);
11349 	return;
11350 }
11351 
11352 
11353 static void bnx2x_54618se_link_reset(struct bnx2x_phy *phy,
11354 				     struct link_params *params)
11355 {
11356 	struct bnx2x *bp = params->bp;
11357 	u32 cfg_pin;
11358 	u8 port;
11359 
11360 	/* In case of no EPIO routed to reset the GPHY, put it
11361 	 * in low power mode.
11362 	 */
11363 	bnx2x_cl22_write(bp, phy, MDIO_PMA_REG_CTRL, 0x800);
11364 	/* This works with E3 only, no need to check the chip
11365 	 * before determining the port.
11366 	 */
11367 	port = params->port;
11368 	cfg_pin = (REG_RD(bp, params->shmem_base +
11369 			offsetof(struct shmem_region,
11370 			dev_info.port_hw_config[port].e3_cmn_pin_cfg)) &
11371 			PORT_HW_CFG_E3_PHY_RESET_MASK) >>
11372 			PORT_HW_CFG_E3_PHY_RESET_SHIFT;
11373 
11374 	/* Drive pin low to put GPHY in reset. */
11375 	bnx2x_set_cfg_pin(bp, cfg_pin, 0);
11376 }
11377 
11378 static u8 bnx2x_54618se_read_status(struct bnx2x_phy *phy,
11379 				    struct link_params *params,
11380 				    struct link_vars *vars)
11381 {
11382 	struct bnx2x *bp = params->bp;
11383 	u16 val;
11384 	u8 link_up = 0;
11385 	u16 legacy_status, legacy_speed;
11386 
11387 	/* Get speed operation status */
11388 	bnx2x_cl22_read(bp, phy,
11389 			MDIO_REG_GPHY_AUX_STATUS,
11390 			&legacy_status);
11391 	DP(NETIF_MSG_LINK, "54618SE read_status: 0x%x\n", legacy_status);
11392 
11393 	/* Read status to clear the PHY interrupt. */
11394 	bnx2x_cl22_read(bp, phy,
11395 			MDIO_REG_INTR_STATUS,
11396 			&val);
11397 
11398 	link_up = ((legacy_status & (1<<2)) == (1<<2));
11399 
11400 	if (link_up) {
11401 		legacy_speed = (legacy_status & (7<<8));
11402 		if (legacy_speed == (7<<8)) {
11403 			vars->line_speed = SPEED_1000;
11404 			vars->duplex = DUPLEX_FULL;
11405 		} else if (legacy_speed == (6<<8)) {
11406 			vars->line_speed = SPEED_1000;
11407 			vars->duplex = DUPLEX_HALF;
11408 		} else if (legacy_speed == (5<<8)) {
11409 			vars->line_speed = SPEED_100;
11410 			vars->duplex = DUPLEX_FULL;
11411 		}
11412 		/* Omitting 100Base-T4 for now */
11413 		else if (legacy_speed == (3<<8)) {
11414 			vars->line_speed = SPEED_100;
11415 			vars->duplex = DUPLEX_HALF;
11416 		} else if (legacy_speed == (2<<8)) {
11417 			vars->line_speed = SPEED_10;
11418 			vars->duplex = DUPLEX_FULL;
11419 		} else if (legacy_speed == (1<<8)) {
11420 			vars->line_speed = SPEED_10;
11421 			vars->duplex = DUPLEX_HALF;
11422 		} else /* Should not happen */
11423 			vars->line_speed = 0;
11424 
11425 		DP(NETIF_MSG_LINK,
11426 		   "Link is up in %dMbps, is_duplex_full= %d\n",
11427 		   vars->line_speed,
11428 		   (vars->duplex == DUPLEX_FULL));
11429 
11430 		/* Check legacy speed AN resolution */
11431 		bnx2x_cl22_read(bp, phy,
11432 				0x01,
11433 				&val);
11434 		if (val & (1<<5))
11435 			vars->link_status |=
11436 				LINK_STATUS_AUTO_NEGOTIATE_COMPLETE;
11437 		bnx2x_cl22_read(bp, phy,
11438 				0x06,
11439 				&val);
11440 		if ((val & (1<<0)) == 0)
11441 			vars->link_status |=
11442 				LINK_STATUS_PARALLEL_DETECTION_USED;
11443 
11444 		DP(NETIF_MSG_LINK, "BCM54618SE: link speed is %d\n",
11445 			   vars->line_speed);
11446 
11447 		bnx2x_ext_phy_resolve_fc(phy, params, vars);
11448 
11449 		if (vars->link_status & LINK_STATUS_AUTO_NEGOTIATE_COMPLETE) {
11450 			/* Report LP advertised speeds */
11451 			bnx2x_cl22_read(bp, phy, 0x5, &val);
11452 
11453 			if (val & (1<<5))
11454 				vars->link_status |=
11455 				  LINK_STATUS_LINK_PARTNER_10THD_CAPABLE;
11456 			if (val & (1<<6))
11457 				vars->link_status |=
11458 				  LINK_STATUS_LINK_PARTNER_10TFD_CAPABLE;
11459 			if (val & (1<<7))
11460 				vars->link_status |=
11461 				  LINK_STATUS_LINK_PARTNER_100TXHD_CAPABLE;
11462 			if (val & (1<<8))
11463 				vars->link_status |=
11464 				  LINK_STATUS_LINK_PARTNER_100TXFD_CAPABLE;
11465 			if (val & (1<<9))
11466 				vars->link_status |=
11467 				  LINK_STATUS_LINK_PARTNER_100T4_CAPABLE;
11468 
11469 			bnx2x_cl22_read(bp, phy, 0xa, &val);
11470 			if (val & (1<<10))
11471 				vars->link_status |=
11472 				  LINK_STATUS_LINK_PARTNER_1000THD_CAPABLE;
11473 			if (val & (1<<11))
11474 				vars->link_status |=
11475 				  LINK_STATUS_LINK_PARTNER_1000TFD_CAPABLE;
11476 
11477 			if ((phy->flags & FLAGS_EEE) &&
11478 			    bnx2x_eee_has_cap(params))
11479 				bnx2x_eee_an_resolve(phy, params, vars);
11480 		}
11481 	}
11482 	return link_up;
11483 }
11484 
11485 static void bnx2x_54618se_config_loopback(struct bnx2x_phy *phy,
11486 					  struct link_params *params)
11487 {
11488 	struct bnx2x *bp = params->bp;
11489 	u16 val;
11490 	u32 umac_base = params->port ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
11491 
11492 	DP(NETIF_MSG_LINK, "2PMA/PMD ext_phy_loopback: 54618se\n");
11493 
11494 	/* Enable master/slave manual mmode and set to master */
11495 	/* mii write 9 [bits set 11 12] */
11496 	bnx2x_cl22_write(bp, phy, 0x09, 3<<11);
11497 
11498 	/* forced 1G and disable autoneg */
11499 	/* set val [mii read 0] */
11500 	/* set val [expr $val & [bits clear 6 12 13]] */
11501 	/* set val [expr $val | [bits set 6 8]] */
11502 	/* mii write 0 $val */
11503 	bnx2x_cl22_read(bp, phy, 0x00, &val);
11504 	val &= ~((1<<6) | (1<<12) | (1<<13));
11505 	val |= (1<<6) | (1<<8);
11506 	bnx2x_cl22_write(bp, phy, 0x00, val);
11507 
11508 	/* Set external loopback and Tx using 6dB coding */
11509 	/* mii write 0x18 7 */
11510 	/* set val [mii read 0x18] */
11511 	/* mii write 0x18 [expr $val | [bits set 10 15]] */
11512 	bnx2x_cl22_write(bp, phy, 0x18, 7);
11513 	bnx2x_cl22_read(bp, phy, 0x18, &val);
11514 	bnx2x_cl22_write(bp, phy, 0x18, val | (1<<10) | (1<<15));
11515 
11516 	/* This register opens the gate for the UMAC despite its name */
11517 	REG_WR(bp, NIG_REG_EGRESS_EMAC0_PORT + params->port*4, 1);
11518 
11519 	/* Maximum Frame Length (RW). Defines a 14-Bit maximum frame
11520 	 * length used by the MAC receive logic to check frames.
11521 	 */
11522 	REG_WR(bp, umac_base + UMAC_REG_MAXFR, 0x2710);
11523 }
11524 
11525 /******************************************************************/
11526 /*			SFX7101 PHY SECTION			  */
11527 /******************************************************************/
11528 static void bnx2x_7101_config_loopback(struct bnx2x_phy *phy,
11529 				       struct link_params *params)
11530 {
11531 	struct bnx2x *bp = params->bp;
11532 	/* SFX7101_XGXS_TEST1 */
11533 	bnx2x_cl45_write(bp, phy,
11534 			 MDIO_XS_DEVAD, MDIO_XS_SFX7101_XGXS_TEST1, 0x100);
11535 }
11536 
11537 static int bnx2x_7101_config_init(struct bnx2x_phy *phy,
11538 				  struct link_params *params,
11539 				  struct link_vars *vars)
11540 {
11541 	u16 fw_ver1, fw_ver2, val;
11542 	struct bnx2x *bp = params->bp;
11543 	DP(NETIF_MSG_LINK, "Setting the SFX7101 LASI indication\n");
11544 
11545 	/* Restore normal power mode*/
11546 	bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
11547 		       MISC_REGISTERS_GPIO_OUTPUT_HIGH, params->port);
11548 	/* HW reset */
11549 	bnx2x_ext_phy_hw_reset(bp, params->port);
11550 	bnx2x_wait_reset_complete(bp, phy, params);
11551 
11552 	bnx2x_cl45_write(bp, phy,
11553 			 MDIO_PMA_DEVAD, MDIO_PMA_LASI_CTRL, 0x1);
11554 	DP(NETIF_MSG_LINK, "Setting the SFX7101 LED to blink on traffic\n");
11555 	bnx2x_cl45_write(bp, phy,
11556 			 MDIO_PMA_DEVAD, MDIO_PMA_REG_7107_LED_CNTL, (1<<3));
11557 
11558 	bnx2x_ext_phy_set_pause(params, phy, vars);
11559 	/* Restart autoneg */
11560 	bnx2x_cl45_read(bp, phy,
11561 			MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, &val);
11562 	val |= 0x200;
11563 	bnx2x_cl45_write(bp, phy,
11564 			 MDIO_AN_DEVAD, MDIO_AN_REG_CTRL, val);
11565 
11566 	/* Save spirom version */
11567 	bnx2x_cl45_read(bp, phy,
11568 			MDIO_PMA_DEVAD, MDIO_PMA_REG_7101_VER1, &fw_ver1);
11569 
11570 	bnx2x_cl45_read(bp, phy,
11571 			MDIO_PMA_DEVAD, MDIO_PMA_REG_7101_VER2, &fw_ver2);
11572 	bnx2x_save_spirom_version(bp, params->port,
11573 				  (u32)(fw_ver1<<16 | fw_ver2), phy->ver_addr);
11574 	return 0;
11575 }
11576 
11577 static u8 bnx2x_7101_read_status(struct bnx2x_phy *phy,
11578 				 struct link_params *params,
11579 				 struct link_vars *vars)
11580 {
11581 	struct bnx2x *bp = params->bp;
11582 	u8 link_up;
11583 	u16 val1, val2;
11584 	bnx2x_cl45_read(bp, phy,
11585 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_STAT, &val2);
11586 	bnx2x_cl45_read(bp, phy,
11587 			MDIO_PMA_DEVAD, MDIO_PMA_LASI_STAT, &val1);
11588 	DP(NETIF_MSG_LINK, "10G-base-T LASI status 0x%x->0x%x\n",
11589 		   val2, val1);
11590 	bnx2x_cl45_read(bp, phy,
11591 			MDIO_PMA_DEVAD, MDIO_PMA_REG_STATUS, &val2);
11592 	bnx2x_cl45_read(bp, phy,
11593 			MDIO_PMA_DEVAD, MDIO_PMA_REG_STATUS, &val1);
11594 	DP(NETIF_MSG_LINK, "10G-base-T PMA status 0x%x->0x%x\n",
11595 		   val2, val1);
11596 	link_up = ((val1 & 4) == 4);
11597 	/* If link is up print the AN outcome of the SFX7101 PHY */
11598 	if (link_up) {
11599 		bnx2x_cl45_read(bp, phy,
11600 				MDIO_AN_DEVAD, MDIO_AN_REG_MASTER_STATUS,
11601 				&val2);
11602 		vars->line_speed = SPEED_10000;
11603 		vars->duplex = DUPLEX_FULL;
11604 		DP(NETIF_MSG_LINK, "SFX7101 AN status 0x%x->Master=%x\n",
11605 			   val2, (val2 & (1<<14)));
11606 		bnx2x_ext_phy_10G_an_resolve(bp, phy, vars);
11607 		bnx2x_ext_phy_resolve_fc(phy, params, vars);
11608 
11609 		/* Read LP advertised speeds */
11610 		if (val2 & (1<<11))
11611 			vars->link_status |=
11612 				LINK_STATUS_LINK_PARTNER_10GXFD_CAPABLE;
11613 	}
11614 	return link_up;
11615 }
11616 
11617 static int bnx2x_7101_format_ver(u32 spirom_ver, u8 *str, u16 *len)
11618 {
11619 	if (*len < 5)
11620 		return -EINVAL;
11621 	str[0] = (spirom_ver & 0xFF);
11622 	str[1] = (spirom_ver & 0xFF00) >> 8;
11623 	str[2] = (spirom_ver & 0xFF0000) >> 16;
11624 	str[3] = (spirom_ver & 0xFF000000) >> 24;
11625 	str[4] = '\0';
11626 	*len -= 5;
11627 	return 0;
11628 }
11629 
11630 void bnx2x_sfx7101_sp_sw_reset(struct bnx2x *bp, struct bnx2x_phy *phy)
11631 {
11632 	u16 val, cnt;
11633 
11634 	bnx2x_cl45_read(bp, phy,
11635 			MDIO_PMA_DEVAD,
11636 			MDIO_PMA_REG_7101_RESET, &val);
11637 
11638 	for (cnt = 0; cnt < 10; cnt++) {
11639 		msleep(50);
11640 		/* Writes a self-clearing reset */
11641 		bnx2x_cl45_write(bp, phy,
11642 				 MDIO_PMA_DEVAD,
11643 				 MDIO_PMA_REG_7101_RESET,
11644 				 (val | (1<<15)));
11645 		/* Wait for clear */
11646 		bnx2x_cl45_read(bp, phy,
11647 				MDIO_PMA_DEVAD,
11648 				MDIO_PMA_REG_7101_RESET, &val);
11649 
11650 		if ((val & (1<<15)) == 0)
11651 			break;
11652 	}
11653 }
11654 
11655 static void bnx2x_7101_hw_reset(struct bnx2x_phy *phy,
11656 				struct link_params *params) {
11657 	/* Low power mode is controlled by GPIO 2 */
11658 	bnx2x_set_gpio(params->bp, MISC_REGISTERS_GPIO_2,
11659 		       MISC_REGISTERS_GPIO_OUTPUT_LOW, params->port);
11660 	/* The PHY reset is controlled by GPIO 1 */
11661 	bnx2x_set_gpio(params->bp, MISC_REGISTERS_GPIO_1,
11662 		       MISC_REGISTERS_GPIO_OUTPUT_LOW, params->port);
11663 }
11664 
11665 static void bnx2x_7101_set_link_led(struct bnx2x_phy *phy,
11666 				    struct link_params *params, u8 mode)
11667 {
11668 	u16 val = 0;
11669 	struct bnx2x *bp = params->bp;
11670 	switch (mode) {
11671 	case LED_MODE_FRONT_PANEL_OFF:
11672 	case LED_MODE_OFF:
11673 		val = 2;
11674 		break;
11675 	case LED_MODE_ON:
11676 		val = 1;
11677 		break;
11678 	case LED_MODE_OPER:
11679 		val = 0;
11680 		break;
11681 	}
11682 	bnx2x_cl45_write(bp, phy,
11683 			 MDIO_PMA_DEVAD,
11684 			 MDIO_PMA_REG_7107_LINK_LED_CNTL,
11685 			 val);
11686 }
11687 
11688 /******************************************************************/
11689 /*			STATIC PHY DECLARATION			  */
11690 /******************************************************************/
11691 
11692 static const struct bnx2x_phy phy_null = {
11693 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN,
11694 	.addr		= 0,
11695 	.def_md_devad	= 0,
11696 	.flags		= FLAGS_INIT_XGXS_FIRST,
11697 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11698 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11699 	.mdio_ctrl	= 0,
11700 	.supported	= 0,
11701 	.media_type	= ETH_PHY_NOT_PRESENT,
11702 	.ver_addr	= 0,
11703 	.req_flow_ctrl	= 0,
11704 	.req_line_speed	= 0,
11705 	.speed_cap_mask	= 0,
11706 	.req_duplex	= 0,
11707 	.rsrv		= 0,
11708 	.config_init	= (config_init_t)NULL,
11709 	.read_status	= (read_status_t)NULL,
11710 	.link_reset	= (link_reset_t)NULL,
11711 	.config_loopback = (config_loopback_t)NULL,
11712 	.format_fw_ver	= (format_fw_ver_t)NULL,
11713 	.hw_reset	= (hw_reset_t)NULL,
11714 	.set_link_led	= (set_link_led_t)NULL,
11715 	.phy_specific_func = (phy_specific_func_t)NULL
11716 };
11717 
11718 static const struct bnx2x_phy phy_serdes = {
11719 	.type		= PORT_HW_CFG_SERDES_EXT_PHY_TYPE_DIRECT,
11720 	.addr		= 0xff,
11721 	.def_md_devad	= 0,
11722 	.flags		= 0,
11723 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11724 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11725 	.mdio_ctrl	= 0,
11726 	.supported	= (SUPPORTED_10baseT_Half |
11727 			   SUPPORTED_10baseT_Full |
11728 			   SUPPORTED_100baseT_Half |
11729 			   SUPPORTED_100baseT_Full |
11730 			   SUPPORTED_1000baseT_Full |
11731 			   SUPPORTED_2500baseX_Full |
11732 			   SUPPORTED_TP |
11733 			   SUPPORTED_Autoneg |
11734 			   SUPPORTED_Pause |
11735 			   SUPPORTED_Asym_Pause),
11736 	.media_type	= ETH_PHY_BASE_T,
11737 	.ver_addr	= 0,
11738 	.req_flow_ctrl	= 0,
11739 	.req_line_speed	= 0,
11740 	.speed_cap_mask	= 0,
11741 	.req_duplex	= 0,
11742 	.rsrv		= 0,
11743 	.config_init	= (config_init_t)bnx2x_xgxs_config_init,
11744 	.read_status	= (read_status_t)bnx2x_link_settings_status,
11745 	.link_reset	= (link_reset_t)bnx2x_int_link_reset,
11746 	.config_loopback = (config_loopback_t)NULL,
11747 	.format_fw_ver	= (format_fw_ver_t)NULL,
11748 	.hw_reset	= (hw_reset_t)NULL,
11749 	.set_link_led	= (set_link_led_t)NULL,
11750 	.phy_specific_func = (phy_specific_func_t)NULL
11751 };
11752 
11753 static const struct bnx2x_phy phy_xgxs = {
11754 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT,
11755 	.addr		= 0xff,
11756 	.def_md_devad	= 0,
11757 	.flags		= 0,
11758 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11759 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11760 	.mdio_ctrl	= 0,
11761 	.supported	= (SUPPORTED_10baseT_Half |
11762 			   SUPPORTED_10baseT_Full |
11763 			   SUPPORTED_100baseT_Half |
11764 			   SUPPORTED_100baseT_Full |
11765 			   SUPPORTED_1000baseT_Full |
11766 			   SUPPORTED_2500baseX_Full |
11767 			   SUPPORTED_10000baseT_Full |
11768 			   SUPPORTED_FIBRE |
11769 			   SUPPORTED_Autoneg |
11770 			   SUPPORTED_Pause |
11771 			   SUPPORTED_Asym_Pause),
11772 	.media_type	= ETH_PHY_CX4,
11773 	.ver_addr	= 0,
11774 	.req_flow_ctrl	= 0,
11775 	.req_line_speed	= 0,
11776 	.speed_cap_mask	= 0,
11777 	.req_duplex	= 0,
11778 	.rsrv		= 0,
11779 	.config_init	= (config_init_t)bnx2x_xgxs_config_init,
11780 	.read_status	= (read_status_t)bnx2x_link_settings_status,
11781 	.link_reset	= (link_reset_t)bnx2x_int_link_reset,
11782 	.config_loopback = (config_loopback_t)bnx2x_set_xgxs_loopback,
11783 	.format_fw_ver	= (format_fw_ver_t)NULL,
11784 	.hw_reset	= (hw_reset_t)NULL,
11785 	.set_link_led	= (set_link_led_t)NULL,
11786 	.phy_specific_func = (phy_specific_func_t)bnx2x_xgxs_specific_func
11787 };
11788 static const struct bnx2x_phy phy_warpcore = {
11789 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT,
11790 	.addr		= 0xff,
11791 	.def_md_devad	= 0,
11792 	.flags		= FLAGS_TX_ERROR_CHECK,
11793 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11794 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11795 	.mdio_ctrl	= 0,
11796 	.supported	= (SUPPORTED_10baseT_Half |
11797 			   SUPPORTED_10baseT_Full |
11798 			   SUPPORTED_100baseT_Half |
11799 			   SUPPORTED_100baseT_Full |
11800 			   SUPPORTED_1000baseT_Full |
11801 			   SUPPORTED_1000baseKX_Full |
11802 			   SUPPORTED_10000baseT_Full |
11803 			   SUPPORTED_10000baseKR_Full |
11804 			   SUPPORTED_20000baseKR2_Full |
11805 			   SUPPORTED_20000baseMLD2_Full |
11806 			   SUPPORTED_FIBRE |
11807 			   SUPPORTED_Autoneg |
11808 			   SUPPORTED_Pause |
11809 			   SUPPORTED_Asym_Pause),
11810 	.media_type	= ETH_PHY_UNSPECIFIED,
11811 	.ver_addr	= 0,
11812 	.req_flow_ctrl	= 0,
11813 	.req_line_speed	= 0,
11814 	.speed_cap_mask	= 0,
11815 	/* req_duplex = */0,
11816 	/* rsrv = */0,
11817 	.config_init	= (config_init_t)bnx2x_warpcore_config_init,
11818 	.read_status	= (read_status_t)bnx2x_warpcore_read_status,
11819 	.link_reset	= (link_reset_t)bnx2x_warpcore_link_reset,
11820 	.config_loopback = (config_loopback_t)bnx2x_set_warpcore_loopback,
11821 	.format_fw_ver	= (format_fw_ver_t)NULL,
11822 	.hw_reset	= (hw_reset_t)bnx2x_warpcore_hw_reset,
11823 	.set_link_led	= (set_link_led_t)NULL,
11824 	.phy_specific_func = (phy_specific_func_t)NULL
11825 };
11826 
11827 
11828 static const struct bnx2x_phy phy_7101 = {
11829 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101,
11830 	.addr		= 0xff,
11831 	.def_md_devad	= 0,
11832 	.flags		= FLAGS_FAN_FAILURE_DET_REQ,
11833 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11834 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11835 	.mdio_ctrl	= 0,
11836 	.supported	= (SUPPORTED_10000baseT_Full |
11837 			   SUPPORTED_TP |
11838 			   SUPPORTED_Autoneg |
11839 			   SUPPORTED_Pause |
11840 			   SUPPORTED_Asym_Pause),
11841 	.media_type	= ETH_PHY_BASE_T,
11842 	.ver_addr	= 0,
11843 	.req_flow_ctrl	= 0,
11844 	.req_line_speed	= 0,
11845 	.speed_cap_mask	= 0,
11846 	.req_duplex	= 0,
11847 	.rsrv		= 0,
11848 	.config_init	= (config_init_t)bnx2x_7101_config_init,
11849 	.read_status	= (read_status_t)bnx2x_7101_read_status,
11850 	.link_reset	= (link_reset_t)bnx2x_common_ext_link_reset,
11851 	.config_loopback = (config_loopback_t)bnx2x_7101_config_loopback,
11852 	.format_fw_ver	= (format_fw_ver_t)bnx2x_7101_format_ver,
11853 	.hw_reset	= (hw_reset_t)bnx2x_7101_hw_reset,
11854 	.set_link_led	= (set_link_led_t)bnx2x_7101_set_link_led,
11855 	.phy_specific_func = (phy_specific_func_t)NULL
11856 };
11857 static const struct bnx2x_phy phy_8073 = {
11858 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073,
11859 	.addr		= 0xff,
11860 	.def_md_devad	= 0,
11861 	.flags		= 0,
11862 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11863 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11864 	.mdio_ctrl	= 0,
11865 	.supported	= (SUPPORTED_10000baseT_Full |
11866 			   SUPPORTED_2500baseX_Full |
11867 			   SUPPORTED_1000baseT_Full |
11868 			   SUPPORTED_FIBRE |
11869 			   SUPPORTED_Autoneg |
11870 			   SUPPORTED_Pause |
11871 			   SUPPORTED_Asym_Pause),
11872 	.media_type	= ETH_PHY_KR,
11873 	.ver_addr	= 0,
11874 	.req_flow_ctrl	= 0,
11875 	.req_line_speed	= 0,
11876 	.speed_cap_mask	= 0,
11877 	.req_duplex	= 0,
11878 	.rsrv		= 0,
11879 	.config_init	= (config_init_t)bnx2x_8073_config_init,
11880 	.read_status	= (read_status_t)bnx2x_8073_read_status,
11881 	.link_reset	= (link_reset_t)bnx2x_8073_link_reset,
11882 	.config_loopback = (config_loopback_t)NULL,
11883 	.format_fw_ver	= (format_fw_ver_t)bnx2x_format_ver,
11884 	.hw_reset	= (hw_reset_t)NULL,
11885 	.set_link_led	= (set_link_led_t)NULL,
11886 	.phy_specific_func = (phy_specific_func_t)bnx2x_8073_specific_func
11887 };
11888 static const struct bnx2x_phy phy_8705 = {
11889 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8705,
11890 	.addr		= 0xff,
11891 	.def_md_devad	= 0,
11892 	.flags		= FLAGS_INIT_XGXS_FIRST,
11893 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11894 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11895 	.mdio_ctrl	= 0,
11896 	.supported	= (SUPPORTED_10000baseT_Full |
11897 			   SUPPORTED_FIBRE |
11898 			   SUPPORTED_Pause |
11899 			   SUPPORTED_Asym_Pause),
11900 	.media_type	= ETH_PHY_XFP_FIBER,
11901 	.ver_addr	= 0,
11902 	.req_flow_ctrl	= 0,
11903 	.req_line_speed	= 0,
11904 	.speed_cap_mask	= 0,
11905 	.req_duplex	= 0,
11906 	.rsrv		= 0,
11907 	.config_init	= (config_init_t)bnx2x_8705_config_init,
11908 	.read_status	= (read_status_t)bnx2x_8705_read_status,
11909 	.link_reset	= (link_reset_t)bnx2x_common_ext_link_reset,
11910 	.config_loopback = (config_loopback_t)NULL,
11911 	.format_fw_ver	= (format_fw_ver_t)bnx2x_null_format_ver,
11912 	.hw_reset	= (hw_reset_t)NULL,
11913 	.set_link_led	= (set_link_led_t)NULL,
11914 	.phy_specific_func = (phy_specific_func_t)NULL
11915 };
11916 static const struct bnx2x_phy phy_8706 = {
11917 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8706,
11918 	.addr		= 0xff,
11919 	.def_md_devad	= 0,
11920 	.flags		= FLAGS_INIT_XGXS_FIRST,
11921 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11922 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11923 	.mdio_ctrl	= 0,
11924 	.supported	= (SUPPORTED_10000baseT_Full |
11925 			   SUPPORTED_1000baseT_Full |
11926 			   SUPPORTED_FIBRE |
11927 			   SUPPORTED_Pause |
11928 			   SUPPORTED_Asym_Pause),
11929 	.media_type	= ETH_PHY_SFPP_10G_FIBER,
11930 	.ver_addr	= 0,
11931 	.req_flow_ctrl	= 0,
11932 	.req_line_speed	= 0,
11933 	.speed_cap_mask	= 0,
11934 	.req_duplex	= 0,
11935 	.rsrv		= 0,
11936 	.config_init	= (config_init_t)bnx2x_8706_config_init,
11937 	.read_status	= (read_status_t)bnx2x_8706_read_status,
11938 	.link_reset	= (link_reset_t)bnx2x_common_ext_link_reset,
11939 	.config_loopback = (config_loopback_t)NULL,
11940 	.format_fw_ver	= (format_fw_ver_t)bnx2x_format_ver,
11941 	.hw_reset	= (hw_reset_t)NULL,
11942 	.set_link_led	= (set_link_led_t)NULL,
11943 	.phy_specific_func = (phy_specific_func_t)NULL
11944 };
11945 
11946 static const struct bnx2x_phy phy_8726 = {
11947 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726,
11948 	.addr		= 0xff,
11949 	.def_md_devad	= 0,
11950 	.flags		= (FLAGS_INIT_XGXS_FIRST |
11951 			   FLAGS_TX_ERROR_CHECK),
11952 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11953 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11954 	.mdio_ctrl	= 0,
11955 	.supported	= (SUPPORTED_10000baseT_Full |
11956 			   SUPPORTED_1000baseT_Full |
11957 			   SUPPORTED_Autoneg |
11958 			   SUPPORTED_FIBRE |
11959 			   SUPPORTED_Pause |
11960 			   SUPPORTED_Asym_Pause),
11961 	.media_type	= ETH_PHY_NOT_PRESENT,
11962 	.ver_addr	= 0,
11963 	.req_flow_ctrl	= 0,
11964 	.req_line_speed	= 0,
11965 	.speed_cap_mask	= 0,
11966 	.req_duplex	= 0,
11967 	.rsrv		= 0,
11968 	.config_init	= (config_init_t)bnx2x_8726_config_init,
11969 	.read_status	= (read_status_t)bnx2x_8726_read_status,
11970 	.link_reset	= (link_reset_t)bnx2x_8726_link_reset,
11971 	.config_loopback = (config_loopback_t)bnx2x_8726_config_loopback,
11972 	.format_fw_ver	= (format_fw_ver_t)bnx2x_format_ver,
11973 	.hw_reset	= (hw_reset_t)NULL,
11974 	.set_link_led	= (set_link_led_t)NULL,
11975 	.phy_specific_func = (phy_specific_func_t)NULL
11976 };
11977 
11978 static const struct bnx2x_phy phy_8727 = {
11979 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727,
11980 	.addr		= 0xff,
11981 	.def_md_devad	= 0,
11982 	.flags		= (FLAGS_FAN_FAILURE_DET_REQ |
11983 			   FLAGS_TX_ERROR_CHECK),
11984 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11985 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
11986 	.mdio_ctrl	= 0,
11987 	.supported	= (SUPPORTED_10000baseT_Full |
11988 			   SUPPORTED_1000baseT_Full |
11989 			   SUPPORTED_FIBRE |
11990 			   SUPPORTED_Pause |
11991 			   SUPPORTED_Asym_Pause),
11992 	.media_type	= ETH_PHY_NOT_PRESENT,
11993 	.ver_addr	= 0,
11994 	.req_flow_ctrl	= 0,
11995 	.req_line_speed	= 0,
11996 	.speed_cap_mask	= 0,
11997 	.req_duplex	= 0,
11998 	.rsrv		= 0,
11999 	.config_init	= (config_init_t)bnx2x_8727_config_init,
12000 	.read_status	= (read_status_t)bnx2x_8727_read_status,
12001 	.link_reset	= (link_reset_t)bnx2x_8727_link_reset,
12002 	.config_loopback = (config_loopback_t)NULL,
12003 	.format_fw_ver	= (format_fw_ver_t)bnx2x_format_ver,
12004 	.hw_reset	= (hw_reset_t)bnx2x_8727_hw_reset,
12005 	.set_link_led	= (set_link_led_t)bnx2x_8727_set_link_led,
12006 	.phy_specific_func = (phy_specific_func_t)bnx2x_8727_specific_func
12007 };
12008 static const struct bnx2x_phy phy_8481 = {
12009 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481,
12010 	.addr		= 0xff,
12011 	.def_md_devad	= 0,
12012 	.flags		= FLAGS_FAN_FAILURE_DET_REQ |
12013 			  FLAGS_REARM_LATCH_SIGNAL,
12014 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12015 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12016 	.mdio_ctrl	= 0,
12017 	.supported	= (SUPPORTED_10baseT_Half |
12018 			   SUPPORTED_10baseT_Full |
12019 			   SUPPORTED_100baseT_Half |
12020 			   SUPPORTED_100baseT_Full |
12021 			   SUPPORTED_1000baseT_Full |
12022 			   SUPPORTED_10000baseT_Full |
12023 			   SUPPORTED_TP |
12024 			   SUPPORTED_Autoneg |
12025 			   SUPPORTED_Pause |
12026 			   SUPPORTED_Asym_Pause),
12027 	.media_type	= ETH_PHY_BASE_T,
12028 	.ver_addr	= 0,
12029 	.req_flow_ctrl	= 0,
12030 	.req_line_speed	= 0,
12031 	.speed_cap_mask	= 0,
12032 	.req_duplex	= 0,
12033 	.rsrv		= 0,
12034 	.config_init	= (config_init_t)bnx2x_8481_config_init,
12035 	.read_status	= (read_status_t)bnx2x_848xx_read_status,
12036 	.link_reset	= (link_reset_t)bnx2x_8481_link_reset,
12037 	.config_loopback = (config_loopback_t)NULL,
12038 	.format_fw_ver	= (format_fw_ver_t)bnx2x_848xx_format_ver,
12039 	.hw_reset	= (hw_reset_t)bnx2x_8481_hw_reset,
12040 	.set_link_led	= (set_link_led_t)bnx2x_848xx_set_link_led,
12041 	.phy_specific_func = (phy_specific_func_t)NULL
12042 };
12043 
12044 static const struct bnx2x_phy phy_84823 = {
12045 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84823,
12046 	.addr		= 0xff,
12047 	.def_md_devad	= 0,
12048 	.flags		= (FLAGS_FAN_FAILURE_DET_REQ |
12049 			   FLAGS_REARM_LATCH_SIGNAL |
12050 			   FLAGS_TX_ERROR_CHECK),
12051 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12052 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12053 	.mdio_ctrl	= 0,
12054 	.supported	= (SUPPORTED_10baseT_Half |
12055 			   SUPPORTED_10baseT_Full |
12056 			   SUPPORTED_100baseT_Half |
12057 			   SUPPORTED_100baseT_Full |
12058 			   SUPPORTED_1000baseT_Full |
12059 			   SUPPORTED_10000baseT_Full |
12060 			   SUPPORTED_TP |
12061 			   SUPPORTED_Autoneg |
12062 			   SUPPORTED_Pause |
12063 			   SUPPORTED_Asym_Pause),
12064 	.media_type	= ETH_PHY_BASE_T,
12065 	.ver_addr	= 0,
12066 	.req_flow_ctrl	= 0,
12067 	.req_line_speed	= 0,
12068 	.speed_cap_mask	= 0,
12069 	.req_duplex	= 0,
12070 	.rsrv		= 0,
12071 	.config_init	= (config_init_t)bnx2x_848x3_config_init,
12072 	.read_status	= (read_status_t)bnx2x_848xx_read_status,
12073 	.link_reset	= (link_reset_t)bnx2x_848x3_link_reset,
12074 	.config_loopback = (config_loopback_t)NULL,
12075 	.format_fw_ver	= (format_fw_ver_t)bnx2x_848xx_format_ver,
12076 	.hw_reset	= (hw_reset_t)NULL,
12077 	.set_link_led	= (set_link_led_t)bnx2x_848xx_set_link_led,
12078 	.phy_specific_func = (phy_specific_func_t)bnx2x_848xx_specific_func
12079 };
12080 
12081 static const struct bnx2x_phy phy_84833 = {
12082 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833,
12083 	.addr		= 0xff,
12084 	.def_md_devad	= 0,
12085 	.flags		= (FLAGS_FAN_FAILURE_DET_REQ |
12086 			   FLAGS_REARM_LATCH_SIGNAL |
12087 			   FLAGS_TX_ERROR_CHECK),
12088 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12089 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12090 	.mdio_ctrl	= 0,
12091 	.supported	= (SUPPORTED_100baseT_Half |
12092 			   SUPPORTED_100baseT_Full |
12093 			   SUPPORTED_1000baseT_Full |
12094 			   SUPPORTED_10000baseT_Full |
12095 			   SUPPORTED_TP |
12096 			   SUPPORTED_Autoneg |
12097 			   SUPPORTED_Pause |
12098 			   SUPPORTED_Asym_Pause),
12099 	.media_type	= ETH_PHY_BASE_T,
12100 	.ver_addr	= 0,
12101 	.req_flow_ctrl	= 0,
12102 	.req_line_speed	= 0,
12103 	.speed_cap_mask	= 0,
12104 	.req_duplex	= 0,
12105 	.rsrv		= 0,
12106 	.config_init	= (config_init_t)bnx2x_848x3_config_init,
12107 	.read_status	= (read_status_t)bnx2x_848xx_read_status,
12108 	.link_reset	= (link_reset_t)bnx2x_848x3_link_reset,
12109 	.config_loopback = (config_loopback_t)NULL,
12110 	.format_fw_ver	= (format_fw_ver_t)bnx2x_848xx_format_ver,
12111 	.hw_reset	= (hw_reset_t)bnx2x_84833_hw_reset_phy,
12112 	.set_link_led	= (set_link_led_t)bnx2x_848xx_set_link_led,
12113 	.phy_specific_func = (phy_specific_func_t)bnx2x_848xx_specific_func
12114 };
12115 
12116 static const struct bnx2x_phy phy_84834 = {
12117 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84834,
12118 	.addr		= 0xff,
12119 	.def_md_devad	= 0,
12120 	.flags		= FLAGS_FAN_FAILURE_DET_REQ |
12121 			    FLAGS_REARM_LATCH_SIGNAL,
12122 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12123 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12124 	.mdio_ctrl	= 0,
12125 	.supported	= (SUPPORTED_100baseT_Half |
12126 			   SUPPORTED_100baseT_Full |
12127 			   SUPPORTED_1000baseT_Full |
12128 			   SUPPORTED_10000baseT_Full |
12129 			   SUPPORTED_TP |
12130 			   SUPPORTED_Autoneg |
12131 			   SUPPORTED_Pause |
12132 			   SUPPORTED_Asym_Pause),
12133 	.media_type	= ETH_PHY_BASE_T,
12134 	.ver_addr	= 0,
12135 	.req_flow_ctrl	= 0,
12136 	.req_line_speed	= 0,
12137 	.speed_cap_mask	= 0,
12138 	.req_duplex	= 0,
12139 	.rsrv		= 0,
12140 	.config_init	= (config_init_t)bnx2x_848x3_config_init,
12141 	.read_status	= (read_status_t)bnx2x_848xx_read_status,
12142 	.link_reset	= (link_reset_t)bnx2x_848x3_link_reset,
12143 	.config_loopback = (config_loopback_t)NULL,
12144 	.format_fw_ver	= (format_fw_ver_t)bnx2x_848xx_format_ver,
12145 	.hw_reset	= (hw_reset_t)bnx2x_84833_hw_reset_phy,
12146 	.set_link_led	= (set_link_led_t)bnx2x_848xx_set_link_led,
12147 	.phy_specific_func = (phy_specific_func_t)bnx2x_848xx_specific_func
12148 };
12149 
12150 static const struct bnx2x_phy phy_84858 = {
12151 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858,
12152 	.addr		= 0xff,
12153 	.def_md_devad	= 0,
12154 	.flags		= FLAGS_FAN_FAILURE_DET_REQ |
12155 			    FLAGS_REARM_LATCH_SIGNAL,
12156 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12157 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12158 	.mdio_ctrl	= 0,
12159 	.supported	= (SUPPORTED_100baseT_Half |
12160 			   SUPPORTED_100baseT_Full |
12161 			   SUPPORTED_1000baseT_Full |
12162 			   SUPPORTED_10000baseT_Full |
12163 			   SUPPORTED_TP |
12164 			   SUPPORTED_Autoneg |
12165 			   SUPPORTED_Pause |
12166 			   SUPPORTED_Asym_Pause),
12167 	.media_type	= ETH_PHY_BASE_T,
12168 	.ver_addr	= 0,
12169 	.req_flow_ctrl	= 0,
12170 	.req_line_speed	= 0,
12171 	.speed_cap_mask	= 0,
12172 	.req_duplex	= 0,
12173 	.rsrv		= 0,
12174 	.config_init	= (config_init_t)bnx2x_848x3_config_init,
12175 	.read_status	= (read_status_t)bnx2x_848xx_read_status,
12176 	.link_reset	= (link_reset_t)bnx2x_848x3_link_reset,
12177 	.config_loopback = (config_loopback_t)NULL,
12178 	.format_fw_ver	= (format_fw_ver_t)bnx2x_8485x_format_ver,
12179 	.hw_reset	= (hw_reset_t)bnx2x_84833_hw_reset_phy,
12180 	.set_link_led	= (set_link_led_t)bnx2x_848xx_set_link_led,
12181 	.phy_specific_func = (phy_specific_func_t)bnx2x_848xx_specific_func
12182 };
12183 
12184 static const struct bnx2x_phy phy_54618se = {
12185 	.type		= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54618SE,
12186 	.addr		= 0xff,
12187 	.def_md_devad	= 0,
12188 	.flags		= FLAGS_INIT_XGXS_FIRST,
12189 	.rx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12190 	.tx_preemphasis	= {0xffff, 0xffff, 0xffff, 0xffff},
12191 	.mdio_ctrl	= 0,
12192 	.supported	= (SUPPORTED_10baseT_Half |
12193 			   SUPPORTED_10baseT_Full |
12194 			   SUPPORTED_100baseT_Half |
12195 			   SUPPORTED_100baseT_Full |
12196 			   SUPPORTED_1000baseT_Full |
12197 			   SUPPORTED_TP |
12198 			   SUPPORTED_Autoneg |
12199 			   SUPPORTED_Pause |
12200 			   SUPPORTED_Asym_Pause),
12201 	.media_type	= ETH_PHY_BASE_T,
12202 	.ver_addr	= 0,
12203 	.req_flow_ctrl	= 0,
12204 	.req_line_speed	= 0,
12205 	.speed_cap_mask	= 0,
12206 	/* req_duplex = */0,
12207 	/* rsrv = */0,
12208 	.config_init	= (config_init_t)bnx2x_54618se_config_init,
12209 	.read_status	= (read_status_t)bnx2x_54618se_read_status,
12210 	.link_reset	= (link_reset_t)bnx2x_54618se_link_reset,
12211 	.config_loopback = (config_loopback_t)bnx2x_54618se_config_loopback,
12212 	.format_fw_ver	= (format_fw_ver_t)NULL,
12213 	.hw_reset	= (hw_reset_t)NULL,
12214 	.set_link_led	= (set_link_led_t)bnx2x_5461x_set_link_led,
12215 	.phy_specific_func = (phy_specific_func_t)bnx2x_54618se_specific_func
12216 };
12217 /*****************************************************************/
12218 /*                                                               */
12219 /* Populate the phy according. Main function: bnx2x_populate_phy   */
12220 /*                                                               */
12221 /*****************************************************************/
12222 
12223 static void bnx2x_populate_preemphasis(struct bnx2x *bp, u32 shmem_base,
12224 				     struct bnx2x_phy *phy, u8 port,
12225 				     u8 phy_index)
12226 {
12227 	/* Get the 4 lanes xgxs config rx and tx */
12228 	u32 rx = 0, tx = 0, i;
12229 	for (i = 0; i < 2; i++) {
12230 		/* INT_PHY and EXT_PHY1 share the same value location in
12231 		 * the shmem. When num_phys is greater than 1, than this value
12232 		 * applies only to EXT_PHY1
12233 		 */
12234 		if (phy_index == INT_PHY || phy_index == EXT_PHY1) {
12235 			rx = REG_RD(bp, shmem_base +
12236 				    offsetof(struct shmem_region,
12237 			  dev_info.port_hw_config[port].xgxs_config_rx[i<<1]));
12238 
12239 			tx = REG_RD(bp, shmem_base +
12240 				    offsetof(struct shmem_region,
12241 			  dev_info.port_hw_config[port].xgxs_config_tx[i<<1]));
12242 		} else {
12243 			rx = REG_RD(bp, shmem_base +
12244 				    offsetof(struct shmem_region,
12245 			 dev_info.port_hw_config[port].xgxs_config2_rx[i<<1]));
12246 
12247 			tx = REG_RD(bp, shmem_base +
12248 				    offsetof(struct shmem_region,
12249 			 dev_info.port_hw_config[port].xgxs_config2_rx[i<<1]));
12250 		}
12251 
12252 		phy->rx_preemphasis[i << 1] = ((rx>>16) & 0xffff);
12253 		phy->rx_preemphasis[(i << 1) + 1] = (rx & 0xffff);
12254 
12255 		phy->tx_preemphasis[i << 1] = ((tx>>16) & 0xffff);
12256 		phy->tx_preemphasis[(i << 1) + 1] = (tx & 0xffff);
12257 	}
12258 }
12259 
12260 static u32 bnx2x_get_ext_phy_config(struct bnx2x *bp, u32 shmem_base,
12261 				    u8 phy_index, u8 port)
12262 {
12263 	u32 ext_phy_config = 0;
12264 	switch (phy_index) {
12265 	case EXT_PHY1:
12266 		ext_phy_config = REG_RD(bp, shmem_base +
12267 					      offsetof(struct shmem_region,
12268 			dev_info.port_hw_config[port].external_phy_config));
12269 		break;
12270 	case EXT_PHY2:
12271 		ext_phy_config = REG_RD(bp, shmem_base +
12272 					      offsetof(struct shmem_region,
12273 			dev_info.port_hw_config[port].external_phy_config2));
12274 		break;
12275 	default:
12276 		DP(NETIF_MSG_LINK, "Invalid phy_index %d\n", phy_index);
12277 		return -EINVAL;
12278 	}
12279 
12280 	return ext_phy_config;
12281 }
12282 static int bnx2x_populate_int_phy(struct bnx2x *bp, u32 shmem_base, u8 port,
12283 				  struct bnx2x_phy *phy)
12284 {
12285 	u32 phy_addr;
12286 	u32 chip_id;
12287 	u32 switch_cfg = (REG_RD(bp, shmem_base +
12288 				       offsetof(struct shmem_region,
12289 			dev_info.port_feature_config[port].link_config)) &
12290 			  PORT_FEATURE_CONNECTED_SWITCH_MASK);
12291 	chip_id = (REG_RD(bp, MISC_REG_CHIP_NUM) << 16) |
12292 		((REG_RD(bp, MISC_REG_CHIP_REV) & 0xf) << 12);
12293 
12294 	DP(NETIF_MSG_LINK, ":chip_id = 0x%x\n", chip_id);
12295 	if (USES_WARPCORE(bp)) {
12296 		u32 serdes_net_if;
12297 		phy_addr = REG_RD(bp,
12298 				  MISC_REG_WC0_CTRL_PHY_ADDR);
12299 		*phy = phy_warpcore;
12300 		if (REG_RD(bp, MISC_REG_PORT4MODE_EN_OVWR) == 0x3)
12301 			phy->flags |= FLAGS_4_PORT_MODE;
12302 		else
12303 			phy->flags &= ~FLAGS_4_PORT_MODE;
12304 			/* Check Dual mode */
12305 		serdes_net_if = (REG_RD(bp, shmem_base +
12306 					offsetof(struct shmem_region, dev_info.
12307 					port_hw_config[port].default_cfg)) &
12308 				 PORT_HW_CFG_NET_SERDES_IF_MASK);
12309 		/* Set the appropriate supported and flags indications per
12310 		 * interface type of the chip
12311 		 */
12312 		switch (serdes_net_if) {
12313 		case PORT_HW_CFG_NET_SERDES_IF_SGMII:
12314 			phy->supported &= (SUPPORTED_10baseT_Half |
12315 					   SUPPORTED_10baseT_Full |
12316 					   SUPPORTED_100baseT_Half |
12317 					   SUPPORTED_100baseT_Full |
12318 					   SUPPORTED_1000baseT_Full |
12319 					   SUPPORTED_FIBRE |
12320 					   SUPPORTED_Autoneg |
12321 					   SUPPORTED_Pause |
12322 					   SUPPORTED_Asym_Pause);
12323 			phy->media_type = ETH_PHY_BASE_T;
12324 			break;
12325 		case PORT_HW_CFG_NET_SERDES_IF_XFI:
12326 			phy->supported &= (SUPPORTED_1000baseT_Full |
12327 					   SUPPORTED_10000baseT_Full |
12328 					   SUPPORTED_FIBRE |
12329 					   SUPPORTED_Pause |
12330 					   SUPPORTED_Asym_Pause);
12331 			phy->media_type = ETH_PHY_XFP_FIBER;
12332 			break;
12333 		case PORT_HW_CFG_NET_SERDES_IF_SFI:
12334 			phy->supported &= (SUPPORTED_1000baseT_Full |
12335 					   SUPPORTED_10000baseT_Full |
12336 					   SUPPORTED_FIBRE |
12337 					   SUPPORTED_Pause |
12338 					   SUPPORTED_Asym_Pause);
12339 			phy->media_type = ETH_PHY_SFPP_10G_FIBER;
12340 			break;
12341 		case PORT_HW_CFG_NET_SERDES_IF_KR:
12342 			phy->media_type = ETH_PHY_KR;
12343 			phy->supported &= (SUPPORTED_1000baseKX_Full |
12344 					   SUPPORTED_10000baseKR_Full |
12345 					   SUPPORTED_FIBRE |
12346 					   SUPPORTED_Autoneg |
12347 					   SUPPORTED_Pause |
12348 					   SUPPORTED_Asym_Pause);
12349 			break;
12350 		case PORT_HW_CFG_NET_SERDES_IF_DXGXS:
12351 			phy->media_type = ETH_PHY_KR;
12352 			phy->flags |= FLAGS_WC_DUAL_MODE;
12353 			phy->supported &= (SUPPORTED_20000baseMLD2_Full |
12354 					   SUPPORTED_FIBRE |
12355 					   SUPPORTED_Pause |
12356 					   SUPPORTED_Asym_Pause);
12357 			break;
12358 		case PORT_HW_CFG_NET_SERDES_IF_KR2:
12359 			phy->media_type = ETH_PHY_KR;
12360 			phy->flags |= FLAGS_WC_DUAL_MODE;
12361 			phy->supported &= (SUPPORTED_20000baseKR2_Full |
12362 					   SUPPORTED_10000baseKR_Full |
12363 					   SUPPORTED_1000baseKX_Full |
12364 					   SUPPORTED_Autoneg |
12365 					   SUPPORTED_FIBRE |
12366 					   SUPPORTED_Pause |
12367 					   SUPPORTED_Asym_Pause);
12368 			phy->flags &= ~FLAGS_TX_ERROR_CHECK;
12369 			break;
12370 		default:
12371 			DP(NETIF_MSG_LINK, "Unknown WC interface type 0x%x\n",
12372 				       serdes_net_if);
12373 			break;
12374 		}
12375 
12376 		/* Enable MDC/MDIO work-around for E3 A0 since free running MDC
12377 		 * was not set as expected. For B0, ECO will be enabled so there
12378 		 * won't be an issue there
12379 		 */
12380 		if (CHIP_REV(bp) == CHIP_REV_Ax)
12381 			phy->flags |= FLAGS_MDC_MDIO_WA;
12382 		else
12383 			phy->flags |= FLAGS_MDC_MDIO_WA_B0;
12384 	} else {
12385 		switch (switch_cfg) {
12386 		case SWITCH_CFG_1G:
12387 			phy_addr = REG_RD(bp,
12388 					  NIG_REG_SERDES0_CTRL_PHY_ADDR +
12389 					  port * 0x10);
12390 			*phy = phy_serdes;
12391 			break;
12392 		case SWITCH_CFG_10G:
12393 			phy_addr = REG_RD(bp,
12394 					  NIG_REG_XGXS0_CTRL_PHY_ADDR +
12395 					  port * 0x18);
12396 			*phy = phy_xgxs;
12397 			break;
12398 		default:
12399 			DP(NETIF_MSG_LINK, "Invalid switch_cfg\n");
12400 			return -EINVAL;
12401 		}
12402 	}
12403 	phy->addr = (u8)phy_addr;
12404 	phy->mdio_ctrl = bnx2x_get_emac_base(bp,
12405 					    SHARED_HW_CFG_MDC_MDIO_ACCESS1_BOTH,
12406 					    port);
12407 	if (CHIP_IS_E2(bp))
12408 		phy->def_md_devad = E2_DEFAULT_PHY_DEV_ADDR;
12409 	else
12410 		phy->def_md_devad = DEFAULT_PHY_DEV_ADDR;
12411 
12412 	DP(NETIF_MSG_LINK, "Internal phy port=%d, addr=0x%x, mdio_ctl=0x%x\n",
12413 		   port, phy->addr, phy->mdio_ctrl);
12414 
12415 	bnx2x_populate_preemphasis(bp, shmem_base, phy, port, INT_PHY);
12416 	return 0;
12417 }
12418 
12419 static int bnx2x_populate_ext_phy(struct bnx2x *bp,
12420 				  u8 phy_index,
12421 				  u32 shmem_base,
12422 				  u32 shmem2_base,
12423 				  u8 port,
12424 				  struct bnx2x_phy *phy)
12425 {
12426 	u32 ext_phy_config, phy_type, config2;
12427 	u32 mdc_mdio_access = SHARED_HW_CFG_MDC_MDIO_ACCESS1_BOTH;
12428 	ext_phy_config = bnx2x_get_ext_phy_config(bp, shmem_base,
12429 						  phy_index, port);
12430 	phy_type = XGXS_EXT_PHY_TYPE(ext_phy_config);
12431 	/* Select the phy type */
12432 	switch (phy_type) {
12433 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
12434 		mdc_mdio_access = SHARED_HW_CFG_MDC_MDIO_ACCESS1_SWAPPED;
12435 		*phy = phy_8073;
12436 		break;
12437 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8705:
12438 		*phy = phy_8705;
12439 		break;
12440 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8706:
12441 		*phy = phy_8706;
12442 		break;
12443 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
12444 		mdc_mdio_access = SHARED_HW_CFG_MDC_MDIO_ACCESS1_EMAC1;
12445 		*phy = phy_8726;
12446 		break;
12447 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727_NOC:
12448 		/* BCM8727_NOC => BCM8727 no over current */
12449 		mdc_mdio_access = SHARED_HW_CFG_MDC_MDIO_ACCESS1_EMAC1;
12450 		*phy = phy_8727;
12451 		phy->flags |= FLAGS_NOC;
12452 		break;
12453 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8722:
12454 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
12455 		mdc_mdio_access = SHARED_HW_CFG_MDC_MDIO_ACCESS1_EMAC1;
12456 		*phy = phy_8727;
12457 		break;
12458 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481:
12459 		*phy = phy_8481;
12460 		break;
12461 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84823:
12462 		*phy = phy_84823;
12463 		break;
12464 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833:
12465 		*phy = phy_84833;
12466 		break;
12467 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84834:
12468 		*phy = phy_84834;
12469 		break;
12470 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858:
12471 		*phy = phy_84858;
12472 		break;
12473 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54616:
12474 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54618SE:
12475 		*phy = phy_54618se;
12476 		if (phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM54618SE)
12477 			phy->flags |= FLAGS_EEE;
12478 		break;
12479 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
12480 		*phy = phy_7101;
12481 		break;
12482 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE:
12483 		*phy = phy_null;
12484 		return -EINVAL;
12485 	default:
12486 		*phy = phy_null;
12487 		/* In case external PHY wasn't found */
12488 		if ((phy_type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT) &&
12489 		    (phy_type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
12490 			return -EINVAL;
12491 		return 0;
12492 	}
12493 
12494 	phy->addr = XGXS_EXT_PHY_ADDR(ext_phy_config);
12495 	bnx2x_populate_preemphasis(bp, shmem_base, phy, port, phy_index);
12496 
12497 	/* The shmem address of the phy version is located on different
12498 	 * structures. In case this structure is too old, do not set
12499 	 * the address
12500 	 */
12501 	config2 = REG_RD(bp, shmem_base + offsetof(struct shmem_region,
12502 					dev_info.shared_hw_config.config2));
12503 	if (phy_index == EXT_PHY1) {
12504 		phy->ver_addr = shmem_base + offsetof(struct shmem_region,
12505 				port_mb[port].ext_phy_fw_version);
12506 
12507 		/* Check specific mdc mdio settings */
12508 		if (config2 & SHARED_HW_CFG_MDC_MDIO_ACCESS1_MASK)
12509 			mdc_mdio_access = config2 &
12510 			SHARED_HW_CFG_MDC_MDIO_ACCESS1_MASK;
12511 	} else {
12512 		u32 size = REG_RD(bp, shmem2_base);
12513 
12514 		if (size >
12515 		    offsetof(struct shmem2_region, ext_phy_fw_version2)) {
12516 			phy->ver_addr = shmem2_base +
12517 			    offsetof(struct shmem2_region,
12518 				     ext_phy_fw_version2[port]);
12519 		}
12520 		/* Check specific mdc mdio settings */
12521 		if (config2 & SHARED_HW_CFG_MDC_MDIO_ACCESS2_MASK)
12522 			mdc_mdio_access = (config2 &
12523 			SHARED_HW_CFG_MDC_MDIO_ACCESS2_MASK) >>
12524 			(SHARED_HW_CFG_MDC_MDIO_ACCESS2_SHIFT -
12525 			 SHARED_HW_CFG_MDC_MDIO_ACCESS1_SHIFT);
12526 	}
12527 	phy->mdio_ctrl = bnx2x_get_emac_base(bp, mdc_mdio_access, port);
12528 
12529 	if (bnx2x_is_8483x_8485x(phy) && (phy->ver_addr)) {
12530 		/* Remove 100Mb link supported for BCM84833/4 when phy fw
12531 		 * version lower than or equal to 1.39
12532 		 */
12533 		u32 raw_ver = REG_RD(bp, phy->ver_addr);
12534 		if (((raw_ver & 0x7F) <= 39) &&
12535 		    (((raw_ver & 0xF80) >> 7) <= 1))
12536 			phy->supported &= ~(SUPPORTED_100baseT_Half |
12537 					    SUPPORTED_100baseT_Full);
12538 	}
12539 
12540 	DP(NETIF_MSG_LINK, "phy_type 0x%x port %d found in index %d\n",
12541 		   phy_type, port, phy_index);
12542 	DP(NETIF_MSG_LINK, "             addr=0x%x, mdio_ctl=0x%x\n",
12543 		   phy->addr, phy->mdio_ctrl);
12544 	return 0;
12545 }
12546 
12547 static int bnx2x_populate_phy(struct bnx2x *bp, u8 phy_index, u32 shmem_base,
12548 			      u32 shmem2_base, u8 port, struct bnx2x_phy *phy)
12549 {
12550 	int status = 0;
12551 	phy->type = PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN;
12552 	if (phy_index == INT_PHY)
12553 		return bnx2x_populate_int_phy(bp, shmem_base, port, phy);
12554 	status = bnx2x_populate_ext_phy(bp, phy_index, shmem_base, shmem2_base,
12555 					port, phy);
12556 	return status;
12557 }
12558 
12559 static void bnx2x_phy_def_cfg(struct link_params *params,
12560 			      struct bnx2x_phy *phy,
12561 			      u8 phy_index)
12562 {
12563 	struct bnx2x *bp = params->bp;
12564 	u32 link_config;
12565 	/* Populate the default phy configuration for MF mode */
12566 	if (phy_index == EXT_PHY2) {
12567 		link_config = REG_RD(bp, params->shmem_base +
12568 				     offsetof(struct shmem_region, dev_info.
12569 			port_feature_config[params->port].link_config2));
12570 		phy->speed_cap_mask = REG_RD(bp, params->shmem_base +
12571 					     offsetof(struct shmem_region,
12572 						      dev_info.
12573 			port_hw_config[params->port].speed_capability_mask2));
12574 	} else {
12575 		link_config = REG_RD(bp, params->shmem_base +
12576 				     offsetof(struct shmem_region, dev_info.
12577 				port_feature_config[params->port].link_config));
12578 		phy->speed_cap_mask = REG_RD(bp, params->shmem_base +
12579 					     offsetof(struct shmem_region,
12580 						      dev_info.
12581 			port_hw_config[params->port].speed_capability_mask));
12582 	}
12583 	DP(NETIF_MSG_LINK,
12584 	   "Default config phy idx %x cfg 0x%x speed_cap_mask 0x%x\n",
12585 	   phy_index, link_config, phy->speed_cap_mask);
12586 
12587 	phy->req_duplex = DUPLEX_FULL;
12588 	switch (link_config  & PORT_FEATURE_LINK_SPEED_MASK) {
12589 	case PORT_FEATURE_LINK_SPEED_10M_HALF:
12590 		phy->req_duplex = DUPLEX_HALF;
12591 	case PORT_FEATURE_LINK_SPEED_10M_FULL:
12592 		phy->req_line_speed = SPEED_10;
12593 		break;
12594 	case PORT_FEATURE_LINK_SPEED_100M_HALF:
12595 		phy->req_duplex = DUPLEX_HALF;
12596 	case PORT_FEATURE_LINK_SPEED_100M_FULL:
12597 		phy->req_line_speed = SPEED_100;
12598 		break;
12599 	case PORT_FEATURE_LINK_SPEED_1G:
12600 		phy->req_line_speed = SPEED_1000;
12601 		break;
12602 	case PORT_FEATURE_LINK_SPEED_2_5G:
12603 		phy->req_line_speed = SPEED_2500;
12604 		break;
12605 	case PORT_FEATURE_LINK_SPEED_10G_CX4:
12606 		phy->req_line_speed = SPEED_10000;
12607 		break;
12608 	default:
12609 		phy->req_line_speed = SPEED_AUTO_NEG;
12610 		break;
12611 	}
12612 
12613 	switch (link_config  & PORT_FEATURE_FLOW_CONTROL_MASK) {
12614 	case PORT_FEATURE_FLOW_CONTROL_AUTO:
12615 		phy->req_flow_ctrl = BNX2X_FLOW_CTRL_AUTO;
12616 		break;
12617 	case PORT_FEATURE_FLOW_CONTROL_TX:
12618 		phy->req_flow_ctrl = BNX2X_FLOW_CTRL_TX;
12619 		break;
12620 	case PORT_FEATURE_FLOW_CONTROL_RX:
12621 		phy->req_flow_ctrl = BNX2X_FLOW_CTRL_RX;
12622 		break;
12623 	case PORT_FEATURE_FLOW_CONTROL_BOTH:
12624 		phy->req_flow_ctrl = BNX2X_FLOW_CTRL_BOTH;
12625 		break;
12626 	default:
12627 		phy->req_flow_ctrl = BNX2X_FLOW_CTRL_NONE;
12628 		break;
12629 	}
12630 }
12631 
12632 u32 bnx2x_phy_selection(struct link_params *params)
12633 {
12634 	u32 phy_config_swapped, prio_cfg;
12635 	u32 return_cfg = PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT;
12636 
12637 	phy_config_swapped = params->multi_phy_config &
12638 		PORT_HW_CFG_PHY_SWAPPED_ENABLED;
12639 
12640 	prio_cfg = params->multi_phy_config &
12641 			PORT_HW_CFG_PHY_SELECTION_MASK;
12642 
12643 	if (phy_config_swapped) {
12644 		switch (prio_cfg) {
12645 		case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
12646 		     return_cfg = PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY;
12647 		     break;
12648 		case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
12649 		     return_cfg = PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY;
12650 		     break;
12651 		case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
12652 		     return_cfg = PORT_HW_CFG_PHY_SELECTION_FIRST_PHY;
12653 		     break;
12654 		case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
12655 		     return_cfg = PORT_HW_CFG_PHY_SELECTION_SECOND_PHY;
12656 		     break;
12657 		}
12658 	} else
12659 		return_cfg = prio_cfg;
12660 
12661 	return return_cfg;
12662 }
12663 
12664 int bnx2x_phy_probe(struct link_params *params)
12665 {
12666 	u8 phy_index, actual_phy_idx;
12667 	u32 phy_config_swapped, sync_offset, media_types;
12668 	struct bnx2x *bp = params->bp;
12669 	struct bnx2x_phy *phy;
12670 	params->num_phys = 0;
12671 	DP(NETIF_MSG_LINK, "Begin phy probe\n");
12672 	phy_config_swapped = params->multi_phy_config &
12673 		PORT_HW_CFG_PHY_SWAPPED_ENABLED;
12674 
12675 	for (phy_index = INT_PHY; phy_index < MAX_PHYS;
12676 	      phy_index++) {
12677 		actual_phy_idx = phy_index;
12678 		if (phy_config_swapped) {
12679 			if (phy_index == EXT_PHY1)
12680 				actual_phy_idx = EXT_PHY2;
12681 			else if (phy_index == EXT_PHY2)
12682 				actual_phy_idx = EXT_PHY1;
12683 		}
12684 		DP(NETIF_MSG_LINK, "phy_config_swapped %x, phy_index %x,"
12685 			       " actual_phy_idx %x\n", phy_config_swapped,
12686 			   phy_index, actual_phy_idx);
12687 		phy = &params->phy[actual_phy_idx];
12688 		if (bnx2x_populate_phy(bp, phy_index, params->shmem_base,
12689 				       params->shmem2_base, params->port,
12690 				       phy) != 0) {
12691 			params->num_phys = 0;
12692 			DP(NETIF_MSG_LINK, "phy probe failed in phy index %d\n",
12693 				   phy_index);
12694 			for (phy_index = INT_PHY;
12695 			      phy_index < MAX_PHYS;
12696 			      phy_index++)
12697 				*phy = phy_null;
12698 			return -EINVAL;
12699 		}
12700 		if (phy->type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN)
12701 			break;
12702 
12703 		if (params->feature_config_flags &
12704 		    FEATURE_CONFIG_DISABLE_REMOTE_FAULT_DET)
12705 			phy->flags &= ~FLAGS_TX_ERROR_CHECK;
12706 
12707 		if (!(params->feature_config_flags &
12708 		      FEATURE_CONFIG_MT_SUPPORT))
12709 			phy->flags |= FLAGS_MDC_MDIO_WA_G;
12710 
12711 		sync_offset = params->shmem_base +
12712 			offsetof(struct shmem_region,
12713 			dev_info.port_hw_config[params->port].media_type);
12714 		media_types = REG_RD(bp, sync_offset);
12715 
12716 		/* Update media type for non-PMF sync only for the first time
12717 		 * In case the media type changes afterwards, it will be updated
12718 		 * using the update_status function
12719 		 */
12720 		if ((media_types & (PORT_HW_CFG_MEDIA_TYPE_PHY0_MASK <<
12721 				    (PORT_HW_CFG_MEDIA_TYPE_PHY1_SHIFT *
12722 				     actual_phy_idx))) == 0) {
12723 			media_types |= ((phy->media_type &
12724 					PORT_HW_CFG_MEDIA_TYPE_PHY0_MASK) <<
12725 				(PORT_HW_CFG_MEDIA_TYPE_PHY1_SHIFT *
12726 				 actual_phy_idx));
12727 		}
12728 		REG_WR(bp, sync_offset, media_types);
12729 
12730 		bnx2x_phy_def_cfg(params, phy, phy_index);
12731 		params->num_phys++;
12732 	}
12733 
12734 	DP(NETIF_MSG_LINK, "End phy probe. #phys found %x\n", params->num_phys);
12735 	return 0;
12736 }
12737 
12738 static void bnx2x_init_bmac_loopback(struct link_params *params,
12739 				     struct link_vars *vars)
12740 {
12741 	struct bnx2x *bp = params->bp;
12742 		vars->link_up = 1;
12743 		vars->line_speed = SPEED_10000;
12744 		vars->duplex = DUPLEX_FULL;
12745 		vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
12746 		vars->mac_type = MAC_TYPE_BMAC;
12747 
12748 		vars->phy_flags = PHY_XGXS_FLAG;
12749 
12750 		bnx2x_xgxs_deassert(params);
12751 
12752 		/* Set bmac loopback */
12753 		bnx2x_bmac_enable(params, vars, 1, 1);
12754 
12755 		REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
12756 }
12757 
12758 static void bnx2x_init_emac_loopback(struct link_params *params,
12759 				     struct link_vars *vars)
12760 {
12761 	struct bnx2x *bp = params->bp;
12762 		vars->link_up = 1;
12763 		vars->line_speed = SPEED_1000;
12764 		vars->duplex = DUPLEX_FULL;
12765 		vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
12766 		vars->mac_type = MAC_TYPE_EMAC;
12767 
12768 		vars->phy_flags = PHY_XGXS_FLAG;
12769 
12770 		bnx2x_xgxs_deassert(params);
12771 		/* Set bmac loopback */
12772 		bnx2x_emac_enable(params, vars, 1);
12773 		bnx2x_emac_program(params, vars);
12774 		REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
12775 }
12776 
12777 static void bnx2x_init_xmac_loopback(struct link_params *params,
12778 				     struct link_vars *vars)
12779 {
12780 	struct bnx2x *bp = params->bp;
12781 	vars->link_up = 1;
12782 	if (!params->req_line_speed[0])
12783 		vars->line_speed = SPEED_10000;
12784 	else
12785 		vars->line_speed = params->req_line_speed[0];
12786 	vars->duplex = DUPLEX_FULL;
12787 	vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
12788 	vars->mac_type = MAC_TYPE_XMAC;
12789 	vars->phy_flags = PHY_XGXS_FLAG;
12790 	/* Set WC to loopback mode since link is required to provide clock
12791 	 * to the XMAC in 20G mode
12792 	 */
12793 	bnx2x_set_aer_mmd(params, &params->phy[0]);
12794 	bnx2x_warpcore_reset_lane(bp, &params->phy[0], 0);
12795 	params->phy[INT_PHY].config_loopback(
12796 			&params->phy[INT_PHY],
12797 			params);
12798 
12799 	bnx2x_xmac_enable(params, vars, 1);
12800 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
12801 }
12802 
12803 static void bnx2x_init_umac_loopback(struct link_params *params,
12804 				     struct link_vars *vars)
12805 {
12806 	struct bnx2x *bp = params->bp;
12807 	vars->link_up = 1;
12808 	vars->line_speed = SPEED_1000;
12809 	vars->duplex = DUPLEX_FULL;
12810 	vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
12811 	vars->mac_type = MAC_TYPE_UMAC;
12812 	vars->phy_flags = PHY_XGXS_FLAG;
12813 	bnx2x_umac_enable(params, vars, 1);
12814 
12815 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
12816 }
12817 
12818 static void bnx2x_init_xgxs_loopback(struct link_params *params,
12819 				     struct link_vars *vars)
12820 {
12821 	struct bnx2x *bp = params->bp;
12822 	struct bnx2x_phy *int_phy = &params->phy[INT_PHY];
12823 	vars->link_up = 1;
12824 	vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
12825 	vars->duplex = DUPLEX_FULL;
12826 	if (params->req_line_speed[0] == SPEED_1000)
12827 		vars->line_speed = SPEED_1000;
12828 	else if ((params->req_line_speed[0] == SPEED_20000) ||
12829 		 (int_phy->flags & FLAGS_WC_DUAL_MODE))
12830 		vars->line_speed = SPEED_20000;
12831 	else
12832 		vars->line_speed = SPEED_10000;
12833 
12834 	if (!USES_WARPCORE(bp))
12835 		bnx2x_xgxs_deassert(params);
12836 	bnx2x_link_initialize(params, vars);
12837 
12838 	if (params->req_line_speed[0] == SPEED_1000) {
12839 		if (USES_WARPCORE(bp))
12840 			bnx2x_umac_enable(params, vars, 0);
12841 		else {
12842 			bnx2x_emac_program(params, vars);
12843 			bnx2x_emac_enable(params, vars, 0);
12844 		}
12845 	} else {
12846 		if (USES_WARPCORE(bp))
12847 			bnx2x_xmac_enable(params, vars, 0);
12848 		else
12849 			bnx2x_bmac_enable(params, vars, 0, 1);
12850 	}
12851 
12852 	if (params->loopback_mode == LOOPBACK_XGXS) {
12853 		/* Set 10G XGXS loopback */
12854 		int_phy->config_loopback(int_phy, params);
12855 	} else {
12856 		/* Set external phy loopback */
12857 		u8 phy_index;
12858 		for (phy_index = EXT_PHY1;
12859 		      phy_index < params->num_phys; phy_index++)
12860 			if (params->phy[phy_index].config_loopback)
12861 				params->phy[phy_index].config_loopback(
12862 					&params->phy[phy_index],
12863 					params);
12864 	}
12865 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
12866 
12867 	bnx2x_set_led(params, vars, LED_MODE_OPER, vars->line_speed);
12868 }
12869 
12870 void bnx2x_set_rx_filter(struct link_params *params, u8 en)
12871 {
12872 	struct bnx2x *bp = params->bp;
12873 	u8 val = en * 0x1F;
12874 
12875 	/* Open / close the gate between the NIG and the BRB */
12876 	if (!CHIP_IS_E1x(bp))
12877 		val |= en * 0x20;
12878 	REG_WR(bp, NIG_REG_LLH0_BRB1_DRV_MASK + params->port*4, val);
12879 
12880 	if (!CHIP_IS_E1(bp)) {
12881 		REG_WR(bp, NIG_REG_LLH0_BRB1_DRV_MASK_MF + params->port*4,
12882 		       en*0x3);
12883 	}
12884 
12885 	REG_WR(bp, (params->port ? NIG_REG_LLH1_BRB1_NOT_MCP :
12886 		    NIG_REG_LLH0_BRB1_NOT_MCP), en);
12887 }
12888 static int bnx2x_avoid_link_flap(struct link_params *params,
12889 					    struct link_vars *vars)
12890 {
12891 	u32 phy_idx;
12892 	u32 dont_clear_stat, lfa_sts;
12893 	struct bnx2x *bp = params->bp;
12894 
12895 	bnx2x_set_mdio_emac_per_phy(bp, params);
12896 	/* Sync the link parameters */
12897 	bnx2x_link_status_update(params, vars);
12898 
12899 	/*
12900 	 * The module verification was already done by previous link owner,
12901 	 * so this call is meant only to get warning message
12902 	 */
12903 
12904 	for (phy_idx = INT_PHY; phy_idx < params->num_phys; phy_idx++) {
12905 		struct bnx2x_phy *phy = &params->phy[phy_idx];
12906 		if (phy->phy_specific_func) {
12907 			DP(NETIF_MSG_LINK, "Calling PHY specific func\n");
12908 			phy->phy_specific_func(phy, params, PHY_INIT);
12909 		}
12910 		if ((phy->media_type == ETH_PHY_SFPP_10G_FIBER) ||
12911 		    (phy->media_type == ETH_PHY_SFP_1G_FIBER) ||
12912 		    (phy->media_type == ETH_PHY_DA_TWINAX))
12913 			bnx2x_verify_sfp_module(phy, params);
12914 	}
12915 	lfa_sts = REG_RD(bp, params->lfa_base +
12916 			 offsetof(struct shmem_lfa,
12917 				  lfa_sts));
12918 
12919 	dont_clear_stat = lfa_sts & SHMEM_LFA_DONT_CLEAR_STAT;
12920 
12921 	/* Re-enable the NIG/MAC */
12922 	if (CHIP_IS_E3(bp)) {
12923 		if (!dont_clear_stat) {
12924 			REG_WR(bp, GRCBASE_MISC +
12925 			       MISC_REGISTERS_RESET_REG_2_CLEAR,
12926 			       (MISC_REGISTERS_RESET_REG_2_MSTAT0 <<
12927 				params->port));
12928 			REG_WR(bp, GRCBASE_MISC +
12929 			       MISC_REGISTERS_RESET_REG_2_SET,
12930 			       (MISC_REGISTERS_RESET_REG_2_MSTAT0 <<
12931 				params->port));
12932 		}
12933 		if (vars->line_speed < SPEED_10000)
12934 			bnx2x_umac_enable(params, vars, 0);
12935 		else
12936 			bnx2x_xmac_enable(params, vars, 0);
12937 	} else {
12938 		if (vars->line_speed < SPEED_10000)
12939 			bnx2x_emac_enable(params, vars, 0);
12940 		else
12941 			bnx2x_bmac_enable(params, vars, 0, !dont_clear_stat);
12942 	}
12943 
12944 	/* Increment LFA count */
12945 	lfa_sts = ((lfa_sts & ~LINK_FLAP_AVOIDANCE_COUNT_MASK) |
12946 		   (((((lfa_sts & LINK_FLAP_AVOIDANCE_COUNT_MASK) >>
12947 		       LINK_FLAP_AVOIDANCE_COUNT_OFFSET) + 1) & 0xff)
12948 		    << LINK_FLAP_AVOIDANCE_COUNT_OFFSET));
12949 	/* Clear link flap reason */
12950 	lfa_sts &= ~LFA_LINK_FLAP_REASON_MASK;
12951 
12952 	REG_WR(bp, params->lfa_base +
12953 	       offsetof(struct shmem_lfa, lfa_sts), lfa_sts);
12954 
12955 	/* Disable NIG DRAIN */
12956 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
12957 
12958 	/* Enable interrupts */
12959 	bnx2x_link_int_enable(params);
12960 	return 0;
12961 }
12962 
12963 static void bnx2x_cannot_avoid_link_flap(struct link_params *params,
12964 					 struct link_vars *vars,
12965 					 int lfa_status)
12966 {
12967 	u32 lfa_sts, cfg_idx, tmp_val;
12968 	struct bnx2x *bp = params->bp;
12969 
12970 	bnx2x_link_reset(params, vars, 1);
12971 
12972 	if (!params->lfa_base)
12973 		return;
12974 	/* Store the new link parameters */
12975 	REG_WR(bp, params->lfa_base +
12976 	       offsetof(struct shmem_lfa, req_duplex),
12977 	       params->req_duplex[0] | (params->req_duplex[1] << 16));
12978 
12979 	REG_WR(bp, params->lfa_base +
12980 	       offsetof(struct shmem_lfa, req_flow_ctrl),
12981 	       params->req_flow_ctrl[0] | (params->req_flow_ctrl[1] << 16));
12982 
12983 	REG_WR(bp, params->lfa_base +
12984 	       offsetof(struct shmem_lfa, req_line_speed),
12985 	       params->req_line_speed[0] | (params->req_line_speed[1] << 16));
12986 
12987 	for (cfg_idx = 0; cfg_idx < SHMEM_LINK_CONFIG_SIZE; cfg_idx++) {
12988 		REG_WR(bp, params->lfa_base +
12989 		       offsetof(struct shmem_lfa,
12990 				speed_cap_mask[cfg_idx]),
12991 		       params->speed_cap_mask[cfg_idx]);
12992 	}
12993 
12994 	tmp_val = REG_RD(bp, params->lfa_base +
12995 			 offsetof(struct shmem_lfa, additional_config));
12996 	tmp_val &= ~REQ_FC_AUTO_ADV_MASK;
12997 	tmp_val |= params->req_fc_auto_adv;
12998 
12999 	REG_WR(bp, params->lfa_base +
13000 	       offsetof(struct shmem_lfa, additional_config), tmp_val);
13001 
13002 	lfa_sts = REG_RD(bp, params->lfa_base +
13003 			 offsetof(struct shmem_lfa, lfa_sts));
13004 
13005 	/* Clear the "Don't Clear Statistics" bit, and set reason */
13006 	lfa_sts &= ~SHMEM_LFA_DONT_CLEAR_STAT;
13007 
13008 	/* Set link flap reason */
13009 	lfa_sts &= ~LFA_LINK_FLAP_REASON_MASK;
13010 	lfa_sts |= ((lfa_status & LFA_LINK_FLAP_REASON_MASK) <<
13011 		    LFA_LINK_FLAP_REASON_OFFSET);
13012 
13013 	/* Increment link flap counter */
13014 	lfa_sts = ((lfa_sts & ~LINK_FLAP_COUNT_MASK) |
13015 		   (((((lfa_sts & LINK_FLAP_COUNT_MASK) >>
13016 		       LINK_FLAP_COUNT_OFFSET) + 1) & 0xff)
13017 		    << LINK_FLAP_COUNT_OFFSET));
13018 	REG_WR(bp, params->lfa_base +
13019 	       offsetof(struct shmem_lfa, lfa_sts), lfa_sts);
13020 	/* Proceed with regular link initialization */
13021 }
13022 
13023 int bnx2x_phy_init(struct link_params *params, struct link_vars *vars)
13024 {
13025 	int lfa_status;
13026 	struct bnx2x *bp = params->bp;
13027 	DP(NETIF_MSG_LINK, "Phy Initialization started\n");
13028 	DP(NETIF_MSG_LINK, "(1) req_speed %d, req_flowctrl %d\n",
13029 		   params->req_line_speed[0], params->req_flow_ctrl[0]);
13030 	DP(NETIF_MSG_LINK, "(2) req_speed %d, req_flowctrl %d\n",
13031 		   params->req_line_speed[1], params->req_flow_ctrl[1]);
13032 	DP(NETIF_MSG_LINK, "req_adv_flow_ctrl 0x%x\n", params->req_fc_auto_adv);
13033 	vars->link_status = 0;
13034 	vars->phy_link_up = 0;
13035 	vars->link_up = 0;
13036 	vars->line_speed = 0;
13037 	vars->duplex = DUPLEX_FULL;
13038 	vars->flow_ctrl = BNX2X_FLOW_CTRL_NONE;
13039 	vars->mac_type = MAC_TYPE_NONE;
13040 	vars->phy_flags = 0;
13041 	vars->check_kr2_recovery_cnt = 0;
13042 	params->link_flags = PHY_INITIALIZED;
13043 	/* Driver opens NIG-BRB filters */
13044 	bnx2x_set_rx_filter(params, 1);
13045 	bnx2x_chng_link_count(params, true);
13046 	/* Check if link flap can be avoided */
13047 	lfa_status = bnx2x_check_lfa(params);
13048 
13049 	if (lfa_status == 0) {
13050 		DP(NETIF_MSG_LINK, "Link Flap Avoidance in progress\n");
13051 		return bnx2x_avoid_link_flap(params, vars);
13052 	}
13053 
13054 	DP(NETIF_MSG_LINK, "Cannot avoid link flap lfa_sta=0x%x\n",
13055 		       lfa_status);
13056 	bnx2x_cannot_avoid_link_flap(params, vars, lfa_status);
13057 
13058 	/* Disable attentions */
13059 	bnx2x_bits_dis(bp, NIG_REG_MASK_INTERRUPT_PORT0 + params->port*4,
13060 		       (NIG_MASK_XGXS0_LINK_STATUS |
13061 			NIG_MASK_XGXS0_LINK10G |
13062 			NIG_MASK_SERDES0_LINK_STATUS |
13063 			NIG_MASK_MI_INT));
13064 
13065 	bnx2x_emac_init(params, vars);
13066 
13067 	if (params->feature_config_flags & FEATURE_CONFIG_PFC_ENABLED)
13068 		vars->link_status |= LINK_STATUS_PFC_ENABLED;
13069 
13070 	if (params->num_phys == 0) {
13071 		DP(NETIF_MSG_LINK, "No phy found for initialization !!\n");
13072 		return -EINVAL;
13073 	}
13074 	set_phy_vars(params, vars);
13075 
13076 	DP(NETIF_MSG_LINK, "Num of phys on board: %d\n", params->num_phys);
13077 	switch (params->loopback_mode) {
13078 	case LOOPBACK_BMAC:
13079 		bnx2x_init_bmac_loopback(params, vars);
13080 		break;
13081 	case LOOPBACK_EMAC:
13082 		bnx2x_init_emac_loopback(params, vars);
13083 		break;
13084 	case LOOPBACK_XMAC:
13085 		bnx2x_init_xmac_loopback(params, vars);
13086 		break;
13087 	case LOOPBACK_UMAC:
13088 		bnx2x_init_umac_loopback(params, vars);
13089 		break;
13090 	case LOOPBACK_XGXS:
13091 	case LOOPBACK_EXT_PHY:
13092 		bnx2x_init_xgxs_loopback(params, vars);
13093 		break;
13094 	default:
13095 		if (!CHIP_IS_E3(bp)) {
13096 			if (params->switch_cfg == SWITCH_CFG_10G)
13097 				bnx2x_xgxs_deassert(params);
13098 			else
13099 				bnx2x_serdes_deassert(bp, params->port);
13100 		}
13101 		bnx2x_link_initialize(params, vars);
13102 		msleep(30);
13103 		bnx2x_link_int_enable(params);
13104 		break;
13105 	}
13106 	bnx2x_update_mng(params, vars->link_status);
13107 
13108 	bnx2x_update_mng_eee(params, vars->eee_status);
13109 	return 0;
13110 }
13111 
13112 int bnx2x_link_reset(struct link_params *params, struct link_vars *vars,
13113 		     u8 reset_ext_phy)
13114 {
13115 	struct bnx2x *bp = params->bp;
13116 	u8 phy_index, port = params->port, clear_latch_ind = 0;
13117 	DP(NETIF_MSG_LINK, "Resetting the link of port %d\n", port);
13118 	/* Disable attentions */
13119 	vars->link_status = 0;
13120 	bnx2x_chng_link_count(params, true);
13121 	bnx2x_update_mng(params, vars->link_status);
13122 	vars->eee_status &= ~(SHMEM_EEE_LP_ADV_STATUS_MASK |
13123 			      SHMEM_EEE_ACTIVE_BIT);
13124 	bnx2x_update_mng_eee(params, vars->eee_status);
13125 	bnx2x_bits_dis(bp, NIG_REG_MASK_INTERRUPT_PORT0 + port*4,
13126 		       (NIG_MASK_XGXS0_LINK_STATUS |
13127 			NIG_MASK_XGXS0_LINK10G |
13128 			NIG_MASK_SERDES0_LINK_STATUS |
13129 			NIG_MASK_MI_INT));
13130 
13131 	/* Activate nig drain */
13132 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + port*4, 1);
13133 
13134 	/* Disable nig egress interface */
13135 	if (!CHIP_IS_E3(bp)) {
13136 		REG_WR(bp, NIG_REG_BMAC0_OUT_EN + port*4, 0);
13137 		REG_WR(bp, NIG_REG_EGRESS_EMAC0_OUT_EN + port*4, 0);
13138 	}
13139 
13140 		if (!CHIP_IS_E3(bp)) {
13141 			bnx2x_set_bmac_rx(bp, params->chip_id, port, 0);
13142 		} else {
13143 			bnx2x_set_xmac_rxtx(params, 0);
13144 			bnx2x_set_umac_rxtx(params, 0);
13145 		}
13146 	/* Disable emac */
13147 	if (!CHIP_IS_E3(bp))
13148 		REG_WR(bp, NIG_REG_NIG_EMAC0_EN + port*4, 0);
13149 
13150 	usleep_range(10000, 20000);
13151 	/* The PHY reset is controlled by GPIO 1
13152 	 * Hold it as vars low
13153 	 */
13154 	 /* Clear link led */
13155 	bnx2x_set_mdio_emac_per_phy(bp, params);
13156 	bnx2x_set_led(params, vars, LED_MODE_OFF, 0);
13157 
13158 	if (reset_ext_phy) {
13159 		for (phy_index = EXT_PHY1; phy_index < params->num_phys;
13160 		      phy_index++) {
13161 			if (params->phy[phy_index].link_reset) {
13162 				bnx2x_set_aer_mmd(params,
13163 						  &params->phy[phy_index]);
13164 				params->phy[phy_index].link_reset(
13165 					&params->phy[phy_index],
13166 					params);
13167 			}
13168 			if (params->phy[phy_index].flags &
13169 			    FLAGS_REARM_LATCH_SIGNAL)
13170 				clear_latch_ind = 1;
13171 		}
13172 	}
13173 
13174 	if (clear_latch_ind) {
13175 		/* Clear latching indication */
13176 		bnx2x_rearm_latch_signal(bp, port, 0);
13177 		bnx2x_bits_dis(bp, NIG_REG_LATCH_BC_0 + port*4,
13178 			       1 << NIG_LATCH_BC_ENABLE_MI_INT);
13179 	}
13180 	if (params->phy[INT_PHY].link_reset)
13181 		params->phy[INT_PHY].link_reset(
13182 			&params->phy[INT_PHY], params);
13183 
13184 	/* Disable nig ingress interface */
13185 	if (!CHIP_IS_E3(bp)) {
13186 		/* Reset BigMac */
13187 		REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
13188 		       (MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port));
13189 		REG_WR(bp, NIG_REG_BMAC0_IN_EN + port*4, 0);
13190 		REG_WR(bp, NIG_REG_EMAC0_IN_EN + port*4, 0);
13191 	} else {
13192 		u32 xmac_base = (params->port) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
13193 		bnx2x_set_xumac_nig(params, 0, 0);
13194 		if (REG_RD(bp, MISC_REG_RESET_REG_2) &
13195 		    MISC_REGISTERS_RESET_REG_2_XMAC)
13196 			REG_WR(bp, xmac_base + XMAC_REG_CTRL,
13197 			       XMAC_CTRL_REG_SOFT_RESET);
13198 	}
13199 	vars->link_up = 0;
13200 	vars->phy_flags = 0;
13201 	return 0;
13202 }
13203 int bnx2x_lfa_reset(struct link_params *params,
13204 			       struct link_vars *vars)
13205 {
13206 	struct bnx2x *bp = params->bp;
13207 	vars->link_up = 0;
13208 	vars->phy_flags = 0;
13209 	params->link_flags &= ~PHY_INITIALIZED;
13210 	if (!params->lfa_base)
13211 		return bnx2x_link_reset(params, vars, 1);
13212 	/*
13213 	 * Activate NIG drain so that during this time the device won't send
13214 	 * anything while it is unable to response.
13215 	 */
13216 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 1);
13217 
13218 	/*
13219 	 * Close gracefully the gate from BMAC to NIG such that no half packets
13220 	 * are passed.
13221 	 */
13222 	if (!CHIP_IS_E3(bp))
13223 		bnx2x_set_bmac_rx(bp, params->chip_id, params->port, 0);
13224 
13225 	if (CHIP_IS_E3(bp)) {
13226 		bnx2x_set_xmac_rxtx(params, 0);
13227 		bnx2x_set_umac_rxtx(params, 0);
13228 	}
13229 	/* Wait 10ms for the pipe to clean up*/
13230 	usleep_range(10000, 20000);
13231 
13232 	/* Clean the NIG-BRB using the network filters in a way that will
13233 	 * not cut a packet in the middle.
13234 	 */
13235 	bnx2x_set_rx_filter(params, 0);
13236 
13237 	/*
13238 	 * Re-open the gate between the BMAC and the NIG, after verifying the
13239 	 * gate to the BRB is closed, otherwise packets may arrive to the
13240 	 * firmware before driver had initialized it. The target is to achieve
13241 	 * minimum management protocol down time.
13242 	 */
13243 	if (!CHIP_IS_E3(bp))
13244 		bnx2x_set_bmac_rx(bp, params->chip_id, params->port, 1);
13245 
13246 	if (CHIP_IS_E3(bp)) {
13247 		bnx2x_set_xmac_rxtx(params, 1);
13248 		bnx2x_set_umac_rxtx(params, 1);
13249 	}
13250 	/* Disable NIG drain */
13251 	REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
13252 	return 0;
13253 }
13254 
13255 /****************************************************************************/
13256 /*				Common function				    */
13257 /****************************************************************************/
13258 static int bnx2x_8073_common_init_phy(struct bnx2x *bp,
13259 				      u32 shmem_base_path[],
13260 				      u32 shmem2_base_path[], u8 phy_index,
13261 				      u32 chip_id)
13262 {
13263 	struct bnx2x_phy phy[PORT_MAX];
13264 	struct bnx2x_phy *phy_blk[PORT_MAX];
13265 	u16 val;
13266 	s8 port = 0;
13267 	s8 port_of_path = 0;
13268 	u32 swap_val, swap_override;
13269 	swap_val = REG_RD(bp,  NIG_REG_PORT_SWAP);
13270 	swap_override = REG_RD(bp,  NIG_REG_STRAP_OVERRIDE);
13271 	port ^= (swap_val && swap_override);
13272 	bnx2x_ext_phy_hw_reset(bp, port);
13273 	/* PART1 - Reset both phys */
13274 	for (port = PORT_MAX - 1; port >= PORT_0; port--) {
13275 		u32 shmem_base, shmem2_base;
13276 		/* In E2, same phy is using for port0 of the two paths */
13277 		if (CHIP_IS_E1x(bp)) {
13278 			shmem_base = shmem_base_path[0];
13279 			shmem2_base = shmem2_base_path[0];
13280 			port_of_path = port;
13281 		} else {
13282 			shmem_base = shmem_base_path[port];
13283 			shmem2_base = shmem2_base_path[port];
13284 			port_of_path = 0;
13285 		}
13286 
13287 		/* Extract the ext phy address for the port */
13288 		if (bnx2x_populate_phy(bp, phy_index, shmem_base, shmem2_base,
13289 				       port_of_path, &phy[port]) !=
13290 		    0) {
13291 			DP(NETIF_MSG_LINK, "populate_phy failed\n");
13292 			return -EINVAL;
13293 		}
13294 		/* Disable attentions */
13295 		bnx2x_bits_dis(bp, NIG_REG_MASK_INTERRUPT_PORT0 +
13296 			       port_of_path*4,
13297 			       (NIG_MASK_XGXS0_LINK_STATUS |
13298 				NIG_MASK_XGXS0_LINK10G |
13299 				NIG_MASK_SERDES0_LINK_STATUS |
13300 				NIG_MASK_MI_INT));
13301 
13302 		/* Need to take the phy out of low power mode in order
13303 		 * to write to access its registers
13304 		 */
13305 		bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
13306 			       MISC_REGISTERS_GPIO_OUTPUT_HIGH,
13307 			       port);
13308 
13309 		/* Reset the phy */
13310 		bnx2x_cl45_write(bp, &phy[port],
13311 				 MDIO_PMA_DEVAD,
13312 				 MDIO_PMA_REG_CTRL,
13313 				 1<<15);
13314 	}
13315 
13316 	/* Add delay of 150ms after reset */
13317 	msleep(150);
13318 
13319 	if (phy[PORT_0].addr & 0x1) {
13320 		phy_blk[PORT_0] = &(phy[PORT_1]);
13321 		phy_blk[PORT_1] = &(phy[PORT_0]);
13322 	} else {
13323 		phy_blk[PORT_0] = &(phy[PORT_0]);
13324 		phy_blk[PORT_1] = &(phy[PORT_1]);
13325 	}
13326 
13327 	/* PART2 - Download firmware to both phys */
13328 	for (port = PORT_MAX - 1; port >= PORT_0; port--) {
13329 		if (CHIP_IS_E1x(bp))
13330 			port_of_path = port;
13331 		else
13332 			port_of_path = 0;
13333 
13334 		DP(NETIF_MSG_LINK, "Loading spirom for phy address 0x%x\n",
13335 			   phy_blk[port]->addr);
13336 		if (bnx2x_8073_8727_external_rom_boot(bp, phy_blk[port],
13337 						      port_of_path))
13338 			return -EINVAL;
13339 
13340 		/* Only set bit 10 = 1 (Tx power down) */
13341 		bnx2x_cl45_read(bp, phy_blk[port],
13342 				MDIO_PMA_DEVAD,
13343 				MDIO_PMA_REG_TX_POWER_DOWN, &val);
13344 
13345 		/* Phase1 of TX_POWER_DOWN reset */
13346 		bnx2x_cl45_write(bp, phy_blk[port],
13347 				 MDIO_PMA_DEVAD,
13348 				 MDIO_PMA_REG_TX_POWER_DOWN,
13349 				 (val | 1<<10));
13350 	}
13351 
13352 	/* Toggle Transmitter: Power down and then up with 600ms delay
13353 	 * between
13354 	 */
13355 	msleep(600);
13356 
13357 	/* PART3 - complete TX_POWER_DOWN process, and set GPIO2 back to low */
13358 	for (port = PORT_MAX - 1; port >= PORT_0; port--) {
13359 		/* Phase2 of POWER_DOWN_RESET */
13360 		/* Release bit 10 (Release Tx power down) */
13361 		bnx2x_cl45_read(bp, phy_blk[port],
13362 				MDIO_PMA_DEVAD,
13363 				MDIO_PMA_REG_TX_POWER_DOWN, &val);
13364 
13365 		bnx2x_cl45_write(bp, phy_blk[port],
13366 				MDIO_PMA_DEVAD,
13367 				MDIO_PMA_REG_TX_POWER_DOWN, (val & (~(1<<10))));
13368 		usleep_range(15000, 30000);
13369 
13370 		/* Read modify write the SPI-ROM version select register */
13371 		bnx2x_cl45_read(bp, phy_blk[port],
13372 				MDIO_PMA_DEVAD,
13373 				MDIO_PMA_REG_EDC_FFE_MAIN, &val);
13374 		bnx2x_cl45_write(bp, phy_blk[port],
13375 				 MDIO_PMA_DEVAD,
13376 				 MDIO_PMA_REG_EDC_FFE_MAIN, (val | (1<<12)));
13377 
13378 		/* set GPIO2 back to LOW */
13379 		bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_2,
13380 			       MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
13381 	}
13382 	return 0;
13383 }
13384 static int bnx2x_8726_common_init_phy(struct bnx2x *bp,
13385 				      u32 shmem_base_path[],
13386 				      u32 shmem2_base_path[], u8 phy_index,
13387 				      u32 chip_id)
13388 {
13389 	u32 val;
13390 	s8 port;
13391 	struct bnx2x_phy phy;
13392 	/* Use port1 because of the static port-swap */
13393 	/* Enable the module detection interrupt */
13394 	val = REG_RD(bp, MISC_REG_GPIO_EVENT_EN);
13395 	val |= ((1<<MISC_REGISTERS_GPIO_3)|
13396 		(1<<(MISC_REGISTERS_GPIO_3 + MISC_REGISTERS_GPIO_PORT_SHIFT)));
13397 	REG_WR(bp, MISC_REG_GPIO_EVENT_EN, val);
13398 
13399 	bnx2x_ext_phy_hw_reset(bp, 0);
13400 	usleep_range(5000, 10000);
13401 	for (port = 0; port < PORT_MAX; port++) {
13402 		u32 shmem_base, shmem2_base;
13403 
13404 		/* In E2, same phy is using for port0 of the two paths */
13405 		if (CHIP_IS_E1x(bp)) {
13406 			shmem_base = shmem_base_path[0];
13407 			shmem2_base = shmem2_base_path[0];
13408 		} else {
13409 			shmem_base = shmem_base_path[port];
13410 			shmem2_base = shmem2_base_path[port];
13411 		}
13412 		/* Extract the ext phy address for the port */
13413 		if (bnx2x_populate_phy(bp, phy_index, shmem_base, shmem2_base,
13414 				       port, &phy) !=
13415 		    0) {
13416 			DP(NETIF_MSG_LINK, "populate phy failed\n");
13417 			return -EINVAL;
13418 		}
13419 
13420 		/* Reset phy*/
13421 		bnx2x_cl45_write(bp, &phy,
13422 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_GEN_CTRL, 0x0001);
13423 
13424 
13425 		/* Set fault module detected LED on */
13426 		bnx2x_set_gpio(bp, MISC_REGISTERS_GPIO_0,
13427 			       MISC_REGISTERS_GPIO_HIGH,
13428 			       port);
13429 	}
13430 
13431 	return 0;
13432 }
13433 static void bnx2x_get_ext_phy_reset_gpio(struct bnx2x *bp, u32 shmem_base,
13434 					 u8 *io_gpio, u8 *io_port)
13435 {
13436 
13437 	u32 phy_gpio_reset = REG_RD(bp, shmem_base +
13438 					  offsetof(struct shmem_region,
13439 				dev_info.port_hw_config[PORT_0].default_cfg));
13440 	switch (phy_gpio_reset) {
13441 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO0_P0:
13442 		*io_gpio = 0;
13443 		*io_port = 0;
13444 		break;
13445 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO1_P0:
13446 		*io_gpio = 1;
13447 		*io_port = 0;
13448 		break;
13449 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO2_P0:
13450 		*io_gpio = 2;
13451 		*io_port = 0;
13452 		break;
13453 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO3_P0:
13454 		*io_gpio = 3;
13455 		*io_port = 0;
13456 		break;
13457 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO0_P1:
13458 		*io_gpio = 0;
13459 		*io_port = 1;
13460 		break;
13461 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO1_P1:
13462 		*io_gpio = 1;
13463 		*io_port = 1;
13464 		break;
13465 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO2_P1:
13466 		*io_gpio = 2;
13467 		*io_port = 1;
13468 		break;
13469 	case PORT_HW_CFG_EXT_PHY_GPIO_RST_GPIO3_P1:
13470 		*io_gpio = 3;
13471 		*io_port = 1;
13472 		break;
13473 	default:
13474 		/* Don't override the io_gpio and io_port */
13475 		break;
13476 	}
13477 }
13478 
13479 static int bnx2x_8727_common_init_phy(struct bnx2x *bp,
13480 				      u32 shmem_base_path[],
13481 				      u32 shmem2_base_path[], u8 phy_index,
13482 				      u32 chip_id)
13483 {
13484 	s8 port, reset_gpio;
13485 	u32 swap_val, swap_override;
13486 	struct bnx2x_phy phy[PORT_MAX];
13487 	struct bnx2x_phy *phy_blk[PORT_MAX];
13488 	s8 port_of_path;
13489 	swap_val = REG_RD(bp, NIG_REG_PORT_SWAP);
13490 	swap_override = REG_RD(bp, NIG_REG_STRAP_OVERRIDE);
13491 
13492 	reset_gpio = MISC_REGISTERS_GPIO_1;
13493 	port = 1;
13494 
13495 	/* Retrieve the reset gpio/port which control the reset.
13496 	 * Default is GPIO1, PORT1
13497 	 */
13498 	bnx2x_get_ext_phy_reset_gpio(bp, shmem_base_path[0],
13499 				     (u8 *)&reset_gpio, (u8 *)&port);
13500 
13501 	/* Calculate the port based on port swap */
13502 	port ^= (swap_val && swap_override);
13503 
13504 	/* Initiate PHY reset*/
13505 	bnx2x_set_gpio(bp, reset_gpio, MISC_REGISTERS_GPIO_OUTPUT_LOW,
13506 		       port);
13507 	usleep_range(1000, 2000);
13508 	bnx2x_set_gpio(bp, reset_gpio, MISC_REGISTERS_GPIO_OUTPUT_HIGH,
13509 		       port);
13510 
13511 	usleep_range(5000, 10000);
13512 
13513 	/* PART1 - Reset both phys */
13514 	for (port = PORT_MAX - 1; port >= PORT_0; port--) {
13515 		u32 shmem_base, shmem2_base;
13516 
13517 		/* In E2, same phy is using for port0 of the two paths */
13518 		if (CHIP_IS_E1x(bp)) {
13519 			shmem_base = shmem_base_path[0];
13520 			shmem2_base = shmem2_base_path[0];
13521 			port_of_path = port;
13522 		} else {
13523 			shmem_base = shmem_base_path[port];
13524 			shmem2_base = shmem2_base_path[port];
13525 			port_of_path = 0;
13526 		}
13527 
13528 		/* Extract the ext phy address for the port */
13529 		if (bnx2x_populate_phy(bp, phy_index, shmem_base, shmem2_base,
13530 				       port_of_path, &phy[port]) !=
13531 				       0) {
13532 			DP(NETIF_MSG_LINK, "populate phy failed\n");
13533 			return -EINVAL;
13534 		}
13535 		/* disable attentions */
13536 		bnx2x_bits_dis(bp, NIG_REG_MASK_INTERRUPT_PORT0 +
13537 			       port_of_path*4,
13538 			       (NIG_MASK_XGXS0_LINK_STATUS |
13539 				NIG_MASK_XGXS0_LINK10G |
13540 				NIG_MASK_SERDES0_LINK_STATUS |
13541 				NIG_MASK_MI_INT));
13542 
13543 
13544 		/* Reset the phy */
13545 		bnx2x_cl45_write(bp, &phy[port],
13546 				 MDIO_PMA_DEVAD, MDIO_PMA_REG_CTRL, 1<<15);
13547 	}
13548 
13549 	/* Add delay of 150ms after reset */
13550 	msleep(150);
13551 	if (phy[PORT_0].addr & 0x1) {
13552 		phy_blk[PORT_0] = &(phy[PORT_1]);
13553 		phy_blk[PORT_1] = &(phy[PORT_0]);
13554 	} else {
13555 		phy_blk[PORT_0] = &(phy[PORT_0]);
13556 		phy_blk[PORT_1] = &(phy[PORT_1]);
13557 	}
13558 	/* PART2 - Download firmware to both phys */
13559 	for (port = PORT_MAX - 1; port >= PORT_0; port--) {
13560 		if (CHIP_IS_E1x(bp))
13561 			port_of_path = port;
13562 		else
13563 			port_of_path = 0;
13564 		DP(NETIF_MSG_LINK, "Loading spirom for phy address 0x%x\n",
13565 			   phy_blk[port]->addr);
13566 		if (bnx2x_8073_8727_external_rom_boot(bp, phy_blk[port],
13567 						      port_of_path))
13568 			return -EINVAL;
13569 		/* Disable PHY transmitter output */
13570 		bnx2x_cl45_write(bp, phy_blk[port],
13571 				 MDIO_PMA_DEVAD,
13572 				 MDIO_PMA_REG_TX_DISABLE, 1);
13573 
13574 	}
13575 	return 0;
13576 }
13577 
13578 static int bnx2x_84833_common_init_phy(struct bnx2x *bp,
13579 						u32 shmem_base_path[],
13580 						u32 shmem2_base_path[],
13581 						u8 phy_index,
13582 						u32 chip_id)
13583 {
13584 	u8 reset_gpios;
13585 	reset_gpios = bnx2x_84833_get_reset_gpios(bp, shmem_base_path, chip_id);
13586 	bnx2x_set_mult_gpio(bp, reset_gpios, MISC_REGISTERS_GPIO_OUTPUT_LOW);
13587 	udelay(10);
13588 	bnx2x_set_mult_gpio(bp, reset_gpios, MISC_REGISTERS_GPIO_OUTPUT_HIGH);
13589 	DP(NETIF_MSG_LINK, "84833 reset pulse on pin values 0x%x\n",
13590 		reset_gpios);
13591 	return 0;
13592 }
13593 
13594 static int bnx2x_ext_phy_common_init(struct bnx2x *bp, u32 shmem_base_path[],
13595 				     u32 shmem2_base_path[], u8 phy_index,
13596 				     u32 ext_phy_type, u32 chip_id)
13597 {
13598 	int rc = 0;
13599 
13600 	switch (ext_phy_type) {
13601 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
13602 		rc = bnx2x_8073_common_init_phy(bp, shmem_base_path,
13603 						shmem2_base_path,
13604 						phy_index, chip_id);
13605 		break;
13606 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8722:
13607 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
13608 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727_NOC:
13609 		rc = bnx2x_8727_common_init_phy(bp, shmem_base_path,
13610 						shmem2_base_path,
13611 						phy_index, chip_id);
13612 		break;
13613 
13614 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
13615 		/* GPIO1 affects both ports, so there's need to pull
13616 		 * it for single port alone
13617 		 */
13618 		rc = bnx2x_8726_common_init_phy(bp, shmem_base_path,
13619 						shmem2_base_path,
13620 						phy_index, chip_id);
13621 		break;
13622 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833:
13623 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84834:
13624 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84858:
13625 		/* GPIO3's are linked, and so both need to be toggled
13626 		 * to obtain required 2us pulse.
13627 		 */
13628 		rc = bnx2x_84833_common_init_phy(bp, shmem_base_path,
13629 						shmem2_base_path,
13630 						phy_index, chip_id);
13631 		break;
13632 	case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE:
13633 		rc = -EINVAL;
13634 		break;
13635 	default:
13636 		DP(NETIF_MSG_LINK,
13637 			   "ext_phy 0x%x common init not required\n",
13638 			   ext_phy_type);
13639 		break;
13640 	}
13641 
13642 	if (rc)
13643 		netdev_err(bp->dev,  "Warning: PHY was not initialized,"
13644 				      " Port %d\n",
13645 			 0);
13646 	return rc;
13647 }
13648 
13649 int bnx2x_common_init_phy(struct bnx2x *bp, u32 shmem_base_path[],
13650 			  u32 shmem2_base_path[], u32 chip_id)
13651 {
13652 	int rc = 0;
13653 	u32 phy_ver, val;
13654 	u8 phy_index = 0;
13655 	u32 ext_phy_type, ext_phy_config;
13656 
13657 	bnx2x_set_mdio_clk(bp, chip_id, GRCBASE_EMAC0);
13658 	bnx2x_set_mdio_clk(bp, chip_id, GRCBASE_EMAC1);
13659 	DP(NETIF_MSG_LINK, "Begin common phy init\n");
13660 	if (CHIP_IS_E3(bp)) {
13661 		/* Enable EPIO */
13662 		val = REG_RD(bp, MISC_REG_GEN_PURP_HWG);
13663 		REG_WR(bp, MISC_REG_GEN_PURP_HWG, val | 1);
13664 	}
13665 	/* Check if common init was already done */
13666 	phy_ver = REG_RD(bp, shmem_base_path[0] +
13667 			 offsetof(struct shmem_region,
13668 				  port_mb[PORT_0].ext_phy_fw_version));
13669 	if (phy_ver) {
13670 		DP(NETIF_MSG_LINK, "Not doing common init; phy ver is 0x%x\n",
13671 			       phy_ver);
13672 		return 0;
13673 	}
13674 
13675 	/* Read the ext_phy_type for arbitrary port(0) */
13676 	for (phy_index = EXT_PHY1; phy_index < MAX_PHYS;
13677 	      phy_index++) {
13678 		ext_phy_config = bnx2x_get_ext_phy_config(bp,
13679 							  shmem_base_path[0],
13680 							  phy_index, 0);
13681 		ext_phy_type = XGXS_EXT_PHY_TYPE(ext_phy_config);
13682 		rc |= bnx2x_ext_phy_common_init(bp, shmem_base_path,
13683 						shmem2_base_path,
13684 						phy_index, ext_phy_type,
13685 						chip_id);
13686 	}
13687 	return rc;
13688 }
13689 
13690 static void bnx2x_check_over_curr(struct link_params *params,
13691 				  struct link_vars *vars)
13692 {
13693 	struct bnx2x *bp = params->bp;
13694 	u32 cfg_pin;
13695 	u8 port = params->port;
13696 	u32 pin_val;
13697 
13698 	cfg_pin = (REG_RD(bp, params->shmem_base +
13699 			  offsetof(struct shmem_region,
13700 			       dev_info.port_hw_config[port].e3_cmn_pin_cfg1)) &
13701 		   PORT_HW_CFG_E3_OVER_CURRENT_MASK) >>
13702 		PORT_HW_CFG_E3_OVER_CURRENT_SHIFT;
13703 
13704 	/* Ignore check if no external input PIN available */
13705 	if (bnx2x_get_cfg_pin(bp, cfg_pin, &pin_val) != 0)
13706 		return;
13707 
13708 	if (!pin_val) {
13709 		if ((vars->phy_flags & PHY_OVER_CURRENT_FLAG) == 0) {
13710 			netdev_err(bp->dev, "Error:  Power fault on Port %d has"
13711 					    " been detected and the power to "
13712 					    "that SFP+ module has been removed"
13713 					    " to prevent failure of the card."
13714 					    " Please remove the SFP+ module and"
13715 					    " restart the system to clear this"
13716 					    " error.\n",
13717 			 params->port);
13718 			vars->phy_flags |= PHY_OVER_CURRENT_FLAG;
13719 			bnx2x_warpcore_power_module(params, 0);
13720 		}
13721 	} else
13722 		vars->phy_flags &= ~PHY_OVER_CURRENT_FLAG;
13723 }
13724 
13725 /* Returns 0 if no change occurred since last check; 1 otherwise. */
13726 static u8 bnx2x_analyze_link_error(struct link_params *params,
13727 				    struct link_vars *vars, u32 status,
13728 				    u32 phy_flag, u32 link_flag, u8 notify)
13729 {
13730 	struct bnx2x *bp = params->bp;
13731 	/* Compare new value with previous value */
13732 	u8 led_mode;
13733 	u32 old_status = (vars->phy_flags & phy_flag) ? 1 : 0;
13734 
13735 	if ((status ^ old_status) == 0)
13736 		return 0;
13737 
13738 	/* If values differ */
13739 	switch (phy_flag) {
13740 	case PHY_HALF_OPEN_CONN_FLAG:
13741 		DP(NETIF_MSG_LINK, "Analyze Remote Fault\n");
13742 		break;
13743 	case PHY_SFP_TX_FAULT_FLAG:
13744 		DP(NETIF_MSG_LINK, "Analyze TX Fault\n");
13745 		break;
13746 	default:
13747 		DP(NETIF_MSG_LINK, "Analyze UNKNOWN\n");
13748 	}
13749 	DP(NETIF_MSG_LINK, "Link changed:[%x %x]->%x\n", vars->link_up,
13750 	   old_status, status);
13751 
13752 	/* Do not touch the link in case physical link down */
13753 	if ((vars->phy_flags & PHY_PHYSICAL_LINK_FLAG) == 0)
13754 		return 1;
13755 
13756 	/* a. Update shmem->link_status accordingly
13757 	 * b. Update link_vars->link_up
13758 	 */
13759 	if (status) {
13760 		vars->link_status &= ~LINK_STATUS_LINK_UP;
13761 		vars->link_status |= link_flag;
13762 		vars->link_up = 0;
13763 		vars->phy_flags |= phy_flag;
13764 
13765 		/* activate nig drain */
13766 		REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 1);
13767 		/* Set LED mode to off since the PHY doesn't know about these
13768 		 * errors
13769 		 */
13770 		led_mode = LED_MODE_OFF;
13771 	} else {
13772 		vars->link_status |= LINK_STATUS_LINK_UP;
13773 		vars->link_status &= ~link_flag;
13774 		vars->link_up = 1;
13775 		vars->phy_flags &= ~phy_flag;
13776 		led_mode = LED_MODE_OPER;
13777 
13778 		/* Clear nig drain */
13779 		REG_WR(bp, NIG_REG_EGRESS_DRAIN0_MODE + params->port*4, 0);
13780 	}
13781 	bnx2x_sync_link(params, vars);
13782 	/* Update the LED according to the link state */
13783 	bnx2x_set_led(params, vars, led_mode, SPEED_10000);
13784 
13785 	/* Update link status in the shared memory */
13786 	bnx2x_update_mng(params, vars->link_status);
13787 
13788 	/* C. Trigger General Attention */
13789 	vars->periodic_flags |= PERIODIC_FLAGS_LINK_EVENT;
13790 	if (notify)
13791 		bnx2x_notify_link_changed(bp);
13792 
13793 	return 1;
13794 }
13795 
13796 /******************************************************************************
13797 * Description:
13798 *	This function checks for half opened connection change indication.
13799 *	When such change occurs, it calls the bnx2x_analyze_link_error
13800 *	to check if Remote Fault is set or cleared. Reception of remote fault
13801 *	status message in the MAC indicates that the peer's MAC has detected
13802 *	a fault, for example, due to break in the TX side of fiber.
13803 *
13804 ******************************************************************************/
13805 static int bnx2x_check_half_open_conn(struct link_params *params,
13806 				      struct link_vars *vars,
13807 				      u8 notify)
13808 {
13809 	struct bnx2x *bp = params->bp;
13810 	u32 lss_status = 0;
13811 	u32 mac_base;
13812 	/* In case link status is physically up @ 10G do */
13813 	if (((vars->phy_flags & PHY_PHYSICAL_LINK_FLAG) == 0) ||
13814 	    (REG_RD(bp, NIG_REG_EGRESS_EMAC0_PORT + params->port*4)))
13815 		return 0;
13816 
13817 	if (CHIP_IS_E3(bp) &&
13818 	    (REG_RD(bp, MISC_REG_RESET_REG_2) &
13819 	      (MISC_REGISTERS_RESET_REG_2_XMAC))) {
13820 		/* Check E3 XMAC */
13821 		/* Note that link speed cannot be queried here, since it may be
13822 		 * zero while link is down. In case UMAC is active, LSS will
13823 		 * simply not be set
13824 		 */
13825 		mac_base = (params->port) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
13826 
13827 		/* Clear stick bits (Requires rising edge) */
13828 		REG_WR(bp, mac_base + XMAC_REG_CLEAR_RX_LSS_STATUS, 0);
13829 		REG_WR(bp, mac_base + XMAC_REG_CLEAR_RX_LSS_STATUS,
13830 		       XMAC_CLEAR_RX_LSS_STATUS_REG_CLEAR_LOCAL_FAULT_STATUS |
13831 		       XMAC_CLEAR_RX_LSS_STATUS_REG_CLEAR_REMOTE_FAULT_STATUS);
13832 		if (REG_RD(bp, mac_base + XMAC_REG_RX_LSS_STATUS))
13833 			lss_status = 1;
13834 
13835 		bnx2x_analyze_link_error(params, vars, lss_status,
13836 					 PHY_HALF_OPEN_CONN_FLAG,
13837 					 LINK_STATUS_NONE, notify);
13838 	} else if (REG_RD(bp, MISC_REG_RESET_REG_2) &
13839 		   (MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << params->port)) {
13840 		/* Check E1X / E2 BMAC */
13841 		u32 lss_status_reg;
13842 		u32 wb_data[2];
13843 		mac_base = params->port ? NIG_REG_INGRESS_BMAC1_MEM :
13844 			NIG_REG_INGRESS_BMAC0_MEM;
13845 		/*  Read BIGMAC_REGISTER_RX_LSS_STATUS */
13846 		if (CHIP_IS_E2(bp))
13847 			lss_status_reg = BIGMAC2_REGISTER_RX_LSS_STAT;
13848 		else
13849 			lss_status_reg = BIGMAC_REGISTER_RX_LSS_STATUS;
13850 
13851 		REG_RD_DMAE(bp, mac_base + lss_status_reg, wb_data, 2);
13852 		lss_status = (wb_data[0] > 0);
13853 
13854 		bnx2x_analyze_link_error(params, vars, lss_status,
13855 					 PHY_HALF_OPEN_CONN_FLAG,
13856 					 LINK_STATUS_NONE, notify);
13857 	}
13858 	return 0;
13859 }
13860 static void bnx2x_sfp_tx_fault_detection(struct bnx2x_phy *phy,
13861 					 struct link_params *params,
13862 					 struct link_vars *vars)
13863 {
13864 	struct bnx2x *bp = params->bp;
13865 	u32 cfg_pin, value = 0;
13866 	u8 led_change, port = params->port;
13867 
13868 	/* Get The SFP+ TX_Fault controlling pin ([eg]pio) */
13869 	cfg_pin = (REG_RD(bp, params->shmem_base + offsetof(struct shmem_region,
13870 			  dev_info.port_hw_config[port].e3_cmn_pin_cfg)) &
13871 		   PORT_HW_CFG_E3_TX_FAULT_MASK) >>
13872 		  PORT_HW_CFG_E3_TX_FAULT_SHIFT;
13873 
13874 	if (bnx2x_get_cfg_pin(bp, cfg_pin, &value)) {
13875 		DP(NETIF_MSG_LINK, "Failed to read pin 0x%02x\n", cfg_pin);
13876 		return;
13877 	}
13878 
13879 	led_change = bnx2x_analyze_link_error(params, vars, value,
13880 					      PHY_SFP_TX_FAULT_FLAG,
13881 					      LINK_STATUS_SFP_TX_FAULT, 1);
13882 
13883 	if (led_change) {
13884 		/* Change TX_Fault led, set link status for further syncs */
13885 		u8 led_mode;
13886 
13887 		if (vars->phy_flags & PHY_SFP_TX_FAULT_FLAG) {
13888 			led_mode = MISC_REGISTERS_GPIO_HIGH;
13889 			vars->link_status |= LINK_STATUS_SFP_TX_FAULT;
13890 		} else {
13891 			led_mode = MISC_REGISTERS_GPIO_LOW;
13892 			vars->link_status &= ~LINK_STATUS_SFP_TX_FAULT;
13893 		}
13894 
13895 		/* If module is unapproved, led should be on regardless */
13896 		if (!(phy->flags & FLAGS_SFP_NOT_APPROVED)) {
13897 			DP(NETIF_MSG_LINK, "Change TX_Fault LED: ->%x\n",
13898 			   led_mode);
13899 			bnx2x_set_e3_module_fault_led(params, led_mode);
13900 		}
13901 	}
13902 }
13903 static void bnx2x_kr2_recovery(struct link_params *params,
13904 			       struct link_vars *vars,
13905 			       struct bnx2x_phy *phy)
13906 {
13907 	struct bnx2x *bp = params->bp;
13908 	DP(NETIF_MSG_LINK, "KR2 recovery\n");
13909 	bnx2x_warpcore_enable_AN_KR2(phy, params, vars);
13910 	bnx2x_warpcore_restart_AN_KR(phy, params);
13911 }
13912 
13913 static void bnx2x_check_kr2_wa(struct link_params *params,
13914 			       struct link_vars *vars,
13915 			       struct bnx2x_phy *phy)
13916 {
13917 	struct bnx2x *bp = params->bp;
13918 	u16 base_page, next_page, not_kr2_device, lane;
13919 	int sigdet;
13920 
13921 	/* Once KR2 was disabled, wait 5 seconds before checking KR2 recovery
13922 	 * Since some switches tend to reinit the AN process and clear the
13923 	 * the advertised BP/NP after ~2 seconds causing the KR2 to be disabled
13924 	 * and recovered many times
13925 	 */
13926 	if (vars->check_kr2_recovery_cnt > 0) {
13927 		vars->check_kr2_recovery_cnt--;
13928 		return;
13929 	}
13930 
13931 	sigdet = bnx2x_warpcore_get_sigdet(phy, params);
13932 	if (!sigdet) {
13933 		if (!(params->link_attr_sync & LINK_ATTR_SYNC_KR2_ENABLE)) {
13934 			bnx2x_kr2_recovery(params, vars, phy);
13935 			DP(NETIF_MSG_LINK, "No sigdet\n");
13936 		}
13937 		return;
13938 	}
13939 
13940 	lane = bnx2x_get_warpcore_lane(phy, params);
13941 	CL22_WR_OVER_CL45(bp, phy, MDIO_REG_BANK_AER_BLOCK,
13942 			  MDIO_AER_BLOCK_AER_REG, lane);
13943 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
13944 			MDIO_AN_REG_LP_AUTO_NEG, &base_page);
13945 	bnx2x_cl45_read(bp, phy, MDIO_AN_DEVAD,
13946 			MDIO_AN_REG_LP_AUTO_NEG2, &next_page);
13947 	bnx2x_set_aer_mmd(params, phy);
13948 
13949 	/* CL73 has not begun yet */
13950 	if (base_page == 0) {
13951 		if (!(params->link_attr_sync & LINK_ATTR_SYNC_KR2_ENABLE)) {
13952 			bnx2x_kr2_recovery(params, vars, phy);
13953 			DP(NETIF_MSG_LINK, "No BP\n");
13954 		}
13955 		return;
13956 	}
13957 
13958 	/* In case NP bit is not set in the BasePage, or it is set,
13959 	 * but only KX is advertised, declare this link partner as non-KR2
13960 	 * device.
13961 	 */
13962 	not_kr2_device = (((base_page & 0x8000) == 0) ||
13963 			  (((base_page & 0x8000) &&
13964 			    ((next_page & 0xe0) == 0x20))));
13965 
13966 	/* In case KR2 is already disabled, check if we need to re-enable it */
13967 	if (!(params->link_attr_sync & LINK_ATTR_SYNC_KR2_ENABLE)) {
13968 		if (!not_kr2_device) {
13969 			DP(NETIF_MSG_LINK, "BP=0x%x, NP=0x%x\n", base_page,
13970 			   next_page);
13971 			bnx2x_kr2_recovery(params, vars, phy);
13972 		}
13973 		return;
13974 	}
13975 	/* KR2 is enabled, but not KR2 device */
13976 	if (not_kr2_device) {
13977 		/* Disable KR2 on both lanes */
13978 		DP(NETIF_MSG_LINK, "BP=0x%x, NP=0x%x\n", base_page, next_page);
13979 		bnx2x_disable_kr2(params, vars, phy);
13980 		/* Restart AN on leading lane */
13981 		bnx2x_warpcore_restart_AN_KR(phy, params);
13982 		return;
13983 	}
13984 }
13985 
13986 void bnx2x_period_func(struct link_params *params, struct link_vars *vars)
13987 {
13988 	u16 phy_idx;
13989 	struct bnx2x *bp = params->bp;
13990 	for (phy_idx = INT_PHY; phy_idx < MAX_PHYS; phy_idx++) {
13991 		if (params->phy[phy_idx].flags & FLAGS_TX_ERROR_CHECK) {
13992 			bnx2x_set_aer_mmd(params, &params->phy[phy_idx]);
13993 			if (bnx2x_check_half_open_conn(params, vars, 1) !=
13994 			    0)
13995 				DP(NETIF_MSG_LINK, "Fault detection failed\n");
13996 			break;
13997 		}
13998 	}
13999 
14000 	if (CHIP_IS_E3(bp)) {
14001 		struct bnx2x_phy *phy = &params->phy[INT_PHY];
14002 		bnx2x_set_aer_mmd(params, phy);
14003 		if (((phy->req_line_speed == SPEED_AUTO_NEG) &&
14004 		     (phy->speed_cap_mask &
14005 		      PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)) ||
14006 		    (phy->req_line_speed == SPEED_20000))
14007 			bnx2x_check_kr2_wa(params, vars, phy);
14008 		bnx2x_check_over_curr(params, vars);
14009 		if (vars->rx_tx_asic_rst)
14010 			bnx2x_warpcore_config_runtime(phy, params, vars);
14011 
14012 		if ((REG_RD(bp, params->shmem_base +
14013 			    offsetof(struct shmem_region, dev_info.
14014 				port_hw_config[params->port].default_cfg))
14015 		    & PORT_HW_CFG_NET_SERDES_IF_MASK) ==
14016 		    PORT_HW_CFG_NET_SERDES_IF_SFI) {
14017 			if (bnx2x_is_sfp_module_plugged(phy, params)) {
14018 				bnx2x_sfp_tx_fault_detection(phy, params, vars);
14019 			} else if (vars->link_status &
14020 				LINK_STATUS_SFP_TX_FAULT) {
14021 				/* Clean trail, interrupt corrects the leds */
14022 				vars->link_status &= ~LINK_STATUS_SFP_TX_FAULT;
14023 				vars->phy_flags &= ~PHY_SFP_TX_FAULT_FLAG;
14024 				/* Update link status in the shared memory */
14025 				bnx2x_update_mng(params, vars->link_status);
14026 			}
14027 		}
14028 	}
14029 }
14030 
14031 u8 bnx2x_fan_failure_det_req(struct bnx2x *bp,
14032 			     u32 shmem_base,
14033 			     u32 shmem2_base,
14034 			     u8 port)
14035 {
14036 	u8 phy_index, fan_failure_det_req = 0;
14037 	struct bnx2x_phy phy;
14038 	for (phy_index = EXT_PHY1; phy_index < MAX_PHYS;
14039 	      phy_index++) {
14040 		if (bnx2x_populate_phy(bp, phy_index, shmem_base, shmem2_base,
14041 				       port, &phy)
14042 		    != 0) {
14043 			DP(NETIF_MSG_LINK, "populate phy failed\n");
14044 			return 0;
14045 		}
14046 		fan_failure_det_req |= (phy.flags &
14047 					FLAGS_FAN_FAILURE_DET_REQ);
14048 	}
14049 	return fan_failure_det_req;
14050 }
14051 
14052 void bnx2x_hw_reset_phy(struct link_params *params)
14053 {
14054 	u8 phy_index;
14055 	struct bnx2x *bp = params->bp;
14056 	bnx2x_update_mng(params, 0);
14057 	bnx2x_bits_dis(bp, NIG_REG_MASK_INTERRUPT_PORT0 + params->port*4,
14058 		       (NIG_MASK_XGXS0_LINK_STATUS |
14059 			NIG_MASK_XGXS0_LINK10G |
14060 			NIG_MASK_SERDES0_LINK_STATUS |
14061 			NIG_MASK_MI_INT));
14062 
14063 	for (phy_index = INT_PHY; phy_index < MAX_PHYS;
14064 	      phy_index++) {
14065 		if (params->phy[phy_index].hw_reset) {
14066 			params->phy[phy_index].hw_reset(
14067 				&params->phy[phy_index],
14068 				params);
14069 			params->phy[phy_index] = phy_null;
14070 		}
14071 	}
14072 }
14073 
14074 void bnx2x_init_mod_abs_int(struct bnx2x *bp, struct link_vars *vars,
14075 			    u32 chip_id, u32 shmem_base, u32 shmem2_base,
14076 			    u8 port)
14077 {
14078 	u8 gpio_num = 0xff, gpio_port = 0xff, phy_index;
14079 	u32 val;
14080 	u32 offset, aeu_mask, swap_val, swap_override, sync_offset;
14081 	if (CHIP_IS_E3(bp)) {
14082 		if (bnx2x_get_mod_abs_int_cfg(bp, chip_id,
14083 					      shmem_base,
14084 					      port,
14085 					      &gpio_num,
14086 					      &gpio_port) != 0)
14087 			return;
14088 	} else {
14089 		struct bnx2x_phy phy;
14090 		for (phy_index = EXT_PHY1; phy_index < MAX_PHYS;
14091 		      phy_index++) {
14092 			if (bnx2x_populate_phy(bp, phy_index, shmem_base,
14093 					       shmem2_base, port, &phy)
14094 			    != 0) {
14095 				DP(NETIF_MSG_LINK, "populate phy failed\n");
14096 				return;
14097 			}
14098 			if (phy.type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726) {
14099 				gpio_num = MISC_REGISTERS_GPIO_3;
14100 				gpio_port = port;
14101 				break;
14102 			}
14103 		}
14104 	}
14105 
14106 	if (gpio_num == 0xff)
14107 		return;
14108 
14109 	/* Set GPIO3 to trigger SFP+ module insertion/removal */
14110 	bnx2x_set_gpio(bp, gpio_num, MISC_REGISTERS_GPIO_INPUT_HI_Z, gpio_port);
14111 
14112 	swap_val = REG_RD(bp, NIG_REG_PORT_SWAP);
14113 	swap_override = REG_RD(bp, NIG_REG_STRAP_OVERRIDE);
14114 	gpio_port ^= (swap_val && swap_override);
14115 
14116 	vars->aeu_int_mask = AEU_INPUTS_ATTN_BITS_GPIO0_FUNCTION_0 <<
14117 		(gpio_num + (gpio_port << 2));
14118 
14119 	sync_offset = shmem_base +
14120 		offsetof(struct shmem_region,
14121 			 dev_info.port_hw_config[port].aeu_int_mask);
14122 	REG_WR(bp, sync_offset, vars->aeu_int_mask);
14123 
14124 	DP(NETIF_MSG_LINK, "Setting MOD_ABS (GPIO%d_P%d) AEU to 0x%x\n",
14125 		       gpio_num, gpio_port, vars->aeu_int_mask);
14126 
14127 	if (port == 0)
14128 		offset = MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
14129 	else
14130 		offset = MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0;
14131 
14132 	/* Open appropriate AEU for interrupts */
14133 	aeu_mask = REG_RD(bp, offset);
14134 	aeu_mask |= vars->aeu_int_mask;
14135 	REG_WR(bp, offset, aeu_mask);
14136 
14137 	/* Enable the GPIO to trigger interrupt */
14138 	val = REG_RD(bp, MISC_REG_GPIO_EVENT_EN);
14139 	val |= 1 << (gpio_num + (gpio_port << 2));
14140 	REG_WR(bp, MISC_REG_GPIO_EVENT_EN, val);
14141 }
14142