ixgbe/base: rework PCIe disabling
[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_pcie_timeout_poll - Return number of times to poll for completion
2997  * @hw: pointer to hardware structure
2998  *
2999  * System-wide timeout range is encoded in PCIe Device Control2 register.
3000  *
3001  * Add 10% to specified maximum and return the number of times to poll for
3002  * completion timeout, in units of 100 microsec.  Never return less than
3003  * 800 = 80 millisec.
3004  */
3005 STATIC u32 ixgbe_pcie_timeout_poll(struct ixgbe_hw *hw)
3006 {
3007         s16 devctl2;
3008         u32 pollcnt;
3009
3010         devctl2 = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_DEVICE_CONTROL2);
3011         devctl2 &= IXGBE_PCIDEVCTRL2_TIMEO_MASK;
3012
3013         switch (devctl2) {
3014         case IXGBE_PCIDEVCTRL2_65_130ms:
3015                 pollcnt = 1300;         /* 130 millisec */
3016                 break;
3017         case IXGBE_PCIDEVCTRL2_260_520ms:
3018                 pollcnt = 5200;         /* 520 millisec */
3019                 break;
3020         case IXGBE_PCIDEVCTRL2_1_2s:
3021                 pollcnt = 20000;        /* 2 sec */
3022                 break;
3023         case IXGBE_PCIDEVCTRL2_4_8s:
3024                 pollcnt = 80000;        /* 8 sec */
3025                 break;
3026         case IXGBE_PCIDEVCTRL2_17_34s:
3027                 pollcnt = 34000;        /* 34 sec */
3028                 break;
3029         case IXGBE_PCIDEVCTRL2_50_100us:        /* 100 microsecs */
3030         case IXGBE_PCIDEVCTRL2_1_2ms:           /* 2 millisecs */
3031         case IXGBE_PCIDEVCTRL2_16_32ms:         /* 32 millisec */
3032         case IXGBE_PCIDEVCTRL2_16_32ms_def:     /* 32 millisec default */
3033         default:
3034                 pollcnt = 800;          /* 80 millisec minimum */
3035                 break;
3036         }
3037
3038         /* add 10% to spec maximum */
3039         return (pollcnt * 11) / 10;
3040 }
3041
3042 /**
3043  *  ixgbe_disable_pcie_master - Disable PCI-express master access
3044  *  @hw: pointer to hardware structure
3045  *
3046  *  Disables PCI-Express master access and verifies there are no pending
3047  *  requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
3048  *  bit hasn't caused the master requests to be disabled, else IXGBE_SUCCESS
3049  *  is returned signifying master requests disabled.
3050  **/
3051 s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
3052 {
3053         s32 status = IXGBE_SUCCESS;
3054         u32 i, poll;
3055         u16 value;
3056
3057         DEBUGFUNC("ixgbe_disable_pcie_master");
3058
3059         /* Always set this bit to ensure any future transactions are blocked */
3060         IXGBE_WRITE_REG(hw, IXGBE_CTRL, IXGBE_CTRL_GIO_DIS);
3061
3062         /* Exit if master requests are blocked */
3063         if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO) ||
3064             IXGBE_REMOVED(hw->hw_addr))
3065                 goto out;
3066
3067         /* Poll for master request bit to clear */
3068         for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
3069                 usec_delay(100);
3070                 if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO))
3071                         goto out;
3072         }
3073
3074         /*
3075          * Two consecutive resets are required via CTRL.RST per datasheet
3076          * 5.2.5.3.2 Master Disable.  We set a flag to inform the reset routine
3077          * of this need.  The first reset prevents new master requests from
3078          * being issued by our device.  We then must wait 1usec or more for any
3079          * remaining completions from the PCIe bus to trickle in, and then reset
3080          * again to clear out any effects they may have had on our device.
3081          */
3082         DEBUGOUT("GIO Master Disable bit didn't clear - requesting resets\n");
3083         hw->mac.flags |= IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
3084
3085         /*
3086          * Before proceeding, make sure that the PCIe block does not have
3087          * transactions pending.
3088          */
3089         poll = ixgbe_pcie_timeout_poll(hw);
3090         for (i = 0; i < poll; i++) {
3091                 usec_delay(100);
3092                 value = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_DEVICE_STATUS);
3093                 if (IXGBE_REMOVED(hw->hw_addr))
3094                         goto out;
3095                 if (!(value & IXGBE_PCI_DEVICE_STATUS_TRANSACTION_PENDING))
3096                         goto out;
3097         }
3098
3099         ERROR_REPORT1(IXGBE_ERROR_POLLING,
3100                      "PCIe transaction pending bit also did not clear.\n");
3101         status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
3102
3103 out:
3104         return status;
3105 }
3106
3107 /**
3108  *  ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
3109  *  @hw: pointer to hardware structure
3110  *  @mask: Mask to specify which semaphore to acquire
3111  *
3112  *  Acquires the SWFW semaphore through the GSSR register for the specified
3113  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
3114  **/
3115 s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u16 mask)
3116 {
3117         u32 gssr;
3118         u32 swmask = mask;
3119         u32 fwmask = mask << 5;
3120         s32 timeout = 200;
3121
3122         DEBUGFUNC("ixgbe_acquire_swfw_sync");
3123
3124         while (timeout) {
3125                 /*
3126                  * SW EEPROM semaphore bit is used for access to all
3127                  * SW_FW_SYNC/GSSR bits (not just EEPROM)
3128                  */
3129                 if (ixgbe_get_eeprom_semaphore(hw))
3130                         return IXGBE_ERR_SWFW_SYNC;
3131
3132                 gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
3133                 if (!(gssr & (fwmask | swmask)))
3134                         break;
3135
3136                 /*
3137                  * Firmware currently using resource (fwmask) or other software
3138                  * thread currently using resource (swmask)
3139                  */
3140                 ixgbe_release_eeprom_semaphore(hw);
3141                 msec_delay(5);
3142                 timeout--;
3143         }
3144
3145         if (!timeout) {
3146                 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
3147                 return IXGBE_ERR_SWFW_SYNC;
3148         }
3149
3150         gssr |= swmask;
3151         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
3152
3153         ixgbe_release_eeprom_semaphore(hw);
3154         return IXGBE_SUCCESS;
3155 }
3156
3157 /**
3158  *  ixgbe_release_swfw_sync - Release SWFW semaphore
3159  *  @hw: pointer to hardware structure
3160  *  @mask: Mask to specify which semaphore to release
3161  *
3162  *  Releases the SWFW semaphore through the GSSR register for the specified
3163  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
3164  **/
3165 void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u16 mask)
3166 {
3167         u32 gssr;
3168         u32 swmask = mask;
3169
3170         DEBUGFUNC("ixgbe_release_swfw_sync");
3171
3172         ixgbe_get_eeprom_semaphore(hw);
3173
3174         gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
3175         gssr &= ~swmask;
3176         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
3177
3178         ixgbe_release_eeprom_semaphore(hw);
3179 }
3180
3181 /**
3182  *  ixgbe_disable_sec_rx_path_generic - Stops the receive data path
3183  *  @hw: pointer to hardware structure
3184  *
3185  *  Stops the receive data path and waits for the HW to internally empty
3186  *  the Rx security block
3187  **/
3188 s32 ixgbe_disable_sec_rx_path_generic(struct ixgbe_hw *hw)
3189 {
3190 #define IXGBE_MAX_SECRX_POLL 40
3191
3192         int i;
3193         int secrxreg;
3194
3195         DEBUGFUNC("ixgbe_disable_sec_rx_path_generic");
3196
3197
3198         secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
3199         secrxreg |= IXGBE_SECRXCTRL_RX_DIS;
3200         IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
3201         for (i = 0; i < IXGBE_MAX_SECRX_POLL; i++) {
3202                 secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT);
3203                 if (secrxreg & IXGBE_SECRXSTAT_SECRX_RDY)
3204                         break;
3205                 else
3206                         /* Use interrupt-safe sleep just in case */
3207                         usec_delay(1000);
3208         }
3209
3210         /* For informational purposes only */
3211         if (i >= IXGBE_MAX_SECRX_POLL)
3212                 DEBUGOUT("Rx unit being enabled before security "
3213                          "path fully disabled.  Continuing with init.\n");
3214
3215         return IXGBE_SUCCESS;
3216 }
3217
3218 /**
3219  *  ixgbe_enable_sec_rx_path_generic - Enables the receive data path
3220  *  @hw: pointer to hardware structure
3221  *
3222  *  Enables the receive data path.
3223  **/
3224 s32 ixgbe_enable_sec_rx_path_generic(struct ixgbe_hw *hw)
3225 {
3226         int secrxreg;
3227
3228         DEBUGFUNC("ixgbe_enable_sec_rx_path_generic");
3229
3230         secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
3231         secrxreg &= ~IXGBE_SECRXCTRL_RX_DIS;
3232         IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
3233         IXGBE_WRITE_FLUSH(hw);
3234
3235         return IXGBE_SUCCESS;
3236 }
3237
3238 /**
3239  *  ixgbe_enable_rx_dma_generic - Enable the Rx DMA unit
3240  *  @hw: pointer to hardware structure
3241  *  @regval: register value to write to RXCTRL
3242  *
3243  *  Enables the Rx DMA unit
3244  **/
3245 s32 ixgbe_enable_rx_dma_generic(struct ixgbe_hw *hw, u32 regval)
3246 {
3247         DEBUGFUNC("ixgbe_enable_rx_dma_generic");
3248
3249         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, regval);
3250
3251         return IXGBE_SUCCESS;
3252 }
3253
3254 /**
3255  *  ixgbe_blink_led_start_generic - Blink LED based on index.
3256  *  @hw: pointer to hardware structure
3257  *  @index: led number to blink
3258  **/
3259 s32 ixgbe_blink_led_start_generic(struct ixgbe_hw *hw, u32 index)
3260 {
3261         ixgbe_link_speed speed = 0;
3262         bool link_up = 0;
3263         u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
3264         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
3265         s32 ret_val = IXGBE_SUCCESS;
3266
3267         DEBUGFUNC("ixgbe_blink_led_start_generic");
3268
3269         /*
3270          * Link must be up to auto-blink the LEDs;
3271          * Force it if link is down.
3272          */
3273         hw->mac.ops.check_link(hw, &speed, &link_up, false);
3274
3275         if (!link_up) {
3276                 /* Need the SW/FW semaphore around AUTOC writes if 82599 and
3277                  * LESM is on.
3278                  */
3279                 bool got_lock = false;
3280                 if ((hw->mac.type == ixgbe_mac_82599EB) &&
3281                     ixgbe_verify_lesm_fw_enabled_82599(hw)) {
3282                         ret_val = hw->mac.ops.acquire_swfw_sync(hw,
3283                                                         IXGBE_GSSR_MAC_CSR_SM);
3284                         if (ret_val != IXGBE_SUCCESS) {
3285                                 ret_val = IXGBE_ERR_SWFW_SYNC;
3286                                 goto out;
3287                         }
3288                         got_lock = true;
3289                 }
3290
3291                 autoc_reg |= IXGBE_AUTOC_AN_RESTART;
3292                 autoc_reg |= IXGBE_AUTOC_FLU;
3293                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
3294                 IXGBE_WRITE_FLUSH(hw);
3295
3296                 if (got_lock)
3297                         hw->mac.ops.release_swfw_sync(hw,
3298                                                       IXGBE_GSSR_MAC_CSR_SM);
3299                 msec_delay(10);
3300         }
3301
3302         led_reg &= ~IXGBE_LED_MODE_MASK(index);
3303         led_reg |= IXGBE_LED_BLINK(index);
3304         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
3305         IXGBE_WRITE_FLUSH(hw);
3306
3307 out:
3308         return ret_val;
3309 }
3310
3311 /**
3312  *  ixgbe_blink_led_stop_generic - Stop blinking LED based on index.
3313  *  @hw: pointer to hardware structure
3314  *  @index: led number to stop blinking
3315  **/
3316 s32 ixgbe_blink_led_stop_generic(struct ixgbe_hw *hw, u32 index)
3317 {
3318         u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
3319         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
3320         s32 ret_val = IXGBE_SUCCESS;
3321         bool got_lock = false;
3322
3323         DEBUGFUNC("ixgbe_blink_led_stop_generic");
3324         /* Need the SW/FW semaphore around AUTOC writes if 82599 and
3325          * LESM is on.
3326          */
3327         if ((hw->mac.type == ixgbe_mac_82599EB) &&
3328             ixgbe_verify_lesm_fw_enabled_82599(hw)) {
3329                 ret_val = hw->mac.ops.acquire_swfw_sync(hw,
3330                                                 IXGBE_GSSR_MAC_CSR_SM);
3331                 if (ret_val != IXGBE_SUCCESS) {
3332                         ret_val = IXGBE_ERR_SWFW_SYNC;
3333                         goto out;
3334                 }
3335                 got_lock = true;
3336         }
3337
3338
3339         autoc_reg &= ~IXGBE_AUTOC_FLU;
3340         autoc_reg |= IXGBE_AUTOC_AN_RESTART;
3341         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
3342
3343         if (hw->mac.type == ixgbe_mac_82599EB)
3344                 ixgbe_reset_pipeline_82599(hw);
3345
3346         if (got_lock)
3347                 hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_MAC_CSR_SM);
3348
3349         led_reg &= ~IXGBE_LED_MODE_MASK(index);
3350         led_reg &= ~IXGBE_LED_BLINK(index);
3351         led_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
3352         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
3353         IXGBE_WRITE_FLUSH(hw);
3354
3355 out:
3356         return ret_val;
3357 }
3358
3359 /**
3360  *  ixgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
3361  *  @hw: pointer to hardware structure
3362  *  @san_mac_offset: SAN MAC address offset
3363  *
3364  *  This function will read the EEPROM location for the SAN MAC address
3365  *  pointer, and returns the value at that location.  This is used in both
3366  *  get and set mac_addr routines.
3367  **/
3368 STATIC s32 ixgbe_get_san_mac_addr_offset(struct ixgbe_hw *hw,
3369                                          u16 *san_mac_offset)
3370 {
3371         s32 ret_val;
3372
3373         DEBUGFUNC("ixgbe_get_san_mac_addr_offset");
3374
3375         /*
3376          * First read the EEPROM pointer to see if the MAC addresses are
3377          * available.
3378          */
3379         ret_val = hw->eeprom.ops.read(hw, IXGBE_SAN_MAC_ADDR_PTR,
3380                                       san_mac_offset);
3381         if (ret_val) {
3382                 ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
3383                               "eeprom at offset %d failed",
3384                               IXGBE_SAN_MAC_ADDR_PTR);
3385         }
3386
3387         return ret_val;
3388 }
3389
3390 /**
3391  *  ixgbe_get_san_mac_addr_generic - SAN MAC address retrieval from the EEPROM
3392  *  @hw: pointer to hardware structure
3393  *  @san_mac_addr: SAN MAC address
3394  *
3395  *  Reads the SAN MAC address from the EEPROM, if it's available.  This is
3396  *  per-port, so set_lan_id() must be called before reading the addresses.
3397  *  set_lan_id() is called by identify_sfp(), but this cannot be relied
3398  *  upon for non-SFP connections, so we must call it here.
3399  **/
3400 s32 ixgbe_get_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
3401 {
3402         u16 san_mac_data, san_mac_offset;
3403         u8 i;
3404         s32 ret_val;
3405
3406         DEBUGFUNC("ixgbe_get_san_mac_addr_generic");
3407
3408         /*
3409          * First read the EEPROM pointer to see if the MAC addresses are
3410          * available.  If they're not, no point in calling set_lan_id() here.
3411          */
3412         ret_val = ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
3413         if (ret_val || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
3414                 goto san_mac_addr_out;
3415
3416         /* make sure we know which port we need to program */
3417         hw->mac.ops.set_lan_id(hw);
3418         /* apply the port offset to the address offset */
3419         (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
3420                          (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
3421         for (i = 0; i < 3; i++) {
3422                 ret_val = hw->eeprom.ops.read(hw, san_mac_offset,
3423                                               &san_mac_data);
3424                 if (ret_val) {
3425                         ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
3426                                       "eeprom read at offset %d failed",
3427                                       san_mac_offset);
3428                         goto san_mac_addr_out;
3429                 }
3430                 san_mac_addr[i * 2] = (u8)(san_mac_data);
3431                 san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
3432                 san_mac_offset++;
3433         }
3434         return IXGBE_SUCCESS;
3435
3436 san_mac_addr_out:
3437         /*
3438          * No addresses available in this EEPROM.  It's not an
3439          * error though, so just wipe the local address and return.
3440          */
3441         for (i = 0; i < 6; i++)
3442                 san_mac_addr[i] = 0xFF;
3443         return IXGBE_SUCCESS;
3444 }
3445
3446 /**
3447  *  ixgbe_set_san_mac_addr_generic - Write the SAN MAC address to the EEPROM
3448  *  @hw: pointer to hardware structure
3449  *  @san_mac_addr: SAN MAC address
3450  *
3451  *  Write a SAN MAC address to the EEPROM.
3452  **/
3453 s32 ixgbe_set_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
3454 {
3455         s32 ret_val;
3456         u16 san_mac_data, san_mac_offset;
3457         u8 i;
3458
3459         DEBUGFUNC("ixgbe_set_san_mac_addr_generic");
3460
3461         /* Look for SAN mac address pointer.  If not defined, return */
3462         ret_val = ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
3463         if (ret_val || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
3464                 return IXGBE_ERR_NO_SAN_ADDR_PTR;
3465
3466         /* Make sure we know which port we need to write */
3467         hw->mac.ops.set_lan_id(hw);
3468         /* Apply the port offset to the address offset */
3469         (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
3470                          (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
3471
3472         for (i = 0; i < 3; i++) {
3473                 san_mac_data = (u16)((u16)(san_mac_addr[i * 2 + 1]) << 8);
3474                 san_mac_data |= (u16)(san_mac_addr[i * 2]);
3475                 hw->eeprom.ops.write(hw, san_mac_offset, san_mac_data);
3476                 san_mac_offset++;
3477         }
3478
3479         return IXGBE_SUCCESS;
3480 }
3481
3482 /**
3483  *  ixgbe_get_pcie_msix_count_generic - Gets MSI-X vector count
3484  *  @hw: pointer to hardware structure
3485  *
3486  *  Read PCIe configuration space, and get the MSI-X vector count from
3487  *  the capabilities table.
3488  **/
3489 u16 ixgbe_get_pcie_msix_count_generic(struct ixgbe_hw *hw)
3490 {
3491         u16 msix_count = 1;
3492         u16 max_msix_count;
3493         u16 pcie_offset;
3494
3495         switch (hw->mac.type) {
3496         case ixgbe_mac_82598EB:
3497                 pcie_offset = IXGBE_PCIE_MSIX_82598_CAPS;
3498                 max_msix_count = IXGBE_MAX_MSIX_VECTORS_82598;
3499                 break;
3500         case ixgbe_mac_82599EB:
3501         case ixgbe_mac_X540:
3502                 pcie_offset = IXGBE_PCIE_MSIX_82599_CAPS;
3503                 max_msix_count = IXGBE_MAX_MSIX_VECTORS_82599;
3504                 break;
3505         default:
3506                 return msix_count;
3507         }
3508
3509         DEBUGFUNC("ixgbe_get_pcie_msix_count_generic");
3510         msix_count = IXGBE_READ_PCIE_WORD(hw, pcie_offset);
3511         if (IXGBE_REMOVED(hw->hw_addr))
3512                 msix_count = 0;
3513         msix_count &= IXGBE_PCIE_MSIX_TBL_SZ_MASK;
3514
3515         /* MSI-X count is zero-based in HW */
3516         msix_count++;
3517
3518         if (msix_count > max_msix_count)
3519                 msix_count = max_msix_count;
3520
3521         return msix_count;
3522 }
3523
3524 /**
3525  *  ixgbe_insert_mac_addr_generic - Find a RAR for this mac address
3526  *  @hw: pointer to hardware structure
3527  *  @addr: Address to put into receive address register
3528  *  @vmdq: VMDq pool to assign
3529  *
3530  *  Puts an ethernet address into a receive address register, or
3531  *  finds the rar that it is aleady in; adds to the pool list
3532  **/
3533 s32 ixgbe_insert_mac_addr_generic(struct ixgbe_hw *hw, u8 *addr, u32 vmdq)
3534 {
3535         static const u32 NO_EMPTY_RAR_FOUND = 0xFFFFFFFF;
3536         u32 first_empty_rar = NO_EMPTY_RAR_FOUND;
3537         u32 rar;
3538         u32 rar_low, rar_high;
3539         u32 addr_low, addr_high;
3540
3541         DEBUGFUNC("ixgbe_insert_mac_addr_generic");
3542
3543         /* swap bytes for HW little endian */
3544         addr_low  = addr[0] | (addr[1] << 8)
3545                             | (addr[2] << 16)
3546                             | (addr[3] << 24);
3547         addr_high = addr[4] | (addr[5] << 8);
3548
3549         /*
3550          * Either find the mac_id in rar or find the first empty space.
3551          * rar_highwater points to just after the highest currently used
3552          * rar in order to shorten the search.  It grows when we add a new
3553          * rar to the top.
3554          */
3555         for (rar = 0; rar < hw->mac.rar_highwater; rar++) {
3556                 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(rar));
3557
3558                 if (((IXGBE_RAH_AV & rar_high) == 0)
3559                     && first_empty_rar == NO_EMPTY_RAR_FOUND) {
3560                         first_empty_rar = rar;
3561                 } else if ((rar_high & 0xFFFF) == addr_high) {
3562                         rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(rar));
3563                         if (rar_low == addr_low)
3564                                 break;    /* found it already in the rars */
3565                 }
3566         }
3567
3568         if (rar < hw->mac.rar_highwater) {
3569                 /* already there so just add to the pool bits */
3570                 ixgbe_set_vmdq(hw, rar, vmdq);
3571         } else if (first_empty_rar != NO_EMPTY_RAR_FOUND) {
3572                 /* stick it into first empty RAR slot we found */
3573                 rar = first_empty_rar;
3574                 ixgbe_set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
3575         } else if (rar == hw->mac.rar_highwater) {
3576                 /* add it to the top of the list and inc the highwater mark */
3577                 ixgbe_set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
3578                 hw->mac.rar_highwater++;
3579         } else if (rar >= hw->mac.num_rar_entries) {
3580                 return IXGBE_ERR_INVALID_MAC_ADDR;
3581         }
3582
3583         /*
3584          * If we found rar[0], make sure the default pool bit (we use pool 0)
3585          * remains cleared to be sure default pool packets will get delivered
3586          */
3587         if (rar == 0)
3588                 ixgbe_clear_vmdq(hw, rar, 0);
3589
3590         return rar;
3591 }
3592
3593 /**
3594  *  ixgbe_clear_vmdq_generic - Disassociate a VMDq pool index from a rx address
3595  *  @hw: pointer to hardware struct
3596  *  @rar: receive address register index to disassociate
3597  *  @vmdq: VMDq pool index to remove from the rar
3598  **/
3599 s32 ixgbe_clear_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
3600 {
3601         u32 mpsar_lo, mpsar_hi;
3602         u32 rar_entries = hw->mac.num_rar_entries;
3603
3604         DEBUGFUNC("ixgbe_clear_vmdq_generic");
3605
3606         /* Make sure we are using a valid rar index range */
3607         if (rar >= rar_entries) {
3608                 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT,
3609                              "RAR index %d is out of range.\n", rar);
3610                 return IXGBE_ERR_INVALID_ARGUMENT;
3611         }
3612
3613         mpsar_lo = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
3614         mpsar_hi = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
3615
3616         if (IXGBE_REMOVED(hw->hw_addr))
3617                 goto done;
3618
3619         if (!mpsar_lo && !mpsar_hi)
3620                 goto done;
3621
3622         if (vmdq == IXGBE_CLEAR_VMDQ_ALL) {
3623                 if (mpsar_lo) {
3624                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
3625                         mpsar_lo = 0;
3626                 }
3627                 if (mpsar_hi) {
3628                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
3629                         mpsar_hi = 0;
3630                 }
3631         } else if (vmdq < 32) {
3632                 mpsar_lo &= ~(1 << vmdq);
3633                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar_lo);
3634         } else {
3635                 mpsar_hi &= ~(1 << (vmdq - 32));
3636                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar_hi);
3637         }
3638
3639         /* was that the last pool using this rar? */
3640         if (mpsar_lo == 0 && mpsar_hi == 0 && rar != 0)
3641                 hw->mac.ops.clear_rar(hw, rar);
3642 done:
3643         return IXGBE_SUCCESS;
3644 }
3645
3646 /**
3647  *  ixgbe_set_vmdq_generic - Associate a VMDq pool index with a rx address
3648  *  @hw: pointer to hardware struct
3649  *  @rar: receive address register index to associate with a VMDq index
3650  *  @vmdq: VMDq pool index
3651  **/
3652 s32 ixgbe_set_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
3653 {
3654         u32 mpsar;
3655         u32 rar_entries = hw->mac.num_rar_entries;
3656
3657         DEBUGFUNC("ixgbe_set_vmdq_generic");
3658
3659         /* Make sure we are using a valid rar index range */
3660         if (rar >= rar_entries) {
3661                 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT,
3662                              "RAR index %d is out of range.\n", rar);
3663                 return IXGBE_ERR_INVALID_ARGUMENT;
3664         }
3665
3666         if (vmdq < 32) {
3667                 mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
3668                 mpsar |= 1 << vmdq;
3669                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar);
3670         } else {
3671                 mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
3672                 mpsar |= 1 << (vmdq - 32);
3673                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar);
3674         }
3675         return IXGBE_SUCCESS;
3676 }
3677
3678 /**
3679  *  This function should only be involved in the IOV mode.
3680  *  In IOV mode, Default pool is next pool after the number of
3681  *  VFs advertized and not 0.
3682  *  MPSAR table needs to be updated for SAN_MAC RAR [hw->mac.san_mac_rar_index]
3683  *
3684  *  ixgbe_set_vmdq_san_mac - Associate default VMDq pool index with a rx address
3685  *  @hw: pointer to hardware struct
3686  *  @vmdq: VMDq pool index
3687  **/
3688 s32 ixgbe_set_vmdq_san_mac_generic(struct ixgbe_hw *hw, u32 vmdq)
3689 {
3690         u32 rar = hw->mac.san_mac_rar_index;
3691
3692         DEBUGFUNC("ixgbe_set_vmdq_san_mac");
3693
3694         if (vmdq < 32) {
3695                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 1 << vmdq);
3696                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
3697         } else {
3698                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
3699                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 1 << (vmdq - 32));
3700         }
3701
3702         return IXGBE_SUCCESS;
3703 }
3704
3705 /**
3706  *  ixgbe_init_uta_tables_generic - Initialize the Unicast Table Array
3707  *  @hw: pointer to hardware structure
3708  **/
3709 s32 ixgbe_init_uta_tables_generic(struct ixgbe_hw *hw)
3710 {
3711         int i;
3712
3713         DEBUGFUNC("ixgbe_init_uta_tables_generic");
3714         DEBUGOUT(" Clearing UTA\n");
3715
3716         for (i = 0; i < 128; i++)
3717                 IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
3718
3719         return IXGBE_SUCCESS;
3720 }
3721
3722 /**
3723  *  ixgbe_find_vlvf_slot - find the vlanid or the first empty slot
3724  *  @hw: pointer to hardware structure
3725  *  @vlan: VLAN id to write to VLAN filter
3726  *
3727  *  return the VLVF index where this VLAN id should be placed
3728  *
3729  **/
3730 s32 ixgbe_find_vlvf_slot(struct ixgbe_hw *hw, u32 vlan)
3731 {
3732         u32 bits = 0;
3733         u32 first_empty_slot = 0;
3734         s32 regindex;
3735
3736         /* short cut the special case */
3737         if (vlan == 0)
3738                 return 0;
3739
3740         /*
3741           * Search for the vlan id in the VLVF entries. Save off the first empty
3742           * slot found along the way
3743           */
3744         for (regindex = 1; regindex < IXGBE_VLVF_ENTRIES; regindex++) {
3745                 bits = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
3746                 if (!bits && !(first_empty_slot))
3747                         first_empty_slot = regindex;
3748                 else if ((bits & 0x0FFF) == vlan)
3749                         break;
3750         }
3751
3752         /*
3753           * If regindex is less than IXGBE_VLVF_ENTRIES, then we found the vlan
3754           * in the VLVF. Else use the first empty VLVF register for this
3755           * vlan id.
3756           */
3757         if (regindex >= IXGBE_VLVF_ENTRIES) {
3758                 if (first_empty_slot)
3759                         regindex = first_empty_slot;
3760                 else {
3761                         ERROR_REPORT1(IXGBE_ERROR_SOFTWARE,
3762                                      "No space in VLVF.\n");
3763                         regindex = IXGBE_ERR_NO_SPACE;
3764                 }
3765         }
3766
3767         return regindex;
3768 }
3769
3770 /**
3771  *  ixgbe_set_vfta_generic - Set VLAN filter table
3772  *  @hw: pointer to hardware structure
3773  *  @vlan: VLAN id to write to VLAN filter
3774  *  @vind: VMDq output index that maps queue to VLAN id in VFVFB
3775  *  @vlan_on: boolean flag to turn on/off VLAN in VFVF
3776  *
3777  *  Turn on/off specified VLAN in the VLAN filter table.
3778  **/
3779 s32 ixgbe_set_vfta_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
3780                            bool vlan_on)
3781 {
3782         s32 regindex;
3783         u32 bitindex;
3784         u32 vfta;
3785         u32 targetbit;
3786         s32 ret_val = IXGBE_SUCCESS;
3787         bool vfta_changed = false;
3788
3789         DEBUGFUNC("ixgbe_set_vfta_generic");
3790
3791         if (vlan > 4095)
3792                 return IXGBE_ERR_PARAM;
3793
3794         /*
3795          * this is a 2 part operation - first the VFTA, then the
3796          * VLVF and VLVFB if VT Mode is set
3797          * We don't write the VFTA until we know the VLVF part succeeded.
3798          */
3799
3800         /* Part 1
3801          * The VFTA is a bitstring made up of 128 32-bit registers
3802          * that enable the particular VLAN id, much like the MTA:
3803          *    bits[11-5]: which register
3804          *    bits[4-0]:  which bit in the register
3805          */
3806         regindex = (vlan >> 5) & 0x7F;
3807         bitindex = vlan & 0x1F;
3808         targetbit = (1 << bitindex);
3809         vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(regindex));
3810
3811         if (vlan_on) {
3812                 if (!(vfta & targetbit)) {
3813                         vfta |= targetbit;
3814                         vfta_changed = true;
3815                 }
3816         } else {
3817                 if ((vfta & targetbit)) {
3818                         vfta &= ~targetbit;
3819                         vfta_changed = true;
3820                 }
3821         }
3822
3823         /* Part 2
3824          * Call ixgbe_set_vlvf_generic to set VLVFB and VLVF
3825          */
3826         ret_val = ixgbe_set_vlvf_generic(hw, vlan, vind, vlan_on,
3827                                          &vfta_changed);
3828         if (ret_val != IXGBE_SUCCESS)
3829                 return ret_val;
3830
3831         if (vfta_changed)
3832                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(regindex), vfta);
3833
3834         return IXGBE_SUCCESS;
3835 }
3836
3837 /**
3838  *  ixgbe_set_vlvf_generic - Set VLAN Pool Filter
3839  *  @hw: pointer to hardware structure
3840  *  @vlan: VLAN id to write to VLAN filter
3841  *  @vind: VMDq output index that maps queue to VLAN id in VFVFB
3842  *  @vlan_on: boolean flag to turn on/off VLAN in VFVF
3843  *  @vfta_changed: pointer to boolean flag which indicates whether VFTA
3844  *                 should be changed
3845  *
3846  *  Turn on/off specified bit in VLVF table.
3847  **/
3848 s32 ixgbe_set_vlvf_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
3849                             bool vlan_on, bool *vfta_changed)
3850 {
3851         u32 vt;
3852
3853         DEBUGFUNC("ixgbe_set_vlvf_generic");
3854
3855         if (vlan > 4095)
3856                 return IXGBE_ERR_PARAM;
3857
3858         /* If VT Mode is set
3859          *   Either vlan_on
3860          *     make sure the vlan is in VLVF
3861          *     set the vind bit in the matching VLVFB
3862          *   Or !vlan_on
3863          *     clear the pool bit and possibly the vind
3864          */
3865         vt = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
3866         if (vt & IXGBE_VT_CTL_VT_ENABLE) {
3867                 s32 vlvf_index;
3868                 u32 bits;
3869
3870                 vlvf_index = ixgbe_find_vlvf_slot(hw, vlan);
3871                 if (vlvf_index < 0)
3872                         return vlvf_index;
3873
3874                 if (vlan_on) {
3875                         /* set the pool bit */
3876                         if (vind < 32) {
3877                                 bits = IXGBE_READ_REG(hw,
3878                                                 IXGBE_VLVFB(vlvf_index * 2));
3879                                 bits |= (1 << vind);
3880                                 IXGBE_WRITE_REG(hw,
3881                                                 IXGBE_VLVFB(vlvf_index * 2),
3882                                                 bits);
3883                         } else {
3884                                 bits = IXGBE_READ_REG(hw,
3885                                         IXGBE_VLVFB((vlvf_index * 2) + 1));
3886                                 bits |= (1 << (vind - 32));
3887                                 IXGBE_WRITE_REG(hw,
3888                                         IXGBE_VLVFB((vlvf_index * 2) + 1),
3889                                         bits);
3890                         }
3891                 } else {
3892                         /* clear the pool bit */
3893                         if (vind < 32) {
3894                                 bits = IXGBE_READ_REG(hw,
3895                                                 IXGBE_VLVFB(vlvf_index * 2));
3896                                 bits &= ~(1 << vind);
3897                                 IXGBE_WRITE_REG(hw,
3898                                                 IXGBE_VLVFB(vlvf_index * 2),
3899                                                 bits);
3900                                 bits |= IXGBE_READ_REG(hw,
3901                                         IXGBE_VLVFB((vlvf_index * 2) + 1));
3902                         } else {
3903                                 bits = IXGBE_READ_REG(hw,
3904                                         IXGBE_VLVFB((vlvf_index * 2) + 1));
3905                                 bits &= ~(1 << (vind - 32));
3906                                 IXGBE_WRITE_REG(hw,
3907                                         IXGBE_VLVFB((vlvf_index * 2) + 1),
3908                                         bits);
3909                                 bits |= IXGBE_READ_REG(hw,
3910                                                 IXGBE_VLVFB(vlvf_index * 2));
3911                         }
3912                 }
3913
3914                 /*
3915                  * If there are still bits set in the VLVFB registers
3916                  * for the VLAN ID indicated we need to see if the
3917                  * caller is requesting that we clear the VFTA entry bit.
3918                  * If the caller has requested that we clear the VFTA
3919                  * entry bit but there are still pools/VFs using this VLAN
3920                  * ID entry then ignore the request.  We're not worried
3921                  * about the case where we're turning the VFTA VLAN ID
3922                  * entry bit on, only when requested to turn it off as
3923                  * there may be multiple pools and/or VFs using the
3924                  * VLAN ID entry.  In that case we cannot clear the
3925                  * VFTA bit until all pools/VFs using that VLAN ID have also
3926                  * been cleared.  This will be indicated by "bits" being
3927                  * zero.
3928                  */
3929                 if (bits) {
3930                         IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index),
3931                                         (IXGBE_VLVF_VIEN | vlan));
3932                         if ((!vlan_on) && (vfta_changed != NULL)) {
3933                                 /* someone wants to clear the vfta entry
3934                                  * but some pools/VFs are still using it.
3935                                  * Ignore it. */
3936                                 *vfta_changed = false;
3937                         }
3938                 } else
3939                         IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), 0);
3940         }
3941
3942         return IXGBE_SUCCESS;
3943 }
3944
3945 /**
3946  *  ixgbe_clear_vfta_generic - Clear VLAN filter table
3947  *  @hw: pointer to hardware structure
3948  *
3949  *  Clears the VLAN filer table, and the VMDq index associated with the filter
3950  **/
3951 s32 ixgbe_clear_vfta_generic(struct ixgbe_hw *hw)
3952 {
3953         u32 offset;
3954
3955         DEBUGFUNC("ixgbe_clear_vfta_generic");
3956
3957         for (offset = 0; offset < hw->mac.vft_size; offset++)
3958                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(offset), 0);
3959
3960         for (offset = 0; offset < IXGBE_VLVF_ENTRIES; offset++) {
3961                 IXGBE_WRITE_REG(hw, IXGBE_VLVF(offset), 0);
3962                 IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset * 2), 0);
3963                 IXGBE_WRITE_REG(hw, IXGBE_VLVFB((offset * 2) + 1), 0);
3964         }
3965
3966         return IXGBE_SUCCESS;
3967 }
3968
3969 /**
3970  *  ixgbe_check_mac_link_generic - Determine link and speed status
3971  *  @hw: pointer to hardware structure
3972  *  @speed: pointer to link speed
3973  *  @link_up: true when link is up
3974  *  @link_up_wait_to_complete: bool used to wait for link up or not
3975  *
3976  *  Reads the links register to determine if link is up and the current speed
3977  **/
3978 s32 ixgbe_check_mac_link_generic(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
3979                                  bool *link_up, bool link_up_wait_to_complete)
3980 {
3981         u32 links_reg, links_orig;
3982         u32 i;
3983
3984         DEBUGFUNC("ixgbe_check_mac_link_generic");
3985
3986         /* clear the old state */
3987         links_orig = IXGBE_READ_REG(hw, IXGBE_LINKS);
3988
3989         links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
3990
3991         if (links_orig != links_reg) {
3992                 DEBUGOUT2("LINKS changed from %08X to %08X\n",
3993                           links_orig, links_reg);
3994         }
3995
3996         if (link_up_wait_to_complete) {
3997                 for (i = 0; i < IXGBE_LINK_UP_TIME; i++) {
3998                         if (links_reg & IXGBE_LINKS_UP) {
3999                                 *link_up = true;
4000                                 break;
4001                         } else {
4002                                 *link_up = false;
4003                         }
4004                         msec_delay(100);
4005                         links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
4006                 }
4007         } else {
4008                 if (links_reg & IXGBE_LINKS_UP)
4009                         *link_up = true;
4010                 else
4011                         *link_up = false;
4012         }
4013
4014         if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
4015             IXGBE_LINKS_SPEED_10G_82599)
4016                 *speed = IXGBE_LINK_SPEED_10GB_FULL;
4017         else if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
4018                  IXGBE_LINKS_SPEED_1G_82599)
4019                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
4020         else if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
4021                  IXGBE_LINKS_SPEED_100_82599)
4022                 *speed = IXGBE_LINK_SPEED_100_FULL;
4023         else
4024                 *speed = IXGBE_LINK_SPEED_UNKNOWN;
4025
4026         return IXGBE_SUCCESS;
4027 }
4028
4029 /**
4030  *  ixgbe_get_wwn_prefix_generic - Get alternative WWNN/WWPN prefix from
4031  *  the EEPROM
4032  *  @hw: pointer to hardware structure
4033  *  @wwnn_prefix: the alternative WWNN prefix
4034  *  @wwpn_prefix: the alternative WWPN prefix
4035  *
4036  *  This function will read the EEPROM from the alternative SAN MAC address
4037  *  block to check the support for the alternative WWNN/WWPN prefix support.
4038  **/
4039 s32 ixgbe_get_wwn_prefix_generic(struct ixgbe_hw *hw, u16 *wwnn_prefix,
4040                                  u16 *wwpn_prefix)
4041 {
4042         u16 offset, caps;
4043         u16 alt_san_mac_blk_offset;
4044
4045         DEBUGFUNC("ixgbe_get_wwn_prefix_generic");
4046
4047         /* clear output first */
4048         *wwnn_prefix = 0xFFFF;
4049         *wwpn_prefix = 0xFFFF;
4050
4051         /* check if alternative SAN MAC is supported */
4052         offset = IXGBE_ALT_SAN_MAC_ADDR_BLK_PTR;
4053         if (hw->eeprom.ops.read(hw, offset, &alt_san_mac_blk_offset))
4054                 goto wwn_prefix_err;
4055
4056         if ((alt_san_mac_blk_offset == 0) ||
4057             (alt_san_mac_blk_offset == 0xFFFF))
4058                 goto wwn_prefix_out;
4059
4060         /* check capability in alternative san mac address block */
4061         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_CAPS_OFFSET;
4062         if (hw->eeprom.ops.read(hw, offset, &caps))
4063                 goto wwn_prefix_err;
4064         if (!(caps & IXGBE_ALT_SAN_MAC_ADDR_CAPS_ALTWWN))
4065                 goto wwn_prefix_out;
4066
4067         /* get the corresponding prefix for WWNN/WWPN */
4068         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWNN_OFFSET;
4069         if (hw->eeprom.ops.read(hw, offset, wwnn_prefix)) {
4070                 ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
4071                               "eeprom read at offset %d failed", offset);
4072         }
4073
4074         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWPN_OFFSET;
4075         if (hw->eeprom.ops.read(hw, offset, wwpn_prefix))
4076                 goto wwn_prefix_err;
4077
4078 wwn_prefix_out:
4079         return IXGBE_SUCCESS;
4080
4081 wwn_prefix_err:
4082         ERROR_REPORT2(IXGBE_ERROR_INVALID_STATE,
4083                       "eeprom read at offset %d failed", offset);
4084         return IXGBE_SUCCESS;
4085 }
4086
4087 /**
4088  *  ixgbe_get_fcoe_boot_status_generic - Get FCOE boot status from EEPROM
4089  *  @hw: pointer to hardware structure
4090  *  @bs: the fcoe boot status
4091  *
4092  *  This function will read the FCOE boot status from the iSCSI FCOE block
4093  **/
4094 s32 ixgbe_get_fcoe_boot_status_generic(struct ixgbe_hw *hw, u16 *bs)
4095 {
4096         u16 offset, caps, flags;
4097         s32 status;
4098
4099         DEBUGFUNC("ixgbe_get_fcoe_boot_status_generic");
4100
4101         /* clear output first */
4102         *bs = ixgbe_fcoe_bootstatus_unavailable;
4103
4104         /* check if FCOE IBA block is present */
4105         offset = IXGBE_FCOE_IBA_CAPS_BLK_PTR;
4106         status = hw->eeprom.ops.read(hw, offset, &caps);
4107         if (status != IXGBE_SUCCESS)
4108                 goto out;
4109
4110         if (!(caps & IXGBE_FCOE_IBA_CAPS_FCOE))
4111                 goto out;
4112
4113         /* check if iSCSI FCOE block is populated */
4114         status = hw->eeprom.ops.read(hw, IXGBE_ISCSI_FCOE_BLK_PTR, &offset);
4115         if (status != IXGBE_SUCCESS)
4116                 goto out;
4117
4118         if ((offset == 0) || (offset == 0xFFFF))
4119                 goto out;
4120
4121         /* read fcoe flags in iSCSI FCOE block */
4122         offset = offset + IXGBE_ISCSI_FCOE_FLAGS_OFFSET;
4123         status = hw->eeprom.ops.read(hw, offset, &flags);
4124         if (status != IXGBE_SUCCESS)
4125                 goto out;
4126
4127         if (flags & IXGBE_ISCSI_FCOE_FLAGS_ENABLE)
4128                 *bs = ixgbe_fcoe_bootstatus_enabled;
4129         else
4130                 *bs = ixgbe_fcoe_bootstatus_disabled;
4131
4132 out:
4133         return status;
4134 }
4135
4136 /**
4137  *  ixgbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
4138  *  @hw: pointer to hardware structure
4139  *  @enable: enable or disable switch for anti-spoofing
4140  *  @pf: Physical Function pool - do not enable anti-spoofing for the PF
4141  *
4142  **/
4143 void ixgbe_set_mac_anti_spoofing(struct ixgbe_hw *hw, bool enable, int pf)
4144 {
4145         int j;
4146         int pf_target_reg = pf >> 3;
4147         int pf_target_shift = pf % 8;
4148         u32 pfvfspoof = 0;
4149
4150         if (hw->mac.type == ixgbe_mac_82598EB)
4151                 return;
4152
4153         if (enable)
4154                 pfvfspoof = IXGBE_SPOOF_MACAS_MASK;
4155
4156         /*
4157          * PFVFSPOOF register array is size 8 with 8 bits assigned to
4158          * MAC anti-spoof enables in each register array element.
4159          */
4160         for (j = 0; j < pf_target_reg; j++)
4161                 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
4162
4163         /*
4164          * The PF should be allowed to spoof so that it can support
4165          * emulation mode NICs.  Do not set the bits assigned to the PF
4166          */
4167         pfvfspoof &= (1 << pf_target_shift) - 1;
4168         IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
4169
4170         /*
4171          * Remaining pools belong to the PF so they do not need to have
4172          * anti-spoofing enabled.
4173          */
4174         for (j++; j < IXGBE_PFVFSPOOF_REG_COUNT; j++)
4175                 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), 0);
4176 }
4177
4178 /**
4179  *  ixgbe_set_vlan_anti_spoofing - Enable/Disable VLAN anti-spoofing
4180  *  @hw: pointer to hardware structure
4181  *  @enable: enable or disable switch for VLAN anti-spoofing
4182  *  @pf: Virtual Function pool - VF Pool to set for VLAN anti-spoofing
4183  *
4184  **/
4185 void ixgbe_set_vlan_anti_spoofing(struct ixgbe_hw *hw, bool enable, int vf)
4186 {
4187         int vf_target_reg = vf >> 3;
4188         int vf_target_shift = vf % 8 + IXGBE_SPOOF_VLANAS_SHIFT;
4189         u32 pfvfspoof;
4190
4191         if (hw->mac.type == ixgbe_mac_82598EB)
4192                 return;
4193
4194         pfvfspoof = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
4195         if (enable)
4196                 pfvfspoof |= (1 << vf_target_shift);
4197         else
4198                 pfvfspoof &= ~(1 << vf_target_shift);
4199         IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), pfvfspoof);
4200 }
4201
4202 /**
4203  *  ixgbe_get_device_caps_generic - Get additional device capabilities
4204  *  @hw: pointer to hardware structure
4205  *  @device_caps: the EEPROM word with the extra device capabilities
4206  *
4207  *  This function will read the EEPROM location for the device capabilities,
4208  *  and return the word through device_caps.
4209  **/
4210 s32 ixgbe_get_device_caps_generic(struct ixgbe_hw *hw, u16 *device_caps)
4211 {
4212         DEBUGFUNC("ixgbe_get_device_caps_generic");
4213
4214         hw->eeprom.ops.read(hw, IXGBE_DEVICE_CAPS, device_caps);
4215
4216         return IXGBE_SUCCESS;
4217 }
4218
4219 /**
4220  *  ixgbe_enable_relaxed_ordering_gen2 - Enable relaxed ordering
4221  *  @hw: pointer to hardware structure
4222  *
4223  **/
4224 void ixgbe_enable_relaxed_ordering_gen2(struct ixgbe_hw *hw)
4225 {
4226         u32 regval;
4227         u32 i;
4228
4229         DEBUGFUNC("ixgbe_enable_relaxed_ordering_gen2");
4230
4231         /* Enable relaxed ordering */
4232         for (i = 0; i < hw->mac.max_tx_queues; i++) {
4233                 regval = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
4234                 regval |= IXGBE_DCA_TXCTRL_DESC_WRO_EN;
4235                 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), regval);
4236         }
4237
4238         for (i = 0; i < hw->mac.max_rx_queues; i++) {
4239                 regval = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
4240                 regval |= IXGBE_DCA_RXCTRL_DATA_WRO_EN |
4241                           IXGBE_DCA_RXCTRL_HEAD_WRO_EN;
4242                 IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(i), regval);
4243         }
4244
4245 }
4246
4247 /**
4248  *  ixgbe_calculate_checksum - Calculate checksum for buffer
4249  *  @buffer: pointer to EEPROM
4250  *  @length: size of EEPROM to calculate a checksum for
4251  *  Calculates the checksum for some buffer on a specified length.  The
4252  *  checksum calculated is returned.
4253  **/
4254 u8 ixgbe_calculate_checksum(u8 *buffer, u32 length)
4255 {
4256         u32 i;
4257         u8 sum = 0;
4258
4259         DEBUGFUNC("ixgbe_calculate_checksum");
4260
4261         if (!buffer)
4262                 return 0;
4263
4264         for (i = 0; i < length; i++)
4265                 sum += buffer[i];
4266
4267         return (u8) (0 - sum);
4268 }
4269
4270 /**
4271  *  ixgbe_host_interface_command - Issue command to manageability block
4272  *  @hw: pointer to the HW structure
4273  *  @buffer: contains the command to write and where the return status will
4274  *   be placed
4275  *  @length: length of buffer, must be multiple of 4 bytes
4276  *
4277  *  Communicates with the manageability block.  On success return IXGBE_SUCCESS
4278  *  else return IXGBE_ERR_HOST_INTERFACE_COMMAND.
4279  **/
4280 s32 ixgbe_host_interface_command(struct ixgbe_hw *hw, u32 *buffer,
4281                                  u32 length)
4282 {
4283         u32 hicr, i, bi;
4284         u32 hdr_size = sizeof(struct ixgbe_hic_hdr);
4285         u8 buf_len, dword_len;
4286
4287         s32 ret_val = IXGBE_SUCCESS;
4288
4289         DEBUGFUNC("ixgbe_host_interface_command");
4290
4291         if (length == 0 || length & 0x3 ||
4292             length > IXGBE_HI_MAX_BLOCK_BYTE_LENGTH) {
4293                 DEBUGOUT("Buffer length failure.\n");
4294                 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
4295                 goto out;
4296         }
4297
4298         /* Check that the host interface is enabled. */
4299         hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
4300         if ((hicr & IXGBE_HICR_EN) == 0) {
4301                 DEBUGOUT("IXGBE_HOST_EN bit disabled.\n");
4302                 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
4303                 goto out;
4304         }
4305
4306         /* Calculate length in DWORDs */
4307         dword_len = length >> 2;
4308
4309         /*
4310          * The device driver writes the relevant command block
4311          * into the ram area.
4312          */
4313         for (i = 0; i < dword_len; i++)
4314                 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_FLEX_MNG,
4315                                       i, IXGBE_CPU_TO_LE32(buffer[i]));
4316
4317         /* Setting this bit tells the ARC that a new command is pending. */
4318         IXGBE_WRITE_REG(hw, IXGBE_HICR, hicr | IXGBE_HICR_C);
4319
4320         for (i = 0; i < IXGBE_HI_COMMAND_TIMEOUT; i++) {
4321                 hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
4322                 if (!(hicr & IXGBE_HICR_C))
4323                         break;
4324                 msec_delay(1);
4325         }
4326
4327         /* Check command successful completion. */
4328         if (i == IXGBE_HI_COMMAND_TIMEOUT ||
4329             (!(IXGBE_READ_REG(hw, IXGBE_HICR) & IXGBE_HICR_SV))) {
4330                 DEBUGOUT("Command has failed with no status valid.\n");
4331                 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
4332                 goto out;
4333         }
4334
4335         /* Calculate length in DWORDs */
4336         dword_len = hdr_size >> 2;
4337
4338         /* first pull in the header so we know the buffer length */
4339         for (bi = 0; bi < dword_len; bi++) {
4340                 buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
4341                 IXGBE_LE32_TO_CPUS(&buffer[bi]);
4342         }
4343
4344         /* If there is any thing in data position pull it in */
4345         buf_len = ((struct ixgbe_hic_hdr *)buffer)->buf_len;
4346         if (buf_len == 0)
4347                 goto out;
4348
4349         if (length < (buf_len + hdr_size)) {
4350                 DEBUGOUT("Buffer not large enough for reply message.\n");
4351                 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
4352                 goto out;
4353         }
4354
4355         /* Calculate length in DWORDs, add 3 for odd lengths */
4356         dword_len = (buf_len + 3) >> 2;
4357
4358         /* Pull in the rest of the buffer (bi is where we left off)*/
4359         for (; bi <= dword_len; bi++) {
4360                 buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
4361                 IXGBE_LE32_TO_CPUS(&buffer[bi]);
4362         }
4363
4364 out:
4365         return ret_val;
4366 }
4367
4368 /**
4369  *  ixgbe_set_fw_drv_ver_generic - Sends driver version to firmware
4370  *  @hw: pointer to the HW structure
4371  *  @maj: driver version major number
4372  *  @min: driver version minor number
4373  *  @build: driver version build number
4374  *  @sub: driver version sub build number
4375  *
4376  *  Sends driver version number to firmware through the manageability
4377  *  block.  On success return IXGBE_SUCCESS
4378  *  else returns IXGBE_ERR_SWFW_SYNC when encountering an error acquiring
4379  *  semaphore or IXGBE_ERR_HOST_INTERFACE_COMMAND when command fails.
4380  **/
4381 s32 ixgbe_set_fw_drv_ver_generic(struct ixgbe_hw *hw, u8 maj, u8 min,
4382                                  u8 build, u8 sub)
4383 {
4384         struct ixgbe_hic_drv_info fw_cmd;
4385         int i;
4386         s32 ret_val = IXGBE_SUCCESS;
4387
4388         DEBUGFUNC("ixgbe_set_fw_drv_ver_generic");
4389
4390         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM)
4391             != IXGBE_SUCCESS) {
4392                 ret_val = IXGBE_ERR_SWFW_SYNC;
4393                 goto out;
4394         }
4395
4396         fw_cmd.hdr.cmd = FW_CEM_CMD_DRIVER_INFO;
4397         fw_cmd.hdr.buf_len = FW_CEM_CMD_DRIVER_INFO_LEN;
4398         fw_cmd.hdr.cmd_or_resp.cmd_resv = FW_CEM_CMD_RESERVED;
4399         fw_cmd.port_num = (u8)hw->bus.func;
4400         fw_cmd.ver_maj = maj;
4401         fw_cmd.ver_min = min;
4402         fw_cmd.ver_build = build;
4403         fw_cmd.ver_sub = sub;
4404         fw_cmd.hdr.checksum = 0;
4405         fw_cmd.hdr.checksum = ixgbe_calculate_checksum((u8 *)&fw_cmd,
4406                                 (FW_CEM_HDR_LEN + fw_cmd.hdr.buf_len));
4407         fw_cmd.pad = 0;
4408         fw_cmd.pad2 = 0;
4409
4410         for (i = 0; i <= FW_CEM_MAX_RETRIES; i++) {
4411                 ret_val = ixgbe_host_interface_command(hw, (u32 *)&fw_cmd,
4412                                                        sizeof(fw_cmd));
4413                 if (ret_val != IXGBE_SUCCESS)
4414                         continue;
4415
4416                 if (fw_cmd.hdr.cmd_or_resp.ret_status ==
4417                     FW_CEM_RESP_STATUS_SUCCESS)
4418                         ret_val = IXGBE_SUCCESS;
4419                 else
4420                         ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
4421
4422                 break;
4423         }
4424
4425         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM);
4426 out:
4427         return ret_val;
4428 }
4429
4430 /**
4431  * ixgbe_set_rxpba_generic - Initialize Rx packet buffer
4432  * @hw: pointer to hardware structure
4433  * @num_pb: number of packet buffers to allocate
4434  * @headroom: reserve n KB of headroom
4435  * @strategy: packet buffer allocation strategy
4436  **/
4437 void ixgbe_set_rxpba_generic(struct ixgbe_hw *hw, int num_pb, u32 headroom,
4438                              int strategy)
4439 {
4440         u32 pbsize = hw->mac.rx_pb_size;
4441         int i = 0;
4442         u32 rxpktsize, txpktsize, txpbthresh;
4443
4444         /* Reserve headroom */
4445         pbsize -= headroom;
4446
4447         if (!num_pb)
4448                 num_pb = 1;
4449
4450         /* Divide remaining packet buffer space amongst the number of packet
4451          * buffers requested using supplied strategy.
4452          */
4453         switch (strategy) {
4454         case PBA_STRATEGY_WEIGHTED:
4455                 /* ixgbe_dcb_pba_80_48 strategy weight first half of packet
4456                  * buffer with 5/8 of the packet buffer space.
4457                  */
4458                 rxpktsize = (pbsize * 5) / (num_pb * 4);
4459                 pbsize -= rxpktsize * (num_pb / 2);
4460                 rxpktsize <<= IXGBE_RXPBSIZE_SHIFT;
4461                 for (; i < (num_pb / 2); i++)
4462                         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
4463                 /* Fall through to configure remaining packet buffers */
4464         case PBA_STRATEGY_EQUAL:
4465                 rxpktsize = (pbsize / (num_pb - i)) << IXGBE_RXPBSIZE_SHIFT;
4466                 for (; i < num_pb; i++)
4467                         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
4468                 break;
4469         default:
4470                 break;
4471         }
4472
4473         /* Only support an equally distributed Tx packet buffer strategy. */
4474         txpktsize = IXGBE_TXPBSIZE_MAX / num_pb;
4475         txpbthresh = (txpktsize / 1024) - IXGBE_TXPKT_SIZE_MAX;
4476         for (i = 0; i < num_pb; i++) {
4477                 IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), txpktsize);
4478                 IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), txpbthresh);
4479         }
4480
4481         /* Clear unused TCs, if any, to zero buffer size*/
4482         for (; i < IXGBE_MAX_PB; i++) {
4483                 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
4484                 IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), 0);
4485                 IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), 0);
4486         }
4487 }
4488
4489 /**
4490  * ixgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
4491  * @hw: pointer to the hardware structure
4492  *
4493  * The 82599 and x540 MACs can experience issues if TX work is still pending
4494  * when a reset occurs.  This function prevents this by flushing the PCIe
4495  * buffers on the system.
4496  **/
4497 void ixgbe_clear_tx_pending(struct ixgbe_hw *hw)
4498 {
4499         u32 gcr_ext, hlreg0;
4500
4501         /*
4502          * If double reset is not requested then all transactions should
4503          * already be clear and as such there is no work to do
4504          */
4505         if (!(hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
4506                 return;
4507
4508         /*
4509          * Set loopback enable to prevent any transmits from being sent
4510          * should the link come up.  This assumes that the RXCTRL.RXEN bit
4511          * has already been cleared.
4512          */
4513         hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
4514         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0 | IXGBE_HLREG0_LPBK);
4515
4516         /* initiate cleaning flow for buffers in the PCIe transaction layer */
4517         gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
4518         IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT,
4519                         gcr_ext | IXGBE_GCR_EXT_BUFFERS_CLEAR);
4520
4521         /* Flush all writes and allow 20usec for all transactions to clear */
4522         IXGBE_WRITE_FLUSH(hw);
4523         usec_delay(20);
4524
4525         /* restore previous register values */
4526         IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
4527         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
4528 }
4529