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