update Intel copyright years to 2014
[dpdk.git] / lib / librte_pmd_ixgbe / ixgbe_82599_bypass.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "ixgbe/ixgbe_type.h"
35 #include "ixgbe/ixgbe_82599.h"
36 #include "ixgbe/ixgbe_api.h"
37 #include "ixgbe/ixgbe_common.h"
38 #include "ixgbe/ixgbe_phy.h"
39 #include "ixgbe_bypass_defines.h"
40 #include "ixgbe_bypass.h"
41
42 /**
43  *  ixgbe_set_fiber_fixed_speed - Set module link speed for fixed fiber
44  *  @hw: pointer to hardware structure
45  *  @speed: link speed to set
46  *
47  *  We set the module speed differently for fixed fiber.  For other
48  *  multi-speed devices we don't have an error value so here if we
49  *  detect an error we just log it and exit.
50  */
51 static void
52 ixgbe_set_fiber_fixed_speed(struct ixgbe_hw *hw, ixgbe_link_speed speed)
53 {
54         s32 status;
55         u8 rs, eeprom_data;
56
57         switch (speed) {
58         case IXGBE_LINK_SPEED_10GB_FULL:
59                 /* one bit mask same as setting on */
60                 rs = IXGBE_SFF_SOFT_RS_SELECT_10G;
61                 break;
62         case IXGBE_LINK_SPEED_1GB_FULL:
63                 rs = IXGBE_SFF_SOFT_RS_SELECT_1G;
64                 break;
65         default:
66                 DEBUGOUT("Invalid fixed module speed\n");
67                 return;
68         }
69
70         /* Set RS0 */
71         status = hw->phy.ops.read_i2c_byte(hw, IXGBE_SFF_SFF_8472_OSCB,
72                                            IXGBE_I2C_EEPROM_DEV_ADDR2,
73                                            &eeprom_data);
74         if (status) {
75                 DEBUGOUT("Failed to read Rx Rate Select RS0\n");
76                 goto out;
77         }
78
79         eeprom_data = (eeprom_data & ~IXGBE_SFF_SOFT_RS_SELECT_MASK) & rs;
80
81         status = hw->phy.ops.write_i2c_byte(hw, IXGBE_SFF_SFF_8472_OSCB,
82                                             IXGBE_I2C_EEPROM_DEV_ADDR2,
83                                             eeprom_data);
84         if (status) {
85                 DEBUGOUT("Failed to write Rx Rate Select RS0\n");
86                 goto out;
87         }
88
89         /* Set RS1 */
90         status = hw->phy.ops.read_i2c_byte(hw, IXGBE_SFF_SFF_8472_ESCB,
91                                            IXGBE_I2C_EEPROM_DEV_ADDR2,
92                                            &eeprom_data);
93         if (status) {
94                 DEBUGOUT("Failed to read Rx Rate Select RS1\n");
95                 goto out;
96         }
97
98         eeprom_data = (eeprom_data & ~IXGBE_SFF_SOFT_RS_SELECT_MASK) & rs;
99
100         status = hw->phy.ops.write_i2c_byte(hw, IXGBE_SFF_SFF_8472_ESCB,
101                                             IXGBE_I2C_EEPROM_DEV_ADDR2,
102                                             eeprom_data);
103         if (status) {
104                 DEBUGOUT("Failed to write Rx Rate Select RS1\n");
105                 goto out;
106         }
107 out:
108         return;
109 }
110
111 /**
112  *  ixgbe_setup_mac_link_multispeed_fixed_fiber - Set MAC link speed
113  *  @hw: pointer to hardware structure
114  *  @speed: new link speed
115  *  @autoneg: true if autonegotiation enabled
116  *  @autoneg_wait_to_complete: true when waiting for completion is needed
117  *
118  *  Set the link speed in the AUTOC register and restarts link.
119  **/
120 static s32
121 ixgbe_setup_mac_link_multispeed_fixed_fiber(struct ixgbe_hw *hw,
122                                      ixgbe_link_speed speed, bool autoneg,
123                                      bool autoneg_wait_to_complete)
124 {
125         s32 status = IXGBE_SUCCESS;
126         ixgbe_link_speed link_speed = IXGBE_LINK_SPEED_UNKNOWN;
127         ixgbe_link_speed highest_link_speed = IXGBE_LINK_SPEED_UNKNOWN;
128         u32 speedcnt = 0;
129         u32 esdp_reg = IXGBE_READ_REG(hw, IXGBE_ESDP);
130         u32 i = 0;
131         bool link_up = false;
132         bool negotiation;
133
134         DEBUGFUNC("");
135
136         /* Mask off requested but non-supported speeds */
137         status = ixgbe_get_link_capabilities(hw, &link_speed, &negotiation);
138         if (status != IXGBE_SUCCESS)
139                 return status;
140
141         speed &= link_speed;
142
143         /*
144          * Try each speed one by one, highest priority first.  We do this in
145          * software because 10gb fiber doesn't support speed autonegotiation.
146          */
147         if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
148                 speedcnt++;
149                 highest_link_speed = IXGBE_LINK_SPEED_10GB_FULL;
150
151                 /* If we already have link at this speed, just jump out */
152                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
153                 if (status != IXGBE_SUCCESS)
154                         return status;
155
156                 if ((link_speed == IXGBE_LINK_SPEED_10GB_FULL) && link_up)
157                         goto out;
158                 /* Set the module link speed */
159                 ixgbe_set_fiber_fixed_speed(hw, IXGBE_LINK_SPEED_10GB_FULL);
160                                             
161                 /* Set the module link speed */
162                 esdp_reg |= (IXGBE_ESDP_SDP5_DIR | IXGBE_ESDP_SDP5);
163                 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp_reg);
164                 IXGBE_WRITE_FLUSH(hw);
165
166                 /* Allow module to change analog characteristics (1G->10G) */
167                 msec_delay(40);
168
169                 status = ixgbe_setup_mac_link_82599(hw,
170                                                     IXGBE_LINK_SPEED_10GB_FULL,
171                                                     autoneg,
172                                                     autoneg_wait_to_complete);
173                 if (status != IXGBE_SUCCESS)
174                         return status;
175
176                 /* Flap the tx laser if it has not already been done */
177                 ixgbe_flap_tx_laser(hw);
178
179                 /*
180                  * Wait for the controller to acquire link.  Per IEEE 802.3ap,
181                  * Section 73.10.2, we may have to wait up to 500ms if KR is
182                  * attempted.  82599 uses the same timing for 10g SFI.
183                  */
184                 for (i = 0; i < 5; i++) {
185                         /* Wait for the link partner to also set speed */
186                         msec_delay(100);
187
188                         /* If we have link, just jump out */
189                         status = ixgbe_check_link(hw, &link_speed,
190                                                   &link_up, false);
191                         if (status != IXGBE_SUCCESS)
192                                 return status;
193
194                         if (link_up)
195                                 goto out;
196                 }
197         }
198
199         if (speed & IXGBE_LINK_SPEED_1GB_FULL) {
200                 speedcnt++;
201                 if (highest_link_speed == IXGBE_LINK_SPEED_UNKNOWN)
202                         highest_link_speed = IXGBE_LINK_SPEED_1GB_FULL;
203
204                 /* If we already have link at this speed, just jump out */
205                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
206                 if (status != IXGBE_SUCCESS)
207                         return status;
208
209                 if ((link_speed == IXGBE_LINK_SPEED_1GB_FULL) && link_up)
210                         goto out;
211
212                 /* Set the module link speed */
213                 ixgbe_set_fiber_fixed_speed(hw, IXGBE_LINK_SPEED_1GB_FULL);
214                                                     
215                 /* Allow module to change analog characteristics (10G->1G) */
216                 msec_delay(40);
217
218                 status = ixgbe_setup_mac_link_82599(hw,
219                                                     IXGBE_LINK_SPEED_1GB_FULL,
220                                                     autoneg,
221                                                     autoneg_wait_to_complete);
222                 if (status != IXGBE_SUCCESS)
223                         return status;
224
225                 /* Flap the tx laser if it has not already been done */
226                 ixgbe_flap_tx_laser(hw);
227
228                 /* Wait for the link partner to also set speed */
229                 msec_delay(100);
230
231                 /* If we have link, just jump out */
232                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
233                 if (status != IXGBE_SUCCESS)
234                         return status;
235
236                 if (link_up)
237                         goto out;
238         }
239
240         /*
241          * We didn't get link.  Configure back to the highest speed we tried,
242          * (if there was more than one).  We call ourselves back with just the
243          * single highest speed that the user requested.
244          */
245         if (speedcnt > 1)
246                 status = ixgbe_setup_mac_link_multispeed_fixed_fiber(hw,
247                         highest_link_speed, autoneg, autoneg_wait_to_complete);
248
249 out:
250         /* Set autoneg_advertised value based on input link speed */
251         hw->phy.autoneg_advertised = 0;
252
253         if (speed & IXGBE_LINK_SPEED_10GB_FULL)
254                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
255
256         if (speed & IXGBE_LINK_SPEED_1GB_FULL)
257                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
258
259         return status;
260 }
261
262 /*
263  * Wrapper around ND functions to support BYPASS nic.
264  */
265 s32
266 ixgbe_bypass_init_shared_code(struct ixgbe_hw *hw)
267 {
268         if (hw->device_id == IXGBE_DEV_ID_82599_BYPASS) {
269                 hw->mac.type = ixgbe_mac_82599EB;
270         }
271
272         return (ixgbe_init_shared_code(hw));
273 }
274
275 static enum ixgbe_media_type
276 ixgbe_bypass_get_media_type(struct ixgbe_hw *hw)
277 {
278         enum ixgbe_media_type media_type;
279
280         DEBUGFUNC("");
281
282         if (hw->device_id == IXGBE_DEV_ID_82599_BYPASS) {
283                 media_type = ixgbe_media_type_fiber;
284         } else {
285                 media_type = ixgbe_get_media_type_82599(hw);
286         }
287         return (media_type);
288 }
289
290 s32
291 ixgbe_bypass_init_hw(struct ixgbe_hw *hw)
292 {
293         int rc;
294         
295         if ((rc  = ixgbe_init_hw(hw)) == 0 &&
296                         hw->device_id == IXGBE_DEV_ID_82599_BYPASS) {
297
298                 hw->mac.ops.setup_link =
299                         &ixgbe_setup_mac_link_multispeed_fixed_fiber;
300
301                 hw->mac.ops.get_media_type = &ixgbe_bypass_get_media_type;
302
303                 hw->mac.ops.disable_tx_laser = NULL;
304                 hw->mac.ops.enable_tx_laser = NULL;
305                 hw->mac.ops.flap_tx_laser = NULL;
306         }
307
308         return (rc);
309 }