xref: /netbsd-src/sys/dev/pci/ixgbe/ixgbe_phy.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /* $NetBSD: ixgbe_phy.c,v 1.21 2020/04/17 02:21:25 msaitoh Exp $ */
2 
3 /******************************************************************************
4   SPDX-License-Identifier: BSD-3-Clause
5 
6   Copyright (c) 2001-2017, Intel Corporation
7   All rights reserved.
8 
9   Redistribution and use in source and binary forms, with or without
10   modification, are permitted provided that the following conditions are met:
11 
12    1. Redistributions of source code must retain the above copyright notice,
13       this list of conditions and the following disclaimer.
14 
15    2. Redistributions in binary form must reproduce the above copyright
16       notice, this list of conditions and the following disclaimer in the
17       documentation and/or other materials provided with the distribution.
18 
19    3. Neither the name of the Intel Corporation nor the names of its
20       contributors may be used to endorse or promote products derived from
21       this software without specific prior written permission.
22 
23   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33   POSSIBILITY OF SUCH DAMAGE.
34 
35 ******************************************************************************/
36 /*$FreeBSD: head/sys/dev/ixgbe/ixgbe_phy.c 331224 2018-03-19 20:55:05Z erj $*/
37 
38 #include "ixgbe_api.h"
39 #include "ixgbe_common.h"
40 #include "ixgbe_phy.h"
41 
42 #include <dev/mii/mdio.h>
43 
44 static void ixgbe_i2c_start(struct ixgbe_hw *hw);
45 static void ixgbe_i2c_stop(struct ixgbe_hw *hw);
46 static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data);
47 static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data);
48 static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw);
49 static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data);
50 static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data);
51 static void ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
52 static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
53 static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data);
54 static bool ixgbe_get_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl);
55 static s32 ixgbe_read_i2c_sff8472_generic(struct ixgbe_hw *hw, u8 byte_offset,
56 					  u8 *sff8472_data);
57 
58 /**
59  * ixgbe_out_i2c_byte_ack - Send I2C byte with ack
60  * @hw: pointer to the hardware structure
61  * @byte: byte to send
62  *
63  * Returns an error code on error.
64  */
65 static s32 ixgbe_out_i2c_byte_ack(struct ixgbe_hw *hw, u8 byte)
66 {
67 	s32 status;
68 
69 	status = ixgbe_clock_out_i2c_byte(hw, byte);
70 	if (status)
71 		return status;
72 	return ixgbe_get_i2c_ack(hw);
73 }
74 
75 /**
76  * ixgbe_in_i2c_byte_ack - Receive an I2C byte and send ack
77  * @hw: pointer to the hardware structure
78  * @byte: pointer to a u8 to receive the byte
79  *
80  * Returns an error code on error.
81  */
82 static s32 ixgbe_in_i2c_byte_ack(struct ixgbe_hw *hw, u8 *byte)
83 {
84 	s32 status;
85 
86 	status = ixgbe_clock_in_i2c_byte(hw, byte);
87 	if (status)
88 		return status;
89 	/* ACK */
90 	return ixgbe_clock_out_i2c_bit(hw, FALSE);
91 }
92 
93 /**
94  * ixgbe_ones_comp_byte_add - Perform one's complement addition
95  * @add1: addend 1
96  * @add2: addend 2
97  *
98  * Returns one's complement 8-bit sum.
99  */
100 static u8 ixgbe_ones_comp_byte_add(u8 add1, u8 add2)
101 {
102 	u16 sum = add1 + add2;
103 
104 	sum = (sum & 0xFF) + (sum >> 8);
105 	return sum & 0xFF;
106 }
107 
108 /**
109  * ixgbe_read_i2c_combined_generic_int - Perform I2C read combined operation
110  * @hw: pointer to the hardware structure
111  * @addr: I2C bus address to read from
112  * @reg: I2C device register to read from
113  * @val: pointer to location to receive read value
114  * @lock: TRUE if to take and release semaphore
115  *
116  * Returns an error code on error.
117  */
118 s32 ixgbe_read_i2c_combined_generic_int(struct ixgbe_hw *hw, u8 addr, u16 reg,
119 					u16 *val, bool lock)
120 {
121 	u32 swfw_mask = hw->phy.phy_semaphore_mask;
122 	int max_retry = 3;
123 	int retry = 0;
124 	u8 csum_byte;
125 	u8 high_bits;
126 	u8 low_bits;
127 	u8 reg_high;
128 	u8 csum;
129 
130 	reg_high = ((reg >> 7) & 0xFE) | 1;	/* Indicate read combined */
131 	csum = ixgbe_ones_comp_byte_add(reg_high, reg & 0xFF);
132 	csum = ~csum;
133 	do {
134 		if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
135 			return IXGBE_ERR_SWFW_SYNC;
136 		ixgbe_i2c_start(hw);
137 		/* Device Address and write indication */
138 		if (ixgbe_out_i2c_byte_ack(hw, addr))
139 			goto fail;
140 		/* Write bits 14:8 */
141 		if (ixgbe_out_i2c_byte_ack(hw, reg_high))
142 			goto fail;
143 		/* Write bits 7:0 */
144 		if (ixgbe_out_i2c_byte_ack(hw, reg & 0xFF))
145 			goto fail;
146 		/* Write csum */
147 		if (ixgbe_out_i2c_byte_ack(hw, csum))
148 			goto fail;
149 		/* Re-start condition */
150 		ixgbe_i2c_start(hw);
151 		/* Device Address and read indication */
152 		if (ixgbe_out_i2c_byte_ack(hw, addr | 1))
153 			goto fail;
154 		/* Get upper bits */
155 		if (ixgbe_in_i2c_byte_ack(hw, &high_bits))
156 			goto fail;
157 		/* Get low bits */
158 		if (ixgbe_in_i2c_byte_ack(hw, &low_bits))
159 			goto fail;
160 		/* Get csum */
161 		if (ixgbe_clock_in_i2c_byte(hw, &csum_byte))
162 			goto fail;
163 		/* NACK */
164 		if (ixgbe_clock_out_i2c_bit(hw, FALSE))
165 			goto fail;
166 		ixgbe_i2c_stop(hw);
167 		if (lock)
168 			hw->mac.ops.release_swfw_sync(hw, swfw_mask);
169 		*val = (high_bits << 8) | low_bits;
170 		return 0;
171 
172 fail:
173 		ixgbe_i2c_bus_clear(hw);
174 		if (lock)
175 			hw->mac.ops.release_swfw_sync(hw, swfw_mask);
176 		retry++;
177 		if (retry < max_retry)
178 			DEBUGOUT("I2C byte read combined error - Retrying.\n");
179 		else
180 			DEBUGOUT("I2C byte read combined error.\n");
181 	} while (retry < max_retry);
182 
183 	return IXGBE_ERR_I2C;
184 }
185 
186 /**
187  * ixgbe_write_i2c_combined_generic_int - Perform I2C write combined operation
188  * @hw: pointer to the hardware structure
189  * @addr: I2C bus address to write to
190  * @reg: I2C device register to write to
191  * @val: value to write
192  * @lock: TRUE if to take and release semaphore
193  *
194  * Returns an error code on error.
195  */
196 s32 ixgbe_write_i2c_combined_generic_int(struct ixgbe_hw *hw, u8 addr, u16 reg,
197 					 u16 val, bool lock)
198 {
199 	u32 swfw_mask = hw->phy.phy_semaphore_mask;
200 	int max_retry = 1;
201 	int retry = 0;
202 	u8 reg_high;
203 	u8 csum;
204 
205 	reg_high = (reg >> 7) & 0xFE;	/* Indicate write combined */
206 	csum = ixgbe_ones_comp_byte_add(reg_high, reg & 0xFF);
207 	csum = ixgbe_ones_comp_byte_add(csum, val >> 8);
208 	csum = ixgbe_ones_comp_byte_add(csum, val & 0xFF);
209 	csum = ~csum;
210 	do {
211 		if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
212 			return IXGBE_ERR_SWFW_SYNC;
213 		ixgbe_i2c_start(hw);
214 		/* Device Address and write indication */
215 		if (ixgbe_out_i2c_byte_ack(hw, addr))
216 			goto fail;
217 		/* Write bits 14:8 */
218 		if (ixgbe_out_i2c_byte_ack(hw, reg_high))
219 			goto fail;
220 		/* Write bits 7:0 */
221 		if (ixgbe_out_i2c_byte_ack(hw, reg & 0xFF))
222 			goto fail;
223 		/* Write data 15:8 */
224 		if (ixgbe_out_i2c_byte_ack(hw, val >> 8))
225 			goto fail;
226 		/* Write data 7:0 */
227 		if (ixgbe_out_i2c_byte_ack(hw, val & 0xFF))
228 			goto fail;
229 		/* Write csum */
230 		if (ixgbe_out_i2c_byte_ack(hw, csum))
231 			goto fail;
232 		ixgbe_i2c_stop(hw);
233 		if (lock)
234 			hw->mac.ops.release_swfw_sync(hw, swfw_mask);
235 		return 0;
236 
237 fail:
238 		ixgbe_i2c_bus_clear(hw);
239 		if (lock)
240 			hw->mac.ops.release_swfw_sync(hw, swfw_mask);
241 		retry++;
242 		if (retry < max_retry)
243 			DEBUGOUT("I2C byte write combined error - Retrying.\n");
244 		else
245 			DEBUGOUT("I2C byte write combined error.\n");
246 	} while (retry < max_retry);
247 
248 	return IXGBE_ERR_I2C;
249 }
250 
251 /**
252  *  ixgbe_init_phy_ops_generic - Inits PHY function ptrs
253  *  @hw: pointer to the hardware structure
254  *
255  *  Initialize the function pointers.
256  **/
257 s32 ixgbe_init_phy_ops_generic(struct ixgbe_hw *hw)
258 {
259 	struct ixgbe_phy_info *phy = &hw->phy;
260 
261 	DEBUGFUNC("ixgbe_init_phy_ops_generic");
262 
263 	/* PHY */
264 	phy->ops.identify = ixgbe_identify_phy_generic;
265 	phy->ops.reset = ixgbe_reset_phy_generic;
266 	phy->ops.read_reg = ixgbe_read_phy_reg_generic;
267 	phy->ops.write_reg = ixgbe_write_phy_reg_generic;
268 	phy->ops.read_reg_mdi = ixgbe_read_phy_reg_mdi;
269 	phy->ops.write_reg_mdi = ixgbe_write_phy_reg_mdi;
270 	phy->ops.setup_link = ixgbe_setup_phy_link_generic;
271 	phy->ops.setup_link_speed = ixgbe_setup_phy_link_speed_generic;
272 	phy->ops.check_link = NULL;
273 	phy->ops.get_firmware_version = ixgbe_get_phy_firmware_version_generic;
274 	phy->ops.read_i2c_byte = ixgbe_read_i2c_byte_generic;
275 	phy->ops.write_i2c_byte = ixgbe_write_i2c_byte_generic;
276 	phy->ops.read_i2c_sff8472 = ixgbe_read_i2c_sff8472_generic;
277 	phy->ops.read_i2c_eeprom = ixgbe_read_i2c_eeprom_generic;
278 	phy->ops.write_i2c_eeprom = ixgbe_write_i2c_eeprom_generic;
279 	phy->ops.i2c_bus_clear = ixgbe_i2c_bus_clear;
280 	phy->ops.identify_sfp = ixgbe_identify_module_generic;
281 	phy->sfp_type = ixgbe_sfp_type_unknown;
282 	phy->ops.read_i2c_byte_unlocked = ixgbe_read_i2c_byte_generic_unlocked;
283 	phy->ops.write_i2c_byte_unlocked =
284 				ixgbe_write_i2c_byte_generic_unlocked;
285 	phy->ops.check_overtemp = ixgbe_tn_check_overtemp;
286 	return IXGBE_SUCCESS;
287 }
288 
289 /**
290  * ixgbe_probe_phy - Probe a single address for a PHY
291  * @hw: pointer to hardware structure
292  * @phy_addr: PHY address to probe
293  *
294  * Returns TRUE if PHY found
295  */
296 static bool ixgbe_probe_phy(struct ixgbe_hw *hw, u16 phy_addr)
297 {
298 	u16 ext_ability = 0;
299 
300 	if (!ixgbe_validate_phy_addr(hw, phy_addr)) {
301 		DEBUGOUT1("Unable to validate PHY address 0x%04X\n",
302 			phy_addr);
303 		return FALSE;
304 	}
305 
306 	if (ixgbe_get_phy_id(hw))
307 		return FALSE;
308 
309 	hw->phy.type = ixgbe_get_phy_type_from_id(hw->phy.id);
310 
311 	if (hw->phy.type == ixgbe_phy_unknown) {
312 		hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_EXT_ABILITY,
313 				     IXGBE_MDIO_PMA_PMD_DEV_TYPE, &ext_ability);
314 		if (ext_ability &
315 		    (IXGBE_MDIO_PHY_10GBASET_ABILITY |
316 		     IXGBE_MDIO_PHY_1000BASET_ABILITY))
317 			hw->phy.type = ixgbe_phy_cu_unknown;
318 		else
319 			hw->phy.type = ixgbe_phy_generic;
320 	}
321 
322 	return TRUE;
323 }
324 
325 /**
326  *  ixgbe_identify_phy_generic - Get physical layer module
327  *  @hw: pointer to hardware structure
328  *
329  *  Determines the physical layer module found on the current adapter.
330  **/
331 s32 ixgbe_identify_phy_generic(struct ixgbe_hw *hw)
332 {
333 	s32 status = IXGBE_ERR_PHY_ADDR_INVALID;
334 	u16 phy_addr;
335 
336 	DEBUGFUNC("ixgbe_identify_phy_generic");
337 
338 	if (!hw->phy.phy_semaphore_mask) {
339 		if (hw->bus.lan_id)
340 			hw->phy.phy_semaphore_mask = IXGBE_GSSR_PHY1_SM;
341 		else
342 			hw->phy.phy_semaphore_mask = IXGBE_GSSR_PHY0_SM;
343 	}
344 
345 	if (hw->phy.type != ixgbe_phy_unknown)
346 		return IXGBE_SUCCESS;
347 
348 	if (hw->phy.nw_mng_if_sel) {
349 		phy_addr = (hw->phy.nw_mng_if_sel &
350 			    IXGBE_NW_MNG_IF_SEL_MDIO_PHY_ADD) >>
351 			   IXGBE_NW_MNG_IF_SEL_MDIO_PHY_ADD_SHIFT;
352 		if (ixgbe_probe_phy(hw, phy_addr))
353 			return IXGBE_SUCCESS;
354 		else
355 			return IXGBE_ERR_PHY_ADDR_INVALID;
356 	}
357 
358 	for (phy_addr = 0; phy_addr < IXGBE_MAX_PHY_ADDR; phy_addr++) {
359 		if (ixgbe_probe_phy(hw, phy_addr)) {
360 			status = IXGBE_SUCCESS;
361 			break;
362 		}
363 	}
364 
365 	/* Certain media types do not have a phy so an address will not
366 	 * be found and the code will take this path.  Caller has to
367 	 * decide if it is an error or not.
368 	 */
369 	if (status != IXGBE_SUCCESS)
370 		hw->phy.addr = 0;
371 
372 	return status;
373 }
374 
375 /**
376  * ixgbe_check_reset_blocked - check status of MNG FW veto bit
377  * @hw: pointer to the hardware structure
378  *
379  * This function checks the MMNGC.MNG_VETO bit to see if there are
380  * any constraints on link from manageability.  For MAC's that don't
381  * have this bit just return faluse since the link can not be blocked
382  * via this method.
383  **/
384 s32 ixgbe_check_reset_blocked(struct ixgbe_hw *hw)
385 {
386 	u32 mmngc;
387 
388 	DEBUGFUNC("ixgbe_check_reset_blocked");
389 
390 	/* If we don't have this bit, it can't be blocking */
391 	if (hw->mac.type == ixgbe_mac_82598EB)
392 		return FALSE;
393 
394 	mmngc = IXGBE_READ_REG(hw, IXGBE_MMNGC);
395 	if (mmngc & IXGBE_MMNGC_MNG_VETO) {
396 		ERROR_REPORT1(IXGBE_ERROR_SOFTWARE,
397 			      "MNG_VETO bit detected.\n");
398 		return TRUE;
399 	}
400 
401 	return FALSE;
402 }
403 
404 /**
405  *  ixgbe_validate_phy_addr - Determines phy address is valid
406  *  @hw: pointer to hardware structure
407  *  @phy_addr: PHY address
408  *
409  **/
410 bool ixgbe_validate_phy_addr(struct ixgbe_hw *hw, u32 phy_addr)
411 {
412 	u16 phy_id = 0;
413 	bool valid = FALSE;
414 
415 	DEBUGFUNC("ixgbe_validate_phy_addr");
416 
417 	hw->phy.addr = phy_addr;
418 	hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_ID_HIGH,
419 			     IXGBE_MDIO_PMA_PMD_DEV_TYPE, &phy_id);
420 
421 	if (phy_id != 0xFFFF && phy_id != 0x0)
422 		valid = TRUE;
423 
424 	DEBUGOUT1("PHY ID HIGH is 0x%04X\n", phy_id);
425 
426 	return valid;
427 }
428 
429 /**
430  *  ixgbe_get_phy_id - Get the phy type
431  *  @hw: pointer to hardware structure
432  *
433  **/
434 s32 ixgbe_get_phy_id(struct ixgbe_hw *hw)
435 {
436 	u32 status;
437 	u16 phy_id_high = 0;
438 	u16 phy_id_low = 0;
439 
440 	DEBUGFUNC("ixgbe_get_phy_id");
441 
442 	status = hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_ID_HIGH,
443 				      IXGBE_MDIO_PMA_PMD_DEV_TYPE,
444 				      &phy_id_high);
445 
446 	if (status == IXGBE_SUCCESS) {
447 		hw->phy.id = (u32)(phy_id_high << 16);
448 		status = hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_ID_LOW,
449 					      IXGBE_MDIO_PMA_PMD_DEV_TYPE,
450 					      &phy_id_low);
451 		hw->phy.id |= (u32)(phy_id_low & IXGBE_PHY_REVISION_MASK);
452 		hw->phy.revision = (u32)(phy_id_low & ~IXGBE_PHY_REVISION_MASK);
453 	}
454 	DEBUGOUT2("PHY_ID_HIGH 0x%04X, PHY_ID_LOW 0x%04X\n",
455 		  phy_id_high, phy_id_low);
456 
457 	return status;
458 }
459 
460 /**
461  *  ixgbe_get_phy_type_from_id - Get the phy type
462  *  @phy_id: PHY ID information
463  *
464  **/
465 enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id)
466 {
467 	enum ixgbe_phy_type phy_type;
468 
469 	DEBUGFUNC("ixgbe_get_phy_type_from_id");
470 
471 	switch (phy_id) {
472 	case TN1010_PHY_ID:
473 		phy_type = ixgbe_phy_tn;
474 		break;
475 	case X550_PHY_ID2:
476 	case X550_PHY_ID3:
477 	case X540_PHY_ID:
478 		phy_type = ixgbe_phy_aq;
479 		break;
480 	case QT2022_PHY_ID:
481 		phy_type = ixgbe_phy_qt;
482 		break;
483 	case ATH_PHY_ID:
484 		phy_type = ixgbe_phy_nl;
485 		break;
486 	case X557_PHY_ID:
487 	case X557_PHY_ID2:
488 		phy_type = ixgbe_phy_x550em_ext_t;
489 		break;
490 	case IXGBE_M88E1500_E_PHY_ID:
491 	case IXGBE_M88E1543_E_PHY_ID:
492 		phy_type = ixgbe_phy_ext_1g_t;
493 		break;
494 	default:
495 		phy_type = ixgbe_phy_unknown;
496 		break;
497 	}
498 	return phy_type;
499 }
500 
501 /**
502  *  ixgbe_reset_phy_generic - Performs a PHY reset
503  *  @hw: pointer to hardware structure
504  **/
505 s32 ixgbe_reset_phy_generic(struct ixgbe_hw *hw)
506 {
507 	u32 i;
508 	u16 ctrl = 0;
509 	s32 status = IXGBE_SUCCESS;
510 
511 	DEBUGFUNC("ixgbe_reset_phy_generic");
512 
513 	if (hw->phy.type == ixgbe_phy_unknown)
514 		status = ixgbe_identify_phy_generic(hw);
515 
516 	if (status != IXGBE_SUCCESS || hw->phy.type == ixgbe_phy_none)
517 		goto out;
518 
519 	/* Don't reset PHY if it's shut down due to overtemp. */
520 	if (!hw->phy.reset_if_overtemp &&
521 	    (IXGBE_ERR_OVERTEMP == hw->phy.ops.check_overtemp(hw)))
522 		goto out;
523 
524 	/* Blocked by MNG FW so bail */
525 	if (ixgbe_check_reset_blocked(hw))
526 		goto out;
527 
528 	/*
529 	 * Perform soft PHY reset to the PHY_XS.
530 	 * This will cause a soft reset to the PHY
531 	 */
532 	hw->phy.ops.write_reg(hw, IXGBE_MDIO_PHY_XS_CONTROL,
533 			      IXGBE_MDIO_PHY_XS_DEV_TYPE,
534 			      IXGBE_MDIO_PHY_XS_RESET);
535 
536 	/*
537 	 * Poll for reset bit to self-clear indicating reset is complete.
538 	 * Some PHYs could take up to 3 seconds to complete and need about
539 	 * 1.7 usec delay after the reset is complete.
540 	 */
541 	for (i = 0; i < 30; i++) {
542 		msec_delay(100);
543 		if (hw->phy.type == ixgbe_phy_x550em_ext_t) {
544 			status = hw->phy.ops.read_reg(hw,
545 						  IXGBE_MDIO_TX_VENDOR_ALARMS_3,
546 						  IXGBE_MDIO_PMA_PMD_DEV_TYPE,
547 						  &ctrl);
548 			if (status != IXGBE_SUCCESS)
549 				return status;
550 
551 			if (ctrl & IXGBE_MDIO_TX_VENDOR_ALARMS_3_RST_MASK) {
552 				usec_delay(2);
553 				break;
554 			}
555 		} else {
556 			status = hw->phy.ops.read_reg(hw,
557 						     IXGBE_MDIO_PHY_XS_CONTROL,
558 						     IXGBE_MDIO_PHY_XS_DEV_TYPE,
559 						     &ctrl);
560 			if (status != IXGBE_SUCCESS)
561 				return status;
562 
563 			if (!(ctrl & IXGBE_MDIO_PHY_XS_RESET)) {
564 				usec_delay(2);
565 				break;
566 			}
567 		}
568 	}
569 
570 	if (ctrl & IXGBE_MDIO_PHY_XS_RESET) {
571 		status = IXGBE_ERR_RESET_FAILED;
572 		ERROR_REPORT1(IXGBE_ERROR_POLLING,
573 			     "PHY reset polling failed to complete.\n");
574 	}
575 
576 out:
577 	return status;
578 }
579 
580 /**
581  *  ixgbe_read_phy_mdi - Reads a value from a specified PHY register without
582  *  the SWFW lock
583  *  @hw: pointer to hardware structure
584  *  @reg_addr: 32 bit address of PHY register to read
585  *  @device_type: 5 bit device type
586  *  @phy_data: Pointer to read data from PHY register
587  **/
588 s32 ixgbe_read_phy_reg_mdi(struct ixgbe_hw *hw, u32 reg_addr, u32 device_type,
589 			   u16 *phy_data)
590 {
591 	u32 i, data, command;
592 
593 	/* Setup and write the address cycle command */
594 	command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT)  |
595 		   (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
596 		   (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
597 		   (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
598 
599 	IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
600 
601 	/*
602 	 * Check every 10 usec to see if the address cycle completed.
603 	 * The MDI Command bit will clear when the operation is
604 	 * complete
605 	 */
606 	for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
607 		usec_delay(10);
608 
609 		command = IXGBE_READ_REG(hw, IXGBE_MSCA);
610 		if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
611 			break;
612 	}
613 
614 
615 	if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
616 		ERROR_REPORT1(IXGBE_ERROR_POLLING, "PHY address command did not complete.\n");
617 		DEBUGOUT("PHY address command did not complete, returning IXGBE_ERR_PHY\n");
618 		return IXGBE_ERR_PHY;
619 	}
620 
621 	/*
622 	 * Address cycle complete, setup and write the read
623 	 * command
624 	 */
625 	command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT)  |
626 		   (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
627 		   (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
628 		   (IXGBE_MSCA_READ | IXGBE_MSCA_MDI_COMMAND));
629 
630 	IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
631 
632 	/*
633 	 * Check every 10 usec to see if the address cycle
634 	 * completed. The MDI Command bit will clear when the
635 	 * operation is complete
636 	 */
637 	for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
638 		usec_delay(10);
639 
640 		command = IXGBE_READ_REG(hw, IXGBE_MSCA);
641 		if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
642 			break;
643 	}
644 
645 	if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
646 		ERROR_REPORT1(IXGBE_ERROR_POLLING, "PHY read command didn't complete\n");
647 		DEBUGOUT("PHY read command didn't complete, returning IXGBE_ERR_PHY\n");
648 		return IXGBE_ERR_PHY;
649 	}
650 
651 	/*
652 	 * Read operation is complete.  Get the data
653 	 * from MSRWD
654 	 */
655 	data = IXGBE_READ_REG(hw, IXGBE_MSRWD);
656 	data >>= IXGBE_MSRWD_READ_DATA_SHIFT;
657 	*phy_data = (u16)(data);
658 
659 	return IXGBE_SUCCESS;
660 }
661 
662 /**
663  *  ixgbe_read_phy_reg_generic - Reads a value from a specified PHY register
664  *  using the SWFW lock - this function is needed in most cases
665  *  @hw: pointer to hardware structure
666  *  @reg_addr: 32 bit address of PHY register to read
667  *  @device_type: 5 bit device type
668  *  @phy_data: Pointer to read data from PHY register
669  **/
670 s32 ixgbe_read_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
671 			       u32 device_type, u16 *phy_data)
672 {
673 	s32 status;
674 	u32 gssr = hw->phy.phy_semaphore_mask;
675 
676 	DEBUGFUNC("ixgbe_read_phy_reg_generic");
677 
678 	if (hw->mac.ops.acquire_swfw_sync(hw, gssr))
679 		return IXGBE_ERR_SWFW_SYNC;
680 
681 	status = hw->phy.ops.read_reg_mdi(hw, reg_addr, device_type, phy_data);
682 
683 	hw->mac.ops.release_swfw_sync(hw, gssr);
684 
685 	return status;
686 }
687 
688 /**
689  *  ixgbe_write_phy_reg_mdi - Writes a value to specified PHY register
690  *  without SWFW lock
691  *  @hw: pointer to hardware structure
692  *  @reg_addr: 32 bit PHY register to write
693  *  @device_type: 5 bit device type
694  *  @phy_data: Data to write to the PHY register
695  **/
696 s32 ixgbe_write_phy_reg_mdi(struct ixgbe_hw *hw, u32 reg_addr,
697 				u32 device_type, u16 phy_data)
698 {
699 	u32 i, command;
700 
701 	/* Put the data in the MDI single read and write data register*/
702 	IXGBE_WRITE_REG(hw, IXGBE_MSRWD, (u32)phy_data);
703 
704 	/* Setup and write the address cycle command */
705 	command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT)  |
706 		   (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
707 		   (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
708 		   (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
709 
710 	IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
711 
712 	/*
713 	 * Check every 10 usec to see if the address cycle completed.
714 	 * The MDI Command bit will clear when the operation is
715 	 * complete
716 	 */
717 	for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
718 		usec_delay(10);
719 
720 		command = IXGBE_READ_REG(hw, IXGBE_MSCA);
721 		if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
722 			break;
723 	}
724 
725 	if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
726 		ERROR_REPORT1(IXGBE_ERROR_POLLING, "PHY address cmd didn't complete\n");
727 		return IXGBE_ERR_PHY;
728 	}
729 
730 	/*
731 	 * Address cycle complete, setup and write the write
732 	 * command
733 	 */
734 	command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT)  |
735 		   (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
736 		   (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
737 		   (IXGBE_MSCA_WRITE | IXGBE_MSCA_MDI_COMMAND));
738 
739 	IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
740 
741 	/*
742 	 * Check every 10 usec to see if the address cycle
743 	 * completed. The MDI Command bit will clear when the
744 	 * operation is complete
745 	 */
746 	for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
747 		usec_delay(10);
748 
749 		command = IXGBE_READ_REG(hw, IXGBE_MSCA);
750 		if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
751 			break;
752 	}
753 
754 	if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
755 		ERROR_REPORT1(IXGBE_ERROR_POLLING, "PHY write cmd didn't complete\n");
756 		return IXGBE_ERR_PHY;
757 	}
758 
759 	return IXGBE_SUCCESS;
760 }
761 
762 /**
763  *  ixgbe_write_phy_reg_generic - Writes a value to specified PHY register
764  *  using SWFW lock- this function is needed in most cases
765  *  @hw: pointer to hardware structure
766  *  @reg_addr: 32 bit PHY register to write
767  *  @device_type: 5 bit device type
768  *  @phy_data: Data to write to the PHY register
769  **/
770 s32 ixgbe_write_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
771 				u32 device_type, u16 phy_data)
772 {
773 	s32 status;
774 	u32 gssr = hw->phy.phy_semaphore_mask;
775 
776 	DEBUGFUNC("ixgbe_write_phy_reg_generic");
777 
778 	if (hw->mac.ops.acquire_swfw_sync(hw, gssr) == IXGBE_SUCCESS) {
779 		status = hw->phy.ops.write_reg_mdi(hw, reg_addr, device_type,
780 						 phy_data);
781 		hw->mac.ops.release_swfw_sync(hw, gssr);
782 	} else {
783 		status = IXGBE_ERR_SWFW_SYNC;
784 	}
785 
786 	return status;
787 }
788 
789 /**
790  *  ixgbe_setup_phy_link_generic - Set and restart auto-neg
791  *  @hw: pointer to hardware structure
792  *
793  *  Restart auto-negotiation and PHY and waits for completion.
794  **/
795 s32 ixgbe_setup_phy_link_generic(struct ixgbe_hw *hw)
796 {
797 	s32 status = IXGBE_SUCCESS;
798 	u16 autoneg_reg = IXGBE_MII_AUTONEG_REG;
799 	bool autoneg = FALSE;
800 	ixgbe_link_speed speed;
801 
802 	DEBUGFUNC("ixgbe_setup_phy_link_generic");
803 
804 	ixgbe_get_copper_link_capabilities_generic(hw, &speed, &autoneg);
805 
806 	/* Set or unset auto-negotiation 10G advertisement */
807 	hw->phy.ops.read_reg(hw, IXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
808 			     IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
809 			     &autoneg_reg);
810 
811 	autoneg_reg &= ~IXGBE_MII_10GBASE_T_ADVERTISE;
812 	if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL) &&
813 	    (speed & IXGBE_LINK_SPEED_10GB_FULL))
814 		autoneg_reg |= IXGBE_MII_10GBASE_T_ADVERTISE;
815 
816 	hw->phy.ops.write_reg(hw, IXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
817 			      IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
818 			      autoneg_reg);
819 
820 	hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
821 			     IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
822 			     &autoneg_reg);
823 
824 	if (hw->mac.type == ixgbe_mac_X550) {
825 		/* Set or unset auto-negotiation 5G advertisement */
826 		autoneg_reg &= ~IXGBE_MII_5GBASE_T_ADVERTISE;
827 		if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_5GB_FULL) &&
828 		    (speed & IXGBE_LINK_SPEED_5GB_FULL))
829 			autoneg_reg |= IXGBE_MII_5GBASE_T_ADVERTISE;
830 
831 		/* Set or unset auto-negotiation 2.5G advertisement */
832 		autoneg_reg &= ~IXGBE_MII_2_5GBASE_T_ADVERTISE;
833 		if ((hw->phy.autoneg_advertised &
834 		     IXGBE_LINK_SPEED_2_5GB_FULL) &&
835 		    (speed & IXGBE_LINK_SPEED_2_5GB_FULL))
836 			autoneg_reg |= IXGBE_MII_2_5GBASE_T_ADVERTISE;
837 	}
838 
839 	/* Set or unset auto-negotiation 1G advertisement */
840 	autoneg_reg &= ~IXGBE_MII_1GBASE_T_ADVERTISE;
841 	if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL) &&
842 	    (speed & IXGBE_LINK_SPEED_1GB_FULL))
843 		autoneg_reg |= IXGBE_MII_1GBASE_T_ADVERTISE;
844 
845 	hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
846 			      IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
847 			      autoneg_reg);
848 
849 	/* Set or unset auto-negotiation 100M advertisement */
850 	hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_ADVERTISE_REG,
851 			     IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
852 			     &autoneg_reg);
853 
854 	autoneg_reg &= ~(IXGBE_MII_100BASE_T_ADVERTISE |
855 			 IXGBE_MII_100BASE_T_ADVERTISE_HALF);
856 	if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL) &&
857 	    (speed & IXGBE_LINK_SPEED_100_FULL))
858 		autoneg_reg |= IXGBE_MII_100BASE_T_ADVERTISE;
859 
860 	hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_ADVERTISE_REG,
861 			      IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
862 			      autoneg_reg);
863 
864 	if (hw->phy.autoneg_advertised == IXGBE_LINK_SPEED_100_FULL) {
865 		u16 ctrl;
866 
867 		/* Force 100Mbps */
868 		hw->phy.ops.read_reg(hw, MDIO_PMAPMD_CTRL1, MDIO_MMD_PMAPMD,
869 		    &ctrl);
870 		ctrl &= ~PMAPMD_CTRL1_SPEED_MASK;
871 		ctrl |= PMAPMD_CTRL1_SPEED_100;
872 		hw->phy.ops.write_reg(hw, MDIO_PMAPMD_CTRL1,MDIO_MMD_PMAPMD,
873 		    ctrl);
874 
875 		/* Don't use auto-nego for 100Mbps */
876 		hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
877 			     IXGBE_MDIO_AUTO_NEG_DEV_TYPE, &autoneg_reg);
878 
879 		autoneg_reg &= ~AN_CTRL1_AUTOEN;
880 
881 		hw->phy.ops.write_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
882 			      IXGBE_MDIO_AUTO_NEG_DEV_TYPE, autoneg_reg);
883 	} else {
884 		/* Blocked by MNG FW so don't reset PHY */
885 		if (ixgbe_check_reset_blocked(hw))
886 			return status;
887 
888 		/* Restart PHY auto-negotiation. */
889 		hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
890 		    IXGBE_MDIO_AUTO_NEG_DEV_TYPE, &autoneg_reg);
891 
892 		autoneg_reg |= IXGBE_MII_RESTART | AN_CTRL1_AUTOEN;
893 
894 		hw->phy.ops.write_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
895 		    IXGBE_MDIO_AUTO_NEG_DEV_TYPE, autoneg_reg);
896 	}
897 
898 	return status;
899 }
900 
901 /**
902  *  ixgbe_setup_phy_link_speed_generic - Sets the auto advertised capabilities
903  *  @hw: pointer to hardware structure
904  *  @speed: new link speed
905  *  @autoneg_wait_to_complete: unused
906  **/
907 s32 ixgbe_setup_phy_link_speed_generic(struct ixgbe_hw *hw,
908 				       ixgbe_link_speed speed,
909 				       bool autoneg_wait_to_complete)
910 {
911 	UNREFERENCED_1PARAMETER(autoneg_wait_to_complete);
912 
913 	DEBUGFUNC("ixgbe_setup_phy_link_speed_generic");
914 
915 	/*
916 	 * Clear autoneg_advertised and set new values based on input link
917 	 * speed.
918 	 */
919 	hw->phy.autoneg_advertised = 0;
920 
921 	if (speed & IXGBE_LINK_SPEED_10GB_FULL)
922 		hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
923 
924 	if (speed & IXGBE_LINK_SPEED_5GB_FULL)
925 		hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_5GB_FULL;
926 
927 	if (speed & IXGBE_LINK_SPEED_2_5GB_FULL)
928 		hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_2_5GB_FULL;
929 
930 	if (speed & IXGBE_LINK_SPEED_1GB_FULL)
931 		hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
932 
933 	if (speed & IXGBE_LINK_SPEED_100_FULL)
934 		hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_100_FULL;
935 
936 	if (speed & IXGBE_LINK_SPEED_10_FULL)
937 		hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10_FULL;
938 
939 	/* Setup link based on the new speed settings */
940 	ixgbe_setup_phy_link(hw);
941 
942 	return IXGBE_SUCCESS;
943 }
944 
945 /**
946  * ixgbe_get_copper_speeds_supported - Get copper link speeds from phy
947  * @hw: pointer to hardware structure
948  *
949  * Determines the supported link capabilities by reading the PHY auto
950  * negotiation register.
951  **/
952 static s32 ixgbe_get_copper_speeds_supported(struct ixgbe_hw *hw)
953 {
954 	s32 status;
955 	u16 speed_ability;
956 
957 	status = hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_SPEED_ABILITY,
958 				      IXGBE_MDIO_PMA_PMD_DEV_TYPE,
959 				      &speed_ability);
960 	if (status)
961 		return status;
962 
963 	if (speed_ability & IXGBE_MDIO_PHY_SPEED_10G)
964 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_10GB_FULL;
965 	if (speed_ability & IXGBE_MDIO_PHY_SPEED_1G)
966 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_1GB_FULL;
967 	if (speed_ability & IXGBE_MDIO_PHY_SPEED_100M)
968 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_100_FULL;
969 
970 	switch (hw->mac.type) {
971 	case ixgbe_mac_X550:
972 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_2_5GB_FULL;
973 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_5GB_FULL;
974 		break;
975 	case ixgbe_mac_X550EM_x:
976 	case ixgbe_mac_X550EM_a:
977 		hw->phy.speeds_supported &= ~IXGBE_LINK_SPEED_100_FULL;
978 		break;
979 	default:
980 		break;
981 	}
982 
983 	return status;
984 }
985 
986 /**
987  *  ixgbe_get_copper_link_capabilities_generic - Determines link capabilities
988  *  @hw: pointer to hardware structure
989  *  @speed: pointer to link speed
990  *  @autoneg: boolean auto-negotiation value
991  **/
992 s32 ixgbe_get_copper_link_capabilities_generic(struct ixgbe_hw *hw,
993 					       ixgbe_link_speed *speed,
994 					       bool *autoneg)
995 {
996 	s32 status = IXGBE_SUCCESS;
997 
998 	DEBUGFUNC("ixgbe_get_copper_link_capabilities_generic");
999 
1000 	*autoneg = TRUE;
1001 	if (!hw->phy.speeds_supported)
1002 		status = ixgbe_get_copper_speeds_supported(hw);
1003 
1004 	*speed = hw->phy.speeds_supported;
1005 	return status;
1006 }
1007 
1008 /**
1009  *  ixgbe_check_phy_link_tnx - Determine link and speed status
1010  *  @hw: pointer to hardware structure
1011  *  @speed: current link speed
1012  *  @link_up: TRUE is link is up, FALSE otherwise
1013  *
1014  *  Reads the VS1 register to determine if link is up and the current speed for
1015  *  the PHY.
1016  **/
1017 s32 ixgbe_check_phy_link_tnx(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
1018 			     bool *link_up)
1019 {
1020 	s32 status = IXGBE_SUCCESS;
1021 	u32 time_out;
1022 	u32 max_time_out = 10;
1023 	u16 phy_link = 0;
1024 	u16 phy_speed = 0;
1025 	u16 phy_data = 0;
1026 
1027 	DEBUGFUNC("ixgbe_check_phy_link_tnx");
1028 
1029 	/* Initialize speed and link to default case */
1030 	*link_up = FALSE;
1031 	*speed = IXGBE_LINK_SPEED_10GB_FULL;
1032 
1033 	/*
1034 	 * Check current speed and link status of the PHY register.
1035 	 * This is a vendor specific register and may have to
1036 	 * be changed for other copper PHYs.
1037 	 */
1038 	for (time_out = 0; time_out < max_time_out; time_out++) {
1039 		usec_delay(10);
1040 		status = hw->phy.ops.read_reg(hw,
1041 					IXGBE_MDIO_VENDOR_SPECIFIC_1_STATUS,
1042 					IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
1043 					&phy_data);
1044 		phy_link = phy_data & IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS;
1045 		phy_speed = phy_data &
1046 				 IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS;
1047 		if (phy_link == IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS) {
1048 			*link_up = TRUE;
1049 			if (phy_speed ==
1050 			    IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS)
1051 				*speed = IXGBE_LINK_SPEED_1GB_FULL;
1052 			break;
1053 		}
1054 	}
1055 
1056 	return status;
1057 }
1058 
1059 /**
1060  *	ixgbe_setup_phy_link_tnx - Set and restart auto-neg
1061  *	@hw: pointer to hardware structure
1062  *
1063  *	Restart auto-negotiation and PHY and waits for completion.
1064  **/
1065 s32 ixgbe_setup_phy_link_tnx(struct ixgbe_hw *hw)
1066 {
1067 	s32 status = IXGBE_SUCCESS;
1068 	u16 autoneg_reg = IXGBE_MII_AUTONEG_REG;
1069 	bool autoneg = FALSE;
1070 	ixgbe_link_speed speed;
1071 
1072 	DEBUGFUNC("ixgbe_setup_phy_link_tnx");
1073 
1074 	ixgbe_get_copper_link_capabilities_generic(hw, &speed, &autoneg);
1075 
1076 	if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
1077 		/* Set or unset auto-negotiation 10G advertisement */
1078 		hw->phy.ops.read_reg(hw, IXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
1079 				     IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
1080 				     &autoneg_reg);
1081 
1082 		autoneg_reg &= ~IXGBE_MII_10GBASE_T_ADVERTISE;
1083 		if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL)
1084 			autoneg_reg |= IXGBE_MII_10GBASE_T_ADVERTISE;
1085 
1086 		hw->phy.ops.write_reg(hw, IXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
1087 				      IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
1088 				      autoneg_reg);
1089 	}
1090 
1091 	if (speed & IXGBE_LINK_SPEED_1GB_FULL) {
1092 		/* Set or unset auto-negotiation 1G advertisement */
1093 		hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_XNP_TX_REG,
1094 				     IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
1095 				     &autoneg_reg);
1096 
1097 		autoneg_reg &= ~IXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
1098 		if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL)
1099 			autoneg_reg |= IXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
1100 
1101 		hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_XNP_TX_REG,
1102 				      IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
1103 				      autoneg_reg);
1104 	}
1105 
1106 	if (speed & IXGBE_LINK_SPEED_100_FULL) {
1107 		/* Set or unset auto-negotiation 100M advertisement */
1108 		hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_ADVERTISE_REG,
1109 				     IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
1110 				     &autoneg_reg);
1111 
1112 		autoneg_reg &= ~IXGBE_MII_100BASE_T_ADVERTISE;
1113 		if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL)
1114 			autoneg_reg |= IXGBE_MII_100BASE_T_ADVERTISE;
1115 
1116 		hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_ADVERTISE_REG,
1117 				      IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
1118 				      autoneg_reg);
1119 	}
1120 
1121 	/* Blocked by MNG FW so don't reset PHY */
1122 	if (ixgbe_check_reset_blocked(hw))
1123 		return status;
1124 
1125 	/* Restart PHY auto-negotiation. */
1126 	hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
1127 			     IXGBE_MDIO_AUTO_NEG_DEV_TYPE, &autoneg_reg);
1128 
1129 	autoneg_reg |= IXGBE_MII_RESTART;
1130 
1131 	hw->phy.ops.write_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
1132 			      IXGBE_MDIO_AUTO_NEG_DEV_TYPE, autoneg_reg);
1133 
1134 	return status;
1135 }
1136 
1137 /**
1138  *  ixgbe_get_phy_firmware_version_tnx - Gets the PHY Firmware Version
1139  *  @hw: pointer to hardware structure
1140  *  @firmware_version: pointer to the PHY Firmware Version
1141  **/
1142 s32 ixgbe_get_phy_firmware_version_tnx(struct ixgbe_hw *hw,
1143 				       u16 *firmware_version)
1144 {
1145 	s32 status;
1146 
1147 	DEBUGFUNC("ixgbe_get_phy_firmware_version_tnx");
1148 
1149 	status = hw->phy.ops.read_reg(hw, TNX_FW_REV,
1150 				      IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
1151 				      firmware_version);
1152 
1153 	return status;
1154 }
1155 
1156 /**
1157  *  ixgbe_get_phy_firmware_version_generic - Gets the PHY Firmware Version
1158  *  @hw: pointer to hardware structure
1159  *  @firmware_version: pointer to the PHY Firmware Version
1160  **/
1161 s32 ixgbe_get_phy_firmware_version_generic(struct ixgbe_hw *hw,
1162 					   u16 *firmware_version)
1163 {
1164 	s32 status;
1165 
1166 	DEBUGFUNC("ixgbe_get_phy_firmware_version_generic");
1167 
1168 	status = hw->phy.ops.read_reg(hw, AQ_FW_REV,
1169 				      IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
1170 				      firmware_version);
1171 
1172 	return status;
1173 }
1174 
1175 /**
1176  *  ixgbe_reset_phy_nl - Performs a PHY reset
1177  *  @hw: pointer to hardware structure
1178  **/
1179 s32 ixgbe_reset_phy_nl(struct ixgbe_hw *hw)
1180 {
1181 	u16 phy_offset, control, eword, edata, block_crc;
1182 	bool end_data = FALSE;
1183 	u16 list_offset, data_offset;
1184 	u16 phy_data = 0;
1185 	s32 ret_val = IXGBE_SUCCESS;
1186 	u32 i;
1187 
1188 	DEBUGFUNC("ixgbe_reset_phy_nl");
1189 
1190 	/* Blocked by MNG FW so bail */
1191 	if (ixgbe_check_reset_blocked(hw))
1192 		goto out;
1193 
1194 	hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_XS_CONTROL,
1195 			     IXGBE_MDIO_PHY_XS_DEV_TYPE, &phy_data);
1196 
1197 	/* reset the PHY and poll for completion */
1198 	hw->phy.ops.write_reg(hw, IXGBE_MDIO_PHY_XS_CONTROL,
1199 			      IXGBE_MDIO_PHY_XS_DEV_TYPE,
1200 			      (phy_data | IXGBE_MDIO_PHY_XS_RESET));
1201 
1202 	for (i = 0; i < 100; i++) {
1203 		hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_XS_CONTROL,
1204 				     IXGBE_MDIO_PHY_XS_DEV_TYPE, &phy_data);
1205 		if ((phy_data & IXGBE_MDIO_PHY_XS_RESET) == 0)
1206 			break;
1207 		msec_delay(10);
1208 	}
1209 
1210 	if ((phy_data & IXGBE_MDIO_PHY_XS_RESET) != 0) {
1211 		DEBUGOUT("PHY reset did not complete.\n");
1212 		ret_val = IXGBE_ERR_PHY;
1213 		goto out;
1214 	}
1215 
1216 	/* Get init offsets */
1217 	ret_val = ixgbe_get_sfp_init_sequence_offsets(hw, &list_offset,
1218 						      &data_offset);
1219 	if (ret_val != IXGBE_SUCCESS)
1220 		goto out;
1221 
1222 	ret_val = hw->eeprom.ops.read(hw, data_offset, &block_crc);
1223 	data_offset++;
1224 	while (!end_data) {
1225 		/*
1226 		 * Read control word from PHY init contents offset
1227 		 */
1228 		ret_val = hw->eeprom.ops.read(hw, data_offset, &eword);
1229 		if (ret_val)
1230 			goto err_eeprom;
1231 		control = (eword & IXGBE_CONTROL_MASK_NL) >>
1232 			   IXGBE_CONTROL_SHIFT_NL;
1233 		edata = eword & IXGBE_DATA_MASK_NL;
1234 		switch (control) {
1235 		case IXGBE_DELAY_NL:
1236 			data_offset++;
1237 			DEBUGOUT1("DELAY: %d MS\n", edata);
1238 			msec_delay(edata);
1239 			break;
1240 		case IXGBE_DATA_NL:
1241 			DEBUGOUT("DATA:\n");
1242 			data_offset++;
1243 			ret_val = hw->eeprom.ops.read(hw, data_offset,
1244 						      &phy_offset);
1245 			if (ret_val)
1246 				goto err_eeprom;
1247 			data_offset++;
1248 			for (i = 0; i < edata; i++) {
1249 				ret_val = hw->eeprom.ops.read(hw, data_offset,
1250 							      &eword);
1251 				if (ret_val)
1252 					goto err_eeprom;
1253 				hw->phy.ops.write_reg(hw, phy_offset,
1254 						      IXGBE_TWINAX_DEV, eword);
1255 				DEBUGOUT2("Wrote %4.4x to %4.4x\n", eword,
1256 					  phy_offset);
1257 				data_offset++;
1258 				phy_offset++;
1259 			}
1260 			break;
1261 		case IXGBE_CONTROL_NL:
1262 			data_offset++;
1263 			DEBUGOUT("CONTROL:\n");
1264 			if (edata == IXGBE_CONTROL_EOL_NL) {
1265 				DEBUGOUT("EOL\n");
1266 				end_data = TRUE;
1267 			} else if (edata == IXGBE_CONTROL_SOL_NL) {
1268 				DEBUGOUT("SOL\n");
1269 			} else {
1270 				DEBUGOUT("Bad control value\n");
1271 				ret_val = IXGBE_ERR_PHY;
1272 				goto out;
1273 			}
1274 			break;
1275 		default:
1276 			DEBUGOUT("Bad control type\n");
1277 			ret_val = IXGBE_ERR_PHY;
1278 			goto out;
1279 		}
1280 	}
1281 
1282 out:
1283 	return ret_val;
1284 
1285 err_eeprom:
1286 	ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
1287 		      "eeprom read at offset %d failed", data_offset);
1288 	return IXGBE_ERR_PHY;
1289 }
1290 
1291 /**
1292  *  ixgbe_identify_module_generic - Identifies module type
1293  *  @hw: pointer to hardware structure
1294  *
1295  *  Determines HW type and calls appropriate function.
1296  **/
1297 s32 ixgbe_identify_module_generic(struct ixgbe_hw *hw)
1298 {
1299 	s32 status = IXGBE_ERR_SFP_NOT_PRESENT;
1300 
1301 	DEBUGFUNC("ixgbe_identify_module_generic");
1302 
1303 	switch (hw->mac.ops.get_media_type(hw)) {
1304 	case ixgbe_media_type_fiber:
1305 		status = ixgbe_identify_sfp_module_generic(hw);
1306 		break;
1307 
1308 	case ixgbe_media_type_fiber_qsfp:
1309 		status = ixgbe_identify_qsfp_module_generic(hw);
1310 		break;
1311 
1312 	default:
1313 		hw->phy.sfp_type = ixgbe_sfp_type_not_present;
1314 		status = IXGBE_ERR_SFP_NOT_PRESENT;
1315 		break;
1316 	}
1317 
1318 	return status;
1319 }
1320 
1321 /**
1322  *  ixgbe_identify_sfp_module_generic - Identifies SFP modules
1323  *  @hw: pointer to hardware structure
1324  *
1325  *  Searches for and identifies the SFP module and assigns appropriate PHY type.
1326  **/
1327 s32 ixgbe_identify_sfp_module_generic(struct ixgbe_hw *hw)
1328 {
1329 	s32 status = IXGBE_ERR_PHY_ADDR_INVALID;
1330 	u32 vendor_oui = 0;
1331 	enum ixgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
1332 	u8 identifier = 0;
1333 	u8 comp_codes_1g = 0;
1334 	u8 comp_codes_10g = 0;
1335 	u8 oui_bytes[3] = {0, 0, 0};
1336 	u8 cable_tech = 0;
1337 	u8 cable_spec = 0;
1338 	u16 enforce_sfp = 0;
1339 
1340 	DEBUGFUNC("ixgbe_identify_sfp_module_generic");
1341 
1342 	if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_fiber) {
1343 		hw->phy.sfp_type = ixgbe_sfp_type_not_present;
1344 		status = IXGBE_ERR_SFP_NOT_PRESENT;
1345 		goto out;
1346 	}
1347 
1348 	/* LAN ID is needed for I2C access */
1349 	hw->mac.ops.set_lan_id(hw);
1350 
1351 	status = hw->phy.ops.read_i2c_eeprom(hw,
1352 					     IXGBE_SFF_IDENTIFIER,
1353 					     &identifier);
1354 
1355 	if (status != IXGBE_SUCCESS)
1356 		goto err_read_i2c_eeprom;
1357 
1358 	if (identifier != IXGBE_SFF_IDENTIFIER_SFP) {
1359 		hw->phy.type = ixgbe_phy_sfp_unsupported;
1360 		status = IXGBE_ERR_SFP_NOT_SUPPORTED;
1361 	} else {
1362 		status = hw->phy.ops.read_i2c_eeprom(hw,
1363 						     IXGBE_SFF_1GBE_COMP_CODES,
1364 						     &comp_codes_1g);
1365 
1366 		if (status != IXGBE_SUCCESS)
1367 			goto err_read_i2c_eeprom;
1368 
1369 		status = hw->phy.ops.read_i2c_eeprom(hw,
1370 						     IXGBE_SFF_10GBE_COMP_CODES,
1371 						     &comp_codes_10g);
1372 
1373 		if (status != IXGBE_SUCCESS)
1374 			goto err_read_i2c_eeprom;
1375 		status = hw->phy.ops.read_i2c_eeprom(hw,
1376 						     IXGBE_SFF_CABLE_TECHNOLOGY,
1377 						     &cable_tech);
1378 
1379 		if (status != IXGBE_SUCCESS)
1380 			goto err_read_i2c_eeprom;
1381 
1382 		 /* ID Module
1383 		  * =========
1384 		  * 0   SFP_DA_CU
1385 		  * 1   SFP_SR
1386 		  * 2   SFP_LR
1387 		  * 3   SFP_DA_CORE0 - 82599-specific
1388 		  * 4   SFP_DA_CORE1 - 82599-specific
1389 		  * 5   SFP_SR/LR_CORE0 - 82599-specific
1390 		  * 6   SFP_SR/LR_CORE1 - 82599-specific
1391 		  * 7   SFP_act_lmt_DA_CORE0 - 82599-specific
1392 		  * 8   SFP_act_lmt_DA_CORE1 - 82599-specific
1393 		  * 9   SFP_1g_cu_CORE0 - 82599-specific
1394 		  * 10  SFP_1g_cu_CORE1 - 82599-specific
1395 		  * 11  SFP_1g_sx_CORE0 - 82599-specific
1396 		  * 12  SFP_1g_sx_CORE1 - 82599-specific
1397 		  */
1398 		if (hw->mac.type == ixgbe_mac_82598EB) {
1399 			if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
1400 				hw->phy.sfp_type = ixgbe_sfp_type_da_cu;
1401 			else if (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)
1402 				hw->phy.sfp_type = ixgbe_sfp_type_sr;
1403 			else if (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)
1404 				hw->phy.sfp_type = ixgbe_sfp_type_lr;
1405 			else
1406 				hw->phy.sfp_type = ixgbe_sfp_type_unknown;
1407 		} else {
1408 			if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE) {
1409 				if (hw->bus.lan_id == 0)
1410 					hw->phy.sfp_type =
1411 						     ixgbe_sfp_type_da_cu_core0;
1412 				else
1413 					hw->phy.sfp_type =
1414 						     ixgbe_sfp_type_da_cu_core1;
1415 			} else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE) {
1416 				hw->phy.ops.read_i2c_eeprom(
1417 						hw, IXGBE_SFF_CABLE_SPEC_COMP,
1418 						&cable_spec);
1419 				if (cable_spec &
1420 				    IXGBE_SFF_DA_SPEC_ACTIVE_LIMITING) {
1421 					if (hw->bus.lan_id == 0)
1422 						hw->phy.sfp_type =
1423 						ixgbe_sfp_type_da_act_lmt_core0;
1424 					else
1425 						hw->phy.sfp_type =
1426 						ixgbe_sfp_type_da_act_lmt_core1;
1427 				} else {
1428 					hw->phy.sfp_type =
1429 							ixgbe_sfp_type_unknown;
1430 				}
1431 			} else if (comp_codes_10g &
1432 				   (IXGBE_SFF_10GBASESR_CAPABLE |
1433 				    IXGBE_SFF_10GBASELR_CAPABLE)) {
1434 				if (hw->bus.lan_id == 0)
1435 					hw->phy.sfp_type =
1436 						      ixgbe_sfp_type_srlr_core0;
1437 				else
1438 					hw->phy.sfp_type =
1439 						      ixgbe_sfp_type_srlr_core1;
1440 			} else if (comp_codes_1g & IXGBE_SFF_1GBASET_CAPABLE) {
1441 				if (hw->bus.lan_id == 0)
1442 					hw->phy.sfp_type =
1443 						ixgbe_sfp_type_1g_cu_core0;
1444 				else
1445 					hw->phy.sfp_type =
1446 						ixgbe_sfp_type_1g_cu_core1;
1447 			} else if (comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) {
1448 				if (hw->bus.lan_id == 0)
1449 					hw->phy.sfp_type =
1450 						ixgbe_sfp_type_1g_sx_core0;
1451 				else
1452 					hw->phy.sfp_type =
1453 						ixgbe_sfp_type_1g_sx_core1;
1454 			} else if (comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) {
1455 				if (hw->bus.lan_id == 0)
1456 					hw->phy.sfp_type =
1457 						ixgbe_sfp_type_1g_lx_core0;
1458 				else
1459 					hw->phy.sfp_type =
1460 						ixgbe_sfp_type_1g_lx_core1;
1461 			} else {
1462 				hw->phy.sfp_type = ixgbe_sfp_type_unknown;
1463 			}
1464 		}
1465 
1466 		if (hw->phy.sfp_type != stored_sfp_type)
1467 			hw->phy.sfp_setup_needed = TRUE;
1468 
1469 		/* Determine if the SFP+ PHY is dual speed or not. */
1470 		hw->phy.multispeed_fiber = FALSE;
1471 		if (((comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) &&
1472 		   (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)) ||
1473 		   ((comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) &&
1474 		   (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)))
1475 			hw->phy.multispeed_fiber = TRUE;
1476 
1477 		/* Determine PHY vendor */
1478 		if (hw->phy.type != ixgbe_phy_nl) {
1479 			hw->phy.id = identifier;
1480 			status = hw->phy.ops.read_i2c_eeprom(hw,
1481 						    IXGBE_SFF_VENDOR_OUI_BYTE0,
1482 						    &oui_bytes[0]);
1483 
1484 			if (status != IXGBE_SUCCESS)
1485 				goto err_read_i2c_eeprom;
1486 
1487 			status = hw->phy.ops.read_i2c_eeprom(hw,
1488 						    IXGBE_SFF_VENDOR_OUI_BYTE1,
1489 						    &oui_bytes[1]);
1490 
1491 			if (status != IXGBE_SUCCESS)
1492 				goto err_read_i2c_eeprom;
1493 
1494 			status = hw->phy.ops.read_i2c_eeprom(hw,
1495 						    IXGBE_SFF_VENDOR_OUI_BYTE2,
1496 						    &oui_bytes[2]);
1497 
1498 			if (status != IXGBE_SUCCESS)
1499 				goto err_read_i2c_eeprom;
1500 
1501 			vendor_oui =
1502 			  ((oui_bytes[0] << IXGBE_SFF_VENDOR_OUI_BYTE0_SHIFT) |
1503 			   (oui_bytes[1] << IXGBE_SFF_VENDOR_OUI_BYTE1_SHIFT) |
1504 			   (oui_bytes[2] << IXGBE_SFF_VENDOR_OUI_BYTE2_SHIFT));
1505 
1506 			switch (vendor_oui) {
1507 			case IXGBE_SFF_VENDOR_OUI_TYCO:
1508 				if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
1509 					hw->phy.type =
1510 						    ixgbe_phy_sfp_passive_tyco;
1511 				break;
1512 			case IXGBE_SFF_VENDOR_OUI_FTL:
1513 				if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
1514 					hw->phy.type = ixgbe_phy_sfp_ftl_active;
1515 				else
1516 					hw->phy.type = ixgbe_phy_sfp_ftl;
1517 				break;
1518 			case IXGBE_SFF_VENDOR_OUI_AVAGO:
1519 				hw->phy.type = ixgbe_phy_sfp_avago;
1520 				break;
1521 			case IXGBE_SFF_VENDOR_OUI_INTEL:
1522 				hw->phy.type = ixgbe_phy_sfp_intel;
1523 				break;
1524 			default:
1525 				hw->phy.type = ixgbe_phy_sfp_unknown;
1526 				break;
1527 			}
1528 		}
1529 
1530 		/* Allow any DA cable vendor */
1531 		if (cable_tech & (IXGBE_SFF_DA_PASSIVE_CABLE |
1532 			IXGBE_SFF_DA_ACTIVE_CABLE)) {
1533 			if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
1534 				hw->phy.type = ixgbe_phy_sfp_passive_unknown;
1535 			else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
1536 				hw->phy.type = ixgbe_phy_sfp_active_unknown;
1537 			status = IXGBE_SUCCESS;
1538 			goto out;
1539 		}
1540 
1541 		/* Verify supported 1G SFP modules */
1542 		if (comp_codes_10g == 0 &&
1543 		    !(hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
1544 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
1545 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
1546 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
1547 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core0 ||
1548 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core1)) {
1549 			hw->phy.type = ixgbe_phy_sfp_unsupported;
1550 			status = IXGBE_ERR_SFP_NOT_SUPPORTED;
1551 			goto out;
1552 		}
1553 
1554 		/* Anything else 82598-based is supported */
1555 		if (hw->mac.type == ixgbe_mac_82598EB) {
1556 			status = IXGBE_SUCCESS;
1557 			goto out;
1558 		}
1559 
1560 		ixgbe_get_device_caps(hw, &enforce_sfp);
1561 		if (!(enforce_sfp & IXGBE_DEVICE_CAPS_ALLOW_ANY_SFP) &&
1562 		    !(hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
1563 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
1564 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
1565 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
1566 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core0 ||
1567 		      hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core1)) {
1568 			/* Make sure we're a supported PHY type */
1569 			if (hw->phy.type == ixgbe_phy_sfp_intel) {
1570 				status = IXGBE_SUCCESS;
1571 			} else {
1572 				if (hw->allow_unsupported_sfp == TRUE) {
1573 					EWARN(hw, "WARNING: Intel (R) Network Connections are quality tested using Intel (R) Ethernet Optics. Using untested modules is not supported and may cause unstable operation or damage to the module or the adapter. Intel Corporation is not responsible for any harm caused by using untested modules.\n");
1574 					status = IXGBE_SUCCESS;
1575 				} else {
1576 					DEBUGOUT("SFP+ module not supported\n");
1577 					hw->phy.type =
1578 						ixgbe_phy_sfp_unsupported;
1579 					status = IXGBE_ERR_SFP_NOT_SUPPORTED;
1580 				}
1581 			}
1582 		} else {
1583 			status = IXGBE_SUCCESS;
1584 		}
1585 	}
1586 
1587 out:
1588 	if (hw->phy.type == ixgbe_phy_sfp_unsupported)
1589 		hw->need_unsupported_sfp_recovery = true;
1590 	return status;
1591 
1592 err_read_i2c_eeprom:
1593 	hw->phy.sfp_type = ixgbe_sfp_type_not_present;
1594 	if (hw->phy.type != ixgbe_phy_nl) {
1595 		hw->phy.id = 0;
1596 		hw->phy.type = ixgbe_phy_unknown;
1597 	}
1598 	return IXGBE_ERR_SFP_NOT_PRESENT;
1599 }
1600 
1601 /**
1602  *  ixgbe_get_supported_phy_sfp_layer_generic - Returns physical layer type
1603  *  @hw: pointer to hardware structure
1604  *
1605  *  Determines physical layer capabilities of the current SFP.
1606  */
1607 u64 ixgbe_get_supported_phy_sfp_layer_generic(struct ixgbe_hw *hw)
1608 {
1609 	u64 physical_layer = IXGBE_PHYSICAL_LAYER_UNKNOWN;
1610 	u8 comp_codes_10g = 0;
1611 	u8 comp_codes_1g = 0;
1612 
1613 	DEBUGFUNC("ixgbe_get_supported_phy_sfp_layer_generic");
1614 
1615 	hw->phy.ops.identify_sfp(hw);
1616 	if (hw->phy.sfp_type == ixgbe_sfp_type_not_present)
1617 		return physical_layer;
1618 
1619 	switch (hw->phy.type) {
1620 	case ixgbe_phy_sfp_passive_tyco:
1621 	case ixgbe_phy_sfp_passive_unknown:
1622 	case ixgbe_phy_qsfp_passive_unknown:
1623 		physical_layer = IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU;
1624 		break;
1625 	case ixgbe_phy_sfp_ftl_active:
1626 	case ixgbe_phy_sfp_active_unknown:
1627 	case ixgbe_phy_qsfp_active_unknown:
1628 		physical_layer = IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA;
1629 		break;
1630 	case ixgbe_phy_sfp_avago:
1631 	case ixgbe_phy_sfp_ftl:
1632 	case ixgbe_phy_sfp_intel:
1633 	case ixgbe_phy_sfp_unknown:
1634 		hw->phy.ops.read_i2c_eeprom(hw,
1635 		      IXGBE_SFF_1GBE_COMP_CODES, &comp_codes_1g);
1636 		hw->phy.ops.read_i2c_eeprom(hw,
1637 		      IXGBE_SFF_10GBE_COMP_CODES, &comp_codes_10g);
1638 		if (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)
1639 			physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_SR;
1640 		else if (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)
1641 			physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_LR;
1642 		else if (comp_codes_1g & IXGBE_SFF_1GBASET_CAPABLE)
1643 			physical_layer = IXGBE_PHYSICAL_LAYER_1000BASE_T;
1644 		else if (comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE)
1645 			physical_layer = IXGBE_PHYSICAL_LAYER_1000BASE_SX;
1646 		break;
1647 	case ixgbe_phy_qsfp_intel:
1648 	case ixgbe_phy_qsfp_unknown:
1649 		hw->phy.ops.read_i2c_eeprom(hw,
1650 		      IXGBE_SFF_QSFP_10GBE_COMP, &comp_codes_10g);
1651 		if (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)
1652 			physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_SR;
1653 		else if (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)
1654 			physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_LR;
1655 		break;
1656 	default:
1657 		break;
1658 	}
1659 
1660 	return physical_layer;
1661 }
1662 
1663 /**
1664  *  ixgbe_identify_qsfp_module_generic - Identifies QSFP modules
1665  *  @hw: pointer to hardware structure
1666  *
1667  *  Searches for and identifies the QSFP module and assigns appropriate PHY type
1668  **/
1669 s32 ixgbe_identify_qsfp_module_generic(struct ixgbe_hw *hw)
1670 {
1671 	s32 status = IXGBE_ERR_PHY_ADDR_INVALID;
1672 	u32 vendor_oui = 0;
1673 	enum ixgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
1674 	u8 identifier = 0;
1675 	u8 comp_codes_1g = 0;
1676 	u8 comp_codes_10g = 0;
1677 	u8 oui_bytes[3] = {0, 0, 0};
1678 	u16 enforce_sfp = 0;
1679 	u8 connector = 0;
1680 	u8 cable_length = 0;
1681 	u8 device_tech = 0;
1682 	bool active_cable = FALSE;
1683 
1684 	DEBUGFUNC("ixgbe_identify_qsfp_module_generic");
1685 
1686 	if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_fiber_qsfp) {
1687 		hw->phy.sfp_type = ixgbe_sfp_type_not_present;
1688 		status = IXGBE_ERR_SFP_NOT_PRESENT;
1689 		goto out;
1690 	}
1691 
1692 	/* LAN ID is needed for I2C access */
1693 	hw->mac.ops.set_lan_id(hw);
1694 
1695 	status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_IDENTIFIER,
1696 					     &identifier);
1697 
1698 	if (status != IXGBE_SUCCESS)
1699 		goto err_read_i2c_eeprom;
1700 
1701 	if (identifier != IXGBE_SFF_IDENTIFIER_QSFP_PLUS) {
1702 		hw->phy.type = ixgbe_phy_sfp_unsupported;
1703 		status = IXGBE_ERR_SFP_NOT_SUPPORTED;
1704 		goto out;
1705 	}
1706 
1707 	hw->phy.id = identifier;
1708 
1709 	status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_QSFP_10GBE_COMP,
1710 					     &comp_codes_10g);
1711 
1712 	if (status != IXGBE_SUCCESS)
1713 		goto err_read_i2c_eeprom;
1714 
1715 	status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_QSFP_1GBE_COMP,
1716 					     &comp_codes_1g);
1717 
1718 	if (status != IXGBE_SUCCESS)
1719 		goto err_read_i2c_eeprom;
1720 
1721 	if (comp_codes_10g & IXGBE_SFF_QSFP_DA_PASSIVE_CABLE) {
1722 		hw->phy.type = ixgbe_phy_qsfp_passive_unknown;
1723 		if (hw->bus.lan_id == 0)
1724 			hw->phy.sfp_type = ixgbe_sfp_type_da_cu_core0;
1725 		else
1726 			hw->phy.sfp_type = ixgbe_sfp_type_da_cu_core1;
1727 	} else if (comp_codes_10g & (IXGBE_SFF_10GBASESR_CAPABLE |
1728 				     IXGBE_SFF_10GBASELR_CAPABLE)) {
1729 		if (hw->bus.lan_id == 0)
1730 			hw->phy.sfp_type = ixgbe_sfp_type_srlr_core0;
1731 		else
1732 			hw->phy.sfp_type = ixgbe_sfp_type_srlr_core1;
1733 	} else {
1734 		if (comp_codes_10g & IXGBE_SFF_QSFP_DA_ACTIVE_CABLE)
1735 			active_cable = TRUE;
1736 
1737 		if (!active_cable) {
1738 			/* check for active DA cables that pre-date
1739 			 * SFF-8436 v3.6 */
1740 			hw->phy.ops.read_i2c_eeprom(hw,
1741 					IXGBE_SFF_QSFP_CONNECTOR,
1742 					&connector);
1743 
1744 			hw->phy.ops.read_i2c_eeprom(hw,
1745 					IXGBE_SFF_QSFP_CABLE_LENGTH,
1746 					&cable_length);
1747 
1748 			hw->phy.ops.read_i2c_eeprom(hw,
1749 					IXGBE_SFF_QSFP_DEVICE_TECH,
1750 					&device_tech);
1751 
1752 			if ((connector ==
1753 				     IXGBE_SFF_QSFP_CONNECTOR_NOT_SEPARABLE) &&
1754 			    (cable_length > 0) &&
1755 			    ((device_tech >> 4) ==
1756 				     IXGBE_SFF_QSFP_TRANSMITER_850NM_VCSEL))
1757 				active_cable = TRUE;
1758 		}
1759 
1760 		if (active_cable) {
1761 			hw->phy.type = ixgbe_phy_qsfp_active_unknown;
1762 			if (hw->bus.lan_id == 0)
1763 				hw->phy.sfp_type =
1764 						ixgbe_sfp_type_da_act_lmt_core0;
1765 			else
1766 				hw->phy.sfp_type =
1767 						ixgbe_sfp_type_da_act_lmt_core1;
1768 		} else {
1769 			/* unsupported module type */
1770 			hw->phy.type = ixgbe_phy_sfp_unsupported;
1771 			status = IXGBE_ERR_SFP_NOT_SUPPORTED;
1772 			goto out;
1773 		}
1774 	}
1775 
1776 	if (hw->phy.sfp_type != stored_sfp_type)
1777 		hw->phy.sfp_setup_needed = TRUE;
1778 
1779 	/* Determine if the QSFP+ PHY is dual speed or not. */
1780 	hw->phy.multispeed_fiber = FALSE;
1781 	if (((comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) &&
1782 	   (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)) ||
1783 	   ((comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) &&
1784 	   (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)))
1785 		hw->phy.multispeed_fiber = TRUE;
1786 
1787 	/* Determine PHY vendor for optical modules */
1788 	if (comp_codes_10g & (IXGBE_SFF_10GBASESR_CAPABLE |
1789 			      IXGBE_SFF_10GBASELR_CAPABLE)) {
1790 		status = hw->phy.ops.read_i2c_eeprom(hw,
1791 					    IXGBE_SFF_QSFP_VENDOR_OUI_BYTE0,
1792 					    &oui_bytes[0]);
1793 
1794 		if (status != IXGBE_SUCCESS)
1795 			goto err_read_i2c_eeprom;
1796 
1797 		status = hw->phy.ops.read_i2c_eeprom(hw,
1798 					    IXGBE_SFF_QSFP_VENDOR_OUI_BYTE1,
1799 					    &oui_bytes[1]);
1800 
1801 		if (status != IXGBE_SUCCESS)
1802 			goto err_read_i2c_eeprom;
1803 
1804 		status = hw->phy.ops.read_i2c_eeprom(hw,
1805 					    IXGBE_SFF_QSFP_VENDOR_OUI_BYTE2,
1806 					    &oui_bytes[2]);
1807 
1808 		if (status != IXGBE_SUCCESS)
1809 			goto err_read_i2c_eeprom;
1810 
1811 		vendor_oui =
1812 		  ((oui_bytes[0] << IXGBE_SFF_VENDOR_OUI_BYTE0_SHIFT) |
1813 		   (oui_bytes[1] << IXGBE_SFF_VENDOR_OUI_BYTE1_SHIFT) |
1814 		   (oui_bytes[2] << IXGBE_SFF_VENDOR_OUI_BYTE2_SHIFT));
1815 
1816 		if (vendor_oui == IXGBE_SFF_VENDOR_OUI_INTEL)
1817 			hw->phy.type = ixgbe_phy_qsfp_intel;
1818 		else
1819 			hw->phy.type = ixgbe_phy_qsfp_unknown;
1820 
1821 		ixgbe_get_device_caps(hw, &enforce_sfp);
1822 		if (!(enforce_sfp & IXGBE_DEVICE_CAPS_ALLOW_ANY_SFP)) {
1823 			/* Make sure we're a supported PHY type */
1824 			if (hw->phy.type == ixgbe_phy_qsfp_intel) {
1825 				status = IXGBE_SUCCESS;
1826 			} else {
1827 				if (hw->allow_unsupported_sfp == TRUE) {
1828 					EWARN(hw, "WARNING: Intel (R) Network Connections are quality tested using Intel (R) Ethernet Optics. Using untested modules is not supported and may cause unstable operation or damage to the module or the adapter. Intel Corporation is not responsible for any harm caused by using untested modules.\n");
1829 					status = IXGBE_SUCCESS;
1830 				} else {
1831 					DEBUGOUT("QSFP module not supported\n");
1832 					hw->phy.type =
1833 						ixgbe_phy_sfp_unsupported;
1834 					status = IXGBE_ERR_SFP_NOT_SUPPORTED;
1835 				}
1836 			}
1837 		} else {
1838 			status = IXGBE_SUCCESS;
1839 		}
1840 	}
1841 
1842 out:
1843 	if (hw->phy.type == ixgbe_phy_sfp_unsupported)
1844 		hw->need_unsupported_sfp_recovery = true;
1845 	return status;
1846 
1847 err_read_i2c_eeprom:
1848 	hw->phy.sfp_type = ixgbe_sfp_type_not_present;
1849 	hw->phy.id = 0;
1850 	hw->phy.type = ixgbe_phy_unknown;
1851 
1852 	return IXGBE_ERR_SFP_NOT_PRESENT;
1853 }
1854 
1855 /**
1856  *  ixgbe_get_sfp_init_sequence_offsets - Provides offset of PHY init sequence
1857  *  @hw: pointer to hardware structure
1858  *  @list_offset: offset to the SFP ID list
1859  *  @data_offset: offset to the SFP data block
1860  *
1861  *  Checks the MAC's EEPROM to see if it supports a given SFP+ module type, if
1862  *  so it returns the offsets to the phy init sequence block.
1863  **/
1864 s32 ixgbe_get_sfp_init_sequence_offsets(struct ixgbe_hw *hw,
1865 					u16 *list_offset,
1866 					u16 *data_offset)
1867 {
1868 	u16 sfp_id;
1869 	u16 sfp_type = hw->phy.sfp_type;
1870 
1871 	DEBUGFUNC("ixgbe_get_sfp_init_sequence_offsets");
1872 
1873 	if (hw->phy.sfp_type == ixgbe_sfp_type_unknown)
1874 		return IXGBE_ERR_SFP_NOT_SUPPORTED;
1875 
1876 	if (hw->phy.sfp_type == ixgbe_sfp_type_not_present)
1877 		return IXGBE_ERR_SFP_NOT_PRESENT;
1878 
1879 	if ((hw->device_id == IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM) &&
1880 	    (hw->phy.sfp_type == ixgbe_sfp_type_da_cu))
1881 		return IXGBE_ERR_SFP_NOT_SUPPORTED;
1882 
1883 	/*
1884 	 * Limiting active cables and 1G Phys must be initialized as
1885 	 * SR modules
1886 	 */
1887 	if (sfp_type == ixgbe_sfp_type_da_act_lmt_core0 ||
1888 	    sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
1889 	    sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
1890 	    sfp_type == ixgbe_sfp_type_1g_sx_core0)
1891 		sfp_type = ixgbe_sfp_type_srlr_core0;
1892 	else if (sfp_type == ixgbe_sfp_type_da_act_lmt_core1 ||
1893 		 sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
1894 		 sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
1895 		 sfp_type == ixgbe_sfp_type_1g_sx_core1)
1896 		sfp_type = ixgbe_sfp_type_srlr_core1;
1897 
1898 	/* Read offset to PHY init contents */
1899 	if (hw->eeprom.ops.read(hw, IXGBE_PHY_INIT_OFFSET_NL, list_offset)) {
1900 		ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
1901 			      "eeprom read at offset %d failed",
1902 			      IXGBE_PHY_INIT_OFFSET_NL);
1903 		return IXGBE_ERR_SFP_NO_INIT_SEQ_PRESENT;
1904 	}
1905 
1906 	if ((!*list_offset) || (*list_offset == 0xFFFF))
1907 		return IXGBE_ERR_SFP_NO_INIT_SEQ_PRESENT;
1908 
1909 	/* Shift offset to first ID word */
1910 	(*list_offset)++;
1911 
1912 	/*
1913 	 * Find the matching SFP ID in the EEPROM
1914 	 * and program the init sequence
1915 	 */
1916 	if (hw->eeprom.ops.read(hw, *list_offset, &sfp_id))
1917 		goto err_phy;
1918 
1919 	while (sfp_id != IXGBE_PHY_INIT_END_NL) {
1920 		if (sfp_id == sfp_type) {
1921 			(*list_offset)++;
1922 			if (hw->eeprom.ops.read(hw, *list_offset, data_offset))
1923 				goto err_phy;
1924 			if ((!*data_offset) || (*data_offset == 0xFFFF)) {
1925 				DEBUGOUT("SFP+ module not supported\n");
1926 				return IXGBE_ERR_SFP_NOT_SUPPORTED;
1927 			} else {
1928 				break;
1929 			}
1930 		} else {
1931 			(*list_offset) += 2;
1932 			if (hw->eeprom.ops.read(hw, *list_offset, &sfp_id))
1933 				goto err_phy;
1934 		}
1935 	}
1936 
1937 	if (sfp_id == IXGBE_PHY_INIT_END_NL) {
1938 		DEBUGOUT("No matching SFP+ module found\n");
1939 		return IXGBE_ERR_SFP_NOT_SUPPORTED;
1940 	}
1941 
1942 	return IXGBE_SUCCESS;
1943 
1944 err_phy:
1945 	ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
1946 		      "eeprom read at offset %d failed", *list_offset);
1947 	return IXGBE_ERR_PHY;
1948 }
1949 
1950 /**
1951  *  ixgbe_read_i2c_eeprom_generic - Reads 8 bit EEPROM word over I2C interface
1952  *  @hw: pointer to hardware structure
1953  *  @byte_offset: EEPROM byte offset to read
1954  *  @eeprom_data: value read
1955  *
1956  *  Performs byte read operation to SFP module's EEPROM over I2C interface.
1957  **/
1958 s32 ixgbe_read_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
1959 				  u8 *eeprom_data)
1960 {
1961 	DEBUGFUNC("ixgbe_read_i2c_eeprom_generic");
1962 
1963 	return hw->phy.ops.read_i2c_byte(hw, byte_offset,
1964 					 IXGBE_I2C_EEPROM_DEV_ADDR,
1965 					 eeprom_data);
1966 }
1967 
1968 /**
1969  *  ixgbe_read_i2c_sff8472_generic - Reads 8 bit word over I2C interface
1970  *  @hw: pointer to hardware structure
1971  *  @byte_offset: byte offset at address 0xA2
1972  *  @sff8472_data: value read
1973  *
1974  *  Performs byte read operation to SFP module's SFF-8472 data over I2C
1975  **/
1976 static s32 ixgbe_read_i2c_sff8472_generic(struct ixgbe_hw *hw, u8 byte_offset,
1977 					  u8 *sff8472_data)
1978 {
1979 	return hw->phy.ops.read_i2c_byte(hw, byte_offset,
1980 					 IXGBE_I2C_EEPROM_DEV_ADDR2,
1981 					 sff8472_data);
1982 }
1983 
1984 /**
1985  *  ixgbe_write_i2c_eeprom_generic - Writes 8 bit EEPROM word over I2C interface
1986  *  @hw: pointer to hardware structure
1987  *  @byte_offset: EEPROM byte offset to write
1988  *  @eeprom_data: value to write
1989  *
1990  *  Performs byte write operation to SFP module's EEPROM over I2C interface.
1991  **/
1992 s32 ixgbe_write_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
1993 				   u8 eeprom_data)
1994 {
1995 	DEBUGFUNC("ixgbe_write_i2c_eeprom_generic");
1996 
1997 	return hw->phy.ops.write_i2c_byte(hw, byte_offset,
1998 					  IXGBE_I2C_EEPROM_DEV_ADDR,
1999 					  eeprom_data);
2000 }
2001 
2002 /**
2003  * ixgbe_is_sfp_probe - Returns TRUE if SFP is being detected
2004  * @hw: pointer to hardware structure
2005  * @offset: eeprom offset to be read
2006  * @addr: I2C address to be read
2007  */
2008 static bool ixgbe_is_sfp_probe(struct ixgbe_hw *hw, u8 offset, u8 addr)
2009 {
2010 	if (addr == IXGBE_I2C_EEPROM_DEV_ADDR &&
2011 	    offset == IXGBE_SFF_IDENTIFIER &&
2012 	    hw->phy.sfp_type == ixgbe_sfp_type_not_present)
2013 		return TRUE;
2014 	return FALSE;
2015 }
2016 
2017 /**
2018  *  ixgbe_read_i2c_byte_generic_int - Reads 8 bit word over I2C
2019  *  @hw: pointer to hardware structure
2020  *  @byte_offset: byte offset to read
2021  *  @dev_addr: address to read from
2022  *  @data: value read
2023  *  @lock: TRUE if to take and release semaphore
2024  *
2025  *  Performs byte read operation to SFP module's EEPROM over I2C interface at
2026  *  a specified device address.
2027  **/
2028 static s32 ixgbe_read_i2c_byte_generic_int(struct ixgbe_hw *hw, u8 byte_offset,
2029 					   u8 dev_addr, u8 *data, bool lock)
2030 {
2031 	s32 status;
2032 	u32 max_retry = 10;
2033 	u32 retry = 0;
2034 	u32 swfw_mask = hw->phy.phy_semaphore_mask;
2035 	bool nack = 1;
2036 	*data = 0;
2037 
2038 	DEBUGFUNC("ixgbe_read_i2c_byte_generic");
2039 
2040 	if (hw->mac.type >= ixgbe_mac_X550)
2041 		max_retry = 3;
2042 	if (ixgbe_is_sfp_probe(hw, byte_offset, dev_addr))
2043 		max_retry = IXGBE_SFP_DETECT_RETRIES;
2044 
2045 	do {
2046 		if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
2047 			return IXGBE_ERR_SWFW_SYNC;
2048 
2049 		ixgbe_i2c_start(hw);
2050 
2051 		/* Device Address and write indication */
2052 		status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
2053 		if (status != IXGBE_SUCCESS)
2054 			goto fail;
2055 
2056 		status = ixgbe_get_i2c_ack(hw);
2057 		if (status != IXGBE_SUCCESS)
2058 			goto fail;
2059 
2060 		status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
2061 		if (status != IXGBE_SUCCESS)
2062 			goto fail;
2063 
2064 		status = ixgbe_get_i2c_ack(hw);
2065 		if (status != IXGBE_SUCCESS)
2066 			goto fail;
2067 
2068 		ixgbe_i2c_start(hw);
2069 
2070 		/* Device Address and read indication */
2071 		status = ixgbe_clock_out_i2c_byte(hw, (dev_addr | 0x1));
2072 		if (status != IXGBE_SUCCESS)
2073 			goto fail;
2074 
2075 		status = ixgbe_get_i2c_ack(hw);
2076 		if (status != IXGBE_SUCCESS)
2077 			goto fail;
2078 
2079 		status = ixgbe_clock_in_i2c_byte(hw, data);
2080 		if (status != IXGBE_SUCCESS)
2081 			goto fail;
2082 
2083 		status = ixgbe_clock_out_i2c_bit(hw, nack);
2084 		if (status != IXGBE_SUCCESS)
2085 			goto fail;
2086 
2087 		ixgbe_i2c_stop(hw);
2088 		if (lock)
2089 			hw->mac.ops.release_swfw_sync(hw, swfw_mask);
2090 		return IXGBE_SUCCESS;
2091 
2092 fail:
2093 		ixgbe_i2c_bus_clear(hw);
2094 		if (lock) {
2095 			hw->mac.ops.release_swfw_sync(hw, swfw_mask);
2096 			msec_delay(100);
2097 		}
2098 		retry++;
2099 		if (retry < max_retry)
2100 			DEBUGOUT("I2C byte read error - Retrying.\n");
2101 		else
2102 			DEBUGOUT("I2C byte read error.\n");
2103 
2104 	} while (retry < max_retry);
2105 
2106 	return status;
2107 }
2108 
2109 /**
2110  *  ixgbe_read_i2c_byte_generic - Reads 8 bit word over I2C
2111  *  @hw: pointer to hardware structure
2112  *  @byte_offset: byte offset to read
2113  *  @dev_addr: address to read from
2114  *  @data: value read
2115  *
2116  *  Performs byte read operation to SFP module's EEPROM over I2C interface at
2117  *  a specified device address.
2118  **/
2119 s32 ixgbe_read_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
2120 				u8 dev_addr, u8 *data)
2121 {
2122 	return ixgbe_read_i2c_byte_generic_int(hw, byte_offset, dev_addr,
2123 					       data, TRUE);
2124 }
2125 
2126 /**
2127  *  ixgbe_read_i2c_byte_generic_unlocked - Reads 8 bit word over I2C
2128  *  @hw: pointer to hardware structure
2129  *  @byte_offset: byte offset to read
2130  *  @dev_addr: address to read from
2131  *  @data: value read
2132  *
2133  *  Performs byte read operation to SFP module's EEPROM over I2C interface at
2134  *  a specified device address.
2135  **/
2136 s32 ixgbe_read_i2c_byte_generic_unlocked(struct ixgbe_hw *hw, u8 byte_offset,
2137 					 u8 dev_addr, u8 *data)
2138 {
2139 	return ixgbe_read_i2c_byte_generic_int(hw, byte_offset, dev_addr,
2140 					       data, FALSE);
2141 }
2142 
2143 /**
2144  *  ixgbe_write_i2c_byte_generic_int - Writes 8 bit word over I2C
2145  *  @hw: pointer to hardware structure
2146  *  @byte_offset: byte offset to write
2147  *  @dev_addr: address to write to
2148  *  @data: value to write
2149  *  @lock: TRUE if to take and release semaphore
2150  *
2151  *  Performs byte write operation to SFP module's EEPROM over I2C interface at
2152  *  a specified device address.
2153  **/
2154 static s32 ixgbe_write_i2c_byte_generic_int(struct ixgbe_hw *hw, u8 byte_offset,
2155 					    u8 dev_addr, u8 data, bool lock)
2156 {
2157 	s32 status;
2158 	u32 max_retry = 2;
2159 	u32 retry = 0;
2160 	u32 swfw_mask = hw->phy.phy_semaphore_mask;
2161 
2162 	DEBUGFUNC("ixgbe_write_i2c_byte_generic");
2163 
2164 	if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask) !=
2165 	    IXGBE_SUCCESS)
2166 		return IXGBE_ERR_SWFW_SYNC;
2167 
2168 	do {
2169 		ixgbe_i2c_start(hw);
2170 
2171 		status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
2172 		if (status != IXGBE_SUCCESS)
2173 			goto fail;
2174 
2175 		status = ixgbe_get_i2c_ack(hw);
2176 		if (status != IXGBE_SUCCESS)
2177 			goto fail;
2178 
2179 		status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
2180 		if (status != IXGBE_SUCCESS)
2181 			goto fail;
2182 
2183 		status = ixgbe_get_i2c_ack(hw);
2184 		if (status != IXGBE_SUCCESS)
2185 			goto fail;
2186 
2187 		status = ixgbe_clock_out_i2c_byte(hw, data);
2188 		if (status != IXGBE_SUCCESS)
2189 			goto fail;
2190 
2191 		status = ixgbe_get_i2c_ack(hw);
2192 		if (status != IXGBE_SUCCESS)
2193 			goto fail;
2194 
2195 		ixgbe_i2c_stop(hw);
2196 		if (lock)
2197 			hw->mac.ops.release_swfw_sync(hw, swfw_mask);
2198 		return IXGBE_SUCCESS;
2199 
2200 fail:
2201 		ixgbe_i2c_bus_clear(hw);
2202 		retry++;
2203 		if (retry < max_retry)
2204 			DEBUGOUT("I2C byte write error - Retrying.\n");
2205 		else
2206 			DEBUGOUT("I2C byte write error.\n");
2207 	} while (retry < max_retry);
2208 
2209 	if (lock)
2210 		hw->mac.ops.release_swfw_sync(hw, swfw_mask);
2211 
2212 	return status;
2213 }
2214 
2215 /**
2216  *  ixgbe_write_i2c_byte_generic - Writes 8 bit word over I2C
2217  *  @hw: pointer to hardware structure
2218  *  @byte_offset: byte offset to write
2219  *  @dev_addr: address to write to
2220  *  @data: value to write
2221  *
2222  *  Performs byte write operation to SFP module's EEPROM over I2C interface at
2223  *  a specified device address.
2224  **/
2225 s32 ixgbe_write_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
2226 				 u8 dev_addr, u8 data)
2227 {
2228 	return ixgbe_write_i2c_byte_generic_int(hw, byte_offset, dev_addr,
2229 						data, TRUE);
2230 }
2231 
2232 /**
2233  *  ixgbe_write_i2c_byte_generic_unlocked - Writes 8 bit word over I2C
2234  *  @hw: pointer to hardware structure
2235  *  @byte_offset: byte offset to write
2236  *  @dev_addr: address to write to
2237  *  @data: value to write
2238  *
2239  *  Performs byte write operation to SFP module's EEPROM over I2C interface at
2240  *  a specified device address.
2241  **/
2242 s32 ixgbe_write_i2c_byte_generic_unlocked(struct ixgbe_hw *hw, u8 byte_offset,
2243 					  u8 dev_addr, u8 data)
2244 {
2245 	return ixgbe_write_i2c_byte_generic_int(hw, byte_offset, dev_addr,
2246 						data, FALSE);
2247 }
2248 
2249 /**
2250  *  ixgbe_i2c_start - Sets I2C start condition
2251  *  @hw: pointer to hardware structure
2252  *
2253  *  Sets I2C start condition (High -> Low on SDA while SCL is High)
2254  *  Set bit-bang mode on X550 hardware.
2255  **/
2256 static void ixgbe_i2c_start(struct ixgbe_hw *hw)
2257 {
2258 	u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2259 
2260 	DEBUGFUNC("ixgbe_i2c_start");
2261 
2262 	i2cctl |= IXGBE_I2C_BB_EN_BY_MAC(hw);
2263 
2264 	/* Start condition must begin with data and clock high */
2265 	ixgbe_set_i2c_data(hw, &i2cctl, 1);
2266 	ixgbe_raise_i2c_clk(hw, &i2cctl);
2267 
2268 	/* Setup time for start condition (4.7us) */
2269 	usec_delay(IXGBE_I2C_T_SU_STA);
2270 
2271 	ixgbe_set_i2c_data(hw, &i2cctl, 0);
2272 
2273 	/* Hold time for start condition (4us) */
2274 	usec_delay(IXGBE_I2C_T_HD_STA);
2275 
2276 	ixgbe_lower_i2c_clk(hw, &i2cctl);
2277 
2278 	/* Minimum low period of clock is 4.7 us */
2279 	usec_delay(IXGBE_I2C_T_LOW);
2280 
2281 }
2282 
2283 /**
2284  *  ixgbe_i2c_stop - Sets I2C stop condition
2285  *  @hw: pointer to hardware structure
2286  *
2287  *  Sets I2C stop condition (Low -> High on SDA while SCL is High)
2288  *  Disables bit-bang mode and negates data output enable on X550
2289  *  hardware.
2290  **/
2291 static void ixgbe_i2c_stop(struct ixgbe_hw *hw)
2292 {
2293 	u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2294 	u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN_BY_MAC(hw);
2295 	u32 clk_oe_bit = IXGBE_I2C_CLK_OE_N_EN_BY_MAC(hw);
2296 	u32 bb_en_bit = IXGBE_I2C_BB_EN_BY_MAC(hw);
2297 
2298 	DEBUGFUNC("ixgbe_i2c_stop");
2299 
2300 	/* Stop condition must begin with data low and clock high */
2301 	ixgbe_set_i2c_data(hw, &i2cctl, 0);
2302 	ixgbe_raise_i2c_clk(hw, &i2cctl);
2303 
2304 	/* Setup time for stop condition (4us) */
2305 	usec_delay(IXGBE_I2C_T_SU_STO);
2306 
2307 	ixgbe_set_i2c_data(hw, &i2cctl, 1);
2308 
2309 	/* bus free time between stop and start (4.7us)*/
2310 	usec_delay(IXGBE_I2C_T_BUF);
2311 
2312 	if (bb_en_bit || data_oe_bit || clk_oe_bit) {
2313 		i2cctl &= ~bb_en_bit;
2314 		i2cctl |= data_oe_bit | clk_oe_bit;
2315 		IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), i2cctl);
2316 		IXGBE_WRITE_FLUSH(hw);
2317 	}
2318 }
2319 
2320 /**
2321  *  ixgbe_clock_in_i2c_byte - Clocks in one byte via I2C
2322  *  @hw: pointer to hardware structure
2323  *  @data: data byte to clock in
2324  *
2325  *  Clocks in one byte data via I2C data/clock
2326  **/
2327 static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data)
2328 {
2329 	s32 i;
2330 	bool bit = 0;
2331 
2332 	DEBUGFUNC("ixgbe_clock_in_i2c_byte");
2333 
2334 	*data = 0;
2335 	for (i = 7; i >= 0; i--) {
2336 		ixgbe_clock_in_i2c_bit(hw, &bit);
2337 		*data |= bit << i;
2338 	}
2339 
2340 	return IXGBE_SUCCESS;
2341 }
2342 
2343 /**
2344  *  ixgbe_clock_out_i2c_byte - Clocks out one byte via I2C
2345  *  @hw: pointer to hardware structure
2346  *  @data: data byte clocked out
2347  *
2348  *  Clocks out one byte data via I2C data/clock
2349  **/
2350 static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data)
2351 {
2352 	s32 status = IXGBE_SUCCESS;
2353 	s32 i;
2354 	u32 i2cctl;
2355 	bool bit;
2356 
2357 	DEBUGFUNC("ixgbe_clock_out_i2c_byte");
2358 
2359 	for (i = 7; i >= 0; i--) {
2360 		bit = (data >> i) & 0x1;
2361 		status = ixgbe_clock_out_i2c_bit(hw, bit);
2362 
2363 		if (status != IXGBE_SUCCESS)
2364 			break;
2365 	}
2366 
2367 	/* Release SDA line (set high) */
2368 	i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2369 	i2cctl |= IXGBE_I2C_DATA_OUT_BY_MAC(hw);
2370 	i2cctl |= IXGBE_I2C_DATA_OE_N_EN_BY_MAC(hw);
2371 	IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), i2cctl);
2372 	IXGBE_WRITE_FLUSH(hw);
2373 
2374 	return status;
2375 }
2376 
2377 /**
2378  *  ixgbe_get_i2c_ack - Polls for I2C ACK
2379  *  @hw: pointer to hardware structure
2380  *
2381  *  Clocks in/out one bit via I2C data/clock
2382  **/
2383 static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw)
2384 {
2385 	u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN_BY_MAC(hw);
2386 	s32 status = IXGBE_SUCCESS;
2387 	u32 i = 0;
2388 	u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2389 	u32 timeout = 10;
2390 	bool ack = 1;
2391 
2392 	DEBUGFUNC("ixgbe_get_i2c_ack");
2393 
2394 	if (data_oe_bit) {
2395 		i2cctl |= IXGBE_I2C_DATA_OUT_BY_MAC(hw);
2396 		i2cctl |= data_oe_bit;
2397 		IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), i2cctl);
2398 		IXGBE_WRITE_FLUSH(hw);
2399 	}
2400 	ixgbe_raise_i2c_clk(hw, &i2cctl);
2401 
2402 	/* Minimum high period of clock is 4us */
2403 	usec_delay(IXGBE_I2C_T_HIGH);
2404 
2405 	/* Poll for ACK.  Note that ACK in I2C spec is
2406 	 * transition from 1 to 0 */
2407 	for (i = 0; i < timeout; i++) {
2408 		i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2409 		ack = ixgbe_get_i2c_data(hw, &i2cctl);
2410 
2411 		usec_delay(1);
2412 		if (!ack)
2413 			break;
2414 	}
2415 
2416 	if (ack) {
2417 		DEBUGOUT("I2C ack was not received.\n");
2418 		status = IXGBE_ERR_I2C;
2419 	}
2420 
2421 	ixgbe_lower_i2c_clk(hw, &i2cctl);
2422 
2423 	/* Minimum low period of clock is 4.7 us */
2424 	usec_delay(IXGBE_I2C_T_LOW);
2425 
2426 	return status;
2427 }
2428 
2429 /**
2430  *  ixgbe_clock_in_i2c_bit - Clocks in one bit via I2C data/clock
2431  *  @hw: pointer to hardware structure
2432  *  @data: read data value
2433  *
2434  *  Clocks in one bit via I2C data/clock
2435  **/
2436 static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data)
2437 {
2438 	u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2439 	u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN_BY_MAC(hw);
2440 
2441 	DEBUGFUNC("ixgbe_clock_in_i2c_bit");
2442 
2443 	if (data_oe_bit) {
2444 		i2cctl |= IXGBE_I2C_DATA_OUT_BY_MAC(hw);
2445 		i2cctl |= data_oe_bit;
2446 		IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), i2cctl);
2447 		IXGBE_WRITE_FLUSH(hw);
2448 	}
2449 	ixgbe_raise_i2c_clk(hw, &i2cctl);
2450 
2451 	/* Minimum high period of clock is 4us */
2452 	usec_delay(IXGBE_I2C_T_HIGH);
2453 
2454 	i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2455 	*data = ixgbe_get_i2c_data(hw, &i2cctl);
2456 
2457 	ixgbe_lower_i2c_clk(hw, &i2cctl);
2458 
2459 	/* Minimum low period of clock is 4.7 us */
2460 	usec_delay(IXGBE_I2C_T_LOW);
2461 
2462 	return IXGBE_SUCCESS;
2463 }
2464 
2465 /**
2466  *  ixgbe_clock_out_i2c_bit - Clocks in/out one bit via I2C data/clock
2467  *  @hw: pointer to hardware structure
2468  *  @data: data value to write
2469  *
2470  *  Clocks out one bit via I2C data/clock
2471  **/
2472 static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data)
2473 {
2474 	s32 status;
2475 	u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2476 
2477 	DEBUGFUNC("ixgbe_clock_out_i2c_bit");
2478 
2479 	status = ixgbe_set_i2c_data(hw, &i2cctl, data);
2480 	if (status == IXGBE_SUCCESS) {
2481 		ixgbe_raise_i2c_clk(hw, &i2cctl);
2482 
2483 		/* Minimum high period of clock is 4us */
2484 		usec_delay(IXGBE_I2C_T_HIGH);
2485 
2486 		ixgbe_lower_i2c_clk(hw, &i2cctl);
2487 
2488 		/* Minimum low period of clock is 4.7 us.
2489 		 * This also takes care of the data hold time.
2490 		 */
2491 		usec_delay(IXGBE_I2C_T_LOW);
2492 	} else {
2493 		status = IXGBE_ERR_I2C;
2494 		ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
2495 			     "I2C data was not set to %X\n", data);
2496 	}
2497 
2498 	return status;
2499 }
2500 
2501 /**
2502  *  ixgbe_raise_i2c_clk - Raises the I2C SCL clock
2503  *  @hw: pointer to hardware structure
2504  *  @i2cctl: Current value of I2CCTL register
2505  *
2506  *  Raises the I2C clock line '0'->'1'
2507  *  Negates the I2C clock output enable on X550 hardware.
2508  **/
2509 static void ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
2510 {
2511 	u32 clk_oe_bit = IXGBE_I2C_CLK_OE_N_EN_BY_MAC(hw);
2512 	u32 i = 0;
2513 	u32 timeout = IXGBE_I2C_CLOCK_STRETCHING_TIMEOUT;
2514 	u32 i2cctl_r = 0;
2515 
2516 	DEBUGFUNC("ixgbe_raise_i2c_clk");
2517 
2518 	if (clk_oe_bit) {
2519 		*i2cctl |= clk_oe_bit;
2520 		IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), *i2cctl);
2521 	}
2522 
2523 	for (i = 0; i < timeout; i++) {
2524 		*i2cctl |= IXGBE_I2C_CLK_OUT_BY_MAC(hw);
2525 
2526 		IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), *i2cctl);
2527 		IXGBE_WRITE_FLUSH(hw);
2528 		/* SCL rise time (1000ns) */
2529 		usec_delay(IXGBE_I2C_T_RISE);
2530 
2531 		i2cctl_r = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2532 		if (i2cctl_r & IXGBE_I2C_CLK_IN_BY_MAC(hw))
2533 			break;
2534 	}
2535 }
2536 
2537 /**
2538  *  ixgbe_lower_i2c_clk - Lowers the I2C SCL clock
2539  *  @hw: pointer to hardware structure
2540  *  @i2cctl: Current value of I2CCTL register
2541  *
2542  *  Lowers the I2C clock line '1'->'0'
2543  *  Asserts the I2C clock output enable on X550 hardware.
2544  **/
2545 static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
2546 {
2547 	DEBUGFUNC("ixgbe_lower_i2c_clk");
2548 
2549 	*i2cctl &= ~(IXGBE_I2C_CLK_OUT_BY_MAC(hw));
2550 	*i2cctl &= ~IXGBE_I2C_CLK_OE_N_EN_BY_MAC(hw);
2551 
2552 	IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), *i2cctl);
2553 	IXGBE_WRITE_FLUSH(hw);
2554 
2555 	/* SCL fall time (300ns) */
2556 	usec_delay(IXGBE_I2C_T_FALL);
2557 }
2558 
2559 /**
2560  *  ixgbe_set_i2c_data - Sets the I2C data bit
2561  *  @hw: pointer to hardware structure
2562  *  @i2cctl: Current value of I2CCTL register
2563  *  @data: I2C data value (0 or 1) to set
2564  *
2565  *  Sets the I2C data bit
2566  *  Asserts the I2C data output enable on X550 hardware.
2567  **/
2568 static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data)
2569 {
2570 	u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN_BY_MAC(hw);
2571 	s32 status = IXGBE_SUCCESS;
2572 
2573 	DEBUGFUNC("ixgbe_set_i2c_data");
2574 
2575 	if (data)
2576 		*i2cctl |= IXGBE_I2C_DATA_OUT_BY_MAC(hw);
2577 	else
2578 		*i2cctl &= ~(IXGBE_I2C_DATA_OUT_BY_MAC(hw));
2579 	*i2cctl &= ~data_oe_bit;
2580 
2581 	IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), *i2cctl);
2582 	IXGBE_WRITE_FLUSH(hw);
2583 
2584 	/* Data rise/fall (1000ns/300ns) and set-up time (250ns) */
2585 	usec_delay(IXGBE_I2C_T_RISE + IXGBE_I2C_T_FALL + IXGBE_I2C_T_SU_DATA);
2586 
2587 	if (!data)	/* Can't verify data in this case */
2588 		return IXGBE_SUCCESS;
2589 	if (data_oe_bit) {
2590 		*i2cctl |= data_oe_bit;
2591 		IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), *i2cctl);
2592 		IXGBE_WRITE_FLUSH(hw);
2593 	}
2594 
2595 	/* Verify data was set correctly */
2596 	*i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2597 	if (data != ixgbe_get_i2c_data(hw, i2cctl)) {
2598 		status = IXGBE_ERR_I2C;
2599 		ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
2600 			     "Error - I2C data was not set to %X.\n",
2601 			     data);
2602 	}
2603 
2604 	return status;
2605 }
2606 
2607 /**
2608  *  ixgbe_get_i2c_data - Reads the I2C SDA data bit
2609  *  @hw: pointer to hardware structure
2610  *  @i2cctl: Current value of I2CCTL register
2611  *
2612  *  Returns the I2C data bit value
2613  *  Negates the I2C data output enable on X550 hardware.
2614  **/
2615 static bool ixgbe_get_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl)
2616 {
2617 	u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN_BY_MAC(hw);
2618 	bool data;
2619 
2620 	DEBUGFUNC("ixgbe_get_i2c_data");
2621 
2622 	if (data_oe_bit) {
2623 		*i2cctl |= data_oe_bit;
2624 		IXGBE_WRITE_REG(hw, IXGBE_I2CCTL_BY_MAC(hw), *i2cctl);
2625 		IXGBE_WRITE_FLUSH(hw);
2626 		usec_delay(IXGBE_I2C_T_FALL);
2627 	}
2628 
2629 	if (*i2cctl & IXGBE_I2C_DATA_IN_BY_MAC(hw))
2630 		data = 1;
2631 	else
2632 		data = 0;
2633 
2634 	return data;
2635 }
2636 
2637 /**
2638  *  ixgbe_i2c_bus_clear - Clears the I2C bus
2639  *  @hw: pointer to hardware structure
2640  *
2641  *  Clears the I2C bus by sending nine clock pulses.
2642  *  Used when data line is stuck low.
2643  **/
2644 void ixgbe_i2c_bus_clear(struct ixgbe_hw *hw)
2645 {
2646 	u32 i2cctl;
2647 	u32 i;
2648 
2649 	DEBUGFUNC("ixgbe_i2c_bus_clear");
2650 
2651 	ixgbe_i2c_start(hw);
2652 	i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL_BY_MAC(hw));
2653 
2654 	ixgbe_set_i2c_data(hw, &i2cctl, 1);
2655 
2656 	for (i = 0; i < 9; i++) {
2657 		ixgbe_raise_i2c_clk(hw, &i2cctl);
2658 
2659 		/* Min high period of clock is 4us */
2660 		usec_delay(IXGBE_I2C_T_HIGH);
2661 
2662 		ixgbe_lower_i2c_clk(hw, &i2cctl);
2663 
2664 		/* Min low period of clock is 4.7us*/
2665 		usec_delay(IXGBE_I2C_T_LOW);
2666 	}
2667 
2668 	ixgbe_i2c_start(hw);
2669 
2670 	/* Put the i2c bus back to default state */
2671 	ixgbe_i2c_stop(hw);
2672 }
2673 
2674 /**
2675  *  ixgbe_tn_check_overtemp - Checks if an overtemp occurred.
2676  *  @hw: pointer to hardware structure
2677  *
2678  *  Checks if the LASI temp alarm status was triggered due to overtemp
2679  **/
2680 s32 ixgbe_tn_check_overtemp(struct ixgbe_hw *hw)
2681 {
2682 	s32 status = IXGBE_SUCCESS;
2683 	u16 phy_data = 0;
2684 
2685 	DEBUGFUNC("ixgbe_tn_check_overtemp");
2686 
2687 	if (hw->device_id != IXGBE_DEV_ID_82599_T3_LOM)
2688 		goto out;
2689 
2690 	/* Check that the LASI temp alarm status was triggered */
2691 	hw->phy.ops.read_reg(hw, IXGBE_TN_LASI_STATUS_REG,
2692 			     IXGBE_MDIO_PMA_PMD_DEV_TYPE, &phy_data);
2693 
2694 	if (!(phy_data & IXGBE_TN_LASI_STATUS_TEMP_ALARM))
2695 		goto out;
2696 
2697 	status = IXGBE_ERR_OVERTEMP;
2698 	ERROR_REPORT1(IXGBE_ERROR_CAUTION, "Device over temperature");
2699 out:
2700 	return status;
2701 }
2702 
2703 /**
2704  * ixgbe_set_copper_phy_power - Control power for copper phy
2705  * @hw: pointer to hardware structure
2706  * @on: TRUE for on, FALSE for off
2707  */
2708 s32 ixgbe_set_copper_phy_power(struct ixgbe_hw *hw, bool on)
2709 {
2710 	u32 status;
2711 	u16 reg;
2712 
2713 	if (!on && ixgbe_mng_present(hw))
2714 		return 0;
2715 
2716 	status = hw->phy.ops.read_reg(hw, IXGBE_MDIO_VENDOR_SPECIFIC_1_CONTROL,
2717 				      IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
2718 				      &reg);
2719 	if (status)
2720 		return status;
2721 
2722 	if (on) {
2723 		reg &= ~IXGBE_MDIO_PHY_SET_LOW_POWER_MODE;
2724 	} else {
2725 		if (ixgbe_check_reset_blocked(hw))
2726 			return 0;
2727 		reg |= IXGBE_MDIO_PHY_SET_LOW_POWER_MODE;
2728 	}
2729 
2730 	status = hw->phy.ops.write_reg(hw, IXGBE_MDIO_VENDOR_SPECIFIC_1_CONTROL,
2731 				       IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
2732 				       reg);
2733 	return status;
2734 }
2735