net/txgbe: add link status change
[dpdk.git] / drivers / net / txgbe / base / txgbe_phy.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include "txgbe_hw.h"
6 #include "txgbe_eeprom.h"
7 #include "txgbe_mng.h"
8 #include "txgbe_phy.h"
9
10 static void txgbe_i2c_start(struct txgbe_hw *hw);
11 static void txgbe_i2c_stop(struct txgbe_hw *hw);
12
13 /**
14  * txgbe_identify_extphy - Identify a single address for a PHY
15  * @hw: pointer to hardware structure
16  * @phy_addr: PHY address to probe
17  *
18  * Returns true if PHY found
19  */
20 static bool txgbe_identify_extphy(struct txgbe_hw *hw)
21 {
22         u16 phy_addr = 0;
23
24         if (!txgbe_validate_phy_addr(hw, phy_addr)) {
25                 DEBUGOUT("Unable to validate PHY address 0x%04X\n",
26                         phy_addr);
27                 return false;
28         }
29
30         if (txgbe_get_phy_id(hw))
31                 return false;
32
33         hw->phy.type = txgbe_get_phy_type_from_id(hw->phy.id);
34         if (hw->phy.type == txgbe_phy_unknown) {
35                 u16 ext_ability = 0;
36                 hw->phy.read_reg(hw, TXGBE_MD_PHY_EXT_ABILITY,
37                                  TXGBE_MD_DEV_PMA_PMD,
38                                  &ext_ability);
39
40                 if (ext_ability & (TXGBE_MD_PHY_10GBASET_ABILITY |
41                         TXGBE_MD_PHY_1000BASET_ABILITY))
42                         hw->phy.type = txgbe_phy_cu_unknown;
43                 else
44                         hw->phy.type = txgbe_phy_generic;
45         }
46
47         return true;
48 }
49
50 /**
51  *  txgbe_read_phy_if - Read TXGBE_ETHPHYIF register
52  *  @hw: pointer to hardware structure
53  *
54  *  Read TXGBE_ETHPHYIF register and save field values,
55  *  and check for valid field values.
56  **/
57 static s32 txgbe_read_phy_if(struct txgbe_hw *hw)
58 {
59         hw->phy.media_type = hw->phy.get_media_type(hw);
60
61         /* Save NW management interface connected on board. This is used
62          * to determine internal PHY mode.
63          */
64         hw->phy.nw_mng_if_sel = rd32(hw, TXGBE_ETHPHYIF);
65
66         /* If MDIO is connected to external PHY, then set PHY address. */
67         if (hw->phy.nw_mng_if_sel & TXGBE_ETHPHYIF_MDIO_ACT)
68                 hw->phy.addr = TXGBE_ETHPHYIF_MDIO_BASE(hw->phy.nw_mng_if_sel);
69
70         if (!hw->phy.phy_semaphore_mask) {
71                 if (hw->bus.lan_id)
72                         hw->phy.phy_semaphore_mask = TXGBE_MNGSEM_SWPHY;
73                 else
74                         hw->phy.phy_semaphore_mask = TXGBE_MNGSEM_SWPHY;
75         }
76
77         return 0;
78 }
79
80 /**
81  *  txgbe_identify_phy - Get physical layer module
82  *  @hw: pointer to hardware structure
83  *
84  *  Determines the physical layer module found on the current adapter.
85  **/
86 s32 txgbe_identify_phy(struct txgbe_hw *hw)
87 {
88         s32 err = TXGBE_ERR_PHY_ADDR_INVALID;
89
90         DEBUGFUNC("txgbe_identify_phy");
91
92         txgbe_read_phy_if(hw);
93
94         if (hw->phy.type != txgbe_phy_unknown)
95                 return 0;
96
97         /* Raptor 10GBASE-T requires an external PHY */
98         if (hw->phy.media_type == txgbe_media_type_copper) {
99                 err = txgbe_identify_extphy(hw);
100         } else if (hw->phy.media_type == txgbe_media_type_fiber) {
101                 err = txgbe_identify_module(hw);
102         } else {
103                 hw->phy.type = txgbe_phy_none;
104                 return 0;
105         }
106
107         /* Return error if SFP module has been detected but is not supported */
108         if (hw->phy.type == txgbe_phy_sfp_unsupported)
109                 return TXGBE_ERR_SFP_NOT_SUPPORTED;
110
111         return err;
112 }
113
114 /**
115  * txgbe_check_reset_blocked - check status of MNG FW veto bit
116  * @hw: pointer to the hardware structure
117  *
118  * This function checks the STAT.MNGVETO bit to see if there are
119  * any constraints on link from manageability.  For MAC's that don't
120  * have this bit just return faluse since the link can not be blocked
121  * via this method.
122  **/
123 s32 txgbe_check_reset_blocked(struct txgbe_hw *hw)
124 {
125         u32 mmngc;
126
127         DEBUGFUNC("txgbe_check_reset_blocked");
128
129         mmngc = rd32(hw, TXGBE_STAT);
130         if (mmngc & TXGBE_STAT_MNGVETO) {
131                 DEBUGOUT("MNG_VETO bit detected.\n");
132                 return true;
133         }
134
135         return false;
136 }
137
138 /**
139  *  txgbe_validate_phy_addr - Determines phy address is valid
140  *  @hw: pointer to hardware structure
141  *  @phy_addr: PHY address
142  *
143  **/
144 bool txgbe_validate_phy_addr(struct txgbe_hw *hw, u32 phy_addr)
145 {
146         u16 phy_id = 0;
147         bool valid = false;
148
149         DEBUGFUNC("txgbe_validate_phy_addr");
150
151         hw->phy.addr = phy_addr;
152         hw->phy.read_reg(hw, TXGBE_MD_PHY_ID_HIGH,
153                              TXGBE_MD_DEV_PMA_PMD, &phy_id);
154
155         if (phy_id != 0xFFFF && phy_id != 0x0)
156                 valid = true;
157
158         DEBUGOUT("PHY ID HIGH is 0x%04X\n", phy_id);
159
160         return valid;
161 }
162
163 /**
164  *  txgbe_get_phy_id - Get the phy type
165  *  @hw: pointer to hardware structure
166  *
167  **/
168 s32 txgbe_get_phy_id(struct txgbe_hw *hw)
169 {
170         u32 err;
171         u16 phy_id_high = 0;
172         u16 phy_id_low = 0;
173
174         DEBUGFUNC("txgbe_get_phy_id");
175
176         err = hw->phy.read_reg(hw, TXGBE_MD_PHY_ID_HIGH,
177                                       TXGBE_MD_DEV_PMA_PMD,
178                                       &phy_id_high);
179
180         if (err == 0) {
181                 hw->phy.id = (u32)(phy_id_high << 16);
182                 err = hw->phy.read_reg(hw, TXGBE_MD_PHY_ID_LOW,
183                                               TXGBE_MD_DEV_PMA_PMD,
184                                               &phy_id_low);
185                 hw->phy.id |= (u32)(phy_id_low & TXGBE_PHY_REVISION_MASK);
186                 hw->phy.revision = (u32)(phy_id_low & ~TXGBE_PHY_REVISION_MASK);
187         }
188         DEBUGOUT("PHY_ID_HIGH 0x%04X, PHY_ID_LOW 0x%04X\n",
189                   phy_id_high, phy_id_low);
190
191         return err;
192 }
193
194 /**
195  *  txgbe_get_phy_type_from_id - Get the phy type
196  *  @phy_id: PHY ID information
197  *
198  **/
199 enum txgbe_phy_type txgbe_get_phy_type_from_id(u32 phy_id)
200 {
201         enum txgbe_phy_type phy_type;
202
203         DEBUGFUNC("txgbe_get_phy_type_from_id");
204
205         switch (phy_id) {
206         case TXGBE_PHYID_TN1010:
207                 phy_type = txgbe_phy_tn;
208                 break;
209         case TXGBE_PHYID_QT2022:
210                 phy_type = txgbe_phy_qt;
211                 break;
212         case TXGBE_PHYID_ATH:
213                 phy_type = txgbe_phy_nl;
214                 break;
215         case TXGBE_PHYID_MTD3310:
216                 phy_type = txgbe_phy_cu_mtd;
217                 break;
218         default:
219                 phy_type = txgbe_phy_unknown;
220                 break;
221         }
222
223         return phy_type;
224 }
225
226 static s32
227 txgbe_reset_extphy(struct txgbe_hw *hw)
228 {
229         u16 ctrl = 0;
230         int err, i;
231
232         err = hw->phy.read_reg(hw, TXGBE_MD_PORT_CTRL,
233                         TXGBE_MD_DEV_GENERAL, &ctrl);
234         if (err != 0)
235                 return err;
236         ctrl |= TXGBE_MD_PORT_CTRL_RESET;
237         err = hw->phy.write_reg(hw, TXGBE_MD_PORT_CTRL,
238                         TXGBE_MD_DEV_GENERAL, ctrl);
239         if (err != 0)
240                 return err;
241
242         /*
243          * Poll for reset bit to self-clear indicating reset is complete.
244          * Some PHYs could take up to 3 seconds to complete and need about
245          * 1.7 usec delay after the reset is complete.
246          */
247         for (i = 0; i < 30; i++) {
248                 msec_delay(100);
249                 err = hw->phy.read_reg(hw, TXGBE_MD_PORT_CTRL,
250                         TXGBE_MD_DEV_GENERAL, &ctrl);
251                 if (err != 0)
252                         return err;
253
254                 if (!(ctrl & TXGBE_MD_PORT_CTRL_RESET)) {
255                         usec_delay(2);
256                         break;
257                 }
258         }
259
260         if (ctrl & TXGBE_MD_PORT_CTRL_RESET) {
261                 err = TXGBE_ERR_RESET_FAILED;
262                 DEBUGOUT("PHY reset polling failed to complete.\n");
263         }
264
265         return err;
266 }
267
268 /**
269  *  txgbe_reset_phy - Performs a PHY reset
270  *  @hw: pointer to hardware structure
271  **/
272 s32 txgbe_reset_phy(struct txgbe_hw *hw)
273 {
274         s32 err = 0;
275
276         DEBUGFUNC("txgbe_reset_phy");
277
278         if (hw->phy.type == txgbe_phy_unknown)
279                 err = txgbe_identify_phy(hw);
280
281         if (err != 0 || hw->phy.type == txgbe_phy_none)
282                 return err;
283
284         /* Don't reset PHY if it's shut down due to overtemp. */
285         if (hw->phy.check_overtemp(hw) == TXGBE_ERR_OVERTEMP)
286                 return err;
287
288         /* Blocked by MNG FW so bail */
289         if (txgbe_check_reset_blocked(hw))
290                 return err;
291
292         switch (hw->phy.type) {
293         case txgbe_phy_cu_mtd:
294                 err = txgbe_reset_extphy(hw);
295                 break;
296         default:
297                 break;
298         }
299
300         return err;
301 }
302
303 /**
304  *  txgbe_read_phy_mdi - Reads a value from a specified PHY register without
305  *  the SWFW lock
306  *  @hw: pointer to hardware structure
307  *  @reg_addr: 32 bit address of PHY register to read
308  *  @device_type: 5 bit device type
309  *  @phy_data: Pointer to read data from PHY register
310  **/
311 s32 txgbe_read_phy_reg_mdi(struct txgbe_hw *hw, u32 reg_addr, u32 device_type,
312                            u16 *phy_data)
313 {
314         u32 command, data;
315
316         /* Setup and write the address cycle command */
317         command = TXGBE_MDIOSCA_REG(reg_addr) |
318                   TXGBE_MDIOSCA_DEV(device_type) |
319                   TXGBE_MDIOSCA_PORT(hw->phy.addr);
320         wr32(hw, TXGBE_MDIOSCA, command);
321
322         command = TXGBE_MDIOSCD_CMD_READ |
323                   TXGBE_MDIOSCD_BUSY;
324         wr32(hw, TXGBE_MDIOSCD, command);
325
326         /*
327          * Check every 10 usec to see if the address cycle completed.
328          * The MDI Command bit will clear when the operation is
329          * complete
330          */
331         if (!po32m(hw, TXGBE_MDIOSCD, TXGBE_MDIOSCD_BUSY,
332                 0, NULL, 100, 100)) {
333                 DEBUGOUT("PHY address command did not complete\n");
334                 return TXGBE_ERR_PHY;
335         }
336
337         data = rd32(hw, TXGBE_MDIOSCD);
338         *phy_data = (u16)TXGBD_MDIOSCD_DAT(data);
339
340         return 0;
341 }
342
343 /**
344  *  txgbe_read_phy_reg - Reads a value from a specified PHY register
345  *  using the SWFW lock - this function is needed in most cases
346  *  @hw: pointer to hardware structure
347  *  @reg_addr: 32 bit address of PHY register to read
348  *  @device_type: 5 bit device type
349  *  @phy_data: Pointer to read data from PHY register
350  **/
351 s32 txgbe_read_phy_reg(struct txgbe_hw *hw, u32 reg_addr,
352                                u32 device_type, u16 *phy_data)
353 {
354         s32 err;
355         u32 gssr = hw->phy.phy_semaphore_mask;
356
357         DEBUGFUNC("txgbe_read_phy_reg");
358
359         if (hw->mac.acquire_swfw_sync(hw, gssr))
360                 return TXGBE_ERR_SWFW_SYNC;
361
362         err = hw->phy.read_reg_mdi(hw, reg_addr, device_type, phy_data);
363
364         hw->mac.release_swfw_sync(hw, gssr);
365
366         return err;
367 }
368
369 /**
370  *  txgbe_write_phy_reg_mdi - Writes a value to specified PHY register
371  *  without SWFW lock
372  *  @hw: pointer to hardware structure
373  *  @reg_addr: 32 bit PHY register to write
374  *  @device_type: 5 bit device type
375  *  @phy_data: Data to write to the PHY register
376  **/
377 s32 txgbe_write_phy_reg_mdi(struct txgbe_hw *hw, u32 reg_addr,
378                                 u32 device_type, u16 phy_data)
379 {
380         u32 command;
381
382         /* write command */
383         command = TXGBE_MDIOSCA_REG(reg_addr) |
384                   TXGBE_MDIOSCA_DEV(device_type) |
385                   TXGBE_MDIOSCA_PORT(hw->phy.addr);
386         wr32(hw, TXGBE_MDIOSCA, command);
387
388         command = TXGBE_MDIOSCD_CMD_WRITE |
389                   TXGBE_MDIOSCD_DAT(phy_data) |
390                   TXGBE_MDIOSCD_BUSY;
391         wr32(hw, TXGBE_MDIOSCD, command);
392
393         /* wait for completion */
394         if (!po32m(hw, TXGBE_MDIOSCD, TXGBE_MDIOSCD_BUSY,
395                 0, NULL, 100, 100)) {
396                 TLOG_DEBUG("PHY write cmd didn't complete\n");
397                 return -TERR_PHY;
398         }
399
400         return 0;
401 }
402
403 /**
404  *  txgbe_write_phy_reg - Writes a value to specified PHY register
405  *  using SWFW lock- this function is needed in most cases
406  *  @hw: pointer to hardware structure
407  *  @reg_addr: 32 bit PHY register to write
408  *  @device_type: 5 bit device type
409  *  @phy_data: Data to write to the PHY register
410  **/
411 s32 txgbe_write_phy_reg(struct txgbe_hw *hw, u32 reg_addr,
412                                 u32 device_type, u16 phy_data)
413 {
414         s32 err;
415         u32 gssr = hw->phy.phy_semaphore_mask;
416
417         DEBUGFUNC("txgbe_write_phy_reg");
418
419         if (hw->mac.acquire_swfw_sync(hw, gssr))
420                 err = TXGBE_ERR_SWFW_SYNC;
421
422         err = hw->phy.write_reg_mdi(hw, reg_addr, device_type,
423                                          phy_data);
424         hw->mac.release_swfw_sync(hw, gssr);
425
426         return err;
427 }
428
429 /**
430  *  txgbe_setup_phy_link - Set and restart auto-neg
431  *  @hw: pointer to hardware structure
432  *
433  *  Restart auto-negotiation and PHY and waits for completion.
434  **/
435 s32 txgbe_setup_phy_link(struct txgbe_hw *hw)
436 {
437         s32 err = 0;
438         u16 autoneg_reg = TXGBE_MII_AUTONEG_REG;
439         bool autoneg = false;
440         u32 speed;
441
442         DEBUGFUNC("txgbe_setup_phy_link");
443
444         txgbe_get_copper_link_capabilities(hw, &speed, &autoneg);
445
446         /* Set or unset auto-negotiation 10G advertisement */
447         hw->phy.read_reg(hw, TXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
448                              TXGBE_MD_DEV_AUTO_NEG,
449                              &autoneg_reg);
450
451         autoneg_reg &= ~TXGBE_MII_10GBASE_T_ADVERTISE;
452         if ((hw->phy.autoneg_advertised & TXGBE_LINK_SPEED_10GB_FULL) &&
453             (speed & TXGBE_LINK_SPEED_10GB_FULL))
454                 autoneg_reg |= TXGBE_MII_10GBASE_T_ADVERTISE;
455
456         hw->phy.write_reg(hw, TXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
457                               TXGBE_MD_DEV_AUTO_NEG,
458                               autoneg_reg);
459
460         hw->phy.read_reg(hw, TXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
461                              TXGBE_MD_DEV_AUTO_NEG,
462                              &autoneg_reg);
463
464         /* Set or unset auto-negotiation 5G advertisement */
465         autoneg_reg &= ~TXGBE_MII_5GBASE_T_ADVERTISE;
466         if ((hw->phy.autoneg_advertised & TXGBE_LINK_SPEED_5GB_FULL) &&
467             (speed & TXGBE_LINK_SPEED_5GB_FULL))
468                 autoneg_reg |= TXGBE_MII_5GBASE_T_ADVERTISE;
469
470         /* Set or unset auto-negotiation 2.5G advertisement */
471         autoneg_reg &= ~TXGBE_MII_2_5GBASE_T_ADVERTISE;
472         if ((hw->phy.autoneg_advertised &
473              TXGBE_LINK_SPEED_2_5GB_FULL) &&
474             (speed & TXGBE_LINK_SPEED_2_5GB_FULL))
475                 autoneg_reg |= TXGBE_MII_2_5GBASE_T_ADVERTISE;
476         /* Set or unset auto-negotiation 1G advertisement */
477         autoneg_reg &= ~TXGBE_MII_1GBASE_T_ADVERTISE;
478         if ((hw->phy.autoneg_advertised & TXGBE_LINK_SPEED_1GB_FULL) &&
479             (speed & TXGBE_LINK_SPEED_1GB_FULL))
480                 autoneg_reg |= TXGBE_MII_1GBASE_T_ADVERTISE;
481
482         hw->phy.write_reg(hw, TXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
483                               TXGBE_MD_DEV_AUTO_NEG,
484                               autoneg_reg);
485
486         /* Set or unset auto-negotiation 100M advertisement */
487         hw->phy.read_reg(hw, TXGBE_MII_AUTONEG_ADVERTISE_REG,
488                              TXGBE_MD_DEV_AUTO_NEG,
489                              &autoneg_reg);
490
491         autoneg_reg &= ~(TXGBE_MII_100BASE_T_ADVERTISE |
492                          TXGBE_MII_100BASE_T_ADVERTISE_HALF);
493         if ((hw->phy.autoneg_advertised & TXGBE_LINK_SPEED_100M_FULL) &&
494             (speed & TXGBE_LINK_SPEED_100M_FULL))
495                 autoneg_reg |= TXGBE_MII_100BASE_T_ADVERTISE;
496
497         hw->phy.write_reg(hw, TXGBE_MII_AUTONEG_ADVERTISE_REG,
498                               TXGBE_MD_DEV_AUTO_NEG,
499                               autoneg_reg);
500
501         /* Blocked by MNG FW so don't reset PHY */
502         if (txgbe_check_reset_blocked(hw))
503                 return err;
504
505         /* Restart PHY auto-negotiation. */
506         hw->phy.read_reg(hw, TXGBE_MD_AUTO_NEG_CONTROL,
507                              TXGBE_MD_DEV_AUTO_NEG, &autoneg_reg);
508
509         autoneg_reg |= TXGBE_MII_RESTART;
510
511         hw->phy.write_reg(hw, TXGBE_MD_AUTO_NEG_CONTROL,
512                               TXGBE_MD_DEV_AUTO_NEG, autoneg_reg);
513
514         return err;
515 }
516
517 /**
518  *  txgbe_setup_phy_link_speed - Sets the auto advertised capabilities
519  *  @hw: pointer to hardware structure
520  *  @speed: new link speed
521  *  @autoneg_wait_to_complete: unused
522  **/
523 s32 txgbe_setup_phy_link_speed(struct txgbe_hw *hw,
524                                        u32 speed,
525                                        bool autoneg_wait_to_complete)
526 {
527         UNREFERENCED_PARAMETER(autoneg_wait_to_complete);
528
529         DEBUGFUNC("txgbe_setup_phy_link_speed");
530
531         /*
532          * Clear autoneg_advertised and set new values based on input link
533          * speed.
534          */
535         hw->phy.autoneg_advertised = 0;
536
537         if (speed & TXGBE_LINK_SPEED_10GB_FULL)
538                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10GB_FULL;
539
540         if (speed & TXGBE_LINK_SPEED_5GB_FULL)
541                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_5GB_FULL;
542
543         if (speed & TXGBE_LINK_SPEED_2_5GB_FULL)
544                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_2_5GB_FULL;
545
546         if (speed & TXGBE_LINK_SPEED_1GB_FULL)
547                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_1GB_FULL;
548
549         if (speed & TXGBE_LINK_SPEED_100M_FULL)
550                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_100M_FULL;
551
552         if (speed & TXGBE_LINK_SPEED_10M_FULL)
553                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10M_FULL;
554
555         /* Setup link based on the new speed settings */
556         hw->phy.setup_link(hw);
557
558         return 0;
559 }
560
561 /**
562  * txgbe_get_copper_speeds_supported - Get copper link speeds from phy
563  * @hw: pointer to hardware structure
564  *
565  * Determines the supported link capabilities by reading the PHY auto
566  * negotiation register.
567  **/
568 static s32 txgbe_get_copper_speeds_supported(struct txgbe_hw *hw)
569 {
570         s32 err;
571         u16 speed_ability;
572
573         err = hw->phy.read_reg(hw, TXGBE_MD_PHY_SPEED_ABILITY,
574                                       TXGBE_MD_DEV_PMA_PMD,
575                                       &speed_ability);
576         if (err)
577                 return err;
578
579         if (speed_ability & TXGBE_MD_PHY_SPEED_10G)
580                 hw->phy.speeds_supported |= TXGBE_LINK_SPEED_10GB_FULL;
581         if (speed_ability & TXGBE_MD_PHY_SPEED_1G)
582                 hw->phy.speeds_supported |= TXGBE_LINK_SPEED_1GB_FULL;
583         if (speed_ability & TXGBE_MD_PHY_SPEED_100M)
584                 hw->phy.speeds_supported |= TXGBE_LINK_SPEED_100M_FULL;
585
586         return err;
587 }
588
589 /**
590  *  txgbe_get_copper_link_capabilities - Determines link capabilities
591  *  @hw: pointer to hardware structure
592  *  @speed: pointer to link speed
593  *  @autoneg: boolean auto-negotiation value
594  **/
595 s32 txgbe_get_copper_link_capabilities(struct txgbe_hw *hw,
596                                                u32 *speed,
597                                                bool *autoneg)
598 {
599         s32 err = 0;
600
601         DEBUGFUNC("txgbe_get_copper_link_capabilities");
602
603         *autoneg = true;
604         if (!hw->phy.speeds_supported)
605                 err = txgbe_get_copper_speeds_supported(hw);
606
607         *speed = hw->phy.speeds_supported;
608         return err;
609 }
610
611 /**
612  *  txgbe_check_phy_link_tnx - Determine link and speed status
613  *  @hw: pointer to hardware structure
614  *  @speed: current link speed
615  *  @link_up: true is link is up, false otherwise
616  *
617  *  Reads the VS1 register to determine if link is up and the current speed for
618  *  the PHY.
619  **/
620 s32 txgbe_check_phy_link_tnx(struct txgbe_hw *hw, u32 *speed,
621                              bool *link_up)
622 {
623         s32 err = 0;
624         u32 time_out;
625         u32 max_time_out = 10;
626         u16 phy_link = 0;
627         u16 phy_speed = 0;
628         u16 phy_data = 0;
629
630         DEBUGFUNC("txgbe_check_phy_link_tnx");
631
632         /* Initialize speed and link to default case */
633         *link_up = false;
634         *speed = TXGBE_LINK_SPEED_10GB_FULL;
635
636         /*
637          * Check current speed and link status of the PHY register.
638          * This is a vendor specific register and may have to
639          * be changed for other copper PHYs.
640          */
641         for (time_out = 0; time_out < max_time_out; time_out++) {
642                 usec_delay(10);
643                 err = hw->phy.read_reg(hw,
644                                         TXGBE_MD_VENDOR_SPECIFIC_1_STATUS,
645                                         TXGBE_MD_DEV_VENDOR_1,
646                                         &phy_data);
647                 phy_link = phy_data & TXGBE_MD_VENDOR_SPECIFIC_1_LINK_STATUS;
648                 phy_speed = phy_data &
649                                  TXGBE_MD_VENDOR_SPECIFIC_1_SPEED_STATUS;
650                 if (phy_link == TXGBE_MD_VENDOR_SPECIFIC_1_LINK_STATUS) {
651                         *link_up = true;
652                         if (phy_speed ==
653                             TXGBE_MD_VENDOR_SPECIFIC_1_SPEED_STATUS)
654                                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
655                         break;
656                 }
657         }
658
659         return err;
660 }
661
662 /**
663  *  txgbe_setup_phy_link_tnx - Set and restart auto-neg
664  *  @hw: pointer to hardware structure
665  *
666  *  Restart auto-negotiation and PHY and waits for completion.
667  **/
668 s32 txgbe_setup_phy_link_tnx(struct txgbe_hw *hw)
669 {
670         s32 err = 0;
671         u16 autoneg_reg = TXGBE_MII_AUTONEG_REG;
672         bool autoneg = false;
673         u32 speed;
674
675         DEBUGFUNC("txgbe_setup_phy_link_tnx");
676
677         txgbe_get_copper_link_capabilities(hw, &speed, &autoneg);
678
679         if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
680                 /* Set or unset auto-negotiation 10G advertisement */
681                 hw->phy.read_reg(hw, TXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
682                                      TXGBE_MD_DEV_AUTO_NEG,
683                                      &autoneg_reg);
684
685                 autoneg_reg &= ~TXGBE_MII_10GBASE_T_ADVERTISE;
686                 if (hw->phy.autoneg_advertised & TXGBE_LINK_SPEED_10GB_FULL)
687                         autoneg_reg |= TXGBE_MII_10GBASE_T_ADVERTISE;
688
689                 hw->phy.write_reg(hw, TXGBE_MII_10GBASE_T_AUTONEG_CTRL_REG,
690                                       TXGBE_MD_DEV_AUTO_NEG,
691                                       autoneg_reg);
692         }
693
694         if (speed & TXGBE_LINK_SPEED_1GB_FULL) {
695                 /* Set or unset auto-negotiation 1G advertisement */
696                 hw->phy.read_reg(hw, TXGBE_MII_AUTONEG_XNP_TX_REG,
697                                      TXGBE_MD_DEV_AUTO_NEG,
698                                      &autoneg_reg);
699
700                 autoneg_reg &= ~TXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
701                 if (hw->phy.autoneg_advertised & TXGBE_LINK_SPEED_1GB_FULL)
702                         autoneg_reg |= TXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
703
704                 hw->phy.write_reg(hw, TXGBE_MII_AUTONEG_XNP_TX_REG,
705                                       TXGBE_MD_DEV_AUTO_NEG,
706                                       autoneg_reg);
707         }
708
709         if (speed & TXGBE_LINK_SPEED_100M_FULL) {
710                 /* Set or unset auto-negotiation 100M advertisement */
711                 hw->phy.read_reg(hw, TXGBE_MII_AUTONEG_ADVERTISE_REG,
712                                      TXGBE_MD_DEV_AUTO_NEG,
713                                      &autoneg_reg);
714
715                 autoneg_reg &= ~TXGBE_MII_100BASE_T_ADVERTISE;
716                 if (hw->phy.autoneg_advertised & TXGBE_LINK_SPEED_100M_FULL)
717                         autoneg_reg |= TXGBE_MII_100BASE_T_ADVERTISE;
718
719                 hw->phy.write_reg(hw, TXGBE_MII_AUTONEG_ADVERTISE_REG,
720                                       TXGBE_MD_DEV_AUTO_NEG,
721                                       autoneg_reg);
722         }
723
724         /* Blocked by MNG FW so don't reset PHY */
725         if (txgbe_check_reset_blocked(hw))
726                 return err;
727
728         /* Restart PHY auto-negotiation. */
729         hw->phy.read_reg(hw, TXGBE_MD_AUTO_NEG_CONTROL,
730                              TXGBE_MD_DEV_AUTO_NEG, &autoneg_reg);
731
732         autoneg_reg |= TXGBE_MII_RESTART;
733
734         hw->phy.write_reg(hw, TXGBE_MD_AUTO_NEG_CONTROL,
735                               TXGBE_MD_DEV_AUTO_NEG, autoneg_reg);
736
737         return err;
738 }
739
740 /**
741  *  txgbe_identify_module - Identifies module type
742  *  @hw: pointer to hardware structure
743  *
744  *  Determines HW type and calls appropriate function.
745  **/
746 s32 txgbe_identify_module(struct txgbe_hw *hw)
747 {
748         s32 err = TXGBE_ERR_SFP_NOT_PRESENT;
749
750         DEBUGFUNC("txgbe_identify_module");
751
752         switch (hw->phy.media_type) {
753         case txgbe_media_type_fiber:
754                 err = txgbe_identify_sfp_module(hw);
755                 break;
756
757         case txgbe_media_type_fiber_qsfp:
758                 err = txgbe_identify_qsfp_module(hw);
759                 break;
760
761         default:
762                 hw->phy.sfp_type = txgbe_sfp_type_not_present;
763                 err = TXGBE_ERR_SFP_NOT_PRESENT;
764                 break;
765         }
766
767         return err;
768 }
769
770 /**
771  *  txgbe_identify_sfp_module - Identifies SFP modules
772  *  @hw: pointer to hardware structure
773  *
774  *  Searches for and identifies the SFP module and assigns appropriate PHY type.
775  **/
776 s32 txgbe_identify_sfp_module(struct txgbe_hw *hw)
777 {
778         s32 err = TXGBE_ERR_PHY_ADDR_INVALID;
779         u32 vendor_oui = 0;
780         enum txgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
781         u8 identifier = 0;
782         u8 comp_codes_1g = 0;
783         u8 comp_codes_10g = 0;
784         u8 oui_bytes[3] = {0, 0, 0};
785         u8 cable_tech = 0;
786         u8 cable_spec = 0;
787         u16 enforce_sfp = 0;
788
789         DEBUGFUNC("txgbe_identify_sfp_module");
790
791         if (hw->phy.media_type != txgbe_media_type_fiber) {
792                 hw->phy.sfp_type = txgbe_sfp_type_not_present;
793                 return TXGBE_ERR_SFP_NOT_PRESENT;
794         }
795
796         err = hw->phy.read_i2c_eeprom(hw, TXGBE_SFF_IDENTIFIER,
797                                              &identifier);
798         if (err != 0) {
799 ERR_I2C:
800                 hw->phy.sfp_type = txgbe_sfp_type_not_present;
801                 if (hw->phy.type != txgbe_phy_nl) {
802                         hw->phy.id = 0;
803                         hw->phy.type = txgbe_phy_unknown;
804                 }
805                 return TXGBE_ERR_SFP_NOT_PRESENT;
806         }
807
808         if (identifier != TXGBE_SFF_IDENTIFIER_SFP) {
809                 hw->phy.type = txgbe_phy_sfp_unsupported;
810                 return TXGBE_ERR_SFP_NOT_SUPPORTED;
811         }
812
813         err = hw->phy.read_i2c_eeprom(hw, TXGBE_SFF_1GBE_COMP_CODES,
814                                              &comp_codes_1g);
815         if (err != 0)
816                 goto ERR_I2C;
817
818         err = hw->phy.read_i2c_eeprom(hw, TXGBE_SFF_10GBE_COMP_CODES,
819                                              &comp_codes_10g);
820         if (err != 0)
821                 goto ERR_I2C;
822
823         err = hw->phy.read_i2c_eeprom(hw, TXGBE_SFF_CABLE_TECHNOLOGY,
824                                              &cable_tech);
825         if (err != 0)
826                 goto ERR_I2C;
827
828          /* ID Module
829           * =========
830           * 0   SFP_DA_CU
831           * 1   SFP_SR
832           * 2   SFP_LR
833           * 3   SFP_DA_CORE0 - chip-specific
834           * 4   SFP_DA_CORE1 - chip-specific
835           * 5   SFP_SR/LR_CORE0 - chip-specific
836           * 6   SFP_SR/LR_CORE1 - chip-specific
837           * 7   SFP_act_lmt_DA_CORE0 - chip-specific
838           * 8   SFP_act_lmt_DA_CORE1 - chip-specific
839           * 9   SFP_1g_cu_CORE0 - chip-specific
840           * 10  SFP_1g_cu_CORE1 - chip-specific
841           * 11  SFP_1g_sx_CORE0 - chip-specific
842           * 12  SFP_1g_sx_CORE1 - chip-specific
843           */
844         if (cable_tech & TXGBE_SFF_CABLE_DA_PASSIVE) {
845                 if (hw->bus.lan_id == 0)
846                         hw->phy.sfp_type = txgbe_sfp_type_da_cu_core0;
847                 else
848                         hw->phy.sfp_type = txgbe_sfp_type_da_cu_core1;
849         } else if (cable_tech & TXGBE_SFF_CABLE_DA_ACTIVE) {
850                 err = hw->phy.read_i2c_eeprom(hw,
851                         TXGBE_SFF_CABLE_SPEC_COMP, &cable_spec);
852                 if (err != 0)
853                         goto ERR_I2C;
854                 if (cable_spec & TXGBE_SFF_DA_SPEC_ACTIVE_LIMITING) {
855                         hw->phy.sfp_type = (hw->bus.lan_id == 0
856                                 ? txgbe_sfp_type_da_act_lmt_core0
857                                 : txgbe_sfp_type_da_act_lmt_core1);
858                 } else {
859                         hw->phy.sfp_type = txgbe_sfp_type_unknown;
860                 }
861         } else if (comp_codes_10g &
862                    (TXGBE_SFF_10GBASESR_CAPABLE |
863                     TXGBE_SFF_10GBASELR_CAPABLE)) {
864                 hw->phy.sfp_type = (hw->bus.lan_id == 0
865                                 ? txgbe_sfp_type_srlr_core0
866                                 : txgbe_sfp_type_srlr_core1);
867         } else if (comp_codes_1g & TXGBE_SFF_1GBASET_CAPABLE) {
868                 hw->phy.sfp_type = (hw->bus.lan_id == 0
869                                 ? txgbe_sfp_type_1g_cu_core0
870                                 : txgbe_sfp_type_1g_cu_core1);
871         } else if (comp_codes_1g & TXGBE_SFF_1GBASESX_CAPABLE) {
872                 hw->phy.sfp_type = (hw->bus.lan_id == 0
873                                 ? txgbe_sfp_type_1g_sx_core0
874                                 : txgbe_sfp_type_1g_sx_core1);
875         } else if (comp_codes_1g & TXGBE_SFF_1GBASELX_CAPABLE) {
876                 hw->phy.sfp_type = (hw->bus.lan_id == 0
877                                 ? txgbe_sfp_type_1g_lx_core0
878                                 : txgbe_sfp_type_1g_lx_core1);
879         } else {
880                 hw->phy.sfp_type = txgbe_sfp_type_unknown;
881         }
882
883         if (hw->phy.sfp_type != stored_sfp_type)
884                 hw->phy.sfp_setup_needed = true;
885
886         /* Determine if the SFP+ PHY is dual speed or not. */
887         hw->phy.multispeed_fiber = false;
888         if (((comp_codes_1g & TXGBE_SFF_1GBASESX_CAPABLE) &&
889              (comp_codes_10g & TXGBE_SFF_10GBASESR_CAPABLE)) ||
890             ((comp_codes_1g & TXGBE_SFF_1GBASELX_CAPABLE) &&
891              (comp_codes_10g & TXGBE_SFF_10GBASELR_CAPABLE)))
892                 hw->phy.multispeed_fiber = true;
893
894         /* Determine PHY vendor */
895         if (hw->phy.type != txgbe_phy_nl) {
896                 hw->phy.id = identifier;
897                 err = hw->phy.read_i2c_eeprom(hw,
898                         TXGBE_SFF_VENDOR_OUI_BYTE0, &oui_bytes[0]);
899                 if (err != 0)
900                         goto ERR_I2C;
901
902                 err = hw->phy.read_i2c_eeprom(hw,
903                         TXGBE_SFF_VENDOR_OUI_BYTE1, &oui_bytes[1]);
904                 if (err != 0)
905                         goto ERR_I2C;
906
907                 err = hw->phy.read_i2c_eeprom(hw,
908                         TXGBE_SFF_VENDOR_OUI_BYTE2, &oui_bytes[2]);
909                 if (err != 0)
910                         goto ERR_I2C;
911
912                 vendor_oui = ((u32)oui_bytes[0] << 24) |
913                              ((u32)oui_bytes[1] << 16) |
914                              ((u32)oui_bytes[2] << 8);
915                 switch (vendor_oui) {
916                 case TXGBE_SFF_VENDOR_OUI_TYCO:
917                         if (cable_tech & TXGBE_SFF_CABLE_DA_PASSIVE)
918                                 hw->phy.type = txgbe_phy_sfp_tyco_passive;
919                         break;
920                 case TXGBE_SFF_VENDOR_OUI_FTL:
921                         if (cable_tech & TXGBE_SFF_CABLE_DA_ACTIVE)
922                                 hw->phy.type = txgbe_phy_sfp_ftl_active;
923                         else
924                                 hw->phy.type = txgbe_phy_sfp_ftl;
925                         break;
926                 case TXGBE_SFF_VENDOR_OUI_AVAGO:
927                         hw->phy.type = txgbe_phy_sfp_avago;
928                         break;
929                 case TXGBE_SFF_VENDOR_OUI_INTEL:
930                         hw->phy.type = txgbe_phy_sfp_intel;
931                         break;
932                 default:
933                         if (cable_tech & TXGBE_SFF_CABLE_DA_PASSIVE)
934                                 hw->phy.type = txgbe_phy_sfp_unknown_passive;
935                         else if (cable_tech & TXGBE_SFF_CABLE_DA_ACTIVE)
936                                 hw->phy.type = txgbe_phy_sfp_unknown_active;
937                         else
938                                 hw->phy.type = txgbe_phy_sfp_unknown;
939                         break;
940                 }
941         }
942
943         /* Allow any DA cable vendor */
944         if (cable_tech & (TXGBE_SFF_CABLE_DA_PASSIVE |
945                           TXGBE_SFF_CABLE_DA_ACTIVE)) {
946                 return 0;
947         }
948
949         /* Verify supported 1G SFP modules */
950         if (comp_codes_10g == 0 &&
951             !(hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core1 ||
952               hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core0 ||
953               hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core0 ||
954               hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core1 ||
955               hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core0 ||
956               hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core1)) {
957                 hw->phy.type = txgbe_phy_sfp_unsupported;
958                 return TXGBE_ERR_SFP_NOT_SUPPORTED;
959         }
960
961         hw->mac.get_device_caps(hw, &enforce_sfp);
962         if (!(enforce_sfp & TXGBE_DEVICE_CAPS_ALLOW_ANY_SFP) &&
963             !hw->allow_unsupported_sfp &&
964             !(hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core0 ||
965               hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core1 ||
966               hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core0 ||
967               hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core1 ||
968               hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core0 ||
969               hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core1)) {
970                 DEBUGOUT("SFP+ module not supported\n");
971                 hw->phy.type = txgbe_phy_sfp_unsupported;
972                 return TXGBE_ERR_SFP_NOT_SUPPORTED;
973         }
974
975         return err;
976 }
977
978 /**
979  *  txgbe_identify_qsfp_module - Identifies QSFP modules
980  *  @hw: pointer to hardware structure
981  *
982  *  Searches for and identifies the QSFP module and assigns appropriate PHY type
983  **/
984 s32 txgbe_identify_qsfp_module(struct txgbe_hw *hw)
985 {
986         s32 err = TXGBE_ERR_PHY_ADDR_INVALID;
987         u32 vendor_oui = 0;
988         enum txgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
989         u8 identifier = 0;
990         u8 comp_codes_1g = 0;
991         u8 comp_codes_10g = 0;
992         u8 oui_bytes[3] = {0, 0, 0};
993         u16 enforce_sfp = 0;
994         u8 connector = 0;
995         u8 cable_length = 0;
996         u8 device_tech = 0;
997         bool active_cable = false;
998
999         DEBUGFUNC("txgbe_identify_qsfp_module");
1000
1001         if (hw->phy.media_type != txgbe_media_type_fiber_qsfp) {
1002                 hw->phy.sfp_type = txgbe_sfp_type_not_present;
1003                 err = TXGBE_ERR_SFP_NOT_PRESENT;
1004                 goto out;
1005         }
1006
1007         err = hw->phy.read_i2c_eeprom(hw, TXGBE_SFF_IDENTIFIER,
1008                                              &identifier);
1009 ERR_I2C:
1010         if (err != 0) {
1011                 hw->phy.sfp_type = txgbe_sfp_type_not_present;
1012                 hw->phy.id = 0;
1013                 hw->phy.type = txgbe_phy_unknown;
1014                 return TXGBE_ERR_SFP_NOT_PRESENT;
1015         }
1016         if (identifier != TXGBE_SFF_IDENTIFIER_QSFP_PLUS) {
1017                 hw->phy.type = txgbe_phy_sfp_unsupported;
1018                 err = TXGBE_ERR_SFP_NOT_SUPPORTED;
1019                 goto out;
1020         }
1021
1022         hw->phy.id = identifier;
1023
1024         err = hw->phy.read_i2c_eeprom(hw, TXGBE_SFF_QSFP_10GBE_COMP,
1025                                              &comp_codes_10g);
1026
1027         if (err != 0)
1028                 goto ERR_I2C;
1029
1030         err = hw->phy.read_i2c_eeprom(hw, TXGBE_SFF_QSFP_1GBE_COMP,
1031                                              &comp_codes_1g);
1032
1033         if (err != 0)
1034                 goto ERR_I2C;
1035
1036         if (comp_codes_10g & TXGBE_SFF_QSFP_DA_PASSIVE_CABLE) {
1037                 hw->phy.type = txgbe_phy_qsfp_unknown_passive;
1038                 if (hw->bus.lan_id == 0)
1039                         hw->phy.sfp_type = txgbe_sfp_type_da_cu_core0;
1040                 else
1041                         hw->phy.sfp_type = txgbe_sfp_type_da_cu_core1;
1042         } else if (comp_codes_10g & (TXGBE_SFF_10GBASESR_CAPABLE |
1043                                      TXGBE_SFF_10GBASELR_CAPABLE)) {
1044                 if (hw->bus.lan_id == 0)
1045                         hw->phy.sfp_type = txgbe_sfp_type_srlr_core0;
1046                 else
1047                         hw->phy.sfp_type = txgbe_sfp_type_srlr_core1;
1048         } else {
1049                 if (comp_codes_10g & TXGBE_SFF_QSFP_DA_ACTIVE_CABLE)
1050                         active_cable = true;
1051
1052                 if (!active_cable) {
1053                         hw->phy.read_i2c_eeprom(hw,
1054                                         TXGBE_SFF_QSFP_CONNECTOR,
1055                                         &connector);
1056
1057                         hw->phy.read_i2c_eeprom(hw,
1058                                         TXGBE_SFF_QSFP_CABLE_LENGTH,
1059                                         &cable_length);
1060
1061                         hw->phy.read_i2c_eeprom(hw,
1062                                         TXGBE_SFF_QSFP_DEVICE_TECH,
1063                                         &device_tech);
1064
1065                         if (connector ==
1066                                      TXGBE_SFF_QSFP_CONNECTOR_NOT_SEPARABLE &&
1067                             cable_length > 0 &&
1068                             ((device_tech >> 4) ==
1069                                      TXGBE_SFF_QSFP_TRANSMITTER_850NM_VCSEL))
1070                                 active_cable = true;
1071                 }
1072
1073                 if (active_cable) {
1074                         hw->phy.type = txgbe_phy_qsfp_unknown_active;
1075                         if (hw->bus.lan_id == 0)
1076                                 hw->phy.sfp_type =
1077                                         txgbe_sfp_type_da_act_lmt_core0;
1078                         else
1079                                 hw->phy.sfp_type =
1080                                         txgbe_sfp_type_da_act_lmt_core1;
1081                 } else {
1082                         /* unsupported module type */
1083                         hw->phy.type = txgbe_phy_sfp_unsupported;
1084                         err = TXGBE_ERR_SFP_NOT_SUPPORTED;
1085                         goto out;
1086                 }
1087         }
1088
1089         if (hw->phy.sfp_type != stored_sfp_type)
1090                 hw->phy.sfp_setup_needed = true;
1091
1092         /* Determine if the QSFP+ PHY is dual speed or not. */
1093         hw->phy.multispeed_fiber = false;
1094         if (((comp_codes_1g & TXGBE_SFF_1GBASESX_CAPABLE) &&
1095            (comp_codes_10g & TXGBE_SFF_10GBASESR_CAPABLE)) ||
1096            ((comp_codes_1g & TXGBE_SFF_1GBASELX_CAPABLE) &&
1097            (comp_codes_10g & TXGBE_SFF_10GBASELR_CAPABLE)))
1098                 hw->phy.multispeed_fiber = true;
1099
1100         /* Determine PHY vendor for optical modules */
1101         if (comp_codes_10g & (TXGBE_SFF_10GBASESR_CAPABLE |
1102                               TXGBE_SFF_10GBASELR_CAPABLE))  {
1103                 err = hw->phy.read_i2c_eeprom(hw,
1104                                             TXGBE_SFF_QSFP_VENDOR_OUI_BYTE0,
1105                                             &oui_bytes[0]);
1106
1107                 if (err != 0)
1108                         goto ERR_I2C;
1109
1110                 err = hw->phy.read_i2c_eeprom(hw,
1111                                             TXGBE_SFF_QSFP_VENDOR_OUI_BYTE1,
1112                                             &oui_bytes[1]);
1113
1114                 if (err != 0)
1115                         goto ERR_I2C;
1116
1117                 err = hw->phy.read_i2c_eeprom(hw,
1118                                             TXGBE_SFF_QSFP_VENDOR_OUI_BYTE2,
1119                                             &oui_bytes[2]);
1120
1121                 if (err != 0)
1122                         goto ERR_I2C;
1123
1124                 vendor_oui =
1125                   ((oui_bytes[0] << 24) |
1126                    (oui_bytes[1] << 16) |
1127                    (oui_bytes[2] << 8));
1128
1129                 if (vendor_oui == TXGBE_SFF_VENDOR_OUI_INTEL)
1130                         hw->phy.type = txgbe_phy_qsfp_intel;
1131                 else
1132                         hw->phy.type = txgbe_phy_qsfp_unknown;
1133
1134                 hw->mac.get_device_caps(hw, &enforce_sfp);
1135                 if (!(enforce_sfp & TXGBE_DEVICE_CAPS_ALLOW_ANY_SFP)) {
1136                         /* Make sure we're a supported PHY type */
1137                         if (hw->phy.type == txgbe_phy_qsfp_intel) {
1138                                 err = 0;
1139                         } else {
1140                                 if (hw->allow_unsupported_sfp) {
1141                                         DEBUGOUT("WARNING: Wangxun (R) Network Connections are quality tested using Wangxun (R) Ethernet Optics. "
1142                                                 "Using untested modules is not supported and may cause unstable operation or damage to the module or the adapter. "
1143                                                 "Wangxun Corporation is not responsible for any harm caused by using untested modules.\n");
1144                                         err = 0;
1145                                 } else {
1146                                         DEBUGOUT("QSFP module not supported\n");
1147                                         hw->phy.type =
1148                                                 txgbe_phy_sfp_unsupported;
1149                                         err = TXGBE_ERR_SFP_NOT_SUPPORTED;
1150                                 }
1151                         }
1152                 } else {
1153                         err = 0;
1154                 }
1155         }
1156
1157 out:
1158         return err;
1159 }
1160
1161 /**
1162  *  txgbe_read_i2c_eeprom - Reads 8 bit EEPROM word over I2C interface
1163  *  @hw: pointer to hardware structure
1164  *  @byte_offset: EEPROM byte offset to read
1165  *  @eeprom_data: value read
1166  *
1167  *  Performs byte read operation to SFP module's EEPROM over I2C interface.
1168  **/
1169 s32 txgbe_read_i2c_eeprom(struct txgbe_hw *hw, u8 byte_offset,
1170                                   u8 *eeprom_data)
1171 {
1172         DEBUGFUNC("txgbe_read_i2c_eeprom");
1173
1174         return hw->phy.read_i2c_byte(hw, byte_offset,
1175                                          TXGBE_I2C_EEPROM_DEV_ADDR,
1176                                          eeprom_data);
1177 }
1178
1179 /**
1180  *  txgbe_write_i2c_eeprom - Writes 8 bit EEPROM word over I2C interface
1181  *  @hw: pointer to hardware structure
1182  *  @byte_offset: EEPROM byte offset to write
1183  *  @eeprom_data: value to write
1184  *
1185  *  Performs byte write operation to SFP module's EEPROM over I2C interface.
1186  **/
1187 s32 txgbe_write_i2c_eeprom(struct txgbe_hw *hw, u8 byte_offset,
1188                                    u8 eeprom_data)
1189 {
1190         DEBUGFUNC("txgbe_write_i2c_eeprom");
1191
1192         return hw->phy.write_i2c_byte(hw, byte_offset,
1193                                           TXGBE_I2C_EEPROM_DEV_ADDR,
1194                                           eeprom_data);
1195 }
1196
1197 /**
1198  *  txgbe_read_i2c_byte_unlocked - Reads 8 bit word over I2C
1199  *  @hw: pointer to hardware structure
1200  *  @byte_offset: byte offset to read
1201  *  @dev_addr: address to read from
1202  *  @data: value read
1203  *
1204  *  Performs byte read operation to SFP module's EEPROM over I2C interface at
1205  *  a specified device address.
1206  **/
1207 s32 txgbe_read_i2c_byte_unlocked(struct txgbe_hw *hw, u8 byte_offset,
1208                                            u8 dev_addr, u8 *data)
1209 {
1210         UNREFERENCED_PARAMETER(dev_addr);
1211
1212         DEBUGFUNC("txgbe_read_i2c_byte");
1213
1214         txgbe_i2c_start(hw);
1215
1216         /* wait tx empty */
1217         if (!po32m(hw, TXGBE_I2CICR, TXGBE_I2CICR_TXEMPTY,
1218                 TXGBE_I2CICR_TXEMPTY, NULL, 100, 100)) {
1219                 return -TERR_TIMEOUT;
1220         }
1221
1222         /* read data */
1223         wr32(hw, TXGBE_I2CDATA,
1224                         byte_offset | TXGBE_I2CDATA_STOP);
1225         wr32(hw, TXGBE_I2CDATA, TXGBE_I2CDATA_READ);
1226
1227         /* wait for read complete */
1228         if (!po32m(hw, TXGBE_I2CICR, TXGBE_I2CICR_RXFULL,
1229                 TXGBE_I2CICR_RXFULL, NULL, 100, 100)) {
1230                 return -TERR_TIMEOUT;
1231         }
1232
1233         txgbe_i2c_stop(hw);
1234
1235         *data = 0xFF & rd32(hw, TXGBE_I2CDATA);
1236
1237         return 0;
1238 }
1239
1240 /**
1241  *  txgbe_read_i2c_byte - Reads 8 bit word over I2C
1242  *  @hw: pointer to hardware structure
1243  *  @byte_offset: byte offset to read
1244  *  @dev_addr: address to read from
1245  *  @data: value read
1246  *
1247  *  Performs byte read operation to SFP module's EEPROM over I2C interface at
1248  *  a specified device address.
1249  **/
1250 s32 txgbe_read_i2c_byte(struct txgbe_hw *hw, u8 byte_offset,
1251                                 u8 dev_addr, u8 *data)
1252 {
1253         u32 swfw_mask = hw->phy.phy_semaphore_mask;
1254         int err = 0;
1255
1256         if (hw->mac.acquire_swfw_sync(hw, swfw_mask))
1257                 return TXGBE_ERR_SWFW_SYNC;
1258         err = txgbe_read_i2c_byte_unlocked(hw, byte_offset, dev_addr, data);
1259         hw->mac.release_swfw_sync(hw, swfw_mask);
1260         return err;
1261 }
1262
1263 /**
1264  *  txgbe_write_i2c_byte_unlocked - Writes 8 bit word over I2C
1265  *  @hw: pointer to hardware structure
1266  *  @byte_offset: byte offset to write
1267  *  @dev_addr: address to write to
1268  *  @data: value to write
1269  *
1270  *  Performs byte write operation to SFP module's EEPROM over I2C interface at
1271  *  a specified device address.
1272  **/
1273 s32 txgbe_write_i2c_byte_unlocked(struct txgbe_hw *hw, u8 byte_offset,
1274                                             u8 dev_addr, u8 data)
1275 {
1276         UNREFERENCED_PARAMETER(dev_addr);
1277
1278         DEBUGFUNC("txgbe_write_i2c_byte");
1279
1280         txgbe_i2c_start(hw);
1281
1282         /* wait tx empty */
1283         if (!po32m(hw, TXGBE_I2CICR, TXGBE_I2CICR_TXEMPTY,
1284                 TXGBE_I2CICR_TXEMPTY, NULL, 100, 100)) {
1285                 return -TERR_TIMEOUT;
1286         }
1287
1288         wr32(hw, TXGBE_I2CDATA, byte_offset | TXGBE_I2CDATA_STOP);
1289         wr32(hw, TXGBE_I2CDATA, data | TXGBE_I2CDATA_WRITE);
1290
1291         /* wait for write complete */
1292         if (!po32m(hw, TXGBE_I2CICR, TXGBE_I2CICR_RXFULL,
1293                 TXGBE_I2CICR_RXFULL, NULL, 100, 100)) {
1294                 return -TERR_TIMEOUT;
1295         }
1296         txgbe_i2c_stop(hw);
1297
1298         return 0;
1299 }
1300
1301 /**
1302  *  txgbe_write_i2c_byte - Writes 8 bit word over I2C
1303  *  @hw: pointer to hardware structure
1304  *  @byte_offset: byte offset to write
1305  *  @dev_addr: address to write to
1306  *  @data: value to write
1307  *
1308  *  Performs byte write operation to SFP module's EEPROM over I2C interface at
1309  *  a specified device address.
1310  **/
1311 s32 txgbe_write_i2c_byte(struct txgbe_hw *hw, u8 byte_offset,
1312                                  u8 dev_addr, u8 data)
1313 {
1314         u32 swfw_mask = hw->phy.phy_semaphore_mask;
1315         int err = 0;
1316
1317         if (hw->mac.acquire_swfw_sync(hw, swfw_mask))
1318                 return TXGBE_ERR_SWFW_SYNC;
1319         err = txgbe_write_i2c_byte_unlocked(hw, byte_offset, dev_addr, data);
1320         hw->mac.release_swfw_sync(hw, swfw_mask);
1321
1322         return err;
1323 }
1324
1325 /**
1326  *  txgbe_i2c_start - Sets I2C start condition
1327  *  @hw: pointer to hardware structure
1328  *
1329  *  Sets I2C start condition (High -> Low on SDA while SCL is High)
1330  **/
1331 static void txgbe_i2c_start(struct txgbe_hw *hw)
1332 {
1333         DEBUGFUNC("txgbe_i2c_start");
1334
1335         wr32(hw, TXGBE_I2CENA, 0);
1336
1337         wr32(hw, TXGBE_I2CCON,
1338                 (TXGBE_I2CCON_MENA |
1339                 TXGBE_I2CCON_SPEED(1) |
1340                 TXGBE_I2CCON_RESTART |
1341                 TXGBE_I2CCON_SDIA));
1342         wr32(hw, TXGBE_I2CTAR, TXGBE_I2C_SLAVEADDR);
1343         wr32(hw, TXGBE_I2CSSSCLHCNT, 600);
1344         wr32(hw, TXGBE_I2CSSSCLLCNT, 600);
1345         wr32(hw, TXGBE_I2CRXTL, 0); /* 1byte for rx full signal */
1346         wr32(hw, TXGBE_I2CTXTL, 4);
1347         wr32(hw, TXGBE_I2CSCLTMOUT, 0xFFFFFF);
1348         wr32(hw, TXGBE_I2CSDATMOUT, 0xFFFFFF);
1349
1350         wr32(hw, TXGBE_I2CICM, 0);
1351         wr32(hw, TXGBE_I2CENA, 1);
1352 }
1353
1354 /**
1355  *  txgbe_i2c_stop - Sets I2C stop condition
1356  *  @hw: pointer to hardware structure
1357  *
1358  *  Sets I2C stop condition (Low -> High on SDA while SCL is High)
1359  **/
1360 static void txgbe_i2c_stop(struct txgbe_hw *hw)
1361 {
1362         DEBUGFUNC("txgbe_i2c_stop");
1363
1364         /* wait for completion */
1365         if (!po32m(hw, TXGBE_I2CSTAT, TXGBE_I2CSTAT_MST,
1366                 0, NULL, 100, 100)) {
1367                 DEBUGFUNC("i2c stop timeout.");
1368         }
1369
1370         wr32(hw, TXGBE_I2CENA, 0);
1371 }
1372