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