]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/ixgbe/ixgbe_common.c
ixgbe: make silicon specific functions generic
[net-next-2.6.git] / drivers / net / ixgbe / ixgbe_common.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2010 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/netdevice.h>
32
33 #include "ixgbe.h"
34 #include "ixgbe_common.h"
35 #include "ixgbe_phy.h"
36
37 static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw);
38 static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw);
39 static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw);
40 static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw);
41 static void ixgbe_standby_eeprom(struct ixgbe_hw *hw);
42 static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
43                                         u16 count);
44 static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count);
45 static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
46 static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
47 static void ixgbe_release_eeprom(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_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq);
53 static s32 ixgbe_setup_fc(struct ixgbe_hw *hw, s32 packetbuf_num);
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         /* Clear the VLAN filter table */
75         hw->mac.ops.clear_vfta(hw);
76
77         /* Clear statistics registers */
78         hw->mac.ops.clear_hw_cntrs(hw);
79
80         /* Set No Snoop Disable */
81         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
82         ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
83         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
84         IXGBE_WRITE_FLUSH(hw);
85
86         /* Setup flow control */
87         ixgbe_setup_fc(hw, 0);
88
89         /* Clear adapter stopped flag */
90         hw->adapter_stopped = false;
91
92         return 0;
93 }
94
95 /**
96  *  ixgbe_init_hw_generic - Generic hardware initialization
97  *  @hw: pointer to hardware structure
98  *
99  *  Initialize the hardware by resetting the hardware, filling the bus info
100  *  structure and media type, clears all on chip counters, initializes receive
101  *  address registers, multicast table, VLAN filter table, calls routine to set
102  *  up link and flow control settings, and leaves transmit and receive units
103  *  disabled and uninitialized
104  **/
105 s32 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
106 {
107         s32 status;
108
109         /* Reset the hardware */
110         status = hw->mac.ops.reset_hw(hw);
111
112         if (status == 0) {
113                 /* Start the HW */
114                 status = hw->mac.ops.start_hw(hw);
115         }
116
117         return status;
118 }
119
120 /**
121  *  ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
122  *  @hw: pointer to hardware structure
123  *
124  *  Clears all hardware statistics counters by reading them from the hardware
125  *  Statistics counters are clear on read.
126  **/
127 s32 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
128 {
129         u16 i = 0;
130
131         IXGBE_READ_REG(hw, IXGBE_CRCERRS);
132         IXGBE_READ_REG(hw, IXGBE_ILLERRC);
133         IXGBE_READ_REG(hw, IXGBE_ERRBC);
134         IXGBE_READ_REG(hw, IXGBE_MSPDC);
135         for (i = 0; i < 8; i++)
136                 IXGBE_READ_REG(hw, IXGBE_MPC(i));
137
138         IXGBE_READ_REG(hw, IXGBE_MLFC);
139         IXGBE_READ_REG(hw, IXGBE_MRFC);
140         IXGBE_READ_REG(hw, IXGBE_RLEC);
141         IXGBE_READ_REG(hw, IXGBE_LXONTXC);
142         IXGBE_READ_REG(hw, IXGBE_LXONRXC);
143         IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
144         IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
145
146         for (i = 0; i < 8; i++) {
147                 IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
148                 IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
149                 IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
150                 IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
151         }
152
153         IXGBE_READ_REG(hw, IXGBE_PRC64);
154         IXGBE_READ_REG(hw, IXGBE_PRC127);
155         IXGBE_READ_REG(hw, IXGBE_PRC255);
156         IXGBE_READ_REG(hw, IXGBE_PRC511);
157         IXGBE_READ_REG(hw, IXGBE_PRC1023);
158         IXGBE_READ_REG(hw, IXGBE_PRC1522);
159         IXGBE_READ_REG(hw, IXGBE_GPRC);
160         IXGBE_READ_REG(hw, IXGBE_BPRC);
161         IXGBE_READ_REG(hw, IXGBE_MPRC);
162         IXGBE_READ_REG(hw, IXGBE_GPTC);
163         IXGBE_READ_REG(hw, IXGBE_GORCL);
164         IXGBE_READ_REG(hw, IXGBE_GORCH);
165         IXGBE_READ_REG(hw, IXGBE_GOTCL);
166         IXGBE_READ_REG(hw, IXGBE_GOTCH);
167         for (i = 0; i < 8; i++)
168                 IXGBE_READ_REG(hw, IXGBE_RNBC(i));
169         IXGBE_READ_REG(hw, IXGBE_RUC);
170         IXGBE_READ_REG(hw, IXGBE_RFC);
171         IXGBE_READ_REG(hw, IXGBE_ROC);
172         IXGBE_READ_REG(hw, IXGBE_RJC);
173         IXGBE_READ_REG(hw, IXGBE_MNGPRC);
174         IXGBE_READ_REG(hw, IXGBE_MNGPDC);
175         IXGBE_READ_REG(hw, IXGBE_MNGPTC);
176         IXGBE_READ_REG(hw, IXGBE_TORL);
177         IXGBE_READ_REG(hw, IXGBE_TORH);
178         IXGBE_READ_REG(hw, IXGBE_TPR);
179         IXGBE_READ_REG(hw, IXGBE_TPT);
180         IXGBE_READ_REG(hw, IXGBE_PTC64);
181         IXGBE_READ_REG(hw, IXGBE_PTC127);
182         IXGBE_READ_REG(hw, IXGBE_PTC255);
183         IXGBE_READ_REG(hw, IXGBE_PTC511);
184         IXGBE_READ_REG(hw, IXGBE_PTC1023);
185         IXGBE_READ_REG(hw, IXGBE_PTC1522);
186         IXGBE_READ_REG(hw, IXGBE_MPTC);
187         IXGBE_READ_REG(hw, IXGBE_BPTC);
188         for (i = 0; i < 16; i++) {
189                 IXGBE_READ_REG(hw, IXGBE_QPRC(i));
190                 IXGBE_READ_REG(hw, IXGBE_QBRC(i));
191                 IXGBE_READ_REG(hw, IXGBE_QPTC(i));
192                 IXGBE_READ_REG(hw, IXGBE_QBTC(i));
193         }
194
195         return 0;
196 }
197
198 /**
199  *  ixgbe_read_pba_num_generic - Reads part number from EEPROM
200  *  @hw: pointer to hardware structure
201  *  @pba_num: stores the part number from the EEPROM
202  *
203  *  Reads the part number from the EEPROM.
204  **/
205 s32 ixgbe_read_pba_num_generic(struct ixgbe_hw *hw, u32 *pba_num)
206 {
207         s32 ret_val;
208         u16 data;
209
210         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
211         if (ret_val) {
212                 hw_dbg(hw, "NVM Read Error\n");
213                 return ret_val;
214         }
215         *pba_num = (u32)(data << 16);
216
217         ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &data);
218         if (ret_val) {
219                 hw_dbg(hw, "NVM Read Error\n");
220                 return ret_val;
221         }
222         *pba_num |= data;
223
224         return 0;
225 }
226
227 /**
228  *  ixgbe_get_mac_addr_generic - Generic get MAC address
229  *  @hw: pointer to hardware structure
230  *  @mac_addr: Adapter MAC address
231  *
232  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
233  *  A reset of the adapter must be performed prior to calling this function
234  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
235  **/
236 s32 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
237 {
238         u32 rar_high;
239         u32 rar_low;
240         u16 i;
241
242         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
243         rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
244
245         for (i = 0; i < 4; i++)
246                 mac_addr[i] = (u8)(rar_low >> (i*8));
247
248         for (i = 0; i < 2; i++)
249                 mac_addr[i+4] = (u8)(rar_high >> (i*8));
250
251         return 0;
252 }
253
254 /**
255  *  ixgbe_get_bus_info_generic - Generic set PCI bus info
256  *  @hw: pointer to hardware structure
257  *
258  *  Sets the PCI bus info (speed, width, type) within the ixgbe_hw structure
259  **/
260 s32 ixgbe_get_bus_info_generic(struct ixgbe_hw *hw)
261 {
262         struct ixgbe_adapter *adapter = hw->back;
263         struct ixgbe_mac_info *mac = &hw->mac;
264         u16 link_status;
265
266         hw->bus.type = ixgbe_bus_type_pci_express;
267
268         /* Get the negotiated link width and speed from PCI config space */
269         pci_read_config_word(adapter->pdev, IXGBE_PCI_LINK_STATUS,
270                              &link_status);
271
272         switch (link_status & IXGBE_PCI_LINK_WIDTH) {
273         case IXGBE_PCI_LINK_WIDTH_1:
274                 hw->bus.width = ixgbe_bus_width_pcie_x1;
275                 break;
276         case IXGBE_PCI_LINK_WIDTH_2:
277                 hw->bus.width = ixgbe_bus_width_pcie_x2;
278                 break;
279         case IXGBE_PCI_LINK_WIDTH_4:
280                 hw->bus.width = ixgbe_bus_width_pcie_x4;
281                 break;
282         case IXGBE_PCI_LINK_WIDTH_8:
283                 hw->bus.width = ixgbe_bus_width_pcie_x8;
284                 break;
285         default:
286                 hw->bus.width = ixgbe_bus_width_unknown;
287                 break;
288         }
289
290         switch (link_status & IXGBE_PCI_LINK_SPEED) {
291         case IXGBE_PCI_LINK_SPEED_2500:
292                 hw->bus.speed = ixgbe_bus_speed_2500;
293                 break;
294         case IXGBE_PCI_LINK_SPEED_5000:
295                 hw->bus.speed = ixgbe_bus_speed_5000;
296                 break;
297         default:
298                 hw->bus.speed = ixgbe_bus_speed_unknown;
299                 break;
300         }
301
302         mac->ops.set_lan_id(hw);
303
304         return 0;
305 }
306
307 /**
308  *  ixgbe_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
309  *  @hw: pointer to the HW structure
310  *
311  *  Determines the LAN function id by reading memory-mapped registers
312  *  and swaps the port value if requested.
313  **/
314 void ixgbe_set_lan_id_multi_port_pcie(struct ixgbe_hw *hw)
315 {
316         struct ixgbe_bus_info *bus = &hw->bus;
317         u32 reg;
318
319         reg = IXGBE_READ_REG(hw, IXGBE_STATUS);
320         bus->func = (reg & IXGBE_STATUS_LAN_ID) >> IXGBE_STATUS_LAN_ID_SHIFT;
321         bus->lan_id = bus->func;
322
323         /* check for a port swap */
324         reg = IXGBE_READ_REG(hw, IXGBE_FACTPS);
325         if (reg & IXGBE_FACTPS_LFS)
326                 bus->func ^= 0x1;
327 }
328
329 /**
330  *  ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
331  *  @hw: pointer to hardware structure
332  *
333  *  Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
334  *  disables transmit and receive units. The adapter_stopped flag is used by
335  *  the shared code and drivers to determine if the adapter is in a stopped
336  *  state and should not touch the hardware.
337  **/
338 s32 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
339 {
340         u32 number_of_queues;
341         u32 reg_val;
342         u16 i;
343
344         /*
345          * Set the adapter_stopped flag so other driver functions stop touching
346          * the hardware
347          */
348         hw->adapter_stopped = true;
349
350         /* Disable the receive unit */
351         reg_val = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
352         reg_val &= ~(IXGBE_RXCTRL_RXEN);
353         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, reg_val);
354         IXGBE_WRITE_FLUSH(hw);
355         msleep(2);
356
357         /* Clear interrupt mask to stop from interrupts being generated */
358         IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
359
360         /* Clear any pending interrupts */
361         IXGBE_READ_REG(hw, IXGBE_EICR);
362
363         /* Disable the transmit unit.  Each queue must be disabled. */
364         number_of_queues = hw->mac.max_tx_queues;
365         for (i = 0; i < number_of_queues; i++) {
366                 reg_val = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
367                 if (reg_val & IXGBE_TXDCTL_ENABLE) {
368                         reg_val &= ~IXGBE_TXDCTL_ENABLE;
369                         IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), reg_val);
370                 }
371         }
372
373         /*
374          * Prevent the PCI-E bus from from hanging by disabling PCI-E master
375          * access and verify no pending requests
376          */
377         if (ixgbe_disable_pcie_master(hw) != 0)
378                 hw_dbg(hw, "PCI-E Master disable polling has failed.\n");
379
380         return 0;
381 }
382
383 /**
384  *  ixgbe_led_on_generic - Turns on the software controllable LEDs.
385  *  @hw: pointer to hardware structure
386  *  @index: led number to turn on
387  **/
388 s32 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
389 {
390         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
391
392         /* To turn on the LED, set mode to ON. */
393         led_reg &= ~IXGBE_LED_MODE_MASK(index);
394         led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
395         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
396         IXGBE_WRITE_FLUSH(hw);
397
398         return 0;
399 }
400
401 /**
402  *  ixgbe_led_off_generic - Turns off the software controllable LEDs.
403  *  @hw: pointer to hardware structure
404  *  @index: led number to turn off
405  **/
406 s32 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
407 {
408         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
409
410         /* To turn off the LED, set mode to OFF. */
411         led_reg &= ~IXGBE_LED_MODE_MASK(index);
412         led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
413         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
414         IXGBE_WRITE_FLUSH(hw);
415
416         return 0;
417 }
418
419 /**
420  *  ixgbe_init_eeprom_params_generic - Initialize EEPROM params
421  *  @hw: pointer to hardware structure
422  *
423  *  Initializes the EEPROM parameters ixgbe_eeprom_info within the
424  *  ixgbe_hw struct in order to set up EEPROM access.
425  **/
426 s32 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
427 {
428         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
429         u32 eec;
430         u16 eeprom_size;
431
432         if (eeprom->type == ixgbe_eeprom_uninitialized) {
433                 eeprom->type = ixgbe_eeprom_none;
434                 /* Set default semaphore delay to 10ms which is a well
435                  * tested value */
436                 eeprom->semaphore_delay = 10;
437
438                 /*
439                  * Check for EEPROM present first.
440                  * If not present leave as none
441                  */
442                 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
443                 if (eec & IXGBE_EEC_PRES) {
444                         eeprom->type = ixgbe_eeprom_spi;
445
446                         /*
447                          * SPI EEPROM is assumed here.  This code would need to
448                          * change if a future EEPROM is not SPI.
449                          */
450                         eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
451                                             IXGBE_EEC_SIZE_SHIFT);
452                         eeprom->word_size = 1 << (eeprom_size +
453                                                   IXGBE_EEPROM_WORD_SIZE_SHIFT);
454                 }
455
456                 if (eec & IXGBE_EEC_ADDR_SIZE)
457                         eeprom->address_bits = 16;
458                 else
459                         eeprom->address_bits = 8;
460                 hw_dbg(hw, "Eeprom params: type = %d, size = %d, address bits: "
461                           "%d\n", eeprom->type, eeprom->word_size,
462                           eeprom->address_bits);
463         }
464
465         return 0;
466 }
467
468 /**
469  *  ixgbe_write_eeprom_generic - Writes 16 bit value to EEPROM
470  *  @hw: pointer to hardware structure
471  *  @offset: offset within the EEPROM to be written to
472  *  @data: 16 bit word to be written to the EEPROM
473  *
474  *  If ixgbe_eeprom_update_checksum is not called after this function, the
475  *  EEPROM will most likely contain an invalid checksum.
476  **/
477 s32 ixgbe_write_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
478 {
479         s32 status;
480         u8 write_opcode = IXGBE_EEPROM_WRITE_OPCODE_SPI;
481
482         hw->eeprom.ops.init_params(hw);
483
484         if (offset >= hw->eeprom.word_size) {
485                 status = IXGBE_ERR_EEPROM;
486                 goto out;
487         }
488
489         /* Prepare the EEPROM for writing  */
490         status = ixgbe_acquire_eeprom(hw);
491
492         if (status == 0) {
493                 if (ixgbe_ready_eeprom(hw) != 0) {
494                         ixgbe_release_eeprom(hw);
495                         status = IXGBE_ERR_EEPROM;
496                 }
497         }
498
499         if (status == 0) {
500                 ixgbe_standby_eeprom(hw);
501
502                 /*  Send the WRITE ENABLE command (8 bit opcode )  */
503                 ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_WREN_OPCODE_SPI,
504                                             IXGBE_EEPROM_OPCODE_BITS);
505
506                 ixgbe_standby_eeprom(hw);
507
508                 /*
509                  * Some SPI eeproms use the 8th address bit embedded in the
510                  * opcode
511                  */
512                 if ((hw->eeprom.address_bits == 8) && (offset >= 128))
513                         write_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
514
515                 /* Send the Write command (8-bit opcode + addr) */
516                 ixgbe_shift_out_eeprom_bits(hw, write_opcode,
517                                             IXGBE_EEPROM_OPCODE_BITS);
518                 ixgbe_shift_out_eeprom_bits(hw, (u16)(offset*2),
519                                             hw->eeprom.address_bits);
520
521                 /* Send the data */
522                 data = (data >> 8) | (data << 8);
523                 ixgbe_shift_out_eeprom_bits(hw, data, 16);
524                 ixgbe_standby_eeprom(hw);
525
526                 msleep(hw->eeprom.semaphore_delay);
527                 /* Done with writing - release the EEPROM */
528                 ixgbe_release_eeprom(hw);
529         }
530
531 out:
532         return status;
533 }
534
535 /**
536  *  ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
537  *  @hw: pointer to hardware structure
538  *  @offset: offset within the EEPROM to be read
539  *  @data: read 16 bit value from EEPROM
540  *
541  *  Reads 16 bit value from EEPROM through bit-bang method
542  **/
543 s32 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
544                                        u16 *data)
545 {
546         s32 status;
547         u16 word_in;
548         u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
549
550         hw->eeprom.ops.init_params(hw);
551
552         if (offset >= hw->eeprom.word_size) {
553                 status = IXGBE_ERR_EEPROM;
554                 goto out;
555         }
556
557         /* Prepare the EEPROM for reading  */
558         status = ixgbe_acquire_eeprom(hw);
559
560         if (status == 0) {
561                 if (ixgbe_ready_eeprom(hw) != 0) {
562                         ixgbe_release_eeprom(hw);
563                         status = IXGBE_ERR_EEPROM;
564                 }
565         }
566
567         if (status == 0) {
568                 ixgbe_standby_eeprom(hw);
569
570                 /*
571                  * Some SPI eeproms use the 8th address bit embedded in the
572                  * opcode
573                  */
574                 if ((hw->eeprom.address_bits == 8) && (offset >= 128))
575                         read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
576
577                 /* Send the READ command (opcode + addr) */
578                 ixgbe_shift_out_eeprom_bits(hw, read_opcode,
579                                             IXGBE_EEPROM_OPCODE_BITS);
580                 ixgbe_shift_out_eeprom_bits(hw, (u16)(offset*2),
581                                             hw->eeprom.address_bits);
582
583                 /* Read the data. */
584                 word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
585                 *data = (word_in >> 8) | (word_in << 8);
586
587                 /* End this read operation */
588                 ixgbe_release_eeprom(hw);
589         }
590
591 out:
592         return status;
593 }
594
595 /**
596  *  ixgbe_read_eerd_generic - Read EEPROM word using EERD
597  *  @hw: pointer to hardware structure
598  *  @offset: offset of  word in the EEPROM to read
599  *  @data: word read from the EEPROM
600  *
601  *  Reads a 16 bit word from the EEPROM using the EERD register.
602  **/
603 s32 ixgbe_read_eerd_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
604 {
605         u32 eerd;
606         s32 status;
607
608         hw->eeprom.ops.init_params(hw);
609
610         if (offset >= hw->eeprom.word_size) {
611                 status = IXGBE_ERR_EEPROM;
612                 goto out;
613         }
614
615         eerd = (offset << IXGBE_EEPROM_RW_ADDR_SHIFT) +
616                IXGBE_EEPROM_RW_REG_START;
617
618         IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
619         status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_READ);
620
621         if (status == 0)
622                 *data = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
623                          IXGBE_EEPROM_RW_REG_DATA);
624         else
625                 hw_dbg(hw, "Eeprom read timed out\n");
626
627 out:
628         return status;
629 }
630
631 /**
632  *  ixgbe_poll_eerd_eewr_done - Poll EERD read or EEWR write status
633  *  @hw: pointer to hardware structure
634  *  @ee_reg: EEPROM flag for polling
635  *
636  *  Polls the status bit (bit 1) of the EERD or EEWR to determine when the
637  *  read or write is done respectively.
638  **/
639 s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg)
640 {
641         u32 i;
642         u32 reg;
643         s32 status = IXGBE_ERR_EEPROM;
644
645         for (i = 0; i < IXGBE_EERD_EEWR_ATTEMPTS; i++) {
646                 if (ee_reg == IXGBE_NVM_POLL_READ)
647                         reg = IXGBE_READ_REG(hw, IXGBE_EERD);
648                 else
649                         reg = IXGBE_READ_REG(hw, IXGBE_EEWR);
650
651                 if (reg & IXGBE_EEPROM_RW_REG_DONE) {
652                         status = 0;
653                         break;
654                 }
655                 udelay(5);
656         }
657         return status;
658 }
659
660 /**
661  *  ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
662  *  @hw: pointer to hardware structure
663  *
664  *  Prepares EEPROM for access using bit-bang method. This function should
665  *  be called before issuing a command to the EEPROM.
666  **/
667 static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
668 {
669         s32 status = 0;
670         u32 eec = 0;
671         u32 i;
672
673         if (ixgbe_acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) != 0)
674                 status = IXGBE_ERR_SWFW_SYNC;
675
676         if (status == 0) {
677                 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
678
679                 /* Request EEPROM Access */
680                 eec |= IXGBE_EEC_REQ;
681                 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
682
683                 for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
684                         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
685                         if (eec & IXGBE_EEC_GNT)
686                                 break;
687                         udelay(5);
688                 }
689
690                 /* Release if grant not acquired */
691                 if (!(eec & IXGBE_EEC_GNT)) {
692                         eec &= ~IXGBE_EEC_REQ;
693                         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
694                         hw_dbg(hw, "Could not acquire EEPROM grant\n");
695
696                         ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
697                         status = IXGBE_ERR_EEPROM;
698                 }
699         }
700
701         /* Setup EEPROM for Read/Write */
702         if (status == 0) {
703                 /* Clear CS and SK */
704                 eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
705                 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
706                 IXGBE_WRITE_FLUSH(hw);
707                 udelay(1);
708         }
709         return status;
710 }
711
712 /**
713  *  ixgbe_get_eeprom_semaphore - Get hardware semaphore
714  *  @hw: pointer to hardware structure
715  *
716  *  Sets the hardware semaphores so EEPROM access can occur for bit-bang method
717  **/
718 static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
719 {
720         s32 status = IXGBE_ERR_EEPROM;
721         u32 timeout;
722         u32 i;
723         u32 swsm;
724
725         /* Set timeout value based on size of EEPROM */
726         timeout = hw->eeprom.word_size + 1;
727
728         /* Get SMBI software semaphore between device drivers first */
729         for (i = 0; i < timeout; i++) {
730                 /*
731                  * If the SMBI bit is 0 when we read it, then the bit will be
732                  * set and we have the semaphore
733                  */
734                 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
735                 if (!(swsm & IXGBE_SWSM_SMBI)) {
736                         status = 0;
737                         break;
738                 }
739                 msleep(1);
740         }
741
742         /* Now get the semaphore between SW/FW through the SWESMBI bit */
743         if (status == 0) {
744                 for (i = 0; i < timeout; i++) {
745                         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
746
747                         /* Set the SW EEPROM semaphore bit to request access */
748                         swsm |= IXGBE_SWSM_SWESMBI;
749                         IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
750
751                         /*
752                          * If we set the bit successfully then we got the
753                          * semaphore.
754                          */
755                         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
756                         if (swsm & IXGBE_SWSM_SWESMBI)
757                                 break;
758
759                         udelay(50);
760                 }
761
762                 /*
763                  * Release semaphores and return error if SW EEPROM semaphore
764                  * was not granted because we don't have access to the EEPROM
765                  */
766                 if (i >= timeout) {
767                         hw_dbg(hw, "Driver can't access the Eeprom - Semaphore "
768                                "not granted.\n");
769                         ixgbe_release_eeprom_semaphore(hw);
770                         status = IXGBE_ERR_EEPROM;
771                 }
772         }
773
774         return status;
775 }
776
777 /**
778  *  ixgbe_release_eeprom_semaphore - Release hardware semaphore
779  *  @hw: pointer to hardware structure
780  *
781  *  This function clears hardware semaphore bits.
782  **/
783 static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
784 {
785         u32 swsm;
786
787         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
788
789         /* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
790         swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
791         IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
792         IXGBE_WRITE_FLUSH(hw);
793 }
794
795 /**
796  *  ixgbe_ready_eeprom - Polls for EEPROM ready
797  *  @hw: pointer to hardware structure
798  **/
799 static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
800 {
801         s32 status = 0;
802         u16 i;
803         u8 spi_stat_reg;
804
805         /*
806          * Read "Status Register" repeatedly until the LSB is cleared.  The
807          * EEPROM will signal that the command has been completed by clearing
808          * bit 0 of the internal status register.  If it's not cleared within
809          * 5 milliseconds, then error out.
810          */
811         for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
812                 ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
813                                             IXGBE_EEPROM_OPCODE_BITS);
814                 spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
815                 if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
816                         break;
817
818                 udelay(5);
819                 ixgbe_standby_eeprom(hw);
820         };
821
822         /*
823          * On some parts, SPI write time could vary from 0-20mSec on 3.3V
824          * devices (and only 0-5mSec on 5V devices)
825          */
826         if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
827                 hw_dbg(hw, "SPI EEPROM Status error\n");
828                 status = IXGBE_ERR_EEPROM;
829         }
830
831         return status;
832 }
833
834 /**
835  *  ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
836  *  @hw: pointer to hardware structure
837  **/
838 static void ixgbe_standby_eeprom(struct ixgbe_hw *hw)
839 {
840         u32 eec;
841
842         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
843
844         /* Toggle CS to flush commands */
845         eec |= IXGBE_EEC_CS;
846         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
847         IXGBE_WRITE_FLUSH(hw);
848         udelay(1);
849         eec &= ~IXGBE_EEC_CS;
850         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
851         IXGBE_WRITE_FLUSH(hw);
852         udelay(1);
853 }
854
855 /**
856  *  ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
857  *  @hw: pointer to hardware structure
858  *  @data: data to send to the EEPROM
859  *  @count: number of bits to shift out
860  **/
861 static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
862                                         u16 count)
863 {
864         u32 eec;
865         u32 mask;
866         u32 i;
867
868         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
869
870         /*
871          * Mask is used to shift "count" bits of "data" out to the EEPROM
872          * one bit at a time.  Determine the starting bit based on count
873          */
874         mask = 0x01 << (count - 1);
875
876         for (i = 0; i < count; i++) {
877                 /*
878                  * A "1" is shifted out to the EEPROM by setting bit "DI" to a
879                  * "1", and then raising and then lowering the clock (the SK
880                  * bit controls the clock input to the EEPROM).  A "0" is
881                  * shifted out to the EEPROM by setting "DI" to "0" and then
882                  * raising and then lowering the clock.
883                  */
884                 if (data & mask)
885                         eec |= IXGBE_EEC_DI;
886                 else
887                         eec &= ~IXGBE_EEC_DI;
888
889                 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
890                 IXGBE_WRITE_FLUSH(hw);
891
892                 udelay(1);
893
894                 ixgbe_raise_eeprom_clk(hw, &eec);
895                 ixgbe_lower_eeprom_clk(hw, &eec);
896
897                 /*
898                  * Shift mask to signify next bit of data to shift in to the
899                  * EEPROM
900                  */
901                 mask = mask >> 1;
902         };
903
904         /* We leave the "DI" bit set to "0" when we leave this routine. */
905         eec &= ~IXGBE_EEC_DI;
906         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
907         IXGBE_WRITE_FLUSH(hw);
908 }
909
910 /**
911  *  ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
912  *  @hw: pointer to hardware structure
913  **/
914 static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
915 {
916         u32 eec;
917         u32 i;
918         u16 data = 0;
919
920         /*
921          * In order to read a register from the EEPROM, we need to shift
922          * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
923          * the clock input to the EEPROM (setting the SK bit), and then reading
924          * the value of the "DO" bit.  During this "shifting in" process the
925          * "DI" bit should always be clear.
926          */
927         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
928
929         eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
930
931         for (i = 0; i < count; i++) {
932                 data = data << 1;
933                 ixgbe_raise_eeprom_clk(hw, &eec);
934
935                 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
936
937                 eec &= ~(IXGBE_EEC_DI);
938                 if (eec & IXGBE_EEC_DO)
939                         data |= 1;
940
941                 ixgbe_lower_eeprom_clk(hw, &eec);
942         }
943
944         return data;
945 }
946
947 /**
948  *  ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
949  *  @hw: pointer to hardware structure
950  *  @eec: EEC register's current value
951  **/
952 static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
953 {
954         /*
955          * Raise the clock input to the EEPROM
956          * (setting the SK bit), then delay
957          */
958         *eec = *eec | IXGBE_EEC_SK;
959         IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
960         IXGBE_WRITE_FLUSH(hw);
961         udelay(1);
962 }
963
964 /**
965  *  ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
966  *  @hw: pointer to hardware structure
967  *  @eecd: EECD's current value
968  **/
969 static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
970 {
971         /*
972          * Lower the clock input to the EEPROM (clearing the SK bit), then
973          * delay
974          */
975         *eec = *eec & ~IXGBE_EEC_SK;
976         IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
977         IXGBE_WRITE_FLUSH(hw);
978         udelay(1);
979 }
980
981 /**
982  *  ixgbe_release_eeprom - Release EEPROM, release semaphores
983  *  @hw: pointer to hardware structure
984  **/
985 static void ixgbe_release_eeprom(struct ixgbe_hw *hw)
986 {
987         u32 eec;
988
989         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
990
991         eec |= IXGBE_EEC_CS;  /* Pull CS high */
992         eec &= ~IXGBE_EEC_SK; /* Lower SCK */
993
994         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
995         IXGBE_WRITE_FLUSH(hw);
996
997         udelay(1);
998
999         /* Stop requesting EEPROM access */
1000         eec &= ~IXGBE_EEC_REQ;
1001         IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1002
1003         ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
1004 }
1005
1006 /**
1007  *  ixgbe_calc_eeprom_checksum - Calculates and returns the checksum
1008  *  @hw: pointer to hardware structure
1009  **/
1010 u16 ixgbe_calc_eeprom_checksum_generic(struct ixgbe_hw *hw)
1011 {
1012         u16 i;
1013         u16 j;
1014         u16 checksum = 0;
1015         u16 length = 0;
1016         u16 pointer = 0;
1017         u16 word = 0;
1018
1019         /* Include 0x0-0x3F in the checksum */
1020         for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
1021                 if (hw->eeprom.ops.read(hw, i, &word) != 0) {
1022                         hw_dbg(hw, "EEPROM read failed\n");
1023                         break;
1024                 }
1025                 checksum += word;
1026         }
1027
1028         /* Include all data from pointers except for the fw pointer */
1029         for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
1030                 hw->eeprom.ops.read(hw, i, &pointer);
1031
1032                 /* Make sure the pointer seems valid */
1033                 if (pointer != 0xFFFF && pointer != 0) {
1034                         hw->eeprom.ops.read(hw, pointer, &length);
1035
1036                         if (length != 0xFFFF && length != 0) {
1037                                 for (j = pointer+1; j <= pointer+length; j++) {
1038                                         hw->eeprom.ops.read(hw, j, &word);
1039                                         checksum += word;
1040                                 }
1041                         }
1042                 }
1043         }
1044
1045         checksum = (u16)IXGBE_EEPROM_SUM - checksum;
1046
1047         return checksum;
1048 }
1049
1050 /**
1051  *  ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
1052  *  @hw: pointer to hardware structure
1053  *  @checksum_val: calculated checksum
1054  *
1055  *  Performs checksum calculation and validates the EEPROM checksum.  If the
1056  *  caller does not need checksum_val, the value can be NULL.
1057  **/
1058 s32 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
1059                                            u16 *checksum_val)
1060 {
1061         s32 status;
1062         u16 checksum;
1063         u16 read_checksum = 0;
1064
1065         /*
1066          * Read the first word from the EEPROM. If this times out or fails, do
1067          * not continue or we could be in for a very long wait while every
1068          * EEPROM read fails
1069          */
1070         status = hw->eeprom.ops.read(hw, 0, &checksum);
1071
1072         if (status == 0) {
1073                 checksum = hw->eeprom.ops.calc_checksum(hw);
1074
1075                 hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
1076
1077                 /*
1078                  * Verify read checksum from EEPROM is the same as
1079                  * calculated checksum
1080                  */
1081                 if (read_checksum != checksum)
1082                         status = IXGBE_ERR_EEPROM_CHECKSUM;
1083
1084                 /* If the user cares, return the calculated checksum */
1085                 if (checksum_val)
1086                         *checksum_val = checksum;
1087         } else {
1088                 hw_dbg(hw, "EEPROM read failed\n");
1089         }
1090
1091         return status;
1092 }
1093
1094 /**
1095  *  ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
1096  *  @hw: pointer to hardware structure
1097  **/
1098 s32 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
1099 {
1100         s32 status;
1101         u16 checksum;
1102
1103         /*
1104          * Read the first word from the EEPROM. If this times out or fails, do
1105          * not continue or we could be in for a very long wait while every
1106          * EEPROM read fails
1107          */
1108         status = hw->eeprom.ops.read(hw, 0, &checksum);
1109
1110         if (status == 0) {
1111                 checksum = hw->eeprom.ops.calc_checksum(hw);
1112                 status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM,
1113                                             checksum);
1114         } else {
1115                 hw_dbg(hw, "EEPROM read failed\n");
1116         }
1117
1118         return status;
1119 }
1120
1121 /**
1122  *  ixgbe_validate_mac_addr - Validate MAC address
1123  *  @mac_addr: pointer to MAC address.
1124  *
1125  *  Tests a MAC address to ensure it is a valid Individual Address
1126  **/
1127 s32 ixgbe_validate_mac_addr(u8 *mac_addr)
1128 {
1129         s32 status = 0;
1130
1131         /* Make sure it is not a multicast address */
1132         if (IXGBE_IS_MULTICAST(mac_addr))
1133                 status = IXGBE_ERR_INVALID_MAC_ADDR;
1134         /* Not a broadcast address */
1135         else if (IXGBE_IS_BROADCAST(mac_addr))
1136                 status = IXGBE_ERR_INVALID_MAC_ADDR;
1137         /* Reject the zero address */
1138         else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
1139                  mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0)
1140                 status = IXGBE_ERR_INVALID_MAC_ADDR;
1141
1142         return status;
1143 }
1144
1145 /**
1146  *  ixgbe_set_rar_generic - Set Rx address register
1147  *  @hw: pointer to hardware structure
1148  *  @index: Receive address register to write
1149  *  @addr: Address to put into receive address register
1150  *  @vmdq: VMDq "set" or "pool" index
1151  *  @enable_addr: set flag that address is active
1152  *
1153  *  Puts an ethernet address into a receive address register.
1154  **/
1155 s32 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
1156                           u32 enable_addr)
1157 {
1158         u32 rar_low, rar_high;
1159         u32 rar_entries = hw->mac.num_rar_entries;
1160
1161         /* setup VMDq pool selection before this RAR gets enabled */
1162         hw->mac.ops.set_vmdq(hw, index, vmdq);
1163
1164         /* Make sure we are using a valid rar index range */
1165         if (index < rar_entries) {
1166                 /*
1167                  * HW expects these in little endian so we reverse the byte
1168                  * order from network order (big endian) to little endian
1169                  */
1170                 rar_low = ((u32)addr[0] |
1171                            ((u32)addr[1] << 8) |
1172                            ((u32)addr[2] << 16) |
1173                            ((u32)addr[3] << 24));
1174                 /*
1175                  * Some parts put the VMDq setting in the extra RAH bits,
1176                  * so save everything except the lower 16 bits that hold part
1177                  * of the address and the address valid bit.
1178                  */
1179                 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1180                 rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
1181                 rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
1182
1183                 if (enable_addr != 0)
1184                         rar_high |= IXGBE_RAH_AV;
1185
1186                 IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
1187                 IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1188         } else {
1189                 hw_dbg(hw, "RAR index %d is out of range.\n", index);
1190                 return IXGBE_ERR_RAR_INDEX;
1191         }
1192
1193         return 0;
1194 }
1195
1196 /**
1197  *  ixgbe_clear_rar_generic - Remove Rx address register
1198  *  @hw: pointer to hardware structure
1199  *  @index: Receive address register to write
1200  *
1201  *  Clears an ethernet address from a receive address register.
1202  **/
1203 s32 ixgbe_clear_rar_generic(struct ixgbe_hw *hw, u32 index)
1204 {
1205         u32 rar_high;
1206         u32 rar_entries = hw->mac.num_rar_entries;
1207
1208         /* Make sure we are using a valid rar index range */
1209         if (index < rar_entries) {
1210                 /*
1211                  * Some parts put the VMDq setting in the extra RAH bits,
1212                  * so save everything except the lower 16 bits that hold part
1213                  * of the address and the address valid bit.
1214                  */
1215                 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1216                 rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
1217
1218                 IXGBE_WRITE_REG(hw, IXGBE_RAL(index), 0);
1219                 IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1220         } else {
1221                 hw_dbg(hw, "RAR index %d is out of range.\n", index);
1222                 return IXGBE_ERR_RAR_INDEX;
1223         }
1224
1225         /* clear VMDq pool/queue selection for this RAR */
1226         hw->mac.ops.clear_vmdq(hw, index, IXGBE_CLEAR_VMDQ_ALL);
1227
1228         return 0;
1229 }
1230
1231 /**
1232  *  ixgbe_enable_rar - Enable Rx address register
1233  *  @hw: pointer to hardware structure
1234  *  @index: index into the RAR table
1235  *
1236  *  Enables the select receive address register.
1237  **/
1238 static void ixgbe_enable_rar(struct ixgbe_hw *hw, u32 index)
1239 {
1240         u32 rar_high;
1241
1242         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1243         rar_high |= IXGBE_RAH_AV;
1244         IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1245 }
1246
1247 /**
1248  *  ixgbe_disable_rar - Disable Rx address register
1249  *  @hw: pointer to hardware structure
1250  *  @index: index into the RAR table
1251  *
1252  *  Disables the select receive address register.
1253  **/
1254 static void ixgbe_disable_rar(struct ixgbe_hw *hw, u32 index)
1255 {
1256         u32 rar_high;
1257
1258         rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1259         rar_high &= (~IXGBE_RAH_AV);
1260         IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1261 }
1262
1263 /**
1264  *  ixgbe_init_rx_addrs_generic - Initializes receive address filters.
1265  *  @hw: pointer to hardware structure
1266  *
1267  *  Places the MAC address in receive address register 0 and clears the rest
1268  *  of the receive address registers. Clears the multicast table. Assumes
1269  *  the receiver is in reset when the routine is called.
1270  **/
1271 s32 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
1272 {
1273         u32 i;
1274         u32 rar_entries = hw->mac.num_rar_entries;
1275
1276         /*
1277          * If the current mac address is valid, assume it is a software override
1278          * to the permanent address.
1279          * Otherwise, use the permanent address from the eeprom.
1280          */
1281         if (ixgbe_validate_mac_addr(hw->mac.addr) ==
1282             IXGBE_ERR_INVALID_MAC_ADDR) {
1283                 /* Get the MAC address from the RAR0 for later reference */
1284                 hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
1285
1286                 hw_dbg(hw, " Keeping Current RAR0 Addr =%pM\n", hw->mac.addr);
1287         } else {
1288                 /* Setup the receive address. */
1289                 hw_dbg(hw, "Overriding MAC Address in RAR[0]\n");
1290                 hw_dbg(hw, " New MAC Addr =%pM\n", hw->mac.addr);
1291
1292                 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
1293         }
1294         hw->addr_ctrl.overflow_promisc = 0;
1295
1296         hw->addr_ctrl.rar_used_count = 1;
1297
1298         /* Zero out the other receive addresses. */
1299         hw_dbg(hw, "Clearing RAR[1-%d]\n", rar_entries - 1);
1300         for (i = 1; i < rar_entries; i++) {
1301                 IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
1302                 IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
1303         }
1304
1305         /* Clear the MTA */
1306         hw->addr_ctrl.mc_addr_in_rar_count = 0;
1307         hw->addr_ctrl.mta_in_use = 0;
1308         IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
1309
1310         hw_dbg(hw, " Clearing MTA\n");
1311         for (i = 0; i < hw->mac.mcft_size; i++)
1312                 IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
1313
1314         if (hw->mac.ops.init_uta_tables)
1315                 hw->mac.ops.init_uta_tables(hw);
1316
1317         return 0;
1318 }
1319
1320 /**
1321  *  ixgbe_add_uc_addr - Adds a secondary unicast address.
1322  *  @hw: pointer to hardware structure
1323  *  @addr: new address
1324  *
1325  *  Adds it to unused receive address register or goes into promiscuous mode.
1326  **/
1327 static void ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq)
1328 {
1329         u32 rar_entries = hw->mac.num_rar_entries;
1330         u32 rar;
1331
1332         hw_dbg(hw, " UC Addr = %.2X %.2X %.2X %.2X %.2X %.2X\n",
1333                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
1334
1335         /*
1336          * Place this address in the RAR if there is room,
1337          * else put the controller into promiscuous mode
1338          */
1339         if (hw->addr_ctrl.rar_used_count < rar_entries) {
1340                 rar = hw->addr_ctrl.rar_used_count -
1341                       hw->addr_ctrl.mc_addr_in_rar_count;
1342                 hw->mac.ops.set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
1343                 hw_dbg(hw, "Added a secondary address to RAR[%d]\n", rar);
1344                 hw->addr_ctrl.rar_used_count++;
1345         } else {
1346                 hw->addr_ctrl.overflow_promisc++;
1347         }
1348
1349         hw_dbg(hw, "ixgbe_add_uc_addr Complete\n");
1350 }
1351
1352 /**
1353  *  ixgbe_update_uc_addr_list_generic - Updates MAC list of secondary addresses
1354  *  @hw: pointer to hardware structure
1355  *  @netdev: pointer to net device structure
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 net_device *netdev)
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 + 1);
1383         for (i = 0; i < uc_addr_in_use; i++) {
1384                 IXGBE_WRITE_REG(hw, IXGBE_RAL(1+i), 0);
1385                 IXGBE_WRITE_REG(hw, IXGBE_RAH(1+i), 0);
1386         }
1387
1388         /* Add the new addresses */
1389         netdev_for_each_uc_addr(ha, netdev) {
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                         hw->addr_ctrl.uc_set_promisc = true;
1402                 }
1403         } else {
1404                 /* only disable if set by overflow, not by user */
1405                 if ((old_promisc_setting && hw->addr_ctrl.uc_set_promisc) &&
1406                    !(hw->addr_ctrl.user_set_promisc)) {
1407                         hw_dbg(hw, " Leaving address overflow promisc mode\n");
1408                         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1409                         fctrl &= ~IXGBE_FCTRL_UPE;
1410                         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1411                         hw->addr_ctrl.uc_set_promisc = false;
1412                 }
1413         }
1414
1415         hw_dbg(hw, "ixgbe_update_uc_addr_list_generic Complete\n");
1416         return 0;
1417 }
1418
1419 /**
1420  *  ixgbe_mta_vector - Determines bit-vector in multicast table to set
1421  *  @hw: pointer to hardware structure
1422  *  @mc_addr: the multicast address
1423  *
1424  *  Extracts the 12 bits, from a multicast address, to determine which
1425  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
1426  *  incoming rx multicast addresses, to determine the bit-vector to check in
1427  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
1428  *  by the MO field of the MCSTCTRL. The MO field is set during initialization
1429  *  to mc_filter_type.
1430  **/
1431 static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
1432 {
1433         u32 vector = 0;
1434
1435         switch (hw->mac.mc_filter_type) {
1436         case 0:   /* use bits [47:36] of the address */
1437                 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
1438                 break;
1439         case 1:   /* use bits [46:35] of the address */
1440                 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
1441                 break;
1442         case 2:   /* use bits [45:34] of the address */
1443                 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
1444                 break;
1445         case 3:   /* use bits [43:32] of the address */
1446                 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
1447                 break;
1448         default:  /* Invalid mc_filter_type */
1449                 hw_dbg(hw, "MC filter type param set incorrectly\n");
1450                 break;
1451         }
1452
1453         /* vector can only be 12-bits or boundary will be exceeded */
1454         vector &= 0xFFF;
1455         return vector;
1456 }
1457
1458 /**
1459  *  ixgbe_set_mta - Set bit-vector in multicast table
1460  *  @hw: pointer to hardware structure
1461  *  @hash_value: Multicast address hash value
1462  *
1463  *  Sets the bit-vector in the multicast table.
1464  **/
1465 static void ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
1466 {
1467         u32 vector;
1468         u32 vector_bit;
1469         u32 vector_reg;
1470         u32 mta_reg;
1471
1472         hw->addr_ctrl.mta_in_use++;
1473
1474         vector = ixgbe_mta_vector(hw, mc_addr);
1475         hw_dbg(hw, " bit-vector = 0x%03X\n", vector);
1476
1477         /*
1478          * The MTA is a register array of 128 32-bit registers. It is treated
1479          * like an array of 4096 bits.  We want to set bit
1480          * BitArray[vector_value]. So we figure out what register the bit is
1481          * in, read it, OR in the new bit, then write back the new value.  The
1482          * register is determined by the upper 7 bits of the vector value and
1483          * the bit within that register are determined by the lower 5 bits of
1484          * the value.
1485          */
1486         vector_reg = (vector >> 5) & 0x7F;
1487         vector_bit = vector & 0x1F;
1488         mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
1489         mta_reg |= (1 << vector_bit);
1490         IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
1491 }
1492
1493 /**
1494  *  ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
1495  *  @hw: pointer to hardware structure
1496  *  @netdev: pointer to net device structure
1497  *
1498  *  The given list replaces any existing list. Clears the MC addrs from receive
1499  *  address registers and the multicast table. Uses unused receive address
1500  *  registers for the first multicast addresses, and hashes the rest into the
1501  *  multicast table.
1502  **/
1503 s32 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw,
1504                                       struct net_device *netdev)
1505 {
1506         struct netdev_hw_addr *ha;
1507         u32 i;
1508
1509         /*
1510          * Set the new number of MC addresses that we are being requested to
1511          * use.
1512          */
1513         hw->addr_ctrl.num_mc_addrs = netdev_mc_count(netdev);
1514         hw->addr_ctrl.mta_in_use = 0;
1515
1516         /* Clear the MTA */
1517         hw_dbg(hw, " Clearing MTA\n");
1518         for (i = 0; i < hw->mac.mcft_size; i++)
1519                 IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
1520
1521         /* Add the new addresses */
1522         netdev_for_each_mc_addr(ha, netdev) {
1523                 hw_dbg(hw, " Adding the multicast addresses:\n");
1524                 ixgbe_set_mta(hw, ha->addr);
1525         }
1526
1527         /* Enable mta */
1528         if (hw->addr_ctrl.mta_in_use > 0)
1529                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
1530                                 IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
1531
1532         hw_dbg(hw, "ixgbe_update_mc_addr_list_generic Complete\n");
1533         return 0;
1534 }
1535
1536 /**
1537  *  ixgbe_enable_mc_generic - Enable multicast address in RAR
1538  *  @hw: pointer to hardware structure
1539  *
1540  *  Enables multicast address in RAR and the use of the multicast hash table.
1541  **/
1542 s32 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
1543 {
1544         u32 i;
1545         u32 rar_entries = hw->mac.num_rar_entries;
1546         struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
1547
1548         if (a->mc_addr_in_rar_count > 0)
1549                 for (i = (rar_entries - a->mc_addr_in_rar_count);
1550                      i < rar_entries; i++)
1551                         ixgbe_enable_rar(hw, i);
1552
1553         if (a->mta_in_use > 0)
1554                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
1555                                 hw->mac.mc_filter_type);
1556
1557         return 0;
1558 }
1559
1560 /**
1561  *  ixgbe_disable_mc_generic - Disable multicast address in RAR
1562  *  @hw: pointer to hardware structure
1563  *
1564  *  Disables multicast address in RAR and the use of the multicast hash table.
1565  **/
1566 s32 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
1567 {
1568         u32 i;
1569         u32 rar_entries = hw->mac.num_rar_entries;
1570         struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
1571
1572         if (a->mc_addr_in_rar_count > 0)
1573                 for (i = (rar_entries - a->mc_addr_in_rar_count);
1574                      i < rar_entries; i++)
1575                         ixgbe_disable_rar(hw, i);
1576
1577         if (a->mta_in_use > 0)
1578                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
1579
1580         return 0;
1581 }
1582
1583 /**
1584  *  ixgbe_fc_enable_generic - Enable flow control
1585  *  @hw: pointer to hardware structure
1586  *  @packetbuf_num: packet buffer number (0-7)
1587  *
1588  *  Enable flow control according to the current settings.
1589  **/
1590 s32 ixgbe_fc_enable_generic(struct ixgbe_hw *hw, s32 packetbuf_num)
1591 {
1592         s32 ret_val = 0;
1593         u32 mflcn_reg, fccfg_reg;
1594         u32 reg;
1595         u32 rx_pba_size;
1596         u32 fcrtl, fcrth;
1597
1598 #ifdef CONFIG_DCB
1599         if (hw->fc.requested_mode == ixgbe_fc_pfc)
1600                 goto out;
1601
1602 #endif /* CONFIG_DCB */
1603         /* Negotiate the fc mode to use */
1604         ret_val = ixgbe_fc_autoneg(hw);
1605         if (ret_val)
1606                 goto out;
1607
1608         /* Disable any previous flow control settings */
1609         mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
1610         mflcn_reg &= ~(IXGBE_MFLCN_RFCE | IXGBE_MFLCN_RPFCE);
1611
1612         fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
1613         fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
1614
1615         /*
1616          * The possible values of fc.current_mode are:
1617          * 0: Flow control is completely disabled
1618          * 1: Rx flow control is enabled (we can receive pause frames,
1619          *    but not send pause frames).
1620          * 2: Tx flow control is enabled (we can send pause frames but
1621          *    we do not support receiving pause frames).
1622          * 3: Both Rx and Tx flow control (symmetric) are enabled.
1623          * 4: Priority Flow Control is enabled.
1624          * other: Invalid.
1625          */
1626         switch (hw->fc.current_mode) {
1627         case ixgbe_fc_none:
1628                 /*
1629                  * Flow control is disabled by software override or autoneg.
1630                  * The code below will actually disable it in the HW.
1631                  */
1632                 break;
1633         case ixgbe_fc_rx_pause:
1634                 /*
1635                  * Rx Flow control is enabled and Tx Flow control is
1636                  * disabled by software override. Since there really
1637                  * isn't a way to advertise that we are capable of RX
1638                  * Pause ONLY, we will advertise that we support both
1639                  * symmetric and asymmetric Rx PAUSE.  Later, we will
1640                  * disable the adapter's ability to send PAUSE frames.
1641                  */
1642                 mflcn_reg |= IXGBE_MFLCN_RFCE;
1643                 break;
1644         case ixgbe_fc_tx_pause:
1645                 /*
1646                  * Tx Flow control is enabled, and Rx Flow control is
1647                  * disabled by software override.
1648                  */
1649                 fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
1650                 break;
1651         case ixgbe_fc_full:
1652                 /* Flow control (both Rx and Tx) is enabled by SW override. */
1653                 mflcn_reg |= IXGBE_MFLCN_RFCE;
1654                 fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
1655                 break;
1656 #ifdef CONFIG_DCB
1657         case ixgbe_fc_pfc:
1658                 goto out;
1659                 break;
1660 #endif /* CONFIG_DCB */
1661         default:
1662                 hw_dbg(hw, "Flow control param set incorrectly\n");
1663                 ret_val = IXGBE_ERR_CONFIG;
1664                 goto out;
1665                 break;
1666         }
1667
1668         /* Set 802.3x based flow control settings. */
1669         mflcn_reg |= IXGBE_MFLCN_DPF;
1670         IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
1671         IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
1672
1673         rx_pba_size = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(packetbuf_num));
1674         rx_pba_size >>= IXGBE_RXPBSIZE_SHIFT;
1675
1676         fcrth = (rx_pba_size - hw->fc.high_water) << 10;
1677         fcrtl = (rx_pba_size - hw->fc.low_water) << 10;
1678
1679         if (hw->fc.current_mode & ixgbe_fc_tx_pause) {
1680                 fcrth |= IXGBE_FCRTH_FCEN;
1681                 if (hw->fc.send_xon)
1682                         fcrtl |= IXGBE_FCRTL_XONE;
1683         }
1684
1685         IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(packetbuf_num), fcrth);
1686         IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(packetbuf_num), fcrtl);
1687
1688         /* Configure pause time (2 TCs per register) */
1689         reg = IXGBE_READ_REG(hw, IXGBE_FCTTV(packetbuf_num / 2));
1690         if ((packetbuf_num & 1) == 0)
1691                 reg = (reg & 0xFFFF0000) | hw->fc.pause_time;
1692         else
1693                 reg = (reg & 0x0000FFFF) | (hw->fc.pause_time << 16);
1694         IXGBE_WRITE_REG(hw, IXGBE_FCTTV(packetbuf_num / 2), reg);
1695
1696         IXGBE_WRITE_REG(hw, IXGBE_FCRTV, (hw->fc.pause_time >> 1));
1697
1698 out:
1699         return ret_val;
1700 }
1701
1702 /**
1703  *  ixgbe_fc_autoneg - Configure flow control
1704  *  @hw: pointer to hardware structure
1705  *
1706  *  Compares our advertised flow control capabilities to those advertised by
1707  *  our link partner, and determines the proper flow control mode to use.
1708  **/
1709 s32 ixgbe_fc_autoneg(struct ixgbe_hw *hw)
1710 {
1711         s32 ret_val = 0;
1712         ixgbe_link_speed speed;
1713         u32 pcs_anadv_reg, pcs_lpab_reg, linkstat;
1714         u32 links2, anlp1_reg, autoc_reg, links;
1715         bool link_up;
1716
1717         /*
1718          * AN should have completed when the cable was plugged in.
1719          * Look for reasons to bail out.  Bail out if:
1720          * - FC autoneg is disabled, or if
1721          * - link is not up.
1722          *
1723          * Since we're being called from an LSC, link is already known to be up.
1724          * So use link_up_wait_to_complete=false.
1725          */
1726         hw->mac.ops.check_link(hw, &speed, &link_up, false);
1727
1728         if (hw->fc.disable_fc_autoneg || (!link_up)) {
1729                 hw->fc.fc_was_autonegged = false;
1730                 hw->fc.current_mode = hw->fc.requested_mode;
1731                 goto out;
1732         }
1733
1734         /*
1735          * On backplane, bail out if
1736          * - backplane autoneg was not completed, or if
1737          * - we are 82599 and link partner is not AN enabled
1738          */
1739         if (hw->phy.media_type == ixgbe_media_type_backplane) {
1740                 links = IXGBE_READ_REG(hw, IXGBE_LINKS);
1741                 if ((links & IXGBE_LINKS_KX_AN_COMP) == 0) {
1742                         hw->fc.fc_was_autonegged = false;
1743                         hw->fc.current_mode = hw->fc.requested_mode;
1744                         goto out;
1745                 }
1746
1747                 if (hw->mac.type == ixgbe_mac_82599EB) {
1748                         links2 = IXGBE_READ_REG(hw, IXGBE_LINKS2);
1749                         if ((links2 & IXGBE_LINKS2_AN_SUPPORTED) == 0) {
1750                                 hw->fc.fc_was_autonegged = false;
1751                                 hw->fc.current_mode = hw->fc.requested_mode;
1752                                 goto out;
1753                         }
1754                 }
1755         }
1756
1757         /*
1758          * On multispeed fiber at 1g, bail out if
1759          * - link is up but AN did not complete, or if
1760          * - link is up and AN completed but timed out
1761          */
1762         if (hw->phy.multispeed_fiber && (speed == IXGBE_LINK_SPEED_1GB_FULL)) {
1763                 linkstat = IXGBE_READ_REG(hw, IXGBE_PCS1GLSTA);
1764                 if (((linkstat & IXGBE_PCS1GLSTA_AN_COMPLETE) == 0) ||
1765                     ((linkstat & IXGBE_PCS1GLSTA_AN_TIMED_OUT) == 1)) {
1766                         hw->fc.fc_was_autonegged = false;
1767                         hw->fc.current_mode = hw->fc.requested_mode;
1768                         goto out;
1769                 }
1770         }
1771
1772         /*
1773          * Bail out on
1774          * - copper or CX4 adapters
1775          * - fiber adapters running at 10gig
1776          */
1777         if ((hw->phy.media_type == ixgbe_media_type_copper) ||
1778              (hw->phy.media_type == ixgbe_media_type_cx4) ||
1779              ((hw->phy.media_type == ixgbe_media_type_fiber) &&
1780              (speed == IXGBE_LINK_SPEED_10GB_FULL))) {
1781                 hw->fc.fc_was_autonegged = false;
1782                 hw->fc.current_mode = hw->fc.requested_mode;
1783                 goto out;
1784         }
1785
1786         /*
1787          * Read the AN advertisement and LP ability registers and resolve
1788          * local flow control settings accordingly
1789          */
1790         if ((speed == IXGBE_LINK_SPEED_1GB_FULL) &&
1791             (hw->phy.media_type != ixgbe_media_type_backplane)) {
1792                 pcs_anadv_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
1793                 pcs_lpab_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANLP);
1794                 if ((pcs_anadv_reg & IXGBE_PCS1GANA_SYM_PAUSE) &&
1795                     (pcs_lpab_reg & IXGBE_PCS1GANA_SYM_PAUSE)) {
1796                         /*
1797                          * Now we need to check if the user selected Rx ONLY
1798                          * of pause frames.  In this case, we had to advertise
1799                          * FULL flow control because we could not advertise RX
1800                          * ONLY. Hence, we must now check to see if we need to
1801                          * turn OFF the TRANSMISSION of PAUSE frames.
1802                          */
1803                         if (hw->fc.requested_mode == ixgbe_fc_full) {
1804                                 hw->fc.current_mode = ixgbe_fc_full;
1805                                 hw_dbg(hw, "Flow Control = FULL.\n");
1806                         } else {
1807                                 hw->fc.current_mode = ixgbe_fc_rx_pause;
1808                                 hw_dbg(hw, "Flow Control=RX PAUSE only\n");
1809                         }
1810                 } else if (!(pcs_anadv_reg & IXGBE_PCS1GANA_SYM_PAUSE) &&
1811                            (pcs_anadv_reg & IXGBE_PCS1GANA_ASM_PAUSE) &&
1812                            (pcs_lpab_reg & IXGBE_PCS1GANA_SYM_PAUSE) &&
1813                            (pcs_lpab_reg & IXGBE_PCS1GANA_ASM_PAUSE)) {
1814                         hw->fc.current_mode = ixgbe_fc_tx_pause;
1815                         hw_dbg(hw, "Flow Control = TX PAUSE frames only.\n");
1816                 } else if ((pcs_anadv_reg & IXGBE_PCS1GANA_SYM_PAUSE) &&
1817                            (pcs_anadv_reg & IXGBE_PCS1GANA_ASM_PAUSE) &&
1818                            !(pcs_lpab_reg & IXGBE_PCS1GANA_SYM_PAUSE) &&
1819                            (pcs_lpab_reg & IXGBE_PCS1GANA_ASM_PAUSE)) {
1820                         hw->fc.current_mode = ixgbe_fc_rx_pause;
1821                         hw_dbg(hw, "Flow Control = RX PAUSE frames only.\n");
1822                 } else {
1823                         hw->fc.current_mode = ixgbe_fc_none;
1824                         hw_dbg(hw, "Flow Control = NONE.\n");
1825                 }
1826         }
1827
1828         if (hw->phy.media_type == ixgbe_media_type_backplane) {
1829                 /*
1830                  * Read the 10g AN autoc and LP ability registers and resolve
1831                  * local flow control settings accordingly
1832                  */
1833                 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
1834                 anlp1_reg = IXGBE_READ_REG(hw, IXGBE_ANLP1);
1835
1836                 if ((autoc_reg & IXGBE_AUTOC_SYM_PAUSE) &&
1837                     (anlp1_reg & IXGBE_ANLP1_SYM_PAUSE)) {
1838                         /*
1839                          * Now we need to check if the user selected Rx ONLY
1840                          * of pause frames.  In this case, we had to advertise
1841                          * FULL flow control because we could not advertise RX
1842                          * ONLY. Hence, we must now check to see if we need to
1843                          * turn OFF the TRANSMISSION of PAUSE frames.
1844                          */
1845                         if (hw->fc.requested_mode == ixgbe_fc_full) {
1846                                 hw->fc.current_mode = ixgbe_fc_full;
1847                                 hw_dbg(hw, "Flow Control = FULL.\n");
1848                         } else {
1849                                 hw->fc.current_mode = ixgbe_fc_rx_pause;
1850                                 hw_dbg(hw, "Flow Control=RX PAUSE only\n");
1851                         }
1852                 } else if (!(autoc_reg & IXGBE_AUTOC_SYM_PAUSE) &&
1853                            (autoc_reg & IXGBE_AUTOC_ASM_PAUSE) &&
1854                            (anlp1_reg & IXGBE_ANLP1_SYM_PAUSE) &&
1855                            (anlp1_reg & IXGBE_ANLP1_ASM_PAUSE)) {
1856                         hw->fc.current_mode = ixgbe_fc_tx_pause;
1857                         hw_dbg(hw, "Flow Control = TX PAUSE frames only.\n");
1858                 } else if ((autoc_reg & IXGBE_AUTOC_SYM_PAUSE) &&
1859                            (autoc_reg & IXGBE_AUTOC_ASM_PAUSE) &&
1860                            !(anlp1_reg & IXGBE_ANLP1_SYM_PAUSE) &&
1861                            (anlp1_reg & IXGBE_ANLP1_ASM_PAUSE)) {
1862                         hw->fc.current_mode = ixgbe_fc_rx_pause;
1863                         hw_dbg(hw, "Flow Control = RX PAUSE frames only.\n");
1864                 } else {
1865                         hw->fc.current_mode = ixgbe_fc_none;
1866                         hw_dbg(hw, "Flow Control = NONE.\n");
1867                 }
1868         }
1869         /* Record that current_mode is the result of a successful autoneg */
1870         hw->fc.fc_was_autonegged = true;
1871
1872 out:
1873         return ret_val;
1874 }
1875
1876 /**
1877  *  ixgbe_setup_fc - Set up flow control
1878  *  @hw: pointer to hardware structure
1879  *
1880  *  Called at init time to set up flow control.
1881  **/
1882 static s32 ixgbe_setup_fc(struct ixgbe_hw *hw, s32 packetbuf_num)
1883 {
1884         s32 ret_val = 0;
1885         u32 reg;
1886
1887 #ifdef CONFIG_DCB
1888         if (hw->fc.requested_mode == ixgbe_fc_pfc) {
1889                 hw->fc.current_mode = hw->fc.requested_mode;
1890                 goto out;
1891         }
1892
1893 #endif
1894         /* Validate the packetbuf configuration */
1895         if (packetbuf_num < 0 || packetbuf_num > 7) {
1896                 hw_dbg(hw, "Invalid packet buffer number [%d], expected range "
1897                        "is 0-7\n", packetbuf_num);
1898                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
1899                 goto out;
1900         }
1901
1902         /*
1903          * Validate the water mark configuration.  Zero water marks are invalid
1904          * because it causes the controller to just blast out fc packets.
1905          */
1906         if (!hw->fc.low_water || !hw->fc.high_water || !hw->fc.pause_time) {
1907                 hw_dbg(hw, "Invalid water mark configuration\n");
1908                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
1909                 goto out;
1910         }
1911
1912         /*
1913          * Validate the requested mode.  Strict IEEE mode does not allow
1914          * ixgbe_fc_rx_pause because it will cause us to fail at UNH.
1915          */
1916         if (hw->fc.strict_ieee && hw->fc.requested_mode == ixgbe_fc_rx_pause) {
1917                 hw_dbg(hw, "ixgbe_fc_rx_pause not valid in strict "
1918                        "IEEE mode\n");
1919                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
1920                 goto out;
1921         }
1922
1923         /*
1924          * 10gig parts do not have a word in the EEPROM to determine the
1925          * default flow control setting, so we explicitly set it to full.
1926          */
1927         if (hw->fc.requested_mode == ixgbe_fc_default)
1928                 hw->fc.requested_mode = ixgbe_fc_full;
1929
1930         /*
1931          * Set up the 1G flow control advertisement registers so the HW will be
1932          * able to do fc autoneg once the cable is plugged in.  If we end up
1933          * using 10g instead, this is harmless.
1934          */
1935         reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
1936
1937         /*
1938          * The possible values of fc.requested_mode are:
1939          * 0: Flow control is completely disabled
1940          * 1: Rx flow control is enabled (we can receive pause frames,
1941          *    but not send pause frames).
1942          * 2: Tx flow control is enabled (we can send pause frames but
1943          *    we do not support receiving pause frames).
1944          * 3: Both Rx and Tx flow control (symmetric) are enabled.
1945 #ifdef CONFIG_DCB
1946          * 4: Priority Flow Control is enabled.
1947 #endif
1948          * other: Invalid.
1949          */
1950         switch (hw->fc.requested_mode) {
1951         case ixgbe_fc_none:
1952                 /* Flow control completely disabled by software override. */
1953                 reg &= ~(IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE);
1954                 break;
1955         case ixgbe_fc_rx_pause:
1956                 /*
1957                  * Rx Flow control is enabled and Tx Flow control is
1958                  * disabled by software override. Since there really
1959                  * isn't a way to advertise that we are capable of RX
1960                  * Pause ONLY, we will advertise that we support both
1961                  * symmetric and asymmetric Rx PAUSE.  Later, we will
1962                  * disable the adapter's ability to send PAUSE frames.
1963                  */
1964                 reg |= (IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE);
1965                 break;
1966         case ixgbe_fc_tx_pause:
1967                 /*
1968                  * Tx Flow control is enabled, and Rx Flow control is
1969                  * disabled by software override.
1970                  */
1971                 reg |= (IXGBE_PCS1GANA_ASM_PAUSE);
1972                 reg &= ~(IXGBE_PCS1GANA_SYM_PAUSE);
1973                 break;
1974         case ixgbe_fc_full:
1975                 /* Flow control (both Rx and Tx) is enabled by SW override. */
1976                 reg |= (IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE);
1977                 break;
1978 #ifdef CONFIG_DCB
1979         case ixgbe_fc_pfc:
1980                 goto out;
1981                 break;
1982 #endif /* CONFIG_DCB */
1983         default:
1984                 hw_dbg(hw, "Flow control param set incorrectly\n");
1985                 ret_val = IXGBE_ERR_CONFIG;
1986                 goto out;
1987                 break;
1988         }
1989
1990         IXGBE_WRITE_REG(hw, IXGBE_PCS1GANA, reg);
1991         reg = IXGBE_READ_REG(hw, IXGBE_PCS1GLCTL);
1992
1993         /* Disable AN timeout */
1994         if (hw->fc.strict_ieee)
1995                 reg &= ~IXGBE_PCS1GLCTL_AN_1G_TIMEOUT_EN;
1996
1997         IXGBE_WRITE_REG(hw, IXGBE_PCS1GLCTL, reg);
1998         hw_dbg(hw, "Set up FC; PCS1GLCTL = 0x%08X\n", reg);
1999
2000         /*
2001          * Set up the 10G flow control advertisement registers so the HW
2002          * can do fc autoneg once the cable is plugged in.  If we end up
2003          * using 1g instead, this is harmless.
2004          */
2005         reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2006
2007         /*
2008          * The possible values of fc.requested_mode are:
2009          * 0: Flow control is completely disabled
2010          * 1: Rx flow control is enabled (we can receive pause frames,
2011          *    but not send pause frames).
2012          * 2: Tx flow control is enabled (we can send pause frames but
2013          *    we do not support receiving pause frames).
2014          * 3: Both Rx and Tx flow control (symmetric) are enabled.
2015          * other: Invalid.
2016          */
2017         switch (hw->fc.requested_mode) {
2018         case ixgbe_fc_none:
2019                 /* Flow control completely disabled by software override. */
2020                 reg &= ~(IXGBE_AUTOC_SYM_PAUSE | IXGBE_AUTOC_ASM_PAUSE);
2021                 break;
2022         case ixgbe_fc_rx_pause:
2023                 /*
2024                  * Rx Flow control is enabled and Tx Flow control is
2025                  * disabled by software override. Since there really
2026                  * isn't a way to advertise that we are capable of RX
2027                  * Pause ONLY, we will advertise that we support both
2028                  * symmetric and asymmetric Rx PAUSE.  Later, we will
2029                  * disable the adapter's ability to send PAUSE frames.
2030                  */
2031                 reg |= (IXGBE_AUTOC_SYM_PAUSE | IXGBE_AUTOC_ASM_PAUSE);
2032                 break;
2033         case ixgbe_fc_tx_pause:
2034                 /*
2035                  * Tx Flow control is enabled, and Rx Flow control is
2036                  * disabled by software override.
2037                  */
2038                 reg |= (IXGBE_AUTOC_ASM_PAUSE);
2039                 reg &= ~(IXGBE_AUTOC_SYM_PAUSE);
2040                 break;
2041         case ixgbe_fc_full:
2042                 /* Flow control (both Rx and Tx) is enabled by SW override. */
2043                 reg |= (IXGBE_AUTOC_SYM_PAUSE | IXGBE_AUTOC_ASM_PAUSE);
2044                 break;
2045 #ifdef CONFIG_DCB
2046         case ixgbe_fc_pfc:
2047                 goto out;
2048                 break;
2049 #endif /* CONFIG_DCB */
2050         default:
2051                 hw_dbg(hw, "Flow control param set incorrectly\n");
2052                 ret_val = IXGBE_ERR_CONFIG;
2053                 goto out;
2054                 break;
2055         }
2056         /*
2057          * AUTOC restart handles negotiation of 1G and 10G. There is
2058          * no need to set the PCS1GCTL register.
2059          */
2060         reg |= IXGBE_AUTOC_AN_RESTART;
2061         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, reg);
2062         hw_dbg(hw, "Set up FC; IXGBE_AUTOC = 0x%08X\n", reg);
2063
2064 out:
2065         return ret_val;
2066 }
2067
2068 /**
2069  *  ixgbe_disable_pcie_master - Disable PCI-express master access
2070  *  @hw: pointer to hardware structure
2071  *
2072  *  Disables PCI-Express master access and verifies there are no pending
2073  *  requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
2074  *  bit hasn't caused the master requests to be disabled, else 0
2075  *  is returned signifying master requests disabled.
2076  **/
2077 s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
2078 {
2079         u32 i;
2080         u32 reg_val;
2081         u32 number_of_queues;
2082         s32 status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
2083
2084         /* Disable the receive unit by stopping each queue */
2085         number_of_queues = hw->mac.max_rx_queues;
2086         for (i = 0; i < number_of_queues; i++) {
2087                 reg_val = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
2088                 if (reg_val & IXGBE_RXDCTL_ENABLE) {
2089                         reg_val &= ~IXGBE_RXDCTL_ENABLE;
2090                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), reg_val);
2091                 }
2092         }
2093
2094         reg_val = IXGBE_READ_REG(hw, IXGBE_CTRL);
2095         reg_val |= IXGBE_CTRL_GIO_DIS;
2096         IXGBE_WRITE_REG(hw, IXGBE_CTRL, reg_val);
2097
2098         for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
2099                 if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO)) {
2100                         status = 0;
2101                         break;
2102                 }
2103                 udelay(100);
2104         }
2105
2106         return status;
2107 }
2108
2109
2110 /**
2111  *  ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
2112  *  @hw: pointer to hardware structure
2113  *  @mask: Mask to specify which semaphore to acquire
2114  *
2115  *  Acquires the SWFW semaphore thought the GSSR register for the specified
2116  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
2117  **/
2118 s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u16 mask)
2119 {
2120         u32 gssr;
2121         u32 swmask = mask;
2122         u32 fwmask = mask << 5;
2123         s32 timeout = 200;
2124
2125         while (timeout) {
2126                 if (ixgbe_get_eeprom_semaphore(hw))
2127                         return IXGBE_ERR_SWFW_SYNC;
2128
2129                 gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
2130                 if (!(gssr & (fwmask | swmask)))
2131                         break;
2132
2133                 /*
2134                  * Firmware currently using resource (fwmask) or other software
2135                  * thread currently using resource (swmask)
2136                  */
2137                 ixgbe_release_eeprom_semaphore(hw);
2138                 msleep(5);
2139                 timeout--;
2140         }
2141
2142         if (!timeout) {
2143                 hw_dbg(hw, "Driver can't access resource, GSSR timeout.\n");
2144                 return IXGBE_ERR_SWFW_SYNC;
2145         }
2146
2147         gssr |= swmask;
2148         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
2149
2150         ixgbe_release_eeprom_semaphore(hw);
2151         return 0;
2152 }
2153
2154 /**
2155  *  ixgbe_release_swfw_sync - Release SWFW semaphore
2156  *  @hw: pointer to hardware structure
2157  *  @mask: Mask to specify which semaphore to release
2158  *
2159  *  Releases the SWFW semaphore thought the GSSR register for the specified
2160  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
2161  **/
2162 void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u16 mask)
2163 {
2164         u32 gssr;
2165         u32 swmask = mask;
2166
2167         ixgbe_get_eeprom_semaphore(hw);
2168
2169         gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
2170         gssr &= ~swmask;
2171         IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
2172
2173         ixgbe_release_eeprom_semaphore(hw);
2174 }
2175
2176 /**
2177  *  ixgbe_enable_rx_dma_generic - Enable the Rx DMA unit
2178  *  @hw: pointer to hardware structure
2179  *  @regval: register value to write to RXCTRL
2180  *
2181  *  Enables the Rx DMA unit
2182  **/
2183 s32 ixgbe_enable_rx_dma_generic(struct ixgbe_hw *hw, u32 regval)
2184 {
2185         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, regval);
2186
2187         return 0;
2188 }
2189
2190 /**
2191  *  ixgbe_blink_led_start_generic - Blink LED based on index.
2192  *  @hw: pointer to hardware structure
2193  *  @index: led number to blink
2194  **/
2195 s32 ixgbe_blink_led_start_generic(struct ixgbe_hw *hw, u32 index)
2196 {
2197         ixgbe_link_speed speed = 0;
2198         bool link_up = 0;
2199         u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2200         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
2201
2202         /*
2203          * Link must be up to auto-blink the LEDs;
2204          * Force it if link is down.
2205          */
2206         hw->mac.ops.check_link(hw, &speed, &link_up, false);
2207
2208         if (!link_up) {
2209                 autoc_reg |= IXGBE_AUTOC_AN_RESTART;
2210                 autoc_reg |= IXGBE_AUTOC_FLU;
2211                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
2212                 msleep(10);
2213         }
2214
2215         led_reg &= ~IXGBE_LED_MODE_MASK(index);
2216         led_reg |= IXGBE_LED_BLINK(index);
2217         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
2218         IXGBE_WRITE_FLUSH(hw);
2219
2220         return 0;
2221 }
2222
2223 /**
2224  *  ixgbe_blink_led_stop_generic - Stop blinking LED based on index.
2225  *  @hw: pointer to hardware structure
2226  *  @index: led number to stop blinking
2227  **/
2228 s32 ixgbe_blink_led_stop_generic(struct ixgbe_hw *hw, u32 index)
2229 {
2230         u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2231         u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
2232
2233         autoc_reg &= ~IXGBE_AUTOC_FLU;
2234         autoc_reg |= IXGBE_AUTOC_AN_RESTART;
2235         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
2236
2237         led_reg &= ~IXGBE_LED_MODE_MASK(index);
2238         led_reg &= ~IXGBE_LED_BLINK(index);
2239         led_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
2240         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
2241         IXGBE_WRITE_FLUSH(hw);
2242
2243         return 0;
2244 }
2245
2246 /**
2247  *  ixgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
2248  *  @hw: pointer to hardware structure
2249  *  @san_mac_offset: SAN MAC address offset
2250  *
2251  *  This function will read the EEPROM location for the SAN MAC address
2252  *  pointer, and returns the value at that location.  This is used in both
2253  *  get and set mac_addr routines.
2254  **/
2255 static s32 ixgbe_get_san_mac_addr_offset(struct ixgbe_hw *hw,
2256                                         u16 *san_mac_offset)
2257 {
2258         /*
2259          * First read the EEPROM pointer to see if the MAC addresses are
2260          * available.
2261          */
2262         hw->eeprom.ops.read(hw, IXGBE_SAN_MAC_ADDR_PTR, san_mac_offset);
2263
2264         return 0;
2265 }
2266
2267 /**
2268  *  ixgbe_get_san_mac_addr_generic - SAN MAC address retrieval from the EEPROM
2269  *  @hw: pointer to hardware structure
2270  *  @san_mac_addr: SAN MAC address
2271  *
2272  *  Reads the SAN MAC address from the EEPROM, if it's available.  This is
2273  *  per-port, so set_lan_id() must be called before reading the addresses.
2274  *  set_lan_id() is called by identify_sfp(), but this cannot be relied
2275  *  upon for non-SFP connections, so we must call it here.
2276  **/
2277 s32 ixgbe_get_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
2278 {
2279         u16 san_mac_data, san_mac_offset;
2280         u8 i;
2281
2282         /*
2283          * First read the EEPROM pointer to see if the MAC addresses are
2284          * available.  If they're not, no point in calling set_lan_id() here.
2285          */
2286         ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
2287
2288         if ((san_mac_offset == 0) || (san_mac_offset == 0xFFFF)) {
2289                 /*
2290                  * No addresses available in this EEPROM.  It's not an
2291                  * error though, so just wipe the local address and return.
2292                  */
2293                 for (i = 0; i < 6; i++)
2294                         san_mac_addr[i] = 0xFF;
2295
2296                 goto san_mac_addr_out;
2297         }
2298
2299         /* make sure we know which port we need to program */
2300         hw->mac.ops.set_lan_id(hw);
2301         /* apply the port offset to the address offset */
2302         (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
2303                          (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
2304         for (i = 0; i < 3; i++) {
2305                 hw->eeprom.ops.read(hw, san_mac_offset, &san_mac_data);
2306                 san_mac_addr[i * 2] = (u8)(san_mac_data);
2307                 san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
2308                 san_mac_offset++;
2309         }
2310
2311 san_mac_addr_out:
2312         return 0;
2313 }
2314
2315 /**
2316  *  ixgbe_get_pcie_msix_count_generic - Gets MSI-X vector count
2317  *  @hw: pointer to hardware structure
2318  *
2319  *  Read PCIe configuration space, and get the MSI-X vector count from
2320  *  the capabilities table.
2321  **/
2322 u32 ixgbe_get_pcie_msix_count_generic(struct ixgbe_hw *hw)
2323 {
2324         struct ixgbe_adapter *adapter = hw->back;
2325         u16 msix_count;
2326         pci_read_config_word(adapter->pdev, IXGBE_PCIE_MSIX_82599_CAPS,
2327                              &msix_count);
2328         msix_count &= IXGBE_PCIE_MSIX_TBL_SZ_MASK;
2329
2330         /* MSI-X count is zero-based in HW, so increment to give proper value */
2331         msix_count++;
2332
2333         return msix_count;
2334 }
2335
2336 /**
2337  *  ixgbe_clear_vmdq_generic - Disassociate a VMDq pool index from a rx address
2338  *  @hw: pointer to hardware struct
2339  *  @rar: receive address register index to disassociate
2340  *  @vmdq: VMDq pool index to remove from the rar
2341  **/
2342 s32 ixgbe_clear_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
2343 {
2344         u32 mpsar_lo, mpsar_hi;
2345         u32 rar_entries = hw->mac.num_rar_entries;
2346
2347         if (rar < rar_entries) {
2348                 mpsar_lo = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
2349                 mpsar_hi = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
2350
2351                 if (!mpsar_lo && !mpsar_hi)
2352                         goto done;
2353
2354                 if (vmdq == IXGBE_CLEAR_VMDQ_ALL) {
2355                         if (mpsar_lo) {
2356                                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
2357                                 mpsar_lo = 0;
2358                         }
2359                         if (mpsar_hi) {
2360                                 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
2361                                 mpsar_hi = 0;
2362                         }
2363                 } else if (vmdq < 32) {
2364                         mpsar_lo &= ~(1 << vmdq);
2365                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar_lo);
2366                 } else {
2367                         mpsar_hi &= ~(1 << (vmdq - 32));
2368                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar_hi);
2369                 }
2370
2371                 /* was that the last pool using this rar? */
2372                 if (mpsar_lo == 0 && mpsar_hi == 0 && rar != 0)
2373                         hw->mac.ops.clear_rar(hw, rar);
2374         } else {
2375                 hw_dbg(hw, "RAR index %d is out of range.\n", rar);
2376         }
2377
2378 done:
2379         return 0;
2380 }
2381
2382 /**
2383  *  ixgbe_set_vmdq_generic - Associate a VMDq pool index with a rx address
2384  *  @hw: pointer to hardware struct
2385  *  @rar: receive address register index to associate with a VMDq index
2386  *  @vmdq: VMDq pool index
2387  **/
2388 s32 ixgbe_set_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
2389 {
2390         u32 mpsar;
2391         u32 rar_entries = hw->mac.num_rar_entries;
2392
2393         if (rar < rar_entries) {
2394                 if (vmdq < 32) {
2395                         mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
2396                         mpsar |= 1 << vmdq;
2397                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar);
2398                 } else {
2399                         mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
2400                         mpsar |= 1 << (vmdq - 32);
2401                         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar);
2402                 }
2403         } else {
2404                 hw_dbg(hw, "RAR index %d is out of range.\n", rar);
2405         }
2406         return 0;
2407 }
2408
2409 /**
2410  *  ixgbe_init_uta_tables_generic - Initialize the Unicast Table Array
2411  *  @hw: pointer to hardware structure
2412  **/
2413 s32 ixgbe_init_uta_tables_generic(struct ixgbe_hw *hw)
2414 {
2415         int i;
2416
2417
2418         for (i = 0; i < 128; i++)
2419                 IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
2420
2421         return 0;
2422 }
2423
2424 /**
2425  *  ixgbe_find_vlvf_slot - find the vlanid or the first empty slot
2426  *  @hw: pointer to hardware structure
2427  *  @vlan: VLAN id to write to VLAN filter
2428  *
2429  *  return the VLVF index where this VLAN id should be placed
2430  *
2431  **/
2432 static s32 ixgbe_find_vlvf_slot(struct ixgbe_hw *hw, u32 vlan)
2433 {
2434         u32 bits = 0;
2435         u32 first_empty_slot = 0;
2436         s32 regindex;
2437
2438         /* short cut the special case */
2439         if (vlan == 0)
2440                 return 0;
2441
2442         /*
2443           * Search for the vlan id in the VLVF entries. Save off the first empty
2444           * slot found along the way
2445           */
2446         for (regindex = 1; regindex < IXGBE_VLVF_ENTRIES; regindex++) {
2447                 bits = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
2448                 if (!bits && !(first_empty_slot))
2449                         first_empty_slot = regindex;
2450                 else if ((bits & 0x0FFF) == vlan)
2451                         break;
2452         }
2453
2454         /*
2455           * If regindex is less than IXGBE_VLVF_ENTRIES, then we found the vlan
2456           * in the VLVF. Else use the first empty VLVF register for this
2457           * vlan id.
2458           */
2459         if (regindex >= IXGBE_VLVF_ENTRIES) {
2460                 if (first_empty_slot)
2461                         regindex = first_empty_slot;
2462                 else {
2463                         hw_dbg(hw, "No space in VLVF.\n");
2464                         regindex = IXGBE_ERR_NO_SPACE;
2465                 }
2466         }
2467
2468         return regindex;
2469 }
2470
2471 /**
2472  *  ixgbe_set_vfta_generic - Set VLAN filter table
2473  *  @hw: pointer to hardware structure
2474  *  @vlan: VLAN id to write to VLAN filter
2475  *  @vind: VMDq output index that maps queue to VLAN id in VFVFB
2476  *  @vlan_on: boolean flag to turn on/off VLAN in VFVF
2477  *
2478  *  Turn on/off specified VLAN in the VLAN filter table.
2479  **/
2480 s32 ixgbe_set_vfta_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
2481                            bool vlan_on)
2482 {
2483         s32 regindex;
2484         u32 bitindex;
2485         u32 vfta;
2486         u32 bits;
2487         u32 vt;
2488         u32 targetbit;
2489         bool vfta_changed = false;
2490
2491         if (vlan > 4095)
2492                 return IXGBE_ERR_PARAM;
2493
2494         /*
2495          * this is a 2 part operation - first the VFTA, then the
2496          * VLVF and VLVFB if VT Mode is set
2497          * We don't write the VFTA until we know the VLVF part succeeded.
2498          */
2499
2500         /* Part 1
2501          * The VFTA is a bitstring made up of 128 32-bit registers
2502          * that enable the particular VLAN id, much like the MTA:
2503          *    bits[11-5]: which register
2504          *    bits[4-0]:  which bit in the register
2505          */
2506         regindex = (vlan >> 5) & 0x7F;
2507         bitindex = vlan & 0x1F;
2508         targetbit = (1 << bitindex);
2509         vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(regindex));
2510
2511         if (vlan_on) {
2512                 if (!(vfta & targetbit)) {
2513                         vfta |= targetbit;
2514                         vfta_changed = true;
2515                 }
2516         } else {
2517                 if ((vfta & targetbit)) {
2518                         vfta &= ~targetbit;
2519                         vfta_changed = true;
2520                 }
2521         }
2522
2523         /* Part 2
2524          * If VT Mode is set
2525          *   Either vlan_on
2526          *     make sure the vlan is in VLVF
2527          *     set the vind bit in the matching VLVFB
2528          *   Or !vlan_on
2529          *     clear the pool bit and possibly the vind
2530          */
2531         vt = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
2532         if (vt & IXGBE_VT_CTL_VT_ENABLE) {
2533                 s32 vlvf_index;
2534
2535                 vlvf_index = ixgbe_find_vlvf_slot(hw, vlan);
2536                 if (vlvf_index < 0)
2537                         return vlvf_index;
2538
2539                 if (vlan_on) {
2540                         /* set the pool bit */
2541                         if (vind < 32) {
2542                                 bits = IXGBE_READ_REG(hw,
2543                                                 IXGBE_VLVFB(vlvf_index*2));
2544                                 bits |= (1 << vind);
2545                                 IXGBE_WRITE_REG(hw,
2546                                                 IXGBE_VLVFB(vlvf_index*2),
2547                                                 bits);
2548                         } else {
2549                                 bits = IXGBE_READ_REG(hw,
2550                                                 IXGBE_VLVFB((vlvf_index*2)+1));
2551                                 bits |= (1 << (vind-32));
2552                                 IXGBE_WRITE_REG(hw,
2553                                                 IXGBE_VLVFB((vlvf_index*2)+1),
2554                                                 bits);
2555                         }
2556                 } else {
2557                         /* clear the pool bit */
2558                         if (vind < 32) {
2559                                 bits = IXGBE_READ_REG(hw,
2560                                                 IXGBE_VLVFB(vlvf_index*2));
2561                                 bits &= ~(1 << vind);
2562                                 IXGBE_WRITE_REG(hw,
2563                                                 IXGBE_VLVFB(vlvf_index*2),
2564                                                 bits);
2565                                 bits |= IXGBE_READ_REG(hw,
2566                                                 IXGBE_VLVFB((vlvf_index*2)+1));
2567                         } else {
2568                                 bits = IXGBE_READ_REG(hw,
2569                                                 IXGBE_VLVFB((vlvf_index*2)+1));
2570                                 bits &= ~(1 << (vind-32));
2571                                 IXGBE_WRITE_REG(hw,
2572                                                 IXGBE_VLVFB((vlvf_index*2)+1),
2573                                                 bits);
2574                                 bits |= IXGBE_READ_REG(hw,
2575                                                 IXGBE_VLVFB(vlvf_index*2));
2576                         }
2577                 }
2578
2579                 /*
2580                  * If there are still bits set in the VLVFB registers
2581                  * for the VLAN ID indicated we need to see if the
2582                  * caller is requesting that we clear the VFTA entry bit.
2583                  * If the caller has requested that we clear the VFTA
2584                  * entry bit but there are still pools/VFs using this VLAN
2585                  * ID entry then ignore the request.  We're not worried
2586                  * about the case where we're turning the VFTA VLAN ID
2587                  * entry bit on, only when requested to turn it off as
2588                  * there may be multiple pools and/or VFs using the
2589                  * VLAN ID entry.  In that case we cannot clear the
2590                  * VFTA bit until all pools/VFs using that VLAN ID have also
2591                  * been cleared.  This will be indicated by "bits" being
2592                  * zero.
2593                  */
2594                 if (bits) {
2595                         IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index),
2596                                         (IXGBE_VLVF_VIEN | vlan));
2597                         if (!vlan_on) {
2598                                 /* someone wants to clear the vfta entry
2599                                  * but some pools/VFs are still using it.
2600                                  * Ignore it. */
2601                                 vfta_changed = false;
2602                         }
2603                 }
2604                 else
2605                         IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), 0);
2606         }
2607
2608         if (vfta_changed)
2609                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(regindex), vfta);
2610
2611         return 0;
2612 }
2613
2614 /**
2615  *  ixgbe_clear_vfta_generic - Clear VLAN filter table
2616  *  @hw: pointer to hardware structure
2617  *
2618  *  Clears the VLAN filer table, and the VMDq index associated with the filter
2619  **/
2620 s32 ixgbe_clear_vfta_generic(struct ixgbe_hw *hw)
2621 {
2622         u32 offset;
2623
2624         for (offset = 0; offset < hw->mac.vft_size; offset++)
2625                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(offset), 0);
2626
2627         for (offset = 0; offset < IXGBE_VLVF_ENTRIES; offset++) {
2628                 IXGBE_WRITE_REG(hw, IXGBE_VLVF(offset), 0);
2629                 IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset*2), 0);
2630                 IXGBE_WRITE_REG(hw, IXGBE_VLVFB((offset*2)+1), 0);
2631         }
2632
2633         return 0;
2634 }
2635
2636 /**
2637  *  ixgbe_check_mac_link_generic - Determine link and speed status
2638  *  @hw: pointer to hardware structure
2639  *  @speed: pointer to link speed
2640  *  @link_up: true when link is up
2641  *  @link_up_wait_to_complete: bool used to wait for link up or not
2642  *
2643  *  Reads the links register to determine if link is up and the current speed
2644  **/
2645 s32 ixgbe_check_mac_link_generic(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
2646                                bool *link_up, bool link_up_wait_to_complete)
2647 {
2648         u32 links_reg;
2649         u32 i;
2650
2651         links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
2652         if (link_up_wait_to_complete) {
2653                 for (i = 0; i < IXGBE_LINK_UP_TIME; i++) {
2654                         if (links_reg & IXGBE_LINKS_UP) {
2655                                 *link_up = true;
2656                                 break;
2657                         } else {
2658                                 *link_up = false;
2659                         }
2660                         msleep(100);
2661                         links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
2662                 }
2663         } else {
2664                 if (links_reg & IXGBE_LINKS_UP)
2665                         *link_up = true;
2666                 else
2667                         *link_up = false;
2668         }
2669
2670         if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
2671             IXGBE_LINKS_SPEED_10G_82599)
2672                 *speed = IXGBE_LINK_SPEED_10GB_FULL;
2673         else if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
2674                  IXGBE_LINKS_SPEED_1G_82599)
2675                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
2676         else
2677                 *speed = IXGBE_LINK_SPEED_100_FULL;
2678
2679         /* if link is down, zero out the current_mode */
2680         if (*link_up == false) {
2681                 hw->fc.current_mode = ixgbe_fc_none;
2682                 hw->fc.fc_was_autonegged = false;
2683         }
2684
2685         return 0;
2686 }
2687
2688 /**
2689  *  ixgbe_get_wwn_prefix_generic Get alternative WWNN/WWPN prefix from
2690  *  the EEPROM
2691  *  @hw: pointer to hardware structure
2692  *  @wwnn_prefix: the alternative WWNN prefix
2693  *  @wwpn_prefix: the alternative WWPN prefix
2694  *
2695  *  This function will read the EEPROM from the alternative SAN MAC address
2696  *  block to check the support for the alternative WWNN/WWPN prefix support.
2697  **/
2698 s32 ixgbe_get_wwn_prefix_generic(struct ixgbe_hw *hw, u16 *wwnn_prefix,
2699                                         u16 *wwpn_prefix)
2700 {
2701         u16 offset, caps;
2702         u16 alt_san_mac_blk_offset;
2703
2704         /* clear output first */
2705         *wwnn_prefix = 0xFFFF;
2706         *wwpn_prefix = 0xFFFF;
2707
2708         /* check if alternative SAN MAC is supported */
2709         hw->eeprom.ops.read(hw, IXGBE_ALT_SAN_MAC_ADDR_BLK_PTR,
2710                             &alt_san_mac_blk_offset);
2711
2712         if ((alt_san_mac_blk_offset == 0) ||
2713             (alt_san_mac_blk_offset == 0xFFFF))
2714                 goto wwn_prefix_out;
2715
2716         /* check capability in alternative san mac address block */
2717         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_CAPS_OFFSET;
2718         hw->eeprom.ops.read(hw, offset, &caps);
2719         if (!(caps & IXGBE_ALT_SAN_MAC_ADDR_CAPS_ALTWWN))
2720                 goto wwn_prefix_out;
2721
2722         /* get the corresponding prefix for WWNN/WWPN */
2723         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWNN_OFFSET;
2724         hw->eeprom.ops.read(hw, offset, wwnn_prefix);
2725
2726         offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWPN_OFFSET;
2727         hw->eeprom.ops.read(hw, offset, wwpn_prefix);
2728
2729 wwn_prefix_out:
2730         return 0;
2731 }