blob: 654a402f0e9e3b26989d5edc2de901a720e0a096 [file] [log] [blame]
Auke Kok9a799d72007-09-15 14:07:45 -07001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Don Skidmorec97506a2014-02-27 20:32:43 -08004 Copyright(c) 1999 - 2014 Intel Corporation.
Auke Kok9a799d72007-09-15 14:07:45 -07005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
Jacob Kellerb89aae72014-02-22 01:23:50 +000023 Linux NICS <linux.nics@intel.com>
Auke Kok9a799d72007-09-15 14:07:45 -070024 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
29#include <linux/pci.h>
30#include <linux/delay.h>
31#include <linux/sched.h>
32
Mark Rustadb12babd2014-01-14 18:53:16 -080033#include "ixgbe.h"
Auke Kok9a799d72007-09-15 14:07:45 -070034#include "ixgbe_phy.h"
35
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +000036static void ixgbe_i2c_start(struct ixgbe_hw *hw);
37static void ixgbe_i2c_stop(struct ixgbe_hw *hw);
38static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data);
39static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data);
40static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw);
41static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data);
42static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data);
Emil Tantilove1befd72011-08-27 07:18:47 +000043static void ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +000044static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
45static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data);
Don Skidmore9a75a1a2014-11-07 03:53:35 +000046static bool ixgbe_get_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +000047static void ixgbe_i2c_bus_clear(struct ixgbe_hw *hw);
Auke Kok9a799d72007-09-15 14:07:45 -070048static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id);
49static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw);
Mark Rustad88217542013-11-23 03:19:19 +000050static s32 ixgbe_identify_qsfp_module_generic(struct ixgbe_hw *hw);
Auke Kok9a799d72007-09-15 14:07:45 -070051
52/**
Don Skidmore28abba02014-11-29 05:22:43 +000053 * ixgbe_out_i2c_byte_ack - Send I2C byte with ack
54 * @hw: pointer to the hardware structure
55 * @byte: byte to send
56 *
57 * Returns an error code on error.
58 **/
59static s32 ixgbe_out_i2c_byte_ack(struct ixgbe_hw *hw, u8 byte)
60{
61 s32 status;
62
63 status = ixgbe_clock_out_i2c_byte(hw, byte);
64 if (status)
65 return status;
66 return ixgbe_get_i2c_ack(hw);
67}
68
69/**
70 * ixgbe_in_i2c_byte_ack - Receive an I2C byte and send ack
71 * @hw: pointer to the hardware structure
72 * @byte: pointer to a u8 to receive the byte
73 *
74 * Returns an error code on error.
75 **/
76static s32 ixgbe_in_i2c_byte_ack(struct ixgbe_hw *hw, u8 *byte)
77{
78 s32 status;
79
80 status = ixgbe_clock_in_i2c_byte(hw, byte);
81 if (status)
82 return status;
83 /* ACK */
84 return ixgbe_clock_out_i2c_bit(hw, false);
85}
86
87/**
88 * ixgbe_ones_comp_byte_add - Perform one's complement addition
89 * @add1: addend 1
90 * @add2: addend 2
91 *
92 * Returns one's complement 8-bit sum.
93 **/
94static u8 ixgbe_ones_comp_byte_add(u8 add1, u8 add2)
95{
96 u16 sum = add1 + add2;
97
98 sum = (sum & 0xFF) + (sum >> 8);
99 return sum & 0xFF;
100}
101
102/**
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700103 * ixgbe_read_i2c_combined_generic_int - Perform I2C read combined operation
Don Skidmore28abba02014-11-29 05:22:43 +0000104 * @hw: pointer to the hardware structure
105 * @addr: I2C bus address to read from
106 * @reg: I2C device register to read from
107 * @val: pointer to location to receive read value
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700108 * @lock: true if to take and release semaphore
Don Skidmore28abba02014-11-29 05:22:43 +0000109 *
110 * Returns an error code on error.
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700111 */
Emil Tantilovb71f6c42016-10-10 14:54:03 -0700112s32 ixgbe_read_i2c_combined_generic_int(struct ixgbe_hw *hw, u8 addr,
113 u16 reg, u16 *val, bool lock)
Don Skidmore28abba02014-11-29 05:22:43 +0000114{
115 u32 swfw_mask = hw->phy.phy_semaphore_mask;
Tony Nguyen3f0d6462016-11-10 09:57:29 -0800116 int max_retry = 3;
Don Skidmore28abba02014-11-29 05:22:43 +0000117 int retry = 0;
118 u8 csum_byte;
119 u8 high_bits;
120 u8 low_bits;
121 u8 reg_high;
122 u8 csum;
123
124 reg_high = ((reg >> 7) & 0xFE) | 1; /* Indicate read combined */
125 csum = ixgbe_ones_comp_byte_add(reg_high, reg & 0xFF);
126 csum = ~csum;
127 do {
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700128 if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
Don Skidmore28abba02014-11-29 05:22:43 +0000129 return IXGBE_ERR_SWFW_SYNC;
130 ixgbe_i2c_start(hw);
131 /* Device Address and write indication */
132 if (ixgbe_out_i2c_byte_ack(hw, addr))
133 goto fail;
134 /* Write bits 14:8 */
135 if (ixgbe_out_i2c_byte_ack(hw, reg_high))
136 goto fail;
137 /* Write bits 7:0 */
138 if (ixgbe_out_i2c_byte_ack(hw, reg & 0xFF))
139 goto fail;
140 /* Write csum */
141 if (ixgbe_out_i2c_byte_ack(hw, csum))
142 goto fail;
143 /* Re-start condition */
144 ixgbe_i2c_start(hw);
145 /* Device Address and read indication */
146 if (ixgbe_out_i2c_byte_ack(hw, addr | 1))
147 goto fail;
148 /* Get upper bits */
149 if (ixgbe_in_i2c_byte_ack(hw, &high_bits))
150 goto fail;
151 /* Get low bits */
152 if (ixgbe_in_i2c_byte_ack(hw, &low_bits))
153 goto fail;
154 /* Get csum */
155 if (ixgbe_clock_in_i2c_byte(hw, &csum_byte))
156 goto fail;
157 /* NACK */
158 if (ixgbe_clock_out_i2c_bit(hw, false))
159 goto fail;
160 ixgbe_i2c_stop(hw);
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700161 if (lock)
162 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
Don Skidmore28abba02014-11-29 05:22:43 +0000163 *val = (high_bits << 8) | low_bits;
164 return 0;
165
166fail:
167 ixgbe_i2c_bus_clear(hw);
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700168 if (lock)
169 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
Don Skidmore28abba02014-11-29 05:22:43 +0000170 retry++;
171 if (retry < max_retry)
172 hw_dbg(hw, "I2C byte read combined error - Retry.\n");
173 else
174 hw_dbg(hw, "I2C byte read combined error.\n");
175 } while (retry < max_retry);
176
177 return IXGBE_ERR_I2C;
178}
179
180/**
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700181 * ixgbe_write_i2c_combined_generic_int - Perform I2C write combined operation
Don Skidmore28abba02014-11-29 05:22:43 +0000182 * @hw: pointer to the hardware structure
183 * @addr: I2C bus address to write to
184 * @reg: I2C device register to write to
185 * @val: value to write
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700186 * @lock: true if to take and release semaphore
Don Skidmore28abba02014-11-29 05:22:43 +0000187 *
188 * Returns an error code on error.
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700189 */
Emil Tantilovb71f6c42016-10-10 14:54:03 -0700190s32 ixgbe_write_i2c_combined_generic_int(struct ixgbe_hw *hw, u8 addr,
191 u16 reg, u16 val, bool lock)
Don Skidmore28abba02014-11-29 05:22:43 +0000192{
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700193 u32 swfw_mask = hw->phy.phy_semaphore_mask;
Don Skidmore28abba02014-11-29 05:22:43 +0000194 int max_retry = 1;
195 int retry = 0;
196 u8 reg_high;
197 u8 csum;
198
199 reg_high = (reg >> 7) & 0xFE; /* Indicate write combined */
200 csum = ixgbe_ones_comp_byte_add(reg_high, reg & 0xFF);
201 csum = ixgbe_ones_comp_byte_add(csum, val >> 8);
202 csum = ixgbe_ones_comp_byte_add(csum, val & 0xFF);
203 csum = ~csum;
204 do {
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700205 if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
206 return IXGBE_ERR_SWFW_SYNC;
Don Skidmore28abba02014-11-29 05:22:43 +0000207 ixgbe_i2c_start(hw);
208 /* Device Address and write indication */
209 if (ixgbe_out_i2c_byte_ack(hw, addr))
210 goto fail;
211 /* Write bits 14:8 */
212 if (ixgbe_out_i2c_byte_ack(hw, reg_high))
213 goto fail;
214 /* Write bits 7:0 */
215 if (ixgbe_out_i2c_byte_ack(hw, reg & 0xFF))
216 goto fail;
217 /* Write data 15:8 */
218 if (ixgbe_out_i2c_byte_ack(hw, val >> 8))
219 goto fail;
220 /* Write data 7:0 */
221 if (ixgbe_out_i2c_byte_ack(hw, val & 0xFF))
222 goto fail;
223 /* Write csum */
224 if (ixgbe_out_i2c_byte_ack(hw, csum))
225 goto fail;
226 ixgbe_i2c_stop(hw);
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700227 if (lock)
228 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
Don Skidmore28abba02014-11-29 05:22:43 +0000229 return 0;
230
231fail:
232 ixgbe_i2c_bus_clear(hw);
Mark Rustadbb5ce9a52015-08-08 16:18:02 -0700233 if (lock)
234 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
Don Skidmore28abba02014-11-29 05:22:43 +0000235 retry++;
236 if (retry < max_retry)
237 hw_dbg(hw, "I2C byte write combined error - Retry.\n");
238 else
239 hw_dbg(hw, "I2C byte write combined error.\n");
240 } while (retry < max_retry);
241
242 return IXGBE_ERR_I2C;
243}
244
245/**
Don Skidmore470739b2016-11-03 21:01:37 -0400246 * ixgbe_probe_phy - Probe a single address for a PHY
247 * @hw: pointer to hardware structure
248 * @phy_addr: PHY address to probe
249 *
250 * Returns true if PHY found
251 **/
252static bool ixgbe_probe_phy(struct ixgbe_hw *hw, u16 phy_addr)
253{
254 u16 ext_ability = 0;
255
256 hw->phy.mdio.prtad = phy_addr;
257 if (mdio45_probe(&hw->phy.mdio, phy_addr) != 0)
258 return false;
259
260 if (ixgbe_get_phy_id(hw))
261 return false;
262
263 hw->phy.type = ixgbe_get_phy_type_from_id(hw->phy.id);
264
265 if (hw->phy.type == ixgbe_phy_unknown) {
266 hw->phy.ops.read_reg(hw,
267 MDIO_PMA_EXTABLE,
268 MDIO_MMD_PMAPMD,
269 &ext_ability);
270 if (ext_ability &
271 (MDIO_PMA_EXTABLE_10GBT |
272 MDIO_PMA_EXTABLE_1000BT))
273 hw->phy.type = ixgbe_phy_cu_unknown;
274 else
275 hw->phy.type = ixgbe_phy_generic;
276 }
277
278 return true;
279}
280
281/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700282 * ixgbe_identify_phy_generic - Get physical layer module
Auke Kok9a799d72007-09-15 14:07:45 -0700283 * @hw: pointer to hardware structure
284 *
285 * Determines the physical layer module found on the current adapter.
286 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700287s32 ixgbe_identify_phy_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700288{
Auke Kok9a799d72007-09-15 14:07:45 -0700289 u32 phy_addr;
Don Skidmore470739b2016-11-03 21:01:37 -0400290 u32 status = IXGBE_ERR_PHY_ADDR_INVALID;
Auke Kok9a799d72007-09-15 14:07:45 -0700291
Don Skidmore030eaec2014-11-29 05:22:37 +0000292 if (!hw->phy.phy_semaphore_mask) {
Don Skidmored5702de2015-06-19 12:23:36 -0400293 if (hw->bus.lan_id)
Don Skidmore030eaec2014-11-29 05:22:37 +0000294 hw->phy.phy_semaphore_mask = IXGBE_GSSR_PHY1_SM;
295 else
296 hw->phy.phy_semaphore_mask = IXGBE_GSSR_PHY0_SM;
297 }
298
Don Skidmore470739b2016-11-03 21:01:37 -0400299 if (hw->phy.type != ixgbe_phy_unknown)
300 return 0;
Emil Tantilov037c6d02011-02-25 07:49:39 +0000301
Don Skidmore470739b2016-11-03 21:01:37 -0400302 if (hw->phy.nw_mng_if_sel) {
303 phy_addr = (hw->phy.nw_mng_if_sel &
304 IXGBE_NW_MNG_IF_SEL_MDIO_PHY_ADD) >>
305 IXGBE_NW_MNG_IF_SEL_MDIO_PHY_ADD_SHIFT;
306 if (ixgbe_probe_phy(hw, phy_addr))
307 return 0;
308 else
309 return IXGBE_ERR_PHY_ADDR_INVALID;
Auke Kok9a799d72007-09-15 14:07:45 -0700310 }
Don Skidmore470739b2016-11-03 21:01:37 -0400311
312 for (phy_addr = 0; phy_addr < IXGBE_MAX_PHY_ADDR; phy_addr++) {
313 if (ixgbe_probe_phy(hw, phy_addr)) {
314 status = 0;
315 break;
316 }
317 }
318
319 /* Certain media types do not have a phy so an address will not
320 * be found and the code will take this path. Caller has to
321 * decide if it is an error or not.
322 */
323 if (status)
324 hw->phy.mdio.prtad = MDIO_PRTAD_NONE;
325
326 return status;
Auke Kok9a799d72007-09-15 14:07:45 -0700327}
328
329/**
Don Skidmorec97506a2014-02-27 20:32:43 -0800330 * ixgbe_check_reset_blocked - check status of MNG FW veto bit
331 * @hw: pointer to the hardware structure
332 *
333 * This function checks the MMNGC.MNG_VETO bit to see if there are
334 * any constraints on link from manageability. For MAC's that don't
335 * have this bit just return false since the link can not be blocked
336 * via this method.
337 **/
Jean Sacren6425f0f2014-03-11 05:57:56 +0000338bool ixgbe_check_reset_blocked(struct ixgbe_hw *hw)
Don Skidmorec97506a2014-02-27 20:32:43 -0800339{
340 u32 mmngc;
341
342 /* If we don't have this bit, it can't be blocking */
343 if (hw->mac.type == ixgbe_mac_82598EB)
344 return false;
345
346 mmngc = IXGBE_READ_REG(hw, IXGBE_MMNGC);
347 if (mmngc & IXGBE_MMNGC_MNG_VETO) {
348 hw_dbg(hw, "MNG_VETO bit detected.\n");
349 return true;
350 }
351
352 return false;
353}
354
355/**
Auke Kok9a799d72007-09-15 14:07:45 -0700356 * ixgbe_get_phy_id - Get the phy type
357 * @hw: pointer to hardware structure
358 *
359 **/
360static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw)
361{
Mark Rustada1e869d2015-04-10 10:36:36 -0700362 s32 status;
Auke Kok9a799d72007-09-15 14:07:45 -0700363 u16 phy_id_high = 0;
364 u16 phy_id_low = 0;
365
Ben Hutchings6b73e102009-04-29 08:08:58 +0000366 status = hw->phy.ops.read_reg(hw, MDIO_DEVID1, MDIO_MMD_PMAPMD,
Jacob Kellere7cf7452014-04-09 06:03:10 +0000367 &phy_id_high);
Auke Kok9a799d72007-09-15 14:07:45 -0700368
Mark Rustada1e869d2015-04-10 10:36:36 -0700369 if (!status) {
Auke Kok9a799d72007-09-15 14:07:45 -0700370 hw->phy.id = (u32)(phy_id_high << 16);
Ben Hutchings6b73e102009-04-29 08:08:58 +0000371 status = hw->phy.ops.read_reg(hw, MDIO_DEVID2, MDIO_MMD_PMAPMD,
Jacob Kellere7cf7452014-04-09 06:03:10 +0000372 &phy_id_low);
Auke Kok9a799d72007-09-15 14:07:45 -0700373 hw->phy.id |= (u32)(phy_id_low & IXGBE_PHY_REVISION_MASK);
374 hw->phy.revision = (u32)(phy_id_low & ~IXGBE_PHY_REVISION_MASK);
375 }
Auke Kok9a799d72007-09-15 14:07:45 -0700376 return status;
377}
378
379/**
380 * ixgbe_get_phy_type_from_id - Get the phy type
381 * @hw: pointer to hardware structure
382 *
383 **/
384static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id)
385{
386 enum ixgbe_phy_type phy_type;
387
388 switch (phy_id) {
Jesse Brandeburg0befdb32008-10-31 00:46:40 -0700389 case TN1010_PHY_ID:
390 phy_type = ixgbe_phy_tn;
391 break;
Don Skidmore5f1c3582016-11-04 16:46:16 -0400392 case X550_PHY_ID2:
393 case X550_PHY_ID3:
Don Skidmore2b264902010-12-09 06:55:14 +0000394 case X540_PHY_ID:
Don Skidmorefe15e8e12010-11-16 19:27:16 -0800395 phy_type = ixgbe_phy_aq;
396 break;
Auke Kok9a799d72007-09-15 14:07:45 -0700397 case QT2022_PHY_ID:
398 phy_type = ixgbe_phy_qt;
399 break;
Donald Skidmorec4900be2008-11-20 21:11:42 -0800400 case ATH_PHY_ID:
401 phy_type = ixgbe_phy_nl;
402 break;
Don Skidmorec2c78d52015-06-09 16:04:59 -0700403 case X557_PHY_ID:
Don Skidmore470739b2016-11-03 21:01:37 -0400404 case X557_PHY_ID2:
Don Skidmorec2c78d52015-06-09 16:04:59 -0700405 phy_type = ixgbe_phy_x550em_ext_t;
406 break;
Auke Kok9a799d72007-09-15 14:07:45 -0700407 default:
408 phy_type = ixgbe_phy_unknown;
409 break;
410 }
411
412 return phy_type;
413}
414
415/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700416 * ixgbe_reset_phy_generic - Performs a PHY reset
Auke Kok9a799d72007-09-15 14:07:45 -0700417 * @hw: pointer to hardware structure
418 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700419s32 ixgbe_reset_phy_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700420{
Emil Tantilov17835752011-02-16 01:38:13 +0000421 u32 i;
422 u16 ctrl = 0;
423 s32 status = 0;
424
425 if (hw->phy.type == ixgbe_phy_unknown)
426 status = ixgbe_identify_phy_generic(hw);
427
428 if (status != 0 || hw->phy.type == ixgbe_phy_none)
Mark Rustade90dd262014-07-22 06:51:08 +0000429 return status;
Emil Tantilov17835752011-02-16 01:38:13 +0000430
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -0700431 /* Don't reset PHY if it's shut down due to overtemp. */
432 if (!hw->phy.reset_if_overtemp &&
433 (IXGBE_ERR_OVERTEMP == hw->phy.ops.check_overtemp(hw)))
Mark Rustade90dd262014-07-22 06:51:08 +0000434 return 0;
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -0700435
Don Skidmorec97506a2014-02-27 20:32:43 -0800436 /* Blocked by MNG FW so bail */
437 if (ixgbe_check_reset_blocked(hw))
Mark Rustade90dd262014-07-22 06:51:08 +0000438 return 0;
Don Skidmorec97506a2014-02-27 20:32:43 -0800439
Auke Kok9a799d72007-09-15 14:07:45 -0700440 /*
441 * Perform soft PHY reset to the PHY_XS.
442 * This will cause a soft reset to the PHY
443 */
Emil Tantilov17835752011-02-16 01:38:13 +0000444 hw->phy.ops.write_reg(hw, MDIO_CTRL1,
445 MDIO_MMD_PHYXS,
446 MDIO_CTRL1_RESET);
447
448 /*
449 * Poll for reset bit to self-clear indicating reset is complete.
450 * Some PHYs could take up to 3 seconds to complete and need about
451 * 1.7 usec delay after the reset is complete.
452 */
453 for (i = 0; i < 30; i++) {
454 msleep(100);
Tony Nguyen5c092742016-10-31 12:11:58 -0700455 if (hw->phy.type == ixgbe_phy_x550em_ext_t) {
456 status = hw->phy.ops.read_reg(hw,
457 IXGBE_MDIO_TX_VENDOR_ALARMS_3,
458 MDIO_MMD_PMAPMD, &ctrl);
459 if (status)
460 return status;
461
462 if (ctrl & IXGBE_MDIO_TX_VENDOR_ALARMS_3_RST_MASK) {
463 udelay(2);
464 break;
465 }
466 } else {
467 status = hw->phy.ops.read_reg(hw, MDIO_CTRL1,
468 MDIO_MMD_PHYXS, &ctrl);
469 if (status)
470 return status;
471
472 if (!(ctrl & MDIO_CTRL1_RESET)) {
473 udelay(2);
474 break;
475 }
Emil Tantilov17835752011-02-16 01:38:13 +0000476 }
477 }
478
479 if (ctrl & MDIO_CTRL1_RESET) {
Emil Tantilov17835752011-02-16 01:38:13 +0000480 hw_dbg(hw, "PHY reset polling failed to complete.\n");
Mark Rustade90dd262014-07-22 06:51:08 +0000481 return IXGBE_ERR_RESET_FAILED;
Emil Tantilov17835752011-02-16 01:38:13 +0000482 }
483
Mark Rustade90dd262014-07-22 06:51:08 +0000484 return 0;
Auke Kok9a799d72007-09-15 14:07:45 -0700485}
486
487/**
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000488 * ixgbe_read_phy_mdi - Reads a value from a specified PHY register without
489 * the SWFW lock
490 * @hw: pointer to hardware structure
491 * @reg_addr: 32 bit address of PHY register to read
492 * @phy_data: Pointer to read data from PHY register
493 **/
494s32 ixgbe_read_phy_reg_mdi(struct ixgbe_hw *hw, u32 reg_addr, u32 device_type,
495 u16 *phy_data)
496{
497 u32 i, data, command;
498
499 /* Setup and write the address cycle command */
500 command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
501 (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
502 (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
503 (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
504
505 IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
506
507 /* Check every 10 usec to see if the address cycle completed.
508 * The MDI Command bit will clear when the operation is
509 * complete
510 */
511 for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
512 udelay(10);
513
514 command = IXGBE_READ_REG(hw, IXGBE_MSCA);
515 if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
516 break;
517 }
518
519
520 if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
521 hw_dbg(hw, "PHY address command did not complete.\n");
522 return IXGBE_ERR_PHY;
523 }
524
525 /* Address cycle complete, setup and write the read
526 * command
527 */
528 command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
529 (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
530 (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
531 (IXGBE_MSCA_READ | IXGBE_MSCA_MDI_COMMAND));
532
533 IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
534
535 /* Check every 10 usec to see if the address cycle
536 * completed. The MDI Command bit will clear when the
537 * operation is complete
538 */
539 for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
540 udelay(10);
541
542 command = IXGBE_READ_REG(hw, IXGBE_MSCA);
543 if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
544 break;
545 }
546
547 if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
548 hw_dbg(hw, "PHY read command didn't complete\n");
549 return IXGBE_ERR_PHY;
550 }
551
552 /* Read operation is complete. Get the data
553 * from MSRWD
554 */
555 data = IXGBE_READ_REG(hw, IXGBE_MSRWD);
556 data >>= IXGBE_MSRWD_READ_DATA_SHIFT;
557 *phy_data = (u16)(data);
558
559 return 0;
560}
561
562/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700563 * ixgbe_read_phy_reg_generic - Reads a value from a specified PHY register
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000564 * using the SWFW lock - this function is needed in most cases
Auke Kok9a799d72007-09-15 14:07:45 -0700565 * @hw: pointer to hardware structure
566 * @reg_addr: 32 bit address of PHY register to read
567 * @phy_data: Pointer to read data from PHY register
568 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700569s32 ixgbe_read_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
Jacob Kellere7cf7452014-04-09 06:03:10 +0000570 u32 device_type, u16 *phy_data)
Auke Kok9a799d72007-09-15 14:07:45 -0700571{
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000572 s32 status;
Don Skidmore030eaec2014-11-29 05:22:37 +0000573 u32 gssr = hw->phy.phy_semaphore_mask;
Auke Kok9a799d72007-09-15 14:07:45 -0700574
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000575 if (hw->mac.ops.acquire_swfw_sync(hw, gssr) == 0) {
576 status = ixgbe_read_phy_reg_mdi(hw, reg_addr, device_type,
577 phy_data);
Don Skidmore5e655102011-02-25 01:58:04 +0000578 hw->mac.ops.release_swfw_sync(hw, gssr);
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000579 } else {
Mark Rustade90dd262014-07-22 06:51:08 +0000580 return IXGBE_ERR_SWFW_SYNC;
Auke Kok9a799d72007-09-15 14:07:45 -0700581 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700582
Auke Kok9a799d72007-09-15 14:07:45 -0700583 return status;
584}
585
586/**
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000587 * ixgbe_write_phy_reg_mdi - Writes a value to specified PHY register
588 * without SWFW lock
589 * @hw: pointer to hardware structure
590 * @reg_addr: 32 bit PHY register to write
591 * @device_type: 5 bit device type
592 * @phy_data: Data to write to the PHY register
593 **/
594s32 ixgbe_write_phy_reg_mdi(struct ixgbe_hw *hw, u32 reg_addr,
595 u32 device_type, u16 phy_data)
596{
597 u32 i, command;
598
599 /* Put the data in the MDI single read and write data register*/
600 IXGBE_WRITE_REG(hw, IXGBE_MSRWD, (u32)phy_data);
601
602 /* Setup and write the address cycle command */
603 command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
604 (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
605 (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
606 (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
607
608 IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
609
610 /*
611 * Check every 10 usec to see if the address cycle completed.
612 * The MDI Command bit will clear when the operation is
613 * complete
614 */
615 for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
616 udelay(10);
617
618 command = IXGBE_READ_REG(hw, IXGBE_MSCA);
619 if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
620 break;
621 }
622
623 if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
624 hw_dbg(hw, "PHY address cmd didn't complete\n");
625 return IXGBE_ERR_PHY;
626 }
627
628 /*
629 * Address cycle complete, setup and write the write
630 * command
631 */
632 command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
633 (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
634 (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
635 (IXGBE_MSCA_WRITE | IXGBE_MSCA_MDI_COMMAND));
636
637 IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
638
639 /* Check every 10 usec to see if the address cycle
640 * completed. The MDI Command bit will clear when the
641 * operation is complete
642 */
643 for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
644 udelay(10);
645
646 command = IXGBE_READ_REG(hw, IXGBE_MSCA);
647 if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
648 break;
649 }
650
651 if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
652 hw_dbg(hw, "PHY write cmd didn't complete\n");
653 return IXGBE_ERR_PHY;
654 }
655
656 return 0;
657}
658
659/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700660 * ixgbe_write_phy_reg_generic - Writes a value to specified PHY register
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000661 * using SWFW lock- this function is needed in most cases
Auke Kok9a799d72007-09-15 14:07:45 -0700662 * @hw: pointer to hardware structure
663 * @reg_addr: 32 bit PHY register to write
664 * @device_type: 5 bit device type
665 * @phy_data: Data to write to the PHY register
666 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700667s32 ixgbe_write_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
Jacob Kellere7cf7452014-04-09 06:03:10 +0000668 u32 device_type, u16 phy_data)
Auke Kok9a799d72007-09-15 14:07:45 -0700669{
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000670 s32 status;
Don Skidmore897b9342015-06-19 19:14:57 -0400671 u32 gssr = hw->phy.phy_semaphore_mask;
Auke Kok9a799d72007-09-15 14:07:45 -0700672
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000673 if (hw->mac.ops.acquire_swfw_sync(hw, gssr) == 0) {
674 status = ixgbe_write_phy_reg_mdi(hw, reg_addr, device_type,
675 phy_data);
Don Skidmore5e655102011-02-25 01:58:04 +0000676 hw->mac.ops.release_swfw_sync(hw, gssr);
Emil Tantilov3dcc2f412013-05-29 06:23:05 +0000677 } else {
Mark Rustade90dd262014-07-22 06:51:08 +0000678 return IXGBE_ERR_SWFW_SYNC;
Auke Kok9a799d72007-09-15 14:07:45 -0700679 }
680
681 return status;
682}
683
684/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700685 * ixgbe_setup_phy_link_generic - Set and restart autoneg
Auke Kok9a799d72007-09-15 14:07:45 -0700686 * @hw: pointer to hardware structure
687 *
688 * Restart autonegotiation and PHY and waits for completion.
689 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700690s32 ixgbe_setup_phy_link_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700691{
Emil Tantilov9dda1732011-03-05 01:28:07 +0000692 s32 status = 0;
Emil Tantilov9dda1732011-03-05 01:28:07 +0000693 u16 autoneg_reg = IXGBE_MII_AUTONEG_REG;
694 bool autoneg = false;
695 ixgbe_link_speed speed;
Auke Kok9a799d72007-09-15 14:07:45 -0700696
Emil Tantilov9dda1732011-03-05 01:28:07 +0000697 ixgbe_get_copper_link_capabilities_generic(hw, &speed, &autoneg);
Auke Kok9a799d72007-09-15 14:07:45 -0700698
Don Skidmored2e455a2016-10-21 21:10:54 -0400699 /* Set or unset auto-negotiation 10G advertisement */
700 hw->phy.ops.read_reg(hw, MDIO_AN_10GBT_CTRL, MDIO_MMD_AN, &autoneg_reg);
Emil Tantilov9dda1732011-03-05 01:28:07 +0000701
Don Skidmored2e455a2016-10-21 21:10:54 -0400702 autoneg_reg &= ~MDIO_AN_10GBT_CTRL_ADV10G;
703 if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL) &&
704 (speed & IXGBE_LINK_SPEED_10GB_FULL))
705 autoneg_reg |= MDIO_AN_10GBT_CTRL_ADV10G;
Auke Kok9a799d72007-09-15 14:07:45 -0700706
Don Skidmored2e455a2016-10-21 21:10:54 -0400707 hw->phy.ops.write_reg(hw, MDIO_AN_10GBT_CTRL, MDIO_MMD_AN, autoneg_reg);
708
709 hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
710 MDIO_MMD_AN, &autoneg_reg);
711
712 if (hw->mac.type == ixgbe_mac_X550) {
713 /* Set or unset auto-negotiation 5G advertisement */
714 autoneg_reg &= ~IXGBE_MII_5GBASE_T_ADVERTISE;
715 if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_5GB_FULL) &&
716 (speed & IXGBE_LINK_SPEED_5GB_FULL))
717 autoneg_reg |= IXGBE_MII_5GBASE_T_ADVERTISE;
718
719 /* Set or unset auto-negotiation 2.5G advertisement */
720 autoneg_reg &= ~IXGBE_MII_2_5GBASE_T_ADVERTISE;
721 if ((hw->phy.autoneg_advertised &
722 IXGBE_LINK_SPEED_2_5GB_FULL) &&
723 (speed & IXGBE_LINK_SPEED_2_5GB_FULL))
724 autoneg_reg |= IXGBE_MII_2_5GBASE_T_ADVERTISE;
Emil Tantilov9dda1732011-03-05 01:28:07 +0000725 }
726
Don Skidmored2e455a2016-10-21 21:10:54 -0400727 /* Set or unset auto-negotiation 1G advertisement */
728 autoneg_reg &= ~IXGBE_MII_1GBASE_T_ADVERTISE;
729 if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL) &&
730 (speed & IXGBE_LINK_SPEED_1GB_FULL))
731 autoneg_reg |= IXGBE_MII_1GBASE_T_ADVERTISE;
Emil Tantilov9dda1732011-03-05 01:28:07 +0000732
Don Skidmored2e455a2016-10-21 21:10:54 -0400733 hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
734 MDIO_MMD_AN, autoneg_reg);
Emil Tantilov9dda1732011-03-05 01:28:07 +0000735
Don Skidmored2e455a2016-10-21 21:10:54 -0400736 /* Set or unset auto-negotiation 100M advertisement */
737 hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE, MDIO_MMD_AN, &autoneg_reg);
Emil Tantilov9dda1732011-03-05 01:28:07 +0000738
Don Skidmored2e455a2016-10-21 21:10:54 -0400739 autoneg_reg &= ~(ADVERTISE_100FULL | ADVERTISE_100HALF);
740 if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL) &&
741 (speed & IXGBE_LINK_SPEED_100_FULL))
742 autoneg_reg |= ADVERTISE_100FULL;
Emil Tantilov9dda1732011-03-05 01:28:07 +0000743
Don Skidmored2e455a2016-10-21 21:10:54 -0400744 hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE, MDIO_MMD_AN, autoneg_reg);
Auke Kok9a799d72007-09-15 14:07:45 -0700745
Don Skidmorec97506a2014-02-27 20:32:43 -0800746 /* Blocked by MNG FW so don't reset PHY */
747 if (ixgbe_check_reset_blocked(hw))
Mark Rustade90dd262014-07-22 06:51:08 +0000748 return 0;
Don Skidmorec97506a2014-02-27 20:32:43 -0800749
Auke Kok9a799d72007-09-15 14:07:45 -0700750 /* Restart PHY autonegotiation and wait for completion */
Emil Tantilov9dda1732011-03-05 01:28:07 +0000751 hw->phy.ops.read_reg(hw, MDIO_CTRL1,
752 MDIO_MMD_AN, &autoneg_reg);
Auke Kok9a799d72007-09-15 14:07:45 -0700753
Ben Hutchings6b73e102009-04-29 08:08:58 +0000754 autoneg_reg |= MDIO_AN_CTRL1_RESTART;
Auke Kok9a799d72007-09-15 14:07:45 -0700755
Emil Tantilov9dda1732011-03-05 01:28:07 +0000756 hw->phy.ops.write_reg(hw, MDIO_CTRL1,
757 MDIO_MMD_AN, autoneg_reg);
Auke Kok9a799d72007-09-15 14:07:45 -0700758
Auke Kok9a799d72007-09-15 14:07:45 -0700759 return status;
760}
761
762/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700763 * ixgbe_setup_phy_link_speed_generic - Sets the auto advertised capabilities
Auke Kok9a799d72007-09-15 14:07:45 -0700764 * @hw: pointer to hardware structure
765 * @speed: new link speed
Auke Kok9a799d72007-09-15 14:07:45 -0700766 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700767s32 ixgbe_setup_phy_link_speed_generic(struct ixgbe_hw *hw,
Jacob Kellere7cf7452014-04-09 06:03:10 +0000768 ixgbe_link_speed speed,
769 bool autoneg_wait_to_complete)
Auke Kok9a799d72007-09-15 14:07:45 -0700770{
Tony Nguyen1dc0eb72016-11-10 16:01:33 -0800771 /* Clear autoneg_advertised and set new values based on input link
Auke Kok9a799d72007-09-15 14:07:45 -0700772 * speed.
773 */
774 hw->phy.autoneg_advertised = 0;
775
776 if (speed & IXGBE_LINK_SPEED_10GB_FULL)
777 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700778
Tony Nguyen1dc0eb72016-11-10 16:01:33 -0800779 if (speed & IXGBE_LINK_SPEED_5GB_FULL)
780 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_5GB_FULL;
781
782 if (speed & IXGBE_LINK_SPEED_2_5GB_FULL)
783 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_2_5GB_FULL;
784
Auke Kok9a799d72007-09-15 14:07:45 -0700785 if (speed & IXGBE_LINK_SPEED_1GB_FULL)
786 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
787
Emil Tantilov9dda1732011-03-05 01:28:07 +0000788 if (speed & IXGBE_LINK_SPEED_100_FULL)
789 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_100_FULL;
790
Mark Rustadb3eb4e12016-12-14 11:02:16 -0800791 if (speed & IXGBE_LINK_SPEED_10_FULL)
792 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10_FULL;
793
Auke Kok9a799d72007-09-15 14:07:45 -0700794 /* Setup link based on the new speed settings */
Tony Nguyen5fbf5ad2016-11-01 13:58:27 -0700795 if (hw->phy.ops.setup_link)
796 hw->phy.ops.setup_link(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700797
798 return 0;
799}
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700800
Jesse Brandeburg0befdb32008-10-31 00:46:40 -0700801/**
Mark Rustadae8140a2015-06-25 17:49:57 -0700802 * ixgbe_get_copper_speeds_supported - Get copper link speed from phy
803 * @hw: pointer to hardware structure
804 *
805 * Determines the supported link capabilities by reading the PHY auto
806 * negotiation register.
807 */
808static s32 ixgbe_get_copper_speeds_supported(struct ixgbe_hw *hw)
809{
810 u16 speed_ability;
811 s32 status;
812
813 status = hw->phy.ops.read_reg(hw, MDIO_SPEED, MDIO_MMD_PMAPMD,
814 &speed_ability);
815 if (status)
816 return status;
817
818 if (speed_ability & MDIO_SPEED_10G)
819 hw->phy.speeds_supported |= IXGBE_LINK_SPEED_10GB_FULL;
820 if (speed_ability & MDIO_PMA_SPEED_1000)
821 hw->phy.speeds_supported |= IXGBE_LINK_SPEED_1GB_FULL;
822 if (speed_ability & MDIO_PMA_SPEED_100)
823 hw->phy.speeds_supported |= IXGBE_LINK_SPEED_100_FULL;
824
825 switch (hw->mac.type) {
826 case ixgbe_mac_X550:
827 hw->phy.speeds_supported |= IXGBE_LINK_SPEED_2_5GB_FULL;
828 hw->phy.speeds_supported |= IXGBE_LINK_SPEED_5GB_FULL;
829 break;
830 case ixgbe_mac_X550EM_x:
Don Skidmore470739b2016-11-03 21:01:37 -0400831 case ixgbe_mac_x550em_a:
Mark Rustadae8140a2015-06-25 17:49:57 -0700832 hw->phy.speeds_supported &= ~IXGBE_LINK_SPEED_100_FULL;
833 break;
834 default:
835 break;
836 }
837
838 return 0;
839}
840
841/**
Don Skidmorea391f1d2010-11-16 19:27:15 -0800842 * ixgbe_get_copper_link_capabilities_generic - Determines link capabilities
843 * @hw: pointer to hardware structure
844 * @speed: pointer to link speed
845 * @autoneg: boolean auto-negotiation value
Don Skidmorea391f1d2010-11-16 19:27:15 -0800846 */
847s32 ixgbe_get_copper_link_capabilities_generic(struct ixgbe_hw *hw,
Jacob Kellere7cf7452014-04-09 06:03:10 +0000848 ixgbe_link_speed *speed,
849 bool *autoneg)
Don Skidmorea391f1d2010-11-16 19:27:15 -0800850{
Mark Rustadae8140a2015-06-25 17:49:57 -0700851 s32 status = 0;
Don Skidmorea391f1d2010-11-16 19:27:15 -0800852
Don Skidmorea391f1d2010-11-16 19:27:15 -0800853 *autoneg = true;
Mark Rustadae8140a2015-06-25 17:49:57 -0700854 if (!hw->phy.speeds_supported)
855 status = ixgbe_get_copper_speeds_supported(hw);
Don Skidmorea391f1d2010-11-16 19:27:15 -0800856
Mark Rustadae8140a2015-06-25 17:49:57 -0700857 *speed = hw->phy.speeds_supported;
Don Skidmorea391f1d2010-11-16 19:27:15 -0800858 return status;
859}
860
861/**
Emil Tantilov9dda1732011-03-05 01:28:07 +0000862 * ixgbe_check_phy_link_tnx - Determine link and speed status
863 * @hw: pointer to hardware structure
864 *
865 * Reads the VS1 register to determine if link is up and the current speed for
866 * the PHY.
867 **/
868s32 ixgbe_check_phy_link_tnx(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
869 bool *link_up)
870{
Mark Rustade90dd262014-07-22 06:51:08 +0000871 s32 status;
Emil Tantilov9dda1732011-03-05 01:28:07 +0000872 u32 time_out;
873 u32 max_time_out = 10;
874 u16 phy_link = 0;
875 u16 phy_speed = 0;
876 u16 phy_data = 0;
877
878 /* Initialize speed and link to default case */
879 *link_up = false;
880 *speed = IXGBE_LINK_SPEED_10GB_FULL;
881
882 /*
883 * Check current speed and link status of the PHY register.
884 * This is a vendor specific register and may have to
885 * be changed for other copper PHYs.
886 */
887 for (time_out = 0; time_out < max_time_out; time_out++) {
888 udelay(10);
889 status = hw->phy.ops.read_reg(hw,
890 MDIO_STAT1,
891 MDIO_MMD_VEND1,
892 &phy_data);
893 phy_link = phy_data &
894 IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS;
895 phy_speed = phy_data &
896 IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS;
897 if (phy_link == IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS) {
898 *link_up = true;
899 if (phy_speed ==
900 IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS)
901 *speed = IXGBE_LINK_SPEED_1GB_FULL;
902 break;
903 }
904 }
905
906 return status;
907}
908
909/**
910 * ixgbe_setup_phy_link_tnx - Set and restart autoneg
911 * @hw: pointer to hardware structure
912 *
913 * Restart autonegotiation and PHY and waits for completion.
Don Skidmore9a75a1a2014-11-07 03:53:35 +0000914 * This function always returns success, this is nessary since
915 * it is called via a function pointer that could call other
916 * functions that could return an error.
Emil Tantilov9dda1732011-03-05 01:28:07 +0000917 **/
918s32 ixgbe_setup_phy_link_tnx(struct ixgbe_hw *hw)
919{
Emil Tantilov9dda1732011-03-05 01:28:07 +0000920 u16 autoneg_reg = IXGBE_MII_AUTONEG_REG;
921 bool autoneg = false;
922 ixgbe_link_speed speed;
923
924 ixgbe_get_copper_link_capabilities_generic(hw, &speed, &autoneg);
925
926 if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
927 /* Set or unset auto-negotiation 10G advertisement */
928 hw->phy.ops.read_reg(hw, MDIO_AN_10GBT_CTRL,
929 MDIO_MMD_AN,
930 &autoneg_reg);
931
932 autoneg_reg &= ~MDIO_AN_10GBT_CTRL_ADV10G;
933 if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL)
934 autoneg_reg |= MDIO_AN_10GBT_CTRL_ADV10G;
935
936 hw->phy.ops.write_reg(hw, MDIO_AN_10GBT_CTRL,
937 MDIO_MMD_AN,
938 autoneg_reg);
939 }
940
941 if (speed & IXGBE_LINK_SPEED_1GB_FULL) {
942 /* Set or unset auto-negotiation 1G advertisement */
943 hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_XNP_TX_REG,
944 MDIO_MMD_AN,
945 &autoneg_reg);
946
947 autoneg_reg &= ~IXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
948 if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL)
949 autoneg_reg |= IXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
950
951 hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_XNP_TX_REG,
952 MDIO_MMD_AN,
953 autoneg_reg);
954 }
955
956 if (speed & IXGBE_LINK_SPEED_100_FULL) {
957 /* Set or unset auto-negotiation 100M advertisement */
958 hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
959 MDIO_MMD_AN,
960 &autoneg_reg);
961
Emil Tantilov50c022e2011-03-31 09:36:12 +0000962 autoneg_reg &= ~(ADVERTISE_100FULL |
963 ADVERTISE_100HALF);
Emil Tantilov9dda1732011-03-05 01:28:07 +0000964 if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL)
965 autoneg_reg |= ADVERTISE_100FULL;
966
967 hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE,
968 MDIO_MMD_AN,
969 autoneg_reg);
970 }
971
Don Skidmorec97506a2014-02-27 20:32:43 -0800972 /* Blocked by MNG FW so don't reset PHY */
973 if (ixgbe_check_reset_blocked(hw))
Mark Rustade90dd262014-07-22 06:51:08 +0000974 return 0;
Don Skidmorec97506a2014-02-27 20:32:43 -0800975
Emil Tantilov9dda1732011-03-05 01:28:07 +0000976 /* Restart PHY autonegotiation and wait for completion */
977 hw->phy.ops.read_reg(hw, MDIO_CTRL1,
978 MDIO_MMD_AN, &autoneg_reg);
979
980 autoneg_reg |= MDIO_AN_CTRL1_RESTART;
981
982 hw->phy.ops.write_reg(hw, MDIO_CTRL1,
983 MDIO_MMD_AN, autoneg_reg);
Don Skidmore9a75a1a2014-11-07 03:53:35 +0000984 return 0;
Emil Tantilov9dda1732011-03-05 01:28:07 +0000985}
986
987/**
Donald Skidmorec4900be2008-11-20 21:11:42 -0800988 * ixgbe_reset_phy_nl - Performs a PHY reset
989 * @hw: pointer to hardware structure
990 **/
991s32 ixgbe_reset_phy_nl(struct ixgbe_hw *hw)
992{
993 u16 phy_offset, control, eword, edata, block_crc;
994 bool end_data = false;
995 u16 list_offset, data_offset;
996 u16 phy_data = 0;
Mark Rustade90dd262014-07-22 06:51:08 +0000997 s32 ret_val;
Donald Skidmorec4900be2008-11-20 21:11:42 -0800998 u32 i;
999
Don Skidmorec97506a2014-02-27 20:32:43 -08001000 /* Blocked by MNG FW so bail */
1001 if (ixgbe_check_reset_blocked(hw))
Mark Rustade90dd262014-07-22 06:51:08 +00001002 return 0;
Don Skidmorec97506a2014-02-27 20:32:43 -08001003
Ben Hutchings6b73e102009-04-29 08:08:58 +00001004 hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS, &phy_data);
Donald Skidmorec4900be2008-11-20 21:11:42 -08001005
1006 /* reset the PHY and poll for completion */
Ben Hutchings6b73e102009-04-29 08:08:58 +00001007 hw->phy.ops.write_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001008 (phy_data | MDIO_CTRL1_RESET));
Donald Skidmorec4900be2008-11-20 21:11:42 -08001009
1010 for (i = 0; i < 100; i++) {
Ben Hutchings6b73e102009-04-29 08:08:58 +00001011 hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001012 &phy_data);
Ben Hutchings6b73e102009-04-29 08:08:58 +00001013 if ((phy_data & MDIO_CTRL1_RESET) == 0)
Donald Skidmorec4900be2008-11-20 21:11:42 -08001014 break;
Don Skidmore032b4322011-03-18 09:32:53 +00001015 usleep_range(10000, 20000);
Donald Skidmorec4900be2008-11-20 21:11:42 -08001016 }
1017
Ben Hutchings6b73e102009-04-29 08:08:58 +00001018 if ((phy_data & MDIO_CTRL1_RESET) != 0) {
Donald Skidmorec4900be2008-11-20 21:11:42 -08001019 hw_dbg(hw, "PHY reset did not complete.\n");
Mark Rustade90dd262014-07-22 06:51:08 +00001020 return IXGBE_ERR_PHY;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001021 }
1022
1023 /* Get init offsets */
1024 ret_val = ixgbe_get_sfp_init_sequence_offsets(hw, &list_offset,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001025 &data_offset);
Mark Rustade90dd262014-07-22 06:51:08 +00001026 if (ret_val)
1027 return ret_val;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001028
1029 ret_val = hw->eeprom.ops.read(hw, data_offset, &block_crc);
1030 data_offset++;
1031 while (!end_data) {
1032 /*
1033 * Read control word from PHY init contents offset
1034 */
1035 ret_val = hw->eeprom.ops.read(hw, data_offset, &eword);
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001036 if (ret_val)
1037 goto err_eeprom;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001038 control = (eword & IXGBE_CONTROL_MASK_NL) >>
Jacob Kellere7cf7452014-04-09 06:03:10 +00001039 IXGBE_CONTROL_SHIFT_NL;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001040 edata = eword & IXGBE_DATA_MASK_NL;
1041 switch (control) {
1042 case IXGBE_DELAY_NL:
1043 data_offset++;
1044 hw_dbg(hw, "DELAY: %d MS\n", edata);
Don Skidmore032b4322011-03-18 09:32:53 +00001045 usleep_range(edata * 1000, edata * 2000);
Donald Skidmorec4900be2008-11-20 21:11:42 -08001046 break;
1047 case IXGBE_DATA_NL:
Frans Popd6dbee82010-03-24 07:57:35 +00001048 hw_dbg(hw, "DATA:\n");
Donald Skidmorec4900be2008-11-20 21:11:42 -08001049 data_offset++;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001050 ret_val = hw->eeprom.ops.read(hw, data_offset++,
1051 &phy_offset);
1052 if (ret_val)
1053 goto err_eeprom;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001054 for (i = 0; i < edata; i++) {
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001055 ret_val = hw->eeprom.ops.read(hw, data_offset,
1056 &eword);
1057 if (ret_val)
1058 goto err_eeprom;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001059 hw->phy.ops.write_reg(hw, phy_offset,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001060 MDIO_MMD_PMAPMD, eword);
Donald Skidmorec4900be2008-11-20 21:11:42 -08001061 hw_dbg(hw, "Wrote %4.4x to %4.4x\n", eword,
1062 phy_offset);
1063 data_offset++;
1064 phy_offset++;
1065 }
1066 break;
1067 case IXGBE_CONTROL_NL:
1068 data_offset++;
Frans Popd6dbee82010-03-24 07:57:35 +00001069 hw_dbg(hw, "CONTROL:\n");
Donald Skidmorec4900be2008-11-20 21:11:42 -08001070 if (edata == IXGBE_CONTROL_EOL_NL) {
1071 hw_dbg(hw, "EOL\n");
1072 end_data = true;
1073 } else if (edata == IXGBE_CONTROL_SOL_NL) {
1074 hw_dbg(hw, "SOL\n");
1075 } else {
1076 hw_dbg(hw, "Bad control value\n");
Mark Rustade90dd262014-07-22 06:51:08 +00001077 return IXGBE_ERR_PHY;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001078 }
1079 break;
1080 default:
1081 hw_dbg(hw, "Bad control type\n");
Mark Rustade90dd262014-07-22 06:51:08 +00001082 return IXGBE_ERR_PHY;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001083 }
1084 }
1085
Donald Skidmorec4900be2008-11-20 21:11:42 -08001086 return ret_val;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001087
1088err_eeprom:
1089 hw_err(hw, "eeprom read at offset %d failed\n", data_offset);
1090 return IXGBE_ERR_PHY;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001091}
1092
1093/**
Don Skidmore8f583322013-07-27 06:25:38 +00001094 * ixgbe_identify_module_generic - Identifies module type
Donald Skidmorec4900be2008-11-20 21:11:42 -08001095 * @hw: pointer to hardware structure
1096 *
Don Skidmore8f583322013-07-27 06:25:38 +00001097 * Determines HW type and calls appropriate function.
1098 **/
1099s32 ixgbe_identify_module_generic(struct ixgbe_hw *hw)
1100{
Don Skidmore8f583322013-07-27 06:25:38 +00001101 switch (hw->mac.ops.get_media_type(hw)) {
1102 case ixgbe_media_type_fiber:
Mark Rustade90dd262014-07-22 06:51:08 +00001103 return ixgbe_identify_sfp_module_generic(hw);
Don Skidmore8f583322013-07-27 06:25:38 +00001104 case ixgbe_media_type_fiber_qsfp:
Mark Rustade90dd262014-07-22 06:51:08 +00001105 return ixgbe_identify_qsfp_module_generic(hw);
Don Skidmore8f583322013-07-27 06:25:38 +00001106 default:
1107 hw->phy.sfp_type = ixgbe_sfp_type_not_present;
Mark Rustade90dd262014-07-22 06:51:08 +00001108 return IXGBE_ERR_SFP_NOT_PRESENT;
Don Skidmore8f583322013-07-27 06:25:38 +00001109 }
1110
Mark Rustade90dd262014-07-22 06:51:08 +00001111 return IXGBE_ERR_SFP_NOT_PRESENT;
Don Skidmore8f583322013-07-27 06:25:38 +00001112}
1113
1114/**
1115 * ixgbe_identify_sfp_module_generic - Identifies SFP modules
1116 * @hw: pointer to hardware structure
Mark Rustade90dd262014-07-22 06:51:08 +00001117 *
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001118 * Searches for and identifies the SFP module and assigns appropriate PHY type.
Donald Skidmorec4900be2008-11-20 21:11:42 -08001119 **/
1120s32 ixgbe_identify_sfp_module_generic(struct ixgbe_hw *hw)
1121{
Peter P Waskiewicz Jr8ef78ad2012-02-01 09:19:21 +00001122 struct ixgbe_adapter *adapter = hw->back;
Mark Rustade90dd262014-07-22 06:51:08 +00001123 s32 status;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001124 u32 vendor_oui = 0;
PJ Waskiewicz553b4492009-04-09 22:28:15 +00001125 enum ixgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001126 u8 identifier = 0;
1127 u8 comp_codes_1g = 0;
1128 u8 comp_codes_10g = 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001129 u8 oui_bytes[3] = {0, 0, 0};
Peter P Waskiewicz Jr537d58a2009-05-19 09:18:51 +00001130 u8 cable_tech = 0;
Don Skidmoreea0a04d2010-05-18 16:00:13 +00001131 u8 cable_spec = 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001132 u16 enforce_sfp = 0;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001133
Don Skidmore8ca783a2009-05-26 20:40:47 -07001134 if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_fiber) {
1135 hw->phy.sfp_type = ixgbe_sfp_type_not_present;
Mark Rustade90dd262014-07-22 06:51:08 +00001136 return IXGBE_ERR_SFP_NOT_PRESENT;
Don Skidmore8ca783a2009-05-26 20:40:47 -07001137 }
1138
Mark Rustadda4ea4b2015-08-08 16:18:07 -07001139 /* LAN ID is needed for sfp_type determination */
1140 hw->mac.ops.set_lan_id(hw);
1141
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001142 status = hw->phy.ops.read_i2c_eeprom(hw,
1143 IXGBE_SFF_IDENTIFIER,
Emil Tantilov51d04202013-01-18 02:17:11 +00001144 &identifier);
Donald Skidmorec4900be2008-11-20 21:11:42 -08001145
Mark Rustade90dd262014-07-22 06:51:08 +00001146 if (status)
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001147 goto err_read_i2c_eeprom;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001148
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001149 if (identifier != IXGBE_SFF_IDENTIFIER_SFP) {
1150 hw->phy.type = ixgbe_phy_sfp_unsupported;
Mark Rustade90dd262014-07-22 06:51:08 +00001151 return IXGBE_ERR_SFP_NOT_SUPPORTED;
1152 }
1153 status = hw->phy.ops.read_i2c_eeprom(hw,
1154 IXGBE_SFF_1GBE_COMP_CODES,
1155 &comp_codes_1g);
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001156
Mark Rustade90dd262014-07-22 06:51:08 +00001157 if (status)
1158 goto err_read_i2c_eeprom;
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001159
Mark Rustade90dd262014-07-22 06:51:08 +00001160 status = hw->phy.ops.read_i2c_eeprom(hw,
1161 IXGBE_SFF_10GBE_COMP_CODES,
1162 &comp_codes_10g);
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001163
Mark Rustade90dd262014-07-22 06:51:08 +00001164 if (status)
1165 goto err_read_i2c_eeprom;
1166 status = hw->phy.ops.read_i2c_eeprom(hw,
1167 IXGBE_SFF_CABLE_TECHNOLOGY,
1168 &cable_tech);
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001169
Mark Rustade90dd262014-07-22 06:51:08 +00001170 if (status)
1171 goto err_read_i2c_eeprom;
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001172
Mark Rustade90dd262014-07-22 06:51:08 +00001173 /* ID Module
1174 * =========
1175 * 0 SFP_DA_CU
1176 * 1 SFP_SR
1177 * 2 SFP_LR
1178 * 3 SFP_DA_CORE0 - 82599-specific
1179 * 4 SFP_DA_CORE1 - 82599-specific
1180 * 5 SFP_SR/LR_CORE0 - 82599-specific
1181 * 6 SFP_SR/LR_CORE1 - 82599-specific
1182 * 7 SFP_act_lmt_DA_CORE0 - 82599-specific
1183 * 8 SFP_act_lmt_DA_CORE1 - 82599-specific
1184 * 9 SFP_1g_cu_CORE0 - 82599-specific
1185 * 10 SFP_1g_cu_CORE1 - 82599-specific
1186 * 11 SFP_1g_sx_CORE0 - 82599-specific
1187 * 12 SFP_1g_sx_CORE1 - 82599-specific
1188 */
1189 if (hw->mac.type == ixgbe_mac_82598EB) {
1190 if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
1191 hw->phy.sfp_type = ixgbe_sfp_type_da_cu;
1192 else if (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)
1193 hw->phy.sfp_type = ixgbe_sfp_type_sr;
1194 else if (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)
1195 hw->phy.sfp_type = ixgbe_sfp_type_lr;
1196 else
1197 hw->phy.sfp_type = ixgbe_sfp_type_unknown;
Mark Rustad69eec0c2015-08-08 16:18:43 -07001198 } else {
Mark Rustade90dd262014-07-22 06:51:08 +00001199 if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE) {
1200 if (hw->bus.lan_id == 0)
1201 hw->phy.sfp_type =
1202 ixgbe_sfp_type_da_cu_core0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001203 else
Mark Rustade90dd262014-07-22 06:51:08 +00001204 hw->phy.sfp_type =
1205 ixgbe_sfp_type_da_cu_core1;
1206 } else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE) {
1207 hw->phy.ops.read_i2c_eeprom(
1208 hw, IXGBE_SFF_CABLE_SPEC_COMP,
1209 &cable_spec);
1210 if (cable_spec &
1211 IXGBE_SFF_DA_SPEC_ACTIVE_LIMITING) {
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001212 if (hw->bus.lan_id == 0)
1213 hw->phy.sfp_type =
Mark Rustade90dd262014-07-22 06:51:08 +00001214 ixgbe_sfp_type_da_act_lmt_core0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001215 else
1216 hw->phy.sfp_type =
Mark Rustade90dd262014-07-22 06:51:08 +00001217 ixgbe_sfp_type_da_act_lmt_core1;
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001218 } else {
Mark Rustade90dd262014-07-22 06:51:08 +00001219 hw->phy.sfp_type =
1220 ixgbe_sfp_type_unknown;
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001221 }
Mark Rustade90dd262014-07-22 06:51:08 +00001222 } else if (comp_codes_10g &
1223 (IXGBE_SFF_10GBASESR_CAPABLE |
1224 IXGBE_SFF_10GBASELR_CAPABLE)) {
1225 if (hw->bus.lan_id == 0)
1226 hw->phy.sfp_type =
1227 ixgbe_sfp_type_srlr_core0;
1228 else
1229 hw->phy.sfp_type =
1230 ixgbe_sfp_type_srlr_core1;
1231 } else if (comp_codes_1g & IXGBE_SFF_1GBASET_CAPABLE) {
1232 if (hw->bus.lan_id == 0)
1233 hw->phy.sfp_type =
1234 ixgbe_sfp_type_1g_cu_core0;
1235 else
1236 hw->phy.sfp_type =
1237 ixgbe_sfp_type_1g_cu_core1;
1238 } else if (comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) {
1239 if (hw->bus.lan_id == 0)
1240 hw->phy.sfp_type =
1241 ixgbe_sfp_type_1g_sx_core0;
1242 else
1243 hw->phy.sfp_type =
1244 ixgbe_sfp_type_1g_sx_core1;
1245 } else if (comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) {
1246 if (hw->bus.lan_id == 0)
1247 hw->phy.sfp_type =
1248 ixgbe_sfp_type_1g_lx_core0;
1249 else
1250 hw->phy.sfp_type =
1251 ixgbe_sfp_type_1g_lx_core1;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001252 } else {
Mark Rustade90dd262014-07-22 06:51:08 +00001253 hw->phy.sfp_type = ixgbe_sfp_type_unknown;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001254 }
Donald Skidmorec4900be2008-11-20 21:11:42 -08001255 }
1256
Mark Rustade90dd262014-07-22 06:51:08 +00001257 if (hw->phy.sfp_type != stored_sfp_type)
1258 hw->phy.sfp_setup_needed = true;
1259
1260 /* Determine if the SFP+ PHY is dual speed or not. */
1261 hw->phy.multispeed_fiber = false;
1262 if (((comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) &&
1263 (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)) ||
1264 ((comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) &&
1265 (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)))
1266 hw->phy.multispeed_fiber = true;
1267
1268 /* Determine PHY vendor */
1269 if (hw->phy.type != ixgbe_phy_nl) {
1270 hw->phy.id = identifier;
1271 status = hw->phy.ops.read_i2c_eeprom(hw,
1272 IXGBE_SFF_VENDOR_OUI_BYTE0,
1273 &oui_bytes[0]);
1274
1275 if (status != 0)
1276 goto err_read_i2c_eeprom;
1277
1278 status = hw->phy.ops.read_i2c_eeprom(hw,
1279 IXGBE_SFF_VENDOR_OUI_BYTE1,
1280 &oui_bytes[1]);
1281
1282 if (status != 0)
1283 goto err_read_i2c_eeprom;
1284
1285 status = hw->phy.ops.read_i2c_eeprom(hw,
1286 IXGBE_SFF_VENDOR_OUI_BYTE2,
1287 &oui_bytes[2]);
1288
1289 if (status != 0)
1290 goto err_read_i2c_eeprom;
1291
1292 vendor_oui =
1293 ((oui_bytes[0] << IXGBE_SFF_VENDOR_OUI_BYTE0_SHIFT) |
1294 (oui_bytes[1] << IXGBE_SFF_VENDOR_OUI_BYTE1_SHIFT) |
1295 (oui_bytes[2] << IXGBE_SFF_VENDOR_OUI_BYTE2_SHIFT));
1296
1297 switch (vendor_oui) {
1298 case IXGBE_SFF_VENDOR_OUI_TYCO:
1299 if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
1300 hw->phy.type =
1301 ixgbe_phy_sfp_passive_tyco;
1302 break;
1303 case IXGBE_SFF_VENDOR_OUI_FTL:
1304 if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
1305 hw->phy.type = ixgbe_phy_sfp_ftl_active;
1306 else
1307 hw->phy.type = ixgbe_phy_sfp_ftl;
1308 break;
1309 case IXGBE_SFF_VENDOR_OUI_AVAGO:
1310 hw->phy.type = ixgbe_phy_sfp_avago;
1311 break;
1312 case IXGBE_SFF_VENDOR_OUI_INTEL:
1313 hw->phy.type = ixgbe_phy_sfp_intel;
1314 break;
1315 default:
1316 if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
1317 hw->phy.type =
1318 ixgbe_phy_sfp_passive_unknown;
1319 else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
1320 hw->phy.type =
1321 ixgbe_phy_sfp_active_unknown;
1322 else
1323 hw->phy.type = ixgbe_phy_sfp_unknown;
1324 break;
1325 }
1326 }
1327
1328 /* Allow any DA cable vendor */
1329 if (cable_tech & (IXGBE_SFF_DA_PASSIVE_CABLE |
1330 IXGBE_SFF_DA_ACTIVE_CABLE))
1331 return 0;
1332
1333 /* Verify supported 1G SFP modules */
1334 if (comp_codes_10g == 0 &&
1335 !(hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
1336 hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
1337 hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
1338 hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
1339 hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core0 ||
1340 hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core1)) {
1341 hw->phy.type = ixgbe_phy_sfp_unsupported;
1342 return IXGBE_ERR_SFP_NOT_SUPPORTED;
1343 }
1344
1345 /* Anything else 82598-based is supported */
1346 if (hw->mac.type == ixgbe_mac_82598EB)
1347 return 0;
1348
1349 hw->mac.ops.get_device_caps(hw, &enforce_sfp);
1350 if (!(enforce_sfp & IXGBE_DEVICE_CAPS_ALLOW_ANY_SFP) &&
1351 !(hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
1352 hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
1353 hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
1354 hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
1355 hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core0 ||
1356 hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core1)) {
1357 /* Make sure we're a supported PHY type */
1358 if (hw->phy.type == ixgbe_phy_sfp_intel)
1359 return 0;
1360 if (hw->allow_unsupported_sfp) {
1361 e_warn(drv, "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");
1362 return 0;
1363 }
1364 hw_dbg(hw, "SFP+ module not supported\n");
1365 hw->phy.type = ixgbe_phy_sfp_unsupported;
1366 return IXGBE_ERR_SFP_NOT_SUPPORTED;
1367 }
1368 return 0;
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001369
1370err_read_i2c_eeprom:
1371 hw->phy.sfp_type = ixgbe_sfp_type_not_present;
1372 if (hw->phy.type != ixgbe_phy_nl) {
1373 hw->phy.id = 0;
1374 hw->phy.type = ixgbe_phy_unknown;
1375 }
1376 return IXGBE_ERR_SFP_NOT_PRESENT;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001377}
1378
1379/**
Don Skidmore8f583322013-07-27 06:25:38 +00001380 * ixgbe_identify_qsfp_module_generic - Identifies QSFP modules
1381 * @hw: pointer to hardware structure
1382 *
1383 * Searches for and identifies the QSFP module and assigns appropriate PHY type
1384 **/
Mark Rustad88217542013-11-23 03:19:19 +00001385static s32 ixgbe_identify_qsfp_module_generic(struct ixgbe_hw *hw)
Don Skidmore8f583322013-07-27 06:25:38 +00001386{
1387 struct ixgbe_adapter *adapter = hw->back;
Mark Rustade90dd262014-07-22 06:51:08 +00001388 s32 status;
Don Skidmore8f583322013-07-27 06:25:38 +00001389 u32 vendor_oui = 0;
1390 enum ixgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
1391 u8 identifier = 0;
1392 u8 comp_codes_1g = 0;
1393 u8 comp_codes_10g = 0;
1394 u8 oui_bytes[3] = {0, 0, 0};
1395 u16 enforce_sfp = 0;
Emil Tantilov9a84fea2013-08-16 23:11:14 +00001396 u8 connector = 0;
1397 u8 cable_length = 0;
1398 u8 device_tech = 0;
1399 bool active_cable = false;
Don Skidmore8f583322013-07-27 06:25:38 +00001400
1401 if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_fiber_qsfp) {
1402 hw->phy.sfp_type = ixgbe_sfp_type_not_present;
Mark Rustade90dd262014-07-22 06:51:08 +00001403 return IXGBE_ERR_SFP_NOT_PRESENT;
Don Skidmore8f583322013-07-27 06:25:38 +00001404 }
1405
Don Skidmore7e49d612015-06-09 17:48:54 -07001406 /* LAN ID is needed for sfp_type determination */
1407 hw->mac.ops.set_lan_id(hw);
1408
Don Skidmore8f583322013-07-27 06:25:38 +00001409 status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_IDENTIFIER,
1410 &identifier);
1411
1412 if (status != 0)
1413 goto err_read_i2c_eeprom;
1414
1415 if (identifier != IXGBE_SFF_IDENTIFIER_QSFP_PLUS) {
1416 hw->phy.type = ixgbe_phy_sfp_unsupported;
Mark Rustade90dd262014-07-22 06:51:08 +00001417 return IXGBE_ERR_SFP_NOT_SUPPORTED;
Don Skidmore8f583322013-07-27 06:25:38 +00001418 }
1419
1420 hw->phy.id = identifier;
1421
Don Skidmore8f583322013-07-27 06:25:38 +00001422 status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_QSFP_10GBE_COMP,
1423 &comp_codes_10g);
1424
1425 if (status != 0)
1426 goto err_read_i2c_eeprom;
1427
Emil Tantilov61aaf9e2013-08-13 07:22:16 +00001428 status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_QSFP_1GBE_COMP,
1429 &comp_codes_1g);
1430
1431 if (status != 0)
1432 goto err_read_i2c_eeprom;
1433
Don Skidmore8f583322013-07-27 06:25:38 +00001434 if (comp_codes_10g & IXGBE_SFF_QSFP_DA_PASSIVE_CABLE) {
1435 hw->phy.type = ixgbe_phy_qsfp_passive_unknown;
1436 if (hw->bus.lan_id == 0)
1437 hw->phy.sfp_type = ixgbe_sfp_type_da_cu_core0;
1438 else
1439 hw->phy.sfp_type = ixgbe_sfp_type_da_cu_core1;
Don Skidmore8f583322013-07-27 06:25:38 +00001440 } else if (comp_codes_10g & (IXGBE_SFF_10GBASESR_CAPABLE |
1441 IXGBE_SFF_10GBASELR_CAPABLE)) {
1442 if (hw->bus.lan_id == 0)
1443 hw->phy.sfp_type = ixgbe_sfp_type_srlr_core0;
1444 else
1445 hw->phy.sfp_type = ixgbe_sfp_type_srlr_core1;
1446 } else {
Emil Tantilov9a84fea2013-08-16 23:11:14 +00001447 if (comp_codes_10g & IXGBE_SFF_QSFP_DA_ACTIVE_CABLE)
1448 active_cable = true;
1449
1450 if (!active_cable) {
1451 /* check for active DA cables that pre-date
1452 * SFF-8436 v3.6
1453 */
1454 hw->phy.ops.read_i2c_eeprom(hw,
1455 IXGBE_SFF_QSFP_CONNECTOR,
1456 &connector);
1457
1458 hw->phy.ops.read_i2c_eeprom(hw,
1459 IXGBE_SFF_QSFP_CABLE_LENGTH,
1460 &cable_length);
1461
1462 hw->phy.ops.read_i2c_eeprom(hw,
1463 IXGBE_SFF_QSFP_DEVICE_TECH,
1464 &device_tech);
1465
1466 if ((connector ==
1467 IXGBE_SFF_QSFP_CONNECTOR_NOT_SEPARABLE) &&
1468 (cable_length > 0) &&
1469 ((device_tech >> 4) ==
1470 IXGBE_SFF_QSFP_TRANSMITER_850NM_VCSEL))
1471 active_cable = true;
1472 }
1473
1474 if (active_cable) {
1475 hw->phy.type = ixgbe_phy_qsfp_active_unknown;
1476 if (hw->bus.lan_id == 0)
1477 hw->phy.sfp_type =
1478 ixgbe_sfp_type_da_act_lmt_core0;
1479 else
1480 hw->phy.sfp_type =
1481 ixgbe_sfp_type_da_act_lmt_core1;
1482 } else {
1483 /* unsupported module type */
1484 hw->phy.type = ixgbe_phy_sfp_unsupported;
Mark Rustade90dd262014-07-22 06:51:08 +00001485 return IXGBE_ERR_SFP_NOT_SUPPORTED;
Emil Tantilov9a84fea2013-08-16 23:11:14 +00001486 }
Don Skidmore8f583322013-07-27 06:25:38 +00001487 }
1488
1489 if (hw->phy.sfp_type != stored_sfp_type)
1490 hw->phy.sfp_setup_needed = true;
1491
1492 /* Determine if the QSFP+ PHY is dual speed or not. */
1493 hw->phy.multispeed_fiber = false;
1494 if (((comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) &&
1495 (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)) ||
1496 ((comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) &&
1497 (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)))
1498 hw->phy.multispeed_fiber = true;
1499
1500 /* Determine PHY vendor for optical modules */
1501 if (comp_codes_10g & (IXGBE_SFF_10GBASESR_CAPABLE |
1502 IXGBE_SFF_10GBASELR_CAPABLE)) {
1503 status = hw->phy.ops.read_i2c_eeprom(hw,
1504 IXGBE_SFF_QSFP_VENDOR_OUI_BYTE0,
1505 &oui_bytes[0]);
1506
1507 if (status != 0)
1508 goto err_read_i2c_eeprom;
1509
1510 status = hw->phy.ops.read_i2c_eeprom(hw,
1511 IXGBE_SFF_QSFP_VENDOR_OUI_BYTE1,
1512 &oui_bytes[1]);
1513
1514 if (status != 0)
1515 goto err_read_i2c_eeprom;
1516
1517 status = hw->phy.ops.read_i2c_eeprom(hw,
1518 IXGBE_SFF_QSFP_VENDOR_OUI_BYTE2,
1519 &oui_bytes[2]);
1520
1521 if (status != 0)
1522 goto err_read_i2c_eeprom;
1523
1524 vendor_oui =
1525 ((oui_bytes[0] << IXGBE_SFF_VENDOR_OUI_BYTE0_SHIFT) |
1526 (oui_bytes[1] << IXGBE_SFF_VENDOR_OUI_BYTE1_SHIFT) |
1527 (oui_bytes[2] << IXGBE_SFF_VENDOR_OUI_BYTE2_SHIFT));
1528
1529 if (vendor_oui == IXGBE_SFF_VENDOR_OUI_INTEL)
1530 hw->phy.type = ixgbe_phy_qsfp_intel;
1531 else
1532 hw->phy.type = ixgbe_phy_qsfp_unknown;
1533
1534 hw->mac.ops.get_device_caps(hw, &enforce_sfp);
1535 if (!(enforce_sfp & IXGBE_DEVICE_CAPS_ALLOW_ANY_SFP)) {
1536 /* Make sure we're a supported PHY type */
Mark Rustade90dd262014-07-22 06:51:08 +00001537 if (hw->phy.type == ixgbe_phy_qsfp_intel)
1538 return 0;
1539 if (hw->allow_unsupported_sfp) {
1540 e_warn(drv, "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");
1541 return 0;
Don Skidmore8f583322013-07-27 06:25:38 +00001542 }
Mark Rustade90dd262014-07-22 06:51:08 +00001543 hw_dbg(hw, "QSFP module not supported\n");
1544 hw->phy.type = ixgbe_phy_sfp_unsupported;
1545 return IXGBE_ERR_SFP_NOT_SUPPORTED;
Don Skidmore8f583322013-07-27 06:25:38 +00001546 }
Mark Rustade90dd262014-07-22 06:51:08 +00001547 return 0;
Don Skidmore8f583322013-07-27 06:25:38 +00001548 }
Mark Rustade90dd262014-07-22 06:51:08 +00001549 return 0;
Don Skidmore8f583322013-07-27 06:25:38 +00001550
1551err_read_i2c_eeprom:
1552 hw->phy.sfp_type = ixgbe_sfp_type_not_present;
1553 hw->phy.id = 0;
1554 hw->phy.type = ixgbe_phy_unknown;
1555
1556 return IXGBE_ERR_SFP_NOT_PRESENT;
1557}
1558
1559/**
Emil Tantilov76d97dd2011-02-16 10:14:00 +00001560 * ixgbe_get_sfp_init_sequence_offsets - Provides offset of PHY init sequence
Donald Skidmorec4900be2008-11-20 21:11:42 -08001561 * @hw: pointer to hardware structure
1562 * @list_offset: offset to the SFP ID list
1563 * @data_offset: offset to the SFP data block
Emil Tantilov75f19c32011-02-19 08:43:55 +00001564 *
1565 * Checks the MAC's EEPROM to see if it supports a given SFP+ module type, if
1566 * so it returns the offsets to the phy init sequence block.
Donald Skidmorec4900be2008-11-20 21:11:42 -08001567 **/
1568s32 ixgbe_get_sfp_init_sequence_offsets(struct ixgbe_hw *hw,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001569 u16 *list_offset,
1570 u16 *data_offset)
Donald Skidmorec4900be2008-11-20 21:11:42 -08001571{
1572 u16 sfp_id;
Don Skidmorecb836a92010-06-29 18:30:59 +00001573 u16 sfp_type = hw->phy.sfp_type;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001574
1575 if (hw->phy.sfp_type == ixgbe_sfp_type_unknown)
1576 return IXGBE_ERR_SFP_NOT_SUPPORTED;
1577
1578 if (hw->phy.sfp_type == ixgbe_sfp_type_not_present)
1579 return IXGBE_ERR_SFP_NOT_PRESENT;
1580
1581 if ((hw->device_id == IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM) &&
1582 (hw->phy.sfp_type == ixgbe_sfp_type_da_cu))
1583 return IXGBE_ERR_SFP_NOT_SUPPORTED;
1584
Don Skidmorecb836a92010-06-29 18:30:59 +00001585 /*
1586 * Limiting active cables and 1G Phys must be initialized as
1587 * SR modules
1588 */
1589 if (sfp_type == ixgbe_sfp_type_da_act_lmt_core0 ||
Don Skidmore345be202013-04-11 06:23:34 +00001590 sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
Jacob Kellera49fda32012-06-08 06:59:09 +00001591 sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
1592 sfp_type == ixgbe_sfp_type_1g_sx_core0)
Don Skidmorecb836a92010-06-29 18:30:59 +00001593 sfp_type = ixgbe_sfp_type_srlr_core0;
1594 else if (sfp_type == ixgbe_sfp_type_da_act_lmt_core1 ||
Don Skidmore345be202013-04-11 06:23:34 +00001595 sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
Jacob Kellera49fda32012-06-08 06:59:09 +00001596 sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
1597 sfp_type == ixgbe_sfp_type_1g_sx_core1)
Don Skidmorecb836a92010-06-29 18:30:59 +00001598 sfp_type = ixgbe_sfp_type_srlr_core1;
1599
Donald Skidmorec4900be2008-11-20 21:11:42 -08001600 /* Read offset to PHY init contents */
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001601 if (hw->eeprom.ops.read(hw, IXGBE_PHY_INIT_OFFSET_NL, list_offset)) {
1602 hw_err(hw, "eeprom read at %d failed\n",
1603 IXGBE_PHY_INIT_OFFSET_NL);
1604 return IXGBE_ERR_SFP_NO_INIT_SEQ_PRESENT;
1605 }
Donald Skidmorec4900be2008-11-20 21:11:42 -08001606
1607 if ((!*list_offset) || (*list_offset == 0xFFFF))
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001608 return IXGBE_ERR_SFP_NO_INIT_SEQ_PRESENT;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001609
1610 /* Shift offset to first ID word */
1611 (*list_offset)++;
1612
1613 /*
1614 * Find the matching SFP ID in the EEPROM
1615 * and program the init sequence
1616 */
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001617 if (hw->eeprom.ops.read(hw, *list_offset, &sfp_id))
1618 goto err_phy;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001619
1620 while (sfp_id != IXGBE_PHY_INIT_END_NL) {
Don Skidmorecb836a92010-06-29 18:30:59 +00001621 if (sfp_id == sfp_type) {
Donald Skidmorec4900be2008-11-20 21:11:42 -08001622 (*list_offset)++;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001623 if (hw->eeprom.ops.read(hw, *list_offset, data_offset))
1624 goto err_phy;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001625 if ((!*data_offset) || (*data_offset == 0xFFFF)) {
1626 hw_dbg(hw, "SFP+ module not supported\n");
1627 return IXGBE_ERR_SFP_NOT_SUPPORTED;
1628 } else {
1629 break;
1630 }
1631 } else {
1632 (*list_offset) += 2;
1633 if (hw->eeprom.ops.read(hw, *list_offset, &sfp_id))
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001634 goto err_phy;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001635 }
1636 }
1637
1638 if (sfp_id == IXGBE_PHY_INIT_END_NL) {
1639 hw_dbg(hw, "No matching SFP+ module found\n");
1640 return IXGBE_ERR_SFP_NOT_SUPPORTED;
1641 }
1642
1643 return 0;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00001644
1645err_phy:
1646 hw_err(hw, "eeprom read at offset %d failed\n", *list_offset);
1647 return IXGBE_ERR_PHY;
Donald Skidmorec4900be2008-11-20 21:11:42 -08001648}
1649
1650/**
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001651 * ixgbe_read_i2c_eeprom_generic - Reads 8 bit EEPROM word over I2C interface
1652 * @hw: pointer to hardware structure
1653 * @byte_offset: EEPROM byte offset to read
1654 * @eeprom_data: value read
1655 *
1656 * Performs byte read operation to SFP module's EEPROM over I2C interface.
1657 **/
1658s32 ixgbe_read_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001659 u8 *eeprom_data)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001660{
1661 return hw->phy.ops.read_i2c_byte(hw, byte_offset,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001662 IXGBE_I2C_EEPROM_DEV_ADDR,
1663 eeprom_data);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001664}
1665
1666/**
Emil Tantilov07ce8702012-12-19 07:14:17 +00001667 * ixgbe_read_i2c_sff8472_generic - Reads 8 bit word over I2C interface
1668 * @hw: pointer to hardware structure
1669 * @byte_offset: byte offset at address 0xA2
1670 * @eeprom_data: value read
1671 *
1672 * Performs byte read operation to SFP module's SFF-8472 data over I2C
1673 **/
1674s32 ixgbe_read_i2c_sff8472_generic(struct ixgbe_hw *hw, u8 byte_offset,
1675 u8 *sff8472_data)
1676{
1677 return hw->phy.ops.read_i2c_byte(hw, byte_offset,
1678 IXGBE_I2C_EEPROM_DEV_ADDR2,
1679 sff8472_data);
1680}
1681
1682/**
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001683 * ixgbe_write_i2c_eeprom_generic - Writes 8 bit EEPROM word over I2C interface
1684 * @hw: pointer to hardware structure
1685 * @byte_offset: EEPROM byte offset to write
1686 * @eeprom_data: value to write
1687 *
1688 * Performs byte write operation to SFP module's EEPROM over I2C interface.
1689 **/
1690s32 ixgbe_write_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001691 u8 eeprom_data)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001692{
1693 return hw->phy.ops.write_i2c_byte(hw, byte_offset,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001694 IXGBE_I2C_EEPROM_DEV_ADDR,
1695 eeprom_data);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001696}
1697
1698/**
Mark Rustad56f6ed12015-08-08 16:18:22 -07001699 * ixgbe_is_sfp_probe - Returns true if SFP is being detected
1700 * @hw: pointer to hardware structure
1701 * @offset: eeprom offset to be read
1702 * @addr: I2C address to be read
1703 */
1704static bool ixgbe_is_sfp_probe(struct ixgbe_hw *hw, u8 offset, u8 addr)
1705{
1706 if (addr == IXGBE_I2C_EEPROM_DEV_ADDR &&
1707 offset == IXGBE_SFF_IDENTIFIER &&
1708 hw->phy.sfp_type == ixgbe_sfp_type_not_present)
1709 return true;
1710 return false;
1711}
1712
1713/**
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001714 * ixgbe_read_i2c_byte_generic_int - Reads 8 bit word over I2C
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001715 * @hw: pointer to hardware structure
1716 * @byte_offset: byte offset to read
1717 * @data: value read
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001718 * @lock: true if to take and release semaphore
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001719 *
1720 * Performs byte read operation to SFP module's EEPROM over I2C interface at
Emil Tantilov3fbaa3a2011-08-30 13:33:51 +00001721 * a specified device address.
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001722 */
1723static s32 ixgbe_read_i2c_byte_generic_int(struct ixgbe_hw *hw, u8 byte_offset,
1724 u8 dev_addr, u8 *data, bool lock)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001725{
Mark Rustade90dd262014-07-22 06:51:08 +00001726 s32 status;
Emil Tantilov75f19c32011-02-19 08:43:55 +00001727 u32 max_retry = 10;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001728 u32 retry = 0;
Don Skidmore030eaec2014-11-29 05:22:37 +00001729 u32 swfw_mask = hw->phy.phy_semaphore_mask;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001730 bool nack = true;
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001731
Tony Nguyen3f0d6462016-11-10 09:57:29 -08001732 if (hw->mac.type >= ixgbe_mac_X550)
1733 max_retry = 3;
Mark Rustad56f6ed12015-08-08 16:18:22 -07001734 if (ixgbe_is_sfp_probe(hw, byte_offset, dev_addr))
1735 max_retry = IXGBE_SFP_DETECT_RETRIES;
1736
Emil Tantilov3fbaa3a2011-08-30 13:33:51 +00001737 *data = 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001738
1739 do {
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001740 if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
Mark Rustade90dd262014-07-22 06:51:08 +00001741 return IXGBE_ERR_SWFW_SYNC;
Emil Tantilov75f19c32011-02-19 08:43:55 +00001742
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001743 ixgbe_i2c_start(hw);
1744
1745 /* Device Address and write indication */
1746 status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
1747 if (status != 0)
1748 goto fail;
1749
1750 status = ixgbe_get_i2c_ack(hw);
1751 if (status != 0)
1752 goto fail;
1753
1754 status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
1755 if (status != 0)
1756 goto fail;
1757
1758 status = ixgbe_get_i2c_ack(hw);
1759 if (status != 0)
1760 goto fail;
1761
1762 ixgbe_i2c_start(hw);
1763
1764 /* Device Address and read indication */
1765 status = ixgbe_clock_out_i2c_byte(hw, (dev_addr | 0x1));
1766 if (status != 0)
1767 goto fail;
1768
1769 status = ixgbe_get_i2c_ack(hw);
1770 if (status != 0)
1771 goto fail;
1772
1773 status = ixgbe_clock_in_i2c_byte(hw, data);
1774 if (status != 0)
1775 goto fail;
1776
1777 status = ixgbe_clock_out_i2c_bit(hw, nack);
1778 if (status != 0)
1779 goto fail;
1780
1781 ixgbe_i2c_stop(hw);
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001782 if (lock)
1783 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
1784 return 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001785
1786fail:
Emil Tantilovd0310dc2013-01-18 02:16:41 +00001787 ixgbe_i2c_bus_clear(hw);
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001788 if (lock) {
1789 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
1790 msleep(100);
1791 }
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001792 retry++;
1793 if (retry < max_retry)
1794 hw_dbg(hw, "I2C byte read error - Retrying.\n");
1795 else
1796 hw_dbg(hw, "I2C byte read error.\n");
1797
1798 } while (retry < max_retry);
1799
1800 return status;
1801}
1802
1803/**
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001804 * ixgbe_read_i2c_byte_generic - Reads 8 bit word over I2C
1805 * @hw: pointer to hardware structure
1806 * @byte_offset: byte offset to read
1807 * @data: value read
1808 *
1809 * Performs byte read operation to SFP module's EEPROM over I2C interface at
1810 * a specified device address.
1811 */
1812s32 ixgbe_read_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
1813 u8 dev_addr, u8 *data)
1814{
1815 return ixgbe_read_i2c_byte_generic_int(hw, byte_offset, dev_addr,
1816 data, true);
1817}
1818
1819/**
1820 * ixgbe_read_i2c_byte_generic_unlocked - Reads 8 bit word over I2C
1821 * @hw: pointer to hardware structure
1822 * @byte_offset: byte offset to read
1823 * @data: value read
1824 *
1825 * Performs byte read operation to SFP module's EEPROM over I2C interface at
1826 * a specified device address.
1827 */
1828s32 ixgbe_read_i2c_byte_generic_unlocked(struct ixgbe_hw *hw, u8 byte_offset,
1829 u8 dev_addr, u8 *data)
1830{
1831 return ixgbe_read_i2c_byte_generic_int(hw, byte_offset, dev_addr,
1832 data, false);
1833}
1834
1835/**
1836 * ixgbe_write_i2c_byte_generic_int - Writes 8 bit word over I2C
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001837 * @hw: pointer to hardware structure
1838 * @byte_offset: byte offset to write
1839 * @data: value to write
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001840 * @lock: true if to take and release semaphore
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001841 *
1842 * Performs byte write operation to SFP module's EEPROM over I2C interface at
1843 * a specified device address.
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001844 */
1845static s32 ixgbe_write_i2c_byte_generic_int(struct ixgbe_hw *hw, u8 byte_offset,
1846 u8 dev_addr, u8 data, bool lock)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001847{
Mark Rustade90dd262014-07-22 06:51:08 +00001848 s32 status;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001849 u32 max_retry = 1;
1850 u32 retry = 0;
Don Skidmore030eaec2014-11-29 05:22:37 +00001851 u32 swfw_mask = hw->phy.phy_semaphore_mask;
Emil Tantilov75f19c32011-02-19 08:43:55 +00001852
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001853 if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
Mark Rustade90dd262014-07-22 06:51:08 +00001854 return IXGBE_ERR_SWFW_SYNC;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001855
1856 do {
1857 ixgbe_i2c_start(hw);
1858
1859 status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
1860 if (status != 0)
1861 goto fail;
1862
1863 status = ixgbe_get_i2c_ack(hw);
1864 if (status != 0)
1865 goto fail;
1866
1867 status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
1868 if (status != 0)
1869 goto fail;
1870
1871 status = ixgbe_get_i2c_ack(hw);
1872 if (status != 0)
1873 goto fail;
1874
1875 status = ixgbe_clock_out_i2c_byte(hw, data);
1876 if (status != 0)
1877 goto fail;
1878
1879 status = ixgbe_get_i2c_ack(hw);
1880 if (status != 0)
1881 goto fail;
1882
1883 ixgbe_i2c_stop(hw);
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001884 if (lock)
1885 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
1886 return 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001887
1888fail:
1889 ixgbe_i2c_bus_clear(hw);
1890 retry++;
1891 if (retry < max_retry)
1892 hw_dbg(hw, "I2C byte write error - Retrying.\n");
1893 else
1894 hw_dbg(hw, "I2C byte write error.\n");
1895 } while (retry < max_retry);
1896
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001897 if (lock)
1898 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
Emil Tantilov75f19c32011-02-19 08:43:55 +00001899
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001900 return status;
1901}
1902
1903/**
Mark Rustadbb5ce9a52015-08-08 16:18:02 -07001904 * ixgbe_write_i2c_byte_generic - Writes 8 bit word over I2C
1905 * @hw: pointer to hardware structure
1906 * @byte_offset: byte offset to write
1907 * @data: value to write
1908 *
1909 * Performs byte write operation to SFP module's EEPROM over I2C interface at
1910 * a specified device address.
1911 */
1912s32 ixgbe_write_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
1913 u8 dev_addr, u8 data)
1914{
1915 return ixgbe_write_i2c_byte_generic_int(hw, byte_offset, dev_addr,
1916 data, true);
1917}
1918
1919/**
1920 * ixgbe_write_i2c_byte_generic_unlocked - Writes 8 bit word over I2C
1921 * @hw: pointer to hardware structure
1922 * @byte_offset: byte offset to write
1923 * @data: value to write
1924 *
1925 * Performs byte write operation to SFP module's EEPROM over I2C interface at
1926 * a specified device address.
1927 */
1928s32 ixgbe_write_i2c_byte_generic_unlocked(struct ixgbe_hw *hw, u8 byte_offset,
1929 u8 dev_addr, u8 data)
1930{
1931 return ixgbe_write_i2c_byte_generic_int(hw, byte_offset, dev_addr,
1932 data, false);
1933}
1934
1935/**
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001936 * ixgbe_i2c_start - Sets I2C start condition
1937 * @hw: pointer to hardware structure
1938 *
1939 * Sets I2C start condition (High -> Low on SDA while SCL is High)
Mark Rustad25b10292015-08-08 16:18:12 -07001940 * Set bit-bang mode on X550 hardware.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001941 **/
1942static void ixgbe_i2c_start(struct ixgbe_hw *hw)
1943{
Don Skidmore9a900ec2015-06-09 17:15:01 -07001944 u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001945
Mark Rustad25b10292015-08-08 16:18:12 -07001946 i2cctl |= IXGBE_I2C_BB_EN(hw);
1947
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001948 /* Start condition must begin with data and clock high */
1949 ixgbe_set_i2c_data(hw, &i2cctl, 1);
1950 ixgbe_raise_i2c_clk(hw, &i2cctl);
1951
1952 /* Setup time for start condition (4.7us) */
1953 udelay(IXGBE_I2C_T_SU_STA);
1954
1955 ixgbe_set_i2c_data(hw, &i2cctl, 0);
1956
1957 /* Hold time for start condition (4us) */
1958 udelay(IXGBE_I2C_T_HD_STA);
1959
1960 ixgbe_lower_i2c_clk(hw, &i2cctl);
1961
1962 /* Minimum low period of clock is 4.7 us */
1963 udelay(IXGBE_I2C_T_LOW);
1964
1965}
1966
1967/**
1968 * ixgbe_i2c_stop - Sets I2C stop condition
1969 * @hw: pointer to hardware structure
1970 *
1971 * Sets I2C stop condition (Low -> High on SDA while SCL is High)
Mark Rustad25b10292015-08-08 16:18:12 -07001972 * Disables bit-bang mode and negates data output enable on X550
1973 * hardware.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001974 **/
1975static void ixgbe_i2c_stop(struct ixgbe_hw *hw)
1976{
Don Skidmore9a900ec2015-06-09 17:15:01 -07001977 u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
Mark Rustad25b10292015-08-08 16:18:12 -07001978 u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
1979 u32 clk_oe_bit = IXGBE_I2C_CLK_OE_N_EN(hw);
1980 u32 bb_en_bit = IXGBE_I2C_BB_EN(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00001981
1982 /* Stop condition must begin with data low and clock high */
1983 ixgbe_set_i2c_data(hw, &i2cctl, 0);
1984 ixgbe_raise_i2c_clk(hw, &i2cctl);
1985
1986 /* Setup time for stop condition (4us) */
1987 udelay(IXGBE_I2C_T_SU_STO);
1988
1989 ixgbe_set_i2c_data(hw, &i2cctl, 1);
1990
1991 /* bus free time between stop and start (4.7us)*/
1992 udelay(IXGBE_I2C_T_BUF);
Mark Rustad25b10292015-08-08 16:18:12 -07001993
1994 if (bb_en_bit || data_oe_bit || clk_oe_bit) {
1995 i2cctl &= ~bb_en_bit;
1996 i2cctl |= data_oe_bit | clk_oe_bit;
1997 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
1998 IXGBE_WRITE_FLUSH(hw);
1999 }
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002000}
2001
2002/**
2003 * ixgbe_clock_in_i2c_byte - Clocks in one byte via I2C
2004 * @hw: pointer to hardware structure
2005 * @data: data byte to clock in
2006 *
2007 * Clocks in one byte data via I2C data/clock
2008 **/
2009static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data)
2010{
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002011 s32 i;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002012 bool bit = false;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002013
Mark Rustad6ee8c9a2015-08-08 16:18:17 -07002014 *data = 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002015 for (i = 7; i >= 0; i--) {
Emil Tantilove1befd72011-08-27 07:18:47 +00002016 ixgbe_clock_in_i2c_bit(hw, &bit);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002017 *data |= bit << i;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002018 }
2019
Emil Tantilove1befd72011-08-27 07:18:47 +00002020 return 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002021}
2022
2023/**
2024 * ixgbe_clock_out_i2c_byte - Clocks out one byte via I2C
2025 * @hw: pointer to hardware structure
2026 * @data: data byte clocked out
2027 *
2028 * Clocks out one byte data via I2C data/clock
2029 **/
2030static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data)
2031{
Mark Rustade90dd262014-07-22 06:51:08 +00002032 s32 status;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002033 s32 i;
2034 u32 i2cctl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002035 bool bit = false;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002036
2037 for (i = 7; i >= 0; i--) {
2038 bit = (data >> i) & 0x1;
2039 status = ixgbe_clock_out_i2c_bit(hw, bit);
2040
2041 if (status != 0)
2042 break;
2043 }
2044
2045 /* Release SDA line (set high) */
Don Skidmore9a900ec2015-06-09 17:15:01 -07002046 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
2047 i2cctl |= IXGBE_I2C_DATA_OUT(hw);
Mark Rustad25b10292015-08-08 16:18:12 -07002048 i2cctl |= IXGBE_I2C_DATA_OE_N_EN(hw);
Don Skidmore9a900ec2015-06-09 17:15:01 -07002049 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
Emil Tantilov176f9502011-11-04 06:43:23 +00002050 IXGBE_WRITE_FLUSH(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002051
2052 return status;
2053}
2054
2055/**
2056 * ixgbe_get_i2c_ack - Polls for I2C ACK
2057 * @hw: pointer to hardware structure
2058 *
2059 * Clocks in/out one bit via I2C data/clock
2060 **/
2061static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw)
2062{
Mark Rustad25b10292015-08-08 16:18:12 -07002063 u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
Emil Tantilove1befd72011-08-27 07:18:47 +00002064 s32 status = 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002065 u32 i = 0;
Don Skidmore9a900ec2015-06-09 17:15:01 -07002066 u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002067 u32 timeout = 10;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002068 bool ack = true;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002069
Mark Rustad25b10292015-08-08 16:18:12 -07002070 if (data_oe_bit) {
2071 i2cctl |= IXGBE_I2C_DATA_OUT(hw);
2072 i2cctl |= data_oe_bit;
2073 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
2074 IXGBE_WRITE_FLUSH(hw);
2075 }
Emil Tantilove1befd72011-08-27 07:18:47 +00002076 ixgbe_raise_i2c_clk(hw, &i2cctl);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002077
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002078 /* Minimum high period of clock is 4us */
2079 udelay(IXGBE_I2C_T_HIGH);
2080
2081 /* Poll for ACK. Note that ACK in I2C spec is
2082 * transition from 1 to 0 */
2083 for (i = 0; i < timeout; i++) {
Don Skidmore9a900ec2015-06-09 17:15:01 -07002084 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
Don Skidmore9a75a1a2014-11-07 03:53:35 +00002085 ack = ixgbe_get_i2c_data(hw, &i2cctl);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002086
2087 udelay(1);
2088 if (ack == 0)
2089 break;
2090 }
2091
2092 if (ack == 1) {
2093 hw_dbg(hw, "I2C ack was not received.\n");
2094 status = IXGBE_ERR_I2C;
2095 }
2096
2097 ixgbe_lower_i2c_clk(hw, &i2cctl);
2098
2099 /* Minimum low period of clock is 4.7 us */
2100 udelay(IXGBE_I2C_T_LOW);
2101
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002102 return status;
2103}
2104
2105/**
2106 * ixgbe_clock_in_i2c_bit - Clocks in one bit via I2C data/clock
2107 * @hw: pointer to hardware structure
2108 * @data: read data value
2109 *
2110 * Clocks in one bit via I2C data/clock
2111 **/
2112static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data)
2113{
Don Skidmore9a900ec2015-06-09 17:15:01 -07002114 u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
Mark Rustad25b10292015-08-08 16:18:12 -07002115 u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002116
Mark Rustad25b10292015-08-08 16:18:12 -07002117 if (data_oe_bit) {
2118 i2cctl |= IXGBE_I2C_DATA_OUT(hw);
2119 i2cctl |= data_oe_bit;
2120 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
2121 IXGBE_WRITE_FLUSH(hw);
2122 }
Emil Tantilove1befd72011-08-27 07:18:47 +00002123 ixgbe_raise_i2c_clk(hw, &i2cctl);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002124
2125 /* Minimum high period of clock is 4us */
2126 udelay(IXGBE_I2C_T_HIGH);
2127
Don Skidmore9a900ec2015-06-09 17:15:01 -07002128 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
Don Skidmore9a75a1a2014-11-07 03:53:35 +00002129 *data = ixgbe_get_i2c_data(hw, &i2cctl);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002130
2131 ixgbe_lower_i2c_clk(hw, &i2cctl);
2132
2133 /* Minimum low period of clock is 4.7 us */
2134 udelay(IXGBE_I2C_T_LOW);
2135
Emil Tantilove1befd72011-08-27 07:18:47 +00002136 return 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002137}
2138
2139/**
2140 * ixgbe_clock_out_i2c_bit - Clocks in/out one bit via I2C data/clock
2141 * @hw: pointer to hardware structure
2142 * @data: data value to write
2143 *
2144 * Clocks out one bit via I2C data/clock
2145 **/
2146static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data)
2147{
2148 s32 status;
Don Skidmore9a900ec2015-06-09 17:15:01 -07002149 u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002150
2151 status = ixgbe_set_i2c_data(hw, &i2cctl, data);
2152 if (status == 0) {
Emil Tantilove1befd72011-08-27 07:18:47 +00002153 ixgbe_raise_i2c_clk(hw, &i2cctl);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002154
2155 /* Minimum high period of clock is 4us */
2156 udelay(IXGBE_I2C_T_HIGH);
2157
2158 ixgbe_lower_i2c_clk(hw, &i2cctl);
2159
2160 /* Minimum low period of clock is 4.7 us.
2161 * This also takes care of the data hold time.
2162 */
2163 udelay(IXGBE_I2C_T_LOW);
2164 } else {
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002165 hw_dbg(hw, "I2C data was not set to %X\n", data);
Mark Rustade90dd262014-07-22 06:51:08 +00002166 return IXGBE_ERR_I2C;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002167 }
2168
Mark Rustade90dd262014-07-22 06:51:08 +00002169 return 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002170}
2171/**
2172 * ixgbe_raise_i2c_clk - Raises the I2C SCL clock
2173 * @hw: pointer to hardware structure
2174 * @i2cctl: Current value of I2CCTL register
2175 *
2176 * Raises the I2C clock line '0'->'1'
Mark Rustad25b10292015-08-08 16:18:12 -07002177 * Negates the I2C clock output enable on X550 hardware.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002178 **/
Emil Tantilove1befd72011-08-27 07:18:47 +00002179static void ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002180{
Mark Rustad25b10292015-08-08 16:18:12 -07002181 u32 clk_oe_bit = IXGBE_I2C_CLK_OE_N_EN(hw);
Don Skidmore8f56e4b2012-03-15 07:36:37 +00002182 u32 i = 0;
2183 u32 timeout = IXGBE_I2C_CLOCK_STRETCHING_TIMEOUT;
2184 u32 i2cctl_r = 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002185
Mark Rustad25b10292015-08-08 16:18:12 -07002186 if (clk_oe_bit) {
2187 *i2cctl |= clk_oe_bit;
2188 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
2189 }
2190
Don Skidmore8f56e4b2012-03-15 07:36:37 +00002191 for (i = 0; i < timeout; i++) {
Don Skidmore9a900ec2015-06-09 17:15:01 -07002192 *i2cctl |= IXGBE_I2C_CLK_OUT(hw);
2193 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
Don Skidmore8f56e4b2012-03-15 07:36:37 +00002194 IXGBE_WRITE_FLUSH(hw);
2195 /* SCL rise time (1000ns) */
2196 udelay(IXGBE_I2C_T_RISE);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002197
Don Skidmore9a900ec2015-06-09 17:15:01 -07002198 i2cctl_r = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
2199 if (i2cctl_r & IXGBE_I2C_CLK_IN(hw))
Don Skidmore8f56e4b2012-03-15 07:36:37 +00002200 break;
2201 }
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002202}
2203
2204/**
2205 * ixgbe_lower_i2c_clk - Lowers the I2C SCL clock
2206 * @hw: pointer to hardware structure
2207 * @i2cctl: Current value of I2CCTL register
2208 *
2209 * Lowers the I2C clock line '1'->'0'
Mark Rustad25b10292015-08-08 16:18:12 -07002210 * Asserts the I2C clock output enable on X550 hardware.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002211 **/
2212static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
2213{
2214
Don Skidmore9a900ec2015-06-09 17:15:01 -07002215 *i2cctl &= ~IXGBE_I2C_CLK_OUT(hw);
Mark Rustad25b10292015-08-08 16:18:12 -07002216 *i2cctl &= ~IXGBE_I2C_CLK_OE_N_EN(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002217
Don Skidmore9a900ec2015-06-09 17:15:01 -07002218 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00002219 IXGBE_WRITE_FLUSH(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002220
2221 /* SCL fall time (300ns) */
2222 udelay(IXGBE_I2C_T_FALL);
2223}
2224
2225/**
2226 * ixgbe_set_i2c_data - Sets the I2C data bit
2227 * @hw: pointer to hardware structure
2228 * @i2cctl: Current value of I2CCTL register
2229 * @data: I2C data value (0 or 1) to set
2230 *
2231 * Sets the I2C data bit
Mark Rustad25b10292015-08-08 16:18:12 -07002232 * Asserts the I2C data output enable on X550 hardware.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002233 **/
2234static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data)
2235{
Mark Rustad25b10292015-08-08 16:18:12 -07002236 u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
2237
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002238 if (data)
Don Skidmore9a900ec2015-06-09 17:15:01 -07002239 *i2cctl |= IXGBE_I2C_DATA_OUT(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002240 else
Don Skidmore9a900ec2015-06-09 17:15:01 -07002241 *i2cctl &= ~IXGBE_I2C_DATA_OUT(hw);
Mark Rustad25b10292015-08-08 16:18:12 -07002242 *i2cctl &= ~data_oe_bit;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002243
Don Skidmore9a900ec2015-06-09 17:15:01 -07002244 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00002245 IXGBE_WRITE_FLUSH(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002246
2247 /* Data rise/fall (1000ns/300ns) and set-up time (250ns) */
2248 udelay(IXGBE_I2C_T_RISE + IXGBE_I2C_T_FALL + IXGBE_I2C_T_SU_DATA);
2249
Mark Rustad25b10292015-08-08 16:18:12 -07002250 if (!data) /* Can't verify data in this case */
2251 return 0;
2252 if (data_oe_bit) {
2253 *i2cctl |= data_oe_bit;
2254 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
2255 IXGBE_WRITE_FLUSH(hw);
2256 }
2257
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002258 /* Verify data was set correctly */
Don Skidmore9a900ec2015-06-09 17:15:01 -07002259 *i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
Don Skidmore9a75a1a2014-11-07 03:53:35 +00002260 if (data != ixgbe_get_i2c_data(hw, i2cctl)) {
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002261 hw_dbg(hw, "Error - I2C data was not set to %X.\n", data);
Mark Rustade90dd262014-07-22 06:51:08 +00002262 return IXGBE_ERR_I2C;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002263 }
2264
Mark Rustade90dd262014-07-22 06:51:08 +00002265 return 0;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002266}
2267
2268/**
2269 * ixgbe_get_i2c_data - Reads the I2C SDA data bit
2270 * @hw: pointer to hardware structure
2271 * @i2cctl: Current value of I2CCTL register
2272 *
2273 * Returns the I2C data bit value
Mark Rustad25b10292015-08-08 16:18:12 -07002274 * Negates the I2C data output enable on X550 hardware.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002275 **/
Don Skidmore9a75a1a2014-11-07 03:53:35 +00002276static bool ixgbe_get_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002277{
Mark Rustad25b10292015-08-08 16:18:12 -07002278 u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
2279
2280 if (data_oe_bit) {
2281 *i2cctl |= data_oe_bit;
2282 IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
2283 IXGBE_WRITE_FLUSH(hw);
2284 udelay(IXGBE_I2C_T_FALL);
2285 }
2286
Don Skidmore9a900ec2015-06-09 17:15:01 -07002287 if (*i2cctl & IXGBE_I2C_DATA_IN(hw))
Mark Rustade90dd262014-07-22 06:51:08 +00002288 return true;
2289 return false;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002290}
2291
2292/**
2293 * ixgbe_i2c_bus_clear - Clears the I2C bus
2294 * @hw: pointer to hardware structure
2295 *
2296 * Clears the I2C bus by sending nine clock pulses.
2297 * Used when data line is stuck low.
2298 **/
2299static void ixgbe_i2c_bus_clear(struct ixgbe_hw *hw)
2300{
Mark Rustad25b10292015-08-08 16:18:12 -07002301 u32 i2cctl;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002302 u32 i;
2303
Emil Tantilov75f19c32011-02-19 08:43:55 +00002304 ixgbe_i2c_start(hw);
Mark Rustad25b10292015-08-08 16:18:12 -07002305 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
Emil Tantilov75f19c32011-02-19 08:43:55 +00002306
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002307 ixgbe_set_i2c_data(hw, &i2cctl, 1);
2308
2309 for (i = 0; i < 9; i++) {
2310 ixgbe_raise_i2c_clk(hw, &i2cctl);
2311
2312 /* Min high period of clock is 4us */
2313 udelay(IXGBE_I2C_T_HIGH);
2314
2315 ixgbe_lower_i2c_clk(hw, &i2cctl);
2316
2317 /* Min low period of clock is 4.7us*/
2318 udelay(IXGBE_I2C_T_LOW);
2319 }
2320
Emil Tantilov75f19c32011-02-19 08:43:55 +00002321 ixgbe_i2c_start(hw);
2322
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002323 /* Put the i2c bus back to default state */
2324 ixgbe_i2c_stop(hw);
2325}
2326
2327/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002328 * ixgbe_tn_check_overtemp - Checks if an overtemp occurred.
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -07002329 * @hw: pointer to hardware structure
2330 *
2331 * Checks if the LASI temp alarm status was triggered due to overtemp
2332 **/
2333s32 ixgbe_tn_check_overtemp(struct ixgbe_hw *hw)
2334{
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -07002335 u16 phy_data = 0;
2336
2337 if (hw->device_id != IXGBE_DEV_ID_82599_T3_LOM)
Mark Rustade90dd262014-07-22 06:51:08 +00002338 return 0;
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -07002339
2340 /* Check that the LASI temp alarm status was triggered */
2341 hw->phy.ops.read_reg(hw, IXGBE_TN_LASI_STATUS_REG,
Jacob Kellere7cf7452014-04-09 06:03:10 +00002342 MDIO_MMD_PMAPMD, &phy_data);
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -07002343
2344 if (!(phy_data & IXGBE_TN_LASI_STATUS_TEMP_ALARM))
Mark Rustade90dd262014-07-22 06:51:08 +00002345 return 0;
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -07002346
Mark Rustade90dd262014-07-22 06:51:08 +00002347 return IXGBE_ERR_OVERTEMP;
Mallikarjuna R Chilakala119fc602010-05-20 23:07:06 -07002348}
Don Skidmore961fac82015-06-09 16:09:47 -07002349
2350/** ixgbe_set_copper_phy_power - Control power for copper phy
2351 * @hw: pointer to hardware structure
2352 * @on: true for on, false for off
2353 **/
2354s32 ixgbe_set_copper_phy_power(struct ixgbe_hw *hw, bool on)
2355{
2356 u32 status;
2357 u16 reg;
2358
2359 /* Bail if we don't have copper phy */
2360 if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_copper)
2361 return 0;
2362
Mark Rustad3c2f2b72015-11-05 11:02:14 -08002363 if (!on && ixgbe_mng_present(hw))
2364 return 0;
2365
Emil Tantilov4dc40002016-09-26 14:08:13 -07002366 status = hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_VEND1, &reg);
Don Skidmore961fac82015-06-09 16:09:47 -07002367 if (status)
2368 return status;
2369
2370 if (on) {
2371 reg &= ~IXGBE_MDIO_PHY_SET_LOW_POWER_MODE;
2372 } else {
2373 if (ixgbe_check_reset_blocked(hw))
2374 return 0;
2375 reg |= IXGBE_MDIO_PHY_SET_LOW_POWER_MODE;
2376 }
2377
Emil Tantilov4dc40002016-09-26 14:08:13 -07002378 status = hw->phy.ops.write_reg(hw, MDIO_CTRL1, MDIO_MMD_VEND1, reg);
Don Skidmore961fac82015-06-09 16:09:47 -07002379 return status;
2380}