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