blob: e3976ea668d068f01bcb3526d17adcb16c9f8377 [file] [log] [blame]
Auke Kokbc7f75f2007-09-17 12:30:59 -07001/*******************************************************************************
2
3 Intel PRO/1000 Linux driver
Bruce Allanc7e54b12009-11-20 23:25:45 +00004 Copyright(c) 1999 - 2009 Intel Corporation.
Auke Kokbc7f75f2007-09-17 12:30:59 -07005
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
Auke Kokbc7f75f2007-09-17 12:30:59 -070029#include "e1000.h"
30
31enum 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
Bruce Allanad680762008-03-28 09:15:03 -070041/* Intel(R) Active Management Technology signature */
42#define E1000_IAMT_SIGNATURE 0x544D4149
Auke Kokbc7f75f2007-09-17 12:30:59 -070043
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 **/
52s32 e1000e_get_bus_info_pcie(struct e1000_hw *hw)
53{
54 struct e1000_bus_info *bus = &hw->bus;
55 struct e1000_adapter *adapter = hw->adapter;
56 u32 status;
57 u16 pcie_link_status, pci_header_type, cap_offset;
58
59 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
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 pci_read_config_word(adapter->pdev, PCI_HEADER_TYPE_REGISTER,
72 &pci_header_type);
73 if (pci_header_type & PCI_HEADER_TYPE_MULTIFUNC) {
74 status = er32(STATUS);
75 bus->func = (status & E1000_STATUS_FUNC_MASK)
76 >> E1000_STATUS_FUNC_SHIFT;
77 } else {
78 bus->func = 0;
79 }
80
81 return 0;
82}
83
84/**
Bruce Allancaaddaf2009-12-01 15:46:43 +000085 * e1000_clear_vfta_generic - Clear VLAN filter table
86 * @hw: pointer to the HW structure
87 *
88 * Clears the register array which contains the VLAN filter table by
89 * setting all the values to 0.
90 **/
91void e1000_clear_vfta_generic(struct e1000_hw *hw)
92{
93 u32 offset;
94
95 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
96 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, 0);
97 e1e_flush();
98 }
99}
100
101/**
102 * e1000_write_vfta_generic - Write value to VLAN filter table
Auke Kokbc7f75f2007-09-17 12:30:59 -0700103 * @hw: pointer to the HW structure
104 * @offset: register offset in VLAN filter table
105 * @value: register value written to VLAN filter table
106 *
107 * Writes value at the given offset in the register array which stores
108 * the VLAN filter table.
109 **/
Bruce Allancaaddaf2009-12-01 15:46:43 +0000110void e1000_write_vfta_generic(struct e1000_hw *hw, u32 offset, u32 value)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700111{
112 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, value);
113 e1e_flush();
114}
115
116/**
117 * e1000e_init_rx_addrs - Initialize receive address's
118 * @hw: pointer to the HW structure
119 * @rar_count: receive address registers
120 *
121 * Setups the receive address registers by setting the base receive address
122 * register to the devices MAC address and clearing all the other receive
123 * address registers to 0.
124 **/
125void e1000e_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
126{
127 u32 i;
128
129 /* Setup the receive address */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000130 e_dbg("Programming MAC Address into RAR[0]\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700131
132 e1000e_rar_set(hw, hw->mac.addr, 0);
133
134 /* Zero out the other (rar_entry_count - 1) receive addresses */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000135 e_dbg("Clearing RAR[1-%u]\n", rar_count-1);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700136 for (i = 1; i < rar_count; i++) {
137 E1000_WRITE_REG_ARRAY(hw, E1000_RA, (i << 1), 0);
138 e1e_flush();
139 E1000_WRITE_REG_ARRAY(hw, E1000_RA, ((i << 1) + 1), 0);
140 e1e_flush();
141 }
142}
143
144/**
145 * e1000e_rar_set - Set receive address register
146 * @hw: pointer to the HW structure
147 * @addr: pointer to the receive address
148 * @index: receive address array register
149 *
150 * Sets the receive address array register at index to the address passed
151 * in by addr.
152 **/
153void e1000e_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
154{
155 u32 rar_low, rar_high;
156
Bruce Allanad680762008-03-28 09:15:03 -0700157 /*
158 * HW expects these in little endian so we reverse the byte order
Auke Kokbc7f75f2007-09-17 12:30:59 -0700159 * from network order (big endian) to little endian
160 */
161 rar_low = ((u32) addr[0] |
162 ((u32) addr[1] << 8) |
163 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
164
165 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
166
167 rar_high |= E1000_RAH_AV;
168
169 E1000_WRITE_REG_ARRAY(hw, E1000_RA, (index << 1), rar_low);
170 E1000_WRITE_REG_ARRAY(hw, E1000_RA, ((index << 1) + 1), rar_high);
171}
172
173/**
Auke Kokbc7f75f2007-09-17 12:30:59 -0700174 * e1000_hash_mc_addr - Generate a multicast hash value
175 * @hw: pointer to the HW structure
176 * @mc_addr: pointer to a multicast address
177 *
178 * Generates a multicast address hash value which is used to determine
179 * the multicast filter table array address and new table value. See
180 * e1000_mta_set_generic()
181 **/
182static u32 e1000_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
183{
184 u32 hash_value, hash_mask;
185 u8 bit_shift = 0;
186
187 /* Register count multiplied by bits per register */
188 hash_mask = (hw->mac.mta_reg_count * 32) - 1;
189
Bruce Allanad680762008-03-28 09:15:03 -0700190 /*
191 * For a mc_filter_type of 0, bit_shift is the number of left-shifts
192 * where 0xFF would still fall within the hash mask.
193 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700194 while (hash_mask >> bit_shift != 0xFF)
195 bit_shift++;
196
Bruce Allanad680762008-03-28 09:15:03 -0700197 /*
198 * The portion of the address that is used for the hash table
Auke Kokbc7f75f2007-09-17 12:30:59 -0700199 * is determined by the mc_filter_type setting.
200 * The algorithm is such that there is a total of 8 bits of shifting.
201 * The bit_shift for a mc_filter_type of 0 represents the number of
202 * left-shifts where the MSB of mc_addr[5] would still fall within
203 * the hash_mask. Case 0 does this exactly. Since there are a total
204 * of 8 bits of shifting, then mc_addr[4] will shift right the
205 * remaining number of bits. Thus 8 - bit_shift. The rest of the
206 * cases are a variation of this algorithm...essentially raising the
207 * number of bits to shift mc_addr[5] left, while still keeping the
208 * 8-bit shifting total.
Bruce Allanad680762008-03-28 09:15:03 -0700209 *
210 * For example, given the following Destination MAC Address and an
Auke Kokbc7f75f2007-09-17 12:30:59 -0700211 * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
212 * we can see that the bit_shift for case 0 is 4. These are the hash
213 * values resulting from each mc_filter_type...
214 * [0] [1] [2] [3] [4] [5]
215 * 01 AA 00 12 34 56
216 * LSB MSB
217 *
218 * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
219 * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
220 * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
221 * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
222 */
223 switch (hw->mac.mc_filter_type) {
224 default:
225 case 0:
226 break;
227 case 1:
228 bit_shift += 1;
229 break;
230 case 2:
231 bit_shift += 2;
232 break;
233 case 3:
234 bit_shift += 4;
235 break;
236 }
237
238 hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
239 (((u16) mc_addr[5]) << bit_shift)));
240
241 return hash_value;
242}
243
244/**
Jeff Kirshere2de3eb2008-03-28 09:15:11 -0700245 * e1000e_update_mc_addr_list_generic - Update Multicast addresses
Auke Kokbc7f75f2007-09-17 12:30:59 -0700246 * @hw: pointer to the HW structure
247 * @mc_addr_list: array of multicast addresses to program
248 * @mc_addr_count: number of multicast addresses to program
249 * @rar_used_count: the first RAR register free to program
250 * @rar_count: total number of supported Receive Address Registers
251 *
252 * Updates the Receive Address Registers and Multicast Table Array.
253 * The caller must have a packed mc_addr_list of multicast addresses.
254 * The parameter rar_count will usually be hw->mac.rar_entry_count
255 * unless there are workarounds that change this.
256 **/
Jeff Kirshere2de3eb2008-03-28 09:15:11 -0700257void e1000e_update_mc_addr_list_generic(struct e1000_hw *hw,
258 u8 *mc_addr_list, u32 mc_addr_count,
259 u32 rar_used_count, u32 rar_count)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700260{
Auke Kokbc7f75f2007-09-17 12:30:59 -0700261 u32 i;
Jesse Brandeburga72d2b22009-03-25 22:05:21 +0000262 u32 *mcarray = kzalloc(hw->mac.mta_reg_count * sizeof(u32), GFP_ATOMIC);
263
264 if (!mcarray) {
265 printk(KERN_ERR "multicast array memory allocation failed\n");
266 return;
267 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700268
Bruce Allanad680762008-03-28 09:15:03 -0700269 /*
270 * Load the first set of multicast addresses into the exact
Auke Kokbc7f75f2007-09-17 12:30:59 -0700271 * filters (RAR). If there are not enough to fill the RAR
272 * array, clear the filters.
273 */
274 for (i = rar_used_count; i < rar_count; i++) {
275 if (mc_addr_count) {
276 e1000e_rar_set(hw, mc_addr_list, i);
277 mc_addr_count--;
278 mc_addr_list += ETH_ALEN;
279 } else {
280 E1000_WRITE_REG_ARRAY(hw, E1000_RA, i << 1, 0);
281 e1e_flush();
282 E1000_WRITE_REG_ARRAY(hw, E1000_RA, (i << 1) + 1, 0);
283 e1e_flush();
284 }
285 }
286
Auke Kokbc7f75f2007-09-17 12:30:59 -0700287 /* Load any remaining multicast addresses into the hash table. */
288 for (; mc_addr_count > 0; mc_addr_count--) {
Jesse Brandeburga72d2b22009-03-25 22:05:21 +0000289 u32 hash_value, hash_reg, hash_bit, mta;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700290 hash_value = e1000_hash_mc_addr(hw, mc_addr_list);
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000291 e_dbg("Hash value = 0x%03X\n", hash_value);
Jesse Brandeburga72d2b22009-03-25 22:05:21 +0000292 hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
293 hash_bit = hash_value & 0x1F;
294 mta = (1 << hash_bit);
295 mcarray[hash_reg] |= mta;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700296 mc_addr_list += ETH_ALEN;
297 }
Jesse Brandeburga72d2b22009-03-25 22:05:21 +0000298
299 /* write the hash table completely */
300 for (i = 0; i < hw->mac.mta_reg_count; i++)
301 E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, mcarray[i]);
302
303 e1e_flush();
304 kfree(mcarray);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700305}
306
307/**
308 * e1000e_clear_hw_cntrs_base - Clear base hardware counters
309 * @hw: pointer to the HW structure
310 *
311 * Clears the base hardware counters by reading the counter registers.
312 **/
313void e1000e_clear_hw_cntrs_base(struct e1000_hw *hw)
314{
Bruce Allan99673d92009-11-20 23:27:21 +0000315 er32(CRCERRS);
316 er32(SYMERRS);
317 er32(MPC);
318 er32(SCC);
319 er32(ECOL);
320 er32(MCC);
321 er32(LATECOL);
322 er32(COLC);
323 er32(DC);
324 er32(SEC);
325 er32(RLEC);
326 er32(XONRXC);
327 er32(XONTXC);
328 er32(XOFFRXC);
329 er32(XOFFTXC);
330 er32(FCRUC);
331 er32(GPRC);
332 er32(BPRC);
333 er32(MPRC);
334 er32(GPTC);
335 er32(GORCL);
336 er32(GORCH);
337 er32(GOTCL);
338 er32(GOTCH);
339 er32(RNBC);
340 er32(RUC);
341 er32(RFC);
342 er32(ROC);
343 er32(RJC);
344 er32(TORL);
345 er32(TORH);
346 er32(TOTL);
347 er32(TOTH);
348 er32(TPR);
349 er32(TPT);
350 er32(MPTC);
351 er32(BPTC);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700352}
353
354/**
355 * e1000e_check_for_copper_link - Check for link (Copper)
356 * @hw: pointer to the HW structure
357 *
358 * Checks to see of the link status of the hardware has changed. If a
359 * change in link status has been detected, then we read the PHY registers
360 * to get the current speed/duplex if link exists.
361 **/
362s32 e1000e_check_for_copper_link(struct e1000_hw *hw)
363{
364 struct e1000_mac_info *mac = &hw->mac;
365 s32 ret_val;
366 bool link;
367
Bruce Allanad680762008-03-28 09:15:03 -0700368 /*
369 * We only want to go out to the PHY registers to see if Auto-Neg
Auke Kokbc7f75f2007-09-17 12:30:59 -0700370 * has completed and/or if our link status has changed. The
371 * get_link_status flag is set upon receiving a Link Status
372 * Change or Rx Sequence Error interrupt.
373 */
374 if (!mac->get_link_status)
375 return 0;
376
Bruce Allanad680762008-03-28 09:15:03 -0700377 /*
378 * First we want to see if the MII Status Register reports
Auke Kokbc7f75f2007-09-17 12:30:59 -0700379 * link. If so, then we want to get the current speed/duplex
380 * of the PHY.
381 */
382 ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
383 if (ret_val)
384 return ret_val;
385
386 if (!link)
387 return ret_val; /* No link detected */
388
Bruce Allan564ea9b2009-11-20 23:26:44 +0000389 mac->get_link_status = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700390
Bruce Allanad680762008-03-28 09:15:03 -0700391 /*
392 * Check if there was DownShift, must be checked
393 * immediately after link-up
394 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700395 e1000e_check_downshift(hw);
396
Bruce Allanad680762008-03-28 09:15:03 -0700397 /*
398 * If we are forcing speed/duplex, then we simply return since
Auke Kokbc7f75f2007-09-17 12:30:59 -0700399 * we have already determined whether we have link or not.
400 */
401 if (!mac->autoneg) {
402 ret_val = -E1000_ERR_CONFIG;
403 return ret_val;
404 }
405
Bruce Allanad680762008-03-28 09:15:03 -0700406 /*
407 * Auto-Neg is enabled. Auto Speed Detection takes care
Auke Kokbc7f75f2007-09-17 12:30:59 -0700408 * of MAC speed/duplex configuration. So we only need to
409 * configure Collision Distance in the MAC.
410 */
411 e1000e_config_collision_dist(hw);
412
Bruce Allanad680762008-03-28 09:15:03 -0700413 /*
414 * Configure Flow Control now that Auto-Neg has completed.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700415 * First, we need to restore the desired flow control
416 * settings because we may have had to re-autoneg with a
417 * different link partner.
418 */
419 ret_val = e1000e_config_fc_after_link_up(hw);
420 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000421 e_dbg("Error configuring flow control\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700422 }
423
424 return ret_val;
425}
426
427/**
428 * e1000e_check_for_fiber_link - Check for link (Fiber)
429 * @hw: pointer to the HW structure
430 *
431 * Checks for link up on the hardware. If link is not up and we have
432 * a signal, then we need to force link up.
433 **/
434s32 e1000e_check_for_fiber_link(struct e1000_hw *hw)
435{
436 struct e1000_mac_info *mac = &hw->mac;
437 u32 rxcw;
438 u32 ctrl;
439 u32 status;
440 s32 ret_val;
441
442 ctrl = er32(CTRL);
443 status = er32(STATUS);
444 rxcw = er32(RXCW);
445
Bruce Allanad680762008-03-28 09:15:03 -0700446 /*
447 * If we don't have link (auto-negotiation failed or link partner
Auke Kokbc7f75f2007-09-17 12:30:59 -0700448 * cannot auto-negotiate), the cable is plugged in (we have signal),
449 * and our link partner is not trying to auto-negotiate with us (we
450 * are receiving idles or data), we need to force link up. We also
451 * need to give auto-negotiation time to complete, in case the cable
452 * was just plugged in. The autoneg_failed flag does this.
453 */
454 /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
455 if ((ctrl & E1000_CTRL_SWDPIN1) && (!(status & E1000_STATUS_LU)) &&
456 (!(rxcw & E1000_RXCW_C))) {
457 if (mac->autoneg_failed == 0) {
458 mac->autoneg_failed = 1;
459 return 0;
460 }
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000461 e_dbg("NOT RXing /C/, disable AutoNeg and force link.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700462
463 /* Disable auto-negotiation in the TXCW register */
464 ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
465
466 /* Force link-up and also force full-duplex. */
467 ctrl = er32(CTRL);
468 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
469 ew32(CTRL, ctrl);
470
471 /* Configure Flow Control after forcing link up. */
472 ret_val = e1000e_config_fc_after_link_up(hw);
473 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000474 e_dbg("Error configuring flow control\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700475 return ret_val;
476 }
477 } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
Bruce Allanad680762008-03-28 09:15:03 -0700478 /*
479 * If we are forcing link and we are receiving /C/ ordered
Auke Kokbc7f75f2007-09-17 12:30:59 -0700480 * sets, re-enable auto-negotiation in the TXCW register
481 * and disable forced link in the Device Control register
482 * in an attempt to auto-negotiate with our link partner.
483 */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000484 e_dbg("RXing /C/, enable AutoNeg and stop forcing link.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700485 ew32(TXCW, mac->txcw);
486 ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
487
Alex Chiang612e2442009-02-05 23:55:45 -0800488 mac->serdes_has_link = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700489 }
490
491 return 0;
492}
493
494/**
495 * e1000e_check_for_serdes_link - Check for link (Serdes)
496 * @hw: pointer to the HW structure
497 *
498 * Checks for link up on the hardware. If link is not up and we have
499 * a signal, then we need to force link up.
500 **/
501s32 e1000e_check_for_serdes_link(struct e1000_hw *hw)
502{
503 struct e1000_mac_info *mac = &hw->mac;
504 u32 rxcw;
505 u32 ctrl;
506 u32 status;
507 s32 ret_val;
508
509 ctrl = er32(CTRL);
510 status = er32(STATUS);
511 rxcw = er32(RXCW);
512
Bruce Allanad680762008-03-28 09:15:03 -0700513 /*
514 * If we don't have link (auto-negotiation failed or link partner
Auke Kokbc7f75f2007-09-17 12:30:59 -0700515 * cannot auto-negotiate), and our link partner is not trying to
516 * auto-negotiate with us (we are receiving idles or data),
517 * we need to force link up. We also need to give auto-negotiation
518 * time to complete.
519 */
520 /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
521 if ((!(status & E1000_STATUS_LU)) && (!(rxcw & E1000_RXCW_C))) {
522 if (mac->autoneg_failed == 0) {
523 mac->autoneg_failed = 1;
524 return 0;
525 }
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000526 e_dbg("NOT RXing /C/, disable AutoNeg and force link.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700527
528 /* Disable auto-negotiation in the TXCW register */
529 ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
530
531 /* Force link-up and also force full-duplex. */
532 ctrl = er32(CTRL);
533 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
534 ew32(CTRL, ctrl);
535
536 /* Configure Flow Control after forcing link up. */
537 ret_val = e1000e_config_fc_after_link_up(hw);
538 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000539 e_dbg("Error configuring flow control\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700540 return ret_val;
541 }
542 } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
Bruce Allanad680762008-03-28 09:15:03 -0700543 /*
544 * If we are forcing link and we are receiving /C/ ordered
Auke Kokbc7f75f2007-09-17 12:30:59 -0700545 * sets, re-enable auto-negotiation in the TXCW register
546 * and disable forced link in the Device Control register
547 * in an attempt to auto-negotiate with our link partner.
548 */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000549 e_dbg("RXing /C/, enable AutoNeg and stop forcing link.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700550 ew32(TXCW, mac->txcw);
551 ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
552
Alex Chiang612e2442009-02-05 23:55:45 -0800553 mac->serdes_has_link = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700554 } else if (!(E1000_TXCW_ANE & er32(TXCW))) {
Bruce Allanad680762008-03-28 09:15:03 -0700555 /*
556 * If we force link for non-auto-negotiation switch, check
Auke Kokbc7f75f2007-09-17 12:30:59 -0700557 * link status based on MAC synchronization for internal
558 * serdes media type.
559 */
560 /* SYNCH bit and IV bit are sticky. */
561 udelay(10);
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800562 rxcw = er32(RXCW);
563 if (rxcw & E1000_RXCW_SYNCH) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700564 if (!(rxcw & E1000_RXCW_IV)) {
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800565 mac->serdes_has_link = true;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000566 e_dbg("SERDES: Link up - forced.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700567 }
568 } else {
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800569 mac->serdes_has_link = false;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000570 e_dbg("SERDES: Link down - force failed.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700571 }
572 }
573
574 if (E1000_TXCW_ANE & er32(TXCW)) {
575 status = er32(STATUS);
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800576 if (status & E1000_STATUS_LU) {
577 /* SYNCH bit and IV bit are sticky, so reread rxcw. */
578 udelay(10);
579 rxcw = er32(RXCW);
580 if (rxcw & E1000_RXCW_SYNCH) {
581 if (!(rxcw & E1000_RXCW_IV)) {
582 mac->serdes_has_link = true;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000583 e_dbg("SERDES: Link up - autoneg "
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800584 "completed sucessfully.\n");
585 } else {
586 mac->serdes_has_link = false;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000587 e_dbg("SERDES: Link down - invalid"
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800588 "codewords detected in autoneg.\n");
589 }
590 } else {
591 mac->serdes_has_link = false;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000592 e_dbg("SERDES: Link down - no sync.\n");
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800593 }
594 } else {
595 mac->serdes_has_link = false;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000596 e_dbg("SERDES: Link down - autoneg failed\n");
Bruce Allan63dcf3d2008-11-21 16:50:34 -0800597 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700598 }
599
600 return 0;
601}
602
603/**
604 * e1000_set_default_fc_generic - Set flow control default values
605 * @hw: pointer to the HW structure
606 *
607 * Read the EEPROM for the default values for flow control and store the
608 * values.
609 **/
610static s32 e1000_set_default_fc_generic(struct e1000_hw *hw)
611{
Auke Kokbc7f75f2007-09-17 12:30:59 -0700612 s32 ret_val;
613 u16 nvm_data;
614
Bruce Allanad680762008-03-28 09:15:03 -0700615 /*
616 * Read and store word 0x0F of the EEPROM. This word contains bits
Auke Kokbc7f75f2007-09-17 12:30:59 -0700617 * that determine the hardware's default PAUSE (flow control) mode,
618 * a bit that determines whether the HW defaults to enabling or
619 * disabling auto-negotiation, and the direction of the
620 * SW defined pins. If there is no SW over-ride of the flow
621 * control setting, then the variable hw->fc will
622 * be initialized based on a value in the EEPROM.
623 */
624 ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
625
626 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000627 e_dbg("NVM Read Error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700628 return ret_val;
629 }
630
631 if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == 0)
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800632 hw->fc.requested_mode = e1000_fc_none;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700633 else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
634 NVM_WORD0F_ASM_DIR)
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800635 hw->fc.requested_mode = e1000_fc_tx_pause;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700636 else
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800637 hw->fc.requested_mode = e1000_fc_full;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700638
639 return 0;
640}
641
642/**
643 * e1000e_setup_link - Setup flow control and link settings
644 * @hw: pointer to the HW structure
645 *
646 * Determines which flow control settings to use, then configures flow
647 * control. Calls the appropriate media-specific link configuration
648 * function. Assuming the adapter has a valid link partner, a valid link
649 * should be established. Assumes the hardware has previously been reset
650 * and the transmitter and receiver are not enabled.
651 **/
652s32 e1000e_setup_link(struct e1000_hw *hw)
653{
654 struct e1000_mac_info *mac = &hw->mac;
655 s32 ret_val;
656
Bruce Allanad680762008-03-28 09:15:03 -0700657 /*
658 * In the case of the phy reset being blocked, we already have a link.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700659 * We do not need to set it up again.
660 */
661 if (e1000_check_reset_block(hw))
662 return 0;
663
Auke Kok309af402007-10-05 15:22:02 -0700664 /*
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800665 * If requested flow control is set to default, set flow control
666 * based on the EEPROM flow control settings.
Auke Kok309af402007-10-05 15:22:02 -0700667 */
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800668 if (hw->fc.requested_mode == e1000_fc_default) {
Auke Kok309af402007-10-05 15:22:02 -0700669 ret_val = e1000_set_default_fc_generic(hw);
670 if (ret_val)
671 return ret_val;
672 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700673
Bruce Allanad680762008-03-28 09:15:03 -0700674 /*
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800675 * Save off the requested flow control mode for use later. Depending
676 * on the link partner's capabilities, we may or may not use this mode.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700677 */
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800678 hw->fc.current_mode = hw->fc.requested_mode;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700679
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000680 e_dbg("After fix-ups FlowControl is now = %x\n",
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800681 hw->fc.current_mode);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700682
683 /* Call the necessary media_type subroutine to configure the link. */
684 ret_val = mac->ops.setup_physical_interface(hw);
685 if (ret_val)
686 return ret_val;
687
Bruce Allanad680762008-03-28 09:15:03 -0700688 /*
689 * Initialize the flow control address, type, and PAUSE timer
Auke Kokbc7f75f2007-09-17 12:30:59 -0700690 * registers to their default values. This is done even if flow
691 * control is disabled, because it does not hurt anything to
692 * initialize these registers.
693 */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000694 e_dbg("Initializing the Flow Control address, type and timer regs\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700695 ew32(FCT, FLOW_CONTROL_TYPE);
696 ew32(FCAH, FLOW_CONTROL_ADDRESS_HIGH);
697 ew32(FCAL, FLOW_CONTROL_ADDRESS_LOW);
698
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700699 ew32(FCTTV, hw->fc.pause_time);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700700
701 return e1000e_set_fc_watermarks(hw);
702}
703
704/**
705 * e1000_commit_fc_settings_generic - Configure flow control
706 * @hw: pointer to the HW structure
707 *
708 * Write the flow control settings to the Transmit Config Word Register (TXCW)
709 * base on the flow control settings in e1000_mac_info.
710 **/
711static s32 e1000_commit_fc_settings_generic(struct e1000_hw *hw)
712{
713 struct e1000_mac_info *mac = &hw->mac;
714 u32 txcw;
715
Bruce Allanad680762008-03-28 09:15:03 -0700716 /*
717 * Check for a software override of the flow control settings, and
Auke Kokbc7f75f2007-09-17 12:30:59 -0700718 * setup the device accordingly. If auto-negotiation is enabled, then
719 * software will have to set the "PAUSE" bits to the correct value in
720 * the Transmit Config Word Register (TXCW) and re-start auto-
721 * negotiation. However, if auto-negotiation is disabled, then
722 * software will have to manually configure the two flow control enable
723 * bits in the CTRL register.
724 *
725 * The possible values of the "fc" parameter are:
726 * 0: Flow control is completely disabled
727 * 1: Rx flow control is enabled (we can receive pause frames,
728 * but not send pause frames).
729 * 2: Tx flow control is enabled (we can send pause frames but we
730 * do not support receiving pause frames).
Bruce Allanad680762008-03-28 09:15:03 -0700731 * 3: Both Rx and Tx flow control (symmetric) are enabled.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700732 */
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800733 switch (hw->fc.current_mode) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700734 case e1000_fc_none:
735 /* Flow control completely disabled by a software over-ride. */
736 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
737 break;
738 case e1000_fc_rx_pause:
Bruce Allanad680762008-03-28 09:15:03 -0700739 /*
740 * Rx Flow control is enabled and Tx Flow control is disabled
Auke Kokbc7f75f2007-09-17 12:30:59 -0700741 * by a software over-ride. Since there really isn't a way to
Bruce Allanad680762008-03-28 09:15:03 -0700742 * advertise that we are capable of Rx Pause ONLY, we will
743 * advertise that we support both symmetric and asymmetric Rx
Auke Kokbc7f75f2007-09-17 12:30:59 -0700744 * PAUSE. Later, we will disable the adapter's ability to send
745 * PAUSE frames.
746 */
747 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
748 break;
749 case e1000_fc_tx_pause:
Bruce Allanad680762008-03-28 09:15:03 -0700750 /*
751 * Tx Flow control is enabled, and Rx Flow control is disabled,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700752 * by a software over-ride.
753 */
754 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
755 break;
756 case e1000_fc_full:
Bruce Allanad680762008-03-28 09:15:03 -0700757 /*
758 * Flow control (both Rx and Tx) is enabled by a software
Auke Kokbc7f75f2007-09-17 12:30:59 -0700759 * over-ride.
760 */
761 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
762 break;
763 default:
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000764 e_dbg("Flow control param set incorrectly\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700765 return -E1000_ERR_CONFIG;
766 break;
767 }
768
769 ew32(TXCW, txcw);
770 mac->txcw = txcw;
771
772 return 0;
773}
774
775/**
776 * e1000_poll_fiber_serdes_link_generic - Poll for link up
777 * @hw: pointer to the HW structure
778 *
779 * Polls for link up by reading the status register, if link fails to come
780 * up with auto-negotiation, then the link is forced if a signal is detected.
781 **/
782static s32 e1000_poll_fiber_serdes_link_generic(struct e1000_hw *hw)
783{
784 struct e1000_mac_info *mac = &hw->mac;
785 u32 i, status;
786 s32 ret_val;
787
Bruce Allanad680762008-03-28 09:15:03 -0700788 /*
789 * If we have a signal (the cable is plugged in, or assumed true for
Auke Kokbc7f75f2007-09-17 12:30:59 -0700790 * serdes media) then poll for a "Link-Up" indication in the Device
791 * Status Register. Time-out if a link isn't seen in 500 milliseconds
792 * seconds (Auto-negotiation should complete in less than 500
793 * milliseconds even if the other end is doing it in SW).
794 */
795 for (i = 0; i < FIBER_LINK_UP_LIMIT; i++) {
796 msleep(10);
797 status = er32(STATUS);
798 if (status & E1000_STATUS_LU)
799 break;
800 }
801 if (i == FIBER_LINK_UP_LIMIT) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000802 e_dbg("Never got a valid link from auto-neg!!!\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700803 mac->autoneg_failed = 1;
Bruce Allanad680762008-03-28 09:15:03 -0700804 /*
805 * AutoNeg failed to achieve a link, so we'll call
Auke Kokbc7f75f2007-09-17 12:30:59 -0700806 * mac->check_for_link. This routine will force the
807 * link up if we detect a signal. This will allow us to
808 * communicate with non-autonegotiating link partners.
809 */
810 ret_val = mac->ops.check_for_link(hw);
811 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000812 e_dbg("Error while checking for link\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700813 return ret_val;
814 }
815 mac->autoneg_failed = 0;
816 } else {
817 mac->autoneg_failed = 0;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000818 e_dbg("Valid Link Found\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700819 }
820
821 return 0;
822}
823
824/**
825 * e1000e_setup_fiber_serdes_link - Setup link for fiber/serdes
826 * @hw: pointer to the HW structure
827 *
828 * Configures collision distance and flow control for fiber and serdes
829 * links. Upon successful setup, poll for link.
830 **/
831s32 e1000e_setup_fiber_serdes_link(struct e1000_hw *hw)
832{
833 u32 ctrl;
834 s32 ret_val;
835
836 ctrl = er32(CTRL);
837
838 /* Take the link out of reset */
839 ctrl &= ~E1000_CTRL_LRST;
840
841 e1000e_config_collision_dist(hw);
842
843 ret_val = e1000_commit_fc_settings_generic(hw);
844 if (ret_val)
845 return ret_val;
846
Bruce Allanad680762008-03-28 09:15:03 -0700847 /*
848 * Since auto-negotiation is enabled, take the link out of reset (the
Auke Kokbc7f75f2007-09-17 12:30:59 -0700849 * link will be in reset, because we previously reset the chip). This
850 * will restart auto-negotiation. If auto-negotiation is successful
851 * then the link-up status bit will be set and the flow control enable
852 * bits (RFCE and TFCE) will be set according to their negotiated value.
853 */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000854 e_dbg("Auto-negotiation enabled\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700855
856 ew32(CTRL, ctrl);
857 e1e_flush();
858 msleep(1);
859
Bruce Allanad680762008-03-28 09:15:03 -0700860 /*
861 * For these adapters, the SW definable pin 1 is set when the optics
Auke Kokbc7f75f2007-09-17 12:30:59 -0700862 * detect a signal. If we have a signal, then poll for a "Link-Up"
863 * indication.
864 */
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700865 if (hw->phy.media_type == e1000_media_type_internal_serdes ||
Auke Kokbc7f75f2007-09-17 12:30:59 -0700866 (er32(CTRL) & E1000_CTRL_SWDPIN1)) {
867 ret_val = e1000_poll_fiber_serdes_link_generic(hw);
868 } else {
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000869 e_dbg("No signal detected\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700870 }
871
872 return 0;
873}
874
875/**
876 * e1000e_config_collision_dist - Configure collision distance
877 * @hw: pointer to the HW structure
878 *
879 * Configures the collision distance to the default value and is used
880 * during link setup. Currently no func pointer exists and all
881 * implementations are handled in the generic version of this function.
882 **/
883void e1000e_config_collision_dist(struct e1000_hw *hw)
884{
885 u32 tctl;
886
887 tctl = er32(TCTL);
888
889 tctl &= ~E1000_TCTL_COLD;
890 tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
891
892 ew32(TCTL, tctl);
893 e1e_flush();
894}
895
896/**
897 * e1000e_set_fc_watermarks - Set flow control high/low watermarks
898 * @hw: pointer to the HW structure
899 *
900 * Sets the flow control high/low threshold (watermark) registers. If
901 * flow control XON frame transmission is enabled, then set XON frame
Bruce Allanad680762008-03-28 09:15:03 -0700902 * transmission as well.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700903 **/
904s32 e1000e_set_fc_watermarks(struct e1000_hw *hw)
905{
Auke Kokbc7f75f2007-09-17 12:30:59 -0700906 u32 fcrtl = 0, fcrth = 0;
907
Bruce Allanad680762008-03-28 09:15:03 -0700908 /*
909 * Set the flow control receive threshold registers. Normally,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700910 * these registers will be set to a default threshold that may be
911 * adjusted later by the driver's runtime code. However, if the
912 * ability to transmit pause frames is not enabled, then these
913 * registers will be set to 0.
914 */
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800915 if (hw->fc.current_mode & e1000_fc_tx_pause) {
Bruce Allanad680762008-03-28 09:15:03 -0700916 /*
917 * We need to set up the Receive Threshold high and low water
Auke Kokbc7f75f2007-09-17 12:30:59 -0700918 * marks as well as (optionally) enabling the transmission of
919 * XON frames.
920 */
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700921 fcrtl = hw->fc.low_water;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700922 fcrtl |= E1000_FCRTL_XONE;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700923 fcrth = hw->fc.high_water;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700924 }
925 ew32(FCRTL, fcrtl);
926 ew32(FCRTH, fcrth);
927
928 return 0;
929}
930
931/**
932 * e1000e_force_mac_fc - Force the MAC's flow control settings
933 * @hw: pointer to the HW structure
934 *
935 * Force the MAC's flow control settings. Sets the TFCE and RFCE bits in the
936 * device control register to reflect the adapter settings. TFCE and RFCE
937 * need to be explicitly set by software when a copper PHY is used because
938 * autonegotiation is managed by the PHY rather than the MAC. Software must
939 * also configure these bits when link is forced on a fiber connection.
940 **/
941s32 e1000e_force_mac_fc(struct e1000_hw *hw)
942{
Auke Kokbc7f75f2007-09-17 12:30:59 -0700943 u32 ctrl;
944
945 ctrl = er32(CTRL);
946
Bruce Allanad680762008-03-28 09:15:03 -0700947 /*
948 * Because we didn't get link via the internal auto-negotiation
Auke Kokbc7f75f2007-09-17 12:30:59 -0700949 * mechanism (we either forced link or we got link via PHY
950 * auto-neg), we have to manually enable/disable transmit an
951 * receive flow control.
952 *
953 * The "Case" statement below enables/disable flow control
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800954 * according to the "hw->fc.current_mode" parameter.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700955 *
956 * The possible values of the "fc" parameter are:
957 * 0: Flow control is completely disabled
958 * 1: Rx flow control is enabled (we can receive pause
959 * frames but not send pause frames).
960 * 2: Tx flow control is enabled (we can send pause frames
961 * frames but we do not receive pause frames).
Bruce Allanad680762008-03-28 09:15:03 -0700962 * 3: Both Rx and Tx flow control (symmetric) is enabled.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700963 * other: No other values should be possible at this point.
964 */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000965 e_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700966
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800967 switch (hw->fc.current_mode) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700968 case e1000_fc_none:
969 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
970 break;
971 case e1000_fc_rx_pause:
972 ctrl &= (~E1000_CTRL_TFCE);
973 ctrl |= E1000_CTRL_RFCE;
974 break;
975 case e1000_fc_tx_pause:
976 ctrl &= (~E1000_CTRL_RFCE);
977 ctrl |= E1000_CTRL_TFCE;
978 break;
979 case e1000_fc_full:
980 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
981 break;
982 default:
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000983 e_dbg("Flow control param set incorrectly\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700984 return -E1000_ERR_CONFIG;
985 }
986
987 ew32(CTRL, ctrl);
988
989 return 0;
990}
991
992/**
993 * e1000e_config_fc_after_link_up - Configures flow control after link
994 * @hw: pointer to the HW structure
995 *
996 * Checks the status of auto-negotiation after link up to ensure that the
997 * speed and duplex were not forced. If the link needed to be forced, then
998 * flow control needs to be forced also. If auto-negotiation is enabled
999 * and did not fail, then we configure flow control based on our link
1000 * partner.
1001 **/
1002s32 e1000e_config_fc_after_link_up(struct e1000_hw *hw)
1003{
1004 struct e1000_mac_info *mac = &hw->mac;
1005 s32 ret_val = 0;
1006 u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
1007 u16 speed, duplex;
1008
Bruce Allanad680762008-03-28 09:15:03 -07001009 /*
1010 * Check for the case where we have fiber media and auto-neg failed
Auke Kokbc7f75f2007-09-17 12:30:59 -07001011 * so we had to force link. In this case, we need to force the
1012 * configuration of the MAC to match the "fc" parameter.
1013 */
1014 if (mac->autoneg_failed) {
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001015 if (hw->phy.media_type == e1000_media_type_fiber ||
1016 hw->phy.media_type == e1000_media_type_internal_serdes)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001017 ret_val = e1000e_force_mac_fc(hw);
1018 } else {
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001019 if (hw->phy.media_type == e1000_media_type_copper)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001020 ret_val = e1000e_force_mac_fc(hw);
1021 }
1022
1023 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001024 e_dbg("Error forcing flow control settings\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001025 return ret_val;
1026 }
1027
Bruce Allanad680762008-03-28 09:15:03 -07001028 /*
1029 * Check for the case where we have copper media and auto-neg is
Auke Kokbc7f75f2007-09-17 12:30:59 -07001030 * enabled. In this case, we need to check and see if Auto-Neg
1031 * has completed, and if so, how the PHY and link partner has
1032 * flow control configured.
1033 */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001034 if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
Bruce Allanad680762008-03-28 09:15:03 -07001035 /*
1036 * Read the MII Status Register and check to see if AutoNeg
Auke Kokbc7f75f2007-09-17 12:30:59 -07001037 * has completed. We read this twice because this reg has
1038 * some "sticky" (latched) bits.
1039 */
1040 ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
1041 if (ret_val)
1042 return ret_val;
1043 ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
1044 if (ret_val)
1045 return ret_val;
1046
1047 if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001048 e_dbg("Copper PHY and Auto Neg "
Auke Kokbc7f75f2007-09-17 12:30:59 -07001049 "has not completed.\n");
1050 return ret_val;
1051 }
1052
Bruce Allanad680762008-03-28 09:15:03 -07001053 /*
1054 * The AutoNeg process has completed, so we now need to
Auke Kokbc7f75f2007-09-17 12:30:59 -07001055 * read both the Auto Negotiation Advertisement
1056 * Register (Address 4) and the Auto_Negotiation Base
1057 * Page Ability Register (Address 5) to determine how
1058 * flow control was negotiated.
1059 */
1060 ret_val = e1e_rphy(hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg);
1061 if (ret_val)
1062 return ret_val;
1063 ret_val = e1e_rphy(hw, PHY_LP_ABILITY, &mii_nway_lp_ability_reg);
1064 if (ret_val)
1065 return ret_val;
1066
Bruce Allanad680762008-03-28 09:15:03 -07001067 /*
1068 * Two bits in the Auto Negotiation Advertisement Register
Auke Kokbc7f75f2007-09-17 12:30:59 -07001069 * (Address 4) and two bits in the Auto Negotiation Base
1070 * Page Ability Register (Address 5) determine flow control
1071 * for both the PHY and the link partner. The following
1072 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1073 * 1999, describes these PAUSE resolution bits and how flow
1074 * control is determined based upon these settings.
1075 * NOTE: DC = Don't Care
1076 *
1077 * LOCAL DEVICE | LINK PARTNER
1078 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1079 *-------|---------|-------|---------|--------------------
1080 * 0 | 0 | DC | DC | e1000_fc_none
1081 * 0 | 1 | 0 | DC | e1000_fc_none
1082 * 0 | 1 | 1 | 0 | e1000_fc_none
1083 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
1084 * 1 | 0 | 0 | DC | e1000_fc_none
1085 * 1 | DC | 1 | DC | e1000_fc_full
1086 * 1 | 1 | 0 | 0 | e1000_fc_none
1087 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
1088 *
Bruce Allanad680762008-03-28 09:15:03 -07001089 *
1090 * Are both PAUSE bits set to 1? If so, this implies
Auke Kokbc7f75f2007-09-17 12:30:59 -07001091 * Symmetric Flow Control is enabled at both ends. The
1092 * ASM_DIR bits are irrelevant per the spec.
1093 *
1094 * For Symmetric Flow Control:
1095 *
1096 * LOCAL DEVICE | LINK PARTNER
1097 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1098 *-------|---------|-------|---------|--------------------
1099 * 1 | DC | 1 | DC | E1000_fc_full
1100 *
1101 */
1102 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1103 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
Bruce Allanad680762008-03-28 09:15:03 -07001104 /*
1105 * Now we need to check if the user selected Rx ONLY
Auke Kokbc7f75f2007-09-17 12:30:59 -07001106 * of pause frames. In this case, we had to advertise
Bruce Allanad680762008-03-28 09:15:03 -07001107 * FULL flow control because we could not advertise Rx
Auke Kokbc7f75f2007-09-17 12:30:59 -07001108 * ONLY. Hence, we must now check to see if we need to
1109 * turn OFF the TRANSMISSION of PAUSE frames.
1110 */
Bruce Allan5c48ef3e22008-11-21 16:57:36 -08001111 if (hw->fc.requested_mode == e1000_fc_full) {
1112 hw->fc.current_mode = e1000_fc_full;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001113 e_dbg("Flow Control = FULL.\r\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001114 } else {
Bruce Allan5c48ef3e22008-11-21 16:57:36 -08001115 hw->fc.current_mode = e1000_fc_rx_pause;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001116 e_dbg("Flow Control = "
Auke Kokbc7f75f2007-09-17 12:30:59 -07001117 "RX PAUSE frames only.\r\n");
1118 }
1119 }
Bruce Allanad680762008-03-28 09:15:03 -07001120 /*
1121 * For receiving PAUSE frames ONLY.
Auke Kokbc7f75f2007-09-17 12:30:59 -07001122 *
1123 * LOCAL DEVICE | LINK PARTNER
1124 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1125 *-------|---------|-------|---------|--------------------
1126 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
1127 *
1128 */
1129 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1130 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1131 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1132 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
Bruce Allan5c48ef3e22008-11-21 16:57:36 -08001133 hw->fc.current_mode = e1000_fc_tx_pause;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001134 e_dbg("Flow Control = Tx PAUSE frames only.\r\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001135 }
Bruce Allanad680762008-03-28 09:15:03 -07001136 /*
1137 * For transmitting PAUSE frames ONLY.
Auke Kokbc7f75f2007-09-17 12:30:59 -07001138 *
1139 * LOCAL DEVICE | LINK PARTNER
1140 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1141 *-------|---------|-------|---------|--------------------
1142 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
1143 *
1144 */
1145 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1146 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1147 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1148 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
Bruce Allan5c48ef3e22008-11-21 16:57:36 -08001149 hw->fc.current_mode = e1000_fc_rx_pause;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001150 e_dbg("Flow Control = Rx PAUSE frames only.\r\n");
Jesse Brandeburgde92d842008-02-21 15:11:02 -08001151 } else {
1152 /*
1153 * Per the IEEE spec, at this point flow control
1154 * should be disabled.
1155 */
Bruce Allan5c48ef3e22008-11-21 16:57:36 -08001156 hw->fc.current_mode = e1000_fc_none;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001157 e_dbg("Flow Control = NONE.\r\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001158 }
1159
Bruce Allanad680762008-03-28 09:15:03 -07001160 /*
1161 * Now we need to do one last check... If we auto-
Auke Kokbc7f75f2007-09-17 12:30:59 -07001162 * negotiated to HALF DUPLEX, flow control should not be
1163 * enabled per IEEE 802.3 spec.
1164 */
1165 ret_val = mac->ops.get_link_up_info(hw, &speed, &duplex);
1166 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001167 e_dbg("Error getting link speed and duplex\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001168 return ret_val;
1169 }
1170
1171 if (duplex == HALF_DUPLEX)
Bruce Allan5c48ef3e22008-11-21 16:57:36 -08001172 hw->fc.current_mode = e1000_fc_none;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001173
Bruce Allanad680762008-03-28 09:15:03 -07001174 /*
1175 * Now we call a subroutine to actually force the MAC
Auke Kokbc7f75f2007-09-17 12:30:59 -07001176 * controller to use the correct flow control settings.
1177 */
1178 ret_val = e1000e_force_mac_fc(hw);
1179 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001180 e_dbg("Error forcing flow control settings\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001181 return ret_val;
1182 }
1183 }
1184
1185 return 0;
1186}
1187
1188/**
Auke Kok489815c2008-02-21 15:11:07 -08001189 * e1000e_get_speed_and_duplex_copper - Retrieve current speed/duplex
Auke Kokbc7f75f2007-09-17 12:30:59 -07001190 * @hw: pointer to the HW structure
1191 * @speed: stores the current speed
1192 * @duplex: stores the current duplex
1193 *
1194 * Read the status register for the current speed/duplex and store the current
1195 * speed and duplex for copper connections.
1196 **/
1197s32 e1000e_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed, u16 *duplex)
1198{
1199 u32 status;
1200
1201 status = er32(STATUS);
1202 if (status & E1000_STATUS_SPEED_1000) {
1203 *speed = SPEED_1000;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001204 e_dbg("1000 Mbs, ");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001205 } else if (status & E1000_STATUS_SPEED_100) {
1206 *speed = SPEED_100;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001207 e_dbg("100 Mbs, ");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001208 } else {
1209 *speed = SPEED_10;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001210 e_dbg("10 Mbs, ");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001211 }
1212
1213 if (status & E1000_STATUS_FD) {
1214 *duplex = FULL_DUPLEX;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001215 e_dbg("Full Duplex\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001216 } else {
1217 *duplex = HALF_DUPLEX;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001218 e_dbg("Half Duplex\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001219 }
1220
1221 return 0;
1222}
1223
1224/**
Auke Kok489815c2008-02-21 15:11:07 -08001225 * e1000e_get_speed_and_duplex_fiber_serdes - Retrieve current speed/duplex
Auke Kokbc7f75f2007-09-17 12:30:59 -07001226 * @hw: pointer to the HW structure
1227 * @speed: stores the current speed
1228 * @duplex: stores the current duplex
1229 *
1230 * Sets the speed and duplex to gigabit full duplex (the only possible option)
1231 * for fiber/serdes links.
1232 **/
1233s32 e1000e_get_speed_and_duplex_fiber_serdes(struct e1000_hw *hw, u16 *speed, u16 *duplex)
1234{
1235 *speed = SPEED_1000;
1236 *duplex = FULL_DUPLEX;
1237
1238 return 0;
1239}
1240
1241/**
1242 * e1000e_get_hw_semaphore - Acquire hardware semaphore
1243 * @hw: pointer to the HW structure
1244 *
1245 * Acquire the HW semaphore to access the PHY or NVM
1246 **/
1247s32 e1000e_get_hw_semaphore(struct e1000_hw *hw)
1248{
1249 u32 swsm;
1250 s32 timeout = hw->nvm.word_size + 1;
1251 s32 i = 0;
1252
1253 /* Get the SW semaphore */
1254 while (i < timeout) {
1255 swsm = er32(SWSM);
1256 if (!(swsm & E1000_SWSM_SMBI))
1257 break;
1258
1259 udelay(50);
1260 i++;
1261 }
1262
1263 if (i == timeout) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001264 e_dbg("Driver can't access device - SMBI bit is set.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001265 return -E1000_ERR_NVM;
1266 }
1267
1268 /* Get the FW semaphore. */
1269 for (i = 0; i < timeout; i++) {
1270 swsm = er32(SWSM);
1271 ew32(SWSM, swsm | E1000_SWSM_SWESMBI);
1272
1273 /* Semaphore acquired if bit latched */
1274 if (er32(SWSM) & E1000_SWSM_SWESMBI)
1275 break;
1276
1277 udelay(50);
1278 }
1279
1280 if (i == timeout) {
1281 /* Release semaphores */
1282 e1000e_put_hw_semaphore(hw);
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001283 e_dbg("Driver can't access the NVM\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001284 return -E1000_ERR_NVM;
1285 }
1286
1287 return 0;
1288}
1289
1290/**
1291 * e1000e_put_hw_semaphore - Release hardware semaphore
1292 * @hw: pointer to the HW structure
1293 *
1294 * Release hardware semaphore used to access the PHY or NVM
1295 **/
1296void e1000e_put_hw_semaphore(struct e1000_hw *hw)
1297{
1298 u32 swsm;
1299
1300 swsm = er32(SWSM);
1301 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1302 ew32(SWSM, swsm);
1303}
1304
1305/**
1306 * e1000e_get_auto_rd_done - Check for auto read completion
1307 * @hw: pointer to the HW structure
1308 *
1309 * Check EEPROM for Auto Read done bit.
1310 **/
1311s32 e1000e_get_auto_rd_done(struct e1000_hw *hw)
1312{
1313 s32 i = 0;
1314
1315 while (i < AUTO_READ_DONE_TIMEOUT) {
1316 if (er32(EECD) & E1000_EECD_AUTO_RD)
1317 break;
1318 msleep(1);
1319 i++;
1320 }
1321
1322 if (i == AUTO_READ_DONE_TIMEOUT) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001323 e_dbg("Auto read by HW from NVM has not completed.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001324 return -E1000_ERR_RESET;
1325 }
1326
1327 return 0;
1328}
1329
1330/**
1331 * e1000e_valid_led_default - Verify a valid default LED config
1332 * @hw: pointer to the HW structure
1333 * @data: pointer to the NVM (EEPROM)
1334 *
1335 * Read the EEPROM for the current default LED configuration. If the
1336 * LED configuration is not valid, set to a valid LED configuration.
1337 **/
1338s32 e1000e_valid_led_default(struct e1000_hw *hw, u16 *data)
1339{
1340 s32 ret_val;
1341
1342 ret_val = e1000_read_nvm(hw, NVM_ID_LED_SETTINGS, 1, data);
1343 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001344 e_dbg("NVM Read Error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001345 return ret_val;
1346 }
1347
1348 if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF)
1349 *data = ID_LED_DEFAULT;
1350
1351 return 0;
1352}
1353
1354/**
1355 * e1000e_id_led_init -
1356 * @hw: pointer to the HW structure
1357 *
1358 **/
1359s32 e1000e_id_led_init(struct e1000_hw *hw)
1360{
1361 struct e1000_mac_info *mac = &hw->mac;
1362 s32 ret_val;
1363 const u32 ledctl_mask = 0x000000FF;
1364 const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
1365 const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
1366 u16 data, i, temp;
1367 const u16 led_mask = 0x0F;
1368
1369 ret_val = hw->nvm.ops.valid_led_default(hw, &data);
1370 if (ret_val)
1371 return ret_val;
1372
1373 mac->ledctl_default = er32(LEDCTL);
1374 mac->ledctl_mode1 = mac->ledctl_default;
1375 mac->ledctl_mode2 = mac->ledctl_default;
1376
1377 for (i = 0; i < 4; i++) {
1378 temp = (data >> (i << 2)) & led_mask;
1379 switch (temp) {
1380 case ID_LED_ON1_DEF2:
1381 case ID_LED_ON1_ON2:
1382 case ID_LED_ON1_OFF2:
1383 mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1384 mac->ledctl_mode1 |= ledctl_on << (i << 3);
1385 break;
1386 case ID_LED_OFF1_DEF2:
1387 case ID_LED_OFF1_ON2:
1388 case ID_LED_OFF1_OFF2:
1389 mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1390 mac->ledctl_mode1 |= ledctl_off << (i << 3);
1391 break;
1392 default:
1393 /* Do nothing */
1394 break;
1395 }
1396 switch (temp) {
1397 case ID_LED_DEF1_ON2:
1398 case ID_LED_ON1_ON2:
1399 case ID_LED_OFF1_ON2:
1400 mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1401 mac->ledctl_mode2 |= ledctl_on << (i << 3);
1402 break;
1403 case ID_LED_DEF1_OFF2:
1404 case ID_LED_ON1_OFF2:
1405 case ID_LED_OFF1_OFF2:
1406 mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1407 mac->ledctl_mode2 |= ledctl_off << (i << 3);
1408 break;
1409 default:
1410 /* Do nothing */
1411 break;
1412 }
1413 }
1414
1415 return 0;
1416}
1417
1418/**
Bruce Allana4f58f52009-06-02 11:29:18 +00001419 * e1000e_setup_led_generic - Configures SW controllable LED
1420 * @hw: pointer to the HW structure
1421 *
1422 * This prepares the SW controllable LED for use and saves the current state
1423 * of the LED so it can be later restored.
1424 **/
1425s32 e1000e_setup_led_generic(struct e1000_hw *hw)
1426{
1427 u32 ledctl;
1428
1429 if (hw->mac.ops.setup_led != e1000e_setup_led_generic) {
1430 return -E1000_ERR_CONFIG;
1431 }
1432
1433 if (hw->phy.media_type == e1000_media_type_fiber) {
1434 ledctl = er32(LEDCTL);
1435 hw->mac.ledctl_default = ledctl;
1436 /* Turn off LED0 */
1437 ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
1438 E1000_LEDCTL_LED0_BLINK |
1439 E1000_LEDCTL_LED0_MODE_MASK);
1440 ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
1441 E1000_LEDCTL_LED0_MODE_SHIFT);
1442 ew32(LEDCTL, ledctl);
1443 } else if (hw->phy.media_type == e1000_media_type_copper) {
1444 ew32(LEDCTL, hw->mac.ledctl_mode1);
1445 }
1446
1447 return 0;
1448}
1449
1450/**
Auke Kokbc7f75f2007-09-17 12:30:59 -07001451 * e1000e_cleanup_led_generic - Set LED config to default operation
1452 * @hw: pointer to the HW structure
1453 *
1454 * Remove the current LED configuration and set the LED configuration
1455 * to the default value, saved from the EEPROM.
1456 **/
1457s32 e1000e_cleanup_led_generic(struct e1000_hw *hw)
1458{
1459 ew32(LEDCTL, hw->mac.ledctl_default);
1460 return 0;
1461}
1462
1463/**
1464 * e1000e_blink_led - Blink LED
1465 * @hw: pointer to the HW structure
1466 *
Auke Kok489815c2008-02-21 15:11:07 -08001467 * Blink the LEDs which are set to be on.
Auke Kokbc7f75f2007-09-17 12:30:59 -07001468 **/
1469s32 e1000e_blink_led(struct e1000_hw *hw)
1470{
1471 u32 ledctl_blink = 0;
1472 u32 i;
1473
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001474 if (hw->phy.media_type == e1000_media_type_fiber) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001475 /* always blink LED0 for PCI-E fiber */
1476 ledctl_blink = E1000_LEDCTL_LED0_BLINK |
1477 (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
1478 } else {
Bruce Allanad680762008-03-28 09:15:03 -07001479 /*
1480 * set the blink bit for each LED that's "on" (0x0E)
1481 * in ledctl_mode2
1482 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001483 ledctl_blink = hw->mac.ledctl_mode2;
1484 for (i = 0; i < 4; i++)
1485 if (((hw->mac.ledctl_mode2 >> (i * 8)) & 0xFF) ==
1486 E1000_LEDCTL_MODE_LED_ON)
1487 ledctl_blink |= (E1000_LEDCTL_LED0_BLINK <<
1488 (i * 8));
1489 }
1490
1491 ew32(LEDCTL, ledctl_blink);
1492
1493 return 0;
1494}
1495
1496/**
1497 * e1000e_led_on_generic - Turn LED on
1498 * @hw: pointer to the HW structure
1499 *
1500 * Turn LED on.
1501 **/
1502s32 e1000e_led_on_generic(struct e1000_hw *hw)
1503{
1504 u32 ctrl;
1505
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001506 switch (hw->phy.media_type) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001507 case e1000_media_type_fiber:
1508 ctrl = er32(CTRL);
1509 ctrl &= ~E1000_CTRL_SWDPIN0;
1510 ctrl |= E1000_CTRL_SWDPIO0;
1511 ew32(CTRL, ctrl);
1512 break;
1513 case e1000_media_type_copper:
1514 ew32(LEDCTL, hw->mac.ledctl_mode2);
1515 break;
1516 default:
1517 break;
1518 }
1519
1520 return 0;
1521}
1522
1523/**
1524 * e1000e_led_off_generic - Turn LED off
1525 * @hw: pointer to the HW structure
1526 *
1527 * Turn LED off.
1528 **/
1529s32 e1000e_led_off_generic(struct e1000_hw *hw)
1530{
1531 u32 ctrl;
1532
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001533 switch (hw->phy.media_type) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001534 case e1000_media_type_fiber:
1535 ctrl = er32(CTRL);
1536 ctrl |= E1000_CTRL_SWDPIN0;
1537 ctrl |= E1000_CTRL_SWDPIO0;
1538 ew32(CTRL, ctrl);
1539 break;
1540 case e1000_media_type_copper:
1541 ew32(LEDCTL, hw->mac.ledctl_mode1);
1542 break;
1543 default:
1544 break;
1545 }
1546
1547 return 0;
1548}
1549
1550/**
1551 * e1000e_set_pcie_no_snoop - Set PCI-express capabilities
1552 * @hw: pointer to the HW structure
1553 * @no_snoop: bitmap of snoop events
1554 *
1555 * Set the PCI-express register to snoop for events enabled in 'no_snoop'.
1556 **/
1557void e1000e_set_pcie_no_snoop(struct e1000_hw *hw, u32 no_snoop)
1558{
1559 u32 gcr;
1560
1561 if (no_snoop) {
1562 gcr = er32(GCR);
1563 gcr &= ~(PCIE_NO_SNOOP_ALL);
1564 gcr |= no_snoop;
1565 ew32(GCR, gcr);
1566 }
1567}
1568
1569/**
1570 * e1000e_disable_pcie_master - Disables PCI-express master access
1571 * @hw: pointer to the HW structure
1572 *
1573 * Returns 0 if successful, else returns -10
Auke Kok489815c2008-02-21 15:11:07 -08001574 * (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
Auke Kokbc7f75f2007-09-17 12:30:59 -07001575 * the master requests to be disabled.
1576 *
1577 * Disables PCI-Express master access and verifies there are no pending
1578 * requests.
1579 **/
1580s32 e1000e_disable_pcie_master(struct e1000_hw *hw)
1581{
1582 u32 ctrl;
1583 s32 timeout = MASTER_DISABLE_TIMEOUT;
1584
1585 ctrl = er32(CTRL);
1586 ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
1587 ew32(CTRL, ctrl);
1588
1589 while (timeout) {
1590 if (!(er32(STATUS) &
1591 E1000_STATUS_GIO_MASTER_ENABLE))
1592 break;
1593 udelay(100);
1594 timeout--;
1595 }
1596
1597 if (!timeout) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001598 e_dbg("Master requests are pending.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001599 return -E1000_ERR_MASTER_REQUESTS_PENDING;
1600 }
1601
1602 return 0;
1603}
1604
1605/**
1606 * e1000e_reset_adaptive - Reset Adaptive Interframe Spacing
1607 * @hw: pointer to the HW structure
1608 *
1609 * Reset the Adaptive Interframe Spacing throttle to default values.
1610 **/
1611void e1000e_reset_adaptive(struct e1000_hw *hw)
1612{
1613 struct e1000_mac_info *mac = &hw->mac;
1614
1615 mac->current_ifs_val = 0;
1616 mac->ifs_min_val = IFS_MIN;
1617 mac->ifs_max_val = IFS_MAX;
1618 mac->ifs_step_size = IFS_STEP;
1619 mac->ifs_ratio = IFS_RATIO;
1620
Bruce Allan564ea9b2009-11-20 23:26:44 +00001621 mac->in_ifs_mode = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001622 ew32(AIT, 0);
1623}
1624
1625/**
1626 * e1000e_update_adaptive - Update Adaptive Interframe Spacing
1627 * @hw: pointer to the HW structure
1628 *
1629 * Update the Adaptive Interframe Spacing Throttle value based on the
1630 * time between transmitted packets and time between collisions.
1631 **/
1632void e1000e_update_adaptive(struct e1000_hw *hw)
1633{
1634 struct e1000_mac_info *mac = &hw->mac;
1635
1636 if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
1637 if (mac->tx_packet_delta > MIN_NUM_XMITS) {
Bruce Allan564ea9b2009-11-20 23:26:44 +00001638 mac->in_ifs_mode = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001639 if (mac->current_ifs_val < mac->ifs_max_val) {
1640 if (!mac->current_ifs_val)
1641 mac->current_ifs_val = mac->ifs_min_val;
1642 else
1643 mac->current_ifs_val +=
1644 mac->ifs_step_size;
Bruce Allanad680762008-03-28 09:15:03 -07001645 ew32(AIT, mac->current_ifs_val);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001646 }
1647 }
1648 } else {
1649 if (mac->in_ifs_mode &&
1650 (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
1651 mac->current_ifs_val = 0;
Bruce Allan564ea9b2009-11-20 23:26:44 +00001652 mac->in_ifs_mode = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001653 ew32(AIT, 0);
1654 }
1655 }
1656}
1657
1658/**
1659 * e1000_raise_eec_clk - Raise EEPROM clock
1660 * @hw: pointer to the HW structure
1661 * @eecd: pointer to the EEPROM
1662 *
1663 * Enable/Raise the EEPROM clock bit.
1664 **/
1665static void e1000_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
1666{
1667 *eecd = *eecd | E1000_EECD_SK;
1668 ew32(EECD, *eecd);
1669 e1e_flush();
1670 udelay(hw->nvm.delay_usec);
1671}
1672
1673/**
1674 * e1000_lower_eec_clk - Lower EEPROM clock
1675 * @hw: pointer to the HW structure
1676 * @eecd: pointer to the EEPROM
1677 *
1678 * Clear/Lower the EEPROM clock bit.
1679 **/
1680static void e1000_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
1681{
1682 *eecd = *eecd & ~E1000_EECD_SK;
1683 ew32(EECD, *eecd);
1684 e1e_flush();
1685 udelay(hw->nvm.delay_usec);
1686}
1687
1688/**
1689 * e1000_shift_out_eec_bits - Shift data bits our to the EEPROM
1690 * @hw: pointer to the HW structure
1691 * @data: data to send to the EEPROM
1692 * @count: number of bits to shift out
1693 *
1694 * We need to shift 'count' bits out to the EEPROM. So, the value in the
1695 * "data" parameter will be shifted out to the EEPROM one bit at a time.
1696 * In order to do this, "data" must be broken down into bits.
1697 **/
1698static void e1000_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
1699{
1700 struct e1000_nvm_info *nvm = &hw->nvm;
1701 u32 eecd = er32(EECD);
1702 u32 mask;
1703
1704 mask = 0x01 << (count - 1);
1705 if (nvm->type == e1000_nvm_eeprom_spi)
1706 eecd |= E1000_EECD_DO;
1707
1708 do {
1709 eecd &= ~E1000_EECD_DI;
1710
1711 if (data & mask)
1712 eecd |= E1000_EECD_DI;
1713
1714 ew32(EECD, eecd);
1715 e1e_flush();
1716
1717 udelay(nvm->delay_usec);
1718
1719 e1000_raise_eec_clk(hw, &eecd);
1720 e1000_lower_eec_clk(hw, &eecd);
1721
1722 mask >>= 1;
1723 } while (mask);
1724
1725 eecd &= ~E1000_EECD_DI;
1726 ew32(EECD, eecd);
1727}
1728
1729/**
1730 * e1000_shift_in_eec_bits - Shift data bits in from the EEPROM
1731 * @hw: pointer to the HW structure
1732 * @count: number of bits to shift in
1733 *
1734 * In order to read a register from the EEPROM, we need to shift 'count' bits
1735 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
1736 * the EEPROM (setting the SK bit), and then reading the value of the data out
1737 * "DO" bit. During this "shifting in" process the data in "DI" bit should
1738 * always be clear.
1739 **/
1740static u16 e1000_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
1741{
1742 u32 eecd;
1743 u32 i;
1744 u16 data;
1745
1746 eecd = er32(EECD);
1747
1748 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
1749 data = 0;
1750
1751 for (i = 0; i < count; i++) {
1752 data <<= 1;
1753 e1000_raise_eec_clk(hw, &eecd);
1754
1755 eecd = er32(EECD);
1756
1757 eecd &= ~E1000_EECD_DI;
1758 if (eecd & E1000_EECD_DO)
1759 data |= 1;
1760
1761 e1000_lower_eec_clk(hw, &eecd);
1762 }
1763
1764 return data;
1765}
1766
1767/**
1768 * e1000e_poll_eerd_eewr_done - Poll for EEPROM read/write completion
1769 * @hw: pointer to the HW structure
1770 * @ee_reg: EEPROM flag for polling
1771 *
1772 * Polls the EEPROM status bit for either read or write completion based
1773 * upon the value of 'ee_reg'.
1774 **/
1775s32 e1000e_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
1776{
1777 u32 attempts = 100000;
1778 u32 i, reg = 0;
1779
1780 for (i = 0; i < attempts; i++) {
1781 if (ee_reg == E1000_NVM_POLL_READ)
1782 reg = er32(EERD);
1783 else
1784 reg = er32(EEWR);
1785
1786 if (reg & E1000_NVM_RW_REG_DONE)
1787 return 0;
1788
1789 udelay(5);
1790 }
1791
1792 return -E1000_ERR_NVM;
1793}
1794
1795/**
1796 * e1000e_acquire_nvm - Generic request for access to EEPROM
1797 * @hw: pointer to the HW structure
1798 *
1799 * Set the EEPROM access request bit and wait for EEPROM access grant bit.
1800 * Return successful if access grant bit set, else clear the request for
1801 * EEPROM access and return -E1000_ERR_NVM (-1).
1802 **/
1803s32 e1000e_acquire_nvm(struct e1000_hw *hw)
1804{
1805 u32 eecd = er32(EECD);
1806 s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
1807
1808 ew32(EECD, eecd | E1000_EECD_REQ);
1809 eecd = er32(EECD);
1810
1811 while (timeout) {
1812 if (eecd & E1000_EECD_GNT)
1813 break;
1814 udelay(5);
1815 eecd = er32(EECD);
1816 timeout--;
1817 }
1818
1819 if (!timeout) {
1820 eecd &= ~E1000_EECD_REQ;
1821 ew32(EECD, eecd);
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001822 e_dbg("Could not acquire NVM grant\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001823 return -E1000_ERR_NVM;
1824 }
1825
1826 return 0;
1827}
1828
1829/**
1830 * e1000_standby_nvm - Return EEPROM to standby state
1831 * @hw: pointer to the HW structure
1832 *
1833 * Return the EEPROM to a standby state.
1834 **/
1835static void e1000_standby_nvm(struct e1000_hw *hw)
1836{
1837 struct e1000_nvm_info *nvm = &hw->nvm;
1838 u32 eecd = er32(EECD);
1839
1840 if (nvm->type == e1000_nvm_eeprom_spi) {
1841 /* Toggle CS to flush commands */
1842 eecd |= E1000_EECD_CS;
1843 ew32(EECD, eecd);
1844 e1e_flush();
1845 udelay(nvm->delay_usec);
1846 eecd &= ~E1000_EECD_CS;
1847 ew32(EECD, eecd);
1848 e1e_flush();
1849 udelay(nvm->delay_usec);
1850 }
1851}
1852
1853/**
1854 * e1000_stop_nvm - Terminate EEPROM command
1855 * @hw: pointer to the HW structure
1856 *
1857 * Terminates the current command by inverting the EEPROM's chip select pin.
1858 **/
1859static void e1000_stop_nvm(struct e1000_hw *hw)
1860{
1861 u32 eecd;
1862
1863 eecd = er32(EECD);
1864 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
1865 /* Pull CS high */
1866 eecd |= E1000_EECD_CS;
1867 e1000_lower_eec_clk(hw, &eecd);
1868 }
1869}
1870
1871/**
1872 * e1000e_release_nvm - Release exclusive access to EEPROM
1873 * @hw: pointer to the HW structure
1874 *
1875 * Stop any current commands to the EEPROM and clear the EEPROM request bit.
1876 **/
1877void e1000e_release_nvm(struct e1000_hw *hw)
1878{
1879 u32 eecd;
1880
1881 e1000_stop_nvm(hw);
1882
1883 eecd = er32(EECD);
1884 eecd &= ~E1000_EECD_REQ;
1885 ew32(EECD, eecd);
1886}
1887
1888/**
1889 * e1000_ready_nvm_eeprom - Prepares EEPROM for read/write
1890 * @hw: pointer to the HW structure
1891 *
1892 * Setups the EEPROM for reading and writing.
1893 **/
1894static s32 e1000_ready_nvm_eeprom(struct e1000_hw *hw)
1895{
1896 struct e1000_nvm_info *nvm = &hw->nvm;
1897 u32 eecd = er32(EECD);
1898 u16 timeout = 0;
1899 u8 spi_stat_reg;
1900
1901 if (nvm->type == e1000_nvm_eeprom_spi) {
1902 /* Clear SK and CS */
1903 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
1904 ew32(EECD, eecd);
1905 udelay(1);
1906 timeout = NVM_MAX_RETRY_SPI;
1907
Bruce Allanad680762008-03-28 09:15:03 -07001908 /*
1909 * Read "Status Register" repeatedly until the LSB is cleared.
Auke Kokbc7f75f2007-09-17 12:30:59 -07001910 * The EEPROM will signal that the command has been completed
1911 * by clearing bit 0 of the internal status register. If it's
Bruce Allanad680762008-03-28 09:15:03 -07001912 * not cleared within 'timeout', then error out.
1913 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001914 while (timeout) {
1915 e1000_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
1916 hw->nvm.opcode_bits);
1917 spi_stat_reg = (u8)e1000_shift_in_eec_bits(hw, 8);
1918 if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
1919 break;
1920
1921 udelay(5);
1922 e1000_standby_nvm(hw);
1923 timeout--;
1924 }
1925
1926 if (!timeout) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001927 e_dbg("SPI NVM Status error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001928 return -E1000_ERR_NVM;
1929 }
1930 }
1931
1932 return 0;
1933}
1934
1935/**
Auke Kokbc7f75f2007-09-17 12:30:59 -07001936 * e1000e_read_nvm_eerd - Reads EEPROM using EERD register
1937 * @hw: pointer to the HW structure
1938 * @offset: offset of word in the EEPROM to read
1939 * @words: number of words to read
1940 * @data: word read from the EEPROM
1941 *
1942 * Reads a 16 bit word from the EEPROM using the EERD register.
1943 **/
1944s32 e1000e_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
1945{
1946 struct e1000_nvm_info *nvm = &hw->nvm;
1947 u32 i, eerd = 0;
1948 s32 ret_val = 0;
1949
Bruce Allanad680762008-03-28 09:15:03 -07001950 /*
1951 * A check for invalid values: offset too large, too many words,
1952 * too many words for the offset, and not enough words.
1953 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001954 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
1955 (words == 0)) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001956 e_dbg("nvm parameter(s) out of bounds\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001957 return -E1000_ERR_NVM;
1958 }
1959
1960 for (i = 0; i < words; i++) {
1961 eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
1962 E1000_NVM_RW_REG_START;
1963
1964 ew32(EERD, eerd);
1965 ret_val = e1000e_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
1966 if (ret_val)
1967 break;
1968
Bruce Allanad680762008-03-28 09:15:03 -07001969 data[i] = (er32(EERD) >> E1000_NVM_RW_REG_DATA);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001970 }
1971
1972 return ret_val;
1973}
1974
1975/**
1976 * e1000e_write_nvm_spi - Write to EEPROM using SPI
1977 * @hw: pointer to the HW structure
1978 * @offset: offset within the EEPROM to be written to
1979 * @words: number of words to write
1980 * @data: 16 bit word(s) to be written to the EEPROM
1981 *
1982 * Writes data to EEPROM at offset using SPI interface.
1983 *
1984 * If e1000e_update_nvm_checksum is not called after this function , the
Auke Kok489815c2008-02-21 15:11:07 -08001985 * EEPROM will most likely contain an invalid checksum.
Auke Kokbc7f75f2007-09-17 12:30:59 -07001986 **/
1987s32 e1000e_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
1988{
1989 struct e1000_nvm_info *nvm = &hw->nvm;
1990 s32 ret_val;
1991 u16 widx = 0;
1992
Bruce Allanad680762008-03-28 09:15:03 -07001993 /*
1994 * A check for invalid values: offset too large, too many words,
1995 * and not enough words.
1996 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001997 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
1998 (words == 0)) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001999 e_dbg("nvm parameter(s) out of bounds\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002000 return -E1000_ERR_NVM;
2001 }
2002
Bruce Allan94d81862009-11-20 23:25:26 +00002003 ret_val = nvm->ops.acquire(hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002004 if (ret_val)
2005 return ret_val;
2006
2007 msleep(10);
2008
2009 while (widx < words) {
2010 u8 write_opcode = NVM_WRITE_OPCODE_SPI;
2011
2012 ret_val = e1000_ready_nvm_eeprom(hw);
2013 if (ret_val) {
Bruce Allan94d81862009-11-20 23:25:26 +00002014 nvm->ops.release(hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002015 return ret_val;
2016 }
2017
2018 e1000_standby_nvm(hw);
2019
2020 /* Send the WRITE ENABLE command (8 bit opcode) */
2021 e1000_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
2022 nvm->opcode_bits);
2023
2024 e1000_standby_nvm(hw);
2025
Bruce Allanad680762008-03-28 09:15:03 -07002026 /*
2027 * Some SPI eeproms use the 8th address bit embedded in the
2028 * opcode
2029 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002030 if ((nvm->address_bits == 8) && (offset >= 128))
2031 write_opcode |= NVM_A8_OPCODE_SPI;
2032
2033 /* Send the Write command (8-bit opcode + addr) */
2034 e1000_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
2035 e1000_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
2036 nvm->address_bits);
2037
2038 /* Loop to allow for up to whole page write of eeprom */
2039 while (widx < words) {
2040 u16 word_out = data[widx];
2041 word_out = (word_out >> 8) | (word_out << 8);
2042 e1000_shift_out_eec_bits(hw, word_out, 16);
2043 widx++;
2044
2045 if ((((offset + widx) * 2) % nvm->page_size) == 0) {
2046 e1000_standby_nvm(hw);
2047 break;
2048 }
2049 }
2050 }
2051
2052 msleep(10);
Bruce Allan94d81862009-11-20 23:25:26 +00002053 nvm->ops.release(hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002054 return 0;
2055}
2056
2057/**
2058 * e1000e_read_mac_addr - Read device MAC address
2059 * @hw: pointer to the HW structure
2060 *
2061 * Reads the device MAC address from the EEPROM and stores the value.
2062 * Since devices with two ports use the same EEPROM, we increment the
2063 * last bit in the MAC address for the second port.
2064 **/
2065s32 e1000e_read_mac_addr(struct e1000_hw *hw)
2066{
2067 s32 ret_val;
2068 u16 offset, nvm_data, i;
Bill Hayes93ca1612007-10-31 15:21:52 -07002069 u16 mac_addr_offset = 0;
2070
2071 if (hw->mac.type == e1000_82571) {
2072 /* Check for an alternate MAC address. An alternate MAC
2073 * address can be setup by pre-boot software and must be
2074 * treated like a permanent address and must override the
Bruce Allanad680762008-03-28 09:15:03 -07002075 * actual permanent MAC address.*/
Bill Hayes93ca1612007-10-31 15:21:52 -07002076 ret_val = e1000_read_nvm(hw, NVM_ALT_MAC_ADDR_PTR, 1,
Bruce Allanad680762008-03-28 09:15:03 -07002077 &mac_addr_offset);
Bill Hayes93ca1612007-10-31 15:21:52 -07002078 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002079 e_dbg("NVM Read Error\n");
Bill Hayes93ca1612007-10-31 15:21:52 -07002080 return ret_val;
2081 }
2082 if (mac_addr_offset == 0xFFFF)
2083 mac_addr_offset = 0;
2084
2085 if (mac_addr_offset) {
2086 if (hw->bus.func == E1000_FUNC_1)
2087 mac_addr_offset += ETH_ALEN/sizeof(u16);
2088
2089 /* make sure we have a valid mac address here
Bruce Allanad680762008-03-28 09:15:03 -07002090 * before using it */
Bill Hayes93ca1612007-10-31 15:21:52 -07002091 ret_val = e1000_read_nvm(hw, mac_addr_offset, 1,
2092 &nvm_data);
2093 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002094 e_dbg("NVM Read Error\n");
Bill Hayes93ca1612007-10-31 15:21:52 -07002095 return ret_val;
2096 }
2097 if (nvm_data & 0x0001)
2098 mac_addr_offset = 0;
2099 }
2100
2101 if (mac_addr_offset)
Bruce Allanad680762008-03-28 09:15:03 -07002102 hw->dev_spec.e82571.alt_mac_addr_is_present = 1;
Bill Hayes93ca1612007-10-31 15:21:52 -07002103 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07002104
2105 for (i = 0; i < ETH_ALEN; i += 2) {
Bill Hayes93ca1612007-10-31 15:21:52 -07002106 offset = mac_addr_offset + (i >> 1);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002107 ret_val = e1000_read_nvm(hw, offset, 1, &nvm_data);
2108 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002109 e_dbg("NVM Read Error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002110 return ret_val;
2111 }
2112 hw->mac.perm_addr[i] = (u8)(nvm_data & 0xFF);
2113 hw->mac.perm_addr[i+1] = (u8)(nvm_data >> 8);
2114 }
2115
2116 /* Flip last bit of mac address if we're on second port */
Bill Hayes93ca1612007-10-31 15:21:52 -07002117 if (!mac_addr_offset && hw->bus.func == E1000_FUNC_1)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002118 hw->mac.perm_addr[5] ^= 1;
2119
2120 for (i = 0; i < ETH_ALEN; i++)
2121 hw->mac.addr[i] = hw->mac.perm_addr[i];
2122
2123 return 0;
2124}
2125
2126/**
2127 * e1000e_validate_nvm_checksum_generic - Validate EEPROM checksum
2128 * @hw: pointer to the HW structure
2129 *
2130 * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
2131 * and then verifies that the sum of the EEPROM is equal to 0xBABA.
2132 **/
2133s32 e1000e_validate_nvm_checksum_generic(struct e1000_hw *hw)
2134{
2135 s32 ret_val;
2136 u16 checksum = 0;
2137 u16 i, nvm_data;
2138
2139 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
2140 ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
2141 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002142 e_dbg("NVM Read Error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002143 return ret_val;
2144 }
2145 checksum += nvm_data;
2146 }
2147
2148 if (checksum != (u16) NVM_SUM) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002149 e_dbg("NVM Checksum Invalid\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002150 return -E1000_ERR_NVM;
2151 }
2152
2153 return 0;
2154}
2155
2156/**
2157 * e1000e_update_nvm_checksum_generic - Update EEPROM checksum
2158 * @hw: pointer to the HW structure
2159 *
2160 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
2161 * up to the checksum. Then calculates the EEPROM checksum and writes the
2162 * value to the EEPROM.
2163 **/
2164s32 e1000e_update_nvm_checksum_generic(struct e1000_hw *hw)
2165{
2166 s32 ret_val;
2167 u16 checksum = 0;
2168 u16 i, nvm_data;
2169
2170 for (i = 0; i < NVM_CHECKSUM_REG; i++) {
2171 ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
2172 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002173 e_dbg("NVM Read Error while updating checksum.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002174 return ret_val;
2175 }
2176 checksum += nvm_data;
2177 }
2178 checksum = (u16) NVM_SUM - checksum;
2179 ret_val = e1000_write_nvm(hw, NVM_CHECKSUM_REG, 1, &checksum);
2180 if (ret_val)
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002181 e_dbg("NVM Write Error while updating checksum.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002182
2183 return ret_val;
2184}
2185
2186/**
2187 * e1000e_reload_nvm - Reloads EEPROM
2188 * @hw: pointer to the HW structure
2189 *
2190 * Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
2191 * extended control register.
2192 **/
2193void e1000e_reload_nvm(struct e1000_hw *hw)
2194{
2195 u32 ctrl_ext;
2196
2197 udelay(10);
2198 ctrl_ext = er32(CTRL_EXT);
2199 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
2200 ew32(CTRL_EXT, ctrl_ext);
2201 e1e_flush();
2202}
2203
2204/**
2205 * e1000_calculate_checksum - Calculate checksum for buffer
2206 * @buffer: pointer to EEPROM
2207 * @length: size of EEPROM to calculate a checksum for
2208 *
2209 * Calculates the checksum for some buffer on a specified length. The
2210 * checksum calculated is returned.
2211 **/
2212static u8 e1000_calculate_checksum(u8 *buffer, u32 length)
2213{
2214 u32 i;
2215 u8 sum = 0;
2216
2217 if (!buffer)
2218 return 0;
2219
2220 for (i = 0; i < length; i++)
2221 sum += buffer[i];
2222
2223 return (u8) (0 - sum);
2224}
2225
2226/**
2227 * e1000_mng_enable_host_if - Checks host interface is enabled
2228 * @hw: pointer to the HW structure
2229 *
2230 * Returns E1000_success upon success, else E1000_ERR_HOST_INTERFACE_COMMAND
2231 *
Auke Kok489815c2008-02-21 15:11:07 -08002232 * This function checks whether the HOST IF is enabled for command operation
Auke Kokbc7f75f2007-09-17 12:30:59 -07002233 * and also checks whether the previous command is completed. It busy waits
2234 * in case of previous command is not completed.
2235 **/
2236static s32 e1000_mng_enable_host_if(struct e1000_hw *hw)
2237{
2238 u32 hicr;
2239 u8 i;
2240
2241 /* Check that the host interface is enabled. */
2242 hicr = er32(HICR);
2243 if ((hicr & E1000_HICR_EN) == 0) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002244 e_dbg("E1000_HOST_EN bit disabled.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002245 return -E1000_ERR_HOST_INTERFACE_COMMAND;
2246 }
2247 /* check the previous command is completed */
2248 for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
2249 hicr = er32(HICR);
2250 if (!(hicr & E1000_HICR_C))
2251 break;
2252 mdelay(1);
2253 }
2254
2255 if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002256 e_dbg("Previous command timeout failed .\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002257 return -E1000_ERR_HOST_INTERFACE_COMMAND;
2258 }
2259
2260 return 0;
2261}
2262
2263/**
Bruce Allan4662e822008-08-26 18:37:06 -07002264 * e1000e_check_mng_mode_generic - check management mode
Auke Kokbc7f75f2007-09-17 12:30:59 -07002265 * @hw: pointer to the HW structure
2266 *
2267 * Reads the firmware semaphore register and returns true (>0) if
2268 * manageability is enabled, else false (0).
2269 **/
Bruce Allan4662e822008-08-26 18:37:06 -07002270bool e1000e_check_mng_mode_generic(struct e1000_hw *hw)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002271{
2272 u32 fwsm = er32(FWSM);
2273
Bruce Allan4662e822008-08-26 18:37:06 -07002274 return (fwsm & E1000_FWSM_MODE_MASK) ==
2275 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002276}
2277
2278/**
Bruce Allanad680762008-03-28 09:15:03 -07002279 * e1000e_enable_tx_pkt_filtering - Enable packet filtering on Tx
Auke Kokbc7f75f2007-09-17 12:30:59 -07002280 * @hw: pointer to the HW structure
2281 *
2282 * Enables packet filtering on transmit packets if manageability is enabled
2283 * and host interface is enabled.
2284 **/
2285bool e1000e_enable_tx_pkt_filtering(struct e1000_hw *hw)
2286{
2287 struct e1000_host_mng_dhcp_cookie *hdr = &hw->mng_cookie;
2288 u32 *buffer = (u32 *)&hw->mng_cookie;
2289 u32 offset;
2290 s32 ret_val, hdr_csum, csum;
2291 u8 i, len;
2292
2293 /* No manageability, no filtering */
2294 if (!e1000e_check_mng_mode(hw)) {
Bruce Allan564ea9b2009-11-20 23:26:44 +00002295 hw->mac.tx_pkt_filtering = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002296 return 0;
2297 }
2298
Bruce Allanad680762008-03-28 09:15:03 -07002299 /*
2300 * If we can't read from the host interface for whatever
Auke Kokbc7f75f2007-09-17 12:30:59 -07002301 * reason, disable filtering.
2302 */
2303 ret_val = e1000_mng_enable_host_if(hw);
2304 if (ret_val != 0) {
Bruce Allan564ea9b2009-11-20 23:26:44 +00002305 hw->mac.tx_pkt_filtering = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002306 return ret_val;
2307 }
2308
2309 /* Read in the header. Length and offset are in dwords. */
2310 len = E1000_MNG_DHCP_COOKIE_LENGTH >> 2;
2311 offset = E1000_MNG_DHCP_COOKIE_OFFSET >> 2;
2312 for (i = 0; i < len; i++)
2313 *(buffer + i) = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset + i);
2314 hdr_csum = hdr->checksum;
2315 hdr->checksum = 0;
2316 csum = e1000_calculate_checksum((u8 *)hdr,
2317 E1000_MNG_DHCP_COOKIE_LENGTH);
Bruce Allanad680762008-03-28 09:15:03 -07002318 /*
2319 * If either the checksums or signature don't match, then
Auke Kokbc7f75f2007-09-17 12:30:59 -07002320 * the cookie area isn't considered valid, in which case we
2321 * take the safe route of assuming Tx filtering is enabled.
2322 */
2323 if ((hdr_csum != csum) || (hdr->signature != E1000_IAMT_SIGNATURE)) {
Bruce Allan564ea9b2009-11-20 23:26:44 +00002324 hw->mac.tx_pkt_filtering = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002325 return 1;
2326 }
2327
2328 /* Cookie area is valid, make the final check for filtering. */
2329 if (!(hdr->status & E1000_MNG_DHCP_COOKIE_STATUS_PARSING)) {
Bruce Allan564ea9b2009-11-20 23:26:44 +00002330 hw->mac.tx_pkt_filtering = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002331 return 0;
2332 }
2333
Bruce Allan564ea9b2009-11-20 23:26:44 +00002334 hw->mac.tx_pkt_filtering = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002335 return 1;
2336}
2337
2338/**
2339 * e1000_mng_write_cmd_header - Writes manageability command header
2340 * @hw: pointer to the HW structure
2341 * @hdr: pointer to the host interface command header
2342 *
2343 * Writes the command header after does the checksum calculation.
2344 **/
2345static s32 e1000_mng_write_cmd_header(struct e1000_hw *hw,
2346 struct e1000_host_mng_command_header *hdr)
2347{
2348 u16 i, length = sizeof(struct e1000_host_mng_command_header);
2349
2350 /* Write the whole command header structure with new checksum. */
2351
2352 hdr->checksum = e1000_calculate_checksum((u8 *)hdr, length);
2353
2354 length >>= 2;
2355 /* Write the relevant command block into the ram area. */
2356 for (i = 0; i < length; i++) {
2357 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, i,
2358 *((u32 *) hdr + i));
2359 e1e_flush();
2360 }
2361
2362 return 0;
2363}
2364
2365/**
2366 * e1000_mng_host_if_write - Writes to the manageability host interface
2367 * @hw: pointer to the HW structure
2368 * @buffer: pointer to the host interface buffer
2369 * @length: size of the buffer
2370 * @offset: location in the buffer to write to
2371 * @sum: sum of the data (not checksum)
2372 *
2373 * This function writes the buffer content at the offset given on the host if.
2374 * It also does alignment considerations to do the writes in most efficient
2375 * way. Also fills up the sum of the buffer in *buffer parameter.
2376 **/
2377static s32 e1000_mng_host_if_write(struct e1000_hw *hw, u8 *buffer,
2378 u16 length, u16 offset, u8 *sum)
2379{
2380 u8 *tmp;
2381 u8 *bufptr = buffer;
2382 u32 data = 0;
2383 u16 remaining, i, j, prev_bytes;
2384
2385 /* sum = only sum of the data and it is not checksum */
2386
2387 if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH)
2388 return -E1000_ERR_PARAM;
2389
2390 tmp = (u8 *)&data;
2391 prev_bytes = offset & 0x3;
2392 offset >>= 2;
2393
2394 if (prev_bytes) {
2395 data = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset);
2396 for (j = prev_bytes; j < sizeof(u32); j++) {
2397 *(tmp + j) = *bufptr++;
2398 *sum += *(tmp + j);
2399 }
2400 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset, data);
2401 length -= j - prev_bytes;
2402 offset++;
2403 }
2404
2405 remaining = length & 0x3;
2406 length -= remaining;
2407
2408 /* Calculate length in DWORDs */
2409 length >>= 2;
2410
Bruce Allanad680762008-03-28 09:15:03 -07002411 /*
2412 * The device driver writes the relevant command block into the
2413 * ram area.
2414 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002415 for (i = 0; i < length; i++) {
2416 for (j = 0; j < sizeof(u32); j++) {
2417 *(tmp + j) = *bufptr++;
2418 *sum += *(tmp + j);
2419 }
2420
2421 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
2422 }
2423 if (remaining) {
2424 for (j = 0; j < sizeof(u32); j++) {
2425 if (j < remaining)
2426 *(tmp + j) = *bufptr++;
2427 else
2428 *(tmp + j) = 0;
2429
2430 *sum += *(tmp + j);
2431 }
2432 E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
2433 }
2434
2435 return 0;
2436}
2437
2438/**
2439 * e1000e_mng_write_dhcp_info - Writes DHCP info to host interface
2440 * @hw: pointer to the HW structure
2441 * @buffer: pointer to the host interface
2442 * @length: size of the buffer
2443 *
2444 * Writes the DHCP information to the host interface.
2445 **/
2446s32 e1000e_mng_write_dhcp_info(struct e1000_hw *hw, u8 *buffer, u16 length)
2447{
2448 struct e1000_host_mng_command_header hdr;
2449 s32 ret_val;
2450 u32 hicr;
2451
2452 hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
2453 hdr.command_length = length;
2454 hdr.reserved1 = 0;
2455 hdr.reserved2 = 0;
2456 hdr.checksum = 0;
2457
2458 /* Enable the host interface */
2459 ret_val = e1000_mng_enable_host_if(hw);
2460 if (ret_val)
2461 return ret_val;
2462
2463 /* Populate the host interface with the contents of "buffer". */
2464 ret_val = e1000_mng_host_if_write(hw, buffer, length,
2465 sizeof(hdr), &(hdr.checksum));
2466 if (ret_val)
2467 return ret_val;
2468
2469 /* Write the manageability command header */
2470 ret_val = e1000_mng_write_cmd_header(hw, &hdr);
2471 if (ret_val)
2472 return ret_val;
2473
2474 /* Tell the ARC a new command is pending. */
2475 hicr = er32(HICR);
2476 ew32(HICR, hicr | E1000_HICR_C);
2477
2478 return 0;
2479}
2480
2481/**
2482 * e1000e_enable_mng_pass_thru - Enable processing of ARP's
2483 * @hw: pointer to the HW structure
2484 *
2485 * Verifies the hardware needs to allow ARPs to be processed by the host.
2486 **/
2487bool e1000e_enable_mng_pass_thru(struct e1000_hw *hw)
2488{
2489 u32 manc;
2490 u32 fwsm, factps;
Bruce Allan564ea9b2009-11-20 23:26:44 +00002491 bool ret_val = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002492
2493 manc = er32(MANC);
2494
2495 if (!(manc & E1000_MANC_RCV_TCO_EN) ||
2496 !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
2497 return ret_val;
2498
2499 if (hw->mac.arc_subsystem_valid) {
2500 fwsm = er32(FWSM);
2501 factps = er32(FACTPS);
2502
2503 if (!(factps & E1000_FACTPS_MNGCG) &&
2504 ((fwsm & E1000_FWSM_MODE_MASK) ==
2505 (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT))) {
Bruce Allan564ea9b2009-11-20 23:26:44 +00002506 ret_val = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002507 return ret_val;
2508 }
2509 } else {
2510 if ((manc & E1000_MANC_SMBUS_EN) &&
2511 !(manc & E1000_MANC_ASF_EN)) {
Bruce Allan564ea9b2009-11-20 23:26:44 +00002512 ret_val = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002513 return ret_val;
2514 }
2515 }
2516
2517 return ret_val;
2518}
2519
Jeff Kirsher69e3fd82008-04-02 13:48:18 -07002520s32 e1000e_read_pba_num(struct e1000_hw *hw, u32 *pba_num)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002521{
2522 s32 ret_val;
2523 u16 nvm_data;
2524
2525 ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
2526 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002527 e_dbg("NVM Read Error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002528 return ret_val;
2529 }
Jeff Kirsher69e3fd82008-04-02 13:48:18 -07002530 *pba_num = (u32)(nvm_data << 16);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002531
2532 ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_1, 1, &nvm_data);
2533 if (ret_val) {
Bruce Allan3bb99fe2009-11-20 23:25:07 +00002534 e_dbg("NVM Read Error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002535 return ret_val;
2536 }
Jeff Kirsher69e3fd82008-04-02 13:48:18 -07002537 *pba_num |= nvm_data;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002538
2539 return 0;
2540}