96456678e25d6aa57ad13f04b21c9c31b808e1fb
[dpdk.git] / drivers / net / ixgbe / base / ixgbe_common.c
1 /*******************************************************************************
2
3 Copyright (c) 2001-2015, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31
32 ***************************************************************************/
33
34 #include "ixgbe_common.h"
35 #include "ixgbe_phy.h"
36 #include "ixgbe_dcb.h"
37 #include "ixgbe_dcb_82599.h"
38 #include "ixgbe_api.h"
39
40 STATIC s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw);
41 STATIC s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw);
42 STATIC void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw);
43 STATIC s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw);
44 STATIC void ixgbe_standby_eeprom(struct ixgbe_hw *hw);
45 STATIC void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
46                                         u16 count);
47 STATIC u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count);
48 STATIC void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
49 STATIC void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
50 STATIC void ixgbe_release_eeprom(struct ixgbe_hw *hw);
51
52 STATIC s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr);
53 STATIC s32 ixgbe_get_san_mac_addr_offset(struct ixgbe_hw *hw,
54                                          u16 *san_mac_offset);
55 STATIC s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
56                                              u16 words, u16 *data);
57 STATIC s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
58                                               u16 words, u16 *data);
59 STATIC s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
60                                                  u16 offset);
61
62 /**
63  *  ixgbe_init_ops_generic - Inits function ptrs
64  *  @hw: pointer to the hardware structure
65  *
66  *  Initialize the function pointers.
67  **/
68 s32 ixgbe_init_ops_generic(struct ixgbe_hw *hw)
69 {
70         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
71         struct ixgbe_mac_info *mac = &hw->mac;
72         u32 eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
73
74         DEBUGFUNC("ixgbe_init_ops_generic");
75
76         /* EEPROM */
77         eeprom->ops.init_params = ixgbe_init_eeprom_params_generic;
78         /* If EEPROM is valid (bit 8 = 1), use EERD otherwise use bit bang */
79         if (eec & IXGBE_EEC_PRES) {
80                 eeprom->ops.read = ixgbe_read_eerd_generic;
81                 eeprom->ops.read_buffer = ixgbe_read_eerd_buffer_generic;
82         } else {
83                 eeprom->ops.read = ixgbe_read_eeprom_bit_bang_generic;
84                 eeprom->ops.read_buffer =
85                                  ixgbe_read_eeprom_buffer_bit_bang_generic;
86         }
87         eeprom->ops.write = ixgbe_write_eeprom_generic;
88         eeprom->ops.write_buffer = ixgbe_write_eeprom_buffer_bit_bang_generic;
89         eeprom->ops.validate_checksum =
90                                       ixgbe_validate_eeprom_checksum_generic;
91         eeprom->ops.update_checksum = ixgbe_update_eeprom_checksum_generic;
92         eeprom->ops.calc_checksum = ixgbe_calc_eeprom_checksum_generic;
93
94         /* MAC */
95         mac->ops.init_hw = ixgbe_init_hw_generic;
96         mac->ops.reset_hw = NULL;
97         mac->ops.start_hw = ixgbe_start_hw_generic;
98         mac->ops.clear_hw_cntrs = ixgbe_clear_hw_cntrs_generic;
99         mac->ops.get_media_type = NULL;
100         mac->ops.get_supported_physical_layer = NULL;
101         mac->ops.enable_rx_dma = ixgbe_enable_rx_dma_generic;
102         mac->ops.get_mac_addr = ixgbe_get_mac_addr_generic;
103         mac->ops.stop_adapter = ixgbe_stop_adapter_generic;
104         mac->ops.get_bus_info = ixgbe_get_bus_info_generic;
105         mac->ops.set_lan_id = ixgbe_set_lan_id_multi_port_pcie;
106         mac->ops.acquire_swfw_sync = ixgbe_acquire_swfw_sync;
107         mac->ops.release_swfw_sync = ixgbe_release_swfw_sync;
108         mac->ops.prot_autoc_read = prot_autoc_read_generic;
109         mac->ops.prot_autoc_write = prot_autoc_write_generic;
110
111         /* LEDs */
112         mac->ops.led_on = ixgbe_led_on_generic;
113         mac->ops.led_off = ixgbe_led_off_generic;
114         mac->ops.blink_led_start = ixgbe_blink_led_start_generic;
115         mac->ops.blink_led_stop = ixgbe_blink_led_stop_generic;
116         mac->ops.init_led_link_act = ixgbe_init_led_link_act_generic;
117
118         /* RAR, Multicast, VLAN */
119         mac->ops.set_rar = ixgbe_set_rar_generic;
120         mac->ops.clear_rar = ixgbe_clear_rar_generic;
121         mac->ops.insert_mac_addr = NULL;
122         mac->ops.set_vmdq = NULL;
123         mac->ops.clear_vmdq = NULL;
124         mac->ops.init_rx_addrs = ixgbe_init_rx_addrs_generic;
125         mac->ops.update_uc_addr_list = ixgbe_update_uc_addr_list_generic;
126         mac->ops.update_mc_addr_list = ixgbe_update_mc_addr_list_generic;
127         mac->ops.enable_mc = ixgbe_enable_mc_generic;
128         mac->ops.disable_mc = ixgbe_disable_mc_generic;
129         mac->ops.clear_vfta = NULL;
130         mac->ops.set_vfta = NULL;
131         mac->ops.set_vlvf = NULL;
132         mac->ops.init_uta_tables = NULL;
133         mac->ops.enable_rx = ixgbe_enable_rx_generic;
134         mac->ops.disable_rx = ixgbe_disable_rx_generic;
135
136         /* Flow Control */
137         mac->ops.fc_enable = ixgbe_fc_enable_generic;
138         mac->ops.setup_fc = ixgbe_setup_fc_generic;
139         mac->ops.fc_autoneg = ixgbe_fc_autoneg;
140
141         /* Link */
142         mac->ops.get_link_capabilities = NULL;
143         mac->ops.setup_link = NULL;
144         mac->ops.check_link = NULL;
145         mac->ops.dmac_config = NULL;
146         mac->ops.dmac_update_tcs = NULL;
147         mac->ops.dmac_config_tcs = NULL;
148
149         return IXGBE_SUCCESS;
150 }
151
152 /**
153  * ixgbe_device_supports_autoneg_fc - Check if device supports autonegotiation
154  * of flow control
155  * @hw: pointer to hardware structure
156  *
157  * This function returns true if the device supports flow control
158  * autonegotiation, and false if it does not.
159  *
160  **/
161 bool ixgbe_device_supports_autoneg_fc(struct ixgbe_hw *hw)
162 {
163         bool supported = false;
164         ixgbe_link_speed speed;
165         bool link_up;
166
167         DEBUGFUNC("ixgbe_device_supports_autoneg_fc");
168
169         switch (hw->phy.media_type) {
170         case ixgbe_media_type_fiber_qsfp:
171         case ixgbe_media_type_fiber:
172                 /* flow control autoneg black list */
173                 switch (hw->device_id) {
174                 case IXGBE_DEV_ID_X550EM_A_SFP:
175                 case IXGBE_DEV_ID_X550EM_A_SFP_N:
176                 case IXGBE_DEV_ID_X550EM_A_QSFP:
177                 case IXGBE_DEV_ID_X550EM_A_QSFP_N:
178                         supported = false;
179                         break;
180                 default:
181                         hw->mac.ops.check_link(hw, &speed, &link_up, false);
182                         /* if link is down, assume supported */
183                         if (link_up)
184                                 supported = speed == IXGBE_LINK_SPEED_1GB_FULL ?
185                                 true : false;
186                         else
187                                 supported = true;
188                 }
189
190                 break;
191         case ixgbe_media_type_backplane:
192                 supported = true;
193                 break;
194         case ixgbe_media_type_copper:
195                 /* only some copper devices support flow control autoneg */
196                 switch (hw->device_id) {
197                 case IXGBE_DEV_ID_82599_T3_LOM:
198                 case IXGBE_DEV_ID_X540T:
199                 case IXGBE_DEV_ID_X540T1:
200                 case IXGBE_DEV_ID_X550T:
201                 case IXGBE_DEV_ID_X550T1:
202                 case IXGBE_DEV_ID_X550EM_X_10G_T:
203                 case IXGBE_DEV_ID_X550EM_A_10G_T:
204                 case IXGBE_DEV_ID_X550EM_A_1G_T:
205                 case IXGBE_DEV_ID_X550EM_A_1G_T_L:
206                         supported = true;
207                         break;
208                 default:
209                         supported = false;
210                 }
211         default:
212                 break;
213         }
214
215         if (!supported)
216                 ERROR_REPORT2(IXGBE_ERROR_UNSUPPORTED,
217                               "Device %x does not support flow control autoneg",
218                               hw->device_id);
219         return supported;
220 }
221
222 /**
223  *  ixgbe_setup_fc_generic - Set up flow control
224  *  @hw: pointer to hardware structure
225  *
226  *  Called at init time to set up flow control.
227  **/
228 s32 ixgbe_setup_fc_generic(struct ixgbe_hw *hw)
229 {
230         s32 ret_val = IXGBE_SUCCESS;
231         u32 reg = 0, reg_bp = 0;
232         u16 reg_cu = 0;
233         bool locked = false;
234
235         DEBUGFUNC("ixgbe_setup_fc_generic");
236
237         /* Validate the requested mode */
238         if (hw->fc.strict_ieee && hw->fc.requested_mode == ixgbe_fc_rx_pause) {
239                 ERROR_REPORT1(IXGBE_ERROR_UNSUPPORTED,
240                            "ixgbe_fc_rx_pause not valid in strict IEEE mode\n");
241                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
242                 goto out;
243         }
244
245         /*
246          * 10gig parts do not have a word in the EEPROM to determine the
247          * default flow control setting, so we explicitly set it to full.
248          */
249         if (hw->fc.requested_mode == ixgbe_fc_default)
250                 hw->fc.requested_mode = ixgbe_fc_full;
251
252         /*
253          * Set up the 1G and 10G flow control advertisement registers so the
254          * HW will be able to do fc autoneg once the cable is plugged in.  If
255          * we link at 10G, the 1G advertisement is harmless and vice versa.
256          */
257         switch (hw->phy.media_type) {
258         case ixgbe_media_type_backplane:
259                 /* some MAC's need RMW protection on AUTOC */
260                 ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &reg_bp);
261                 if (ret_val != IXGBE_SUCCESS)
262                         goto out;
263
264                 /* only backplane uses autoc so fall though */
265         case ixgbe_media_type_fiber_qsfp:
266         case ixgbe_media_type_fiber:
267                 reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
268
269                 break;
270         case ixgbe_media_type_copper:
271                 hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_ADVT,
272                                      IXGBE_MDIO_AUTO_NEG_DEV_TYPE, &reg_cu);
273                 break;
274         default:
275                 break;
276         }
277
278         /*
279          * The possible values of fc.requested_mode are:
280          * 0: Flow control is completely disabled
281          * 1: Rx flow control is enabled (we can receive pause frames,
282          *    but not send pause frames).
283          * 2: Tx flow control is enabled (we can send pause frames but
284          *    we do not support receiving pause frames).
285          * 3: Both Rx and Tx flow control (symmetric) are enabled.
286          * other: Invalid.
287          */
288         switch (hw->fc.requested_mode) {
289         case ixgbe_fc_none:
290                 /* Flow control completely disabled by software override. */
291                 reg &= ~(IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE);
292                 if (hw->phy.media_type == ixgbe_media_type_backplane)
293                         reg_bp &= ~(IXGBE_AUTOC_SYM_PAUSE |
294                                     IXGBE_AUTOC_ASM_PAUSE);
295                 else if (hw->phy.media_type == ixgbe_media_type_copper)
296                         reg_cu &= ~(IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE);
297                 break;
298         case ixgbe_fc_tx_pause:
299                 /*
300                  * Tx Flow control is enabled, and Rx Flow control is
301                  * disabled by software override.
302                  */
303                 reg |= IXGBE_PCS1GANA_ASM_PAUSE;
304                 reg &= ~IXGBE_PCS1GANA_SYM_PAUSE;
305                 if (hw->phy.media_type == ixgbe_media_type_backplane) {
306                         reg_bp |= IXGBE_AUTOC_ASM_PAUSE;
307                         reg_bp &= ~IXGBE_AUTOC_SYM_PAUSE;
308                 } else if (hw->phy.media_type == ixgbe_media_type_copper) {
309                         reg_cu |= IXGBE_TAF_ASM_PAUSE;
310                         reg_cu &= ~IXGBE_TAF_SYM_PAUSE;
311                 }
312                 break;
313         case ixgbe_fc_rx_pause:
314                 /*
315                  * Rx Flow control is enabled and Tx Flow control is
316                  * disabled by software override. Since there really
317                  * isn't a way to advertise that we are capable of RX
318                  * Pause ONLY, we will advertise that we support both
319                  * symmetric and asymmetric Rx PAUSE, as such we fall
320                  * through to the fc_full statement.  Later, we will
321                  * disable the adapter's ability to send PAUSE frames.
322                  */
323         case ixgbe_fc_full:
324                 /* Flow control (both Rx and Tx) is enabled by SW override. */
325                 reg |= IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE;
326                 if (hw->phy.media_type == ixgbe_media_type_backplane)
327                         reg_bp |= IXGBE_AUTOC_SYM_PAUSE |
328                                   IXGBE_AUTOC_ASM_PAUSE;
329                 else if (hw->phy.media_type == ixgbe_media_type_copper)
330                         reg_cu |= IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE;
331                 break;
332         default:
333                 ERROR_REPORT1(IXGBE_ERROR_ARGUMENT,
334                              "Flow control param set incorrectly\n");
335                 ret_val = IXGBE_ERR_CONFIG;
336                 goto out;
337                 break;
338         }
339
340         if (hw->mac.type < ixgbe_mac_X540) {
341                 /*
342                  * Enable auto-negotiation between the MAC & PHY;
343                  * the MAC will advertise clause 37 flow control.
344                  */
345                 IXGBE_WRITE_REG(hw, IXGBE_PCS1GANA, reg);
346                 reg = IXGBE_READ_REG(hw, IXGBE_PCS1GLCTL);
347
348                 /* Disable AN timeout */
349                 if (hw->fc.strict_ieee)
350                         reg &= ~IXGBE_PCS1GLCTL_AN_1G_TIMEOUT_EN;
351
352                 IXGBE_WRITE_REG(hw, IXGBE_PCS1GLCTL, reg);
353                 DEBUGOUT1("Set up FC; PCS1GLCTL = 0x%08X\n", reg);
354         }
355
356         /*
357          * AUTOC restart handles negotiation of 1G and 10G on backplane
358          * and copper. There is no need to set the PCS1GCTL register.
359          *
360          */
361         if (hw->phy.media_type == ixgbe_media_type_backplane) {
362                 reg_bp |= IXGBE_AUTOC_AN_RESTART;
363                 ret_val = hw->mac.ops.prot_autoc_write(hw, reg_bp, locked);
364                 if (ret_val)
365                         goto out;
366         } else if ((hw->phy.media_type == ixgbe_media_type_copper) &&
367                     (ixgbe_device_supports_autoneg_fc(hw))) {
368                 hw->phy.ops.write_reg(hw, IXGBE_MDIO_AUTO_NEG_ADVT,
369                                       IXGBE_MDIO_AUTO_NEG_DEV_TYPE, reg_cu);
370         }
371
372         DEBUGOUT1("Set up FC; PCS1GLCTL = 0x%08X\n", reg);
373 out:
374         return ret_val;
375 }
376
377 /**
378  *  ixgbe_start_hw_generic - Prepare hardware for Tx/Rx
379  *  @hw: pointer to hardware structure
380  *
381  *  Starts the hardware by filling the bus info structure and media type, clears
382  *  all on chip counters, initializes receive address registers, multicast
383  *  table, VLAN filter table, calls routine to set up link and flow control
384  *  settings, and leaves transmit and receive units disabled and uninitialized
385  **/
386 s32 ixgbe_start_hw_generic(struct ixgbe_hw *hw)
387 {
388         s32 ret_val;
389         u32 ctrl_ext;
390         u16 device_caps;
391
392         DEBUGFUNC("ixgbe_start_hw_generic");
393
394         /* Set the media type */
395         hw->phy.media_type = hw->mac.ops.get_media_type(hw);
396
397         /* PHY ops initialization must be done in reset_hw() */
398
399         /* Clear the VLAN filter table */
400         hw->mac.ops.clear_vfta(hw);
401
402         /* Clear statistics registers */
403         hw->mac.ops.clear_hw_cntrs(hw);
404
405         /* Set No Snoop Disable */
406         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
407         ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
408         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
409         IXGBE_WRITE_FLUSH(hw);
410
411         /* Setup flow control */
412         ret_val = ixgbe_setup_fc(hw);
413         if (ret_val != IXGBE_SUCCESS && ret_val != IXGBE_NOT_IMPLEMENTED) {
414                 DEBUGOUT1("Flow control setup failed, returning %d\n", ret_val);
415                 return ret_val;
416         }
417
418         /* Cache bit indicating need for crosstalk fix */
419         switch (hw->mac.type) {
420         case ixgbe_mac_82599EB:
421         case ixgbe_mac_X550EM_x:
422         case ixgbe_mac_X550EM_a:
423                 hw->mac.ops.get_device_caps(hw, &device_caps);
424                 if (device_caps & IXGBE_DEVICE_CAPS_NO_CROSSTALK_WR)
425                         hw->need_crosstalk_fix = false;
426                 else
427                         hw->need_crosstalk_fix = true;
428                 break;
429         default:
430                 hw->need_crosstalk_fix = false;
431                 break;
432         }
433
434         /* Clear adapter stopped flag */
435         hw->adapter_stopped = false;
436
437         return IXGBE_SUCCESS;
438 }
439
440 /**
441  *  ixgbe_start_hw_gen2 - Init sequence for common device family
442  *  @hw: pointer to hw structure
443  *
444  * Performs the init sequence common to the second generation
445  * of 10 GbE devices.
446  * Devices in the second generation:
447  *     82599
448  *     X540
449  **/
450 s32 ixgbe_start_hw_gen2(struct ixgbe_hw *hw)
451 {
452         u32 i;
453         u32 regval;
454
455         /* Clear the rate limiters */
456         for (i = 0; i < hw->mac.max_tx_queues; i++) {
457                 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, i);
458                 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, 0);
459         }
460         IXGBE_WRITE_FLUSH(hw);
461
462         /* Disable relaxed ordering */
463         for (i = 0; i < hw->mac.max_tx_queues; i++) {
464                 regval = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
465                 regval &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
466                 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), regval);
467         }
468
469         for (i = 0; i < hw->mac.max_rx_queues; i++) {
470                 regval = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
471                 regval &= ~(IXGBE_DCA_RXCTRL_DATA_WRO_EN |
472                             IXGBE_DCA_RXCTRL_HEAD_WRO_EN);
473                 IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(i), regval);
474         }
475
476         return IXGBE_SUCCESS;
477 }
478
479 /**
480  *  ixgbe_init_hw_generic - Generic hardware initialization
481  *  @hw: pointer to hardware structure
482  *
483  *  Initialize the hardware by resetting the hardware, filling the bus info
484  *  structure and media type, clears all on chip counters, initializes receive
485  *  address registers, multicast table, VLAN filter table, calls routine to set
486  *  up link and flow control settings, and leaves transmit and receive units
487  *  disabled and uninitialized
488  **/
489 s32 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
490 {
491         s32 status;
492
493         DEBUGFUNC("ixgbe_init_hw_generic");
494
495         /* Reset the hardware */
496         status = hw->mac.ops.reset_hw(hw);
497
498         if (status == IXGBE_SUCCESS) {
499                 /* Start the HW */
500                 status = hw->mac.ops.start_hw(hw);
501         }
502
503         /* Initialize the LED link active for LED blink support */
504         hw->mac.ops.init_led_link_act(hw);
505
506         if (status != IXGBE_SUCCESS)
507                 DEBUGOUT1("Failed to initialize HW, STATUS = %d\n", status);
508
509         return status;
510 }
511
512 /**
513  *  ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
514  *  @hw: pointer to hardware structure
515  *
516  *  Clears all hardware statistics counters by reading them from the hardware
517  *  Statistics counters are clear on read.
518  **/
519 s32 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
520 {
521         u16 i = 0;
522
523         DEBUGFUNC("ixgbe_clear_hw_cntrs_generic");
524
525         IXGBE_READ_REG(hw, IXGBE_CRCERRS);
526         IXGBE_READ_REG(hw, IXGBE_ILLERRC);
527         IXGBE_READ_REG(hw, IXGBE_ERRBC);
528         IXGBE_READ_REG(hw, IXGBE_MSPDC);
529         for (i = 0; i < 8; i++)
530                 IXGBE_READ_REG(hw, IXGBE_MPC(i));
531
532         IXGBE_READ_REG(hw, IXGBE_MLFC);
533         IXGBE_READ_REG(hw, IXGBE_MRFC);
534         IXGBE_READ_REG(hw, IXGBE_RLEC);
535         IXGBE_READ_REG(hw, IXGBE_LXONTXC);
536         IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
537         if (hw->mac.type >= ixgbe_mac_82599EB) {
538                 IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
539                 IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
540         } else {
541                 IXGBE_READ_REG(hw, IXGBE_LXONRXC);
542                 IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
543         }
544
545         for (i = 0; i < 8; i++) {
546                 IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
547                 IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
548                 if (hw->mac.type >= ixgbe_mac_82599EB) {
549                         IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
550                         IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
551                 } else {
552                         IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
553                         IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
554                 }
555         }
556         if (hw->mac.type >= ixgbe_mac_82599EB)
557                 for (i = 0; i < 8; i++)
558                         IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
559         IXGBE_READ_REG(hw, IXGBE_PRC64);
560         IXGBE_READ_REG(hw, IXGBE_PRC127);
561         IXGBE_READ_REG(hw, IXGBE_PRC255);
562         IXGBE_READ_REG(hw, IXGBE_PRC511);
563         IXGBE_READ_REG(hw, IXGBE_PRC1023);
564         IXGBE_READ_REG(hw, IXGBE_PRC1522);
565         IXGBE_READ_REG(hw, IXGBE_GPRC);
566         IXGBE_READ_REG(hw, IXGBE_BPRC);
567         IXGBE_READ_REG(hw, IXGBE_MPRC);
568         IXGBE_READ_REG(hw, IXGBE_GPTC);
569         IXGBE_READ_REG(hw, IXGBE_GORCL);
570         IXGBE_READ_REG(hw, IXGBE_GORCH);
571         IXGBE_READ_REG(hw, IXGBE_GOTCL);
572         IXGBE_READ_REG(hw, IXGBE_GOTCH);
573         if (hw->mac.type == ixgbe_mac_82598EB)
574                 for (i = 0; i < 8; i++)
575                         IXGBE_READ_REG(hw, IXGBE_RNBC(i));
576         IXGBE_READ_REG(hw, IXGBE_RUC);
577         IXGBE_READ_REG(hw, IXGBE_RFC);
578         IXGBE_READ_REG(hw, IXGBE_ROC);
579         IXGBE_READ_REG(hw, IXGBE_RJC);
580         IXGBE_READ_REG(hw, IXGBE_MNGPRC);
581         IXGBE_READ_REG(hw, IXGBE_MNGPDC);
582         IXGBE_READ_REG(hw, IXGBE_MNGPTC);
583         IXGBE_READ_REG(hw, IXGBE_TORL);
584         IXGBE_READ_REG(hw, IXGBE_TORH);
585         IXGBE_READ_REG(hw, IXGBE_TPR);
586         IXGBE_READ_REG(hw, IXGBE_TPT);
587         IXGBE_READ_REG(hw, IXGBE_PTC64);
588         IXGBE_READ_REG(hw, IXGBE_PTC127);
589         IXGBE_READ_REG(hw, IXGBE_PTC255);
590         IXGBE_READ_REG(hw, IXGBE_PTC511);
591         IXGBE_READ_REG(hw, IXGBE_PTC1023);
592         IXGBE_READ_REG(hw, IXGBE_PTC1522);
593         IXGBE_READ_REG(hw, IXGBE_MPTC);
594         IXGBE_READ_REG(hw, IXGBE_BPTC);
595         for (i = 0; i < 16; i++) {
596                 IXGBE_READ_REG(hw, IXGBE_QPRC(i));
597                 IXGBE_READ_REG(hw, IXGBE_QPTC(i));
598                 if (hw->mac.type >= ixgbe_mac_82599EB) {
599                         IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
600                         IXGBE_READ_REG(hw, IXGBE_QBRC_H(i));
601                         IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
602                         IXGBE_READ_REG(hw, IXGBE_QBTC_H(i));
603                         IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
604                 } else {
605                         IXGBE_READ_REG(hw, IXGBE_QBRC(i));
606                         IXGBE_READ_REG(hw, IXGBE_QBTC(i));
607                 }
608         }
609
610         if (hw->mac.type == ixgbe_mac_X550 || hw->mac.type == ixgbe_mac_X540) {
611                 if (hw->phy.id == 0)
612                         ixgbe_identify_phy(hw);
613                 hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECL,
614                                      IXGBE_MDIO_PCS_DEV_TYPE, &i);
615                 hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECH,
616                                      IXGBE_MDIO_PCS_DEV_TYPE, &i);
617                 hw->phy.ops.read_reg(hw, IXGBE_LDPCECL,
618                                      IXGBE_MDIO_PCS_DEV_TYPE, &i);
619                 hw->phy.ops.read_reg(hw, IXGBE_LDPCECH,
620                                      IXGBE_MDIO_PCS_DEV_TYPE, &i);
621         }
622
623         return IXGBE_SUCCESS;
624 }
625
626 /**
627  *  ixgbe_read_pba_string_generic - Reads part number string from EEPROM
628  *  @hw: pointer to hardware structure
629  *  @pba_num: stores the part number string from the EEPROM
630  *  @pba_num_size: part number string buffer length
631  *
632  *  Reads the part number string from the EEPROM.
633  **/
634 s32 ixgbe_read_pba_string_generic(struct ixgbe_hw *hw, u8 *pba_num,
635                                   u32 pba_num_size)
636 {
637         s32 ret_val;
638         u16 data;
639         u16 pba_ptr;
640         u16 offset;
641         u16 length;
642
643         DEBUGFUNC("ixgbe_read_pba_string_generic");
644
645         if (pba_num == NULL) {
646                 DEBUGOUT("PBA string buffer was null\n");
647                 return IXGBE_ERR_INVALID_ARGUMENT;
648         }
649
650         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
651         if (ret_val) {
652                 DEBUGOUT("NVM Read Error\n");
653                 return ret_val;
654         }
655
656         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &pba_ptr);
657         if (ret_val) {
658                 DEBUGOUT("NVM Read Error\n");
659                 return ret_val;
660         }
661
662         /*
663          * if data is not ptr guard the PBA must be in legacy format which
664          * means pba_ptr is actually our second data word for the PBA number
665          * and we can decode it into an ascii string
666          */
667         if (data != IXGBE_PBANUM_PTR_GUARD) {
668                 DEBUGOUT("NVM PBA number is not stored as string\n");
669
670                 /* we will need 11 characters to store the PBA */
671                 if (pba_num_size < 11) {
672                         DEBUGOUT("PBA string buffer too small\n");
673                         return IXGBE_ERR_NO_SPACE;
674                 }
675
676                 /* extract hex string from data and pba_ptr */
677                 pba_num[0] = (data >> 12) & 0xF;
678                 pba_num[1] = (data >> 8) & 0xF;
679                 pba_num[2] = (data >> 4) & 0xF;
680                 pba_num[3] = data & 0xF;
681                 pba_num[4] = (pba_ptr >> 12) & 0xF;
682                 pba_num[5] = (pba_ptr >> 8) & 0xF;
683                 pba_num[6] = '-';
684                 pba_num[7] = 0;
685                 pba_num[8] = (pba_ptr >> 4) & 0xF;
686                 pba_num[9] = pba_ptr & 0xF;
687
688                 /* put a null character on the end of our string */
689                 pba_num[10] = '\0';
690
691                 /* switch all the data but the '-' to hex char */
692                 for (offset = 0; offset < 10; offset++) {
693                         if (pba_num[offset] < 0xA)
694                                 pba_num[offset] += '0';
695                         else if (pba_num[offset] < 0x10)
696                                 pba_num[offset] += 'A' - 0xA;
697                 }
698
699                 return IXGBE_SUCCESS;
700         }
701
702         ret_val = hw->eeprom.ops.read(hw, pba_ptr, &length);
703         if (ret_val) {
704                 DEBUGOUT("NVM Read Error\n");
705                 return ret_val;
706         }
707
708         if (length == 0xFFFF || length == 0) {
709                 DEBUGOUT("NVM PBA number section invalid length\n");
710                 return IXGBE_ERR_PBA_SECTION;
711         }
712
713         /* check if pba_num buffer is big enough */
714         if (pba_num_size  < (((u32)length * 2) - 1)) {
715                 DEBUGOUT("PBA string buffer too small\n");
716                 return IXGBE_ERR_NO_SPACE;
717         }
718
719         /* trim pba length from start of string */
720         pba_ptr++;
721         length--;
722
723         for (offset = 0; offset < length; offset++) {
724                 ret_val = hw->eeprom.ops.read(hw, pba_ptr + offset, &data);
725                 if (ret_val) {
726                         DEBUGOUT("NVM Read Error\n");
727                         return ret_val;
728                 }
729                 pba_num[offset * 2] = (u8)(data >> 8);
730                 pba_num[(offset * 2) + 1] = (u8)(data & 0xFF);
731         }
732         pba_num[offset * 2] = '\0';
733
734         return IXGBE_SUCCESS;
735 }
736
737 /**
738  *  ixgbe_read_pba_num_generic - Reads part number from EEPROM
739  *  @hw: pointer to hardware structure
740  *  @pba_num: stores the part number from the EEPROM
741  *
742  *  Reads the part number from the EEPROM.
743  **/
744 s32 ixgbe_read_pba_num_generic(struct ixgbe_hw *hw, u32 *pba_num)
745 {
746         s32 ret_val;
747         u16 data;
748
749         DEBUGFUNC("ixgbe_read_pba_num_generic");
750
751         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
752         if (ret_val) {
753                 DEBUGOUT("NVM Read Error\n");
754                 return ret_val;
755         } else if (data == IXGBE_PBANUM_PTR_GUARD) {
756                 DEBUGOUT("NVM Not supported\n");
757                 return IXGBE_NOT_IMPLEMENTED;
758         }
759         *pba_num = (u32)(data << 16);
760
761         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &data);
762         if (ret_val) {
763                 DEBUGOUT("NVM Read Error\n");
764                 return ret_val;
765         }
766         *pba_num |= data;
767
768         return IXGBE_SUCCESS;
769 }
770
771 /**
772  *  ixgbe_read_pba_raw
773  *  @hw: pointer to the HW structure
774  *  @eeprom_buf: optional pointer to EEPROM image
775  *  @eeprom_buf_size: size of EEPROM image in words
776  *  @max_pba_block_size: PBA block size limit
777  *  @pba: pointer to output PBA structure
778  *
779  *  Reads PBA from EEPROM image when eeprom_buf is not NULL.
780  *  Reads PBA from physical EEPROM device when eeprom_buf is NULL.
781  *
782  **/
783 s32 ixgbe_read_pba_raw(struct ixgbe_hw *hw, u16 *eeprom_buf,
784                        u32 eeprom_buf_size, u16 max_pba_block_size,
785                        struct ixgbe_pba *pba)
786 {
787         s32 ret_val;
788         u16 pba_block_size;
789
790         if (pba == NULL)
791                 return IXGBE_ERR_PARAM;
792
793         if (eeprom_buf == NULL) {
794                 ret_val = hw->eeprom.ops.read_buffer(hw, IXGBE_PBANUM0_PTR, 2,
795                                                      &pba->word[0]);
796                 if (ret_val)
797                         return ret_val;
798         } else {
799                 if (eeprom_buf_size > IXGBE_PBANUM1_PTR) {
800                         pba->word[0] = eeprom_buf[IXGBE_PBANUM0_PTR];
801                         pba->word[1] = eeprom_buf[IXGBE_PBANUM1_PTR];
802                 } else {
803                         return IXGBE_ERR_PARAM;
804                 }
805         }
806
807         if (pba->word[0] == IXGBE_PBANUM_PTR_GUARD) {
808                 if (pba->pba_block == NULL)
809                         return IXGBE_ERR_PARAM;
810
811                 ret_val = ixgbe_get_pba_block_size(hw, eeprom_buf,
812                                                    eeprom_buf_size,
813                                                    &pba_block_size);
814                 if (ret_val)
815                         return ret_val;
816
817                 if (pba_block_size > max_pba_block_size)
818                         return IXGBE_ERR_PARAM;
819
820                 if (eeprom_buf == NULL) {
821                         ret_val = hw->eeprom.ops.read_buffer(hw, pba->word[1],
822                                                              pba_block_size,
823                                                              pba->pba_block);
824                         if (ret_val)
825                                 return ret_val;
826                 } else {
827                         if (eeprom_buf_size > (u32)(pba->word[1] +
828                                               pba_block_size)) {
829                                 memcpy(pba->pba_block,
830                                        &eeprom_buf[pba->word[1]],
831                                        pba_block_size * sizeof(u16));
832                         } else {
833                                 return IXGBE_ERR_PARAM;
834                         }
835                 }
836         }
837
838         return IXGBE_SUCCESS;
839 }
840
841 /**
842  *  ixgbe_write_pba_raw
843  *  @hw: pointer to the HW structure
844  *  @eeprom_buf: optional pointer to EEPROM image
845  *  @eeprom_buf_size: size of EEPROM image in words
846  *  @pba: pointer to PBA structure
847  *
848  *  Writes PBA to EEPROM image when eeprom_buf is not NULL.
849  *  Writes PBA to physical EEPROM device when eeprom_buf is NULL.
850  *
851  **/
852 s32 ixgbe_write_pba_raw(struct ixgbe_hw *hw, u16 *eeprom_buf,
853                         u32 eeprom_buf_size, struct ixgbe_pba *pba)
854 {
855         s32 ret_val;
856
857         if (pba == NULL)
858                 return IXGBE_ERR_PARAM;
859
860         if (eeprom_buf == NULL) {
861                 ret_val = hw->eeprom.ops.write_buffer(hw, IXGBE_PBANUM0_PTR, 2,
862                                                       &pba->word[0]);
863                 if (ret_val)
864                         return ret_val;
865         } else {
866                 if (eeprom_buf_size > IXGBE_PBANUM1_PTR) {
867                         eeprom_buf[IXGBE_PBANUM0_PTR] = pba->word[0];
868                         eeprom_buf[IXGBE_PBANUM1_PTR] = pba->word[1];
869                 } else {
870                         return IXGBE_ERR_PARAM;
871                 }
872         }
873
874         if (pba->word[0] == IXGBE_PBANUM_PTR_GUARD) {
875                 if (pba->pba_block == NULL)
876                         return IXGBE_ERR_PARAM;
877
878                 if (eeprom_buf == NULL) {
879                         ret_val = hw->eeprom.ops.write_buffer(hw, pba->word[1],
880                                                               pba->pba_block[0],
881                                                               pba->pba_block);
882                         if (ret_val)
883                                 return ret_val;
884                 } else {
885                         if (eeprom_buf_size > (u32)(pba->word[1] +
886                                               pba->pba_block[0])) {
887                                 memcpy(&eeprom_buf[pba->word[1]],
888                                        pba->pba_block,
889                                        pba->pba_block[0] * sizeof(u16));
890                         } else {
891                                 return IXGBE_ERR_PARAM;
892                         }
893                 }
894         }
895
896         return IXGBE_SUCCESS;
897 }
898
899 /**
900  *  ixgbe_get_pba_block_size
901  *  @hw: pointer to the HW structure
902  *  @eeprom_buf: optional pointer to EEPROM image
903  *  @eeprom_buf_size: size of EEPROM image in words
904  *  @pba_data_size: pointer to output variable
905  *
906  *  Returns the size of the PBA block in words. Function operates on EEPROM
907  *  image if the eeprom_buf pointer is not NULL otherwise it accesses physical
908  *  EEPROM device.
909  *
910  **/
911 s32 ixgbe_get_pba_block_size(struct ixgbe_hw *hw, u16 *eeprom_buf,
912                              u32 eeprom_buf_size, u16 *pba_block_size)
913 {
914         s32 ret_val;
915         u16 pba_word[2];
916         u16 length;
917
918         DEBUGFUNC("ixgbe_get_pba_block_size");
919
920         if (eeprom_buf == NULL) {
921                 ret_val = hw->eeprom.ops.read_buffer(hw, IXGBE_PBANUM0_PTR, 2,
922                                                      &pba_word[0]);
923                 if (ret_val)
924                         return ret_val;
925         } else {
926                 if (eeprom_buf_size > IXGBE_PBANUM1_PTR) {
927                         pba_word[0] = eeprom_buf[IXGBE_PBANUM0_PTR];
928                         pba_word[1] = eeprom_buf[IXGBE_PBANUM1_PTR];
929                 } else {
930                         return IXGBE_ERR_PARAM;
931                 }
932         }
933
934         if (pba_word[0] == IXGBE_PBANUM_PTR_GUARD) {
935                 if (eeprom_buf == NULL) {
936                         ret_val = hw->eeprom.ops.read(hw, pba_word[1] + 0,
937                                                       &length);
938                         if (ret_val)
939                                 return ret_val;
940                 } else {
941                         if (eeprom_buf_size > pba_word[1])
942                                 length = eeprom_buf[pba_word[1] + 0];
943                         else
944                                 return IXGBE_ERR_PARAM;
945                 }
946
947                 if (length == 0xFFFF || length == 0)
948                         return IXGBE_ERR_PBA_SECTION;
949         } else {
950                 /* PBA number in legacy format, there is no PBA Block. */
951                 length = 0;
952         }
953
954         if (pba_block_size != NULL)
955                 *pba_block_size = length;
956
957         return IXGBE_SUCCESS;
958 }
959
960 /**
961  *  ixgbe_get_mac_addr_generic - Generic get MAC address
962  *  @hw: pointer to hardware structure
963  *  @mac_addr: Adapter MAC address
964  *
965  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
966  *  A reset of the adapter must be performed prior to calling this function
967  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
968  **/
969 s32 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
970 {
971         u32 rar_high;
972         u32 rar_low;
973         u16 i;
974
975         DEBUGFUNC("ixgbe_get_mac_addr_generic");
976
977         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
978         rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
979
980         for (i = 0; i < 4; i++)
981                 mac_addr[i] = (u8)(rar_low >> (i*8));
982
983         for (i = 0; i < 2; i++)
984                 mac_addr[i+4] = (u8)(rar_high >> (i*8));
985
986         return IXGBE_SUCCESS;
987 }
988
989 /**
990  *  ixgbe_set_pci_config_data_generic - Generic store PCI bus info
991  *  @hw: pointer to hardware structure
992  *  @link_status: the link status returned by the PCI config space
993  *
994  *  Stores the PCI bus info (speed, width, type) within the ixgbe_hw structure
995  **/
996 void ixgbe_set_pci_config_data_generic(struct ixgbe_hw *hw, u16 link_status)
997 {
998         struct ixgbe_mac_info *mac = &hw->mac;
999
1000         if (hw->bus.type == ixgbe_bus_type_unknown)
1001                 hw->bus.type = ixgbe_bus_type_pci_express;
1002
1003         switch (link_status & IXGBE_PCI_LINK_WIDTH) {
1004         case IXGBE_PCI_LINK_WIDTH_1:
1005                 hw->bus.width = ixgbe_bus_width_pcie_x1;
1006                 break;
1007         case IXGBE_PCI_LINK_WIDTH_2:
1008                 hw->bus.width = ixgbe_bus_width_pcie_x2;
1009                 break;
1010         case IXGBE_PCI_LINK_WIDTH_4:
1011                 hw->bus.width = ixgbe_bus_width_pcie_x4;
1012                 break;
1013         case IXGBE_PCI_LINK_WIDTH_8:
1014                 hw->bus.width = ixgbe_bus_width_pcie_x8;
1015                 break;
1016         default:
1017                 hw->bus.width = ixgbe_bus_width_unknown;
1018                 break;
1019         }
1020
1021         switch (link_status & IXGBE_PCI_LINK_SPEED) {
1022         case IXGBE_PCI_LINK_SPEED_2500:
1023                 hw->bus.speed = ixgbe_bus_speed_2500;
1024                 break;
1025         case IXGBE_PCI_LINK_SPEED_5000:
1026                 hw->bus.speed = ixgbe_bus_speed_5000;
1027                 break;
1028         case IXGBE_PCI_LINK_SPEED_8000:
1029                 hw->bus.speed = ixgbe_bus_speed_8000;
1030                 break;
1031         default:
1032                 hw->bus.speed = ixgbe_bus_speed_unknown;
1033                 break;
1034         }
1035
1036         mac->ops.set_lan_id(hw);
1037 }
1038
1039 /**
1040  *  ixgbe_get_bus_info_generic - Generic set PCI bus info
1041  *  @hw: pointer to hardware structure
1042  *
1043  *  Gets the PCI bus info (speed, width, type) then calls helper function to
1044  *  store this data within the ixgbe_hw structure.
1045  **/
1046 s32 ixgbe_get_bus_info_generic(struct ixgbe_hw *hw)
1047 {
1048         u16 link_status;
1049
1050         DEBUGFUNC("ixgbe_get_bus_info_generic");
1051
1052         /* Get the negotiated link width and speed from PCI config space */
1053         link_status = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_LINK_STATUS);
1054
1055         ixgbe_set_pci_config_data_generic(hw, link_status);
1056
1057         return IXGBE_SUCCESS;
1058 }
1059
1060 /**
1061  *  ixgbe_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
1062  *  @hw: pointer to the HW structure
1063  *
1064  *  Determines the LAN function id by reading memory-mapped registers and swaps
1065  *  the port value if requested, and set MAC instance for devices that share
1066  *  CS4227.
1067  **/
1068 void ixgbe_set_lan_id_multi_port_pcie(struct ixgbe_hw *hw)
1069 {
1070         struct ixgbe_bus_info *bus = &hw->bus;
1071         u32 reg;
1072         u16 ee_ctrl_4;
1073
1074         DEBUGFUNC("ixgbe_set_lan_id_multi_port_pcie");
1075
1076         reg = IXGBE_READ_REG(hw, IXGBE_STATUS);
1077         bus->func = (reg & IXGBE_STATUS_LAN_ID) >> IXGBE_STATUS_LAN_ID_SHIFT;
1078         bus->lan_id = (u8)bus->func;
1079
1080         /* check for a port swap */
1081         reg = IXGBE_READ_REG(hw, IXGBE_FACTPS_BY_MAC(hw));
1082         if (reg & IXGBE_FACTPS_LFS)
1083                 bus->func ^= 0x1;
1084
1085         /* Get MAC instance from EEPROM for configuring CS4227 */
1086         if (hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP) {
1087                 hw->eeprom.ops.read(hw, IXGBE_EEPROM_CTRL_4, &ee_ctrl_4);
1088                 bus->instance_id = (ee_ctrl_4 & IXGBE_EE_CTRL_4_INST_ID) >>
1089                                    IXGBE_EE_CTRL_4_INST_ID_SHIFT;
1090         }
1091 }
1092
1093 /**
1094  *  ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
1095  *  @hw: pointer to hardware structure
1096  *
1097  *  Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
1098  *  disables transmit and receive units. The adapter_stopped flag is used by
1099  *  the shared code and drivers to determine if the adapter is in a stopped
1100  *  state and should not touch the hardware.
1101  **/
1102 s32 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
1103 {
1104         u32 reg_val;
1105         u16 i;
1106
1107         DEBUGFUNC("ixgbe_stop_adapter_generic");
1108
1109         /*
1110          * Set the adapter_stopped flag so other driver functions stop touching
1111          * the hardware
1112          */
1113         hw->adapter_stopped = true;
1114
1115         /* Disable the receive unit */
1116         ixgbe_disable_rx(hw);
1117
1118         /* Clear interrupt mask to stop interrupts from being generated */
1119         IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
1120
1121         /* Clear any pending interrupts, flush previous writes */
1122         IXGBE_READ_REG(hw, IXGBE_EICR);
1123
1124         /* Disable the transmit unit.  Each queue must be disabled. */
1125         for (i = 0; i < hw->mac.max_tx_queues; i++)
1126                 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), IXGBE_TXDCTL_SWFLSH);
1127
1128         /* Disable the receive unit by stopping each queue */
1129         for (i = 0; i < hw->mac.max_rx_queues; i++) {
1130                 reg_val = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1131                 reg_val &= ~IXGBE_RXDCTL_ENABLE;
1132                 reg_val |= IXGBE_RXDCTL_SWFLSH;
1133                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), reg_val);
1134         }
1135
1136         /* flush all queues disables */
1137         IXGBE_WRITE_FLUSH(hw);
1138         msec_delay(2);
1139
1140         /*
1141          * Prevent the PCI-E bus from hanging by disabling PCI-E master
1142          * access and verify no pending requests
1143          */
1144         return ixgbe_disable_pcie_master(hw);
1145 }
1146
1147 /**
1148  *  ixgbe_init_led_link_act_generic - Store the LED index link/activity.
1149  *  @hw: pointer to hardware structure
1150  *
1151  *  Store the index for the link active LED. This will be used to support
1152  *  blinking the LED.
1153  **/
1154 s32 ixgbe_init_led_link_act_generic(struct ixgbe_hw *hw)
1155 {
1156         struct ixgbe_mac_info *mac = &hw->mac;
1157         u32 led_reg, led_mode;
1158         u8 i;
1159
1160         led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
1161
1162         /* Get LED link active from the LEDCTL register */
1163         for (i = 0; i < 4; i++) {
1164                 led_mode = led_reg >> IXGBE_LED_MODE_SHIFT(i);
1165
1166                 if ((led_mode & IXGBE_LED_MODE_MASK_BASE) ==
1167                      IXGBE_LED_LINK_ACTIVE) {
1168                         mac->led_link_act = i;
1169                         return IXGBE_SUCCESS;
1170                 }
1171         }
1172
1173         /*
1174          * If LEDCTL register does not have the LED link active set, then use
1175          * known MAC defaults.
1176          */
1177         switch (hw->mac.type) {
1178         case ixgbe_mac_X550EM_a:
1179         case ixgbe_mac_X550EM_x:
1180                 mac->led_link_act = 1;
1181                 break;
1182         default:
1183                 mac->led_link_act = 2;
1184         }
1185         return IXGBE_SUCCESS;
1186 }
1187
1188 /**
1189  *  ixgbe_led_on_generic - Turns on the software controllable LEDs.
1190  *  @hw: pointer to hardware structure
1191  *  @index: led number to turn on
1192  **/
1193 s32 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
1194 {
1195         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
1196
1197         DEBUGFUNC("ixgbe_led_on_generic");
1198
1199         if (index > 3)
1200                 return IXGBE_ERR_PARAM;
1201
1202         /* To turn on the LED, set mode to ON. */
1203         led_reg &= ~IXGBE_LED_MODE_MASK(index);
1204         led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
1205         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
1206         IXGBE_WRITE_FLUSH(hw);
1207
1208         return IXGBE_SUCCESS;
1209 }
1210
1211 /**
1212  *  ixgbe_led_off_generic - Turns off the software controllable LEDs.
1213  *  @hw: pointer to hardware structure
1214  *  @index: led number to turn off
1215  **/
1216 s32 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
1217 {
1218         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
1219
1220         DEBUGFUNC("ixgbe_led_off_generic");
1221
1222         if (index > 3)
1223                 return IXGBE_ERR_PARAM;
1224
1225         /* To turn off the LED, set mode to OFF. */
1226         led_reg &= ~IXGBE_LED_MODE_MASK(index);
1227         led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
1228         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
1229         IXGBE_WRITE_FLUSH(hw);
1230
1231         return IXGBE_SUCCESS;
1232 }
1233
1234 /**
1235  *  ixgbe_init_eeprom_params_generic - Initialize EEPROM params
1236  *  @hw: pointer to hardware structure
1237  *
1238  *  Initializes the EEPROM parameters ixgbe_eeprom_info within the
1239  *  ixgbe_hw struct in order to set up EEPROM access.
1240  **/
1241 s32 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
1242 {
1243         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
1244         u32 eec;
1245         u16 eeprom_size;
1246
1247         DEBUGFUNC("ixgbe_init_eeprom_params_generic");
1248
1249         if (eeprom->type == ixgbe_eeprom_uninitialized) {
1250                 eeprom->type = ixgbe_eeprom_none;
1251                 /* Set default semaphore delay to 10ms which is a well
1252                  * tested value */
1253                 eeprom->semaphore_delay = 10;
1254                 /* Clear EEPROM page size, it will be initialized as needed */
1255                 eeprom->word_page_size = 0;
1256
1257                 /*
1258                  * Check for EEPROM present first.
1259                  * If not present leave as none
1260                  */
1261                 eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
1262                 if (eec & IXGBE_EEC_PRES) {
1263                         eeprom->type = ixgbe_eeprom_spi;
1264
1265                         /*
1266                          * SPI EEPROM is assumed here.  This code would need to
1267                          * change if a future EEPROM is not SPI.
1268                          */
1269                         eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
1270                                             IXGBE_EEC_SIZE_SHIFT);
1271                         eeprom->word_size = 1 << (eeprom_size +
1272                                              IXGBE_EEPROM_WORD_SIZE_SHIFT);
1273                 }
1274
1275                 if (eec & IXGBE_EEC_ADDR_SIZE)
1276                         eeprom->address_bits = 16;
1277                 else
1278                         eeprom->address_bits = 8;
1279                 DEBUGOUT3("Eeprom params: type = %d, size = %d, address bits: "
1280                           "%d\n", eeprom->type, eeprom->word_size,
1281                           eeprom->address_bits);
1282         }
1283
1284         return IXGBE_SUCCESS;
1285 }
1286
1287 /**
1288  *  ixgbe_write_eeprom_buffer_bit_bang_generic - Write EEPROM using bit-bang
1289  *  @hw: pointer to hardware structure
1290  *  @offset: offset within the EEPROM to write
1291  *  @words: number of word(s)
1292  *  @data: 16 bit word(s) to write to EEPROM
1293  *
1294  *  Reads 16 bit word(s) from EEPROM through bit-bang method
1295  **/
1296 s32 ixgbe_write_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
1297                                                u16 words, u16 *data)
1298 {
1299         s32 status = IXGBE_SUCCESS;
1300         u16 i, count;
1301
1302         DEBUGFUNC("ixgbe_write_eeprom_buffer_bit_bang_generic");
1303
1304         hw->eeprom.ops.init_params(hw);
1305
1306         if (words == 0) {
1307                 status = IXGBE_ERR_INVALID_ARGUMENT;
1308                 goto out;
1309         }
1310
1311         if (offset + words > hw->eeprom.word_size) {
1312                 status = IXGBE_ERR_EEPROM;
1313                 goto out;
1314         }
1315
1316         /*
1317          * The EEPROM page size cannot be queried from the chip. We do lazy
1318          * initialization. It is worth to do that when we write large buffer.
1319          */
1320         if ((hw->eeprom.word_page_size == 0) &&
1321             (words > IXGBE_EEPROM_PAGE_SIZE_MAX))
1322                 ixgbe_detect_eeprom_page_size_generic(hw, offset);
1323
1324         /*
1325          * We cannot hold synchronization semaphores for too long
1326          * to avoid other entity starvation. However it is more efficient
1327          * to read in bursts than synchronizing access for each word.
1328          */
1329         for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
1330                 count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
1331                         IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
1332                 status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset + i,
1333                                                             count, &data[i]);
1334
1335                 if (status != IXGBE_SUCCESS)
1336                         break;
1337         }
1338
1339 out:
1340         return status;
1341 }
1342
1343 /**
1344  *  ixgbe_write_eeprom_buffer_bit_bang - Writes 16 bit word(s) to EEPROM
1345  *  @hw: pointer to hardware structure
1346  *  @offset: offset within the EEPROM to be written to
1347  *  @words: number of word(s)
1348  *  @data: 16 bit word(s) to be written to the EEPROM
1349  *
1350  *  If ixgbe_eeprom_update_checksum is not called after this function, the
1351  *  EEPROM will most likely contain an invalid checksum.
1352  **/
1353 STATIC s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
1354                                               u16 words, u16 *data)
1355 {
1356         s32 status;
1357         u16 word;
1358         u16 page_size;
1359         u16 i;
1360         u8 write_opcode = IXGBE_EEPROM_WRITE_OPCODE_SPI;
1361
1362         DEBUGFUNC("ixgbe_write_eeprom_buffer_bit_bang");
1363
1364         /* Prepare the EEPROM for writing  */
1365         status = ixgbe_acquire_eeprom(hw);
1366
1367         if (status == IXGBE_SUCCESS) {
1368                 if (ixgbe_ready_eeprom(hw) != IXGBE_SUCCESS) {
1369                         ixgbe_release_eeprom(hw);
1370                         status = IXGBE_ERR_EEPROM;
1371                 }
1372         }
1373
1374         if (status == IXGBE_SUCCESS) {
1375                 for (i = 0; i < words; i++) {
1376                         ixgbe_standby_eeprom(hw);
1377
1378                         /*  Send the WRITE ENABLE command (8 bit opcode )  */
1379                         ixgbe_shift_out_eeprom_bits(hw,
1380                                                    IXGBE_EEPROM_WREN_OPCODE_SPI,
1381                                                    IXGBE_EEPROM_OPCODE_BITS);
1382
1383                         ixgbe_standby_eeprom(hw);
1384
1385                         /*
1386                          * Some SPI eeproms use the 8th address bit embedded
1387                          * in the opcode
1388                          */
1389                         if ((hw->eeprom.address_bits == 8) &&
1390                             ((offset + i) >= 128))
1391                                 write_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
1392
1393                         /* Send the Write command (8-bit opcode + addr) */
1394                         ixgbe_shift_out_eeprom_bits(hw, write_opcode,
1395                                                     IXGBE_EEPROM_OPCODE_BITS);
1396                         ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
1397                                                     hw->eeprom.address_bits);
1398
1399                         page_size = hw->eeprom.word_page_size;
1400
1401                         /* Send the data in burst via SPI*/
1402                         do {
1403                                 word = data[i];
1404                                 word = (word >> 8) | (word << 8);
1405                                 ixgbe_shift_out_eeprom_bits(hw, word, 16);
1406
1407                                 if (page_size == 0)
1408                                         break;
1409
1410                                 /* do not wrap around page */
1411                                 if (((offset + i) & (page_size - 1)) ==
1412                                     (page_size - 1))
1413                                         break;
1414                         } while (++i < words);
1415
1416                         ixgbe_standby_eeprom(hw);
1417                         msec_delay(10);
1418                 }
1419                 /* Done with writing - release the EEPROM */
1420                 ixgbe_release_eeprom(hw);
1421         }
1422
1423         return status;
1424 }
1425
1426 /**
1427  *  ixgbe_write_eeprom_generic - Writes 16 bit value to EEPROM
1428  *  @hw: pointer to hardware structure
1429  *  @offset: offset within the EEPROM to be written to
1430  *  @data: 16 bit word to be written to the EEPROM
1431  *
1432  *  If ixgbe_eeprom_update_checksum is not called after this function, the
1433  *  EEPROM will most likely contain an invalid checksum.
1434  **/
1435 s32 ixgbe_write_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
1436 {
1437         s32 status;
1438
1439         DEBUGFUNC("ixgbe_write_eeprom_generic");
1440
1441         hw->eeprom.ops.init_params(hw);
1442
1443         if (offset >= hw->eeprom.word_size) {
1444                 status = IXGBE_ERR_EEPROM;
1445                 goto out;
1446         }
1447
1448         status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset, 1, &data);
1449
1450 out:
1451         return status;
1452 }
1453
1454 /**
1455  *  ixgbe_read_eeprom_buffer_bit_bang_generic - Read EEPROM using bit-bang
1456  *  @hw: pointer to hardware structure
1457  *  @offset: offset within the EEPROM to be read
1458  *  @data: read 16 bit words(s) from EEPROM
1459  *  @words: number of word(s)
1460  *
1461  *  Reads 16 bit word(s) from EEPROM through bit-bang method
1462  **/
1463 s32 ixgbe_read_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
1464                                               u16 words, u16 *data)
1465 {
1466         s32 status = IXGBE_SUCCESS;
1467         u16 i, count;
1468
1469         DEBUGFUNC("ixgbe_read_eeprom_buffer_bit_bang_generic");
1470
1471         hw->eeprom.ops.init_params(hw);
1472
1473         if (words == 0) {
1474                 status = IXGBE_ERR_INVALID_ARGUMENT;
1475                 goto out;
1476         }
1477
1478         if (offset + words > hw->eeprom.word_size) {
1479                 status = IXGBE_ERR_EEPROM;
1480                 goto out;
1481         }
1482
1483         /*
1484          * We cannot hold synchronization semaphores for too long
1485          * to avoid other entity starvation. However it is more efficient
1486          * to read in bursts than synchronizing access for each word.
1487          */
1488         for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
1489                 count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
1490                         IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
1491
1492                 status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset + i,
1493                                                            count, &data[i]);
1494
1495                 if (status != IXGBE_SUCCESS)
1496                         break;
1497         }
1498
1499 out:
1500         return status;
1501 }
1502
1503 /**
1504  *  ixgbe_read_eeprom_buffer_bit_bang - Read EEPROM using bit-bang
1505  *  @hw: pointer to hardware structure
1506  *  @offset: offset within the EEPROM to be read
1507  *  @words: number of word(s)
1508  *  @data: read 16 bit word(s) from EEPROM
1509  *
1510  *  Reads 16 bit word(s) from EEPROM through bit-bang method
1511  **/
1512 STATIC s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
1513                                              u16 words, u16 *data)
1514 {
1515         s32 status;
1516         u16 word_in;
1517         u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
1518         u16 i;
1519
1520         DEBUGFUNC("ixgbe_read_eeprom_buffer_bit_bang");
1521
1522         /* Prepare the EEPROM for reading  */
1523         status = ixgbe_acquire_eeprom(hw);
1524
1525         if (status == IXGBE_SUCCESS) {
1526                 if (ixgbe_ready_eeprom(hw) != IXGBE_SUCCESS) {
1527                         ixgbe_release_eeprom(hw);
1528                         status = IXGBE_ERR_EEPROM;
1529                 }
1530         }
1531
1532         if (status == IXGBE_SUCCESS) {
1533                 for (i = 0; i < words; i++) {
1534                         ixgbe_standby_eeprom(hw);
1535                         /*
1536                          * Some SPI eeproms use the 8th address bit embedded
1537                          * in the opcode
1538                          */
1539                         if ((hw->eeprom.address_bits == 8) &&
1540                             ((offset + i) >= 128))
1541                                 read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
1542
1543                         /* Send the READ command (opcode + addr) */
1544                         ixgbe_shift_out_eeprom_bits(hw, read_opcode,
1545                                                     IXGBE_EEPROM_OPCODE_BITS);
1546                         ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
1547                                                     hw->eeprom.address_bits);
1548
1549                         /* Read the data. */
1550                         word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
1551                         data[i] = (word_in >> 8) | (word_in << 8);
1552                 }
1553
1554                 /* End this read operation */
1555                 ixgbe_release_eeprom(hw);
1556         }
1557
1558         return status;
1559 }
1560
1561 /**
1562  *  ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
1563  *  @hw: pointer to hardware structure
1564  *  @offset: offset within the EEPROM to be read
1565  *  @data: read 16 bit value from EEPROM
1566  *
1567  *  Reads 16 bit value from EEPROM through bit-bang method
1568  **/
1569 s32 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
1570                                        u16 *data)
1571 {
1572         s32 status;
1573
1574         DEBUGFUNC("ixgbe_read_eeprom_bit_bang_generic");
1575
1576         hw->eeprom.ops.init_params(hw);
1577
1578         if (offset >= hw->eeprom.word_size) {
1579                 status = IXGBE_ERR_EEPROM;
1580                 goto out;
1581         }
1582
1583         status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
1584
1585 out:
1586         return status;
1587 }
1588
1589 /**
1590  *  ixgbe_read_eerd_buffer_generic - Read EEPROM word(s) using EERD
1591  *  @hw: pointer to hardware structure
1592  *  @offset: offset of word in the EEPROM to read
1593  *  @words: number of word(s)
1594  *  @data: 16 bit word(s) from the EEPROM
1595  *
1596  *  Reads a 16 bit word(s) from the EEPROM using the EERD register.
1597  **/
1598 s32 ixgbe_read_eerd_buffer_generic(struct ixgbe_hw *hw, u16 offset,
1599                                    u16 words, u16 *data)
1600 {
1601         u32 eerd;
1602         s32 status = IXGBE_SUCCESS;
1603         u32 i;
1604
1605         DEBUGFUNC("ixgbe_read_eerd_buffer_generic");
1606
1607         hw->eeprom.ops.init_params(hw);
1608
1609         if (words == 0) {
1610                 status = IXGBE_ERR_INVALID_ARGUMENT;
1611                 ERROR_REPORT1(IXGBE_ERROR_ARGUMENT, "Invalid EEPROM words");
1612                 goto out;
1613         }
1614
1615         if (offset >= hw->eeprom.word_size) {
1616                 status = IXGBE_ERR_EEPROM;
1617                 ERROR_REPORT1(IXGBE_ERROR_ARGUMENT, "Invalid EEPROM offset");
1618                 goto out;
1619         }
1620
1621         for (i = 0; i < words; i++) {
1622                 eerd = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) |
1623                        IXGBE_EEPROM_RW_REG_START;
1624
1625                 IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
1626                 status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_READ);
1627
1628                 if (status == IXGBE_SUCCESS) {
1629                         data[i] = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
1630                                    IXGBE_EEPROM_RW_REG_DATA);
1631                 } else {
1632                         DEBUGOUT("Eeprom read timed out\n");
1633                         goto out;
1634                 }
1635         }
1636 out:
1637         return status;
1638 }
1639
1640 /**
1641  *  ixgbe_detect_eeprom_page_size_generic - Detect EEPROM page size
1642  *  @hw: pointer to hardware structure
1643  *  @offset: offset within the EEPROM to be used as a scratch pad
1644  *
1645  *  Discover EEPROM page size by writing marching data at given offset.
1646  *  This function is called only when we are writing a new large buffer
1647  *  at given offset so the data would be overwritten anyway.
1648  **/
1649 STATIC s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
1650                                                  u16 offset)
1651 {
1652         u16 data[IXGBE_EEPROM_PAGE_SIZE_MAX];
1653         s32 status = IXGBE_SUCCESS;
1654         u16 i;
1655
1656         DEBUGFUNC("ixgbe_detect_eeprom_page_size_generic");
1657
1658         for (i = 0; i < IXGBE_EEPROM_PAGE_SIZE_MAX; i++)
1659                 data[i] = i;
1660
1661         hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX;
1662         status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset,
1663                                              IXGBE_EEPROM_PAGE_SIZE_MAX, data);
1664         hw->eeprom.word_page_size = 0;
1665         if (status != IXGBE_SUCCESS)
1666                 goto out;
1667
1668         status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
1669         if (status != IXGBE_SUCCESS)
1670                 goto out;
1671
1672         /*
1673          * When writing in burst more than the actual page size
1674          * EEPROM address wraps around current page.
1675          */
1676         hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX - data[0];
1677
1678         DEBUGOUT1("Detected EEPROM page size = %d words.",
1679                   hw->eeprom.word_page_size);
1680 out:
1681         return status;
1682 }
1683
1684 /**
1685  *  ixgbe_read_eerd_generic - Read EEPROM word using EERD
1686  *  @hw: pointer to hardware structure
1687  *  @offset: offset of  word in the EEPROM to read
1688  *  @data: word read from the EEPROM
1689  *
1690  *  Reads a 16 bit word from the EEPROM using the EERD register.
1691  **/
1692 s32 ixgbe_read_eerd_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
1693 {
1694         return ixgbe_read_eerd_buffer_generic(hw, offset, 1, data);
1695 }
1696
1697 /**
1698  *  ixgbe_write_eewr_buffer_generic - Write EEPROM word(s) using EEWR
1699  *  @hw: pointer to hardware structure
1700  *  @offset: offset of  word in the EEPROM to write
1701  *  @words: number of word(s)
1702  *  @data: word(s) write to the EEPROM
1703  *
1704  *  Write a 16 bit word(s) to the EEPROM using the EEWR register.
1705  **/
1706 s32 ixgbe_write_eewr_buffer_generic(struct ixgbe_hw *hw, u16 offset,
1707                                     u16 words, u16 *data)
1708 {
1709         u32 eewr;
1710         s32 status = IXGBE_SUCCESS;
1711         u16 i;
1712
1713         DEBUGFUNC("ixgbe_write_eewr_generic");
1714
1715         hw->eeprom.ops.init_params(hw);
1716
1717         if (words == 0) {
1718                 status = IXGBE_ERR_INVALID_ARGUMENT;
1719                 ERROR_REPORT1(IXGBE_ERROR_ARGUMENT, "Invalid EEPROM words");
1720                 goto out;
1721         }
1722
1723         if (offset >= hw->eeprom.word_size) {
1724                 status = IXGBE_ERR_EEPROM;
1725                 ERROR_REPORT1(IXGBE_ERROR_ARGUMENT, "Invalid EEPROM offset");
1726                 goto out;
1727         }
1728
1729         for (i = 0; i < words; i++) {
1730                 eewr = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) |
1731                         (data[i] << IXGBE_EEPROM_RW_REG_DATA) |
1732                         IXGBE_EEPROM_RW_REG_START;
1733
1734                 status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
1735                 if (status != IXGBE_SUCCESS) {
1736                         DEBUGOUT("Eeprom write EEWR timed out\n");
1737                         goto out;
1738                 }
1739
1740                 IXGBE_WRITE_REG(hw, IXGBE_EEWR, eewr);
1741
1742                 status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
1743                 if (status != IXGBE_SUCCESS) {
1744                         DEBUGOUT("Eeprom write EEWR timed out\n");
1745                         goto out;
1746                 }
1747         }
1748
1749 out:
1750         return status;
1751 }
1752
1753 /**
1754  *  ixgbe_write_eewr_generic - Write EEPROM word using EEWR
1755  *  @hw: pointer to hardware structure
1756  *  @offset: offset of  word in the EEPROM to write
1757  *  @data: word write to the EEPROM
1758  *
1759  *  Write a 16 bit word to the EEPROM using the EEWR register.
1760  **/
1761 s32 ixgbe_write_eewr_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
1762 {
1763         return ixgbe_write_eewr_buffer_generic(hw, offset, 1, &data);
1764 }
1765
1766 /**
1767  *  ixgbe_poll_eerd_eewr_done - Poll EERD read or EEWR write status
1768  *  @hw: pointer to hardware structure
1769  *  @ee_reg: EEPROM flag for polling
1770  *
1771  *  Polls the status bit (bit 1) of the EERD or EEWR to determine when the
1772  *  read or write is done respectively.
1773  **/
1774 s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg)
1775 {
1776         u32 i;
1777         u32 reg;
1778         s32 status = IXGBE_ERR_EEPROM;
1779
1780         DEBUGFUNC("ixgbe_poll_eerd_eewr_done");
1781
1782         for (i = 0; i < IXGBE_EERD_EEWR_ATTEMPTS; i++) {
1783                 if (ee_reg == IXGBE_NVM_POLL_READ)
1784                         reg = IXGBE_READ_REG(hw, IXGBE_EERD);
1785                 else
1786                         reg = IXGBE_READ_REG(hw, IXGBE_EEWR);
1787
1788                 if (reg & IXGBE_EEPROM_RW_REG_DONE) {
1789                         status = IXGBE_SUCCESS;
1790                         break;
1791                 }
1792                 usec_delay(5);
1793         }
1794
1795         if (i == IXGBE_EERD_EEWR_ATTEMPTS)
1796                 ERROR_REPORT1(IXGBE_ERROR_POLLING,
1797                              "EEPROM read/write done polling timed out");
1798
1799         return status;
1800 }
1801
1802 /**
1803  *  ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
1804  *  @hw: pointer to hardware structure
1805  *
1806  *  Prepares EEPROM for access using bit-bang method. This function should
1807  *  be called before issuing a command to the EEPROM.
1808  **/
1809 STATIC s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
1810 {
1811         s32 status = IXGBE_SUCCESS;
1812         u32 eec;
1813         u32 i;
1814
1815         DEBUGFUNC("ixgbe_acquire_eeprom");
1816
1817         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM)
1818             != IXGBE_SUCCESS)
1819                 status = IXGBE_ERR_SWFW_SYNC;
1820
1821         if (status == IXGBE_SUCCESS) {
1822                 eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
1823
1824                 /* Request EEPROM Access */
1825                 eec |= IXGBE_EEC_REQ;
1826                 IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
1827
1828                 for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
1829                         eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
1830                         if (eec & IXGBE_EEC_GNT)
1831                                 break;
1832                         usec_delay(5);
1833                 }
1834
1835                 /* Release if grant not acquired */
1836                 if (!(eec & IXGBE_EEC_GNT)) {
1837                         eec &= ~IXGBE_EEC_REQ;
1838                         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
1839                         DEBUGOUT("Could not acquire EEPROM grant\n");
1840
1841                         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
1842                         status = IXGBE_ERR_EEPROM;
1843                 }
1844
1845                 /* Setup EEPROM for Read/Write */
1846                 if (status == IXGBE_SUCCESS) {
1847                         /* Clear CS and SK */
1848                         eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
1849                         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
1850                         IXGBE_WRITE_FLUSH(hw);
1851                         usec_delay(1);
1852                 }
1853         }
1854         return status;
1855 }
1856
1857 /**
1858  *  ixgbe_get_eeprom_semaphore - Get hardware semaphore
1859  *  @hw: pointer to hardware structure
1860  *
1861  *  Sets the hardware semaphores so EEPROM access can occur for bit-bang method
1862  **/
1863 STATIC s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
1864 {
1865         s32 status = IXGBE_ERR_EEPROM;
1866         u32 timeout = 2000;
1867         u32 i;
1868         u32 swsm;
1869
1870         DEBUGFUNC("ixgbe_get_eeprom_semaphore");
1871
1872
1873         /* Get SMBI software semaphore between device drivers first */
1874         for (i = 0; i < timeout; i++) {
1875                 /*
1876                  * If the SMBI bit is 0 when we read it, then the bit will be
1877                  * set and we have the semaphore
1878                  */
1879                 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM_BY_MAC(hw));
1880                 if (!(swsm & IXGBE_SWSM_SMBI)) {
1881                         status = IXGBE_SUCCESS;
1882                         break;
1883                 }
1884                 usec_delay(50);
1885         }
1886
1887         if (i == timeout) {
1888                 DEBUGOUT("Driver can't access the Eeprom - SMBI Semaphore "
1889                          "not granted.\n");
1890                 /*
1891                  * this release is particularly important because our attempts
1892                  * above to get the semaphore may have succeeded, and if there
1893                  * was a timeout, we should unconditionally clear the semaphore
1894                  * bits to free the driver to make progress
1895                  */
1896                 ixgbe_release_eeprom_semaphore(hw);
1897
1898                 usec_delay(50);
1899                 /*
1900                  * one last try
1901                  * If the SMBI bit is 0 when we read it, then the bit will be
1902                  * set and we have the semaphore
1903                  */
1904                 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM_BY_MAC(hw));
1905                 if (!(swsm & IXGBE_SWSM_SMBI))
1906                         status = IXGBE_SUCCESS;
1907         }
1908
1909         /* Now get the semaphore between SW/FW through the SWESMBI bit */
1910         if (status == IXGBE_SUCCESS) {
1911                 for (i = 0; i < timeout; i++) {
1912                         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM_BY_MAC(hw));
1913
1914                         /* Set the SW EEPROM semaphore bit to request access */
1915                         swsm |= IXGBE_SWSM_SWESMBI;
1916                         IXGBE_WRITE_REG(hw, IXGBE_SWSM_BY_MAC(hw), swsm);
1917
1918                         /*
1919                          * If we set the bit successfully then we got the
1920                          * semaphore.
1921                          */
1922                         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM_BY_MAC(hw));
1923                         if (swsm & IXGBE_SWSM_SWESMBI)
1924                                 break;
1925
1926                         usec_delay(50);
1927                 }
1928
1929                 /*
1930                  * Release semaphores and return error if SW EEPROM semaphore
1931                  * was not granted because we don't have access to the EEPROM
1932                  */
1933                 if (i >= timeout) {
1934                         ERROR_REPORT1(IXGBE_ERROR_POLLING,
1935                             "SWESMBI Software EEPROM semaphore not granted.\n");
1936                         ixgbe_release_eeprom_semaphore(hw);
1937                         status = IXGBE_ERR_EEPROM;
1938                 }
1939         } else {
1940                 ERROR_REPORT1(IXGBE_ERROR_POLLING,
1941                              "Software semaphore SMBI between device drivers "
1942                              "not granted.\n");
1943         }
1944
1945         return status;
1946 }
1947
1948 /**
1949  *  ixgbe_release_eeprom_semaphore - Release hardware semaphore
1950  *  @hw: pointer to hardware structure
1951  *
1952  *  This function clears hardware semaphore bits.
1953  **/
1954 STATIC void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
1955 {
1956         u32 swsm;
1957
1958         DEBUGFUNC("ixgbe_release_eeprom_semaphore");
1959
1960         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
1961
1962         /* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
1963         swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
1964         IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
1965         IXGBE_WRITE_FLUSH(hw);
1966 }
1967
1968 /**
1969  *  ixgbe_ready_eeprom - Polls for EEPROM ready
1970  *  @hw: pointer to hardware structure
1971  **/
1972 STATIC s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
1973 {
1974         s32 status = IXGBE_SUCCESS;
1975         u16 i;
1976         u8 spi_stat_reg;
1977
1978         DEBUGFUNC("ixgbe_ready_eeprom");
1979
1980         /*
1981          * Read "Status Register" repeatedly until the LSB is cleared.  The
1982          * EEPROM will signal that the command has been completed by clearing
1983          * bit 0 of the internal status register.  If it's not cleared within
1984          * 5 milliseconds, then error out.
1985          */
1986         for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
1987                 ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
1988                                             IXGBE_EEPROM_OPCODE_BITS);
1989                 spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
1990                 if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
1991                         break;
1992
1993                 usec_delay(5);
1994                 ixgbe_standby_eeprom(hw);
1995         };
1996
1997         /*
1998          * On some parts, SPI write time could vary from 0-20mSec on 3.3V
1999          * devices (and only 0-5mSec on 5V devices)
2000          */
2001         if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
2002                 DEBUGOUT("SPI EEPROM Status error\n");
2003                 status = IXGBE_ERR_EEPROM;
2004         }
2005
2006         return status;
2007 }
2008
2009 /**
2010  *  ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
2011  *  @hw: pointer to hardware structure
2012  **/
2013 STATIC void ixgbe_standby_eeprom(struct ixgbe_hw *hw)
2014 {
2015         u32 eec;
2016
2017         DEBUGFUNC("ixgbe_standby_eeprom");
2018
2019         eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
2020
2021         /* Toggle CS to flush commands */
2022         eec |= IXGBE_EEC_CS;
2023         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
2024         IXGBE_WRITE_FLUSH(hw);
2025         usec_delay(1);
2026         eec &= ~IXGBE_EEC_CS;
2027         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
2028         IXGBE_WRITE_FLUSH(hw);
2029         usec_delay(1);
2030 }
2031
2032 /**
2033  *  ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
2034  *  @hw: pointer to hardware structure
2035  *  @data: data to send to the EEPROM
2036  *  @count: number of bits to shift out
2037  **/
2038 STATIC void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
2039                                         u16 count)
2040 {
2041         u32 eec;
2042         u32 mask;
2043         u32 i;
2044
2045         DEBUGFUNC("ixgbe_shift_out_eeprom_bits");
2046
2047         eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
2048
2049         /*
2050          * Mask is used to shift "count" bits of "data" out to the EEPROM
2051          * one bit at a time.  Determine the starting bit based on count
2052          */
2053         mask = 0x01 << (count - 1);
2054
2055         for (i = 0; i < count; i++) {
2056                 /*
2057                  * A "1" is shifted out to the EEPROM by setting bit "DI" to a
2058                  * "1", and then raising and then lowering the clock (the SK
2059                  * bit controls the clock input to the EEPROM).  A "0" is
2060                  * shifted out to the EEPROM by setting "DI" to "0" and then
2061                  * raising and then lowering the clock.
2062                  */
2063                 if (data & mask)
2064                         eec |= IXGBE_EEC_DI;
2065                 else
2066                         eec &= ~IXGBE_EEC_DI;
2067
2068                 IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
2069                 IXGBE_WRITE_FLUSH(hw);
2070
2071                 usec_delay(1);
2072
2073                 ixgbe_raise_eeprom_clk(hw, &eec);
2074                 ixgbe_lower_eeprom_clk(hw, &eec);
2075
2076                 /*
2077                  * Shift mask to signify next bit of data to shift in to the
2078                  * EEPROM
2079                  */
2080                 mask = mask >> 1;
2081         };
2082
2083         /* We leave the "DI" bit set to "0" when we leave this routine. */
2084         eec &= ~IXGBE_EEC_DI;
2085         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
2086         IXGBE_WRITE_FLUSH(hw);
2087 }
2088
2089 /**
2090  *  ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
2091  *  @hw: pointer to hardware structure
2092  **/
2093 STATIC u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
2094 {
2095         u32 eec;
2096         u32 i;
2097         u16 data = 0;
2098
2099         DEBUGFUNC("ixgbe_shift_in_eeprom_bits");
2100
2101         /*
2102          * In order to read a register from the EEPROM, we need to shift
2103          * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
2104          * the clock input to the EEPROM (setting the SK bit), and then reading
2105          * the value of the "DO" bit.  During this "shifting in" process the
2106          * "DI" bit should always be clear.
2107          */
2108         eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
2109
2110         eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
2111
2112         for (i = 0; i < count; i++) {
2113                 data = data << 1;
2114                 ixgbe_raise_eeprom_clk(hw, &eec);
2115
2116                 eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
2117
2118                 eec &= ~(IXGBE_EEC_DI);
2119                 if (eec & IXGBE_EEC_DO)
2120                         data |= 1;
2121
2122                 ixgbe_lower_eeprom_clk(hw, &eec);
2123         }
2124
2125         return data;
2126 }
2127
2128 /**
2129  *  ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
2130  *  @hw: pointer to hardware structure
2131  *  @eec: EEC register's current value
2132  **/
2133 STATIC void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
2134 {
2135         DEBUGFUNC("ixgbe_raise_eeprom_clk");
2136
2137         /*
2138          * Raise the clock input to the EEPROM
2139          * (setting the SK bit), then delay
2140          */
2141         *eec = *eec | IXGBE_EEC_SK;
2142         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), *eec);
2143         IXGBE_WRITE_FLUSH(hw);
2144         usec_delay(1);
2145 }
2146
2147 /**
2148  *  ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
2149  *  @hw: pointer to hardware structure
2150  *  @eecd: EECD's current value
2151  **/
2152 STATIC void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
2153 {
2154         DEBUGFUNC("ixgbe_lower_eeprom_clk");
2155
2156         /*
2157          * Lower the clock input to the EEPROM (clearing the SK bit), then
2158          * delay
2159          */
2160         *eec = *eec & ~IXGBE_EEC_SK;
2161         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), *eec);
2162         IXGBE_WRITE_FLUSH(hw);
2163         usec_delay(1);
2164 }
2165
2166 /**
2167  *  ixgbe_release_eeprom - Release EEPROM, release semaphores
2168  *  @hw: pointer to hardware structure
2169  **/
2170 STATIC void ixgbe_release_eeprom(struct ixgbe_hw *hw)
2171 {
2172         u32 eec;
2173
2174         DEBUGFUNC("ixgbe_release_eeprom");
2175
2176         eec = IXGBE_READ_REG(hw, IXGBE_EEC_BY_MAC(hw));
2177
2178         eec |= IXGBE_EEC_CS;  /* Pull CS high */
2179         eec &= ~IXGBE_EEC_SK; /* Lower SCK */
2180
2181         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
2182         IXGBE_WRITE_FLUSH(hw);
2183
2184         usec_delay(1);
2185
2186         /* Stop requesting EEPROM access */
2187         eec &= ~IXGBE_EEC_REQ;
2188         IXGBE_WRITE_REG(hw, IXGBE_EEC_BY_MAC(hw), eec);
2189
2190         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
2191
2192         /* Delay before attempt to obtain semaphore again to allow FW access */
2193         msec_delay(hw->eeprom.semaphore_delay);
2194 }
2195
2196 /**
2197  *  ixgbe_calc_eeprom_checksum_generic - Calculates and returns the checksum
2198  *  @hw: pointer to hardware structure
2199  *
2200  *  Returns a negative error code on error, or the 16-bit checksum
2201  **/
2202 s32 ixgbe_calc_eeprom_checksum_generic(struct ixgbe_hw *hw)
2203 {
2204         u16 i;
2205         u16 j;
2206         u16 checksum = 0;
2207         u16 length = 0;
2208         u16 pointer = 0;
2209         u16 word = 0;
2210
2211         DEBUGFUNC("ixgbe_calc_eeprom_checksum_generic");
2212
2213         /* Include 0x0-0x3F in the checksum */
2214         for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
2215                 if (hw->eeprom.ops.read(hw, i, &word)) {
2216                         DEBUGOUT("EEPROM read failed\n");
2217                         return IXGBE_ERR_EEPROM;
2218                 }
2219                 checksum += word;
2220         }
2221
2222         /* Include all data from pointers except for the fw pointer */
2223         for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
2224                 if (hw->eeprom.ops.read(hw, i, &pointer)) {
2225                         DEBUGOUT("EEPROM read failed\n");
2226                         return IXGBE_ERR_EEPROM;
2227                 }
2228
2229                 /* If the pointer seems invalid */
2230                 if (pointer == 0xFFFF || pointer == 0)
2231                         continue;
2232
2233                 if (hw->eeprom.ops.read(hw, pointer, &length)) {
2234                         DEBUGOUT("EEPROM read failed\n");
2235                         return IXGBE_ERR_EEPROM;
2236                 }
2237
2238                 if (length == 0xFFFF || length == 0)
2239                         continue;
2240
2241                 for (j = pointer + 1; j <= pointer + length; j++) {
2242                         if (hw->eeprom.ops.read(hw, j, &word)) {
2243                                 DEBUGOUT("EEPROM read failed\n");
2244                                 return IXGBE_ERR_EEPROM;
2245                         }
2246                         checksum += word;
2247                 }
2248         }
2249
2250         checksum = (u16)IXGBE_EEPROM_SUM - checksum;
2251
2252         return (s32)checksum;
2253 }
2254
2255 /**
2256  *  ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
2257  *  @hw: pointer to hardware structure
2258  *  @checksum_val: calculated checksum
2259  *
2260  *  Performs checksum calculation and validates the EEPROM checksum.  If the
2261  *  caller does not need checksum_val, the value can be NULL.
2262  **/
2263 s32 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
2264                                            u16 *checksum_val)
2265 {
2266         s32 status;
2267         u16 checksum;
2268         u16 read_checksum = 0;
2269
2270         DEBUGFUNC("ixgbe_validate_eeprom_checksum_generic");
2271
2272         /* Read the first word from the EEPROM. If this times out or fails, do
2273          * not continue or we could be in for a very long wait while every
2274          * EEPROM read fails
2275          */
2276         status = hw->eeprom.ops.read(hw, 0, &checksum);
2277         if (status) {
2278                 DEBUGOUT("EEPROM read failed\n");
2279                 return status;
2280         }
2281
2282         status = hw->eeprom.ops.calc_checksum(hw);
2283         if (status < 0)
2284                 return status;
2285
2286         checksum = (u16)(status & 0xffff);
2287
2288         status = hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
2289         if (status) {
2290                 DEBUGOUT("EEPROM read failed\n");
2291                 return status;
2292         }
2293
2294         /* Verify read checksum from EEPROM is the same as
2295          * calculated checksum
2296          */
2297         if (read_checksum != checksum)
2298                 status = IXGBE_ERR_EEPROM_CHECKSUM;
2299
2300         /* If the user cares, return the calculated checksum */
2301         if (checksum_val)
2302                 *checksum_val = checksum;
2303
2304         return status;
2305 }
2306
2307 /**
2308  *  ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
2309  *  @hw: pointer to hardware structure
2310  **/
2311 s32 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
2312 {
2313         s32 status;
2314         u16 checksum;
2315
2316         DEBUGFUNC("ixgbe_update_eeprom_checksum_generic");
2317
2318         /* Read the first word from the EEPROM. If this times out or fails, do
2319          * not continue or we could be in for a very long wait while every
2320          * EEPROM read fails
2321          */
2322         status = hw->eeprom.ops.read(hw, 0, &checksum);
2323         if (status) {
2324                 DEBUGOUT("EEPROM read failed\n");
2325                 return status;
2326         }
2327
2328         status = hw->eeprom.ops.calc_checksum(hw);
2329         if (status < 0)
2330                 return status;
2331
2332         checksum = (u16)(status & 0xffff);
2333
2334         status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM, checksum);
2335
2336         return status;
2337 }
2338
2339 /**
2340  *  ixgbe_validate_mac_addr - Validate MAC address
2341  *  @mac_addr: pointer to MAC address.
2342  *
2343  *  Tests a MAC address to ensure it is a valid Individual Address.
2344  **/
2345 s32 ixgbe_validate_mac_addr(u8 *mac_addr)
2346 {
2347         s32 status = IXGBE_SUCCESS;
2348
2349         DEBUGFUNC("ixgbe_validate_mac_addr");
2350
2351         /* Make sure it is not a multicast address */
2352         if (IXGBE_IS_MULTICAST(mac_addr)) {
2353                 status = IXGBE_ERR_INVALID_MAC_ADDR;
2354         /* Not a broadcast address */
2355         } else if (IXGBE_IS_BROADCAST(mac_addr)) {
2356                 status = IXGBE_ERR_INVALID_MAC_ADDR;
2357         /* Reject the zero address */
2358         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
2359                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
2360                 status = IXGBE_ERR_INVALID_MAC_ADDR;
2361         }
2362         return status;
2363 }
2364
2365 /**
2366  *  ixgbe_set_rar_generic - Set Rx address register
2367  *  @hw: pointer to hardware structure
2368  *  @index: Receive address register to write
2369  *  @addr: Address to put into receive address register
2370  *  @vmdq: VMDq "set" or "pool" index
2371  *  @enable_addr: set flag that address is active
2372  *
2373  *  Puts an ethernet address into a receive address register.
2374  **/
2375 s32 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
2376                           u32 enable_addr)
2377 {
2378         u32 rar_low, rar_high;
2379         u32 rar_entries = hw->mac.num_rar_entries;
2380
2381         DEBUGFUNC("ixgbe_set_rar_generic");
2382
2383         /* Make sure we are using a valid rar index range */
2384         if (index >= rar_entries) {
2385                 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT,
2386                              "RAR index %d is out of range.\n", index);
2387                 return IXGBE_ERR_INVALID_ARGUMENT;
2388         }
2389
2390         /* setup VMDq pool selection before this RAR gets enabled */
2391         hw->mac.ops.set_vmdq(hw, index, vmdq);
2392
2393         /*
2394          * HW expects these in little endian so we reverse the byte
2395          * order from network order (big endian) to little endian
2396          */
2397         rar_low = ((u32)addr[0] |
2398                    ((u32)addr[1] << 8) |
2399                    ((u32)addr[2] << 16) |
2400                    ((u32)addr[3] << 24));
2401         /*
2402          * Some parts put the VMDq setting in the extra RAH bits,
2403          * so save everything except the lower 16 bits that hold part
2404          * of the address and the address valid bit.
2405          */
2406         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
2407         rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
2408         rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
2409
2410         if (enable_addr != 0)
2411                 rar_high |= IXGBE_RAH_AV;
2412
2413         IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
2414         IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
2415
2416         return IXGBE_SUCCESS;
2417 }
2418
2419 /**
2420  *  ixgbe_clear_rar_generic - Remove Rx address register
2421  *  @hw: pointer to hardware structure
2422  *  @index: Receive address register to write
2423  *
2424  *  Clears an ethernet address from a receive address register.
2425  **/
2426 s32 ixgbe_clear_rar_generic(struct ixgbe_hw *hw, u32 index)
2427 {
2428         u32 rar_high;
2429         u32 rar_entries = hw->mac.num_rar_entries;
2430
2431         DEBUGFUNC("ixgbe_clear_rar_generic");
2432
2433         /* Make sure we are using a valid rar index range */
2434         if (index >= rar_entries) {
2435                 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT,
2436                              "RAR index %d is out of range.\n", index);
2437                 return IXGBE_ERR_INVALID_ARGUMENT;
2438         }
2439
2440         /*
2441          * Some parts put the VMDq setting in the extra RAH bits,
2442          * so save everything except the lower 16 bits that hold part
2443          * of the address and the address valid bit.
2444          */
2445         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
2446         rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
2447
2448         IXGBE_WRITE_REG(hw, IXGBE_RAL(index), 0);
2449         IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
2450
2451         /* clear VMDq pool/queue selection for this RAR */
2452         hw->mac.ops.clear_vmdq(hw, index, IXGBE_CLEAR_VMDQ_ALL);
2453
2454         return IXGBE_SUCCESS;
2455 }
2456
2457 /**
2458  *  ixgbe_init_rx_addrs_generic - Initializes receive address filters.
2459  *  @hw: pointer to hardware structure
2460  *
2461  *  Places the MAC address in receive address register 0 and clears the rest
2462  *  of the receive address registers. Clears the multicast table. Assumes
2463  *  the receiver is in reset when the routine is called.
2464  **/
2465 s32 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
2466 {
2467         u32 i;
2468         u32 rar_entries = hw->mac.num_rar_entries;
2469
2470         DEBUGFUNC("ixgbe_init_rx_addrs_generic");
2471
2472         /*
2473          * If the current mac address is valid, assume it is a software override
2474          * to the permanent address.
2475          * Otherwise, use the permanent address from the eeprom.
2476          */
2477         if (ixgbe_validate_mac_addr(hw->mac.addr) ==
2478             IXGBE_ERR_INVALID_MAC_ADDR) {
2479                 /* Get the MAC address from the RAR0 for later reference */
2480                 hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2481
2482                 DEBUGOUT3(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
2483                           hw->mac.addr[0], hw->mac.addr[1],
2484                           hw->mac.addr[2]);
2485                 DEBUGOUT3("%.2X %.2X %.2X\n", hw->mac.addr[3],
2486                           hw->mac.addr[4], hw->mac.addr[5]);
2487         } else {
2488                 /* Setup the receive address. */
2489                 DEBUGOUT("Overriding MAC Address in RAR[0]\n");
2490                 DEBUGOUT3(" New MAC Addr =%.2X %.2X %.2X ",
2491                           hw->mac.addr[0], hw->mac.addr[1],
2492                           hw->mac.addr[2]);
2493                 DEBUGOUT3("%.2X %.2X %.2X\n", hw->mac.addr[3],
2494                           hw->mac.addr[4], hw->mac.addr[5]);
2495
2496                 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
2497         }
2498
2499         /* clear VMDq pool/queue selection for RAR 0 */
2500         hw->mac.ops.clear_vmdq(hw, 0, IXGBE_CLEAR_VMDQ_ALL);
2501
2502         hw->addr_ctrl.overflow_promisc = 0;
2503
2504         hw->addr_ctrl.rar_used_count = 1;
2505
2506         /* Zero out the other receive addresses. */
2507         DEBUGOUT1("Clearing RAR[1-%d]\n", rar_entries - 1);
2508         for (i = 1; i < rar_entries; i++) {
2509                 IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
2510                 IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
2511         }
2512
2513         /* Clear the MTA */
2514         hw->addr_ctrl.mta_in_use = 0;
2515         IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
2516
2517         DEBUGOUT(" Clearing MTA\n");
2518         for (i = 0; i < hw->mac.mcft_size; i++)
2519                 IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
2520
2521         ixgbe_init_uta_tables(hw);
2522
2523         return IXGBE_SUCCESS;
2524 }
2525
2526 /**
2527  *  ixgbe_add_uc_addr - Adds a secondary unicast address.
2528  *  @hw: pointer to hardware structure
2529  *  @addr: new address
2530  *
2531  *  Adds it to unused receive address register or goes into promiscuous mode.
2532  **/
2533 void ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq)
2534 {
2535         u32 rar_entries = hw->mac.num_rar_entries;
2536         u32 rar;
2537
2538         DEBUGFUNC("ixgbe_add_uc_addr");
2539
2540         DEBUGOUT6(" UC Addr = %.2X %.2X %.2X %.2X %.2X %.2X\n",
2541                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
2542
2543         /*
2544          * Place this address in the RAR if there is room,
2545          * else put the controller into promiscuous mode
2546          */
2547         if (hw->addr_ctrl.rar_used_count < rar_entries) {
2548                 rar = hw->addr_ctrl.rar_used_count;
2549                 hw->mac.ops.set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
2550                 DEBUGOUT1("Added a secondary address to RAR[%d]\n", rar);
2551                 hw->addr_ctrl.rar_used_count++;
2552         } else {
2553                 hw->addr_ctrl.overflow_promisc++;
2554         }
2555
2556         DEBUGOUT("ixgbe_add_uc_addr Complete\n");
2557 }
2558
2559 /**
2560  *  ixgbe_update_uc_addr_list_generic - Updates MAC list of secondary addresses
2561  *  @hw: pointer to hardware structure
2562  *  @addr_list: the list of new addresses
2563  *  @addr_count: number of addresses
2564  *  @next: iterator function to walk the address list
2565  *
2566  *  The given list replaces any existing list.  Clears the secondary addrs from
2567  *  receive address registers.  Uses unused receive address registers for the
2568  *  first secondary addresses, and falls back to promiscuous mode as needed.
2569  *
2570  *  Drivers using secondary unicast addresses must set user_set_promisc when
2571  *  manually putting the device into promiscuous mode.
2572  **/
2573 s32 ixgbe_update_uc_addr_list_generic(struct ixgbe_hw *hw, u8 *addr_list,
2574                                       u32 addr_count, ixgbe_mc_addr_itr next)
2575 {
2576         u8 *addr;
2577         u32 i;
2578         u32 old_promisc_setting = hw->addr_ctrl.overflow_promisc;
2579         u32 uc_addr_in_use;
2580         u32 fctrl;
2581         u32 vmdq;
2582
2583         DEBUGFUNC("ixgbe_update_uc_addr_list_generic");
2584
2585         /*
2586          * Clear accounting of old secondary address list,
2587          * don't count RAR[0]
2588          */
2589         uc_addr_in_use = hw->addr_ctrl.rar_used_count - 1;
2590         hw->addr_ctrl.rar_used_count -= uc_addr_in_use;
2591         hw->addr_ctrl.overflow_promisc = 0;
2592
2593         /* Zero out the other receive addresses */
2594         DEBUGOUT1("Clearing RAR[1-%d]\n", uc_addr_in_use+1);
2595         for (i = 0; i < uc_addr_in_use; i++) {
2596                 IXGBE_WRITE_REG(hw, IXGBE_RAL(1+i), 0);
2597                 IXGBE_WRITE_REG(hw, IXGBE_RAH(1+i), 0);
2598         }
2599
2600         /* Add the new addresses */
2601         for (i = 0; i < addr_count; i++) {
2602                 DEBUGOUT(" Adding the secondary addresses:\n");
2603                 addr = next(hw, &addr_list, &vmdq);
2604                 ixgbe_add_uc_addr(hw, addr, vmdq);
2605         }
2606
2607         if (hw->addr_ctrl.overflow_promisc) {
2608                 /* enable promisc if not already in overflow or set by user */
2609                 if (!old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
2610                         DEBUGOUT(" Entering address overflow promisc mode\n");
2611                         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
2612                         fctrl |= IXGBE_FCTRL_UPE;
2613                         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
2614                 }
2615         } else {
2616                 /* only disable if set by overflow, not by user */
2617                 if (old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
2618                         DEBUGOUT(" Leaving address overflow promisc mode\n");
2619                         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
2620                         fctrl &= ~IXGBE_FCTRL_UPE;
2621                         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
2622                 }
2623         }
2624
2625         DEBUGOUT("ixgbe_update_uc_addr_list_generic Complete\n");
2626         return IXGBE_SUCCESS;
2627 }
2628
2629 /**
2630  *  ixgbe_mta_vector - Determines bit-vector in multicast table to set
2631  *  @hw: pointer to hardware structure
2632  *  @mc_addr: the multicast address
2633  *
2634  *  Extracts the 12 bits, from a multicast address, to determine which
2635  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
2636  *  incoming rx multicast addresses, to determine the bit-vector to check in
2637  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
2638  *  by the MO field of the MCSTCTRL. The MO field is set during initialization
2639  *  to mc_filter_type.
2640  **/
2641 STATIC s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
2642 {
2643         u32 vector = 0;
2644
2645         DEBUGFUNC("ixgbe_mta_vector");
2646
2647         switch (hw->mac.mc_filter_type) {
2648         case 0:   /* use bits [47:36] of the address */
2649                 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
2650                 break;
2651         case 1:   /* use bits [46:35] of the address */
2652                 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
2653                 break;
2654         case 2:   /* use bits [45:34] of the address */
2655                 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
2656                 break;
2657         case 3:   /* use bits [43:32] of the address */
2658                 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
2659                 break;
2660         default:  /* Invalid mc_filter_type */
2661                 DEBUGOUT("MC filter type param set incorrectly\n");
2662                 ASSERT(0);
2663                 break;
2664         }
2665
2666         /* vector can only be 12-bits or boundary will be exceeded */
2667         vector &= 0xFFF;
2668         return vector;
2669 }
2670
2671 /**
2672  *  ixgbe_set_mta - Set bit-vector in multicast table
2673  *  @hw: pointer to hardware structure
2674  *  @hash_value: Multicast address hash value
2675  *
2676  *  Sets the bit-vector in the multicast table.
2677  **/
2678 void ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
2679 {
2680         u32 vector;
2681         u32 vector_bit;
2682         u32 vector_reg;
2683
2684         DEBUGFUNC("ixgbe_set_mta");
2685
2686         hw->addr_ctrl.mta_in_use++;
2687
2688         vector = ixgbe_mta_vector(hw, mc_addr);
2689         DEBUGOUT1(" bit-vector = 0x%03X\n", vector);
2690
2691         /*
2692          * The MTA is a register array of 128 32-bit registers. It is treated
2693          * like an array of 4096 bits.  We want to set bit
2694          * BitArray[vector_value]. So we figure out what register the bit is
2695          * in, read it, OR in the new bit, then write back the new value.  The
2696          * register is determined by the upper 7 bits of the vector value and
2697          * the bit within that register are determined by the lower 5 bits of
2698          * the value.
2699          */
2700         vector_reg = (vector >> 5) & 0x7F;
2701         vector_bit = vector & 0x1F;
2702         hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
2703 }
2704
2705 /**
2706  *  ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
2707  *  @hw: pointer to hardware structure
2708  *  @mc_addr_list: the list of new multicast addresses
2709  *  @mc_addr_count: number of addresses
2710  *  @next: iterator function to walk the multicast address list
2711  *  @clear: flag, when set clears the table beforehand
2712  *
2713  *  When the clear flag is set, the given list replaces any existing list.
2714  *  Hashes the given addresses into the multicast table.
2715  **/
2716 s32 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw, u8 *mc_addr_list,
2717                                       u32 mc_addr_count, ixgbe_mc_addr_itr next,
2718                                       bool clear)
2719 {
2720         u32 i;
2721         u32 vmdq;
2722
2723         DEBUGFUNC("ixgbe_update_mc_addr_list_generic");
2724
2725         /*
2726          * Set the new number of MC addresses that we are being requested to
2727          * use.
2728          */
2729         hw->addr_ctrl.num_mc_addrs = mc_addr_count;
2730         hw->addr_ctrl.mta_in_use = 0;
2731
2732         /* Clear mta_shadow */
2733         if (clear) {
2734                 DEBUGOUT(" Clearing MTA\n");
2735                 memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
2736         }
2737
2738         /* Update mta_shadow */
2739         for (i = 0; i < mc_addr_count; i++) {
2740                 DEBUGOUT(" Adding the multicast addresses:\n");
2741                 ixgbe_set_mta(hw, next(hw, &mc_addr_list, &vmdq));
2742         }
2743
2744         /* Enable mta */
2745         for (i = 0; i < hw->mac.mcft_size; i++)
2746                 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_MTA(0), i,
2747                                       hw->mac.mta_shadow[i]);
2748
2749         if (hw->addr_ctrl.mta_in_use > 0)
2750                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
2751                                 IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
2752
2753         DEBUGOUT("ixgbe_update_mc_addr_list_generic Complete\n");
2754         return IXGBE_SUCCESS;
2755 }
2756
2757 /**
2758  *  ixgbe_enable_mc_generic - Enable multicast address in RAR
2759  *  @hw: pointer to hardware structure
2760  *
2761  *  Enables multicast address in RAR and the use of the multicast hash table.
2762  **/
2763 s32 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
2764 {
2765         struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
2766
2767         DEBUGFUNC("ixgbe_enable_mc_generic");
2768
2769         if (a->mta_in_use > 0)
2770                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
2771                                 hw->mac.mc_filter_type);
2772
2773         return IXGBE_SUCCESS;
2774 }
2775
2776 /**
2777  *  ixgbe_disable_mc_generic - Disable multicast address in RAR
2778  *  @hw: pointer to hardware structure
2779  *
2780  *  Disables multicast address in RAR and the use of the multicast hash table.
2781  **/
2782 s32 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
2783 {
2784         struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
2785
2786         DEBUGFUNC("ixgbe_disable_mc_generic");
2787
2788         if (a->mta_in_use > 0)
2789                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
2790
2791         return IXGBE_SUCCESS;
2792 }
2793
2794 /**
2795  *  ixgbe_fc_enable_generic - Enable flow control
2796  *  @hw: pointer to hardware structure
2797  *
2798  *  Enable flow control according to the current settings.
2799  **/
2800 s32 ixgbe_fc_enable_generic(struct ixgbe_hw *hw)
2801 {
2802         s32 ret_val = IXGBE_SUCCESS;
2803         u32 mflcn_reg, fccfg_reg;
2804         u32 reg;
2805         u32 fcrtl, fcrth;
2806         int i;
2807
2808         DEBUGFUNC("ixgbe_fc_enable_generic");
2809
2810         /* Validate the water mark configuration */
2811         if (!hw->fc.pause_time) {
2812                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2813                 goto out;
2814         }
2815
2816         /* Low water mark of zero causes XOFF floods */
2817         for (i = 0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
2818                 if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
2819                     hw->fc.high_water[i]) {
2820                         if (!hw->fc.low_water[i] ||
2821                             hw->fc.low_water[i] >= hw->fc.high_water[i]) {
2822                                 DEBUGOUT("Invalid water mark configuration\n");
2823                                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2824                                 goto out;
2825                         }
2826                 }
2827         }
2828
2829         /* Negotiate the fc mode to use */
2830         hw->mac.ops.fc_autoneg(hw);
2831
2832         /* Disable any previous flow control settings */
2833         mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
2834         mflcn_reg &= ~(IXGBE_MFLCN_RPFCE_MASK | IXGBE_MFLCN_RFCE);
2835
2836         fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
2837         fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
2838
2839         /*
2840          * The possible values of fc.current_mode are:
2841          * 0: Flow control is completely disabled
2842          * 1: Rx flow control is enabled (we can receive pause frames,
2843          *    but not send pause frames).
2844          * 2: Tx flow control is enabled (we can send pause frames but
2845          *    we do not support receiving pause frames).
2846          * 3: Both Rx and Tx flow control (symmetric) are enabled.
2847          * other: Invalid.
2848          */
2849         switch (hw->fc.current_mode) {
2850         case ixgbe_fc_none:
2851                 /*
2852                  * Flow control is disabled by software override or autoneg.
2853                  * The code below will actually disable it in the HW.
2854                  */
2855                 break;
2856         case ixgbe_fc_rx_pause:
2857                 /*
2858                  * Rx Flow control is enabled and Tx Flow control is
2859                  * disabled by software override. Since there really
2860                  * isn't a way to advertise that we are capable of RX
2861                  * Pause ONLY, we will advertise that we support both
2862                  * symmetric and asymmetric Rx PAUSE.  Later, we will
2863                  * disable the adapter's ability to send PAUSE frames.
2864                  */
2865                 mflcn_reg |= IXGBE_MFLCN_RFCE;
2866                 break;
2867         case ixgbe_fc_tx_pause:
2868                 /*
2869                  * Tx Flow control is enabled, and Rx Flow control is
2870                  * disabled by software override.
2871                  */
2872                 fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
2873                 break;
2874         case ixgbe_fc_full:
2875                 /* Flow control (both Rx and Tx) is enabled by SW override. */
2876                 mflcn_reg |= IXGBE_MFLCN_RFCE;
2877                 fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
2878                 break;
2879         default:
2880                 ERROR_REPORT1(IXGBE_ERROR_ARGUMENT,
2881                              "Flow control param set incorrectly\n");
2882                 ret_val = IXGBE_ERR_CONFIG;
2883                 goto out;
2884                 break;
2885         }
2886
2887         /* Set 802.3x based flow control settings. */
2888         mflcn_reg |= IXGBE_MFLCN_DPF;
2889         IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
2890         IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
2891
2892
2893         /* Set up and enable Rx high/low water mark thresholds, enable XON. */
2894         for (i = 0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
2895                 if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
2896                     hw->fc.high_water[i]) {
2897                         fcrtl = (hw->fc.low_water[i] << 10) | IXGBE_FCRTL_XONE;
2898                         IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), fcrtl);
2899                         fcrth = (hw->fc.high_water[i] << 10) | IXGBE_FCRTH_FCEN;
2900                 } else {
2901                         IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), 0);
2902                         /*
2903                          * In order to prevent Tx hangs when the internal Tx
2904                          * switch is enabled we must set the high water mark
2905                          * to the Rx packet buffer size - 24KB.  This allows
2906                          * the Tx switch to function even under heavy Rx
2907                          * workloads.
2908                          */
2909                         fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(i)) - 24576;
2910                 }
2911
2912                 IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(i), fcrth);
2913         }
2914
2915         /* Configure pause time (2 TCs per register) */
2916         reg = hw->fc.pause_time * 0x00010001;
2917         for (i = 0; i < (IXGBE_DCB_MAX_TRAFFIC_CLASS / 2); i++)
2918                 IXGBE_WRITE_REG(hw, IXGBE_FCTTV(i), reg);
2919
2920         /* Configure flow control refresh threshold value */
2921         IXGBE_WRITE_REG(hw, IXGBE_FCRTV, hw->fc.pause_time / 2);
2922
2923 out:
2924         return ret_val;
2925 }
2926
2927 /**
2928  *  ixgbe_negotiate_fc - Negotiate flow control
2929  *  @hw: pointer to hardware structure
2930  *  @adv_reg: flow control advertised settings
2931  *  @lp_reg: link partner's flow control settings
2932  *  @adv_sym: symmetric pause bit in advertisement
2933  *  @adv_asm: asymmetric pause bit in advertisement
2934  *  @lp_sym: symmetric pause bit in link partner advertisement
2935  *  @lp_asm: asymmetric pause bit in link partner advertisement
2936  *
2937  *  Find the intersection between advertised settings and link partner's
2938  *  advertised settings
2939  **/
2940 s32 ixgbe_negotiate_fc(struct ixgbe_hw *hw, u32 adv_reg, u32 lp_reg,
2941                        u32 adv_sym, u32 adv_asm, u32 lp_sym, u32 lp_asm)
2942 {
2943         if ((!(adv_reg)) ||  (!(lp_reg))) {
2944                 ERROR_REPORT3(IXGBE_ERROR_UNSUPPORTED,
2945                              "Local or link partner's advertised flow control "
2946                              "settings are NULL. Local: %x, link partner: %x\n",
2947                              adv_reg, lp_reg);
2948                 return IXGBE_ERR_FC_NOT_NEGOTIATED;
2949         }
2950
2951         if ((adv_reg & adv_sym) && (lp_reg & lp_sym)) {
2952                 /*
2953                  * Now we need to check if the user selected Rx ONLY
2954                  * of pause frames.  In this case, we had to advertise
2955                  * FULL flow control because we could not advertise RX
2956                  * ONLY. Hence, we must now check to see if we need to
2957                  * turn OFF the TRANSMISSION of PAUSE frames.
2958                  */
2959                 if (hw->fc.requested_mode == ixgbe_fc_full) {
2960                         hw->fc.current_mode = ixgbe_fc_full;
2961                         DEBUGOUT("Flow Control = FULL.\n");
2962                 } else {
2963                         hw->fc.current_mode = ixgbe_fc_rx_pause;
2964                         DEBUGOUT("Flow Control=RX PAUSE frames only\n");
2965                 }
2966         } else if (!(adv_reg & adv_sym) && (adv_reg & adv_asm) &&
2967                    (lp_reg & lp_sym) && (lp_reg & lp_asm)) {
2968                 hw->fc.current_mode = ixgbe_fc_tx_pause;
2969                 DEBUGOUT("Flow Control = TX PAUSE frames only.\n");
2970         } else if ((adv_reg & adv_sym) && (adv_reg & adv_asm) &&
2971                    !(lp_reg & lp_sym) && (lp_reg & lp_asm)) {
2972                 hw->fc.current_mode = ixgbe_fc_rx_pause;
2973                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2974         } else {
2975                 hw->fc.current_mode = ixgbe_fc_none;
2976                 DEBUGOUT("Flow Control = NONE.\n");
2977         }
2978         return IXGBE_SUCCESS;
2979 }
2980
2981 /**
2982  *  ixgbe_fc_autoneg_fiber - Enable flow control on 1 gig fiber
2983  *  @hw: pointer to hardware structure
2984  *
2985  *  Enable flow control according on 1 gig fiber.
2986  **/
2987 STATIC s32 ixgbe_fc_autoneg_fiber(struct ixgbe_hw *hw)
2988 {
2989         u32 pcs_anadv_reg, pcs_lpab_reg, linkstat;
2990         s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
2991
2992         /*
2993          * On multispeed fiber at 1g, bail out if
2994          * - link is up but AN did not complete, or if
2995          * - link is up and AN completed but timed out
2996          */
2997
2998         linkstat = IXGBE_READ_REG(hw, IXGBE_PCS1GLSTA);
2999         if ((!!(linkstat & IXGBE_PCS1GLSTA_AN_COMPLETE) == 0) ||
3000             (!!(linkstat & IXGBE_PCS1GLSTA_AN_TIMED_OUT) == 1)) {
3001                 DEBUGOUT("Auto-Negotiation did not complete or timed out\n");
3002                 goto out;
3003         }
3004
3005         pcs_anadv_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
3006         pcs_lpab_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANLP);
3007
3008         ret_val =  ixgbe_negotiate_fc(hw, pcs_anadv_reg,
3009                                       pcs_lpab_reg, IXGBE_PCS1GANA_SYM_PAUSE,
3010                                       IXGBE_PCS1GANA_ASM_PAUSE,
3011                                       IXGBE_PCS1GANA_SYM_PAUSE,
3012                                       IXGBE_PCS1GANA_ASM_PAUSE);
3013
3014 out:
3015         return ret_val;
3016 }
3017
3018 /**
3019  *  ixgbe_fc_autoneg_backplane - Enable flow control IEEE clause 37
3020  *  @hw: pointer to hardware structure
3021  *
3022  *  Enable flow control according to IEEE clause 37.
3023  **/
3024 STATIC s32 ixgbe_fc_autoneg_backplane(struct ixgbe_hw *hw)
3025 {
3026         u32 links2, anlp1_reg, autoc_reg, links;
3027         s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
3028
3029         /*
3030          * On backplane, bail out if
3031          * - backplane autoneg was not completed, or if
3032          * - we are 82599 and link partner is not AN enabled
3033          */
3034         links = IXGBE_READ_REG(hw, IXGBE_LINKS);
3035         if ((links & IXGBE_LINKS_KX_AN_COMP) == 0) {
3036                 DEBUGOUT("Auto-Negotiation did not complete\n");
3037                 goto out;
3038         }
3039
3040         if (hw->mac.type == ixgbe_mac_82599EB) {
3041                 links2 = IXGBE_READ_REG(hw, IXGBE_LINKS2);
3042                 if ((links2 & IXGBE_LINKS2_AN_SUPPORTED) == 0) {
3043                         DEBUGOUT("Link partner is not AN enabled\n");
3044                         goto out;
3045                 }
3046         }
3047         /*
3048          * Read the 10g AN autoc and LP ability registers and resolve
3049          * local flow control settings accordingly
3050          */
3051         autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
3052         anlp1_reg = IXGBE_READ_REG(hw, IXGBE_ANLP1);
3053
3054         ret_val = ixgbe_negotiate_fc(hw, autoc_reg,
3055                 anlp1_reg, IXGBE_AUTOC_SYM_PAUSE, IXGBE_AUTOC_ASM_PAUSE,
3056                 IXGBE_ANLP1_SYM_PAUSE, IXGBE_ANLP1_ASM_PAUSE);
3057
3058 out:
3059         return ret_val;
3060 }
3061
3062 /**
3063  *  ixgbe_fc_autoneg_copper - Enable flow control IEEE clause 37
3064  *  @hw: pointer to hardware structure
3065  *
3066  *  Enable flow control according to IEEE clause 37.
3067  **/
3068 STATIC s32 ixgbe_fc_autoneg_copper(struct ixgbe_hw *hw)
3069 {
3070         u16 technology_ability_reg = 0;
3071         u16 lp_technology_ability_reg = 0;
3072
3073         hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_ADVT,
3074                              IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
3075                              &technology_ability_reg);
3076         hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_LP,
3077                              IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
3078                              &lp_technology_ability_reg);
3079
3080         return ixgbe_negotiate_fc(hw, (u32)technology_ability_reg,
3081                                   (u32)lp_technology_ability_reg,
3082                                   IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE,
3083                                   IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE);
3084 }
3085
3086 /**
3087  *  ixgbe_fc_autoneg - Configure flow control
3088  *  @hw: pointer to hardware structure
3089  *
3090  *  Compares our advertised flow control capabilities to those advertised by
3091  *  our link partner, and determines the proper flow control mode to use.
3092  **/
3093 void ixgbe_fc_autoneg(struct ixgbe_hw *hw)
3094 {
3095         s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
3096         ixgbe_link_speed speed;
3097         bool link_up;
3098
3099         DEBUGFUNC("ixgbe_fc_autoneg");
3100
3101         /*
3102          * AN should have completed when the cable was plugged in.
3103          * Look for reasons to bail out.  Bail out if:
3104          * - FC autoneg is disabled, or if
3105          * - link is not up.
3106          */
3107         if (hw->fc.disable_fc_autoneg) {
3108                 ERROR_REPORT1(IXGBE_ERROR_UNSUPPORTED,
3109                              "Flow control autoneg is disabled");
3110                 goto out;
3111         }
3112
3113         hw->mac.ops.check_link(hw, &speed, &link_up, false);
3114         if (!link_up) {
3115                 ERROR_REPORT1(IXGBE_ERROR_SOFTWARE, "The link is down");
3116                 goto out;
3117         }
3118
3119         switch (hw->phy.media_type) {
3120         /* Autoneg flow control on fiber adapters */
3121         case ixgbe_media_type_fiber_qsfp:
3122         case ixgbe_media_type_fiber:
3123                 if (speed == IXGBE_LINK_SPEED_1GB_FULL)
3124                         ret_val = ixgbe_fc_autoneg_fiber(hw);
3125                 break;
3126
3127         /* Autoneg flow control on backplane adapters */
3128         case ixgbe_media_type_backplane:
3129                 ret_val = ixgbe_fc_autoneg_backplane(hw);
3130                 break;
3131
3132         /* Autoneg flow control on copper adapters */
3133         case ixgbe_media_type_copper:
3134                 if (ixgbe_device_supports_autoneg_fc(hw))
3135                         ret_val = ixgbe_fc_autoneg_copper(hw);
3136                 break;
3137
3138         default:
3139                 break;
3140         }
3141
3142 out:
3143         if (ret_val == IXGBE_SUCCESS) {
3144                 hw->fc.fc_was_autonegged = true;
3145         } else {
3146                 hw->fc.fc_was_autonegged = false;
3147                 hw->fc.current_mode = hw->fc.requested_mode;
3148         }
3149 }
3150
3151 /*
3152  * ixgbe_pcie_timeout_poll - Return number of times to poll for completion
3153  * @hw: pointer to hardware structure
3154  *
3155  * System-wide timeout range is encoded in PCIe Device Control2 register.
3156  *
3157  * Add 10% to specified maximum and return the number of times to poll for
3158  * completion timeout, in units of 100 microsec.  Never return less than
3159  * 800 = 80 millisec.
3160  */
3161 STATIC u32 ixgbe_pcie_timeout_poll(struct ixgbe_hw *hw)
3162 {
3163         s16 devctl2;
3164         u32 pollcnt;
3165
3166         devctl2 = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_DEVICE_CONTROL2);
3167         devctl2 &= IXGBE_PCIDEVCTRL2_TIMEO_MASK;
3168
3169         switch (devctl2) {
3170         case IXGBE_PCIDEVCTRL2_65_130ms:
3171                 pollcnt = 1300;         /* 130 millisec */
3172                 break;
3173         case IXGBE_PCIDEVCTRL2_260_520ms:
3174                 pollcnt = 5200;         /* 520 millisec */
3175                 break;
3176         case IXGBE_PCIDEVCTRL2_1_2s:
3177                 pollcnt = 20000;        /* 2 sec */
3178                 break;
3179         case IXGBE_PCIDEVCTRL2_4_8s:
3180                 pollcnt = 80000;        /* 8 sec */
3181                 break;
3182         case IXGBE_PCIDEVCTRL2_17_34s:
3183                 pollcnt = 34000;        /* 34 sec */
3184                 break;
3185         case IXGBE_PCIDEVCTRL2_50_100us:        /* 100 microsecs */
3186         case IXGBE_PCIDEVCTRL2_1_2ms:           /* 2 millisecs */
3187         case IXGBE_PCIDEVCTRL2_16_32ms:         /* 32 millisec */
3188         case IXGBE_PCIDEVCTRL2_16_32ms_def:     /* 32 millisec default */
3189         default:
3190                 pollcnt = 800;          /* 80 millisec minimum */
3191                 break;
3192         }
3193
3194         /* add 10% to spec maximum */
3195         return (pollcnt * 11) / 10;
3196 }
3197
3198 /**
3199  *  ixgbe_disable_pcie_master - Disable PCI-express master access
3200  *  @hw: pointer to hardware structure
3201  *
3202  *  Disables PCI-Express master access and verifies there are no pending
3203  *  requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
3204  *  bit hasn't caused the master requests to be disabled, else IXGBE_SUCCESS
3205  *  is returned signifying master requests disabled.
3206  **/
3207 s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
3208 {
3209         s32 status = IXGBE_SUCCESS;
3210         u32 i, poll;
3211         u16 value;
3212
3213         DEBUGFUNC("ixgbe_disable_pcie_master");
3214
3215         /* Always set this bit to ensure any future transactions are blocked */
3216         IXGBE_WRITE_REG(hw, IXGBE_CTRL, IXGBE_CTRL_GIO_DIS);
3217
3218         /* Exit if master requests are blocked */
3219         if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO) ||
3220             IXGBE_REMOVED(hw->hw_addr))
3221                 goto out;
3222
3223         /* Poll for master request bit to clear */
3224         for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
3225                 usec_delay(100);
3226                 if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO))
3227                         goto out;
3228         }
3229
3230         /*
3231          * Two consecutive resets are required via CTRL.RST per datasheet
3232          * 5.2.5.3.2 Master Disable.  We set a flag to inform the reset routine
3233          * of this need.  The first reset prevents new master requests from
3234          * being issued by our device.  We then must wait 1usec or more for any
3235          * remaining completions from the PCIe bus to trickle in, and then reset
3236          * again to clear out any effects they may have had on our device.
3237          */
3238         DEBUGOUT("GIO Master Disable bit didn't clear - requesting resets\n");
3239         hw->mac.flags |= IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
3240
3241         if (hw->mac.type >= ixgbe_mac_X550)
3242                 goto out;
3243
3244         /*
3245          * Before proceeding, make sure that the PCIe block does not have
3246          * transactions pending.
3247          */
3248         poll = ixgbe_pcie_timeout_poll(hw);
3249         for (i = 0; i < poll; i++) {
3250                 usec_delay(100);
3251                 value = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_DEVICE_STATUS);
3252                 if (IXGBE_REMOVED(hw->hw_addr))
3253                         goto out;
3254                 if (!(value & IXGBE_PCI_DEVICE_STATUS_TRANSACTION_PENDING))
3255                         goto out;
3256         }
3257
3258         ERROR_REPORT1(IXGBE_ERROR_POLLING,
3259                      "PCIe transaction pending bit also did not clear.\n");
3260         status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
3261
3262 out:
3263         return status;
3264 }
3265
3266 /**
3267  *  ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
3268  *  @hw: pointer to hardware structure
3269  *  @mask: Mask to specify which semaphore to acquire
3270  *
3271  *  Acquires the SWFW semaphore through the GSSR register for the specified
3272  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
3273  **/
3274 s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u32 mask)
3275 {
3276         u32 gssr = 0;
3277         u32 swmask = mask;
3278         u32 fwmask = mask << 5;
3279         u32 timeout = 200;
3280         u32 i;
3281
3282         DEBUGFUNC("ixgbe_acquire_swfw_sync");
3283
3284         for (i = 0; i < timeout; i++) {
3285                 /*
3286                  * SW NVM semaphore bit is used for access to all
3287                  * SW_FW_SYNC bits (not just NVM)
3288                  */
3289                 if (ixgbe_get_eeprom_semaphore(hw))
3290                         return IXGBE_ERR_SWFW_SYNC;
3291
3292                 gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
3293                 if (!(gssr & (fwmask | swmask))) {
3294                         gssr |= swmask;
3295                         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
3296                         ixgbe_release_eeprom_semaphore(hw);
3297                         return IXGBE_SUCCESS;
3298                 } else {
3299                         /* Resource is currently in use by FW or SW */
3300                         ixgbe_release_eeprom_semaphore(hw);
3301                         msec_delay(5);
3302                 }
3303         }
3304
3305         /* If time expired clear the bits holding the lock and retry */
3306         if (gssr & (fwmask | swmask))
3307                 ixgbe_release_swfw_sync(hw, gssr & (fwmask | swmask));
3308
3309         msec_delay(5);
3310         return IXGBE_ERR_SWFW_SYNC;
3311 }
3312
3313 /**
3314  *  ixgbe_release_swfw_sync - Release SWFW semaphore
3315  *  @hw: pointer to hardware structure
3316  *  @mask: Mask to specify which semaphore to release
3317  *
3318  *  Releases the SWFW semaphore through the GSSR register for the specified
3319  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
3320  **/
3321 void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u32 mask)
3322 {
3323         u32 gssr;
3324         u32 swmask = mask;
3325
3326         DEBUGFUNC("ixgbe_release_swfw_sync");
3327
3328         ixgbe_get_eeprom_semaphore(hw);
3329
3330         gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
3331         gssr &= ~swmask;
3332         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
3333
3334         ixgbe_release_eeprom_semaphore(hw);
3335 }
3336
3337 /**
3338  *  ixgbe_disable_sec_rx_path_generic - Stops the receive data path
3339  *  @hw: pointer to hardware structure
3340  *
3341  *  Stops the receive data path and waits for the HW to internally empty
3342  *  the Rx security block
3343  **/
3344 s32 ixgbe_disable_sec_rx_path_generic(struct ixgbe_hw *hw)
3345 {
3346 #define IXGBE_MAX_SECRX_POLL 40
3347
3348         int i;
3349         int secrxreg;
3350
3351         DEBUGFUNC("ixgbe_disable_sec_rx_path_generic");
3352
3353
3354         secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
3355         secrxreg |= IXGBE_SECRXCTRL_RX_DIS;
3356         IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
3357         for (i = 0; i < IXGBE_MAX_SECRX_POLL; i++) {
3358                 secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT);
3359                 if (secrxreg & IXGBE_SECRXSTAT_SECRX_RDY)
3360                         break;
3361                 else
3362                         /* Use interrupt-safe sleep just in case */
3363                         usec_delay(1000);
3364         }
3365
3366         /* For informational purposes only */
3367         if (i >= IXGBE_MAX_SECRX_POLL)
3368                 DEBUGOUT("Rx unit being enabled before security "
3369                          "path fully disabled.  Continuing with init.\n");
3370
3371         return IXGBE_SUCCESS;
3372 }
3373
3374 /**
3375  *  prot_autoc_read_generic - Hides MAC differences needed for AUTOC read
3376  *  @hw: pointer to hardware structure
3377  *  @reg_val: Value we read from AUTOC
3378  *
3379  *  The default case requires no protection so just to the register read.
3380  */
3381 s32 prot_autoc_read_generic(struct ixgbe_hw *hw, bool *locked, u32 *reg_val)
3382 {
3383         *locked = false;
3384         *reg_val = IXGBE_READ_REG(hw, IXGBE_AUTOC);
3385         return IXGBE_SUCCESS;
3386 }
3387
3388 /**
3389  * prot_autoc_write_generic - Hides MAC differences needed for AUTOC write
3390  * @hw: pointer to hardware structure
3391  * @reg_val: value to write to AUTOC
3392  * @locked: bool to indicate whether the SW/FW lock was already taken by
3393  *           previous read.
3394  *
3395  * The default case requires no protection so just to the register write.
3396  */
3397 s32 prot_autoc_write_generic(struct ixgbe_hw *hw, u32 reg_val, bool locked)
3398 {
3399         UNREFERENCED_1PARAMETER(locked);
3400
3401         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, reg_val);
3402         return IXGBE_SUCCESS;
3403 }
3404
3405 /**
3406  *  ixgbe_enable_sec_rx_path_generic - Enables the receive data path
3407  *  @hw: pointer to hardware structure
3408  *
3409  *  Enables the receive data path.
3410  **/
3411 s32 ixgbe_enable_sec_rx_path_generic(struct ixgbe_hw *hw)
3412 {
3413         u32 secrxreg;
3414
3415         DEBUGFUNC("ixgbe_enable_sec_rx_path_generic");
3416
3417         secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
3418         secrxreg &= ~IXGBE_SECRXCTRL_RX_DIS;
3419         IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
3420         IXGBE_WRITE_FLUSH(hw);
3421
3422         return IXGBE_SUCCESS;
3423 }
3424
3425 /**
3426  *  ixgbe_enable_rx_dma_generic - Enable the Rx DMA unit
3427  *  @hw: pointer to hardware structure
3428  *  @regval: register value to write to RXCTRL
3429  *
3430  *  Enables the Rx DMA unit
3431  **/
3432 s32 ixgbe_enable_rx_dma_generic(struct ixgbe_hw *hw, u32 regval)
3433 {
3434         DEBUGFUNC("ixgbe_enable_rx_dma_generic");
3435
3436         if (regval & IXGBE_RXCTRL_RXEN)
3437                 ixgbe_enable_rx(hw);
3438         else
3439                 ixgbe_disable_rx(hw);
3440
3441         return IXGBE_SUCCESS;
3442 }
3443
3444 /**
3445  *  ixgbe_blink_led_start_generic - Blink LED based on index.
3446  *  @hw: pointer to hardware structure
3447  *  @index: led number to blink
3448  **/
3449 s32 ixgbe_blink_led_start_generic(struct ixgbe_hw *hw, u32 index)
3450 {
3451         ixgbe_link_speed speed = 0;
3452         bool link_up = 0;
3453         u32 autoc_reg = 0;
3454         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
3455         s32 ret_val = IXGBE_SUCCESS;
3456         bool locked = false;
3457
3458         DEBUGFUNC("ixgbe_blink_led_start_generic");
3459
3460         if (index > 3)
3461                 return IXGBE_ERR_PARAM;
3462
3463         /*
3464          * Link must be up to auto-blink the LEDs;
3465          * Force it if link is down.
3466          */
3467         hw->mac.ops.check_link(hw, &speed, &link_up, false);
3468
3469         if (!link_up) {
3470                 ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &autoc_reg);
3471                 if (ret_val != IXGBE_SUCCESS)
3472                         goto out;
3473
3474                 autoc_reg |= IXGBE_AUTOC_AN_RESTART;
3475                 autoc_reg |= IXGBE_AUTOC_FLU;
3476
3477                 ret_val = hw->mac.ops.prot_autoc_write(hw, autoc_reg, locked);
3478                 if (ret_val != IXGBE_SUCCESS)
3479                         goto out;
3480
3481                 IXGBE_WRITE_FLUSH(hw);
3482                 msec_delay(10);
3483         }
3484
3485         led_reg &= ~IXGBE_LED_MODE_MASK(index);
3486         led_reg |= IXGBE_LED_BLINK(index);
3487         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
3488         IXGBE_WRITE_FLUSH(hw);
3489
3490 out:
3491         return ret_val;
3492 }
3493
3494 /**
3495  *  ixgbe_blink_led_stop_generic - Stop blinking LED based on index.
3496  *  @hw: pointer to hardware structure
3497  *  @index: led number to stop blinking
3498  **/
3499 s32 ixgbe_blink_led_stop_generic(struct ixgbe_hw *hw, u32 index)
3500 {
3501         u32 autoc_reg = 0;
3502         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
3503         s32 ret_val = IXGBE_SUCCESS;
3504         bool locked = false;
3505
3506         DEBUGFUNC("ixgbe_blink_led_stop_generic");
3507
3508         if (index > 3)
3509                 return IXGBE_ERR_PARAM;
3510
3511
3512         ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &autoc_reg);
3513         if (ret_val != IXGBE_SUCCESS)
3514                 goto out;
3515
3516         autoc_reg &= ~IXGBE_AUTOC_FLU;
3517         autoc_reg |= IXGBE_AUTOC_AN_RESTART;
3518
3519         ret_val = hw->mac.ops.prot_autoc_write(hw, autoc_reg, locked);
3520         if (ret_val != IXGBE_SUCCESS)
3521                 goto out;
3522
3523         led_reg &= ~IXGBE_LED_MODE_MASK(index);
3524         led_reg &= ~IXGBE_LED_BLINK(index);
3525         led_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
3526         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
3527         IXGBE_WRITE_FLUSH(hw);
3528
3529 out:
3530         return ret_val;
3531 }
3532
3533 /**
3534  *  ixgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
3535  *  @hw: pointer to hardware structure
3536  *  @san_mac_offset: SAN MAC address offset
3537  *
3538  *  This function will read the EEPROM location for the SAN MAC address
3539  *  pointer, and returns the value at that location.  This is used in both
3540  *  get and set mac_addr routines.
3541  **/
3542 STATIC s32 ixgbe_get_san_mac_addr_offset(struct ixgbe_hw *hw,
3543                                          u16 *san_mac_offset)
3544 {
3545         s32 ret_val;
3546
3547         DEBUGFUNC("ixgbe_get_san_mac_addr_offset");
3548
3549         /*
3550          * First read the EEPROM pointer to see if the MAC addresses are
3551          * available.
3552          */
3553         ret_val = hw->eeprom.ops.read(hw, IXGBE_SAN_MAC_ADDR_PTR,
3554                                       san_mac_offset);
3555         if (ret_val) {
3556                 ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
3557                               "eeprom at offset %d failed",
3558                               IXGBE_SAN_MAC_ADDR_PTR);
3559         }
3560
3561         return ret_val;
3562 }
3563
3564 /**
3565  *  ixgbe_get_san_mac_addr_generic - SAN MAC address retrieval from the EEPROM
3566  *  @hw: pointer to hardware structure
3567  *  @san_mac_addr: SAN MAC address
3568  *
3569  *  Reads the SAN MAC address from the EEPROM, if it's available.  This is
3570  *  per-port, so set_lan_id() must be called before reading the addresses.
3571  *  set_lan_id() is called by identify_sfp(), but this cannot be relied
3572  *  upon for non-SFP connections, so we must call it here.
3573  **/
3574 s32 ixgbe_get_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
3575 {
3576         u16 san_mac_data, san_mac_offset;
3577         u8 i;
3578         s32 ret_val;
3579
3580         DEBUGFUNC("ixgbe_get_san_mac_addr_generic");
3581
3582         /*
3583          * First read the EEPROM pointer to see if the MAC addresses are
3584          * available.  If they're not, no point in calling set_lan_id() here.
3585          */
3586         ret_val = ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
3587         if (ret_val || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
3588                 goto san_mac_addr_out;
3589
3590         /* make sure we know which port we need to program */
3591         hw->mac.ops.set_lan_id(hw);
3592         /* apply the port offset to the address offset */
3593         (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
3594                          (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
3595         for (i = 0; i < 3; i++) {
3596                 ret_val = hw->eeprom.ops.read(hw, san_mac_offset,
3597                                               &san_mac_data);
3598                 if (ret_val) {
3599                         ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
3600                                       "eeprom read at offset %d failed",
3601                                       san_mac_offset);
3602                         goto san_mac_addr_out;
3603                 }
3604                 san_mac_addr[i * 2] = (u8)(san_mac_data);
3605                 san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
3606                 san_mac_offset++;
3607         }
3608         return IXGBE_SUCCESS;
3609
3610 san_mac_addr_out:
3611         /*
3612          * No addresses available in this EEPROM.  It's not an
3613          * error though, so just wipe the local address and return.
3614          */
3615         for (i = 0; i < 6; i++)
3616                 san_mac_addr[i] = 0xFF;
3617         return IXGBE_SUCCESS;
3618 }
3619
3620 /**
3621  *  ixgbe_set_san_mac_addr_generic - Write the SAN MAC address to the EEPROM
3622  *  @hw: pointer to hardware structure
3623  *  @san_mac_addr: SAN MAC address
3624  *
3625  *  Write a SAN MAC address to the EEPROM.
3626  **/
3627 s32 ixgbe_set_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
3628 {
3629         s32 ret_val;
3630         u16 san_mac_data, san_mac_offset;
3631         u8 i;
3632
3633         DEBUGFUNC("ixgbe_set_san_mac_addr_generic");
3634
3635         /* Look for SAN mac address pointer.  If not defined, return */
3636         ret_val = ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
3637         if (ret_val || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
3638                 return IXGBE_ERR_NO_SAN_ADDR_PTR;
3639
3640         /* Make sure we know which port we need to write */
3641         hw->mac.ops.set_lan_id(hw);
3642         /* Apply the port offset to the address offset */
3643         (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
3644                          (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
3645
3646         for (i = 0; i < 3; i++) {
3647                 san_mac_data = (u16)((u16)(san_mac_addr[i * 2 + 1]) << 8);
3648                 san_mac_data |= (u16)(san_mac_addr[i * 2]);
3649                 hw->eeprom.ops.write(hw, san_mac_offset, san_mac_data);
3650                 san_mac_offset++;
3651         }
3652
3653         return IXGBE_SUCCESS;
3654 }
3655
3656 /**
3657  *  ixgbe_get_pcie_msix_count_generic - Gets MSI-X vector count
3658  *  @hw: pointer to hardware structure
3659  *
3660  *  Read PCIe configuration space, and get the MSI-X vector count from
3661  *  the capabilities table.
3662  **/
3663 u16 ixgbe_get_pcie_msix_count_generic(struct ixgbe_hw *hw)
3664 {
3665         u16 msix_count = 1;
3666         u16 max_msix_count;
3667         u16 pcie_offset;
3668
3669         switch (hw->mac.type) {
3670         case ixgbe_mac_82598EB:
3671                 pcie_offset = IXGBE_PCIE_MSIX_82598_CAPS;
3672                 max_msix_count = IXGBE_MAX_MSIX_VECTORS_82598;
3673                 break;
3674         case ixgbe_mac_82599EB:
3675         case ixgbe_mac_X540:
3676         case ixgbe_mac_X550:
3677         case ixgbe_mac_X550EM_x:
3678         case ixgbe_mac_X550EM_a:
3679                 pcie_offset = IXGBE_PCIE_MSIX_82599_CAPS;
3680                 max_msix_count = IXGBE_MAX_MSIX_VECTORS_82599;
3681                 break;
3682         default:
3683                 return msix_count;
3684         }
3685
3686         DEBUGFUNC("ixgbe_get_pcie_msix_count_generic");
3687         msix_count = IXGBE_READ_PCIE_WORD(hw, pcie_offset);
3688         if (IXGBE_REMOVED(hw->hw_addr))
3689                 msix_count = 0;
3690         msix_count &= IXGBE_PCIE_MSIX_TBL_SZ_MASK;
3691
3692         /* MSI-X count is zero-based in HW */
3693         msix_count++;
3694
3695         if (msix_count > max_msix_count)
3696                 msix_count = max_msix_count;
3697
3698         return msix_count;
3699 }
3700
3701 /**
3702  *  ixgbe_insert_mac_addr_generic - Find a RAR for this mac address
3703  *  @hw: pointer to hardware structure
3704  *  @addr: Address to put into receive address register
3705  *  @vmdq: VMDq pool to assign
3706  *
3707  *  Puts an ethernet address into a receive address register, or
3708  *  finds the rar that it is aleady in; adds to the pool list
3709  **/
3710 s32 ixgbe_insert_mac_addr_generic(struct ixgbe_hw *hw, u8 *addr, u32 vmdq)
3711 {
3712         static const u32 NO_EMPTY_RAR_FOUND = 0xFFFFFFFF;
3713         u32 first_empty_rar = NO_EMPTY_RAR_FOUND;
3714         u32 rar;
3715         u32 rar_low, rar_high;
3716         u32 addr_low, addr_high;
3717
3718         DEBUGFUNC("ixgbe_insert_mac_addr_generic");
3719
3720         /* swap bytes for HW little endian */
3721         addr_low  = addr[0] | (addr[1] << 8)
3722                             | (addr[2] << 16)
3723                             | (addr[3] << 24);
3724         addr_high = addr[4] | (addr[5] << 8);
3725
3726         /*
3727          * Either find the mac_id in rar or find the first empty space.
3728          * rar_highwater points to just after the highest currently used
3729          * rar in order to shorten the search.  It grows when we add a new
3730          * rar to the top.
3731          */
3732         for (rar = 0; rar < hw->mac.rar_highwater; rar++) {
3733                 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(rar));
3734
3735                 if (((IXGBE_RAH_AV & rar_high) == 0)
3736                     && first_empty_rar == NO_EMPTY_RAR_FOUND) {
3737                         first_empty_rar = rar;
3738                 } else if ((rar_high & 0xFFFF) == addr_high) {
3739                         rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(rar));
3740                         if (rar_low == addr_low)
3741                                 break;    /* found it already in the rars */
3742                 }
3743         }
3744
3745         if (rar < hw->mac.rar_highwater) {
3746                 /* already there so just add to the pool bits */
3747                 ixgbe_set_vmdq(hw, rar, vmdq);
3748         } else if (first_empty_rar != NO_EMPTY_RAR_FOUND) {
3749                 /* stick it into first empty RAR slot we found */
3750                 rar = first_empty_rar;
3751                 ixgbe_set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
3752         } else if (rar == hw->mac.rar_highwater) {
3753                 /* add it to the top of the list and inc the highwater mark */
3754                 ixgbe_set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
3755                 hw->mac.rar_highwater++;
3756         } else if (rar >= hw->mac.num_rar_entries) {
3757                 return IXGBE_ERR_INVALID_MAC_ADDR;
3758         }
3759
3760         /*
3761          * If we found rar[0], make sure the default pool bit (we use pool 0)
3762          * remains cleared to be sure default pool packets will get delivered
3763          */
3764         if (rar == 0)
3765                 ixgbe_clear_vmdq(hw, rar, 0);
3766
3767         return rar;
3768 }
3769
3770 /**
3771  *  ixgbe_clear_vmdq_generic - Disassociate a VMDq pool index from a rx address
3772  *  @hw: pointer to hardware struct
3773  *  @rar: receive address register index to disassociate
3774  *  @vmdq: VMDq pool index to remove from the rar
3775  **/
3776 s32 ixgbe_clear_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
3777 {
3778         u32 mpsar_lo, mpsar_hi;
3779         u32 rar_entries = hw->mac.num_rar_entries;
3780
3781         DEBUGFUNC("ixgbe_clear_vmdq_generic");
3782
3783         /* Make sure we are using a valid rar index range */
3784         if (rar >= rar_entries) {
3785                 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT,
3786                              "RAR index %d is out of range.\n", rar);
3787                 return IXGBE_ERR_INVALID_ARGUMENT;
3788         }
3789
3790         mpsar_lo = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
3791         mpsar_hi = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
3792
3793         if (IXGBE_REMOVED(hw->hw_addr))
3794                 goto done;
3795
3796         if (!mpsar_lo && !mpsar_hi)
3797                 goto done;
3798
3799         if (vmdq == IXGBE_CLEAR_VMDQ_ALL) {
3800                 if (mpsar_lo) {
3801                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
3802                         mpsar_lo = 0;
3803                 }
3804                 if (mpsar_hi) {
3805                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
3806                         mpsar_hi = 0;
3807                 }
3808         } else if (vmdq < 32) {
3809                 mpsar_lo &= ~(1 << vmdq);
3810                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar_lo);
3811         } else {
3812                 mpsar_hi &= ~(1 << (vmdq - 32));
3813                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar_hi);
3814         }
3815
3816         /* was that the last pool using this rar? */
3817         if (mpsar_lo == 0 && mpsar_hi == 0 &&
3818             rar != 0 && rar != hw->mac.san_mac_rar_index)
3819                 hw->mac.ops.clear_rar(hw, rar);
3820 done:
3821         return IXGBE_SUCCESS;
3822 }
3823
3824 /**
3825  *  ixgbe_set_vmdq_generic - Associate a VMDq pool index with a rx address
3826  *  @hw: pointer to hardware struct
3827  *  @rar: receive address register index to associate with a VMDq index
3828  *  @vmdq: VMDq pool index
3829  **/
3830 s32 ixgbe_set_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
3831 {
3832         u32 mpsar;
3833         u32 rar_entries = hw->mac.num_rar_entries;
3834
3835         DEBUGFUNC("ixgbe_set_vmdq_generic");
3836
3837         /* Make sure we are using a valid rar index range */
3838         if (rar >= rar_entries) {
3839                 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT,
3840                              "RAR index %d is out of range.\n", rar);
3841                 return IXGBE_ERR_INVALID_ARGUMENT;
3842         }
3843
3844         if (vmdq < 32) {
3845                 mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
3846                 mpsar |= 1 << vmdq;
3847                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar);
3848         } else {
3849                 mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
3850                 mpsar |= 1 << (vmdq - 32);
3851                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar);
3852         }
3853         return IXGBE_SUCCESS;
3854 }
3855
3856 /**
3857  *  This function should only be involved in the IOV mode.
3858  *  In IOV mode, Default pool is next pool after the number of
3859  *  VFs advertized and not 0.
3860  *  MPSAR table needs to be updated for SAN_MAC RAR [hw->mac.san_mac_rar_index]
3861  *
3862  *  ixgbe_set_vmdq_san_mac - Associate default VMDq pool index with a rx address
3863  *  @hw: pointer to hardware struct
3864  *  @vmdq: VMDq pool index
3865  **/
3866 s32 ixgbe_set_vmdq_san_mac_generic(struct ixgbe_hw *hw, u32 vmdq)
3867 {
3868         u32 rar = hw->mac.san_mac_rar_index;
3869
3870         DEBUGFUNC("ixgbe_set_vmdq_san_mac");
3871
3872         if (vmdq < 32) {
3873                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 1 << vmdq);
3874                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
3875         } else {
3876                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
3877                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 1 << (vmdq - 32));
3878         }
3879
3880         return IXGBE_SUCCESS;
3881 }
3882
3883 /**
3884  *  ixgbe_init_uta_tables_generic - Initialize the Unicast Table Array
3885  *  @hw: pointer to hardware structure
3886  **/
3887 s32 ixgbe_init_uta_tables_generic(struct ixgbe_hw *hw)
3888 {
3889         int i;
3890
3891         DEBUGFUNC("ixgbe_init_uta_tables_generic");
3892         DEBUGOUT(" Clearing UTA\n");
3893
3894         for (i = 0; i < 128; i++)
3895                 IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
3896
3897         return IXGBE_SUCCESS;
3898 }
3899
3900 /**
3901  *  ixgbe_find_vlvf_slot - find the vlanid or the first empty slot
3902  *  @hw: pointer to hardware structure
3903  *  @vlan: VLAN id to write to VLAN filter
3904  *
3905  *  return the VLVF index where this VLAN id should be placed
3906  *
3907  **/
3908 s32 ixgbe_find_vlvf_slot(struct ixgbe_hw *hw, u32 vlan, bool vlvf_bypass)
3909 {
3910         s32 regindex, first_empty_slot;
3911         u32 bits;
3912
3913         /* short cut the special case */
3914         if (vlan == 0)
3915                 return 0;
3916
3917         /* if vlvf_bypass is set we don't want to use an empty slot, we
3918          * will simply bypass the VLVF if there are no entries present in the
3919          * VLVF that contain our VLAN
3920          */
3921         first_empty_slot = vlvf_bypass ? IXGBE_ERR_NO_SPACE : 0;
3922
3923         /* add VLAN enable bit for comparison */
3924         vlan |= IXGBE_VLVF_VIEN;
3925
3926         /* Search for the vlan id in the VLVF entries. Save off the first empty
3927          * slot found along the way.
3928          *
3929          * pre-decrement loop covering (IXGBE_VLVF_ENTRIES - 1) .. 1
3930          */
3931         for (regindex = IXGBE_VLVF_ENTRIES; --regindex;) {
3932                 bits = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
3933                 if (bits == vlan)
3934                         return regindex;
3935                 if (!first_empty_slot && !bits)
3936                         first_empty_slot = regindex;
3937         }
3938
3939         /* If we are here then we didn't find the VLAN.  Return first empty
3940          * slot we found during our search, else error.
3941          */
3942         if (!first_empty_slot)
3943                 ERROR_REPORT1(IXGBE_ERROR_SOFTWARE, "No space in VLVF.\n");
3944
3945         return first_empty_slot ? first_empty_slot : IXGBE_ERR_NO_SPACE;
3946 }
3947
3948 /**
3949  *  ixgbe_set_vfta_generic - Set VLAN filter table
3950  *  @hw: pointer to hardware structure
3951  *  @vlan: VLAN id to write to VLAN filter
3952  *  @vind: VMDq output index that maps queue to VLAN id in VLVFB
3953  *  @vlan_on: boolean flag to turn on/off VLAN
3954  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
3955  *
3956  *  Turn on/off specified VLAN in the VLAN filter table.
3957  **/
3958 s32 ixgbe_set_vfta_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
3959                            bool vlan_on, bool vlvf_bypass)
3960 {
3961         u32 regidx, vfta_delta, vfta;
3962         s32 ret_val;
3963
3964         DEBUGFUNC("ixgbe_set_vfta_generic");
3965
3966         if (vlan > 4095 || vind > 63)
3967                 return IXGBE_ERR_PARAM;
3968
3969         /*
3970          * this is a 2 part operation - first the VFTA, then the
3971          * VLVF and VLVFB if VT Mode is set
3972          * We don't write the VFTA until we know the VLVF part succeeded.
3973          */
3974
3975         /* Part 1
3976          * The VFTA is a bitstring made up of 128 32-bit registers
3977          * that enable the particular VLAN id, much like the MTA:
3978          *    bits[11-5]: which register
3979          *    bits[4-0]:  which bit in the register
3980          */
3981         regidx = vlan / 32;
3982         vfta_delta = 1 << (vlan % 32);
3983         vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(regidx));
3984
3985         /*
3986          * vfta_delta represents the difference between the current value
3987          * of vfta and the value we want in the register.  Since the diff
3988          * is an XOR mask we can just update the vfta using an XOR
3989          */
3990         vfta_delta &= vlan_on ? ~vfta : vfta;
3991         vfta ^= vfta_delta;
3992
3993         /* Part 2
3994          * Call ixgbe_set_vlvf_generic to set VLVFB and VLVF
3995          */
3996         ret_val = ixgbe_set_vlvf_generic(hw, vlan, vind, vlan_on, &vfta_delta,
3997                                          vfta, vlvf_bypass);
3998         if (ret_val != IXGBE_SUCCESS) {
3999                 if (vlvf_bypass)
4000                         goto vfta_update;
4001                 return ret_val;
4002         }
4003
4004 vfta_update:
4005         /* Update VFTA now that we are ready for traffic */
4006         if (vfta_delta)
4007                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(regidx), vfta);
4008
4009         return IXGBE_SUCCESS;
4010 }
4011
4012 /**
4013  *  ixgbe_set_vlvf_generic - Set VLAN Pool Filter
4014  *  @hw: pointer to hardware structure
4015  *  @vlan: VLAN id to write to VLAN filter
4016  *  @vind: VMDq output index that maps queue to VLAN id in VLVFB
4017  *  @vlan_on: boolean flag to turn on/off VLAN in VLVF
4018  *  @vfta_delta: pointer to the difference between the current value of VFTA
4019  *               and the desired value
4020  *  @vfta: the desired value of the VFTA
4021  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
4022  *
4023  *  Turn on/off specified bit in VLVF table.
4024  **/
4025 s32 ixgbe_set_vlvf_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
4026                            bool vlan_on, u32 *vfta_delta, u32 vfta,
4027                            bool vlvf_bypass)
4028 {
4029         u32 bits;
4030         s32 vlvf_index;
4031
4032         DEBUGFUNC("ixgbe_set_vlvf_generic");
4033
4034         if (vlan > 4095 || vind > 63)
4035                 return IXGBE_ERR_PARAM;
4036
4037         /* If VT Mode is set
4038          *   Either vlan_on
4039          *     make sure the vlan is in VLVF
4040          *     set the vind bit in the matching VLVFB
4041          *   Or !vlan_on
4042          *     clear the pool bit and possibly the vind
4043          */
4044         if (!(IXGBE_READ_REG(hw, IXGBE_VT_CTL) & IXGBE_VT_CTL_VT_ENABLE))
4045                 return IXGBE_SUCCESS;
4046
4047         vlvf_index = ixgbe_find_vlvf_slot(hw, vlan, vlvf_bypass);
4048         if (vlvf_index < 0)
4049                 return vlvf_index;
4050
4051         bits = IXGBE_READ_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + vind / 32));
4052
4053         /* set the pool bit */
4054         bits |= 1 << (vind % 32);
4055         if (vlan_on)
4056                 goto vlvf_update;
4057
4058         /* clear the pool bit */
4059         bits ^= 1 << (vind % 32);
4060
4061         if (!bits &&
4062             !IXGBE_READ_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + 1 - vind / 32))) {
4063                 /* Clear VFTA first, then disable VLVF.  Otherwise
4064                  * we run the risk of stray packets leaking into
4065                  * the PF via the default pool
4066                  */
4067                 if (*vfta_delta)
4068                         IXGBE_WRITE_REG(hw, IXGBE_VFTA(vlan / 32), vfta);
4069
4070                 /* disable VLVF and clear remaining bit from pool */
4071                 IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), 0);
4072                 IXGBE_WRITE_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + vind / 32), 0);
4073
4074                 return IXGBE_SUCCESS;
4075         }
4076
4077         /* If there are still bits set in the VLVFB registers
4078          * for the VLAN ID indicated we need to see if the
4079          * caller is requesting that we clear the VFTA entry bit.
4080          * If the caller has requested that we clear the VFTA
4081          * entry bit but there are still pools/VFs using this VLAN
4082          * ID entry then ignore the request.  We're not worried
4083          * about the case where we're turning the VFTA VLAN ID
4084          * entry bit on, only when requested to turn it off as
4085          * there may be multiple pools and/or VFs using the
4086          * VLAN ID entry.  In that case we cannot clear the
4087          * VFTA bit until all pools/VFs using that VLAN ID have also
4088          * been cleared.  This will be indicated by "bits" being
4089          * zero.
4090          */
4091         *vfta_delta = 0;
4092
4093 vlvf_update:
4094         /* record pool change and enable VLAN ID if not already enabled */
4095         IXGBE_WRITE_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + vind / 32), bits);
4096         IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), IXGBE_VLVF_VIEN | vlan);
4097
4098         return IXGBE_SUCCESS;
4099 }
4100
4101 /**
4102  *  ixgbe_clear_vfta_generic - Clear VLAN filter table
4103  *  @hw: pointer to hardware structure
4104  *
4105  *  Clears the VLAN filer table, and the VMDq index associated with the filter
4106  **/
4107 s32 ixgbe_clear_vfta_generic(struct ixgbe_hw *hw)
4108 {
4109         u32 offset;
4110
4111         DEBUGFUNC("ixgbe_clear_vfta_generic");
4112
4113         for (offset = 0; offset < hw->mac.vft_size; offset++)
4114                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(offset), 0);
4115
4116         for (offset = 0; offset < IXGBE_VLVF_ENTRIES; offset++) {
4117                 IXGBE_WRITE_REG(hw, IXGBE_VLVF(offset), 0);
4118                 IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset * 2), 0);
4119                 IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset * 2 + 1), 0);
4120         }
4121
4122         return IXGBE_SUCCESS;
4123 }
4124
4125 /**
4126  *  ixgbe_need_crosstalk_fix - Determine if we need to do cross talk fix
4127  *  @hw: pointer to hardware structure
4128  *
4129  *  Contains the logic to identify if we need to verify link for the
4130  *  crosstalk fix
4131  **/
4132 static bool ixgbe_need_crosstalk_fix(struct ixgbe_hw *hw)
4133 {
4134
4135         /* Does FW say we need the fix */
4136         if (!hw->need_crosstalk_fix)
4137                 return false;
4138
4139         /* Only consider SFP+ PHYs i.e. media type fiber */
4140         switch (hw->mac.ops.get_media_type(hw)) {
4141         case ixgbe_media_type_fiber:
4142         case ixgbe_media_type_fiber_qsfp:
4143                 break;
4144         default:
4145                 return false;
4146         }
4147
4148         return true;
4149 }
4150
4151 /**
4152  *  ixgbe_check_mac_link_generic - Determine link and speed status
4153  *  @hw: pointer to hardware structure
4154  *  @speed: pointer to link speed
4155  *  @link_up: true when link is up
4156  *  @link_up_wait_to_complete: bool used to wait for link up or not
4157  *
4158  *  Reads the links register to determine if link is up and the current speed
4159  **/
4160 s32 ixgbe_check_mac_link_generic(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
4161                                  bool *link_up, bool link_up_wait_to_complete)
4162 {
4163         u32 links_reg, links_orig;
4164         u32 i;
4165
4166         DEBUGFUNC("ixgbe_check_mac_link_generic");
4167
4168         /* If Crosstalk fix enabled do the sanity check of making sure
4169          * the SFP+ cage is full.
4170          */
4171         if (ixgbe_need_crosstalk_fix(hw)) {
4172                 u32 sfp_cage_full;
4173
4174                 switch (hw->mac.type) {
4175                 case ixgbe_mac_82599EB:
4176                         sfp_cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
4177                                         IXGBE_ESDP_SDP2;
4178                         break;
4179                 case ixgbe_mac_X550EM_x:
4180                 case ixgbe_mac_X550EM_a:
4181                         sfp_cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
4182                                         IXGBE_ESDP_SDP0;
4183                         break;
4184                 default:
4185                         /* sanity check - No SFP+ devices here */
4186                         sfp_cage_full = false;
4187                         break;
4188                 }
4189
4190                 if (!sfp_cage_full) {
4191                         *link_up = false;
4192                         *speed = IXGBE_LINK_SPEED_UNKNOWN;
4193                         return IXGBE_SUCCESS;
4194                 }
4195         }
4196
4197         /* clear the old state */
4198         links_orig = IXGBE_READ_REG(hw, IXGBE_LINKS);
4199
4200         links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
4201
4202         if (links_orig != links_reg) {
4203                 DEBUGOUT2("LINKS changed from %08X to %08X\n",
4204                           links_orig, links_reg);
4205         }
4206
4207         if (link_up_wait_to_complete) {
4208                 for (i = 0; i < hw->mac.max_link_up_time; i++) {
4209                         if (links_reg & IXGBE_LINKS_UP) {
4210                                 *link_up = true;
4211                                 break;
4212                         } else {
4213                                 *link_up = false;
4214                         }
4215                         msec_delay(100);
4216                         links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
4217                 }
4218         } else {
4219                 if (links_reg & IXGBE_LINKS_UP)
4220                         *link_up = true;
4221                 else
4222                         *link_up = false;
4223         }
4224
4225         switch (links_reg & IXGBE_LINKS_SPEED_82599) {
4226         case IXGBE_LINKS_SPEED_10G_82599:
4227                 *speed = IXGBE_LINK_SPEED_10GB_FULL;
4228                 if (hw->mac.type >= ixgbe_mac_X550) {
4229                         if (links_reg & IXGBE_LINKS_SPEED_NON_STD)
4230                                 *speed = IXGBE_LINK_SPEED_2_5GB_FULL;
4231                 }
4232                 break;
4233         case IXGBE_LINKS_SPEED_1G_82599:
4234                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
4235                 break;
4236         case IXGBE_LINKS_SPEED_100_82599:
4237                 *speed = IXGBE_LINK_SPEED_100_FULL;
4238                 if (hw->mac.type == ixgbe_mac_X550) {
4239                         if (links_reg & IXGBE_LINKS_SPEED_NON_STD)
4240                                 *speed = IXGBE_LINK_SPEED_5GB_FULL;
4241                 }
4242                 break;
4243         case IXGBE_LINKS_SPEED_10_X550EM_A:
4244                 *speed = IXGBE_LINK_SPEED_UNKNOWN;
4245                 if (hw->device_id == IXGBE_DEV_ID_X550EM_A_1G_T ||
4246                     hw->device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L) {
4247                         *speed = IXGBE_LINK_SPEED_10_FULL;
4248                 }
4249                 break;
4250         default:
4251                 *speed = IXGBE_LINK_SPEED_UNKNOWN;
4252         }
4253
4254         return IXGBE_SUCCESS;
4255 }
4256
4257 /**
4258  *  ixgbe_get_wwn_prefix_generic - Get alternative WWNN/WWPN prefix from
4259  *  the EEPROM
4260  *  @hw: pointer to hardware structure
4261  *  @wwnn_prefix: the alternative WWNN prefix
4262  *  @wwpn_prefix: the alternative WWPN prefix
4263  *
4264  *  This function will read the EEPROM from the alternative SAN MAC address
4265  *  block to check the support for the alternative WWNN/WWPN prefix support.
4266  **/
4267 s32 ixgbe_get_wwn_prefix_generic(struct ixgbe_hw *hw, u16 *wwnn_prefix,
4268                                  u16 *wwpn_prefix)
4269 {
4270         u16 offset, caps;
4271         u16 alt_san_mac_blk_offset;
4272
4273         DEBUGFUNC("ixgbe_get_wwn_prefix_generic");
4274
4275         /* clear output first */
4276         *wwnn_prefix = 0xFFFF;
4277         *wwpn_prefix = 0xFFFF;
4278
4279         /* check if alternative SAN MAC is supported */
4280         offset = IXGBE_ALT_SAN_MAC_ADDR_BLK_PTR;
4281         if (hw->eeprom.ops.read(hw, offset, &alt_san_mac_blk_offset))
4282                 goto wwn_prefix_err;
4283
4284         if ((alt_san_mac_blk_offset == 0) ||
4285             (alt_san_mac_blk_offset == 0xFFFF))
4286                 goto wwn_prefix_out;
4287
4288         /* check capability in alternative san mac address block */
4289         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_CAPS_OFFSET;
4290         if (hw->eeprom.ops.read(hw, offset, &caps))
4291                 goto wwn_prefix_err;
4292         if (!(caps & IXGBE_ALT_SAN_MAC_ADDR_CAPS_ALTWWN))
4293                 goto wwn_prefix_out;
4294
4295         /* get the corresponding prefix for WWNN/WWPN */
4296         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWNN_OFFSET;
4297         if (hw->eeprom.ops.read(hw, offset, wwnn_prefix)) {
4298                 ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
4299                               "eeprom read at offset %d failed", offset);
4300         }
4301
4302         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWPN_OFFSET;
4303         if (hw->eeprom.ops.read(hw, offset, wwpn_prefix))
4304                 goto wwn_prefix_err;
4305
4306 wwn_prefix_out:
4307         return IXGBE_SUCCESS;
4308
4309 wwn_prefix_err:
4310         ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
4311                       "eeprom read at offset %d failed", offset);
4312         return IXGBE_SUCCESS;
4313 }
4314
4315 /**
4316  *  ixgbe_get_fcoe_boot_status_generic - Get FCOE boot status from EEPROM
4317  *  @hw: pointer to hardware structure
4318  *  @bs: the fcoe boot status
4319  *
4320  *  This function will read the FCOE boot status from the iSCSI FCOE block
4321  **/
4322 s32 ixgbe_get_fcoe_boot_status_generic(struct ixgbe_hw *hw, u16 *bs)
4323 {
4324         u16 offset, caps, flags;
4325         s32 status;
4326
4327         DEBUGFUNC("ixgbe_get_fcoe_boot_status_generic");
4328
4329         /* clear output first */
4330         *bs = ixgbe_fcoe_bootstatus_unavailable;
4331
4332         /* check if FCOE IBA block is present */
4333         offset = IXGBE_FCOE_IBA_CAPS_BLK_PTR;
4334         status = hw->eeprom.ops.read(hw, offset, &caps);
4335         if (status != IXGBE_SUCCESS)
4336                 goto out;
4337
4338         if (!(caps & IXGBE_FCOE_IBA_CAPS_FCOE))
4339                 goto out;
4340
4341         /* check if iSCSI FCOE block is populated */
4342         status = hw->eeprom.ops.read(hw, IXGBE_ISCSI_FCOE_BLK_PTR, &offset);
4343         if (status != IXGBE_SUCCESS)
4344                 goto out;
4345
4346         if ((offset == 0) || (offset == 0xFFFF))
4347                 goto out;
4348
4349         /* read fcoe flags in iSCSI FCOE block */
4350         offset = offset + IXGBE_ISCSI_FCOE_FLAGS_OFFSET;
4351         status = hw->eeprom.ops.read(hw, offset, &flags);
4352         if (status != IXGBE_SUCCESS)
4353                 goto out;
4354
4355         if (flags & IXGBE_ISCSI_FCOE_FLAGS_ENABLE)
4356                 *bs = ixgbe_fcoe_bootstatus_enabled;
4357         else
4358                 *bs = ixgbe_fcoe_bootstatus_disabled;
4359
4360 out:
4361         return status;
4362 }
4363
4364 /**
4365  *  ixgbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
4366  *  @hw: pointer to hardware structure
4367  *  @enable: enable or disable switch for MAC anti-spoofing
4368  *  @vf: Virtual Function pool - VF Pool to set for MAC anti-spoofing
4369  *
4370  **/
4371 void ixgbe_set_mac_anti_spoofing(struct ixgbe_hw *hw, bool enable, int vf)
4372 {
4373         int vf_target_reg = vf >> 3;
4374         int vf_target_shift = vf % 8;
4375         u32 pfvfspoof;
4376
4377         if (hw->mac.type == ixgbe_mac_82598EB)
4378                 return;
4379
4380         pfvfspoof = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
4381         if (enable)
4382                 pfvfspoof |= (1 << vf_target_shift);
4383         else
4384                 pfvfspoof &= ~(1 << vf_target_shift);
4385         IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), pfvfspoof);
4386 }
4387
4388 /**
4389  *  ixgbe_set_vlan_anti_spoofing - Enable/Disable VLAN anti-spoofing
4390  *  @hw: pointer to hardware structure
4391  *  @enable: enable or disable switch for VLAN anti-spoofing
4392  *  @vf: Virtual Function pool - VF Pool to set for VLAN anti-spoofing
4393  *
4394  **/
4395 void ixgbe_set_vlan_anti_spoofing(struct ixgbe_hw *hw, bool enable, int vf)
4396 {
4397         int vf_target_reg = vf >> 3;
4398         int vf_target_shift = vf % 8 + IXGBE_SPOOF_VLANAS_SHIFT;
4399         u32 pfvfspoof;
4400
4401         if (hw->mac.type == ixgbe_mac_82598EB)
4402                 return;
4403
4404         pfvfspoof = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
4405         if (enable)
4406                 pfvfspoof |= (1 << vf_target_shift);
4407         else
4408                 pfvfspoof &= ~(1 << vf_target_shift);
4409         IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), pfvfspoof);
4410 }
4411
4412 /**
4413  *  ixgbe_get_device_caps_generic - Get additional device capabilities
4414  *  @hw: pointer to hardware structure
4415  *  @device_caps: the EEPROM word with the extra device capabilities
4416  *
4417  *  This function will read the EEPROM location for the device capabilities,
4418  *  and return the word through device_caps.
4419  **/
4420 s32 ixgbe_get_device_caps_generic(struct ixgbe_hw *hw, u16 *device_caps)
4421 {
4422         DEBUGFUNC("ixgbe_get_device_caps_generic");
4423
4424         hw->eeprom.ops.read(hw, IXGBE_DEVICE_CAPS, device_caps);
4425
4426         return IXGBE_SUCCESS;
4427 }
4428
4429 /**
4430  *  ixgbe_enable_relaxed_ordering_gen2 - Enable relaxed ordering
4431  *  @hw: pointer to hardware structure
4432  *
4433  **/
4434 void ixgbe_enable_relaxed_ordering_gen2(struct ixgbe_hw *hw)
4435 {
4436         u32 regval;
4437         u32 i;
4438
4439         DEBUGFUNC("ixgbe_enable_relaxed_ordering_gen2");
4440
4441         /* Enable relaxed ordering */
4442         for (i = 0; i < hw->mac.max_tx_queues; i++) {
4443                 regval = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
4444                 regval |= IXGBE_DCA_TXCTRL_DESC_WRO_EN;
4445                 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), regval);
4446         }
4447
4448         for (i = 0; i < hw->mac.max_rx_queues; i++) {
4449                 regval = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
4450                 regval |= IXGBE_DCA_RXCTRL_DATA_WRO_EN |
4451                           IXGBE_DCA_RXCTRL_HEAD_WRO_EN;
4452                 IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(i), regval);
4453         }
4454
4455 }
4456
4457 /**
4458  *  ixgbe_calculate_checksum - Calculate checksum for buffer
4459  *  @buffer: pointer to EEPROM
4460  *  @length: size of EEPROM to calculate a checksum for
4461  *  Calculates the checksum for some buffer on a specified length.  The
4462  *  checksum calculated is returned.
4463  **/
4464 u8 ixgbe_calculate_checksum(u8 *buffer, u32 length)
4465 {
4466         u32 i;
4467         u8 sum = 0;
4468
4469         DEBUGFUNC("ixgbe_calculate_checksum");
4470
4471         if (!buffer)
4472                 return 0;
4473
4474         for (i = 0; i < length; i++)
4475                 sum += buffer[i];
4476
4477         return (u8) (0 - sum);
4478 }
4479
4480 /**
4481  *  ixgbe_hic_unlocked - Issue command to manageability block unlocked
4482  *  @hw: pointer to the HW structure
4483  *  @buffer: command to write and where the return status will be placed
4484  *  @length: length of buffer, must be multiple of 4 bytes
4485  *  @timeout: time in ms to wait for command completion
4486  *
4487  *  Communicates with the manageability block. On success return IXGBE_SUCCESS
4488  *  else returns semaphore error when encountering an error acquiring
4489  *  semaphore or IXGBE_ERR_HOST_INTERFACE_COMMAND when command fails.
4490  *
4491  *  This function assumes that the IXGBE_GSSR_SW_MNG_SM semaphore is held
4492  *  by the caller.
4493  **/
4494 s32 ixgbe_hic_unlocked(struct ixgbe_hw *hw, u32 *buffer, u32 length,
4495                        u32 timeout)
4496 {
4497         u32 hicr, i, fwsts;
4498         u16 dword_len;
4499
4500         DEBUGFUNC("ixgbe_hic_unlocked");
4501
4502         if (!length || length > IXGBE_HI_MAX_BLOCK_BYTE_LENGTH) {
4503                 DEBUGOUT1("Buffer length failure buffersize=%d.\n", length);
4504                 return IXGBE_ERR_HOST_INTERFACE_COMMAND;
4505         }
4506
4507         /* Set bit 9 of FWSTS clearing FW reset indication */
4508         fwsts = IXGBE_READ_REG(hw, IXGBE_FWSTS);
4509         IXGBE_WRITE_REG(hw, IXGBE_FWSTS, fwsts | IXGBE_FWSTS_FWRI);
4510
4511         /* Check that the host interface is enabled. */
4512         hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
4513         if (!(hicr & IXGBE_HICR_EN)) {
4514                 DEBUGOUT("IXGBE_HOST_EN bit disabled.\n");
4515                 return IXGBE_ERR_HOST_INTERFACE_COMMAND;
4516         }
4517
4518         /* Calculate length in DWORDs. We must be DWORD aligned */
4519         if (length % sizeof(u32)) {
4520                 DEBUGOUT("Buffer length failure, not aligned to dword");
4521                 return IXGBE_ERR_INVALID_ARGUMENT;
4522         }
4523
4524         dword_len = length >> 2;
4525
4526         /* The device driver writes the relevant command block
4527          * into the ram area.
4528          */
4529         for (i = 0; i < dword_len; i++)
4530                 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_FLEX_MNG,
4531                                       i, IXGBE_CPU_TO_LE32(buffer[i]));
4532
4533         /* Setting this bit tells the ARC that a new command is pending. */
4534         IXGBE_WRITE_REG(hw, IXGBE_HICR, hicr | IXGBE_HICR_C);
4535
4536         for (i = 0; i < timeout; i++) {
4537                 hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
4538                 if (!(hicr & IXGBE_HICR_C))
4539                         break;
4540                 msec_delay(1);
4541         }
4542
4543         /* Check command completion */
4544         if ((timeout && i == timeout) ||
4545             !(IXGBE_READ_REG(hw, IXGBE_HICR) & IXGBE_HICR_SV)) {
4546                 ERROR_REPORT1(IXGBE_ERROR_CAUTION,
4547                              "Command has failed with no status valid.\n");
4548                 return IXGBE_ERR_HOST_INTERFACE_COMMAND;
4549         }
4550
4551         return IXGBE_SUCCESS;
4552 }
4553
4554 /**
4555  *  ixgbe_host_interface_command - Issue command to manageability block
4556  *  @hw: pointer to the HW structure
4557  *  @buffer: contains the command to write and where the return status will
4558  *   be placed
4559  *  @length: length of buffer, must be multiple of 4 bytes
4560  *  @timeout: time in ms to wait for command completion
4561  *  @return_data: read and return data from the buffer (true) or not (false)
4562  *   Needed because FW structures are big endian and decoding of
4563  *   these fields can be 8 bit or 16 bit based on command. Decoding
4564  *   is not easily understood without making a table of commands.
4565  *   So we will leave this up to the caller to read back the data
4566  *   in these cases.
4567  *
4568  *  Communicates with the manageability block. On success return IXGBE_SUCCESS
4569  *  else returns semaphore error when encountering an error acquiring
4570  *  semaphore or IXGBE_ERR_HOST_INTERFACE_COMMAND when command fails.
4571  **/
4572 s32 ixgbe_host_interface_command(struct ixgbe_hw *hw, u32 *buffer,
4573                                  u32 length, u32 timeout, bool return_data)
4574 {
4575         u32 hdr_size = sizeof(struct ixgbe_hic_hdr);
4576         u16 dword_len;
4577         u16 buf_len;
4578         s32 status;
4579         u32 bi;
4580
4581         DEBUGFUNC("ixgbe_host_interface_command");
4582
4583         if (length == 0 || length > IXGBE_HI_MAX_BLOCK_BYTE_LENGTH) {
4584                 DEBUGOUT1("Buffer length failure buffersize=%d.\n", length);
4585                 return IXGBE_ERR_HOST_INTERFACE_COMMAND;
4586         }
4587
4588         /* Take management host interface semaphore */
4589         status = hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM);
4590         if (status)
4591                 return status;
4592
4593         status = ixgbe_hic_unlocked(hw, buffer, length, timeout);
4594         if (status)
4595                 goto rel_out;
4596
4597         if (!return_data)
4598                 goto rel_out;
4599
4600         /* Calculate length in DWORDs */
4601         dword_len = hdr_size >> 2;
4602
4603         /* first pull in the header so we know the buffer length */
4604         for (bi = 0; bi < dword_len; bi++) {
4605                 buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
4606                 IXGBE_LE32_TO_CPUS(&buffer[bi]);
4607         }
4608
4609         /* If there is any thing in data position pull it in */
4610         buf_len = ((struct ixgbe_hic_hdr *)buffer)->buf_len;
4611         if (!buf_len)
4612                 goto rel_out;
4613
4614         if (length < buf_len + hdr_size) {
4615                 DEBUGOUT("Buffer not large enough for reply message.\n");
4616                 status = IXGBE_ERR_HOST_INTERFACE_COMMAND;
4617                 goto rel_out;
4618         }
4619
4620         /* Calculate length in DWORDs, add 3 for odd lengths */
4621         dword_len = (buf_len + 3) >> 2;
4622
4623         /* Pull in the rest of the buffer (bi is where we left off) */
4624         for (; bi <= dword_len; bi++) {
4625                 buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
4626                 IXGBE_LE32_TO_CPUS(&buffer[bi]);
4627         }
4628
4629 rel_out:
4630         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM);
4631
4632         return status;
4633 }
4634
4635 /**
4636  *  ixgbe_set_fw_drv_ver_generic - Sends driver version to firmware
4637  *  @hw: pointer to the HW structure
4638  *  @maj: driver version major number
4639  *  @min: driver version minor number
4640  *  @build: driver version build number
4641  *  @sub: driver version sub build number
4642  *
4643  *  Sends driver version number to firmware through the manageability
4644  *  block.  On success return IXGBE_SUCCESS
4645  *  else returns IXGBE_ERR_SWFW_SYNC when encountering an error acquiring
4646  *  semaphore or IXGBE_ERR_HOST_INTERFACE_COMMAND when command fails.
4647  **/
4648 s32 ixgbe_set_fw_drv_ver_generic(struct ixgbe_hw *hw, u8 maj, u8 min,
4649                                  u8 build, u8 sub, u16 len,
4650                                  const char *driver_ver)
4651 {
4652         struct ixgbe_hic_drv_info fw_cmd;
4653         int i;
4654         s32 ret_val = IXGBE_SUCCESS;
4655
4656         DEBUGFUNC("ixgbe_set_fw_drv_ver_generic");
4657         UNREFERENCED_2PARAMETER(len, driver_ver);
4658
4659         fw_cmd.hdr.cmd = FW_CEM_CMD_DRIVER_INFO;
4660         fw_cmd.hdr.buf_len = FW_CEM_CMD_DRIVER_INFO_LEN;
4661         fw_cmd.hdr.cmd_or_resp.cmd_resv = FW_CEM_CMD_RESERVED;
4662         fw_cmd.port_num = (u8)hw->bus.func;
4663         fw_cmd.ver_maj = maj;
4664         fw_cmd.ver_min = min;
4665         fw_cmd.ver_build = build;
4666         fw_cmd.ver_sub = sub;
4667         fw_cmd.hdr.checksum = 0;
4668         fw_cmd.hdr.checksum = ixgbe_calculate_checksum((u8 *)&fw_cmd,
4669                                 (FW_CEM_HDR_LEN + fw_cmd.hdr.buf_len));
4670         fw_cmd.pad = 0;
4671         fw_cmd.pad2 = 0;
4672
4673         for (i = 0; i <= FW_CEM_MAX_RETRIES; i++) {
4674                 ret_val = ixgbe_host_interface_command(hw, (u32 *)&fw_cmd,
4675                                                        sizeof(fw_cmd),
4676                                                        IXGBE_HI_COMMAND_TIMEOUT,
4677                                                        true);
4678                 if (ret_val != IXGBE_SUCCESS)
4679                         continue;
4680
4681                 if (fw_cmd.hdr.cmd_or_resp.ret_status ==
4682                     FW_CEM_RESP_STATUS_SUCCESS)
4683                         ret_val = IXGBE_SUCCESS;
4684                 else
4685                         ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
4686
4687                 break;
4688         }
4689
4690         return ret_val;
4691 }
4692
4693 /**
4694  * ixgbe_set_rxpba_generic - Initialize Rx packet buffer
4695  * @hw: pointer to hardware structure
4696  * @num_pb: number of packet buffers to allocate
4697  * @headroom: reserve n KB of headroom
4698  * @strategy: packet buffer allocation strategy
4699  **/
4700 void ixgbe_set_rxpba_generic(struct ixgbe_hw *hw, int num_pb, u32 headroom,
4701                              int strategy)
4702 {
4703         u32 pbsize = hw->mac.rx_pb_size;
4704         int i = 0;
4705         u32 rxpktsize, txpktsize, txpbthresh;
4706
4707         /* Reserve headroom */
4708         pbsize -= headroom;
4709
4710         if (!num_pb)
4711                 num_pb = 1;
4712
4713         /* Divide remaining packet buffer space amongst the number of packet
4714          * buffers requested using supplied strategy.
4715          */
4716         switch (strategy) {
4717         case PBA_STRATEGY_WEIGHTED:
4718                 /* ixgbe_dcb_pba_80_48 strategy weight first half of packet
4719                  * buffer with 5/8 of the packet buffer space.
4720                  */
4721                 rxpktsize = (pbsize * 5) / (num_pb * 4);
4722                 pbsize -= rxpktsize * (num_pb / 2);
4723                 rxpktsize <<= IXGBE_RXPBSIZE_SHIFT;
4724                 for (; i < (num_pb / 2); i++)
4725                         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
4726                 /* Fall through to configure remaining packet buffers */
4727         case PBA_STRATEGY_EQUAL:
4728                 rxpktsize = (pbsize / (num_pb - i)) << IXGBE_RXPBSIZE_SHIFT;
4729                 for (; i < num_pb; i++)
4730                         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
4731                 break;
4732         default:
4733                 break;
4734         }
4735
4736         /* Only support an equally distributed Tx packet buffer strategy. */
4737         txpktsize = IXGBE_TXPBSIZE_MAX / num_pb;
4738         txpbthresh = (txpktsize / 1024) - IXGBE_TXPKT_SIZE_MAX;
4739         for (i = 0; i < num_pb; i++) {
4740                 IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), txpktsize);
4741                 IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), txpbthresh);
4742         }
4743
4744         /* Clear unused TCs, if any, to zero buffer size*/
4745         for (; i < IXGBE_MAX_PB; i++) {
4746                 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
4747                 IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), 0);
4748                 IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), 0);
4749         }
4750 }
4751
4752 /**
4753  * ixgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
4754  * @hw: pointer to the hardware structure
4755  *
4756  * The 82599 and x540 MACs can experience issues if TX work is still pending
4757  * when a reset occurs.  This function prevents this by flushing the PCIe
4758  * buffers on the system.
4759  **/
4760 void ixgbe_clear_tx_pending(struct ixgbe_hw *hw)
4761 {
4762         u32 gcr_ext, hlreg0, i, poll;
4763         u16 value;
4764
4765         /*
4766          * If double reset is not requested then all transactions should
4767          * already be clear and as such there is no work to do
4768          */
4769         if (!(hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
4770                 return;
4771
4772         /*
4773          * Set loopback enable to prevent any transmits from being sent
4774          * should the link come up.  This assumes that the RXCTRL.RXEN bit
4775          * has already been cleared.
4776          */
4777         hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
4778         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0 | IXGBE_HLREG0_LPBK);
4779
4780         /* Wait for a last completion before clearing buffers */
4781         IXGBE_WRITE_FLUSH(hw);
4782         msec_delay(3);
4783
4784         /*
4785          * Before proceeding, make sure that the PCIe block does not have
4786          * transactions pending.
4787          */
4788         poll = ixgbe_pcie_timeout_poll(hw);
4789         for (i = 0; i < poll; i++) {
4790                 usec_delay(100);
4791                 value = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_DEVICE_STATUS);
4792                 if (IXGBE_REMOVED(hw->hw_addr))
4793                         goto out;
4794                 if (!(value & IXGBE_PCI_DEVICE_STATUS_TRANSACTION_PENDING))
4795                         goto out;
4796         }
4797
4798 out:
4799         /* initiate cleaning flow for buffers in the PCIe transaction layer */
4800         gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
4801         IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT,
4802                         gcr_ext | IXGBE_GCR_EXT_BUFFERS_CLEAR);
4803
4804         /* Flush all writes and allow 20usec for all transactions to clear */
4805         IXGBE_WRITE_FLUSH(hw);
4806         usec_delay(20);
4807
4808         /* restore previous register values */
4809         IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
4810         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
4811 }
4812
4813 STATIC const u8 ixgbe_emc_temp_data[4] = {
4814         IXGBE_EMC_INTERNAL_DATA,
4815         IXGBE_EMC_DIODE1_DATA,
4816         IXGBE_EMC_DIODE2_DATA,
4817         IXGBE_EMC_DIODE3_DATA
4818 };
4819 STATIC const u8 ixgbe_emc_therm_limit[4] = {
4820         IXGBE_EMC_INTERNAL_THERM_LIMIT,
4821         IXGBE_EMC_DIODE1_THERM_LIMIT,
4822         IXGBE_EMC_DIODE2_THERM_LIMIT,
4823         IXGBE_EMC_DIODE3_THERM_LIMIT
4824 };
4825
4826 /**
4827  *  ixgbe_get_thermal_sensor_data - Gathers thermal sensor data
4828  *  @hw: pointer to hardware structure
4829  *  @data: pointer to the thermal sensor data structure
4830  *
4831  *  Returns the thermal sensor data structure
4832  **/
4833 s32 ixgbe_get_thermal_sensor_data_generic(struct ixgbe_hw *hw)
4834 {
4835         s32 status = IXGBE_SUCCESS;
4836         u16 ets_offset;
4837         u16 ets_cfg;
4838         u16 ets_sensor;
4839         u8  num_sensors;
4840         u8  sensor_index;
4841         u8  sensor_location;
4842         u8  i;
4843         struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
4844
4845         DEBUGFUNC("ixgbe_get_thermal_sensor_data_generic");
4846
4847         /* Only support thermal sensors attached to 82599 physical port 0 */
4848         if ((hw->mac.type != ixgbe_mac_82599EB) ||
4849             (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)) {
4850                 status = IXGBE_NOT_IMPLEMENTED;
4851                 goto out;
4852         }
4853
4854         status = hw->eeprom.ops.read(hw, IXGBE_ETS_CFG, &ets_offset);
4855         if (status)
4856                 goto out;
4857
4858         if ((ets_offset == 0x0000) || (ets_offset == 0xFFFF)) {
4859                 status = IXGBE_NOT_IMPLEMENTED;
4860                 goto out;
4861         }
4862
4863         status = hw->eeprom.ops.read(hw, ets_offset, &ets_cfg);
4864         if (status)
4865                 goto out;
4866
4867         if (((ets_cfg & IXGBE_ETS_TYPE_MASK) >> IXGBE_ETS_TYPE_SHIFT)
4868                 != IXGBE_ETS_TYPE_EMC) {
4869                 status = IXGBE_NOT_IMPLEMENTED;
4870                 goto out;
4871         }
4872
4873         num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
4874         if (num_sensors > IXGBE_MAX_SENSORS)
4875                 num_sensors = IXGBE_MAX_SENSORS;
4876
4877         for (i = 0; i < num_sensors; i++) {
4878                 status = hw->eeprom.ops.read(hw, (ets_offset + 1 + i),
4879                                              &ets_sensor);
4880                 if (status)
4881                         goto out;
4882
4883                 sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
4884                                 IXGBE_ETS_DATA_INDEX_SHIFT);
4885                 sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
4886                                    IXGBE_ETS_DATA_LOC_SHIFT);
4887
4888                 if (sensor_location != 0) {
4889                         status = hw->phy.ops.read_i2c_byte(hw,
4890                                         ixgbe_emc_temp_data[sensor_index],
4891                                         IXGBE_I2C_THERMAL_SENSOR_ADDR,
4892                                         &data->sensor[i].temp);
4893                         if (status)
4894                                 goto out;
4895                 }
4896         }
4897 out:
4898         return status;
4899 }
4900
4901 /**
4902  *  ixgbe_init_thermal_sensor_thresh_generic - Inits thermal sensor thresholds
4903  *  @hw: pointer to hardware structure
4904  *
4905  *  Inits the thermal sensor thresholds according to the NVM map
4906  *  and save off the threshold and location values into mac.thermal_sensor_data
4907  **/
4908 s32 ixgbe_init_thermal_sensor_thresh_generic(struct ixgbe_hw *hw)
4909 {
4910         s32 status = IXGBE_SUCCESS;
4911         u16 offset;
4912         u16 ets_offset;
4913         u16 ets_cfg;
4914         u16 ets_sensor;
4915         u8  low_thresh_delta;
4916         u8  num_sensors;
4917         u8  sensor_index;
4918         u8  sensor_location;
4919         u8  therm_limit;
4920         u8  i;
4921         struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
4922
4923         DEBUGFUNC("ixgbe_init_thermal_sensor_thresh_generic");
4924
4925         memset(data, 0, sizeof(struct ixgbe_thermal_sensor_data));
4926
4927         /* Only support thermal sensors attached to 82599 physical port 0 */
4928         if ((hw->mac.type != ixgbe_mac_82599EB) ||
4929             (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1))
4930                 return IXGBE_NOT_IMPLEMENTED;
4931
4932         offset = IXGBE_ETS_CFG;
4933         if (hw->eeprom.ops.read(hw, offset, &ets_offset))
4934                 goto eeprom_err;
4935         if ((ets_offset == 0x0000) || (ets_offset == 0xFFFF))
4936                 return IXGBE_NOT_IMPLEMENTED;
4937
4938         offset = ets_offset;
4939         if (hw->eeprom.ops.read(hw, offset, &ets_cfg))
4940                 goto eeprom_err;
4941         if (((ets_cfg & IXGBE_ETS_TYPE_MASK) >> IXGBE_ETS_TYPE_SHIFT)
4942                 != IXGBE_ETS_TYPE_EMC)
4943                 return IXGBE_NOT_IMPLEMENTED;
4944
4945         low_thresh_delta = ((ets_cfg & IXGBE_ETS_LTHRES_DELTA_MASK) >>
4946                              IXGBE_ETS_LTHRES_DELTA_SHIFT);
4947         num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
4948
4949         for (i = 0; i < num_sensors; i++) {
4950                 offset = ets_offset + 1 + i;
4951                 if (hw->eeprom.ops.read(hw, offset, &ets_sensor)) {
4952                         ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
4953                                       "eeprom read at offset %d failed",
4954                                       offset);
4955                         continue;
4956                 }
4957                 sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
4958                                 IXGBE_ETS_DATA_INDEX_SHIFT);
4959                 sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
4960                                    IXGBE_ETS_DATA_LOC_SHIFT);
4961                 therm_limit = ets_sensor & IXGBE_ETS_DATA_HTHRESH_MASK;
4962
4963                 hw->phy.ops.write_i2c_byte(hw,
4964                         ixgbe_emc_therm_limit[sensor_index],
4965                         IXGBE_I2C_THERMAL_SENSOR_ADDR, therm_limit);
4966
4967                 if ((i < IXGBE_MAX_SENSORS) && (sensor_location != 0)) {
4968                         data->sensor[i].location = sensor_location;
4969                         data->sensor[i].caution_thresh = therm_limit;
4970                         data->sensor[i].max_op_thresh = therm_limit -
4971                                                         low_thresh_delta;
4972                 }
4973         }
4974         return status;
4975
4976 eeprom_err:
4977         ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
4978                       "eeprom read at offset %d failed", offset);
4979         return IXGBE_NOT_IMPLEMENTED;
4980 }
4981
4982
4983 /**
4984  * ixgbe_dcb_get_rtrup2tc_generic - read rtrup2tc reg
4985  * @hw: pointer to hardware structure
4986  * @map: pointer to u8 arr for returning map
4987  *
4988  * Read the rtrup2tc HW register and resolve its content into map
4989  **/
4990 void ixgbe_dcb_get_rtrup2tc_generic(struct ixgbe_hw *hw, u8 *map)
4991 {
4992         u32 reg, i;
4993
4994         reg = IXGBE_READ_REG(hw, IXGBE_RTRUP2TC);
4995         for (i = 0; i < IXGBE_DCB_MAX_USER_PRIORITY; i++)
4996                 map[i] = IXGBE_RTRUP2TC_UP_MASK &
4997                         (reg >> (i * IXGBE_RTRUP2TC_UP_SHIFT));
4998         return;
4999 }
5000
5001 void ixgbe_disable_rx_generic(struct ixgbe_hw *hw)
5002 {
5003         u32 pfdtxgswc;
5004         u32 rxctrl;
5005
5006         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
5007         if (rxctrl & IXGBE_RXCTRL_RXEN) {
5008                 if (hw->mac.type != ixgbe_mac_82598EB) {
5009                         pfdtxgswc = IXGBE_READ_REG(hw, IXGBE_PFDTXGSWC);
5010                         if (pfdtxgswc & IXGBE_PFDTXGSWC_VT_LBEN) {
5011                                 pfdtxgswc &= ~IXGBE_PFDTXGSWC_VT_LBEN;
5012                                 IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, pfdtxgswc);
5013                                 hw->mac.set_lben = true;
5014                         } else {
5015                                 hw->mac.set_lben = false;
5016                         }
5017                 }
5018                 rxctrl &= ~IXGBE_RXCTRL_RXEN;
5019                 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl);
5020         }
5021 }
5022
5023 void ixgbe_enable_rx_generic(struct ixgbe_hw *hw)
5024 {
5025         u32 pfdtxgswc;
5026         u32 rxctrl;
5027
5028         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
5029         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, (rxctrl | IXGBE_RXCTRL_RXEN));
5030
5031         if (hw->mac.type != ixgbe_mac_82598EB) {
5032                 if (hw->mac.set_lben) {
5033                         pfdtxgswc = IXGBE_READ_REG(hw, IXGBE_PFDTXGSWC);
5034                         pfdtxgswc |= IXGBE_PFDTXGSWC_VT_LBEN;
5035                         IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, pfdtxgswc);
5036                         hw->mac.set_lben = false;
5037                 }
5038         }
5039 }
5040
5041 /**
5042  * ixgbe_mng_present - returns true when management capability is present
5043  * @hw: pointer to hardware structure
5044  */
5045 bool ixgbe_mng_present(struct ixgbe_hw *hw)
5046 {
5047         u32 fwsm;
5048
5049         if (hw->mac.type < ixgbe_mac_82599EB)
5050                 return false;
5051
5052         fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM_BY_MAC(hw));
5053         fwsm &= IXGBE_FWSM_MODE_MASK;
5054         return fwsm == IXGBE_FWSM_FW_MODE_PT;
5055 }
5056
5057 /**
5058  * ixgbe_mng_enabled - Is the manageability engine enabled?
5059  * @hw: pointer to hardware structure
5060  *
5061  * Returns true if the manageability engine is enabled.
5062  **/
5063 bool ixgbe_mng_enabled(struct ixgbe_hw *hw)
5064 {
5065         u32 fwsm, manc, factps;
5066
5067         fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM_BY_MAC(hw));
5068         if ((fwsm & IXGBE_FWSM_MODE_MASK) != IXGBE_FWSM_FW_MODE_PT)
5069                 return false;
5070
5071         manc = IXGBE_READ_REG(hw, IXGBE_MANC);
5072         if (!(manc & IXGBE_MANC_RCV_TCO_EN))
5073                 return false;
5074
5075         if (hw->mac.type <= ixgbe_mac_X540) {
5076                 factps = IXGBE_READ_REG(hw, IXGBE_FACTPS_BY_MAC(hw));
5077                 if (factps & IXGBE_FACTPS_MNGCG)
5078                         return false;
5079         }
5080
5081         return true;
5082 }
5083
5084 /**
5085  *  ixgbe_setup_mac_link_multispeed_fiber - Set MAC link speed
5086  *  @hw: pointer to hardware structure
5087  *  @speed: new link speed
5088  *  @autoneg_wait_to_complete: true when waiting for completion is needed
5089  *
5090  *  Set the link speed in the MAC and/or PHY register and restarts link.
5091  **/
5092 s32 ixgbe_setup_mac_link_multispeed_fiber(struct ixgbe_hw *hw,
5093                                           ixgbe_link_speed speed,
5094                                           bool autoneg_wait_to_complete)
5095 {
5096         ixgbe_link_speed link_speed = IXGBE_LINK_SPEED_UNKNOWN;
5097         ixgbe_link_speed highest_link_speed = IXGBE_LINK_SPEED_UNKNOWN;
5098         s32 status = IXGBE_SUCCESS;
5099         u32 speedcnt = 0;
5100         u32 i = 0;
5101         bool autoneg, link_up = false;
5102
5103         DEBUGFUNC("ixgbe_setup_mac_link_multispeed_fiber");
5104
5105         /* Mask off requested but non-supported speeds */
5106         status = ixgbe_get_link_capabilities(hw, &link_speed, &autoneg);
5107         if (status != IXGBE_SUCCESS)
5108                 return status;
5109
5110         speed &= link_speed;
5111
5112         /* Try each speed one by one, highest priority first.  We do this in
5113          * software because 10Gb fiber doesn't support speed autonegotiation.
5114          */
5115         if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
5116                 speedcnt++;
5117                 highest_link_speed = IXGBE_LINK_SPEED_10GB_FULL;
5118
5119                 /* Set the module link speed */
5120                 switch (hw->phy.media_type) {
5121                 case ixgbe_media_type_fiber:
5122                         ixgbe_set_rate_select_speed(hw,
5123                                                     IXGBE_LINK_SPEED_10GB_FULL);
5124                         break;
5125                 case ixgbe_media_type_fiber_qsfp:
5126                         /* QSFP module automatically detects MAC link speed */
5127                         break;
5128                 default:
5129                         DEBUGOUT("Unexpected media type.\n");
5130                         break;
5131                 }
5132
5133                 /* Allow module to change analog characteristics (1G->10G) */
5134                 msec_delay(40);
5135
5136                 status = ixgbe_setup_mac_link(hw,
5137                                               IXGBE_LINK_SPEED_10GB_FULL,
5138                                               autoneg_wait_to_complete);
5139                 if (status != IXGBE_SUCCESS)
5140                         return status;
5141
5142                 /* Flap the Tx laser if it has not already been done */
5143                 ixgbe_flap_tx_laser(hw);
5144
5145                 /* Wait for the controller to acquire link.  Per IEEE 802.3ap,
5146                  * Section 73.10.2, we may have to wait up to 500ms if KR is
5147                  * attempted.  82599 uses the same timing for 10g SFI.
5148                  */
5149                 for (i = 0; i < 5; i++) {
5150                         /* Wait for the link partner to also set speed */
5151                         msec_delay(100);
5152
5153                         /* If we have link, just jump out */
5154                         status = ixgbe_check_link(hw, &link_speed,
5155                                                   &link_up, false);
5156                         if (status != IXGBE_SUCCESS)
5157                                 return status;
5158
5159                         if (link_up)
5160                                 goto out;
5161                 }
5162         }
5163
5164         if (speed & IXGBE_LINK_SPEED_1GB_FULL) {
5165                 speedcnt++;
5166                 if (highest_link_speed == IXGBE_LINK_SPEED_UNKNOWN)
5167                         highest_link_speed = IXGBE_LINK_SPEED_1GB_FULL;
5168
5169                 /* Set the module link speed */
5170                 switch (hw->phy.media_type) {
5171                 case ixgbe_media_type_fiber:
5172                         ixgbe_set_rate_select_speed(hw,
5173                                                     IXGBE_LINK_SPEED_1GB_FULL);
5174                         break;
5175                 case ixgbe_media_type_fiber_qsfp:
5176                         /* QSFP module automatically detects link speed */
5177                         break;
5178                 default:
5179                         DEBUGOUT("Unexpected media type.\n");
5180                         break;
5181                 }
5182
5183                 /* Allow module to change analog characteristics (10G->1G) */
5184                 msec_delay(40);
5185
5186                 status = ixgbe_setup_mac_link(hw,
5187                                               IXGBE_LINK_SPEED_1GB_FULL,
5188                                               autoneg_wait_to_complete);
5189                 if (status != IXGBE_SUCCESS)
5190                         return status;
5191
5192                 /* Flap the Tx laser if it has not already been done */
5193                 ixgbe_flap_tx_laser(hw);
5194
5195                 /* Wait for the link partner to also set speed */
5196                 msec_delay(100);
5197
5198                 /* If we have link, just jump out */
5199                 status = ixgbe_check_link(hw, &link_speed, &link_up, false);
5200                 if (status != IXGBE_SUCCESS)
5201                         return status;
5202
5203                 if (link_up)
5204                         goto out;
5205         }
5206
5207         /* We didn't get link.  Configure back to the highest speed we tried,
5208          * (if there was more than one).  We call ourselves back with just the
5209          * single highest speed that the user requested.
5210          */
5211         if (speedcnt > 1)
5212                 status = ixgbe_setup_mac_link_multispeed_fiber(hw,
5213                                                       highest_link_speed,
5214                                                       autoneg_wait_to_complete);
5215
5216 out:
5217         /* Set autoneg_advertised value based on input link speed */
5218         hw->phy.autoneg_advertised = 0;
5219
5220         if (speed & IXGBE_LINK_SPEED_10GB_FULL)
5221                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
5222
5223         if (speed & IXGBE_LINK_SPEED_1GB_FULL)
5224                 hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
5225
5226         return status;
5227 }
5228
5229 /**
5230  *  ixgbe_set_soft_rate_select_speed - Set module link speed
5231  *  @hw: pointer to hardware structure
5232  *  @speed: link speed to set
5233  *
5234  *  Set module link speed via the soft rate select.
5235  */
5236 void ixgbe_set_soft_rate_select_speed(struct ixgbe_hw *hw,
5237                                         ixgbe_link_speed speed)
5238 {
5239         s32 status;
5240         u8 rs, eeprom_data;
5241
5242         switch (speed) {
5243         case IXGBE_LINK_SPEED_10GB_FULL:
5244                 /* one bit mask same as setting on */
5245                 rs = IXGBE_SFF_SOFT_RS_SELECT_10G;
5246                 break;
5247         case IXGBE_LINK_SPEED_1GB_FULL:
5248                 rs = IXGBE_SFF_SOFT_RS_SELECT_1G;
5249                 break;
5250         default:
5251                 DEBUGOUT("Invalid fixed module speed\n");
5252                 return;
5253         }
5254
5255         /* Set RS0 */
5256         status = hw->phy.ops.read_i2c_byte(hw, IXGBE_SFF_SFF_8472_OSCB,
5257                                            IXGBE_I2C_EEPROM_DEV_ADDR2,
5258                                            &eeprom_data);
5259         if (status) {
5260                 DEBUGOUT("Failed to read Rx Rate Select RS0\n");
5261                 goto out;
5262         }
5263
5264         eeprom_data = (eeprom_data & ~IXGBE_SFF_SOFT_RS_SELECT_MASK) | rs;
5265
5266         status = hw->phy.ops.write_i2c_byte(hw, IXGBE_SFF_SFF_8472_OSCB,
5267                                             IXGBE_I2C_EEPROM_DEV_ADDR2,
5268                                             eeprom_data);
5269         if (status) {
5270                 DEBUGOUT("Failed to write Rx Rate Select RS0\n");
5271                 goto out;
5272         }
5273
5274         /* Set RS1 */
5275         status = hw->phy.ops.read_i2c_byte(hw, IXGBE_SFF_SFF_8472_ESCB,
5276                                            IXGBE_I2C_EEPROM_DEV_ADDR2,
5277                                            &eeprom_data);
5278         if (status) {
5279                 DEBUGOUT("Failed to read Rx Rate Select RS1\n");
5280                 goto out;
5281         }
5282
5283         eeprom_data = (eeprom_data & ~IXGBE_SFF_SOFT_RS_SELECT_MASK) | rs;
5284
5285         status = hw->phy.ops.write_i2c_byte(hw, IXGBE_SFF_SFF_8472_ESCB,
5286                                             IXGBE_I2C_EEPROM_DEV_ADDR2,
5287                                             eeprom_data);
5288         if (status) {
5289                 DEBUGOUT("Failed to write Rx Rate Select RS1\n");
5290                 goto out;
5291         }
5292 out:
5293         return;
5294 }