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