adb75dc58dd62367363bd0373becac89dfd5ce0e
[dpdk.git] / lib / librte_pmd_ixgbe / ixgbe / ixgbe_82599.c
1 /*******************************************************************************
2
3 Copyright (c) 2001-2012, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31
32 ***************************************************************************/
33
34 #include "ixgbe_type.h"
35 #include "ixgbe_82599.h"
36 #include "ixgbe_api.h"
37 #include "ixgbe_common.h"
38 #include "ixgbe_phy.h"
39 #ident "$Id: ixgbe_82599.c,v 1.301 2012/11/08 11:33:27 jtkirshe Exp $"
40
41 STATIC s32 ixgbe_setup_copper_link_82599(struct ixgbe_hw *hw,
42                                          ixgbe_link_speed speed,
43                                          bool autoneg,
44                                          bool autoneg_wait_to_complete);
45 STATIC s32 ixgbe_verify_fw_version_82599(struct ixgbe_hw *hw);
46 STATIC s32 ixgbe_read_eeprom_82599(struct ixgbe_hw *hw,
47                                    u16 offset, u16 *data);
48 STATIC s32 ixgbe_read_eeprom_buffer_82599(struct ixgbe_hw *hw, u16 offset,
49                                           u16 words, u16 *data);
50
51 void ixgbe_init_mac_link_ops_82599(struct ixgbe_hw *hw)
52 {
53         struct ixgbe_mac_info *mac = &hw->mac;
54
55         DEBUGFUNC("ixgbe_init_mac_link_ops_82599");
56
57         /* enable the laser control functions for SFP+ fiber */
58         if (mac->ops.get_media_type(hw) == ixgbe_media_type_fiber) {
59                 mac->ops.disable_tx_laser =
60                                        &ixgbe_disable_tx_laser_multispeed_fiber;
61                 mac->ops.enable_tx_laser =
62                                         &ixgbe_enable_tx_laser_multispeed_fiber;
63                 mac->ops.flap_tx_laser = &ixgbe_flap_tx_laser_multispeed_fiber;
64
65         } else {
66                 mac->ops.disable_tx_laser = NULL;
67                 mac->ops.enable_tx_laser = NULL;
68                 mac->ops.flap_tx_laser = NULL;
69         }
70
71         if (hw->phy.multispeed_fiber) {
72                 /* Set up dual speed SFP+ support */
73                 mac->ops.setup_link = &ixgbe_setup_mac_link_multispeed_fiber;
74         } else {
75                 if ((ixgbe_get_media_type(hw) == ixgbe_media_type_backplane) &&
76                      (hw->phy.smart_speed == ixgbe_smart_speed_auto ||
77                       hw->phy.smart_speed == ixgbe_smart_speed_on) &&
78                       !ixgbe_verify_lesm_fw_enabled_82599(hw)) {
79                         mac->ops.setup_link = &ixgbe_setup_mac_link_smartspeed;
80                 } else {
81                         mac->ops.setup_link = &ixgbe_setup_mac_link_82599;
82                 }
83         }
84 }
85
86 /**
87  *  ixgbe_init_phy_ops_82599 - PHY/SFP specific init
88  *  @hw: pointer to hardware structure
89  *
90  *  Initialize any function pointers that were not able to be
91  *  set during init_shared_code because the PHY/SFP type was
92  *  not known.  Perform the SFP init if necessary.
93  *
94  **/
95 s32 ixgbe_init_phy_ops_82599(struct ixgbe_hw *hw)
96 {
97         struct ixgbe_mac_info *mac = &hw->mac;
98         struct ixgbe_phy_info *phy = &hw->phy;
99         s32 ret_val = IXGBE_SUCCESS;
100
101         DEBUGFUNC("ixgbe_init_phy_ops_82599");
102
103         /* Identify the PHY or SFP module */
104         ret_val = phy->ops.identify(hw);
105         if (ret_val == IXGBE_ERR_SFP_NOT_SUPPORTED)
106                 goto init_phy_ops_out;
107
108         /* Setup function pointers based on detected SFP module and speeds */
109         ixgbe_init_mac_link_ops_82599(hw);
110         if (hw->phy.sfp_type != ixgbe_sfp_type_unknown)
111                 hw->phy.ops.reset = NULL;
112
113         /* If copper media, overwrite with copper function pointers */
114         if (mac->ops.get_media_type(hw) == ixgbe_media_type_copper) {
115                 mac->ops.setup_link = &ixgbe_setup_copper_link_82599;
116                 mac->ops.get_link_capabilities =
117                                   &ixgbe_get_copper_link_capabilities_generic;
118         }
119
120         /* Set necessary function pointers based on phy type */
121         switch (hw->phy.type) {
122         case ixgbe_phy_tn:
123                 phy->ops.setup_link = &ixgbe_setup_phy_link_tnx;
124                 phy->ops.check_link = &ixgbe_check_phy_link_tnx;
125                 phy->ops.get_firmware_version =
126                              &ixgbe_get_phy_firmware_version_tnx;
127                 break;
128         default:
129                 break;
130         }
131 init_phy_ops_out:
132         return ret_val;
133 }
134
135 s32 ixgbe_setup_sfp_modules_82599(struct ixgbe_hw *hw)
136 {
137         s32 ret_val = IXGBE_SUCCESS;
138         u16 list_offset, data_offset, data_value;
139         bool got_lock = false;
140
141         DEBUGFUNC("ixgbe_setup_sfp_modules_82599");
142
143         if (hw->phy.sfp_type != ixgbe_sfp_type_unknown) {
144                 ixgbe_init_mac_link_ops_82599(hw);
145
146                 hw->phy.ops.reset = NULL;
147
148                 ret_val = ixgbe_get_sfp_init_sequence_offsets(hw, &list_offset,
149                                                               &data_offset);
150                 if (ret_val != IXGBE_SUCCESS)
151                         goto setup_sfp_out;
152
153                 /* PHY config will finish before releasing the semaphore */
154                 ret_val = hw->mac.ops.acquire_swfw_sync(hw,
155                                                         IXGBE_GSSR_MAC_CSR_SM);
156                 if (ret_val != IXGBE_SUCCESS) {
157                         ret_val = IXGBE_ERR_SWFW_SYNC;
158                         goto setup_sfp_out;
159                 }
160
161                 hw->eeprom.ops.read(hw, ++data_offset, &data_value);
162                 while (data_value != 0xffff) {
163                         IXGBE_WRITE_REG(hw, IXGBE_CORECTL, data_value);
164                         IXGBE_WRITE_FLUSH(hw);
165                         hw->eeprom.ops.read(hw, ++data_offset, &data_value);
166                 }
167
168                 /* Release the semaphore */
169                 hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_MAC_CSR_SM);
170                 /* Delay obtaining semaphore again to allow FW access */
171                 msec_delay(hw->eeprom.semaphore_delay);
172
173                 /* Need SW/FW semaphore around AUTOC writes if LESM on,
174                  * likewise reset_pipeline requires lock as it also writes
175                  * AUTOC.
176                  */
177                 if (ixgbe_verify_lesm_fw_enabled_82599(hw)) {
178                         ret_val = hw->mac.ops.acquire_swfw_sync(hw,
179                                                         IXGBE_GSSR_MAC_CSR_SM);
180                         if (ret_val != IXGBE_SUCCESS) {
181                                 ret_val = IXGBE_ERR_SWFW_SYNC;
182                                 goto setup_sfp_out;
183                         }
184
185                         got_lock = true;
186                 }
187
188                 /* Restart DSP and set SFI mode */
189                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC, (IXGBE_READ_REG(hw,
190                                 IXGBE_AUTOC) | IXGBE_AUTOC_LMS_10G_SERIAL));
191
192                 ret_val = ixgbe_reset_pipeline_82599(hw);
193
194                 if (got_lock) {
195                         hw->mac.ops.release_swfw_sync(hw,
196                                                       IXGBE_GSSR_MAC_CSR_SM);
197                         got_lock = false;
198                 }
199
200                 if (ret_val) {
201                         DEBUGOUT("sfp module setup not complete\n");
202                         ret_val = IXGBE_ERR_SFP_SETUP_NOT_COMPLETE;
203                         goto setup_sfp_out;
204                 }
205
206         }
207
208 setup_sfp_out:
209         return ret_val;
210 }
211
212 /**
213  *  ixgbe_init_ops_82599 - Inits func ptrs and MAC type
214  *  @hw: pointer to hardware structure
215  *
216  *  Initialize the function pointers and assign the MAC type for 82599.
217  *  Does not touch the hardware.
218  **/
219
220 s32 ixgbe_init_ops_82599(struct ixgbe_hw *hw)
221 {
222         struct ixgbe_mac_info *mac = &hw->mac;
223         struct ixgbe_phy_info *phy = &hw->phy;
224         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
225         s32 ret_val;
226
227         DEBUGFUNC("ixgbe_init_ops_82599");
228
229         ret_val = ixgbe_init_phy_ops_generic(hw);
230         ret_val = ixgbe_init_ops_generic(hw);
231
232         /* PHY */
233         phy->ops.identify = &ixgbe_identify_phy_82599;
234         phy->ops.init = &ixgbe_init_phy_ops_82599;
235
236         /* MAC */
237         mac->ops.reset_hw = &ixgbe_reset_hw_82599;
238         mac->ops.enable_relaxed_ordering = &ixgbe_enable_relaxed_ordering_gen2;
239         mac->ops.get_media_type = &ixgbe_get_media_type_82599;
240         mac->ops.get_supported_physical_layer =
241                                     &ixgbe_get_supported_physical_layer_82599;
242         mac->ops.disable_sec_rx_path = &ixgbe_disable_sec_rx_path_generic;
243         mac->ops.enable_sec_rx_path = &ixgbe_enable_sec_rx_path_generic;
244         mac->ops.enable_rx_dma = &ixgbe_enable_rx_dma_82599;
245         mac->ops.read_analog_reg8 = &ixgbe_read_analog_reg8_82599;
246         mac->ops.write_analog_reg8 = &ixgbe_write_analog_reg8_82599;
247         mac->ops.start_hw = &ixgbe_start_hw_82599;
248         mac->ops.get_san_mac_addr = &ixgbe_get_san_mac_addr_generic;
249         mac->ops.set_san_mac_addr = &ixgbe_set_san_mac_addr_generic;
250         mac->ops.get_device_caps = &ixgbe_get_device_caps_generic;
251         mac->ops.get_wwn_prefix = &ixgbe_get_wwn_prefix_generic;
252         mac->ops.get_fcoe_boot_status = &ixgbe_get_fcoe_boot_status_generic;
253
254         /* RAR, Multicast, VLAN */
255         mac->ops.set_vmdq = &ixgbe_set_vmdq_generic;
256         mac->ops.set_vmdq_san_mac = &ixgbe_set_vmdq_san_mac_generic;
257         mac->ops.clear_vmdq = &ixgbe_clear_vmdq_generic;
258         mac->ops.insert_mac_addr = &ixgbe_insert_mac_addr_generic;
259         mac->rar_highwater = 1;
260         mac->ops.set_vfta = &ixgbe_set_vfta_generic;
261         mac->ops.set_vlvf = &ixgbe_set_vlvf_generic;
262         mac->ops.clear_vfta = &ixgbe_clear_vfta_generic;
263         mac->ops.init_uta_tables = &ixgbe_init_uta_tables_generic;
264         mac->ops.setup_sfp = &ixgbe_setup_sfp_modules_82599;
265         mac->ops.set_mac_anti_spoofing = &ixgbe_set_mac_anti_spoofing;
266         mac->ops.set_vlan_anti_spoofing = &ixgbe_set_vlan_anti_spoofing;
267
268         /* Link */
269         mac->ops.get_link_capabilities = &ixgbe_get_link_capabilities_82599;
270         mac->ops.check_link = &ixgbe_check_mac_link_generic;
271         mac->ops.setup_rxpba = &ixgbe_set_rxpba_generic;
272         ixgbe_init_mac_link_ops_82599(hw);
273
274         mac->mcft_size          = 128;
275         mac->vft_size           = 128;
276         mac->num_rar_entries    = 128;
277         mac->rx_pb_size         = 512;
278         mac->max_tx_queues      = 128;
279         mac->max_rx_queues      = 128;
280         mac->max_msix_vectors   = ixgbe_get_pcie_msix_count_generic(hw);
281
282         mac->arc_subsystem_valid = (IXGBE_READ_REG(hw, IXGBE_FWSM) &
283                                    IXGBE_FWSM_MODE_MASK) ? true : false;
284
285         hw->mbx.ops.init_params = ixgbe_init_mbx_params_pf;
286
287         /* EEPROM */
288         eeprom->ops.read = &ixgbe_read_eeprom_82599;
289         eeprom->ops.read_buffer = &ixgbe_read_eeprom_buffer_82599;
290
291         /* Manageability interface */
292         mac->ops.set_fw_drv_ver = &ixgbe_set_fw_drv_ver_generic;
293
294
295         return ret_val;
296 }
297
298 /**
299  *  ixgbe_get_link_capabilities_82599 - Determines link capabilities
300  *  @hw: pointer to hardware structure
301  *  @speed: pointer to link speed
302  *  @negotiation: true when autoneg or autotry is enabled
303  *
304  *  Determines the link capabilities by reading the AUTOC register.
305  **/
306 s32 ixgbe_get_link_capabilities_82599(struct ixgbe_hw *hw,
307                                       ixgbe_link_speed *speed,
308                                       bool *negotiation)
309 {
310         s32 status = IXGBE_SUCCESS;
311         u32 autoc = 0;
312
313         DEBUGFUNC("ixgbe_get_link_capabilities_82599");
314
315
316         /* Check if 1G SFP module. */
317         if (hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
318             hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
319             hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core0 ||
320             hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core1) {
321                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
322                 *negotiation = true;
323                 goto out;
324         }
325
326         /*
327          * Determine link capabilities based on the stored value of AUTOC,
328          * which represents EEPROM defaults.  If AUTOC value has not
329          * been stored, use the current register values.
330          */
331         if (hw->mac.orig_link_settings_stored)
332                 autoc = hw->mac.orig_autoc;
333         else
334                 autoc = IXGBE_READ_REG(hw, IXGBE_AUTOC);
335
336         switch (autoc & IXGBE_AUTOC_LMS_MASK) {
337         case IXGBE_AUTOC_LMS_1G_LINK_NO_AN:
338                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
339                 *negotiation = false;
340                 break;
341
342         case IXGBE_AUTOC_LMS_10G_LINK_NO_AN:
343                 *speed = IXGBE_LINK_SPEED_10GB_FULL;
344                 *negotiation = false;
345                 break;
346
347         case IXGBE_AUTOC_LMS_1G_AN:
348                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
349                 *negotiation = true;
350                 break;
351
352         case IXGBE_AUTOC_LMS_10G_SERIAL:
353                 *speed = IXGBE_LINK_SPEED_10GB_FULL;
354                 *negotiation = false;
355                 break;
356
357         case IXGBE_AUTOC_LMS_KX4_KX_KR:
358         case IXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN:
359                 *speed = IXGBE_LINK_SPEED_UNKNOWN;
360                 if (autoc & IXGBE_AUTOC_KR_SUPP)
361                         *speed |= IXGBE_LINK_SPEED_10GB_FULL;
362                 if (autoc & IXGBE_AUTOC_KX4_SUPP)
363                         *speed |= IXGBE_LINK_SPEED_10GB_FULL;
364                 if (autoc & IXGBE_AUTOC_KX_SUPP)
365                         *speed |= IXGBE_LINK_SPEED_1GB_FULL;
366                 *negotiation = true;
367                 break;
368
369         case IXGBE_AUTOC_LMS_KX4_KX_KR_SGMII:
370                 *speed = IXGBE_LINK_SPEED_100_FULL;
371                 if (autoc & IXGBE_AUTOC_KR_SUPP)
372                         *speed |= IXGBE_LINK_SPEED_10GB_FULL;
373                 if (autoc & IXGBE_AUTOC_KX4_SUPP)
374                         *speed |= IXGBE_LINK_SPEED_10GB_FULL;
375                 if (autoc & IXGBE_AUTOC_KX_SUPP)
376                         *speed |= IXGBE_LINK_SPEED_1GB_FULL;
377                 *negotiation = true;
378                 break;
379
380         case IXGBE_AUTOC_LMS_SGMII_1G_100M:
381                 *speed = IXGBE_LINK_SPEED_1GB_FULL | IXGBE_LINK_SPEED_100_FULL;
382                 *negotiation = false;
383                 break;
384
385         default:
386                 status = IXGBE_ERR_LINK_SETUP;
387                 goto out;
388                 break;
389         }
390
391         if (hw->phy.multispeed_fiber) {
392                 *speed |= IXGBE_LINK_SPEED_10GB_FULL |
393                           IXGBE_LINK_SPEED_1GB_FULL;
394                 *negotiation = true;
395         }
396
397 out:
398         return status;
399 }
400
401 /**
402  *  ixgbe_get_media_type_82599 - Get media type
403  *  @hw: pointer to hardware structure
404  *
405  *  Returns the media type (fiber, copper, backplane)
406  **/
407 enum ixgbe_media_type ixgbe_get_media_type_82599(struct ixgbe_hw *hw)
408 {
409         enum ixgbe_media_type media_type;
410
411         DEBUGFUNC("ixgbe_get_media_type_82599");
412
413         /* Detect if there is a copper PHY attached. */
414         switch (hw->phy.type) {
415         case ixgbe_phy_cu_unknown:
416         case ixgbe_phy_tn:
417                 media_type = ixgbe_media_type_copper;
418                 goto out;
419         default:
420                 break;
421         }
422
423         switch (hw->device_id) {
424         case IXGBE_DEV_ID_82599_KX4:
425         case IXGBE_DEV_ID_82599_KX4_MEZZ:
426         case IXGBE_DEV_ID_82599_COMBO_BACKPLANE:
427         case IXGBE_DEV_ID_82599_KR:
428         case IXGBE_DEV_ID_82599_BACKPLANE_FCOE:
429         case IXGBE_DEV_ID_82599_XAUI_LOM:
430                 /* Default device ID is mezzanine card KX/KX4 */
431                 media_type = ixgbe_media_type_backplane;
432                 break;
433         case IXGBE_DEV_ID_82599_SFP:
434         case IXGBE_DEV_ID_82599_SFP_FCOE:
435         case IXGBE_DEV_ID_82599_SFP_EM:
436         case IXGBE_DEV_ID_82599_SFP_SF2:
437         case IXGBE_DEV_ID_82599_SFP_SF_QP:
438         case IXGBE_DEV_ID_82599EN_SFP:
439 #ifdef RTE_NIC_BYPASS 
440         case IXGBE_DEV_ID_82599_BYPASS:
441 #endif
442                 media_type = ixgbe_media_type_fiber;
443                 break;
444         case IXGBE_DEV_ID_82599_CX4:
445                 media_type = ixgbe_media_type_cx4;
446                 break;
447         case IXGBE_DEV_ID_82599_T3_LOM:
448                 media_type = ixgbe_media_type_copper;
449                 break;
450         default:
451                 media_type = ixgbe_media_type_unknown;
452                 break;
453         }
454 out:
455         return media_type;
456 }
457
458 /**
459  *  ixgbe_start_mac_link_82599 - Setup MAC link settings
460  *  @hw: pointer to hardware structure
461  *  @autoneg_wait_to_complete: true when waiting for completion is needed
462  *
463  *  Configures link settings based on values in the ixgbe_hw struct.
464  *  Restarts the link.  Performs autonegotiation if needed.
465  **/
466 s32 ixgbe_start_mac_link_82599(struct ixgbe_hw *hw,
467                                bool autoneg_wait_to_complete)
468 {
469         u32 autoc_reg;
470         u32 links_reg;
471         u32 i;
472         s32 status = IXGBE_SUCCESS;
473         bool got_lock = false;
474
475         DEBUGFUNC("ixgbe_start_mac_link_82599");
476
477
478         /*  reset_pipeline requires us to hold this lock as it writes to
479          *  AUTOC.
480          */
481         if (ixgbe_verify_lesm_fw_enabled_82599(hw)) {
482                 status = hw->mac.ops.acquire_swfw_sync(hw,
483                                                        IXGBE_GSSR_MAC_CSR_SM);
484                 if (status != IXGBE_SUCCESS)
485                         goto out;
486
487                 got_lock = true;
488         }
489
490         /* Restart link */
491         ixgbe_reset_pipeline_82599(hw);
492
493         if (got_lock)
494                 hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_MAC_CSR_SM);
495
496         /* Only poll for autoneg to complete if specified to do so */
497         if (autoneg_wait_to_complete) {
498                 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
499                 if ((autoc_reg & IXGBE_AUTOC_LMS_MASK) ==
500                      IXGBE_AUTOC_LMS_KX4_KX_KR ||
501                     (autoc_reg & IXGBE_AUTOC_LMS_MASK) ==
502                      IXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
503                     (autoc_reg & IXGBE_AUTOC_LMS_MASK) ==
504                      IXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
505                         links_reg = 0; /* Just in case Autoneg time = 0 */
506                         for (i = 0; i < IXGBE_AUTO_NEG_TIME; i++) {
507                                 links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
508                                 if (links_reg & IXGBE_LINKS_KX_AN_COMP)
509                                         break;
510                                 msec_delay(100);
511                         }
512                         if (!(links_reg & IXGBE_LINKS_KX_AN_COMP)) {
513                                 status = IXGBE_ERR_AUTONEG_NOT_COMPLETE;
514                                 DEBUGOUT("Autoneg did not complete.\n");
515                         }
516                 }
517         }
518
519         /* Add delay to filter out noises during initial link setup */
520         msec_delay(50);
521
522 out:
523         return status;
524 }
525
526 /**
527  *  ixgbe_disable_tx_laser_multispeed_fiber - Disable Tx laser
528  *  @hw: pointer to hardware structure
529  *
530  *  The base drivers may require better control over SFP+ module
531  *  PHY states.  This includes selectively shutting down the Tx
532  *  laser on the PHY, effectively halting physical link.
533  **/
534 void ixgbe_disable_tx_laser_multispeed_fiber(struct ixgbe_hw *hw)
535 {
536         u32 esdp_reg = IXGBE_READ_REG(hw, IXGBE_ESDP);
537
538         /* Disable tx laser; allow 100us to go dark per spec */
539         esdp_reg |= IXGBE_ESDP_SDP3;
540         IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp_reg);
541         IXGBE_WRITE_FLUSH(hw);
542         usec_delay(100);
543 }
544
545 /**
546  *  ixgbe_enable_tx_laser_multispeed_fiber - Enable Tx laser
547  *  @hw: pointer to hardware structure
548  *
549  *  The base drivers may require better control over SFP+ module
550  *  PHY states.  This includes selectively turning on the Tx
551  *  laser on the PHY, effectively starting physical link.
552  **/
553 void ixgbe_enable_tx_laser_multispeed_fiber(struct ixgbe_hw *hw)
554 {
555         u32 esdp_reg = IXGBE_READ_REG(hw, IXGBE_ESDP);
556
557         /* Enable tx laser; allow 100ms to light up */
558         esdp_reg &= ~IXGBE_ESDP_SDP3;
559         IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp_reg);
560         IXGBE_WRITE_FLUSH(hw);
561         msec_delay(100);
562 }
563
564 /**
565  *  ixgbe_flap_tx_laser_multispeed_fiber - Flap Tx laser
566  *  @hw: pointer to hardware structure
567  *
568  *  When the driver changes the link speeds that it can support,
569  *  it sets autotry_restart to true to indicate that we need to
570  *  initiate a new autotry session with the link partner.  To do
571  *  so, we set the speed then disable and re-enable the tx laser, to
572  *  alert the link partner that it also needs to restart autotry on its
573  *  end.  This is consistent with true clause 37 autoneg, which also
574  *  involves a loss of signal.
575  **/
576 void ixgbe_flap_tx_laser_multispeed_fiber(struct ixgbe_hw *hw)
577 {
578         DEBUGFUNC("ixgbe_flap_tx_laser_multispeed_fiber");
579
580         if (hw->mac.autotry_restart) {
581                 ixgbe_disable_tx_laser_multispeed_fiber(hw);
582                 ixgbe_enable_tx_laser_multispeed_fiber(hw);
583                 hw->mac.autotry_restart = false;
584         }
585 }
586
587 /**
588  *  ixgbe_setup_mac_link_multispeed_fiber - Set MAC link speed
589  *  @hw: pointer to hardware structure
590  *  @speed: new link speed
591  *  @autoneg: true if autonegotiation enabled
592  *  @autoneg_wait_to_complete: true when waiting for completion is needed
593  *
594  *  Set the link speed in the AUTOC register and restarts link.
595  **/
596 s32 ixgbe_setup_mac_link_multispeed_fiber(struct ixgbe_hw *hw,
597                                      ixgbe_link_speed speed, bool autoneg,
598                                      bool autoneg_wait_to_complete)
599 {
600         s32 status = IXGBE_SUCCESS;
601         ixgbe_link_speed link_speed = IXGBE_LINK_SPEED_UNKNOWN;
602         ixgbe_link_speed highest_link_speed = IXGBE_LINK_SPEED_UNKNOWN;
603         u32 speedcnt = 0;
604         u32 esdp_reg = IXGBE_READ_REG(hw, IXGBE_ESDP);
605         u32 i = 0;
606         bool link_up = false;
607         bool negotiation;
608
609         DEBUGFUNC("ixgbe_setup_mac_link_multispeed_fiber");
610
611         /* Mask off requested but non-supported speeds */
612         status = ixgbe_get_link_capabilities(hw, &link_speed, &negotiation);
613         if (status != IXGBE_SUCCESS)
614                 return status;
615
616         speed &= link_speed;
617
618         /*
619          * Try each speed one by one, highest priority first.  We do this in
620          * software because 10gb fiber doesn't support speed autonegotiation.
621          */
622         if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
623                 speedcnt++;
624                 highest_link_speed = IXGBE_LINK_SPEED_10GB_FULL;
625
626                 /* If we already have link at this speed, just jump out */
627                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
628                 if (status != IXGBE_SUCCESS)
629                         return status;
630
631                 if ((link_speed == IXGBE_LINK_SPEED_10GB_FULL) && link_up)
632                         goto out;
633
634                 /* Set the module link speed */
635                 esdp_reg |= (IXGBE_ESDP_SDP5_DIR | IXGBE_ESDP_SDP5);
636                 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp_reg);
637                 IXGBE_WRITE_FLUSH(hw);
638
639                 /* Allow module to change analog characteristics (1G->10G) */
640                 msec_delay(40);
641
642                 status = ixgbe_setup_mac_link_82599(hw,
643                                                     IXGBE_LINK_SPEED_10GB_FULL,
644                                                     autoneg,
645                                                     autoneg_wait_to_complete);
646                 if (status != IXGBE_SUCCESS)
647                         return status;
648
649                 /* Flap the tx laser if it has not already been done */
650                 ixgbe_flap_tx_laser(hw);
651
652                 /*
653                  * Wait for the controller to acquire link.  Per IEEE 802.3ap,
654                  * Section 73.10.2, we may have to wait up to 500ms if KR is
655                  * attempted.  82599 uses the same timing for 10g SFI.
656                  */
657                 for (i = 0; i < 5; i++) {
658                         /* Wait for the link partner to also set speed */
659                         msec_delay(100);
660
661                         /* If we have link, just jump out */
662                         status = ixgbe_check_link(hw, &link_speed,
663                                                   &link_up, false);
664                         if (status != IXGBE_SUCCESS)
665                                 return status;
666
667                         if (link_up)
668                                 goto out;
669                 }
670         }
671
672         if (speed & IXGBE_LINK_SPEED_1GB_FULL) {
673                 speedcnt++;
674                 if (highest_link_speed == IXGBE_LINK_SPEED_UNKNOWN)
675                         highest_link_speed = IXGBE_LINK_SPEED_1GB_FULL;
676
677                 /* If we already have link at this speed, just jump out */
678                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
679                 if (status != IXGBE_SUCCESS)
680                         return status;
681
682                 if ((link_speed == IXGBE_LINK_SPEED_1GB_FULL) && link_up)
683                         goto out;
684
685                 /* Set the module link speed */
686                 esdp_reg &= ~IXGBE_ESDP_SDP5;
687                 esdp_reg |= IXGBE_ESDP_SDP5_DIR;
688                 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp_reg);
689                 IXGBE_WRITE_FLUSH(hw);
690
691                 /* Allow module to change analog characteristics (10G->1G) */
692                 msec_delay(40);
693
694                 status = ixgbe_setup_mac_link_82599(hw,
695                                                     IXGBE_LINK_SPEED_1GB_FULL,
696                                                     autoneg,
697                                                     autoneg_wait_to_complete);
698                 if (status != IXGBE_SUCCESS)
699                         return status;
700
701                 /* Flap the tx laser if it has not already been done */
702                 ixgbe_flap_tx_laser(hw);
703
704                 /* Wait for the link partner to also set speed */
705                 msec_delay(100);
706
707                 /* If we have link, just jump out */
708                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
709                 if (status != IXGBE_SUCCESS)
710                         return status;
711
712                 if (link_up)
713                         goto out;
714         }
715
716         /*
717          * We didn't get link.  Configure back to the highest speed we tried,
718          * (if there was more than one).  We call ourselves back with just the
719          * single highest speed that the user requested.
720          */
721         if (speedcnt > 1)
722                 status = ixgbe_setup_mac_link_multispeed_fiber(hw,
723                         highest_link_speed, autoneg, autoneg_wait_to_complete);
724
725 out:
726         /* Set autoneg_advertised value based on input link speed */
727         hw->phy.autoneg_advertised = 0;
728
729         if (speed & IXGBE_LINK_SPEED_10GB_FULL)
730                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
731
732         if (speed & IXGBE_LINK_SPEED_1GB_FULL)
733                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
734
735         return status;
736 }
737
738 /**
739  *  ixgbe_setup_mac_link_smartspeed - Set MAC link speed using SmartSpeed
740  *  @hw: pointer to hardware structure
741  *  @speed: new link speed
742  *  @autoneg: true if autonegotiation enabled
743  *  @autoneg_wait_to_complete: true when waiting for completion is needed
744  *
745  *  Implements the Intel SmartSpeed algorithm.
746  **/
747 s32 ixgbe_setup_mac_link_smartspeed(struct ixgbe_hw *hw,
748                                     ixgbe_link_speed speed, bool autoneg,
749                                     bool autoneg_wait_to_complete)
750 {
751         s32 status = IXGBE_SUCCESS;
752         ixgbe_link_speed link_speed = IXGBE_LINK_SPEED_UNKNOWN;
753         s32 i, j;
754         bool link_up = false;
755         u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
756
757         DEBUGFUNC("ixgbe_setup_mac_link_smartspeed");
758
759          /* Set autoneg_advertised value based on input link speed */
760         hw->phy.autoneg_advertised = 0;
761
762         if (speed & IXGBE_LINK_SPEED_10GB_FULL)
763                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
764
765         if (speed & IXGBE_LINK_SPEED_1GB_FULL)
766                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
767
768         if (speed & IXGBE_LINK_SPEED_100_FULL)
769                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_100_FULL;
770
771         /*
772          * Implement Intel SmartSpeed algorithm.  SmartSpeed will reduce the
773          * autoneg advertisement if link is unable to be established at the
774          * highest negotiated rate.  This can sometimes happen due to integrity
775          * issues with the physical media connection.
776          */
777
778         /* First, try to get link with full advertisement */
779         hw->phy.smart_speed_active = false;
780         for (j = 0; j < IXGBE_SMARTSPEED_MAX_RETRIES; j++) {
781                 status = ixgbe_setup_mac_link_82599(hw, speed, autoneg,
782                                                     autoneg_wait_to_complete);
783                 if (status != IXGBE_SUCCESS)
784                         goto out;
785
786                 /*
787                  * Wait for the controller to acquire link.  Per IEEE 802.3ap,
788                  * Section 73.10.2, we may have to wait up to 500ms if KR is
789                  * attempted, or 200ms if KX/KX4/BX/BX4 is attempted, per
790                  * Table 9 in the AN MAS.
791                  */
792                 for (i = 0; i < 5; i++) {
793                         msec_delay(100);
794
795                         /* If we have link, just jump out */
796                         status = ixgbe_check_link(hw, &link_speed, &link_up,
797                                                   false);
798                         if (status != IXGBE_SUCCESS)
799                                 goto out;
800
801                         if (link_up)
802                                 goto out;
803                 }
804         }
805
806         /*
807          * We didn't get link.  If we advertised KR plus one of KX4/KX
808          * (or BX4/BX), then disable KR and try again.
809          */
810         if (((autoc_reg & IXGBE_AUTOC_KR_SUPP) == 0) ||
811             ((autoc_reg & IXGBE_AUTOC_KX4_KX_SUPP_MASK) == 0))
812                 goto out;
813
814         /* Turn SmartSpeed on to disable KR support */
815         hw->phy.smart_speed_active = true;
816         status = ixgbe_setup_mac_link_82599(hw, speed, autoneg,
817                                             autoneg_wait_to_complete);
818         if (status != IXGBE_SUCCESS)
819                 goto out;
820
821         /*
822          * Wait for the controller to acquire link.  600ms will allow for
823          * the AN link_fail_inhibit_timer as well for multiple cycles of
824          * parallel detect, both 10g and 1g. This allows for the maximum
825          * connect attempts as defined in the AN MAS table 73-7.
826          */
827         for (i = 0; i < 6; i++) {
828                 msec_delay(100);
829
830                 /* If we have link, just jump out */
831                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
832                 if (status != IXGBE_SUCCESS)
833                         goto out;
834
835                 if (link_up)
836                         goto out;
837         }
838
839         /* We didn't get link.  Turn SmartSpeed back off. */
840         hw->phy.smart_speed_active = false;
841         status = ixgbe_setup_mac_link_82599(hw, speed, autoneg,
842                                             autoneg_wait_to_complete);
843
844 out:
845         if (link_up && (link_speed == IXGBE_LINK_SPEED_1GB_FULL))
846                 DEBUGOUT("Smartspeed has downgraded the link speed "
847                 "from the maximum advertised\n");
848         return status;
849 }
850
851 /**
852  *  ixgbe_setup_mac_link_82599 - Set MAC link speed
853  *  @hw: pointer to hardware structure
854  *  @speed: new link speed
855  *  @autoneg: true if autonegotiation enabled
856  *  @autoneg_wait_to_complete: true when waiting for completion is needed
857  *
858  *  Set the link speed in the AUTOC register and restarts link.
859  **/
860 s32 ixgbe_setup_mac_link_82599(struct ixgbe_hw *hw,
861                                ixgbe_link_speed speed, bool autoneg,
862                                bool autoneg_wait_to_complete)
863 {
864         s32 status = IXGBE_SUCCESS;
865         u32 autoc = IXGBE_READ_REG(hw, IXGBE_AUTOC);
866         u32 autoc2 = IXGBE_READ_REG(hw, IXGBE_AUTOC2);
867         u32 start_autoc = autoc;
868         u32 orig_autoc = 0;
869         u32 link_mode = autoc & IXGBE_AUTOC_LMS_MASK;
870         u32 pma_pmd_1g = autoc & IXGBE_AUTOC_1G_PMA_PMD_MASK;
871         u32 pma_pmd_10g_serial = autoc2 & IXGBE_AUTOC2_10G_SERIAL_PMA_PMD_MASK;
872         u32 links_reg;
873         u32 i;
874         ixgbe_link_speed link_capabilities = IXGBE_LINK_SPEED_UNKNOWN;
875         bool got_lock = false;
876
877         DEBUGFUNC("ixgbe_setup_mac_link_82599");
878
879         /* Check to see if speed passed in is supported. */
880         status = ixgbe_get_link_capabilities(hw, &link_capabilities, &autoneg);
881         if (status)
882                 goto out;
883
884         speed &= link_capabilities;
885
886         if (speed == IXGBE_LINK_SPEED_UNKNOWN) {
887                 status = IXGBE_ERR_LINK_SETUP;
888                 goto out;
889         }
890
891         /* Use stored value (EEPROM defaults) of AUTOC to find KR/KX4 support*/
892         if (hw->mac.orig_link_settings_stored)
893                 orig_autoc = hw->mac.orig_autoc;
894         else
895                 orig_autoc = autoc;
896
897         if (link_mode == IXGBE_AUTOC_LMS_KX4_KX_KR ||
898             link_mode == IXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
899             link_mode == IXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
900                 /* Set KX4/KX/KR support according to speed requested */
901                 autoc &= ~(IXGBE_AUTOC_KX4_KX_SUPP_MASK | IXGBE_AUTOC_KR_SUPP);
902                 if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
903                         if (orig_autoc & IXGBE_AUTOC_KX4_SUPP)
904                                 autoc |= IXGBE_AUTOC_KX4_SUPP;
905                         if ((orig_autoc & IXGBE_AUTOC_KR_SUPP) &&
906                             (hw->phy.smart_speed_active == false))
907                                 autoc |= IXGBE_AUTOC_KR_SUPP;
908                 }
909                 if (speed & IXGBE_LINK_SPEED_1GB_FULL)
910                         autoc |= IXGBE_AUTOC_KX_SUPP;
911         } else if ((pma_pmd_1g == IXGBE_AUTOC_1G_SFI) &&
912                    (link_mode == IXGBE_AUTOC_LMS_1G_LINK_NO_AN ||
913                     link_mode == IXGBE_AUTOC_LMS_1G_AN)) {
914                 /* Switch from 1G SFI to 10G SFI if requested */
915                 if ((speed == IXGBE_LINK_SPEED_10GB_FULL) &&
916                     (pma_pmd_10g_serial == IXGBE_AUTOC2_10G_SFI)) {
917                         autoc &= ~IXGBE_AUTOC_LMS_MASK;
918                         autoc |= IXGBE_AUTOC_LMS_10G_SERIAL;
919                 }
920         } else if ((pma_pmd_10g_serial == IXGBE_AUTOC2_10G_SFI) &&
921                    (link_mode == IXGBE_AUTOC_LMS_10G_SERIAL)) {
922                 /* Switch from 10G SFI to 1G SFI if requested */
923                 if ((speed == IXGBE_LINK_SPEED_1GB_FULL) &&
924                     (pma_pmd_1g == IXGBE_AUTOC_1G_SFI)) {
925                         autoc &= ~IXGBE_AUTOC_LMS_MASK;
926                         if (autoneg)
927                                 autoc |= IXGBE_AUTOC_LMS_1G_AN;
928                         else
929                                 autoc |= IXGBE_AUTOC_LMS_1G_LINK_NO_AN;
930                 }
931         }
932
933         if (autoc != start_autoc) {
934                 /* Need SW/FW semaphore around AUTOC writes if LESM is on,
935                  * likewise reset_pipeline requires us to hold this lock as
936                  * it also writes to AUTOC.
937                  */
938                 if (ixgbe_verify_lesm_fw_enabled_82599(hw)) {
939                         status = hw->mac.ops.acquire_swfw_sync(hw,
940                                                         IXGBE_GSSR_MAC_CSR_SM);
941                         if (status != IXGBE_SUCCESS) {
942                                 status = IXGBE_ERR_SWFW_SYNC;
943                                 goto out;
944                         }
945
946                         got_lock = true;
947                 }
948
949                 /* Restart link */
950                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc);
951                 ixgbe_reset_pipeline_82599(hw);
952
953                 if (got_lock) {
954                         hw->mac.ops.release_swfw_sync(hw,
955                                                       IXGBE_GSSR_MAC_CSR_SM);
956                         got_lock = false;
957                 }
958
959                 /* Only poll for autoneg to complete if specified to do so */
960                 if (autoneg_wait_to_complete) {
961                         if (link_mode == IXGBE_AUTOC_LMS_KX4_KX_KR ||
962                             link_mode == IXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
963                             link_mode == IXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
964                                 links_reg = 0; /*Just in case Autoneg time=0*/
965                                 for (i = 0; i < IXGBE_AUTO_NEG_TIME; i++) {
966                                         links_reg =
967                                                IXGBE_READ_REG(hw, IXGBE_LINKS);
968                                         if (links_reg & IXGBE_LINKS_KX_AN_COMP)
969                                                 break;
970                                         msec_delay(100);
971                                 }
972                                 if (!(links_reg & IXGBE_LINKS_KX_AN_COMP)) {
973                                         status =
974                                                 IXGBE_ERR_AUTONEG_NOT_COMPLETE;
975                                         DEBUGOUT("Autoneg did not complete.\n");
976                                 }
977                         }
978                 }
979
980                 /* Add delay to filter out noises during initial link setup */
981                 msec_delay(50);
982         }
983
984 out:
985         return status;
986 }
987
988 /**
989  *  ixgbe_setup_copper_link_82599 - Set the PHY autoneg advertised field
990  *  @hw: pointer to hardware structure
991  *  @speed: new link speed
992  *  @autoneg: true if autonegotiation enabled
993  *  @autoneg_wait_to_complete: true if waiting is needed to complete
994  *
995  *  Restarts link on PHY and MAC based on settings passed in.
996  **/
997 STATIC s32 ixgbe_setup_copper_link_82599(struct ixgbe_hw *hw,
998                                          ixgbe_link_speed speed,
999                                          bool autoneg,
1000                                          bool autoneg_wait_to_complete)
1001 {
1002         s32 status;
1003
1004         DEBUGFUNC("ixgbe_setup_copper_link_82599");
1005
1006         /* Setup the PHY according to input speed */
1007         status = hw->phy.ops.setup_link_speed(hw, speed, autoneg,
1008                                               autoneg_wait_to_complete);
1009         /* Set up MAC */
1010         ixgbe_start_mac_link_82599(hw, autoneg_wait_to_complete);
1011
1012         return status;
1013 }
1014
1015 /**
1016  *  ixgbe_reset_hw_82599 - Perform hardware reset
1017  *  @hw: pointer to hardware structure
1018  *
1019  *  Resets the hardware by resetting the transmit and receive units, masks
1020  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
1021  *  reset.
1022  **/
1023 s32 ixgbe_reset_hw_82599(struct ixgbe_hw *hw)
1024 {
1025         ixgbe_link_speed link_speed;
1026         s32 status;
1027         u32 ctrl, i, autoc, autoc2;
1028         bool link_up = false;
1029
1030         DEBUGFUNC("ixgbe_reset_hw_82599");
1031
1032         /* Call adapter stop to disable tx/rx and clear interrupts */
1033         status = hw->mac.ops.stop_adapter(hw);
1034         if (status != IXGBE_SUCCESS)
1035                 goto reset_hw_out;
1036
1037         /* flush pending Tx transactions */
1038         ixgbe_clear_tx_pending(hw);
1039
1040         /* PHY ops must be identified and initialized prior to reset */
1041
1042         /* Identify PHY and related function pointers */
1043         status = hw->phy.ops.init(hw);
1044
1045         if (status == IXGBE_ERR_SFP_NOT_SUPPORTED)
1046                 goto reset_hw_out;
1047
1048         /* Setup SFP module if there is one present. */
1049         if (hw->phy.sfp_setup_needed) {
1050                 status = hw->mac.ops.setup_sfp(hw);
1051                 hw->phy.sfp_setup_needed = false;
1052         }
1053
1054         if (status == IXGBE_ERR_SFP_NOT_SUPPORTED)
1055                 goto reset_hw_out;
1056
1057         /* Reset PHY */
1058         if (hw->phy.reset_disable == false && hw->phy.ops.reset != NULL)
1059                 hw->phy.ops.reset(hw);
1060
1061 mac_reset_top:
1062         /*
1063          * Issue global reset to the MAC.  Needs to be SW reset if link is up.
1064          * If link reset is used when link is up, it might reset the PHY when
1065          * mng is using it.  If link is down or the flag to force full link
1066          * reset is set, then perform link reset.
1067          */
1068         ctrl = IXGBE_CTRL_LNK_RST;
1069         if (!hw->force_full_reset) {
1070                 hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
1071                 if (link_up)
1072                         ctrl = IXGBE_CTRL_RST;
1073         }
1074
1075         ctrl |= IXGBE_READ_REG(hw, IXGBE_CTRL);
1076         IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
1077         IXGBE_WRITE_FLUSH(hw);
1078
1079         /* Poll for reset bit to self-clear meaning reset is complete */
1080         for (i = 0; i < 10; i++) {
1081                 usec_delay(1);
1082                 ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
1083                 if (!(ctrl & IXGBE_CTRL_RST_MASK))
1084                         break;
1085         }
1086
1087         if (ctrl & IXGBE_CTRL_RST_MASK) {
1088                 status = IXGBE_ERR_RESET_FAILED;
1089                 DEBUGOUT("Reset polling failed to complete.\n");
1090         }
1091
1092         msec_delay(50);
1093
1094         /*
1095          * Double resets are required for recovery from certain error
1096          * conditions.  Between resets, it is necessary to stall to
1097          * allow time for any pending HW events to complete.
1098          */
1099         if (hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
1100                 hw->mac.flags &= ~IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
1101                 goto mac_reset_top;
1102         }
1103
1104         /*
1105          * Store the original AUTOC/AUTOC2 values if they have not been
1106          * stored off yet.  Otherwise restore the stored original
1107          * values since the reset operation sets back to defaults.
1108          */
1109         autoc = IXGBE_READ_REG(hw, IXGBE_AUTOC);
1110         autoc2 = IXGBE_READ_REG(hw, IXGBE_AUTOC2);
1111
1112         /* Enable link if disabled in NVM */
1113         if (autoc2 & IXGBE_AUTOC2_LINK_DISABLE_MASK) {
1114                 autoc2 &= ~IXGBE_AUTOC2_LINK_DISABLE_MASK;
1115                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC2, autoc2);
1116                 IXGBE_WRITE_FLUSH(hw);
1117         }
1118
1119         if (hw->mac.orig_link_settings_stored == false) {
1120                 hw->mac.orig_autoc = autoc;
1121                 hw->mac.orig_autoc2 = autoc2;
1122                 hw->mac.orig_link_settings_stored = true;
1123         } else {
1124                 if (autoc != hw->mac.orig_autoc) {
1125                         /* Need SW/FW semaphore around AUTOC writes if LESM is
1126                          * on, likewise reset_pipeline requires us to hold
1127                          * this lock as it also writes to AUTOC.
1128                          */
1129                         bool got_lock = false;
1130                         if (ixgbe_verify_lesm_fw_enabled_82599(hw)) {
1131                                 status = hw->mac.ops.acquire_swfw_sync(hw,
1132                                                         IXGBE_GSSR_MAC_CSR_SM);
1133                                 if (status != IXGBE_SUCCESS) {
1134                                         status = IXGBE_ERR_SWFW_SYNC;
1135                                         goto reset_hw_out;
1136                                 }
1137
1138                                 got_lock = true;
1139                         }
1140
1141                         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, hw->mac.orig_autoc);
1142                         ixgbe_reset_pipeline_82599(hw);
1143
1144                         if (got_lock)
1145                                 hw->mac.ops.release_swfw_sync(hw,
1146                                                       IXGBE_GSSR_MAC_CSR_SM);
1147                 }
1148
1149                 if ((autoc2 & IXGBE_AUTOC2_UPPER_MASK) !=
1150                     (hw->mac.orig_autoc2 & IXGBE_AUTOC2_UPPER_MASK)) {
1151                         autoc2 &= ~IXGBE_AUTOC2_UPPER_MASK;
1152                         autoc2 |= (hw->mac.orig_autoc2 &
1153                                    IXGBE_AUTOC2_UPPER_MASK);
1154                         IXGBE_WRITE_REG(hw, IXGBE_AUTOC2, autoc2);
1155                 }
1156         }
1157
1158         /* Store the permanent mac address */
1159         hw->mac.ops.get_mac_addr(hw, hw->mac.perm_addr);
1160
1161         /*
1162          * Store MAC address from RAR0, clear receive address registers, and
1163          * clear the multicast table.  Also reset num_rar_entries to 128,
1164          * since we modify this value when programming the SAN MAC address.
1165          */
1166         hw->mac.num_rar_entries = 128;
1167         hw->mac.ops.init_rx_addrs(hw);
1168
1169         /* Store the permanent SAN mac address */
1170         hw->mac.ops.get_san_mac_addr(hw, hw->mac.san_addr);
1171
1172         /* Add the SAN MAC address to the RAR only if it's a valid address */
1173         if (ixgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
1174                 hw->mac.ops.set_rar(hw, hw->mac.num_rar_entries - 1,
1175                                     hw->mac.san_addr, 0, IXGBE_RAH_AV);
1176
1177                 /* Save the SAN MAC RAR index */
1178                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
1179
1180                 /* Reserve the last RAR for the SAN MAC address */
1181                 hw->mac.num_rar_entries--;
1182         }
1183
1184         /* Store the alternative WWNN/WWPN prefix */
1185         hw->mac.ops.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
1186                                    &hw->mac.wwpn_prefix);
1187
1188 reset_hw_out:
1189         return status;
1190 }
1191
1192 /**
1193  *  ixgbe_reinit_fdir_tables_82599 - Reinitialize Flow Director tables.
1194  *  @hw: pointer to hardware structure
1195  **/
1196 s32 ixgbe_reinit_fdir_tables_82599(struct ixgbe_hw *hw)
1197 {
1198         int i;
1199         u32 fdirctrl = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
1200         fdirctrl &= ~IXGBE_FDIRCTRL_INIT_DONE;
1201
1202         DEBUGFUNC("ixgbe_reinit_fdir_tables_82599");
1203
1204         /*
1205          * Before starting reinitialization process,
1206          * FDIRCMD.CMD must be zero.
1207          */
1208         for (i = 0; i < IXGBE_FDIRCMD_CMD_POLL; i++) {
1209                 if (!(IXGBE_READ_REG(hw, IXGBE_FDIRCMD) &
1210                       IXGBE_FDIRCMD_CMD_MASK))
1211                         break;
1212                 usec_delay(10);
1213         }
1214         if (i >= IXGBE_FDIRCMD_CMD_POLL) {
1215                 DEBUGOUT("Flow Director previous command isn't complete, "
1216                          "aborting table re-initialization.\n");
1217                 return IXGBE_ERR_FDIR_REINIT_FAILED;
1218         }
1219
1220         IXGBE_WRITE_REG(hw, IXGBE_FDIRFREE, 0);
1221         IXGBE_WRITE_FLUSH(hw);
1222         /*
1223          * 82599 adapters flow director init flow cannot be restarted,
1224          * Workaround 82599 silicon errata by performing the following steps
1225          * before re-writing the FDIRCTRL control register with the same value.
1226          * - write 1 to bit 8 of FDIRCMD register &
1227          * - write 0 to bit 8 of FDIRCMD register
1228          */
1229         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
1230                         (IXGBE_READ_REG(hw, IXGBE_FDIRCMD) |
1231                          IXGBE_FDIRCMD_CLEARHT));
1232         IXGBE_WRITE_FLUSH(hw);
1233         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
1234                         (IXGBE_READ_REG(hw, IXGBE_FDIRCMD) &
1235                          ~IXGBE_FDIRCMD_CLEARHT));
1236         IXGBE_WRITE_FLUSH(hw);
1237         /*
1238          * Clear FDIR Hash register to clear any leftover hashes
1239          * waiting to be programmed.
1240          */
1241         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, 0x00);
1242         IXGBE_WRITE_FLUSH(hw);
1243
1244         IXGBE_WRITE_REG(hw, IXGBE_FDIRCTRL, fdirctrl);
1245         IXGBE_WRITE_FLUSH(hw);
1246
1247         /* Poll init-done after we write FDIRCTRL register */
1248         for (i = 0; i < IXGBE_FDIR_INIT_DONE_POLL; i++) {
1249                 if (IXGBE_READ_REG(hw, IXGBE_FDIRCTRL) &
1250                                    IXGBE_FDIRCTRL_INIT_DONE)
1251                         break;
1252                 msec_delay(1);
1253         }
1254         if (i >= IXGBE_FDIR_INIT_DONE_POLL) {
1255                 DEBUGOUT("Flow Director Signature poll time exceeded!\n");
1256                 return IXGBE_ERR_FDIR_REINIT_FAILED;
1257         }
1258
1259         /* Clear FDIR statistics registers (read to clear) */
1260         IXGBE_READ_REG(hw, IXGBE_FDIRUSTAT);
1261         IXGBE_READ_REG(hw, IXGBE_FDIRFSTAT);
1262         IXGBE_READ_REG(hw, IXGBE_FDIRMATCH);
1263         IXGBE_READ_REG(hw, IXGBE_FDIRMISS);
1264         IXGBE_READ_REG(hw, IXGBE_FDIRLEN);
1265
1266         return IXGBE_SUCCESS;
1267 }
1268
1269 /**
1270  *  ixgbe_fdir_enable_82599 - Initialize Flow Director control registers
1271  *  @hw: pointer to hardware structure
1272  *  @fdirctrl: value to write to flow director control register
1273  **/
1274 STATIC void ixgbe_fdir_enable_82599(struct ixgbe_hw *hw, u32 fdirctrl)
1275 {
1276         int i;
1277
1278         DEBUGFUNC("ixgbe_fdir_enable_82599");
1279
1280         /* Prime the keys for hashing */
1281         IXGBE_WRITE_REG(hw, IXGBE_FDIRHKEY, IXGBE_ATR_BUCKET_HASH_KEY);
1282         IXGBE_WRITE_REG(hw, IXGBE_FDIRSKEY, IXGBE_ATR_SIGNATURE_HASH_KEY);
1283
1284         /*
1285          * Poll init-done after we write the register.  Estimated times:
1286          *      10G: PBALLOC = 11b, timing is 60us
1287          *       1G: PBALLOC = 11b, timing is 600us
1288          *     100M: PBALLOC = 11b, timing is 6ms
1289          *
1290          *     Multiple these timings by 4 if under full Rx load
1291          *
1292          * So we'll poll for IXGBE_FDIR_INIT_DONE_POLL times, sleeping for
1293          * 1 msec per poll time.  If we're at line rate and drop to 100M, then
1294          * this might not finish in our poll time, but we can live with that
1295          * for now.
1296          */
1297         IXGBE_WRITE_REG(hw, IXGBE_FDIRCTRL, fdirctrl);
1298         IXGBE_WRITE_FLUSH(hw);
1299         for (i = 0; i < IXGBE_FDIR_INIT_DONE_POLL; i++) {
1300                 if (IXGBE_READ_REG(hw, IXGBE_FDIRCTRL) &
1301                                    IXGBE_FDIRCTRL_INIT_DONE)
1302                         break;
1303                 msec_delay(1);
1304         }
1305
1306         if (i >= IXGBE_FDIR_INIT_DONE_POLL)
1307                 DEBUGOUT("Flow Director poll time exceeded!\n");
1308 }
1309
1310 /**
1311  *  ixgbe_init_fdir_signature_82599 - Initialize Flow Director signature filters
1312  *  @hw: pointer to hardware structure
1313  *  @fdirctrl: value to write to flow director control register, initially
1314  *           contains just the value of the Rx packet buffer allocation
1315  **/
1316 s32 ixgbe_init_fdir_signature_82599(struct ixgbe_hw *hw, u32 fdirctrl)
1317 {
1318         DEBUGFUNC("ixgbe_init_fdir_signature_82599");
1319
1320         /*
1321          * Continue setup of fdirctrl register bits:
1322          *  Move the flexible bytes to use the ethertype - shift 6 words
1323          *  Set the maximum length per hash bucket to 0xA filters
1324          *  Send interrupt when 64 filters are left
1325          */
1326         fdirctrl |= (0x6 << IXGBE_FDIRCTRL_FLEX_SHIFT) |
1327                     (0xA << IXGBE_FDIRCTRL_MAX_LENGTH_SHIFT) |
1328                     (4 << IXGBE_FDIRCTRL_FULL_THRESH_SHIFT);
1329
1330         /* write hashes and fdirctrl register, poll for completion */
1331         ixgbe_fdir_enable_82599(hw, fdirctrl);
1332
1333         return IXGBE_SUCCESS;
1334 }
1335
1336 /**
1337  *  ixgbe_init_fdir_perfect_82599 - Initialize Flow Director perfect filters
1338  *  @hw: pointer to hardware structure
1339  *  @fdirctrl: value to write to flow director control register, initially
1340  *           contains just the value of the Rx packet buffer allocation
1341  **/
1342 s32 ixgbe_init_fdir_perfect_82599(struct ixgbe_hw *hw, u32 fdirctrl)
1343 {
1344         DEBUGFUNC("ixgbe_init_fdir_perfect_82599");
1345
1346         /*
1347          * Continue setup of fdirctrl register bits:
1348          *  Turn perfect match filtering on
1349          *  Report hash in RSS field of Rx wb descriptor
1350          *  Initialize the drop queue
1351          *  Move the flexible bytes to use the ethertype - shift 6 words
1352          *  Set the maximum length per hash bucket to 0xA filters
1353          *  Send interrupt when 64 (0x4 * 16) filters are left
1354          */
1355         fdirctrl |= IXGBE_FDIRCTRL_PERFECT_MATCH |
1356                     IXGBE_FDIRCTRL_REPORT_STATUS |
1357                     (IXGBE_FDIR_DROP_QUEUE << IXGBE_FDIRCTRL_DROP_Q_SHIFT) |
1358                     (0x6 << IXGBE_FDIRCTRL_FLEX_SHIFT) |
1359                     (0xA << IXGBE_FDIRCTRL_MAX_LENGTH_SHIFT) |
1360                     (4 << IXGBE_FDIRCTRL_FULL_THRESH_SHIFT);
1361
1362
1363         /* write hashes and fdirctrl register, poll for completion */
1364         ixgbe_fdir_enable_82599(hw, fdirctrl);
1365
1366         return IXGBE_SUCCESS;
1367 }
1368
1369 /*
1370  * These defines allow us to quickly generate all of the necessary instructions
1371  * in the function below by simply calling out IXGBE_COMPUTE_SIG_HASH_ITERATION
1372  * for values 0 through 15
1373  */
1374 #define IXGBE_ATR_COMMON_HASH_KEY \
1375                 (IXGBE_ATR_BUCKET_HASH_KEY & IXGBE_ATR_SIGNATURE_HASH_KEY)
1376 #define IXGBE_COMPUTE_SIG_HASH_ITERATION(_n) \
1377 do { \
1378         u32 n = (_n); \
1379         if (IXGBE_ATR_COMMON_HASH_KEY & (0x01 << n)) \
1380                 common_hash ^= lo_hash_dword >> n; \
1381         else if (IXGBE_ATR_BUCKET_HASH_KEY & (0x01 << n)) \
1382                 bucket_hash ^= lo_hash_dword >> n; \
1383         else if (IXGBE_ATR_SIGNATURE_HASH_KEY & (0x01 << n)) \
1384                 sig_hash ^= lo_hash_dword << (16 - n); \
1385         if (IXGBE_ATR_COMMON_HASH_KEY & (0x01 << (n + 16))) \
1386                 common_hash ^= hi_hash_dword >> n; \
1387         else if (IXGBE_ATR_BUCKET_HASH_KEY & (0x01 << (n + 16))) \
1388                 bucket_hash ^= hi_hash_dword >> n; \
1389         else if (IXGBE_ATR_SIGNATURE_HASH_KEY & (0x01 << (n + 16))) \
1390                 sig_hash ^= hi_hash_dword << (16 - n); \
1391 } while (0);
1392
1393 /**
1394  *  ixgbe_atr_compute_sig_hash_82599 - Compute the signature hash
1395  *  @stream: input bitstream to compute the hash on
1396  *
1397  *  This function is almost identical to the function above but contains
1398  *  several optomizations such as unwinding all of the loops, letting the
1399  *  compiler work out all of the conditional ifs since the keys are static
1400  *  defines, and computing two keys at once since the hashed dword stream
1401  *  will be the same for both keys.
1402  **/
1403 u32 ixgbe_atr_compute_sig_hash_82599(union ixgbe_atr_hash_dword input,
1404                                      union ixgbe_atr_hash_dword common)
1405 {
1406         u32 hi_hash_dword, lo_hash_dword, flow_vm_vlan;
1407         u32 sig_hash = 0, bucket_hash = 0, common_hash = 0;
1408
1409         /* record the flow_vm_vlan bits as they are a key part to the hash */
1410         flow_vm_vlan = IXGBE_NTOHL(input.dword);
1411
1412         /* generate common hash dword */
1413         hi_hash_dword = IXGBE_NTOHL(common.dword);
1414
1415         /* low dword is word swapped version of common */
1416         lo_hash_dword = (hi_hash_dword >> 16) | (hi_hash_dword << 16);
1417
1418         /* apply flow ID/VM pool/VLAN ID bits to hash words */
1419         hi_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan >> 16);
1420
1421         /* Process bits 0 and 16 */
1422         IXGBE_COMPUTE_SIG_HASH_ITERATION(0);
1423
1424         /*
1425          * apply flow ID/VM pool/VLAN ID bits to lo hash dword, we had to
1426          * delay this because bit 0 of the stream should not be processed
1427          * so we do not add the vlan until after bit 0 was processed
1428          */
1429         lo_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan << 16);
1430
1431         /* Process remaining 30 bit of the key */
1432         IXGBE_COMPUTE_SIG_HASH_ITERATION(1);
1433         IXGBE_COMPUTE_SIG_HASH_ITERATION(2);
1434         IXGBE_COMPUTE_SIG_HASH_ITERATION(3);
1435         IXGBE_COMPUTE_SIG_HASH_ITERATION(4);
1436         IXGBE_COMPUTE_SIG_HASH_ITERATION(5);
1437         IXGBE_COMPUTE_SIG_HASH_ITERATION(6);
1438         IXGBE_COMPUTE_SIG_HASH_ITERATION(7);
1439         IXGBE_COMPUTE_SIG_HASH_ITERATION(8);
1440         IXGBE_COMPUTE_SIG_HASH_ITERATION(9);
1441         IXGBE_COMPUTE_SIG_HASH_ITERATION(10);
1442         IXGBE_COMPUTE_SIG_HASH_ITERATION(11);
1443         IXGBE_COMPUTE_SIG_HASH_ITERATION(12);
1444         IXGBE_COMPUTE_SIG_HASH_ITERATION(13);
1445         IXGBE_COMPUTE_SIG_HASH_ITERATION(14);
1446         IXGBE_COMPUTE_SIG_HASH_ITERATION(15);
1447
1448         /* combine common_hash result with signature and bucket hashes */
1449         bucket_hash ^= common_hash;
1450         bucket_hash &= IXGBE_ATR_HASH_MASK;
1451
1452         sig_hash ^= common_hash << 16;
1453         sig_hash &= IXGBE_ATR_HASH_MASK << 16;
1454
1455         /* return completed signature hash */
1456         return sig_hash ^ bucket_hash;
1457 }
1458
1459 /**
1460  *  ixgbe_atr_add_signature_filter_82599 - Adds a signature hash filter
1461  *  @hw: pointer to hardware structure
1462  *  @input: unique input dword
1463  *  @common: compressed common input dword
1464  *  @queue: queue index to direct traffic to
1465  **/
1466 s32 ixgbe_fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
1467                                           union ixgbe_atr_hash_dword input,
1468                                           union ixgbe_atr_hash_dword common,
1469                                           u8 queue)
1470 {
1471         u64  fdirhashcmd;
1472         u32  fdircmd;
1473
1474         DEBUGFUNC("ixgbe_fdir_add_signature_filter_82599");
1475
1476         /*
1477          * Get the flow_type in order to program FDIRCMD properly
1478          * lowest 2 bits are FDIRCMD.L4TYPE, third lowest bit is FDIRCMD.IPV6
1479          * fifth is FDIRCMD.TUNNEL_FILTER
1480          */
1481         switch (input.formatted.flow_type) {
1482         case IXGBE_ATR_FLOW_TYPE_TCPV4:
1483         case IXGBE_ATR_FLOW_TYPE_UDPV4:
1484         case IXGBE_ATR_FLOW_TYPE_SCTPV4:
1485         case IXGBE_ATR_FLOW_TYPE_TCPV6:
1486         case IXGBE_ATR_FLOW_TYPE_UDPV6:
1487         case IXGBE_ATR_FLOW_TYPE_SCTPV6:
1488                 break;
1489         default:
1490                 DEBUGOUT(" Error on flow type input\n");
1491                 return IXGBE_ERR_CONFIG;
1492         }
1493
1494         /* configure FDIRCMD register */
1495         fdircmd = IXGBE_FDIRCMD_CMD_ADD_FLOW | IXGBE_FDIRCMD_FILTER_UPDATE |
1496                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
1497         fdircmd |= input.formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
1498         fdircmd |= (u32)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
1499
1500         /*
1501          * The lower 32-bits of fdirhashcmd is for FDIRHASH, the upper 32-bits
1502          * is for FDIRCMD.  Then do a 64-bit register write from FDIRHASH.
1503          */
1504         fdirhashcmd = (u64)fdircmd << 32;
1505         fdirhashcmd |= ixgbe_atr_compute_sig_hash_82599(input, common);
1506         IXGBE_WRITE_REG64(hw, IXGBE_FDIRHASH, fdirhashcmd);
1507
1508         DEBUGOUT2("Tx Queue=%x hash=%x\n", queue, (u32)fdirhashcmd);
1509
1510         return IXGBE_SUCCESS;
1511 }
1512
1513 #define IXGBE_COMPUTE_BKT_HASH_ITERATION(_n) \
1514 do { \
1515         u32 n = (_n); \
1516         if (IXGBE_ATR_BUCKET_HASH_KEY & (0x01 << n)) \
1517                 bucket_hash ^= lo_hash_dword >> n; \
1518         if (IXGBE_ATR_BUCKET_HASH_KEY & (0x01 << (n + 16))) \
1519                 bucket_hash ^= hi_hash_dword >> n; \
1520 } while (0);
1521
1522 /**
1523  *  ixgbe_atr_compute_perfect_hash_82599 - Compute the perfect filter hash
1524  *  @atr_input: input bitstream to compute the hash on
1525  *  @input_mask: mask for the input bitstream
1526  *
1527  *  This function serves two main purposes.  First it applys the input_mask
1528  *  to the atr_input resulting in a cleaned up atr_input data stream.
1529  *  Secondly it computes the hash and stores it in the bkt_hash field at
1530  *  the end of the input byte stream.  This way it will be available for
1531  *  future use without needing to recompute the hash.
1532  **/
1533 void ixgbe_atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
1534                                           union ixgbe_atr_input *input_mask)
1535 {
1536
1537         u32 hi_hash_dword, lo_hash_dword, flow_vm_vlan;
1538         u32 bucket_hash = 0;
1539
1540         /* Apply masks to input data */
1541         input->dword_stream[0]  &= input_mask->dword_stream[0];
1542         input->dword_stream[1]  &= input_mask->dword_stream[1];
1543         input->dword_stream[2]  &= input_mask->dword_stream[2];
1544         input->dword_stream[3]  &= input_mask->dword_stream[3];
1545         input->dword_stream[4]  &= input_mask->dword_stream[4];
1546         input->dword_stream[5]  &= input_mask->dword_stream[5];
1547         input->dword_stream[6]  &= input_mask->dword_stream[6];
1548         input->dword_stream[7]  &= input_mask->dword_stream[7];
1549         input->dword_stream[8]  &= input_mask->dword_stream[8];
1550         input->dword_stream[9]  &= input_mask->dword_stream[9];
1551         input->dword_stream[10] &= input_mask->dword_stream[10];
1552
1553         /* record the flow_vm_vlan bits as they are a key part to the hash */
1554         flow_vm_vlan = IXGBE_NTOHL(input->dword_stream[0]);
1555
1556         /* generate common hash dword */
1557         hi_hash_dword = IXGBE_NTOHL(input->dword_stream[1] ^
1558                                     input->dword_stream[2] ^
1559                                     input->dword_stream[3] ^
1560                                     input->dword_stream[4] ^
1561                                     input->dword_stream[5] ^
1562                                     input->dword_stream[6] ^
1563                                     input->dword_stream[7] ^
1564                                     input->dword_stream[8] ^
1565                                     input->dword_stream[9] ^
1566                                     input->dword_stream[10]);
1567
1568         /* low dword is word swapped version of common */
1569         lo_hash_dword = (hi_hash_dword >> 16) | (hi_hash_dword << 16);
1570
1571         /* apply flow ID/VM pool/VLAN ID bits to hash words */
1572         hi_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan >> 16);
1573
1574         /* Process bits 0 and 16 */
1575         IXGBE_COMPUTE_BKT_HASH_ITERATION(0);
1576
1577         /*
1578          * apply flow ID/VM pool/VLAN ID bits to lo hash dword, we had to
1579          * delay this because bit 0 of the stream should not be processed
1580          * so we do not add the vlan until after bit 0 was processed
1581          */
1582         lo_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan << 16);
1583
1584         /* Process remaining 30 bit of the key */
1585         IXGBE_COMPUTE_BKT_HASH_ITERATION(1);
1586         IXGBE_COMPUTE_BKT_HASH_ITERATION(2);
1587         IXGBE_COMPUTE_BKT_HASH_ITERATION(3);
1588         IXGBE_COMPUTE_BKT_HASH_ITERATION(4);
1589         IXGBE_COMPUTE_BKT_HASH_ITERATION(5);
1590         IXGBE_COMPUTE_BKT_HASH_ITERATION(6);
1591         IXGBE_COMPUTE_BKT_HASH_ITERATION(7);
1592         IXGBE_COMPUTE_BKT_HASH_ITERATION(8);
1593         IXGBE_COMPUTE_BKT_HASH_ITERATION(9);
1594         IXGBE_COMPUTE_BKT_HASH_ITERATION(10);
1595         IXGBE_COMPUTE_BKT_HASH_ITERATION(11);
1596         IXGBE_COMPUTE_BKT_HASH_ITERATION(12);
1597         IXGBE_COMPUTE_BKT_HASH_ITERATION(13);
1598         IXGBE_COMPUTE_BKT_HASH_ITERATION(14);
1599         IXGBE_COMPUTE_BKT_HASH_ITERATION(15);
1600
1601         /*
1602          * Limit hash to 13 bits since max bucket count is 8K.
1603          * Store result at the end of the input stream.
1604          */
1605         input->formatted.bkt_hash = bucket_hash & 0x1FFF;
1606 }
1607
1608 /**
1609  *  ixgbe_get_fdirtcpm_82599 - generate a tcp port from atr_input_masks
1610  *  @input_mask: mask to be bit swapped
1611  *
1612  *  The source and destination port masks for flow director are bit swapped
1613  *  in that bit 15 effects bit 0, 14 effects 1, 13, 2 etc.  In order to
1614  *  generate a correctly swapped value we need to bit swap the mask and that
1615  *  is what is accomplished by this function.
1616  **/
1617 STATIC u32 ixgbe_get_fdirtcpm_82599(union ixgbe_atr_input *input_mask)
1618 {
1619         u32 mask = IXGBE_NTOHS(input_mask->formatted.dst_port);
1620         mask <<= IXGBE_FDIRTCPM_DPORTM_SHIFT;
1621         mask |= IXGBE_NTOHS(input_mask->formatted.src_port);
1622         mask = ((mask & 0x55555555) << 1) | ((mask & 0xAAAAAAAA) >> 1);
1623         mask = ((mask & 0x33333333) << 2) | ((mask & 0xCCCCCCCC) >> 2);
1624         mask = ((mask & 0x0F0F0F0F) << 4) | ((mask & 0xF0F0F0F0) >> 4);
1625         return ((mask & 0x00FF00FF) << 8) | ((mask & 0xFF00FF00) >> 8);
1626 }
1627
1628 /*
1629  * These two macros are meant to address the fact that we have registers
1630  * that are either all or in part big-endian.  As a result on big-endian
1631  * systems we will end up byte swapping the value to little-endian before
1632  * it is byte swapped again and written to the hardware in the original
1633  * big-endian format.
1634  */
1635 #define IXGBE_STORE_AS_BE32(_value) \
1636         (((u32)(_value) >> 24) | (((u32)(_value) & 0x00FF0000) >> 8) | \
1637          (((u32)(_value) & 0x0000FF00) << 8) | ((u32)(_value) << 24))
1638
1639 #define IXGBE_WRITE_REG_BE32(a, reg, value) \
1640         IXGBE_WRITE_REG((a), (reg), IXGBE_STORE_AS_BE32(IXGBE_NTOHL(value)))
1641
1642 #define IXGBE_STORE_AS_BE16(_value) \
1643         IXGBE_NTOHS(((u16)(_value) >> 8) | ((u16)(_value) << 8))
1644
1645 s32 ixgbe_fdir_set_input_mask_82599(struct ixgbe_hw *hw,
1646                                     union ixgbe_atr_input *input_mask)
1647 {
1648         /* mask IPv6 since it is currently not supported */
1649         u32 fdirm = IXGBE_FDIRM_DIPv6;
1650         u32 fdirtcpm;
1651         DEBUGFUNC("ixgbe_fdir_set_atr_input_mask_82599");
1652
1653         /*
1654          * Program the relevant mask registers.  If src/dst_port or src/dst_addr
1655          * are zero, then assume a full mask for that field.  Also assume that
1656          * a VLAN of 0 is unspecified, so mask that out as well.  L4type
1657          * cannot be masked out in this implementation.
1658          *
1659          * This also assumes IPv4 only.  IPv6 masking isn't supported at this
1660          * point in time.
1661          */
1662
1663         /* verify bucket hash is cleared on hash generation */
1664         if (input_mask->formatted.bkt_hash)
1665                 DEBUGOUT(" bucket hash should always be 0 in mask\n");
1666
1667         /* Program FDIRM and verify partial masks */
1668         switch (input_mask->formatted.vm_pool & 0x7F) {
1669         case 0x0:
1670                 fdirm |= IXGBE_FDIRM_POOL;
1671         case 0x7F:
1672                 break;
1673         default:
1674                 DEBUGOUT(" Error on vm pool mask\n");
1675                 return IXGBE_ERR_CONFIG;
1676         }
1677
1678         switch (input_mask->formatted.flow_type & IXGBE_ATR_L4TYPE_MASK) {
1679         case 0x0:
1680                 fdirm |= IXGBE_FDIRM_L4P;
1681                 if (input_mask->formatted.dst_port ||
1682                     input_mask->formatted.src_port) {
1683                         DEBUGOUT(" Error on src/dst port mask\n");
1684                         return IXGBE_ERR_CONFIG;
1685                 }
1686         case IXGBE_ATR_L4TYPE_MASK:
1687                 break;
1688         default:
1689                 DEBUGOUT(" Error on flow type mask\n");
1690                 return IXGBE_ERR_CONFIG;
1691         }
1692
1693         switch (IXGBE_NTOHS(input_mask->formatted.vlan_id) & 0xEFFF) {
1694         case 0x0000:
1695                 /* mask VLAN ID, fall through to mask VLAN priority */
1696                 fdirm |= IXGBE_FDIRM_VLANID;
1697         case 0x0FFF:
1698                 /* mask VLAN priority */
1699                 fdirm |= IXGBE_FDIRM_VLANP;
1700                 break;
1701         case 0xE000:
1702                 /* mask VLAN ID only, fall through */
1703                 fdirm |= IXGBE_FDIRM_VLANID;
1704         case 0xEFFF:
1705                 /* no VLAN fields masked */
1706                 break;
1707         default:
1708                 DEBUGOUT(" Error on VLAN mask\n");
1709                 return IXGBE_ERR_CONFIG;
1710         }
1711
1712         switch (input_mask->formatted.flex_bytes & 0xFFFF) {
1713         case 0x0000:
1714                 /* Mask Flex Bytes, fall through */
1715                 fdirm |= IXGBE_FDIRM_FLEX;
1716         case 0xFFFF:
1717                 break;
1718         default:
1719                 DEBUGOUT(" Error on flexible byte mask\n");
1720                 return IXGBE_ERR_CONFIG;
1721         }
1722
1723
1724         /* Now mask VM pool and destination IPv6 - bits 5 and 2 */
1725         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
1726
1727         /* store the TCP/UDP port masks, bit reversed from port layout */
1728         fdirtcpm = ixgbe_get_fdirtcpm_82599(input_mask);
1729
1730         /* write both the same so that UDP and TCP use the same mask */
1731         IXGBE_WRITE_REG(hw, IXGBE_FDIRTCPM, ~fdirtcpm);
1732         IXGBE_WRITE_REG(hw, IXGBE_FDIRUDPM, ~fdirtcpm);
1733
1734         /* store source and destination IP masks (big-enian) */
1735         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIP4M,
1736                              ~input_mask->formatted.src_ip[0]);
1737         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRDIP4M,
1738                              ~input_mask->formatted.dst_ip[0]);
1739
1740         return IXGBE_SUCCESS;
1741 }
1742
1743 s32 ixgbe_fdir_write_perfect_filter_82599(struct ixgbe_hw *hw,
1744                                           union ixgbe_atr_input *input,
1745                                           u16 soft_id, u8 queue)
1746 {
1747         u32 fdirport, fdirvlan, fdirhash, fdircmd;
1748
1749         DEBUGFUNC("ixgbe_fdir_write_perfect_filter_82599");
1750
1751         /* currently IPv6 is not supported, must be programmed with 0 */
1752         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(0),
1753                              input->formatted.src_ip[0]);
1754         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(1),
1755                              input->formatted.src_ip[1]);
1756         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(2),
1757                              input->formatted.src_ip[2]);
1758
1759         /* record the source address (big-endian) */
1760         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPSA, input->formatted.src_ip[0]);
1761
1762         /* record the first 32 bits of the destination address (big-endian) */
1763         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPDA, input->formatted.dst_ip[0]);
1764
1765         /* record source and destination port (little-endian)*/
1766         fdirport = IXGBE_NTOHS(input->formatted.dst_port);
1767         fdirport <<= IXGBE_FDIRPORT_DESTINATION_SHIFT;
1768         fdirport |= IXGBE_NTOHS(input->formatted.src_port);
1769         IXGBE_WRITE_REG(hw, IXGBE_FDIRPORT, fdirport);
1770
1771         /* record vlan (little-endian) and flex_bytes(big-endian) */
1772         fdirvlan = IXGBE_STORE_AS_BE16(input->formatted.flex_bytes);
1773         fdirvlan <<= IXGBE_FDIRVLAN_FLEX_SHIFT;
1774         fdirvlan |= IXGBE_NTOHS(input->formatted.vlan_id);
1775         IXGBE_WRITE_REG(hw, IXGBE_FDIRVLAN, fdirvlan);
1776
1777
1778         /* configure FDIRHASH register */
1779         fdirhash = input->formatted.bkt_hash;
1780         fdirhash |= soft_id << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
1781         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
1782
1783         /*
1784          * flush all previous writes to make certain registers are
1785          * programmed prior to issuing the command
1786          */
1787         IXGBE_WRITE_FLUSH(hw);
1788
1789         /* configure FDIRCMD register */
1790         fdircmd = IXGBE_FDIRCMD_CMD_ADD_FLOW | IXGBE_FDIRCMD_FILTER_UPDATE |
1791                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
1792         if (queue == IXGBE_FDIR_DROP_QUEUE)
1793                 fdircmd |= IXGBE_FDIRCMD_DROP;
1794         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
1795         fdircmd |= (u32)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
1796         fdircmd |= (u32)input->formatted.vm_pool << IXGBE_FDIRCMD_VT_POOL_SHIFT;
1797
1798         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
1799
1800         return IXGBE_SUCCESS;
1801 }
1802
1803 s32 ixgbe_fdir_erase_perfect_filter_82599(struct ixgbe_hw *hw,
1804                                           union ixgbe_atr_input *input,
1805                                           u16 soft_id)
1806 {
1807         u32 fdirhash;
1808         u32 fdircmd = 0;
1809         u32 retry_count;
1810         s32 err = IXGBE_SUCCESS;
1811
1812         /* configure FDIRHASH register */
1813         fdirhash = input->formatted.bkt_hash;
1814         fdirhash |= soft_id << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
1815         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
1816
1817         /* flush hash to HW */
1818         IXGBE_WRITE_FLUSH(hw);
1819
1820         /* Query if filter is present */
1821         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, IXGBE_FDIRCMD_CMD_QUERY_REM_FILT);
1822
1823         for (retry_count = 10; retry_count; retry_count--) {
1824                 /* allow 10us for query to process */
1825                 usec_delay(10);
1826                 /* verify query completed successfully */
1827                 fdircmd = IXGBE_READ_REG(hw, IXGBE_FDIRCMD);
1828                 if (!(fdircmd & IXGBE_FDIRCMD_CMD_MASK))
1829                         break;
1830         }
1831
1832         if (!retry_count)
1833                 err = IXGBE_ERR_FDIR_REINIT_FAILED;
1834
1835         /* if filter exists in hardware then remove it */
1836         if (fdircmd & IXGBE_FDIRCMD_FILTER_VALID) {
1837                 IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
1838                 IXGBE_WRITE_FLUSH(hw);
1839                 IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
1840                                 IXGBE_FDIRCMD_CMD_REMOVE_FLOW);
1841         }
1842
1843         return err;
1844 }
1845
1846 /**
1847  *  ixgbe_fdir_add_perfect_filter_82599 - Adds a perfect filter
1848  *  @hw: pointer to hardware structure
1849  *  @input: input bitstream
1850  *  @input_mask: mask for the input bitstream
1851  *  @soft_id: software index for the filters
1852  *  @queue: queue index to direct traffic to
1853  *
1854  *  Note that the caller to this function must lock before calling, since the
1855  *  hardware writes must be protected from one another.
1856  **/
1857 s32 ixgbe_fdir_add_perfect_filter_82599(struct ixgbe_hw *hw,
1858                                         union ixgbe_atr_input *input,
1859                                         union ixgbe_atr_input *input_mask,
1860                                         u16 soft_id, u8 queue)
1861 {
1862         s32 err = IXGBE_ERR_CONFIG;
1863
1864         DEBUGFUNC("ixgbe_fdir_add_perfect_filter_82599");
1865
1866         /*
1867          * Check flow_type formatting, and bail out before we touch the hardware
1868          * if there's a configuration issue
1869          */
1870         switch (input->formatted.flow_type) {
1871         case IXGBE_ATR_FLOW_TYPE_IPV4:
1872                 input_mask->formatted.flow_type = IXGBE_ATR_L4TYPE_IPV6_MASK;
1873                 if (input->formatted.dst_port || input->formatted.src_port) {
1874                         DEBUGOUT(" Error on src/dst port\n");
1875                         return IXGBE_ERR_CONFIG;
1876                 }
1877                 break;
1878         case IXGBE_ATR_FLOW_TYPE_SCTPV4:
1879                 if (input->formatted.dst_port || input->formatted.src_port) {
1880                         DEBUGOUT(" Error on src/dst port\n");
1881                         return IXGBE_ERR_CONFIG;
1882                 }
1883         case IXGBE_ATR_FLOW_TYPE_TCPV4:
1884         case IXGBE_ATR_FLOW_TYPE_UDPV4:
1885                 input_mask->formatted.flow_type = IXGBE_ATR_L4TYPE_IPV6_MASK |
1886                                                   IXGBE_ATR_L4TYPE_MASK;
1887                 break;
1888         default:
1889                 DEBUGOUT(" Error on flow type input\n");
1890                 return err;
1891         }
1892
1893         /* program input mask into the HW */
1894         err = ixgbe_fdir_set_input_mask_82599(hw, input_mask);
1895         if (err)
1896                 return err;
1897
1898         /* apply mask and compute/store hash */
1899         ixgbe_atr_compute_perfect_hash_82599(input, input_mask);
1900
1901         /* program filters to filter memory */
1902         return ixgbe_fdir_write_perfect_filter_82599(hw, input,
1903                                                      soft_id, queue);
1904 }
1905
1906 /**
1907  *  ixgbe_read_analog_reg8_82599 - Reads 8 bit Omer analog register
1908  *  @hw: pointer to hardware structure
1909  *  @reg: analog register to read
1910  *  @val: read value
1911  *
1912  *  Performs read operation to Omer analog register specified.
1913  **/
1914 s32 ixgbe_read_analog_reg8_82599(struct ixgbe_hw *hw, u32 reg, u8 *val)
1915 {
1916         u32  core_ctl;
1917
1918         DEBUGFUNC("ixgbe_read_analog_reg8_82599");
1919
1920         IXGBE_WRITE_REG(hw, IXGBE_CORECTL, IXGBE_CORECTL_WRITE_CMD |
1921                         (reg << 8));
1922         IXGBE_WRITE_FLUSH(hw);
1923         usec_delay(10);
1924         core_ctl = IXGBE_READ_REG(hw, IXGBE_CORECTL);
1925         *val = (u8)core_ctl;
1926
1927         return IXGBE_SUCCESS;
1928 }
1929
1930 /**
1931  *  ixgbe_write_analog_reg8_82599 - Writes 8 bit Omer analog register
1932  *  @hw: pointer to hardware structure
1933  *  @reg: atlas register to write
1934  *  @val: value to write
1935  *
1936  *  Performs write operation to Omer analog register specified.
1937  **/
1938 s32 ixgbe_write_analog_reg8_82599(struct ixgbe_hw *hw, u32 reg, u8 val)
1939 {
1940         u32  core_ctl;
1941
1942         DEBUGFUNC("ixgbe_write_analog_reg8_82599");
1943
1944         core_ctl = (reg << 8) | val;
1945         IXGBE_WRITE_REG(hw, IXGBE_CORECTL, core_ctl);
1946         IXGBE_WRITE_FLUSH(hw);
1947         usec_delay(10);
1948
1949         return IXGBE_SUCCESS;
1950 }
1951
1952 /**
1953  *  ixgbe_start_hw_82599 - Prepare hardware for Tx/Rx
1954  *  @hw: pointer to hardware structure
1955  *
1956  *  Starts the hardware using the generic start_hw function
1957  *  and the generation start_hw function.
1958  *  Then performs revision-specific operations, if any.
1959  **/
1960 s32 ixgbe_start_hw_82599(struct ixgbe_hw *hw)
1961 {
1962         s32 ret_val = IXGBE_SUCCESS;
1963
1964         DEBUGFUNC("ixgbe_start_hw_82599");
1965
1966         ret_val = ixgbe_start_hw_generic(hw);
1967         if (ret_val != IXGBE_SUCCESS)
1968                 goto out;
1969
1970         ret_val = ixgbe_start_hw_gen2(hw);
1971         if (ret_val != IXGBE_SUCCESS)
1972                 goto out;
1973
1974         /* We need to run link autotry after the driver loads */
1975         hw->mac.autotry_restart = true;
1976
1977         if (ret_val == IXGBE_SUCCESS)
1978                 ret_val = ixgbe_verify_fw_version_82599(hw);
1979 out:
1980         return ret_val;
1981 }
1982
1983 /**
1984  *  ixgbe_identify_phy_82599 - Get physical layer module
1985  *  @hw: pointer to hardware structure
1986  *
1987  *  Determines the physical layer module found on the current adapter.
1988  *  If PHY already detected, maintains current PHY type in hw struct,
1989  *  otherwise executes the PHY detection routine.
1990  **/
1991 s32 ixgbe_identify_phy_82599(struct ixgbe_hw *hw)
1992 {
1993         s32 status;
1994
1995         DEBUGFUNC("ixgbe_identify_phy_82599");
1996
1997         /* Detect PHY if not unknown - returns success if already detected. */
1998         status = ixgbe_identify_phy_generic(hw);
1999         if (status != IXGBE_SUCCESS) {
2000                 /* 82599 10GBASE-T requires an external PHY */
2001                 if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper)
2002                         goto out;
2003                 else
2004                         status = ixgbe_identify_module_generic(hw);
2005         }
2006
2007         /* Set PHY type none if no PHY detected */
2008         if (hw->phy.type == ixgbe_phy_unknown) {
2009                 hw->phy.type = ixgbe_phy_none;
2010                 status = IXGBE_SUCCESS;
2011         }
2012
2013         /* Return error if SFP module has been detected but is not supported */
2014         if (hw->phy.type == ixgbe_phy_sfp_unsupported)
2015                 status = IXGBE_ERR_SFP_NOT_SUPPORTED;
2016
2017 out:
2018         return status;
2019 }
2020
2021 /**
2022  *  ixgbe_get_supported_physical_layer_82599 - Returns physical layer type
2023  *  @hw: pointer to hardware structure
2024  *
2025  *  Determines physical layer capabilities of the current configuration.
2026  **/
2027 u32 ixgbe_get_supported_physical_layer_82599(struct ixgbe_hw *hw)
2028 {
2029         u32 physical_layer = IXGBE_PHYSICAL_LAYER_UNKNOWN;
2030         u32 autoc = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2031         u32 autoc2 = IXGBE_READ_REG(hw, IXGBE_AUTOC2);
2032         u32 pma_pmd_10g_serial = autoc2 & IXGBE_AUTOC2_10G_SERIAL_PMA_PMD_MASK;
2033         u32 pma_pmd_10g_parallel = autoc & IXGBE_AUTOC_10G_PMA_PMD_MASK;
2034         u32 pma_pmd_1g = autoc & IXGBE_AUTOC_1G_PMA_PMD_MASK;
2035         u16 ext_ability = 0;
2036         u8 comp_codes_10g = 0;
2037         u8 comp_codes_1g = 0;
2038
2039         DEBUGFUNC("ixgbe_get_support_physical_layer_82599");
2040
2041         hw->phy.ops.identify(hw);
2042
2043         switch (hw->phy.type) {
2044         case ixgbe_phy_tn:
2045         case ixgbe_phy_cu_unknown:
2046                 hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_EXT_ABILITY,
2047                 IXGBE_MDIO_PMA_PMD_DEV_TYPE, &ext_ability);
2048                 if (ext_ability & IXGBE_MDIO_PHY_10GBASET_ABILITY)
2049                         physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_T;
2050                 if (ext_ability & IXGBE_MDIO_PHY_1000BASET_ABILITY)
2051                         physical_layer |= IXGBE_PHYSICAL_LAYER_1000BASE_T;
2052                 if (ext_ability & IXGBE_MDIO_PHY_100BASETX_ABILITY)
2053                         physical_layer |= IXGBE_PHYSICAL_LAYER_100BASE_TX;
2054                 goto out;
2055         default:
2056                 break;
2057         }
2058
2059         switch (autoc & IXGBE_AUTOC_LMS_MASK) {
2060         case IXGBE_AUTOC_LMS_1G_AN:
2061         case IXGBE_AUTOC_LMS_1G_LINK_NO_AN:
2062                 if (pma_pmd_1g == IXGBE_AUTOC_1G_KX_BX) {
2063                         physical_layer = IXGBE_PHYSICAL_LAYER_1000BASE_KX |
2064                             IXGBE_PHYSICAL_LAYER_1000BASE_BX;
2065                         goto out;
2066                 } else
2067                         /* SFI mode so read SFP module */
2068                         goto sfp_check;
2069                 break;
2070         case IXGBE_AUTOC_LMS_10G_LINK_NO_AN:
2071                 if (pma_pmd_10g_parallel == IXGBE_AUTOC_10G_CX4)
2072                         physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_CX4;
2073                 else if (pma_pmd_10g_parallel == IXGBE_AUTOC_10G_KX4)
2074                         physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_KX4;
2075                 else if (pma_pmd_10g_parallel == IXGBE_AUTOC_10G_XAUI)
2076                         physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_XAUI;
2077                 goto out;
2078                 break;
2079         case IXGBE_AUTOC_LMS_10G_SERIAL:
2080                 if (pma_pmd_10g_serial == IXGBE_AUTOC2_10G_KR) {
2081                         physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_KR;
2082                         goto out;
2083                 } else if (pma_pmd_10g_serial == IXGBE_AUTOC2_10G_SFI)
2084                         goto sfp_check;
2085                 break;
2086         case IXGBE_AUTOC_LMS_KX4_KX_KR:
2087         case IXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN:
2088                 if (autoc & IXGBE_AUTOC_KX_SUPP)
2089                         physical_layer |= IXGBE_PHYSICAL_LAYER_1000BASE_KX;
2090                 if (autoc & IXGBE_AUTOC_KX4_SUPP)
2091                         physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_KX4;
2092                 if (autoc & IXGBE_AUTOC_KR_SUPP)
2093                         physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_KR;
2094                 goto out;
2095                 break;
2096         default:
2097                 goto out;
2098                 break;
2099         }
2100
2101 sfp_check:
2102         /* SFP check must be done last since DA modules are sometimes used to
2103          * test KR mode -  we need to id KR mode correctly before SFP module.
2104          * Call identify_sfp because the pluggable module may have changed */
2105         hw->phy.ops.identify_sfp(hw);
2106         if (hw->phy.sfp_type == ixgbe_sfp_type_not_present)
2107                 goto out;
2108
2109         switch (hw->phy.type) {
2110         case ixgbe_phy_sfp_passive_tyco:
2111         case ixgbe_phy_sfp_passive_unknown:
2112                 physical_layer = IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU;
2113                 break;
2114         case ixgbe_phy_sfp_ftl_active:
2115         case ixgbe_phy_sfp_active_unknown:
2116                 physical_layer = IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA;
2117                 break;
2118         case ixgbe_phy_sfp_avago:
2119         case ixgbe_phy_sfp_ftl:
2120         case ixgbe_phy_sfp_intel:
2121         case ixgbe_phy_sfp_unknown:
2122                 hw->phy.ops.read_i2c_eeprom(hw,
2123                       IXGBE_SFF_1GBE_COMP_CODES, &comp_codes_1g);
2124                 hw->phy.ops.read_i2c_eeprom(hw,
2125                       IXGBE_SFF_10GBE_COMP_CODES, &comp_codes_10g);
2126                 if (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)
2127                         physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_SR;
2128                 else if (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)
2129                         physical_layer = IXGBE_PHYSICAL_LAYER_10GBASE_LR;
2130                 else if (comp_codes_1g & IXGBE_SFF_1GBASET_CAPABLE)
2131                         physical_layer = IXGBE_PHYSICAL_LAYER_1000BASE_T;
2132                 else if (comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE)
2133                         physical_layer = IXGBE_PHYSICAL_LAYER_1000BASE_SX;
2134                 break;
2135         default:
2136                 break;
2137         }
2138
2139 out:
2140         return physical_layer;
2141 }
2142
2143 /**
2144  *  ixgbe_enable_rx_dma_82599 - Enable the Rx DMA unit on 82599
2145  *  @hw: pointer to hardware structure
2146  *  @regval: register value to write to RXCTRL
2147  *
2148  *  Enables the Rx DMA unit for 82599
2149  **/
2150 s32 ixgbe_enable_rx_dma_82599(struct ixgbe_hw *hw, u32 regval)
2151 {
2152
2153         DEBUGFUNC("ixgbe_enable_rx_dma_82599");
2154
2155         /*
2156          * Workaround for 82599 silicon errata when enabling the Rx datapath.
2157          * If traffic is incoming before we enable the Rx unit, it could hang
2158          * the Rx DMA unit.  Therefore, make sure the security engine is
2159          * completely disabled prior to enabling the Rx unit.
2160          */
2161
2162         hw->mac.ops.disable_sec_rx_path(hw);
2163
2164         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, regval);
2165
2166         hw->mac.ops.enable_sec_rx_path(hw);
2167
2168         return IXGBE_SUCCESS;
2169 }
2170
2171 /**
2172  *  ixgbe_verify_fw_version_82599 - verify fw version for 82599
2173  *  @hw: pointer to hardware structure
2174  *
2175  *  Verifies that installed the firmware version is 0.6 or higher
2176  *  for SFI devices. All 82599 SFI devices should have version 0.6 or higher.
2177  *
2178  *  Returns IXGBE_ERR_EEPROM_VERSION if the FW is not present or
2179  *  if the FW version is not supported.
2180  **/
2181 STATIC s32 ixgbe_verify_fw_version_82599(struct ixgbe_hw *hw)
2182 {
2183         s32 status = IXGBE_ERR_EEPROM_VERSION;
2184         u16 fw_offset, fw_ptp_cfg_offset;
2185         u16 fw_version;
2186
2187         DEBUGFUNC("ixgbe_verify_fw_version_82599");
2188
2189         /* firmware check is only necessary for SFI devices */
2190         if (hw->phy.media_type != ixgbe_media_type_fiber) {
2191                 status = IXGBE_SUCCESS;
2192                 goto fw_version_out;
2193         }
2194
2195         /* get the offset to the Firmware Module block */
2196         if (hw->eeprom.ops.read(hw, IXGBE_FW_PTR, &fw_offset)) {
2197                 ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
2198                               "eeprom read at offset %d failed", IXGBE_FW_PTR);
2199                 return IXGBE_ERR_EEPROM_VERSION;
2200         }
2201
2202         if ((fw_offset == 0) || (fw_offset == 0xFFFF))
2203                 goto fw_version_out;
2204
2205         /* get the offset to the Pass Through Patch Configuration block */
2206         if (hw->eeprom.ops.read(hw, (fw_offset +
2207                                  IXGBE_FW_PASSTHROUGH_PATCH_CONFIG_PTR),
2208                                  &fw_ptp_cfg_offset)) {
2209                 ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
2210                               "eeprom read at offset %d failed",
2211                               fw_offset +
2212                               IXGBE_FW_PASSTHROUGH_PATCH_CONFIG_PTR);
2213                 return IXGBE_ERR_EEPROM_VERSION;
2214         }
2215
2216         if ((fw_ptp_cfg_offset == 0) || (fw_ptp_cfg_offset == 0xFFFF))
2217                 goto fw_version_out;
2218
2219         /* get the firmware version */
2220         if (hw->eeprom.ops.read(hw, (fw_ptp_cfg_offset +
2221                             IXGBE_FW_PATCH_VERSION_4), &fw_version)) {
2222                 ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
2223                               "eeprom read at offset %d failed",
2224                               fw_ptp_cfg_offset + IXGBE_FW_PATCH_VERSION_4);
2225                 return IXGBE_ERR_EEPROM_VERSION;
2226         }
2227
2228         if (fw_version > 0x5)
2229                 status = IXGBE_SUCCESS;
2230
2231 fw_version_out:
2232         return status;
2233 }
2234
2235 /**
2236  *  ixgbe_verify_lesm_fw_enabled_82599 - Checks LESM FW module state.
2237  *  @hw: pointer to hardware structure
2238  *
2239  *  Returns true if the LESM FW module is present and enabled. Otherwise
2240  *  returns false. Smart Speed must be disabled if LESM FW module is enabled.
2241  **/
2242 bool ixgbe_verify_lesm_fw_enabled_82599(struct ixgbe_hw *hw)
2243 {
2244         bool lesm_enabled = false;
2245         u16 fw_offset, fw_lesm_param_offset, fw_lesm_state;
2246         s32 status;
2247
2248         DEBUGFUNC("ixgbe_verify_lesm_fw_enabled_82599");
2249
2250         /* get the offset to the Firmware Module block */
2251         status = hw->eeprom.ops.read(hw, IXGBE_FW_PTR, &fw_offset);
2252
2253         if ((status != IXGBE_SUCCESS) ||
2254             (fw_offset == 0) || (fw_offset == 0xFFFF))
2255                 goto out;
2256
2257         /* get the offset to the LESM Parameters block */
2258         status = hw->eeprom.ops.read(hw, (fw_offset +
2259                                      IXGBE_FW_LESM_PARAMETERS_PTR),
2260                                      &fw_lesm_param_offset);
2261
2262         if ((status != IXGBE_SUCCESS) ||
2263             (fw_lesm_param_offset == 0) || (fw_lesm_param_offset == 0xFFFF))
2264                 goto out;
2265
2266         /* get the lesm state word */
2267         status = hw->eeprom.ops.read(hw, (fw_lesm_param_offset +
2268                                      IXGBE_FW_LESM_STATE_1),
2269                                      &fw_lesm_state);
2270
2271         if ((status == IXGBE_SUCCESS) &&
2272             (fw_lesm_state & IXGBE_FW_LESM_STATE_ENABLED))
2273                 lesm_enabled = true;
2274
2275 out:
2276         return lesm_enabled;
2277 }
2278
2279 /**
2280  *  ixgbe_read_eeprom_buffer_82599 - Read EEPROM word(s) using
2281  *  fastest available method
2282  *
2283  *  @hw: pointer to hardware structure
2284  *  @offset: offset of  word in EEPROM to read
2285  *  @words: number of words
2286  *  @data: word(s) read from the EEPROM
2287  *
2288  *  Retrieves 16 bit word(s) read from EEPROM
2289  **/
2290 STATIC s32 ixgbe_read_eeprom_buffer_82599(struct ixgbe_hw *hw, u16 offset,
2291                                           u16 words, u16 *data)
2292 {
2293         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
2294         s32 ret_val = IXGBE_ERR_CONFIG;
2295
2296         DEBUGFUNC("ixgbe_read_eeprom_buffer_82599");
2297
2298         /*
2299          * If EEPROM is detected and can be addressed using 14 bits,
2300          * use EERD otherwise use bit bang
2301          */
2302         if ((eeprom->type == ixgbe_eeprom_spi) &&
2303             (offset + (words - 1) <= IXGBE_EERD_MAX_ADDR))
2304                 ret_val = ixgbe_read_eerd_buffer_generic(hw, offset, words,
2305                                                          data);
2306         else
2307                 ret_val = ixgbe_read_eeprom_buffer_bit_bang_generic(hw, offset,
2308                                                                     words,
2309                                                                     data);
2310
2311         return ret_val;
2312 }
2313
2314 /**
2315  *  ixgbe_read_eeprom_82599 - Read EEPROM word using
2316  *  fastest available method
2317  *
2318  *  @hw: pointer to hardware structure
2319  *  @offset: offset of  word in the EEPROM to read
2320  *  @data: word read from the EEPROM
2321  *
2322  *  Reads a 16 bit word from the EEPROM
2323  **/
2324 STATIC s32 ixgbe_read_eeprom_82599(struct ixgbe_hw *hw,
2325                                    u16 offset, u16 *data)
2326 {
2327         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
2328         s32 ret_val = IXGBE_ERR_CONFIG;
2329
2330         DEBUGFUNC("ixgbe_read_eeprom_82599");
2331
2332         /*
2333          * If EEPROM is detected and can be addressed using 14 bits,
2334          * use EERD otherwise use bit bang
2335          */
2336         if ((eeprom->type == ixgbe_eeprom_spi) &&
2337             (offset <= IXGBE_EERD_MAX_ADDR))
2338                 ret_val = ixgbe_read_eerd_generic(hw, offset, data);
2339         else
2340                 ret_val = ixgbe_read_eeprom_bit_bang_generic(hw, offset, data);
2341
2342         return ret_val;
2343 }
2344
2345 /**
2346  * ixgbe_reset_pipeline_82599 - perform pipeline reset
2347  *
2348  *  @hw: pointer to hardware structure
2349  *
2350  * Reset pipeline by asserting Restart_AN together with LMS change to ensure
2351  * full pipeline reset
2352  **/
2353 s32 ixgbe_reset_pipeline_82599(struct ixgbe_hw *hw)
2354 {
2355         s32 i, autoc_reg, autoc2_reg, ret_val;
2356         s32 anlp1_reg = 0;
2357
2358         /* Enable link if disabled in NVM */
2359         autoc2_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC2);
2360         if (autoc2_reg & IXGBE_AUTOC2_LINK_DISABLE_MASK) {
2361                 autoc2_reg &= ~IXGBE_AUTOC2_LINK_DISABLE_MASK;
2362                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC2, autoc2_reg);
2363                 IXGBE_WRITE_FLUSH(hw);
2364         }
2365
2366         autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2367         autoc_reg |= IXGBE_AUTOC_AN_RESTART;
2368         /* Write AUTOC register with toggled LMS[2] bit and Restart_AN */
2369         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg ^ IXGBE_AUTOC_LMS_1G_AN);
2370         /* Wait for AN to leave state 0 */
2371         for (i = 0; i < 10; i++) {
2372                 msec_delay(4);
2373                 anlp1_reg = IXGBE_READ_REG(hw, IXGBE_ANLP1);
2374                 if (anlp1_reg & IXGBE_ANLP1_AN_STATE_MASK)
2375                         break;
2376         }
2377
2378         if (!(anlp1_reg & IXGBE_ANLP1_AN_STATE_MASK)) {
2379                 DEBUGOUT("auto negotiation not completed\n");
2380                 ret_val = IXGBE_ERR_RESET_FAILED;
2381                 goto reset_pipeline_out;
2382         }
2383
2384         ret_val = IXGBE_SUCCESS;
2385
2386 reset_pipeline_out:
2387         /* Write AUTOC register with original LMS field and Restart_AN */
2388         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
2389         IXGBE_WRITE_FLUSH(hw);
2390
2391         return ret_val;
2392 }
2393
2394
2395