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