e1000e: Fix check_for_link return value with autoneg off
[pandora-kernel.git] / drivers / net / ethernet / intel / e1000e / lib.c
1 /*******************************************************************************
2
3   Intel PRO/1000 Linux driver
4   Copyright(c) 1999 - 2011 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   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 #include "e1000.h"
30
31 enum e1000_mng_mode {
32         e1000_mng_mode_none = 0,
33         e1000_mng_mode_asf,
34         e1000_mng_mode_pt,
35         e1000_mng_mode_ipmi,
36         e1000_mng_mode_host_if_only
37 };
38
39 #define E1000_FACTPS_MNGCG              0x20000000
40
41 /* Intel(R) Active Management Technology signature */
42 #define E1000_IAMT_SIGNATURE            0x544D4149
43
44 /**
45  *  e1000e_get_bus_info_pcie - Get PCIe bus information
46  *  @hw: pointer to the HW structure
47  *
48  *  Determines and stores the system bus information for a particular
49  *  network interface.  The following bus information is determined and stored:
50  *  bus speed, bus width, type (PCIe), and PCIe function.
51  **/
52 s32 e1000e_get_bus_info_pcie(struct e1000_hw *hw)
53 {
54         struct e1000_mac_info *mac = &hw->mac;
55         struct e1000_bus_info *bus = &hw->bus;
56         struct e1000_adapter *adapter = hw->adapter;
57         u16 pcie_link_status, cap_offset;
58
59         cap_offset = adapter->pdev->pcie_cap;
60         if (!cap_offset) {
61                 bus->width = e1000_bus_width_unknown;
62         } else {
63                 pci_read_config_word(adapter->pdev,
64                                      cap_offset + PCIE_LINK_STATUS,
65                                      &pcie_link_status);
66                 bus->width = (enum e1000_bus_width)((pcie_link_status &
67                                                      PCIE_LINK_WIDTH_MASK) >>
68                                                     PCIE_LINK_WIDTH_SHIFT);
69         }
70
71         mac->ops.set_lan_id(hw);
72
73         return 0;
74 }
75
76 /**
77  *  e1000_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
78  *
79  *  @hw: pointer to the HW structure
80  *
81  *  Determines the LAN function id by reading memory-mapped registers
82  *  and swaps the port value if requested.
83  **/
84 void e1000_set_lan_id_multi_port_pcie(struct e1000_hw *hw)
85 {
86         struct e1000_bus_info *bus = &hw->bus;
87         u32 reg;
88
89         /*
90          * The status register reports the correct function number
91          * for the device regardless of function swap state.
92          */
93         reg = er32(STATUS);
94         bus->func = (reg & E1000_STATUS_FUNC_MASK) >> E1000_STATUS_FUNC_SHIFT;
95 }
96
97 /**
98  *  e1000_set_lan_id_single_port - Set LAN id for a single port device
99  *  @hw: pointer to the HW structure
100  *
101  *  Sets the LAN function id to zero for a single port device.
102  **/
103 void e1000_set_lan_id_single_port(struct e1000_hw *hw)
104 {
105         struct e1000_bus_info *bus = &hw->bus;
106
107         bus->func = 0;
108 }
109
110 /**
111  *  e1000_clear_vfta_generic - Clear VLAN filter table
112  *  @hw: pointer to the HW structure
113  *
114  *  Clears the register array which contains the VLAN filter table by
115  *  setting all the values to 0.
116  **/
117 void e1000_clear_vfta_generic(struct e1000_hw *hw)
118 {
119         u32 offset;
120
121         for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
122                 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, 0);
123                 e1e_flush();
124         }
125 }
126
127 /**
128  *  e1000_write_vfta_generic - Write value to VLAN filter table
129  *  @hw: pointer to the HW structure
130  *  @offset: register offset in VLAN filter table
131  *  @value: register value written to VLAN filter table
132  *
133  *  Writes value at the given offset in the register array which stores
134  *  the VLAN filter table.
135  **/
136 void e1000_write_vfta_generic(struct e1000_hw *hw, u32 offset, u32 value)
137 {
138         E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, value);
139         e1e_flush();
140 }
141
142 /**
143  *  e1000e_init_rx_addrs - Initialize receive address's
144  *  @hw: pointer to the HW structure
145  *  @rar_count: receive address registers
146  *
147  *  Setup the receive address registers by setting the base receive address
148  *  register to the devices MAC address and clearing all the other receive
149  *  address registers to 0.
150  **/
151 void e1000e_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
152 {
153         u32 i;
154         u8 mac_addr[ETH_ALEN] = {0};
155
156         /* Setup the receive address */
157         e_dbg("Programming MAC Address into RAR[0]\n");
158
159         e1000e_rar_set(hw, hw->mac.addr, 0);
160
161         /* Zero out the other (rar_entry_count - 1) receive addresses */
162         e_dbg("Clearing RAR[1-%u]\n", rar_count-1);
163         for (i = 1; i < rar_count; i++)
164                 e1000e_rar_set(hw, mac_addr, i);
165 }
166
167 /**
168  *  e1000_check_alt_mac_addr_generic - Check for alternate MAC addr
169  *  @hw: pointer to the HW structure
170  *
171  *  Checks the nvm for an alternate MAC address.  An alternate MAC address
172  *  can be setup by pre-boot software and must be treated like a permanent
173  *  address and must override the actual permanent MAC address. If an
174  *  alternate MAC address is found it is programmed into RAR0, replacing
175  *  the permanent address that was installed into RAR0 by the Si on reset.
176  *  This function will return SUCCESS unless it encounters an error while
177  *  reading the EEPROM.
178  **/
179 s32 e1000_check_alt_mac_addr_generic(struct e1000_hw *hw)
180 {
181         u32 i;
182         s32 ret_val = 0;
183         u16 offset, nvm_alt_mac_addr_offset, nvm_data;
184         u8 alt_mac_addr[ETH_ALEN];
185
186         ret_val = e1000_read_nvm(hw, NVM_COMPAT, 1, &nvm_data);
187         if (ret_val)
188                 goto out;
189
190         /* Check for LOM (vs. NIC) or one of two valid mezzanine cards */
191         if (!((nvm_data & NVM_COMPAT_LOM) ||
192               (hw->adapter->pdev->device == E1000_DEV_ID_82571EB_SERDES_DUAL) ||
193               (hw->adapter->pdev->device == E1000_DEV_ID_82571EB_SERDES_QUAD) ||
194               (hw->adapter->pdev->device == E1000_DEV_ID_82571EB_SERDES)))
195                 goto out;
196
197         ret_val = e1000_read_nvm(hw, NVM_ALT_MAC_ADDR_PTR, 1,
198                                  &nvm_alt_mac_addr_offset);
199         if (ret_val) {
200                 e_dbg("NVM Read Error\n");
201                 goto out;
202         }
203
204         if ((nvm_alt_mac_addr_offset == 0xFFFF) ||
205             (nvm_alt_mac_addr_offset == 0x0000))
206                 /* There is no Alternate MAC Address */
207                 goto out;
208
209         if (hw->bus.func == E1000_FUNC_1)
210                 nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN1;
211         for (i = 0; i < ETH_ALEN; i += 2) {
212                 offset = nvm_alt_mac_addr_offset + (i >> 1);
213                 ret_val = e1000_read_nvm(hw, offset, 1, &nvm_data);
214                 if (ret_val) {
215                         e_dbg("NVM Read Error\n");
216                         goto out;
217                 }
218
219                 alt_mac_addr[i] = (u8)(nvm_data & 0xFF);
220                 alt_mac_addr[i + 1] = (u8)(nvm_data >> 8);
221         }
222
223         /* if multicast bit is set, the alternate address will not be used */
224         if (is_multicast_ether_addr(alt_mac_addr)) {
225                 e_dbg("Ignoring Alternate Mac Address with MC bit set\n");
226                 goto out;
227         }
228
229         /*
230          * We have a valid alternate MAC address, and we want to treat it the
231          * same as the normal permanent MAC address stored by the HW into the
232          * RAR. Do this by mapping this address into RAR0.
233          */
234         e1000e_rar_set(hw, alt_mac_addr, 0);
235
236 out:
237         return ret_val;
238 }
239
240 /**
241  *  e1000e_rar_set - Set receive address register
242  *  @hw: pointer to the HW structure
243  *  @addr: pointer to the receive address
244  *  @index: receive address array register
245  *
246  *  Sets the receive address array register at index to the address passed
247  *  in by addr.
248  **/
249 void e1000e_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
250 {
251         u32 rar_low, rar_high;
252
253         /*
254          * HW expects these in little endian so we reverse the byte order
255          * from network order (big endian) to little endian
256          */
257         rar_low = ((u32) addr[0] |
258                    ((u32) addr[1] << 8) |
259                     ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
260
261         rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
262
263         /* If MAC address zero, no need to set the AV bit */
264         if (rar_low || rar_high)
265                 rar_high |= E1000_RAH_AV;
266
267         /*
268          * Some bridges will combine consecutive 32-bit writes into
269          * a single burst write, which will malfunction on some parts.
270          * The flushes avoid this.
271          */
272         ew32(RAL(index), rar_low);
273         e1e_flush();
274         ew32(RAH(index), rar_high);
275         e1e_flush();
276 }
277
278 /**
279  *  e1000_hash_mc_addr - Generate a multicast hash value
280  *  @hw: pointer to the HW structure
281  *  @mc_addr: pointer to a multicast address
282  *
283  *  Generates a multicast address hash value which is used to determine
284  *  the multicast filter table array address and new table value.  See
285  *  e1000_mta_set_generic()
286  **/
287 static u32 e1000_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
288 {
289         u32 hash_value, hash_mask;
290         u8 bit_shift = 0;
291
292         /* Register count multiplied by bits per register */
293         hash_mask = (hw->mac.mta_reg_count * 32) - 1;
294
295         /*
296          * For a mc_filter_type of 0, bit_shift is the number of left-shifts
297          * where 0xFF would still fall within the hash mask.
298          */
299         while (hash_mask >> bit_shift != 0xFF)
300                 bit_shift++;
301
302         /*
303          * The portion of the address that is used for the hash table
304          * is determined by the mc_filter_type setting.
305          * The algorithm is such that there is a total of 8 bits of shifting.
306          * The bit_shift for a mc_filter_type of 0 represents the number of
307          * left-shifts where the MSB of mc_addr[5] would still fall within
308          * the hash_mask.  Case 0 does this exactly.  Since there are a total
309          * of 8 bits of shifting, then mc_addr[4] will shift right the
310          * remaining number of bits. Thus 8 - bit_shift.  The rest of the
311          * cases are a variation of this algorithm...essentially raising the
312          * number of bits to shift mc_addr[5] left, while still keeping the
313          * 8-bit shifting total.
314          *
315          * For example, given the following Destination MAC Address and an
316          * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
317          * we can see that the bit_shift for case 0 is 4.  These are the hash
318          * values resulting from each mc_filter_type...
319          * [0] [1] [2] [3] [4] [5]
320          * 01  AA  00  12  34  56
321          * LSB           MSB
322          *
323          * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
324          * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
325          * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
326          * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
327          */
328         switch (hw->mac.mc_filter_type) {
329         default:
330         case 0:
331                 break;
332         case 1:
333                 bit_shift += 1;
334                 break;
335         case 2:
336                 bit_shift += 2;
337                 break;
338         case 3:
339                 bit_shift += 4;
340                 break;
341         }
342
343         hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
344                                   (((u16) mc_addr[5]) << bit_shift)));
345
346         return hash_value;
347 }
348
349 /**
350  *  e1000e_update_mc_addr_list_generic - Update Multicast addresses
351  *  @hw: pointer to the HW structure
352  *  @mc_addr_list: array of multicast addresses to program
353  *  @mc_addr_count: number of multicast addresses to program
354  *
355  *  Updates entire Multicast Table Array.
356  *  The caller must have a packed mc_addr_list of multicast addresses.
357  **/
358 void e1000e_update_mc_addr_list_generic(struct e1000_hw *hw,
359                                         u8 *mc_addr_list, u32 mc_addr_count)
360 {
361         u32 hash_value, hash_bit, hash_reg;
362         int i;
363
364         /* clear mta_shadow */
365         memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
366
367         /* update mta_shadow from mc_addr_list */
368         for (i = 0; (u32) i < mc_addr_count; i++) {
369                 hash_value = e1000_hash_mc_addr(hw, mc_addr_list);
370
371                 hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
372                 hash_bit = hash_value & 0x1F;
373
374                 hw->mac.mta_shadow[hash_reg] |= (1 << hash_bit);
375                 mc_addr_list += (ETH_ALEN);
376         }
377
378         /* replace the entire MTA table */
379         for (i = hw->mac.mta_reg_count - 1; i >= 0; i--)
380                 E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, hw->mac.mta_shadow[i]);
381         e1e_flush();
382 }
383
384 /**
385  *  e1000e_clear_hw_cntrs_base - Clear base hardware counters
386  *  @hw: pointer to the HW structure
387  *
388  *  Clears the base hardware counters by reading the counter registers.
389  **/
390 void e1000e_clear_hw_cntrs_base(struct e1000_hw *hw)
391 {
392         er32(CRCERRS);
393         er32(SYMERRS);
394         er32(MPC);
395         er32(SCC);
396         er32(ECOL);
397         er32(MCC);
398         er32(LATECOL);
399         er32(COLC);
400         er32(DC);
401         er32(SEC);
402         er32(RLEC);
403         er32(XONRXC);
404         er32(XONTXC);
405         er32(XOFFRXC);
406         er32(XOFFTXC);
407         er32(FCRUC);
408         er32(GPRC);
409         er32(BPRC);
410         er32(MPRC);
411         er32(GPTC);
412         er32(GORCL);
413         er32(GORCH);
414         er32(GOTCL);
415         er32(GOTCH);
416         er32(RNBC);
417         er32(RUC);
418         er32(RFC);
419         er32(ROC);
420         er32(RJC);
421         er32(TORL);
422         er32(TORH);
423         er32(TOTL);
424         er32(TOTH);
425         er32(TPR);
426         er32(TPT);
427         er32(MPTC);
428         er32(BPTC);
429 }
430
431 /**
432  *  e1000e_check_for_copper_link - Check for link (Copper)
433  *  @hw: pointer to the HW structure
434  *
435  *  Checks to see of the link status of the hardware has changed.  If a
436  *  change in link status has been detected, then we read the PHY registers
437  *  to get the current speed/duplex if link exists.
438  *
439  *  Returns a negative error code (-E1000_ERR_*) or 0 (link down) or 1 (link
440  *  up).
441  **/
442 s32 e1000e_check_for_copper_link(struct e1000_hw *hw)
443 {
444         struct e1000_mac_info *mac = &hw->mac;
445         s32 ret_val;
446         bool link;
447
448         /*
449          * We only want to go out to the PHY registers to see if Auto-Neg
450          * has completed and/or if our link status has changed.  The
451          * get_link_status flag is set upon receiving a Link Status
452          * Change or Rx Sequence Error interrupt.
453          */
454         if (!mac->get_link_status)
455                 return 1;
456
457         /*
458          * First we want to see if the MII Status Register reports
459          * link.  If so, then we want to get the current speed/duplex
460          * of the PHY.
461          */
462         ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
463         if (ret_val)
464                 return ret_val;
465
466         if (!link)
467                 return ret_val; /* No link detected */
468
469         mac->get_link_status = false;
470
471         /*
472          * Check if there was DownShift, must be checked
473          * immediately after link-up
474          */
475         e1000e_check_downshift(hw);
476
477         /*
478          * If we are forcing speed/duplex, then we simply return since
479          * we have already determined whether we have link or not.
480          */
481         if (!mac->autoneg)
482                 return 1;
483
484         /*
485          * Auto-Neg is enabled.  Auto Speed Detection takes care
486          * of MAC speed/duplex configuration.  So we only need to
487          * configure Collision Distance in the MAC.
488          */
489         e1000e_config_collision_dist(hw);
490
491         /*
492          * Configure Flow Control now that Auto-Neg has completed.
493          * First, we need to restore the desired flow control
494          * settings because we may have had to re-autoneg with a
495          * different link partner.
496          */
497         ret_val = e1000e_config_fc_after_link_up(hw);
498         if (ret_val) {
499                 e_dbg("Error configuring flow control\n");
500                 return ret_val;
501         }
502
503         return 1;
504 }
505
506 /**
507  *  e1000e_check_for_fiber_link - Check for link (Fiber)
508  *  @hw: pointer to the HW structure
509  *
510  *  Checks for link up on the hardware.  If link is not up and we have
511  *  a signal, then we need to force link up.
512  **/
513 s32 e1000e_check_for_fiber_link(struct e1000_hw *hw)
514 {
515         struct e1000_mac_info *mac = &hw->mac;
516         u32 rxcw;
517         u32 ctrl;
518         u32 status;
519         s32 ret_val;
520
521         ctrl = er32(CTRL);
522         status = er32(STATUS);
523         rxcw = er32(RXCW);
524
525         /*
526          * If we don't have link (auto-negotiation failed or link partner
527          * cannot auto-negotiate), the cable is plugged in (we have signal),
528          * and our link partner is not trying to auto-negotiate with us (we
529          * are receiving idles or data), we need to force link up. We also
530          * need to give auto-negotiation time to complete, in case the cable
531          * was just plugged in. The autoneg_failed flag does this.
532          */
533         /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
534         if ((ctrl & E1000_CTRL_SWDPIN1) && (!(status & E1000_STATUS_LU)) &&
535             (!(rxcw & E1000_RXCW_C))) {
536                 if (mac->autoneg_failed == 0) {
537                         mac->autoneg_failed = 1;
538                         return 0;
539                 }
540                 e_dbg("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
541
542                 /* Disable auto-negotiation in the TXCW register */
543                 ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
544
545                 /* Force link-up and also force full-duplex. */
546                 ctrl = er32(CTRL);
547                 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
548                 ew32(CTRL, ctrl);
549
550                 /* Configure Flow Control after forcing link up. */
551                 ret_val = e1000e_config_fc_after_link_up(hw);
552                 if (ret_val) {
553                         e_dbg("Error configuring flow control\n");
554                         return ret_val;
555                 }
556         } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
557                 /*
558                  * If we are forcing link and we are receiving /C/ ordered
559                  * sets, re-enable auto-negotiation in the TXCW register
560                  * and disable forced link in the Device Control register
561                  * in an attempt to auto-negotiate with our link partner.
562                  */
563                 e_dbg("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
564                 ew32(TXCW, mac->txcw);
565                 ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
566
567                 mac->serdes_has_link = true;
568         }
569
570         return 0;
571 }
572
573 /**
574  *  e1000e_check_for_serdes_link - Check for link (Serdes)
575  *  @hw: pointer to the HW structure
576  *
577  *  Checks for link up on the hardware.  If link is not up and we have
578  *  a signal, then we need to force link up.
579  **/
580 s32 e1000e_check_for_serdes_link(struct e1000_hw *hw)
581 {
582         struct e1000_mac_info *mac = &hw->mac;
583         u32 rxcw;
584         u32 ctrl;
585         u32 status;
586         s32 ret_val;
587
588         ctrl = er32(CTRL);
589         status = er32(STATUS);
590         rxcw = er32(RXCW);
591
592         /*
593          * If we don't have link (auto-negotiation failed or link partner
594          * cannot auto-negotiate), and our link partner is not trying to
595          * auto-negotiate with us (we are receiving idles or data),
596          * we need to force link up. We also need to give auto-negotiation
597          * time to complete.
598          */
599         /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
600         if ((!(status & E1000_STATUS_LU)) && (!(rxcw & E1000_RXCW_C))) {
601                 if (mac->autoneg_failed == 0) {
602                         mac->autoneg_failed = 1;
603                         return 0;
604                 }
605                 e_dbg("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
606
607                 /* Disable auto-negotiation in the TXCW register */
608                 ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
609
610                 /* Force link-up and also force full-duplex. */
611                 ctrl = er32(CTRL);
612                 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
613                 ew32(CTRL, ctrl);
614
615                 /* Configure Flow Control after forcing link up. */
616                 ret_val = e1000e_config_fc_after_link_up(hw);
617                 if (ret_val) {
618                         e_dbg("Error configuring flow control\n");
619                         return ret_val;
620                 }
621         } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
622                 /*
623                  * If we are forcing link and we are receiving /C/ ordered
624                  * sets, re-enable auto-negotiation in the TXCW register
625                  * and disable forced link in the Device Control register
626                  * in an attempt to auto-negotiate with our link partner.
627                  */
628                 e_dbg("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
629                 ew32(TXCW, mac->txcw);
630                 ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
631
632                 mac->serdes_has_link = true;
633         } else if (!(E1000_TXCW_ANE & er32(TXCW))) {
634                 /*
635                  * If we force link for non-auto-negotiation switch, check
636                  * link status based on MAC synchronization for internal
637                  * serdes media type.
638                  */
639                 /* SYNCH bit and IV bit are sticky. */
640                 udelay(10);
641                 rxcw = er32(RXCW);
642                 if (rxcw & E1000_RXCW_SYNCH) {
643                         if (!(rxcw & E1000_RXCW_IV)) {
644                                 mac->serdes_has_link = true;
645                                 e_dbg("SERDES: Link up - forced.\n");
646                         }
647                 } else {
648                         mac->serdes_has_link = false;
649                         e_dbg("SERDES: Link down - force failed.\n");
650                 }
651         }
652
653         if (E1000_TXCW_ANE & er32(TXCW)) {
654                 status = er32(STATUS);
655                 if (status & E1000_STATUS_LU) {
656                         /* SYNCH bit and IV bit are sticky, so reread rxcw.  */
657                         udelay(10);
658                         rxcw = er32(RXCW);
659                         if (rxcw & E1000_RXCW_SYNCH) {
660                                 if (!(rxcw & E1000_RXCW_IV)) {
661                                         mac->serdes_has_link = true;
662                                         e_dbg("SERDES: Link up - autoneg "
663                                            "completed successfully.\n");
664                                 } else {
665                                         mac->serdes_has_link = false;
666                                         e_dbg("SERDES: Link down - invalid"
667                                            "codewords detected in autoneg.\n");
668                                 }
669                         } else {
670                                 mac->serdes_has_link = false;
671                                 e_dbg("SERDES: Link down - no sync.\n");
672                         }
673                 } else {
674                         mac->serdes_has_link = false;
675                         e_dbg("SERDES: Link down - autoneg failed\n");
676                 }
677         }
678
679         return 0;
680 }
681
682 /**
683  *  e1000_set_default_fc_generic - Set flow control default values
684  *  @hw: pointer to the HW structure
685  *
686  *  Read the EEPROM for the default values for flow control and store the
687  *  values.
688  **/
689 static s32 e1000_set_default_fc_generic(struct e1000_hw *hw)
690 {
691         s32 ret_val;
692         u16 nvm_data;
693
694         /*
695          * Read and store word 0x0F of the EEPROM. This word contains bits
696          * that determine the hardware's default PAUSE (flow control) mode,
697          * a bit that determines whether the HW defaults to enabling or
698          * disabling auto-negotiation, and the direction of the
699          * SW defined pins. If there is no SW over-ride of the flow
700          * control setting, then the variable hw->fc will
701          * be initialized based on a value in the EEPROM.
702          */
703         ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
704
705         if (ret_val) {
706                 e_dbg("NVM Read Error\n");
707                 return ret_val;
708         }
709
710         if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == 0)
711                 hw->fc.requested_mode = e1000_fc_none;
712         else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
713                  NVM_WORD0F_ASM_DIR)
714                 hw->fc.requested_mode = e1000_fc_tx_pause;
715         else
716                 hw->fc.requested_mode = e1000_fc_full;
717
718         return 0;
719 }
720
721 /**
722  *  e1000e_setup_link - Setup flow control and link settings
723  *  @hw: pointer to the HW structure
724  *
725  *  Determines which flow control settings to use, then configures flow
726  *  control.  Calls the appropriate media-specific link configuration
727  *  function.  Assuming the adapter has a valid link partner, a valid link
728  *  should be established.  Assumes the hardware has previously been reset
729  *  and the transmitter and receiver are not enabled.
730  **/
731 s32 e1000e_setup_link(struct e1000_hw *hw)
732 {
733         struct e1000_mac_info *mac = &hw->mac;
734         s32 ret_val;
735
736         /*
737          * In the case of the phy reset being blocked, we already have a link.
738          * We do not need to set it up again.
739          */
740         if (e1000_check_reset_block(hw))
741                 return 0;
742
743         /*
744          * If requested flow control is set to default, set flow control
745          * based on the EEPROM flow control settings.
746          */
747         if (hw->fc.requested_mode == e1000_fc_default) {
748                 ret_val = e1000_set_default_fc_generic(hw);
749                 if (ret_val)
750                         return ret_val;
751         }
752
753         /*
754          * Save off the requested flow control mode for use later.  Depending
755          * on the link partner's capabilities, we may or may not use this mode.
756          */
757         hw->fc.current_mode = hw->fc.requested_mode;
758
759         e_dbg("After fix-ups FlowControl is now = %x\n",
760                 hw->fc.current_mode);
761
762         /* Call the necessary media_type subroutine to configure the link. */
763         ret_val = mac->ops.setup_physical_interface(hw);
764         if (ret_val)
765                 return ret_val;
766
767         /*
768          * Initialize the flow control address, type, and PAUSE timer
769          * registers to their default values.  This is done even if flow
770          * control is disabled, because it does not hurt anything to
771          * initialize these registers.
772          */
773         e_dbg("Initializing the Flow Control address, type and timer regs\n");
774         ew32(FCT, FLOW_CONTROL_TYPE);
775         ew32(FCAH, FLOW_CONTROL_ADDRESS_HIGH);
776         ew32(FCAL, FLOW_CONTROL_ADDRESS_LOW);
777
778         ew32(FCTTV, hw->fc.pause_time);
779
780         return e1000e_set_fc_watermarks(hw);
781 }
782
783 /**
784  *  e1000_commit_fc_settings_generic - Configure flow control
785  *  @hw: pointer to the HW structure
786  *
787  *  Write the flow control settings to the Transmit Config Word Register (TXCW)
788  *  base on the flow control settings in e1000_mac_info.
789  **/
790 static s32 e1000_commit_fc_settings_generic(struct e1000_hw *hw)
791 {
792         struct e1000_mac_info *mac = &hw->mac;
793         u32 txcw;
794
795         /*
796          * Check for a software override of the flow control settings, and
797          * setup the device accordingly.  If auto-negotiation is enabled, then
798          * software will have to set the "PAUSE" bits to the correct value in
799          * the Transmit Config Word Register (TXCW) and re-start auto-
800          * negotiation.  However, if auto-negotiation is disabled, then
801          * software will have to manually configure the two flow control enable
802          * bits in the CTRL register.
803          *
804          * The possible values of the "fc" parameter are:
805          *      0:  Flow control is completely disabled
806          *      1:  Rx flow control is enabled (we can receive pause frames,
807          *          but not send pause frames).
808          *      2:  Tx flow control is enabled (we can send pause frames but we
809          *          do not support receiving pause frames).
810          *      3:  Both Rx and Tx flow control (symmetric) are enabled.
811          */
812         switch (hw->fc.current_mode) {
813         case e1000_fc_none:
814                 /* Flow control completely disabled by a software over-ride. */
815                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
816                 break;
817         case e1000_fc_rx_pause:
818                 /*
819                  * Rx Flow control is enabled and Tx Flow control is disabled
820                  * by a software over-ride. Since there really isn't a way to
821                  * advertise that we are capable of Rx Pause ONLY, we will
822                  * advertise that we support both symmetric and asymmetric Rx
823                  * PAUSE.  Later, we will disable the adapter's ability to send
824                  * PAUSE frames.
825                  */
826                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
827                 break;
828         case e1000_fc_tx_pause:
829                 /*
830                  * Tx Flow control is enabled, and Rx Flow control is disabled,
831                  * by a software over-ride.
832                  */
833                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
834                 break;
835         case e1000_fc_full:
836                 /*
837                  * Flow control (both Rx and Tx) is enabled by a software
838                  * over-ride.
839                  */
840                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
841                 break;
842         default:
843                 e_dbg("Flow control param set incorrectly\n");
844                 return -E1000_ERR_CONFIG;
845                 break;
846         }
847
848         ew32(TXCW, txcw);
849         mac->txcw = txcw;
850
851         return 0;
852 }
853
854 /**
855  *  e1000_poll_fiber_serdes_link_generic - Poll for link up
856  *  @hw: pointer to the HW structure
857  *
858  *  Polls for link up by reading the status register, if link fails to come
859  *  up with auto-negotiation, then the link is forced if a signal is detected.
860  **/
861 static s32 e1000_poll_fiber_serdes_link_generic(struct e1000_hw *hw)
862 {
863         struct e1000_mac_info *mac = &hw->mac;
864         u32 i, status;
865         s32 ret_val;
866
867         /*
868          * If we have a signal (the cable is plugged in, or assumed true for
869          * serdes media) then poll for a "Link-Up" indication in the Device
870          * Status Register.  Time-out if a link isn't seen in 500 milliseconds
871          * seconds (Auto-negotiation should complete in less than 500
872          * milliseconds even if the other end is doing it in SW).
873          */
874         for (i = 0; i < FIBER_LINK_UP_LIMIT; i++) {
875                 usleep_range(10000, 20000);
876                 status = er32(STATUS);
877                 if (status & E1000_STATUS_LU)
878                         break;
879         }
880         if (i == FIBER_LINK_UP_LIMIT) {
881                 e_dbg("Never got a valid link from auto-neg!!!\n");
882                 mac->autoneg_failed = 1;
883                 /*
884                  * AutoNeg failed to achieve a link, so we'll call
885                  * mac->check_for_link. This routine will force the
886                  * link up if we detect a signal. This will allow us to
887                  * communicate with non-autonegotiating link partners.
888                  */
889                 ret_val = mac->ops.check_for_link(hw);
890                 if (ret_val) {
891                         e_dbg("Error while checking for link\n");
892                         return ret_val;
893                 }
894                 mac->autoneg_failed = 0;
895         } else {
896                 mac->autoneg_failed = 0;
897                 e_dbg("Valid Link Found\n");
898         }
899
900         return 0;
901 }
902
903 /**
904  *  e1000e_setup_fiber_serdes_link - Setup link for fiber/serdes
905  *  @hw: pointer to the HW structure
906  *
907  *  Configures collision distance and flow control for fiber and serdes
908  *  links.  Upon successful setup, poll for link.
909  **/
910 s32 e1000e_setup_fiber_serdes_link(struct e1000_hw *hw)
911 {
912         u32 ctrl;
913         s32 ret_val;
914
915         ctrl = er32(CTRL);
916
917         /* Take the link out of reset */
918         ctrl &= ~E1000_CTRL_LRST;
919
920         e1000e_config_collision_dist(hw);
921
922         ret_val = e1000_commit_fc_settings_generic(hw);
923         if (ret_val)
924                 return ret_val;
925
926         /*
927          * Since auto-negotiation is enabled, take the link out of reset (the
928          * link will be in reset, because we previously reset the chip). This
929          * will restart auto-negotiation.  If auto-negotiation is successful
930          * then the link-up status bit will be set and the flow control enable
931          * bits (RFCE and TFCE) will be set according to their negotiated value.
932          */
933         e_dbg("Auto-negotiation enabled\n");
934
935         ew32(CTRL, ctrl);
936         e1e_flush();
937         usleep_range(1000, 2000);
938
939         /*
940          * For these adapters, the SW definable pin 1 is set when the optics
941          * detect a signal.  If we have a signal, then poll for a "Link-Up"
942          * indication.
943          */
944         if (hw->phy.media_type == e1000_media_type_internal_serdes ||
945             (er32(CTRL) & E1000_CTRL_SWDPIN1)) {
946                 ret_val = e1000_poll_fiber_serdes_link_generic(hw);
947         } else {
948                 e_dbg("No signal detected\n");
949         }
950
951         return 0;
952 }
953
954 /**
955  *  e1000e_config_collision_dist - Configure collision distance
956  *  @hw: pointer to the HW structure
957  *
958  *  Configures the collision distance to the default value and is used
959  *  during link setup. Currently no func pointer exists and all
960  *  implementations are handled in the generic version of this function.
961  **/
962 void e1000e_config_collision_dist(struct e1000_hw *hw)
963 {
964         u32 tctl;
965
966         tctl = er32(TCTL);
967
968         tctl &= ~E1000_TCTL_COLD;
969         tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
970
971         ew32(TCTL, tctl);
972         e1e_flush();
973 }
974
975 /**
976  *  e1000e_set_fc_watermarks - Set flow control high/low watermarks
977  *  @hw: pointer to the HW structure
978  *
979  *  Sets the flow control high/low threshold (watermark) registers.  If
980  *  flow control XON frame transmission is enabled, then set XON frame
981  *  transmission as well.
982  **/
983 s32 e1000e_set_fc_watermarks(struct e1000_hw *hw)
984 {
985         u32 fcrtl = 0, fcrth = 0;
986
987         /*
988          * Set the flow control receive threshold registers.  Normally,
989          * these registers will be set to a default threshold that may be
990          * adjusted later by the driver's runtime code.  However, if the
991          * ability to transmit pause frames is not enabled, then these
992          * registers will be set to 0.
993          */
994         if (hw->fc.current_mode & e1000_fc_tx_pause) {
995                 /*
996                  * We need to set up the Receive Threshold high and low water
997                  * marks as well as (optionally) enabling the transmission of
998                  * XON frames.
999                  */
1000                 fcrtl = hw->fc.low_water;
1001                 fcrtl |= E1000_FCRTL_XONE;
1002                 fcrth = hw->fc.high_water;
1003         }
1004         ew32(FCRTL, fcrtl);
1005         ew32(FCRTH, fcrth);
1006
1007         return 0;
1008 }
1009
1010 /**
1011  *  e1000e_force_mac_fc - Force the MAC's flow control settings
1012  *  @hw: pointer to the HW structure
1013  *
1014  *  Force the MAC's flow control settings.  Sets the TFCE and RFCE bits in the
1015  *  device control register to reflect the adapter settings.  TFCE and RFCE
1016  *  need to be explicitly set by software when a copper PHY is used because
1017  *  autonegotiation is managed by the PHY rather than the MAC.  Software must
1018  *  also configure these bits when link is forced on a fiber connection.
1019  **/
1020 s32 e1000e_force_mac_fc(struct e1000_hw *hw)
1021 {
1022         u32 ctrl;
1023
1024         ctrl = er32(CTRL);
1025
1026         /*
1027          * Because we didn't get link via the internal auto-negotiation
1028          * mechanism (we either forced link or we got link via PHY
1029          * auto-neg), we have to manually enable/disable transmit an
1030          * receive flow control.
1031          *
1032          * The "Case" statement below enables/disable flow control
1033          * according to the "hw->fc.current_mode" parameter.
1034          *
1035          * The possible values of the "fc" parameter are:
1036          *      0:  Flow control is completely disabled
1037          *      1:  Rx flow control is enabled (we can receive pause
1038          *          frames but not send pause frames).
1039          *      2:  Tx flow control is enabled (we can send pause frames
1040          *          frames but we do not receive pause frames).
1041          *      3:  Both Rx and Tx flow control (symmetric) is enabled.
1042          *  other:  No other values should be possible at this point.
1043          */
1044         e_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
1045
1046         switch (hw->fc.current_mode) {
1047         case e1000_fc_none:
1048                 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1049                 break;
1050         case e1000_fc_rx_pause:
1051                 ctrl &= (~E1000_CTRL_TFCE);
1052                 ctrl |= E1000_CTRL_RFCE;
1053                 break;
1054         case e1000_fc_tx_pause:
1055                 ctrl &= (~E1000_CTRL_RFCE);
1056                 ctrl |= E1000_CTRL_TFCE;
1057                 break;
1058         case e1000_fc_full:
1059                 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1060                 break;
1061         default:
1062                 e_dbg("Flow control param set incorrectly\n");
1063                 return -E1000_ERR_CONFIG;
1064         }
1065
1066         ew32(CTRL, ctrl);
1067
1068         return 0;
1069 }
1070
1071 /**
1072  *  e1000e_config_fc_after_link_up - Configures flow control after link
1073  *  @hw: pointer to the HW structure
1074  *
1075  *  Checks the status of auto-negotiation after link up to ensure that the
1076  *  speed and duplex were not forced.  If the link needed to be forced, then
1077  *  flow control needs to be forced also.  If auto-negotiation is enabled
1078  *  and did not fail, then we configure flow control based on our link
1079  *  partner.
1080  **/
1081 s32 e1000e_config_fc_after_link_up(struct e1000_hw *hw)
1082 {
1083         struct e1000_mac_info *mac = &hw->mac;
1084         s32 ret_val = 0;
1085         u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
1086         u16 speed, duplex;
1087
1088         /*
1089          * Check for the case where we have fiber media and auto-neg failed
1090          * so we had to force link.  In this case, we need to force the
1091          * configuration of the MAC to match the "fc" parameter.
1092          */
1093         if (mac->autoneg_failed) {
1094                 if (hw->phy.media_type == e1000_media_type_fiber ||
1095                     hw->phy.media_type == e1000_media_type_internal_serdes)
1096                         ret_val = e1000e_force_mac_fc(hw);
1097         } else {
1098                 if (hw->phy.media_type == e1000_media_type_copper)
1099                         ret_val = e1000e_force_mac_fc(hw);
1100         }
1101
1102         if (ret_val) {
1103                 e_dbg("Error forcing flow control settings\n");
1104                 return ret_val;
1105         }
1106
1107         /*
1108          * Check for the case where we have copper media and auto-neg is
1109          * enabled.  In this case, we need to check and see if Auto-Neg
1110          * has completed, and if so, how the PHY and link partner has
1111          * flow control configured.
1112          */
1113         if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
1114                 /*
1115                  * Read the MII Status Register and check to see if AutoNeg
1116                  * has completed.  We read this twice because this reg has
1117                  * some "sticky" (latched) bits.
1118                  */
1119                 ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
1120                 if (ret_val)
1121                         return ret_val;
1122                 ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
1123                 if (ret_val)
1124                         return ret_val;
1125
1126                 if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
1127                         e_dbg("Copper PHY and Auto Neg "
1128                                  "has not completed.\n");
1129                         return ret_val;
1130                 }
1131
1132                 /*
1133                  * The AutoNeg process has completed, so we now need to
1134                  * read both the Auto Negotiation Advertisement
1135                  * Register (Address 4) and the Auto_Negotiation Base
1136                  * Page Ability Register (Address 5) to determine how
1137                  * flow control was negotiated.
1138                  */
1139                 ret_val = e1e_rphy(hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg);
1140                 if (ret_val)
1141                         return ret_val;
1142                 ret_val =
1143                     e1e_rphy(hw, PHY_LP_ABILITY, &mii_nway_lp_ability_reg);
1144                 if (ret_val)
1145                         return ret_val;
1146
1147                 /*
1148                  * Two bits in the Auto Negotiation Advertisement Register
1149                  * (Address 4) and two bits in the Auto Negotiation Base
1150                  * Page Ability Register (Address 5) determine flow control
1151                  * for both the PHY and the link partner.  The following
1152                  * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1153                  * 1999, describes these PAUSE resolution bits and how flow
1154                  * control is determined based upon these settings.
1155                  * NOTE:  DC = Don't Care
1156                  *
1157                  *   LOCAL DEVICE  |   LINK PARTNER
1158                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1159                  *-------|---------|-------|---------|--------------------
1160                  *   0   |    0    |  DC   |   DC    | e1000_fc_none
1161                  *   0   |    1    |   0   |   DC    | e1000_fc_none
1162                  *   0   |    1    |   1   |    0    | e1000_fc_none
1163                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1164                  *   1   |    0    |   0   |   DC    | e1000_fc_none
1165                  *   1   |   DC    |   1   |   DC    | e1000_fc_full
1166                  *   1   |    1    |   0   |    0    | e1000_fc_none
1167                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1168                  *
1169                  * Are both PAUSE bits set to 1?  If so, this implies
1170                  * Symmetric Flow Control is enabled at both ends.  The
1171                  * ASM_DIR bits are irrelevant per the spec.
1172                  *
1173                  * For Symmetric Flow Control:
1174                  *
1175                  *   LOCAL DEVICE  |   LINK PARTNER
1176                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1177                  *-------|---------|-------|---------|--------------------
1178                  *   1   |   DC    |   1   |   DC    | E1000_fc_full
1179                  *
1180                  */
1181                 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1182                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
1183                         /*
1184                          * Now we need to check if the user selected Rx ONLY
1185                          * of pause frames.  In this case, we had to advertise
1186                          * FULL flow control because we could not advertise Rx
1187                          * ONLY. Hence, we must now check to see if we need to
1188                          * turn OFF the TRANSMISSION of PAUSE frames.
1189                          */
1190                         if (hw->fc.requested_mode == e1000_fc_full) {
1191                                 hw->fc.current_mode = e1000_fc_full;
1192                                 e_dbg("Flow Control = FULL.\r\n");
1193                         } else {
1194                                 hw->fc.current_mode = e1000_fc_rx_pause;
1195                                 e_dbg("Flow Control = "
1196                                       "Rx PAUSE frames only.\r\n");
1197                         }
1198                 }
1199                 /*
1200                  * For receiving PAUSE frames ONLY.
1201                  *
1202                  *   LOCAL DEVICE  |   LINK PARTNER
1203                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1204                  *-------|---------|-------|---------|--------------------
1205                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1206                  */
1207                 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1208                           (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1209                           (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1210                           (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1211                         hw->fc.current_mode = e1000_fc_tx_pause;
1212                         e_dbg("Flow Control = Tx PAUSE frames only.\r\n");
1213                 }
1214                 /*
1215                  * For transmitting PAUSE frames ONLY.
1216                  *
1217                  *   LOCAL DEVICE  |   LINK PARTNER
1218                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1219                  *-------|---------|-------|---------|--------------------
1220                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1221                  */
1222                 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1223                          (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1224                          !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1225                          (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1226                         hw->fc.current_mode = e1000_fc_rx_pause;
1227                         e_dbg("Flow Control = Rx PAUSE frames only.\r\n");
1228                 } else {
1229                         /*
1230                          * Per the IEEE spec, at this point flow control
1231                          * should be disabled.
1232                          */
1233                         hw->fc.current_mode = e1000_fc_none;
1234                         e_dbg("Flow Control = NONE.\r\n");
1235                 }
1236
1237                 /*
1238                  * Now we need to do one last check...  If we auto-
1239                  * negotiated to HALF DUPLEX, flow control should not be
1240                  * enabled per IEEE 802.3 spec.
1241                  */
1242                 ret_val = mac->ops.get_link_up_info(hw, &speed, &duplex);
1243                 if (ret_val) {
1244                         e_dbg("Error getting link speed and duplex\n");
1245                         return ret_val;
1246                 }
1247
1248                 if (duplex == HALF_DUPLEX)
1249                         hw->fc.current_mode = e1000_fc_none;
1250
1251                 /*
1252                  * Now we call a subroutine to actually force the MAC
1253                  * controller to use the correct flow control settings.
1254                  */
1255                 ret_val = e1000e_force_mac_fc(hw);
1256                 if (ret_val) {
1257                         e_dbg("Error forcing flow control settings\n");
1258                         return ret_val;
1259                 }
1260         }
1261
1262         return 0;
1263 }
1264
1265 /**
1266  *  e1000e_get_speed_and_duplex_copper - Retrieve current speed/duplex
1267  *  @hw: pointer to the HW structure
1268  *  @speed: stores the current speed
1269  *  @duplex: stores the current duplex
1270  *
1271  *  Read the status register for the current speed/duplex and store the current
1272  *  speed and duplex for copper connections.
1273  **/
1274 s32 e1000e_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed, u16 *duplex)
1275 {
1276         u32 status;
1277
1278         status = er32(STATUS);
1279         if (status & E1000_STATUS_SPEED_1000)
1280                 *speed = SPEED_1000;
1281         else if (status & E1000_STATUS_SPEED_100)
1282                 *speed = SPEED_100;
1283         else
1284                 *speed = SPEED_10;
1285
1286         if (status & E1000_STATUS_FD)
1287                 *duplex = FULL_DUPLEX;
1288         else
1289                 *duplex = HALF_DUPLEX;
1290
1291         e_dbg("%u Mbps, %s Duplex\n",
1292               *speed == SPEED_1000 ? 1000 : *speed == SPEED_100 ? 100 : 10,
1293               *duplex == FULL_DUPLEX ? "Full" : "Half");
1294
1295         return 0;
1296 }
1297
1298 /**
1299  *  e1000e_get_speed_and_duplex_fiber_serdes - Retrieve current speed/duplex
1300  *  @hw: pointer to the HW structure
1301  *  @speed: stores the current speed
1302  *  @duplex: stores the current duplex
1303  *
1304  *  Sets the speed and duplex to gigabit full duplex (the only possible option)
1305  *  for fiber/serdes links.
1306  **/
1307 s32 e1000e_get_speed_and_duplex_fiber_serdes(struct e1000_hw *hw, u16 *speed, u16 *duplex)
1308 {
1309         *speed = SPEED_1000;
1310         *duplex = FULL_DUPLEX;
1311
1312         return 0;
1313 }
1314
1315 /**
1316  *  e1000e_get_hw_semaphore - Acquire hardware semaphore
1317  *  @hw: pointer to the HW structure
1318  *
1319  *  Acquire the HW semaphore to access the PHY or NVM
1320  **/
1321 s32 e1000e_get_hw_semaphore(struct e1000_hw *hw)
1322 {
1323         u32 swsm;
1324         s32 timeout = hw->nvm.word_size + 1;
1325         s32 i = 0;
1326
1327         /* Get the SW semaphore */
1328         while (i < timeout) {
1329                 swsm = er32(SWSM);
1330                 if (!(swsm & E1000_SWSM_SMBI))
1331                         break;
1332
1333                 udelay(50);
1334                 i++;
1335         }
1336
1337         if (i == timeout) {
1338                 e_dbg("Driver can't access device - SMBI bit is set.\n");
1339                 return -E1000_ERR_NVM;
1340         }
1341
1342         /* Get the FW semaphore. */
1343         for (i = 0; i < timeout; i++) {
1344                 swsm = er32(SWSM);
1345                 ew32(SWSM, swsm | E1000_SWSM_SWESMBI);
1346
1347                 /* Semaphore acquired if bit latched */
1348                 if (er32(SWSM) & E1000_SWSM_SWESMBI)
1349                         break;
1350
1351                 udelay(50);
1352         }
1353
1354         if (i == timeout) {
1355                 /* Release semaphores */
1356                 e1000e_put_hw_semaphore(hw);
1357                 e_dbg("Driver can't access the NVM\n");
1358                 return -E1000_ERR_NVM;
1359         }
1360
1361         return 0;
1362 }
1363
1364 /**
1365  *  e1000e_put_hw_semaphore - Release hardware semaphore
1366  *  @hw: pointer to the HW structure
1367  *
1368  *  Release hardware semaphore used to access the PHY or NVM
1369  **/
1370 void e1000e_put_hw_semaphore(struct e1000_hw *hw)
1371 {
1372         u32 swsm;
1373
1374         swsm = er32(SWSM);
1375         swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1376         ew32(SWSM, swsm);
1377 }
1378
1379 /**
1380  *  e1000e_get_auto_rd_done - Check for auto read completion
1381  *  @hw: pointer to the HW structure
1382  *
1383  *  Check EEPROM for Auto Read done bit.
1384  **/
1385 s32 e1000e_get_auto_rd_done(struct e1000_hw *hw)
1386 {
1387         s32 i = 0;
1388
1389         while (i < AUTO_READ_DONE_TIMEOUT) {
1390                 if (er32(EECD) & E1000_EECD_AUTO_RD)
1391                         break;
1392                 usleep_range(1000, 2000);
1393                 i++;
1394         }
1395
1396         if (i == AUTO_READ_DONE_TIMEOUT) {
1397                 e_dbg("Auto read by HW from NVM has not completed.\n");
1398                 return -E1000_ERR_RESET;
1399         }
1400
1401         return 0;
1402 }
1403
1404 /**
1405  *  e1000e_valid_led_default - Verify a valid default LED config
1406  *  @hw: pointer to the HW structure
1407  *  @data: pointer to the NVM (EEPROM)
1408  *
1409  *  Read the EEPROM for the current default LED configuration.  If the
1410  *  LED configuration is not valid, set to a valid LED configuration.
1411  **/
1412 s32 e1000e_valid_led_default(struct e1000_hw *hw, u16 *data)
1413 {
1414         s32 ret_val;
1415
1416         ret_val = e1000_read_nvm(hw, NVM_ID_LED_SETTINGS, 1, data);
1417         if (ret_val) {
1418                 e_dbg("NVM Read Error\n");
1419                 return ret_val;
1420         }
1421
1422         if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF)
1423                 *data = ID_LED_DEFAULT;
1424
1425         return 0;
1426 }
1427
1428 /**
1429  *  e1000e_id_led_init -
1430  *  @hw: pointer to the HW structure
1431  *
1432  **/
1433 s32 e1000e_id_led_init(struct e1000_hw *hw)
1434 {
1435         struct e1000_mac_info *mac = &hw->mac;
1436         s32 ret_val;
1437         const u32 ledctl_mask = 0x000000FF;
1438         const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
1439         const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
1440         u16 data, i, temp;
1441         const u16 led_mask = 0x0F;
1442
1443         ret_val = hw->nvm.ops.valid_led_default(hw, &data);
1444         if (ret_val)
1445                 return ret_val;
1446
1447         mac->ledctl_default = er32(LEDCTL);
1448         mac->ledctl_mode1 = mac->ledctl_default;
1449         mac->ledctl_mode2 = mac->ledctl_default;
1450
1451         for (i = 0; i < 4; i++) {
1452                 temp = (data >> (i << 2)) & led_mask;
1453                 switch (temp) {
1454                 case ID_LED_ON1_DEF2:
1455                 case ID_LED_ON1_ON2:
1456                 case ID_LED_ON1_OFF2:
1457                         mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1458                         mac->ledctl_mode1 |= ledctl_on << (i << 3);
1459                         break;
1460                 case ID_LED_OFF1_DEF2:
1461                 case ID_LED_OFF1_ON2:
1462                 case ID_LED_OFF1_OFF2:
1463                         mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1464                         mac->ledctl_mode1 |= ledctl_off << (i << 3);
1465                         break;
1466                 default:
1467                         /* Do nothing */
1468                         break;
1469                 }
1470                 switch (temp) {
1471                 case ID_LED_DEF1_ON2:
1472                 case ID_LED_ON1_ON2:
1473                 case ID_LED_OFF1_ON2:
1474                         mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1475                         mac->ledctl_mode2 |= ledctl_on << (i << 3);
1476                         break;
1477                 case ID_LED_DEF1_OFF2:
1478                 case ID_LED_ON1_OFF2:
1479                 case ID_LED_OFF1_OFF2:
1480                         mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1481                         mac->ledctl_mode2 |= ledctl_off << (i << 3);
1482                         break;
1483                 default:
1484                         /* Do nothing */
1485                         break;
1486                 }
1487         }
1488
1489         return 0;
1490 }
1491
1492 /**
1493  *  e1000e_setup_led_generic - Configures SW controllable LED
1494  *  @hw: pointer to the HW structure
1495  *
1496  *  This prepares the SW controllable LED for use and saves the current state
1497  *  of the LED so it can be later restored.
1498  **/
1499 s32 e1000e_setup_led_generic(struct e1000_hw *hw)
1500 {
1501         u32 ledctl;
1502
1503         if (hw->mac.ops.setup_led != e1000e_setup_led_generic)
1504                 return -E1000_ERR_CONFIG;
1505
1506         if (hw->phy.media_type == e1000_media_type_fiber) {
1507                 ledctl = er32(LEDCTL);
1508                 hw->mac.ledctl_default = ledctl;
1509                 /* Turn off LED0 */
1510                 ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
1511                             E1000_LEDCTL_LED0_BLINK |
1512                             E1000_LEDCTL_LED0_MODE_MASK);
1513                 ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
1514                            E1000_LEDCTL_LED0_MODE_SHIFT);
1515                 ew32(LEDCTL, ledctl);
1516         } else if (hw->phy.media_type == e1000_media_type_copper) {
1517                 ew32(LEDCTL, hw->mac.ledctl_mode1);
1518         }
1519
1520         return 0;
1521 }
1522
1523 /**
1524  *  e1000e_cleanup_led_generic - Set LED config to default operation
1525  *  @hw: pointer to the HW structure
1526  *
1527  *  Remove the current LED configuration and set the LED configuration
1528  *  to the default value, saved from the EEPROM.
1529  **/
1530 s32 e1000e_cleanup_led_generic(struct e1000_hw *hw)
1531 {
1532         ew32(LEDCTL, hw->mac.ledctl_default);
1533         return 0;
1534 }
1535
1536 /**
1537  *  e1000e_blink_led_generic - Blink LED
1538  *  @hw: pointer to the HW structure
1539  *
1540  *  Blink the LEDs which are set to be on.
1541  **/
1542 s32 e1000e_blink_led_generic(struct e1000_hw *hw)
1543 {
1544         u32 ledctl_blink = 0;
1545         u32 i;
1546
1547         if (hw->phy.media_type == e1000_media_type_fiber) {
1548                 /* always blink LED0 for PCI-E fiber */
1549                 ledctl_blink = E1000_LEDCTL_LED0_BLINK |
1550                      (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
1551         } else {
1552                 /*
1553                  * set the blink bit for each LED that's "on" (0x0E)
1554                  * in ledctl_mode2
1555                  */
1556                 ledctl_blink = hw->mac.ledctl_mode2;
1557                 for (i = 0; i < 4; i++)
1558                         if (((hw->mac.ledctl_mode2 >> (i * 8)) & 0xFF) ==
1559                             E1000_LEDCTL_MODE_LED_ON)
1560                                 ledctl_blink |= (E1000_LEDCTL_LED0_BLINK <<
1561                                                  (i * 8));
1562         }
1563
1564         ew32(LEDCTL, ledctl_blink);
1565
1566         return 0;
1567 }
1568
1569 /**
1570  *  e1000e_led_on_generic - Turn LED on
1571  *  @hw: pointer to the HW structure
1572  *
1573  *  Turn LED on.
1574  **/
1575 s32 e1000e_led_on_generic(struct e1000_hw *hw)
1576 {
1577         u32 ctrl;
1578
1579         switch (hw->phy.media_type) {
1580         case e1000_media_type_fiber:
1581                 ctrl = er32(CTRL);
1582                 ctrl &= ~E1000_CTRL_SWDPIN0;
1583                 ctrl |= E1000_CTRL_SWDPIO0;
1584                 ew32(CTRL, ctrl);
1585                 break;
1586         case e1000_media_type_copper:
1587                 ew32(LEDCTL, hw->mac.ledctl_mode2);
1588                 break;
1589         default:
1590                 break;
1591         }
1592
1593         return 0;
1594 }
1595
1596 /**
1597  *  e1000e_led_off_generic - Turn LED off
1598  *  @hw: pointer to the HW structure
1599  *
1600  *  Turn LED off.
1601  **/
1602 s32 e1000e_led_off_generic(struct e1000_hw *hw)
1603 {
1604         u32 ctrl;
1605
1606         switch (hw->phy.media_type) {
1607         case e1000_media_type_fiber:
1608                 ctrl = er32(CTRL);
1609                 ctrl |= E1000_CTRL_SWDPIN0;
1610                 ctrl |= E1000_CTRL_SWDPIO0;
1611                 ew32(CTRL, ctrl);
1612                 break;
1613         case e1000_media_type_copper:
1614                 ew32(LEDCTL, hw->mac.ledctl_mode1);
1615                 break;
1616         default:
1617                 break;
1618         }
1619
1620         return 0;
1621 }
1622
1623 /**
1624  *  e1000e_set_pcie_no_snoop - Set PCI-express capabilities
1625  *  @hw: pointer to the HW structure
1626  *  @no_snoop: bitmap of snoop events
1627  *
1628  *  Set the PCI-express register to snoop for events enabled in 'no_snoop'.
1629  **/
1630 void e1000e_set_pcie_no_snoop(struct e1000_hw *hw, u32 no_snoop)
1631 {
1632         u32 gcr;
1633
1634         if (no_snoop) {
1635                 gcr = er32(GCR);
1636                 gcr &= ~(PCIE_NO_SNOOP_ALL);
1637                 gcr |= no_snoop;
1638                 ew32(GCR, gcr);
1639         }
1640 }
1641
1642 /**
1643  *  e1000e_disable_pcie_master - Disables PCI-express master access
1644  *  @hw: pointer to the HW structure
1645  *
1646  *  Returns 0 if successful, else returns -10
1647  *  (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
1648  *  the master requests to be disabled.
1649  *
1650  *  Disables PCI-Express master access and verifies there are no pending
1651  *  requests.
1652  **/
1653 s32 e1000e_disable_pcie_master(struct e1000_hw *hw)
1654 {
1655         u32 ctrl;
1656         s32 timeout = MASTER_DISABLE_TIMEOUT;
1657
1658         ctrl = er32(CTRL);
1659         ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
1660         ew32(CTRL, ctrl);
1661
1662         while (timeout) {
1663                 if (!(er32(STATUS) &
1664                       E1000_STATUS_GIO_MASTER_ENABLE))
1665                         break;
1666                 udelay(100);
1667                 timeout--;
1668         }
1669
1670         if (!timeout) {
1671                 e_dbg("Master requests are pending.\n");
1672                 return -E1000_ERR_MASTER_REQUESTS_PENDING;
1673         }
1674
1675         return 0;
1676 }
1677
1678 /**
1679  *  e1000e_reset_adaptive - Reset Adaptive Interframe Spacing
1680  *  @hw: pointer to the HW structure
1681  *
1682  *  Reset the Adaptive Interframe Spacing throttle to default values.
1683  **/
1684 void e1000e_reset_adaptive(struct e1000_hw *hw)
1685 {
1686         struct e1000_mac_info *mac = &hw->mac;
1687
1688         if (!mac->adaptive_ifs) {
1689                 e_dbg("Not in Adaptive IFS mode!\n");
1690                 goto out;
1691         }
1692
1693         mac->current_ifs_val = 0;
1694         mac->ifs_min_val = IFS_MIN;
1695         mac->ifs_max_val = IFS_MAX;
1696         mac->ifs_step_size = IFS_STEP;
1697         mac->ifs_ratio = IFS_RATIO;
1698
1699         mac->in_ifs_mode = false;
1700         ew32(AIT, 0);
1701 out:
1702         return;
1703 }
1704
1705 /**
1706  *  e1000e_update_adaptive - Update Adaptive Interframe Spacing
1707  *  @hw: pointer to the HW structure
1708  *
1709  *  Update the Adaptive Interframe Spacing Throttle value based on the
1710  *  time between transmitted packets and time between collisions.
1711  **/
1712 void e1000e_update_adaptive(struct e1000_hw *hw)
1713 {
1714         struct e1000_mac_info *mac = &hw->mac;
1715
1716         if (!mac->adaptive_ifs) {
1717                 e_dbg("Not in Adaptive IFS mode!\n");
1718                 goto out;
1719         }
1720
1721         if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
1722                 if (mac->tx_packet_delta > MIN_NUM_XMITS) {
1723                         mac->in_ifs_mode = true;
1724                         if (mac->current_ifs_val < mac->ifs_max_val) {
1725                                 if (!mac->current_ifs_val)
1726                                         mac->current_ifs_val = mac->ifs_min_val;
1727                                 else
1728                                         mac->current_ifs_val +=
1729                                                 mac->ifs_step_size;
1730                                 ew32(AIT, mac->current_ifs_val);
1731                         }
1732                 }
1733         } else {
1734                 if (mac->in_ifs_mode &&
1735                     (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
1736                         mac->current_ifs_val = 0;
1737                         mac->in_ifs_mode = false;
1738                         ew32(AIT, 0);
1739                 }
1740         }
1741 out:
1742         return;
1743 }
1744
1745 /**
1746  *  e1000_raise_eec_clk - Raise EEPROM clock
1747  *  @hw: pointer to the HW structure
1748  *  @eecd: pointer to the EEPROM
1749  *
1750  *  Enable/Raise the EEPROM clock bit.
1751  **/
1752 static void e1000_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
1753 {
1754         *eecd = *eecd | E1000_EECD_SK;
1755         ew32(EECD, *eecd);
1756         e1e_flush();
1757         udelay(hw->nvm.delay_usec);
1758 }
1759
1760 /**
1761  *  e1000_lower_eec_clk - Lower EEPROM clock
1762  *  @hw: pointer to the HW structure
1763  *  @eecd: pointer to the EEPROM
1764  *
1765  *  Clear/Lower the EEPROM clock bit.
1766  **/
1767 static void e1000_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
1768 {
1769         *eecd = *eecd & ~E1000_EECD_SK;
1770         ew32(EECD, *eecd);
1771         e1e_flush();
1772         udelay(hw->nvm.delay_usec);
1773 }
1774
1775 /**
1776  *  e1000_shift_out_eec_bits - Shift data bits our to the EEPROM
1777  *  @hw: pointer to the HW structure
1778  *  @data: data to send to the EEPROM
1779  *  @count: number of bits to shift out
1780  *
1781  *  We need to shift 'count' bits out to the EEPROM.  So, the value in the
1782  *  "data" parameter will be shifted out to the EEPROM one bit at a time.
1783  *  In order to do this, "data" must be broken down into bits.
1784  **/
1785 static void e1000_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
1786 {
1787         struct e1000_nvm_info *nvm = &hw->nvm;
1788         u32 eecd = er32(EECD);
1789         u32 mask;
1790
1791         mask = 0x01 << (count - 1);
1792         if (nvm->type == e1000_nvm_eeprom_spi)
1793                 eecd |= E1000_EECD_DO;
1794
1795         do {
1796                 eecd &= ~E1000_EECD_DI;
1797
1798                 if (data & mask)
1799                         eecd |= E1000_EECD_DI;
1800
1801                 ew32(EECD, eecd);
1802                 e1e_flush();
1803
1804                 udelay(nvm->delay_usec);
1805
1806                 e1000_raise_eec_clk(hw, &eecd);
1807                 e1000_lower_eec_clk(hw, &eecd);
1808
1809                 mask >>= 1;
1810         } while (mask);
1811
1812         eecd &= ~E1000_EECD_DI;
1813         ew32(EECD, eecd);
1814 }
1815
1816 /**
1817  *  e1000_shift_in_eec_bits - Shift data bits in from the EEPROM
1818  *  @hw: pointer to the HW structure
1819  *  @count: number of bits to shift in
1820  *
1821  *  In order to read a register from the EEPROM, we need to shift 'count' bits
1822  *  in from the EEPROM.  Bits are "shifted in" by raising the clock input to
1823  *  the EEPROM (setting the SK bit), and then reading the value of the data out
1824  *  "DO" bit.  During this "shifting in" process the data in "DI" bit should
1825  *  always be clear.
1826  **/
1827 static u16 e1000_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
1828 {
1829         u32 eecd;
1830         u32 i;
1831         u16 data;
1832
1833         eecd = er32(EECD);
1834
1835         eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
1836         data = 0;
1837
1838         for (i = 0; i < count; i++) {
1839                 data <<= 1;
1840                 e1000_raise_eec_clk(hw, &eecd);
1841
1842                 eecd = er32(EECD);
1843
1844                 eecd &= ~E1000_EECD_DI;
1845                 if (eecd & E1000_EECD_DO)
1846                         data |= 1;
1847
1848                 e1000_lower_eec_clk(hw, &eecd);
1849         }
1850
1851         return data;
1852 }
1853
1854 /**
1855  *  e1000e_poll_eerd_eewr_done - Poll for EEPROM read/write completion
1856  *  @hw: pointer to the HW structure
1857  *  @ee_reg: EEPROM flag for polling
1858  *
1859  *  Polls the EEPROM status bit for either read or write completion based
1860  *  upon the value of 'ee_reg'.
1861  **/
1862 s32 e1000e_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
1863 {
1864         u32 attempts = 100000;
1865         u32 i, reg = 0;
1866
1867         for (i = 0; i < attempts; i++) {
1868                 if (ee_reg == E1000_NVM_POLL_READ)
1869                         reg = er32(EERD);
1870                 else
1871                         reg = er32(EEWR);
1872
1873                 if (reg & E1000_NVM_RW_REG_DONE)
1874                         return 0;
1875
1876                 udelay(5);
1877         }
1878
1879         return -E1000_ERR_NVM;
1880 }
1881
1882 /**
1883  *  e1000e_acquire_nvm - Generic request for access to EEPROM
1884  *  @hw: pointer to the HW structure
1885  *
1886  *  Set the EEPROM access request bit and wait for EEPROM access grant bit.
1887  *  Return successful if access grant bit set, else clear the request for
1888  *  EEPROM access and return -E1000_ERR_NVM (-1).
1889  **/
1890 s32 e1000e_acquire_nvm(struct e1000_hw *hw)
1891 {
1892         u32 eecd = er32(EECD);
1893         s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
1894
1895         ew32(EECD, eecd | E1000_EECD_REQ);
1896         eecd = er32(EECD);
1897
1898         while (timeout) {
1899                 if (eecd & E1000_EECD_GNT)
1900                         break;
1901                 udelay(5);
1902                 eecd = er32(EECD);
1903                 timeout--;
1904         }
1905
1906         if (!timeout) {
1907                 eecd &= ~E1000_EECD_REQ;
1908                 ew32(EECD, eecd);
1909                 e_dbg("Could not acquire NVM grant\n");
1910                 return -E1000_ERR_NVM;
1911         }
1912
1913         return 0;
1914 }
1915
1916 /**
1917  *  e1000_standby_nvm - Return EEPROM to standby state
1918  *  @hw: pointer to the HW structure
1919  *
1920  *  Return the EEPROM to a standby state.
1921  **/
1922 static void e1000_standby_nvm(struct e1000_hw *hw)
1923 {
1924         struct e1000_nvm_info *nvm = &hw->nvm;
1925         u32 eecd = er32(EECD);
1926
1927         if (nvm->type == e1000_nvm_eeprom_spi) {
1928                 /* Toggle CS to flush commands */
1929                 eecd |= E1000_EECD_CS;
1930                 ew32(EECD, eecd);
1931                 e1e_flush();
1932                 udelay(nvm->delay_usec);
1933                 eecd &= ~E1000_EECD_CS;
1934                 ew32(EECD, eecd);
1935                 e1e_flush();
1936                 udelay(nvm->delay_usec);
1937         }
1938 }
1939
1940 /**
1941  *  e1000_stop_nvm - Terminate EEPROM command
1942  *  @hw: pointer to the HW structure
1943  *
1944  *  Terminates the current command by inverting the EEPROM's chip select pin.
1945  **/
1946 static void e1000_stop_nvm(struct e1000_hw *hw)
1947 {
1948         u32 eecd;
1949
1950         eecd = er32(EECD);
1951         if (hw->nvm.type == e1000_nvm_eeprom_spi) {
1952                 /* Pull CS high */
1953                 eecd |= E1000_EECD_CS;
1954                 e1000_lower_eec_clk(hw, &eecd);
1955         }
1956 }
1957
1958 /**
1959  *  e1000e_release_nvm - Release exclusive access to EEPROM
1960  *  @hw: pointer to the HW structure
1961  *
1962  *  Stop any current commands to the EEPROM and clear the EEPROM request bit.
1963  **/
1964 void e1000e_release_nvm(struct e1000_hw *hw)
1965 {
1966         u32 eecd;
1967
1968         e1000_stop_nvm(hw);
1969
1970         eecd = er32(EECD);
1971         eecd &= ~E1000_EECD_REQ;
1972         ew32(EECD, eecd);
1973 }
1974
1975 /**
1976  *  e1000_ready_nvm_eeprom - Prepares EEPROM for read/write
1977  *  @hw: pointer to the HW structure
1978  *
1979  *  Setups the EEPROM for reading and writing.
1980  **/
1981 static s32 e1000_ready_nvm_eeprom(struct e1000_hw *hw)
1982 {
1983         struct e1000_nvm_info *nvm = &hw->nvm;
1984         u32 eecd = er32(EECD);
1985         u8 spi_stat_reg;
1986
1987         if (nvm->type == e1000_nvm_eeprom_spi) {
1988                 u16 timeout = NVM_MAX_RETRY_SPI;
1989
1990                 /* Clear SK and CS */
1991                 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
1992                 ew32(EECD, eecd);
1993                 e1e_flush();
1994                 udelay(1);
1995
1996                 /*
1997                  * Read "Status Register" repeatedly until the LSB is cleared.
1998                  * The EEPROM will signal that the command has been completed
1999                  * by clearing bit 0 of the internal status register.  If it's
2000                  * not cleared within 'timeout', then error out.
2001                  */
2002                 while (timeout) {
2003                         e1000_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
2004                                                  hw->nvm.opcode_bits);
2005                         spi_stat_reg = (u8)e1000_shift_in_eec_bits(hw, 8);
2006                         if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
2007                                 break;
2008
2009                         udelay(5);
2010                         e1000_standby_nvm(hw);
2011                         timeout--;
2012                 }
2013
2014                 if (!timeout) {
2015                         e_dbg("SPI NVM Status error\n");
2016                         return -E1000_ERR_NVM;
2017                 }
2018         }
2019
2020         return 0;
2021 }
2022
2023 /**
2024  *  e1000e_read_nvm_eerd - Reads EEPROM using EERD register
2025  *  @hw: pointer to the HW structure
2026  *  @offset: offset of word in the EEPROM to read
2027  *  @words: number of words to read
2028  *  @data: word read from the EEPROM
2029  *
2030  *  Reads a 16 bit word from the EEPROM using the EERD register.
2031  **/
2032 s32 e1000e_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
2033 {
2034         struct e1000_nvm_info *nvm = &hw->nvm;
2035         u32 i, eerd = 0;
2036         s32 ret_val = 0;
2037
2038         /*
2039          * A check for invalid values:  offset too large, too many words,
2040          * too many words for the offset, and not enough words.
2041          */
2042         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
2043             (words == 0)) {
2044                 e_dbg("nvm parameter(s) out of bounds\n");
2045                 return -E1000_ERR_NVM;
2046         }
2047
2048         for (i = 0; i < words; i++) {
2049                 eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
2050                        E1000_NVM_RW_REG_START;
2051
2052                 ew32(EERD, eerd);
2053                 ret_val = e1000e_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
2054                 if (ret_val)
2055                         break;
2056
2057                 data[i] = (er32(EERD) >> E1000_NVM_RW_REG_DATA);
2058         }
2059
2060         return ret_val;
2061 }
2062
2063 /**
2064  *  e1000e_write_nvm_spi - Write to EEPROM using SPI
2065  *  @hw: pointer to the HW structure
2066  *  @offset: offset within the EEPROM to be written to
2067  *  @words: number of words to write
2068  *  @data: 16 bit word(s) to be written to the EEPROM
2069  *
2070  *  Writes data to EEPROM at offset using SPI interface.
2071  *
2072  *  If e1000e_update_nvm_checksum is not called after this function , the
2073  *  EEPROM will most likely contain an invalid checksum.
2074  **/
2075 s32 e1000e_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
2076 {
2077         struct e1000_nvm_info *nvm = &hw->nvm;
2078         s32 ret_val;
2079         u16 widx = 0;
2080
2081         /*
2082          * A check for invalid values:  offset too large, too many words,
2083          * and not enough words.
2084          */
2085         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
2086             (words == 0)) {
2087                 e_dbg("nvm parameter(s) out of bounds\n");
2088                 return -E1000_ERR_NVM;
2089         }
2090
2091         ret_val = nvm->ops.acquire(hw);
2092         if (ret_val)
2093                 return ret_val;
2094
2095         while (widx < words) {
2096                 u8 write_opcode = NVM_WRITE_OPCODE_SPI;
2097
2098                 ret_val = e1000_ready_nvm_eeprom(hw);
2099                 if (ret_val) {
2100                         nvm->ops.release(hw);
2101                         return ret_val;
2102                 }
2103
2104                 e1000_standby_nvm(hw);
2105
2106                 /* Send the WRITE ENABLE command (8 bit opcode) */
2107                 e1000_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
2108                                          nvm->opcode_bits);
2109
2110                 e1000_standby_nvm(hw);
2111
2112                 /*
2113                  * Some SPI eeproms use the 8th address bit embedded in the
2114                  * opcode
2115                  */
2116                 if ((nvm->address_bits == 8) && (offset >= 128))
2117                         write_opcode |= NVM_A8_OPCODE_SPI;
2118
2119                 /* Send the Write command (8-bit opcode + addr) */
2120                 e1000_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
2121                 e1000_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
2122                                          nvm->address_bits);
2123
2124                 /* Loop to allow for up to whole page write of eeprom */
2125                 while (widx < words) {
2126                         u16 word_out = data[widx];
2127                         word_out = (word_out >> 8) | (word_out << 8);
2128                         e1000_shift_out_eec_bits(hw, word_out, 16);
2129                         widx++;
2130
2131                         if ((((offset + widx) * 2) % nvm->page_size) == 0) {
2132                                 e1000_standby_nvm(hw);
2133                                 break;
2134                         }
2135                 }
2136         }
2137
2138         usleep_range(10000, 20000);
2139         nvm->ops.release(hw);
2140         return 0;
2141 }
2142
2143 /**
2144  *  e1000_read_pba_string_generic - Read device part number
2145  *  @hw: pointer to the HW structure
2146  *  @pba_num: pointer to device part number
2147  *  @pba_num_size: size of part number buffer
2148  *
2149  *  Reads the product board assembly (PBA) number from the EEPROM and stores
2150  *  the value in pba_num.
2151  **/
2152 s32 e1000_read_pba_string_generic(struct e1000_hw *hw, u8 *pba_num,
2153                                   u32 pba_num_size)
2154 {
2155         s32 ret_val;
2156         u16 nvm_data;
2157         u16 pba_ptr;
2158         u16 offset;
2159         u16 length;
2160
2161         if (pba_num == NULL) {
2162                 e_dbg("PBA string buffer was null\n");
2163                 ret_val = E1000_ERR_INVALID_ARGUMENT;
2164                 goto out;
2165         }
2166
2167         ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
2168         if (ret_val) {
2169                 e_dbg("NVM Read Error\n");
2170                 goto out;
2171         }
2172
2173         ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_1, 1, &pba_ptr);
2174         if (ret_val) {
2175                 e_dbg("NVM Read Error\n");
2176                 goto out;
2177         }
2178
2179         /*
2180          * if nvm_data is not ptr guard the PBA must be in legacy format which
2181          * means pba_ptr is actually our second data word for the PBA number
2182          * and we can decode it into an ascii string
2183          */
2184         if (nvm_data != NVM_PBA_PTR_GUARD) {
2185                 e_dbg("NVM PBA number is not stored as string\n");
2186
2187                 /* we will need 11 characters to store the PBA */
2188                 if (pba_num_size < 11) {
2189                         e_dbg("PBA string buffer too small\n");
2190                         return E1000_ERR_NO_SPACE;
2191                 }
2192
2193                 /* extract hex string from data and pba_ptr */
2194                 pba_num[0] = (nvm_data >> 12) & 0xF;
2195                 pba_num[1] = (nvm_data >> 8) & 0xF;
2196                 pba_num[2] = (nvm_data >> 4) & 0xF;
2197                 pba_num[3] = nvm_data & 0xF;
2198                 pba_num[4] = (pba_ptr >> 12) & 0xF;
2199                 pba_num[5] = (pba_ptr >> 8) & 0xF;
2200                 pba_num[6] = '-';
2201                 pba_num[7] = 0;
2202                 pba_num[8] = (pba_ptr >> 4) & 0xF;
2203                 pba_num[9] = pba_ptr & 0xF;
2204
2205                 /* put a null character on the end of our string */
2206                 pba_num[10] = '\0';
2207
2208                 /* switch all the data but the '-' to hex char */
2209                 for (offset = 0; offset < 10; offset++) {
2210                         if (pba_num[offset] < 0xA)
2211                                 pba_num[offset] += '0';
2212                         else if (pba_num[offset] < 0x10)
2213                                 pba_num[offset] += 'A' - 0xA;
2214                 }
2215
2216                 goto out;
2217         }
2218
2219         ret_val = e1000_read_nvm(hw, pba_ptr, 1, &length);
2220         if (ret_val) {
2221                 e_dbg("NVM Read Error\n");
2222                 goto out;
2223         }
2224
2225         if (length == 0xFFFF || length == 0) {
2226                 e_dbg("NVM PBA number section invalid length\n");
2227                 ret_val = E1000_ERR_NVM_PBA_SECTION;
2228                 goto out;
2229         }
2230         /* check if pba_num buffer is big enough */
2231         if (pba_num_size < (((u32)length * 2) - 1)) {
2232                 e_dbg("PBA string buffer too small\n");
2233                 ret_val = E1000_ERR_NO_SPACE;
2234                 goto out;
2235         }
2236
2237         /* trim pba length from start of string */
2238         pba_ptr++;
2239         length--;
2240
2241         for (offset = 0; offset < length; offset++) {
2242                 ret_val = e1000_read_nvm(hw, pba_ptr + offset, 1, &nvm_data);
2243                 if (ret_val) {
2244                         e_dbg("NVM Read Error\n");
2245                         goto out;
2246                 }
2247                 pba_num[offset * 2] = (u8)(nvm_data >> 8);
2248                 pba_num[(offset * 2) + 1] = (u8)(nvm_data & 0xFF);
2249         }
2250         pba_num[offset * 2] = '\0';
2251
2252 out:
2253         return ret_val;
2254 }
2255
2256 /**
2257  *  e1000_read_mac_addr_generic - Read device MAC address
2258  *  @hw: pointer to the HW structure
2259  *
2260  *  Reads the device MAC address from the EEPROM and stores the value.
2261  *  Since devices with two ports use the same EEPROM, we increment the
2262  *  last bit in the MAC address for the second port.
2263  **/
2264 s32 e1000_read_mac_addr_generic(struct e1000_hw *hw)
2265 {
2266         u32 rar_high;
2267         u32 rar_low;
2268         u16 i;
2269
2270         rar_high = er32(RAH(0));
2271         rar_low = er32(RAL(0));
2272
2273         for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
2274                 hw->mac.perm_addr[i] = (u8)(rar_low >> (i*8));
2275
2276         for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
2277                 hw->mac.perm_addr[i+4] = (u8)(rar_high >> (i*8));
2278
2279         for (i = 0; i < ETH_ALEN; i++)
2280                 hw->mac.addr[i] = hw->mac.perm_addr[i];
2281
2282         return 0;
2283 }
2284
2285 /**
2286  *  e1000e_validate_nvm_checksum_generic - Validate EEPROM checksum
2287  *  @hw: pointer to the HW structure
2288  *
2289  *  Calculates the EEPROM checksum by reading/adding each word of the EEPROM
2290  *  and then verifies that the sum of the EEPROM is equal to 0xBABA.
2291  **/
2292 s32 e1000e_validate_nvm_checksum_generic(struct e1000_hw *hw)
2293 {
2294         s32 ret_val;
2295         u16 checksum = 0;
2296         u16 i, nvm_data;
2297
2298         for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
2299                 ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
2300                 if (ret_val) {
2301                         e_dbg("NVM Read Error\n");
2302                         return ret_val;
2303                 }
2304                 checksum += nvm_data;
2305         }
2306
2307         if (checksum != (u16) NVM_SUM) {
2308                 e_dbg("NVM Checksum Invalid\n");
2309                 return -E1000_ERR_NVM;
2310         }
2311
2312         return 0;
2313 }
2314
2315 /**
2316  *  e1000e_update_nvm_checksum_generic - Update EEPROM checksum
2317  *  @hw: pointer to the HW structure
2318  *
2319  *  Updates the EEPROM checksum by reading/adding each word of the EEPROM
2320  *  up to the checksum.  Then calculates the EEPROM checksum and writes the
2321  *  value to the EEPROM.
2322  **/
2323 s32 e1000e_update_nvm_checksum_generic(struct e1000_hw *hw)
2324 {
2325         s32 ret_val;
2326         u16 checksum = 0;
2327         u16 i, nvm_data;
2328
2329         for (i = 0; i < NVM_CHECKSUM_REG; i++) {
2330                 ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
2331                 if (ret_val) {
2332                         e_dbg("NVM Read Error while updating checksum.\n");
2333                         return ret_val;
2334                 }
2335                 checksum += nvm_data;
2336         }
2337         checksum = (u16) NVM_SUM - checksum;
2338         ret_val = e1000_write_nvm(hw, NVM_CHECKSUM_REG, 1, &checksum);
2339         if (ret_val)
2340                 e_dbg("NVM Write Error while updating checksum.\n");
2341
2342         return ret_val;
2343 }
2344
2345 /**
2346  *  e1000e_reload_nvm - Reloads EEPROM
2347  *  @hw: pointer to the HW structure
2348  *
2349  *  Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
2350  *  extended control register.
2351  **/
2352 void e1000e_reload_nvm(struct e1000_hw *hw)
2353 {
2354         u32 ctrl_ext;
2355
2356         udelay(10);
2357         ctrl_ext = er32(CTRL_EXT);
2358         ctrl_ext |= E1000_CTRL_EXT_EE_RST;
2359         ew32(CTRL_EXT, ctrl_ext);
2360         e1e_flush();
2361 }
2362
2363 /**
2364  *  e1000_calculate_checksum - Calculate checksum for buffer
2365  *  @buffer: pointer to EEPROM
2366  *  @length: size of EEPROM to calculate a checksum for
2367  *
2368  *  Calculates the checksum for some buffer on a specified length.  The
2369  *  checksum calculated is returned.
2370  **/
2371 static u8 e1000_calculate_checksum(u8 *buffer, u32 length)
2372 {
2373         u32 i;
2374         u8  sum = 0;
2375
2376         if (!buffer)
2377                 return 0;
2378
2379         for (i = 0; i < length; i++)
2380                 sum += buffer[i];
2381
2382         return (u8) (0 - sum);
2383 }
2384
2385 /**
2386  *  e1000_mng_enable_host_if - Checks host interface is enabled
2387  *  @hw: pointer to the HW structure
2388  *
2389  *  Returns E1000_success upon success, else E1000_ERR_HOST_INTERFACE_COMMAND
2390  *
2391  *  This function checks whether the HOST IF is enabled for command operation
2392  *  and also checks whether the previous command is completed.  It busy waits
2393  *  in case of previous command is not completed.
2394  **/
2395 static s32 e1000_mng_enable_host_if(struct e1000_hw *hw)
2396 {
2397         u32 hicr;
2398         u8 i;
2399
2400         if (!(hw->mac.arc_subsystem_valid)) {
2401                 e_dbg("ARC subsystem not valid.\n");
2402                 return -E1000_ERR_HOST_INTERFACE_COMMAND;
2403         }
2404
2405         /* Check that the host interface is enabled. */
2406         hicr = er32(HICR);
2407         if ((hicr & E1000_HICR_EN) == 0) {
2408                 e_dbg("E1000_HOST_EN bit disabled.\n");
2409                 return -E1000_ERR_HOST_INTERFACE_COMMAND;
2410         }
2411         /* check the previous command is completed */
2412         for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
2413                 hicr = er32(HICR);
2414                 if (!(hicr & E1000_HICR_C))
2415                         break;
2416                 mdelay(1);
2417         }
2418
2419         if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) {
2420                 e_dbg("Previous command timeout failed .\n");
2421                 return -E1000_ERR_HOST_INTERFACE_COMMAND;
2422         }
2423
2424         return 0;
2425 }
2426
2427 /**
2428  *  e1000e_check_mng_mode_generic - check management mode
2429  *  @hw: pointer to the HW structure
2430  *
2431  *  Reads the firmware semaphore register and returns true (>0) if
2432  *  manageability is enabled, else false (0).
2433  **/
2434 bool e1000e_check_mng_mode_generic(struct e1000_hw *hw)
2435 {
2436         u32 fwsm = er32(FWSM);
2437
2438         return (fwsm & E1000_FWSM_MODE_MASK) ==
2439                 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT);
2440 }
2441
2442 /**
2443  *  e1000e_enable_tx_pkt_filtering - Enable packet filtering on Tx
2444  *  @hw: pointer to the HW structure
2445  *
2446  *  Enables packet filtering on transmit packets if manageability is enabled
2447  *  and host interface is enabled.
2448  **/
2449 bool e1000e_enable_tx_pkt_filtering(struct e1000_hw *hw)
2450 {
2451         struct e1000_host_mng_dhcp_cookie *hdr = &hw->mng_cookie;
2452         u32 *buffer = (u32 *)&hw->mng_cookie;
2453         u32 offset;
2454         s32 ret_val, hdr_csum, csum;
2455         u8 i, len;
2456
2457         hw->mac.tx_pkt_filtering = true;
2458
2459         /* No manageability, no filtering */
2460         if (!e1000e_check_mng_mode(hw)) {
2461                 hw->mac.tx_pkt_filtering = false;
2462                 goto out;
2463         }
2464
2465         /*
2466          * If we can't read from the host interface for whatever
2467          * reason, disable filtering.
2468          */
2469         ret_val = e1000_mng_enable_host_if(hw);
2470         if (ret_val) {
2471                 hw->mac.tx_pkt_filtering = false;
2472                 goto out;
2473         }
2474
2475         /* Read in the header.  Length and offset are in dwords. */
2476         len    = E1000_MNG_DHCP_COOKIE_LENGTH >> 2;
2477         offset = E1000_MNG_DHCP_COOKIE_OFFSET >> 2;
2478         for (i = 0; i < len; i++)
2479                 *(buffer + i) = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset + i);
2480         hdr_csum = hdr->checksum;
2481         hdr->checksum = 0;
2482         csum = e1000_calculate_checksum((u8 *)hdr,
2483                                         E1000_MNG_DHCP_COOKIE_LENGTH);
2484         /*
2485          * If either the checksums or signature don't match, then
2486          * the cookie area isn't considered valid, in which case we
2487          * take the safe route of assuming Tx filtering is enabled.
2488          */
2489         if ((hdr_csum != csum) || (hdr->signature != E1000_IAMT_SIGNATURE)) {
2490                 hw->mac.tx_pkt_filtering = true;
2491                 goto out;
2492         }
2493
2494         /* Cookie area is valid, make the final check for filtering. */
2495         if (!(hdr->status & E1000_MNG_DHCP_COOKIE_STATUS_PARSING)) {
2496                 hw->mac.tx_pkt_filtering = false;
2497                 goto out;
2498         }
2499
2500 out:
2501         return hw->mac.tx_pkt_filtering;
2502 }
2503
2504 /**
2505  *  e1000_mng_write_cmd_header - Writes manageability command header
2506  *  @hw: pointer to the HW structure
2507  *  @hdr: pointer to the host interface command header
2508  *
2509  *  Writes the command header after does the checksum calculation.
2510  **/
2511 static s32 e1000_mng_write_cmd_header(struct e1000_hw *hw,
2512                                   struct e1000_host_mng_command_header *hdr)
2513 {
2514         u16 i, length = sizeof(struct e1000_host_mng_command_header);
2515
2516         /* Write the whole command header structure with new checksum. */
2517
2518         hdr->checksum = e1000_calculate_checksum((u8 *)hdr, length);
2519
2520         length >>= 2;
2521         /* Write the relevant command block into the ram area. */
2522         for (i = 0; i < length; i++) {
2523                 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, i,
2524                                             *((u32 *) hdr + i));
2525                 e1e_flush();
2526         }
2527
2528         return 0;
2529 }
2530
2531 /**
2532  *  e1000_mng_host_if_write - Write to the manageability host interface
2533  *  @hw: pointer to the HW structure
2534  *  @buffer: pointer to the host interface buffer
2535  *  @length: size of the buffer
2536  *  @offset: location in the buffer to write to
2537  *  @sum: sum of the data (not checksum)
2538  *
2539  *  This function writes the buffer content at the offset given on the host if.
2540  *  It also does alignment considerations to do the writes in most efficient
2541  *  way.  Also fills up the sum of the buffer in *buffer parameter.
2542  **/
2543 static s32 e1000_mng_host_if_write(struct e1000_hw *hw, u8 *buffer,
2544                                    u16 length, u16 offset, u8 *sum)
2545 {
2546         u8 *tmp;
2547         u8 *bufptr = buffer;
2548         u32 data = 0;
2549         u16 remaining, i, j, prev_bytes;
2550
2551         /* sum = only sum of the data and it is not checksum */
2552
2553         if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH)
2554                 return -E1000_ERR_PARAM;
2555
2556         tmp = (u8 *)&data;
2557         prev_bytes = offset & 0x3;
2558         offset >>= 2;
2559
2560         if (prev_bytes) {
2561                 data = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset);
2562                 for (j = prev_bytes; j < sizeof(u32); j++) {
2563                         *(tmp + j) = *bufptr++;
2564                         *sum += *(tmp + j);
2565                 }
2566                 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset, data);
2567                 length -= j - prev_bytes;
2568                 offset++;
2569         }
2570
2571         remaining = length & 0x3;
2572         length -= remaining;
2573
2574         /* Calculate length in DWORDs */
2575         length >>= 2;
2576
2577         /*
2578          * The device driver writes the relevant command block into the
2579          * ram area.
2580          */
2581         for (i = 0; i < length; i++) {
2582                 for (j = 0; j < sizeof(u32); j++) {
2583                         *(tmp + j) = *bufptr++;
2584                         *sum += *(tmp + j);
2585                 }
2586
2587                 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
2588         }
2589         if (remaining) {
2590                 for (j = 0; j < sizeof(u32); j++) {
2591                         if (j < remaining)
2592                                 *(tmp + j) = *bufptr++;
2593                         else
2594                                 *(tmp + j) = 0;
2595
2596                         *sum += *(tmp + j);
2597                 }
2598                 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
2599         }
2600
2601         return 0;
2602 }
2603
2604 /**
2605  *  e1000e_mng_write_dhcp_info - Writes DHCP info to host interface
2606  *  @hw: pointer to the HW structure
2607  *  @buffer: pointer to the host interface
2608  *  @length: size of the buffer
2609  *
2610  *  Writes the DHCP information to the host interface.
2611  **/
2612 s32 e1000e_mng_write_dhcp_info(struct e1000_hw *hw, u8 *buffer, u16 length)
2613 {
2614         struct e1000_host_mng_command_header hdr;
2615         s32 ret_val;
2616         u32 hicr;
2617
2618         hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
2619         hdr.command_length = length;
2620         hdr.reserved1 = 0;
2621         hdr.reserved2 = 0;
2622         hdr.checksum = 0;
2623
2624         /* Enable the host interface */
2625         ret_val = e1000_mng_enable_host_if(hw);
2626         if (ret_val)
2627                 return ret_val;
2628
2629         /* Populate the host interface with the contents of "buffer". */
2630         ret_val = e1000_mng_host_if_write(hw, buffer, length,
2631                                           sizeof(hdr), &(hdr.checksum));
2632         if (ret_val)
2633                 return ret_val;
2634
2635         /* Write the manageability command header */
2636         ret_val = e1000_mng_write_cmd_header(hw, &hdr);
2637         if (ret_val)
2638                 return ret_val;
2639
2640         /* Tell the ARC a new command is pending. */
2641         hicr = er32(HICR);
2642         ew32(HICR, hicr | E1000_HICR_C);
2643
2644         return 0;
2645 }
2646
2647 /**
2648  *  e1000e_enable_mng_pass_thru - Check if management passthrough is needed
2649  *  @hw: pointer to the HW structure
2650  *
2651  *  Verifies the hardware needs to leave interface enabled so that frames can
2652  *  be directed to and from the management interface.
2653  **/
2654 bool e1000e_enable_mng_pass_thru(struct e1000_hw *hw)
2655 {
2656         u32 manc;
2657         u32 fwsm, factps;
2658         bool ret_val = false;
2659
2660         manc = er32(MANC);
2661
2662         if (!(manc & E1000_MANC_RCV_TCO_EN))
2663                 goto out;
2664
2665         if (hw->mac.has_fwsm) {
2666                 fwsm = er32(FWSM);
2667                 factps = er32(FACTPS);
2668
2669                 if (!(factps & E1000_FACTPS_MNGCG) &&
2670                     ((fwsm & E1000_FWSM_MODE_MASK) ==
2671                      (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT))) {
2672                         ret_val = true;
2673                         goto out;
2674                 }
2675         } else if ((hw->mac.type == e1000_82574) ||
2676                    (hw->mac.type == e1000_82583)) {
2677                 u16 data;
2678
2679                 factps = er32(FACTPS);
2680                 e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &data);
2681
2682                 if (!(factps & E1000_FACTPS_MNGCG) &&
2683                     ((data & E1000_NVM_INIT_CTRL2_MNGM) ==
2684                      (e1000_mng_mode_pt << 13))) {
2685                         ret_val = true;
2686                         goto out;
2687                 }
2688         } else if ((manc & E1000_MANC_SMBUS_EN) &&
2689                     !(manc & E1000_MANC_ASF_EN)) {
2690                         ret_val = true;
2691                         goto out;
2692         }
2693
2694 out:
2695         return ret_val;
2696 }