]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ixgbe/ixgbe_common.c
Add support for the Apple Big Mac (BMAC) Ethernet controller,
[FreeBSD/FreeBSD.git] / sys / dev / ixgbe / ixgbe_common.c
1 /******************************************************************************
2
3   Copyright (c) 2001-2008, 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_api.h"
37
38 static s32 ixgbe_poll_eeprom_eerd_done(struct ixgbe_hw *hw);
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 static u16 ixgbe_calc_eeprom_checksum(struct ixgbe_hw *hw);
51
52 static void ixgbe_enable_rar(struct ixgbe_hw *hw, u32 index);
53 static void ixgbe_disable_rar(struct ixgbe_hw *hw, u32 index);
54 static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr);
55 void ixgbe_add_mc_addr(struct ixgbe_hw *hw, u8 *mc_addr);
56 void ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq);
57
58 /**
59  *  ixgbe_init_ops_generic - Inits function ptrs
60  *  @hw: pointer to the hardware structure
61  *
62  *  Initialize the function pointers.
63  **/
64 s32 ixgbe_init_ops_generic(struct ixgbe_hw *hw)
65 {
66         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
67         struct ixgbe_mac_info *mac = &hw->mac;
68         u32 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
69
70         /* EEPROM */
71         eeprom->ops.init_params = &ixgbe_init_eeprom_params_generic;
72         /* If EEPROM is valid (bit 8 = 1), use EERD otherwise use bit bang */
73         if (eec & (1 << 8))
74                 eeprom->ops.read = &ixgbe_read_eeprom_generic;
75         else
76                 eeprom->ops.read = &ixgbe_read_eeprom_bit_bang_generic;
77         eeprom->ops.write = &ixgbe_write_eeprom_generic;
78         eeprom->ops.validate_checksum =
79                                       &ixgbe_validate_eeprom_checksum_generic;
80         eeprom->ops.update_checksum = &ixgbe_update_eeprom_checksum_generic;
81
82         /* MAC */
83         mac->ops.init_hw = &ixgbe_init_hw_generic;
84         mac->ops.reset_hw = NULL;
85         mac->ops.start_hw = &ixgbe_start_hw_generic;
86         mac->ops.clear_hw_cntrs = &ixgbe_clear_hw_cntrs_generic;
87         mac->ops.get_media_type = NULL;
88         mac->ops.get_mac_addr = &ixgbe_get_mac_addr_generic;
89         mac->ops.stop_adapter = &ixgbe_stop_adapter_generic;
90         mac->ops.get_bus_info = &ixgbe_get_bus_info_generic;
91         mac->ops.read_analog_reg8 = &ixgbe_read_analog_reg8_generic;
92         mac->ops.write_analog_reg8 = &ixgbe_write_analog_reg8_generic;
93
94         /* LEDs */
95         mac->ops.led_on = &ixgbe_led_on_generic;
96         mac->ops.led_off = &ixgbe_led_off_generic;
97         mac->ops.blink_led_start = NULL;
98         mac->ops.blink_led_stop = NULL;
99
100         /* RAR, Multicast, VLAN */
101         mac->ops.set_rar = &ixgbe_set_rar_generic;
102         mac->ops.set_vmdq = NULL;
103         mac->ops.init_rx_addrs = &ixgbe_init_rx_addrs_generic;
104         mac->ops.update_uc_addr_list = &ixgbe_update_uc_addr_list_generic;
105         mac->ops.update_mc_addr_list = &ixgbe_update_mc_addr_list_generic;
106         mac->ops.enable_mc = &ixgbe_enable_mc_generic;
107         mac->ops.disable_mc = &ixgbe_disable_mc_generic;
108         mac->ops.clear_vfta = &ixgbe_clear_vfta_generic;
109         mac->ops.set_vfta = &ixgbe_set_vfta_generic;
110
111         /* Flow Control */
112         mac->ops.setup_fc = NULL;
113
114         /* Link */
115         mac->ops.get_link_capabilities = NULL;
116         mac->ops.setup_link = NULL;
117         mac->ops.setup_link_speed = NULL;
118         mac->ops.check_link = NULL;
119
120         return IXGBE_SUCCESS;
121 }
122
123 /**
124  *  ixgbe_start_hw_generic - Prepare hardware for Tx/Rx
125  *  @hw: pointer to hardware structure
126  *
127  *  Starts the hardware by filling the bus info structure and media type, clears
128  *  all on chip counters, initializes receive address registers, multicast
129  *  table, VLAN filter table, calls routine to set up link and flow control
130  *  settings, and leaves transmit and receive units disabled and uninitialized
131  **/
132 s32 ixgbe_start_hw_generic(struct ixgbe_hw *hw)
133 {
134         u32 ctrl_ext;
135
136         /* Set the media type */
137         hw->phy.media_type = hw->mac.ops.get_media_type(hw);
138
139         /* Set bus info */
140         hw->mac.ops.get_bus_info(hw);
141
142         /* Identify the PHY */
143         hw->phy.ops.identify(hw);
144
145         /*
146          * Store MAC address from RAR0, clear receive address registers, and
147          * clear the multicast table
148          */
149         hw->mac.ops.init_rx_addrs(hw);
150
151         /* Clear the VLAN filter table */
152         hw->mac.ops.clear_vfta(hw);
153
154         /* Set up link */
155         hw->mac.ops.setup_link(hw);
156
157         /* Clear statistics registers */
158         hw->mac.ops.clear_hw_cntrs(hw);
159
160         /* Set No Snoop Disable */
161         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
162         ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
163         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
164         IXGBE_WRITE_FLUSH(hw);
165
166         /* Clear adapter stopped flag */
167         hw->adapter_stopped = FALSE;
168
169         return IXGBE_SUCCESS;
170 }
171
172 /**
173  *  ixgbe_init_hw_generic - Generic hardware initialization
174  *  @hw: pointer to hardware structure
175  *
176  *  Initialize the hardware by resetting the hardware, filling the bus info
177  *  structure and media type, clears all on chip counters, initializes receive
178  *  address registers, multicast table, VLAN filter table, calls routine to set
179  *  up link and flow control settings, and leaves transmit and receive units
180  *  disabled and uninitialized
181  **/
182 s32 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
183 {
184         /* Reset the hardware */
185         hw->mac.ops.reset_hw(hw);
186
187         /* Start the HW */
188         hw->mac.ops.start_hw(hw);
189
190         return IXGBE_SUCCESS;
191 }
192
193 /**
194  *  ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
195  *  @hw: pointer to hardware structure
196  *
197  *  Clears all hardware statistics counters by reading them from the hardware
198  *  Statistics counters are clear on read.
199  **/
200 s32 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
201 {
202         u16 i = 0;
203
204         IXGBE_READ_REG(hw, IXGBE_CRCERRS);
205         IXGBE_READ_REG(hw, IXGBE_ILLERRC);
206         IXGBE_READ_REG(hw, IXGBE_ERRBC);
207         IXGBE_READ_REG(hw, IXGBE_MSPDC);
208         for (i = 0; i < 8; i++)
209                 IXGBE_READ_REG(hw, IXGBE_MPC(i));
210
211         IXGBE_READ_REG(hw, IXGBE_MLFC);
212         IXGBE_READ_REG(hw, IXGBE_MRFC);
213         IXGBE_READ_REG(hw, IXGBE_RLEC);
214         IXGBE_READ_REG(hw, IXGBE_LXONTXC);
215         IXGBE_READ_REG(hw, IXGBE_LXONRXC);
216         IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
217         IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
218
219         for (i = 0; i < 8; i++) {
220                 IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
221                 IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
222                 IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
223                 IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
224         }
225
226         IXGBE_READ_REG(hw, IXGBE_PRC64);
227         IXGBE_READ_REG(hw, IXGBE_PRC127);
228         IXGBE_READ_REG(hw, IXGBE_PRC255);
229         IXGBE_READ_REG(hw, IXGBE_PRC511);
230         IXGBE_READ_REG(hw, IXGBE_PRC1023);
231         IXGBE_READ_REG(hw, IXGBE_PRC1522);
232         IXGBE_READ_REG(hw, IXGBE_GPRC);
233         IXGBE_READ_REG(hw, IXGBE_BPRC);
234         IXGBE_READ_REG(hw, IXGBE_MPRC);
235         IXGBE_READ_REG(hw, IXGBE_GPTC);
236         IXGBE_READ_REG(hw, IXGBE_GORCL);
237         IXGBE_READ_REG(hw, IXGBE_GORCH);
238         IXGBE_READ_REG(hw, IXGBE_GOTCL);
239         IXGBE_READ_REG(hw, IXGBE_GOTCH);
240         for (i = 0; i < 8; i++)
241                 IXGBE_READ_REG(hw, IXGBE_RNBC(i));
242         IXGBE_READ_REG(hw, IXGBE_RUC);
243         IXGBE_READ_REG(hw, IXGBE_RFC);
244         IXGBE_READ_REG(hw, IXGBE_ROC);
245         IXGBE_READ_REG(hw, IXGBE_RJC);
246         IXGBE_READ_REG(hw, IXGBE_MNGPRC);
247         IXGBE_READ_REG(hw, IXGBE_MNGPDC);
248         IXGBE_READ_REG(hw, IXGBE_MNGPTC);
249         IXGBE_READ_REG(hw, IXGBE_TORL);
250         IXGBE_READ_REG(hw, IXGBE_TORH);
251         IXGBE_READ_REG(hw, IXGBE_TPR);
252         IXGBE_READ_REG(hw, IXGBE_TPT);
253         IXGBE_READ_REG(hw, IXGBE_PTC64);
254         IXGBE_READ_REG(hw, IXGBE_PTC127);
255         IXGBE_READ_REG(hw, IXGBE_PTC255);
256         IXGBE_READ_REG(hw, IXGBE_PTC511);
257         IXGBE_READ_REG(hw, IXGBE_PTC1023);
258         IXGBE_READ_REG(hw, IXGBE_PTC1522);
259         IXGBE_READ_REG(hw, IXGBE_MPTC);
260         IXGBE_READ_REG(hw, IXGBE_BPTC);
261         for (i = 0; i < 16; i++) {
262                 IXGBE_READ_REG(hw, IXGBE_QPRC(i));
263                 IXGBE_READ_REG(hw, IXGBE_QBRC(i));
264                 IXGBE_READ_REG(hw, IXGBE_QPTC(i));
265                 IXGBE_READ_REG(hw, IXGBE_QBTC(i));
266         }
267
268         return IXGBE_SUCCESS;
269 }
270
271 /**
272  *  ixgbe_read_pba_num - Reads part number from EEPROM
273  *  @hw: pointer to hardware structure
274  *  @pba_num: stores the part number from the EEPROM
275  *
276  *  Reads the part number from the EEPROM.
277  **/
278 s32 ixgbe_read_pba_num_generic(struct ixgbe_hw *hw, u32 *pba_num)
279 {
280         s32 ret_val;
281         u16 data;
282
283         DEBUGFUNC("ixgbe_read_pba_num_generic");
284
285         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
286         if (ret_val) {
287                 DEBUGOUT("NVM Read Error\n");
288                 return ret_val;
289         }
290         *pba_num = (u32)(data << 16);
291
292         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &data);
293         if (ret_val) {
294                 DEBUGOUT("NVM Read Error\n");
295                 return ret_val;
296         }
297         *pba_num |= data;
298
299         return IXGBE_SUCCESS;
300 }
301
302 /**
303  *  ixgbe_get_mac_addr_generic - Generic get MAC address
304  *  @hw: pointer to hardware structure
305  *  @mac_addr: Adapter MAC address
306  *
307  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
308  *  A reset of the adapter must be performed prior to calling this function
309  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
310  **/
311 s32 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
312 {
313         u32 rar_high;
314         u32 rar_low;
315         u16 i;
316
317         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
318         rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
319
320         for (i = 0; i < 4; i++)
321                 mac_addr[i] = (u8)(rar_low >> (i*8));
322
323         for (i = 0; i < 2; i++)
324                 mac_addr[i+4] = (u8)(rar_high >> (i*8));
325
326         return IXGBE_SUCCESS;
327 }
328
329 /**
330  *  ixgbe_get_bus_info_generic - Generic set PCI bus info
331  *  @hw: pointer to hardware structure
332  *
333  *  Sets the PCI bus info (speed, width, type) within the ixgbe_hw structure
334  **/
335 s32 ixgbe_get_bus_info_generic(struct ixgbe_hw *hw)
336 {
337         u16 link_status;
338
339         hw->bus.type = ixgbe_bus_type_pci_express;
340
341         /* Get the negotiated link width and speed from PCI config space */
342         link_status = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_LINK_STATUS);
343
344         switch (link_status & IXGBE_PCI_LINK_WIDTH) {
345         case IXGBE_PCI_LINK_WIDTH_1:
346                 hw->bus.width = ixgbe_bus_width_pcie_x1;
347                 break;
348         case IXGBE_PCI_LINK_WIDTH_2:
349                 hw->bus.width = ixgbe_bus_width_pcie_x2;
350                 break;
351         case IXGBE_PCI_LINK_WIDTH_4:
352                 hw->bus.width = ixgbe_bus_width_pcie_x4;
353                 break;
354         case IXGBE_PCI_LINK_WIDTH_8:
355                 hw->bus.width = ixgbe_bus_width_pcie_x8;
356                 break;
357         default:
358                 hw->bus.width = ixgbe_bus_width_unknown;
359                 break;
360         }
361
362         switch (link_status & IXGBE_PCI_LINK_SPEED) {
363         case IXGBE_PCI_LINK_SPEED_2500:
364                 hw->bus.speed = ixgbe_bus_speed_2500;
365                 break;
366         case IXGBE_PCI_LINK_SPEED_5000:
367                 hw->bus.speed = ixgbe_bus_speed_5000;
368                 break;
369         default:
370                 hw->bus.speed = ixgbe_bus_speed_unknown;
371                 break;
372         }
373
374         return IXGBE_SUCCESS;
375 }
376
377 /**
378  *  ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
379  *  @hw: pointer to hardware structure
380  *
381  *  Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
382  *  disables transmit and receive units. The adapter_stopped flag is used by
383  *  the shared code and drivers to determine if the adapter is in a stopped
384  *  state and should not touch the hardware.
385  **/
386 s32 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
387 {
388         u32 number_of_queues;
389         u32 reg_val;
390         u16 i;
391
392         /*
393          * Set the adapter_stopped flag so other driver functions stop touching
394          * the hardware
395          */
396         hw->adapter_stopped = TRUE;
397
398         /* Disable the receive unit */
399         reg_val = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
400         reg_val &= ~(IXGBE_RXCTRL_RXEN);
401         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, reg_val);
402         IXGBE_WRITE_FLUSH(hw);
403         msec_delay(2);
404
405         /* Clear interrupt mask to stop from interrupts being generated */
406         IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
407
408         /* Clear any pending interrupts */
409         IXGBE_READ_REG(hw, IXGBE_EICR);
410
411         /* Disable the transmit unit.  Each queue must be disabled. */
412         number_of_queues = hw->mac.max_tx_queues;
413         for (i = 0; i < number_of_queues; i++) {
414                 reg_val = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
415                 if (reg_val & IXGBE_TXDCTL_ENABLE) {
416                         reg_val &= ~IXGBE_TXDCTL_ENABLE;
417                         IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), reg_val);
418                 }
419         }
420
421         /*
422          * Prevent the PCI-E bus from from hanging by disabling PCI-E master
423          * access and verify no pending requests
424          */
425         if (ixgbe_disable_pcie_master(hw) != IXGBE_SUCCESS) {
426                 DEBUGOUT("PCI-E Master disable polling has failed.\n");
427         }
428
429         return IXGBE_SUCCESS;
430 }
431
432 /**
433  *  ixgbe_led_on_generic - Turns on the software controllable LEDs.
434  *  @hw: pointer to hardware structure
435  *  @index: led number to turn on
436  **/
437 s32 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
438 {
439         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
440
441         /* To turn on the LED, set mode to ON. */
442         led_reg &= ~IXGBE_LED_MODE_MASK(index);
443         led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
444         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
445         IXGBE_WRITE_FLUSH(hw);
446
447         return IXGBE_SUCCESS;
448 }
449
450 /**
451  *  ixgbe_led_off_generic - Turns off the software controllable LEDs.
452  *  @hw: pointer to hardware structure
453  *  @index: led number to turn off
454  **/
455 s32 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
456 {
457         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
458
459         /* To turn off the LED, set mode to OFF. */
460         led_reg &= ~IXGBE_LED_MODE_MASK(index);
461         led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
462         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
463         IXGBE_WRITE_FLUSH(hw);
464
465         return IXGBE_SUCCESS;
466 }
467
468 /**
469  *  ixgbe_init_eeprom_params_generic - Initialize EEPROM params
470  *  @hw: pointer to hardware structure
471  *
472  *  Initializes the EEPROM parameters ixgbe_eeprom_info within the
473  *  ixgbe_hw struct in order to set up EEPROM access.
474  **/
475 s32 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
476 {
477         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
478         u32 eec;
479         u16 eeprom_size;
480
481         if (eeprom->type == ixgbe_eeprom_uninitialized) {
482                 eeprom->type = ixgbe_eeprom_none;
483
484                 /*
485                  * Check for EEPROM present first.
486                  * If not present leave as none
487                  */
488                 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
489                 if (eec & IXGBE_EEC_PRES) {
490                         eeprom->type = ixgbe_eeprom_spi;
491
492                         /*
493                          * SPI EEPROM is assumed here.  This code would need to
494                          * change if a future EEPROM is not SPI.
495                          */
496                         eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
497                                             IXGBE_EEC_SIZE_SHIFT);
498                         eeprom->word_size = 1 << (eeprom_size +
499                                                   IXGBE_EEPROM_WORD_SIZE_SHIFT);
500                 }
501
502                 if (eec & IXGBE_EEC_ADDR_SIZE)
503                         eeprom->address_bits = 16;
504                 else
505                         eeprom->address_bits = 8;
506                 DEBUGOUT3("Eeprom params: type = %d, size = %d, address bits: "
507                           "%d\n", eeprom->type, eeprom->word_size,
508                           eeprom->address_bits);
509         }
510
511         return IXGBE_SUCCESS;
512 }
513
514 /**
515  *  ixgbe_write_eeprom_generic - Writes 16 bit value to EEPROM
516  *  @hw: pointer to hardware structure
517  *  @offset: offset within the EEPROM to be written to
518  *  @data: 16 bit word to be written to the EEPROM
519  *
520  *  If ixgbe_eeprom_update_checksum is not called after this function, the
521  *  EEPROM will most likely contain an invalid checksum.
522  **/
523 s32 ixgbe_write_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
524 {
525         s32 status;
526         u8 write_opcode = IXGBE_EEPROM_WRITE_OPCODE_SPI;
527
528         hw->eeprom.ops.init_params(hw);
529
530         if (offset >= hw->eeprom.word_size) {
531                 status = IXGBE_ERR_EEPROM;
532                 goto out;
533         }
534
535         /* Prepare the EEPROM for writing  */
536         status = ixgbe_acquire_eeprom(hw);
537
538         if (status == IXGBE_SUCCESS) {
539                 if (ixgbe_ready_eeprom(hw) != IXGBE_SUCCESS) {
540                         ixgbe_release_eeprom(hw);
541                         status = IXGBE_ERR_EEPROM;
542                 }
543         }
544
545         if (status == IXGBE_SUCCESS) {
546                 ixgbe_standby_eeprom(hw);
547
548                 /*  Send the WRITE ENABLE command (8 bit opcode )  */
549                 ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_WREN_OPCODE_SPI,
550                                             IXGBE_EEPROM_OPCODE_BITS);
551
552                 ixgbe_standby_eeprom(hw);
553
554                 /*
555                  * Some SPI eeproms use the 8th address bit embedded in the
556                  * opcode
557                  */
558                 if ((hw->eeprom.address_bits == 8) && (offset >= 128))
559                         write_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
560
561                 /* Send the Write command (8-bit opcode + addr) */
562                 ixgbe_shift_out_eeprom_bits(hw, write_opcode,
563                                             IXGBE_EEPROM_OPCODE_BITS);
564                 ixgbe_shift_out_eeprom_bits(hw, (u16)(offset*2),
565                                             hw->eeprom.address_bits);
566
567                 /* Send the data */
568                 data = (data >> 8) | (data << 8);
569                 ixgbe_shift_out_eeprom_bits(hw, data, 16);
570                 ixgbe_standby_eeprom(hw);
571
572                 msec_delay(10);
573
574                 /* Done with writing - release the EEPROM */
575                 ixgbe_release_eeprom(hw);
576         }
577
578 out:
579         return status;
580 }
581
582 /**
583  *  ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
584  *  @hw: pointer to hardware structure
585  *  @offset: offset within the EEPROM to be read
586  *  @data: read 16 bit value from EEPROM
587  *
588  *  Reads 16 bit value from EEPROM through bit-bang method
589  **/
590 s32 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
591                                        u16 *data)
592 {
593         s32 status;
594         u16 word_in;
595         u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
596
597         hw->eeprom.ops.init_params(hw);
598
599         if (offset >= hw->eeprom.word_size) {
600                 status = IXGBE_ERR_EEPROM;
601                 goto out;
602         }
603
604         /* Prepare the EEPROM for reading  */
605         status = ixgbe_acquire_eeprom(hw);
606
607         if (status == IXGBE_SUCCESS) {
608                 if (ixgbe_ready_eeprom(hw) != IXGBE_SUCCESS) {
609                         ixgbe_release_eeprom(hw);
610                         status = IXGBE_ERR_EEPROM;
611                 }
612         }
613
614         if (status == IXGBE_SUCCESS) {
615                 ixgbe_standby_eeprom(hw);
616
617                 /*
618                  * Some SPI eeproms use the 8th address bit embedded in the
619                  * opcode
620                  */
621                 if ((hw->eeprom.address_bits == 8) && (offset >= 128))
622                         read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
623
624                 /* Send the READ command (opcode + addr) */
625                 ixgbe_shift_out_eeprom_bits(hw, read_opcode,
626                                             IXGBE_EEPROM_OPCODE_BITS);
627                 ixgbe_shift_out_eeprom_bits(hw, (u16)(offset*2),
628                                             hw->eeprom.address_bits);
629
630                 /* Read the data. */
631                 word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
632                 *data = (word_in >> 8) | (word_in << 8);
633
634                 /* End this read operation */
635                 ixgbe_release_eeprom(hw);
636         }
637
638 out:
639         return status;
640 }
641
642 /**
643  *  ixgbe_read_eeprom_generic - Read EEPROM word using EERD
644  *  @hw: pointer to hardware structure
645  *  @offset: offset of  word in the EEPROM to read
646  *  @data: word read from the EEPROM
647  *
648  *  Reads a 16 bit word from the EEPROM using the EERD register.
649  **/
650 s32 ixgbe_read_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
651 {
652         u32 eerd;
653         s32 status;
654
655         hw->eeprom.ops.init_params(hw);
656
657         if (offset >= hw->eeprom.word_size) {
658                 status = IXGBE_ERR_EEPROM;
659                 goto out;
660         }
661
662         eerd = (offset << IXGBE_EEPROM_READ_ADDR_SHIFT) +
663                IXGBE_EEPROM_READ_REG_START;
664
665         IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
666         status = ixgbe_poll_eeprom_eerd_done(hw);
667
668         if (status == IXGBE_SUCCESS)
669                 *data = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
670                          IXGBE_EEPROM_READ_REG_DATA);
671         else
672                 DEBUGOUT("Eeprom read timed out\n");
673
674 out:
675         return status;
676 }
677
678 /**
679  *  ixgbe_poll_eeprom_eerd_done - Poll EERD status
680  *  @hw: pointer to hardware structure
681  *
682  *  Polls the status bit (bit 1) of the EERD to determine when the read is done.
683  **/
684 static s32 ixgbe_poll_eeprom_eerd_done(struct ixgbe_hw *hw)
685 {
686         u32 i;
687         u32 reg;
688         s32 status = IXGBE_ERR_EEPROM;
689
690         for (i = 0; i < IXGBE_EERD_ATTEMPTS; i++) {
691                 reg = IXGBE_READ_REG(hw, IXGBE_EERD);
692                 if (reg & IXGBE_EEPROM_READ_REG_DONE) {
693                         status = IXGBE_SUCCESS;
694                         break;
695                 }
696                 usec_delay(5);
697         }
698         return status;
699 }
700
701 /**
702  *  ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
703  *  @hw: pointer to hardware structure
704  *
705  *  Prepares EEPROM for access using bit-bang method. This function should
706  *  be called before issuing a command to the EEPROM.
707  **/
708 static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
709 {
710         s32 status = IXGBE_SUCCESS;
711         u32 eec;
712         u32 i;
713
714         if (ixgbe_acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) != IXGBE_SUCCESS)
715                 status = IXGBE_ERR_SWFW_SYNC;
716
717         if (status == IXGBE_SUCCESS) {
718                 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
719
720                 /* Request EEPROM Access */
721                 eec |= IXGBE_EEC_REQ;
722                 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
723
724                 for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
725                         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
726                         if (eec & IXGBE_EEC_GNT)
727                                 break;
728                         usec_delay(5);
729                 }
730
731                 /* Release if grant not acquired */
732                 if (!(eec & IXGBE_EEC_GNT)) {
733                         eec &= ~IXGBE_EEC_REQ;
734                         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
735                         DEBUGOUT("Could not acquire EEPROM grant\n");
736
737                         ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
738                         status = IXGBE_ERR_EEPROM;
739                 }
740         }
741
742         /* Setup EEPROM for Read/Write */
743         if (status == IXGBE_SUCCESS) {
744                 /* Clear CS and SK */
745                 eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
746                 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
747                 IXGBE_WRITE_FLUSH(hw);
748                 usec_delay(1);
749         }
750         return status;
751 }
752
753 /**
754  *  ixgbe_get_eeprom_semaphore - Get hardware semaphore
755  *  @hw: pointer to hardware structure
756  *
757  *  Sets the hardware semaphores so EEPROM access can occur for bit-bang method
758  **/
759 static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
760 {
761         s32 status = IXGBE_ERR_EEPROM;
762         u32 timeout;
763         u32 i;
764         u32 swsm;
765
766         /* Set timeout value based on size of EEPROM */
767         timeout = hw->eeprom.word_size + 1;
768
769         /* Get SMBI software semaphore between device drivers first */
770         for (i = 0; i < timeout; i++) {
771                 /*
772                  * If the SMBI bit is 0 when we read it, then the bit will be
773                  * set and we have the semaphore
774                  */
775                 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
776                 if (!(swsm & IXGBE_SWSM_SMBI)) {
777                         status = IXGBE_SUCCESS;
778                         break;
779                 }
780                 msec_delay(1);
781         }
782
783         /* Now get the semaphore between SW/FW through the SWESMBI bit */
784         if (status == IXGBE_SUCCESS) {
785                 for (i = 0; i < timeout; i++) {
786                         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
787
788                         /* Set the SW EEPROM semaphore bit to request access */
789                         swsm |= IXGBE_SWSM_SWESMBI;
790                         IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
791
792                         /*
793                          * If we set the bit successfully then we got the
794                          * semaphore.
795                          */
796                         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
797                         if (swsm & IXGBE_SWSM_SWESMBI)
798                                 break;
799
800                         usec_delay(50);
801                 }
802
803                 /*
804                  * Release semaphores and return error if SW EEPROM semaphore
805                  * was not granted because we don't have access to the EEPROM
806                  */
807                 if (i >= timeout) {
808                         DEBUGOUT("Driver can't access the Eeprom - Semaphore "
809                                  "not granted.\n");
810                         ixgbe_release_eeprom_semaphore(hw);
811                         status = IXGBE_ERR_EEPROM;
812                 }
813         }
814
815         return status;
816 }
817
818 /**
819  *  ixgbe_release_eeprom_semaphore - Release hardware semaphore
820  *  @hw: pointer to hardware structure
821  *
822  *  This function clears hardware semaphore bits.
823  **/
824 static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
825 {
826         u32 swsm;
827
828         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
829
830         /* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
831         swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
832         IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
833         IXGBE_WRITE_FLUSH(hw);
834 }
835
836 /**
837  *  ixgbe_ready_eeprom - Polls for EEPROM ready
838  *  @hw: pointer to hardware structure
839  **/
840 static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
841 {
842         s32 status = IXGBE_SUCCESS;
843         u16 i;
844         u8 spi_stat_reg;
845
846         /*
847          * Read "Status Register" repeatedly until the LSB is cleared.  The
848          * EEPROM will signal that the command has been completed by clearing
849          * bit 0 of the internal status register.  If it's not cleared within
850          * 5 milliseconds, then error out.
851          */
852         for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
853                 ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
854                                             IXGBE_EEPROM_OPCODE_BITS);
855                 spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
856                 if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
857                         break;
858
859                 usec_delay(5);
860                 ixgbe_standby_eeprom(hw);
861         };
862
863         /*
864          * On some parts, SPI write time could vary from 0-20mSec on 3.3V
865          * devices (and only 0-5mSec on 5V devices)
866          */
867         if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
868                 DEBUGOUT("SPI EEPROM Status error\n");
869                 status = IXGBE_ERR_EEPROM;
870         }
871
872         return status;
873 }
874
875 /**
876  *  ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
877  *  @hw: pointer to hardware structure
878  **/
879 static void ixgbe_standby_eeprom(struct ixgbe_hw *hw)
880 {
881         u32 eec;
882
883         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
884
885         /* Toggle CS to flush commands */
886         eec |= IXGBE_EEC_CS;
887         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
888         IXGBE_WRITE_FLUSH(hw);
889         usec_delay(1);
890         eec &= ~IXGBE_EEC_CS;
891         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
892         IXGBE_WRITE_FLUSH(hw);
893         usec_delay(1);
894 }
895
896 /**
897  *  ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
898  *  @hw: pointer to hardware structure
899  *  @data: data to send to the EEPROM
900  *  @count: number of bits to shift out
901  **/
902 static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
903                                         u16 count)
904 {
905         u32 eec;
906         u32 mask;
907         u32 i;
908
909         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
910
911         /*
912          * Mask is used to shift "count" bits of "data" out to the EEPROM
913          * one bit at a time.  Determine the starting bit based on count
914          */
915         mask = 0x01 << (count - 1);
916
917         for (i = 0; i < count; i++) {
918                 /*
919                  * A "1" is shifted out to the EEPROM by setting bit "DI" to a
920                  * "1", and then raising and then lowering the clock (the SK
921                  * bit controls the clock input to the EEPROM).  A "0" is
922                  * shifted out to the EEPROM by setting "DI" to "0" and then
923                  * raising and then lowering the clock.
924                  */
925                 if (data & mask)
926                         eec |= IXGBE_EEC_DI;
927                 else
928                         eec &= ~IXGBE_EEC_DI;
929
930                 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
931                 IXGBE_WRITE_FLUSH(hw);
932
933                 usec_delay(1);
934
935                 ixgbe_raise_eeprom_clk(hw, &eec);
936                 ixgbe_lower_eeprom_clk(hw, &eec);
937
938                 /*
939                  * Shift mask to signify next bit of data to shift in to the
940                  * EEPROM
941                  */
942                 mask = mask >> 1;
943         };
944
945         /* We leave the "DI" bit set to "0" when we leave this routine. */
946         eec &= ~IXGBE_EEC_DI;
947         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
948         IXGBE_WRITE_FLUSH(hw);
949 }
950
951 /**
952  *  ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
953  *  @hw: pointer to hardware structure
954  **/
955 static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
956 {
957         u32 eec;
958         u32 i;
959         u16 data = 0;
960
961         /*
962          * In order to read a register from the EEPROM, we need to shift
963          * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
964          * the clock input to the EEPROM (setting the SK bit), and then reading
965          * the value of the "DO" bit.  During this "shifting in" process the
966          * "DI" bit should always be clear.
967          */
968         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
969
970         eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
971
972         for (i = 0; i < count; i++) {
973                 data = data << 1;
974                 ixgbe_raise_eeprom_clk(hw, &eec);
975
976                 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
977
978                 eec &= ~(IXGBE_EEC_DI);
979                 if (eec & IXGBE_EEC_DO)
980                         data |= 1;
981
982                 ixgbe_lower_eeprom_clk(hw, &eec);
983         }
984
985         return data;
986 }
987
988 /**
989  *  ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
990  *  @hw: pointer to hardware structure
991  *  @eec: EEC register's current value
992  **/
993 static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
994 {
995         /*
996          * Raise the clock input to the EEPROM
997          * (setting the SK bit), then delay
998          */
999         *eec = *eec | IXGBE_EEC_SK;
1000         IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
1001         IXGBE_WRITE_FLUSH(hw);
1002         usec_delay(1);
1003 }
1004
1005 /**
1006  *  ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
1007  *  @hw: pointer to hardware structure
1008  *  @eecd: EECD's current value
1009  **/
1010 static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
1011 {
1012         /*
1013          * Lower the clock input to the EEPROM (clearing the SK bit), then
1014          * delay
1015          */
1016         *eec = *eec & ~IXGBE_EEC_SK;
1017         IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
1018         IXGBE_WRITE_FLUSH(hw);
1019         usec_delay(1);
1020 }
1021
1022 /**
1023  *  ixgbe_release_eeprom - Release EEPROM, release semaphores
1024  *  @hw: pointer to hardware structure
1025  **/
1026 static void ixgbe_release_eeprom(struct ixgbe_hw *hw)
1027 {
1028         u32 eec;
1029
1030         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1031
1032         eec |= IXGBE_EEC_CS;  /* Pull CS high */
1033         eec &= ~IXGBE_EEC_SK; /* Lower SCK */
1034
1035         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1036         IXGBE_WRITE_FLUSH(hw);
1037
1038         usec_delay(1);
1039
1040         /* Stop requesting EEPROM access */
1041         eec &= ~IXGBE_EEC_REQ;
1042         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1043
1044         ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
1045 }
1046
1047 /**
1048  *  ixgbe_calc_eeprom_checksum - Calculates and returns the checksum
1049  *  @hw: pointer to hardware structure
1050  **/
1051 static u16 ixgbe_calc_eeprom_checksum(struct ixgbe_hw *hw)
1052 {
1053         u16 i;
1054         u16 j;
1055         u16 checksum = 0;
1056         u16 length = 0;
1057         u16 pointer = 0;
1058         u16 word = 0;
1059
1060         /* Include 0x0-0x3F in the checksum */
1061         for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
1062                 if (hw->eeprom.ops.read(hw, i, &word) != IXGBE_SUCCESS) {
1063                         DEBUGOUT("EEPROM read failed\n");
1064                         break;
1065                 }
1066                 checksum += word;
1067         }
1068
1069         /* Include all data from pointers except for the fw pointer */
1070         for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
1071                 hw->eeprom.ops.read(hw, i, &pointer);
1072
1073                 /* Make sure the pointer seems valid */
1074                 if (pointer != 0xFFFF && pointer != 0) {
1075                         hw->eeprom.ops.read(hw, pointer, &length);
1076
1077                         if (length != 0xFFFF && length != 0) {
1078                                 for (j = pointer+1; j <= pointer+length; j++) {
1079                                         hw->eeprom.ops.read(hw, j, &word);
1080                                         checksum += word;
1081                                 }
1082                         }
1083                 }
1084         }
1085
1086         checksum = (u16)IXGBE_EEPROM_SUM - checksum;
1087
1088         return checksum;
1089 }
1090
1091 /**
1092  *  ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
1093  *  @hw: pointer to hardware structure
1094  *  @checksum_val: calculated checksum
1095  *
1096  *  Performs checksum calculation and validates the EEPROM checksum.  If the
1097  *  caller does not need checksum_val, the value can be NULL.
1098  **/
1099 s32 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
1100                                            u16 *checksum_val)
1101 {
1102         s32 status;
1103         u16 checksum;
1104         u16 read_checksum = 0;
1105
1106         /*
1107          * Read the first word from the EEPROM. If this times out or fails, do
1108          * not continue or we could be in for a very long wait while every
1109          * EEPROM read fails
1110          */
1111         status = hw->eeprom.ops.read(hw, 0, &checksum);
1112
1113         if (status == IXGBE_SUCCESS) {
1114                 checksum = ixgbe_calc_eeprom_checksum(hw);
1115
1116                 hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
1117
1118                 /*
1119                  * Verify read checksum from EEPROM is the same as
1120                  * calculated checksum
1121                  */
1122                 if (read_checksum != checksum)
1123                         status = IXGBE_ERR_EEPROM_CHECKSUM;
1124
1125                 /* If the user cares, return the calculated checksum */
1126                 if (checksum_val)
1127                         *checksum_val = checksum;
1128         } else {
1129                 DEBUGOUT("EEPROM read failed\n");
1130         }
1131
1132         return status;
1133 }
1134
1135 /**
1136  *  ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
1137  *  @hw: pointer to hardware structure
1138  **/
1139 s32 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
1140 {
1141         s32 status;
1142         u16 checksum;
1143
1144         /*
1145          * Read the first word from the EEPROM. If this times out or fails, do
1146          * not continue or we could be in for a very long wait while every
1147          * EEPROM read fails
1148          */
1149         status = hw->eeprom.ops.read(hw, 0, &checksum);
1150
1151         if (status == IXGBE_SUCCESS) {
1152                 checksum = ixgbe_calc_eeprom_checksum(hw);
1153                 status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM,
1154                                             checksum);
1155         } else {
1156                 DEBUGOUT("EEPROM read failed\n");
1157         }
1158
1159         return status;
1160 }
1161
1162 /**
1163  *  ixgbe_validate_mac_addr - Validate MAC address
1164  *  @mac_addr: pointer to MAC address.
1165  *
1166  *  Tests a MAC address to ensure it is a valid Individual Address
1167  **/
1168 s32 ixgbe_validate_mac_addr(u8 *mac_addr)
1169 {
1170         s32 status = IXGBE_SUCCESS;
1171
1172         /* Make sure it is not a multicast address */
1173         if (IXGBE_IS_MULTICAST(mac_addr)) {
1174                 DEBUGOUT("MAC address is multicast\n");
1175                 status = IXGBE_ERR_INVALID_MAC_ADDR;
1176         /* Not a broadcast address */
1177         } else if (IXGBE_IS_BROADCAST(mac_addr)) {
1178                 DEBUGOUT("MAC address is broadcast\n");
1179                 status = IXGBE_ERR_INVALID_MAC_ADDR;
1180         /* Reject the zero address */
1181         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
1182                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
1183                 DEBUGOUT("MAC address is all zeros\n");
1184                 status = IXGBE_ERR_INVALID_MAC_ADDR;
1185         }
1186         return status;
1187 }
1188
1189 /**
1190  *  ixgbe_set_rar_generic - Set Rx address register
1191  *  @hw: pointer to hardware structure
1192  *  @index: Receive address register to write
1193  *  @addr: Address to put into receive address register
1194  *  @vmdq: VMDq "set" or "pool" index
1195  *  @enable_addr: set flag that address is active
1196  *
1197  *  Puts an ethernet address into a receive address register.
1198  **/
1199 s32 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
1200                           u32 enable_addr)
1201 {
1202         u32 rar_low, rar_high;
1203         u32 rar_entries = hw->mac.num_rar_entries;
1204
1205         /* setup VMDq pool selection before this RAR gets enabled */
1206         hw->mac.ops.set_vmdq(hw, index, vmdq);
1207
1208         /* Make sure we are using a valid rar index range */
1209         if (index < rar_entries) {
1210                 /*
1211                  * HW expects these in little endian so we reverse the byte
1212                  * order from network order (big endian) to little endian
1213                  */
1214                 rar_low = ((u32)addr[0] |
1215                            ((u32)addr[1] << 8) |
1216                            ((u32)addr[2] << 16) |
1217                            ((u32)addr[3] << 24));
1218                 /*
1219                  * Some parts put the VMDq setting in the extra RAH bits,
1220                  * so save everything except the lower 16 bits that hold part
1221                  * of the address and the address valid bit.
1222                  */
1223                 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1224                 rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
1225                 rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
1226
1227                 if (enable_addr != 0)
1228                         rar_high |= IXGBE_RAH_AV;
1229
1230                 IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
1231                 IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1232         } else {
1233                 DEBUGOUT("Current RAR index is out of range.");
1234         }
1235
1236         return IXGBE_SUCCESS;
1237 }
1238
1239 /**
1240  *  ixgbe_enable_rar - Enable Rx address register
1241  *  @hw: pointer to hardware structure
1242  *  @index: index into the RAR table
1243  *
1244  *  Enables the select receive address register.
1245  **/
1246 static void ixgbe_enable_rar(struct ixgbe_hw *hw, u32 index)
1247 {
1248         u32 rar_high;
1249
1250         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1251         rar_high |= IXGBE_RAH_AV;
1252         IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1253 }
1254
1255 /**
1256  *  ixgbe_disable_rar - Disable Rx address register
1257  *  @hw: pointer to hardware structure
1258  *  @index: index into the RAR table
1259  *
1260  *  Disables the select receive address register.
1261  **/
1262 static void ixgbe_disable_rar(struct ixgbe_hw *hw, u32 index)
1263 {
1264         u32 rar_high;
1265
1266         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1267         rar_high &= (~IXGBE_RAH_AV);
1268         IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1269 }
1270
1271 /**
1272  *  ixgbe_init_rx_addrs_generic - Initializes receive address filters.
1273  *  @hw: pointer to hardware structure
1274  *
1275  *  Places the MAC address in receive address register 0 and clears the rest
1276  *  of the receive address registers. Clears the multicast table. Assumes
1277  *  the receiver is in reset when the routine is called.
1278  **/
1279 s32 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
1280 {
1281         u32 i;
1282         u32 rar_entries = hw->mac.num_rar_entries;
1283
1284         /*
1285          * If the current mac address is valid, assume it is a software override
1286          * to the permanent address.
1287          * Otherwise, use the permanent address from the eeprom.
1288          */
1289         if (ixgbe_validate_mac_addr(hw->mac.addr) ==
1290             IXGBE_ERR_INVALID_MAC_ADDR) {
1291                 /* Get the MAC address from the RAR0 for later reference */
1292                 hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
1293
1294                 DEBUGOUT3(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
1295                           hw->mac.addr[0], hw->mac.addr[1],
1296                           hw->mac.addr[2]);
1297                 DEBUGOUT3("%.2X %.2X %.2X\n", hw->mac.addr[3],
1298                           hw->mac.addr[4], hw->mac.addr[5]);
1299         } else {
1300                 /* Setup the receive address. */
1301                 DEBUGOUT("Overriding MAC Address in RAR[0]\n");
1302                 DEBUGOUT3(" New MAC Addr =%.2X %.2X %.2X ",
1303                           hw->mac.addr[0], hw->mac.addr[1],
1304                           hw->mac.addr[2]);
1305                 DEBUGOUT3("%.2X %.2X %.2X\n", hw->mac.addr[3],
1306                           hw->mac.addr[4], hw->mac.addr[5]);
1307
1308                 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
1309         }
1310         hw->addr_ctrl.overflow_promisc = 0;
1311
1312         hw->addr_ctrl.rar_used_count = 1;
1313
1314         /* Zero out the other receive addresses. */
1315         DEBUGOUT1("Clearing RAR[1-%d]\n", rar_entries - 1);
1316         for (i = 1; i < rar_entries; i++) {
1317                 IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
1318                 IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
1319         }
1320
1321         /* Clear the MTA */
1322         hw->addr_ctrl.mc_addr_in_rar_count = 0;
1323         hw->addr_ctrl.mta_in_use = 0;
1324         IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
1325
1326         DEBUGOUT(" Clearing MTA\n");
1327         for (i = 0; i < hw->mac.mcft_size; i++)
1328                 IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
1329
1330         return IXGBE_SUCCESS;
1331 }
1332
1333 /**
1334  *  ixgbe_add_uc_addr - Adds a secondary unicast address.
1335  *  @hw: pointer to hardware structure
1336  *  @addr: new address
1337  *
1338  *  Adds it to unused receive address register or goes into promiscuous mode.
1339  **/
1340 void ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq)
1341 {
1342         u32 rar_entries = hw->mac.num_rar_entries;
1343         u32 rar;
1344
1345         DEBUGOUT6(" UC Addr = %.2X %.2X %.2X %.2X %.2X %.2X\n",
1346                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
1347
1348         /*
1349          * Place this address in the RAR if there is room,
1350          * else put the controller into promiscuous mode
1351          */
1352         if (hw->addr_ctrl.rar_used_count < rar_entries) {
1353                 rar = hw->addr_ctrl.rar_used_count -
1354                       hw->addr_ctrl.mc_addr_in_rar_count;
1355                 hw->mac.ops.set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
1356                 DEBUGOUT1("Added a secondary address to RAR[%d]\n", rar);
1357                 hw->addr_ctrl.rar_used_count++;
1358         } else {
1359                 hw->addr_ctrl.overflow_promisc++;
1360         }
1361
1362         DEBUGOUT("ixgbe_add_uc_addr Complete\n");
1363 }
1364
1365 /**
1366  *  ixgbe_update_uc_addr_list_generic - Updates MAC list of secondary addresses
1367  *  @hw: pointer to hardware structure
1368  *  @addr_list: the list of new addresses
1369  *  @addr_count: number of addresses
1370  *  @next: iterator function to walk the address list
1371  *
1372  *  The given list replaces any existing list.  Clears the secondary addrs from
1373  *  receive address registers.  Uses unused receive address registers for the
1374  *  first secondary addresses, and falls back to promiscuous mode as needed.
1375  *
1376  *  Drivers using secondary unicast addresses must set user_set_promisc when
1377  *  manually putting the device into promiscuous mode.
1378  **/
1379 s32 ixgbe_update_uc_addr_list_generic(struct ixgbe_hw *hw, u8 *addr_list,
1380                                       u32 addr_count, ixgbe_mc_addr_itr next)
1381 {
1382         u8 *addr;
1383         u32 i;
1384         u32 old_promisc_setting = hw->addr_ctrl.overflow_promisc;
1385         u32 uc_addr_in_use;
1386         u32 fctrl;
1387         u32 vmdq;
1388
1389         /*
1390          * Clear accounting of old secondary address list,
1391          * don't count RAR[0]
1392          */
1393         uc_addr_in_use = hw->addr_ctrl.rar_used_count -
1394                          hw->addr_ctrl.mc_addr_in_rar_count - 1;
1395         hw->addr_ctrl.rar_used_count -= uc_addr_in_use;
1396         hw->addr_ctrl.overflow_promisc = 0;
1397
1398         /* Zero out the other receive addresses */
1399         DEBUGOUT1("Clearing RAR[1-%d]\n", uc_addr_in_use);
1400         for (i = 1; i <= uc_addr_in_use; i++) {
1401                 IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
1402                 IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
1403         }
1404
1405         /* Add the new addresses */
1406         for (i = 0; i < addr_count; i++) {
1407                 DEBUGOUT(" Adding the secondary addresses:\n");
1408                 addr = next(hw, &addr_list, &vmdq);
1409                 ixgbe_add_uc_addr(hw, addr, vmdq);
1410         }
1411
1412         if (hw->addr_ctrl.overflow_promisc) {
1413                 /* enable promisc if not already in overflow or set by user */
1414                 if (!old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
1415                         DEBUGOUT( " Entering address overflow promisc mode\n");
1416                         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1417                         fctrl |= IXGBE_FCTRL_UPE;
1418                         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1419                 }
1420         } else {
1421                 /* only disable if set by overflow, not by user */
1422                 if (old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
1423                         DEBUGOUT(" Leaving address overflow promisc mode\n");
1424                         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1425                         fctrl &= ~IXGBE_FCTRL_UPE;
1426                         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1427                 }
1428         }
1429
1430         DEBUGOUT("ixgbe_update_uc_addr_list_generic Complete\n");
1431         return IXGBE_SUCCESS;
1432 }
1433
1434 /**
1435  *  ixgbe_mta_vector - Determines bit-vector in multicast table to set
1436  *  @hw: pointer to hardware structure
1437  *  @mc_addr: the multicast address
1438  *
1439  *  Extracts the 12 bits, from a multicast address, to determine which
1440  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
1441  *  incoming rx multicast addresses, to determine the bit-vector to check in
1442  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
1443  *  by the MO field of the MCSTCTRL. The MO field is set during initialization
1444  *  to mc_filter_type.
1445  **/
1446 static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
1447 {
1448         u32 vector = 0;
1449
1450         switch (hw->mac.mc_filter_type) {
1451         case 0:   /* use bits [47:36] of the address */
1452                 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
1453                 break;
1454         case 1:   /* use bits [46:35] of the address */
1455                 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
1456                 break;
1457         case 2:   /* use bits [45:34] of the address */
1458                 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
1459                 break;
1460         case 3:   /* use bits [43:32] of the address */
1461                 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
1462                 break;
1463         default:  /* Invalid mc_filter_type */
1464                 DEBUGOUT("MC filter type param set incorrectly\n");
1465                 ASSERT(0);
1466                 break;
1467         }
1468
1469         /* vector can only be 12-bits or boundary will be exceeded */
1470         vector &= 0xFFF;
1471         return vector;
1472 }
1473
1474 /**
1475  *  ixgbe_set_mta - Set bit-vector in multicast table
1476  *  @hw: pointer to hardware structure
1477  *  @hash_value: Multicast address hash value
1478  *
1479  *  Sets the bit-vector in the multicast table.
1480  **/
1481 void ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
1482 {
1483         u32 vector;
1484         u32 vector_bit;
1485         u32 vector_reg;
1486         u32 mta_reg;
1487
1488         hw->addr_ctrl.mta_in_use++;
1489
1490         vector = ixgbe_mta_vector(hw, mc_addr);
1491         DEBUGOUT1(" bit-vector = 0x%03X\n", vector);
1492
1493         /*
1494          * The MTA is a register array of 128 32-bit registers. It is treated
1495          * like an array of 4096 bits.  We want to set bit
1496          * BitArray[vector_value]. So we figure out what register the bit is
1497          * in, read it, OR in the new bit, then write back the new value.  The
1498          * register is determined by the upper 7 bits of the vector value and
1499          * the bit within that register are determined by the lower 5 bits of
1500          * the value.
1501          */
1502         vector_reg = (vector >> 5) & 0x7F;
1503         vector_bit = vector & 0x1F;
1504         mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
1505         mta_reg |= (1 << vector_bit);
1506         IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
1507 }
1508
1509 /**
1510  *  ixgbe_add_mc_addr - Adds a multicast address.
1511  *  @hw: pointer to hardware structure
1512  *  @mc_addr: new multicast address
1513  *
1514  *  Adds it to unused receive address register or to the multicast table.
1515  **/
1516 void ixgbe_add_mc_addr(struct ixgbe_hw *hw, u8 *mc_addr)
1517 {
1518         u32 rar_entries = hw->mac.num_rar_entries;
1519         u32 rar;
1520
1521         DEBUGOUT6(" MC Addr =%.2X %.2X %.2X %.2X %.2X %.2X\n",
1522                   mc_addr[0], mc_addr[1], mc_addr[2],
1523                   mc_addr[3], mc_addr[4], mc_addr[5]);
1524
1525         /*
1526          * Place this multicast address in the RAR if there is room,
1527          * else put it in the MTA
1528          */
1529         if (hw->addr_ctrl.rar_used_count < rar_entries) {
1530                 /* use RAR from the end up for multicast */
1531                 rar = rar_entries - hw->addr_ctrl.mc_addr_in_rar_count - 1;
1532                 hw->mac.ops.set_rar(hw, rar, mc_addr, 0, IXGBE_RAH_AV);
1533                 DEBUGOUT1("Added a multicast address to RAR[%d]\n", rar);
1534                 hw->addr_ctrl.rar_used_count++;
1535                 hw->addr_ctrl.mc_addr_in_rar_count++;
1536         } else {
1537                 ixgbe_set_mta(hw, mc_addr);
1538         }
1539
1540         DEBUGOUT("ixgbe_add_mc_addr Complete\n");
1541 }
1542
1543 /**
1544  *  ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
1545  *  @hw: pointer to hardware structure
1546  *  @mc_addr_list: the list of new multicast addresses
1547  *  @mc_addr_count: number of addresses
1548  *  @next: iterator function to walk the multicast address list
1549  *
1550  *  The given list replaces any existing list. Clears the MC addrs from receive
1551  *  address registers and the multicast table. Uses unused receive address
1552  *  registers for the first multicast addresses, and hashes the rest into the
1553  *  multicast table.
1554  **/
1555 s32 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw, u8 *mc_addr_list,
1556                                       u32 mc_addr_count, ixgbe_mc_addr_itr next)
1557 {
1558         u32 i;
1559         u32 rar_entries = hw->mac.num_rar_entries;
1560         u32 vmdq;
1561
1562         /*
1563          * Set the new number of MC addresses that we are being requested to
1564          * use.
1565          */
1566         hw->addr_ctrl.num_mc_addrs = mc_addr_count;
1567         hw->addr_ctrl.rar_used_count -= hw->addr_ctrl.mc_addr_in_rar_count;
1568         hw->addr_ctrl.mc_addr_in_rar_count = 0;
1569         hw->addr_ctrl.mta_in_use = 0;
1570
1571         /* Zero out the other receive addresses. */
1572         DEBUGOUT2("Clearing RAR[%d-%d]\n", hw->addr_ctrl.rar_used_count,
1573                   rar_entries - 1);
1574         for (i = hw->addr_ctrl.rar_used_count; i < rar_entries; i++) {
1575                 IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
1576                 IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
1577         }
1578
1579         /* Clear the MTA */
1580         DEBUGOUT(" Clearing MTA\n");
1581         for (i = 0; i < hw->mac.mcft_size; i++)
1582                 IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
1583
1584         /* Add the new addresses */
1585         for (i = 0; i < mc_addr_count; i++) {
1586                 DEBUGOUT(" Adding the multicast addresses:\n");
1587                 ixgbe_add_mc_addr(hw, next(hw, &mc_addr_list, &vmdq));
1588         }
1589
1590         /* Enable mta */
1591         if (hw->addr_ctrl.mta_in_use > 0)
1592                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
1593                                 IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
1594
1595         DEBUGOUT("ixgbe_update_mc_addr_list_generic Complete\n");
1596         return IXGBE_SUCCESS;
1597 }
1598
1599 /**
1600  *  ixgbe_enable_mc_generic - Enable multicast address in RAR
1601  *  @hw: pointer to hardware structure
1602  *
1603  *  Enables multicast address in RAR and the use of the multicast hash table.
1604  **/
1605 s32 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
1606 {
1607         u32 i;
1608         u32 rar_entries = hw->mac.num_rar_entries;
1609         struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
1610
1611         if (a->mc_addr_in_rar_count > 0)
1612                 for (i = (rar_entries - a->mc_addr_in_rar_count);
1613                      i < rar_entries; i++)
1614                         ixgbe_enable_rar(hw, i);
1615
1616         if (a->mta_in_use > 0)
1617                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
1618                                 hw->mac.mc_filter_type);
1619
1620         return IXGBE_SUCCESS;
1621 }
1622
1623 /**
1624  *  ixgbe_disable_mc_generic - Disable multicast address in RAR
1625  *  @hw: pointer to hardware structure
1626  *
1627  *  Disables multicast address in RAR and the use of the multicast hash table.
1628  **/
1629 s32 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
1630 {
1631         u32 i;
1632         u32 rar_entries = hw->mac.num_rar_entries;
1633         struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
1634
1635         if (a->mc_addr_in_rar_count > 0)
1636                 for (i = (rar_entries - a->mc_addr_in_rar_count);
1637                      i < rar_entries; i++)
1638                         ixgbe_disable_rar(hw, i);
1639
1640         if (a->mta_in_use > 0)
1641                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
1642
1643         return IXGBE_SUCCESS;
1644 }
1645
1646 /**
1647  *  ixgbe_clear_vfta_generic - Clear VLAN filter table
1648  *  @hw: pointer to hardware structure
1649  *
1650  *  Clears the VLAN filer table, and the VMDq index associated with the filter
1651  **/
1652 s32 ixgbe_clear_vfta_generic(struct ixgbe_hw *hw)
1653 {
1654         u32 offset;
1655         u32 vlanbyte;
1656
1657         for (offset = 0; offset < hw->mac.vft_size; offset++)
1658                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(offset), 0);
1659
1660         for (vlanbyte = 0; vlanbyte < 4; vlanbyte++)
1661                 for (offset = 0; offset < hw->mac.vft_size; offset++)
1662                         IXGBE_WRITE_REG(hw, IXGBE_VFTAVIND(vlanbyte, offset),
1663                                         0);
1664
1665         return IXGBE_SUCCESS;
1666 }
1667
1668 /**
1669  *  ixgbe_set_vfta_generic - Set VLAN filter table
1670  *  @hw: pointer to hardware structure
1671  *  @vlan: VLAN id to write to VLAN filter
1672  *  @vind: VMDq output index that maps queue to VLAN id in VFTA
1673  *  @vlan_on: boolean flag to turn on/off VLAN in VFTA
1674  *
1675  *  Turn on/off specified VLAN in the VLAN filter table.
1676  **/
1677 s32 ixgbe_set_vfta_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
1678                            bool vlan_on)
1679 {
1680         u32 VftaIndex;
1681         u32 BitOffset;
1682         u32 VftaReg;
1683         u32 VftaByte;
1684
1685         /* Determine 32-bit word position in array */
1686         VftaIndex = (vlan >> 5) & 0x7F;   /* upper seven bits */
1687
1688         /* Determine the location of the (VMD) queue index */
1689         VftaByte =  ((vlan >> 3) & 0x03); /* bits (4:3) indicating byte array */
1690         BitOffset = (vlan & 0x7) << 2;    /* lower 3 bits indicate nibble */
1691
1692         /* Set the nibble for VMD queue index */
1693         VftaReg = IXGBE_READ_REG(hw, IXGBE_VFTAVIND(VftaByte, VftaIndex));
1694         VftaReg &= (~(0x0F << BitOffset));
1695         VftaReg |= (vind << BitOffset);
1696         IXGBE_WRITE_REG(hw, IXGBE_VFTAVIND(VftaByte, VftaIndex), VftaReg);
1697
1698         /* Determine the location of the bit for this VLAN id */
1699         BitOffset = vlan & 0x1F;   /* lower five bits */
1700
1701         VftaReg = IXGBE_READ_REG(hw, IXGBE_VFTA(VftaIndex));
1702         if (vlan_on)
1703                 /* Turn on this VLAN id */
1704                 VftaReg |= (1 << BitOffset);
1705         else
1706                 /* Turn off this VLAN id */
1707                 VftaReg &= ~(1 << BitOffset);
1708         IXGBE_WRITE_REG(hw, IXGBE_VFTA(VftaIndex), VftaReg);
1709
1710         return IXGBE_SUCCESS;
1711 }
1712
1713 /**
1714  *  ixgbe_disable_pcie_master - Disable PCI-express master access
1715  *  @hw: pointer to hardware structure
1716  *
1717  *  Disables PCI-Express master access and verifies there are no pending
1718  *  requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
1719  *  bit hasn't caused the master requests to be disabled, else IXGBE_SUCCESS
1720  *  is returned signifying master requests disabled.
1721  **/
1722 s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
1723 {
1724         u32 ctrl;
1725         s32 i;
1726         s32 status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
1727
1728         ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
1729         ctrl |= IXGBE_CTRL_GIO_DIS;
1730         IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
1731
1732         for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
1733                 if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO)) {
1734                         status = IXGBE_SUCCESS;
1735                         break;
1736                 }
1737                 usec_delay(100);
1738         }
1739
1740         return status;
1741 }
1742
1743
1744 /**
1745  *  ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
1746  *  @hw: pointer to hardware structure
1747  *  @mask: Mask to specify which semaphore to acquire
1748  *
1749  *  Acquires the SWFW semaphore thought the GSSR register for the specified
1750  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1751  **/
1752 s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u16 mask)
1753 {
1754         u32 gssr;
1755         u32 swmask = mask;
1756         u32 fwmask = mask << 5;
1757         s32 timeout = 200;
1758
1759         while (timeout) {
1760                 if (ixgbe_get_eeprom_semaphore(hw))
1761                         return -IXGBE_ERR_SWFW_SYNC;
1762
1763                 gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
1764                 if (!(gssr & (fwmask | swmask)))
1765                         break;
1766
1767                 /*
1768                  * Firmware currently using resource (fwmask) or other software
1769                  * thread currently using resource (swmask)
1770                  */
1771                 ixgbe_release_eeprom_semaphore(hw);
1772                 msec_delay(5);
1773                 timeout--;
1774         }
1775
1776         if (!timeout) {
1777                 DEBUGOUT("Driver can't access resource, GSSR timeout.\n");
1778                 return -IXGBE_ERR_SWFW_SYNC;
1779         }
1780
1781         gssr |= swmask;
1782         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
1783
1784         ixgbe_release_eeprom_semaphore(hw);
1785         return IXGBE_SUCCESS;
1786 }
1787
1788 /**
1789  *  ixgbe_release_swfw_sync - Release SWFW semaphore
1790  *  @hw: pointer to hardware structure
1791  *  @mask: Mask to specify which semaphore to release
1792  *
1793  *  Releases the SWFW semaphore thought the GSSR register for the specified
1794  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1795  **/
1796 void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u16 mask)
1797 {
1798         u32 gssr;
1799         u32 swmask = mask;
1800
1801         ixgbe_get_eeprom_semaphore(hw);
1802
1803         gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
1804         gssr &= ~swmask;
1805         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
1806
1807         ixgbe_release_eeprom_semaphore(hw);
1808 }
1809
1810 /**
1811  *  ixgbe_read_analog_reg8_generic - Reads 8 bit Atlas analog register
1812  *  @hw: pointer to hardware structure
1813  *  @reg: analog register to read
1814  *  @val: read value
1815  *
1816  *  Performs read operation to Atlas analog register specified.
1817  **/
1818 s32 ixgbe_read_analog_reg8_generic(struct ixgbe_hw *hw, u32 reg, u8 *val)
1819 {
1820         u32  atlas_ctl;
1821
1822         IXGBE_WRITE_REG(hw, IXGBE_ATLASCTL, IXGBE_ATLASCTL_WRITE_CMD | (reg << 8));
1823         IXGBE_WRITE_FLUSH(hw);
1824         usec_delay(10);
1825         atlas_ctl = IXGBE_READ_REG(hw, IXGBE_ATLASCTL);
1826         *val = (u8)atlas_ctl;
1827
1828         return IXGBE_SUCCESS;
1829 }
1830
1831 /**
1832  *  ixgbe_write_analog_reg8_generic - Writes 8 bit Atlas analog register
1833  *  @hw: pointer to hardware structure
1834  *  @reg: atlas register to write
1835  *  @val: value to write
1836  *
1837  *  Performs write operation to Atlas analog register specified.
1838  **/
1839 s32 ixgbe_write_analog_reg8_generic(struct ixgbe_hw *hw, u32 reg, u8 val)
1840 {
1841         u32  atlas_ctl;
1842
1843         atlas_ctl = (reg << 8) | val;
1844         IXGBE_WRITE_REG(hw, IXGBE_ATLASCTL, atlas_ctl);
1845         IXGBE_WRITE_FLUSH(hw);
1846         usec_delay(10);
1847
1848         return IXGBE_SUCCESS;
1849 }
1850