igb: change how we handle alternate mac addresses
[cascardo/linux.git] / drivers / net / igb / e1000_mac.c
1 /*******************************************************************************
2
3   Intel(R) Gigabit Ethernet Linux driver
4   Copyright(c) 2007-2009 Intel Corporation.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26 *******************************************************************************/
27
28 #include <linux/if_ether.h>
29 #include <linux/delay.h>
30 #include <linux/pci.h>
31 #include <linux/netdevice.h>
32
33 #include "e1000_mac.h"
34
35 #include "igb.h"
36
37 static s32 igb_set_default_fc(struct e1000_hw *hw);
38 static s32 igb_set_fc_watermarks(struct e1000_hw *hw);
39
40 /**
41  *  igb_get_bus_info_pcie - Get PCIe bus information
42  *  @hw: pointer to the HW structure
43  *
44  *  Determines and stores the system bus information for a particular
45  *  network interface.  The following bus information is determined and stored:
46  *  bus speed, bus width, type (PCIe), and PCIe function.
47  **/
48 s32 igb_get_bus_info_pcie(struct e1000_hw *hw)
49 {
50         struct e1000_bus_info *bus = &hw->bus;
51         s32 ret_val;
52         u32 reg;
53         u16 pcie_link_status;
54
55         bus->type = e1000_bus_type_pci_express;
56         bus->speed = e1000_bus_speed_2500;
57
58         ret_val = igb_read_pcie_cap_reg(hw,
59                                           PCIE_LINK_STATUS,
60                                           &pcie_link_status);
61         if (ret_val)
62                 bus->width = e1000_bus_width_unknown;
63         else
64                 bus->width = (enum e1000_bus_width)((pcie_link_status &
65                                                      PCIE_LINK_WIDTH_MASK) >>
66                                                      PCIE_LINK_WIDTH_SHIFT);
67
68         reg = rd32(E1000_STATUS);
69         bus->func = (reg & E1000_STATUS_FUNC_MASK) >> E1000_STATUS_FUNC_SHIFT;
70
71         return 0;
72 }
73
74 /**
75  *  igb_clear_vfta - Clear VLAN filter table
76  *  @hw: pointer to the HW structure
77  *
78  *  Clears the register array which contains the VLAN filter table by
79  *  setting all the values to 0.
80  **/
81 void igb_clear_vfta(struct e1000_hw *hw)
82 {
83         u32 offset;
84
85         for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
86                 array_wr32(E1000_VFTA, offset, 0);
87                 wrfl();
88         }
89 }
90
91 /**
92  *  igb_write_vfta - Write value to VLAN filter table
93  *  @hw: pointer to the HW structure
94  *  @offset: register offset in VLAN filter table
95  *  @value: register value written to VLAN filter table
96  *
97  *  Writes value at the given offset in the register array which stores
98  *  the VLAN filter table.
99  **/
100 static void igb_write_vfta(struct e1000_hw *hw, u32 offset, u32 value)
101 {
102         array_wr32(E1000_VFTA, offset, value);
103         wrfl();
104 }
105
106 /**
107  *  igb_init_rx_addrs - Initialize receive address's
108  *  @hw: pointer to the HW structure
109  *  @rar_count: receive address registers
110  *
111  *  Setups the receive address registers by setting the base receive address
112  *  register to the devices MAC address and clearing all the other receive
113  *  address registers to 0.
114  **/
115 void igb_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
116 {
117         u32 i;
118         u8 mac_addr[ETH_ALEN] = {0};
119
120         /* Setup the receive address */
121         hw_dbg("Programming MAC Address into RAR[0]\n");
122
123         hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
124
125         /* Zero out the other (rar_entry_count - 1) receive addresses */
126         hw_dbg("Clearing RAR[1-%u]\n", rar_count-1);
127         for (i = 1; i < rar_count; i++)
128                 hw->mac.ops.rar_set(hw, mac_addr, i);
129 }
130
131 /**
132  *  igb_vfta_set - enable or disable vlan in VLAN filter table
133  *  @hw: pointer to the HW structure
134  *  @vid: VLAN id to add or remove
135  *  @add: if true add filter, if false remove
136  *
137  *  Sets or clears a bit in the VLAN filter table array based on VLAN id
138  *  and if we are adding or removing the filter
139  **/
140 s32 igb_vfta_set(struct e1000_hw *hw, u32 vid, bool add)
141 {
142         u32 index = (vid >> E1000_VFTA_ENTRY_SHIFT) & E1000_VFTA_ENTRY_MASK;
143         u32 mask = 1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK);
144         u32 vfta = array_rd32(E1000_VFTA, index);
145         s32 ret_val = 0;
146
147         /* bit was set/cleared before we started */
148         if ((!!(vfta & mask)) == add) {
149                 ret_val = -E1000_ERR_CONFIG;
150         } else {
151                 if (add)
152                         vfta |= mask;
153                 else
154                         vfta &= ~mask;
155         }
156
157         igb_write_vfta(hw, index, vfta);
158
159         return ret_val;
160 }
161
162 /**
163  *  igb_check_alt_mac_addr - Check for alternate MAC addr
164  *  @hw: pointer to the HW structure
165  *
166  *  Checks the nvm for an alternate MAC address.  An alternate MAC address
167  *  can be setup by pre-boot software and must be treated like a permanent
168  *  address and must override the actual permanent MAC address.  If an
169  *  alternate MAC address is fopund it is saved in the hw struct and
170  *  prgrammed into RAR0 and the cuntion returns success, otherwise the
171  *  fucntion returns an error.
172  **/
173 s32 igb_check_alt_mac_addr(struct e1000_hw *hw)
174 {
175         u32 i;
176         s32 ret_val = 0;
177         u16 offset, nvm_alt_mac_addr_offset, nvm_data;
178         u8 alt_mac_addr[ETH_ALEN];
179
180         ret_val = hw->nvm.ops.read(hw, NVM_ALT_MAC_ADDR_PTR, 1,
181                                  &nvm_alt_mac_addr_offset);
182         if (ret_val) {
183                 hw_dbg("NVM Read Error\n");
184                 goto out;
185         }
186
187         if (nvm_alt_mac_addr_offset == 0xFFFF) {
188                 /* There is no Alternate MAC Address */
189                 goto out;
190         }
191
192         if (hw->bus.func == E1000_FUNC_1)
193                 nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN1;
194         for (i = 0; i < ETH_ALEN; i += 2) {
195                 offset = nvm_alt_mac_addr_offset + (i >> 1);
196                 ret_val = hw->nvm.ops.read(hw, offset, 1, &nvm_data);
197                 if (ret_val) {
198                         hw_dbg("NVM Read Error\n");
199                         goto out;
200                 }
201
202                 alt_mac_addr[i] = (u8)(nvm_data & 0xFF);
203                 alt_mac_addr[i + 1] = (u8)(nvm_data >> 8);
204         }
205
206         /* if multicast bit is set, the alternate address will not be used */
207         if (alt_mac_addr[0] & 0x01) {
208                 hw_dbg("Ignoring Alternate Mac Address with MC bit set\n");
209                 goto out;
210         }
211
212         /*
213          * We have a valid alternate MAC address, and we want to treat it the
214          * same as the normal permanent MAC address stored by the HW into the
215          * RAR. Do this by mapping this address into RAR0.
216          */
217         hw->mac.ops.rar_set(hw, alt_mac_addr, 0);
218
219 out:
220         return ret_val;
221 }
222
223 /**
224  *  igb_rar_set - Set receive address register
225  *  @hw: pointer to the HW structure
226  *  @addr: pointer to the receive address
227  *  @index: receive address array register
228  *
229  *  Sets the receive address array register at index to the address passed
230  *  in by addr.
231  **/
232 void igb_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
233 {
234         u32 rar_low, rar_high;
235
236         /*
237          * HW expects these in little endian so we reverse the byte order
238          * from network order (big endian) to little endian
239          */
240         rar_low = ((u32) addr[0] |
241                    ((u32) addr[1] << 8) |
242                     ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
243
244         rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
245
246         /* If MAC address zero, no need to set the AV bit */
247         if (rar_low || rar_high)
248                 rar_high |= E1000_RAH_AV;
249
250         wr32(E1000_RAL(index), rar_low);
251         wr32(E1000_RAH(index), rar_high);
252 }
253
254 /**
255  *  igb_mta_set - Set multicast filter table address
256  *  @hw: pointer to the HW structure
257  *  @hash_value: determines the MTA register and bit to set
258  *
259  *  The multicast table address is a register array of 32-bit registers.
260  *  The hash_value is used to determine what register the bit is in, the
261  *  current value is read, the new bit is OR'd in and the new value is
262  *  written back into the register.
263  **/
264 void igb_mta_set(struct e1000_hw *hw, u32 hash_value)
265 {
266         u32 hash_bit, hash_reg, mta;
267
268         /*
269          * The MTA is a register array of 32-bit registers. It is
270          * treated like an array of (32*mta_reg_count) bits.  We want to
271          * set bit BitArray[hash_value]. So we figure out what register
272          * the bit is in, read it, OR in the new bit, then write
273          * back the new value.  The (hw->mac.mta_reg_count - 1) serves as a
274          * mask to bits 31:5 of the hash value which gives us the
275          * register we're modifying.  The hash bit within that register
276          * is determined by the lower 5 bits of the hash value.
277          */
278         hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
279         hash_bit = hash_value & 0x1F;
280
281         mta = array_rd32(E1000_MTA, hash_reg);
282
283         mta |= (1 << hash_bit);
284
285         array_wr32(E1000_MTA, hash_reg, mta);
286         wrfl();
287 }
288
289 /**
290  *  igb_hash_mc_addr - Generate a multicast hash value
291  *  @hw: pointer to the HW structure
292  *  @mc_addr: pointer to a multicast address
293  *
294  *  Generates a multicast address hash value which is used to determine
295  *  the multicast filter table array address and new table value.  See
296  *  igb_mta_set()
297  **/
298 static u32 igb_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
299 {
300         u32 hash_value, hash_mask;
301         u8 bit_shift = 0;
302
303         /* Register count multiplied by bits per register */
304         hash_mask = (hw->mac.mta_reg_count * 32) - 1;
305
306         /*
307          * For a mc_filter_type of 0, bit_shift is the number of left-shifts
308          * where 0xFF would still fall within the hash mask.
309          */
310         while (hash_mask >> bit_shift != 0xFF)
311                 bit_shift++;
312
313         /*
314          * The portion of the address that is used for the hash table
315          * is determined by the mc_filter_type setting.
316          * The algorithm is such that there is a total of 8 bits of shifting.
317          * The bit_shift for a mc_filter_type of 0 represents the number of
318          * left-shifts where the MSB of mc_addr[5] would still fall within
319          * the hash_mask.  Case 0 does this exactly.  Since there are a total
320          * of 8 bits of shifting, then mc_addr[4] will shift right the
321          * remaining number of bits. Thus 8 - bit_shift.  The rest of the
322          * cases are a variation of this algorithm...essentially raising the
323          * number of bits to shift mc_addr[5] left, while still keeping the
324          * 8-bit shifting total.
325          *
326          * For example, given the following Destination MAC Address and an
327          * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
328          * we can see that the bit_shift for case 0 is 4.  These are the hash
329          * values resulting from each mc_filter_type...
330          * [0] [1] [2] [3] [4] [5]
331          * 01  AA  00  12  34  56
332          * LSB                 MSB
333          *
334          * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
335          * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
336          * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
337          * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
338          */
339         switch (hw->mac.mc_filter_type) {
340         default:
341         case 0:
342                 break;
343         case 1:
344                 bit_shift += 1;
345                 break;
346         case 2:
347                 bit_shift += 2;
348                 break;
349         case 3:
350                 bit_shift += 4;
351                 break;
352         }
353
354         hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
355                                   (((u16) mc_addr[5]) << bit_shift)));
356
357         return hash_value;
358 }
359
360 /**
361  *  igb_update_mc_addr_list - Update Multicast addresses
362  *  @hw: pointer to the HW structure
363  *  @mc_addr_list: array of multicast addresses to program
364  *  @mc_addr_count: number of multicast addresses to program
365  *
366  *  Updates entire Multicast Table Array.
367  *  The caller must have a packed mc_addr_list of multicast addresses.
368  **/
369 void igb_update_mc_addr_list(struct e1000_hw *hw,
370                              u8 *mc_addr_list, u32 mc_addr_count)
371 {
372         u32 hash_value, hash_bit, hash_reg;
373         int i;
374
375         /* clear mta_shadow */
376         memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
377
378         /* update mta_shadow from mc_addr_list */
379         for (i = 0; (u32) i < mc_addr_count; i++) {
380                 hash_value = igb_hash_mc_addr(hw, mc_addr_list);
381
382                 hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
383                 hash_bit = hash_value & 0x1F;
384
385                 hw->mac.mta_shadow[hash_reg] |= (1 << hash_bit);
386                 mc_addr_list += (ETH_ALEN);
387         }
388
389         /* replace the entire MTA table */
390         for (i = hw->mac.mta_reg_count - 1; i >= 0; i--)
391                 array_wr32(E1000_MTA, i, hw->mac.mta_shadow[i]);
392         wrfl();
393 }
394
395 /**
396  *  igb_clear_hw_cntrs_base - Clear base hardware counters
397  *  @hw: pointer to the HW structure
398  *
399  *  Clears the base hardware counters by reading the counter registers.
400  **/
401 void igb_clear_hw_cntrs_base(struct e1000_hw *hw)
402 {
403         rd32(E1000_CRCERRS);
404         rd32(E1000_SYMERRS);
405         rd32(E1000_MPC);
406         rd32(E1000_SCC);
407         rd32(E1000_ECOL);
408         rd32(E1000_MCC);
409         rd32(E1000_LATECOL);
410         rd32(E1000_COLC);
411         rd32(E1000_DC);
412         rd32(E1000_SEC);
413         rd32(E1000_RLEC);
414         rd32(E1000_XONRXC);
415         rd32(E1000_XONTXC);
416         rd32(E1000_XOFFRXC);
417         rd32(E1000_XOFFTXC);
418         rd32(E1000_FCRUC);
419         rd32(E1000_GPRC);
420         rd32(E1000_BPRC);
421         rd32(E1000_MPRC);
422         rd32(E1000_GPTC);
423         rd32(E1000_GORCL);
424         rd32(E1000_GORCH);
425         rd32(E1000_GOTCL);
426         rd32(E1000_GOTCH);
427         rd32(E1000_RNBC);
428         rd32(E1000_RUC);
429         rd32(E1000_RFC);
430         rd32(E1000_ROC);
431         rd32(E1000_RJC);
432         rd32(E1000_TORL);
433         rd32(E1000_TORH);
434         rd32(E1000_TOTL);
435         rd32(E1000_TOTH);
436         rd32(E1000_TPR);
437         rd32(E1000_TPT);
438         rd32(E1000_MPTC);
439         rd32(E1000_BPTC);
440 }
441
442 /**
443  *  igb_check_for_copper_link - Check for link (Copper)
444  *  @hw: pointer to the HW structure
445  *
446  *  Checks to see of the link status of the hardware has changed.  If a
447  *  change in link status has been detected, then we read the PHY registers
448  *  to get the current speed/duplex if link exists.
449  **/
450 s32 igb_check_for_copper_link(struct e1000_hw *hw)
451 {
452         struct e1000_mac_info *mac = &hw->mac;
453         s32 ret_val;
454         bool link;
455
456         /*
457          * We only want to go out to the PHY registers to see if Auto-Neg
458          * has completed and/or if our link status has changed.  The
459          * get_link_status flag is set upon receiving a Link Status
460          * Change or Rx Sequence Error interrupt.
461          */
462         if (!mac->get_link_status) {
463                 ret_val = 0;
464                 goto out;
465         }
466
467         /*
468          * First we want to see if the MII Status Register reports
469          * link.  If so, then we want to get the current speed/duplex
470          * of the PHY.
471          */
472         ret_val = igb_phy_has_link(hw, 1, 0, &link);
473         if (ret_val)
474                 goto out;
475
476         if (!link)
477                 goto out; /* No link detected */
478
479         mac->get_link_status = false;
480
481         /*
482          * Check if there was DownShift, must be checked
483          * immediately after link-up
484          */
485         igb_check_downshift(hw);
486
487         /*
488          * If we are forcing speed/duplex, then we simply return since
489          * we have already determined whether we have link or not.
490          */
491         if (!mac->autoneg) {
492                 ret_val = -E1000_ERR_CONFIG;
493                 goto out;
494         }
495
496         /*
497          * Auto-Neg is enabled.  Auto Speed Detection takes care
498          * of MAC speed/duplex configuration.  So we only need to
499          * configure Collision Distance in the MAC.
500          */
501         igb_config_collision_dist(hw);
502
503         /*
504          * Configure Flow Control now that Auto-Neg has completed.
505          * First, we need to restore the desired flow control
506          * settings because we may have had to re-autoneg with a
507          * different link partner.
508          */
509         ret_val = igb_config_fc_after_link_up(hw);
510         if (ret_val)
511                 hw_dbg("Error configuring flow control\n");
512
513 out:
514         return ret_val;
515 }
516
517 /**
518  *  igb_setup_link - Setup flow control and link settings
519  *  @hw: pointer to the HW structure
520  *
521  *  Determines which flow control settings to use, then configures flow
522  *  control.  Calls the appropriate media-specific link configuration
523  *  function.  Assuming the adapter has a valid link partner, a valid link
524  *  should be established.  Assumes the hardware has previously been reset
525  *  and the transmitter and receiver are not enabled.
526  **/
527 s32 igb_setup_link(struct e1000_hw *hw)
528 {
529         s32 ret_val = 0;
530
531         /*
532          * In the case of the phy reset being blocked, we already have a link.
533          * We do not need to set it up again.
534          */
535         if (igb_check_reset_block(hw))
536                 goto out;
537
538         /*
539          * If requested flow control is set to default, set flow control
540          * based on the EEPROM flow control settings.
541          */
542         if (hw->fc.requested_mode == e1000_fc_default) {
543                 ret_val = igb_set_default_fc(hw);
544                 if (ret_val)
545                         goto out;
546         }
547
548         /*
549          * We want to save off the original Flow Control configuration just
550          * in case we get disconnected and then reconnected into a different
551          * hub or switch with different Flow Control capabilities.
552          */
553         hw->fc.current_mode = hw->fc.requested_mode;
554
555         hw_dbg("After fix-ups FlowControl is now = %x\n", hw->fc.current_mode);
556
557         /* Call the necessary media_type subroutine to configure the link. */
558         ret_val = hw->mac.ops.setup_physical_interface(hw);
559         if (ret_val)
560                 goto out;
561
562         /*
563          * Initialize the flow control address, type, and PAUSE timer
564          * registers to their default values.  This is done even if flow
565          * control is disabled, because it does not hurt anything to
566          * initialize these registers.
567          */
568         hw_dbg("Initializing the Flow Control address, type and timer regs\n");
569         wr32(E1000_FCT, FLOW_CONTROL_TYPE);
570         wr32(E1000_FCAH, FLOW_CONTROL_ADDRESS_HIGH);
571         wr32(E1000_FCAL, FLOW_CONTROL_ADDRESS_LOW);
572
573         wr32(E1000_FCTTV, hw->fc.pause_time);
574
575         ret_val = igb_set_fc_watermarks(hw);
576
577 out:
578         return ret_val;
579 }
580
581 /**
582  *  igb_config_collision_dist - Configure collision distance
583  *  @hw: pointer to the HW structure
584  *
585  *  Configures the collision distance to the default value and is used
586  *  during link setup. Currently no func pointer exists and all
587  *  implementations are handled in the generic version of this function.
588  **/
589 void igb_config_collision_dist(struct e1000_hw *hw)
590 {
591         u32 tctl;
592
593         tctl = rd32(E1000_TCTL);
594
595         tctl &= ~E1000_TCTL_COLD;
596         tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
597
598         wr32(E1000_TCTL, tctl);
599         wrfl();
600 }
601
602 /**
603  *  igb_set_fc_watermarks - Set flow control high/low watermarks
604  *  @hw: pointer to the HW structure
605  *
606  *  Sets the flow control high/low threshold (watermark) registers.  If
607  *  flow control XON frame transmission is enabled, then set XON frame
608  *  tansmission as well.
609  **/
610 static s32 igb_set_fc_watermarks(struct e1000_hw *hw)
611 {
612         s32 ret_val = 0;
613         u32 fcrtl = 0, fcrth = 0;
614
615         /*
616          * Set the flow control receive threshold registers.  Normally,
617          * these registers will be set to a default threshold that may be
618          * adjusted later by the driver's runtime code.  However, if the
619          * ability to transmit pause frames is not enabled, then these
620          * registers will be set to 0.
621          */
622         if (hw->fc.current_mode & e1000_fc_tx_pause) {
623                 /*
624                  * We need to set up the Receive Threshold high and low water
625                  * marks as well as (optionally) enabling the transmission of
626                  * XON frames.
627                  */
628                 fcrtl = hw->fc.low_water;
629                 if (hw->fc.send_xon)
630                         fcrtl |= E1000_FCRTL_XONE;
631
632                 fcrth = hw->fc.high_water;
633         }
634         wr32(E1000_FCRTL, fcrtl);
635         wr32(E1000_FCRTH, fcrth);
636
637         return ret_val;
638 }
639
640 /**
641  *  igb_set_default_fc - Set flow control default values
642  *  @hw: pointer to the HW structure
643  *
644  *  Read the EEPROM for the default values for flow control and store the
645  *  values.
646  **/
647 static s32 igb_set_default_fc(struct e1000_hw *hw)
648 {
649         s32 ret_val = 0;
650         u16 nvm_data;
651
652         /*
653          * Read and store word 0x0F of the EEPROM. This word contains bits
654          * that determine the hardware's default PAUSE (flow control) mode,
655          * a bit that determines whether the HW defaults to enabling or
656          * disabling auto-negotiation, and the direction of the
657          * SW defined pins. If there is no SW over-ride of the flow
658          * control setting, then the variable hw->fc will
659          * be initialized based on a value in the EEPROM.
660          */
661         ret_val = hw->nvm.ops.read(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
662
663         if (ret_val) {
664                 hw_dbg("NVM Read Error\n");
665                 goto out;
666         }
667
668         if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == 0)
669                 hw->fc.requested_mode = e1000_fc_none;
670         else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
671                  NVM_WORD0F_ASM_DIR)
672                 hw->fc.requested_mode = e1000_fc_tx_pause;
673         else
674                 hw->fc.requested_mode = e1000_fc_full;
675
676 out:
677         return ret_val;
678 }
679
680 /**
681  *  igb_force_mac_fc - Force the MAC's flow control settings
682  *  @hw: pointer to the HW structure
683  *
684  *  Force the MAC's flow control settings.  Sets the TFCE and RFCE bits in the
685  *  device control register to reflect the adapter settings.  TFCE and RFCE
686  *  need to be explicitly set by software when a copper PHY is used because
687  *  autonegotiation is managed by the PHY rather than the MAC.  Software must
688  *  also configure these bits when link is forced on a fiber connection.
689  **/
690 s32 igb_force_mac_fc(struct e1000_hw *hw)
691 {
692         u32 ctrl;
693         s32 ret_val = 0;
694
695         ctrl = rd32(E1000_CTRL);
696
697         /*
698          * Because we didn't get link via the internal auto-negotiation
699          * mechanism (we either forced link or we got link via PHY
700          * auto-neg), we have to manually enable/disable transmit an
701          * receive flow control.
702          *
703          * The "Case" statement below enables/disable flow control
704          * according to the "hw->fc.current_mode" parameter.
705          *
706          * The possible values of the "fc" parameter are:
707          *      0:  Flow control is completely disabled
708          *      1:  Rx flow control is enabled (we can receive pause
709          *          frames but not send pause frames).
710          *      2:  Tx flow control is enabled (we can send pause frames
711          *          frames but we do not receive pause frames).
712          *      3:  Both Rx and TX flow control (symmetric) is enabled.
713          *  other:  No other values should be possible at this point.
714          */
715         hw_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
716
717         switch (hw->fc.current_mode) {
718         case e1000_fc_none:
719                 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
720                 break;
721         case e1000_fc_rx_pause:
722                 ctrl &= (~E1000_CTRL_TFCE);
723                 ctrl |= E1000_CTRL_RFCE;
724                 break;
725         case e1000_fc_tx_pause:
726                 ctrl &= (~E1000_CTRL_RFCE);
727                 ctrl |= E1000_CTRL_TFCE;
728                 break;
729         case e1000_fc_full:
730                 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
731                 break;
732         default:
733                 hw_dbg("Flow control param set incorrectly\n");
734                 ret_val = -E1000_ERR_CONFIG;
735                 goto out;
736         }
737
738         wr32(E1000_CTRL, ctrl);
739
740 out:
741         return ret_val;
742 }
743
744 /**
745  *  igb_config_fc_after_link_up - Configures flow control after link
746  *  @hw: pointer to the HW structure
747  *
748  *  Checks the status of auto-negotiation after link up to ensure that the
749  *  speed and duplex were not forced.  If the link needed to be forced, then
750  *  flow control needs to be forced also.  If auto-negotiation is enabled
751  *  and did not fail, then we configure flow control based on our link
752  *  partner.
753  **/
754 s32 igb_config_fc_after_link_up(struct e1000_hw *hw)
755 {
756         struct e1000_mac_info *mac = &hw->mac;
757         s32 ret_val = 0;
758         u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
759         u16 speed, duplex;
760
761         /*
762          * Check for the case where we have fiber media and auto-neg failed
763          * so we had to force link.  In this case, we need to force the
764          * configuration of the MAC to match the "fc" parameter.
765          */
766         if (mac->autoneg_failed) {
767                 if (hw->phy.media_type == e1000_media_type_internal_serdes)
768                         ret_val = igb_force_mac_fc(hw);
769         } else {
770                 if (hw->phy.media_type == e1000_media_type_copper)
771                         ret_val = igb_force_mac_fc(hw);
772         }
773
774         if (ret_val) {
775                 hw_dbg("Error forcing flow control settings\n");
776                 goto out;
777         }
778
779         /*
780          * Check for the case where we have copper media and auto-neg is
781          * enabled.  In this case, we need to check and see if Auto-Neg
782          * has completed, and if so, how the PHY and link partner has
783          * flow control configured.
784          */
785         if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
786                 /*
787                  * Read the MII Status Register and check to see if AutoNeg
788                  * has completed.  We read this twice because this reg has
789                  * some "sticky" (latched) bits.
790                  */
791                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
792                                                    &mii_status_reg);
793                 if (ret_val)
794                         goto out;
795                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
796                                                    &mii_status_reg);
797                 if (ret_val)
798                         goto out;
799
800                 if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
801                         hw_dbg("Copper PHY and Auto Neg "
802                                  "has not completed.\n");
803                         goto out;
804                 }
805
806                 /*
807                  * The AutoNeg process has completed, so we now need to
808                  * read both the Auto Negotiation Advertisement
809                  * Register (Address 4) and the Auto_Negotiation Base
810                  * Page Ability Register (Address 5) to determine how
811                  * flow control was negotiated.
812                  */
813                 ret_val = hw->phy.ops.read_reg(hw, PHY_AUTONEG_ADV,
814                                             &mii_nway_adv_reg);
815                 if (ret_val)
816                         goto out;
817                 ret_val = hw->phy.ops.read_reg(hw, PHY_LP_ABILITY,
818                                             &mii_nway_lp_ability_reg);
819                 if (ret_val)
820                         goto out;
821
822                 /*
823                  * Two bits in the Auto Negotiation Advertisement Register
824                  * (Address 4) and two bits in the Auto Negotiation Base
825                  * Page Ability Register (Address 5) determine flow control
826                  * for both the PHY and the link partner.  The following
827                  * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
828                  * 1999, describes these PAUSE resolution bits and how flow
829                  * control is determined based upon these settings.
830                  * NOTE:  DC = Don't Care
831                  *
832                  *   LOCAL DEVICE  |   LINK PARTNER
833                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
834                  *-------|---------|-------|---------|--------------------
835                  *   0   |    0    |  DC   |   DC    | e1000_fc_none
836                  *   0   |    1    |   0   |   DC    | e1000_fc_none
837                  *   0   |    1    |   1   |    0    | e1000_fc_none
838                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
839                  *   1   |    0    |   0   |   DC    | e1000_fc_none
840                  *   1   |   DC    |   1   |   DC    | e1000_fc_full
841                  *   1   |    1    |   0   |    0    | e1000_fc_none
842                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
843                  *
844                  * Are both PAUSE bits set to 1?  If so, this implies
845                  * Symmetric Flow Control is enabled at both ends.  The
846                  * ASM_DIR bits are irrelevant per the spec.
847                  *
848                  * For Symmetric Flow Control:
849                  *
850                  *   LOCAL DEVICE  |   LINK PARTNER
851                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
852                  *-------|---------|-------|---------|--------------------
853                  *   1   |   DC    |   1   |   DC    | E1000_fc_full
854                  *
855                  */
856                 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
857                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
858                         /*
859                          * Now we need to check if the user selected RX ONLY
860                          * of pause frames.  In this case, we had to advertise
861                          * FULL flow control because we could not advertise RX
862                          * ONLY. Hence, we must now check to see if we need to
863                          * turn OFF  the TRANSMISSION of PAUSE frames.
864                          */
865                         if (hw->fc.requested_mode == e1000_fc_full) {
866                                 hw->fc.current_mode = e1000_fc_full;
867                                 hw_dbg("Flow Control = FULL.\r\n");
868                         } else {
869                                 hw->fc.current_mode = e1000_fc_rx_pause;
870                                 hw_dbg("Flow Control = "
871                                        "RX PAUSE frames only.\r\n");
872                         }
873                 }
874                 /*
875                  * For receiving PAUSE frames ONLY.
876                  *
877                  *   LOCAL DEVICE  |   LINK PARTNER
878                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
879                  *-------|---------|-------|---------|--------------------
880                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
881                  */
882                 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
883                           (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
884                           (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
885                           (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
886                         hw->fc.current_mode = e1000_fc_tx_pause;
887                         hw_dbg("Flow Control = TX PAUSE frames only.\r\n");
888                 }
889                 /*
890                  * For transmitting PAUSE frames ONLY.
891                  *
892                  *   LOCAL DEVICE  |   LINK PARTNER
893                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
894                  *-------|---------|-------|---------|--------------------
895                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
896                  */
897                 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
898                          (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
899                          !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
900                          (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
901                         hw->fc.current_mode = e1000_fc_rx_pause;
902                         hw_dbg("Flow Control = RX PAUSE frames only.\r\n");
903                 }
904                 /*
905                  * Per the IEEE spec, at this point flow control should be
906                  * disabled.  However, we want to consider that we could
907                  * be connected to a legacy switch that doesn't advertise
908                  * desired flow control, but can be forced on the link
909                  * partner.  So if we advertised no flow control, that is
910                  * what we will resolve to.  If we advertised some kind of
911                  * receive capability (Rx Pause Only or Full Flow Control)
912                  * and the link partner advertised none, we will configure
913                  * ourselves to enable Rx Flow Control only.  We can do
914                  * this safely for two reasons:  If the link partner really
915                  * didn't want flow control enabled, and we enable Rx, no
916                  * harm done since we won't be receiving any PAUSE frames
917                  * anyway.  If the intent on the link partner was to have
918                  * flow control enabled, then by us enabling RX only, we
919                  * can at least receive pause frames and process them.
920                  * This is a good idea because in most cases, since we are
921                  * predominantly a server NIC, more times than not we will
922                  * be asked to delay transmission of packets than asking
923                  * our link partner to pause transmission of frames.
924                  */
925                 else if ((hw->fc.requested_mode == e1000_fc_none ||
926                           hw->fc.requested_mode == e1000_fc_tx_pause) ||
927                          hw->fc.strict_ieee) {
928                         hw->fc.current_mode = e1000_fc_none;
929                         hw_dbg("Flow Control = NONE.\r\n");
930                 } else {
931                         hw->fc.current_mode = e1000_fc_rx_pause;
932                         hw_dbg("Flow Control = RX PAUSE frames only.\r\n");
933                 }
934
935                 /*
936                  * Now we need to do one last check...  If we auto-
937                  * negotiated to HALF DUPLEX, flow control should not be
938                  * enabled per IEEE 802.3 spec.
939                  */
940                 ret_val = hw->mac.ops.get_speed_and_duplex(hw, &speed, &duplex);
941                 if (ret_val) {
942                         hw_dbg("Error getting link speed and duplex\n");
943                         goto out;
944                 }
945
946                 if (duplex == HALF_DUPLEX)
947                         hw->fc.current_mode = e1000_fc_none;
948
949                 /*
950                  * Now we call a subroutine to actually force the MAC
951                  * controller to use the correct flow control settings.
952                  */
953                 ret_val = igb_force_mac_fc(hw);
954                 if (ret_val) {
955                         hw_dbg("Error forcing flow control settings\n");
956                         goto out;
957                 }
958         }
959
960 out:
961         return ret_val;
962 }
963
964 /**
965  *  igb_get_speed_and_duplex_copper - Retreive current speed/duplex
966  *  @hw: pointer to the HW structure
967  *  @speed: stores the current speed
968  *  @duplex: stores the current duplex
969  *
970  *  Read the status register for the current speed/duplex and store the current
971  *  speed and duplex for copper connections.
972  **/
973 s32 igb_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed,
974                                       u16 *duplex)
975 {
976         u32 status;
977
978         status = rd32(E1000_STATUS);
979         if (status & E1000_STATUS_SPEED_1000) {
980                 *speed = SPEED_1000;
981                 hw_dbg("1000 Mbs, ");
982         } else if (status & E1000_STATUS_SPEED_100) {
983                 *speed = SPEED_100;
984                 hw_dbg("100 Mbs, ");
985         } else {
986                 *speed = SPEED_10;
987                 hw_dbg("10 Mbs, ");
988         }
989
990         if (status & E1000_STATUS_FD) {
991                 *duplex = FULL_DUPLEX;
992                 hw_dbg("Full Duplex\n");
993         } else {
994                 *duplex = HALF_DUPLEX;
995                 hw_dbg("Half Duplex\n");
996         }
997
998         return 0;
999 }
1000
1001 /**
1002  *  igb_get_hw_semaphore - Acquire hardware semaphore
1003  *  @hw: pointer to the HW structure
1004  *
1005  *  Acquire the HW semaphore to access the PHY or NVM
1006  **/
1007 s32 igb_get_hw_semaphore(struct e1000_hw *hw)
1008 {
1009         u32 swsm;
1010         s32 ret_val = 0;
1011         s32 timeout = hw->nvm.word_size + 1;
1012         s32 i = 0;
1013
1014         /* Get the SW semaphore */
1015         while (i < timeout) {
1016                 swsm = rd32(E1000_SWSM);
1017                 if (!(swsm & E1000_SWSM_SMBI))
1018                         break;
1019
1020                 udelay(50);
1021                 i++;
1022         }
1023
1024         if (i == timeout) {
1025                 hw_dbg("Driver can't access device - SMBI bit is set.\n");
1026                 ret_val = -E1000_ERR_NVM;
1027                 goto out;
1028         }
1029
1030         /* Get the FW semaphore. */
1031         for (i = 0; i < timeout; i++) {
1032                 swsm = rd32(E1000_SWSM);
1033                 wr32(E1000_SWSM, swsm | E1000_SWSM_SWESMBI);
1034
1035                 /* Semaphore acquired if bit latched */
1036                 if (rd32(E1000_SWSM) & E1000_SWSM_SWESMBI)
1037                         break;
1038
1039                 udelay(50);
1040         }
1041
1042         if (i == timeout) {
1043                 /* Release semaphores */
1044                 igb_put_hw_semaphore(hw);
1045                 hw_dbg("Driver can't access the NVM\n");
1046                 ret_val = -E1000_ERR_NVM;
1047                 goto out;
1048         }
1049
1050 out:
1051         return ret_val;
1052 }
1053
1054 /**
1055  *  igb_put_hw_semaphore - Release hardware semaphore
1056  *  @hw: pointer to the HW structure
1057  *
1058  *  Release hardware semaphore used to access the PHY or NVM
1059  **/
1060 void igb_put_hw_semaphore(struct e1000_hw *hw)
1061 {
1062         u32 swsm;
1063
1064         swsm = rd32(E1000_SWSM);
1065
1066         swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1067
1068         wr32(E1000_SWSM, swsm);
1069 }
1070
1071 /**
1072  *  igb_get_auto_rd_done - Check for auto read completion
1073  *  @hw: pointer to the HW structure
1074  *
1075  *  Check EEPROM for Auto Read done bit.
1076  **/
1077 s32 igb_get_auto_rd_done(struct e1000_hw *hw)
1078 {
1079         s32 i = 0;
1080         s32 ret_val = 0;
1081
1082
1083         while (i < AUTO_READ_DONE_TIMEOUT) {
1084                 if (rd32(E1000_EECD) & E1000_EECD_AUTO_RD)
1085                         break;
1086                 msleep(1);
1087                 i++;
1088         }
1089
1090         if (i == AUTO_READ_DONE_TIMEOUT) {
1091                 hw_dbg("Auto read by HW from NVM has not completed.\n");
1092                 ret_val = -E1000_ERR_RESET;
1093                 goto out;
1094         }
1095
1096 out:
1097         return ret_val;
1098 }
1099
1100 /**
1101  *  igb_valid_led_default - Verify a valid default LED config
1102  *  @hw: pointer to the HW structure
1103  *  @data: pointer to the NVM (EEPROM)
1104  *
1105  *  Read the EEPROM for the current default LED configuration.  If the
1106  *  LED configuration is not valid, set to a valid LED configuration.
1107  **/
1108 static s32 igb_valid_led_default(struct e1000_hw *hw, u16 *data)
1109 {
1110         s32 ret_val;
1111
1112         ret_val = hw->nvm.ops.read(hw, NVM_ID_LED_SETTINGS, 1, data);
1113         if (ret_val) {
1114                 hw_dbg("NVM Read Error\n");
1115                 goto out;
1116         }
1117
1118         if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF) {
1119                 switch(hw->phy.media_type) {
1120                 case e1000_media_type_internal_serdes:
1121                         *data = ID_LED_DEFAULT_82575_SERDES;
1122                         break;
1123                 case e1000_media_type_copper:
1124                 default:
1125                         *data = ID_LED_DEFAULT;
1126                         break;
1127                 }
1128         }
1129 out:
1130         return ret_val;
1131 }
1132
1133 /**
1134  *  igb_id_led_init -
1135  *  @hw: pointer to the HW structure
1136  *
1137  **/
1138 s32 igb_id_led_init(struct e1000_hw *hw)
1139 {
1140         struct e1000_mac_info *mac = &hw->mac;
1141         s32 ret_val;
1142         const u32 ledctl_mask = 0x000000FF;
1143         const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
1144         const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
1145         u16 data, i, temp;
1146         const u16 led_mask = 0x0F;
1147
1148         ret_val = igb_valid_led_default(hw, &data);
1149         if (ret_val)
1150                 goto out;
1151
1152         mac->ledctl_default = rd32(E1000_LEDCTL);
1153         mac->ledctl_mode1 = mac->ledctl_default;
1154         mac->ledctl_mode2 = mac->ledctl_default;
1155
1156         for (i = 0; i < 4; i++) {
1157                 temp = (data >> (i << 2)) & led_mask;
1158                 switch (temp) {
1159                 case ID_LED_ON1_DEF2:
1160                 case ID_LED_ON1_ON2:
1161                 case ID_LED_ON1_OFF2:
1162                         mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1163                         mac->ledctl_mode1 |= ledctl_on << (i << 3);
1164                         break;
1165                 case ID_LED_OFF1_DEF2:
1166                 case ID_LED_OFF1_ON2:
1167                 case ID_LED_OFF1_OFF2:
1168                         mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1169                         mac->ledctl_mode1 |= ledctl_off << (i << 3);
1170                         break;
1171                 default:
1172                         /* Do nothing */
1173                         break;
1174                 }
1175                 switch (temp) {
1176                 case ID_LED_DEF1_ON2:
1177                 case ID_LED_ON1_ON2:
1178                 case ID_LED_OFF1_ON2:
1179                         mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1180                         mac->ledctl_mode2 |= ledctl_on << (i << 3);
1181                         break;
1182                 case ID_LED_DEF1_OFF2:
1183                 case ID_LED_ON1_OFF2:
1184                 case ID_LED_OFF1_OFF2:
1185                         mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1186                         mac->ledctl_mode2 |= ledctl_off << (i << 3);
1187                         break;
1188                 default:
1189                         /* Do nothing */
1190                         break;
1191                 }
1192         }
1193
1194 out:
1195         return ret_val;
1196 }
1197
1198 /**
1199  *  igb_cleanup_led - Set LED config to default operation
1200  *  @hw: pointer to the HW structure
1201  *
1202  *  Remove the current LED configuration and set the LED configuration
1203  *  to the default value, saved from the EEPROM.
1204  **/
1205 s32 igb_cleanup_led(struct e1000_hw *hw)
1206 {
1207         wr32(E1000_LEDCTL, hw->mac.ledctl_default);
1208         return 0;
1209 }
1210
1211 /**
1212  *  igb_blink_led - Blink LED
1213  *  @hw: pointer to the HW structure
1214  *
1215  *  Blink the led's which are set to be on.
1216  **/
1217 s32 igb_blink_led(struct e1000_hw *hw)
1218 {
1219         u32 ledctl_blink = 0;
1220         u32 i;
1221
1222         /*
1223          * set the blink bit for each LED that's "on" (0x0E)
1224          * in ledctl_mode2
1225          */
1226         ledctl_blink = hw->mac.ledctl_mode2;
1227         for (i = 0; i < 4; i++)
1228                 if (((hw->mac.ledctl_mode2 >> (i * 8)) & 0xFF) ==
1229                     E1000_LEDCTL_MODE_LED_ON)
1230                         ledctl_blink |= (E1000_LEDCTL_LED0_BLINK <<
1231                                          (i * 8));
1232
1233         wr32(E1000_LEDCTL, ledctl_blink);
1234
1235         return 0;
1236 }
1237
1238 /**
1239  *  igb_led_off - Turn LED off
1240  *  @hw: pointer to the HW structure
1241  *
1242  *  Turn LED off.
1243  **/
1244 s32 igb_led_off(struct e1000_hw *hw)
1245 {
1246         switch (hw->phy.media_type) {
1247         case e1000_media_type_copper:
1248                 wr32(E1000_LEDCTL, hw->mac.ledctl_mode1);
1249                 break;
1250         default:
1251                 break;
1252         }
1253
1254         return 0;
1255 }
1256
1257 /**
1258  *  igb_disable_pcie_master - Disables PCI-express master access
1259  *  @hw: pointer to the HW structure
1260  *
1261  *  Returns 0 (0) if successful, else returns -10
1262  *  (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not casued
1263  *  the master requests to be disabled.
1264  *
1265  *  Disables PCI-Express master access and verifies there are no pending
1266  *  requests.
1267  **/
1268 s32 igb_disable_pcie_master(struct e1000_hw *hw)
1269 {
1270         u32 ctrl;
1271         s32 timeout = MASTER_DISABLE_TIMEOUT;
1272         s32 ret_val = 0;
1273
1274         if (hw->bus.type != e1000_bus_type_pci_express)
1275                 goto out;
1276
1277         ctrl = rd32(E1000_CTRL);
1278         ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
1279         wr32(E1000_CTRL, ctrl);
1280
1281         while (timeout) {
1282                 if (!(rd32(E1000_STATUS) &
1283                       E1000_STATUS_GIO_MASTER_ENABLE))
1284                         break;
1285                 udelay(100);
1286                 timeout--;
1287         }
1288
1289         if (!timeout) {
1290                 hw_dbg("Master requests are pending.\n");
1291                 ret_val = -E1000_ERR_MASTER_REQUESTS_PENDING;
1292                 goto out;
1293         }
1294
1295 out:
1296         return ret_val;
1297 }
1298
1299 /**
1300  *  igb_reset_adaptive - Reset Adaptive Interframe Spacing
1301  *  @hw: pointer to the HW structure
1302  *
1303  *  Reset the Adaptive Interframe Spacing throttle to default values.
1304  **/
1305 void igb_reset_adaptive(struct e1000_hw *hw)
1306 {
1307         struct e1000_mac_info *mac = &hw->mac;
1308
1309         if (!mac->adaptive_ifs) {
1310                 hw_dbg("Not in Adaptive IFS mode!\n");
1311                 goto out;
1312         }
1313
1314         if (!mac->ifs_params_forced) {
1315                 mac->current_ifs_val = 0;
1316                 mac->ifs_min_val = IFS_MIN;
1317                 mac->ifs_max_val = IFS_MAX;
1318                 mac->ifs_step_size = IFS_STEP;
1319                 mac->ifs_ratio = IFS_RATIO;
1320         }
1321
1322         mac->in_ifs_mode = false;
1323         wr32(E1000_AIT, 0);
1324 out:
1325         return;
1326 }
1327
1328 /**
1329  *  igb_update_adaptive - Update Adaptive Interframe Spacing
1330  *  @hw: pointer to the HW structure
1331  *
1332  *  Update the Adaptive Interframe Spacing Throttle value based on the
1333  *  time between transmitted packets and time between collisions.
1334  **/
1335 void igb_update_adaptive(struct e1000_hw *hw)
1336 {
1337         struct e1000_mac_info *mac = &hw->mac;
1338
1339         if (!mac->adaptive_ifs) {
1340                 hw_dbg("Not in Adaptive IFS mode!\n");
1341                 goto out;
1342         }
1343
1344         if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
1345                 if (mac->tx_packet_delta > MIN_NUM_XMITS) {
1346                         mac->in_ifs_mode = true;
1347                         if (mac->current_ifs_val < mac->ifs_max_val) {
1348                                 if (!mac->current_ifs_val)
1349                                         mac->current_ifs_val = mac->ifs_min_val;
1350                                 else
1351                                         mac->current_ifs_val +=
1352                                                 mac->ifs_step_size;
1353                                 wr32(E1000_AIT,
1354                                                 mac->current_ifs_val);
1355                         }
1356                 }
1357         } else {
1358                 if (mac->in_ifs_mode &&
1359                     (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
1360                         mac->current_ifs_val = 0;
1361                         mac->in_ifs_mode = false;
1362                         wr32(E1000_AIT, 0);
1363                 }
1364         }
1365 out:
1366         return;
1367 }
1368
1369 /**
1370  *  igb_validate_mdi_setting - Verify MDI/MDIx settings
1371  *  @hw: pointer to the HW structure
1372  *
1373  *  Verify that when not using auto-negotitation that MDI/MDIx is correctly
1374  *  set, which is forced to MDI mode only.
1375  **/
1376 s32 igb_validate_mdi_setting(struct e1000_hw *hw)
1377 {
1378         s32 ret_val = 0;
1379
1380         if (!hw->mac.autoneg && (hw->phy.mdix == 0 || hw->phy.mdix == 3)) {
1381                 hw_dbg("Invalid MDI setting detected\n");
1382                 hw->phy.mdix = 1;
1383                 ret_val = -E1000_ERR_CONFIG;
1384                 goto out;
1385         }
1386
1387 out:
1388         return ret_val;
1389 }
1390
1391 /**
1392  *  igb_write_8bit_ctrl_reg - Write a 8bit CTRL register
1393  *  @hw: pointer to the HW structure
1394  *  @reg: 32bit register offset such as E1000_SCTL
1395  *  @offset: register offset to write to
1396  *  @data: data to write at register offset
1397  *
1398  *  Writes an address/data control type register.  There are several of these
1399  *  and they all have the format address << 8 | data and bit 31 is polled for
1400  *  completion.
1401  **/
1402 s32 igb_write_8bit_ctrl_reg(struct e1000_hw *hw, u32 reg,
1403                               u32 offset, u8 data)
1404 {
1405         u32 i, regvalue = 0;
1406         s32 ret_val = 0;
1407
1408         /* Set up the address and data */
1409         regvalue = ((u32)data) | (offset << E1000_GEN_CTL_ADDRESS_SHIFT);
1410         wr32(reg, regvalue);
1411
1412         /* Poll the ready bit to see if the MDI read completed */
1413         for (i = 0; i < E1000_GEN_POLL_TIMEOUT; i++) {
1414                 udelay(5);
1415                 regvalue = rd32(reg);
1416                 if (regvalue & E1000_GEN_CTL_READY)
1417                         break;
1418         }
1419         if (!(regvalue & E1000_GEN_CTL_READY)) {
1420                 hw_dbg("Reg %08x did not indicate ready\n", reg);
1421                 ret_val = -E1000_ERR_PHY;
1422                 goto out;
1423         }
1424
1425 out:
1426         return ret_val;
1427 }
1428
1429 /**
1430  *  igb_enable_mng_pass_thru - Enable processing of ARP's
1431  *  @hw: pointer to the HW structure
1432  *
1433  *  Verifies the hardware needs to allow ARPs to be processed by the host.
1434  **/
1435 bool igb_enable_mng_pass_thru(struct e1000_hw *hw)
1436 {
1437         u32 manc;
1438         u32 fwsm, factps;
1439         bool ret_val = false;
1440
1441         if (!hw->mac.asf_firmware_present)
1442                 goto out;
1443
1444         manc = rd32(E1000_MANC);
1445
1446         if (!(manc & E1000_MANC_RCV_TCO_EN) ||
1447             !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
1448                 goto out;
1449
1450         if (hw->mac.arc_subsystem_valid) {
1451                 fwsm = rd32(E1000_FWSM);
1452                 factps = rd32(E1000_FACTPS);
1453
1454                 if (!(factps & E1000_FACTPS_MNGCG) &&
1455                     ((fwsm & E1000_FWSM_MODE_MASK) ==
1456                      (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT))) {
1457                         ret_val = true;
1458                         goto out;
1459                 }
1460         } else {
1461                 if ((manc & E1000_MANC_SMBUS_EN) &&
1462                     !(manc & E1000_MANC_ASF_EN)) {
1463                         ret_val = true;
1464                         goto out;
1465                 }
1466         }
1467
1468 out:
1469         return ret_val;
1470 }